32 #ifndef String_INCLUDE_ONCE 33 #define String_INCLUDE_ONCE 40 #if defined(VL_PLATFORM_WINDOWS) 41 #define VL_PLATFORM_DEFAULT_ENCODING SE_LATIN1 43 #define VL_PLATFORM_DEFAULT_ENCODING SE_UTF8 81 static unsigned short getUpperCase(
unsigned short ch);
84 static unsigned short getLowerCase(
unsigned short ch);
87 static unsigned short getTitleCase(
unsigned short ch);
90 static void filterStrings(std::vector<String>& strings,
const String& filter);
95 #if defined(VL_PLATFORM_WINDOWS) 103 static std::string trimStdString(
const std::string& text);
112 String(
const wchar_t* wstr);
118 String(
const std::string& str) { *
this = fromStdString(str,
true); }
121 String(
const std::wstring& str) { *
this = fromStdWString(str); }
124 explicit String(
wchar_t ch,
int count=1);
127 const wchar_t*
ptr()
const { createData();
return &(*mString)[0]; }
130 wchar_t*
ptr() { acquireData();
return &(*mString)[0]; }
133 int length()
const {
if (!mString)
return 0; createData();
return (
int)mString->length(); };
139 bool null()
const {
return !mString; }
142 String&
clear() { acquireData(); mString->clear();
return *
this; }
145 const wchar_t&
operator[](
int i)
const { createData();
return (*mString)[i]; }
148 wchar_t&
operator[](
int i) { acquireData();
return (*mString)[i]; }
151 String& replace(
wchar_t old_ch,
wchar_t new_ch );
154 String& replace(
int start,
int count,
wchar_t ch );
157 String& replace(
int start,
int count,
const String& str );
160 String& replace(
const String& oldstr,
const String& newstr,
bool case_sensitive=
true );
164 String&
remove(
wchar_t ch,
int start=0,
int count=-1);
168 String&
remove(
const String& str,
int start=0,
int count=-1);
171 String&
remove(
int start,
int count );
177 String& normalizeSlashes();
180 int count(
wchar_t ch,
int start=0)
const;
183 int count(
const String& str,
int start=0)
const;
189 int compare(
const String& other)
const;
192 bool endsWith(
const String& str)
const;
195 bool startsWith(
const String& str)
const;
198 bool endsWith(
wchar_t ch)
const;
201 bool startsWith(
wchar_t ch)
const;
207 String& insert(
int pos,
wchar_t ch,
int count=1);
210 String left(
int count)
const;
213 String right(
int count)
const;
216 String extractPath()
const;
219 String extractFileName()
const;
222 String extractFileExtension(
bool require_dot=
true)
const;
225 String& resize(
int character_count);
228 String substring(
int start,
int count=-1)
const;
243 void split(
wchar_t separator, std::vector<String>& fields,
bool remove_empty_fields=
false)
const;
246 void split(
const String& separator_list, std::vector<String>& fields,
bool remove_empty_fields=
false)
const;
249 void splitLines(std::vector<String>& lines)
const;
252 String field(
wchar_t separator,
int field_index)
const;
258 String& append(
wchar_t ch,
int count=1);
264 String& prepend(
wchar_t ch,
int count);
267 int find(
wchar_t ch,
int start=0)
const;
270 int find(
const String& substr,
int start=0)
const;
274 int findInLargeText(
const String& substr,
int start=0)
const;
277 int findBackwards(
wchar_t ch)
const;
280 int findBackwards(
const String& str)
const;
283 bool contains(
wchar_t ch)
const;
286 bool contains(
const String& str)
const;
294 static String fromPointer(
const void* value);
297 static String fromInt(
int value);
300 static String fromUInt(
unsigned int value);
303 static String fromLongLong(
long long value);
306 static String fromULongLong(
unsigned long long value);
310 static String fromDouble(
double value,
int decimals=6);
313 static String fromStdString(
const std::string& str,
bool utf8=
true);
316 static String fromStdWString(
const std::wstring& str);
319 static String fromAscii(
const char* str,
int size=-1);
324 static String fromUTF16BE(
const unsigned short* str,
int byte_count=-1);
329 static String fromUTF16LE(
const unsigned short* str,
int byte_count=-1);
333 static String fromUTF16(
const unsigned short* str,
int byte_count=-1);
336 static String fromUTF8(
const char* str,
int byte_count=-1);
339 static String fromLatin1(
const char* str,
int character_count=-1);
342 int toInt(
bool hex=
false)
const;
345 double toDouble()
const;
348 float toFloat()
const {
return (
float)toDouble(); }
351 static String printf(
const char* fmt, ...);
354 std::string toStdString()
const;
357 std::wstring toStdWString()
const;
360 void toAscii(std::string& ascii,
bool translate_non_ascii_chars=
true)
const;
363 void toUTF8(std::vector<unsigned char>& utf8,
bool include_utf8_signature=
true)
const;
366 void toUTF8(std::string& utf8,
bool include_utf8_signature=
true)
const;
369 void toUTF16BE(std::vector<unsigned char>& utf16,
bool include_utf16be_signature=
true)
const;
372 void toUTF16LE(std::vector<unsigned char>& utf16,
bool include_utf16le_signature=
true)
const;
375 void toLatin1(std::vector<unsigned char>& latin1)
const;
378 String toLowerCase()
const;
381 String toUpperCase()
const;
386 return compare(other) < 0;
392 *
this = fromUTF8(str);
399 *
this = fromUTF8(str.c_str());
410 for(
int i=0; wstr[i]; ++i)
411 mString->push_back(wstr[i]);
418 return operator=(str.c_str());
421 #if VL_STRING_COPY_ON_WRITE == 0 425 mString =
new StringData(*other.mString);
434 if ( empty() && other.
empty() )
436 if ( empty() && !other.
empty() )
438 if ( !empty() && other.
empty() )
443 if (mString == other.mString)
448 return memcmp( ptr(), other.
ptr(),
sizeof(wchar_t)*
length() ) == 0;
458 if ( (
int)other.length() ==
length() )
460 for(
int i=0; i<
length(); ++i)
461 if ((*mString)[i] != (wchar_t)other[i])
473 if ( (
int)other.length() ==
length() )
475 for(
int i=0; i<
length(); ++i)
476 if ((*mString)[i] != other[i])
489 for(; other[i] && i<
length(); ++i)
490 if ( (*mString)[i] != (wchar_t)other[i] )
492 return i ==
length() && other[i] == 0;
500 for(; other[i] && i<
length(); ++i)
501 if ( (*mString)[i] != other[i] )
503 return i ==
length() && other[i] == 0;
534 mString->push_back(ch);
547 return append(other);
562 if (mString->referenceCount() > 1)
563 mString =
new StringData(*mString);
567 void createData()
const {
if (!mString) mString =
new StringData; }
570 class StringData:
public Object 573 void clear() { mWString.clear(); }
574 void push_back(
wchar_t a) { mWString.push_back(a); }
575 const wchar_t& operator[](
int i)
const {
return mWString[i]; }
576 wchar_t& operator[](
int i) {
return mWString[i]; }
577 int length()
const {
return (
int)mWString.length(); }
578 void resize(
int size) { mWString.
resize(size); }
581 std::wstring new_string = mWString;
582 mWString.swap( new_string );
585 std::wstring mWString;
593 return String(pstr) + str;
598 return String(pstr) + str;
603 return String(ch,1) + str;
608 return String(ch,1) + str;
bool operator==(const String &other) const
static String loadText(const char *path, EStringEncoding encoding=VL_PLATFORM_DEFAULT_ENCODING)
Loads a String from the specified path.
float operator+(float a, const half &b)
An abstract class representing a file.
String(const std::string &str)
Constructor.
bool operator<(const String &other) const
Lexicographic sorting, equivalent to "compare(other) < 0".
bool operator!=(const String &other) const
String & resize(int character_count)
Resizes the string to the specified character count.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
String & operator=(const wchar_t *wstr)
wchar_t & operator[](int i)
Returns the character at position i.
String & operator=(const std::wstring &str)
String operator+(const String &other) const
bool operator==(const char *other) const
String & operator=(const char *str)
Equivalent to '*this = fromUTF8(str);'.
bool null() const
Returns true if the String has never been assigned or resized.
String & operator+=(const String &other)
const wchar_t * ptr() const
Returns the 0-terminated utf16 string.
String(const std::wstring &str)
Constructor.
bool operator!=(const char *other) const
Visualization Library main namespace.
int length() const
Returns the length of the string.
String & operator=(const String &other)
The base class for all the reference counted objects.
float toFloat() const
Returns the float number represented by the string. The conversion is done using the standard atof() ...
String & operator+=(wchar_t ch)
void acquireData() const
Acquires a private copy of the data if the string has been copied from another one.
String operator+(wchar_t ch) const
String & append(const String &other)
Appends the specified String to another String.
bool empty() const
Returns true if length() == 0.
bool operator==(const std::wstring &other) const
#define VL_PLATFORM_DEFAULT_ENCODING
bool operator==(const wchar_t *other) const
bool operator!=(const wchar_t *other) const
bool operator==(const std::string &other) const
wchar_t * ptr()
Returns the 0-terminated utf16 string.
bool operator==(const ref< T1 > &o1, const ref< T2 > &o2)
static wchar_t platformSlash()
Returns '\' under windows and '/' under Linux and Mac.
const wchar_t & operator[](int i) const
Returns the character at position i.
Visualization Library's enums in the 'vl' namespace.
String & operator=(const std::string &str)
Equivalent to '*this = fromUTF8(str.c_str());'.
String & clear()
Clears the string.
bool operator!=(const std::wstring &other) const
bool operator!=(const std::string &other) const