You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm parsing lots of CSV files where values are empty - i.e. just ,,,, columns in CSV.
System.Buffers.Text.Utf8Parser.TryParse will in this case simply return consumed=0
whereas csFastFloat in such case raises System.ArgumentException.
The problem is that in such case I have to either make additional checks before calling ParseFloat
or even handling exceptions which makes the code much slower.
I'd suggest to change the behaviour of functions accepting consumed to not raise exceptions in such cases,
but rather set consumed=0.
public static double ParseDouble(string s, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
public static double ParseDouble(string s, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
+public static bool TryParseDouble(string s, out double value, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
public static double ParseDouble(ReadOnlySpan<char> s, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
public static double ParseDouble(ReadOnlySpan<char> s, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
+public static bool TryParseDouble(ReadOnlySpan<char> s, out double value, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.');
public static double ParseDouble(char* first, char* last, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.')
For float analogous.
This follows the common pattern in .NET for TryXyz-methods.
If it can be parsed successfully, return true and set the value in the out-parameter.
If it can't be parsed, return false and don't throw an exception, characters_consumed can (if possible) be set to the position that caused the failure in parsing. If this isn't possible without regressing performance, just return 0.
The current method, that throw an exception, can be made to be a thin-wrapper around the new overloads. Conceptionally:
Those overloads will soon be added in next version (as well as numberstyles instead of char_formats). I was just talking about this with prof. @lemire this morning !
Activity
gfoidl commentedon Mar 5, 2021
Great point!
I suppose to add overloads
For
floatanalogous.This follows the common pattern in .NET for TryXyz-methods.
If it can be parsed successfully, return
trueand set the value in the out-parameter.If it can't be parsed, return
falseand don't throw an exception,characters_consumedcan (if possible) be set to the position that caused the failure in parsing. If this isn't possible without regressing performance, just return 0.The current method, that throw an exception, can be made to be a thin-wrapper around the new overloads. Conceptionally:
PS: I can submit a PR if you want.
lemire commentedon Mar 5, 2021
I would invite a PR. It seems reasonable.
CarlVerret commentedon Mar 5, 2021
Those overloads will soon be added in next version (as well as numberstyles instead of char_formats). I was just talking about this with prof. @lemire this morning !
gfoidl commentedon Mar 5, 2021
@CarlVerret
So you have something in the pipeline for this already?
(Perfect 😃).
CarlVerret commentedon Mar 5, 2021
i'll create a new branch for this later today.