Skip to content

Commit

Permalink
Merge pull request #26 from goccy/feature/fix-multi-bytes
Browse files Browse the repository at this point in the history
Support multi bytes encoding/decoding
  • Loading branch information
goccy authored Nov 7, 2019
2 parents e7aa107 + a5c9b0d commit 0863153
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,12 @@ func TestDecoder(t *testing.T) {
},
},
},

// Multi bytes
{
"v: あいうえお",
map[string]string{"v": "あいうえお"},
},
}
for _, test := range tests {
buf := bytes.NewBufferString(test.source)
Expand Down
6 changes: 6 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ func TestEncoder(t *testing.T) {
} "a,flow"
}{struct{ B, D string }{"c", "e"}},
},

// Multi bytes
{
"v: あいうえお\n",
map[string]string{"v": "あいうえお"},
},
}
for _, test := range tests {
var buf bytes.Buffer
Expand Down
15 changes: 8 additions & 7 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Context struct {
idx int
size int
src string
src []rune
buf []rune
obuf []rune
tokens token.Tokens
Expand All @@ -20,7 +20,8 @@ type Context struct {
literalOpt string
}

func newContext(src string) *Context {
func newContext(s string) *Context {
src := []rune(s)
return &Context{
idx: 0,
size: len(src),
Expand Down Expand Up @@ -75,31 +76,31 @@ func (c *Context) next() bool {
}

func (c *Context) source(s, e int) string {
return c.src[s:e]
return string(c.src[s:e])
}

func (c *Context) previousChar() rune {
if c.idx > 0 {
return rune(c.src[c.idx-1])
return c.src[c.idx-1]
}
return rune(0)
}

func (c *Context) currentChar() rune {
return rune(c.src[c.idx])
return c.src[c.idx]
}

func (c *Context) nextChar() rune {
if c.size > c.idx+1 {
return rune(c.src[c.idx+1])
return c.src[c.idx+1]
}
return rune(0)
}

func (c *Context) repeatNum(r rune) int {
cnt := 0
for i := c.idx; i < c.size; i++ {
if rune(c.src[i]) == r {
if c.src[i] == r {
cnt++
} else {
break
Expand Down

0 comments on commit 0863153

Please sign in to comment.