Skip to content

Commit 2bbade0

Browse files
committed
Support 'F' as a suffix for floating point in TextFormat.
Per https://protobuf.dev/reference/protobuf/textformat-spec/#numeric, a `FLOAT` value can be appended with 'f' or 'F'. So the support for the uppercase one was missing.
1 parent 8d39a0b commit 2bbade0

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Sources/SwiftProtobuf/TextFormatScanner.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,9 @@ internal struct TextFormatScanner {
839839
asciiLowerE,
840840
asciiUpperE: // 0...9, ., +, -, e, E
841841
p += 1
842-
case asciiLowerF: // f
843-
// proto1 allowed floats to be suffixed with 'f'
842+
case asciiLowerF, asciiUpperF: // f or F
844843
let d = doubleParser.utf8ToDouble(bytes: UnsafeRawBufferPointer(start: start, count: p - start))
845-
// Just skip the 'f'
844+
// Just skip the 'f'/'F'
846845
p += 1
847846
skipWhitespace()
848847
return d

Tests/SwiftProtobufTests/Test_TextFormat_proto3.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ final class Test_TextFormat_proto3: XCTestCase, PBTestHelpers {
315315
(o: MessageTestType) in
316316
return o.optionalFloat == 1.0
317317
}
318+
assertTextFormatDecodeSucceeds("optional_float: 1.0F\n") {
319+
(o: MessageTestType) in
320+
return o.optionalFloat == 1.0
321+
}
318322
assertTextFormatDecodeSucceeds("optional_float: 11\n") {
319323
(o: MessageTestType) in
320324
return o.optionalFloat == 11.0
@@ -323,6 +327,10 @@ final class Test_TextFormat_proto3: XCTestCase, PBTestHelpers {
323327
(o: MessageTestType) in
324328
return o.optionalFloat == 11.0
325329
}
330+
assertTextFormatDecodeSucceeds("optional_float: 11F\n") {
331+
(o: MessageTestType) in
332+
return o.optionalFloat == 11.0
333+
}
326334
assertTextFormatDecodeSucceeds("optional_float: 0\n") {
327335
(o: MessageTestType) in
328336
return o.optionalFloat == 0.0
@@ -331,6 +339,10 @@ final class Test_TextFormat_proto3: XCTestCase, PBTestHelpers {
331339
(o: MessageTestType) in
332340
return o.optionalFloat == 0.0
333341
}
342+
assertTextFormatDecodeSucceeds("optional_float: 0F\n") {
343+
(o: MessageTestType) in
344+
return o.optionalFloat == 0.0
345+
}
334346
assertTextFormatEncode("optional_float: inf\n") {(o: inout MessageTestType) in o.optionalFloat = Float.infinity}
335347
assertTextFormatEncode("optional_float: -inf\n") {(o: inout MessageTestType) in o.optionalFloat = -Float.infinity}
336348

@@ -454,12 +466,30 @@ final class Test_TextFormat_proto3: XCTestCase, PBTestHelpers {
454466
assertTextFormatDecodeSucceeds("optional_double: 1.0\n") {(o: MessageTestType) in
455467
return o.optionalDouble == 1.0
456468
}
469+
assertTextFormatDecodeSucceeds("optional_double: 1.0f\n") {(o: MessageTestType) in
470+
return o.optionalDouble == 1.0
471+
}
472+
assertTextFormatDecodeSucceeds("optional_double: 1.0F\n") {(o: MessageTestType) in
473+
return o.optionalDouble == 1.0
474+
}
457475
assertTextFormatDecodeSucceeds("optional_double: 1\n") {(o: MessageTestType) in
458476
return o.optionalDouble == 1.0
459477
}
478+
assertTextFormatDecodeSucceeds("optional_double: 1f\n") {(o: MessageTestType) in
479+
return o.optionalDouble == 1.0
480+
}
481+
assertTextFormatDecodeSucceeds("optional_double: 1F\n") {(o: MessageTestType) in
482+
return o.optionalDouble == 1.0
483+
}
460484
assertTextFormatDecodeSucceeds("optional_double: 0\n") {(o: MessageTestType) in
461485
return o.optionalDouble == 0.0
462486
}
487+
assertTextFormatDecodeSucceeds("optional_double: 0f\n") {(o: MessageTestType) in
488+
return o.optionalDouble == 0.0
489+
}
490+
assertTextFormatDecodeSucceeds("optional_double: 0F\n") {(o: MessageTestType) in
491+
return o.optionalDouble == 0.0
492+
}
463493
assertTextFormatDecodeSucceeds("12: 1.0\n") {(o: MessageTestType) in
464494
return o.optionalDouble == 1.0
465495
}

0 commit comments

Comments
 (0)