Skip to content

Commit

Permalink
Fix Float.parse benchmark regression (#11402)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryTravis authored Oct 29, 2024
1 parent 74220f2 commit 442123b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,20 @@ type Float
Float.parse "(123,456,789.654)" format="###,###.##;(###,###.##)"
# => -123456789.654
parse : Text -> Locale | Nothing -> Float ! Number_Parse_Error ! Illegal_Argument
parse text locale:Locale=Locale.default format:Text="" -> Float ! Number_Parse_Error ! Illegal_Argument =
Illegal_Argument.handle_java_exception <|
# `getInstance` returns `DecimalFormat` or a subclass of `DecimalFormat`.
decimal_format = NumberFormat.getInstance locale.java_locale
decimal_format.applyLocalizedPattern format
Panic.catch ParseException (decimal_format.parse text) _->
Error.throw (Number_Parse_Error.Error text)
parse text (locale : Locale | Nothing = Nothing) (format : Text | Nothing = Nothing) -> Float ! Number_Parse_Error ! Illegal_Argument =
case locale.is_nothing && format.is_nothing of
True ->
Panic.catch NumberFormatException (Double.parseDouble text) _->
Error.throw (Number_Parse_Error.Error text)
False ->
Illegal_Argument.handle_java_exception <|
defaulted_locale = locale.if_nothing Locale.default
defaulted_format = format.if_nothing ""
# `getInstance` returns `DecimalFormat` or a subclass of `DecimalFormat`.
decimal_format = NumberFormat.getInstance defaulted_locale.java_locale
decimal_format.applyLocalizedPattern defaulted_format
Panic.catch ParseException (decimal_format.parse text) _->
Error.throw (Number_Parse_Error.Error text)

## ICON input_number

Expand Down
2 changes: 1 addition & 1 deletion test/Base_Tests/src/Data/Numbers_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ add_specs suite_builder =
Float.parse "aaaa" l . should_fail_with Number_Parse_Error

group_builder.specify "should parse correctly with format and/or locale" <|
Float.parse "123,456,789.87654" . should_equal 123456789.87654
Float.parse "123,456,789.87654" locale=Locale.default . should_equal 123456789.87654
Float.parse "123.456.789,87654" locale=Locale.italy . should_equal 123456789.87654

Float.parse "123,456,789.88" format="#,###.##" . should_equal 123456789.88
Expand Down

0 comments on commit 442123b

Please sign in to comment.