Skip to content

AStringView#

Represents a UTF-8 string view.

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

Detailed Description#

AStringView stores a pointer and size of constant 8-bit integer sequence representing UTF-8 code units. Each Unicode character (codepoint) is encoded using 1-4 consecutive code units, supporting the full Unicode standard.

Unicode provides comprehensive support for international writing systems and symbols.

AStringView points to constant data. For owning version of AStringView, see AString.

To work with raw bytes, use AStringView::bytes() function.

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.

length#


size_type AStringView::length()

Returns the number of Unicode characters in the string

sizeBytes#


size_type AStringView::sizeBytes()

Returns the number of bytes in the UTF-8 encoded string

substr#


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