CSEngine
Loading...
Searching...
No Matches
MoreString.h
1#pragma once
2
3#include <string>
4#include <sstream>
5#include <algorithm>
6#include <utility>
7#include <vector>
8#include <random>
9
10
11namespace CSE {
12
13 static std::string ReplaceAll(std::string& str, const std::string& from, const std::string& to) {
14 size_t start_pos = 0;
15 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
16 str.replace(start_pos, from.length(), to);
17 }
18 return str;
19 }
20
21 static std::string
22 ReplaceFunction(std::string& str, const std::string& from, const std::string& from2, const std::string& to,
23 const std::string& to2) {
24 size_t start_pos = 0;
25 size_t start_pos2 = 0;
26 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
27 str.replace(start_pos, from.length(), to);
28 start_pos += to.length();
29
30 if ((start_pos2 = str.find(from2, start_pos)) != std::string::npos) {
31 if (str.find(from, start_pos) < start_pos2) continue;
32
33 str.replace(start_pos2, from2.length(), to2);
34 start_pos = start_pos2 + to2.length();
35
36 }
37 }
38 return str;
39 }
40
41 static std::string trim(std::string str) {
42 std::string r = str.erase(str.find_last_not_of(" \n\t\r") + 1);
43 return r.erase(0, r.find_first_not_of(" \n\t\r"));
44 }
45
46 static std::vector<std::string> split(const std::string& s, char seperator) {
47 std::vector<std::string> output;
48
49 std::string::size_type prev_pos = 0;
50 std::string::size_type pos = s.find(seperator);
51
52 // 문자열을 순회하면서 새로운 단어를 찾음
53 while (pos != std::string::npos) {
54 // 새로운 단어를 추가
55 output.push_back(s.substr(prev_pos, pos - prev_pos));
56
57 // 다음 단어를 찾기 위해 위치를 업데이트
58 prev_pos = pos + 1;
59 pos = s.find(seperator, prev_pos);
60 }
61
62 // 마지막 단어를 추가
63 output.push_back(s.substr(prev_pos, pos - prev_pos));
64
65 return output;
66 }
67
68
69 static void make_upper(std::string& str) {
70 std::transform(str.begin(), str.end(), str.begin(), toupper);
71 }
72
73 static void make_lower(std::string& str) {
74 std::transform(str.begin(), str.end(), str.begin(), tolower);
75 }
76
77 static std::string make_lower_copy(std::string _str) {
78 std::string str(std::move(_str));
79 std::transform(str.begin(), str.end(), str.begin(), tolower);
80 return str;
81 }
82
83 template<typename T>
84 std::string appandAll(std::stringstream& sstream, T param) {
85 sstream << param;
86 auto result = sstream.str();
87 sstream.str(std::string());
88 sstream.clear();
89
90 return result;
91 }
92
93 template<typename T0, typename ... Tn>
94 std::string appandAll(std::stringstream& sstream, T0 param0, Tn... paramN) {
95 sstream << param0;
96 return appandAll(sstream, paramN...);
97 }
98
99 static std::string ConvertSpaceStr(const std::string& str, bool SpaceNotChange = false) {
100 std::string converted_str = str;
101
102 if (SpaceNotChange) {
103 // $nbsp: 문자열을 공백 문자로 대체
104 size_t pos = 0;
105 while ((pos = converted_str.find("$nbsp:", pos)) != std::string::npos) {
106 converted_str.replace(pos, 6, " ");
107 pos += 1;
108 }
109 } else {
110 // 공백 문자를 $nbsp: 문자열로 대체
111 size_t pos = 0;
112 while ((pos = converted_str.find(' ', pos)) != std::string::npos) {
113 converted_str.replace(pos, 1, "$nbsp:");
114 pos += 6;
115 }
116 }
117
118 return converted_str;
119 }
120
121 static std::string GetRandomHash(int size) {
122 std::random_device rd;
123 std::mt19937 gen(rd());
124 std::uniform_int_distribution<> dis(0, 61); // 0~9, A~Z, a~z까지 숫자 생성
125
126 std::string result;
127 result.reserve(size);
128
129 for (int i = 0; i < size; i++) {
130 int num = dis(gen);
131 if (num < 10) {
132 // 0~9인 경우, 숫자 문자로 추가
133 result += std::to_string(num);
134 } else if (num < 36) {
135 // 10~35인 경우, A~Z인 문자로 추가
136 result += static_cast<char>(num + 'A' - 10);
137 } else {
138 // 36~61인 경우, a~z인 문자로 추가
139 result += static_cast<char>(num + 'a' - 36);
140 }
141 }
142
143 return result;
144 }
145}