5#include <unordered_map>
8 #include <boost/locale.hpp>
31template <
typename... Args>
inline bool is_or(
const std::string& value, Args... args) {
32 std::vector<std::string> values = {args...};
33 return std::find(values.begin(), values.end(), value) != values.end();
48 inline std::wstring to_wstring(
const std::string& u8) {
49 return boost::locale::conv::to_utf<wchar_t>(u8,
"UTF-8");
60inline void replace_r(std::string& str,
const std::string from,
const std::string to) {
61 if (from.empty())
return;
63 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
64 str.replace(start_pos, from.size(), to);
65 start_pos += to.size();
76inline std::string
replace(std::string str,
const std::string from,
const std::string to) {
88inline std::vector<std::string>
find(
const std::string& input, std::string start, std::string end) {
89 std::vector<std::string> output;
93 size_t f = input.find(start,startpos);
94 size_t b = input.find(end, f);
95 if (f==std::string::npos || b==std::string::npos)
break;
97 output.emplace_back(input.substr(f + start.size(), b - f - start.size()));
108inline int toi(
const std::string& str) {
109 std::string filtered;
110 std::copy_if(str.begin(), str.end(), std::back_inserter(filtered),
111 [](
unsigned char c) { return std::isdigit(c); });
113 return filtered.empty() ? 0 : std::stoi(filtered);
121inline std::vector<int>
toi(
const std::vector<std::string>& input) {
122 std::vector<int> result;
123 for (std::string e: input) {
124 result.emplace_back(
st::toi(e));
135inline std::vector<std::string>
split(
const std::string& str,
const std::string& delimiter) {
136 std::vector<std::string> tokens;
137 size_t start = 0, end;
139 while ((end = str.find(delimiter, start)) != std::string::npos) {
140 tokens.emplace_back(str.substr(start, end - start));
141 start = end + delimiter.size();
143 tokens.emplace_back(str.substr(start));
153inline std::vector<int>
spliti(
const std::string& str,
const std::string& delimiter) {
154 std::vector<std::string> output =
split(str,delimiter);
155 std::vector<int> result;
156 for (std::string e: output) {
157 result.emplace_back(
st::toi(e));
163typedef std::unordered_map<std::string,std::vector<std::string>>
splits;
165typedef std::unordered_map<std::string,std::vector<int>>
splitsi;
173inline splits split(
const std::string& input,
const std::vector<std::string>& targets) {
175 std::string current = input;
176 std::string current_segment;
177 std::string current_delimiter;
179 auto find_next_target = [&](
const std::string& str,
size_t start_pos) {
180 size_t min_pos = std::string::npos;
181 std::string found_target;
182 for (
const auto& target : targets) {
183 size_t pos = str.find(target, start_pos);
184 if (pos != std::string::npos && (min_pos == std::string::npos || pos < min_pos)) {
186 found_target = target;
189 return std::make_pair(min_pos, found_target);
193 while (pos < current.size()) {
194 auto [next_pos, delimiter] = find_next_target(current, pos);
195 if (next_pos == std::string::npos)
break;
196 current_segment = current.substr(pos, next_pos - pos + delimiter.size());
197 current_delimiter = delimiter;
199 result[current_delimiter].push_back(current_segment);
201 pos = next_pos + delimiter.size();
213inline splitsi spliti(
const std::string& input,
const std::vector<std::string>& targets) {
216 for (
const auto& [key, segments] : output) {
217 for (
const auto& segment : segments) {
218 result[key].push_back(
st::toi(segment));
230inline std::vector<std::string>
charV(
const int i_argc,
const char*
const i_argv[]) {
231 std::vector<std::string> result;
232 for (
int i = 0; i < i_argc; ++i) {
233 result.emplace_back(i_argv[i]);
244inline std::vector<int>
charVi(
const int i_argc,
const char* i_argv[]) {
254inline size_t size(
const std::string& input) {
256 size_t char_size=0, input_size=0, pos;
257 for (pos = 0; pos < input.size(); pos += char_size) {
260 if (lead < 0x80) char_size=1;
else if (lead < 0xE0) char_size=2;
else if (lead < 0xF0) char_size=3;
else char_size=4;
size_t size(const std::string &input)
UTF-8 文字列のマルチバイト文字数(文字単位の長さ)を返す
std::vector< std::string > find(const std::string &input, std::string start, std::string end)
文字列中から start〜end に囲まれた部分文字列をすべて取り出す
std::unordered_map< std::string, std::vector< std::string > > splits
split() の複数デリミタ対応版の戻り値型: デリミタ → 部分文字列リスト
std::vector< int > charVi(const int i_argc, const char *i_argv[])
char 配列 (argc/argv 形式) を整数ベクタに変換する
std::vector< int > spliti(const std::string &str, const std::string &delimiter)
文字列を単一のデリミタで分割し、各要素を整数に変換する
std::vector< std::string > charV(const int i_argc, const char *const i_argv[])
char 配列 (argc/argv 形式) を文字列ベクタに変換する
std::string replace(std::string str, const std::string from, const std::string to)
文字列中のすべての from を to に置換した新しい文字列を返す(値返し版)
void replace_r(std::string &str, const std::string from, const std::string to)
文字列中のすべての from を to に置換する(参照渡し、インプレース版)
std::vector< std::string > split(const std::string &str, const std::string &delimiter)
文字列を単一のデリミタで分割する(文字列ベクタを返す)
int toi(const std::string &str)
文字列中の数字のみを抽出して整数に変換する
std::unordered_map< std::string, std::vector< int > > splitsi
spliti() の複数デリミタ対応版の戻り値型: デリミタ → 整数リスト
bool is_or(const std::string &value, Args... args)
文字列が複数の候補値のいずれかと等しいか判定する