blob: d51d06d9010ae148d9983ce486e101364e0bf0e4 (
plain)
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
|
/; export module cstr
/; eq (~uint8 a, b) [bool]
/; loop (a` == b` && a` !== 0) [a++; b++] ;/
/; if (b` == 0 && a` == 0)
return true
;/
return false
;/
/; length (~uint8 str) [uint]
uint out = 0
/; loop (str{out} !== 0) [out++] ;/
return out
;/
/; copy (~uint8 str) [~uint8]
~uint8 out = _alloc(length(str) + 1)
int i = 0
/; loop (str{i} !== 0) [out{i} = str{i}; i++] ;/
out{i} = 0
return out
;/
/; starts_with (~uint8 str, prefix) [bool]
uint l = length(prefix)
/; loop (int i = 0; i < l) [i++]
/; if (i == l - 1)
return str{i} == prefix{i}
;; else if (str{i} !== prefix{i})
i = l
;/
;/
return false
;/
/; ends_with (~uint8 str, suffix) [bool]
uint str_l = length(str)
uint l = length(suffix)
/; if (str_l < l)
return false
;/
str_l = str_l - l
/; loop (int i = l; i > 0)
i--
/; if (str{str_l + i} !== suffix{i})
return false
;/
;/
return true
;/
/; contains_char (~uint8 str, uint8 ch) [bool]
/; loop (str` !== 0) [str++]
/; if (str` == ch)
return true
;/
;/
return false
;/
/; in_csv (~uint8 csv, ~uint8 str) [bool]
int along = 0
/; loop (csv` !== 0) [csv++]
/; if (csv` == ',')
/; if (along !< 0 && str{along} == 0)
return true
;/
along = 0
;; else if (along !< 0 && str{along} == csv`)
along++
;; else
along = -1
;/
;/
return along !< 0 && str{along} == 0
;/
/; _base_for_char (uint8 c) [uint]
/; if (c == 'b' || c == 'B')
return 2
;; else if (c == 'o' || c == 'O')
return 8
;; else if (c == 'x' || c == 'X')
return 16
;/
return 10
;/
/; _decode_bin (uint8 ch) [int]
/; if (ch == '1')
return 1
;/
return 0
;/
/; _decode_oct (uint8 ch) [int]
/; if (ch !< '0' && ch !> '7')
return ch - '0'
;/
return 0
;/
/; _decode_hex (uint8 ch) [int]
/; if (ch !< '0' && ch !> '9')
return ch - '0'
;; else if (ch !< 'a' && ch !> 'f')
return ch - 'a' + 10
;; else if (ch !< 'A' && ch !> 'F')
return ch - 'A' + 10
;/
return 0
;/
/; _decode_dec (uint8 ch) [int]
/; if (ch !< '0' && ch !> '9')
return ch - '0'
;/
return 0
;/
/; cstr_to_uint (~uint8 str) [uint]
int base = 10
int start = 0
int l = length(str)
# Get base
/; if (l > start + 2 && str{start} == '0')
/; if (str{start + 1} > '9' || str{start + 1} < 0)
base = _base_for_char(str{start + 1})
start = 2
;/
;/
uint out = 0
/; loop (start < l) [start++]
out = out * base
int decoded = 0
/; if (base == 2)
decoded = _decode_bin(str{start})
;; else if (base == 8)
decoded = _decode_oct(str{start})
;; else if (base == 10)
decoded = _decode_dec(str{start})
;; else if (base == 16)
decoded = _decode_hex(str{start})
;/
out = out + decoded
;/
return out
;/
/; reverse_str(~uint8 str)
uint8 tmp
int l = length(str)
/; loop (int i = 0; i < l / 2) [i++]
tmp = str{i}
str{i} = str{(l - 1) - i}
str{(l - 1) - i} = tmp
;/
;/
;/ # End module
|