-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Get decimal separator from locale if available. It is not always comma. #1339
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
#include <clocale> | ||
#endif | ||
|
||
#include <algorithm> | ||
|
||
/* This header provides common string manipulation support, such as UTF-8, | ||
* portable conversion from/to string... | ||
* | ||
|
@@ -86,30 +88,25 @@ static inline void uintToString(LargestUInt value, char*& current) { | |
} while (value != 0); | ||
} | ||
|
||
/** Change ',' to '.' everywhere in buffer. | ||
/** Change decimal point from current locale (i.e. ',') to '.' everywhere in buffer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I guess I don't understand the logic of the change then because this isn't what it does if your locale's decimalPoint happens to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree, I made this for backward compatibility (if decimal point is '.' than try to replace all ',' in buffer as it was before). From my point of view it would be better to skip replace if decimal point is '\0' or '.', I can change it this way if you are ok with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a worse breaking change than initially anticipated. The call to Previously the only call was in |
||
* | ||
* We had a sophisticated way, but it did not work in WinCE. | ||
* @see https://github.com/open-source-parsers/jsoncpp/pull/9 | ||
*/ | ||
template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end) { | ||
for (; begin != end; ++begin) { | ||
if (*begin == ',') { | ||
*begin = '.'; | ||
} | ||
const char decimalPoint = getDecimalPoint(); | ||
if (decimalPoint != '\0' && decimalPoint != '.') { | ||
std::replace(begin, end, decimalPoint, '.'); | ||
} | ||
return begin; | ||
return end; | ||
Comment on lines
+97
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compared to a previous implementation it has an undesired side-effect: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This private helper function is only ever used to correct the small buffers produced by The idea was that To really fix this problem we would have to synchronize the Because we're a library we can't control the application locale. We really need to just use a different formatter. There's also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds briliant.
Tried that not so long ago: some compilers that mostly support C++17 might not have it, e.g. libc++ still has a partial support of it. |
||
} | ||
|
||
template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end) { | ||
char decimalPoint = getDecimalPoint(); | ||
if (decimalPoint == '\0' || decimalPoint == '.') { | ||
return; | ||
} | ||
for (; begin != end; ++begin) { | ||
if (*begin == '.') { | ||
*begin = decimalPoint; | ||
} | ||
} | ||
std::replace(begin, end, '.', decimalPoint); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is actually a heavy dependency to include just for a single (and simple) algorithm function to use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it matters enough to optimize around it.
A header like this is likely to be brought in by other headers anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never directly and not but all compilers
(and it gets better each time)