Fixes #6: implement \uXXXX escaping
This commit is contained in:
@@ -6,6 +6,7 @@ package toml
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -70,6 +71,11 @@ func isDigit(r rune) bool {
|
|||||||
return unicode.IsNumber(r)
|
return unicode.IsNumber(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isHexDigit(r rune) bool {
|
||||||
|
return isDigit(r) ||
|
||||||
|
r == 'A' || r == 'B' || r == 'C' || r == 'D' || r == 'E' || r == 'F'
|
||||||
|
}
|
||||||
|
|
||||||
// Define lexer
|
// Define lexer
|
||||||
type lexer struct {
|
type lexer struct {
|
||||||
input string
|
input string
|
||||||
@@ -323,6 +329,23 @@ func lexString(l *lexer) stateFn {
|
|||||||
} else if l.follow("\\\\") {
|
} else if l.follow("\\\\") {
|
||||||
l.pos += 1
|
l.pos += 1
|
||||||
growing_string += "\\"
|
growing_string += "\\"
|
||||||
|
} else if l.follow("\\u") {
|
||||||
|
l.pos += 2
|
||||||
|
code := ""
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
c := l.peek()
|
||||||
|
l.pos += 1
|
||||||
|
if !isHexDigit(c) {
|
||||||
|
return l.errorf("unfinished unicode escape")
|
||||||
|
}
|
||||||
|
code = code + string(c)
|
||||||
|
}
|
||||||
|
l.pos -= 1
|
||||||
|
intcode, err := strconv.ParseInt(code, 16, 32)
|
||||||
|
if err != nil {
|
||||||
|
return l.errorf("invalid unicode escape: \\u" + code)
|
||||||
|
}
|
||||||
|
growing_string += string(rune(intcode))
|
||||||
} else {
|
} else {
|
||||||
growing_string += string(l.peek())
|
growing_string += string(l.peek())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -300,3 +300,12 @@ func TestMultiline(t *testing.T) {
|
|||||||
token{tokenEOF, ""},
|
token{tokenEOF, ""},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKeyEqualStringUnicodeEscape(t *testing.T) {
|
||||||
|
testFlow(t, "foo = \"hello \\u2665\"", []token{
|
||||||
|
token{tokenKey, "foo"},
|
||||||
|
token{tokenEqual, "="},
|
||||||
|
token{tokenString, "hello ♥"},
|
||||||
|
token{tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user