Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Text

A collection of general-purpose text manipulation utilities. More...

Concepts

concept  sourcemeta::core::text_spellable_integer

Typedefs

using sourcemeta::core::DigitsBuffer

Functions

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::to_title_case (std::string &value) -> void
template<typename Character>
constexpr auto sourcemeta::core::to_lowercase (const Character character) noexcept -> Character
template<typename Character, typename Traits, typename Allocator>
auto sourcemeta::core::to_lowercase (std::basic_string< Character, Traits, Allocator > &value) -> void
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::to_lowercase (std::filesystem::path &value) -> void
template<typename Character>
constexpr auto sourcemeta::core::is_lowercase (const Character character) noexcept -> bool
template<typename String>
auto sourcemeta::core::is_lowercase (const String &value) noexcept -> bool
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::is_lowercase (const std::filesystem::path &value) noexcept -> bool
template<typename Character>
constexpr auto sourcemeta::core::is_alpha (const Character character) noexcept -> bool
constexpr auto sourcemeta::core::is_alpha (const std::string_view value) noexcept -> bool
template<typename Character>
constexpr auto sourcemeta::core::is_digit (const Character character) noexcept -> bool
constexpr auto sourcemeta::core::is_digit (const std::string_view value) noexcept -> bool
template<typename Character>
constexpr auto sourcemeta::core::is_alphanum (const Character character) noexcept -> bool
constexpr auto sourcemeta::core::is_alphanum (const std::string_view value) noexcept -> bool
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::truncate (std::string &input, const std::size_t maximum_length, const std::string_view marker) -> void
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::replace (const std::string_view input, const std::string_view target, const std::string_view replacement) -> std::string
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::trim (const std::string_view input) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::strip_left (const std::string_view input, const char character) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::strip_right (const std::string_view input, const char character) noexcept -> std::string_view
constexpr auto sourcemeta::core::unquote (const std::string_view input, const char quote) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::pad_left (const std::string_view input, const std::size_t width, const char character) -> std::string
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::take_until (const std::string_view input, const char marker) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::split_once (const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::split_once (const std::string_view input, const std::string_view delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::rsplit_once (const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >
template<typename Callback>
auto sourcemeta::core::split (const std::string_view input, const char delimiter, Callback callback) -> void
auto sourcemeta::core::split (const std::string_view input, const char delimiter) -> std::vector< std::string_view >
template<typename Range>
auto sourcemeta::core::join (const Range &items, const std::string_view separator) -> std::string
template<typename Range>
auto sourcemeta::core::join_to (std::ostream &stream, const Range &items, const std::string_view separator) -> void
template<typename Integer, std::size_t Capacity>
auto sourcemeta::core::digits_view (const Integer value, std::array< char, Capacity > &buffer) noexcept -> std::string_view
template<typename Output, typename Integer>
auto sourcemeta::core::digits_append (Output &output, const Integer value) -> void
template<typename CharT, typename Traits, typename Integer>
auto sourcemeta::core::digits_write (std::basic_ostream< CharT, Traits > &stream, const Integer value) -> void
auto sourcemeta::core::hex_digit_value (const char character) noexcept -> std::int8_t
auto sourcemeta::core::is_hex_digit (const char character) noexcept -> bool
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::hex_to_bytes (const std::string_view input, const bool allow_odd_length=false) -> std::optional< std::string >
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::bytes_to_hex (const std::string_view input) -> std::string
auto sourcemeta::core::equals_ignore_case (const std::string_view left, const std::string_view right) noexcept -> bool
auto sourcemeta::core::starts_with_ignore_case (const std::string_view value, const std::string_view prefix) noexcept -> bool
auto sourcemeta::core::less_ignore_case (const std::string_view left, const std::string_view right) noexcept -> bool
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::squeeze (const std::string_view input, const char character) -> std::string
template<typename Output>
auto sourcemeta::core::squeeze (const std::string_view input, const char character, Output &output) -> void
SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::remove_suffix_ignore_case (const std::string_view input, const std::string_view suffix) noexcept -> std::string_view

Variables

template<typename Integer>
constexpr std::size_t sourcemeta::core::digits_capacity

Detailed Description

A collection of general-purpose text manipulation utilities.

This functionality is included as follows:

#include <sourcemeta/core/text.h>

Typedef Documentation

◆ DigitsBuffer

Initial value:
std::array<char, std::max(digits_capacity<std::uint64_t>,
constexpr std::size_t digits_capacity
Definition text.h:647

A buffer wide enough for the decimal spelling of any integer of up to 64 bits, including a leading sign. That bound is twenty characters, reached by both the largest unsigned value and the smallest signed one.

Function Documentation

◆ bytes_to_hex()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::bytes_to_hex ( const std::string_view input) -> std::string

Encode a byte sequence as a lowercase hexadecimal string. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::bytes_to_hex("foo") == "666f6f");
SOURCEMETA_CORE_TEXT_EXPORT auto bytes_to_hex(const std::string_view input) -> std::string

◆ digits_append()

template<typename Output, typename Integer>
auto sourcemeta::core::digits_append ( Output & output,
const Integer value ) -> void
inline

Append the decimal spelling of an integer to a string like output sink. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
std::string result{"port="};
assert(result == "port=8080");
auto digits_append(Output &output, const Integer value) -> void
Definition text.h:708

◆ digits_view()

template<typename Integer, std::size_t Capacity>
auto sourcemeta::core::digits_view ( const Integer value,
std::array< char, Capacity > & buffer ) -> std::string_view
inlinenoexcept

Write the decimal spelling of an integer into a caller-provided buffer, returning a view of the characters written. That view stays valid for as long as the buffer does. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::digits_view(1234, buffer) == "1234");
std::array< char, std::max(digits_capacity< std::uint64_t >, digits_capacity< std::int64_t >)> DigitsBuffer
Definition text.h:656
auto digits_view(const Integer value, std::array< char, Capacity > &buffer) noexcept -> std::string_view
Definition text.h:684

Use this when the result feeds a lookup or another view-taking interface. It never consults a locale, so a digit grouping separator can never appear in the result.

Any sufficiently large buffer is accepted, so a caller that only ever formats a narrow type can size one down to its capacity. Requiring the buffer to fit the widest possible value is what makes the conversion unable to fail, which is why it reports no error.

◆ digits_write()

template<typename CharT, typename Traits, typename Integer>
auto sourcemeta::core::digits_write ( std::basic_ostream< CharT, Traits > & stream,
const Integer value ) -> void
inline

Write the decimal spelling of an integer to a stream. For example:

#include <sourcemeta/core/text.h>
#include <iostream>
// prints: 1234
auto digits_write(std::basic_ostream< CharT, Traits > &stream, const Integer value) -> void
Definition text.h:732

Prefer this to the stream insertion operator wherever the output has a machine-readable grammar. Insertion formats through the stream's imbued locale, so a locale carrying a digit grouping separator would otherwise break the result apart.

◆ equals_ignore_case()

auto sourcemeta::core::equals_ignore_case ( const std::string_view left,
const std::string_view right ) -> bool
inlinenoexcept

Return whether two strings are equal under ASCII case-insensitive comparison. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::equals_ignore_case("Hello", "hELLO"));
assert(!sourcemeta::core::equals_ignore_case("foo", "bar"));
auto equals_ignore_case(const std::string_view left, const std::string_view right) noexcept -> bool
Definition text.h:838

This comparison is allocation-free, as it compares the lowercase form of each character without materialising lowercased copies of its arguments.

◆ hex_digit_value()

auto sourcemeta::core::hex_digit_value ( const char character) -> std::int8_t
inlinenoexcept

Decode a single hexadecimal digit into its numeric value, returning a negative value when the character is not a hexadecimal digit. Both letter cases are accepted. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
auto hex_digit_value(const char character) noexcept -> std::int8_t
Definition text.h:752

◆ hex_to_bytes()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::hex_to_bytes ( const std::string_view input,
const bool allow_odd_length = false ) -> std::optional< std::string >

Decode a hexadecimal string into its raw bytes, returning no value when the input contains a character outside the hexadecimal alphabet, or has an odd length unless allow_odd_length is set, in which case a leading zero nibble is assumed. Both letter cases are accepted. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
const auto bytes{sourcemeta::core::hex_to_bytes("666f6f")};
assert(bytes.has_value());
assert(bytes.value() == "foo");
SOURCEMETA_CORE_TEXT_EXPORT auto hex_to_bytes(const std::string_view input, const bool allow_odd_length=false) -> std::optional< std::string >

◆ is_alpha() [1/2]

template<typename Character>
auto sourcemeta::core::is_alpha ( const Character character) -> bool
inlineconstexprnoexcept

Return whether a character is an ASCII letter (A-Z or a-z). For example:

#include <sourcemeta/core/text.h>
#include <cassert>
constexpr auto is_alpha(const Character character) noexcept -> bool
Definition text.h:197

◆ is_alpha() [2/2]

auto sourcemeta::core::is_alpha ( const std::string_view value) -> bool
inlineconstexprnoexcept

Return whether a string is non-empty and consists entirely of ASCII letters (A-Z or a-z). An empty string is not considered a match. For example:

#include <sourcemeta/core/text.h>
#include <cassert>

◆ is_alphanum() [1/2]

template<typename Character>
auto sourcemeta::core::is_alphanum ( const Character character) -> bool
inlineconstexprnoexcept

Return whether a character is an ASCII letter or digit. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
constexpr auto is_alphanum(const Character character) noexcept -> bool
Definition text.h:289

◆ is_alphanum() [2/2]

auto sourcemeta::core::is_alphanum ( const std::string_view value) -> bool
inlineconstexprnoexcept

Return whether a string is non-empty and consists entirely of ASCII letters or digits. An empty string is not considered a match. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(!sourcemeta::core::is_alphanum("abc-123"));

◆ is_digit() [1/2]

template<typename Character>
auto sourcemeta::core::is_digit ( const Character character) -> bool
inlineconstexprnoexcept

Return whether a character is an ASCII digit (0-9). For example:

#include <sourcemeta/core/text.h>
#include <cassert>
constexpr auto is_digit(const char character) -> bool
Definition numeric_util.h:51

◆ is_digit() [2/2]

auto sourcemeta::core::is_digit ( const std::string_view value) -> bool
inlineconstexprnoexcept

Return whether a string is non-empty and consists entirely of ASCII digits (0-9). An empty string is not considered a match. For example:

#include <sourcemeta/core/text.h>
#include <cassert>

◆ is_hex_digit()

auto sourcemeta::core::is_hex_digit ( const char character) -> bool
inlinenoexcept

Check whether the given character is an ASCII hexadecimal digit ('0'-'9', 'a'-'f', 'A'-'F'). For example:

#include <sourcemeta/core/text.h>
#include <cassert>
auto is_hex_digit(const char character) noexcept -> bool
Definition text.h:786

◆ is_lowercase() [1/3]

template<typename Character>
auto sourcemeta::core::is_lowercase ( const Character character) -> bool
inlineconstexprnoexcept

Return whether a character is not ASCII uppercase. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
constexpr auto is_lowercase(const Character character) noexcept -> bool
Definition text.h:134

◆ is_lowercase() [2/3]

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::is_lowercase ( const std::filesystem::path & value) -> bool
noexcept

Return whether every code unit of a filesystem path is not ASCII uppercase. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <filesystem>
assert(sourcemeta::core::is_lowercase(std::filesystem::path{"/foo/bar"}));
assert(!sourcemeta::core::is_lowercase(std::filesystem::path{"/Foo/Bar"}));

◆ is_lowercase() [3/3]

template<typename String>
auto sourcemeta::core::is_lowercase ( const String & value) -> bool
inlinenoexcept

Return whether every code unit of a string is not ASCII uppercase. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
assert(sourcemeta::core::is_lowercase(std::string{"hello"}));
assert(!sourcemeta::core::is_lowercase(std::string{"Hello"}));

◆ join()

template<typename Range>
auto sourcemeta::core::join ( const Range & items,
const std::string_view separator ) -> std::string

Return the items of items as a string, separated by separator. The items must be string-like, as nothing is formatted along the way. For example:

#include <sourcemeta/core/text.h>
#include <array>
#include <cassert>
#include <string_view>
constexpr std::array<std::string_view, 3> values{{"a", "b", "c"}};
assert(sourcemeta::core::join(values, ", ") == "a, b, c");
auto join(const Range &items, const std::string_view separator) -> std::string
Definition text.h:588

◆ join_to()

template<typename Range>
auto sourcemeta::core::join_to ( std::ostream & stream,
const Range & items,
const std::string_view separator ) -> void

Stream each item of items to stream, separated by separator. For example:

#include <sourcemeta/core/text.h>
#include <array>
#include <iostream>
constexpr std::array<int, 3> values{{1, 2, 3}};
sourcemeta::core::join_to(std::cout, values, ", ");
// prints: 1, 2, 3
auto join_to(std::ostream &stream, const Range &items, const std::string_view separator) -> void
Definition text.h:618

◆ less_ignore_case()

auto sourcemeta::core::less_ignore_case ( const std::string_view left,
const std::string_view right ) -> bool
inlinenoexcept

Return whether one string orders before another under ASCII case-insensitive lexicographic comparison, which is useful as a sort comparator. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::less_ignore_case("apple", "Banana"));
assert(!sourcemeta::core::less_ignore_case("Banana", "apple"));
auto less_ignore_case(const std::string_view left, const std::string_view right) noexcept -> bool
Definition text.h:897

