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

String.format(Locale.ROOT, ...) wrapper #1946

Open
cpovirk opened this issue Jan 13, 2015 · 5 comments
Open

String.format(Locale.ROOT, ...) wrapper #1946

cpovirk opened this issue Jan 13, 2015 · 5 comments

Comments

@cpovirk
Copy link
Member

cpovirk commented Jan 13, 2015

String.format depends on the default locale, leading to problems like #1567. We can fix it by passing Locale.ROOT to each call, but this is kind of ugly, especially for an API that exists in large part to eliminate ugly string-concatenation code.

Where would it go?

  • Strings.format: looks like String.format, possibly too much so
  • Strings.rootLocaleFormat: for easy static import
  • RootLocale.format: gives a place for future such methods. Are there any candidates?

And how worried are we about use of the default locale here? Oddly, I'm not sure I've ever heard anyone crusading against String.format, even though there are crusaders against many other default-locale APIs. Maybe it's just less frequently noticed there, in part because users often just use %s? Plus, the output is probably more often used for debugging than it would be for, e.g., SimpleDateFormat.

@Stephan202
Copy link
Contributor

Interesting topic. I've seen issues with String.format and the default locale several times in production code, most recently today. I think it's a problem worthy of Guava's help.

As you mentioned, this issue is seemingly on few people's radar. Raising awareness would be a good thing to do.

@bernardosulzbach
Copy link

I would like this. Not as Strings.format, though. Although different, when skimming over code this can easily get unnoticed.

@cpovirk
Copy link
Member Author

cpovirk commented Jul 30, 2019

I suppose there's some overlap with the recently added Strings.lenientFormat, though that API is purportedly only for cases in which String.format is unavailable or throwing an exception would obscure another exception.

@cpovirk
Copy link
Member Author

cpovirk commented Jul 17, 2024

(One thing that I'd overlooked is that there is (probably?) no problem if you use only %s (via). That suggests that I overdid things a little back in cl/83859950 (e.g., in CacheBuilderSpec). That's not really a problem, though.)

@tbroyer
Copy link

tbroyer commented Jul 20, 2024

Fwiw, %s is only safe if the corresponding argument is not Formattable.

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

No branches or pull requests

5 participants