parsav  Diff

Differences From Artifact [004fceba8a]:

To Artifact [d98a573fe7]:


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