DataReelDataReelC++ Toolkit Documentation

String Utilities

Header-only helpers for std::string in the datareel namespace. They replace the 6.x UString/gxString classes.

Contents

Overview
Trimming and Case
Comparing and Searching
Editing and Splitting
Formatting and Conversion

Overview

DataReel 7 uses std::string and std::string_view throughout. strutil.h adds what the C++17 standard library does not have. Conversions to numbers use atoi() rules: leading space is skipped, conversion stops at the first invalid character and 0 is returned for no number.

Porting from gxString: s.TrimLeadingSpaces() becomes trim_left_in_place(s), s.DeleteBeforeIncluding("x") becomes delete_before_including(s, "x"), s.Atoi() becomes to_int(s), s.IFind(p) becomes ifind(s, p), s.ToUpper() becomes s = to_upper(s), and chains such as s << "id=" << 42 become str_append(s, "id=", 42).

Trimming and Case

std::string trim(std::string_view s) / trim_left / trim_right

Copies without leading and/or trailing white space. trim_in_place(), trim_left_in_place() and trim_right_in_place() change a string.

std::string to_upper(std::string_view s) / to_lower

ASCII case conversion.

Comparing and Searching

bool iequals(std::string_view a, std::string_view b)

Case-insensitive (ASCII) equality.

std::size_t ifind(std::string_view s, std::string_view p, std::size_t pos = 0)

Case-insensitive find; std::string::npos if not found.

bool starts_with / ends_with / istarts_with

Prefix and suffix tests.

int compare3(std::string_view a, std::string_view b)

Three-way compare returning -1, 0 or 1.

int natural_compare(std::string_view a, std::string_view b, bool check_case = true)

Digit runs compare by value, so item9 sorts before item10.

Editing and Splitting

std::size_t replace_all(std::string &s, std::string_view from, std::string_view to)

Replaces every occurrence; returns the count.

void erase_char(std::string &s, char c)

Removes every c.

bool delete_before / delete_before_including / delete_before_last / delete_after / delete_after_including / delete_after_last ...

Delete text around the first or last occurrence of a pattern. Each returns true if the pattern was found.

std::vector<std::string> split(std::string_view s, char sep, bool skip_empty = false)

Splits on a separator.

int convert_escape_seq(std::string &s, char esc = '%', int num_octets = 2)

Converts hex escapes such as %41 to characters.

Formatting and Conversion

std::string &str_append(std::string &s, const Args &...args)

Appends each argument: strings and characters as text, numbers in decimal. str_cat(args...) returns a new string.

std::string format(const char *fmt, ...)

printf-style formatting into a std::string.

int to_int(const std::string &s) / to_long / to_llong / to_ullong / to_double

Number conversion without exceptions.

std::size_t copy_to(char *dest, std::size_t dest_size, std::string_view s)

Copies into a fixed buffer, always null terminated.

std::size_t bounded_length(const char *s, std::size_t max)

Length of a fixed buffer that may not be null terminated.