resonance 1.0.1
Loading...
Searching...
No Matches
hxstr.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4
5#include "os/mem.h"
6
19extern const char *g_szEmptyString;
20
41class HxStr {
42public:
51#ifdef VIDEO_STANDARD_PAL
52 HxStr() : mLen(0), mStr(const_cast<char *>(g_szEmptyString)) {
53 }
54#else
55 HxStr() : mLen(0), mStr(nullptr) {
56 }
57#endif
58
68 HxStr(const char *pszText);
69
81 HxStr(const HxStr &other);
82
91 HxStr(unsigned nCount, char ch);
92
94#ifdef VIDEO_STANDARD_PAL
95 if (mStr != g_szEmptyString && mStr != nullptr) {
96 delete[] mStr;
97 }
98#else
99 if (mStr != nullptr) {
100 delete[] mStr;
101 }
102#endif
103 }
104
113 HxStr &operator+=(const HxStr &other);
114
134 HxStr &operator+=(const char *pszText) {
135 return *this += HxStr(pszText);
136 }
137
146 HxStr &operator+=(char ch);
147
156 HxStr &operator=(const char *pszText);
157
166 HxStr &operator=(const HxStr &other);
167
178 char operator[](unsigned i) const;
179
191 bool operator!=(const char *pszRight) const;
192
201 bool operator!=(const HxStr &right) const;
202
213 bool operator==(const char *pszRight) const;
214
223 bool operator==(const HxStr &right) const;
224
235 bool operator<(const HxStr &right) const;
236
248 void Alloc(unsigned nLen);
249
258 int Find(char ch) const;
259
269 int Find(char ch, unsigned nStart) const;
270
279 int Find(const char *pszText) const;
280
289 int ReverseFind(char ch) const;
290
300 int ReverseFindOneOf(const char *pszChars) const;
301
314 int Compare(unsigned pos, unsigned len, const char *str) const;
315
324 HxStr Mid(unsigned pos) const;
325
337 HxStr Mid(unsigned pos, unsigned len) const;
338
352 HxStr &Replace(unsigned pos, unsigned len, char ch);
353
366 HxStr &Replace(unsigned pos, unsigned len, const HxStr &other);
367
379
391 HxStr &Truncate(unsigned pos);
392
405 HxStr &Erase(unsigned pos, unsigned len);
406
417 HxStr &Insert(unsigned pos, unsigned nCount, char ch);
418
428 HxStr &Insert(unsigned pos, const HxStr &other);
429
441 std::ostream &Print(std::ostream &stream) const;
442
453 unsigned mLen;
454
464 char *mStr;
465
466private:
467 // Adopts a buffer of nLen + 1 bytes that the caller has already filled and terminated. The
468 // name is inferred. The constructor is inlined at its one call site, in Mid().
469 HxStr(char *pOwnedText, unsigned nLen) : mLen(nLen), mStr(pOwnedText) {
470 }
471};
472
492inline HxStr operator+(const HxStr &left, const HxStr &right) {
493 HxStr result(left);
494 result += right;
495 return result;
496}
497
511inline HxStr operator+(const HxStr &left, const char *pszRight) {
512 HxStr result(left);
513 result += pszRight;
514 return result;
515}
516
528inline HxStr operator+(const HxStr &left, char ch) {
529 HxStr result(left);
530 result += ch;
531 return result;
532}
533
546extern const unsigned g_nHxStrNoPosition;
547
560std::ostream &operator<<(std::ostream &stream, const HxStr &text);
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
HxStr & operator+=(const HxStr &other)
Append another string.
unsigned mLen
Length excluding the terminator.
Definition hxstr.h:453
HxStr(const HxStr &other)
Construct a copy of another string.
bool operator==(const char *pszRight) const
Compare against a C string for equality.
HxStr Mid(unsigned pos, unsigned len) const
Extract a run of text.
HxStr & Truncate(unsigned pos)
Shorten the string in place.
int Compare(unsigned pos, unsigned len, const char *str) const
Compare a run of this string against a C string.
HxStr & Erase(unsigned pos, unsigned len)
Remove a run of text.
bool operator!=(const char *pszRight) const
Compare against a C string for inequality.
HxStr & operator+=(char ch)
Append one character.
~HxStr()
Definition hxstr.h:93
std::ostream & Print(std::ostream &stream) const
Write this string to a stream.
bool operator==(const HxStr &right) const
Compare against another string for equality.
HxStr Mid(unsigned pos) const
Extract the text from a position to the end.
bool operator!=(const HxStr &right) const
Compare against another string for inequality.
HxStr & operator+=(const char *pszText)
Append a C string.
Definition hxstr.h:134
HxStr & operator=(const char *pszText)
Replace this string with a copy of a C string.
int Find(const char *pszText) const
Find the first occurrence of a substring.
int ReverseFind(char ch) const
Find the last occurrence of a character.
HxStr & operator=(const HxStr &other)
Replace this string with a copy of another.
int Find(char ch, unsigned nStart) const
Find the first occurrence of a character at or after a position.
int Find(char ch) const
Find the first occurrence of a character.
char operator[](unsigned i) const
Read one character.
void Alloc(unsigned nLen)
Discard the text and reserve a zero-filled buffer.
HxStr & Insert(unsigned pos, const HxStr &other)
Insert another string.
HxStr & Insert(unsigned pos, unsigned nCount, char ch)
Insert a run of one repeated character.
HxStr(const char *pszText)
Construct a copy of a C string.
char * mStr
Text buffer.
Definition hxstr.h:464
int ReverseFindOneOf(const char *pszChars) const
Find the last occurrence of any of a set of characters.
bool operator<(const HxStr &right) const
Order against another string.
HxStr & Clear()
Empty the string in place.
HxStr & Replace(unsigned pos, unsigned len, const HxStr &other)
Overwrite a run of text with another string.
HxStr()
Construct an empty string.
Definition hxstr.h:55
HxStr & Replace(unsigned pos, unsigned len, char ch)
Overwrite one character.
HxStr(unsigned nCount, char ch)
Construct a run of one repeated character.
const unsigned g_nHxStrNoPosition
Sentinel a search returns when it finds nothing.
const char * g_szEmptyString
Text an empty HxStr points at, and the substitute for a null buffer.
HxStr operator+(const HxStr &left, const HxStr &right)
Concatenate two strings.
Definition hxstr.h:492
std::ostream & operator<<(std::ostream &stream, const HxStr &text)
Write a string to a stream.