Like the equality comparison, this is allocation-free.

◆ pad_left()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::pad_left ( const std::string_view input,
const std::size_t width,
const char character ) -> std::string

Return input left-padded with character to at least width bytes, or a copy of input when it is already that long. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::pad_left("42", 5, '0') == "00042");
assert(sourcemeta::core::pad_left("hello", 3, '0') == "hello");
SOURCEMETA_CORE_TEXT_EXPORT auto pad_left(const std::string_view input, const std::size_t width, const char character) -> std::string

◆ remove_suffix_ignore_case()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::remove_suffix_ignore_case ( const std::string_view input,
const std::string_view suffix ) -> std::string_view
noexcept

Return input with suffix removed from the end under ASCII case-insensitive comparison, or input unchanged when the suffix does not match. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string_view>
"schema.JSON", ".json")};
assert(trimmed == "schema");
SOURCEMETA_CORE_TEXT_EXPORT auto remove_suffix_ignore_case(const std::string_view input, const std::string_view suffix) noexcept -> std::string_view

◆ replace()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::replace ( const std::string_view input,
const std::string_view target,
const std::string_view replacement ) -> std::string

Return input with every occurrence of target replaced by replacement. An empty target matches nothing. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::replace("a.b.c", ".", "/") == "a/b/c");
SOURCEMETA_CORE_TEXT_EXPORT auto replace(const std::string_view input, const std::string_view target, const std::string_view replacement) -> std::string

