17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
ndup = terralib.externfunction('strndup',{rawstring, intptr} -> rawstring);
fmt = terralib.externfunction('asprintf',
terralib.types.funcpointer({&rawstring,rawstring},{int},true));
bfmt = terralib.externfunction('sprintf',
terralib.types.funcpointer({rawstring,rawstring},{int},true));
span = terralib.externfunction('strspn',{rawstring, rawstring} -> rawstring);
}
do local strptr = (lib.mem.ptr(int8))
local strref = (lib.mem.ref(int8))
local byteptr = (lib.mem.ptr(uint8))
strptr.metamethods.__cast = function(from,to,e)
if from == &int8 then
return `strptr {ptr = e, ct = m.sz(e)}
................................................................................
var sz = lib.math.biggest(self.ct, other.ct)
for i = 0, sz do
if self.ptr[i] == 0 and other.ptr[i] == 0 then return true end
if self.ptr[i] ~= other.ptr[i] then return false end
end
return true
end
strptr.methods.cmpl = macro(function(self,other)
return `self:cmp(strptr { ptr = [other:asvalue()], ct = [#(other:asvalue())] })
end)
strref.methods.cmpl = macro(function(self,other)
return `self:cmp(strref { ptr = [other:asvalue()], ct = [#(other:asvalue())] })
end)
................................................................................
for j=0, reject.ct do
if str.ptr[i] == reject.ptr[j] then return i end
end
end
return maxlen
end
terra m.ffw(str: &int8, maxlen: intptr)
while maxlen > 0 and @str ~= 0 and
(@str == @' ' or @str == @'\t' or @str == @'\n') do
str = str + 1
maxlen = maxlen - 1
end
return str
end
terra m.ffw_unsafe(str: &int8)
while @str ~= 0 and
(@str == @' ' or @str == @'\t' or @str == @'\n') do
str = str + 1
end
return str
end
return m
|
>
>
>
>
>
>
>
>
>
>
>
<
>
>
>
>
>
|
|
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
..
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
ndup = terralib.externfunction('strndup',{rawstring, intptr} -> rawstring);
fmt = terralib.externfunction('asprintf',
terralib.types.funcpointer({&rawstring,rawstring},{int},true));
bfmt = terralib.externfunction('sprintf',
terralib.types.funcpointer({rawstring,rawstring},{int},true));
span = terralib.externfunction('strspn',{rawstring, rawstring} -> rawstring);
}
terra m.ffw(str: &int8, maxlen: intptr)
if maxlen == 0 then maxlen = m.sz(str) end
while maxlen > 0 and @str ~= 0 and
(@str == @' ' or @str == @'\t' or @str == @'\n') do
str = str + 1
maxlen = maxlen - 1
end
return str
end
do local strptr = (lib.mem.ptr(int8))
local strref = (lib.mem.ref(int8))
local byteptr = (lib.mem.ptr(uint8))
strptr.metamethods.__cast = function(from,to,e)
if from == &int8 then
return `strptr {ptr = e, ct = m.sz(e)}
................................................................................
var sz = lib.math.biggest(self.ct, other.ct)
for i = 0, sz do
if self.ptr[i] == 0 and other.ptr[i] == 0 then return true end
if self.ptr[i] ~= other.ptr[i] then return false end
end
return true
end
terra strptr:ffw()
var newp = m.ffw(self.ptr,self.ct)
var newct = self.ct - (newp - self.ptr)
return strptr { ptr = newp, ct = newct }
end
strptr.methods.cmpl = macro(function(self,other)
return `self:cmp(strptr { ptr = [other:asvalue()], ct = [#(other:asvalue())] })
end)
strref.methods.cmpl = macro(function(self,other)
return `self:cmp(strref { ptr = [other:asvalue()], ct = [#(other:asvalue())] })
end)
................................................................................
for j=0, reject.ct do
if str.ptr[i] == reject.ptr[j] then return i end
end
end
return maxlen
end
terra m.ffw_unsafe(str: &int8)
while @str ~= 0 and
(@str == @' ' or @str == @'\t' or @str == @'\n') do
str = str + 1
end
return str
end
terra m.find(haystack: pstr, needle: pstr): pstr
for i=0,haystack.ct do
for j=0, needle.ct do
if haystack(i + j) ~= needle(j) then goto nomatch end
end
do return pstr {
ptr = haystack.ptr + i;
ct = haystack.ct - i;
} end
::nomatch::end
return pstr.null()
end
terra m.splitmap(str: pstr, delim: pstr, expect: uint16)
var vec: lib.mem.vec(pstr) vec:init(expect)
var start = pstr{str.ptr, str.ct}
while true do
var n = m.find(start, delim)
if not n then break end
vec:push(pstr {ptr = start.ptr, ct = start.ct - n.ct})
n.ptr = n.ptr + delim.ct
n.ct = n.ct - delim.ct
start = n
end
vec:push(start)
return vec:crush()
end
terra m.toknext(str: m.t, delim: int8, brkspace: bool): {pstr,intptr,bool}
var b: m.acc b:init(48)
var mode: int8 = 0
var esc = false
var spacebroke = false
var max = 0
for i=0, str.ct do
max = i
if str(i) == 0 then break
elseif esc == true then b:push(str.ptr + i,1) esc = false
elseif str(i) == @'\\' then esc = true
elseif mode == 0 and str(i) == delim then break
elseif mode ~= 2 and str(i) == @'"' then
if mode == 1
then mode = 0
else mode = 1
end
elseif mode ~= 1 and str(i) == @"'" then
if mode == 2
then mode = 0
else mode = 2
end
elseif brkspace and mode == 0 and (
str(i) == @' ' or str(i) == @'\t' or
str(i) == @'\r' or str(i) == @'\n') then
spacebroke = true
break
else b:push(str.ptr + i,1) end
end
if mode ~= 0 then return m.t.null(), 0, false end
return b:finalize(), max, spacebroke
end
return m
|