Corrade::Utility::String namespace

String utilities.

This library is built if CORRADE_WITH_UTILITY is enabled when building Corrade. To use this library with CMake, request the Utility component of the Corrade package and link to the Corrade::Utility target.

find_package(Corrade REQUIRED Utility)

# ...
target_link_libraries(your-app PRIVATE Corrade::Utility)

See also Downloading and building Corrade and Using Corrade with CMake for more information.

Classes

class ParseResult new in Git master
String parse result.

Enums

enum class ParseState: std::uint8_t { Success, Clamped, Failed } new in Git master
String parse state.
enum class ParseDecimalFlag: std::uint8_t { DisallowSign = 1 << 0 } new in Git master
Decimal string parse flag.
enum class ParseHexadecimalFlag: std::uint8_t { DisallowSign = 1 << 0, AllowBasePrefix = 1 << 1, AllowHashPrefix = 1 << 2 } new in Git master
Hexadecimal string parse flag.
enum class ParseFloatFlag: std::uint8_t { DisallowSign = 1 << 0 } new in Git master
Float string parse flag.

Typedefs

using ParseDecimalFlags = Containers::EnumSet<ParseDecimalFlag> new in Git master
Decimal string parse flags.
using ParseHexadecimalFlags = Containers::EnumSet<ParseHexadecimalFlag> new in Git master
Hexadecimal string parse flags.
using ParseFloatFlags = Containers::EnumSet<ParseFloatFlag> new in Git master
Float string parse flags.

Functions

