Skip to content

Commit

Permalink
refactor: tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Oct 28, 2021
1 parent d724be8 commit d3ba3d6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage:
status:
patch: false
project:
default:
threshold: 0.5%
6 changes: 3 additions & 3 deletions iter_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
floatDigits[' '] = endOfNumber
floatDigits['\t'] = endOfNumber
floatDigits['\n'] = endOfNumber
floatDigits['.'] = dotInNumber
floatDigits[tDot] = dotInNumber
}

// ReadBigFloat read big.Float
Expand Down Expand Up @@ -126,7 +126,7 @@ NonDecimalLoop:
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
}
// chars after dot
if c == '.' {
if c == tDot {
i++
decimalPlaces := 0
if i == it.tail {
Expand Down Expand Up @@ -322,7 +322,7 @@ func validateFloat(str string) string {
if str[0] == '-' {
return "-- is not valid"
}
dotPos := strings.IndexByte(str, '.')
dotPos := strings.IndexByte(str, tDot)
if dotPos != -1 {
if dotPos == len(str)-1 {
return "dot can not be last character"
Expand Down
2 changes: 1 addition & 1 deletion iter_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (it *Iterator) readUint64(c byte) (ret uint64) {
}

func (it *Iterator) assertInteger() {
if it.head < it.tail && it.buf[it.head] == '.' {
if it.head < it.tail && it.buf[it.head] == tDot {
it.ReportError("assertInteger", "can not decode float as int")
}
}
2 changes: 1 addition & 1 deletion iter_skip_strict.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (it *Iterator) trySkipNumber() bool {
c := it.buf[i]
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
case '.':
case tDot:
if dotFound {
it.ReportError("validateNumber", `more than one dot found in number`)
return true // already failed
Expand Down
28 changes: 12 additions & 16 deletions stream_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (
"strconv"
)

var pow10 []uint64

func init() {
pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
}
var pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}

// WriteFloat32 write float32 to stream
func (s *Stream) WriteFloat32(val float32) {
Expand All @@ -36,7 +32,7 @@ func (s *Stream) WriteFloat32Lossy(val float32) {
return
}
if val < 0 {
s.writeByte('-')
s.writeByte(tMinus)
val = -val
}
if val > 0x4ffffff {
Expand All @@ -51,12 +47,12 @@ func (s *Stream) WriteFloat32Lossy(val float32) {
if fval == 0 {
return
}
s.writeByte('.')
s.writeByte(tDot)
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
s.writeByte('0')
s.writeByte(tZero)
}
s.WriteUint64(fval)
for s.buf[len(s.buf)-1] == '0' {
for s.buf[len(s.buf)-1] == tZero {
s.buf = s.buf[:len(s.buf)-1]
}
}
Expand All @@ -83,12 +79,12 @@ func (s *Stream) WriteFloat64(val float64) {

// Ensure that we are still float.
for _, c := range s.buf[start:] {
if c == '.' {
if c == tDot {
return
}
}
s.buf = appendRune(s.buf, '.')
s.buf = appendRune(s.buf, '0')
s.buf = appendRune(s.buf, tDot)
s.buf = appendRune(s.buf, tZero)
}

// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
Expand All @@ -98,7 +94,7 @@ func (s *Stream) WriteFloat64Lossy(val float64) {
return
}
if val < 0 {
s.writeByte('-')
s.writeByte(tMinus)
val = -val
}
if val > 0x4ffffff {
Expand All @@ -113,12 +109,12 @@ func (s *Stream) WriteFloat64Lossy(val float64) {
if fval == 0 {
return
}
s.writeByte('.')
s.writeByte(tDot)
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
s.writeByte('0')
s.writeByte(tZero)
}
s.WriteUint64(fval)
for s.buf[len(s.buf)-1] == '0' {
for s.buf[len(s.buf)-1] == tZero {
s.buf = s.buf[:len(s.buf)-1]
}
}
4 changes: 2 additions & 2 deletions stream_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var htmlSafeSet = [utf8.RuneSelf]bool{
'+': true,
',': true,
'-': true,
'.': true,
tDot: true,
'/': true,
'0': true,
'1': true,
Expand Down Expand Up @@ -131,7 +131,7 @@ var safeSet = [utf8.RuneSelf]bool{
'+': true,
',': true,
'-': true,
'.': true,
tDot: true,
'/': true,
'0': true,
'1': true,
Expand Down
13 changes: 13 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jir

// Common tokens.
const (
tDot = '.'
tMinus = '-'
tZero = '0'
tStart = '{'
tEnd = '}'

tArrStart = '['
tArrEnd = ']'
)

0 comments on commit d3ba3d6

Please sign in to comment.