27 if ( n >= 1<<16 ) { n >>= 16; pos += 16; }
28 if ( n >= 1<< 8 ) { n >>= 8; pos += 8; }
29 if ( n >= 1<< 4 ) { n >>= 4; pos += 4; }
30 if ( n >= 1<< 2 ) { n >>= 2; pos += 2; }
31 if ( n >= 1<< 1 ) { pos += 1; }
32 return ( (n == 0) ? (-1) : pos );
35 unsigned _lookup(
const std::string& str,
const char* values[],
unsigned size,
int def )
38 for( ; i < size && str != values[i]; ++i )
40 return ( i == size && def >= 0 ) ? (unsigned)def : i;
43 const std::string
_lookup(
unsigned code,
const char* values[],
unsigned size,
const std::string& def )
45 return code < size ? std::string( values[code] ) : def;
48 unsigned _lookup2(
const std::string& str,
const char* values[],
49 unsigned size,
int def )
54 const std::string
_lookup2(
unsigned code,
const char* values[],
unsigned size,
const std::string& def )
57 return i < size ? std::string( values[i] ) : def;
60 std::string
hex(
const std::string& input )
62 const char* H = input.c_str();
63 char* buf =
new char[input.length() * 2 + 1];
64 for(
unsigned int i = 0; i < input.length(); ++i )
65 sprintf( buf + i * 2,
"%02x", (
unsigned char)( H[i] ) );
66 return std::string( buf, 40 );
69 static const char escape_chars[] = {
'&',
'<',
'>',
'\'',
'"' };
71 static const std::string escape_seqs[] = {
"amp;",
"lt;",
"gt;",
"apos;",
"quot;" };
73 static const std::string escape_seqs_full[] = {
"&",
"<",
">",
"'",
""" };
75 static const unsigned escape_size = 5;
77 const std::string
escape( std::string what )
79 for(
size_t val, i = 0; i < what.length(); ++i )
81 for( val = 0; val < escape_size; ++val )
83 if( what[i] == escape_chars[val] )
86 what.insert( i+1, escape_seqs[val] );
87 i += escape_seqs[val].length();
97 size_t rangeStart = 0, rangeCount = 0;
98 size_t length = data.length();
99 const char* dataPtr = data.data();
100 for(
size_t val, i = 0; i < length; ++i )
102 const char current = dataPtr[i];
103 for( val = 0; val < escape_size; ++val )
105 if( current == escape_chars[val] )
116 target.append( data, rangeStart, rangeCount );
118 target.append( escape_seqs_full[val] );
125 if( rangeStart <= i )
137 target.append( data, rangeStart, rangeCount );
146 const char* dataPtr = data.data();
147 const char* end = dataPtr + data.length();
148 for( ; dataPtr != end; ++dataPtr )
150 unsigned char current = (
unsigned char) *dataPtr;
162 else if( current >= 0xf5 )
165 else if( current == 0xc0
174 return ( dataPtr == end );
177 void replaceAll( std::string& target,
const std::string& find,
const std::string& replace )
179 std::string::size_type findSize = find.size();
180 std::string::size_type replaceSize = replace.size();
185 std::string::size_type index = target.find( find, 0 );
187 while( index != std::string::npos )
189 target.replace( index, findSize, replace );
190 index = target.find( find, index+replaceSize );
void replaceAll(std::string &target, const std::string &find, const std::string &replace)
bool checkValidXMLChars(const std::string &data)
const std::string escape(std::string what)
void appendEscaped(std::string &target, const std::string &data)
int internalLog2(unsigned int n)
std::string hex(const std::string &input)
The namespace for the gloox library.
unsigned _lookup(const std::string &str, const char *values[], unsigned size, int def)
unsigned _lookup2(const std::string &str, const char *values[], unsigned size, int def)