Replacement for snprintf

berni44 dlang at d-ecke.de
Thu Nov 7 09:31:27 UTC 2019


On Wednesday, 6 November 2019 at 17:28:58 UTC, H. S. Teoh wrote:
> Yes, I think in the long run this will be the more viable 
> approach. Depending on locale as a global state is problematic 
> because it forces formatting to be impure, and also forces 
> users to implement hacks when they need to temporarily change 
> the locale. E.g., in a system like snprintf, if you need to 
> format German text with snippets of English quotations, you 
> will have to temporarily override LC_* somehow in order to 
> print a number with two different separators, or hack it with 
> string postprocessing, etc..

My current approch is a pure and @safe function that's doing the 
formating, but ignores the locale completely. This function is 
called from formatValueImpl and could be modified there, if 
desired.

Currently (I want to make small steps), the function can only be 
used for the f (and F) specifier (and only for float and double). 
For all other specifiers/types snprintf is still called. That 
might result in different behaviour depending on the specifier 
and the type. I'd prefere to make it behave identically.

Having said this, I completely agree, that it would be better if 
format ignores the locale and let's the user do this in a 
wrapper, if desired.


More information about the Digitalmars-d mailing list