|
||||
File indexing completed on 2025-01-18 10:05:18
0001 // Created on: 1993-02-22 0002 // Created by: Mireille MERCIEN 0003 // Copyright (c) 1993-1999 Matra Datavision 0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS 0005 // 0006 // This file is part of Open CASCADE Technology software library. 0007 // 0008 // This library is free software; you can redistribute it and/or modify it under 0009 // the terms of the GNU Lesser General Public License version 2.1 as published 0010 // by the Free Software Foundation, with special exception defined in the file 0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 0012 // distribution for complete text of the license and disclaimer of any warranty. 0013 // 0014 // Alternatively, this file may be used under the terms of Open CASCADE 0015 // commercial license or contractual agreement. 0016 0017 #ifndef _TCollection_ExtendedString_HeaderFile 0018 #define _TCollection_ExtendedString_HeaderFile 0019 0020 #include <Standard.hxx> 0021 #include <Standard_DefineAlloc.hxx> 0022 #include <Standard_Handle.hxx> 0023 0024 #include <Standard_PExtCharacter.hxx> 0025 #include <Standard_Integer.hxx> 0026 #include <Standard_CString.hxx> 0027 #include <Standard_Boolean.hxx> 0028 #include <Standard_Character.hxx> 0029 #include <Standard_ExtCharacter.hxx> 0030 #include <Standard_Real.hxx> 0031 #include <Standard_OStream.hxx> 0032 #include <Standard_PCharacter.hxx> 0033 #include <Standard_Macro.hxx> 0034 class TCollection_AsciiString; 0035 0036 0037 //! A variable-length sequence of "extended" (UNICODE) characters (16-bit character type). 0038 //! It provides editing operations with built-in memory management 0039 //! to make ExtendedString objects easier to use than ordinary extended character arrays. 0040 //! ExtendedString objects follow "value semantics", that is, they are the actual strings, 0041 //! not handles to strings, and are copied through assignment. 0042 //! You may use HExtendedString objects to get handles to strings. 0043 //! 0044 //! Beware that class can transparently store UTF-16 string with surrogate pairs 0045 //! (Unicode symbol represented by two 16-bit code units). 0046 //! However, surrogate pairs are not considered by the following methods: 0047 //! - Method ::Length() return the number of 16-bit code units, not the number of Unicode symbols. 0048 //! - Methods taking/returning symbol index work with 16-bit code units, not true Unicode symbols, 0049 //! including ::Remove(), ::SetValue(), ::Value(), ::Search(), ::Trunc() and others. 0050 //! If application needs to process surrogate pairs, NCollection_Utf16Iter class can be used 0051 //! for iterating through Unicode string (UTF-32 code unit will be returned for each position). 0052 class TCollection_ExtendedString 0053 { 0054 public: 0055 0056 DEFINE_STANDARD_ALLOC 0057 0058 0059 //! Initializes a ExtendedString to an empty ExtendedString. 0060 Standard_EXPORT TCollection_ExtendedString(); 0061 0062 //! Creation by converting a CString to an extended 0063 //! string. If <isMultiByte> is true then the string is 0064 //! treated as having UTF-8 coding. If it is not a UTF-8 0065 //! then <isMultiByte> is ignored and each character is 0066 //! copied to ExtCharacter. 0067 Standard_EXPORT TCollection_ExtendedString(const Standard_CString astring, const Standard_Boolean isMultiByte = Standard_False); 0068 0069 //! Creation by converting an ExtString to an extended string. 0070 Standard_EXPORT TCollection_ExtendedString(const Standard_ExtString astring); 0071 0072 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 0073 //! Initialize from wide-char string considering it as Unicode string 0074 //! (the size of wide char is a platform-dependent - e.g. on Windows wchar_t is UTF-16). 0075 //! 0076 //! This constructor is unavailable if application is built with deprecated msvc option "-Zc:wchar_t-", 0077 //! since OCCT itself is never built with this option. 0078 Standard_EXPORT TCollection_ExtendedString (const Standard_WideChar* theStringUtf); 0079 #endif 0080 0081 //! Initializes a AsciiString with a single character. 0082 Standard_EXPORT TCollection_ExtendedString(const Standard_Character aChar); 0083 0084 //! Initializes a ExtendedString with a single character. 0085 Standard_EXPORT TCollection_ExtendedString(const Standard_ExtCharacter aChar); 0086 0087 //! Initializes a ExtendedString with <length> space allocated. 0088 //! and filled with <filler>.This is useful for buffers. 0089 Standard_EXPORT TCollection_ExtendedString(const Standard_Integer length, const Standard_ExtCharacter filler); 0090 0091 //! Initializes an ExtendedString with an integer value 0092 Standard_EXPORT TCollection_ExtendedString(const Standard_Integer value); 0093 0094 //! Initializes an ExtendedString with a real value 0095 Standard_EXPORT TCollection_ExtendedString(const Standard_Real value); 0096 0097 //! Initializes a ExtendedString with another ExtendedString. 0098 Standard_EXPORT TCollection_ExtendedString(const TCollection_ExtendedString& astring); 0099 0100 //! Move constructor 0101 Standard_EXPORT TCollection_ExtendedString (TCollection_ExtendedString&& theOther) Standard_Noexcept; 0102 0103 //! Creation by converting an Ascii string to an extended 0104 //! string. The string is treated as having UTF-8 coding. 0105 //! If it is not a UTF-8 or multi byte then 0106 //! each character is copied to ExtCharacter. 0107 Standard_EXPORT TCollection_ExtendedString(const TCollection_AsciiString& astring, const Standard_Boolean isMultiByte = Standard_True); 0108 0109 //! Appends the other extended string to this extended string. 0110 //! Note that this method is an alias of operator +=. 0111 //! Example: aString += anotherString 0112 Standard_EXPORT void AssignCat (const TCollection_ExtendedString& other); 0113 void operator += (const TCollection_ExtendedString& other) 0114 { 0115 AssignCat(other); 0116 } 0117 0118 //! Appends the utf16 char to this extended string. 0119 Standard_EXPORT void AssignCat (const Standard_Utf16Char theChar); 0120 0121 //! Appends <other> to me. 0122 Standard_EXPORT TCollection_ExtendedString Cat (const TCollection_ExtendedString& other) const; 0123 TCollection_ExtendedString operator + (const TCollection_ExtendedString& other) const 0124 { 0125 return Cat(other); 0126 } 0127 0128 //! Substitutes all the characters equal to aChar by NewChar 0129 //! in the ExtendedString <me>. 0130 //! The substitution can be case sensitive. 0131 //! If you don't use default case sensitive, no matter whether aChar is uppercase or not. 0132 Standard_EXPORT void ChangeAll (const Standard_ExtCharacter aChar, const Standard_ExtCharacter NewChar); 0133 0134 //! Removes all characters contained in <me>. 0135 //! This produces an empty ExtendedString. 0136 Standard_EXPORT void Clear(); 0137 0138 //! Copy <fromwhere> to <me>. 0139 //! Used as operator = 0140 Standard_EXPORT void Copy (const TCollection_ExtendedString& fromwhere); 0141 0142 //! Copy assignment operator 0143 TCollection_ExtendedString& operator= (const TCollection_ExtendedString& theOther) 0144 { 0145 Copy(theOther); 0146 return *this; 0147 } 0148 0149 //! Moves string without reallocations 0150 Standard_EXPORT void Move (TCollection_ExtendedString&& theOther); 0151 0152 //! Move assignment operator 0153 TCollection_ExtendedString& operator= (TCollection_ExtendedString&& theOther) noexcept 0154 { 0155 Move(std::forward<TCollection_ExtendedString>(theOther)); 0156 return *this; 0157 } 0158 0159 //! Exchange the data of two strings (without reallocating memory). 0160 Standard_EXPORT void Swap (TCollection_ExtendedString& theOther); 0161 0162 //! Frees memory allocated by ExtendedString. 0163 Standard_EXPORT ~TCollection_ExtendedString(); 0164 0165 //! Insert a Character at position <where>. 0166 Standard_EXPORT void Insert (const Standard_Integer where, const Standard_ExtCharacter what); 0167 0168 //! Insert a ExtendedString at position <where>. 0169 Standard_EXPORT void Insert (const Standard_Integer where, const TCollection_ExtendedString& what); 0170 0171 //! Returns True if this string contains no characters. 0172 Standard_Boolean IsEmpty() const { return mylength == 0; } 0173 0174 //! Returns true if the characters in this extended 0175 //! string are identical to the characters in the other extended string. 0176 //! Note that this method is an alias of operator == 0177 Standard_EXPORT Standard_Boolean IsEqual (const Standard_ExtString other) const; 0178 Standard_Boolean operator == (const Standard_ExtString other) const 0179 { 0180 return IsEqual(other); 0181 } 0182 0183 //! Returns true if the characters in this extended 0184 //! string are identical to the characters in the other extended string. 0185 //! Note that this method is an alias of operator == 0186 Standard_EXPORT Standard_Boolean IsEqual (const TCollection_ExtendedString& other) const; 0187 Standard_Boolean operator == (const TCollection_ExtendedString& other) const 0188 { 0189 return IsEqual(other); 0190 } 0191 0192 //! Returns true if there are differences between the 0193 //! characters in this extended string and the other extended string. 0194 //! Note that this method is an alias of operator !=. 0195 Standard_EXPORT Standard_Boolean IsDifferent (const Standard_ExtString other) const; 0196 Standard_Boolean operator != (const Standard_ExtString other) const 0197 { 0198 return IsDifferent(other); 0199 } 0200 0201 //! Returns true if there are differences between the 0202 //! characters in this extended string and the other extended string. 0203 //! Note that this method is an alias of operator !=. 0204 Standard_EXPORT Standard_Boolean IsDifferent (const TCollection_ExtendedString& other) const; 0205 Standard_Boolean operator != (const TCollection_ExtendedString& other) const 0206 { 0207 return IsDifferent(other); 0208 } 0209 0210 //! Returns TRUE if <me> is less than <other>. 0211 Standard_EXPORT Standard_Boolean IsLess (const Standard_ExtString other) const; 0212 Standard_Boolean operator < (const Standard_ExtString other) const 0213 { 0214 return IsLess(other); 0215 } 0216 0217 //! Returns TRUE if <me> is less than <other>. 0218 Standard_EXPORT Standard_Boolean IsLess (const TCollection_ExtendedString& other) const; 0219 Standard_Boolean operator < (const TCollection_ExtendedString& other) const 0220 { 0221 return IsLess(other); 0222 } 0223 0224 //! Returns TRUE if <me> is greater than <other>. 0225 Standard_EXPORT Standard_Boolean IsGreater (const Standard_ExtString other) const; 0226 Standard_Boolean operator > (const Standard_ExtString other) const 0227 { 0228 return IsGreater(other); 0229 } 0230 0231 //! Returns TRUE if <me> is greater than <other>. 0232 Standard_EXPORT Standard_Boolean IsGreater (const TCollection_ExtendedString& other) const; 0233 Standard_Boolean operator > (const TCollection_ExtendedString& other) const 0234 { 0235 return IsGreater(other); 0236 } 0237 0238 //! Determines whether the beginning of this string instance matches the specified string. 0239 Standard_EXPORT Standard_Boolean StartsWith (const TCollection_ExtendedString& theStartString) const; 0240 0241 //! Determines whether the end of this string instance matches the specified string. 0242 Standard_EXPORT Standard_Boolean EndsWith (const TCollection_ExtendedString& theEndString) const; 0243 0244 //! Returns True if the ExtendedString contains only 0245 //! "Ascii Range" characters . 0246 Standard_EXPORT Standard_Boolean IsAscii() const; 0247 0248 //! Returns the number of 16-bit code units 0249 //! (might be greater than number of Unicode symbols if string contains surrogate pairs). 0250 Standard_EXPORT Standard_Integer Length() const; 0251 0252 //! Displays <me> . 0253 Standard_EXPORT void Print (Standard_OStream& astream) const; 0254 friend Standard_EXPORT Standard_OStream& operator << (Standard_OStream& astream,const TCollection_ExtendedString& astring); 0255 0256 //! Removes every <what> characters from <me>. 0257 Standard_EXPORT void RemoveAll (const Standard_ExtCharacter what); 0258 0259 //! Erases <ahowmany> characters from position <where>,<where> included. 0260 Standard_EXPORT void Remove (const Standard_Integer where, const Standard_Integer ahowmany = 1); 0261 0262 //! Searches a ExtendedString in <me> from the beginning 0263 //! and returns position of first item <what> matching. 0264 //! it returns -1 if not found. 0265 Standard_EXPORT Standard_Integer Search (const TCollection_ExtendedString& what) const; 0266 0267 //! Searches a ExtendedString in another ExtendedString from the 0268 //! end and returns position of first item <what> matching. 0269 //! it returns -1 if not found. 0270 Standard_EXPORT Standard_Integer SearchFromEnd (const TCollection_ExtendedString& what) const; 0271 0272 //! Replaces one character in the ExtendedString at position <where>. 0273 //! If <where> is less than zero or greater than the length of <me> 0274 //! an exception is raised. 0275 Standard_EXPORT void SetValue (const Standard_Integer where, const Standard_ExtCharacter what); 0276 0277 //! Replaces a part of <me> by another ExtendedString see above. 0278 Standard_EXPORT void SetValue (const Standard_Integer where, const TCollection_ExtendedString& what); 0279 0280 //! Splits this extended string into two sub-strings at position where. 0281 //! - The second sub-string (from position 0282 //! where + 1 of this string to the end) is 0283 //! returned in a new extended string. 0284 //! - this extended string is modified: its last 0285 //! characters are removed, it becomes equal to 0286 //! the first sub-string (from the first character to position where). 0287 //! Example: 0288 //! aString contains "abcdefg" 0289 //! aString.Split(3) gives <me> = "abc" and returns "defg" 0290 Standard_EXPORT TCollection_ExtendedString Split (const Standard_Integer where); 0291 0292 //! Extracts <whichone> token from <me>. 0293 //! By default, the <separators> is set to space and tabulation. 0294 //! By default, the token extracted is the first one (whichone = 1). 0295 //! <separators> contains all separators you need. 0296 //! If no token indexed by <whichone> is found, it returns an empty AsciiString. 0297 //! Example: 0298 //! aString contains "This is a message" 0299 //! aString.Token() returns "This" 0300 //! aString.Token(" ",4) returns "message" 0301 //! aString.Token(" ",2) returns "is" 0302 //! aString.Token(" ",9) returns "" 0303 //! Other separators than space character and tabulation are allowed : 0304 //! aString contains "1234; test:message , value" 0305 //! aString.Token("; :,",4) returns "value" 0306 //! aString.Token("; :,",2) returns "test" 0307 Standard_EXPORT TCollection_ExtendedString Token (const Standard_ExtString separators, const Standard_Integer whichone = 1) const; 0308 0309 //! Returns pointer to ExtString 0310 Standard_EXPORT Standard_ExtString ToExtString() const; 0311 0312 #ifdef _WIN32 0313 //! Returns pointer to string as wchar_t* on Windows platform where wchar_t* is considered as UTF-16 string. 0314 //! This method is useful to pass string into wide-char system APIs, 0315 //! and makes sense only on Windows (other systems use UTF-8 and can miss wide-char functions at all). 0316 const Standard_WideChar* ToWideString() const { return (const Standard_WideChar*)ToExtString(); } 0317 #endif 0318 0319 //! Truncates <me> to <ahowmany> characters. 0320 //! Example: me = "Hello Dolly" -> Trunc(3) -> me = "Hel" 0321 //! Exceptions 0322 //! Standard_OutOfRange if ahowmany is greater 0323 //! than the length of this string. 0324 Standard_EXPORT void Trunc (const Standard_Integer ahowmany); 0325 0326 //! Returns character at position <where> in <me>. 0327 //! If <where> is less than zero or greater than the length of 0328 //! <me>, an exception is raised. 0329 //! Example: 0330 //! aString contains "Hello" 0331 //! aString.Value(2) returns 'e' 0332 //! Exceptions 0333 //! Standard_OutOfRange if where lies outside 0334 //! the bounds of this extended string. 0335 Standard_EXPORT Standard_ExtCharacter Value (const Standard_Integer where) const; 0336 0337 //! Returns a hashed value for the extended string. 0338 //! Note: if string is ASCII, the computed value is the same as the value computed with the HashCode function on a 0339 //! TCollection_AsciiString string composed with equivalent ASCII characters. 0340 //! @return a computed hash code 0341 size_t HashCode () const 0342 { 0343 const int aSize = mylength * sizeof(Standard_ExtCharacter); 0344 if (mylength < 2) 0345 { 0346 return opencascade::FNVHash::hash_combine(*mystring, aSize); 0347 } 0348 return opencascade::hashBytes(mystring, aSize); 0349 } 0350 0351 //! Returns true if the characters in this extended 0352 //! string are identical to the characters in the other extended string. 0353 //! Note that this method is an alias of operator ==. 0354 static Standard_Boolean IsEqual (const TCollection_ExtendedString& theString1, 0355 const TCollection_ExtendedString& theString2) 0356 { 0357 return theString1.IsEqual (theString2); 0358 } 0359 0360 //! Converts the internal <mystring> to UTF8 coding and 0361 //! returns length of the out CString. A memory for the 0362 //! <theCString> should be allocated before call! 0363 Standard_EXPORT Standard_Integer ToUTF8CString (Standard_PCharacter& theCString) const; 0364 0365 //! Returns expected CString length in UTF8 coding. 0366 //! It can be used for memory calculation before converting 0367 //! to CString containing symbols in UTF8 coding. 0368 Standard_EXPORT Standard_Integer LengthOfCString() const; 0369 0370 private: 0371 0372 //! Returns true if the input CString was successfully converted to UTF8 coding. 0373 Standard_Boolean ConvertToUnicode (const Standard_CString astring); 0374 0375 //! Internal wrapper to allocate on stack or heap 0376 void allocate(const int theLength); 0377 0378 //! Internal wrapper to reallocate on stack or heap 0379 void reallocate(const int theLength); 0380 0381 //! Internal wrapper to deallocate on stack 0382 void deallocate(); 0383 0384 private: 0385 0386 Standard_PExtCharacter mystring{}; //!< NULL-terminated string 0387 Standard_Integer mylength{}; //!< length in 16-bit code units (excluding terminating NULL symbol) 0388 }; 0389 0390 namespace std 0391 { 0392 template <> 0393 struct hash<TCollection_ExtendedString> 0394 { 0395 size_t operator()(const TCollection_ExtendedString& theString) const 0396 { 0397 return theString.HashCode(); 0398 } 0399 }; 0400 } 0401 0402 #endif // _TCollection_ExtendedString_HeaderFile
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |