Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
String.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef String_INCLUDE_ONCE
33 #define String_INCLUDE_ONCE
34 
35 #include <vlCore/vlnamespace.hpp>
36 #include <vlCore/Object.hpp>
37 #include <vector>
38 #include <string.h>
39 
40 #if defined(VL_PLATFORM_WINDOWS)
41  #define VL_PLATFORM_DEFAULT_ENCODING SE_LATIN1
42 #else
43  #define VL_PLATFORM_DEFAULT_ENCODING SE_UTF8
44 #endif
45 
46 namespace vl
47 {
48  class VirtualFile;
49 
63  {
64  public:
66  static EStringEncoding detectEncoding(const void* str, int byte_count, EStringEncoding encoding = VL_PLATFORM_DEFAULT_ENCODING);
67 
69  static String loadText(const String& path, EStringEncoding encoding = VL_PLATFORM_DEFAULT_ENCODING);
70 
72  static String loadText(const char* path, EStringEncoding encoding = VL_PLATFORM_DEFAULT_ENCODING) { return loadText(String(path),encoding); }
73 
75  static String loadText(VirtualFile* file, EStringEncoding encoding = VL_PLATFORM_DEFAULT_ENCODING);
76 
78  static String loadText(void* data, int bytes, EStringEncoding encoding = VL_PLATFORM_DEFAULT_ENCODING);
79 
81  static unsigned short getUpperCase(unsigned short ch);
82 
84  static unsigned short getLowerCase(unsigned short ch);
85 
87  static unsigned short getTitleCase(unsigned short ch);
88 
90  static void filterStrings(std::vector<String>& strings, const String& filter);
91 
93  static wchar_t platformSlash()
94  {
95  #if defined(VL_PLATFORM_WINDOWS)
96  return '\\';
97  #else
98  return '/';
99  #endif
100  }
101 
103  static std::string trimStdString(const std::string& text);
104 
106  String();
107 
109  String(const String& other);
110 
112  String(const wchar_t* wstr);
113 
115  String(const char* str);
116 
118  String(const std::string& str) { *this = fromStdString(str, true); }
119 
121  String(const std::wstring& str) { *this = fromStdWString(str); }
122 
124  explicit String(wchar_t ch, int count=1);
125 
127  const wchar_t* ptr() const { createData(); return &(*mString)[0]; }
128 
130  wchar_t* ptr() { acquireData(); return &(*mString)[0]; }
131 
133  int length() const { if (!mString) return 0; createData(); return (int)mString->length(); };
134 
136  bool empty() const { return length() == 0; }
137 
139  bool null() const { return !mString; }
140 
142  String& clear() { acquireData(); mString->clear(); return *this; }
143 
145  const wchar_t& operator[](int i) const { createData(); return (*mString)[i]; }
146 
148  wchar_t& operator[](int i) { acquireData(); return (*mString)[i]; }
149 
151  String& replace( wchar_t old_ch, wchar_t new_ch );
152 
154  String& replace( int start, int count, wchar_t ch );
155 
157  String& replace( int start, int count, const String& str );
158 
160  String& replace( const String& oldstr, const String& newstr, bool case_sensitive=true );
161 
164  String& remove(wchar_t ch, int start=0, int count=-1);
165 
168  String& remove(const String& str, int start=0, int count=-1);
169 
171  String& remove( int start, int count );
172 
174  String& reverse();
175 
177  String& normalizeSlashes();
178 
180  int count(wchar_t ch, int start=0) const;
181 
183  int count(const String& str, int start=0) const;
184 
189  int compare(const String& other) const;
190 
192  bool endsWith(const String& str) const;
193 
195  bool startsWith(const String& str) const;
196 
198  bool endsWith(wchar_t ch) const;
199 
201  bool startsWith(wchar_t ch) const;
202 
204  String& insert(int pos, const String& str);
205 
207  String& insert(int pos, wchar_t ch, int count=1);
208 
210  String left(int count) const;
211 
213  String right(int count) const;
214 
216  String extractPath() const;
217 
219  String extractFileName() const;
220 
222  String extractFileExtension(bool require_dot=true) const;
223 
225  String& resize(int character_count);
226 
228  String substring(int start, int count=-1) const;
229 
231  String& fill(wchar_t ch);
232 
234  String& trim(wchar_t ch);
235 
237  String& trim(const String& chars);
238 
240  String& trim();
241 
243  void split(wchar_t separator, std::vector<String>& fields, bool remove_empty_fields=false) const;
244 
246  void split(const String& separator_list, std::vector<String>& fields, bool remove_empty_fields=false) const;
247 
249  void splitLines(std::vector<String>& lines) const;
250 
252  String field(wchar_t separator, int field_index) const;
253 
255  String& append(const String& other);
256 
258  String& append(wchar_t ch, int count=1);
259 
261  String& prepend(const String& str);
262 
264  String& prepend(wchar_t ch, int count);
265 
267  int find(wchar_t ch, int start=0) const;
268 
270  int find(const String& substr, int start=0) const;
271 
274  int findInLargeText(const String& substr, int start=0) const;
275 
277  int findBackwards(wchar_t ch) const;
278 
280  int findBackwards(const String& str) const;
281 
283  bool contains(wchar_t ch) const;
284 
286  bool contains(const String& str) const;
287 
289  void squeeze();
290 
291  // transcoding routines
292 
294  static String fromPointer(const void* value);
295 
297  static String fromInt(int value);
298 
300  static String fromUInt(unsigned int value);
301 
303  static String fromLongLong(long long value);
304 
306  static String fromULongLong(unsigned long long value);
307 
310  static String fromDouble(double value, int decimals=6);
311 
313  static String fromStdString(const std::string& str, bool utf8=true);
314 
316  static String fromStdWString(const std::wstring& str);
317 
319  static String fromAscii(const char* str, int size=-1);
320 
324  static String fromUTF16BE(const unsigned short* str, int byte_count=-1);
325 
329  static String fromUTF16LE(const unsigned short* str, int byte_count=-1);
330 
333  static String fromUTF16(const unsigned short* str, int byte_count=-1);
334 
336  static String fromUTF8(const char* str, int byte_count=-1);
337 
339  static String fromLatin1(const char* str, int character_count=-1);
340 
342  int toInt(bool hex=false) const;
343 
345  double toDouble() const;
346 
348  float toFloat() const { return (float)toDouble(); }
349 
351  static String printf(const char* fmt, ...);
352 
354  std::string toStdString() const;
355 
357  std::wstring toStdWString() const;
358 
360  void toAscii(std::string& ascii, bool translate_non_ascii_chars=true) const;
361 
363  void toUTF8(std::vector<unsigned char>& utf8, bool include_utf8_signature=true) const;
364 
366  void toUTF8(std::string& utf8, bool include_utf8_signature=true) const;
367 
369  void toUTF16BE(std::vector<unsigned char>& utf16, bool include_utf16be_signature=true) const;
370 
372  void toUTF16LE(std::vector<unsigned char>& utf16, bool include_utf16le_signature=true) const;
373 
375  void toLatin1(std::vector<unsigned char>& latin1) const;
376 
378  String toLowerCase() const;
379 
381  String toUpperCase() const;
382 
384  bool operator<(const String& other) const
385  {
386  return compare(other) < 0;
387  }
388 
390  String& operator=(const char* str)
391  {
392  *this = fromUTF8(str);
393  return *this;
394  }
395 
397  String& operator=(const std::string& str)
398  {
399  *this = fromUTF8(str.c_str());
400  return *this;
401  }
402 
403  String& operator=(const wchar_t* wstr)
404  {
405  acquireData();
406 
407  mString->clear();
408  if (wstr)
409  {
410  for(int i=0; wstr[i]; ++i)
411  mString->push_back(wstr[i]);
412  }
413  return *this;
414  }
415 
416  String& operator=(const std::wstring& str)
417  {
418  return operator=(str.c_str());
419  }
420 
421 #if VL_STRING_COPY_ON_WRITE == 0
422  String& operator=(const String& other)
423  {
424  other.acquireData();
425  mString = new StringData(*other.mString);
426  return *this;
427  }
428 #endif
429 
430  bool operator==(const String& other) const
431  {
432  if ( empty() && other.empty() )
433  return true;
434  if ( empty() && !other.empty() )
435  return false;
436  if ( !empty() && other.empty() )
437  return false;
438 
439  createData();
440 
441  if (mString == other.mString)
442  return true;
443  else
444  if ( other.length() == length() )
445  {
446  return memcmp( ptr(), other.ptr(), sizeof(wchar_t)*length() ) == 0;
447  }
448  else
449  return false;
450  }
451 
452  bool operator==(const std::string& other) const
453  {
454  createData();
455 
456  if ( (int)other.length() == length() )
457  {
458  for(int i=0; i<length(); ++i)
459  if ((*mString)[i] != (wchar_t)other[i])
460  return false;
461  return true;
462  }
463  else
464  return false;
465  }
466 
467  bool operator==(const std::wstring& other) const
468  {
469  createData();
470 
471  if ( (int)other.length() == length() )
472  {
473  for(int i=0; i<length(); ++i)
474  if ((*mString)[i] != other[i])
475  return false;
476  return true;
477  }
478  else
479  return false;
480  }
481 
482  bool operator==(const char* other) const
483  {
484  createData();
485 
486  int i=0;
487  for(; other[i] && i<length(); ++i)
488  if ( (*mString)[i] != (wchar_t)other[i] )
489  return false;
490  return i == length() && other[i] == 0;
491  }
492 
493  bool operator==(const wchar_t* other) const
494  {
495  createData();
496 
497  int i=0;
498  for(; other[i] && i<length(); ++i)
499  if ( (*mString)[i] != other[i] )
500  return false;
501  return i == length() && other[i] == 0;
502  }
503 
504  bool operator!=(const String& other) const
505  {
506  return !this->operator==(other);
507  }
508 
509  bool operator!=(const std::string& other) const
510  {
511  return !this->operator==(other);
512  }
513 
514  bool operator!=(const std::wstring& other) const
515  {
516  return !this->operator==(other);
517  }
518 
519  bool operator!=(const char* other) const
520  {
521  return !this->operator==(other);
522  }
523 
524  bool operator!=(const wchar_t* other) const
525  {
526  return !this->operator==(other);
527  }
528 
529  String& operator+=(wchar_t ch)
530  {
531  acquireData();
532  mString->push_back(ch);
533  return *this;
534  }
535 
536  String operator+(wchar_t ch) const
537  {
538  String tmp = *this;
539  tmp += ch;
540  return tmp;
541  }
542 
543  String& operator+=(const String& other)
544  {
545  return append(other);
546  }
547 
548  String operator+(const String& other) const
549  {
550  String tmp = *this;
551  tmp.append(other);
552  return tmp;
553  }
554 
556  void acquireData() const
557  {
558  createData();
559 
560  if (mString->referenceCount() > 1)
561  mString = new StringData(*mString);
562  }
563 
564  protected:
565  void createData() const { if (!mString) mString = new StringData; }
566 
567  private:
568  class StringData: public Object
569  {
570  public:
571  void clear() { mWString.clear(); }
572  void push_back(wchar_t a) { mWString.push_back(a); }
573  const wchar_t& operator[](int i) const { return mWString[i]; }
574  wchar_t& operator[](int i) { return mWString[i]; }
575  int length() const { return (int)mWString.length(); }
576  void resize(int size) { mWString.resize(size); }
577  void squeeze()
578  {
579  std::wstring new_string = mWString;
580  mWString.swap( new_string );
581  }
582  protected:
583  std::wstring mWString;
584  };
585 
586  mutable ref<StringData> mString;
587  };
588 //-----------------------------------------------------------------------------
589  inline String operator+(const wchar_t* pstr, const String& str)
590  {
591  return String(pstr) + str;
592  }
593 //-----------------------------------------------------------------------------
594  inline String operator+(const char* pstr, const String& str)
595  {
596  return String(pstr) + str;
597  }
598 //-----------------------------------------------------------------------------
599  inline String operator+(wchar_t ch, const String& str)
600  {
601  return String(ch,1) + str;
602  }
603 //-----------------------------------------------------------------------------
604  inline String operator+(char ch, const String& str)
605  {
606  return String(ch,1) + str;
607  }
608 //-----------------------------------------------------------------------------
609 }
610 
611 #endif
void createData() const
Definition: String.hpp:565
bool operator==(const String &other) const
Definition: String.hpp:430
static String loadText(const char *path, EStringEncoding encoding=VL_PLATFORM_DEFAULT_ENCODING)
Loads a String from the specified path.
Definition: String.hpp:72
float operator+(float a, const half &b)
Definition: half.hpp:510
An abstract class representing a file.
Definition: VirtualFile.hpp:60
String(const std::string &str)
Constructor.
Definition: String.hpp:118
bool operator<(const String &other) const
Lexicographic sorting, equivalent to "compare(other) < 0".
Definition: String.hpp:384
bool operator!=(const String &other) const
Definition: String.hpp:504
String & resize(int character_count)
Resizes the string to the specified character count.
Definition: String.cpp:146
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
String & operator=(const wchar_t *wstr)
Definition: String.hpp:403
wchar_t & operator[](int i)
Returns the character at position i.
Definition: String.hpp:148
String & operator=(const std::wstring &str)
Definition: String.hpp:416
String operator+(const String &other) const
Definition: String.hpp:548
bool operator==(const char *other) const
Definition: String.hpp:482
String & operator=(const char *str)
Equivalent to &#39;*this = fromUTF8(str);&#39;.
Definition: String.hpp:390
bool null() const
Returns true if the String has never been assigned or resized.
Definition: String.hpp:139
String & operator+=(const String &other)
Definition: String.hpp:543
EStringEncoding
const wchar_t * ptr() const
Returns the 0-terminated utf16 string.
Definition: String.hpp:127
String(const std::wstring &str)
Constructor.
Definition: String.hpp:121
bool operator!=(const char *other) const
Definition: String.hpp:519
Visualization Library main namespace.
int length() const
Returns the length of the string.
Definition: String.hpp:133
String & operator=(const String &other)
Definition: String.hpp:422
The base class for all the reference counted objects.
Definition: Object.hpp:158
float toFloat() const
Returns the float number represented by the string. The conversion is done using the standard atof() ...
Definition: String.hpp:348
String & operator+=(wchar_t ch)
Definition: String.hpp:529
void acquireData() const
Acquires a private copy of the data if the string has been copied from another one.
Definition: String.hpp:556
String operator+(wchar_t ch) const
Definition: String.hpp:536
String & append(const String &other)
Appends the specified String to another String.
Definition: String.cpp:597
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
bool operator==(const std::wstring &other) const
Definition: String.hpp:467
#define VL_PLATFORM_DEFAULT_ENCODING
Definition: String.hpp:43
bool operator==(const wchar_t *other) const
Definition: String.hpp:493
bool operator!=(const wchar_t *other) const
Definition: String.hpp:524
bool operator==(const std::string &other) const
Definition: String.hpp:452
wchar_t * ptr()
Returns the 0-terminated utf16 string.
Definition: String.hpp:130
bool operator==(const ref< T1 > &o1, const ref< T2 > &o2)
Definition: Object.hpp:144
T length(T v)
Definition: glsl_math.hpp:1084
static wchar_t platformSlash()
Returns &#39;\&#39; under windows and &#39;/&#39; under Linux and Mac.
Definition: String.hpp:93
const wchar_t & operator[](int i) const
Returns the character at position i.
Definition: String.hpp:145
Visualization Library&#39;s enums in the &#39;vl&#39; namespace.
String & operator=(const std::string &str)
Equivalent to &#39;*this = fromUTF8(str.c_str());&#39;.
Definition: String.hpp:397
String & clear()
Clears the string.
Definition: String.hpp:142
bool operator!=(const std::wstring &other) const
Definition: String.hpp:514
bool operator!=(const std::string &other) const
Definition: String.hpp:509