2017-07-21 18:44:36 +08:00
|
|
|
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
|
2010-04-21 05:35:19 +08:00
|
|
|
// Distributed under MIT license, or public domain if desired and
|
|
|
|
// recognized in your jurisdiction.
|
|
|
|
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
|
|
|
|
|
|
|
#ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|
2014-07-01 06:48:54 +08:00
|
|
|
#define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|
2016-09-06 19:39:59 +08:00
|
|
|
|
2018-06-05 15:17:36 +08:00
|
|
|
#if !defined(JSON_IS_AMALGAMATION)
|
2018-05-21 04:32:06 +08:00
|
|
|
#include <json/config.h>
|
2018-06-05 15:17:36 +08:00
|
|
|
#endif
|
2016-11-08 16:46:34 +08:00
|
|
|
|
|
|
|
// Also support old flag NO_LOCALE_SUPPORT
|
|
|
|
#ifdef NO_LOCALE_SUPPORT
|
|
|
|
#define JSONCPP_NO_LOCALE_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef JSONCPP_NO_LOCALE_SUPPORT
|
2016-08-22 09:12:53 +08:00
|
|
|
#include <clocale>
|
2016-09-06 19:39:59 +08:00
|
|
|
#endif
|
2010-04-21 05:35:19 +08:00
|
|
|
|
|
|
|
/* This header provides common string manipulation support, such as UTF-8,
|
|
|
|
* portable conversion from/to string...
|
|
|
|
*
|
|
|
|
* It is an internal header that must not be exposed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Json {
|
2018-05-21 04:32:06 +08:00
|
|
|
static inline char getDecimalPoint() {
|
2016-11-08 16:46:34 +08:00
|
|
|
#ifdef JSONCPP_NO_LOCALE_SUPPORT
|
2016-09-06 19:39:59 +08:00
|
|
|
return '\0';
|
|
|
|
#else
|
|
|
|
struct lconv* lc = localeconv();
|
|
|
|
return lc ? *(lc->decimal_point) : '\0';
|
|
|
|
#endif
|
|
|
|
}
|
2016-08-27 05:30:18 +08:00
|
|
|
|
2010-04-21 05:35:19 +08:00
|
|
|
/// Converts a unicode code-point to UTF-8.
|
2019-01-18 05:35:29 +08:00
|
|
|
static inline String codePointToUTF8(unsigned int cp) {
|
|
|
|
String result;
|
2010-04-21 05:35:19 +08:00
|
|
|
|
2014-07-01 06:48:54 +08:00
|
|
|
// based on description from http://en.wikipedia.org/wiki/UTF-8
|
2010-04-21 05:35:19 +08:00
|
|
|
|
2014-07-01 06:48:54 +08:00
|
|
|
if (cp <= 0x7f) {
|
|
|
|
result.resize(1);
|
|
|
|
result[0] = static_cast<char>(cp);
|
|
|
|
} else if (cp <= 0x7FF) {
|
|
|
|
result.resize(2);
|
|
|
|
result[1] = static_cast<char>(0x80 | (0x3f & cp));
|
|
|
|
result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
|
|
|
|
} else if (cp <= 0xFFFF) {
|
|
|
|
result.resize(3);
|
|
|
|
result[2] = static_cast<char>(0x80 | (0x3f & cp));
|
2015-07-13 01:55:18 +08:00
|
|
|
result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
|
|
|
|
result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
|
2014-07-01 06:48:54 +08:00
|
|
|
} else if (cp <= 0x10FFFF) {
|
|
|
|
result.resize(4);
|
|
|
|
result[3] = static_cast<char>(0x80 | (0x3f & cp));
|
|
|
|
result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
|
|
|
|
result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
|
|
|
|
result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
|
|
|
|
}
|
2010-04-21 05:35:19 +08:00
|
|
|
|
2014-07-01 06:48:54 +08:00
|
|
|
return result;
|
2010-04-21 05:35:19 +08:00
|
|
|
}
|
|
|
|
|
2014-07-01 06:48:54 +08:00
|
|
|
enum {
|
|
|
|
/// Constant that specify the size of the buffer that must be passed to
|
|
|
|
/// uintToString.
|
|
|
|
uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
|
2010-04-21 05:35:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Defines a char buffer for use with uintToString().
|
2019-12-04 09:08:45 +08:00
|
|
|
using UIntToStringBuffer = char[uintToStringBufferSize];
|
2010-04-21 05:35:19 +08:00
|
|
|
|
|
|
|
/** Converts an unsigned integer to string.
|
2017-12-04 00:54:29 +08:00
|
|
|
* @param value Unsigned integer to convert to string
|
2014-07-01 06:48:54 +08:00
|
|
|
* @param current Input/Output string buffer.
|
2010-04-21 05:35:19 +08:00
|
|
|
* Must have at least uintToStringBufferSize chars free.
|
|
|
|
*/
|
2014-09-15 08:15:29 +08:00
|
|
|
static inline void uintToString(LargestUInt value, char*& current) {
|
2014-07-01 06:48:54 +08:00
|
|
|
*--current = 0;
|
|
|
|
do {
|
2016-03-24 11:33:18 +08:00
|
|
|
*--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
|
2014-07-01 06:48:54 +08:00
|
|
|
value /= 10;
|
|
|
|
} while (value != 0);
|
2010-04-21 05:35:19 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 11:22:47 +08:00
|
|
|
/** Change ',' to '.' everywhere in buffer.
|
|
|
|
*
|
|
|
|
* We had a sophisticated way, but it did not work in WinCE.
|
|
|
|
* @see https://github.com/open-source-parsers/jsoncpp/pull/9
|
|
|
|
*/
|
2018-05-21 04:55:27 +08:00
|
|
|
template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end) {
|
2018-05-11 22:20:04 +08:00
|
|
|
for (; begin != end; ++begin) {
|
2014-07-11 11:22:47 +08:00
|
|
|
if (*begin == ',') {
|
|
|
|
*begin = '.';
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 22:20:04 +08:00
|
|
|
return begin;
|
2014-07-11 11:22:47 +08:00
|
|
|
}
|
|
|
|
|
2018-05-21 04:55:27 +08:00
|
|
|
template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end) {
|
2016-09-06 19:39:59 +08:00
|
|
|
char decimalPoint = getDecimalPoint();
|
2018-05-11 22:20:04 +08:00
|
|
|
if (decimalPoint == '\0' || decimalPoint == '.') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
if (*begin == '.') {
|
|
|
|
*begin = decimalPoint;
|
2016-08-22 09:12:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 04:35:31 +08:00
|
|
|
/**
|
2018-05-11 22:20:04 +08:00
|
|
|
* 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 '.'.
|
2018-03-14 04:35:31 +08:00
|
|
|
*/
|
2020-12-16 03:08:05 +08:00
|
|
|
template <typename Iter>
|
|
|
|
Iter fixZerosInTheEnd(Iter begin, Iter end, unsigned int precision) {
|
2018-05-11 22:20:04 +08:00
|
|
|
for (; begin != end; --end) {
|
2018-05-21 04:55:27 +08:00
|
|
|
if (*(end - 1) != '0') {
|
2018-05-11 22:20:04 +08:00
|
|
|
return end;
|
|
|
|
}
|
|
|
|
// Don't delete the last zero before the decimal point.
|
2020-12-16 03:08:05 +08:00
|
|
|
if (begin != (end - 1) && begin != (end - 2) && *(end - 2) == '.') {
|
|
|
|
if (precision) {
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
return end - 2;
|
2018-03-14 04:35:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 22:20:04 +08:00
|
|
|
return end;
|
2018-03-14 04:35:31 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 22:20:04 +08:00
|
|
|
} // namespace Json
|
2010-04-21 05:35:19 +08:00
|
|
|
|
|
|
|
#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|