@@ -15,17 +15,12 @@ func (d *Decoder) parseNumberValue() (Token, bool) {
15
15
if num .neg {
16
16
numAttrs |= isNegative
17
17
}
18
- strSize := num .size
19
- last := num .size - 1
20
- if num .kind == numFloat && (d .in [last ] == 'f' || d .in [last ] == 'F' ) {
21
- strSize = last
22
- }
23
18
tok := Token {
24
19
kind : Scalar ,
25
20
attrs : numberValue ,
26
21
pos : len (d .orig ) - len (d .in ),
27
22
raw : d .in [:num .size ],
28
- str : string (d .in [: strSize ] ),
23
+ str : num . string (d .in ),
29
24
numAttrs : numAttrs ,
30
25
}
31
26
d .consume (num .size )
@@ -46,6 +41,27 @@ type number struct {
46
41
kind uint8
47
42
neg bool
48
43
size int
44
+ // if neg, this is the length of whitespace and comments between
45
+ // the minus sign and the rest fo the number literal
46
+ sep int
47
+ }
48
+
49
+ func (num number ) string (data []byte ) string {
50
+ strSize := num .size
51
+ last := num .size - 1
52
+ if num .kind == numFloat && (data [last ] == 'f' || data [last ] == 'F' ) {
53
+ strSize = last
54
+ }
55
+ if num .neg && num .sep > 0 {
56
+ // strip whitespace/comments between negative sign and the rest
57
+ strLen := strSize - num .sep
58
+ str := make ([]byte , strLen )
59
+ str [0 ] = data [0 ]
60
+ copy (str [1 :], data [num .sep + 1 :strSize ])
61
+ return string (str )
62
+ }
63
+ return string (data [:strSize ])
64
+
49
65
}
50
66
51
67
// parseNumber constructs a number object from given input. It allows for the
@@ -67,19 +83,22 @@ func parseNumber(input []byte) number {
67
83
}
68
84
69
85
// Optional -
86
+ var sep int
70
87
if s [0 ] == '-' {
71
88
neg = true
72
89
s = s [1 :]
73
90
size ++
74
91
if len (s ) == 0 {
75
92
return number {}
76
93
}
94
+ // Consume any whitespace or comments between the
95
+ // negative sign and the rest of the number
96
+ lenBefore := len (s )
97
+ s = consume (s , 0 )
98
+ sep = lenBefore - len (s )
99
+ size += sep
77
100
}
78
101
79
- // C++ allows for whitespace and comments in between the negative sign and
80
- // the rest of the number. This logic currently does not but is consistent
81
- // with v1.
82
-
83
102
switch {
84
103
case s [0 ] == '0' :
85
104
if len (s ) > 1 {
@@ -116,7 +135,7 @@ func parseNumber(input []byte) number {
116
135
if len (s ) > 0 && ! isDelim (s [0 ]) {
117
136
return number {}
118
137
}
119
- return number {kind : kind , neg : neg , size : size }
138
+ return number {kind : kind , neg : neg , size : size , sep : sep }
120
139
}
121
140
}
122
141
s = s [1 :]
@@ -188,5 +207,5 @@ func parseNumber(input []byte) number {
188
207
return number {}
189
208
}
190
209
191
- return number {kind : kind , neg : neg , size : size }
210
+ return number {kind : kind , neg : neg , size : size , sep : sep }
192
211
}
0 commit comments