◆ rsplit_once()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::rsplit_once ( const std::string_view input,
const char delimiter ) -> std::optional< std::pair< std::string_view, std::string_view > >
noexcept

Split input at the last occurrence of delimiter, returning the parts before and after it. Return std::nullopt when the delimiter is absent. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
const auto parts{sourcemeta::core::rsplit_once("a.b.c", '.')};
assert(parts.has_value());
assert(parts->first == "a.b");
assert(parts->second == "c");
assert(!sourcemeta::core::rsplit_once("no separator", '.').has_value());
SOURCEMETA_CORE_TEXT_EXPORT auto rsplit_once(const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >

◆ split() [1/2]

auto sourcemeta::core::split ( const std::string_view input,
const char delimiter ) -> std::vector< std::string_view >
inline

Return the parts of input separated by delimiter as a vector, preserving empty parts. The parts are views into input, which must outlive them. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
const auto parts{sourcemeta::core::split("alpha;beta;gamma", ';')};
assert(parts.size() == 3);
assert(parts.at(0) == "alpha");
assert(parts.at(2) == "gamma");
auto split(const std::string_view input, const char delimiter, Callback callback) -> void
Definition text.h:535

◆ split() [2/2]

template<typename Callback>
auto sourcemeta::core::split ( const std::string_view input,
const char delimiter,
Callback callback ) -> void

Iterate the parts of input separated by delimiter, invoking callback with each part. For example:

#include <sourcemeta/core/text.h>
#include <iostream>
sourcemeta::core::split("alpha;beta;gamma", ';',
[](const std::string_view part) {
std::cout << part << '\n';
});

◆ split_once() [1/2]

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::split_once ( const std::string_view input,
const char delimiter ) -> std::optional< std::pair< std::string_view, std::string_view > >
noexcept

Split input at the first occurrence of delimiter, returning the parts before and after it. Return std::nullopt when the delimiter is absent. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
const auto parts{sourcemeta::core::split_once("key=value", '=')};
assert(parts.has_value());
assert(parts->first == "key");
assert(parts->second == "value");
assert(!sourcemeta::core::split_once("no separator", '=').has_value());
SOURCEMETA_CORE_TEXT_EXPORT auto split_once(const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >

◆ split_once() [2/2]

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::split_once ( const std::string_view input,
const std::string_view delimiter ) -> std::optional< std::pair< std::string_view, std::string_view > >
noexcept

Split input at the first occurrence of delimiter, returning the parts before and after it. Return std::nullopt when the delimiter is absent or empty. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
const auto parts{sourcemeta::core::split_once("1..5", "..")};
assert(parts.has_value());
assert(parts->first == "1");
assert(parts->second == "5");

◆ squeeze() [1/2]

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::squeeze ( const std::string_view input,
const char character ) -> std::string

Collapse consecutive runs of a character into a single occurrence. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::squeeze("a//b///c", '/') == "a/b/c");
SOURCEMETA_CORE_TEXT_EXPORT auto squeeze(const std::string_view input, const char character) -> std::string

◆ squeeze() [2/2]

template<typename Output>
auto sourcemeta::core::squeeze ( const std::string_view input,
const char character,
Output & output ) -> void

Collapse consecutive runs of a character into a single occurrence, appending the result to a string like output sink rather than allocating a new string. The output must not alias the input. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
std::string output{"path="};
sourcemeta::core::squeeze("a//b", '/', output);
assert(output == "path=a/b");

◆ starts_with_ignore_case()

auto sourcemeta::core::starts_with_ignore_case ( const std::string_view value,
const std::string_view prefix ) -> bool
inlinenoexcept

Return whether value begins with prefix under ASCII case-insensitive comparison. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::starts_with_ignore_case("__Host-id", "__host-"));
auto starts_with_ignore_case(const std::string_view value, const std::string_view prefix) noexcept -> bool
Definition text.h:867

