home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pstrings / part01 / updateS.p < prev    next >
Encoding:
Text File  |  1991-08-07  |  1.6 KB  |  65 lines

  1.  
  2.  
  3.  
  4.  
  5. # include "strings.h"
  6.  
  7. procedure updateS{(var s: String; i: Nat1; c:Char)};
  8. {
  9. * Updates the string s at position  i  with the char c.
  10. * if i > lengthS(s), s is first space filled upto i-1.
  11. }
  12.    var j: 2..maxint;
  13.        chunk: stringtail;
  14.   
  15.   procedure copy(var lhs: String; rhs: String);
  16.   {
  17.   *  lhs := rhs (forces a string copy)
  18.   }
  19.     var ExtraChunks: Nat0; i: Nat1; temp, l, r: stringtail;
  20.   begin
  21.     new(lhs);
  22.     { -- Copy string head }
  23.     lhs^ := rhs^;
  24.     with lhs^ do begin
  25.       REFS := 1;
  26.       ExtraChunks := (rhs^.LEN-1) div slength;
  27.       TAIL := nil;
  28.           { -- Allocate and link in any extra string chunks needed }
  29.       for i := 1 to ExtraChunks do begin
  30.         new(temp); temp^.REST := TAIL; TAIL := temp
  31.        end
  32.      end;
  33.         { -- Loop through copying string tail if required }
  34.      l := lhs^.TAIL;  r := rhs^.TAIL;
  35.      for i := 1 to ExtraChunks do begin
  36.         l^.MORE := r^.MORE;
  37.         l := l^.REST;
  38.         r := r^.REST
  39.      end
  40.   end{ -- copy};
  41.  
  42.  
  43. begin { --  of updateS }
  44.   if s <> nil then
  45.      with s^ do
  46.       if REFS > 1 then begin
  47.     { -- Make a unique copy before update }
  48.     REFS := REFS-1;    
  49.     copy(s, s) { --  N.B. careful (!) use of var and value params. }
  50.       end;
  51.   if i <= lengthS(s) then
  52.     with s^ do
  53.       if i <= slength
  54.       then { -- pos is in string head } HEAD[i] := c
  55.       else begin
  56.         { -- find tail chunk containing pos. i }
  57.         chunk := TAIL;
  58.         for j := 2 to (i-1) div slength do
  59.             chunk := chunk^.REST;
  60.         chunk^.MORE[ (i-1) mod slength + 1 ]  := c
  61.       end
  62.   else { -- Inefficient but rare case }
  63.     assignS(s, concatS(s,concatS(repS(CtoS(' '),i-lengthS(s)-1),CtoS(c))))
  64. end{ -- updateS};
  65.