auto commonPrefix(Containers::StringView a, Containers::StringView b) -> Containers::StringView new in Git master
Longest common prefix of two strings.
void lowercaseInPlace(Containers::MutableStringView string) new in Git master
Convert ASCII characters in a string to lowercase, in place.
auto lowercase(Containers::StringView string) -> Containers::String new in Git master
Convert ASCII characters in a string to lowercase.
auto lowercase(Containers::String string) -> Containers::String new in Git master
void uppercaseInPlace(Containers::MutableStringView string) new in Git master
Convert ASCII characters in a string to uppercase, in place.
auto uppercase(Containers::StringView string) -> Containers::String new in Git master
Convert ASCII characters in a string to uppercase, in place.
auto uppercase(Containers::String string) -> Containers::String new in Git master
auto replaceFirst(Containers::StringView string, Containers::StringView search, Containers::StringView replace) -> Containers::String new in Git master
Replace first occurrence in a string.
auto replaceAll(Containers::StringView string, Containers::StringView search, Containers::StringView replace) -> Containers::String new in Git master
Replace all occurrences in a string.
auto replaceAll(Containers::String string, char search, char replace) -> Containers::String new in Git master
Replace all occurrences of a character in a string with another character.
void replaceAllInPlace(const Containers::MutableStringView string, const char search, const char replace) new in Git master
Replace all occurrences of a character in a string with another character in-place.
auto operator<<(Utility::Debug& debug, ParseState value) -> Utility::Debug& new in Git master
Debug output operator.
auto operator<<(Utility::Debug& debug, ParseDecimalFlag value) -> Utility::Debug& new in Git master
Debug output operator.
auto operator<<(Utility::Debug& debug, ParseDecimalFlags value) -> Utility::Debug& new in Git master
Debug output operator.
auto parseDecimal(Containers::StringView string, std::uint64_t& value, std::uint64_t min, std::uint64_t max, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned decimal number.
auto parseDecimal(Containers::StringView string, std::int64_t& value, std::int64_t min, std::int64_t max, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed decimal number.
auto parseDecimal(Containers::StringView string, std::uint8_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 8-bit decimal number.
auto parseDecimal(Containers::StringView string, std::int8_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 8-bit decimal number.
auto parseDecimal(Containers::StringView string, std::uint16_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 16-bit decimal number.
auto parseDecimal(Containers::StringView string, std::int16_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 16-bit decimal number.
auto parseDecimal(Containers::StringView string, std::uint32_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 32-bit decimal number.
auto parseDecimal(Containers::StringView string, std::int32_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 32-bit decimal number.
auto parseDecimal(Containers::StringView string, std::uint64_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 64-bit decimal number.
auto parseDecimal(Containers::StringView string, std::int64_t& value, ParseDecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 64-bit decimal number.
auto operator<<(Utility::Debug& debug, ParseHexadecimalFlag value) -> Utility::Debug& new in Git master
Debug output operator.
auto operator<<(Utility::Debug& debug, ParseHexadecimalFlags value) -> Utility::Debug& new in Git master
Debug output operator.
auto parseHexadecimal(Containers::StringView string, std::uint64_t& value, std::uint64_t min, std::uint64_t max, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::int64_t& value, std::int64_t min, std::int64_t max, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::uint8_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 8-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::int8_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 8-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::uint16_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 16-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::int16_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 16-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::uint32_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 32-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::int32_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 32-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::uint64_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing an unsigned 64-bit hexadecimal number.
auto parseHexadecimal(Containers::StringView string, std::int64_t& value, ParseHexadecimalFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a signed 64-bit hexadecimal number.
auto operator<<(Utility::Debug& debug, ParseFloatFlag value) -> Utility::Debug& new in Git master
Debug output operator.
auto operator<<(Utility::Debug& debug, ParseFloatFlags value) -> Utility::Debug& new in Git master
Debug output operator.
auto parseFloat(Containers::StringView string, float& value, ParseFloatFlags flags = {}) -> ParseResult new in Git master
Parse a string containing a floating-point number.
auto parseFloat(Containers::StringView string, double& value, ParseFloatFlags flags = {}) -> ParseResult new in Git master
auto parseNumberSequence(Containers::StringView string, std::uint32_t min, std::uint32_t max) -> Containers::Optional<Containers::Array<std::uint32_t>> new in Git master
Parse a number sequence.
auto fromArray(const char* string) -> std::string deprecated in Git master
Safely construct string from char array.
auto fromArray(const char* string, std::size_t length) -> std::string deprecated in Git master
Safely construct string from char array with explicit length.
auto ltrim(std::string string, const std::string& characters) -> std::string deprecated in Git master
Trim leading characters from string.
auto ltrim(std::string string) -> std::string deprecated in Git master
Trim leading whitespace from string.
auto rtrim(std::string string, const std::string& characters) -> std::string deprecated in Git master
Trim trailing characters from string.
auto rtrim(std::string string) -> std::string deprecated in Git master
Trim trailing whitespace from string.
auto trim(std::string string, const std::string& characters) -> std::string deprecated in Git master
Trim leading and trailing characters from string.
auto trim(std::string string) -> std::string deprecated in Git master
Trim leading and trailing whitespace from string.
void ltrimInPlace(std::string& string, const std::string& characters) deprecated in Git master
Trim leading characters from a string, in place.
void ltrimInPlace(std::string& string) deprecated in Git master
Trim leading whitespace from a string, in place.
void rtrimInPlace(std::string& string, const std::string& characters) deprecated in Git master
Trim trailing characters from a string, in place.
void rtrimInPlace(std::string& string) deprecated in Git master
Trim trailing whitespace from a string, in place.
void trimInPlace(std::string& string, const std::string& characters) deprecated in Git master
Trim leading and trailing characters from a string, in place.
void trimInPlace(std::string& string) deprecated in Git master
Trim leading and trailing whitespace from a string, in place.
auto split(const std::string& string, char delimiter) -> std::vector<std::string> deprecated in Git master
Split a string on given character.
auto split(Containers::StringView string, char delimiter) -> Containers::Array<Containers::StringView> deprecated in Git master
auto splitWithoutEmptyParts(const std::string& string, char delimiter) -> std::vector<std::string> deprecated in Git master
Split a string on given character and remove empty parts.
auto splitWithoutEmptyParts(Containers::StringView string, char delimiter) -> Containers::Array<Containers::StringView> deprecated in Git master
auto splitWithoutEmptyParts(const std::string& string, const std::string& delimiters) -> std::vector<std::string> deprecated in Git master
Split a string on any character from given set and remove empty parts.
auto splitWithoutEmptyParts(Containers::StringView string, Containers::StringView delimiters) -> Containers::Array<Containers::StringView> deprecated in Git master
auto splitWithoutEmptyParts(const std::string& string) -> std::vector<std::string> deprecated in Git master
Split a string on whitespace and remove empty parts.
auto splitWithoutEmptyParts(const Containers::StringView string) -> Containers::Array<Containers::StringView> deprecated in Git master
auto partition(const std::string& string, char separator) -> Containers::StaticArray<3, std::string> deprecated in Git master
Partition a string.
auto partition(const std::string& string, const std::string& separator) -> Containers::StaticArray<3, std::string> deprecated in Git master
auto rpartition(const std::string& string, char separator) -> Containers::StaticArray<3, std::string> deprecated in Git master
Right-partition a string.
auto rpartition(const std::string& string, const std::string& separator) -> Containers::StaticArray<3, std::string> deprecated in Git master
auto join(const std::vector<std::string>& strings, char delimiter) -> std::string deprecated in Git master
Join strings with given character.
auto join(const std::vector<std::string>& strings, const std::string& delimiter) -> std::string deprecated in Git master
auto joinWithoutEmptyParts(const std::vector<std::string>& strings, char delimiter) -> std::string deprecated in Git master
Join strings with given character and remove empty parts.
auto joinWithoutEmptyParts(const std::vector<std::string>& strings, const std::string& delimiter) -> std::string deprecated in Git master
auto beginsWith(const std::string& string, const std::string& prefix) -> bool deprecated in Git master
Whether the string has given prefix.
auto beginsWith(const std::string& string, char prefix) -> bool deprecated in Git master
auto viewBeginsWith(Containers::ArrayView<const char> string, Containers::ArrayView<const char> prefix) -> bool deprecated in Git master
Whether string view has given prefix.
auto viewBeginsWith(Containers::ArrayView<const char> string, char prefix) -> bool deprecated in Git master
auto endsWith(const std::string& string, const std::string& suffix) -> bool deprecated in Git master
Whether the string has given suffix.
auto endsWith(const std::string& string, char suffix) -> bool deprecated in Git master
auto viewEndsWith(Containers::ArrayView<const char> string, Containers::ArrayView<const char> suffix) -> bool deprecated in Git master
Whether string view has given suffix.
auto viewEndsWith(Containers::ArrayView<const char> string, char suffix) -> bool deprecated in Git master
auto stripPrefix(std::string string, const std::string& prefix) -> std::string deprecated in Git master
Strip given prefix from a string.
auto stripPrefix(std::string string, char prefix) -> std::string deprecated in Git master
auto stripSuffix(std::string string, const std::string& suffix) -> std::string deprecated in Git master
Strip given suffix from a string.
auto stripSuffix(std::string string, char suffix) -> std::string deprecated in Git master

Enum documentation

enum class Corrade::Utility::String::ParseState: std::uint8_t new in Git master

String parse state.

Returned as part of ParseResult from parseDecimal() and parseHexadecimal().

Enumerators
Success

Parsing succeeded with no information loss, i.e. the value can fit into the desired range without being clamped.

Clamped

Parsing succeeded but the parsed value had to be clamped to fit into the desired range. The output value is set to the appropriate min or max value of given range.

Failed

Parsing the value failed, for example because it contains invalid characters. The output value is left in an unspecified state in this case, ParseResult::index() contains the index of a byte on which a parsing failure happened.

enum class Corrade::Utility::String::ParseDecimalFlag: std::uint8_t new in Git master

Decimal string parse flag.

Enumerators
DisallowSign

Disallow - and + sign in front of the number. Note that, unlike with std::strtoull(), which silently accepts negative numbers and wraps them around, for an unsigned output type a - is never allowed regardless of this flag being present.

enum class Corrade::Utility::String::ParseHexadecimalFlag: std::uint8_t new in Git master

Hexadecimal string parse flag.

Enumerators
DisallowSign

Disallow - and + sign in front of the number. Note that, unlike with std::strtoull(), which silently accepts negative numbers and wraps them around, for an unsigned output type a - is never allowed regardless of this flag being present.

AllowBasePrefix

Allow also a 0x or 0X prefix in front of the number and after the sign, if any. If neither ParseHexadecimalFlag::AllowBasePrefix nor AllowHashPrefix is set, no prefix is allowed.

AllowHashPrefix

Allow also a # prefix in front of the number and after the sign, if any, such as for a hexadecimal color representation. If neither ParseHexadecimalFlag::AllowBasePrefix nor AllowHashPrefix is set, no prefix is allowed.

enum class Corrade::Utility::String::ParseFloatFlag: std::uint8_t new in Git master

Float string parse flag.

Enumerators
DisallowSign

Disallow - and + sign in front of the number

Typedef documentation

Function documentation

Containers::StringView Corrade::Utility::String::commonPrefix(Containers::StringView a, Containers::StringView b) new in Git master

Longest common prefix of two strings.

The returned view is a prefix of a.

void Corrade::Utility::String::lowercaseInPlace(Containers::MutableStringView string) new in Git master

Convert ASCII characters in a string to lowercase, in place.

Replaces any character from ABCDEFGHIJKLMNOPQRSTUVWXYZ with a corresponding character from abcdefghijklmnopqrstuvwxyz. Deliberately supports only ASCII as Unicode-aware case conversion is a much more complex topic.

Containers::String Corrade::Utility::String::lowercase(Containers::StringView string) new in Git master

Convert ASCII characters in a string to lowercase.

Allocates a copy and replaces any character from ABCDEFGHIJKLMNOPQRSTUVWXYZ with a corresponding character from abcdefghijklmnopqrstuvwxyz. Deliberately supports only ASCII as Unicode-aware case conversion is a much more complex topic.

Containers::String Corrade::Utility::String::lowercase(Containers::String string) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Compared to lowercase(Containers::StringView) is able to perform the operation in-place if string is owned, transferring the data ownership to the returned instance. Makes a owned copy first if not.

void Corrade::Utility::String::uppercaseInPlace(Containers::MutableStringView string) new in Git master

Convert ASCII characters in a string to uppercase, in place.

Replaces any character from abcdefghijklmnopqrstuvwxyz with a corresponding character from ABCDEFGHIJKLMNOPQRSTUVWXYZ. Deliberately supports only ASCII as Unicode-aware case conversion is a much more complex topic.

Containers::String Corrade::Utility::String::uppercase(Containers::StringView string) new in Git master

Convert ASCII characters in a string to uppercase, in place.

Allocates a copy and replaces any character from abcdefghijklmnopqrstuvwxyz with a corresponding character from ABCDEFGHIJKLMNOPQRSTUVWXYZ. Deliberately supports only ASCII as Unicode-aware case conversion is a much more complex topic.

Containers::String Corrade::Utility::String::uppercase(Containers::String string) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Compared to uppercase(Containers::StringView) is able to perform the operation in-place if string is owned, transferring the data ownership to the returned instance. Makes a owned copy first if not.

Containers::String Corrade::Utility::String::replaceFirst(Containers::StringView string, Containers::StringView search, Containers::StringView replace) new in Git master

Replace first occurrence in a string.

Returns string unmodified if it doesn't contain search. Having empty search causes replace to be prepended to string.

Containers::String Corrade::Utility::String::replaceAll(Containers::StringView string, Containers::StringView search, Containers::StringView replace) new in Git master

Replace all occurrences in a string.

Returns string unmodified if it doesn't contain search. Expects that search is not empty, as that would cause an infinite loop. For substituting a single character with another the replaceAll(Containers::String, char, char) variant is more optimal.

Containers::String Corrade::Utility::String::replaceAll(Containers::String string, char search, char replace) new in Git master

Replace all occurrences of a character in a string with another character.

The string is passed through unmodified if it doesn't contain search. Otherwise the operation is performed in-place if string is owned, transferring the data ownership to the returned instance. An owned copy is made if not. See also replaceAllInPlace() for a variant that operates on string views.

void Corrade::Utility::String::replaceAllInPlace(const Containers::MutableStringView string, const char search, const char replace) new in Git master

Replace all occurrences of a character in a string with another character in-place.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseState value) new in Git master

Debug output operator.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseDecimalFlag value) new in Git master

Debug output operator.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseDecimalFlags value) new in Git master

Debug output operator.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::uint64_t& value, std::uint64_t min, std::uint64_t max, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned decimal number.

Parameters
string in Input string
value out Output value
min in Minimal allowed value
max in Maximal allowed value
flags in Flags
Returns Parse state

If the string is a decimal numeric value, optionally prepended with a + sign unless ParseDecimalFlag::DisallowSign is set, parses it into value and returns ParseState::Success if the value fits into the range defined by min and max. If the value doesn't fit into the range defined by min and max, returns ParseState::Clamped and value is set to either min or max as appropriate. If the string isn't a valid number or has non-numeric characters before or after, returns ParseState::Failed, with ParseResult::index() pointing to the byte at which a parsing failure happened, and value left in an unspecified state.

The string can have any number of leading zeros after the sign, unlike std::strtoull() a leading zero never causes the number to be interpreted as octal, and a 0x or 0X prefix is treated as a parsing failure.

Expects that min is less or equal to max. Common usage is through one of the type-specific overloads such as parseDecimal(Containers::StringView, std::uint32_t&, ParseDecimalFlags) which have the min and max values implicit based on the type. Example usage:

std::uint32_t value;
Utility::String::ParseResult result = Utility::String::parseDecimal(, value);
if(result == Utility::String::ParseState::Failed) {
    // handle a parsing failure ...
} else if(result == Utility::String::ParseState::Clamped) {
    // handle value out of bounds ...
} else {
    // handle success ...
}

If clamping / overflow doesn't need to be handled, it's enough to check that the function doesn't return ParseState::Failed.

Note that in comparison to std::strtoull(), which accepts negative numbers and wraps them around, this function returns ParseState::Failed for any number with a - sign. Furthermore, the function does not discard any whitespace characters around the number — if you need to do so, pass the string as trimmed(), with trimmedPrefix() or with trimmedSuffix():

std::uint32_t value;
if(Utility::String::parseDecimal(string.trimmed(), value) !=
   Utility::String::ParseState::Failed)
{
    
}

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::int64_t& value, std::int64_t min, std::int64_t max, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing a signed decimal number.

Parameters
string in Input string
value out Output value
min in Minimal allowed value
max in Maximal allowed value
flags in Flags
Returns Parse state

If the string is a decimal numeric value, optionally prepended with a + or - sign unless ParseDecimalFlag::DisallowSign is set, parses it into value and returns ParseState::Success if the value fits into the range defined by min and max. If the value doesn't fit into the range defined by min and max, returns ParseState::Clamped and value is set to either min or max as appropriate. If the string isn't a valid number or has non-numeric characters before or after, returns ParseState::Failed, with ParseResult::index() pointing to the byte at which a parsing failure happened, and value left in an unspecified state.

The string can have any number of leading zeros after the sign, if any, unlike std::strtoull() a leading zero never causes the number to be interpreted as octal, and a 0x or 0X prefix is treated as a parsing failure.

Expects that min is less or equal to max. Common usage is through one of the type-specific overloads such as parseDecimal(Containers::StringView, std::int32_t&, ParseDecimalFlags) which have the min and max values implicit based on the type. Example usage:

std::int32_t value;
Utility::String::ParseResult result = Utility::String::parseDecimal(, value);
if(result == Utility::String::ParseState::Failed) {
    // handle a parsing failure ...
} else if(result == Utility::String::ParseState::Clamped) {
    // handle value out of bounds ...
} else {
    // handle success ...
}

If clamping / overflow doesn't need to be handled, it's enough to check that the function doesn't return ParseState::Failed.

Note that in comparison to std::strtoll(), the function does not discard any whitespace characters around the number — if you need to do so, pass the string as trimmed(), with trimmedPrefix() or with trimmedSuffix():

std::int32_t value;
if(Utility::String::parseDecimal(string.trimmed(), value) !=
   Utility::String::ParseState::Failed)
{
    
}

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::uint8_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 8-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseDecimalFlags) with min set to 0 and max set to 255 and converting the value to a 8-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::int8_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing a signed 8-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseDecimalFlags) with min set to -128 and max set to 127 and converting the value to a 8-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::uint16_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 16-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseDecimalFlags) with min set to 0 and max set to 65535 and converting the value to a 16-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::int16_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing a signed 16-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseDecimalFlags) with min set to -32768 and max set to 32767 and converting the value to a 16-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::uint32_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 32-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseDecimalFlags) with min set to 0 and max set to a max representable unsigned 32-bit value and converting the value to a 32-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::int32_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing a signed 32-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseDecimalFlags) with min and max set to a min and max representable signed 32-bit value and converting the value to a 32-bit type.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::uint64_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 64-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseDecimalFlags) with min set to 0 and max set to a max representable unsigned 64-bit value.

ParseResult Corrade::Utility::String::parseDecimal(Containers::StringView string, std::int64_t& value, ParseDecimalFlags flags = {}) new in Git master

Parse a string containing a signed 64-bit decimal number.

Equivalent to calling parseDecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseDecimalFlags) with min and max set to a min and max representable signed 64-bit value.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseHexadecimalFlag value) new in Git master