Like the other case-insensitive comparisons, this is allocation-free.

◆ strip_left()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::strip_left ( const std::string_view input,
const char character ) -> std::string_view
noexcept

Return input with leading occurrences of character removed. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::strip_left("000123", '0') == "123");
assert(sourcemeta::core::strip_left("abc", '0') == "abc");
SOURCEMETA_CORE_TEXT_EXPORT auto strip_left(const std::string_view input, const char character) noexcept -> std::string_view

◆ strip_right()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::strip_right ( const std::string_view input,
const char character ) -> std::string_view
noexcept

Return input with trailing occurrences of character removed. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::strip_right("hello\r\r", '\r') == "hello");
assert(sourcemeta::core::strip_right("abc", '\r') == "abc");
SOURCEMETA_CORE_TEXT_EXPORT auto strip_right(const std::string_view input, const char character) noexcept -> std::string_view

◆ take_until()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::take_until ( const std::string_view input,
const char marker ) -> std::string_view
noexcept

Return the prefix of input up to (but excluding) the first occurrence of marker, or the full input when marker is absent. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::take_until("foo # bar", '#') == "foo ");
assert(sourcemeta::core::take_until("no marker", '#') == "no marker");
assert(sourcemeta::core::take_until("#leading", '#').empty());
SOURCEMETA_CORE_TEXT_EXPORT auto take_until(const std::string_view input, const char marker) noexcept -> std::string_view

