Re: String types
- Posted by jbrown105 at speedymail.org Nov 08, 2002
- 403 views
Yeah, this is it. Personally, I'd prefer the C char arrary lib, but thats just me. On 0, Juergen Luethje <jluethje at gmx.de> wrote: > > jbrown wrote: > > <snip> > > Didnt some one find a way to "compress" a sequence, so that each > > element would have space to hold 4 characters? (Hence 100mb would take > > 25 or so mil elements, and actually BE only 100 mb in memory, then you > > could use the same scheme and access like 50 characters at a time, > > hence saving on memory.) > > > > That said (i.e. that workarounds do exist) its still a bit of a kludge. > > It'd be nice if Euphoria supported such things directly. <snip> > > Do you mean something like this? > > --====================================================================-- > > -- pack_str.e, v1.0 > > type string (object x) > object t > > if atom(x) then return 0 end if > > for i = 1 to length(x) do > t = x[i] > if not integer(t) or t < 0 or t > #FF then > return 0 > end if > end for > return 1 > end type > > > function ceil (object x) > -- the opposite of floor() > return -floor(-x) > end function > > > function pack4 (string s) > -- length(s) must be <= 4 > atom ret > > ret = 0 > for i = length(s) to 1 by -1 do > ret = ret*#100 + s[i] > end for > return ret > end function > > > function unpack4 (atom d) > sequence ret > integer i > > ret = {0,0,0,0} > i = 1 > while d do > ret[i] = remainder(d, #100) > d = floor(d/#100) > i += 1 > end while > return ret > end function > > ------------------------------------------------------------------------ > > global function pack_string (string u) > sequence ret > integer n, i > > n = ceil(length(u)/4) + 1 > ret = repeat(0, n) > ret[1] = length(u) -- If length() is not stored, there will be > i = 1 -- problems with trailing zeros. > for k = 2 to n-1 do > ret[k] = pack4(u[i..i+3]) > i += 4 > end for > ret[n] = pack4(u[i..length(u)]) > return ret > end function > > > global function unpack_string (sequence p) > sequence ret > integer i > > ret = repeat(0, length(p)*4) > i = 1 > for k = 2 to length(p) do > ret[i..i+3] = unpack4(p[k]) > i += 4 > end for > return ret[1..p[1]] > end function > > ------------------------------------------------------------------------ > > include misc.e <snip> > > > --