JsonCpp project page JsonCpp home page

json_tool.h

Go to the documentation of this file.
00001 // Copyright 2007-2010 Baptiste Lepilleur
00002 // Distributed under MIT license, or public domain if desired and
00003 // recognized in your jurisdiction.
00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
00005 
00006 #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
00007 # define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
00008 
00009 /* This header provides common string manipulation support, such as UTF-8,
00010  * portable conversion from/to string...
00011  *
00012  * It is an internal header that must not be exposed.
00013  */
00014 
00015 namespace Json {
00016 
00018 static inline std::string 
00019 codePointToUTF8(unsigned int cp)
00020 {
00021    std::string result;
00022    
00023    // based on description from http://en.wikipedia.org/wiki/UTF-8
00024 
00025    if (cp <= 0x7f) 
00026    {
00027       result.resize(1);
00028       result[0] = static_cast<char>(cp);
00029    } 
00030    else if (cp <= 0x7FF) 
00031    {
00032       result.resize(2);
00033       result[1] = static_cast<char>(0x80 | (0x3f & cp));
00034       result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
00035    } 
00036    else if (cp <= 0xFFFF) 
00037    {
00038       result.resize(3);
00039       result[2] = static_cast<char>(0x80 | (0x3f & cp));
00040       result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6)));
00041       result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12)));
00042    }
00043    else if (cp <= 0x10FFFF) 
00044    {
00045       result.resize(4);
00046       result[3] = static_cast<char>(0x80 | (0x3f & cp));
00047       result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
00048       result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
00049       result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
00050    }
00051 
00052    return result;
00053 }
00054 
00055 
00057 static inline bool 
00058 isControlCharacter(char ch)
00059 {
00060    return ch > 0 && ch <= 0x1F;
00061 }
00062 
00063 
00064 enum { 
00066    uintToStringBufferSize = 3*sizeof(LargestUInt)+1 
00067 };
00068 
00069 // Defines a char buffer for use with uintToString().
00070 typedef char UIntToStringBuffer[uintToStringBufferSize];
00071 
00072 
00078 static inline void 
00079 uintToString( LargestUInt value, 
00080               char *&current )
00081 {
00082    *--current = 0;
00083    do
00084    {
00085       *--current = char(value % 10) + '0';
00086       value /= 10;
00087    }
00088    while ( value != 0 );
00089 }
00090 
00091 } // namespace Json {
00092 
00093 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED

SourceForge Logo hosts this site. Send comments to:
Json-cpp Developers