home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / strings.swg / 0132_Null terminated string functions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  5.9 KB  |  207 lines

  1. unit StrPlus;
  2.  
  3. {---------------------------------------------------------------------------}
  4. { Extra string manipulation - by Michael Dales                              }
  5. {                                                                           }
  6. { Defines a standard null terminated string, called cString and several     }
  7. { manipulation functions. Nothing brilliant, but it all works. Using this   }
  8. { along with the strings unit gives you just about all atring functions you }
  9. { could ever need. Just like christmas eh? :-)                              }
  10. {                                                                           }
  11. { Email comments to: 9402198d@udcf.gla.ac.uk                                }
  12. { URL: http://www.gla.ac.uk/Clubs/WebSoc/~9402198d/index.html               }
  13. {---------------------------------------------------------------------------}
  14.  
  15. interface
  16.  
  17. uses Strings;
  18.  
  19. const StringSize = 512;         {Size of string type}
  20.  
  21. type cString = array[0..StringSize] of Char; {New string type}
  22.  
  23. {BlankString - Empties a string}
  24. procedure BlankString(var S:cString);
  25.  
  26. {IsLetter - Returns true if C is alphabetic}
  27. function IsLetter(C:Char):Boolean;
  28.  
  29. {StripTo - Strip all characters in S up to C}
  30. procedure StripTo(C:Char; var S:cString);
  31.  
  32. {StripFrom - Strip all characters in S from C}
  33. procedure StripFrom(C:Char; var S:cString);
  34.  
  35. {RemoveFirstChar - Remove the first character from S}
  36. procedure RemoveFirstChar(var S:cString);
  37.  
  38. {RemoveLeadingSpaces - Removes any spaces at the start of S}
  39. procedure RemoveLeadingSpaces(var S:cString);
  40.  
  41. {GetFirstWord - Gets first all letter word from S}
  42. procedure GetFirstWord(S:cString;var Out:cString);
  43.  
  44. {GetFirstBlock - Gets the first block of text (letters & symbols) from S}
  45. procedure GetFirstBlock(S:cString;var Out:cString);
  46.  
  47. {RemoveFirstWord - Removes first word from S}
  48. procedure RemoveFirstWord(var S:cString);
  49.  
  50. {RemoveFirstWord - Removes first block of text from S}
  51. procedure RemoveFirstBlock(var S:cString);
  52.  
  53. {AddChar - Adds character C to the end of S}
  54. procedure AddChar(var S:cString; C:Char);
  55.  
  56. {---------------------------------------------------------------------------}
  57. implementation
  58. {---------------------------------------------------------------------------}
  59.  
  60.    {IsLetter - Returns true if C is alphabetic}
  61.  
  62. function IsLetter(C:Char):Boolean;
  63. begin
  64.      IsLetter:=(UpCase(C)>='A') and (UpCase(C)<='Z');
  65. end;
  66.  
  67.  
  68.     {BlankString - Empties a string}
  69.  
  70. procedure BlankString(var S:cString);
  71. begin
  72.      FillChar(S,SizeOf(S),#0);
  73. end;
  74.  
  75.     {StripFrom - Strip all characters in S from C}
  76.  
  77. procedure StripFrom(C:Char; var S:cString);
  78. var temp   : cString;
  79.     reslen : integer;
  80. begin
  81.      if (StrLen(S)>0) and (StrRScan(S,C)<>nil) then
  82.      begin
  83.           StrCopy(temp,StrRScan(S,C));
  84.           reslen:=StrLen(S)-StrLen(temp);
  85.           StrLCopy(temp,S,reslen);
  86.           StrCopy(S,temp);
  87.      end;
  88. end;
  89.  
  90.     {StripTo - Strip all characters in S up to C}
  91.  
  92. procedure StripTo(C:Char; var S:cString);
  93. var pos  : word;
  94.     temp : cString;
  95. begin
  96.      if (StrScan(S,C)<>nil) then        {If we find C in S then}
  97.      begin
  98.           StrCopy(temp,StrScan(S,C));   {Get rest of string}
  99.           StrCopy(S,temp);              {Put it in S}
  100.      end;
  101. end;
  102.  
  103.     {RemoveFirstChar - Remove the first character from S}
  104.  
  105. procedure RemoveFirstChar(var S:cString);
  106. var temp : cString;
  107. begin
  108.      if StrLen(S)>1 then                {If data in string then}
  109.      begin
  110.           StrCopy(temp,S+1);            {Get string from second character}
  111.           StrCopy(S,temp);              {Put string in S}
  112.      end else
  113.          if StrLen(S)=1 then
  114.          begin
  115.               S[0]:=#0;
  116.          end;
  117. end;
  118.  
  119.     {RemoveLeadingSpaces - Removes any spaces at the start of S}
  120.  
  121. procedure RemoveLeadingSpaces(var S:cString);
  122. begin
  123.      while S[0]=' ' do RemoveFirstChar(S);
  124. end;
  125.  
  126.     {GetFirstWord - Gets first all letter word from S}
  127.  
  128. procedure GetFirstWord(S:cString;var out:cString);
  129. var n    : integer;
  130.     temp : array[0..255] of char;
  131. begin
  132.      RemoveLeadingSpaces(S);            {Find start of word}
  133.      n:=0;
  134.      FillChar(temp,SizeOf(temp),#0);    
  135.      while IsLetter(S[n]) do            {While still letters do}
  136.      begin
  137.           temp[n]:=S[n];                {Copy character}
  138.           inc(n);
  139.      end;
  140.      StrCopy(out,temp);                 {Out set to word}
  141. end;
  142.  
  143.     {GetFirstBlock - Gets the first block of text (letters & symbols) from S}
  144.  
  145. procedure GetFirstBlock(S:cString;var out:cString);
  146. var n,a     : integer;
  147.     temp    : array[0..255] of char;
  148.     isspace : boolean;
  149. begin
  150.      IsSpace:=false;
  151.      RemoveLeadingSpaces(S);
  152.      if s[0]<>#0 then
  153.      begin
  154.           n:=0;
  155.           repeat
  156.                 IsSpace:=s[n]=' ';
  157.                 inc(n);
  158.           until IsSpace or (n=StrLen(s));
  159.           FillChar(temp,SizeOf(temp),#0);
  160.           if IsSpace then n:=Pred(n);
  161.           for a:=0 to Pred(n) do temp[a]:=s[a];
  162.           StrCopy(out,temp);
  163.      end else
  164.          BlankString(out);
  165. end;
  166.  
  167.  
  168.     {RemoveFirstWord - Removes first word from S}
  169.  
  170. procedure RemoveFirstWord(var S:cString);
  171. begin
  172.      RemoveLeadingSpaces(S);            {Get to word}
  173.      while IsLetter(S[0]) do RemoveFirstChar(S);
  174.      RemoveLeadingSpaces(S);
  175. end;
  176.  
  177.     {RemoveFirstWord - Removes first block of text from S}
  178.  
  179. procedure RemoveFirstBlock(var S:cString);
  180. var temp : boolean;
  181.     n    : integer;
  182. begin
  183.      RemoveLeadingSpaces(S);
  184.      temp:=false;
  185.      n:=0;
  186.      repeat
  187.            temp:=(s[n]=' ');
  188.            inc(n);
  189.      until temp or (pred(n)=StrLen(S));
  190.      if temp then
  191.         StripTo(' ',S)
  192.      else
  193.          StrCopy(S,#0);
  194.      RemoveLeadingSpaces(S);
  195. end;
  196.  
  197.     {AddChar - Adds character C to the end of S}
  198.  
  199. procedure AddChar(var S:cString; C:Char);
  200. var temp : array[0..1] of char;
  201. begin
  202.      temp[0]:=c;
  203.      temp[1]:=#0;
  204.      StrCat(S,temp);
  205. end;
  206.  
  207. end.