ark::strings

Variables

Defined in “ark/strings/join.hh”:

  • concept JoinFundamentalType= std::is_fundamental_v

Classes

  • ark::strings::NumericRange
    Represents a parsed numeric range. General convention is that both minimum and maximum are inclusive.

Functions

Declared in “ark/strings/case.hh”:

  • std::string lowercase(const std::string_view & input)
    Takes the given input string and returns a copy that is all in lowercase (as if tolower was called on every character).

     EXPECT_EQ(lowercase("THIS is A test STRING"), "this is a test string");
    

Declared in “ark/strings/convert.hh”:

  • Type from_string(size_t base)
    Converts the given string into the specified templated type. Dispatches to equivalent strtoXX() functions.

     EXPECT_TRUE(from_string<bool>("true"));
     EXPECT_NEAR(from_string<double>("3.14159"), 3.14159, 0.00001);
     EXPECT_EQ(from_string<uint32_t>("123456789"), 123456789);
     EXPECT_EQ(from_string<uint32_t>("FA0A9A01", 16), 0xfa0a9a01);
    
  • uint8_t from_string(std::string_view string, size_t base)
    from_string for uint8_t.

    from_string for a std::string. from_string for bool. from_string for double. from_string for float. from_string for int64_t. from_string for uint64_t. from_string for int32_t. from_string for uint32_t. from_string for int16_t. from_string for uint16_t. from_string for int8_t. This conversion will return true for any string that starts with a upper or lowercase T or a 1.

  • void from_string(std::string_view string, Type & output)
    This allows you to convert from a string when you aren’t sure about the incoming type.

     int64_t type;
     from_string("-5000", type);
    
     EXPECT_EQ(type, -5000);
    
  • Type from_string_any_base(std::string_view string)
    This allows you to convert from a string, when you aren’t sure of its base. This handles conversion from hex or decimal.

     EXPECT_EQ(from_string_any_base<uint16_t>("0xabcd"), 0xabcd);
     EXPECT_EQ(from_string_any_base<uint32_t>("0x0123abcd"), 0x0123abcd);
     EXPECT_EQ(from_string_any_base<int8_t>("0x1"), 0x1);
     EXPECT_EQ(from_string_any_base<uint8_t>("0x0"), 0);
    
  • void from_string_any_base(std::string_view string, Type & output)
    This allows you to convert from a string, when you aren’t sure of its base. This handles conversion from hex or decimal, and additionally helps when you aren’t sure about the incoming type.

     uint16_t value = 0;
    
     from_string_any_base("0xabcd", value);
     EXPECT_EQ(value, 0xabcd);
    
     from_string_any_base("1", value);
     EXPECT_EQ(value, 1);
    
  • bool is_number(std::string_view string)
    Returns true if the given string is an integer (given by type) or false otherwise.

Declared in “ark/strings/distance.hh”:

  • size_t edit_distance(const std::string & query, const std::string & target)
    Returns the edit distance between the query string and the target string.

  • const std::string & closest_string_match(const std::string & query, InputIt first, InputIt last)
    The specified iterators will walk over the given collection, which is expected to be a collection of strings. A const reference to the string which most closely matches the query string is returned.

    This is typically used to correct minor mispellings, when you have some idea of what they might be looking for.

Declared in “ark/strings/filter.hh”:

  • bool does_string_pass_filters(const std::vector< std::string > & allow_list, const std::vector< std::string > & block_list, const std::string & test_value)
    Returns true if the given string passes the given allow/block list filters. This will work on the following logic:

    If the allow list is not empty, only pass strings that are in the allow list and not in the block list. If the block list is not empty, never pass strings that are in the block list, even if they are in the allow list. If both the block list and allow list are empty, all strings will be returned. Strings in the allow/block list are assumed to be regexes.

     const std::vector<std::string> allow_list = {"Abc", "Def"};
     const std::vector<std::string> block_list = {"Abc"};
    
     EXPECT_FALSE(does_string_pass_filters(allow_list, block_list, "Abc"));
     EXPECT_TRUE(does_string_pass_filters(allow_list, block_list, "Def"));
     EXPECT_FALSE(does_string_pass_filters(allow_list, block_list, "abc"));
     EXPECT_FALSE(does_string_pass_filters(allow_list, block_list, "def"));
     EXPECT_FALSE(does_string_pass_filters(allow_list, block_list, "Abcd"));
     EXPECT_FALSE(does_string_pass_filters(allow_list, block_list, "AbcDef"));
    

Declared in “ark/strings/format.hh”:

  • std::string format_capacity(uint64_t capacity)
    Formats the given capacity value (megabytes, gigabytes, etc), as a human readable string with an appropriate suffix.

  • std::string format_seconds(std::chrono::seconds value)
    formats seconds as human readable string

  • std::string format_expiration_timepoint(const std::chrono::system_clock::time_point & expiration_time)
    Formats expiration time point as datetime and also display a duration from the current time. YYYY/MM/DD HH:MM:SS TZ [hours minutes].

Declared in “ark/strings/join.hh”:

  • std::string join(const std::vector< std::string > & array, const std::string & delimiter)
    Joins the given array of strings into a single string, separated by the specified delimiter.

  • std::string join(const std::vector< Type > & array, const std::string & delimiter)
    Joints the given array into a single string, separated by the specified delimiter. Converts the type into a string by using std::to_string.

