SYNOPSIS
Functional string helpers.
USAGE
This module is designed to work with the datcxx
build tool. To add this
module to your project us the following command...
build add datcxx/util-string
TEST
build test
STRING
SEARCH
Search a string for regular expression matches, returns a vector<string>
.
std::string f = "/test/parallel/test-path.js";
std::vector<string> r = search(f, "\\w+");
// r.size() == 5
MATCH
Same as search but return a cmatch starting with the complete input at
index 0
. This will potentially be depricated.
std::string f = "/test/parallel/test-path.js";
cmatch r = match(f, "/(\\w+)/(.*)");
// r.size() == 3
SPLIT
Split on a regular expression.
std::string f = "/test/parallel/test-path.js";
vector<string> r = split(f, "/");
// r.size() == 4
TEST
Test if a string is an exact match.
std::string f = "/test/parallel/test-path.js";
bool r = test(f, "^/(\\w+)/(\\w+)/(\\w+)-path.js$");
// r == true
TRIM, RTRIM, LTRIM
string r = trim("/foobar//", "/");
// r.trim() == "foobar"
string r = rtrim("!Hello, World!!", "!");
// r == "!Hello, World"
string r = ltrim("!Hello, World!!", "!");
// r == "Hello, World!!"
REPLACE
string r = replace("Hello, World!", "o", "x");
// r == "Hellx, Wxrld!"
const string s = "Hello, World!";
string r = replace(s, "l", [&](string const& m, int index) {
return string("L" + to_string(index));
});
// r == "HeL0L1o, WorL2d!"