◆ to_lowercase() [1/3]

template<typename Character>
auto sourcemeta::core::to_lowercase ( const Character character) -> Character
inlineconstexprnoexcept

Return the ASCII lowercase form of a character. Non-ASCII code units pass through unchanged. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::to_lowercase('A') == 'a');
assert(sourcemeta::core::to_lowercase('a') == 'a');
assert(sourcemeta::core::to_lowercase('5') == '5');
constexpr auto to_lowercase(const Character character) noexcept -> Character
Definition text.h:70

◆ to_lowercase() [2/3]

template<typename Character, typename Traits, typename Allocator>
auto sourcemeta::core::to_lowercase ( std::basic_string< Character, Traits, Allocator > & value) -> void
inline

Convert a string to ASCII lowercase in place. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
std::string value{"Hello WORLD"};
assert(value == "hello world");

◆ to_lowercase() [3/3]

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::to_lowercase ( std::filesystem::path & value) -> void

Convert a filesystem path to ASCII lowercase in place. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <filesystem>
std::filesystem::path value{"/Foo/Bar.JSON"};
assert(value == std::filesystem::path{"/foo/bar.json"});

◆ to_title_case()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::to_title_case ( std::string & value) -> void

Convert a string to Title Case in place. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
std::string value{"hello_world"};
assert(value == "Hello World");
SOURCEMETA_CORE_TEXT_EXPORT auto to_title_case(std::string &value) -> void

◆ trim()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::trim ( const std::string_view input) -> std::string_view
noexcept

Return input with leading and trailing ASCII whitespace removed. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::trim(" hello ") == "hello");
assert(sourcemeta::core::trim("\t\nfoo\r\n") == "foo");
assert(sourcemeta::core::trim(" ").empty());
SOURCEMETA_CORE_TEXT_EXPORT auto trim(const std::string_view input) noexcept -> std::string_view

◆ truncate()

SOURCEMETA_CORE_TEXT_EXPORT auto sourcemeta::core::truncate ( std::string & input,
const std::size_t maximum_length,
const std::string_view marker ) -> void

Truncate a string in place to at most maximum_length bytes, appending marker on truncation. Rewinds to a UTF-8 code-point boundary so multi-byte characters are never split. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
#include <string>
std::string value{"hello"};
sourcemeta::core::truncate(value, 1, "...");
assert(value == "h...");
SOURCEMETA_CORE_TEXT_EXPORT auto truncate(std::string &input, const std::size_t maximum_length, const std::string_view marker) -> void

◆ unquote()

auto sourcemeta::core::unquote ( const std::string_view input,
const char quote ) -> std::string_view
inlineconstexprnoexcept

Return the content of input with a single matched pair of surrounding quote characters removed, or input unchanged when it is not wrapped in that pair. For example:

#include <sourcemeta/core/text.h>
#include <cassert>
assert(sourcemeta::core::unquote("\"abc\"", '"') == "abc");
assert(sourcemeta::core::unquote("abc", '"') == "abc");
assert(sourcemeta::core::unquote("\"", '"') == "\"");
constexpr auto unquote(const std::string_view input, const char quote) noexcept -> std::string_view
Definition text.h:415

Variable Documentation

◆ digits_capacity

template<typename Integer>
std::size_t sourcemeta::core::digits_capacity
inlineconstexpr
Initial value:
{
static_cast<std::size_t>(std::numeric_limits<Integer>::digits10) + 1 +
(std::numeric_limits<Integer>::is_signed ? 1U : 0U)}

The exact number of characters that the decimal spelling of an integer type can require. The largest magnitude needs one character more than the type represents without loss, and a signed type needs one more again for a sign.