|
| 1 | +// Vita3K emulator project |
| 2 | +// Copyright (C) 2025 Vita3K team |
| 3 | +// |
| 4 | +// This program is free software; you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation; either version 2 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License along |
| 15 | +// with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <codecvt> |
| 22 | +#include <cstdint> |
| 23 | +#include <locale> |
| 24 | +#include <sstream> |
| 25 | +#include <string> |
| 26 | +#include <util/log.h> |
| 27 | +#include <util/string_utils.h> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +namespace string_utils |
| 31 | +{ |
| 32 | + |
| 33 | +std::vector<std::string> split_string(const std::string &str, char delimiter) |
| 34 | +{ |
| 35 | + std::istringstream str_stream(str); |
| 36 | + std::string segment; |
| 37 | + std::vector<std::string> seglist; |
| 38 | + |
| 39 | + const size_t num_segments = |
| 40 | + std::count_if(str.begin(), str.end(), [&](char c) { return c == delimiter; }) + |
| 41 | + (str.empty() ? 1 : 0); |
| 42 | + |
| 43 | + seglist.reserve(num_segments); |
| 44 | + |
| 45 | + while (std::getline(str_stream, segment, delimiter)) { |
| 46 | + seglist.push_back(segment); |
| 47 | + } |
| 48 | + return seglist; |
| 49 | +} |
| 50 | + |
| 51 | +std::wstring utf_to_wide(const std::string &str) |
| 52 | +{ |
| 53 | + std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; |
| 54 | + return myconv.from_bytes(str); |
| 55 | +} |
| 56 | + |
| 57 | +std::string wide_to_utf(const std::wstring &str) |
| 58 | +{ |
| 59 | + std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; |
| 60 | + return myconv.to_bytes(str); |
| 61 | +} |
| 62 | + |
| 63 | +std::string remove_special_chars(std::string str) |
| 64 | +{ |
| 65 | + for (char &c : str) { |
| 66 | + switch (c) { |
| 67 | + case '/': |
| 68 | + case '\\': |
| 69 | + case ':': |
| 70 | + case '?': |
| 71 | + case '"': |
| 72 | + case '<': |
| 73 | + case '>': |
| 74 | + case '|': |
| 75 | + case '*': |
| 76 | + c = '_'; |
| 77 | + break; |
| 78 | + default: |
| 79 | + continue; |
| 80 | + } |
| 81 | + } |
| 82 | + return str; |
| 83 | +} |
| 84 | + |
| 85 | +// Based on: https://stackoverflow.com/a/23135441 |
| 86 | +// Search and replace "in" with "out" in the given string |
| 87 | +void replace(std::string &str, const std::string &in, const std::string &out) |
| 88 | +{ |
| 89 | + size_t pos = 0; |
| 90 | + while ((pos = str.find(in, pos)) != std::string::npos) { |
| 91 | + str.replace(pos, in.length(), out); |
| 92 | + pos += out.length(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +std::vector<uint8_t> string_to_byte_array(const std::string &string) |
| 97 | +{ |
| 98 | + std::vector<uint8_t> hex_bytes; |
| 99 | + hex_bytes.reserve(string.length() / 2); |
| 100 | + |
| 101 | + if (string.length() % 2 != 0) |
| 102 | + LOG_WARN("Hex string length ({}) is not even", string.length()); |
| 103 | + |
| 104 | + for (size_t i = 0; i < string.length(); i += 2) { |
| 105 | + std::string byte = string.substr(i, 2); |
| 106 | + hex_bytes.push_back(static_cast<uint8_t>(std::strtoul(byte.c_str(), nullptr, 16))); |
| 107 | + } |
| 108 | + return hex_bytes; |
| 109 | +} |
| 110 | + |
| 111 | +#ifdef _MSC_VER |
| 112 | +std::string utf16_to_utf8(const std::u16string &str) |
| 113 | +{ |
| 114 | + std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> myconv; |
| 115 | + auto p = reinterpret_cast<const int16_t *>(str.data()); |
| 116 | + return myconv.to_bytes(p, p + str.size()); |
| 117 | +} |
| 118 | + |
| 119 | +std::u16string utf8_to_utf16(const std::string &str) |
| 120 | +{ |
| 121 | + static_assert(sizeof(std::wstring::value_type) == sizeof(std::u16string::value_type), |
| 122 | + "std::wstring and std::u16string are expected to have the same character size"); |
| 123 | + |
| 124 | + std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> myconv; |
| 125 | + const char *p = str.data(); |
| 126 | + auto a = myconv.from_bytes(p, p + str.size()); |
| 127 | + return std::u16string(a.begin(), a.end()); |
| 128 | +} |
| 129 | +#else |
| 130 | +std::string utf16_to_utf8(const std::u16string &str) |
| 131 | +{ |
| 132 | + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> myconv; |
| 133 | + return myconv.to_bytes(str); |
| 134 | +} |
| 135 | + |
| 136 | +std::u16string utf8_to_utf16(const std::string &str) |
| 137 | +{ |
| 138 | + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> myconv; |
| 139 | + return myconv.from_bytes(str); |
| 140 | +} |
| 141 | +#endif |
| 142 | + |
| 143 | +std::string toupper(const std::string &s) |
| 144 | +{ |
| 145 | + std::string r = s; |
| 146 | + std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::toupper(c); }); |
| 147 | + return r; |
| 148 | +} |
| 149 | + |
| 150 | +std::string tolower(const std::string &s) |
| 151 | +{ |
| 152 | + std::string r = s; |
| 153 | + std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 154 | + return r; |
| 155 | +} |
| 156 | + |
| 157 | +int stoi_def(const std::string &str, int default_value, const char *name) |
| 158 | +{ |
| 159 | + try { |
| 160 | + return std::stoi(str); |
| 161 | + } catch (std::invalid_argument &_) { |
| 162 | + LOG_ERROR("Invalid {}: \"{}\"", name, str); |
| 163 | + } catch (std::out_of_range &_) { |
| 164 | + LOG_ERROR("Out of range {}: \"{}\"", name, str); |
| 165 | + } |
| 166 | + return default_value; |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace string_utils |
0 commit comments