LCOV - code coverage report
Current view: top level - source/ps - CStr.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 5 5 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /**
      19             :  * Description : Contains CStr class which is a versatile class for making string use easy.
      20             :  *             : The class implements a series of string manipulation/formatting functions.
      21             :  **/
      22             : 
      23             : #ifndef INCLUDED_CSTR
      24             : #define INCLUDED_CSTR
      25             : 
      26             : /**
      27             :  * Whitespace trim identifier for Trim functions
      28             :  **/
      29             : enum PS_TRIM_MODE
      30             : {
      31             :     PS_TRIM_LEFT,   /// Trim all white space from the beginning of the string
      32             :     PS_TRIM_RIGHT,  /// Trim all white space from the end of the string
      33             :     PS_TRIM_BOTH    /// Trim all white space from the beginning and end of the string
      34             : };
      35             : 
      36             : #ifndef IN_UNIDOUBLER
      37             :  #define UNIDOUBLER_HEADER "CStr.h"
      38             :  #include "UniDoubler.h"
      39             : #endif
      40             : 
      41             : #endif
      42             : 
      43             : // Include this section when in unidoubler mode, and when this unicode/ascii
      44             : // version has not already been included.
      45             : #if defined(IN_UNIDOUBLER) && ( (defined(_UNICODE) && !defined(CSTR_H_U)) || (!defined(_UNICODE) && !defined(CSTR_H_A)) )
      46             : 
      47             : #ifdef _UNICODE
      48             : #define CSTR_H_U
      49             : #else
      50             : #define CSTR_H_A
      51             : #endif
      52             : 
      53             : #include "ps/CStrForward.h"
      54             : 
      55             : #include <string>
      56             : 
      57             : /**
      58             :  * The base class of all strings
      59             :  **/
      60     1080056 : class CStr : public std::tstring
      61             : {
      62             : public:
      63             :     using StrBase = std::tstring;
      64             :     using Char = typename std::tstring::value_type;
      65             : 
      66       61100 :     CStr(const StrBase& str) : StrBase(str) {}
      67             : 
      68      694491 :     using StrBase::StrBase;
      69             : 
      70             :     /**
      71             :      * Repeat: Named constructor, to avoid overload overload.
      72             :      *
      73             :      * @param const CStr & str reference to another CStr object to be repeated for initialization
      74             :      * @param size_t Reps number of times to repeat the initialization
      75             :      * @return CStr new CStr object
      76             :      **/
      77             :     static CStr Repeat(const CStr& str, size_t reps);
      78             : 
      79             :     // Conversion to/from UTF-8, encoded in a CStr8.
      80             :     // Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
      81             :     // above U+FFFF) are silently replaced with U+FFFD.
      82             :     #ifdef _UNICODE
      83             :         CStr8 ToUTF8() const;
      84             :     #else
      85             :         CStrW FromUTF8() const;
      86             :     #endif
      87             : 
      88             :     // Conversions:
      89             : 
      90             :     /**
      91             :      * Return CStr as Integer.
      92             :      * Conversion is from the beginning of CStr.
      93             :      *
      94             :      * @return int CStr represented as an integer.
      95             :      **/
      96             :     int ToInt() const;
      97             :     /**
      98             :      * Return CStr as Unsigned Integer.
      99             :      * Conversion is from the beginning of CStr.
     100             :      *
     101             :      * @return unsigned int CStr represented as an unsigned integer.
     102             :      **/
     103             :     unsigned int ToUInt() const;
     104             :     /**
     105             :      * Return CStr as Long.
     106             :      * Conversion is from the beginning of CStr.
     107             :      *
     108             :      * @return long CStr represented as a long.
     109             :      **/
     110             :     long ToLong() const;
     111             :     /**
     112             :      * Return CStr as Unsigned Long.
     113             :      * Conversion is from the beginning of CStr.
     114             :      *
     115             :      * @return unsigned long CStr represented as an unsigned long.
     116             :      **/
     117             :     unsigned long ToULong() const;
     118             :     /**
     119             :      * Return CStr as Float.
     120             :      * Conversion is from the beginning of CStr.
     121             :      *
     122             :      * @return float CStr represented as a float.
     123             :      **/
     124             :     float ToFloat() const;
     125             :     /**
     126             :      * Return CStr as Double.
     127             :      * Conversion is from the beginning of CStr.
     128             :      *
     129             :      * @return double CStr represented as a double.
     130             :      **/
     131             :     double ToDouble() const;
     132             : 
     133             :     /**
     134             :      * Make a copy of the CStr in lower-case.
     135             :      *
     136             :      * @return CStr converted copy of CStr.
     137             :      **/
     138             :     CStr LowerCase() const;
     139             :     /**
     140             :      * Make a copy of the CStr in upper-case.
     141             :      *
     142             :      * @return CStr converted copy of CStr.
     143             :      **/
     144             :     CStr UpperCase() const;
     145             : 
     146             :     /**
     147             :      * Retrieve substring of the CStr after last occurrence of a string.
     148             :      * Return substring of the CStr after the last occurrence of the search string.
     149             :      *
     150             :      * @param const CStr & str reference to search string
     151             :      * @param size_t startPos character position to start searching from
     152             :      * @return CStr substring remaining after match
     153             :      *                   the CStr if no match is found
     154             :      **/
     155             :     CStr AfterLast(const CStr& str, size_t startPos = npos) const;
     156             : 
     157             :     /**
     158             :      * Retrieve substring of the CStr preceding last occurrence of a string.
     159             :      * Return substring of the CStr preceding the last occurrence of the search string.
     160             :      *
     161             :      * @param const CStr & str reference to search string
     162             :      * @param size_t startPos character position to start searching from
     163             :      * @return CStr substring preceding before match
     164             :      *                   the CStr if no match is found
     165             :      **/
     166             :     CStr BeforeLast(const CStr& str, size_t startPos = npos) const;
     167             : 
     168             :     /**
     169             :      * Retrieve substring of the CStr after first occurrence of a string.
     170             :      * Return substring of the CStr after the first occurrence of the search string.
     171             :      *
     172             :      * @param const CStr & str reference to search string
     173             :      * @param size_t startPos character position to start searching from
     174             :      * @return CStr substring remaining after match
     175             :      *                   the CStr if no match is found
     176             :      **/
     177             :     CStr AfterFirst(const CStr& str, size_t startPos = 0) const;
     178             : 
     179             :     /**
     180             :      * Retrieve substring of the CStr preceding first occurrence of a string.
     181             :      * Return substring of the CStr preceding the first occurrence of the search string.
     182             :      *
     183             :      * @param const CStr & str reference to search string
     184             :      * @param size_t startPos character position to start searching from
     185             :      * @return CStr substring preceding before match
     186             :      *                   the CStr if no match is found
     187             :      **/
     188             :     CStr BeforeFirst(const CStr& str, size_t startPos = 0) const;
     189             : 
     190             :     /**
     191             :      * Remove all occurrences of a string from the CStr.
     192             :      *
     193             :      * @param const CStr & str reference to search string to remove.
     194             :      **/
     195             :     void Remove(const CStr& str);
     196             : 
     197             :     /**
     198             :      * Convert strings to printable ASCII characters with JSON-style escapes.
     199             :      */
     200             :     std::string EscapeToPrintableASCII() const;
     201             : 
     202             :     /**
     203             :      * Return a trimmed copy of the CStr.
     204             :      *
     205             :      * @param PS_TRIM_MODE Mode value from trim mode enumeration.
     206             :      * @return CStr copy of trimmed CStr.
     207             :      **/
     208             :     CStr Trim(PS_TRIM_MODE mode) const;
     209             : 
     210             :     // Calculates a hash of the string's contents
     211             :     size_t GetHashCode() const;
     212             : 
     213             :     // Serialization functions
     214             :     // (These are not virtual or inherited from ISerializable, to avoid
     215             :     // adding a vtable and making the strings larger than std::string)
     216             :     size_t GetSerializedLength() const;
     217             :     std::uint8_t* Serialize(std::uint8_t* buffer) const;
     218             :     const std::uint8_t* Deserialize(const std::uint8_t* buffer, const std::uint8_t* bufferend);
     219             : };
     220             : 
     221             : namespace std
     222             : {
     223             : template <>
     224             : struct hash<CStr>
     225             : {
     226          76 :     std::size_t operator()(const CStr& str) const
     227             :     {
     228          76 :         return str.GetHashCode();
     229             :     }
     230             : };
     231             : }
     232             : 
     233             : #endif // INCLUDED_CSTR

Generated by: LCOV version 1.16