FOSSredder 0.5.0
Loading...
Searching...
No Matches
StringUtils.cpp
Go to the documentation of this file.
1
6
7#include "StringUtils.h"
8
9#include <algorithm>
10#include <cctype>
11#include <regex>
12#include <sstream>
13#include <string>
14#include <utility>
15#include <vector>
16
17namespace core::utils {
18
19std::string trim(std::string s) {
20 const auto notSpace = [](unsigned char c) {
21 return !std::isspace(c);
22 };
23 s.erase(s.begin(), std::find_if(s.begin(), s.end(), notSpace));
24 s.erase(std::find_if(s.rbegin(), s.rend(), notSpace).base(), s.end());
25 return s;
26}
27
28std::string lowerAscii(std::string s) {
29 for (auto& ch : s)
30 ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
31 return s;
32}
33
34std::string collapseWhitespace(std::string s) {
35 static const std::regex spacesRe(R"(\s+)");
36 s = std::regex_replace(std::move(s), spacesRe, " ");
37 return trim(std::move(s));
38}
39
40std::vector<std::string> splitWhitespace(const std::string& s) {
41 std::istringstream iss(s);
42 std::vector<std::string> out;
43 std::string t;
44 while (iss >> t)
45 out.push_back(std::move(t));
46 return out;
47}
48
49} // namespace core::utils
Declares private string helpers shared across core implementation files.
std::vector< std::string > splitWhitespace(const std::string &s)
Splits a string on ASCII whitespace.
std::string collapseWhitespace(std::string s)
Collapses consecutive whitespace to single spaces and trims the result.
std::string lowerAscii(std::string s)
Converts ASCII letters in a string to lowercase.
std::string trim(std::string s)
Removes leading and trailing ASCII whitespace from a string.