unit StrngUtils;

// ------------- P16, P18 and P24 --------------- //

// D. Rosseel
// Original: 24-07-2008
// Latest update: 26-06-2012

{
  * This unit can be used together with the original mE string and conversion libraries or together with the replacement libaries of
    Janni/Yo2lio (Florin). In the first case make directive "__Lib_Strings_Janni_Yo2Lio" (see above) "undefined", in the latter case
    make "__Lib_Strings_Janni_Yo2Lio" "defined".

  * In some cases the stack depth can be reduced by "defining" the directive "SMALLSTACK". It does not have effect on every procedure/function.

  * This unit provides extra string and conversion procedures and functions with respect to the mE libraries. It does not replace
    the mE libraries (it uses them). All extra procedures/functions provided in the string and conversions replacement
    libraries of Janni/Yo2Lio are provided, except "StrScan", "StrScanC" and "StrEnd".
    The procedures/functions in this unit try to mimick as much as possible their counterpart's behaviour in the
    Janni/Yo2Lio replacement libraries, the execution speed is much lower though (3x lower on average),
    and the codesize is a somewhat higher :-( . So, there is possibly room for optimisation (without going to assembler).

  * All code in this unit is strictly written in Pascal, no assembly language or processor dependant features are used.
    This means the code should be also usable for the P16Fxxx PIC family.

  * Additionally some extra procedures/functions with respect to the Janni/Yo2Lio replacement libraries and mE libraries:

     - Routines that do range checking on string length:
       CopyX, InsertX, StrInsertChrX, StrCatX, StrCatCX, StrAppendSufX, StrAppendPreX, StrCpyX, StrCpyCX, StrnCpyCX

     - Additional routines:
       Stuff, StuffX, PadLeft, PadLeftX, PadRight, PadRightX, ClearString, StrLenC, StrCmpC, StrnCmpC, StrCmpNoCase,
       BCDByteToStr, Hex2dWord, ShortToStrWithZeros, ByteToStrWithZeros

     - Routines that duplicate functionality under an other routine name, left in for compatibility reasons:
        StrCat_Const, StrCatX_Const, StrCatCharX, StrLen_Const, Length_Const, StrChr_Const, StrCpy_Const, StrCpyX_Const, StrnCpy_Const,
        StrnCpyX_Const, StrCmp_Const, StrnCmp_Const, StrCmp_CI, ByteToHexStr, WordToHexStr, DWordToHexStr,
        ByteToBinStr, WordToBinStr, DWordToBinStr, WordToStrWithZeros

}

// ------------------------- interface ----------------------------

{$IFNDEF P18}
{$UNDEF __Lib_Strings_Janni_Yo2Lio}
{$ENDIF}

uses __Lib_Conversions
     , __Lib_CType
     
{$IFDEF __Lib_Strings_Janni_Yo2Lio}
     , __Lib_String    // added to avoid linking errors
{$ENDIF}
     ;
     
     // --------------------- Constants --------------------------------
{$IFNDEF __Lib_Strings_Janni_Yo2Lio}
const maxByte    =  255;
      minShort   = -128;
      maxShort   =  127;
      maxWord    =  65535;
      minInt     = -32768;
      maxInt     =  32767;
      minLongInt = -2147483648;
      maxLongInt =  2147483647;
      maxdWord   =  4294967295;
{$ENDIF}
     
      // --------------------- String manipulation ----------------------
     
{$IFNDEF __Lib_Strings_Janni_Yo2Lio}
procedure memCpyC(p1: ^Byte; const p2: ^byte; N: word);
// copies N bytes string from flash address P2 to Ram memory area beginning at P1

function LengthC(const Str: ^char): word;
// Returns the length of constant string SRC
// To be called with the address of Src,
// e.g. WordVar := LengthC(@ConstStr);

procedure Setlength(var S: string; L: word);
// sets the string termination character at S[L]

procedure StringOfChar(var S: string; Ch: char; Count: word);
// fills S with Count of Ch characters and adds string termination character

procedure StrCatC(var Dest: string; const Src: ^char);
// Concatenates constant string Src to Dest.
// To be called with the address of Src,
// e.g. StrCatC(Destination, @Source);

procedure StrLCat(var S1,S2:string; MaxLen:word);
// appends up to a specified maximum number of characters from S2 to S1

procedure StrLCatC(var S1:string; S2:^const char; MaxLen:word);
// appends at most MaxLen characters from constant string at address S2 to string S1

procedure StrSplit(var S1,S2:string; n:word);
// splits S1 in 2 strings: S1 - from 0 to n-1 and S2 - from n to end

procedure StrAddCRLF(var S:string);
// appends CR+LF to the input string, S

procedure StrInsertChr(var S: string; Ch: char; N: word);
// inserts character at specified position - after n-th character (after insertion index of the new character will
// equal n and length of string will increase by 1)

procedure StrRemoveChr(var S:string; n:word);
// removes character of specified index n from S (after removal the length of string will decrease by 1)

procedure StrCpyC(var s1: string; const s2 : ^char);
// The function copies the value of the constant string s2 to the string s1
// and appends a null character to the end of s1.
// To be called with the address of S2,
// e.g. StrCpyC(VarStr, @ConstStr);

procedure StrnCpyC(var s1: string; const s2 : ^char; Len: word);
// The function copies max "Len" character from the constant string s2 to the string s1
// and appends a null character to the end of s1.
// To be called with the address of S2,
// e.g. StrnCpyC(VarStr, @ConstStr, 5);

procedure Copy(var Dst, Src: string; Index, Cnt: word);
// Copies "Cnt" characters from "Src" at position "Index" to "Dst" (Index begins at 0!)
// Make sure "Dst" can hold at least "Cnt" characters (no check possible!)

procedure CopyC(var Dest:string; Source:^const char; Index,Count: word);
// copies Count characters from constant Source to Dest starting at Source[Index]. If Index is larger than the
// length of Source, CopyC returns empty Dest string. If Count specifies more characters than are available,
// only the characters from Source[Index] to the end of Source are copied.

procedure CopyCL(var Dest:string; Source:^const char; LineNo: byte);
// copies line of the index LineNo (starting from 0) from constant Source to Dest string. If there is no line of
// this number, CopyCL returns empty Dest string.

procedure StrLCopy(var Dest,Source:string; MaxLen:word);
// copies at most Maxlen characters from Source to Dest

procedure StrLCopyC(var Dest:string; Source:^const char; MaxLen:word);
// copies at most MaxLen characters from constant at Source to Dest

procedure StrCutChr(var S:string; ch: byte);
// removes all characters equal ch from front of the string

procedure TrimLeft(var S: string);
// Removes spaces at the beginning of the string

procedure TrimRight(var S: string);
// Removes spaces at the end of the string

procedure Trim(var S: string);
// Removes spaces ate the beginning and end of the string

procedure Uppercase(var S: string);
// Makes S uppercase

procedure Lowercase(var S: string);
// Makes S lowercase

function StrChrN(var S:string; ch:char; n:word):word;
// operates as StrChr but starts search from index n in S; n should be smaller than length of S.

function StrNchr(var S:string; ch:char):word;
// returns number of occurences of ch in S

procedure StrReplChr(var S:string; ch1,ch2: char);
// replaces all occurences of ch1 with ch2 in S

function CompareStr(var S1,S2:string):integer;
// compares S1 to S2, with case-sensitivity. The return value is less than 0 if S1 is less than S2, 0 if S1 equals
// S2, or greater than 0 if S1 is greater than S2. The compare operation is based on the 8-bit ordinal value of
// each  character. Terminating char (0) is taken into account thus allowing to compare string lengths, if this is
// the only difference.

function StrLComp(var S1,S2:string; MaxLen:word):integer;
// same as CompareStr but compares at most MaxLen chars of S1 to S2

function StrStrN(var S1,S2:string; n:word):word;
// performs the same task as StrStr but from index n in S1; n should be smaller than length of S1

function Pos(var SubStr: string{[255]}; var S: string): word;
// searches for Substr within S and returns a value that is the index of the first character of Substr within S; if
// Substr is not found or Substr is a null string, function returns $FFFF (maxWord).
// SubStr can be maximal 255 chars.

function PosC(var Substr:string{[255]}; atS:^const char):word;
// searches for Substr within constant atS and returns a value that is the index of the first character of Substr
// within string atS; if Substr is not found or Substr is a null string, function returns $FFFF (maxWord).
// SubStr can be maximal 255 chars.

function PosEx(var Substr:string{[255]}; var S:string; Offset:word):word;
// searches for Substr within S starting from Offset and returns a value that is the index of the first character of
// Substr within S; if Substr is not found or Substr is a null string, function returns $FFFF (maxWord).
// SubStr can be maximal 255 chars.

function PosExC(var Substr:string{[255]}; atS:^const char; Offset:word):word;
// searches for Substr within constant atS starting from Offset and returns a value that is the index of the first
// character of Substr within string atS; if Substr is not found or Substr is a null string, function returns $FFFF
// (maxWord).
// SubStr can be maximal 255 chars.

procedure Insert(var Src, Dst: string; Index: word);
// Inserts all characters from "Src" into "Dst" at position "Index" (Index begins at 0!)
// If necessary "Dst" is extended with spaces before the insert operation
// Make sure "Dst" can hold at least "Length(Src)" + "Length(Dst)" characters (no check done!)

procedure Delete(var S: string; Index, Cnt: word);
// Removes "Cnt" characters at position "Index" in strings S (Index begins at 0!)

function StrReplace(var S1:string; var S2, S3:string{[255]}; ReplAll:boolean):byte;
// depending on ReplAll, replaces first or all occurrences of S2 in S1 with S3; result gives the number of
// replacements made.
// S2 and S3 can be maximal 255 chars.
{$ENDIF}

// -------------------------- Extra procedures and functions ------------------------------------

{
function StrCmp_(var S1: string; var S2 : string) : integer;
// replacement for StrCmp (see http://www.mikroe.com/forum/viewtopic.php?f=86&t=28440)

procedure rTrim_(var S: string);
// Removes spaces at the end of the string
}

procedure CopyX(var Dst, Src: string; Index, Cnt, DestSize: word);
// Copies "Cnt" characters from "Src" at position "Index" to "Dst" (Index begins at 0!),
// keeping into account the size of Dest. To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. CopyX(Source, Destination, 5, 10, SizeOf(Destination));

procedure InsertX(var Src, Dst: string; Index, DestSize: word);
// Inserts all characters from "Src" into "Dst" at position "Index" (Index begins at 0!)
// If necessary "Dst" is extended with spaces before the insert operation
// Keeps into account the size of Dest. To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. InsertX(Source, Destination, 5, SizeOf(Destination));

procedure Stuff(var Src, Dst: string; Index: word);
// Replaces the current content of "Dst" at position Index with the content of "Src"
// If necessary "Dst" is extended with spaces before the insert operation
// Make sure "Dst" can hold at least "Index" + "Length(Src)" characters (no check possible!)

procedure StuffX(var Src, Dst: string; Index, DestSize: word);
// Replaces the current content of "Dst" at position Index with the content of "Src"
// If necessary "Dst" is extended with spaces before the insert operation
// Keeps into account the size of Dest. To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StuffX(Source, Destination, 5, SizeOf(Destination));

procedure StrInsertChrX(var S: string; Ch: char; N, DestSize: word);
// inserts character at specified position - after n-th character (after insertion index of the new character will
// equal n and length of string will increase by 1).
// Keeps into account the size of destination S. To be called with DestSize equal to "SizeOf(S)"
// e.g. StrInsertChrX(Destination, 'Z', 5, SizeOf(Destination));

procedure PadLeft(Var Str: string; Len:word);
// Makes the length of "Str" "Len" characters by adding spaces at the beginning of "Str"
// Make sure "Src" can hold "Len" characters (no check possible!)
// If the length of "Src" was already "Len" or more characters then "Src" is not changed.

procedure PadLeftX(Var Str: string; Len, DestSize: word);
// Makes the length of "Str" "Len" characters by adding spaces at the beginning of "Str",
// keeping into account the size of Dest. To be called with DestSize equal to "SizeOf(Stringtype variable).
// If the length of "Src" was already "Len" or more characters then "Src" is not changed.

procedure PadRight(Var Str: string; Len: word);
// Makes the length of "Str" "Len" characters by adding spaces at the end of "Str".
// Make sure "Src" can hold "Len" characters (no check possible!)
// If the length of "Src" was already "Len" or more characters then "Src" is not changed.

procedure PadRightX(Var Str: string; Len, DestSize: word);
// Makes the length of "Str" "Len" characters by adding spaces at the end of "Str",
// keeping into account the size of Dest. To be called with DestSize equal to "SizeOf(Stringtype variable).
// If the length of "Src" was already "Len" or more characters then "Src" is not changed.

procedure ClearString(var Str: string; Size: word);
// Fills all positions in the string with zero
// To be called with Size equal to "SizeOf(Stringtype variable)"
// e.g. ClearString(Destination, SizeOf(Destination));

procedure StrCatX(var Dest, Src: string; DestSize: word);
// Concatenates Src to Dest, keeping into account the size of Dest.
// To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCatX(Destination, Source, SizeOf(Destination));

procedure StrCatCX(var Dest: string; const Src: ^char; DestSize: word);
// Concatenates constant string Src to Dest, keeping into account the size of Dest.
// To be called with the address of Src, and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCatCX(Destination, @Source, SizeOf(Destination));

procedure StrAppendSufX(var Dest:string; Ch: Char; DestSize: word);
// Concatenates the character "Ch" to the end of string "Dest", keeping into
// account the size of Dest.
// To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrAppendSufX(Dest, Source, SizeOf(Dest));

procedure StrAppendPreX(var Dest: string; Ch: char; DestSize: word);
// Inserts the character "Ch" at the beginning of string "Dest", keeping into
// account the size of Dest.
// To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrAppendPreX(Dest, Source, SizeOf(Dest));

function StrLenC(const Str: ^char): word;
// Returns the length of constant string SRC.
// To be called with the address of Src,
// e.g. WordVar := StrLenC(@ConstStr);

function StrChrC(const Str: ^char; ch : char) : word;
// The function returns the position (index) of the first character ch found in Str;
// if no matching character was found, the function returns 0xFFFF.
// To be called with the address of Str,
// e.g. WordVar := StrChrC(@ConstStr, 'b');

procedure StrCpyX(var s1, S2: string; DestSize: word);
// The function copies the value of the string s2 to the string s1
// and appends a null character to the end of s1, keeping into account the size of S1.
// To be called with the address of S2 and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCpyX(Str1, Str2, SizeOf(Str1));

procedure StrCpyCX(var s1: string; const s2 : ^char; DestSize: word);
// The function copies the value of the constant string s2 to the string s1
// and appends a null character to the end of s1, keeping into account the size of S1.
// To be called with the address of S2 and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCpyCX(VarStr, @ConstStr, SizeOf(VarStr));

procedure StrnCpyCX(var s1: string; const s2 : ^char; Len: word; DestSize: word);
// The function copies at most Len characters from the constant string s2 to the string s1
// and appends a null character to the end of s1, keeping into account the size of S1.
// To be called with the address of S2 and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrnCpyCX(VarStr, @ConstStr, 5, SizeOf(VarStr));

function StrCmpC(const S1: ^char; var S2 : string) : integer;
// Compares S1 and S2, and
// Returns
//  Value    Meaning
//  < 0      s1 "less than" s2
//  = 0      s1 "equal to" s2
//  > 0      s1 "greater than" s2
// usage: ShortVar := StrCmpC(@ConstStr, VarStr);

function StrnCmpC(const S1: ^char; var S2 : string; Len: word) : integer;
// Compares the first "Len" characters of S1 and S2, and
// Returns
//  Value    Meaning
//  < 0      s1 "less than" s2
//  = 0      s1 "equal to" s2
//  > 0      s1 "greater than" s2
// usage: ShortVar := StrnCmpC(@ConstStr, VarStr, 4);

function StrCmpNoCase(var S1, S2: string): integer;
// same function as "StrCmp", but case independant

// ----- Functions added for compatibility reasons with previous version of StrngUtils ------

procedure StrCat_Const(var Dest: string; const Src: ^char);
// Concatenates constant string Src to Dest.
// To be called with the address of Src,
// e.g. StrCat_Const(Destination, @Source);

procedure StrCatX_Const(var Dest: string; const Src: ^char; DestSize: word);
// Concatenates constant string Src to Dest, keeping into account the size of Dest.
// To be called with the address of Src, and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCatX_Const(Destination, @Source, SizeOf(Destination));

procedure StrCatCharX(var Dest:string; Ch: Char; DestSize: word);
// Concatenates the character "Ch" to the end of string "Dest", keeping into
// account the size of Dest.
// To be called with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCatCharX(Dest, Source, SizeOf(Dest));

function StrLen_Const(const Str: ^char): word;
// Returns the length of constant string SRC.
// To be called with the address of Src,
// e.g. WordVar := StrLen_Const(@ConstStr);

function Length_Const(const Str: ^char): word;
// Returns the length of constant string SRC
// To be called with the address of Src,
// e.g. WordVar := Length_Const(@ConstStr);

function StrChr_Const(const Str: ^char; ch : char) : word;
// The function returns the position (index) of the first character ch found in Str;
// if no matching character was found, the function returns 0xFFFF.
// To be called with the address of Str,
// e.g. WordVar := StrChr_Const(@ConstStr, 'b');

procedure StrCpy_Const(var s1: string; const s2 : ^char);
// The function copies the value of the constant string s2 to the string s1
// and appends a null character to the end of s1.
// To be called with the address of S2,
// e.g. StrCpy_Const(VarStr, @ConstStr);

procedure StrCpyX_Const(var s1: string; const s2 : ^char; DestSize: word);
// The function copies the value of the constant string s2 to the string s1
// and appends a null character to the end of s1, keeping into account the size of S1.
// To be called with the address of S2 and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrCpyX_Const(VarStr, @ConstStr, SizeOf(VarStr));

procedure StrnCpy_Const(var s1: string; const s2 : ^char; Len: word);
// The function copies max "Len" character from the constant string s2 to the string s1
// and appends a null character to the end of s1.
// To be called with the address of S2,
// e.g. StrnCpy_Const(VarStr, @ConstStr, 5);

procedure StrnCpyX_Const(var s1: string; const s2 : ^char; Len: word; DestSize: word);
// The function copies at most Len characters from the constant string s2 to the string s1
// and appends a null character to the end of s1, keeping into account the size of S1.
// To be called with the address of S2 and with DestSize equal to "SizeOf(Stringtype variable)"
// e.g. StrnCpyX_Const(VarStr, @ConstStr, 5, SizeOf(VarStr));

function StrCmp_Const(const S1: ^char; var S2 : string) : integer;
// Compares S1 and S2, and
// Returns
//  Value    Meaning
//  < 0      s1 "less than" s2
//  = 0      s1 "equal to" s2
//  > 0      s1 "greater than" s2
// usage: ShortVar := StrCmp_Const(@ConstStr, VarStr);

function StrnCmp_Const(const S1: ^char; var S2 : string; Len: word) : integer;
// Compares the first "Len" characters of S1 and S2, and
// Returns
//  Value    Meaning
//  < 0      s1 "less than" s2
//  = 0      s1 "equal to" s2
//  > 0      s1 "greater than" s2
// usage: ShortVar := StrnCmp_Const(@ConstStr, VarStr, 4);

function StrCmp_CI(var S1, S2: string): integer;
// same function as "StrCmp", but case independant

{
function strstr_(var s1, s2 : string) : word; // temprary replacement of "StrStr"
// The function locates the first occurrence of the string s2 in the string s1 (excluding the terminating null character).
// The function returns a number indicating the position of the first occurrence of s2 in s1;
// if no string was found, the function returns 0xFFFF. If s2 is a null string, the function also returns $FFFF.
}

// ----------- end of compatibility section ---------


// --------------------- Conversions -----------------------

{$IFNDEF __Lib_Strings_Janni_Yo2Lio}
procedure Byte2Bin(Value: byte; var S: string[8]);
// converts a byte to its binary string representation
// S must be minimal string[8]

procedure Word2Bin(Value: word; var S: string[16]);
// converts a word to its binary string representation
// S must be minimal string[16]

procedure DWord2Bin(Value: dword; var S: string[32]);
// converts a dword (or longint) to its binary string representation
// S must be minimal string[32]

procedure Byte2Hex(number: byte; var output: string[2]);
// converts a byte/short to hex representation in the output string
// Output must be minimal string[2]

function Hex2Byte(var hex_in: string{[2]}): byte;
// converts hex code number in hex_in into byte; accepts both lower and uppercase characters

procedure Word2Hex(number: word; var output: string[4]);
// converts a word/integer to hex representation in the output string
// Output must be minimal string[4]

function Hex2Word(var hex_in: string{[4]}): word;
// converts the hex code number in hex_in into word; accepts both lower and uppercase characters

procedure dWord2Hex(number: dword; var output: string[8]);
// converts a dword/longint to hex representation in the output string
// Output must be minimal string[8]

procedure dWordToHex(number: dword; var output: string[8]);
// converts a dword/longint to hex representation in the output string
// Output must be minimal string[8]

function Bcd2Dec_8(number: byte):byte;
//  converts BCD coded number to byte

function Dec2Bcd_8(number: byte):byte;
//  codes number of type byte in BCD

function Bcd2Dec_16(number: word):word;
//  converts BCD coded number to word

function Dec2Bcd_16(number: word):word;
//  codes number of type word in BCD

procedure StrU(number: DWord; var output: string[10]; len: byte);
// Converts any (except real) unsigned number to a string using only the required string space
// i.e. working with bytes one may use just string[3] as output; len is the minimum resulting string length,
// if len is larger than the number representation width then the number will preceded by spaces.
// Output must be minimal string[10].

procedure StrS(number: LongInt; var output: string[11]; len: byte);
// Converts any (except real) signed number to a string using only the required string space
// i.e. working with short type one may use just string[4] as output; len is the minimum resulting string length,
// if len is larger than the number representation width then the number will preceded by spaces
// Output must be minimal string[11].

procedure StrAddSpaces(var output: string[20]; len: byte; minus: boolean);
// Inserts a minus sign in front of output if minus = true and then inserts spaces at in front of output,
// the resulting string will have at least len-length
// Output must be minimal string[20]

procedure Byte2Str(number: byte; var output: string[3]);
// converts number of type byte to string containing up to 3 characters; uses only necessary string space
// Output must be minimal string[3]

procedure Byte2StrN(number: byte; var output: string[20]; len: byte);
// converts number of type byte to string containing minimum len characters - spaces are used to fill the string
// before the representation number.
// Output must be minimal string[20] worst case.

procedure Byte2StrWithZeros(number: byte; var output: string[3]);
// converts number of type byte to string of 3 characters - zeros fill the string before representation
// of the number.
// Output must be minimal string[3]

procedure Short2str(number:short; var output:string[4]);
// converts number of type short to string containing up to 4 characters; uses only necessary string space
// Output must be minimal string[4]

procedure Short2strN(number:short; var output:string[20]; len:byte);
// converts number of type short to string containing minimum len characters - spaces are used to fill the string
// before representation of number.
// Output must be minimal string[20] worst case

procedure Short2strWithZeros(number:short; var output:string[4]);
// converts number of type short to string of four characters - zeros (with minus sign, if negative) fill the string
// before representation of number.
// Output must be minimal string[4]

procedure Word2str(number:word; var output:string[5]);
// converts number of type word to string containing up to 5 characters; uses only necessary string space
// Output must be minimal string[5]

procedure Word2strN(number:word; var output:string[20]; len:byte);
// converts number of type word to string containing minimum len characters - zeros are used to fill the string
// before representation of number.
// Output must be minimal string[20] worst case

procedure Word2strWithZeros(number:word; var output:string[5]);
// converts number of type word to string of 5 characters - spaces are used to fill the string
// before representation of number.
// Output must be minimal string[5]

procedure Int2str(number:integer; var output:string[6]);
// converts number of type integer to string containing up to 6 characters; uses only necessary string space
// Output must be minimal string[6]

procedure Int2strN(number:integer; var output:string[20]; len:byte);
// converts number of type integer to string containing minimum len characters - spaces are used to fill the
// string before representation of number
// Output must be minimal string[20]

procedure Int2strWithZeros(number:integer; var output:string[6]);
// converts number of type word to string of 6 characters - zeros (with minus sign, if negative) are used to fill the string
// before representation of number
// Output must be minimal string[6]

procedure dWord2str(number:dWord; var output:string[10]);
// converts dWord number to string using only necessary string space - up to 10 characters
// Output must be minimal string[10]

procedure dWordTostr(number:dWord; var output:string[10]);
// converts input double word number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks.
// Output must be minimal string[10]

procedure dWord2strN(number:dWord; var output:string[20]; len:byte);
// converts number of type dWord to string containing minimum len characters - spaces are used to fill the
// string before representation of number
// Output must be minimal string[20]

procedure dWord2strWithZeros(number:dWord; var output:string[10]);
// converts number of type word to string of 10 characters - zeros are used to fill the string
// before representation of number
// Output must be minimal string[10]

procedure dWordTostrWithZeros(number:dWord; var output:string[10]);
// converts input dword to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.
// Output must be minimal string[10]

procedure Long2str(number:longint; var output:string[11]);
// converts Longint number to string using only necessary string space - up to 11 characters
// Output must be minimal string[11]
    
procedure Long2strN(number:longint; var output:string[20]; len:byte);
// converts number of type longint to string containing minimum len characters - spaces are used to fill the
// string before representation of number
// Output must be minimal string[20]

procedure Long2strWithZeros(number:longint; var output:string[11]);
// converts number of type word to string of 11 characters - zeros (with minus sign, if negative) are used to fill the string
// before representation of number
// Output must be minimal string[11]

procedure Float2Str(number: real; var output: string[17]; fract: byte);
// converts value of type real to string of up to 17 characters; fract is the number of decimal places (0..5)
// This version simply truncates (for now) the decimal places
// Output must be minimal string[17]

procedure Real2Str(number: real; var output: string[17]; inum, dpoint: byte);
// Converts the floating-point value to string with basic formatting:
//    inum - minimal number of chars before decimal point [0  11],
//    dpoint - number of decimal places [0..5];
//    If inum is larger than the size of the integer part, leading spaces will be added. It may be smaller - the
//    number will be still correctly converted

// ------  string to number -------

function strIsNumber(var Str_in:string{[20]}): boolean;
//  checks whether S consists entirely of chars from ['0'..'9','+','-']; '+' or '-' may appear only as first character.

function Val(var Str_in:string{[11]}; var Res:byte):longint;
//  converts string of length 1-11 to number of any type; Res equals zero if the conversion is successful; if the
//  number is out of dWord type range, or negative limit for Longint, Res=255; if unrecognizable character is
//  spotted, Res will give it's position in the input string (index+1) - this is in practice true only for last char as
//  Val tolerates non-numeric characters preceding number

function str2byte(var Str_in:string{[3]}):byte;
//  converts string of length 1-3 to number of type byte

function str2short(var Str_in:string{[4]}):short;
//  converts string of length 1-4 to number of type short

function str2word(var Str_in:string{[5]}):word;
//  converts string of length 1-5 to number of type word

function str2int(var Str_in:string{[5]}):integer;
//  converts string of length 1-5 to number of type integer

function str2dWord(var Str_in:string{[10]}):dWord;
//  converts string of length 1-10 to number of type str2dWord (this function uses Val, so direct use of the latter
//  is advised)

function str2long(var Str_in:string{[11]}):longint;
//  converts string of length 1-11 to number of type Longint (this function uses Val, so direct use of the latter is
//  advised)

function str2float(var Str_in: string{[17]}): real;
//    converts string of length 1-17 to number of type real.


// ------  other conversions -------

procedure IP2str(var IP_in: array[4] of byte; var Str_out: String{[15]});
//  converts IP address (array[4] of byte) into string. Lenght of Str_out may vary from 7 to 15.
// Str_Out must be minimal string[15]

procedure MAC2str(var MAC_in: array[6] of byte; var Str_out: String{[12]});
//  converts MAC (array[6] of byte) into String[12].
// Str_Out must be minimal string[12]

procedure str2IP(var Str_in: String{[15]}; var IP_out :array[4] of byte);
//  converts Str_in (of length from 7 to 15 and including three '.') to IP address

procedure str2MAC(var Str_in: String{[12]}; var MAC_out: array[6] of byte);
//  converts Str_in (of length 12) to MAC address

{$ENDIF}

// ----------------------- Extra procedures and functions -------------------------

procedure BcdByteToStr(Value: byte; var S: string[2]);
// Converts a byte with a BCD value in it to its string representation
// S must be minimal string[2]

function Hex2dWord(var hex_in: string{[8]}): dWord;
// converts the hex code number in hex_in into dword; accepts both lower and uppercase characters

procedure ByteToStrWithZeros(number: byte; var output: string[3]);
// converts number of type byte to string of 3 characters - zeros fill the string before representation
// of the number
// Output must be minimal string[3]

procedure ShortTostrWithZeros(number:short; var output:string[4]);
// converts number of type short to string of four characters - zeros (with minus sign, if negative) fill the string
// before representation of number
// Output must be minimal string[4]

// ----- Functions added for compatibility reasons with previous version of StrngUtils ------

procedure ByteToHexStr(Value: byte; var S: string[2]);
// Converts a byte to its hexadecimal string representation
// S must be minimal string[2]

procedure WordToHexStr(Value: word; var S: string[4]);
// Converts a word to its hexadecimal string representation
// S must be minimal string[4]

procedure DWordToHexStr(Value: DWord; var S: string[8]);
// Converts a Dword to its hexadecimal string representation
// S must be minimal string[8]

procedure ByteToBinStr(Value: byte; var S: string[8]);
// converts a byte to its binary string representation
// S must be minimal string[8]

procedure WordToBinStr(Value: word; var S: string{[16]});
// converts a word to its binary string representation
// S must be minimal string[16]

procedure DWordToBinStr(Value: dword; var S: string{[32]});
// converts a dword (or longint) to its binary string representation
// S must be minimal string[32]

// ------------ end of compatibility section ------------------

// ---------------------------------------------------------------------------------

implementation