Path: blob/master/Utilities/cmjsoncpp/src/lib_json/json_tool.h
3158 views
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors1// Distributed under MIT license, or public domain if desired and2// recognized in your jurisdiction.3// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE45#ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED6#define LIB_JSONCPP_JSON_TOOL_H_INCLUDED78#if !defined(JSON_IS_AMALGAMATION)9#include <json/config.h>10#endif1112// Also support old flag NO_LOCALE_SUPPORT13#ifdef NO_LOCALE_SUPPORT14#define JSONCPP_NO_LOCALE_SUPPORT15#endif1617#ifndef JSONCPP_NO_LOCALE_SUPPORT18#include <clocale>19#endif2021/* This header provides common string manipulation support, such as UTF-8,22* portable conversion from/to string...23*24* It is an internal header that must not be exposed.25*/2627namespace Json {28static inline char getDecimalPoint() {29#ifdef JSONCPP_NO_LOCALE_SUPPORT30return '\0';31#else32struct lconv* lc = localeconv();33return lc ? *(lc->decimal_point) : '\0';34#endif35}3637/// Converts a unicode code-point to UTF-8.38static inline String codePointToUTF8(unsigned int cp) {39String result;4041// based on description from http://en.wikipedia.org/wiki/UTF-84243if (cp <= 0x7f) {44result.resize(1);45result[0] = static_cast<char>(cp);46} else if (cp <= 0x7FF) {47result.resize(2);48result[1] = static_cast<char>(0x80 | (0x3f & cp));49result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));50} else if (cp <= 0xFFFF) {51result.resize(3);52result[2] = static_cast<char>(0x80 | (0x3f & cp));53result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));54result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));55} else if (cp <= 0x10FFFF) {56result.resize(4);57result[3] = static_cast<char>(0x80 | (0x3f & cp));58result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));59result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));60result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));61}6263return result;64}6566enum {67/// Constant that specify the size of the buffer that must be passed to68/// uintToString.69uintToStringBufferSize = 3 * sizeof(LargestUInt) + 170};7172// Defines a char buffer for use with uintToString().73using UIntToStringBuffer = char[uintToStringBufferSize];7475/** Converts an unsigned integer to string.76* @param value Unsigned integer to convert to string77* @param current Input/Output string buffer.78* Must have at least uintToStringBufferSize chars free.79*/80static inline void uintToString(LargestUInt value, char*& current) {81*--current = 0;82do {83*--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));84value /= 10;85} while (value != 0);86}8788/** Change ',' to '.' everywhere in buffer.89*90* We had a sophisticated way, but it did not work in WinCE.91* @see https://github.com/open-source-parsers/jsoncpp/pull/992*/93template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end) {94for (; begin != end; ++begin) {95if (*begin == ',') {96*begin = '.';97}98}99return begin;100}101102template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end) {103char decimalPoint = getDecimalPoint();104if (decimalPoint == '\0' || decimalPoint == '.') {105return;106}107for (; begin != end; ++begin) {108if (*begin == '.') {109*begin = decimalPoint;110}111}112}113114/**115* Return iterator that would be the new end of the range [begin,end), if we116* were to delete zeros in the end of string, but not the last zero before '.'.117*/118template <typename Iter>119Iter fixZerosInTheEnd(Iter begin, Iter end, unsigned int precision) {120for (; begin != end; --end) {121if (*(end - 1) != '0') {122return end;123}124// Don't delete the last zero before the decimal point.125if (begin != (end - 1) && begin != (end - 2) && *(end - 2) == '.') {126if (precision) {127return end;128}129return end - 2;130}131}132return end;133}134135} // namespace Json136137#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED138139140