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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
-
-
!
-
|
|
|
|
|
|
|
|
|
|
!
-
|
|
|
|
-
|
|
|
|
|
|
|
|
-
|
|
|
-
|
!
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
-
-
-
!
-
|
|
!
-
-
|
|
!
|
!
-
-
|
|
!
|
!
-
-
|
|
!
|
!
-
-
|
|
!
|
!
-
|
|
-
|
-
|
-
|
|
!
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
!
| #module strlist item_count, item_sorttable, item_data, item_len, deli_count, deli_table
#define ctype min( %1, %2 ) limit( (%1), 0x80000001, (%2) )
#modinit
item_count = 0 dim item_sorttable, 1 sdim item_data, 64, 1 dim item_len, 1
deli_count = 0 dim deli_table, 1
sdim sl_str, 64, 3
dim sl_l, 8
return
#modfunc SL_Prop int mode___
if ( mode___ == 0 ) { return ( item_count )
} else : if ( mode___ == 1 ) { return ( deli_count )
}
return ( -1 )
#defcfunc SL_cCount var mv___
if ( vartype( mv___ ) != 5 ) : return ( -1 )
SL_Prop mv___, 0
return ( stat )
#defcfunc SL_cEmptycount var mv___
if ( vartype( mv___ ) != 5 ) : return ( -1 )
SL_Prop mv___, 1
return ( stat )
#define global SL_Find( %1, %2 ) SL_Func___ %1, %2, 0
#define global SL_Add( %1, %2 ) SL_Func___ %1, %2, 1
#define global SL_Del( %1, %2 ) SL_Func___ %1, %2, 2
#modfunc SL_Func___ str str___, int mode___
sl_i = -1
if ( item_count ) {
sl_j = 0
sl_k = item_count - 1
repeat item_count
sl_i = (sl_j + sl_k) / 2
sl_n = (str___ != item_data(item_sorttable(sl_i)))
if ( sl_n == 0 ) { break }
if ( sl_j == sl_k ) { sl_i = -(sl_i + (sl_n + 3) / 2) : break }
if ( sl_n > 0 ) { sl_j = sl_i + 1 } else { sl_k = sl_i }
loop
}
if ( mode___ == 0 ) {
if ( sl_i >= 0 ) { return ( item_sorttable(sl_i) ) } else { return ( sl_i ) }
} else : if ( mode___ == 1 ) {
if ( sl_i >= 0 ) : return ( item_sorttable(sl_i) )
sl_i = -(sl_i + 1)
item_sorttable(item_count) = 0
memcpy item_sorttable, item_sorttable, (item_count - sl_i) * 4, (sl_i + 1) * 4, sl_i * 4
if ( deli_count > 0 ) {
item_sorttable(sl_i) = deli_table(deli_count - 1)
deli_count --
} else {
item_sorttable(sl_i) = item_count
}
item_data(item_sorttable(sl_i)) = str___
item_len(item_sorttable(sl_i)) = strlen( item_data(item_sorttable(sl_i)) )
item_count ++
return ( item_sorttable( sl_i ) )
} else : if ( mode___ == 2 ) {
if ( sl_i < 0 ) : return ( -1 )
deli_table(deli_count) = item_sorttable(sl_i)
deli_count ++
item_data(item_sorttable(sl_i)) = ""
item_len(item_sorttable(sl_i)) = -1
memcpy item_sorttable, item_sorttable, (item_count - sl_i - 1) * 4, sl_i * 4, (sl_i + 1) * 4
item_count --
return ( 0 )
}
return ( -1 )
#modfunc SL_iGet var ret___, int itemindex___
mref sl_ctx, 68
if ( (itemindex___ < 0) | (itemindex___ >= length(item_data)) ) { sl_ctx(197) = -1 : return }
ret___ = item_data(itemindex___)
sl_ctx(197) = item_len(itemindex___)
return
#defcfunc SL_ciGet var mv___, int itemindex___, local rv___
mref sl_ctx, 68
if ( vartype( mv___ ) != 5 ) { sl_ctx(197) = -1 : return ( "" ) }
SL_iGet mv___, rv___, itemindex___
if ( strsize == -1 ) : return ( "" )
return ( rv___ )
#modfunc SL_iPtr var ret___, int itemindex___
mref sl_ctx, 68
if ( (itemindex___ < 0) | (itemindex___ >= length(item_data)) ) { sl_ctx(197) = -1 : return }
ret___ = varptr( item_data(itemindex___) )
sl_ctx(197) = item_len(itemindex___)
return
#defcfunc SL_ciPtr var mv___, int itemindex___, local rv___
mref sl_ctx, 68
if ( vartype( mv___ ) != 5 ) { sl_ctx(197) = -1 : return ( 0 ) }
SL_iPtr mv___, rv___, itemindex___
if ( strsize == -1 ) : return ( 0 )
return ( rv___ )
#modfunc SL_iDel int itemindex___, local rv___
SL_iGet thismod, rv___, itemindex___
if ( strsize == -1 ) : return ( -1 )
SL_Del thismod, rv___
return ( stat )
#define global SL_ItemsToStr( %1, %2, %3 = "\\n", %4 = "", %5 = "", %6 = 0 ) SL_ItemsToStr___ %1, %2, %3, %4, %5, %6
#modfunc SL_ItemsToStr___ var ret___, str sep___, str pre___, str pst___, int mode___
mref sl_ctx, 68
sdim ret___, 4096
sl_k = 4096
if ( item_count == 0 ) { sl_ctx(197) = -1 : return }
sl_str(0) = sep___, pre___, pst___
sl_l(0) = strlen( sep___ ), strlen( pre___ ), strlen( pst___ )
sl_l(3) = sl_l(0) + sl_l(1) + sl_l(2)
sl_n = 0
sl_j = 0
repeat length( item_data )
if ( mode___ == 2 ) { sl_i = item_sorttable( cnt )
} else : if ( mode___ == 3 ) { sl_i = item_sorttable( item_count - cnt - 1 )
} else { sl_i = cnt : if ( item_len(sl_i) == -1 ) : continue
}
if ( sl_j + item_len(sl_i) + sl_l(3) >= sl_k ) { sl_k += 4096 : memexpand ret___, sl_k }
memcpy ret___, sl_str(1), sl_l(1), sl_j, 0 : sl_j += sl_l(1)
memcpy ret___, item_data(sl_i), item_len(sl_i), sl_j, 0 : sl_j += item_len(sl_i)
memcpy ret___, sl_str(2), sl_l(2), sl_j, 0 : sl_j += sl_l(2)
memcpy ret___, sl_str(0), sl_l(0), sl_j, 0 : sl_j += sl_l(0)
sl_n ++
if ( sl_n == item_count ) : break
loop
poke ret___, sl_j - sl_l(0), 0
sl_ctx(197) = sl_j - sl_l(0)
return
#define global SL_StrToItems( %1, %2, %3 = "", %4 = 1 ) SL_StrToItems___ %1, %2, %3, %4
#modfunc SL_StrToItems___ str str___, str sep___, int mode___, local i0___, local i1___, local i2___, local i3___
i0___ = 0 i1___ = 0 i2___ = 0
sl_str(0) = str___
sl_l(0) = strlen( str___ ), strlen( sep___ ), -1, -1, -1, -1, -1, -1
repeat sl_l(0)
if ( sl_l(4) - i2___ < 0 ) {
sl_l(4) = instr( sl_str(0), i2___, sep___ )
if ( sl_l(4) == -1 ) { sl_l(4) = sl_l(0) } else { sl_l(4) += i2___ }
}
i3___ = sl_l(4)
if ( mode___ & 0x10 ) {
if ( sl_l(2) - i2___ < 0 ) {
sl_l(2) = instr( sl_str(0), i2___, "\"" )
if ( sl_l(2) == -1 ) { sl_l(2) = sl_l(0) } else { sl_l(2) += i2___ }
}
i3___ = min( i3___, sl_l(2) )
}
if ( mode___ & 0x01 ) {
if ( sl_l(5) - i2___ < 0 ) {
sl_l(5) = instr( sl_str(0), i2___, "\n" )
if ( sl_l(5) == -1 ) { sl_l(5) = sl_l(0) } else { sl_l(5) += i2___ }
}
i3___ = min( i3___, sl_l(5) )
}
if ( mode___ & 0x02 ) {
if ( sl_l(6) - i2___ < 0 ) {
sl_l(6) = instr( sl_str(0), i2___, "\t" )
if ( sl_l(6) == -1 ) { sl_l(6) = sl_l(0) } else { sl_l(6) += i2___ }
}
i3___ = min( i3___, sl_l(6) )
}
if ( mode___ & 0x04 ) {
if ( sl_l(7) - i2___ < 0 ) {
sl_l(7) = instr( sl_str(0), i2___, " " )
if ( sl_l(7) == -1 ) { sl_l(7) = sl_l(0) } else { sl_l(7) += i2___ }
}
i3___ = min( i3___, sl_l(7) )
}
if (i3___ > 0) { sl_n = peek( sl_str(0), i3___ - 1 ) } else { sl_n = 0 }
if ( ((sl_n >= 0x80) & (sl_n <= 0x9F)) | (sl_n >= 0xE0) ) { i2___ += i3___ + 1 : continue }
if ( i3___ == sl_l(0) ) {
i1___ = sl_l(0)
i2___ = sl_l(0)
} else : if ( (i3___ == sl_l(2)) & ((mode___ & 0x10) != 0) ) {
if ( sl_l(2) == sl_l(0) ) {
sl_l(3) = sl_l(0)
} else {
sl_l(3) = instr( sl_str(0), sl_l(2) + 1, "\"" )
if ( sl_l(3) == -1 ) { sl_l(3) = sl_l(0) } else { sl_l(3) += sl_l(2) + 1 }
}
i2___ = sl_l(3) + 1
continue
} else : if ( (i3___ == sl_l(5)) & ((mode___ & 0x01) != 0) ) {
i1___ = sl_l(5)
i2___ = sl_l(5) + 2
} else : if ( (i3___ == sl_l(6)) & ((mode___ & 0x02) != 0) ) {
i1___ = sl_l(6)
i2___ = sl_l(6) + 1
} else : if ( (i3___ == sl_l(7)) & ((mode___ & 0x04) != 0) ) {
i1___ = sl_l(7)
i2___ = sl_l(7) + 1
} else {
i1___ = sl_l(4)
i2___ = sl_l(4) + sl_l(1)
}
if ( i0___ != i1___ ) : SL_Add thismod, strmid( sl_str(0), i0___, i1___ - i0___ )
i0___ = i2___
if ( i2___ >= sl_l(0) ) : break
loop
return
#global
#if 0
newmod sl, strlist
s0 = "123,456,789,abc,123"
SL_StrToItems sl, s0, ",", 0
SL_ItemsToStr sl, s1, "\n",,, 2
mes s0
mes s1
#endif
|