-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpy_string.pyc++
173 lines (152 loc) · 4.78 KB
/
tpy_string.pyc++
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
def tpy_str_startswith(TP) ->tp_obj:
print("tpy_str_startswith")
std::string self = TP_OBJ()
std::string val = TP_OBJ()
print(self)
print(val)
print("--------------------")
if self.substr(0, val.size()) == val:
return tp_number(1)
else:
return tp_number(0)
def tpy_str_endswith(TP) ->tp_obj:
std::string self = TP_OBJ()
std::string val = TP_OBJ()
if val.size() > self.size():
return tp_number(0)
elif self.substr(self.size() - val.size(), self.size()) == val:
return tp_number(1)
else:
return tp_number(0)
def tpy_str_reverse(TP) ->tp_obj:
tp_obj self = TP_OBJ()
if self.type.type_id==TP_STRING_TINY:
std::string s = std::string( (const char*)&self.str.val, strlen(self.str.val) )
std::reverse(s.begin(), s.end())
return tp_string_tiny(s.c_str(), s.size())
elif self.type.type_id==TP_STRING_4BIT:
std::string s = self.str4.as_string()
std::reverse(s.begin(), s.end())
return tp_string_4bit(s.c_str(), s.size())
else:
std::string s = std::string(tp_string_getptr(self), tp_string_len(self) )
std::reverse(s.begin(), s.end())
return tp_string_from_stdstring(tp, s)
def tpy_str_join(TP) ->tp_obj:
tp_obj delim = TP_OBJ()
tp_obj val = TP_OBJ()
if delim.type.type_id==TP_STRING_TINY:
std::string de = std::string((const char*)&delim.str.val, strlen(delim.str.val))
std::string ss = ""
int n = len(val)
for i in range(n):
tp_obj item = val.list.val->items[i]
ss += tp_as_string(tp, item, false)
if i<n-1:
ss += de
if ss.size() <=12:
return tp_string_tiny(ss.c_str(), ss.size())
else:
return tp_string_from_stdstring(tp, ss)
else:
int l=0,i
tp_obj r
char *s
for (i=0; i<val.list.val->len; i++):
if i!=0:
l += tp_string_len(delim)
l += tp_string_len(tp_str(tp, val.list.val->items[i]))
r = tp_string_t(tp,l)
s = tp_string_getptr(r)
l = 0
for (i=0; i<val.list.val->len; i++):
tp_obj e
if i!=0:
memcpy(s+l, tp_string_getptr(delim), tp_string_len(delim)); l += tp_string_len(delim)
e = tp_str(tp, val.list.val->items[i])
memcpy(s+l, tp_string_getptr(e), tp_string_len(e)); l += tp_string_len(e)
return r
@static
def split_tiny_string(TP, tp_obj txt, tp_obj delims) -> tp_obj:
tp_obj r = tp_list_t(tp)
if strlen(delims.str.val) != 1:
throw "ERROR: can only split a tiny string by a single character tiny string"
int len = 1
std::string text = std::string( (const char*)&txt.str.val, strlen(txt.str.val))
std::size_t start = text.find(delims.str.val[0]), end = 0
if start == std::string::npos:
r.append(text)
return r
if start != std::string::npos:
r.append(text.substr(0, start))
while (end = text.find(delims.str.val[0], start+len)) != std::string::npos:
r.append(text.substr(start+len, (end-len) - start))
start = text.find(delims.str.val[0], end)
if start != std::string::npos:
r.append(text.substr(start+len))
return r
def tpy_str_split(TP) ->tp_obj:
tp_obj v = TP_OBJ()
tp_obj d = TP_OBJ()
if v.type.type_id==TP_STRING_TINY and d.type.type_id==TP_STRING_TINY:
return split_tiny_string(tp, v,d)
else:
tp_obj r = tp_list_t(tp)
auto parts = __split_string__(v, d)
for (auto & part : parts):
r.append(part)
return r
def tpy_str_splitlines(TP) ->tp_obj:
tp_obj v = TP_OBJ()
tp_obj r = tp_list_t(tp)
auto parts = __split_string__(v, std::string("\n"))
for (auto & part : parts):
r.append(part)
return r
def tpy_str_find(TP) ->tp_obj:
tp_obj s = TP_OBJ()
tp_obj v = TP_OBJ()
return tp_number(tp_str_index(s,v))
def tpy_str_index(TP) ->tp_obj:
tp_obj s = TP_OBJ()
tp_obj v = TP_OBJ()
int n = tp_str_index(s,v)
if n >= 0:
return tp_number(n)
tp_raise(tp_None,tp_string_atom(tp, "tp_str_index ValueError: substring not found"))
def tpy_chr(TP) ->tp_obj:
int v = TP_NUM()
return tp_string_tiny( (unsigned char)v )
##return tp_string_t_from_const(tp, tp->chars[(unsigned char)v], 1)
def tpy_ord(TP) ->tp_obj:
##tp_obj s = TP_STR()
##if tp_string_len(s) != 1:
## tp_raise(tp_None,tp_string_atom(tp, "tp_ord TypeError: `ord()` expected a character"))
##return tp_number((unsigned char)tp_string_getptr(s)[0])
tp_obj s = TP_OBJ()
return tp_number( (unsigned char)s.str.val[0] )
def tpy_str_strip(TP) ->tp_obj:
tp_obj o = TP_TYPE(TP_STRING)
char const *v = tp_string_getptr(o)
int l = tp_string_len(o)
int i; int a = l, b = 0
tp_obj r
char *s
for (i=0; i<l; i++):
if v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r':
a = _tp_min(a,i); b = _tp_max(b,i+1)
if (b-a) < 0:
return tp_string_atom(tp, "")
r = tp_string_t(tp,b-a)
s = tp_string_getptr(r)
memcpy(s,v+a,b-a)
return r
def tpy_str_replace(TP) ->tp_obj:
std::string str = TP_OBJ()
std::string tag = TP_OBJ()
std::string target = TP_OBJ()
size_t pos = 0;
while (pos = str.find(tag, pos)) != std::string::npos:
str.replace(pos, tag.length(), target)
pos += target.length()
return tp_string_from_stdstring(tp, str )