Debug output operator.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseHexadecimalFlags value) new in Git master

Debug output operator.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::uint64_t& value, std::uint64_t min, std::uint64_t max, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned hexadecimal number.

Parameters
string in Input string
value out Output value
min in Minimal allowed value
max in Maximal allowed value
flags in Flags
Returns Parse state

If the string is a hexadecimal numeric value, optionally prepended with a + sign unless ParseHexadecimalFlag::DisallowSign is set, parses it into value and returns ParseState::Success if the value fits into the range defined by min and max. If the value doesn't fit into the range defined by min and max, returns ParseState::Clamped and value is set to either min or max as appropriate. If the string isn't a valid number or has non-hexadecimal characters before or after, returns ParseState::Failed, with ParseResult::index() pointing to the byte at which a parsing failure happened, and value left in an unspecified state.

Both lowercase and uppercase hexadecimal characters are accepted. By default no prefix is allowed, pass ParseHexadecimalFlag::AllowBasePrefix to accept also numbers prefixed with 0x or 0X after the sign, if any, and ParseHexadecimalFlag::AllowHashPrefix to accept also numbers prefixed with a # character after the sign, if any, such as for a hexadecimal color representation. The string can have any number of leading zeros after the sign and prefix, if any, unlike std::strtoull() an omitted prefix or a leading zero never causes the number to be interpreted as decimal or octal.

