unit HNVString;

interface

{*********************************************************}
{*                  OPSTRING.PAS 1.20                    *}
{*     Copyright (c) TurboPower Software 1987, 1992.     *}
{*                 All rights reserved.                  *}
{*********************************************************}

Type
   CharSet =
      Set of Char;

function WordCount(S : string; WordDelims : CharSet) : Byte;
    {-Given a set of word delimiters, return number of words in S}

function WordPosition(N : Byte; S : string; WordDelims : CharSet) : Byte;
    {-Given a set of word delimiters, return start position of N'th word in S}

function ExtractWord(N : Byte; S : string; WordDelims : CharSet) : string;
    {-Given a set of word delimiters, return the N'th word in S}

function RightJust( S : String; N : Word ) : String;

Function HexB( B : Byte ):String;

Function HexText( Var Buff; N : integer ) : String;

implementation

Const
   Dig : Array[0..$F]of Char = '0123456789ABCDEF';

{
   Convert BYTE to Hex String
}
Function HexB( B : Byte ):String;
Var
   T : String;
begin
   T := '';
   T := T + Dig[ B shr 4 ];
   T := T + Dig[ B and $0F ];
   HexB := T;
end;
{
   Convert BUFFER to HEX String
}
Function HexText( Var Buff; N : integer ) : String;
Type
   ByteArray =
      Array of Byte;
Var
   i : integer;
   S : String;
   B : ByteArray Absolute Buff;
begin
   S := '';
   for i:=1 to N do
      S := S + HexB( B[i] );
   HexText := S;
end;


function RightJust( S : String; N : Word ) : String;
Var
   T : String;
   i : Integer;
   j : Integer;
begin
   T := '';
   j := N;
   if Length( S ) > j then
      T := S
   else
      begin
         j := j - Length( S );
         for i:=1 to j do
            T := T + ' ';
         T := T + S;
      end;
   RightJust := T;
end;

function WordCount(S : string; WordDelims : CharSet) : Byte;
    {-Given a set of word delimiters, return number of words in S}
var
   Count : Byte;         {!!.02}
   I : Word;                  {!!.02}
   SLen : Integer;
begin

   SLen := Length( S );
   Count := 0;
   I := 1;

   while I <= SLen do
      begin
         {skip over delimiters}
         while (I <= SLen) and (S[I] in WordDelims) do
            Inc(I);

         {if we're not beyond end of S, we're at the start of a word}
         if I <= SLen then
            Inc(Count);

         {find the end of the current word}
         while (I <= SLen) and not(S[I] in WordDelims) do
            Inc(I);
      end;

   WordCount := Count;
end;

function WordPosition(N : Byte; S : string; WordDelims : CharSet) : Byte;
    {-Given a set of word delimiters, return start position of N'th word in S}
var
   Count : Byte;         {!!.02}
   I : Word;                  {!!.02}
   SLen : Word;
begin
   Count := 0;
   I := 1;

   SLen := Length( S );

   WordPosition := 0;

   while (I <= SLen) and (Count <> N) do
      begin
         {skip over delimiters}
         while (I <= SLen) and (S[I] in WordDelims) do
            Inc(I);

         {if we're not beyond end of S, we're at the start of a word}
         if I <= SLen then
            Inc(Count);

         {if not finished, find the end of the current word}
         if Count <> N then
            while (I <= SLen) and not(S[I] in WordDelims) do
               Inc(I)
         else
            WordPosition := I;
      end;
end;

function ExtractWord(N : Byte; S : string; WordDelims : CharSet) : string;
    {-Given a set of word delimiters, return the N'th word in S}
var
   I : Word; {!!.12}
   SLen : Integer;
   Temp : String;
begin

   SLen := Length( S );
   Temp := '';
   I := WordPosition(N, S, WordDelims);

   if I <> 0 then
      {find the end of the current word}
      while (I <= SLen) and not(S[I] in WordDelims) do
         begin
            {add the I'th character to result}
            Temp := Temp + S[i];
            Inc(I);
         end;

    ExtractWord := Temp;

end;

end.
