Mercurial > minori
diff dep/animone/src/util.cc @ 301:b1f625b0227c
*: convert all files CRLF -> LF
some files were in DOS format, others were in unix. now everything
(that at least is under our control) should all be the same format
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 13 May 2024 15:04:51 -0400 |
parents | 862d0d8619f6 |
children |
line wrap: on
line diff
--- a/dep/animone/src/util.cc Mon May 13 14:56:37 2024 -0400 +++ b/dep/animone/src/util.cc Mon May 13 15:04:51 2024 -0400 @@ -1,85 +1,85 @@ -#include <algorithm> -#include <fstream> -#include <regex> -#include <sstream> -#include <string> - -#include "animone/util.h" - -namespace animone::internal::util { - -bool ReadFile(const std::string& path, std::string& data) { - std::ifstream file(path.c_str(), std::ios::in | std::ios::binary); - if (!file) - return false; - - std::ostringstream string; - string << file.rdbuf(); - file.close(); - - data = string.str(); - - return true; -} - -/* this assumes ASCII... which really should be the case for what we need, anyway */ -bool EqualStrings(const std::string& str1, const std::string& str2) { - auto tolower = [](const char c) -> char { return ('A' <= c && c <= 'Z') ? c + ('a' - 'A') : c; }; - - auto equal_chars = [&tolower](const char c1, const char c2) -> bool { return tolower(c1) == tolower(c2); }; - - return str1.length() == str2.length() && std::equal(str1.begin(), str1.end(), str2.begin(), equal_chars); -} - -bool Stem(const std::string& filename, std::string& stem) { - unsigned long long pos = filename.find_last_of("."); - if (pos != std::string::npos) - return false; - - stem = filename.substr(0, pos); - return true; -} - -bool CheckPattern(const std::string& pattern, const std::string& str) { - if (pattern.empty()) - return false; - if (pattern.front() == '^' && std::regex_match(str, std::regex(pattern))) - return true; - return util::EqualStrings(pattern, str); -} - -bool TrimLeft(std::string& str, const char* chars) { - if (str.empty()) - return false; - - const auto found = str.find_first_not_of(chars); - - if (found == 0) - return false; - - if (found == std::string::npos) - str.clear(); - else - str.erase(0, found); - - return true; -} - -bool TrimRight(std::string& str, const char* chars) { - if (str.empty()) - return false; - - const auto found = str.find_last_not_of(chars); - - if (found == str.size() - 1) - return false; - - if (found == std::string::npos) - str.clear(); - else - str.resize(found + 1); - - return true; -} - -} // namespace animone::internal::util +#include <algorithm> +#include <fstream> +#include <regex> +#include <sstream> +#include <string> + +#include "animone/util.h" + +namespace animone::internal::util { + +bool ReadFile(const std::string& path, std::string& data) { + std::ifstream file(path.c_str(), std::ios::in | std::ios::binary); + if (!file) + return false; + + std::ostringstream string; + string << file.rdbuf(); + file.close(); + + data = string.str(); + + return true; +} + +/* this assumes ASCII... which really should be the case for what we need, anyway */ +bool EqualStrings(const std::string& str1, const std::string& str2) { + auto tolower = [](const char c) -> char { return ('A' <= c && c <= 'Z') ? c + ('a' - 'A') : c; }; + + auto equal_chars = [&tolower](const char c1, const char c2) -> bool { return tolower(c1) == tolower(c2); }; + + return str1.length() == str2.length() && std::equal(str1.begin(), str1.end(), str2.begin(), equal_chars); +} + +bool Stem(const std::string& filename, std::string& stem) { + unsigned long long pos = filename.find_last_of("."); + if (pos != std::string::npos) + return false; + + stem = filename.substr(0, pos); + return true; +} + +bool CheckPattern(const std::string& pattern, const std::string& str) { + if (pattern.empty()) + return false; + if (pattern.front() == '^' && std::regex_match(str, std::regex(pattern))) + return true; + return util::EqualStrings(pattern, str); +} + +bool TrimLeft(std::string& str, const char* chars) { + if (str.empty()) + return false; + + const auto found = str.find_first_not_of(chars); + + if (found == 0) + return false; + + if (found == std::string::npos) + str.clear(); + else + str.erase(0, found); + + return true; +} + +bool TrimRight(std::string& str, const char* chars) { + if (str.empty()) + return false; + + const auto found = str.find_last_not_of(chars); + + if (found == str.size() - 1) + return false; + + if (found == std::string::npos) + str.clear(); + else + str.resize(found + 1); + + return true; +} + +} // namespace animone::internal::util