Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
d528e1af
Commit
d528e1af
authored
Jun 13, 2024
by
Michael Yang
Browse files
fix utf16 for multibyte runes
parent
cd234ce2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
29 deletions
+6
-29
parser/parser.go
parser/parser.go
+6
-29
No files found.
parser/parser.go
View file @
d528e1af
...
@@ -8,7 +8,9 @@ import (
...
@@ -8,7 +8,9 @@ import (
"io"
"io"
"strconv"
"strconv"
"strings"
"strings"
"unicode"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
)
type
File
struct
{
type
File
struct
{
...
@@ -69,14 +71,11 @@ func ParseFile(r io.Reader) (*File, error) {
...
@@ -69,14 +71,11 @@ func ParseFile(r io.Reader) (*File, error) {
var
b
bytes
.
Buffer
var
b
bytes
.
Buffer
var
role
string
var
role
string
var
lineCount
int
var
linePos
int
var
utf16
bool
var
f
File
var
f
File
br
:=
bufio
.
NewReader
(
r
)
tr
:=
unicode
.
BOMOverride
(
unicode
.
UTF8
.
NewDecoder
())
br
:=
bufio
.
NewReader
(
transform
.
NewReader
(
r
,
tr
))
for
{
for
{
r
,
_
,
err
:=
br
.
ReadRune
()
r
,
_
,
err
:=
br
.
ReadRune
()
if
errors
.
Is
(
err
,
io
.
EOF
)
{
if
errors
.
Is
(
err
,
io
.
EOF
)
{
...
@@ -85,17 +84,6 @@ func ParseFile(r io.Reader) (*File, error) {
...
@@ -85,17 +84,6 @@ func ParseFile(r io.Reader) (*File, error) {
return
nil
,
err
return
nil
,
err
}
}
// the utf16 byte order mark will be read as "unreadable" by ReadRune()
if
isUnreadable
(
r
)
&&
lineCount
==
0
&&
linePos
==
0
{
utf16
=
true
continue
}
// skip the second byte if we're reading utf16
if
utf16
&&
r
==
0
{
continue
}
next
,
r
,
err
:=
parseRuneForState
(
r
,
curr
)
next
,
r
,
err
:=
parseRuneForState
(
r
,
curr
)
if
errors
.
Is
(
err
,
io
.
ErrUnexpectedEOF
)
{
if
errors
.
Is
(
err
,
io
.
ErrUnexpectedEOF
)
{
return
nil
,
fmt
.
Errorf
(
"%w: %s"
,
err
,
b
.
String
())
return
nil
,
fmt
.
Errorf
(
"%w: %s"
,
err
,
b
.
String
())
...
@@ -103,13 +91,6 @@ func ParseFile(r io.Reader) (*File, error) {
...
@@ -103,13 +91,6 @@ func ParseFile(r io.Reader) (*File, error) {
return
nil
,
err
return
nil
,
err
}
}
if
isNewline
(
r
)
{
lineCount
++
linePos
=
0
}
else
{
linePos
++
}
// process the state transition, some transitions need to be intercepted and redirected
// process the state transition, some transitions need to be intercepted and redirected
if
next
!=
curr
{
if
next
!=
curr
{
switch
curr
{
switch
curr
{
...
@@ -309,10 +290,6 @@ func isNewline(r rune) bool {
...
@@ -309,10 +290,6 @@ func isNewline(r rune) bool {
return
r
==
'\r'
||
r
==
'\n'
return
r
==
'\r'
||
r
==
'\n'
}
}
func
isUnreadable
(
r
rune
)
bool
{
return
r
==
unicode
.
ReplacementChar
}
func
isValidMessageRole
(
role
string
)
bool
{
func
isValidMessageRole
(
role
string
)
bool
{
return
role
==
"system"
||
role
==
"user"
||
role
==
"assistant"
return
role
==
"system"
||
role
==
"user"
||
role
==
"assistant"
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment