forked from google/diff-match-patch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff_match_patch_util.cpp
More file actions
205 lines (154 loc) · 4.7 KB
/
Copy pathdiff_match_patch_util.cpp
File metadata and controls
205 lines (154 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <algorithm>
#include <limits>
#include <deque>
#include <regex>
#include <sstream>
#include <iostream>
#include <cstdarg>
#include <cstring>
#include <iomanip>
#include <locale>
#include <codecvt>
#include <list>
#include "diff_match_patch_util.h"
namespace std {
void replace_all(std::wstring& str, const std::wstring& from, const std::wstring& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::wstring::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
bool ends_with(const std::wstring & a, const std::wstring& b) {
if (b.length() == 0) return true;
if (a.length() == 0) return false;
if (a.length() < b.length()) return false;
auto r = a.rfind(b);
if (r == std::wstring::npos) return false;
return r + b.length() == a.length();
}
bool starts_with(const std::wstring & a, const std::wstring& b) {
return a.find(b) == 0;
}
std::wstring_list split(const std::wstring & s, wchar_t delimiter, bool skip_empty) {
std::wstring_list tokens;
std::wistringstream f(s);
std::wstring ss;
while(std::getline(f, ss, delimiter)) {
if (skip_empty && ss.length() == 0)
continue;
tokens.push_back(ss);
}
return tokens;
}
std::wstring format(const wchar_t * f, ...)
{
va_list args;
size_t len = 4096;
int result = -1;
std::vector<wchar_t> vec{};
do {
vec.resize(len);
va_start (args, f);
result = std::vswprintf(&vec[0], len - 1, f, args);
if (result > 0) {
vec[result] = 0;
} else {
len *= 2;
}
va_end (args);
} while(result < 0);
return std::wstring{&vec[0]};
}
void debug_print(const wchar_t * f, ...)
{
va_list args;
size_t len = 4096;
int result = -1;
std::vector<wchar_t> vec{};
do {
vec.resize(len);
va_start (args, f);
result = std::vswprintf(&vec[0], len - 1, f, args);
if (result > 0) {
vec[result] = 0;
} else {
len *= 2;
}
va_end (args);
} while(result < 0);
std::wcerr << std::wstring{&vec[0]} << std::endl;
}
static
std::wstring_convert<std::codecvt_utf8<wchar_t
#if defined(_WIN32) && defined(__GNUC__)
, 0x10ffff, std::little_endian
#endif
>, wchar_t> wcharconv;
std::string url_encode(const std::string &value, const std::string & exclude) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
std::string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || exclude.find(c) != std::string::npos) {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
}
return escaped.str();
}
std::wstring url_encode(const std::wstring &value, const std::string & exclude) {
std::string utf8_str = wcharconv.to_bytes(value);
auto result = url_encode(utf8_str, exclude);
return wcharconv.from_bytes(result);
}
static
int get_val(std::string::value_type c) {
if (c >= '0' && c <='9')
return c - '0';
if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
return -1;
}
std::string url_decode(const std::string &value) {
std::ostringstream escaped;
for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
std::string::value_type c = (*i);
if (c == '%') {
auto v1 = get_val(*(i + 1));
auto v2 = get_val(*(i + 2));
if (v1 < 0 || v2 < 0)
throw std::string("invalid value:") + value;
escaped << (unsigned char)((v1 << 4) + v2);
i+=2;
continue;
}
escaped << c;
}
return escaped.str();
}
std::wstring url_decode(const std::wstring &value) {
std::string utf8_str = wcharconv.to_bytes(value);
auto result = url_decode(utf8_str);
return wcharconv.from_bytes(result);
}
std::wstring var_to_string(const dmp_variant & var) {
if (std::holds_alternative<std::wstring>(var)) {
return std::get<std::wstring>(var);
}
if (std::holds_alternative<std::wstring_list>(var)) {
return join(std::get<std::wstring_list>(var), L",");
}
return L"";
}
}