ian@0: // Copyright 2007-2010 Baptiste Lepilleur ian@0: // Distributed under MIT license, or public domain if desired and ian@0: // recognized in your jurisdiction. ian@0: // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE ian@0: ian@0: #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED ian@0: # define LIB_JSONCPP_JSON_TOOL_H_INCLUDED ian@0: ian@0: /* This header provides common string manipulation support, such as UTF-8, ian@0: * portable conversion from/to string... ian@0: * ian@0: * It is an internal header that must not be exposed. ian@0: */ ian@0: ian@0: namespace Json { ian@0: ian@0: /// Converts a unicode code-point to UTF-8. ian@0: static inline std::string ian@0: codePointToUTF8(unsigned int cp) ian@0: { ian@0: std::string result; ian@0: ian@0: // based on description from http://en.wikipedia.org/wiki/UTF-8 ian@0: ian@0: if (cp <= 0x7f) ian@0: { ian@0: result.resize(1); ian@0: result[0] = static_cast(cp); ian@0: } ian@0: else if (cp <= 0x7FF) ian@0: { ian@0: result.resize(2); ian@0: result[1] = static_cast(0x80 | (0x3f & cp)); ian@0: result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); ian@0: } ian@0: else if (cp <= 0xFFFF) ian@0: { ian@0: result.resize(3); ian@0: result[2] = static_cast(0x80 | (0x3f & cp)); ian@0: result[1] = 0x80 | static_cast((0x3f & (cp >> 6))); ian@0: result[0] = 0xE0 | static_cast((0xf & (cp >> 12))); ian@0: } ian@0: else if (cp <= 0x10FFFF) ian@0: { ian@0: result.resize(4); ian@0: result[3] = static_cast(0x80 | (0x3f & cp)); ian@0: result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); ian@0: result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); ian@0: result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); ian@0: } ian@0: ian@0: return result; ian@0: } ian@0: ian@0: ian@0: /// Returns true if ch is a control character (in range [0,32[). ian@0: static inline bool ian@0: isControlCharacter(char ch) ian@0: { ian@0: return ch > 0 && ch <= 0x1F; ian@0: } ian@0: ian@0: ian@0: enum { ian@0: /// Constant that specify the size of the buffer that must be passed to uintToString. ian@0: uintToStringBufferSize = 3*sizeof(LargestUInt)+1 ian@0: }; ian@0: ian@0: // Defines a char buffer for use with uintToString(). ian@0: typedef char UIntToStringBuffer[uintToStringBufferSize]; ian@0: ian@0: ian@0: /** Converts an unsigned integer to string. ian@0: * @param value Unsigned interger to convert to string ian@0: * @param current Input/Output string buffer. ian@0: * Must have at least uintToStringBufferSize chars free. ian@0: */ ian@0: static inline void ian@0: uintToString( LargestUInt value, ian@0: char *¤t ) ian@0: { ian@0: *--current = 0; ian@0: do ian@0: { ian@0: *--current = char(value % 10) + '0'; ian@0: value /= 10; ian@0: } ian@0: while ( value != 0 ); ian@0: } ian@0: ian@0: } // namespace Json { ian@0: ian@0: #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED