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: 7 8 87.5 %
Date: 2022-06-14 00:41:00 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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 and Pad 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       87215 : class CStr : public std::tstring
      61             : {
      62             : public:
      63             :     using StrBase = std::tstring;
      64             :     using Char = typename std::tstring::value_type;
      65             : 
      66        2378 :     CStr() {}
      67        4261 :     CStr(const Char* str) : StrBase(str) {}
      68          36 :     CStr(const Char* str, size_t len) : StrBase(str, len) {}
      69       75055 :     CStr(const StrBase& str) : StrBase(str) {}
      70             :     template<class InputIterator>
      71         462 :     CStr (InputIterator first, InputIterator last) : StrBase(first, last) {}
      72             : 
      73             :     /**
      74             :      * Repeat: Named constructor, to avoid overload overload.
      75             :      *
      76             :      * @param const CStr & str reference to another CStr object to be repeated for initialization
      77             :      * @param size_t Reps number of times to repeat the initialization
      78             :      * @return CStr new CStr object
      79             :      **/
      80             :     static CStr Repeat(const CStr& str, size_t reps);
      81             : 
      82             :     /**
      83             :      * Construction from u16strings.
      84             :      *
      85             :      * @param u16string String u16string to be used for initialization.
      86             :      **/
      87             :     explicit CStr(const std::u16string& str) : StrBase(str.begin(), str.end()) {}
      88             : 
      89             :     // Conversion to/from UTF-8, encoded in a CStr8.
      90             :     // Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
      91             :     // above U+FFFF) are silently replaced with U+FFFD.
      92             :     #ifdef _UNICODE
      93             :         CStr8 ToUTF8() const;
      94             :     #else
      95             :         CStrW FromUTF8() const;
      96             :     #endif
      97             : 
      98             :     // Conversions:
      99             : 
     100             :     static CStr FromInt(int n);
     101             :     static CStr FromUInt(unsigned int n);
     102             :     static CStr FromInt64(i64 n);
     103             :     static CStr FromDouble(double n);
     104             : 
     105             :     /**
     106             :      * Return CStr as Integer.
     107             :      * Conversion is from the beginning of CStr.
     108             :      *
     109             :      * @return int CStr represented as an integer.
     110             :      **/
     111             :     int ToInt() const;
     112             :     /**
     113             :      * Return CStr as Unsigned Integer.
     114             :      * Conversion is from the beginning of CStr.
     115             :      *
     116             :      * @return unsigned int CStr represented as an unsigned integer.
     117             :      **/
     118             :     unsigned int ToUInt() const;
     119             :     /**
     120             :      * Return CStr as Long.
     121             :      * Conversion is from the beginning of CStr.
     122             :      *
     123             :      * @return long CStr represented as a long.
     124             :      **/
     125             :     long ToLong() const;
     126             :     /**
     127             :      * Return CStr as Unsigned Long.
     128             :      * Conversion is from the beginning of CStr.
     129             :      *
     130             :      * @return unsigned long CStr represented as an unsigned long.
     131             :      **/
     132             :     unsigned long ToULong() const;
     133             :     /**
     134             :      * Return CStr as Float.
     135             :      * Conversion is from the beginning of CStr.
     136             :      *
     137             :      * @return float CStr represented as a float.
     138             :      **/
     139             :     float ToFloat() const;
     140             :     /**
     141             :      * Return CStr as Double.
     142             :      * Conversion is from the beginning of CStr.
     143             :      *
     144             :      * @return double CStr represented as a double.
     145             :      **/
     146             :     double ToDouble() const;
     147             : 
     148             :     /**
     149             :      * Search the CStr for another string.
     150             :      * The search is case-sensitive.
     151             :      *
     152             :      * @param const CStr & str reference to the search string
     153             :      * @return long offset into the CStr of the first occurrence of the search string
     154             :      *              -1 if the search string is not found
     155             :      **/
     156             :     long Find(const CStr& str) const;
     157             :     /**
     158             :      * Search the CStr for another string.
     159             :      * The search is case-sensitive.
     160             :      *
     161             :      * @param const {t|w}char_t & chr reference to the search string
     162             :      * @return long offset into the CStr of the first occurrence of the search string
     163             :      *              -1 if the search string is not found
     164             :      **/
     165             :     long Find(const Char chr) const;
     166             :     /**
     167             :      * Search the CStr for another string with starting offset.
     168             :      * The search is case-sensitive.
     169             :      *
     170             :      * @param const int & start character offset into CStr to begin search
     171             :      * @param const {t|w}char_t & chr reference to the search string
     172             :      * @return long offset into the CStr of the first occurrence of the search string
     173             :      *              -1 if the search string is not found
     174             :      **/
     175             :     long Find(const int start, const Char chr) const;
     176             : 
     177             :     /**
     178             :      * Search the CStr for another string.
     179             :      * The search is case-insensitive.
     180             :      *
     181             :      * @param const CStr & str reference to the search string
     182             :      * @return long offset into the CStr of the first occurrence of the search string
     183             :      *              -1 if the search string is not found
     184             :      **/
     185             :     long FindInsensitive(const CStr& str) const;
     186             :     /**
     187             :      * Search the CStr for another string.
     188             :      * The search is case-insensitive.
     189             :      *
     190             :      * @param const {t|w}char_t & chr reference to the search string
     191             :      * @return long offset into the CStr of the first occurrence of the search string
     192             :      *              -1 if the search string is not found
     193             :      **/
     194             :     long FindInsensitive(const Char chr) const;
     195             :     /**
     196             :      * Search the CStr for another string with starting offset.
     197             :      * The search is case-insensitive.
     198             :      *
     199             :      * @param const int & start character offset into CStr to begin search
     200             :      * @param const {t|w}char_t & chr reference to the search string
     201             :      * @return long offset into the CStr of the first occurrence of the search string
     202             :      *              -1 if the search string is not found
     203             :      **/
     204             :     long FindInsensitive(const int start, const Char chr) const;
     205             : 
     206             :     /**
     207             :      * Search the CStr for another string.
     208             :      * The search is case-sensitive.
     209             :      *
     210             :      * @param const CStr & str reference to the search string
     211             :      * @return long offset into the CStr of the last occurrence of the search string
     212             :      *              -1 if the search string is not found
     213             :      **/
     214             :     long ReverseFind(const CStr& str) const;
     215             : 
     216             :     /**
     217             :      * Make a copy of the CStr in lower-case.
     218             :      *
     219             :      * @return CStr converted copy of CStr.
     220             :      **/
     221             :     CStr LowerCase() const;
     222             :     /**
     223             :      * Make a copy of the CStr in upper-case.
     224             :      *
     225             :      * @return CStr converted copy of CStr.
     226             :      **/
     227             :     CStr UpperCase() const;
     228             : 
     229             :     /**
     230             :      * Retrieve first n characters of the CStr.
     231             :      *
     232             :      * @param size_t len the number of characters to retrieve.
     233             :      * @return CStr retrieved substring.
     234             :      **/
     235             :     CStr Left(size_t len) const;
     236             : 
     237             :     /**
     238             :      * Retrieve last n characters of the CStr.
     239             :      *
     240             :      * @param size_t len the number of characters to retrieve.
     241             :      * @return CStr retrieved substring.
     242             :      **/
     243             :     CStr Right(size_t len) const;
     244             : 
     245             :     /**
     246             :      * Retrieve substring of the CStr after last occurrence of a string.
     247             :      * Return substring of the CStr after the last occurrence of the search string.
     248             :      *
     249             :      * @param const CStr & str reference to search string
     250             :      * @param size_t startPos character position to start searching from
     251             :      * @return CStr substring remaining after match
     252             :      *                   the CStr if no match is found
     253             :      **/
     254             :     CStr AfterLast(const CStr& str, size_t startPos = npos) const;
     255             : 
     256             :     /**
     257             :      * Retrieve substring of the CStr preceding last occurrence of a string.
     258             :      * Return substring of the CStr preceding the last occurrence of the search string.
     259             :      *
     260             :      * @param const CStr & str reference to search string
     261             :      * @param size_t startPos character position to start searching from
     262             :      * @return CStr substring preceding before match
     263             :      *                   the CStr if no match is found
     264             :      **/
     265             :     CStr BeforeLast(const CStr& str, size_t startPos = npos) const;
     266             : 
     267             :     /**
     268             :      * Retrieve substring of the CStr after first occurrence of a string.
     269             :      * Return substring of the CStr after the first occurrence of the search string.
     270             :      *
     271             :      * @param const CStr & str reference to search string
     272             :      * @param size_t startPos character position to start searching from
     273             :      * @return CStr substring remaining after match
     274             :      *                   the CStr if no match is found
     275             :      **/
     276             :     CStr AfterFirst(const CStr& str, size_t startPos = 0) const;
     277             : 
     278             :     /**
     279             :      * Retrieve substring of the CStr preceding first occurrence of a string.
     280             :      * Return substring of the CStr preceding the first occurrence of the search string.
     281             :      *
     282             :      * @param const CStr & str reference to search string
     283             :      * @param size_t startPos character position to start searching from
     284             :      * @return CStr substring preceding before match
     285             :      *                   the CStr if no match is found
     286             :      **/
     287             :     CStr BeforeFirst(const CStr& str, size_t startPos = 0) const;
     288             : 
     289             :     /**
     290             :      * Remove all occurrences of a string from the CStr.
     291             :      *
     292             :      * @param const CStr & str reference to search string to remove.
     293             :      **/
     294             :     void Remove(const CStr& str);
     295             : 
     296             :     /**
     297             :      * Replace all occurrences of one string by another string in the CStr.
     298             :      *
     299             :      * @param const CStr & strToReplace reference to search string.
     300             :      * @param const CStr & replaceWith reference to replace string.
     301             :      **/
     302             :     void Replace(const CStr& toReplace, const CStr& replaceWith);
     303             : 
     304             :     /**
     305             :      * Convert strings to printable ASCII characters with JSON-style escapes.
     306             :      */
     307             :     std::string EscapeToPrintableASCII() const;
     308             : 
     309             :     /**
     310             :      * Return a trimmed copy of the CStr.
     311             :      *
     312             :      * @param PS_TRIM_MODE Mode value from trim mode enumeration.
     313             :      * @return CStr copy of trimmed CStr.
     314             :      **/
     315             :     CStr Trim(PS_TRIM_MODE mode) const;
     316             : 
     317             :     /**
     318             :      * Return a space padded copy of the CStr.
     319             :      *
     320             :      * @param PS_TRIM_MODE Mode value from trim mode enumeration.
     321             :      * @param size_t Length number of pad spaces to add
     322             :      * @return CStr copy of padded CStr.
     323             :      **/
     324             :     CStr Pad(PS_TRIM_MODE mode, size_t len) const;
     325             : 
     326             :     // Conversion to u16string
     327             :     std::u16string utf16() const { return std::u16string(begin(), end()); }
     328             : 
     329             :     // Calculates a hash of the string's contents
     330             :     size_t GetHashCode() const;
     331             : 
     332             :     // Serialization functions
     333             :     // (These are not virtual or inherited from ISerializable, to avoid
     334             :     // adding a vtable and making the strings larger than std::string)
     335             :     size_t GetSerializedLength() const;
     336             :     u8* Serialize(u8* buffer) const;
     337             :     const u8* Deserialize(const u8* buffer, const u8* bufferend);
     338             : };
     339             : 
     340             : namespace std
     341             : {
     342             : template <>
     343             : struct hash<CStr>
     344             : {
     345           0 :     std::size_t operator()(const CStr& str) const
     346             :     {
     347          84 :         return str.GetHashCode();
     348             :     }
     349             : };
     350             : }
     351             : 
     352             : #endif // INCLUDED_CSTR

Generated by: LCOV version 1.13