forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.cpp
More file actions
224 lines (197 loc) · 5.63 KB
/
Copy pathstring_utils.cpp
File metadata and controls
224 lines (197 loc) · 5.63 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <cctype>
#include <regex>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <libasr/string_utils.h>
#include <libasr/containers.h>
namespace LCompilers {
bool startswith(const std::string &s, const std::string &e)
{
if (s.size() < e.size()) return false;
return s.substr(0, e.size()) == e;
}
bool endswith(const std::string &s, const std::string &e)
{
if (s.size() < e.size()) return false;
return s.substr(s.size()-e.size()) == e;
}
std::string to_lower(const std::string &s) {
std::string res = s;
std::transform(res.begin(), res.end(), res.begin(),
[](unsigned char c){ return std::tolower(c); });
return res;
}
char *s2c(Allocator &al, const std::string &s) {
Str x; x.from_str_view(s);
return x.c_str(al);
}
std::vector<std::string> split(const std::string &s)
{
std::vector<std::string> result;
std::string split_chars = " \n";
size_t old_pos = 0;
size_t new_pos;
while ((new_pos = s.find_first_of(split_chars, old_pos)) != std::string::npos) {
std::string substr = s.substr(old_pos, new_pos-old_pos);
if (substr.size() > 0) result.push_back(substr);
old_pos = new_pos+1;
}
result.push_back(s.substr(old_pos));
return result;
}
std::string join(const std::string j, const std::vector<std::string> &l)
{
std::string result;
for (size_t i=0; i<l.size(); i++) {
result += l[i];
if (i < l.size()-1) result += j;
}
return result;
}
std::vector<std::string> slice(const std::vector<std::string>& v, int start, int end)
{
int oldlen = v.size();
int newlen;
if ((end == -1) || (end >= oldlen)) {
newlen = oldlen-start;
} else {
newlen = end-start;
}
std::vector<std::string> nv(newlen);
for (int i=0; i<newlen; i++) {
nv[i] = v[start+i];
}
return nv;
}
std::string replace(const std::string &s,
const std::string ®ex, const std::string &replace)
{
return std::regex_replace(s, std::regex(regex), replace);
}
std::string read_file(const std::string &filename)
{
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary
| std::ios::ate);
std::ifstream::pos_type filesize = ifs.tellg();
if (filesize < 0) return std::string();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);
return std::string(&bytes[0], filesize);
}
std::string parent_path(const std::string &path) {
int pos = path.size()-1;
while (pos >= 0 && path[pos] != '/') pos--;
if (pos == -1) {
return "";
} else {
return path.substr(0, pos);
}
}
bool is_relative_path(const std::string &path) {
return !startswith(path, "/");
}
std::string join_paths(const std::vector<std::string> &paths) {
std::string p;
std::string delim = "/";
for (auto &path : paths) {
if (path.size() > 0) {
if (p.size() > 0 && !endswith(p, delim)) {
p.append(delim);
}
p.append(path);
}
}
return p;
}
std::string str_escape_c(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; break;
case '\b': o << "\\b"; break;
case '\f': o << "\\f"; break;
case '\n': o << "\\n"; break;
case '\r': o << "\\r"; break;
case '\t': o << "\\t"; break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u"
<< std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c);
} else {
o << *c;
}
}
}
return o.str();
}
char* str_unescape_c(Allocator &al, LCompilers::Str &s) {
std::string x = "";
size_t idx = 0;
for (; idx + 1 < s.size(); idx++) {
if (s[idx] == '\\' && s[idx+1] == '\n') { // continuation character
idx++;
} else if (s[idx] == '\\' && s[idx+1] == 'n') {
x += "\n";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == 't') {
x += "\t";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == 'r') {
x += "\r";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == 'b') {
x += "\b";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == 'v') {
x += "\v";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == '\\') {
x += "\\";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == '"') {
x += '"';
idx++;
} else if (s[idx] == '\\' && s[idx+1] == '\'') {
x += '\'';
idx++;
} else {
x += s[idx];
}
}
if (idx < s.size()) {
x += s[idx];
}
return LCompilers::s2c(al, x);
}
std::string str_escape_fortran_double_quote(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\"\""; break;
}
}
return o.str();
}
char* str_unescape_fortran(Allocator &al, LCompilers::Str &s, char ch) {
std::string x = "";
size_t idx = 0;
for (; idx + 1 < s.size(); idx++) {
if (s[idx] == ch && s[idx + 1] == ch) {
x += s[idx];
idx++;
} else {
x += s[idx];
}
}
if (idx < s.size()) {
x += s[idx];
}
return LCompilers::s2c(al, x);
}
} // namespace LCompilers