Expects that min is less or equal to max. Common usage is through one of the type-specific overloads such as parseHexadecimal(Containers::StringView, std::uint32_t&, ParseHexadecimalFlags) which have the min and max values implicit based on the type. Example usage:

std::uint32_t value;
Utility::String::ParseResult result = Utility::String::parseHexadecimal(, value,
    Utility::String::ParseHexadecimalFlag::AllowBasePrefix);
if(result == Utility::String::ParseState::Failed) {
    // handle a parsing failure ...
} else if(result == Utility::String::ParseState::Clamped) {
    // handle value out of bounds ...
} else {
    // handle success ...
}

If clamping / overflow doesn't need to be handled, it's enough to check that the function doesn't return ParseState::Failed.

Note that in comparison to std::strtoull(), which accepts negative numbers and wraps them around, this function returns ParseState::Failed for any number with a - sign. Furthermore, the function does not discard any whitespace characters around the number — if you need to do so, pass the string as trimmed(), with trimmedPrefix() or with trimmedSuffix():

std::uint32_t value;
if(Utility::String::parseHexadecimal(string.trimmed(), value) !=
   Utility::String::ParseState::Failed)
{
    
}

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::int64_t& value, std::int64_t min, std::int64_t max, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing a signed hexadecimal number.

Parameters
string in Input string
value out Output value
min in Minimal allowed value
max in Maximal allowed value
flags in Flags
Returns Parse state

