Skip to content

TString

Jay edited this page Oct 3, 2026 · 2 revisions

TString<...>

#include <certpp/string.hpp> --- certpp

Template: template <typename T = char>

A template class representing a string of characters of type T. By default, T is char, but it can be specialized for other character types. @tparam T The character type of the string. Defaults to char.

Methods

TString()

Default constructor initializes an empty TString.

TString(const SelfType& other)

Copy constructor.

  • other --- The TString instance to copy from.

TString(SelfType&& other)

Move constructor.

  • other --- The TString instance to move from.

TString(const T* cStr)

Constructs a TString from a null-terminated C-style string.

  • cStr --- The null-terminated string to copy from.

TString(const T* data, size_t limit)

Constructs a TString from a character array with a specified limit. The array is not required to be null-terminated; up to limit characters are copied.

  • data --- Pointer to the character array to copy from.
  • limit --- The maximum number of characters to copy.

template<typename U> TString(const TString<U>& cStr)

Template copy constructor for converting between different character types.

  • cStr --- The TString instance of a different character type to copy from.

~TString()

Destructor. Clears the TString and releases any allocated memory.

inline size_t capacity() const

Returns the capacity of the TString.

inline size_t size() const

Returns the size of the TString.

inline bool empty() const

Checks if the TString is empty.

inline const T* toPtr() const

Returns a pointer to the underlying character array. This can be null when empty.

inline T* toPtr()

Returns a mutable pointer to the underlying character array. This can be null when empty.

inline TSpan<T> toSpan()

Returns a reference to the underlying character array as a span. This can be empty when the TString is empty.

inline TReadOnlySpan<T> toSpan() const

Returns a const reference to the underlying character array as a span. This can be empty when the TString is empty.

inline operator bool() const

Conversion operator to bool. Returns true if the TString is not empty, false otherwise.

inline bool operator!() const

Logical NOT operator. Returns true if the TString is empty, false otherwise.

inline T operator[](size_t index) const

Subscript operator for accessing characters by index.

  • index --- The index of the character to access.

Returns: The character at the specified index.

inline SelfType& operator=(const SelfType& other)

Copy assignment operator.

  • other --- The TString instance to copy from.

inline SelfType& operator=(SelfType&& other) noexcept

Move assignment operator.

  • other --- The TString instance to move from.

inline SelfType& operator=(const T* cStr)

Assignment operator for C-style strings.

  • cStr --- The C-style string to assign from.

inline bool reserve(size_t capacity)

Reserves capacity for the TString. Returns true if the reservation was successful, false otherwise.

  • capacity --- The desired capacity to reserve.

inline bool trimExcess()

Trims the excess capacity of the TString. Returns true if the TString's capacity is minimal after the call (whether or not there was anything to trim), false only on actual allocation failure.

inline bool resize(size_t size)

Resizes the TString to the specified size. Returns true if the resizing was successful, false otherwise.

  • size --- The new size to resize the TString to.

inline bool clear()

Clears the TString, setting its size to zero. Returns true if the clearing was successful, false otherwise.

inline SelfType& append(const T* str, size_t limit = size_t(-1))

Appends the specified string to the TString. Returns the current instance.

  • str --- The string to append.
  • limit --- The maximum number of characters to append from the string.

Returns: The current instance after appending.

inline SelfType& append(const SelfType& other)

Appends the specified TString to the current TString. Returns the current instance.

  • other --- The TString to append.

Returns: The current instance after appending.

template<typename U> inline SelfType& append(const TString<U>& other)

Appends the specified TString of a different character type to the current TString. Returns the current instance. @tparam U The character type of the TString to append.

  • other --- The TString to append.

Returns: The current instance after appending.

inline SelfType& append(T ch)

Appends the specified character to the TString. Returns the current instance.

  • ch --- The character to append.

inline SelfType& erase(size_t start, size_t count = size_t(-1))

Erases a portion of the TString starting from the specified position. Returns the current instance.

  • start --- The starting position to begin erasing.
  • count --- The number of characters to erase. If not specified, erases until the end.

Returns: The current instance after erasing.

inline offset_t find(T ch, size_t skipBefore = 0) const

Finds the first occurrence of the specified character starting from the given position. Returns the offset of the character if found, or -1 if not found.

  • ch --- The character to find.
  • skipBefore --- The count to skip from the beginning when searching for the character.

Returns: The offset of the character if found, or -1 if not found.

inline offset_t find(const T* str, size_t skipBefore = 0) const

Finds the first occurrence of the specified substring starting from the given position. Returns the offset of the substring if found, or -1 if not found.

  • str --- The substring to find.
  • skipBefore --- The count to skip from the beginning when searching for the substring.

Returns: The offset of the substring if found, or -1 if not found.

inline offset_t findLast(T ch, size_t skipFromEnd = 0) const

Finds the last occurrence of the specified character starting from the given position from the end. Returns the offset of the character if found, or -1 if not found.

  • ch --- The character to find.
  • skipFromEnd --- The count to skip from the end when searching for the character.

Returns: The offset of the character if found, or -1 if not found.

inline offset_t findLast(const T* str, size_t skipBefore = 0) const

