Skip to content

TStringConverter

Jay edited this page Oct 3, 2026 · 2 revisions

TStringConverter<...>

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

Template: template <typename TDest, typename TSrc>

A template class for converting strings between different character types. @tparam TDest The destination character type. @tparam TSrc The source character type.

Methods

inline size_t measure(const TReadOnlySpan<TSrc>& src)

Measures the length of the source string span.

  • src --- The source string span.

Returns: The length of the source string span.

inline size_t convert(const TSpan<TDest>& dst, const TReadOnlySpan<TSrc>& src)

Converts a source string span to a destination string span.

  • dst --- The destination string span.
  • src --- The source string span.

Returns: The number of characters converted.

Example

Transcodes between char and wchar_t one span at a time -- what TString::convertTo() uses underneath. The template arguments are <destination, source> in that order, and measure() has to run before convert() because the destination length is not the source length.

// given: const CString& narrow
TStringConverter<wchar_t, char> conv;

TReadOnlySpan<char> src = narrow.toSpan();
const size_t units = conv.measure(src);
if (units == 0) {
    return;
}

CWideString wide;
if (!wide.resize(units)) {
    return;
}

// An instance carries an mbstate_t, so one converter belongs to one conversion: reuse it
// for a second, unrelated string and it resumes mid-sequence.
TSpan<wchar_t> dst = wide.toSpan();
const size_t converted = conv.convert(dst, src);
if (converted != units) {
    wide.clear();
}

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