fix warnings
This commit is contained in:
parent
e959a9e795
commit
f3ea2ccc21
@ -22,15 +22,17 @@
|
||||
#ifndef INCLUDE_UTILS_STRING_UTILS_HPP_
|
||||
#define INCLUDE_UTILS_STRING_UTILS_HPP_
|
||||
|
||||
REPERTORY_IGNORE_WARNINGS_ENABLE()
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
REPERTORY_IGNORE_WARNINGS_DISABLE()
|
||||
|
||||
namespace repertory::utils::string {
|
||||
// Prototypes
|
||||
constexpr auto begins_with(std::string_view str, std::string_view val) -> bool {
|
||||
return (str.find(val) == 0u);
|
||||
return (str.find(val) == 0U);
|
||||
}
|
||||
|
||||
constexpr auto contains(std::string_view str, std::string_view search) -> bool {
|
||||
@ -48,14 +50,15 @@ constexpr auto contains(std::string_view str, std::string_view search) -> bool {
|
||||
|
||||
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
|
||||
|
||||
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool;
|
||||
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str)
|
||||
-> bool;
|
||||
|
||||
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
|
||||
-> std::string;
|
||||
|
||||
auto left_trim(std::string &s) -> std::string &;
|
||||
auto left_trim(std::string &str) -> std::string &;
|
||||
|
||||
auto left_trim(std::string &s, const char &c) -> std::string &;
|
||||
auto left_trim(std::string &str, const char &trim_ch) -> std::string &;
|
||||
|
||||
auto replace(std::string &src, const char &character, const char &with)
|
||||
-> std::string &;
|
||||
@ -70,9 +73,9 @@ auto replace(std::string &src, const std::string &find, const std::string &with,
|
||||
const std::string &with, size_t start_pos = 0)
|
||||
-> std::string;
|
||||
|
||||
auto right_trim(std::string &s) -> std::string &;
|
||||
auto right_trim(std::string &str) -> std::string &;
|
||||
|
||||
auto right_trim(std::string &s, const char &c) -> std::string &;
|
||||
auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
|
||||
|
||||
[[nodiscard]] auto split(const std::string &str, const char &delim,
|
||||
bool should_trim = true) -> std::vector<std::string>;
|
||||
@ -108,11 +111,12 @@ auto right_trim(std::string &s, const char &c) -> std::string &;
|
||||
|
||||
auto trim(std::string &str) -> std::string &;
|
||||
|
||||
auto trim(std::string &str, const char &c) -> std::string &;
|
||||
auto trim(std::string &str, const char &trim_ch) -> std::string &;
|
||||
|
||||
[[nodiscard]] auto trim_copy(std::string str) -> std::string;
|
||||
|
||||
[[nodiscard]] auto trim_copy(std::string str, const char &c) -> std::string;
|
||||
[[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
|
||||
-> std::string;
|
||||
} // namespace repertory::utils::string
|
||||
|
||||
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_
|
||||
|
@ -30,13 +30,15 @@ namespace repertory::utils::string {
|
||||
return std::equal(val.rbegin(), val.rend(), str.rbegin());
|
||||
}
|
||||
|
||||
auto from_bool(bool val) -> std::string { return std::to_string(val); }
|
||||
auto from_bool(bool val) -> std::string {
|
||||
return std::to_string(static_cast<int>(val));
|
||||
}
|
||||
|
||||
auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string {
|
||||
std::stringstream ss;
|
||||
boost::archive::text_oarchive archive(ss);
|
||||
std::stringstream stream;
|
||||
boost::archive::text_oarchive archive(stream);
|
||||
archive << bitset;
|
||||
return ss.str();
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
auto from_utf8(const std::string &str) -> std::wstring {
|
||||
@ -46,27 +48,28 @@ auto from_utf8(const std::string &str) -> std::wstring {
|
||||
.from_bytes(str);
|
||||
}
|
||||
|
||||
/* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool {
|
||||
if ((s.length() > 1u) && (s[0u] == '+' || s[0u] == '-')) {
|
||||
s = s.substr(1u);
|
||||
/* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool {
|
||||
if ((str.length() > 1U) && (str[0U] == '+' || str[0U] == '-')) {
|
||||
str = str.substr(1U);
|
||||
}
|
||||
|
||||
if (s.empty()) {
|
||||
if (str.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto has_decimal = false;
|
||||
return std::find_if(
|
||||
s.begin(), s.end(),
|
||||
[&has_decimal](const std::string_view::value_type &c) -> bool {
|
||||
if (has_decimal && c == '.') {
|
||||
return true;
|
||||
}
|
||||
if ((has_decimal = has_decimal || c == '.')) {
|
||||
return false;
|
||||
}
|
||||
return not std::isdigit(c);
|
||||
}) == s.end();
|
||||
return std::find_if(str.begin(), str.end(),
|
||||
[&has_decimal](
|
||||
const std::string_view::value_type &cur_ch) -> bool {
|
||||
if (has_decimal && cur_ch == '.') {
|
||||
return true;
|
||||
}
|
||||
has_decimal = has_decimal || cur_ch == '.';
|
||||
if (has_decimal) {
|
||||
return false;
|
||||
}
|
||||
return std::isdigit(cur_ch) == 0;
|
||||
}) == str.end();
|
||||
}
|
||||
|
||||
auto join(const std::vector<std::string> &arr, const char &delim)
|
||||
@ -76,15 +79,17 @@ auto join(const std::vector<std::string> &arr, const char &delim)
|
||||
}
|
||||
|
||||
return std::accumulate(
|
||||
std::next(arr.begin()), arr.end(), arr[0u],
|
||||
[&delim](auto s, const auto &v) { return s + delim + v; });
|
||||
std::next(arr.begin()), arr.end(), arr[0U],
|
||||
[&delim](auto str, const auto &cur) { return str + delim + cur; });
|
||||
}
|
||||
|
||||
auto left_trim(std::string &s) -> std::string & { return left_trim(s, ' '); }
|
||||
auto left_trim(std::string &str) -> std::string & {
|
||||
return left_trim(str, ' ');
|
||||
}
|
||||
|
||||
auto left_trim(std::string &s, const char &c) -> std::string & {
|
||||
s.erase(0, s.find_first_not_of(c));
|
||||
return s;
|
||||
auto left_trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
str.erase(0, str.find_first_not_of(trim_ch));
|
||||
return str;
|
||||
}
|
||||
|
||||
auto replace(std::string &src, const char &character, const char &with)
|
||||
@ -115,47 +120,49 @@ auto replace_copy(std::string src, const std::string &find,
|
||||
return replace(src, find, with, start_pos);
|
||||
}
|
||||
|
||||
auto right_trim(std::string &s) -> std::string & { return right_trim(s, ' '); }
|
||||
auto right_trim(std::string &str) -> std::string & {
|
||||
return right_trim(str, ' ');
|
||||
}
|
||||
|
||||
auto right_trim(std::string &s, const char &c) -> std::string & {
|
||||
s.erase(s.find_last_not_of(c) + 1);
|
||||
return s;
|
||||
auto right_trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
str.erase(str.find_last_not_of(trim_ch) + 1);
|
||||
return str;
|
||||
}
|
||||
|
||||
auto split(const std::string &str, const char &delim, bool should_trim)
|
||||
-> std::vector<std::string> {
|
||||
std::vector<std::string> ret;
|
||||
std::stringstream ss(str);
|
||||
std::stringstream stream(str);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim)) {
|
||||
while (std::getline(stream, item, delim)) {
|
||||
ret.push_back(should_trim ? trim(item) : item);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto to_bool(std::string val) -> bool {
|
||||
auto b = false;
|
||||
auto ret = false;
|
||||
|
||||
trim(val);
|
||||
if (is_numeric(val)) {
|
||||
if (contains(val, ".")) {
|
||||
b = (to_double(val) != 0.0);
|
||||
ret = (to_double(val) != 0.0);
|
||||
} else {
|
||||
std::istringstream(val) >> b;
|
||||
std::istringstream(val) >> ret;
|
||||
}
|
||||
} else {
|
||||
std::istringstream(to_lower(val)) >> std::boolalpha >> b;
|
||||
std::istringstream(to_lower(val)) >> std::boolalpha >> ret;
|
||||
}
|
||||
|
||||
return b;
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto to_double(const std::string &str) -> double { return std::stod(str); }
|
||||
|
||||
auto to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<> {
|
||||
std::stringstream ss(val);
|
||||
std::stringstream stream(val);
|
||||
boost::dynamic_bitset<> bitset;
|
||||
boost::archive::text_iarchive archive(ss);
|
||||
boost::archive::text_iarchive archive(stream);
|
||||
archive >> bitset;
|
||||
return bitset;
|
||||
}
|
||||
@ -209,15 +216,15 @@ auto trim(std::string &str) -> std::string & {
|
||||
return right_trim(left_trim(str));
|
||||
}
|
||||
|
||||
auto trim(std::string &str, const char &c) -> std::string & {
|
||||
return right_trim(left_trim(str, c), c);
|
||||
auto trim(std::string &str, const char &trim_ch) -> std::string & {
|
||||
return right_trim(left_trim(str, trim_ch), trim_ch);
|
||||
}
|
||||
|
||||
auto trim_copy(std::string str) -> std::string {
|
||||
return right_trim(left_trim(str));
|
||||
}
|
||||
|
||||
auto trim_copy(std::string str, const char &c) -> std::string {
|
||||
return right_trim(left_trim(str, c), c);
|
||||
auto trim_copy(std::string str, const char &trim_ch) -> std::string {
|
||||
return right_trim(left_trim(str, trim_ch), trim_ch);
|
||||
}
|
||||
} // namespace repertory::utils::string
|
||||
|
Loading…
x
Reference in New Issue
Block a user