Finds the last occurrence of the specified substring starting from the given position from the end. Returns the offset of the substring if found, or -1 if not found.

  • str --- The substring to find.
  • skipBefore --- The count to skip from the beginning when searching for the substring.

Returns: The offset of the substring if found, or -1 if not found.

inline SelfType subString(size_t start, size_t length) const

Returns a substring of the current TString starting from the specified position with the given length. If the start position is out of bounds, an empty TString is returned. If the requested length exceeds the available size, it is adjusted accordingly.

  • start --- The starting position of the substring.
  • length --- The length of the substring.

Returns: A TString representing the substring.

inline SelfType subString(size_t start) const

Returns a substring of the current TString starting from the specified position to the end. If the start position is out of bounds, an empty TString is returned.

  • start --- The starting position of the substring.

Returns: A TString representing the substring from the start position to the end.

inline SelfType trim() const

Returns a trimmed version of the current TString, removing leading and trailing whitespace characters. If the current TString is empty, an empty TString is returned.

Returns: A TString representing the trimmed version of the current TString.

inline SelfType& trimSelf()

Trims the current TString in place, removing leading and trailing whitespace characters. If the current TString is empty, the current instance remains unchanged.

Returns: The current instance after trimming.

inline SelfType toLower() const

Returns a lowercase version of the current TString. If the current TString is empty, an empty TString is returned.

Returns: A TString representing the lowercase version of the current TString.

inline SelfType toUpper() const

Returns an uppercase version of the current TString. If the current TString is empty, an empty TString is returned.

Returns: A TString representing the uppercase version of the current TString.

inline SelfType& reverse()

Reverses the current TString in place. If the current TString is empty, the current instance is returned.

Returns: The current instance after reversing.

inline int32_t compare(const SelfType& other) const

Compares the current TString with another TString.

  • other --- The TString to compare with.

Returns: A negative value if the current TString is less than other, a positive value if greater, 0 if equal (magnitude beyond the sign isn't meaningful).

inline int32_t compareIgnoreCase(const SelfType& other) const

Compares the current TString with another TString, ignoring ASCII case (i.e. 'A'-'Z' are folded to 'a'-'z' before comparing; non-ASCII/non-letter characters compare as-is).

  • other --- The TString to compare with.

Returns: A negative value if the current TString is less than other, a positive value if greater, 0 if equal (the same "sign only" contract as compare()).

inline int32_t compare(const T* data, size_t limit = size_t(-1)) const

Compares the current TString with a raw character array, optionally limited to at most limit characters (a null terminator within that limit still ends the comparand, same as countOf()).

  • data --- The raw character array to compare with; may be null.
  • limit --- The maximum number of characters of data to consider. Defaults to unlimited, i.e. data is treated as a full null-terminated string.

Returns: A negative value if the current TString is less than data, a positive value if greater, 0 if equal (the same "sign only" contract as compare(const SelfType&)).

inline int32_t compareIgnoreCase(const T* data, size_t limit = size_t(-1)) const

Compares the current TString with a raw character array, ignoring ASCII case; see compareIgnoreCase(const SelfType&) and compare(const T*, limit) for the exact rules.

  • data --- The raw character array to compare with; may be null.
  • limit --- The maximum number of characters of data to consider. Defaults to unlimited, i.e. data is treated as a full null-terminated string.

Returns: A negative value if the current TString is less than data, a positive value if greater, 0 if equal (the same "sign only" contract as compare()).

inline bool operator==(const SelfType& other) const

Checks if the current TString is equal to another TString.

  • other --- The other TString to compare with.

Returns: true if both TStrings are equal, false otherwise.

inline bool operator!=(const SelfType& other) const

Checks if the current TString is not equal to another TString.

  • other --- The other TString to compare with.

Returns: true if both TStrings are not equal, false otherwise.

inline SelfType& operator+=(const SelfType& other)

Appends another TString to the current TString.

  • other --- The TString to append.

Returns: A reference to the current TString after appending.

inline SelfType& operator+=(const T* data)

Appends a C-style string to the current TString.

  • data --- The C-style string to append.

Returns: A reference to the current TString after appending.

template<typename U> inline TString<U> convertTo() const

Converts the current TString to a TString of a different character type. @tparam U The destination character type.

Returns: A TString of the destination character type.

Example

CString is the alias a caller names (CWideString for wchar_t). size() excludes the terminator, find() returns an offset_t that is negative when there is no match, and the growing calls report allocation failure with a bool.

// given: const CString& subject
CString line("CN=");
line.append(subject);

const offset_t eq = line.find('=');
if (eq < 0) {
    return;
}

CString value = line.subString(static_cast<size_t>(eq) + 1);
if (value.empty()) {
    return;
}

if (value.compareIgnoreCase(value.toUpper()) != 0) {
    return;
}

// convertTo() transcodes through TStringConverter rather than truncating each unit.
CWideString wide = value.convertTo<wchar_t>();
if (wide.empty()) {
    return;
}

Reference generated from certpp/string.hpp at commit 0834b29 (v1.0.1). The header is authoritative --- if this page disagrees with it, the header is right and this page needs regenerating.

Clone this wiki locally