Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse ByteSize with IFormatProvider? #1061

Closed
dahlbyk opened this issue Apr 30, 2021 · 4 comments · Fixed by #1062
Closed

Parse ByteSize with IFormatProvider? #1061

dahlbyk opened this issue Apr 30, 2021 · 4 comments · Fixed by #1062

Comments

@dahlbyk
Copy link
Contributor

dahlbyk commented Apr 30, 2021

While poking around for #1060 I noticed CultureInfo.CurrentCulture in ByteSize.TryParse():

var decSep = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

Would you be interested TryParse/Parse overloads that accept IFormatProvider? Asking first since it's a public API change.

It would check if the IFormatProvider is either CultureInfo or a NumberFormatInfo.

@clairernovotny
Copy link
Member

Can we do it with a new overload? I'm okay with adding overloads; been trying to avoid breaking changes for the most part.

@dahlbyk
Copy link
Contributor Author

dahlbyk commented May 1, 2021

Yeah, should have clarified I meant an additive public API change.

While we're here, there's also a reference to CurrentCulture here:

public string Transform(string input)
{
return CultureInfo.CurrentCulture.TextInfo.ToLower(input);
}

But not here:

public string Transform(string input)
{
return input.ToUpper();
}

Seems like they should be consistent? Is there any value in allowing a culture-specific transform?

@clairernovotny
Copy link
Member

TBH, it's been so long since I've looked at that code. It does seem odd and I agree that CurrentCulture should be used where appropriate.

I'm not sure if having a culture aware one makes a difference, but I'm open to additional overloads that take the culture.

@dahlbyk
Copy link
Contributor Author

dahlbyk commented May 2, 2021

TBH, it's been so long since I've looked at that code. It does seem odd and I agree that CurrentCulture should be used where appropriate.

ToUpper() uses CurrentCulture, so the behavior is close to the same. The only difference is a NullReferenceException vs ArgumentNullException given null.

At minimum we can fix the NullRef here and in ToSentenceCase. I would argue that since null handling isn't well-defined, it's just as useful for most transforms to short-circuit and return null instead of throwing.

I'm not sure if having a culture aware one makes a difference, but I'm open to additional overloads that take the culture.

Can't overload the To properties that return the given transformers, but I think it could work to do something like this:

// Better name ideas?
public interface ICulturedStringTransformer : IStringTransformer
{
    string Transform(string input, CultureInfo culture);
}

With a new overload:

        public static string Transform(this string input, CultureInfo culture, params ICulturedStringTransformer[] transformers)
        {
            return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current, culture));
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants