Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
string_algorithms: Algorithms For Manipulating Strings

Arene base provides algorithms to operate on strings, for trimming whitespace, comparisons, and constexpr length calculations.

The public header is:

Public export header for the string_algorithms subpackage.

The Bazel target is

//:string_algorithms

Introduction

The string_algorithms package provides functions for working with strings:

  • arene::base::ltrim, arene::base::rtrim and arene::base::trim for removing whitespace characters from the ends of a string,
  • arene::base::string_length, which is a constexpr-compatible version of strlen, and
  • arene::base::lexicographic_string_compare, which does a 3-way comparison of two strings, and is used internally in the comparison functions of string_view and inline_string.

String trimming functions

Arene Base provides three sets of string trimming functions:

In all cases, an overload is provided which accepts an arene::base::string_view, and returns an arene::base::string_view to the same underlying characters, except omitting the trimmed whitespace, along with an overload that accepts a reference to an arene::base::inline_string, and returns a copy of the string, with the trimmed whitespace removed.

arene::base::inline_string<20> s{" hello world "};
auto s1 = arene::base::ltrim(s); // copy of "hello world "
auto s2 = arene::base::rtrim(s); // copy of " hello world"
auto s3 = arene::base::trim(arene::base::string_view{s}); // string_view for "hello world" in s
A string type with a fixed maximum capacity, where the data is stored internally without allocations.
Definition inline_string.hpp:160
Backport of the C++17 std::string_view class.
Definition string_view.hpp:52
constexpr auto trim(string_view const str) -> string_view
Create a copy of the string view with leading and trailing whitespace removed.
Definition trim.hpp:77
constexpr auto rtrim(string_view const str) -> string_view
Create a copy of the string view with trailing whitespace removed.
Definition trim.hpp:55
constexpr auto ltrim(string_view const str) -> string_view
Create a copy of the string view with leading whitespace removed.
Definition trim.hpp:32

arene::base::string_length

This function is purely a constexpr-compatible version of strlen, and is therefore used in the implementation of the string types, to enable them to work in constexpr code.

arene::base::lexicographic_string_compare

This function does a three-way comparison of two strings, represented either as a C-style const char* string or a arene::base::span<const char>, and is used internally in the comparison functions of string_view and inline_string. It returns an arene::base::strong_ordering value indicating whether the first string is less than, equal to, or greater than the second string, using a lexical ordering that ensures UTF-8 characters are ordered correctly.