31 #ifndef HELPER_STRINGS_HPP_ 32 #define HELPER_STRINGS_HPP_ 34 #include <boost/algorithm/string.hpp> 46 #include <string_view> 57 using std::string_view_literals::operator
""sv;
91 "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"sv
104 std::string& strInOut,
105 std::string_view needle,
106 std::string_view replacement
119 bool isDec(std::string_view inputString);
120 bool isHex(std::string_view inputString);
126 void trim(std::string& stringToTrim);
133 const std::vector<std::string>& strings,
138 const std::vector<std::string>& strings,
139 std::string_view delimiter,
143 std::queue<std::string>& strings,
148 std::queue<std::string>& strings,
149 std::string_view delimiter,
153 const std::vector<std::string>& strings,
156 std::string& appendTo
159 const std::vector<std::string>& strings,
160 std::string_view delimiter,
162 std::string& appendTo
165 std::queue<std::string>& strings,
168 std::string& appendTo
171 std::queue<std::string>& strings,
172 std::string_view delimiter,
174 std::string& appendTo
181 std::vector<std::string>
split(
const std::string& str,
char delimiter);
182 std::vector<std::string>
split(std::string_view str, std::string_view delimiter);
185 std::string_view str,
190 std::string_view str,
191 std::string_view delimiter,
217 void utfTidy(std::string& stringToTidy);
247 std::string& strInOut,
248 std::string_view needle,
249 std::string_view replacement
251 std::size_t startPos{};
257 while((startPos = strInOut.find(needle, startPos)) != std::string::npos) {
258 strInOut.replace(startPos, needle.length(), replacement);
260 startPos += replacement.length();
283 return std::tolower(c);
287 std::istringstream strStr(inputString);
291 strStr >> std::boolalpha >> result;
303 inline bool isDec(std::string_view inputString) {
306 for(
const auto c : inputString) {
346 inline bool isHex(std::string_view inputString) {
347 return inputString.find_first_not_of(
348 "0123456789AaBbCcDdEeFf" 349 ) == std::string_view::npos;
360 inline void trim(std::string& stringToTrim) {
362 stringToTrim.begin(),
364 stringToTrim.begin(),
367 return std::isspace(ch) == 0;
374 stringToTrim.rbegin(),
377 return std::isspace(ch) == 0;
379 ).base(), stringToTrim.end());
397 const std::vector<std::string>& strings,
405 for(
const auto&
string : strings) {
406 if(!ignoreEmpty || !
string.empty()) {
407 size +=
string.size() + 1;
411 result.reserve(size);
414 for(
const auto&
string : strings) {
415 if(!ignoreEmpty || !
string.empty()) {
421 if(!result.empty()) {
444 const std::vector<std::string>& strings,
445 std::string_view delimiter,
452 for(
const auto&
string : strings) {
453 if(!ignoreEmpty || !
string.empty()) {
454 size +=
string.size() + delimiter.size();
458 result.reserve(size);
461 for(
const auto&
string : strings) {
462 if(!ignoreEmpty || !
string.empty()) {
468 if(!result.empty()) {
494 std::queue<std::string>& strings,
501 while(!strings.empty()) {
502 if(!ignoreEmpty || !(strings.front().empty())) {
503 result += strings.front() + delimiter;
509 if(!result.empty()) {
535 std::queue<std::string>& strings,
536 std::string_view delimiter,
542 while(!strings.empty()) {
543 if(!ignoreEmpty || !(strings.front().empty())) {
544 result += strings.front();
551 if(!result.empty()) {
573 const std::vector<std::string>& strings,
576 std::string& appendTo
579 const auto oldSize{appendTo.size()};
584 for(
const auto&
string : strings) {
585 if(!ignoreEmpty || !
string.empty()) {
586 size +=
string.size() + 1;
590 appendTo.reserve(size);
593 for(
const auto&
string : strings) {
594 if(!ignoreEmpty || !
string.empty()) {
595 appendTo +=
string + delimiter;
599 if(appendTo.size() > oldSize) {
618 const std::vector<std::string>& strings,
619 std::string_view delimiter,
621 std::string& appendTo
624 const auto oldSize{appendTo.size()};
629 for(
const auto&
string : strings) {
630 if(!ignoreEmpty || !
string.empty()) {
631 size +=
string.size() + delimiter.size();
635 appendTo.reserve(size);
638 for(
const auto&
string : strings) {
639 if(!ignoreEmpty || !
string.empty()) {
641 appendTo += delimiter;
645 if(appendTo.size() > oldSize) {
667 std::queue<std::string>& strings,
670 std::string& appendTo
673 const auto oldSize{appendTo.size()};
676 while(!strings.empty()) {
677 if(!ignoreEmpty || !(strings.front().empty())) {
678 appendTo += strings.front() + delimiter;
684 if(appendTo.size() > oldSize) {
706 std::queue<std::string>& strings,
707 std::string_view delimiter,
709 std::string& appendTo
712 const auto oldSize{appendTo.size()};
715 while(!strings.empty()) {
716 if(!ignoreEmpty || !(strings.front().empty())) {
717 appendTo += strings.front();
718 appendTo += delimiter;
724 if(appendTo.size() > oldSize) {
739 inline std::vector<std::string>
split(
const std::string& str,
char delimiter) {
740 std::vector<std::string> result;
743 return c == delimiter;
759 inline std::vector<std::string>
split(std::string_view str, std::string_view delimiter) {
760 std::string tmp(str);
761 std::vector<std::string> result;
763 while(!tmp.empty()) {
764 auto index{tmp.find(delimiter)};
766 if(index != std::string::npos) {
767 result.emplace_back(tmp, 0, index);
769 tmp = tmp.substr(index + delimiter.size());
771 else if(!tmp.empty()) {
772 result.emplace_back(tmp);
795 std::string_view str,
799 std::queue<std::string>::container_type result;
802 return c == delimiter;
810 [](
const auto& str) {
818 return std::queue<std::string>(result);
835 std::string_view str,
836 std::string_view delimiter,
839 std::string tmp(str);
840 std::queue<std::string> result;
842 while(!tmp.empty()) {
843 auto index{tmp.find(delimiter)};
845 if(index != std::string::npos) {
846 if(!removeEmpty || index > 0) {
847 result.emplace(tmp, 0, index);
850 tmp = tmp.substr(index + delimiter.size());
852 else if(!tmp.empty()) {
874 std::vector<std::string>& vectorOfStrings,
879 std::sort(vectorOfStrings.begin(), vectorOfStrings.end());
882 vectorOfStrings.erase(
884 vectorOfStrings.begin(),
885 vectorOfStrings.end()
887 vectorOfStrings.end()
893 vectorOfStrings.begin(),
894 vectorOfStrings.end(),
895 [](
const auto& s1,
const auto& s2) {
902 [](
const auto& s1,
const auto& s2) {
903 return (s1 == s2) || std::tolower(s1) == std::tolower(s2);
908 return result.second != s2.cend()
910 result.first == s1.cend()
911 || std::tolower(*result.first) < std::tolower(*result.second)
917 vectorOfStrings.erase(std::unique(vectorOfStrings.begin(), vectorOfStrings.end(),
918 [](
const auto& s1,
const auto& s2) {
919 return (s1.size() == s2.size()) && std::equal(s1.cbegin(), s1.cend(), s2.cbegin(),
920 [](
const auto& c1,
const auto& c2) {
921 return (c1 == c2) || std::tolower(c1) == std::tolower(c2);
925 ), vectorOfStrings.end());
950 if(from.at(0) ==
'\\' && from.length() > 1) {
981 pos = stringToEncode.find(
'%', pos);
983 if(pos == std::string::npos) {
989 || !
isHex(stringToEncode.substr(pos + 1, 2))
991 stringToEncode.insert(pos + 1,
"25");
998 }
while(pos < stringToEncode.length());
1018 stringToTidy.begin(),
1020 stringToTidy.begin(),
1039 while(stringToTidy.find(
" ") != std::string::npos) {
1068 return name.find_first_of(
"/'") == std::string_view::npos;
1080 return name.find_first_not_of(
1081 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_" 1082 ) == std::string_view::npos;
1093 thread_local
static std::default_random_engine rengine(
1094 std::random_device{}()
1097 thread_local
static std::uniform_int_distribution<std::size_t> distribution(
1104 result.reserve(length);
1106 while(length-- > 0) {
constexpr std::array utfWhitespaces
UTF-8 whitespaces used by utfTidy().
Definition: Strings.hpp:63
Namespace for global string helper functions.
Definition: Strings.hpp:51
std::string join(const std::vector< std::string > &strings, char delimiter, bool ignoreEmpty)
Concatenates all elements of a vector into a single string.
Definition: Strings.hpp:396
void encodePercentage(std::string &stringToEncode)
Encodes percentage signs that are not followed by a two-digit hexadecimal number with %25...
Definition: Strings.hpp:977
void trim(std::string &stringToTrim)
Removes whitespaces around a string.
Definition: Strings.hpp:360
std::queue< std::string > splitToQueue(std::string_view str, char delimiter, bool removeEmpty)
Splits a string into a queue of strings using the given delimiter.
Definition: Strings.hpp:794
void sortAndRemoveDuplicates(std::vector< std::string > &vectorOfStrings, bool caseSensitive)
Sorts the given vector of strings and removes duplicates.
Definition: Strings.hpp:873
void utfTidy(std::string &stringToTidy)
Removes new lines and unnecessary spaces, including UTF-8 whitespaces.
Definition: Strings.hpp:1007
void replaceAll(std::string &strInOut, std::string_view needle, std::string_view replacement)
Replaces all occurences within a string with another string.
Definition: Strings.hpp:246
char getFirstOrEscapeChar(std::string_view from)
Gets the first character or an escaped character from the beginning of the given string.
Definition: Strings.hpp:948
bool isDec(std::string_view inputString)
Checks whether a string contains only decimal digits and max. one dot (.).
Definition: Strings.hpp:303
std::size_t length(std::string_view str)
Definition: Utf8.hpp:327
bool stringToBool(std::string inputString)
Converts a string into a boolean value.
Definition: Strings.hpp:277
bool checkSQLName(std::string_view name)
Checks whether the given string is a valid name for MySQL tables and fields.
Definition: Strings.hpp:1079
std::vector< std::string > split(std::string_view str, std::string_view delimiter)
Splits a string into a vector of strings using the given delimiter.
Definition: Strings.hpp:759
constexpr auto checkHexLength
Length of a two-digit hexademical number including the preceding percentage sign. ...
Definition: Strings.hpp:87
bool checkDomainName(std::string_view name)
Checks whether the given string is a a valid domain name.
Definition: Strings.hpp:1067
constexpr auto randCharSet
Characters to be chosen from for random string generation performed by generateRandom().
Definition: Strings.hpp:90
bool isHex(std::string_view inputString)
Checks whether a string contains only hexadecimal digits.
Definition: Strings.hpp:346
std::string generateRandom(std::size_t length)
Generates a random alpha-numerical string of the given length.
Definition: Strings.hpp:1092
std::vector< std::string > split(const std::string &str, char delimiter)
Splits a string into a vector of strings using the given delimiter.
Definition: Strings.hpp:739