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
160 lines (136 loc) · 3.85 KB
/
string_utils.cpp
File metadata and controls
160 lines (136 loc) · 3.85 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
#include <cctype>
#include <regex>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#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 unescape_string(Allocator &/*al*/, std::string s) {
std::string x;
for (size_t idx=0; idx < s.size(); idx++) {
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] == '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 {
x += s[idx];
}
}
return x;
}
} // namespace LCompilers