-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtil.cpp
More file actions
148 lines (119 loc) · 4.51 KB
/
Copy pathUtil.cpp
File metadata and controls
148 lines (119 loc) · 4.51 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
#include "Util.h"
#include <sstream>
#include <iostream>
#include <codecvt>
using namespace std;
namespace tns {
string Util::JniClassPathToCanonicalName(const string &jniClassPath) {
std::string canonicalName;
const char prefix = jniClassPath[0];
std::string rest;
int lastIndex;
switch (prefix) {
case 'L':
canonicalName = jniClassPath.substr(1, jniClassPath.size() - 2);
std::replace(canonicalName.begin(), canonicalName.end(), '/', '.');
std::replace(canonicalName.begin(), canonicalName.end(), '$', '.');
break;
case '[':
canonicalName = jniClassPath;
lastIndex = canonicalName.find_last_of('[');
rest = canonicalName.substr(lastIndex + 1);
canonicalName = canonicalName.substr(0, lastIndex + 1);
canonicalName.append(JniClassPathToCanonicalName(rest));
break;
default:
// TODO:
canonicalName = jniClassPath;
break;
}
return canonicalName;
}
void Util::SplitString(const string &str, const string &delimiters, vector <string> &tokens) {
string::size_type delimPos = 0, tokenPos = 0, pos = 0;
if (str.length() < 1) {
return;
}
while (true) {
delimPos = str.find_first_of(delimiters, pos);
tokenPos = str.find_first_not_of(delimiters, pos);
if (string::npos != delimPos) {
if (string::npos != tokenPos) {
if (tokenPos < delimPos) {
tokens.push_back(str.substr(pos, delimPos - pos));
} else {
tokens.emplace_back("");
}
} else {
tokens.emplace_back("");
}
pos = delimPos + 1;
} else {
if (string::npos != tokenPos) {
tokens.push_back(str.substr(pos));
} else {
tokens.emplace_back("");
}
break;
}
}
}
bool Util::EndsWith(const string &str, const string &suffix) {
bool res = false;
if (str.size() > suffix.size()) {
res = equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
return res;
}
bool Util::Contains(const string &str, const string &sequence) {
return str.find(sequence) != string::npos;
}
string Util::ConvertFromJniToCanonicalName(const string &name) {
string converted = name;
replace(converted.begin(), converted.end(), '/', '.');
return converted;
}
string Util::ConvertFromCanonicalToJniName(const string &name) {
string converted = name;
replace(converted.begin(), converted.end(), '.', '/');
return converted;
}
string Util::ReplaceAll(string &str, const string &from, const string &to) {
if (from.empty()) {
return str;
}
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
u16string Util::ConvertFromUtf8ToUtf16(const string &str) {
auto utf16String =
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>().from_bytes(str);
return utf16String;
}
void Util::JoinString(const std::vector<std::string> &list, const std::string &delimiter,
std::string &out) {
out.clear();
stringstream ss;
for (auto it = list.begin(); it != list.end(); ++it) {
ss << *it;
if (it != list.end() - 1) {
ss << delimiter;
}
}
out = ss.str();
}
std::vector<uint16_t> Util::ToVector(const std::string &value) {
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// FIXME: std::codecvt_utf8_utf16 is deprecated
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
std::u16string value16 = convert.from_bytes(value);
const uint16_t *begin = reinterpret_cast<uint16_t const *>(value16.data());
const uint16_t *end = reinterpret_cast<uint16_t const *>(value16.data() + value16.size());
std::vector<uint16_t> vector(begin, end);
return vector;
}
};