@@ -897,8 +897,18 @@ internal struct TextFormatScanner {
897
897
898
898
// If the next token is the identifier "nan", return true.
899
899
private mutating func skipOptionalNaN( ) -> Bool {
900
- return skipOptionalKeyword ( bytes:
901
- [ asciiLowerN, asciiLowerA, asciiLowerN] )
900
+ let start = p
901
+ // "-nan" doesn't mean anything, but upstream handles it, so skip
902
+ // over any leading minus when checking for "nan".
903
+ if p != end && p [ 0 ] == asciiMinus {
904
+ p += 1
905
+ }
906
+ if skipOptionalKeyword ( bytes: [ asciiLowerN, asciiLowerA, asciiLowerN] ) {
907
+ return true
908
+ } else {
909
+ p = start // It wasn't "nan", rewind incase we skipped a minus sign.
910
+ return false
911
+ }
902
912
}
903
913
904
914
// If the next token is a recognized spelling of "infinity",
@@ -1243,40 +1253,40 @@ internal struct TextFormatScanner {
1243
1253
1244
1254
/// Helper to see if this could be the start of a hex or octal number so unknown field
1245
1255
/// value parsing can decide how to parse/validate.
1246
- private func isNextNumberFloatingPoint ( ) -> Bool {
1247
- // NOTE: If we run out of characters can can 't tell, say it isn 't and let
1248
- // the other code error handle.
1256
+ private func mustParseNumberAsDecimal ( ) -> Bool {
1257
+ // NOTE: If we run out of characters/ can't tell; then just say it doesn 't have
1258
+ // to be decimal, and let the other code error handle it .
1249
1259
var scan = p
1250
1260
var c = scan [ 0 ]
1251
1261
1252
- // Floats for decimals can have leading '-'
1262
+ // Floats or decimals can have leading '-'
1253
1263
if c == asciiMinus {
1254
1264
scan += 1
1255
1265
if scan == end { return false }
1256
1266
c = scan [ 0 ]
1257
1267
}
1258
1268
1259
1269
if c == asciiPeriod {
1260
- return true // "(-)." : clearly a float
1270
+ return false // "(-)." : clearly a float
1261
1271
}
1262
1272
1263
1273
if c == asciiZero {
1264
1274
scan += 1
1265
- if scan == end { return false } // "(-)0[end]" : call it not a float
1275
+ if scan == end { return true } // "(-)0[end]" : parse it as decimal
1266
1276
c = scan [ 0 ]
1267
- if c == asciiLowerX || // "(-)0x" : hex - not a float
1268
- ( c >= asciiZero && c <= asciiSeven) { // "(-)0[0-7]" : octal - not a float
1269
- return false
1277
+ if c == asciiLowerX || // "(-)0x" : hex - must parse as decimal
1278
+ ( c >= asciiZero && c <= asciiSeven) { // "(-)0[0-7]" : octal - must parse as decimal
1279
+ return true
1270
1280
}
1271
1281
if c == asciiPeriod {
1272
- return true // "(-)0." : clearly a float
1282
+ return false // "(-)0." : clearly a float
1273
1283
}
1274
1284
}
1275
1285
1276
1286
// At this point, it doesn't realy matter what comes next. We'll call it a floating
1277
1287
// point value since even if it was a decimal, it might be too large for a UInt64 but
1278
1288
// would still be valid for a float/double field.
1279
- return true
1289
+ return false
1280
1290
}
1281
1291
1282
1292
private mutating func skipUnknownPrimativeFieldValue( ) throws {
@@ -1311,24 +1321,22 @@ internal struct TextFormatScanner {
1311
1321
}
1312
1322
}
1313
1323
1314
- // This will also cover "true", "false" for booleans, "nan" for floats.
1324
+ // NOTE: This will also cover "true", "false" for booleans, "nan"/"inf " for floats.
1315
1325
if let _ = try nextOptionalEnumName ( ) {
1316
1326
skipWhitespace ( ) // `nextOptionalEnumName()` doesn't skip trailing whitespace
1317
1327
return
1318
1328
}
1319
- // The above will handing "inf", but this is needed for "-inf".
1320
- if let _ = skipOptionalInfinity ( ) {
1321
- return
1322
- }
1323
1329
1324
- if isNextNumberFloatingPoint ( ) {
1325
- let _ = try nextDouble ( )
1326
- } else {
1330
+ // NOTE: We don't need to special case "-nan"/"-inf", as they won't be forced
1331
+ // to parse as decimal, and ` nextDouble()` already supports them.
1332
+ if mustParseNumberAsDecimal ( ) {
1327
1333
if c == asciiMinus {
1328
1334
let _ = try nextSInt ( )
1329
1335
} else {
1330
1336
let _ = try nextUInt ( )
1331
1337
}
1338
+ } else {
1339
+ let _ = try nextDouble ( )
1332
1340
}
1333
1341
}
1334
1342
0 commit comments