1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* Copyright (C) 2014 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* File : CStr.h
* Project : engine
* Description : Contains CStr class which is a versatile class for making string use easy.
* : The class implements a series of string manipulation/formatting functions.
**/
#ifndef INCLUDED_CSTR
#define INCLUDED_CSTR
/**
* Whitespace trim identifier for Trim and Pad functions
**/
enum PS_TRIM_MODE
{
PS_TRIM_LEFT, /// Trim all white space from the beginning of the string
PS_TRIM_RIGHT, /// Trim all white space from the end of the string
PS_TRIM_BOTH /// Trim all white space from the beginning and end of the string
};
#ifndef IN_UNIDOUBLER
#define UNIDOUBLER_HEADER "CStr.h"
#include "UniDoubler.h"
#endif
#endif
// Include this section when in unidoubler mode, and when this unicode/ascii
// version has not already been included.
#if defined(IN_UNIDOUBLER) && ( (defined(_UNICODE) && !defined(CSTR_H_U)) || (!defined(_UNICODE) && !defined(CSTR_H_A)) )
#ifdef _UNICODE
#define CSTR_H_U
#else
#define CSTR_H_A
#endif
#include <string>
#include "ps/utf16string.h"
class CStr8;
class CStrW;
/**
* The base class of all strings
**/
class CStr: public std::tstring
{
public:
// CONSTRUCTORS
CStr() {}
CStr(const tchar* String) : std::tstring(String) {}
CStr(const tchar* String, size_t Length) : std::tstring(String, Length) {}
CStr(const std::tstring& String) : std::tstring(String) {}
template <class InputIterator>
CStr (InputIterator first, InputIterator last) : std::tstring(first, last) {}
/**
* Repeat: Named constructor, to avoid overload overload.
*
* @param const CStr & String reference to another CStr object to be repeated for initialization
* @param size_t Reps number of times to repeat the initialization
* @return CStr new CStr object
**/
static CStr Repeat(const CStr& String, size_t Reps);
/**
* Construction from utf16strings.
*
* @param utf16string String utf16string to be used for initialization.
**/
explicit CStr(const utf16string& String) : std::tstring(String.begin(), String.end()) {}
// Conversion to/from UTF-8, encoded in a CStr8.
// Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
// above U+FFFF) are silently replaced with U+FFFD.
#ifdef _UNICODE
CStr8 ToUTF8() const;
#else
CStrW FromUTF8() const;
#endif
// Conversions:
static CStr FromInt(int n);
static CStr FromUInt(unsigned int n);
static CStr FromInt64(i64 n);
static CStr FromDouble(double n);
/**
* Return CStr as Integer.
* Conversion is from the beginning of CStr.
*
* @return int CStr represented as an integer.
**/
int ToInt() const;
/**
* Return CStr as Unsigned Integer.
* Conversion is from the beginning of CStr.
*
* @return unsigned int CStr represented as an unsigned integer.
**/
unsigned int ToUInt() const;
/**
* Return CStr as Long.
* Conversion is from the beginning of CStr.
*
* @return long CStr represented as a long.
**/
long ToLong() const;
/**
* Return CStr as Unsigned Long.
* Conversion is from the beginning of CStr.
*
* @return unsigned long CStr represented as an unsigned long.
**/
unsigned long ToULong() const;
/**
* Return CStr as Float.
* Conversion is from the beginning of CStr.
*
* @return float CStr represented as a float.
**/
float ToFloat() const;
/**
* Return CStr as Double.
* Conversion is from the beginning of CStr.
*
* @return double CStr represented as a double.
**/
double ToDouble() const;
/**
* Search the CStr for another string.
* The search is case-sensitive.
*
* @param const CStr & Str reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long Find(const CStr& Str) const;
/**
* Search the CStr for another string.
* The search is case-sensitive.
*
* @param const {t|w}char_t & chr reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long Find(const tchar chr) const;
/**
* Search the CStr for another string with starting offset.
* The search is case-sensitive.
*
* @param const int & start character offset into CStr to begin search
* @param const {t|w}char_t & chr reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long Find(const int start, const tchar chr) const;
/**
* Search the CStr for another string.
* The search is case-insensitive.
*
* @param const CStr & Str reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long FindInsensitive(const CStr& Str) const;
/**
* Search the CStr for another string.
* The search is case-insensitive.
*
* @param const {t|w}char_t & chr reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long FindInsensitive(const tchar chr) const;
/**
* Search the CStr for another string with starting offset.
* The search is case-insensitive.
*
* @param const int & start character offset into CStr to begin search
* @param const {t|w}char_t & chr reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long FindInsensitive(const int start, const tchar chr) const;
/**
* Search the CStr for another string.
* The search is case-sensitive.
*
* @param const CStr & Str reference to the search string
* @return long offset into the CStr of the last occurrence of the search string
* -1 if the search string is not found
**/
long ReverseFind(const CStr& Str) const;
/**
* Make a copy of the CStr in lower-case.
*
* @return CStr converted copy of CStr.
**/
CStr LowerCase() const;
/**
* Make a copy of the CStr in upper-case.
*
* @return CStr converted copy of CStr.
**/
CStr UpperCase() const;
/**
* Retrieve first n characters of the CStr.
*
* @param size_t len the number of characters to retrieve.
* @return CStr retrieved substring.
**/
CStr Left(size_t len) const;
/**
* Retrieve last n characters of the CStr.
*
* @param size_t len the number of characters to retrieve.
* @return CStr retrieved substring.
**/
CStr Right(size_t len) const;
/**
* Retrieve substring of the CStr after last occurrence of a string.
* Return substring of the CStr after the last occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring remaining after match
* the CStr if no match is found
**/
CStr AfterLast(const CStr& Str, size_t startPos = npos) const;
/**
* Retrieve substring of the CStr preceding last occurrence of a string.
* Return substring of the CStr preceding the last occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring preceding before match
* the CStr if no match is found
**/
CStr BeforeLast(const CStr& Str, size_t startPos = npos) const;
/**
* Retrieve substring of the CStr after first occurrence of a string.
* Return substring of the CStr after the first occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring remaining after match
* the CStr if no match is found
**/
CStr AfterFirst(const CStr& Str, size_t startPos = 0) const;
/**
* Retrieve substring of the CStr preceding first occurrence of a string.
* Return substring of the CStr preceding the first occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring preceding before match
* the CStr if no match is found
**/
CStr BeforeFirst(const CStr& Str, size_t startPos = 0) const;
/**
* Remove all occurrences of a string from the CStr.
*
* @param const CStr & Str reference to search string to remove.
**/
void Remove(const CStr& Str);
/**
* Replace all occurrences of one string by another string in the CStr.
*
* @param const CStr & StrToReplace reference to search string.
* @param const CStr & ReplaceWith reference to replace string.
**/
void Replace(const CStr& StrToReplace, const CStr& ReplaceWith);
/**
* Convert strings to printable ASCII characters with JSON-style escapes.
*/
std::string EscapeToPrintableASCII() const;
/**
* Return a trimmed copy of the CStr.
*
* @param PS_TRIM_MODE Mode value from trim mode enumeration.
* @return CStr copy of trimmed CStr.
**/
CStr Trim(PS_TRIM_MODE Mode) const;
/**
* Return a space padded copy of the CStr.
*
* @param PS_TRIM_MODE Mode value from trim mode enumeration.
* @param size_t Length number of pad spaces to add
* @return CStr copy of padded CStr.
**/
CStr Pad(PS_TRIM_MODE Mode, size_t Length) const;
// Conversion to utf16string
utf16string utf16() const { return utf16string(begin(), end()); }
// Calculates a hash of the string's contents
size_t GetHashCode() const;
// Serialization functions
// (These are not virtual or inherited from ISerializable, to avoid
// adding a vtable and making the strings larger than std::string)
size_t GetSerializedLength() const;
u8* Serialize(u8* buffer) const;
const u8* Deserialize(const u8* buffer, const u8* bufferend);
};
static inline size_t hash_value(const CStr& s)
{
return s.GetHashCode();
}
#endif
|