If the string is a hexadecimal numeric value, optionally prepended with a + or - sign unless ParseDecimalFlag::DisallowSign is set, parses it into value and returns ParseState::Success if the value fits into the range defined by min and max. If the value doesn't fit into the range defined by min and max, returns ParseState::Clamped and value is set to either min or max as appropriate. If the string isn't a valid number or has non-hexadecimal characters before or after, returns ParseState::Failed, with ParseResult::index() pointing to the byte at which a parsing failure happened, and value left in an unspecified state.

Both lowercase and uppercase hexadecimal characters are accepted. By default no prefix is allowed, pass ParseHexadecimalFlag::AllowBasePrefix to accept also numbers prefixed with 0x or 0X after the sign, and ParseHexadecimalFlag::AllowHashPrefix to accept also numbers prefixed with a # character after the sign, such as for hexadecimal color representation. The string can have any number of leading zeros after the sign and prefix, unlike std::strtoull() an omitted prefix or a leading zero never causes the number to be interpreted as decimal or octal.

Expects that min is less or equal to max. Common usage is through one of the type-specific overloads such as parseHexadecimal(Containers::StringView, std::int32_t&, ParseHexadecimalFlags) which have the min and max values implicit based on the type. Example usage:

std::int32_t value;
Utility::String::ParseResult result = Utility::String::parseHexadecimal(, value,
    Utility::String::ParseHexadecimalFlag::AllowBasePrefix);
