Skip to content

Consumed=0 vs raising Exceptions #74

Closed

Description

@pkese

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.

Activity

  1. gfoidl commented on Mar 5, 2021

    @gfoidl
    Contributor

    Great point!

    I suppose to add overloads

    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:

    public static double ParseDouble(ReadOnlySpan<char> s, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.')
    {
        if (TryParseDouble(s, out double value, out characters_consumed, expectedFormat, decimal_separator))
        {
            return value;
        }
    
        throw ...
    }
    
    public static bool TryParseDouble(ReadOnlySpan<char> s, out double value, out int characters_consumed, chars_format expectedFormat = chars_format.is_general, char decimal_separator = '.')
    {
        // ...
    }

    PS: I can submit a PR if you want.

  2. lemire commented on Mar 5, 2021

    @lemire
    Collaborator

    I would invite a PR. It seems reasonable.

  3. CarlVerret commented on Mar 5, 2021

    @CarlVerret
    Owner

    Great point!

    I suppose to add overloads

    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 !

  4. gfoidl commented on Mar 5, 2021

    @gfoidl
    Contributor

    @CarlVerret

    will soon be added in next version

    So you have something in the pipeline for this already?
    (Perfect 😃).

  5. CarlVerret commented on Mar 5, 2021

    @CarlVerret
    Owner

    i'll create a new branch for this later today.

  6. added this to the version 3.0 milestone on Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions