|
@@ -27,66 +27,33 @@ type
|
|
|
pbyte = ^byte;
|
|
|
CharArray = array[0..0] of char;
|
|
|
|
|
|
-{ StrLen returns the length of Str ( terminator not included ) }
|
|
|
+{ Processor dependent part, shared withs strings unit }
|
|
|
+{$i strings.inc }
|
|
|
|
|
|
-function StrLen(Str: PChar): cardinal;
|
|
|
-begin
|
|
|
-result := 0;
|
|
|
-if Str <> nil then begin
|
|
|
- while CharArray(Str^)[result] <> #0 do
|
|
|
- result := result + 1;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrEnd returns a pointer to the last character (terminator) of Str }
|
|
|
-
|
|
|
-function StrEnd(Str: PChar): PChar;
|
|
|
-begin
|
|
|
-result := Str;
|
|
|
-if Str <> nil then begin
|
|
|
- while result^ <> #0 do
|
|
|
- result := result + 1;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
+{ Processor independent part, shared with strings unit }
|
|
|
+{$i stringsi.inc }
|
|
|
|
|
|
-{ StrMove copies Count bytes from source to dest, source and dest may overlap. }
|
|
|
-
|
|
|
-function StrMove(Dest, Source: PChar; Count: cardinal): PChar;
|
|
|
-begin
|
|
|
-result := Dest;
|
|
|
-if (Dest <> nil) and (Source <> nil) and (Count > 0) then
|
|
|
- move(Source^, Dest^, Count);
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrCopy copies StrLen(Source) characters from Source to Dest and returns Dest }
|
|
|
+{ StrPas converts a PChar to a pascal string }
|
|
|
|
|
|
-function StrCopy(Dest, Source: PChar): PChar;
|
|
|
+function StrPas(Str: PChar): string;
|
|
|
begin
|
|
|
-result := StrMove(Dest, Source, 1 + StrLen(Source)); { copy nul character too ! }
|
|
|
+SetLength(result, StrLen(Str));
|
|
|
+Move(Str^, result[1], Length(result));
|
|
|
end ;
|
|
|
|
|
|
-{ StrECopy copies StrLen(Source) characters from Source to Dest and returns StrEnd(Dest) }
|
|
|
+{ StrAlloc allocates a buffer of Size + 4
|
|
|
+ the size of the allocated buffer is stored at result - 4
|
|
|
+ StrDispose should be used to destroy the buffer }
|
|
|
|
|
|
-function StrECopy(Dest, Source: PChar): PChar;
|
|
|
+function StrAlloc(Size: cardinal): PChar;
|
|
|
+var Temp: pointer;
|
|
|
begin
|
|
|
-StrMove(Dest, Source, 1 + StrLen(Source));
|
|
|
-result := StrEnd(Dest);
|
|
|
+GetMem(Temp, Size + SizeOf(cardinal));
|
|
|
+Move(Size, Temp^, SizeOf(cardinal));
|
|
|
+pbyte(Temp + SizeOf(cardinal))^ := 0;
|
|
|
+result := PChar(Temp + SizeOf(cardinal));
|
|
|
end ;
|
|
|
|
|
|
-{ StrLCopy copies MaxLen or less characters from Source to Dest and returns Dest }
|
|
|
-
|
|
|
-function StrLCopy(Dest, Source: PChar; MaxLen: cardinal): PChar;
|
|
|
-var count: cardinal;
|
|
|
-begin
|
|
|
-result := Dest;
|
|
|
-if result <> Nil then begin
|
|
|
- count := StrLen(Source);
|
|
|
- if count > MaxLen then
|
|
|
- count := MaxLen;
|
|
|
- StrMove(Dest, Source, count);
|
|
|
- CharArray(result^)[Count] := #0; { terminate ! }
|
|
|
- end ;
|
|
|
-end ;
|
|
|
|
|
|
{ StrPCopy copies the pascal string Source to Dest and returns Dest }
|
|
|
|
|
@@ -111,216 +78,20 @@ if (Result <> Nil) and (MaxLen <> 0) then begin
|
|
|
end ;
|
|
|
end ;
|
|
|
|
|
|
-{ StrCat concatenates Dest and Source and returns Dest }
|
|
|
-
|
|
|
-function StrCat(Dest, Source: PChar): PChar;
|
|
|
-begin
|
|
|
-result := Dest;
|
|
|
-StrMove(StrEnd(Dest), Source, 1 + StrLen(Source)); { include #0 }
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrLCat concatenates Dest and MaxLen - StrLen(Dest) (or less) characters
|
|
|
- from Source, and returns Dest }
|
|
|
-
|
|
|
-function StrLCat(Dest, Source: PChar; MaxLen: cardinal): PChar;
|
|
|
-var Count: cardinal; P: PChar;
|
|
|
-begin
|
|
|
-result := Dest;
|
|
|
-if (Dest <> nil) and (MaxLen <> 0) then begin
|
|
|
- P := StrEnd(Dest);
|
|
|
- Count := StrLen(Source);
|
|
|
- if Count > MaxLen - (P - Dest) then
|
|
|
- Count := MaxLen - (P - Dest);
|
|
|
- if Count <> 0 then begin
|
|
|
- StrMove(P, Source, Count);
|
|
|
- CharArray(p^)[Count] := #0; { terminate Dest }
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrComp returns 0 if Str1 and Str2 are equal,
|
|
|
- a value less than 0 in case Str1 < Str2
|
|
|
- and a value greater than 0 in case Str1 > Str2 }
|
|
|
-
|
|
|
-function StrComp(Str1, Str2: PChar): integer;
|
|
|
-begin
|
|
|
-result := 0;
|
|
|
-if (Str1 <> Nil) and (Str2 <> Nil) then begin
|
|
|
- while result = 0 do begin
|
|
|
- result := byte(Str1^) - byte(Str2^);
|
|
|
- if (Str1^ = #0) or (Str2^ = #0) then break;
|
|
|
- Str1 := Str1 + 1;
|
|
|
- Str2 := Str2 + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrIComp returns 0 if Str1 and Str2 are equal,
|
|
|
- a value less than 0 in case Str1 < Str2
|
|
|
- and a value greater than 0 in case Str1 > Str2;
|
|
|
- comparison is case insensitive }
|
|
|
-
|
|
|
-function StrIComp(Str1, Str2: PChar): integer;
|
|
|
-var Chr1, Chr2: byte;
|
|
|
-begin
|
|
|
-result := 0;
|
|
|
-if (Str1 <> Nil) and (Str2 <> Nil) then begin
|
|
|
- while result = 0 do begin
|
|
|
- Chr1 := byte(Str1^);
|
|
|
- Chr2 := byte(Str2^);
|
|
|
- if Chr1 in [97..122] then Chr1 := Chr1 - 32;
|
|
|
- if Chr2 in [97..122] then Chr2 := Chr2 - 32;
|
|
|
- result := Chr1 - Chr2;
|
|
|
- if (Chr1 = 0) or (Chr2 = 0) then break;
|
|
|
- Str1 := Str1 + 1;
|
|
|
- Str2 := Str2 + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrLComp returns 0 if Str1 and Str2 are equal,
|
|
|
- a value less than 0 in case Str1 < Str2
|
|
|
- and a value greater than 0 in case Str1 > Str2;
|
|
|
- MaxLen or less characters are compared }
|
|
|
-
|
|
|
-function StrLComp(Str1, Str2: PChar; MaxLen: cardinal): integer;
|
|
|
-var I: integer;
|
|
|
-begin
|
|
|
-result := 0;
|
|
|
-if (Str1 <> Nil) and (Str2 <> Nil) then begin
|
|
|
- I := 0;
|
|
|
- while (I < MaxLen) and (result = 0) do begin
|
|
|
- result := byte(Str1^) - byte(Str2^);
|
|
|
- if (Str1^ = #0) or (Str2^ = #0) then break;
|
|
|
- Str1 := Str1 + 1;
|
|
|
- Str2 := Str2 + 1;
|
|
|
- I := I + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrLIComp returns 0 if Str1 and Str2 are equal,
|
|
|
- a value less than 0 in case Str1 < Str2
|
|
|
- and a value greater than 0 in case Str1 > Str2;
|
|
|
- comparison is case insensitive and MaxLen or less characters are compared }
|
|
|
-
|
|
|
-function StrLIComp(Str1, Str2: PChar; MaxLen: cardinal): integer;
|
|
|
-var Chr1, Chr2: byte; I: integer;
|
|
|
-begin
|
|
|
-result := 0;
|
|
|
-if (Str1 <> Nil) and (Str2 <> Nil) then begin
|
|
|
- I := 0;
|
|
|
- while (I < MaxLen) and (result = 0) do begin
|
|
|
- Chr1 := byte(Str1^);
|
|
|
- Chr2 := byte(Str2^);
|
|
|
- if Chr1 in [97..122] then Chr1 := Chr1 - 32;
|
|
|
- if Chr2 in [97..122] then Chr2 := Chr2 - 32;
|
|
|
- result := Chr1 - Chr2;
|
|
|
- if (Chr1 = 0) or (Chr2 = 0) then break;
|
|
|
- Str1 := Str1 + 1;
|
|
|
- Str2 := Str2 + 1;
|
|
|
- I := I + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrScan returns a PChar to the first character Chr in Str }
|
|
|
-
|
|
|
-function StrScan(Str: PChar; Chr: char): PChar;
|
|
|
-var P: PChar;
|
|
|
-begin
|
|
|
-result := Nil;
|
|
|
-if Str <> Nil then begin
|
|
|
- P := Str;
|
|
|
- while (P^ <> #0) and (P^ <> Chr) do
|
|
|
- P := P + 1;
|
|
|
- if P^ = Chr then result := P;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrRScan returns a PChar to the last character Chr in Str }
|
|
|
-
|
|
|
-function StrRScan(Str: PChar; Chr: char): PChar;
|
|
|
-var P: PChar;
|
|
|
-begin
|
|
|
-result := Nil;
|
|
|
-if Str <> Nil then begin
|
|
|
- P := StrEnd(Str);
|
|
|
- While (P^ <> Chr) and (P <> Str) do
|
|
|
- P := P - 1;
|
|
|
- If P^ = Chr then result := P;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrPos returns a PChar to the first occurance of Str2 contained in Str1
|
|
|
- if no occurance can be found StrPos returns Nil }
|
|
|
-
|
|
|
-function StrPos(Str1, Str2: PChar): PChar;
|
|
|
-var E: PChar; Count1, Count2: Cardinal;
|
|
|
-begin
|
|
|
-Count1 := StrLen(Str1);
|
|
|
-Count2 := StrLen(Str2);
|
|
|
-if (Str1 <> Nil) and (Str2 <> Nil) and (Count1 > 0) and (Count1 >= Count2) then begin
|
|
|
- E := Str1 + 1 + Count1 - Count2;
|
|
|
- result := Str1;
|
|
|
- While result <> E do begin
|
|
|
- if StrLComp(result, Str2, Count2) = 0 then
|
|
|
- exit;
|
|
|
- result := result + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-result := Nil;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrUpper converts all lowercase characters in Str to uppercase }
|
|
|
-
|
|
|
-function StrUpper(Str: PChar): PChar;
|
|
|
-begin
|
|
|
-Result := Str;
|
|
|
-if Str <> Nil then begin
|
|
|
- While Str^ <> #0 do begin
|
|
|
- if Str^ in ['a'..'z'] then
|
|
|
- dec(byte(Str^), 32);
|
|
|
- Str := Str + 1;
|
|
|
- end ;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
|
|
|
-{ StrLower converts all uppercase characters in Str to lowercase }
|
|
|
+{ StrDispose clears the memory allocated with StrAlloc }
|
|
|
|
|
|
-function StrLower(Str: PChar): PChar;
|
|
|
+procedure StrDispose(var Str: PChar);
|
|
|
+var Size: cardinal;
|
|
|
begin
|
|
|
-Result := Str;
|
|
|
-if Str <> Nil then begin
|
|
|
- While Str^ <> #0 do begin
|
|
|
- if Str^ in ['A'..'Z'] then
|
|
|
- inc(byte(Str^), 32);
|
|
|
- Str := Str + 1;
|
|
|
- end ;
|
|
|
+if (Str <> Nil) then begin
|
|
|
+ Str := PChar(Str - SizeOf(cardinal));
|
|
|
+ Move(Str^, Size, SizeOf(cardinal));
|
|
|
+ FreeMem(Str, Size + SizeOf(cardinal));
|
|
|
+ Str := Nil;
|
|
|
end ;
|
|
|
end ;
|
|
|
|
|
|
-{ StrPas converts a PChar to a pascal string }
|
|
|
-
|
|
|
-function StrPas(Str: PChar): string;
|
|
|
-begin
|
|
|
-SetLength(result, StrLen(Str));
|
|
|
-Move(Str^, result[1], Length(result));
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrAlloc allocates a buffer of Size + 4
|
|
|
- the size of the allocated buffer is stored at result - 4
|
|
|
- StrDispose should be used to destroy the buffer }
|
|
|
-
|
|
|
-function StrAlloc(Size: cardinal): PChar;
|
|
|
-var Temp: pointer;
|
|
|
-begin
|
|
|
-GetMem(Temp, Size + SizeOf(cardinal));
|
|
|
-Move(Size, Temp^, SizeOf(cardinal));
|
|
|
-pbyte(Temp + SizeOf(cardinal))^ := 0;
|
|
|
-result := PChar(Temp + SizeOf(cardinal));
|
|
|
-end ;
|
|
|
-
|
|
|
{ StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
|
|
|
|
|
|
function StrBufSize(var Str: PChar): cardinal;
|
|
@@ -331,33 +102,12 @@ else
|
|
|
result := 0;
|
|
|
end ;
|
|
|
|
|
|
-{ StrNew creates an exact copy of Str }
|
|
|
-
|
|
|
-function StrNew(Str: PChar): PChar;
|
|
|
-begin
|
|
|
-if Str <> Nil then begin
|
|
|
- result := StrAlloc(1 + StrLen(Str));
|
|
|
- StrCopy(result, Str);
|
|
|
- end
|
|
|
-else result := Nil;
|
|
|
-end ;
|
|
|
-
|
|
|
-{ StrDispose clears the memory allocated with StrAlloc }
|
|
|
-
|
|
|
-procedure StrDispose(var Str: PChar);
|
|
|
-var Size: cardinal;
|
|
|
-begin
|
|
|
-if (Str <> Nil) then begin
|
|
|
- Str := PChar(Str - SizeOf(cardinal));
|
|
|
- Move(Str^, Size, SizeOf(cardinal));
|
|
|
- FreeMem(Str, Size + SizeOf(cardinal));
|
|
|
- Str := Nil;
|
|
|
- end ;
|
|
|
-end ;
|
|
|
-
|
|
|
{
|
|
|
$Log$
|
|
|
- Revision 1.3 1999-02-10 22:15:11 michael
|
|
|
+ Revision 1.4 1999-02-25 07:39:57 michael
|
|
|
+ * Joined strings and sysutils
|
|
|
+
|
|
|
+ Revision 1.3 1999/02/10 22:15:11 michael
|
|
|
+ Changed to ansistrings
|
|
|
|
|
|
Revision 1.2 1998/09/16 08:28:40 michael
|