Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/TCollection_ExtendedString.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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_Integer.hxx>
0025 #include <Standard_CString.hxx>
0026 #include <Standard_Boolean.hxx>
0027 #include <Standard_Character.hxx>
0028 #include <Standard_ExtCharacter.hxx>
0029 #include <Standard_Real.hxx>
0030 #include <Standard_OStream.hxx>
0031 #include <Standard_PCharacter.hxx>
0032 #include <Standard_Macro.hxx>
0033 
0034 #if Standard_CPP17_OR_HIGHER
0035   #include <string_view>
0036 #endif
0037 
0038 class TCollection_AsciiString;
0039 
0040 //! A variable-length sequence of "extended" (UNICODE) characters (16-bit character type).
0041 //! It provides editing operations with built-in memory management
0042 //! to make ExtendedString objects easier to use than ordinary extended character arrays.
0043 //! ExtendedString objects follow "value semantics", that is, they are the actual strings,
0044 //! not handles to strings, and are copied through assignment.
0045 //! You may use HExtendedString objects to get handles to strings.
0046 //!
0047 //! Beware that class can transparently store UTF-16 string with surrogate pairs
0048 //! (Unicode symbol represented by two 16-bit code units).
0049 //! However, surrogate pairs are not considered by the following methods:
0050 //! - Method ::Length() return the number of 16-bit code units, not the number of Unicode symbols.
0051 //! - Methods taking/returning symbol index work with 16-bit code units, not true Unicode symbols,
0052 //!   including ::Remove(), ::SetValue(), ::Value(), ::Search(), ::Trunc() and others.
0053 //! If application needs to process surrogate pairs, NCollection_UtfIterator<char16_t> class can be
0054 //! used for iterating through Unicode string (UTF-32 code unit will be returned for each position).
0055 class TCollection_ExtendedString
0056 {
0057 public:
0058   DEFINE_STANDARD_ALLOC
0059 
0060   //! Initializes an ExtendedString to an empty ExtendedString.
0061   Standard_EXPORT TCollection_ExtendedString() noexcept;
0062 
0063   //! Creation by converting a CString to an extended string.
0064   //! If theIsMultiByte is true then the string is treated as having UTF-8 coding.
0065   //! If it is not a UTF-8 then theIsMultiByte is ignored and each character is
0066   //! copied to ExtCharacter.
0067   //! @param[in] theString the C string to convert
0068   //! @param[in] theIsMultiByte flag indicating UTF-8 coding
0069   Standard_EXPORT TCollection_ExtendedString(const char* const theString,
0070                                              const bool        theIsMultiByte = false);
0071 
0072   //! Creation by converting an ExtString (char16_t*) to an extended string.
0073   //! @param[in] theString the char16_t string to copy
0074   Standard_EXPORT TCollection_ExtendedString(const char16_t* const theString);
0075 
0076 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
0077   //! Initialize from wide-char string considering it as Unicode string
0078   //! (the size of wide char is a platform-dependent - e.g. on Windows wchar_t is UTF-16).
0079   //!
0080   //! This constructor is unavailable if application is built with deprecated msvc option
0081   //! "-Zc:wchar_t-", since OCCT itself is never built with this option.
0082   //! @param[in] theStringUtf the wide character string to convert
0083   Standard_EXPORT TCollection_ExtendedString(const wchar_t* theStringUtf);
0084 #endif
0085 
0086   //! Initializes an ExtendedString with a single ASCII character.
0087   //! @param[in] theChar the ASCII character to initialize from
0088   Standard_EXPORT TCollection_ExtendedString(const char theChar);
0089 
0090   //! Initializes an ExtendedString with a single extended character.
0091   //! @param[in] theChar the extended character to initialize from
0092   Standard_EXPORT TCollection_ExtendedString(const char16_t theChar);
0093 
0094   //! Initializes an ExtendedString with specified length space allocated
0095   //! and filled with filler character. This is useful for buffers.
0096   //! @param[in] theLength the length to allocate
0097   //! @param[in] theFiller the character to fill with
0098   Standard_EXPORT TCollection_ExtendedString(const int theLength, const char16_t theFiller);
0099 
0100   //! Initializes an ExtendedString with an integer value.
0101   //! @param[in] theValue the integer value to convert to string
0102   Standard_EXPORT TCollection_ExtendedString(const int theValue);
0103 
0104   //! Initializes an ExtendedString with a real value.
0105   //! @param[in] theValue the real value to convert to string
0106   Standard_EXPORT TCollection_ExtendedString(const double theValue);
0107 
0108   //! Initializes an ExtendedString with another ExtendedString.
0109   //! @param[in] theString the string to copy from
0110   Standard_EXPORT TCollection_ExtendedString(const TCollection_ExtendedString& theString);
0111 
0112   //! Move constructor.
0113   //! @param[in] theOther the string to move from
0114   Standard_EXPORT TCollection_ExtendedString(TCollection_ExtendedString&& theOther) noexcept;
0115 
0116   //! Creation by converting an AsciiString to an extended string.
0117   //! The string is treated as having UTF-8 coding.
0118   //! If it is not a UTF-8 or multi byte then each character is copied to ExtCharacter.
0119   //! @param[in] theString the ASCII string to convert
0120   //! @param[in] theIsMultiByte flag indicating UTF-8 coding
0121   Standard_EXPORT TCollection_ExtendedString(const TCollection_AsciiString& theString,
0122                                              const bool                     theIsMultiByte = true);
0123 
0124   //! Initializes an ExtendedString with a char16_t string and explicit length.
0125   //! @param[in] theString the char16_t string to initialize from
0126   //! @param[in] theLength the length of the string
0127   Standard_EXPORT TCollection_ExtendedString(const char16_t* const theString, const int theLength);
0128 
0129   //! Template constructor for char16_t string literals or arrays.
0130   //! For true string literals (const char16_t[N] in code), the size is computed at runtime
0131   //! by scanning for null terminator to handle both literals and buffers correctly.
0132   //!
0133   //! Example:
0134   //! ```cpp
0135   //! TCollection_ExtendedString aString(u"Hello World");  // Works with string literals
0136   //! char16_t buffer[100];
0137   //! // ... fill buffer ...
0138   //! TCollection_ExtendedString aBufferString(buffer);    // Works with buffers too
0139   //! ```
0140   //! @param[in] theLiteral the string literal or char16_t array
0141   template <std::size_t N>
0142   TCollection_ExtendedString(const char16_t (&theLiteral)[N])
0143       : TCollection_ExtendedString(theLiteral, extStringLen(theLiteral))
0144   {
0145   }
0146 
0147 #if Standard_CPP17_OR_HIGHER
0148   //! Initializes an ExtendedString from a std::u16string_view.
0149   //! @param[in] theStringView the string view to copy
0150   explicit TCollection_ExtendedString(const std::u16string_view& theStringView)
0151   {
0152     myLength = static_cast<int>(theStringView.size());
0153     if (myLength == 0)
0154     {
0155       allocate(0);
0156     }
0157     else
0158     {
0159       allocate(myLength);
0160       memcpy(myString, theStringView.data(), myLength * sizeof(char16_t));
0161     }
0162   }
0163 
0164   //! Assignment from a std::u16string_view.
0165   //! @param[in] theStringView the string view to copy
0166   TCollection_ExtendedString& operator=(const std::u16string_view& theStringView)
0167   {
0168     const int aNewLen = static_cast<int>(theStringView.size());
0169     reallocate(aNewLen);
0170     if (aNewLen > 0)
0171     {
0172       memcpy(myString, theStringView.data(), aNewLen * sizeof(char16_t));
0173     }
0174     return *this;
0175   }
0176 
0177   //! Conversion to std::u16string_view.
0178   //! @return a non-owning view of the string data
0179   operator std::u16string_view() const noexcept { return std::u16string_view(myString, myLength); }
0180 #endif
0181 
0182   //! Appends the other extended string to this extended string.
0183   //! Note that this method is an alias of operator +=.
0184   //!
0185   //! Example:
0186   //! ```cpp
0187   //! TCollection_ExtendedString aString(u"Hello");
0188   //! TCollection_ExtendedString anotherString(u" World");
0189   //! aString += anotherString;
0190   //! // Result: aString == u"Hello World"
0191   //! ```
0192   //! @param[in] theOther the string to append
0193   Standard_EXPORT void AssignCat(const TCollection_ExtendedString& theOther);
0194 
0195   void operator+=(const TCollection_ExtendedString& theOther) { AssignCat(theOther); }
0196 
0197   //! Appends the integer value to this extended string.
0198   //! @param[in] theOther the integer to append
0199   Standard_EXPORT void AssignCat(const int theOther);
0200 
0201   void operator+=(const int theOther) { AssignCat(theOther); }
0202 
0203   //! Appends the ASCII character to this extended string.
0204   //! @param[in] theChar the character to append
0205   Standard_EXPORT void AssignCat(const char theChar);
0206 
0207   void operator+=(const char theChar) { AssignCat(theChar); }
0208 
0209   //! Appends the real value to this extended string.
0210   //! @param[in] theOther the real value to append
0211   Standard_EXPORT void AssignCat(const double theOther);
0212 
0213   void operator+=(const double theOther) { AssignCat(theOther); }
0214 
0215   //! Appends the utf16 char to this extended string.
0216   //! @param[in] theChar the character to append
0217   Standard_EXPORT void AssignCat(const char16_t theChar);
0218 
0219   //! Core implementation: Appends char16_t string (pointer and length) to this extended string.
0220   //! This is the primary implementation that all other AssignCat overloads redirect to.
0221   //! @param[in] theString pointer to the string to append
0222   //! @param[in] theLength length of the string to append
0223   Standard_EXPORT void AssignCat(const char16_t* const theString, const int theLength);
0224 
0225   //! Appends the char16_t string to this extended string.
0226   //! @param[in] theString the string to append
0227   inline void AssignCat(const char16_t* const theString)
0228   {
0229     if (theString != nullptr)
0230     {
0231       AssignCat(theString, extStringLen(theString));
0232     }
0233   }
0234 
0235   //! Appends the char16_t string to this extended string (alias of AssignCat()).
0236   void operator+=(const char16_t* const theString) { AssignCat(theString); }
0237 
0238   //! Template method for appending char16_t string literals or arrays.
0239   //! For char arrays (like buffers), scans for null terminator to get actual length.
0240   //!
0241   //! Example:
0242   //! ```cpp
0243   //! TCollection_ExtendedString aString(u"Hello");
0244   //! aString += u" World";  // Works with string literals
0245   //! char16_t buffer[100];
0246   //! // ... fill buffer ...
0247   //! aString += buffer;     // Works with buffers too
0248   //! ```
0249   //! @param[in] theLiteral the string literal or char16_t array to append
0250   template <std::size_t N>
0251   void AssignCat(const char16_t (&theLiteral)[N])
0252   {
0253     AssignCat(theLiteral, extStringLen(theLiteral));
0254   }
0255 
0256   template <std::size_t N>
0257   void operator+=(const char16_t (&theLiteral)[N])
0258   {
0259     AssignCat(theLiteral, extStringLen(theLiteral));
0260   }
0261 
0262 #if Standard_CPP17_OR_HIGHER
0263   //! Appends the std::u16string_view to this extended string.
0264   //! @param[in] theStringView the string view to append
0265   void AssignCat(const std::u16string_view& theStringView)
0266   {
0267     if (theStringView.empty())
0268     {
0269       return;
0270     }
0271     const int anOtherLen = static_cast<int>(theStringView.size());
0272     const int anOldLen   = myLength;
0273     reallocate(myLength + anOtherLen);
0274     memcpy(myString + anOldLen, theStringView.data(), anOtherLen * sizeof(char16_t));
0275   }
0276 
0277   //! Appends the std::u16string_view to this extended string (alias of AssignCat()).
0278   void operator+=(const std::u16string_view& theStringView) { AssignCat(theStringView); }
0279 #endif
0280 
0281   //! Core implementation: Concatenates char16_t string (pointer and length)
0282   //! and returns a new string.
0283   //! @param[in] theOther pointer to the string to append
0284   //! @param[in] theLength length of the string to append
0285   //! @return new string with theOther appended
0286   Standard_EXPORT TCollection_ExtendedString Cat(const char16_t* const theOther,
0287                                                  const int             theLength) const;
0288 
0289   //! Concatenates char16_t string and returns a new string.
0290   //! @param[in] theOther the null-terminated string to append
0291   //! @return new string with theOther appended
0292   TCollection_ExtendedString Cat(const char16_t* const theOther) const
0293   {
0294     return Cat(theOther, extStringLen(theOther));
0295   }
0296 
0297   TCollection_ExtendedString operator+(const char16_t* const theOther) const
0298   {
0299     return Cat(theOther);
0300   }
0301 
0302   //! Appends the integer value to this string and returns a new string.
0303   //! @param[in] theOther the integer to append
0304   //! @return new string with integer appended
0305   Standard_EXPORT TCollection_ExtendedString Cat(const int theOther) const;
0306 
0307   TCollection_ExtendedString operator+(const int theOther) const { return Cat(theOther); }
0308 
0309   //! Appends the real value to this string and returns a new string.
0310   //! @param[in] theOther the real value to append
0311   //! @return new string with real value appended
0312   Standard_EXPORT TCollection_ExtendedString Cat(const double theOther) const;
0313 
0314   TCollection_ExtendedString operator+(const double theOther) const { return Cat(theOther); }
0315 
0316   //! Appends a single ASCII character to this string and returns a new string.
0317   //! @param[in] theChar the ASCII character to append
0318   Standard_EXPORT TCollection_ExtendedString Cat(const char theChar) const;
0319 
0320   TCollection_ExtendedString operator+(const char theChar) const { return Cat(theChar); }
0321 
0322   //! Appends a single extended (char16_t) character to this string and returns a new string.
0323   //! @param[in] theChar the extended character to append
0324   Standard_EXPORT TCollection_ExtendedString Cat(const char16_t theChar) const;
0325 
0326   TCollection_ExtendedString operator+(const char16_t theChar) const { return Cat(theChar); }
0327 
0328   //! Appends the other extended string to this string and returns a new string.
0329   //!
0330   //! Example:
0331   //! ```cpp
0332   //! TCollection_ExtendedString aString(u"Hello");
0333   //! TCollection_ExtendedString anotherString(u" World");
0334   //! TCollection_ExtendedString aResult = aString + anotherString;
0335   //! // Result: aResult == u"Hello World"
0336   //! ```
0337   //! @param[in] theOther the string to append
0338   //! @return new string with theOther appended
0339   TCollection_ExtendedString Cat(const TCollection_ExtendedString& theOther) const
0340   {
0341     return Cat(theOther.myString, theOther.myLength);
0342   }
0343 
0344   TCollection_ExtendedString operator+(const TCollection_ExtendedString& theOther) const
0345   {
0346     return Cat(theOther);
0347   }
0348 
0349   //! Substitutes all the characters equal to theChar by theNewChar
0350   //! in this ExtendedString.
0351   //! The substitution can be case sensitive.
0352   //! If you don't use default case sensitive, no matter whether theChar is uppercase or not.
0353   //!
0354   //! Example:
0355   //! ```cpp
0356   //! TCollection_ExtendedString aString(u"Histake");
0357   //! aString.ChangeAll(u'H', u'M');
0358   //! // Result: aString == u"Mistake"
0359   //! ```
0360   //! @param[in] theChar the character to replace
0361   //! @param[in] theNewChar the replacement character
0362   Standard_EXPORT void ChangeAll(const char16_t theChar, const char16_t theNewChar);
0363 
0364   //! Removes all characters contained in this string.
0365   //! This produces an empty ExtendedString.
0366   Standard_EXPORT void Clear();
0367 
0368   //! Core implementation: Copy from a char16_t pointer with explicit length.
0369   //! @param[in] theString pointer to the string to copy
0370   //! @param[in] theLength length of the string to copy
0371   Standard_EXPORT void Copy(const char16_t* const theString, const int theLength);
0372 
0373   //! Copy from a char16_t pointer.
0374   //! @param[in] theString the null-terminated string to copy
0375   void Copy(const char16_t* const theString) { Copy(theString, extStringLen(theString)); }
0376 
0377   //! Copy theFromWhere to this string.
0378   //! Used as operator =
0379   //!
0380   //! Example:
0381   //! ```cpp
0382   //! TCollection_ExtendedString aString;
0383   //! TCollection_ExtendedString anotherString(u"Hello World");
0384   //! aString = anotherString;  // operator=
0385   //! // Result: aString == u"Hello World"
0386   //! ```
0387   //! @param[in] theFromWhere the string to copy from
0388   void Copy(const TCollection_ExtendedString& theFromWhere)
0389   {
0390     if (&theFromWhere != this)
0391     {
0392       Copy(theFromWhere.myString, theFromWhere.myLength);
0393     }
0394   }
0395 
0396   //! Copy assignment operator.
0397   TCollection_ExtendedString& operator=(const TCollection_ExtendedString& theOther)
0398   {
0399     Copy(theOther);
0400     return *this;
0401   }
0402 
0403   //! Assignment from char16_t pointer.
0404   TCollection_ExtendedString& operator=(const char16_t* const theString)
0405   {
0406     Copy(theString);
0407     return *this;
0408   }
0409 
0410   //! Moves string without reallocations.
0411   //! @param[in] theOther the string to move from
0412   Standard_EXPORT void Move(TCollection_ExtendedString&& theOther);
0413 
0414   //! Move assignment operator.
0415   TCollection_ExtendedString& operator=(TCollection_ExtendedString&& theOther) noexcept
0416   {
0417     Move(std::forward<TCollection_ExtendedString>(theOther));
0418     return *this;
0419   }
0420 
0421   //! Exchange the data of two strings (without reallocating memory).
0422   //! @param[in,out] theOther the string to exchange data with
0423   Standard_EXPORT void Swap(TCollection_ExtendedString& theOther);
0424 
0425   //! Frees memory allocated by ExtendedString.
0426   Standard_EXPORT ~TCollection_ExtendedString();
0427 
0428   //! Insert a Character at position theWhere.
0429   //!
0430   //! Example:
0431   //! ```cpp
0432   //! TCollection_ExtendedString aString(u"hy not ?");
0433   //! aString.Insert(1, u'W');
0434   //! // Result: aString == u"Why not ?"
0435   //! ```
0436   //! @param[in] theWhere the position to insert at (1-based)
0437   //! @param[in] theWhat the character to insert
0438   Standard_EXPORT void Insert(const int theWhere, const char16_t theWhat);
0439 
0440   //! Core implementation: Insert a char16_t string (pointer and length) at position theWhere.
0441   //! @param[in] theWhere the position to insert at (1-based)
0442   //! @param[in] theWhat pointer to the string to insert
0443   //! @param[in] theLength length of the string to insert
0444   Standard_EXPORT void Insert(const int             theWhere,
0445                               const char16_t* const theWhat,
0446                               const int             theLength);
0447 
0448   //! Insert a char16_t string at position theWhere.
0449   //! @param[in] theWhere the position to insert at (1-based)
0450   //! @param[in] theWhat the null-terminated string to insert
0451   void Insert(const int theWhere, const char16_t* const theWhat)
0452   {
0453     Insert(theWhere, theWhat, extStringLen(theWhat));
0454   }
0455 
0456   //! Insert an ExtendedString at position theWhere.
0457   //! @param[in] theWhere the position to insert at (1-based)
0458   //! @param[in] theWhat the string to insert
0459   void Insert(const int theWhere, const TCollection_ExtendedString& theWhat)
0460   {
0461     Insert(theWhere, theWhat.myString, theWhat.myLength);
0462   }
0463 
0464   //! Returns True if this string contains no characters.
0465   bool IsEmpty() const { return myLength == 0; }
0466 
0467   //! Core implementation: Returns true if this string equals theOther (pointer and length).
0468   //! @param[in] theOther pointer to the string to compare with
0469   //! @param[in] theLength length of the string to compare with
0470   //! @return true if strings are equal, false otherwise
0471   Standard_EXPORT bool IsEqual(const char16_t* const theOther, const int theLength) const;
0472 
0473   //! Returns true if this string equals theOther null-terminated string.
0474   //! Note that this method is an alias of operator ==.
0475   //! @param[in] theOther the char16_t string to compare with
0476   //! @return true if strings are equal, false otherwise
0477   bool IsEqual(const char16_t* const theOther) const
0478   {
0479     return IsEqual(theOther, extStringLen(theOther));
0480   }
0481 
0482   bool operator==(const char16_t* const theOther) const { return IsEqual(theOther); }
0483 
0484   //! Returns true if the characters in this extended
0485   //! string are identical to the characters in theOther extended string.
0486   //! Note that this method is an alias of operator ==.
0487   //! @param[in] theOther the extended string to compare with
0488   //! @return true if strings are equal, false otherwise
0489   bool IsEqual(const TCollection_ExtendedString& theOther) const
0490   {
0491     return myLength == theOther.myLength && IsEqual(theOther.myString, theOther.myLength);
0492   }
0493 
0494   bool operator==(const TCollection_ExtendedString& theOther) const { return IsEqual(theOther); }
0495 
0496   //! Core implementation: Returns true if this string differs from theOther (pointer and length).
0497   //! @param[in] theOther pointer to the string to compare with
0498   //! @param[in] theLength length of the string to compare with
0499   //! @return true if strings are different, false otherwise
0500   Standard_EXPORT bool IsDifferent(const char16_t* const theOther, const int theLength) const;
0501 
0502   //! Returns true if this string differs from theOther null-terminated string.
0503   //! Note that this method is an alias of operator !=.
0504   //! @param[in] theOther the char16_t string to compare with
0505   //! @return true if strings are different, false otherwise
0506   bool IsDifferent(const char16_t* const theOther) const
0507   {
0508     return IsDifferent(theOther, extStringLen(theOther));
0509   }
0510 
0511   bool operator!=(const char16_t* const theOther) const { return IsDifferent(theOther); }
0512 
0513   //! Returns true if there are differences between the
0514   //! characters in this extended string and theOther extended string.
0515   //! Note that this method is an alias of operator !=.
0516   //! @param[in] theOther the extended string to compare with
0517   //! @return true if strings are different, false otherwise
0518   bool IsDifferent(const TCollection_ExtendedString& theOther) const
0519   {
0520     return IsDifferent(theOther.myString, theOther.myLength);
0521   }
0522 
0523   bool operator!=(const TCollection_ExtendedString& theOther) const
0524   {
0525     return IsDifferent(theOther);
0526   }
0527 
0528   //! Core implementation: Returns TRUE if this string is lexicographically less than theOther.
0529   //! @param[in] theOther pointer to the string to compare with
0530   //! @param[in] theLength length of the string to compare with
0531   //! @return true if this string is less than theOther
0532   Standard_EXPORT bool IsLess(const char16_t* const theOther, const int theLength) const;
0533 
0534   //! Returns TRUE if this string is lexicographically less than theOther.
0535   //! @param[in] theOther the char16_t string to compare with
0536   //! @return true if this string is less than theOther
0537   bool IsLess(const char16_t* const theOther) const
0538   {
0539     return IsLess(theOther, extStringLen(theOther));
0540   }
0541 
0542   bool operator<(const char16_t* const theOther) const { return IsLess(theOther); }
0543 
0544   //! Returns TRUE if this string is lexicographically less than theOther.
0545   //! @param[in] theOther the extended string to compare with
0546   //! @return true if this string is less than theOther
0547   bool IsLess(const TCollection_ExtendedString& theOther) const
0548   {
0549     return IsLess(theOther.myString, theOther.myLength);
0550   }
0551 
0552   bool operator<(const TCollection_ExtendedString& theOther) const { return IsLess(theOther); }
0553 
0554   //! Core implementation: Returns TRUE if this string is lexicographically greater than theOther.
0555   //! @param[in] theOther pointer to the string to compare with
0556   //! @param[in] theLength length of the string to compare with
0557   //! @return true if this string is greater than theOther
0558   Standard_EXPORT bool IsGreater(const char16_t* const theOther, const int theLength) const;
0559 
0560   //! Returns TRUE if this string is lexicographically greater than theOther.
0561   //! @param[in] theOther the char16_t string to compare with
0562   //! @return true if this string is greater than theOther
0563   bool IsGreater(const char16_t* const theOther) const
0564   {
0565     return IsGreater(theOther, extStringLen(theOther));
0566   }
0567 
0568   bool operator>(const char16_t* const theOther) const { return IsGreater(theOther); }
0569 
0570   //! Returns TRUE if this string is lexicographically greater than theOther.
0571   //! @param[in] theOther the extended string to compare with
0572   //! @return true if this string is greater than theOther
0573   bool IsGreater(const TCollection_ExtendedString& theOther) const
0574   {
0575     return IsGreater(theOther.myString, theOther.myLength);
0576   }
0577 
0578   bool operator>(const TCollection_ExtendedString& theOther) const { return IsGreater(theOther); }
0579 
0580   //! Core implementation: Determines whether this string starts with theStartString.
0581   //! @param[in] theStartString pointer to the string to check for
0582   //! @param[in] theLength length of the string to check for
0583   //! @return true if this string starts with theStartString
0584   Standard_EXPORT bool StartsWith(const char16_t* const theStartString, const int theLength) const;
0585 
0586   //! Determines whether this string starts with theStartString.
0587   //! @param[in] theStartString the null-terminated string to check for
0588   //! @return true if this string starts with theStartString
0589   bool StartsWith(const char16_t* const theStartString) const
0590   {
0591     return StartsWith(theStartString, extStringLen(theStartString));
0592   }
0593 
0594   //! Determines whether the beginning of this string instance matches the specified string.
0595   //! @param[in] theStartString the string to check for at the beginning
0596   //! @return true if this string starts with theStartString
0597   bool StartsWith(const TCollection_ExtendedString& theStartString) const
0598   {
0599     return StartsWith(theStartString.myString, theStartString.myLength);
0600   }
0601 
0602   //! Core implementation: Determines whether this string ends with theEndString.
0603   //! @param[in] theEndString pointer to the string to check for
0604   //! @param[in] theLength length of the string to check for
0605   //! @return true if this string ends with theEndString
0606   Standard_EXPORT bool EndsWith(const char16_t* const theEndString, const int theLength) const;
0607 
0608   //! Determines whether this string ends with theEndString.
0609   //! @param[in] theEndString the null-terminated string to check for
0610   //! @return true if this string ends with theEndString
0611   bool EndsWith(const char16_t* const theEndString) const
0612   {
0613     return EndsWith(theEndString, extStringLen(theEndString));
0614   }
0615 
0616   //! Determines whether the end of this string instance matches the specified string.
0617   //! @param[in] theEndString the string to check for at the end
0618   //! @return true if this string ends with theEndString
0619   bool EndsWith(const TCollection_ExtendedString& theEndString) const
0620   {
0621     return EndsWith(theEndString.myString, theEndString.myLength);
0622   }
0623 
0624   //! Returns True if the ExtendedString contains only "Ascii Range" characters.
0625   //! @return true if string contains only ASCII characters
0626   Standard_EXPORT bool IsAscii() const;
0627 
0628   //! Returns the number of 16-bit code units
0629   //! (might be greater than number of Unicode symbols if string contains surrogate pairs).
0630   //! @return the number of 16-bit code units
0631   Standard_EXPORT int Length() const;
0632 
0633   //! Displays this string on a stream.
0634   //! @param[in] theStream the output stream
0635   Standard_EXPORT void                     Print(Standard_OStream& theStream) const;
0636   friend Standard_EXPORT Standard_OStream& operator<<(Standard_OStream&                 theStream,
0637                                                       const TCollection_ExtendedString& theString);
0638 
0639   //! Removes every theWhat characters from this string.
0640   //! @param[in] theWhat the character to remove
0641   Standard_EXPORT void RemoveAll(const char16_t theWhat);
0642 
0643   //! Erases theHowMany characters from position theWhere, theWhere included.
0644   //!
0645   //! Example:
0646   //! ```cpp
0647   //! TCollection_ExtendedString aString(u"Hello");
0648   //! aString.Remove(2, 2); // erases 2 characters from position 2
0649   //! // Result: aString == u"Hlo"
0650   //! ```
0651   //! @param[in] theWhere the position to start erasing from (1-based)
0652   //! @param[in] theHowMany the number of characters to erase
0653   Standard_EXPORT void Remove(const int theWhere, const int theHowMany = 1);
0654 
0655   //! Core implementation: Searches for theWhat (pointer and length) from the beginning.
0656   //! @param[in] theWhat pointer to the string to search for
0657   //! @param[in] theLength length of the string to search for
0658   //! @return the position of first match (1-based), or -1 if not found
0659   Standard_EXPORT int Search(const char16_t* const theWhat, const int theLength) const;
0660 
0661   //! Searches for theWhat null-terminated string from the beginning.
0662   //! @param[in] theWhat the null-terminated string to search for
0663   //! @return the position of first match (1-based), or -1 if not found
0664   int Search(const char16_t* const theWhat) const { return Search(theWhat, extStringLen(theWhat)); }
0665 
0666   //! Searches an ExtendedString in this string from the beginning
0667   //! and returns position of first item theWhat matching.
0668   //! It returns -1 if not found.
0669   //! @param[in] theWhat the string to search for
0670   //! @return the position of first match (1-based), or -1 if not found
0671   int Search(const TCollection_ExtendedString& theWhat) const
0672   {
0673     return Search(theWhat.myString, theWhat.myLength);
0674   }
0675 
0676   //! Core implementation: Searches for theWhat (pointer and length) from the end.
0677   //! @param[in] theWhat pointer to the string to search for
0678   //! @param[in] theLength length of the string to search for
0679   //! @return the position of first match from end (1-based), or -1 if not found
0680   Standard_EXPORT int SearchFromEnd(const char16_t* const theWhat, const int theLength) const;
0681 
0682   //! Searches for theWhat null-terminated string from the end.
0683   //! @param[in] theWhat the null-terminated string to search for
0684   //! @return the position of first match from end (1-based), or -1 if not found
0685   int SearchFromEnd(const char16_t* const theWhat) const
0686   {
0687     return SearchFromEnd(theWhat, extStringLen(theWhat));
0688   }
0689 
0690   //! Searches an ExtendedString in this string from the end
0691   //! and returns position of first item theWhat matching.
0692   //! It returns -1 if not found.
0693   //! @param[in] theWhat the string to search for
0694   //! @return the position of first match from end (1-based), or -1 if not found
0695   int SearchFromEnd(const TCollection_ExtendedString& theWhat) const
0696   {
0697     return SearchFromEnd(theWhat.myString, theWhat.myLength);
0698   }
0699 
0700   //! Replaces one character in the ExtendedString at position theWhere.
0701   //! If theWhere is less than zero or greater than the length of this string
0702   //! an exception is raised.
0703   //!
0704   //! Example:
0705   //! ```cpp
0706   //! TCollection_ExtendedString aString(u"Garbake");
0707   //! aString.SetValue(6, u'g');
0708   //! // Result: aString == u"Garbage"
0709   //! ```
0710   //! @param[in] theWhere the position to replace at (1-based)
0711   //! @param[in] theWhat the character to replace with
0712   Standard_EXPORT void SetValue(const int theWhere, const char16_t theWhat);
0713 
0714   //! Core implementation: Replaces a part of this string by char16_t string (pointer and length).
0715   //! @param[in] theWhere the position to start replacement (1-based)
0716   //! @param[in] theWhat pointer to the string to replace with
0717   //! @param[in] theLength length of the string to replace with
0718   Standard_EXPORT void SetValue(const int             theWhere,
0719                                 const char16_t* const theWhat,
0720                                 const int             theLength);
0721 
0722   //! Replaces a part of this string by a null-terminated char16_t string.
0723   //! @param[in] theWhere the position to start replacement (1-based)
0724   //! @param[in] theWhat the null-terminated string to replace with
0725   void SetValue(const int theWhere, const char16_t* const theWhat)
0726   {
0727     SetValue(theWhere, theWhat, extStringLen(theWhat));
0728   }
0729 
0730   //! Replaces a part of this string by another ExtendedString.
0731   //! @param[in] theWhere the position to start replacement (1-based)
0732   //! @param[in] theWhat the string to replace with
0733   void SetValue(const int theWhere, const TCollection_ExtendedString& theWhat)
0734   {
0735     SetValue(theWhere, theWhat.myString, theWhat.myLength);
0736   }
0737 
0738   //! Copies characters from this string starting from index theFromIndex
0739   //! to the index theToIndex (inclusive).
0740   //! Raises an exception if theToIndex or theFromIndex is out of bounds.
0741   //!
0742   //! Example:
0743   //! ```cpp
0744   //! TCollection_ExtendedString aString(u"abcdefg");
0745   //! TCollection_ExtendedString aSubString = aString.SubString(3, 6);
0746   //! // Result: aSubString == u"cdef"
0747   //! ```
0748   //! @param[in] theFromIndex the starting index (1-based)
0749   //! @param[in] theToIndex the ending index (1-based, inclusive)
0750   //! @return the substring from theFromIndex to theToIndex
0751   Standard_EXPORT TCollection_ExtendedString SubString(const int theFromIndex,
0752                                                        const int theToIndex) const;
0753 
0754   //! Splits this extended string into two sub-strings at position theWhere.
0755   //! -   The second sub-string (from position theWhere + 1 of this string to the end) is
0756   //!     returned in a new extended string.
0757   //! -   This extended string is modified: its last characters are removed, it becomes equal to
0758   //!     the first sub-string (from the first character to position theWhere).
0759   //!
0760   //! Example:
0761   //! ```cpp
0762   //! TCollection_ExtendedString aString(u"abcdefg");
0763   //! TCollection_ExtendedString aSecondPart = aString.Split(3);
0764   //! // Result: aString == u"abc" and aSecondPart == u"defg"
0765   //! ```
0766   //! @param[in] theWhere the position to split at (0-based)
0767   //! @return the second part of the split string
0768   Standard_EXPORT TCollection_ExtendedString Split(const int theWhere);
0769 
0770   //! Extracts theWhichOne token from this string.
0771   //! By default, the theSeparators is set to space and tabulation.
0772   //! By default, the token extracted is the first one (theWhichOne = 1).
0773   //! theSeparators contains all separators you need.
0774   //! If no token indexed by theWhichOne is found, it returns an empty ExtendedString.
0775   //!
0776   //! Example:
0777   //! ```cpp
0778   //! TCollection_ExtendedString aString(u"This is a     message");
0779   //! TCollection_ExtendedString aToken1 = aString.Token();
0780   //! // Result: aToken1 == u"This"
0781   //!
0782   //! TCollection_ExtendedString aToken2 = aString.Token(u" ", 4);
0783   //! // Result: aToken2 == u"message"
0784   //!
0785   //! TCollection_ExtendedString aToken3 = aString.Token(u" ", 2);
0786   //! // Result: aToken3 == u"is"
0787   //!
0788   //! TCollection_ExtendedString aToken4 = aString.Token(u" ", 9);
0789   //! // Result: aToken4 == u""
0790   //!
0791   //! TCollection_ExtendedString bString(u"1234; test:message   , value");
0792   //! TCollection_ExtendedString bToken1 = bString.Token(u"; :,", 4);
0793   //! // Result: bToken1 == u"value"
0794   //! ```
0795   //! @param[in] theSeparators the separator characters
0796   //! @param[in] theWhichOne the token number to extract (1-based)
0797   //! @return the extracted token
0798   Standard_EXPORT TCollection_ExtendedString Token(const char16_t* const theSeparators,
0799                                                    const int             theWhichOne = 1) const;
0800 
0801   //! Returns pointer to ExtString (char16_t*).
0802   //! @return the char16_t string representation
0803   Standard_EXPORT const char16_t* ToExtString() const;
0804 
0805 #ifdef _WIN32
0806   //! Returns pointer to string as wchar_t* on Windows platform where wchar_t* is considered as
0807   //! UTF-16 string. This method is useful to pass string into wide-char system APIs, and makes
0808   //! sense only on Windows (other systems use UTF-8 and can miss wide-char functions at all).
0809   //! @return the wchar_t string representation
0810   const wchar_t* ToWideString() const { return (const wchar_t*)ToExtString(); }
0811 #endif
0812 
0813   //! Truncates this string to theHowMany characters.
0814   //!
0815   //! Example:
0816   //! ```cpp
0817   //! TCollection_ExtendedString aString(u"Hello Dolly");
0818   //! aString.Trunc(3);
0819   //! // Result: aString == u"Hel"
0820   //! ```
0821   //! @param[in] theHowMany the number of characters to keep
0822   Standard_EXPORT void Trunc(const int theHowMany);
0823 
0824   //! Returns character at position theWhere in this string.
0825   //! If theWhere is less than zero or greater than the length of
0826   //! this string, an exception is raised.
0827   //!
0828   //! Example:
0829   //! ```cpp
0830   //! TCollection_ExtendedString aString(u"Hello");
0831   //! char16_t aChar = aString.Value(2);
0832   //! // Result: aChar == u'e'
0833   //! ```
0834   //! @param[in] theWhere the position to get character from (1-based)
0835   //! @return the character at the specified position
0836   Standard_EXPORT char16_t Value(const int theWhere) const;
0837 
0838   //! Returns a hashed value for the extended string.
0839   //! Note: if string is ASCII, the computed value is the same as the value computed with the
0840   //! HashCode function on a TCollection_AsciiString string composed with equivalent ASCII
0841   //! characters.
0842   //! @return a computed hash code
0843   size_t HashCode() const
0844   {
0845     const int aSize = myLength * sizeof(char16_t);
0846     if (myLength < 2)
0847     {
0848       return opencascade::FNVHash::hash_combine(*myString, aSize);
0849     }
0850     return opencascade::hashBytes(myString, aSize);
0851   }
0852 
0853   //! Returns a const reference to a single shared empty string instance.
0854   //! This method provides access to a static empty string to avoid creating temporary empty
0855   //! strings. Use this method instead of constructing empty strings when you need a const
0856   //! reference.
0857   //!
0858   //! Example:
0859   //! ```cpp
0860   //! const TCollection_ExtendedString& anEmptyStr = TCollection_ExtendedString::EmptyString();
0861   //! // Use anEmptyStr instead of TCollection_ExtendedString()
0862   //! ```
0863   //! @return const reference to static empty string
0864   Standard_EXPORT static const TCollection_ExtendedString& EmptyString() noexcept;
0865 
0866   //! Returns true if the characters in this extended
0867   //! string are identical to the characters in the other extended string.
0868   //! Note that this method is an alias of operator ==.
0869   //! @param[in] theString1 first string to compare
0870   //! @param[in] theString2 second string to compare
0871   //! @return true if strings are equal
0872   static bool IsEqual(const TCollection_ExtendedString& theString1,
0873                       const TCollection_ExtendedString& theString2)
0874   {
0875     return theString1.IsEqual(theString2);
0876   }
0877 
0878   //! Converts the internal myString to UTF8 coding and
0879   //! returns length of the out CString. A memory for the
0880   //! theCString should be allocated before call!
0881   //! @param[in,out] theCString pointer to the output buffer
0882   //! @return length of the UTF-8 string
0883   Standard_EXPORT int ToUTF8CString(Standard_PCharacter& theCString) const;
0884 
0885   //! Returns expected CString length in UTF8 coding (like strlen, without null terminator).
0886   //! It can be used for memory calculation before converting to CString containing symbols in UTF8
0887   //! coding. For external allocation, use: char* buf = new char[str.LengthOfCString() + 1];
0888   //! @return expected UTF-8 string length
0889   Standard_EXPORT int LengthOfCString() const;
0890 
0891   //! Removes all space characters in the beginning of the string.
0892   Standard_EXPORT void LeftAdjust();
0893 
0894   //! Removes all space characters at the end of the string.
0895   Standard_EXPORT void RightAdjust();
0896 
0897   //! Left justify.
0898   //! Length becomes equal to theWidth and the new characters are
0899   //! equal to theFiller.
0900   //! If theWidth < Length nothing happens.
0901   //! @param[in] theWidth the desired width of the string
0902   //! @param[in] theFiller the character to fill with
0903   Standard_EXPORT void LeftJustify(const int theWidth, const char16_t theFiller);
0904 
0905   //! Right justify.
0906   //! Length becomes equal to theWidth and the new characters are
0907   //! equal to theFiller.
0908   //! If theWidth < Length nothing happens.
0909   //! @param[in] theWidth the desired width of the string
0910   //! @param[in] theFiller the character to fill with
0911   Standard_EXPORT void RightJustify(const int theWidth, const char16_t theFiller);
0912 
0913   //! Modifies this string so that its length becomes equal to theWidth
0914   //! and the new characters are equal to theFiller.
0915   //! New characters are added both at the beginning and at the end of this string.
0916   //! If theWidth is less than the length of this string, nothing happens.
0917   //! @param[in] theWidth the desired width of the string
0918   //! @param[in] theFiller the character to fill with
0919   Standard_EXPORT void Center(const int theWidth, const char16_t theFiller);
0920 
0921   //! Converts the first character into its corresponding
0922   //! upper-case character and the other characters into lowercase.
0923   //! @note Only ASCII characters (a-z, A-Z) are affected by case conversion.
0924   Standard_EXPORT void Capitalize();
0925 
0926   //! Core implementation: Inserts char16_t string (pointer and length) at the beginning.
0927   //! @param[in] theOther pointer to the string to prepend
0928   //! @param[in] theLength length of the string to prepend
0929   Standard_EXPORT void Prepend(const char16_t* const theOther, const int theLength);
0930 
0931   //! Inserts a null-terminated char16_t string at the beginning.
0932   //! @param[in] theOther the null-terminated string to prepend
0933   void Prepend(const char16_t* const theOther) { Prepend(theOther, extStringLen(theOther)); }
0934 
0935   //! Inserts the other extended string at the beginning of this string.
0936   //! @param[in] theOther the string to prepend
0937   void Prepend(const TCollection_ExtendedString& theOther)
0938   {
0939     Prepend(theOther.myString, theOther.myLength);
0940   }
0941 
0942   //! Returns the index of the first character of this string that is
0943   //! present in theSet.
0944   //! The search begins at index theFromIndex and ends at index theToIndex.
0945   //! Returns zero if failure.
0946   //! @param[in] theSet the set of characters to search for
0947   //! @param[in] theFromIndex the starting index for search (1-based)
0948   //! @param[in] theToIndex the ending index for search (1-based)
0949   //! @return the index of first character found in set, or 0 if not found
0950   Standard_EXPORT int FirstLocationInSet(const TCollection_ExtendedString& theSet,
0951                                          const int                         theFromIndex,
0952                                          const int                         theToIndex) const;
0953 
0954   //! Returns the index of the first character of this string that is
0955   //! NOT present in theSet.
0956   //! The search begins at index theFromIndex and ends at index theToIndex.
0957   //! Returns zero if failure.
0958   //! @param[in] theSet the set of characters to check against
0959   //! @param[in] theFromIndex the starting index for search (1-based)
0960   //! @param[in] theToIndex the ending index for search (1-based)
0961   //! @return the index of first character not in set, or 0 if not found
0962   Standard_EXPORT int FirstLocationNotInSet(const TCollection_ExtendedString& theSet,
0963                                             const int                         theFromIndex,
0964                                             const int                         theToIndex) const;
0965 
0966   //! Converts this extended string containing a numeric expression to an Integer.
0967   //! @return the integer value
0968   Standard_EXPORT int IntegerValue() const;
0969 
0970   //! Returns True if this extended string contains an integer value.
0971   //! @return true if string represents an integer value
0972   Standard_EXPORT bool IsIntegerValue() const;
0973 
0974   //! Converts this extended string containing a numeric expression to a Real.
0975   //! @return the real value
0976   Standard_EXPORT double RealValue() const;
0977 
0978   //! Returns True if this extended string starts with characters that can be
0979   //! interpreted as a real value.
0980   //! @param[in] theToCheckFull when TRUE, checks if entire string defines a real value;
0981   //!                           otherwise checks if string starts with a real value
0982   //! @return true if string represents a real value
0983   Standard_EXPORT bool IsRealValue(bool theToCheckFull = false) const;
0984 
0985   //! Returns True if the strings contain same characters.
0986   //! @param[in] theOther the string to compare with
0987   //! @param[in] theIsCaseSensitive flag indicating case sensitivity
0988   //! @note When case-insensitive, only ASCII characters (a-z, A-Z) are affected.
0989   //! @return true if strings contain same characters
0990   Standard_EXPORT bool IsSameString(const TCollection_ExtendedString& theOther,
0991                                     const bool                        theIsCaseSensitive) const;
0992 
0993 private:
0994   //! Returns true if the input CString was successfully converted to UTF8 coding.
0995   bool ConvertToUnicode(const char* const theString);
0996 
0997   //! Internal wrapper to allocate on stack or heap
0998   void allocate(const int theLength);
0999 
1000   //! Internal wrapper to reallocate on stack or heap
1001   void reallocate(const int theLength);
1002 
1003   //! Internal wrapper to deallocate on stack
1004   void deallocate();
1005 
1006   //! Helper function to compute length of char16_t string (like strlen for char).
1007   //! Scans for null terminator to find actual string length.
1008   //! @param[in] theString the char16_t string
1009   //! @return length of string (excluding null terminator)
1010   static int extStringLen(const char16_t* const theString)
1011   {
1012     if (theString == nullptr)
1013     {
1014       return 0;
1015     }
1016     int aLen = 0;
1017     while (theString[aLen] != u'\0')
1018     {
1019       ++aLen;
1020     }
1021     return aLen;
1022   }
1023 
1024 private:
1025   char16_t* myString{}; //!< NULL-terminated string
1026   int       myLength{}; //!< length in 16-bit code units (excluding terminating NULL symbol)
1027 };
1028 
1029 namespace std
1030 {
1031 template <>
1032 struct hash<TCollection_ExtendedString>
1033 {
1034   size_t operator()(const TCollection_ExtendedString& theString) const
1035   {
1036     return theString.HashCode();
1037   }
1038 };
1039 } // namespace std
1040 
1041 #endif // _TCollection_ExtendedString_HeaderFile