Skip to content

AStringView#

Non-owning view into a UTF-8 string - a lightweight const reference to character data you don't own.

Header:#include <AUI/Common/AStringView.h>
CMake:aui_link(my_target PUBLIC aui::core)

Detailed Description#

AStringView is the read-only counterpart to @ref AString. It inherits from std::string_view and holds only a pointer and a length - no heap allocation, no copy. This makes it the correct type to use for any function parameter that only needs to read a string, regardless of whether the caller holds an AString, a std::string, a const char*, or a string literal.

Why AStringView exists#

Before non-owning views, a function that accepted a string had two bad options: - Take const AString& - forces the caller to own an AString; a raw const char* or std::string triggers a silent heap allocation just to call the function. - Take const char* - loses the length, breaks on embedded nulls, and can't accept std::string without .c_str().

AStringView eliminates both problems. It constructs implicitly from const char*, std::string_view, AString, and char8_t* with zero overhead, so callers pass whatever they already have:

Examples#

examples/7guis/flight_booker/src/main.cpp

7GUIs Flight Booker - Flight Booker.

};

auto formatDate(system_clock::time_point date) { return "{0:%d}.{0:%m}.{0:%G}"_format(date); }

AOptional<system_clock::time_point> parseDate(AStringView s) {
    auto std = s.bytes();
    auto match = REGEX_DATE.match(std);
    if (!match) {
        return std::nullopt;
    }

Public Methods#

data#


constexpr const char* AStringView::data()

Raw const char* pointer to the string.

Returns
A pointer to the first character of the string.

The returned pointer is not guaranteed to be valid C-style string, because we can't insert a null character at the end.

encode#


AByteBuffer AStringView::encode(AStringEncoding encoding)

Encodes the string into a null-terminated byte buffer using the specified encoding.

substr#


constexpr AStringView AStringView::substr(size_type pos = 0, size_type count = npos)

Returns a substring [pos, pos + count).

Arguments
pos
The starting position of the substring.
count
The number of characters to include in the substring. If the requested substring extends past the end of the string, i.e. the count is greater than `size() - pos` (e.g. if `count == npos`), the returned substring is `[pos, size())`. Since AString encapsulates a UTF-8 encoded string, the returned substring is always valid UTF-8, hence, it operates on top of UTF-8 code points. `pos` and `count` are interpreted as code points positions, not as byte.

toBool#


bool AStringView::toBool()

Converts the string to boolean value.

Returns
If the string equals to "true", true returned, false otherwise.

toDouble#


AOptional<double> AStringView::toDouble()

Converts the string to a double number.

Returns
The string converted to a double number. If conversion to int is not possible, nullopt is returned.

Examples:

examples/7guis/cells/src/Formula.cpp

7GUIs Cells - Spreadsheet processor (Excel).

formula::Precompiled formula::precompile(const AString& expression) {
    if (expression.empty()) {
        return [](const Spreadsheet&) { return std::nullopt; };
    }
    if (auto d = expression.toDouble()) {
        return [d = *d](const Spreadsheet&) { return d; };
    }
    if (!expression.startsWith('=')) {
        return [stringConstant = expression](const Spreadsheet&) { return stringConstant; };
    }

toFloat#


AOptional<float> AStringView::toFloat()

Converts the string to a float number.

Returns
The string converted to a float number. If conversion to int is not possible, nullopt is returned.

toInt#


AOptional<int32_t> AStringView::toInt()

Converts the string to int value.

Returns
The string converted to an integer value using base 10. If the string starts with 0x or 0X, the base 16 used. If conversion to int is not possible, nullopt is returned.

toLong#


AOptional<int64_t> AStringView::toLong()

Converts the string to long value.

Returns
The string converted to an integer value using base 10. If the string starts with 0x or 0X, the base 16 used. If conversion to long is not possible, nullopt is returned.

toNumber#


AOptional<int> AStringView::toNumber(aui::ranged_number<int, 2, 36> base)

Returns the string converted to an int using base. Returns std::nullopt if the conversion fails.

toUInt#


AOptional<uint32_t> AStringView::toUInt()

Converts the string to unsigned int value.

Returns
The string converted to an integer value using base 10. If the string starts with 0x or 0X, the base 16 used. If conversion to unsigned int is not possible, exception is thrown.

toULong#


AOptional<uint64_t> AStringView::toULong()

Converts the string to unsigned long value.

Returns
The string converted to an integer value using base 10. If the string starts with 0x or 0X, the base 16 used. If conversion to unsigned long is not possible, exception is thrown.

toUtf16#


std::u16string AStringView::toUtf16()

Encodes the UTF-8 string into a UTF-16 string

toUtf32#


std::u32string AStringView::toUtf32()

Encodes the UTF-8 string into a UTF-32 string