@@ -355,7 +355,7 @@ extension Decimal {
355
355
result = product
356
356
}
357
357
// Get the decimal point
358
- if index != utf8View. endIndex && stringViewContainsDecimalSeparator ( at: index) {
358
+ if index < utf8View. endIndex && stringViewContainsDecimalSeparator ( at: index) {
359
359
utf8View. formIndex ( & index, offsetBy: decimalSeparator. count)
360
360
// Continue to build the mantissa
361
361
while index != utf8View. endIndex,
@@ -381,8 +381,21 @@ extension Decimal {
381
381
}
382
382
}
383
383
// Get the exponent if any
384
- if index != utf8View. endIndex && ( utf8View [ index] == UInt8 . _E || utf8View [ index] == UInt8 . _e) {
384
+ if index < utf8View. endIndex && ( utf8View [ index] == UInt8 . _E || utf8View [ index] == UInt8 . _e) {
385
385
utf8View. formIndex ( after: & index)
386
+ // If there is no content after e, the string is invalid
387
+ guard index != utf8View. endIndex else {
388
+ // Normally we should return .parseFailure
389
+ // However, NSDecimal historically parses any
390
+ // - Invalid strings starting with `e` as 0
391
+ // - "en" -> 0
392
+ // - "e" -> 0
393
+ // - Strings ending with `e` but nothing after as valid
394
+ // - "1234e" -> 1234
395
+ // So let's keep that behavior here as well
396
+ let processedLength = utf8View. distance ( from: utf8View. startIndex, to: index)
397
+ return . success( result, processedLength: processedLength)
398
+ }
386
399
var exponentIsNegative = false
387
400
var exponent = 0
388
401
// Get the exponent sign
0 commit comments