Ch9.12: string

Overview

::fast_io::string is a dynamic, null-terminated character sequence. This section covers the essentials: character type variants, concat_fast_io, and to<T>.

The string type has many additional operations (search, trim, compare, etc.). You can skip the details for now and return to the Appendix §9.12.1 when you need them.

1. Character Type Variants

Alias Character Type Allocator
::fast_io::string char Global (default)
::fast_io::wstring wchar_t Global
::fast_io::u8string char8_t Global
::fast_io::u16string char16_t Global
::fast_io::u32string char32_t Global

Thread-local allocator variants are available in the ::fast_io::tlc namespace: ::fast_io::tlc::string, ::fast_io::tlc::wstring, etc.

2. concat_fast_io — String Concatenation

concat_fast_io formats all its arguments into a new string and returns it. It is the string-producing counterpart to print:


#include <fast_io_dsal/string.h>
#include <fast_io.h>

int main() {
    // Concatenate mixed types into a string
    ::fast_io::string s = ::fast_io::concat_fast_io(
        "Hello, ", "user #", 42, " (score: ", 3.14, ")");
    // s == "Hello, user #42 (score: 3.14)"

    ::fast_io::io::println(s);

    // concatln_fast_io appends a newline
    ::fast_io::string line = ::fast_io::concatln_fast_io("done");
    // line == "done\n"

    // Wide-string variants
    ::fast_io::wstring ws = ::fast_io::wconcat_fast_io(L"wide: ", 100);

    // UTF-8, UTF-16, UTF-32 variants
    ::fast_io::u8string u8s = ::fast_io::u8concat_fast_io(u8"utf8: ", 100);
    ::fast_io::u16string u16s = ::fast_io::u16concat_fast_io(u"utf16: ", 100);
    ::fast_io::u32string u32s = ::fast_io::u32concat_fast_io(U"utf32: ", 100);
}

3. to<T> — Parse from Print

to<T> is the reverse of print: it formats its arguments, then parses the result into type T. Think of it as sprintf + sscanf in one step, but compile-time optimised:


#include <fast_io.h>

int main() {
    // Print 42, parse it back as a double
    double d = ::fast_io::to<double>(42); // d == 42.0

    // Concatenate then parse
    ::std::size_t n = ::fast_io::to<::std::size_t>(1zu, 23zu); // "1" + "23" -> 123

    // In-place version (writes into existing variable)
    double x{};
    ::fast_io::inplace_to(x, "3.14");
    // x == 3.14

    // Wide-string variants
    ::std::size_t w = ::fast_io::wto<::std::size_t>(L"42");
}

Key takeaways