Format ucstring source

develop
kaetemi 4 years ago
parent 1300d5d4d4
commit dd28130617

@ -35,95 +35,98 @@ typedef std::basic_string<ucchar> ucstringbase;
class ucstring : public ucstringbase class ucstring : public ucstringbase
{ {
public: public:
ucstring() { }
ucstring () {} ucstring(const ucstringbase &str)
: ucstringbase(str)
ucstring (const ucstringbase &str) : ucstringbase (str) {} {
}
ucstring (const std::string &str) : ucstringbase () ucstring(const std::string &str)
: ucstringbase()
{ {
rawCopy(str); rawCopy(str);
} }
~ucstring () {} ~ucstring() { }
ucstring &operator= (ucchar c) ucstring &operator=(ucchar c)
{ {
resize (1); resize(1);
operator[](0) = c; operator[](0) = c;
return *this; return *this;
} }
ucstring &operator= (const char *str) ucstring &operator=(const char *str)
{ {
resize (strlen (str)); resize(strlen(str));
for (uint i = 0; i < strlen (str); i++) for (uint i = 0; i < strlen(str); i++)
{ {
operator[](i) = uint8(str[i]); operator[](i) = uint8(str[i]);
} }
return *this; return *this;
} }
ucstring &operator= (const std::string &str) ucstring &operator=(const std::string &str)
{ {
resize (str.size ()); resize(str.size());
for (uint i = 0; i < str.size (); i++) for (uint i = 0; i < str.size(); i++)
{ {
operator[](i) = uint8(str[i]); operator[](i) = uint8(str[i]);
} }
return *this; return *this;
} }
ucstring &operator= (const ucstringbase &str) ucstring &operator=(const ucstringbase &str)
{ {
ucstringbase::operator =(str); ucstringbase::operator=(str);
return *this; return *this;
} }
ucstring& operator= (const ucchar *str) ucstring &operator=(const ucchar *str)
{ {
ucstringbase::operator =(str); ucstringbase::operator=(str);
return *this; return *this;
} }
ucstring &operator+= (ucchar c) ucstring &operator+=(ucchar c)
{ {
resize (size() + 1); resize(size() + 1);
operator[](size()-1) = c; operator[](size() - 1) = c;
return *this; return *this;
} }
ucstring &operator+= (const char *str) ucstring &operator+=(const char *str)
{ {
size_t s = size(); size_t s = size();
resize (s + strlen(str)); resize(s + strlen(str));
for (uint i = 0; i < strlen(str); i++) for (uint i = 0; i < strlen(str); i++)
{ {
operator[](s+i) = uint8(str[i]); operator[](s + i) = uint8(str[i]);
} }
return *this; return *this;
} }
ucstring &operator+= (const std::string &str) ucstring &operator+=(const std::string &str)
{ {
size_t s = size(); size_t s = size();
resize (s + str.size()); resize(s + str.size());
for (uint i = 0; i < str.size(); i++) for (uint i = 0; i < str.size(); i++)
{ {
operator[](s+i) = uint8(str[i]); operator[](s + i) = uint8(str[i]);
} }
return *this; return *this;
} }
ucstring &operator+= (const ucstringbase &str) ucstring &operator+=(const ucstringbase &str)
{ {
ucstringbase::operator +=(str); ucstringbase::operator+=(str);
return *this; return *this;
} }
const ucchar *c_str() const const ucchar *c_str() const
{ {
const ucchar *tmp = ucstringbase::c_str(); const ucchar *tmp = ucstringbase::c_str();
const_cast<ucchar*>(tmp)[size()] = 0; const_cast<ucchar *>(tmp)[size()] = 0;
return tmp; return tmp;
} }
@ -131,7 +134,7 @@ public:
void toString(std::string &str) const; void toString(std::string &str) const;
/// Converts the controlled ucstring and returns the resulting string /// Converts the controlled ucstring and returns the resulting string
std::string toString () const std::string toString() const
{ {
std::string str; std::string str;
toString(str); toString(str);
@ -165,70 +168,72 @@ public:
private: private:
void rawCopy(const std::string &str); void rawCopy(const std::string &str);
}; };
inline ucstring operator+(const ucstringbase &ucstr, ucchar c) inline ucstring operator+(const ucstringbase &ucstr, ucchar c)
{ {
ucstring ret; ucstring ret;
ret= ucstr; ret = ucstr;
ret+= c; ret += c;
return ret; return ret;
} }
inline ucstring operator+(const ucstringbase &ucstr, const char *c) inline ucstring operator+(const ucstringbase &ucstr, const char *c)
{ {
ucstring ret; ucstring ret;
ret= ucstr; ret = ucstr;
ret+= c; ret += c;
return ret; return ret;
} }
inline ucstring operator+(const ucstringbase &ucstr, const std::string &c) inline ucstring operator+(const ucstringbase &ucstr, const std::string &c)
{ {
ucstring ret; ucstring ret;
ret= ucstr; ret = ucstr;
ret+= c; ret += c;
return ret; return ret;
} }
inline ucstring operator+(ucchar c, const ucstringbase &ucstr) inline ucstring operator+(ucchar c, const ucstringbase &ucstr)
{ {
ucstring ret; ucstring ret;
ret= c; ret = c;
ret += ucstr; ret += ucstr;
return ret; return ret;
} }
inline ucstring operator+(const char *c, const ucstringbase &ucstr) inline ucstring operator+(const char *c, const ucstringbase &ucstr)
{ {
ucstring ret; ucstring ret;
ret= c; ret = c;
ret += ucstr; ret += ucstr;
return ret; return ret;
} }
inline ucstring operator+(const std::string &c, const ucstringbase &ucstr) inline ucstring operator+(const std::string &c, const ucstringbase &ucstr)
{ {
ucstring ret; ucstring ret;
ret= c; ret = c;
ret += ucstr; ret += ucstr;
return ret; return ret;
} }
namespace NLMISC namespace NLMISC {
{
// Traits for hash_map using CEntityId // Traits for hash_map using CEntityId
struct CUCStringHashMapTraits struct CUCStringHashMapTraits
{ {
enum { bucket_size = 4, min_buckets = 8 }; enum
{
bucket_size = 4,
min_buckets = 8
};
CUCStringHashMapTraits() { } CUCStringHashMapTraits() { }
size_t operator() (const ucstring &id ) const size_t operator()(const ucstring &id) const
{ {
return id.size(); return id.size();
} }
bool operator() (const ucstring &id1, const ucstring &id2) const bool operator()(const ucstring &id1, const ucstring &id2) const
{ {
return id1 < id2; return id1 < id2;
} }
@ -239,18 +244,18 @@ struct CUCStringHashMapTraits
* \param a string or a char to transform to lower case * \param a string or a char to transform to lower case
*/ */
ucstring toLower (const ucstring &str); ucstring toLower(const ucstring &str);
void toLower (ucchar *str); void toLower(ucchar *str);
ucchar toLower (ucchar c); ucchar toLower(ucchar c);
/** Convert an unicode string in upper case. /** Convert an unicode string in upper case.
* Characters with accent are converted in a uppercase character without accent * Characters with accent are converted in a uppercase character without accent
* \param a string or a char to transform to upper case * \param a string or a char to transform to upper case
*/ */
ucstring toUpper (const ucstring &str); ucstring toUpper(const ucstring &str);
void toUpper (ucchar *str); void toUpper(ucchar *str);
ucchar toUpper (ucchar c); ucchar toUpper(ucchar c);
}; };

@ -20,18 +20,18 @@
void ucstring::toString(std::string &str) const void ucstring::toString(std::string &str) const
{ {
str.resize(size()); str.resize(size());
for (uint i = 0; i < str.size (); i++) for (uint i = 0; i < str.size(); i++)
{ {
if (operator[](i) > 255) if (operator[](i) > 255)
str[i] = '?'; str[i] = '?';
else else
str[i] = (char) operator[](i); str[i] = (char)operator[](i);
} }
} }
std::string ucstring::toUtf8() const std::string ucstring::toUtf8() const
{ {
std::string res; std::string res;
ucstring::const_iterator first(begin()), last(end()); ucstring::const_iterator first(begin()), last(end());
for (; first != last; ++first) for (; first != last; ++first)
{ {
@ -56,9 +56,9 @@ std::string ucstring::toUtf8() const
nbLoop = 2; nbLoop = 2;
} }
for (uint i=0; i<nbLoop; ++i) for (uint i = 0; i < nbLoop; ++i)
{ {
ucchar c = *first; ucchar c = *first;
c = c >> ((nbLoop - i - 1) * 6); c = c >> ((nbLoop - i - 1) * 6);
c = c & 0x3F; c = c & 0x3F;
res += char(c) | 0x80; res += char(c) | 0x80;
@ -77,7 +77,7 @@ void ucstring::fromUtf8(const std::string &stringUtf8)
sint iterations = 0; sint iterations = 0;
std::string::const_iterator first(stringUtf8.begin()), last(stringUtf8.end()); std::string::const_iterator first(stringUtf8.begin()), last(stringUtf8.end());
for (; first != last; ) for (; first != last;)
{ {
c = *first++; c = *first++;
code = c; code = c;
@ -131,7 +131,7 @@ void ucstring::fromUtf8(const std::string &stringUtf8)
} }
uint8 ch; uint8 ch;
ch = *first ++; ch = *first++;
if ((ch & 0xC0) != 0x80) if ((ch & 0xC0) != 0x80)
{ {
@ -155,7 +155,7 @@ void ucstring::rawCopy(const std::string &str)
resize(str.size()); resize(str.size());
std::string::const_iterator first(str.begin()), last(str.end()); std::string::const_iterator first(str.begin()), last(str.end());
iterator dest(begin()); iterator dest(begin());
for (;first != last; ++first, ++dest) for (; first != last; ++first, ++dest)
{ {
*dest = uint8(*first); *dest = uint8(*first);
} }

Loading…
Cancel
Save