-
Notifications
You must be signed in to change notification settings - Fork 8
/
istr.h
227 lines (207 loc) · 4.91 KB
/
istr.h
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
225
226
227
/*
* Copyright 2020-2023 Fabian Groffen <grobian@gentoo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License in the file COPYING for more details.
*/
#ifndef ISTR_H
#define ISTR_H 1
#include <vector>
#include <memory>
#include <cstring>
/* crude temp hack until we properly use wchar, glibc isspace crashes on
* too large values */
#define isspace(X) (((int)(X)) > 0 && ((int)(X)) < 256 && isspace((int)(X)))
class istr {
public:
istr():
elems({})
{
}
istr(const char *p):
elems({})
{
for (; *p != '\0'; p++)
elems.push_back(((int)*p) & 0xFF);
}
istr(const string &p):
elems({})
{
size_t i;
for (i = 0; i < (size_t)p.length(); i++)
elems.push_back(((int)p.at(i)) & 0xFF);
}
~istr()
{
if (c_str_buf != nullptr)
delete[] c_str_buf;
}
bool empty(void)
{
return elems.empty();
}
int get(size_t pos)
{
return elems.size() < pos ? -1 : elems[pos];
}
string::size_type length(void) const
{
return (string::size_type)elems.size();
}
istr &erase(size_t pos = 0, size_t len = string::npos)
{
if (len == string::npos)
len = elems.size();
/* we assume pos within range here (should throw out_of_range) */
if (pos + len >= elems.size())
len = elems.size() - pos;
elems.erase(elems.begin() + pos, elems.begin() + pos + len);
return *this;
}
istr &replace(size_t pos, size_t len, const char *s)
{
erase(pos, len);
for (int i = 0; *s != '\0'; s++, i++)
elems.insert(elems.begin() + pos + i, ((int)*s) & 0xFF);
return *this;
}
istr &replace(size_t pos, size_t len, const int i)
{
erase(pos, len);
elems.insert(elems.begin() + pos, i);
return *this;
}
istr slice(size_t pos = 0, size_t len = string::npos)
{
istr ret = istr();
if (len == string::npos)
len = elems.size();
/* we assume pos within range here (should throw out_of_range) */
if (pos + len >= elems.size())
len = elems.size() - pos;
for (len += pos; pos < len; pos++)
ret += elems[pos];
return ret;
}
int compare(size_t pos, size_t len, const char *s) const
{
int ret;
int elm;
for (size_t i = 0; i < len; i++) {
elm = (pos + i) < elems.size() ? elems[pos + i] : 0;
if ((ret = s[i] - elm) != 0)
break;
}
return ret;
}
int icompare(size_t pos, size_t len, const char *s) const
{
int ret;
int elm;
/* this is not handling utf-8 conversions properly but for
* the usage this is not a problem */
for (size_t i = 0; i < len; i++) {
elm = (pos + i) < elems.size() ? elems[pos + i] : 0;
if ((ret = toupper(s[i]) - toupper(elm)) != 0)
break;
}
return ret;
}
bool iequals(const char *s) const
{
size_t len;
if (s == NULL)
return false;
len = strlen(s);
return (len == elems.size() && this->icompare(0, len, s) == 0);
}
istr &operator+=(const int inp)
{
elems.push_back(inp);
return *this;
}
istr &operator+=(const char *p)
{
for (; *p != '\0'; p++)
elems.push_back(((int)*p) & 0xFF);
return *this;
}
istr &operator+=(const string &p)
{
for (const char c : p)
elems.push_back(((int)c) & 0xFF);
return *this;
}
istr &operator<<=(const int inp)
{
elems.push_back(inp);
return *this;
}
istr &operator<<=(const char inp)
{
return *this <<= (((int)inp) & 0xFF);
}
istr &operator<<=(const char *inp)
{
return *this += inp;
}
istr &operator>>=(const char inp)
{
elems.insert(elems.begin(), ((int)inp) & 0xFF);
return *this;
}
istr &operator>>=(const char *inp)
{
for (int i = 0; *inp != '\0'; i++, inp++)
elems.insert(elems.begin() + i, ((int)*inp) & 0xFF);
return *this;
}
int operator[](const int pos) const
{
return elems[pos];
}
bool operator==(const char *inp)
{
size_t len;
if (inp == NULL)
return false;
len = strlen(inp);
return (len == elems.size() && this->compare(0, len, inp) == 0);
}
bool operator!=(const char *inp)
{
return !(*this == inp);
}
const char *c_str(void) const
{
std::string s = std::string("");
for (int c : elems) {
s += c & 0xFF;
if ((c >> 7) & 1) {
unsigned int d = c;
unsigned char point = 1;
while ((c >> (7 - point++)) & 1) {
d >>= 8;
s += d & 0xFF;
};
}
}
if (c_str_buf != nullptr)
delete[] c_str_buf;
c_str_buf = new char[s.size() + 1];
memcpy(c_str_buf, s.data(), s.size());
c_str_buf[s.size()] = '\0';
return c_str_buf;
}
private:
std::vector<int> elems;
mutable char *c_str_buf = nullptr;
};
#endif