!!!!!!!!!!!!!!!!!!!!   String procedures  !!!!!!!!!!!!!!!!!!!!!!!!

string (255)fn  SUBSTRING(string (255) s, integer  from,to)
string (255) t
  t = ""
  while  from <= to cycle 
    t = t.tostring(charno(s,from));  from = from+1
  repeat 
  result  = t
end 

integerfn  INDEX(string (*)name  var, string (255) match)
!Return index position of first occurrence of match within string
integer  i=0,j,l;  l=length(match)
  cycle 
    result  = 0 if  i > length(var)-l
    i = i+1
    j = 0
    cycle 
      result  = i if  j = l
      j = j+1
    repeat  until  charno(var,i+j-1) # charno(match,j)
  repeat 
end 

predicate  RESOLVE(string (*)name  var, string (255) match,
                   string (*)name  fore,aft)
!Test for (first) occurrence of MATCH within VAR
! and assign preceding part to FORE and following part to AFT.
! So "%if RESOLVE(A,B,C,D)" equates to "%if A -> B.(C).D"
integer  i
routine  do aft
  aft = substring(var,i+length(match),length(var))
end 
  i = index(var,match)
  false  if  i = 0
  if  addr(fore) # addr(var) start 
    fore = substring(var,1,i-1)
    do aft
  finish  else  start 
    do aft
    length(var) = i-1
  finish 
  true 
end 

string (127)fn  ITOS(integer  v,p)
integer  vv,q,pos
byteintegerarray  store(0:127)
  vv = v;  vv = -vv if  vv > 0
  pos = 127
  while  vv <= -10 cycle 
    q = vv//10
    store(pos) = q*10-vv+'0';  pos = pos-1
    vv = q
  repeat 
  store(pos) = '0'-vv
  pos = pos-1 and  store(pos) = '-' if  v < 0
  if  p <= 0 start 
    p = pos-128-p
  finish  else  start 
    p = pos-128+p;  p = 0 if  p < 0
    p = p+1 if  v >= 0
  finish 
  while  p > 0 cycle 
    pos = pos-1;  store(pos) = ' ';  p = p-1
  repeat 
  pos = pos-1;  store(pos) = 127-pos
  result  = string(addr(store(pos)))
end