File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed
Sources/FoundationEssentials/Decimal
Tests/FoundationEssentialsTests Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -345,6 +345,11 @@ extension Decimal {
345345 return nil
346346 }
347347 }
348+ if index == stringView. startIndex {
349+ // If we weren't able to process any character
350+ // the entire string isn't a valid decimal
351+ return nil
352+ }
348353 result. compact ( )
349354 // if we get to this point, and have NaN,
350355 // then the input string was probably "-0"
Original file line number Diff line number Diff line change @@ -258,4 +258,14 @@ final class DecimalTests : XCTestCase {
258258 XCTAssertNotNil ( decimal)
259259 XCTAssertEqual ( decimal!. description, " 3.14 " )
260260 }
261+
262+ func testStringNoMatch( ) {
263+ // This test makes sure Decimal returns nil
264+ // if the does not start with a number
265+ var notDecimal = Decimal ( string: " A Flamingo's head has to be upside down when it eats. " )
266+ XCTAssertNil ( notDecimal)
267+ // Same if the number does not appear at the beginning
268+ notDecimal = Decimal ( string: " Jump 22 Street " )
269+ XCTAssertNil ( notDecimal)
270+ }
261271}
You can’t perform that action at this time.
0 commit comments