if(result == Utility::String::ParseState::Failed) {
    // handle a parsing failure ...
} else if(result == Utility::String::ParseState::Clamped) {
    // handle value out of bounds ...
} else {
    // handle success ...
}

If clamping / overflow doesn't need to be handled, it's enough to check that the function doesn't return ParseState::Failed.

Note that in comparison to std::strtoll(), the function does not discard any whitespace characters around the number — if you need to do so, pass the string as trimmed(), with trimmedPrefix() or with trimmedSuffix():

std::int32_t value;
if(Utility::String::parseHexadecimal(string.trimmed(), value) !=
   Utility::String::ParseState::Failed)
{
    
}

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::uint8_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 8-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseHexadecimalFlags) with min set to 0 and max set to 255 and converting the value to a 8-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::int8_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing a signed 8-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseHexadecimalFlags) with min set to -128 and max set to 127 and converting the value to a 8-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::uint16_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 16-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseHexadecimalFlags) with min set to 0 and max set to 65535 and converting the value to a 16-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::int16_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing a signed 16-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseHexadecimalFlags) with min set to -32768 and max set to 32767 and converting the value to a 16-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::uint32_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 32-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseHexadecimalFlags) with min set to 0 and max set to a max representable unsigned 32-bit value and converting the value to a 32-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::int32_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing a signed 32-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseHexadecimalFlags) with min and max set to a min and max representable signed 32-bit value and converting the value to a 32-bit type.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::uint64_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing an unsigned 64-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::uint64_t&, std::uint64_t, std::uint64_t, ParseHexadecimalFlags) with min set to 0 and max set to a max representable unsigned 64-bit value.

ParseResult Corrade::Utility::String::parseHexadecimal(Containers::StringView string, std::int64_t& value, ParseHexadecimalFlags flags = {}) new in Git master

Parse a string containing a signed 64-bit hexadecimal number.

Equivalent to calling parseHexadecimal(Containers::StringView, std::int64_t&, std::int64_t, std::int64_t, ParseHexadecimalFlags) with min and max set to a min and max representable signed 64-bit value.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseFloatFlag value) new in Git master

Debug output operator.

Utility::Debug& Corrade::Utility::String::operator<<(Utility::Debug& debug, ParseFloatFlags value) new in Git master

Debug output operator.

ParseResult Corrade::Utility::String::parseFloat(Containers::StringView string, float& value, ParseFloatFlags flags = {}) new in Git master

Parse a string containing a floating-point number.

Parameters
string in Input string
value out Output value
flags in Flags
Returns Parse state

If the string is a floating-point numberic value, optionally prepended with a + or - sign unless ParseDecimalFlag::DisallowSign is set, and optionally with an exponent, parses it into value and returns ParseState::Success if the value fits into the output type. If the value doesn't fit into the output type, returns ParseState::Clamped and value is set to either positive or negative infinity as appropriate. If the input string is a literal inf or nan, optionally with a sign, it results in ParseState::Success, not ParseState::Clamped, and value set to either infinity or NaN with an appropriate sign. If the string isn't a valid floating-point number, returns ParseState::Failed, with ParseResult::index() pointing to the byte at which a parsing failure happened, and value left in an unspecified state. Example usage:

float value;
Utility::String::ParseResult result = Utility::String::parseFloat(, value);
if(result == Utility::String::ParseState::Failed) {
    // handle a parsing failure ...
} else if(result == Utility::String::ParseState::Clamped) {
    // handle value out of bounds ...
} else {
    // handle success ...
}

