Open Inventor Release 2023.2.3
 
Loading...
Searching...
No Matches
SbString.h
Go to the documentation of this file.
1/*=======================================================================
2 * Copyright 1991-1996, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 *
5 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
6 * States. Use of a copyright notice is precautionary only and does not
7 * imply publication or disclosure.
8 *
9 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
10 * Use, duplication or disclosure by the Government is subject to restrictions
11 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
12 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
13 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
14 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc.,
15 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
16 *
17 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
18 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
19 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
20 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
21 * GRAPHICS, INC.
22**=======================================================================*/
23/*=======================================================================
24** Author : Paul S. Strauss (MMM yyyy)
25** Modified by : Nick Thompson (MMM yyyy)
26**=======================================================================*/
27/*=======================================================================
28 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
29 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
30 *** ***
31 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
32 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
33 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
34 *** ***
35 *** RESTRICTED RIGHTS LEGEND ***
36 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
37 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
38 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
39 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
40 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
41 *** ***
42 *** COPYRIGHT (C) 1996-2023 BY FEI S.A.S, ***
43 *** BORDEAUX, FRANCE ***
44 *** ALL RIGHTS RESERVED ***
45**=======================================================================*/
46/*=======================================================================
47** Modified by : VSG (MMM YYYY)
48**=======================================================================*/
49
50
51#ifndef _SB_STRING_
52#define _SB_STRING_
53
54#include <string.h>
55#include <wchar.h>
56#include <stdarg.h>
57
59#include <Inventor/SbBase.h>
60#include <Inventor/STL/string>
61#include <Inventor/STL/ostream>
62#include <Inventor/STL/vector>
63
64#ifdef _WIN32
65#pragma warning(push)
66#pragma warning(disable:4251)
67#endif
68
202class SbString {
203 public:
204
209
213 SbString(const std::string &std_string);
214
218 SbString(const std::wstring &std_wstring);
219
223 SbString(const char *str);
224
230 SbString(const char *str, int start, int end);
231
237 SbString(const wchar_t *str, int start, int end);
238
239
243 SbString(const SbString &str);
244
248 SbString( const wchar_t* str );
249
254
259 static const int npos;
260
266 SbBool isEmpty() const;
267
272 SbBool isNull() const;
273
277 int getLength() const;
278
284 void makeEmpty( const SbBool freeOld = TRUE );
285
289 void makeNull();
290
296 SbString getSubString(int startChar, int endChar = -1) const;
297
303 void deleteSubString(int startChar, int endChar = -1);
304
308 SbBool contains( const SbString& str ) const;
309
313 bool endsWith( const SbString& str ) const;
314
319 int find( const SbString& str ) const;
320
325 int rfind( const SbString& str ) const;
326
332 int findLast( const SbString& str ) const;
333
337 SbBool containsAny( const SbString& str ) const;
338
344 int findAny( const SbString& str ) const;
345
349 void replace( const SbString& patternStr, const SbString& substStr );
350
351 // Conversion functions
352
358
364
373 SbString& setNum( short value, int base = 10 );
374
378 SbString& setNum( unsigned short value, int base = 10 );
379
383 SbString& setNum( int value, int base = 10 );
384
388 SbString& setNum( unsigned int value, int base = 10 );
389
393 SbString& setNum( long value, int base = 10 );
394
398 SbString& setNum( unsigned long value, int base = 10 );
399
403 SbString& setNum( double value );
404
408 SbString& setNum( float value );
409
416 unsigned short toUShort( SbBool* ok = NULL ) const;
417
421 short toShort( SbBool* ok = NULL ) const;
422
426 unsigned int toUInt( SbBool* ok = NULL ) const;
427
431 int toInt( SbBool* ok = NULL ) const;
432
436 unsigned long toULong( SbBool* ok = NULL ) const;
437
441 long toLong( SbBool* ok = NULL ) const;
442
446 float toFloat( SbBool* ok = NULL ) const;
447
451 double toDouble( SbBool* ok = NULL ) const;
452
453
454 // Unicode conversion functions
455
456 // - Unicode managment functions
457
462
463 // - From
464
468 SbString& fromLatin1( const char* str );
469
473 SbString& fromUtf16( const unsigned short* str );
474
478 SbString& fromWideChar( const wchar_t* str );
479
480 // - To
481
485 const char* toLatin1() const;
486
490 unsigned short* toUtf16() const;
491
495 const wchar_t* toWideChar() const;
496
500 std::wstring toStdWString() const;
501
505 std::string toStdString() const;
506
507 // Operators
508
513
517 SbString &operator +=( const char *str );
518
523
527 SbString &operator =(const char *str);
528
529
533 friend const SbString operator+( const SbString &s1, const SbString &s2 );
534
540 friend int operator ==(const SbString &str1, const SbString &str2);
541
547 friend int operator ==(const SbString &str, const char *s);
548
554 friend int operator ==(const char *s, const SbString &str);
555
561 friend int operator !=(const SbString &str1, const SbString &str2);
562
568 friend bool operator < (const SbString &str1, const SbString &str2);
569
575 friend bool operator > (const SbString &str1, const SbString &str2);
576
582 friend bool operator <= (const SbString &str1, const SbString &str2);
583
589 friend bool operator >= (const SbString &str1, const SbString &str2);
590
594 friend std::ostream& operator << (std::ostream& os, const SbString& str)
595 {
596 if (str.isUnicode())
597 return os << str.toUtf8();
598
599 return os << str.toLatin1();
600 }
601
602
606 uint32_t hash() const;
607
608 // Deprecated functions
609
617 SoNONUNICODE SbString( const int digitString );
618
625 SoNONUNICODE const char *getString() const;
626
633 SoNONUNICODE const std::string &getSString() const;
634
641 SoNONUNICODE SbString & sprintf(const char * cformat, ... );
642
648 SoNONUNICODE SbString & vprintf(const char * cformat, va_list arg);
649
656 SoNONUNICODE friend const SbString operator+( const SbString &s1, const char *s2 );
657
664 SoNONUNICODE friend const SbString operator+( const char *s1, const SbString &s2 );
665
671 SoNONUNICODE char operator[]( int i ) const;
672
678 SoNONUNICODE int operator !() const;
679
687 SoNONUNICODE friend int operator ==(const SbString &str, const std::string &s);
688
696 SoNONUNICODE friend int operator ==(const std::string &s, const SbString &str);
697
705 SoNONUNICODE friend int operator !=(const SbString &str, const char *s);
706
714 SoNONUNICODE friend int operator !=(const char *s, const SbString &str);
715
723 SoNONUNICODE friend bool operator < (const SbString &str, const char *s);
724
732 SoNONUNICODE friend bool operator < (const char *s, const SbString &str);
733
741 SoNONUNICODE friend bool operator < (const SbString &str, const std::string &s);
742
750 SoNONUNICODE friend bool operator < (const std::string &s, const SbString &str);
751
759 SoNONUNICODE friend bool operator > (const SbString &str, const char *s);
760
768 SoNONUNICODE friend bool operator > (const char *s, const SbString &str);
769
777 SoNONUNICODE friend bool operator > (const SbString &str, const std::string &s);
778
786 SoNONUNICODE friend bool operator > (const std::string &s, const SbString &str);
787
795 SoNONUNICODE friend bool operator <= (const SbString &str, const char *s);
796
804 SoNONUNICODE friend bool operator <= (const char *s, const SbString &str);
805
813 SoNONUNICODE friend bool operator <= (const SbString &str, const std::string &s);
814
822 SoNONUNICODE friend bool operator <= (const std::string &s, const SbString &str);
823
831 SoNONUNICODE friend bool operator >= (const SbString &str, const char *s);
832
840 SoNONUNICODE friend bool operator >= (const char *s, const SbString &str);
841
849 SoNONUNICODE friend bool operator >= (const SbString &str, const std::string &s);
850
857 SoNONUNICODE friend bool operator >= (const std::string &s, const SbString &str);
858
860// Wide String
862
866 SoNONUNICODE const SO_WCHAR_T *wgetString() const;
867
871 SoNONUNICODE const std::wstring &wgetSString() const;
872
876 SoNONUNICODE SbString &operator =(const SO_WCHAR_T *str);
877
881 SoNONUNICODE SbString &operator +=(const SO_WCHAR_T *str);
882
888 SoNONUNICODE friend int operator ==(const SbString &str, const SO_WCHAR_T *s);
889
895 SoNONUNICODE friend int operator ==(const SO_WCHAR_T *s, const SbString &str);
896
902 SoNONUNICODE friend int operator ==(const SbString &str, const std::wstring &s);
903
909 SoNONUNICODE friend int operator ==(const std::wstring &s, const SbString &str);
910
916 SoNONUNICODE friend int operator !=(const SbString &str, const SO_WCHAR_T *s);
917
923 SoNONUNICODE friend int operator !=(const SO_WCHAR_T *s, const SbString &str);
924
930 SoNONUNICODE friend bool operator < (const SbString &str, const SO_WCHAR_T *s);
931
937 SoNONUNICODE friend bool operator < (const SO_WCHAR_T *s, const SbString &str);
938
944 SoNONUNICODE friend bool operator < (const SbString &str, const std::wstring &s);
945
951 SoNONUNICODE friend bool operator < (const std::wstring &s, const SbString &str);
952
958 SoNONUNICODE friend bool operator > (const SbString &str, const SO_WCHAR_T *s);
959
965 SoNONUNICODE friend bool operator > (const SO_WCHAR_T *s, const SbString &str);
966
972 SoNONUNICODE friend bool operator > (const SbString &str, const std::wstring &s);
973
979 SoNONUNICODE friend bool operator > (const std::wstring &s, const SbString &str);
980
986 SoNONUNICODE friend bool operator <= (const SbString &str, const SO_WCHAR_T *s);
987
993 SoNONUNICODE friend bool operator <= (const SO_WCHAR_T *s, const SbString &str);
994
1000 SoNONUNICODE friend bool operator <= (const SbString &str, const std::wstring &s);
1001
1007 SoNONUNICODE friend bool operator <= (const std::wstring &s, const SbString &str);
1008
1014 SoNONUNICODE friend bool operator >= (const SbString &str, const SO_WCHAR_T *s);
1015
1021 SoNONUNICODE friend bool operator >= (const SO_WCHAR_T *s, const SbString &str);
1022
1028 SoNONUNICODE friend bool operator >= (const SbString &str, const std::wstring &s);
1029
1035 SoNONUNICODE friend bool operator >= (const std::wstring &s, const SbString &str);
1036
1037private:
1038
1044 SbString& fromUtf8( const char* str );
1045
1046
1052 const char* toUtf8() const;
1053
1054 static uint32_t hash(const char *s); // Hash function
1055 static uint32_t hash(const SO_WCHAR_T *s); // Hash function
1056
1057 static inline bool isDigit(const int c);
1058 static inline bool isSpace(const int c);
1059
1061 void split(std::vector<SbString>& tokens) const;
1062
1069 void setCapacity( int nBytes );
1070
1072 const std::string& nonUnicodeToStdString() const { return m_cdata; }
1073
1074private:
1075
1076 static void initClass();
1077
1078 static void exitClass();
1079
1080#ifdef _WIN32
1081# define SB_LOCALE_DATA_TYPE _locale_t
1082# define SB_LOCALE_ALLOC_FUNC _create_locale
1083# define SB_LOCALE_FREE_FUNC _free_locale
1084# define SB_LOCALE_STRTOD_FUNC _strtod_l
1085# define SB_LOCALE_STRTOL_FUNC _strtol_l
1086# define SB_LOCALE_SPRINTF_FUNC _sprintf_l
1087# define SB_LOCALE_SNPRINTF_FUNC _snprintf_l
1088# define SB_LOCALE_SCANF_FUNC _scanf_l
1089# define SB_LOCALE_SSCANF_FUNC _sscanf_l
1090# define SB_LOCALE_FSCANF_FUNC _fscanf_l
1091# define SB_LOCALE_VPRINTF _vprintf_l
1092# define SB_LOCALE_FPRINTF_FUNC _fprintf_l
1093# define SB_LOCALE , (SB_LOCALE_DATA_TYPE)SbString::getLocaleObject()
1094#else
1095# define SB_LOCALE_DATA_TYPE locale_t
1096# define SB_LOCALE_ALLOC_FUNC newlocale
1097# define SB_LOCALE_FREE_FUNC freelocale
1098# define SB_LOCALE_STRTOD_FUNC strtod
1099# define SB_LOCALE_STRTOL_FUNC strtol
1100# define SB_LOCALE_SPRINTF_FUNC ::sprintf
1101# define SB_LOCALE_FPRINTF_FUNC fprintf
1102# define SB_LOCALE_SNPRINTF_FUNC snprintf
1103# define SB_LOCALE_SCANF_FUNC scanf
1104# define SB_LOCALE_SSCANF_FUNC sscanf
1105# define SB_LOCALE_FSCANF_FUNC fscanf
1106# define SB_LOCALE_VPRINTF vprintf
1107# define SB_LOCALE
1108#endif
1109
1110 static float toFloat_l( const char* str, SbBool* ok = NULL );
1111
1112 static double toDouble_l( const char* str, SbBool* ok = NULL );
1113
1114 static long toLong_l( const char* str, SbBool* ok = NULL );
1115
1116 static void toString_l( char* str, const char* format, float num );
1117
1118 static void toString_l( char* str, const char* format, double num );
1119
1120 static void* getLocaleObject();
1121
1122 //****************************************************************************************
1123 // Returns a new SbString from the input string, without any tailing blank char
1124 static SbString trimRight( const SbString & _str );
1125
1126 //****************************************************************************************
1127 // Returns a new SbString from the input string, without any leading blank char
1128 static SbString trimLeft( const SbString & _str );
1129
1130 //****************************************************************************************
1131 // Returns a new SbString from the input string, without any leading nor tailing blank char
1132 static SbString trim( const SbString & _str );
1133
1134private:
1135
1136 // For old functions
1137 static void buildLocaleCache();
1138 static bool localeCacheInitialized;
1139 static bool localeDigitCache[256];
1140 static bool localeSpaceCache[256];
1141 static char* s_tmpstr;
1142 static size_t s_tmpStrSize;
1143
1144 std::wstring m_data;
1145 std::string m_cdata;
1146
1147 mutable std::wstring m_tmpData;
1148 mutable std::string m_tmpCData;
1149
1150 bool m_isNull;
1151
1152
1153#if !defined(_WIN32)
1154 // On non win32 platforms wchar_t uses 4 bytes so we must keep
1155 // a utf16 buffer for the toUtf16 function.
1156 mutable unsigned short* m_utf16Buffer;
1157#endif
1158};
1159
1160
1161inline SbBool
1163{
1164 return (isNull() || (getLength() == 0));
1165}
1166
1167inline SbBool
1169{
1170 return m_isNull;
1171}
1172
1173inline bool SbString::isDigit(const int c)
1174{
1175 if ((c < 0) || (c>=256))
1176 return false;
1177
1178 if (!localeCacheInitialized)
1179 buildLocaleCache();
1180 return localeDigitCache[c];
1181}
1182
1183inline bool SbString::isSpace(const int c)
1184{
1185 if (!localeCacheInitialized)
1186 buildLocaleCache();
1187 return localeSpaceCache[c];
1188}
1189
1190#ifdef _WIN32
1191#pragma warning(pop)
1192#endif
1193
1194#include <Inventor/SbName.h>
1195
1196#endif // _SB_STRING_
1197
1198
#define TRUE
Possible value of SbBool.
Definition SbBase.h:77
#define SO_WCHAR_T
Definition SbTypes.h:60
void start()
Class for smart character strings.
Definition SbString.h:202
friend bool operator<=(const SbString &str1, const SbString &str2)
"less than or equal" relational operator for SbString Returns TRUE if the first operand is less tha...
uint32_t hash() const
Returns a reasonable hash key for string.
SoNONUNICODE friend const SbString operator+(const SbString &s1, const char *s2)
This is an overloaded member function, provided for convenience.
SoNONUNICODE const std::string & getSString() const
Returns pointer to the std::string.
SoNONUNICODE const SO_WCHAR_T * wgetString() const
Returns pointer to the character string.
int findAny(const SbString &str) const
Returns the position of the first occurence of any character contained in the specified sub string.
SoNONUNICODE SbString(const int digitString)
Constructor that takes an integer to be turned into a string.
SbBool isUnicode() const
Returns TRUE if the string contains Unicode characters.
int rfind(const SbString &str) const
Returns the position of the last occurence of the specified substring.
SbString(const SbString &str)
Constructor that takes an SbString.
unsigned long toULong(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
SbString & fromWideChar(const wchar_t *str)
Set the string from a wide char string.
bool endsWith(const SbString &str) const
Returns true if the string ends with the specified string.
unsigned int toUInt(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
short toShort(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
~SbString()
Destructor.
friend bool operator<(const SbString &str1, const SbString &str2)
"less than" relational operator for SbString Returns TRUE if the first operand is less than the sec...
SbBool isNull() const
Returns TRUE if the string is a null string.
Definition SbString.h:1168
SbString & setNum(long value, int base=10)
Sets the string to a formatted numeric value.
unsigned short * toUtf16() const
Returns the string as a UTF-16 string.
float toFloat(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
std::string toStdString() const
Returns the string as an STL string.
SbString(const std::string &std_string)
Constructor that takes an STL string.
SbString & setNum(float value)
Sets the string to a formatted numeric value.
SbString & setNum(unsigned long value, int base=10)
Sets the string to a formatted numeric value.
SoNONUNICODE friend const SbString operator+(const char *s1, const SbString &s2)
This is an overloaded member function, provided for convenience.
SbString & operator=(const SbString &str)
Assignment operator for SbString.
int find(const SbString &str) const
Returns the position of the first occurence of the specified substring.
friend int operator==(const SbString &str1, const SbString &str2)
Equality operator for SbString / SbString comparison.
SbString & setNum(unsigned short value, int base=10)
Sets the string to a formatted numeric value.
SoNONUNICODE const char * getString() const
Returns pointer to the character string.
long toLong(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
const wchar_t * toWideChar() const
Returns the string as a wide char string.
SoNONUNICODE const std::wstring & wgetSString() const
Returns pointer to the stl::wstring.
SoNONUNICODE char operator[](int i) const
Returns the character at index i, or 0 if i is beyond the length of the string.
SbString(const std::wstring &std_wstring)
Constructor that takes an STL wstring.
SbBool containsAny(const SbString &str) const
Returns TRUE if any of the characters listed in the str parameter exist in the string.
unsigned short toUShort(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
SbString()
Default constructor.
SbString & setNum(double value)
Sets the string to a formatted numeric value.
SbBool contains(const SbString &str) const
Returns TRUE if the string contains the specified substring.
void makeEmpty(const SbBool freeOld=TRUE)
Sets string to be the empty string ("").
SbString & setNum(unsigned int value, int base=10)
Sets the string to a formatted numeric value.
SbString upper() const
Returns an uppercase copy of the string.
SbString getSubString(int startChar, int endChar=-1) const
Returns a new string containing the sub-string from startChar (position) to endChar,...
SbString lower() const
Returns a lowercase copy of the string.
SoNONUNICODE SbString & sprintf(const char *cformat,...)
Builds a formatted string from the format string cformat and an arbitrary list of arguments.
int findLast(const SbString &str) const
Returns the position of the last occurence of any character contained in the specified sub string.
void makeNull()
Makes the current string null (same as uninitialized).
friend bool operator>(const SbString &str1, const SbString &str2)
"greater than" relational operator for SbString Returns TRUE if the first operand is greater than t...
friend std::ostream & operator<<(std::ostream &os, const SbString &str)
Writes the string to the specified output stream.
Definition SbString.h:594
SbString & setNum(int value, int base=10)
Sets the string to a formatted numeric value.
friend bool operator>=(const SbString &str1, const SbString &str2)
"greater than or equal" relational operator for SbString Returns TRUE if the first operand is great...
friend int operator!=(const SbString &str1, const SbString &str2)
Inequality operator for SbString / SbString comparison.
int toInt(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
void deleteSubString(int startChar, int endChar=-1)
Deletes the characters from startChar to endChar, inclusive, from the string.
SbString & fromLatin1(const char *str)
Set the string from a Latin-1/ASCII C string.
SbString(const wchar_t *str, int start, int end)
Constructor that takes the subset of a wide char string from start to end (inclusive).
SbString(const char *str)
Constructor that takes a character string.
SbString(const wchar_t *str)
Constructor that takes a wide character string.
std::wstring toStdWString() const
Returns the string as an STL wstring.
SbString & setNum(short value, int base=10)
Sets the string to a formatted numeric value.
const char * toLatin1() const
Returns the string as a Latin-1/ASCII C string.
SbBool isEmpty() const
Returns TRUE if the string is empty.
Definition SbString.h:1162
friend const SbString operator+(const SbString &s1, const SbString &s2)
Returns a string which is the result of concatenating the string s1 and the string s2.
static const int npos
This constant is used to indicate when a find string action failed.
Definition SbString.h:259
SoNONUNICODE SbString & vprintf(const char *cformat, va_list arg)
Write into string using vprintf() syntax.
int getLength() const
Returns length of string (zero if null or empty).
void replace(const SbString &patternStr, const SbString &substStr)
This function replaces all the occurence of patternStr by the new string substStr.
SbString(const char *str, int start, int end)
Constructor that takes the subset of a character string from start to end (inclusive).
double toDouble(SbBool *ok=NULL) const
Returns the numeric value represented by the string.
SbString & operator+=(const SbString &str)
Concatenation operator "+=" for SbString, SbString.
SoNONUNICODE int operator!() const
Unary "not" operator; returns TRUE if string is empty ("").
SbString & fromUtf16(const unsigned short *str)
Set the string from a UTF-16 string.
int SbBool
Boolean type.
Definition SbBase.h:87
iterator end()