Line data Source code
1 : /* Copyright (C) 2025 Robert Schiwon 2 : * 3 : * This file is part of libjdx. 4 : * 5 : * libjdx is free software: you can redistribute it and/or modify it under the 6 : * terms of the GNU Lesser General Public License as published by the Free 7 : * Software Foundation, either version 3 of the License, or (at your option) any 8 : * later version. 9 : * 10 : * libjdx is distributed in the hope that it will be useful, but WITHOUT ANY 11 : * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 : * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 13 : * details. 14 : * 15 : * You should have received a copy of the GNU Lesser General Public License 16 : * along with libjdx. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : #include "util/StringUtils.hpp" 19 : #include "jdx/ParseException.hpp" 20 : 21 : #include <algorithm> 22 : #include <functional> 23 : #include <regex> 24 : #include <string> 25 : 26 8351 : void libjdx::jdx::util::trimLeft(std::string& s) 27 : { 28 8351 : s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not_fn(isSpace))); 29 8351 : } 30 : 31 8377 : void libjdx::jdx::util::trimRight(std::string& s) 32 : { 33 8377 : s.erase(std::find_if(s.rbegin(), s.rend(), std::not_fn(isSpace)).base(), 34 16754 : s.end()); 35 8377 : } 36 : 37 8280 : void libjdx::jdx::util::trim(std::string& s) 38 : { 39 8280 : trimRight(s); 40 8280 : trimLeft(s); 41 8280 : } 42 : 43 35301 : bool libjdx::jdx::util::isSpace(char c) 44 : { 45 35301 : return static_cast<bool>(std::isspace(static_cast<unsigned char>(c))); 46 : } 47 : 48 70 : void libjdx::jdx::util::toLower(std::string& s) 49 : { 50 : std::transform(s.begin(), s.end(), s.begin(), 51 743 : [](unsigned char c) { return std::tolower(c); }); 52 70 : } 53 : 54 578 : std::vector<std::string> libjdx::jdx::util::split(const std::string& input, 55 : const std::string& delimiterRegEx, bool trimSegments, size_t matchGroup) 56 : { 57 1156 : auto remainder = input; 58 1156 : std::regex delimiter{delimiterRegEx}; 59 1156 : std::smatch match; 60 578 : std::vector<std::string> output; 61 1908 : while (std::regex_search(remainder, match, delimiter)) 62 : { 63 1330 : auto matchPos = static_cast<size_t>(match.position(matchGroup)); 64 1330 : std::string segment = remainder.substr(0, matchPos); 65 1330 : output.push_back(segment); 66 : auto nextPos 67 1330 : = matchPos + static_cast<size_t>(match[matchGroup].length()); 68 1330 : remainder = remainder.substr(nextPos); 69 : } 70 578 : output.push_back(remainder); 71 : 72 578 : if (trimSegments) 73 : { 74 576 : std::for_each(output.begin(), output.end(), trim); 75 : } 76 : 77 1156 : return output; 78 : }