If clamping / overflow doesn't need to be handled, it's enough to check that the function doesn't return ParseState::Failed.

Note that in comparison to std::strtof(), which accepts also a hexadecimal float representation, this function returns ParseState::Failed for those. Furthermore, the function does not discard any whitespace characters around the number — if you need to do so, pass the string as trimmed(), with trimmedPrefix() or with trimmedSuffix():

float value;
if(Utility::String::parseFloat(string.trimmed(), value) !=
   Utility::String::ParseState::Failed)
{
    
}

ParseResult Corrade::Utility::String::parseFloat(Containers::StringView string, double& value, ParseFloatFlags flags = {}) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Containers::Optional<Containers::Array<std::uint32_t>> Corrade::Utility::String::parseNumberSequence(Containers::StringView string, std::uint32_t min, std::uint32_t max) new in Git master

Parse a number sequence.

Parses a string containing a sequence of numbers, returning them converted to integers. The numbers can be delimited by commas (,), semicolons (;) or an arbitrary whitespace character. Order in which the numbers were specified is kept in the output including possible duplicates. Empty string results in an empty array returned.

Additionally it's possible to specify a range using the - character, in which case the range will be expanded in the output. The range is inclusive, meaning 3-6 will result in {3, 4, 5, 6} in the output. Ranges where the end is smaller than the start (such as 6-3) will be treated as empty. If the number before the - is omitted, a min is used instead; if the number after the - is omitted, max - 1 is used instead.

If an unrecognized character is encountered, the function prints an error and returns a Containers::NullOpt. If any parsed number is less than min, greater than or equal to max or doesn't fit into 32 bits, it's omitted in the output.

Example usage:

  • 4,3 5;5;17 results in {4, 3, 5, 5, 17}
  • 12-,3-5,1 with max set to 15 results in {12, 13, 14, 3, 4, 5, 1}
  • -3, 13- with min set to 0 and max to 15 results in {0, 1, 2, 3, 13, 14}
  • any input with min set to 0 and max set to 0 results in an empty output
  • - results in a range from min to max - 1

std::string Corrade::Utility::String::fromArray(const char* string)

Safely construct string from char array.

If string is nullptr, returns empty string.

nullptr as an empty string on its own

std::string Corrade::Utility::String::fromArray(const char* string, std::size_t length)

Safely construct string from char array with explicit length.

If string is nullptr, returns empty string. Otherwise takes also length into account.

nullptr as an empty string on its own

std::string Corrade::Utility::String::ltrim(std::string string, const std::string& characters)

Trim leading characters from string.

Parameters
string String to be trimmed
characters Characters which will be trimmed

Implemented using ltrimInPlace().

std::string Corrade::Utility::String::ltrim(std::string string)

Trim leading whitespace from string.

Equivalent to calling ltrim(std::string, const std::string&) with " \t\f\v\r\n" as second parameter. Implemented using ltrimInPlace().

std::string Corrade::Utility::String::rtrim(std::string string, const std::string& characters)

Trim trailing characters from string.

Parameters
string String to be trimmed
characters Characters which will be trimmed

Implemented using rtrimInPlace().

std::string Corrade::Utility::String::rtrim(std::string string)

Trim trailing whitespace from string.

Equivalent to calling rtrim(std::string, const std::string&) with " \t\f\v\r\n" as second parameter. Implemented using trimInPlace().

std::string Corrade::Utility::String::trim(std::string string, const std::string& characters)

Trim leading and trailing characters from string.

Parameters
string String to be trimmed
characters Characters which will be trimmed

Equivalent to ltrim(rtrim(string, characters), characters). Implemented using trimInPlace().

std::string Corrade::Utility::String::trim(std::string string)

Trim leading and trailing whitespace from string.

Equivalent to calling trim(std::string, const std::string&) with " \t\f\v\r\n" as second parameter. Implemented using trimInPlace().

void Corrade::Utility::String::ltrimInPlace(std::string& string, const std::string& characters)

Trim leading characters from a string, in place.

Parameters
string String to be trimmed in place
characters Characters which will be trimmed

void Corrade::Utility::String::ltrimInPlace(std::string& string)

Trim leading whitespace from a string, in place.

Equivalent to calling ltrimInPlace(std::string&, const std::string&) with " \t\f\v\r\n" as second parameter.

void Corrade::Utility::String::rtrimInPlace(std::string& string, const std::string& characters)

Trim trailing characters from a string, in place.

Parameters
string String to be trimmed
characters Characters which will be trimmed

void Corrade::Utility::String::rtrimInPlace(std::string& string)

Trim trailing whitespace from a string, in place.

Equivalent to calling rtrimInPlace(std::string&, const std::string&) with " \t\f\v\r\n" as second parameter.

void Corrade::Utility::String::trimInPlace(std::string& string, const std::string& characters)