Declared in “ark/strings/range.hh”:

  • std::vector< NumericRange > parse_range(const std::string_view & range)
    Parses a string, in the form of “1-3” or “1” or “1,2,3” and returns some number of NumericRange structures representing the parse result.

     auto parsed = parse_range("1,-2,-3,4");
    
     ASSERT_EQ(parsed.size(), 4);
     EXPECT_EQ(parsed[0].minimum, 1);
     EXPECT_EQ(parsed[0].maximum, 1);
     EXPECT_EQ(parsed[1].minimum, -2);
     EXPECT_EQ(parsed[1].maximum, -2);
     EXPECT_EQ(parsed[2].minimum, -3);
     EXPECT_EQ(parsed[2].maximum, -3);
     EXPECT_EQ(parsed[3].minimum, 4);
     EXPECT_EQ(parsed[3].maximum, 4);
    
  • std::vector< bool > range_to_bitmask(const std::vector< NumericRange > & ranges)
    Takes an array of numeric ranges and returns them as a bitmask, with each bit in the vector set to indicate if that range is true or false.

     auto result = range_to_bitmask(parse_range("1-3,5-7,10"));
    
     ASSERT_EQ(result.size(), 11);
    
     EXPECT_EQ(result[0], false);
     EXPECT_EQ(result[1], true);
     EXPECT_EQ(result[2], true);
     EXPECT_EQ(result[3], true);
     EXPECT_EQ(result[4], false);
     EXPECT_EQ(result[5], true);
     EXPECT_EQ(result[6], true);
     EXPECT_EQ(result[7], true);
     EXPECT_EQ(result[8], false);
     EXPECT_EQ(result[9], false);
     EXPECT_EQ(result[10], true);
    
  • std::vector< bool > range_to_bitmask(const std::string_view & range)
    Convenience API that takes a string range (as if you would pass to parse_range) and converts it to a bitmask (as if returning a result from range_to_bitmask).

     EXPECT_EQ(range_to_bitmask(parse_range("6-4,2-1")), range_to_bitmask("6-4,2-1"));
     EXPECT_EQ(range_to_bitmask(parse_range("1-4")), range_to_bitmask("1-4"));
     EXPECT_EQ(range_to_bitmask(parse_range("2")), range_to_bitmask("2"));
    

Declared in “ark/strings/replace.hh”:

  • std::string replace(const std::string_view & source, const std::initializer_list< std::pair< std::string_view, std::string_view » & replacements)
    Replaces all instances of the “keys” in the replacements list with the “values” in the replacements list, in the source string. Returns a copt of the final string.

  • std::string replace(const std::string_view & source, const StringMapping & replacements)
    Replaces all instances of the “keys” in the replacements list with the “values” in the replacements list, in the source string. Returns a copy of the final string.

     auto result = replace("Test String", {{"Test", "Hello"}, {"String", "Value"}});
    
     EXPECT_EQ(result, "Hello Value");
    

Declared in “ark/strings/split.hh”:

  • std::vector< std::string_view > split_view(const std::string_view & string, const std::string_view & deliminators)
    Splits up the given string according to the specified deliminators, returning a list of string_view objects pointing to the splits.

    This avoids copying strings, and may be more efficient if you can guarantee the source string doesn’t fall out of scope while you use this API.

     auto results = split_view("This is a test", " ");
    
     ASSERT_EQ(results.size(), 4);
     EXPECT_EQ(results[0], "This");
     EXPECT_EQ(results[1], "is");
     EXPECT_EQ(results[2], "a");
     EXPECT_EQ(results[3], "test");
    
  • Container split(const std::string_view & string, const std::string_view & deliminators)
    Splits up the given string according to the specified deliminators, returning a list of string_view objects pointing to the splits.

    By default, this assumes you want to return into a vector of strings.

     auto results = split<std::set<std::string>>("Key1 Key2 Key3", " ");
    
     EXPECT_EQ(results.size(), 3);
     EXPECT_GT(results.count("Key1"), 0);
     EXPECT_GT(results.count("Key2"), 0);
     EXPECT_GT(results.count("Key3"), 0);
    

Declared in “ark/strings/trim.hh”:

  • std::string_view trim(const std::string_view & string)
    Trims the given string, removing leading and trailing whitespace. Whitespace is currently defined as spaces, carriage returns, newlines, and tabs.

  • std::string_view trim_left(const std::string_view & string)
    Trims the given string, removing leading whitespace. Whitespace is currently defined as spaces, carriage returns, newlines, and tabs.

  • std::string_view trim_right(const std::string_view & string)
    Trims the given string, removing trailing whitespace. Whitespace is currently defined as spaces, carriage returns, newlines, and tabs.

  • std::string_view trim(const std::string_view & string, const char * whitespace)
    Trims the given string, removing leading and trailing whitespace. Whitespace is defined by the second parameter (each character is a unique whitespace character).

  • std::string_view trim_left(const std::string_view & string, const char * whitespace)
    Trims the given string, removing leading whitespace. Whitespace is defined by the second parameter (each character is a unique whitespace character).

  • std::string_view trim_right(const std::string_view & string, const char * whitespace)
    Trims the given string, removing trailing whitespace. Whitespace is defined by the second parameter (each character is a unique whitespace character).

Declared in “ark/strings/wrap.hh”:

  • std::vector< std::string > line_wrap_string(std::string_view text, size_t columns)
    Takes the given text and wraps it to the specified number of columns. Returns back a string view containing all of the text. Word breaks will be made on whitespace boundaries, and newlines will be preserved.

  • std::vector< std::string > line_wrap_string_ignoring_breaks(std::string_view source, size_t columns)
    Line wraps a string with the given number of columns, ignoring all formatting or wordbreaks.