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

  1.  
  2.  
  3.  
  4.  
  5. # include "strings.h"
  6.  
  7. function repS{(s: String; n: Nat0):String};
  8. {
  9. * [[ Returns s * n ]]
  10. * Replicates s, n times.
  11. }
  12.   var  null, ChFromS: Char;
  13.        lens, StaticLength: Nat0;
  14.        i, ExtraChunks, CurrentLength: Nat0;
  15.        StringHead: String;
  16.        temp: stringtail;
  17.        k: Integer;
  18.        j: 1..slength;
  19.        sp: CharOfString;
  20. begin
  21.   null := chr(0); lens := lengthS(s); StaticLength := lens*n;
  22.   if StaticLength = 0 then repS := nil { -- emptyS} else begin
  23.     ExtraChunks := (StaticLength-1) div slength;
  24.     { -- Copy into String head }
  25.     new(StringHead);
  26.     with StringHead^ do begin
  27.         LEN := StaticLength;
  28.         REFS := 0;
  29.         TAIL := nil;
  30.         first(sp, s); k := 1;
  31.         { -- Copy string, null padding if necessary }
  32.         for j := 1 to slength do
  33.            if j > StaticLength
  34.            then HEAD[j] := null
  35.            else begin
  36.             next(sp, ChFromS);
  37.             if k = lens then begin
  38.             k := 1; first(sp, s)
  39.             end else
  40.             k := k+1;
  41.             HEAD[j] := ChFromS
  42.            end;
  43.         { -- Allocate and link in any extra string chunks needed}
  44.         for i := 1 to ExtraChunks do begin
  45.            new(temp); temp^.REST := TAIL; TAIL := temp
  46.         end;   
  47.         { -- Loop through copying string tail if required }
  48.         temp := TAIL;
  49.         CurrentLength := 0;
  50.         while temp <> nil do begin
  51.            with temp^ do begin
  52.             CurrentLength := CurrentLength+slength;
  53.                     { -- Copy string, null padding if necessary }
  54.             for j := 1 to slength do
  55.                if j+CurrentLength > StaticLength
  56.                then MORE[j] := null
  57.                else begin
  58.                     next(sp, ChFromS);
  59.                     if k = lens then begin
  60.                         k := 1; first(sp, s)
  61.                     end else
  62.                         k := k+1;
  63.                     MORE[j] := ChFromS
  64.                 end
  65.            end;
  66.            temp := temp^.REST
  67.         end{ -- while};
  68.         end{ -- with};
  69.     { -- Return the newly created dynamic string }
  70.     repS := StringHead
  71.   end;
  72.   if s <> nil then if s^.REFS = 0 then disposeS(s);
  73. end{ -- repS};
  74.