Improvements in writing precision and json_tool.h helpers

This commit is contained in:
Billy Donahue
2018-05-11 10:20:04 -04:00
parent cf73619e28
commit 0ba5c435f4
2 changed files with 58 additions and 55 deletions

View File

@@ -88,41 +88,47 @@ static inline void uintToString(LargestUInt value, char*& current) {
* We had a sophisticated way, but it did not work in WinCE.
* @see https://github.com/open-source-parsers/jsoncpp/pull/9
*/
static inline void fixNumericLocale(char* begin, char* end) {
while (begin < end) {
template <typename Iter>
Iter fixNumericLocale(Iter begin, Iter end) {
for (; begin != end; ++begin) {
if (*begin == ',') {
*begin = '.';
}
++begin;
}
return begin;
}
static inline void fixNumericLocaleInput(char* begin, char* end) {
template <typename Iter>
void fixNumericLocaleInput(Iter begin, Iter end) {
char decimalPoint = getDecimalPoint();
if (decimalPoint != '\0' && decimalPoint != '.') {
while (begin < end) {
if (*begin == '.') {
*begin = decimalPoint;
}
++begin;
if (decimalPoint == '\0' || decimalPoint == '.') {
return;
}
for (; begin != end; ++begin) {
if (*begin == '.') {
*begin = decimalPoint;
}
}
}
/**
* Delete zeros in the end of string, if it isn't last zero before '.' character.
* Return iterator that would be the new end of the range [begin,end), if we
* were to delete zeros in the end of string, but not the last zero before '.'.
*/
static inline void fixZerosInTheEnd(char* begin, char* end) {
end--;
while ((begin < end) && (*end == '0')) {
// don't delete last zero before point.
if (*(end - 1) != '.') {
*end = '\0';
template <typename Iter>
Iter fixZerosInTheEnd(Iter begin, Iter end) {
for (; begin != end; --end) {
if (*(end-1) != '0') {
return end;
}
// Don't delete the last zero before the decimal point.
if (begin != (end-1) && *(end-2) == '.') {
return end;
}
end--;
}
return end;
}
} // namespace Json {
} // namespace Json
#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED