Skip to content

Commit 84a4f87

Browse files
committed
refactor to use collateString collate2String #17.
1 parent d6fdbef commit 84a4f87

7 files changed

+34
-40
lines changed

cbor_collate.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,7 @@ func collateCborT2(buf, out []byte, config *Config) (int, int) {
186186

187187
func collateCborT3(buf, out []byte, config *Config) (int, int) {
188188
ln, m := cborItemLength(buf)
189-
if config.doMissing && MissingLiteral.Equal(bytes2str(buf[m:m+ln])) {
190-
out[0], out[1] = TypeMissing, Terminator
191-
return m + ln, 2
192-
}
193-
n := 0
194-
out[n] = TypeString
195-
n++
196-
n += suffixEncodeString(buf[m:m+ln], out[n:])
197-
out[n] = Terminator
198-
n++
189+
n := collateString(bytes2str(buf[m:m+ln]), out, config)
199190
return m + ln, n
200191
}
201192

collate_cbor.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ func collate2cbor(code, out []byte, config *Config) (int, int) {
4343
return m + x, n
4444

4545
case TypeString:
46+
var x int
47+
4648
poolstr := config.pools.stringPool.Get()
4749
defer config.pools.stringPool.Put(poolstr)
4850
scratch := poolstr.([]byte)
49-
x, y := suffixDecodeString(code[m:], scratch)
50-
n += valtext2cbor(bytes2str(scratch[:y]), out[n:])
51+
scratch, x = collate2String(code[m:], scratch[:])
52+
n += valtext2cbor(bytes2str(scratch), out[n:])
5153
return m + x, n
5254

5355
case TypeBinary:

collate_json.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@ func collate2json(code []byte, text []byte, config *Config) (int, int) {
3232
return n + x, m + y
3333

3434
case TypeString:
35+
var x int
36+
3537
scratchi := config.pools.stringPool.Get()
3638
scratch := scratchi.([]byte)
3739
defer config.pools.stringPool.Put(scratchi)
3840

39-
x, y := suffixDecodeString(code[n:], scratch[:])
41+
scratch, x = collate2String(code[n:], scratch[:])
4042

4143
if config.strict {
4244
config.buf.Reset()
43-
if err := config.enc.Encode(bytes2str(scratch[:y])); err != nil {
45+
if err := config.enc.Encode(bytes2str(scratch)); err != nil {
4446
panic(err)
4547
}
4648
s := config.buf.Bytes()
4749
m += copy(text[m:], s[:len(s)-1]) // -1 to strip \n
4850
return n + x, m
4951
}
50-
remtxt, err := encodeString(scratch[:y], text[m:m])
52+
remtxt, err := encodeString(scratch, text[m:m])
5153
if err != nil {
5254
panic(err)
5355
}

collate_value.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func collate2gson(code []byte, config *Config) (interface{}, int) {
3939

4040
case TypeString:
4141
s := make([]byte, encodedStringSize(code[n:]))
42-
x, y := suffixDecodeString(code[n:], s)
43-
return bytes2str(s[:y]), n + x
42+
s, x := collate2String(code[n:], s)
43+
return bytes2str(s), n + x
4444

4545
case TypeBinary:
4646
m := getDatum(code[n:])

json_collate.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,8 @@ func json2collate(txt string, code []byte, config *Config) (string, int) {
5151
defer config.pools.stringPool.Put(scratchi)
5252

5353
txt, p := scanString(txt, scratch)
54-
if config.doMissing && MissingLiteral.Equal(bytes2str(scratch[:p])) {
55-
code[n], code[n+1] = TypeMissing, Terminator
56-
return txt, n + 2
57-
}
58-
code[n] = TypeString
59-
n++
60-
n += suffixEncodeString(scratch[:p], code[n:])
61-
code[n] = Terminator
62-
n++
54+
str := bytes2str(scratch[:p])
55+
n += collateString(str, code[n:], config)
6356
return txt, n
6457

6558
case '[':
@@ -123,7 +116,8 @@ func json2collate(txt string, code []byte, config *Config) (string, int) {
123116
for {
124117
// NOTE: empty string is also a valid key
125118
txt, x = scanString(txt, altcode[p:])
126-
if txt = skipWS(txt, config.ws); len(txt) == 0 || txt[0] != ':' {
119+
txt = skipWS(txt, config.ws)
120+
if len(txt) == 0 || txt[0] != ':' {
127121
panic("collate scanner expectedColon")
128122
}
129123
key := bytes2str(altcode[p : p+x])

util.go

+18
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ func collateJsonNumber(value string, code []byte, config *Config) int {
181181
return collateFloat(bs, code)
182182
}
183183

184+
func collateString(str string, code []byte, config *Config) (n int) {
185+
if config.doMissing && MissingLiteral.Equal(str) {
186+
code[0], code[1] = TypeMissing, Terminator
187+
return 2
188+
}
189+
code[n] = TypeString
190+
n++
191+
n += suffixEncodeString(str2bytes(str), code[n:])
192+
code[n] = Terminator
193+
n++
194+
return n
195+
}
196+
197+
func collate2String(code []byte, str []byte) ([]byte, int) {
198+
x, y := suffixDecodeString(code, str)
199+
return str[:y], x
200+
}
201+
184202
func collated2Number(code []byte, nk NumberKind) (uint64, int64, float64, int) {
185203
var mantissa, scratch [64]byte
186204
_, y := collated2Float(code, scratch[:])

value_collate.go

-13
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,3 @@ func collateLength(length int, code []byte) (n int) {
144144
n++
145145
return n
146146
}
147-
148-
func collateString(str string, code []byte, config *Config) (n int) {
149-
if config.doMissing && MissingLiteral.Equal(str) {
150-
code[0], code[1] = TypeMissing, Terminator
151-
return 2
152-
}
153-
code[n] = TypeString
154-
n++
155-
n += suffixEncodeString(str2bytes(str), code[n:])
156-
code[n] = Terminator
157-
n++
158-
return n
159-
}

0 commit comments

Comments
 (0)