Trim leading and trailing characters from a string, in place.

Parameters
string String to be trimmed
characters Characters which will be trimmed

Equivalent to calling both ltrimInPlace() and rtrimInPlace().

void Corrade::Utility::String::trimInPlace(std::string& string)

Trim leading and trailing whitespace from a string, in place.

Equivalent to calling trimInPlace(std::string&, const std::string&) with " \t\f\v\r\n" as second parameter.

std::vector<std::string> Corrade::Utility::String::split(const std::string& string, char delimiter)

Split a string on given character.

Parameters
string String to split
delimiter Delimiter

Containers::Array<Containers::StringView> Corrade::Utility::String::split(Containers::StringView string, char delimiter)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::vector<std::string> Corrade::Utility::String::splitWithoutEmptyParts(const std::string& string, char delimiter)

Split a string on given character and remove empty parts.

Parameters
string String to split
delimiter Delimiter

Containers::Array<Containers::StringView> Corrade::Utility::String::splitWithoutEmptyParts(Containers::StringView string, char delimiter)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::vector<std::string> Corrade::Utility::String::splitWithoutEmptyParts(const std::string& string, const std::string& delimiters)

Split a string on any character from given set and remove empty parts.

Parameters
string String to split
delimiters Delimiter characters

Containers::Array<Containers::StringView> Corrade::Utility::String::splitWithoutEmptyParts(Containers::StringView string, Containers::StringView delimiters)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::vector<std::string> Corrade::Utility::String::splitWithoutEmptyParts(const std::string& string)

Split a string on whitespace and remove empty parts.

Equivalent to calling splitWithoutEmptyParts(const std::string&, const std::string&) with " \t\f\v\r\n" as second parameter.

Containers::Array<Containers::StringView> Corrade::Utility::String::splitWithoutEmptyParts(const Containers::StringView string)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Containers::StaticArray<3, std::string> Corrade::Utility::String::partition(const std::string& string, char separator)

Partition a string.

Equivalent to Python's str.partition(). Splits string at the first occurrence of separator. First returned value is the part before the separator, second the separator, third a part after the separator. If the separator is not found, returns the input string followed by two empty strings.

Containers::StaticArray<3, std::string> Corrade::Utility::String::partition(const std::string& string, const std::string& separator)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Containers::StaticArray<3, std::string> Corrade::Utility::String::rpartition(const std::string& string, char separator)

Right-partition a string.

Equivalent to Python's str.rpartition(). Splits string at the last occurrence of separator. First returned value is the part before the separator, second the separator, third a part after the separator. If the separator is not found, returns two empty strings followed by the input string.

Containers::StaticArray<3, std::string> Corrade::Utility::String::rpartition(const std::string& string, const std::string& separator)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::string Corrade::Utility::String::join(const std::vector<std::string>& strings, char delimiter)

Join strings with given character.

Parameters
strings Strings to join
delimiter Delimiter

std::string Corrade::Utility::String::join(const std::vector<std::string>& strings, const std::string& delimiter)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::string Corrade::Utility::String::joinWithoutEmptyParts(const std::vector<std::string>& strings, char delimiter)

Join strings with given character and remove empty parts.

Parameters
strings Strings to join
delimiter Delimiter

std::string Corrade::Utility::String::joinWithoutEmptyParts(const std::vector<std::string>& strings, const std::string& delimiter)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

bool Corrade::Utility::String::beginsWith(const std::string& string, const std::string& prefix)

Whether the string has given prefix.

In particular, returns true for empty string only if prefix is empty as well.

bool Corrade::Utility::String::beginsWith(const std::string& string, char prefix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

bool Corrade::Utility::String::viewBeginsWith(Containers::ArrayView<const char> string, Containers::ArrayView<const char> prefix)

Whether string view has given prefix.

bool Corrade::Utility::String::viewBeginsWith(Containers::ArrayView<const char> string, char prefix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

bool Corrade::Utility::String::endsWith(const std::string& string, const std::string& suffix)

Whether the string has given suffix.

In particular, returns true for empty string only if suffix is empty as well.

bool Corrade::Utility::String::endsWith(const std::string& string, char suffix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

bool Corrade::Utility::String::viewEndsWith(Containers::ArrayView<const char> string, Containers::ArrayView<const char> suffix)

Whether string view has given suffix.

bool Corrade::Utility::String::viewEndsWith(Containers::ArrayView<const char> string, char suffix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::string Corrade::Utility::String::stripPrefix(std::string string, const std::string& prefix)

Strip given prefix from a string.

Expects that the string actually begins with given prefix.

std::string Corrade::Utility::String::stripPrefix(std::string string, char prefix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

std::string Corrade::Utility::String::stripSuffix(std::string string, const std::string& suffix)

Strip given suffix from a string.

Expects that the string actually ends with given suffix.

std::string Corrade::Utility::String::stripSuffix(std::string string, char suffix)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.