/[projects]/dao/DelphiScanner/Components/tpsystools_4.04/source/StStrL.pas
ViewVC logotype

Annotation of /dao/DelphiScanner/Components/tpsystools_4.04/source/StStrL.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2671 - (hide annotations) (download)
Tue Aug 25 18:15:15 2015 UTC (8 years, 10 months ago) by torben
File size: 102949 byte(s)
Added tpsystools component
1 torben 2671 // TODO-UNICODE
2    
3     (* ***** BEGIN LICENSE BLOCK *****
4     * Version: MPL 1.1
5     *
6     * The contents of this file are subject to the Mozilla Public License Version
7     * 1.1 (the "License"); you may not use this file except in compliance with
8     * the License. You may obtain a copy of the License at
9     * http://www.mozilla.org/MPL/
10     *
11     * Software distributed under the License is distributed on an "AS IS" basis,
12     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13     * for the specific language governing rights and limitations under the
14     * License.
15     *
16     * The Original Code is TurboPower SysTools
17     *
18     * The Initial Developer of the Original Code is
19     * TurboPower Software
20     *
21     * Portions created by the Initial Developer are Copyright (C) 1996-2002
22     * the Initial Developer. All Rights Reserved.
23     *
24     * Contributor(s):
25     *
26     * ***** END LICENSE BLOCK ***** *)
27    
28     {*********************************************************}
29     {* SysTools: StStrL.pas 4.04 *}
30     {*********************************************************}
31     {* SysTools: Long string routines *}
32     {*********************************************************}
33    
34     {$I StDefine.inc}
35    
36     unit StStrL;
37    
38     interface
39    
40     uses
41     Windows,
42     Classes,
43     SysUtils,
44     StConst,
45     StBase;
46    
47     {.Z+}
48     type
49     LStrRec = record
50     AllocSize : Longint;
51     RefCount : Longint;
52     Length : Longint;
53     end;
54    
55     const
56     StrOffset = SizeOf(LStrRec);
57     {.Z-}
58    
59     {-------- Numeric conversion -----------}
60    
61     function HexBL(B : Byte) : AnsiString;
62     {-Return the hex string for a byte.}
63    
64     function HexWL(W : Word) : AnsiString;
65     {-Return the hex string for a word.}
66    
67     function HexLL(L : LongInt) : AnsiString;
68     {-Return the hex string for a long integer.}
69    
70     function HexPtrL(P : Pointer) : AnsiString;
71     {-Return the hex string for a pointer.}
72    
73     function BinaryBL(B : Byte) : AnsiString;
74     {-Return a binary string for a byte.}
75    
76     function BinaryWL(W : Word) : AnsiString;
77     {-Return the binary string for a word.}
78    
79     function BinaryLL(L : LongInt) : AnsiString;
80     {-Return the binary string for a long integer.}
81    
82     function OctalBL(B : Byte) : AnsiString;
83     {-Return an octal string for a byte.}
84    
85     function OctalWL(W : Word) : AnsiString;
86     {-Return an octal string for a word.}
87    
88     function OctalLL(L : LongInt) : AnsiString;
89     {-Return an octal string for a long integer.}
90    
91     function Str2Int16L(const S : AnsiString; var I : SmallInt) : Boolean;
92     {-Convert a string to an SmallInt.}
93    
94     function Str2WordL(const S : AnsiString; var I : Word) : Boolean;
95     {-Convert a string to a word.}
96    
97     function Str2LongL(const S : AnsiString; var I : LongInt) : Boolean;
98     {-Convert a string to a long integer.}
99    
100     {$IFDEF VER93}
101     function Str2RealL(const S : AnsiString; var R : Double) : Boolean;
102     {$ELSE}
103     function Str2RealL(const S : AnsiString; var R : Real) : Boolean;
104     {-Convert a string to a real.}
105     {$ENDIF}
106    
107     function Str2ExtL(const S : AnsiString; var R : Extended) : Boolean;
108     {-Convert a string to an extended.}
109    
110     function Long2StrL(L : LongInt) : String;
111     {-Convert an integer type to a string.}
112    
113     function Real2StrL(R : Double; Width : Byte; Places : ShortInt) : String;
114     {-Convert a real to a string.}
115    
116     function Ext2StrL(R : Extended; Width : Byte; Places : ShortInt) : String;
117     {-Convert an extended to a string.}
118    
119     function ValPrepL(const S : String) : String;
120     {-Prepares a string for calling Val.}
121    
122     {-------- General purpose string manipulation --------}
123    
124     function CharStrL(C : Char; Len : Cardinal) : String;
125     {-Return a string filled with the specified character.}
126    
127     function PadChL(const S : String; C : Char; Len : Cardinal) : String;
128     {-Pad a string on the right with a specified character.}
129    
130     function PadL(const S : String; Len : Cardinal) : String;
131     {-Pad a string on the right with spaces.}
132    
133     function LeftPadChL(const S : String; C : Char; Len : Cardinal) : String;
134     {-Pad a string on the left with a specified character.}
135    
136     function LeftPadL(const S : String; Len : Cardinal) : String;
137     {-Pad a string on the left with spaces.}
138    
139     function TrimLeadL(const S : String) : String;
140     {-Return a string with leading white space removed.}
141    
142     function TrimTrailL(const S : String) : String;
143     {-Return a string with trailing white space removed.}
144    
145     function TrimL(const S : String) : String;
146     {-Return a string with leading and trailing white space removed.}
147    
148     function TrimSpacesL(const S : String) : String;
149     {-Return a string with leading and trailing spaces removed.}
150    
151     function CenterChL(const S : String; C : Char; Len : Cardinal) : String;
152     {-Pad a string on the left and right with a specified character.}
153    
154     function CenterL(const S : String; Len : Cardinal) : String;
155     {-Pad a string on the left and right with spaces.}
156    
157     function EntabL(const S : AnsiString; TabSize : Byte) : AnsiString;
158     {-Convert blanks in a string to tabs.}
159    
160     function DetabL(const S : AnsiString; TabSize : Byte) : AnsiString;
161     {-Expand tabs in a string to blanks.}
162    
163     function ScrambleL(const S, Key : AnsiString) : AnsiString;
164     {-Encrypt / Decrypt string with enhanced XOR encryption.}
165    
166     function SubstituteL(const S, FromStr, ToStr : String) : String;
167     {-Map the characters found in FromStr to the corresponding ones in ToStr.}
168    
169     function FilterL(const S, Filters : String) : String;
170     {-Remove characters from a string. The characters to remove are specified in
171     ChSet.}
172    
173     {--------------- Word / Char manipulation -------------------------}
174    
175     function CharExistsL(const S : String; C : Char) : Boolean;
176     {-Determine whether a given character exists in a string. }
177    
178     function CharCountL(const S : String; C : Char) : Cardinal;
179     {-Count the number of a given character in a string. }
180    
181     function WordCountL(const S, WordDelims : String) : Cardinal;
182     {-Given an array of word delimiters, return the number of words in a string.}
183    
184     function WordPositionL(N : Cardinal; const S, WordDelims : String;
185     var Pos : Cardinal) : Boolean;
186     {-Given an array of word delimiters, set Pos to the start position of the
187     N'th word in a string. Result indicates success/failure.}
188    
189     function ExtractWordL(N : Cardinal; const S, WordDelims : String) : String;
190     {-Given an array of word delimiters, return the N'th word in a string.}
191    
192     function AsciiCountL(const S, WordDelims : String; Quote : Char) : Cardinal;
193     {-Return the number of words in a string.}
194    
195     function AsciiPositionL(N : Cardinal; const S, WordDelims : String;
196     Quote : Char; var Pos : Cardinal) : Boolean;
197     {-Return the position of the N'th word in a string.}
198    
199     function ExtractAsciiL(N : Cardinal; const S, WordDelims : String;
200     Quote : Char) : String;
201     {-Given an array of word delimiters, return the N'th word in a string. Any
202     text within Quote characters is counted as one word.}
203    
204     procedure WordWrapL(const InSt : String; var OutSt, Overlap : String;
205     Margin : Cardinal; PadToMargin : Boolean);
206     {-Wrap a text string at a specified margin.}
207    
208     {--------------- String comparison and searching -----------------}
209     function CompStringL(const S1, S2 : String) : Integer;
210     {-Compare two strings.}
211    
212     function CompUCStringL(const S1, S2 : String) : Integer;
213     {-Compare two strings. This compare is not case sensitive.}
214    
215     function SoundexL(const S : AnsiString) : AnsiString;
216     {-Return 4 character soundex of an input string.}
217    
218     function MakeLetterSetL(const S : AnsiString) : Longint;
219     {-Return a bit-mapped long storing the individual letters contained in S.}
220    
221     {$IFDEF UNICODE}
222     procedure BMMakeTableL(const MatchString : UnicodeString; var BT : BTable); overload;
223     {$ELSE}
224     procedure BMMakeTableL(const MatchString : AnsiString; var BT : BTable); overload;
225     {$ENDIF}
226     {-Build a Boyer-Moore link table}
227    
228     {$IFDEF UNICODE}
229     function BMSearchL(var Buffer; BufLength: Cardinal; var BT: BTable;
230     const MatchString : String; out Pos : Cardinal) : Boolean; overload;
231     {$ELSE}
232     function BMSearchL(var Buffer; BufLength : Cardinal; var BT : BTable;
233     const MatchString : AnsiString; var Pos : Cardinal) : Boolean; overload;
234     {$ENDIF}
235     {-Use the Boyer-Moore search method to search a buffer for a string.}
236    
237     {$IFDEF UNICODE}
238     function BMSearchUCL(var Buffer; BufLength : Cardinal; var BT : BTable;
239     const MatchString : String ; var Pos : Cardinal) : Boolean;
240     {$ELSE}
241     function BMSearchUCL(var Buffer; BufLength : Cardinal; var BT : BTable;
242     const MatchString : AnsiString ; var Pos : Cardinal) : Boolean;
243     {$ENDIF}
244     {-Use the Boyer-Moore search method to search a buffer for a string. This
245     search is not case sensitive.}
246    
247     {--------------- DOS pathname parsing -----------------}
248    
249     function DefaultExtensionL(const Name, Ext : String) : String;
250     {-Return a file name with a default extension attached.}
251    
252     function ForceExtensionL(const Name, Ext : String) : String;
253     {-Force the specified extension onto the file name.}
254    
255     function JustFilenameL(const PathName : String) : String;
256     {-Return just the filename and extension of a pathname.}
257    
258     function JustNameL(const PathName : String) : String;
259     {-Return just the filename (no extension, path, or drive) of a pathname.}
260    
261     function JustExtensionL(const Name : String) : String;
262     {-Return just the extension of a pathname.}
263    
264     function JustPathnameL(const PathName : String) : String;
265     {-Return just the drive and directory portion of a pathname.}
266    
267     function AddBackSlashL(const DirName : String) : String;
268     {-Add a default backslash to a directory name.}
269    
270     function CleanPathNameL(const PathName : String) : String;
271     {-Return a pathname cleaned up as DOS does it.}
272    
273     function HasExtensionL(const Name : String; var DotPos : Cardinal) : Boolean;
274     {-Determine if a pathname contains an extension and, if so, return the
275     position of the dot in front of the extension.}
276    
277     {------------------ Formatting routines --------------------}
278    
279     function CommaizeL(L : LongInt) : String;
280     {-Convert a long integer to a string with commas.}
281    
282     function CommaizeChL(L : Longint; Ch : Char) : String;
283     {-Convert a long integer to a string with Ch in comma positions.}
284    
285     function FloatFormL(const Mask : String ; R : TstFloat ; const LtCurr,
286     RtCurr : String ; Sep, DecPt : Char) : String;
287     {-Return a formatted string with digits from R merged into mask.}
288    
289     function LongIntFormL(const Mask : String ; L : LongInt ; const LtCurr,
290     RtCurr : String ; Sep : Char) : String;
291     {-Return a formatted string with digits from L merged into mask.}
292    
293     function StrChPosL(const P : String; C : Char; var Pos : Cardinal) : Boolean;
294     {-Return the position of a specified character within a string.}
295    
296     function StrStPosL(const P, S : String; var Pos : Cardinal) : Boolean;
297     {-Return the position of a specified substring within a string.}
298    
299     function StrStCopyL(const S : String; Pos, Count : Cardinal) : String;
300     {-Copy characters at a specified position in a string.}
301    
302     function StrChInsertL(const S : String; C : Char; Pos : Cardinal) : String;
303     {-Insert a character into a string at a specified position.}
304    
305     function StrStInsertL(const S1, S2 : String; Pos : Cardinal) : String;
306     {-Insert a string into another string at a specified position.}
307    
308     function StrChDeleteL(const S : String; Pos : Cardinal) : String;
309     {-Delete the character at a specified position in a string.}
310    
311     function StrStDeleteL(const S : String; Pos, Count : Cardinal) : String;
312     {-Delete characters at a specified position in a string.}
313    
314    
315     {-------------------------- New Functions -----------------------------------}
316    
317     function ContainsOnlyL(const S, Chars : String;
318     var BadPos : Cardinal) : Boolean;
319    
320     function ContainsOtherThanL(const S, Chars : String;
321     var BadPos : Cardinal) : Boolean;
322    
323     function CopyLeftL(const S : String; Len : Cardinal) : String;
324     {-Return the left Len characters of a string}
325    
326     function CopyMidL(const S : String; First, Len : Cardinal) : String;
327     {-Return the mid part of a string}
328    
329     function CopyRightL(const S : String; First : Cardinal) : String;
330     {-Return the right Len characters of a string}
331    
332     function CopyRightAbsL(const S : String; NumChars : Cardinal) : String;
333     {-Return NumChar characters starting from end}
334    
335     function CopyFromNthWordL(const S, WordDelims : String;
336     const AWord : String; N : Cardinal; {!!.02}
337     var SubString : String) : Boolean;
338    
339     function CopyFromToWordL(const S, WordDelims, Word1, Word2 : String;
340     N1, N2 : Cardinal;
341     var SubString : String) : Boolean;
342    
343     function CopyWithinL(const S, Delimiter : String;
344     Strip : Boolean) : String;
345    
346     function DeleteFromNthWordL(const S, WordDelims : String;
347     const AWord : String; N : Cardinal; {!!.02}
348     var SubString : String) : Boolean;
349    
350     function DeleteFromToWordL(const S, WordDelims, Word1, Word2 : String;
351     N1, N2 : Cardinal;
352     var SubString : String) : Boolean;
353    
354     function DeleteWithinL(const S, Delimiter : String) : String;
355    
356     function ExtractTokensL(const S, Delims: String;
357     QuoteChar : Char;
358     AllowNulls : Boolean;
359     Tokens : TStrings) : Cardinal;
360    
361     function IsChAlphaL(C : Char) : Boolean;
362     {-Returns true if Ch is an alpha}
363    
364     function IsChNumericL(C : Char; const Numbers : String) : Boolean; {!!.02}
365     {-Returns true if Ch in numeric set}
366    
367     function IsChAlphaNumericL(C : Char; const Numbers : String) : Boolean; {!!.02}
368     {-Returns true if Ch is an alpha or numeric}
369    
370     function IsStrAlphaL(const S : String) : Boolean;
371     {-Returns true if all characters in string are an alpha}
372    
373     function IsStrNumericL(const S, Numbers : String) : Boolean;
374     {-Returns true if all characters in string are in numeric set}
375    
376     function IsStrAlphaNumericL(const S, Numbers : String) : Boolean;
377     {-Returns true if all characters in string are alpha or numeric}
378    
379     function KeepCharsL(const S, Chars : String) : String;
380    
381     function LastWordL(const S, WordDelims, AWord : String;
382     var Position : Cardinal) : Boolean;
383    
384     function LastWordAbsL(const S, WordDelims : String;
385     var Position : Cardinal) : Boolean;
386    
387     function LastStringL(const S, AString : String;
388     var Position : Cardinal) : Boolean;
389    
390     function LeftTrimCharsL(const S, Chars : String) : String;
391    
392     function ReplaceWordL(const S, WordDelims, OldWord, NewWord : String;
393     N : Cardinal;
394     var Replacements : Cardinal) : String;
395    
396     function ReplaceWordAllL(const S, WordDelims, OldWord, NewWord : String;
397     var Replacements : Cardinal) : String;
398    
399     function ReplaceStringL(const S, OldString, NewString : String;
400     N : Cardinal;
401     var Replacements : Cardinal) : String;
402    
403     function ReplaceStringAllL(const S, OldString, NewString : String;
404     var Replacements : Cardinal) : String;
405    
406     function RepeatStringL(const RepeatString : String;
407     var Repetitions : Cardinal;
408     MaxLen : Cardinal) : String;
409    
410     function RightTrimCharsL(const S, Chars : String) : String;
411    
412     function StrWithinL(const S, SearchStr : string;
413     Start : Cardinal;
414     var Position : Cardinal) : boolean;
415     {-finds the position of a substring within a string starting at a given point}
416    
417     function TrimCharsL(const S, Chars : String) : String;
418    
419     function WordPosL(const S, WordDelims, AWord : String;
420     N : Cardinal; var Position : Cardinal) : Boolean;
421     {-returns the Occurrence instance of a word within a string}
422    
423    
424     implementation
425    
426     {-------- Numeric conversion -----------}
427    
428     function HexBL(B : Byte) : AnsiString;
429     {-Return the hex string for a byte.}
430     begin
431     SetLength(Result, 2);
432     Result[1] := StHexDigits[B shr 4];
433     Result[2] := StHexDigits[B and $F];
434     end;
435    
436     function HexWL(W : Word) : AnsiString;
437     {-Return the hex string for a word.}
438     begin
439     SetLength(Result, 4);
440     Result[1] := StHexDigits[hi(W) shr 4];
441     Result[2] := StHexDigits[hi(W) and $F];
442     Result[3] := StHexDigits[lo(W) shr 4];
443     Result[4] := StHexDigits[lo(W) and $F];
444     end;
445    
446     function HexLL(L : LongInt) : AnsiString;
447     {-Return the hex string for a long integer.}
448     begin
449     SetLength(Result, 8);
450     Result := HexWL(HiWord(DWORD(L))) + HexWL(LoWord(DWORD(L))); {!!.02}
451     end;
452    
453     function HexPtrL(P : Pointer) : AnsiString;
454     {-Return the hex string for a pointer.}
455     begin
456     SetLength(Result, 9);
457     Result := ':' + HexLL(LongInt(P));
458     end;
459    
460     function BinaryBL(B : Byte) : AnsiString;
461     {-Return a binary string for a byte.}
462     var
463     I, N : Word;
464     begin
465     N := 1;
466     SetLength(Result, 8);
467     for I := 7 downto 0 do begin
468     Result[N] := StHexDigits[Ord(B and (1 shl I) <> 0)]; {0 or 1}
469     Inc(N);
470     end;
471     end;
472    
473     function BinaryWL(W : Word) : AnsiString;
474     {-Return the binary string for a word.}
475     var
476     I, N : Word;
477     begin
478     N := 1;
479     SetLength(Result, 16);
480     for I := 15 downto 0 do begin
481     Result[N] := StHexDigits[Ord(W and (1 shl I) <> 0)]; {0 or 1}
482     Inc(N);
483     end;
484     end;
485    
486     function BinaryLL(L : LongInt) : AnsiString;
487     {-Return the binary string for a long integer.}
488     var
489     I : Longint;
490     N : Byte;
491     begin
492     N := 1;
493     SetLength(Result, 32);
494     for I := 31 downto 0 do begin
495     Result[N] := StHexDigits[Ord(L and LongInt(1 shl I) <> 0)]; {0 or 1}
496     Inc(N);
497     end;
498     end;
499    
500     function OctalBL(B : Byte) : AnsiString;
501     {-Return an octal string for a byte.}
502     var
503     I : Word;
504     begin
505     SetLength(Result, 3);
506     for I := 0 to 2 do begin
507     Result[3-I] := StHexDigits[B and 7];
508     B := B shr 3;
509     end;
510     end;
511    
512     function OctalWL(W : Word) : AnsiString;
513     {-Return an octal string for a word.}
514     var
515     I : Word;
516     begin
517     SetLength(Result, 6);
518     for I := 0 to 5 do begin
519     Result[6-I] := StHexDigits[W and 7];
520     W := W shr 3;
521     end;
522     end;
523    
524     function OctalLL(L : LongInt) : AnsiString;
525     {-Return an octal string for a long integer.}
526     var
527     I : Word;
528     begin
529     SetLength(Result, 12);
530     for I := 0 to 11 do begin
531     Result[12-I] := StHexDigits[L and 7];
532     L := L shr 3;
533     end;
534     end;
535    
536     function Str2Int16L(const S : AnsiString; var I : SmallInt) : Boolean;
537     {-Convert a string to an SmallInt.}
538    
539     var
540     ec : Integer;
541     begin
542     {note the automatic string conversion}
543     ValSmallint(S, I, ec);
544     if (ec = 0) then
545     Result := true
546     else begin
547     Result := false;
548     if (ec < 0) then
549     I := succ(length(S))
550     else
551     I := ec;
552     end;
553     end;
554    
555     function Str2WordL(const S : AnsiString; var I : Word) : Boolean;
556     {-Convert a string to a word.}
557    
558     var
559     ec : Integer;
560     begin
561     {note the automatic string conversion}
562     ValWord(S, I, ec);
563     if (ec = 0) then
564     Result := true
565     else begin
566     Result := false;
567     if (ec < 0) then
568     I := succ(length(S))
569     else
570     I := ec;
571     end;
572     end;
573    
574     function Str2LongL(const S : AnsiString; var I : LongInt) : Boolean;
575     {-Convert a string to a long integer.}
576    
577     var
578     ec : Integer;
579     begin
580     {note the automatic string conversion}
581     ValLongint(S, I, ec);
582     if (ec = 0) then
583     Result := true
584     else begin
585     Result := false;
586     if (ec < 0) then
587     I := succ(length(S))
588     else
589     I := ec;
590     end;
591     end;
592    
593     {$IFDEF VER93}
594     function Str2RealL(const S : AnsiString; var R : Double) : Boolean;
595     {$ELSE}
596     function Str2RealL(const S : AnsiString; var R : Real) : Boolean;
597     {$ENDIF}
598     {-Convert a string to a real.}
599     var
600     Code : Integer;
601     St : AnsiString;
602     begin
603     Result := False;
604     if S = '' then Exit;
605     St := TrimTrailL(S);
606     if St = '' then Exit;
607     Val(ValPrepL(St), R, Code);
608     if Code <> 0 then begin
609     R := Code;
610     end else
611     Result := True;
612     end;
613    
614     function Str2ExtL(const S : AnsiString; var R : Extended) : Boolean;
615     {-Convert a string to an extended.}
616     var
617     Code : Integer;
618     P : AnsiString;
619     begin
620     Result := False;
621     if S = '' then Exit;
622     P := TrimTrailL(S);
623     if P = '' then Exit;
624     Val(ValPrepL(P), R, Code);
625     if Code <> 0 then begin
626     R := Code - 1;
627     end else
628     Result := True;
629     end;
630    
631     function Long2StrL(L : LongInt) : String;
632     {-Convert an integer type to a string.}
633     begin
634     Str(L, Result);
635     end;
636    
637     function Real2StrL(R : Double; Width : Byte; Places : ShortInt) : String;
638     {-Convert a real to a string.}
639     begin
640     Str(R:Width:Places, Result);
641     end;
642    
643     function Ext2StrL(R : Extended; Width : Byte; Places : ShortInt) : String;
644     {-Convert an extended to a string.}
645     begin
646     Str(R:Width:Places, Result);
647     end;
648    
649     function ValPrepL(const S : String) : String;
650     {-Prepares a string for calling Val.}
651     var
652     P : Cardinal;
653     C : Longint;
654     begin
655     Result := TrimSpacesL(S);
656     if Result <> '' then begin
657     if StrChPosL(Result, {$IFDEF DELPHIXE2}FormatSettings.{$ENDIF}DecimalSeparator, P) then begin
658     C := P;
659     Result[C] := '.';
660     if C = Length(Result) then
661     SetLength(Result, Pred(C));
662     end;
663     end else
664     Result := '0';
665     end;
666    
667     {-------- General purpose string manipulation --------}
668    
669     function CharStrL(C : Char; Len : Cardinal) : String;
670     {-Return a string filled with the specified character.}
671     begin
672     Result := StringOfChar(C, Len)
673     end;
674    
675     function PadChL(const S : String; C : Char; Len : Cardinal) : String;
676     {-Pad a string on the right with a specified character.}
677     {$IFDEF UNICODE}
678     begin
679     Result := S;
680     if Length(Result) < Len then
681     Result := Result + StringOfChar(C, Len - Length(Result));
682     end;
683     {$ELSE}
684     begin
685     if Length(S) >= LongInt(Len) then
686     Result := S
687     else begin
688     SetLength(Result, Len);
689     { copy current contents (if any) of S to Result }
690     if (Length(S) > 0) then {!!.01}
691     Move(S[1], Result[1], Length(S));
692    
693     { add pad chars }
694     FillChar(Result[Succ(Length(S))], LongInt(Len)-Length(S), C);
695     end;
696     end;
697     {$ENDIF}
698    
699     function PadL(const S : String; Len : Cardinal) : String;
700     {-Pad a string on the right with spaces.}
701     begin
702     Result := PadChL(S, ' ', Len);
703     end;
704    
705     function LeftPadChL(const S : String; C : Char; Len : Cardinal) : String;
706     {-Pad a string on the left with a specified character.}
707     begin
708     {$IFDEF UNICODE}
709     if Length(S) > Len then
710     Result := S
711     else
712     Result := StringOfChar(C, Len - Length(S)) + S;
713     {$ELSE}
714     if Length(S) >= LongInt(Len) then
715     Result := S
716     else if Length(S) < MaxLongInt then begin
717     SetLength(Result, Len);
718    
719     { copy current contents (if any) of S to Result }
720     if (Length(S) > 0) then {!!.01}
721     Move(S[1], Result[Succ(Word(Len))-Length(S)], Length(S));
722    
723     { add pad chars }
724     FillChar(Result[1], LongInt(Len)-Length(S), C);
725     end;
726     {$ENDIF}
727     end;
728    
729     function LeftPadL(const S : String; Len : Cardinal) : String;
730     {-Pad a string on the left with spaces.}
731     begin
732     Result := LeftPadChL(S, ' ', Len);
733     end;
734    
735     function TrimLeadL(const S : String) : String;
736     {-Return a string with leading white space removed}
737     begin
738     Result := TrimLeft(S);
739     end;
740    
741     function TrimTrailL(const S : String) : String;
742     {-Return a string with trailing white space removed.}
743     begin
744     Result := TrimRight(S);
745     end;
746    
747     function TrimL(const S : String) : String;
748     {-Return a string with leading and trailing white space removed.}
749     var
750     I : Longint;
751     begin
752     Result := S;
753     while (Length(Result) > 0) and (Result[Length(Result)] <= ' ') do
754     SetLength(Result, Pred(Length(Result)));
755    
756     I := 1;
757     while (I <= Length(Result)) and (Result[I] <= ' ') do
758     Inc(I);
759     Dec(I);
760     if I > 0 then
761     System.Delete(Result, 1, I);
762     end;
763    
764     function TrimSpacesL(const S : String) : String;
765     {-Return a string with leading and trailing spaces removed.}
766     var
767     I : Longint;
768     begin
769     Result := S;
770     while (Length(Result) > 0) and (Result[Length(Result)] = ' ') do
771     SetLength(Result, Pred(Length(Result)));
772     I := 1;
773     while (I <= Length(Result)) and (S[I] = ' ') do
774     Inc(I);
775     Dec(I);
776     if I > 0 then
777     System.Delete(Result, 1, I);
778     end;
779    
780     function CenterChL(const S : String; C : Char; Len : Cardinal) : String;
781     {-Pad a string on the left and right with a specified character.}
782     begin
783     if Length(S) >= LongInt(Len) then
784     Result := S
785     else if Length(S) < MaxLongInt then begin
786     // SetLength(Result, Len);
787     // FillChar(Result[1], Len, C);
788     Result := StringOfChar(C, Len);
789     if Length(S) > 0 then {!!.01}
790     Move(S[1], Result[Succ((LongInt(Len)-Length(S)) shr 1)], Length(S)*SizeOf(Char));
791     end;
792     end;
793    
794     function CenterL(const S : String; Len : Cardinal) : String;
795     {-Pad a string on the left and right with spaces.}
796     begin
797     Result := CenterChL(S, ' ', Len);
798     end;
799    
800     function EntabL(const S : AnsiString; TabSize : Byte) : AnsiString; //TODO-UNICODE
801     {-Convert blanks in a string to tabs.}
802     var
803     InLen, OutLen : Cardinal;
804     begin
805     if S = '' then Exit;
806     InLen := Length(S);
807     OutLen := 0;
808     SetLength(Result, InLen);
809     asm
810     push ebx { Save registers }
811     push edi
812     push esi
813    
814     mov edi, [Result]
815     mov edi, [edi]
816     xor ecx, ecx
817     add cl, TabSize
818     jz @@Done
819    
820     mov esi, S
821     xor ebx, ebx { Zero EBX and EDX }
822     xor edx, edx
823     inc edx { Set output length to 1 }
824    
825     @@Next:
826     or ebx, ebx
827     je @@NoTab { Jump to NoTab if spacecount is zero }
828     mov eax, edx { IPos to EAX }
829     push edx
830     xor edx, edx
831     div ecx
832     cmp edx, 1 { Is mod = 1? }
833     pop edx
834     jne @@NoTab { If not, no tab }
835    
836     sub edi, ebx
837     sub OutLen, ebx
838     inc OutLen
839     xor ebx, ebx { Reset spacecount }
840     mov byte ptr [edi], 9h { Store a tab }
841     inc edi
842    
843     @@NoTab:
844     mov al, [esi] { Get next input character }
845     inc esi
846     cmp edx, InLen { End of string? }
847     jg @@Done { Yes, done }
848     inc ebx { Increment SpaceCount }
849     cmp al, 20h { Is character a space? }
850     jz @@Store { Yes, store it for now }
851     xor ebx, ebx { Reset SpaceCount }
852     cmp al, 27h { Is it a quote? }
853     jz @@Quotes { Yep, enter quote loop }
854     cmp al, 22h { Is it a doublequote? }
855     jnz @@Store { Nope, store it }
856    
857     @@Quotes:
858     mov ah, al { Save quote start }
859    
860     @@NextQ:
861     mov [edi], al { Store quoted character }
862     inc edi
863     inc OutLen
864     mov al, [esi] { Get next character }
865     inc esi
866     inc edx { Increment Ipos }
867    
868     cmp edx, ecx { At end of line? }
869     jae @@Store { If so, exit quote loop }
870    
871     cmp al, ah { Matching end quote? }
872     jnz @@NextQ { Nope, stay in quote loop }
873    
874     cmp al, 27h { Single quote? }
875     jz @@Store { Exit quote loop }
876    
877     cmp byte ptr [esi-2],'\' { Previous character an escape? }
878     jz @@NextQ { Stay in if so }
879    
880     @@Store:
881     mov [edi], al { Store last character }
882     inc edi
883     inc OutLen
884     inc edx { Increment input position }
885     jmp @@Next { Repeat while characters left }
886    
887     @@Done:
888     mov byte ptr [edi], 0h
889     pop esi
890     pop edi
891     pop ebx
892     end;
893     SetLength(Result, OutLen);
894     end;
895    
896     function DetabL(const S : AnsiString; TabSize : Byte) : AnsiString; //TODO-UNICODE
897     {-Expand tabs in a string to blanks.}
898     var
899     NumTabs : Integer;
900     begin
901     Result := '';
902     if S = '' then Exit;
903     if TabSize = 0 then Exit;
904     Result := S;
905     NumTabs := CharCountL(S, #9);
906     if NumTabs = 0 then Exit;
907     SetLength(Result, Length(Result)+NumTabs*(Pred(TabSize)));
908     asm
909     push ebx { Save registers since we'll be changing them. }
910     push edi
911     push esi
912    
913     mov edi, Result { EDI => output string. }
914     mov esi, S { ESI => input string. }
915     xor ebx, ebx
916     mov bl, TabSize
917     mov edi, [edi]
918     xor ecx, ecx { Default input length = 0. }
919     xor edx, edx { Zero EDX for output length }
920     xor eax, eax { Zero EAX }
921     mov ecx, [esi-StrOffset].LStrRec.Length { Get input length. }
922     or ebx, ebx { TabSize = 0? }
923     jnz @@DefLength
924     mov ecx, edx { Return zero length string if TabSize = 0. }
925    
926     @@DefLength:
927     mov [edi-StrOffset].LStrRec.Length, ecx { Store default output length. }
928     or ecx, ecx
929     jz @@Done { Done if empty input string. }
930    
931     @@Next:
932     mov al, [esi] { Next input character. }
933     inc esi
934     cmp al, 09h { Is it a tab? }
935     jz @@Tab { Yes, compute next tab stop. }
936     mov [edi], al { No, store to output. }
937     inc edi
938     inc edx { Increment output length. }
939     dec ecx { Decrement input length. }
940     jnz @@Next
941     jmp @@StoreLen { Loop termination. }
942    
943     @@Tab:
944     push ecx { Save input length. }
945     push edx { Save output length. }
946     mov eax, edx { Get current output length in EDX:EAX. }
947     xor edx, edx
948     div ebx { Output length MOD TabSize in DX. }
949     mov ecx, ebx { Calc number of spaces to insert... }
950     sub ecx, edx { = TabSize - Mod value. }
951     pop edx
952     add edx, ecx { Add count of spaces into current output length. }
953    
954     mov eax,$2020 { Blank in AH, Blank in AL. }
955     shr ecx, 1 { Store blanks. }
956     rep stosw
957     adc ecx, ecx
958     rep stosb
959     pop ecx { Restore input length. }
960     dec ecx
961     jnz @@Next
962     {jmp @@Next} { Back for next input. }
963    
964     @@StoreLen:
965     xor ebx, ebx
966     mov [edi], bl { Store terminating null }
967     mov eax, edx
968     sub edi, eax
969     mov [edi-StrOffset].LStrRec.Length, edx { Store final length. }
970    
971     @@Done:
972     pop esi
973     pop edi
974     pop ebx
975     end;
976     end;
977    
978     function ScrambleL(const S, Key : AnsiString) : AnsiString;
979     {-Encrypt / Decrypt string with enhanced XOR encryption.}
980     var
981     I, J, LKey, LStr : Cardinal;
982     begin
983     Result := S;
984     if Key = '' then Exit;
985     if S = '' then Exit;
986     LKey := Length(Key);
987     LStr := Length(S);
988     I := 1;
989     J := LKey;
990     while I <= LStr do begin
991     if J = 0 then
992     J := LKey;
993     if (S[I] <> Key[J]) then
994     Result[I] := AnsiChar(Byte(S[I]) xor Byte(Key[J]));
995     Inc(I);
996     Dec(J);
997     end;
998     end;
999    
1000     function SubstituteL(const S, FromStr, ToStr : String) : String;
1001     {-Map the characters found in FromStr to the corresponding ones in ToStr.}
1002     var
1003     I : Cardinal;
1004     P : Cardinal;
1005     begin
1006     Result := S;
1007     if Length(FromStr) = Length(ToStr) then
1008     for I := 1 to Length(Result) do begin
1009     {P := System.Pos(S[I], FromStr);}
1010     {if P <> 0 then}
1011     if StrChPosL(FromStr, S[I], P) then
1012     Result[I] := ToStr[P];
1013     end;
1014     end;
1015    
1016     function FilterL(const S, Filters : String) : String;
1017     {-Remove characters from a string. The characters to remove are specified in
1018     ChSet.}
1019     var
1020     I : Cardinal;
1021     Len : Cardinal;
1022     begin
1023     Len := 0;
1024     SetLength(Result, Length(S));
1025     for I := 1 to Length(S) do
1026     if not CharExistsL(Filters, S[I]) then begin
1027     Inc(Len);
1028     Result[Len] := S[I];
1029     end;
1030     SetLength(Result, Len);
1031     end;
1032    
1033     {--------------- Word / Char manipulation -------------------------}
1034    
1035     function CharExistsL(const S : String; C : Char) : Boolean; register;
1036     {-Count the number of a given character in a string. }
1037     {$IFDEF UNICODE}
1038     var
1039     I: Integer;
1040     begin
1041     Result := False;
1042     for I := 1 to Length(S) do
1043     begin
1044     if S[I] = C then
1045     begin
1046     Result := True;
1047     Break;
1048     end;
1049     end;
1050     end;
1051     {$ELSE}
1052     asm
1053     push ebx
1054     xor ecx, ecx
1055     or eax, eax
1056     jz @@Done
1057     mov ebx, [eax-StrOffset].LStrRec.Length
1058     or ebx, ebx
1059     jz @@Done
1060     jmp @@5
1061    
1062     @@Loop:
1063     cmp dl, [eax+3]
1064     jne @@1
1065     inc ecx
1066     jmp @@Done
1067    
1068     @@1:
1069     cmp dl, [eax+2]
1070     jne @@2
1071     inc ecx
1072     jmp @@Done
1073    
1074     @@2:
1075     cmp dl, [eax+1]
1076     jne @@3
1077     inc ecx
1078     jmp @@Done
1079    
1080     @@3:
1081     cmp dl, [eax+0]
1082     jne @@4
1083     inc ecx
1084     jmp @@Done
1085    
1086     @@4:
1087     add eax, 4
1088     sub ebx, 4
1089    
1090     @@5:
1091     cmp ebx, 4
1092     jge @@Loop
1093    
1094     cmp ebx, 3
1095     je @@1
1096    
1097     cmp ebx, 2
1098     je @@2
1099    
1100     cmp ebx, 1
1101     je @@3
1102    
1103     @@Done:
1104     mov eax, ecx
1105     pop ebx
1106     end;
1107     {$ENDIF}
1108    
1109     function CharCountL(const S : String; C : Char) : Cardinal; register;
1110     {-Count the number of a given character in a string. }
1111     {$IFDEF UNICODE}
1112     var
1113     I: Integer;
1114     begin
1115     Result := 0;
1116     for I := 1 to Length(S) do
1117     if S[I] = C then
1118     Inc(Result);
1119     end;
1120     {$ELSE}
1121     asm
1122     push ebx
1123     xor ecx, ecx
1124     or eax, eax
1125     jz @@Done
1126     mov ebx, [eax-StrOffset].LStrRec.Length
1127     or ebx, ebx
1128     jz @@Done
1129     jmp @@5
1130    
1131     @@Loop:
1132     cmp dl, [eax+3]
1133     jne @@1
1134     inc ecx
1135    
1136     @@1:
1137     cmp dl, [eax+2]
1138     jne @@2
1139     inc ecx
1140    
1141     @@2:
1142     cmp dl, [eax+1]
1143     jne @@3
1144     inc ecx
1145    
1146     @@3:
1147     cmp dl, [eax+0]
1148     jne @@4
1149     inc ecx
1150    
1151     @@4:
1152     add eax, 4
1153     sub ebx, 4
1154    
1155     @@5:
1156     cmp ebx, 4
1157     jge @@Loop
1158    
1159     cmp ebx, 3
1160     je @@1
1161    
1162     cmp ebx, 2
1163     je @@2
1164    
1165     cmp ebx, 1
1166     je @@3
1167    
1168     @@Done:
1169     mov eax, ecx
1170     pop ebx
1171     end;
1172     {$ENDIF}
1173    
1174     function WordCountL(const S, WordDelims : String) : Cardinal;
1175     {-Given an array of word delimiters, return the number of words in a string.}
1176     var
1177     I : Cardinal;
1178     SLen : Cardinal;
1179     begin
1180     Result := 0;
1181     I := 1;
1182     SLen := Length(S);
1183    
1184     while I <= SLen do begin
1185     {skip over delimiters}
1186     while (I <= SLen) and CharExistsL(WordDelims, S[I]) do
1187     Inc(I);
1188    
1189     {if we're not beyond end of S, we're at the start of a word}
1190     if I <= SLen then
1191     Inc(Result);
1192    
1193     {find the end of the current word}
1194     while (I <= SLen) and not CharExistsL(WordDelims, S[I]) do
1195     Inc(I);
1196     end;
1197     end;
1198    
1199     function WordPositionL(N : Cardinal; const S, WordDelims : String;
1200     var Pos : Cardinal) : Boolean;
1201     {-Given an array of word delimiters, set Pos to the start position of the
1202     N'th word in a string. Result indicates success/failure.}
1203     var
1204     Count : Longint;
1205     I : Longint;
1206     begin
1207     Count := 0;
1208     I := 1;
1209     Result := False;
1210    
1211     while (I <= Length(S)) and (Count <> LongInt(N)) do begin
1212     {skip over delimiters}
1213     while (I <= Length(S)) and CharExistsL(WordDelims, S[I]) do
1214     Inc(I);
1215    
1216     {if we're not beyond end of S, we're at the start of a word}
1217     if I <= Length(S) then
1218     Inc(Count);
1219    
1220     {if not finished, find the end of the current word}
1221     if Count <> LongInt(N) then
1222     while (I <= Length(S)) and not CharExistsL(WordDelims, S[I]) do
1223     Inc(I)
1224     else begin
1225     Pos := I;
1226     Result := True;
1227     end;
1228     end;
1229     end;
1230    
1231     function ExtractWordL(N : Cardinal; const S, WordDelims : String) : String;
1232     {-Given an array of word delimiters, return the N'th word in a string.}
1233     var
1234     C : Cardinal;
1235     I, J : Longint;
1236     begin
1237     Result := '';
1238     if WordPositionL(N, S, WordDelims, C) then begin
1239     I := C;
1240     {find the end of the current word}
1241     J := I;
1242     while (I <= Length(S)) and not
1243     CharExistsL(WordDelims, S[I]) do
1244     Inc(I);
1245     SetLength(Result, I-J);
1246     Move(S[J], Result[1], (I-J) * SizeOf(Char));
1247     end;
1248     end;
1249    
1250    
1251     function AsciiCountL(const S, WordDelims : String; Quote : Char) : Cardinal;
1252     {-Return the number of words in a string.}
1253     var
1254     I : Longint;
1255     InQuote : Boolean;
1256     begin
1257     Result := 0;
1258     I := 1;
1259     InQuote := False;
1260     while I <= Length(S) do begin
1261     {skip over delimiters}
1262     while (I <= Length(S)) and (S[I] <> Quote)
1263     and CharExistsL(WordDelims, S[I]) do
1264     Inc(I);
1265     {if we're not beyond end of S, we're at the start of a word}
1266     if I <= Length(S) then
1267     Inc(Result);
1268     {find the end of the current word}
1269     while (I <= Length(S)) and
1270     (InQuote or not CharExistsL(WordDelims, S[I])) do begin
1271     if S[I] = Quote then
1272     InQuote := not InQuote;
1273     Inc(I);
1274     end;
1275     end;
1276     end;
1277    
1278     function AsciiPositionL(N : Cardinal; const S, WordDelims : String;
1279     Quote : Char; var Pos : Cardinal) : Boolean;
1280     {-Return the position of the N'th word in a string.}
1281     var
1282     Count, I : Longint;
1283     InQuote : Boolean;
1284     begin
1285     Count := 0;
1286     InQuote := False;
1287     Result := False;
1288     I := 1;
1289     while (I <= Length(S)) and (Count <> LongInt(N)) do begin
1290     {skip over delimiters}
1291     while (I <= Length(S)) and (S[I] <> Quote) and
1292     CharExistsL(WordDelims, S[I]) do
1293     Inc(I);
1294     {if we're not beyond end of S, we're at the start of a word}
1295     if I <= Length(S) then
1296     Inc(Count);
1297     {if not finished, find the end of the current word}
1298     if Count <> LongInt(N) then
1299     while (I <= Length(S)) and (InQuote or not
1300     CharExistsL(WordDelims, S[I])) do begin
1301     if S[I] = Quote then
1302     InQuote := not InQuote;
1303     Inc(I);
1304     end
1305     else begin
1306     Pos := I;
1307     Result := True;
1308     end;
1309     end;
1310     end;
1311    
1312     function ExtractAsciiL(N : Cardinal; const S, WordDelims : String;
1313     Quote : Char) : String;
1314     {-Given an array of word delimiters, return the N'th word in a string. Any
1315     text within Quote characters is counted as one word.}
1316     var
1317     C : Cardinal;
1318     I, J : Longint;
1319     InQuote : Boolean;
1320     begin
1321     InQuote := False;
1322     if AsciiPositionL(N, S, WordDelims, Quote, C) then begin
1323     I := C;
1324     J := I;
1325     {find the end of the current word}
1326     while (I <= Length(S)) and ((InQuote)
1327     or not CharExistsL(WordDelims, S[I])) do begin
1328     if S[I] = Quote then
1329     InQuote := not(InQuote);
1330     Inc(I);
1331     end;
1332     SetLength(Result, I-J);
1333     Move(S[J], Result[1], I-J);
1334     end;
1335     end;
1336    
1337     procedure WordWrapL(const InSt : String; var OutSt, Overlap : String;
1338     Margin : Cardinal; PadToMargin : Boolean);
1339     {-Wrap a text string at a specified margin.}
1340     var
1341     InStLen : Cardinal;
1342     EOS, BOS : Cardinal;
1343     Len : Integer; {!!.02}
1344     begin
1345     InStLen := Length(InSt);
1346    
1347     {!!.02 - Added }
1348     { handle empty string on input }
1349     if InStLen = 0 then begin
1350     OutSt := '';
1351     Overlap := '';
1352     Exit;
1353     end;
1354     {!!.02 - End Added }
1355    
1356     {find the end of the output string}
1357     if InStLen > Margin then begin
1358     {find the end of the word at the margin, if any}
1359     EOS := Margin;
1360     while (EOS <= InStLen) and (InSt[EOS] <> ' ') do
1361     Inc(EOS);
1362     if EOS > InStLen then
1363     EOS := InStLen;
1364    
1365     {trim trailing blanks}
1366     while (EOS > 0) and (InSt[EOS] = ' ') do {!!.04}
1367     Dec(EOS);
1368    
1369     if EOS > Margin then begin
1370     {look for the space before the current word}
1371     while (EOS > 0) and (InSt[EOS] <> ' ') do
1372     Dec(EOS);
1373    
1374     {if EOS = 0 then we can't wrap it}
1375     if EOS = 0 then
1376     EOS := Margin
1377     else
1378     {trim trailing blanks}
1379     while (EOS > 0) and (InSt[EOS] = ' ') do {!!.04}
1380     Dec(EOS);
1381     end;
1382     end else
1383     EOS := InStLen;
1384    
1385     {copy the unwrapped portion of the line}
1386     if EOS > 0 then begin {!!.04}
1387     SetLength(OutSt, EOS);
1388     Move(InSt[1], OutSt[1], Length(OutSt) * SizeOf(Char));
1389     end; {!!.04}
1390    
1391     {find the start of the next word in the line}
1392     BOS := Succ(EOS);
1393     while (BOS <= InStLen) and (InSt[BOS] = ' ') do
1394     Inc(BOS);
1395    
1396     if BOS > InStLen then
1397     SetLength(OverLap, 0)
1398     else begin
1399     {copy from the start of the next word to the end of the line}
1400    
1401     SetLength(OverLap, InStLen);
1402     Move(InSt[BOS], Overlap[1], Succ(InStLen-BOS) * SizeOf(Char));
1403     SetLength(OverLap, Succ(InStLen-BOS));
1404     end;
1405    
1406     {pad the end of the output string if requested}
1407     {!!.02 - Rewritten}
1408     Len := Length(OutSt);
1409     if PadToMargin and (Len < LongInt(Margin)) then begin
1410     // SetLength(OutSt, Margin);
1411     // FillChar(OutSt[Succ(Len)], LongInt(Margin)-Length(OutSt), ' ');
1412     OutSt := OutSt + StringOfChar(' ', Margin - Length(OutSt));
1413     end;
1414     {!!.02 - End Rewritten}
1415     end;
1416    
1417     {--------------- String comparison and searching -----------------}
1418     function CompStringL(const S1, S2 : String) : Integer; register;
1419     {-Compare two strings.}
1420     {$IFDEF UNICODE}
1421     begin
1422     Result := AnsiCompareStr(S1, S2);
1423     end;
1424     {$ELSE}
1425     asm
1426     push edi
1427     mov edi, edx { EDI points to S2 }
1428     push esi
1429     mov esi, eax { ESI points to S1 }
1430    
1431     xor edx, edx
1432     xor ecx, ecx
1433    
1434     or edi, edi
1435     jz @@1
1436     mov edx, [edi-StrOffset].LStrRec.Length
1437    
1438     @@1:
1439     or esi, esi
1440     jz @@2
1441     mov ecx, [esi-StrOffset].LStrRec.Length
1442    
1443     @@2:
1444     or eax, -1 { EAX holds temporary result }
1445    
1446     cmp ecx, edx { Compare lengths }
1447     je @@EqLen { Lengths equal? }
1448     jb @@Comp { Jump if S1 shorter than S1 }
1449    
1450     inc eax { S1 longer than S2 }
1451     mov ecx, edx { Length(S2) in CL }
1452    
1453     @@EqLen:
1454     inc eax { Equal or greater }
1455    
1456     @@Comp:
1457     or ecx, ecx
1458     jz @@Done { Done if either is empty }
1459    
1460     repe cmpsb { Compare until no match or ECX = 0 }
1461     je @@Done { If Equal, result ready based on length }
1462    
1463     mov eax, 1
1464     ja @@Done { S1 Greater? Return 1 }
1465     or eax, -1 { Else S1 Less, Return -1 }
1466    
1467     @@Done:
1468     pop esi
1469     pop edi
1470     end;
1471     {$ENDIF}
1472    
1473     function CompUCStringL(const S1, S2 : String) : Integer; register;
1474     {-Compare two strings. This compare is not case sensitive.}
1475     {$IFDEF UNICODE}
1476     begin
1477     Result := AnsiCompareText(S1, S2);
1478     end;
1479     {$ELSE}
1480     asm
1481     push ebx { Save registers }
1482     push edi
1483     push esi
1484    
1485     mov edi, edx { EDI points to S2 }
1486     mov esi, eax { ESI points to S1 }
1487    
1488     xor eax, eax
1489     xor ecx, ecx
1490     xor edx, edx { DL chars from S2 }
1491     or ebx, -1
1492    
1493     or edi, edi
1494     jz @@1
1495     mov eax, [edi-StrOffset].LStrRec.Length
1496    
1497     @@1:
1498     or esi, esi
1499     jz @@2
1500     mov ecx, [esi-StrOffset].LStrRec.Length
1501    
1502     @@2:
1503     cmp ecx, eax { Compare lengths }
1504     je @@EqLen { Lengths equal? }
1505     jb @@Comp { Jump if S1 shorter than S1 }
1506    
1507     inc ebx { S1 longer than S2 }
1508     mov ecx, eax { Shorter length in ECX }
1509    
1510     @@EqLen:
1511     inc ebx { Equal or greater }
1512    
1513     @@Comp:
1514     or ecx, ecx
1515     jz @@Done { Done if lesser string is empty }
1516    
1517     @@Start:
1518     xor eax, eax { EAX holds chars from S1 }
1519     mov al, [esi] { S1[?] into AL }
1520     inc esi
1521    
1522     push ecx { Save registers }
1523     push edx
1524     push eax { Push Char onto stack for CharUpper }
1525     call CharUpper
1526     pop edx { Restore registers }
1527     pop ecx
1528    
1529     mov dl, [edi] { S2[?] into DL }
1530     inc edi { Point EDI to next char in S2 }
1531     mov dh, al
1532     mov al, dl
1533     mov dl, dh
1534    
1535     push ecx { Save registers }
1536     push edx
1537     push eax { Push Char onto stack for CharUpper }
1538     call CharUpper
1539     pop edx { Restore registers }
1540     pop ecx
1541    
1542     cmp dl, al { Compare until no match }
1543     jne @@Output
1544     dec ecx
1545     jnz @@Start
1546    
1547     je @@Done { If Equal, result ready based on length }
1548    
1549     @@Output:
1550     mov ebx, 1
1551     ja @@Done { S1 Greater? Return 1 }
1552     or ebx, -1 { Else S1 Less, Return -1 }
1553    
1554     @@Done:
1555     mov eax, ebx { Result into EAX }
1556     pop esi { Restore Registers }
1557     pop edi
1558     pop ebx
1559     end;
1560     {$ENDIF}
1561    
1562     function SoundexL(const S : AnsiString) : AnsiString;
1563     {-Return 4 character soundex of an input string}
1564     const
1565     SoundexTable : array[0..255] of Char =
1566     (#0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1567     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1568     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1569     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1570     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1571     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1572     #0, #0, #0, #0, #0,
1573     { A B C D E F G H I J K L M }
1574     #0, '1', '2', '3', #0, '1', '2', #0, #0, '2', '2', '4', '5',
1575     { N O P Q R S T U V W X Y X }
1576     '5', #0, '1', '2', '6', '2', '3', #0, '1', #0, '2', #0, '2',
1577     #0, #0, #0, #0, #0, #0,
1578     { a b c d e f g h i j k l m }
1579     #0, '1', '2', '3', #0, '1', '2', #0, #0, '2', '2', '4', '5',
1580     { n o p q r s t u v w x y x }
1581     '5', #0, '1', '2', '6', '2', '3', #0, '1', #0, '2', #0, '2',
1582     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1583     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1584     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1585     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1586     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1587     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1588     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1589     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1590     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1591     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1592     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1593     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1594     #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,
1595     #0, #0, #0);
1596     begin
1597     if S = '' then Exit;
1598     SetLength(Result, 4);
1599     asm
1600     push edi
1601     mov edi, [Result] { EDI => output string. }
1602     mov edi, [edi]
1603     push ebx
1604     push esi
1605    
1606     mov esi, S { ESI => input string. }
1607     mov dword ptr [edi], '0000' { Initialize output string to '0000'. }
1608     xor eax, eax
1609     mov [edi+4], al { Set null at end of string. }
1610    
1611     mov ecx, [esi-StrOffset].LStrRec.Length
1612     or ecx, ecx { Exit if null string. }
1613     jz @@Done
1614    
1615     mov al, [esi] { Get first character of input string. }
1616     inc esi
1617    
1618     push ecx { Save ECX across call to CharUpper. }
1619     push eax { Push Char onto stack for CharUpper. }
1620     call CharUpper { Uppercase AL. }
1621     pop ecx { Restore saved register. }
1622    
1623     mov [edi], al { Store first output character. }
1624     inc edi
1625    
1626     dec ecx { One input character used. }
1627     jz @@Done { Was input string one char long?. }
1628    
1629     mov bh, 03h { Output max 3 chars beyond first. }
1630     mov edx, offset SoundexTable { EDX => Soundex table. }
1631     xor eax, eax { Prepare for address calc. }
1632     xor bl, bl { BL will be used to store 'previous char'. }
1633    
1634     @@Next:
1635     mov al, [esi] { Get next char in AL. }
1636     inc esi
1637     mov al, [edx+eax] { Get soundex code into AL. }
1638     or al, al { Is AL zero? }
1639     jz @@NoStore { If yes, skip this char. }
1640     cmp bl, al { Is it the same as the previous stored char? }
1641     je @@NoStore { If yes, skip this char. }
1642     mov [edi], al { Store char to Dest. }
1643     inc edi
1644     dec bh { Decrement output counter. }
1645     jz @@Done { If zero, we're done. }
1646     mov bl, al { New previous character. }
1647    
1648     @@NoStore:
1649     dec ecx { Decrement input counter. }
1650     jnz @@Next
1651    
1652     @@Done:
1653     pop esi
1654     pop ebx
1655     pop edi
1656     end;
1657     end;
1658    
1659     function MakeLetterSetL(const S : AnsiString) : Longint; register;
1660     {-Return a bit-mapped long storing the individual letters contained in S.}
1661     asm
1662     push ebx { Save registers }
1663     push esi
1664    
1665     mov esi, eax { ESI => string }
1666     xor ecx, ecx { Zero ECX }
1667     xor edx, edx { Zero EDX }
1668     {or edx, edx}
1669     or eax, eax
1670     jz @@Exit
1671     xor eax, eax { Zero EAX }
1672     add ecx, [esi-StrOffset].LStrRec.Length
1673     jz @@Exit { Done if ECX is 0 }
1674    
1675     @@Next:
1676     mov al, [esi] { EAX has next char in S }
1677     inc esi
1678    
1679     push ecx { Save registers }
1680     push edx
1681     push eax { Push Char onto stack for CharUpper }
1682     call CharUpper
1683     pop edx { Restore registers }
1684     pop ecx
1685    
1686     sub eax, 'A' { Convert to bit number }
1687     cmp eax, 'Z'-'A' { Was char in range 'A'..'Z'? }
1688     ja @@Skip { Skip it if not }
1689    
1690     mov ebx, eax { Exchange EAX and ECX }
1691     mov eax, ecx
1692     mov ecx, ebx
1693     ror edx, cl
1694     or edx, 01h { Set appropriate bit }
1695     rol edx, cl
1696     mov ebx, eax { Exchange EAX and ECX }
1697     mov eax, ecx
1698     mov ecx, ebx
1699    
1700     @@Skip:
1701     dec ecx
1702     jnz @@Next { Get next character }
1703    
1704     @@Exit:
1705     mov eax, edx { Move EDX to result }
1706     pop esi { Restore registers }
1707     pop ebx
1708     end;
1709    
1710     {$IFDEF UNICODE}
1711     procedure BMMakeTableL(const MatchString : UnicodeString; var BT : BTable);
1712     begin
1713     // Do nothing until BMSearchL is fixed
1714     {var
1715     I: Integer;
1716     Len: Byte;
1717     begin
1718     Len := Length(MatchString);
1719     if Len > 255 then
1720     Len := 255;
1721    
1722     FillChar(BT, SizeOf(BT), Len);
1723     for I := 1 to Length(MatchString) - 1 do
1724     BT[Word(MatchString[I])] := Len - I; }
1725     end;
1726     {$ELSE}
1727     procedure BMMakeTableL(const MatchString : AnsiString; var BT : BTable); register;
1728     {-Build a Boyer-Moore link table}
1729     asm
1730     push edi { Save registers because they will be changed }
1731     push esi
1732     mov esi, eax { Move EAX to ESI }
1733     push ebx
1734    
1735     or eax, eax
1736     jz @@MTDone
1737    
1738     xor eax, eax { Zero EAX }
1739     mov ecx, [esi-StrOffset].LStrRec.Length
1740     cmp ecx, 0FFh { If ECX > 255, force to 255 }
1741     jbe @@1
1742     mov ecx, 0FFh
1743    
1744     @@1:
1745     mov ch, cl { Duplicate CL in CH }
1746     mov eax, ecx { Fill each byte in EAX with length }
1747     shl eax, 16
1748     mov ax, cx
1749     mov edi, edx { Point to the table }
1750     mov ecx, 64 { Fill table bytes with length }
1751     rep stosd
1752     cmp al, 1 { If length <= 1, we're done }
1753     jbe @@MTDone
1754     mov edi, edx { Reset EDI to beginning of table }
1755     xor ebx, ebx { Zero EBX }
1756     mov cl, al { Restore CL to length of string }
1757     dec ecx
1758    
1759     @@MTNext:
1760     mov al, [esi] { Load table with positions of letters }
1761     mov bl, al { that exist in the search string }
1762     inc esi
1763     mov [edi+ebx], cl
1764     dec cl
1765     jnz @@MTNext
1766    
1767     @@MTDone:
1768     pop ebx { Restore registers }
1769     pop esi
1770     pop edi
1771     end;
1772     {$ENDIF}
1773    
1774     {$IFDEF UNICODE}
1775     function BMSearchL(var Buffer; BufLength: Cardinal; var BT: BTable; // TODO-UNICODE
1776     const MatchString : String; out Pos : Cardinal) : Boolean;
1777     var
1778     BufPtr: PChar;
1779     // s: string;
1780     // Len: Integer;
1781     // I,J,K: Integer;
1782     begin
1783     // the commented code doesn't work correctly, so use a simple Pos for now
1784     BufPtr := PChar(@Buffer);
1785     Pos := System.Pos(MatchString, BufPtr);
1786     Exit(Pos <> 0);
1787    
1788    
1789     (* BufPtr := PChar(@Buffer);
1790     s := bufptr;
1791     Len := Length(MatchString);
1792     // if Len > 255 then
1793     // Len := 255;
1794    
1795     // Check to see if we have a trivial case
1796     if Len = 0 then
1797     begin
1798     Pos := 0;
1799     Exit(False);
1800     end
1801     else if Len = 1 then
1802     begin
1803     Pos := System.Pos(MatchString, BufPtr);
1804     Exit(Pos <> 0);
1805     end
1806     else
1807     begin
1808     I := Len;
1809     while I < BufLength do
1810     begin
1811     J := 0; // Matched letter count
1812     while J < Len do
1813     begin
1814     if s[I - J - 1] = MatchString[Len - J] then
1815     Inc(J)
1816     else
1817     Break;
1818     end;
1819     if J = Len then
1820     begin
1821     Pos := J;
1822     Exit(True);
1823     end;
1824     Inc(I, BT[Word(s[I])]);
1825     end;
1826     end; *)
1827    
1828     end;
1829     {$ELSE}
1830     function BMSearchL(var Buffer; BufLength : Cardinal; var BT : BTable;
1831     const MatchString : AnsiString; var Pos : Cardinal) : Boolean; register;
1832     {-Use the Boyer-Moore search method to search a buffer for a string.}
1833     var
1834     BufPtr : Pointer;
1835     asm
1836     push edi { Save registers since we will be changing }
1837     push esi
1838     push ebx
1839    
1840     mov BufPtr, eax { Copy Buffer to local variable and ESI }
1841     mov esi, MatchString { Set ESI to beginning of MatchString }
1842     or esi, esi
1843     jz @@BMSNotFound
1844     mov edi, eax
1845     mov ebx, ecx { Copy BT ptr to EBX }
1846     mov ecx, edx { Length of buffer to ECX }
1847     xor eax, eax { Zero EAX }
1848    
1849     mov edx, [esi-StrOffset].LStrRec.Length
1850     cmp edx, 0FFh { If EDX > 255, force to 255 }
1851     jbe @@1
1852     mov edx, 0FFh
1853    
1854     @@1:
1855     cmp dl, 1 { Check to see if we have a trivial case }
1856     ja @@BMSInit { If Length(MatchString) > 1 do BM search }
1857     jb @@BMSNotFound { If Length(MatchString) = 0 we're done }
1858    
1859     mov al,[esi] { If Length(MatchString) = 1 do a REPNE SCASB }
1860     mov ebx, edi
1861     repne scasb
1862     jne @@BMSNotFound { No match during REP SCASB }
1863     mov esi, Pos { Set position in Pos }
1864     {dec edi} { Found, calculate position }
1865     sub edi, ebx
1866     mov eax, 1 { Set result to True }
1867     mov [esi], edi
1868     jmp @@BMSDone { We're done }
1869    
1870     @@BMSInit:
1871     dec edx { Set up for BM Search }
1872     add esi, edx { Set ESI to end of MatchString }
1873     add ecx, edi { Set ECX to end of buffer }
1874     add edi, edx { Set EDI to first check point }
1875     std { Backward string ops }
1876     mov dh, [esi] { Set DH to character we'll be looking for }
1877     dec esi { Dec ESI in prep for BMSFound loop }
1878     jmp @@BMSComp { Jump to first comparison }
1879    
1880     @@BMSNext:
1881     mov al, [ebx+eax] { Look up skip distance from table }
1882     add edi, eax { Skip EDI ahead to next check point }
1883    
1884     @@BMSComp:
1885     cmp edi, ecx { Have we reached end of buffer? }
1886     jae @@BMSNotFound { If so, we're done }
1887     mov al, [edi] { Move character from buffer into AL for comparison }
1888     cmp dh, al { Compare }
1889     jne @@BMSNext { If not equal, go to next checkpoint }
1890    
1891     push ecx { Save ECX }
1892     dec edi
1893     xor ecx, ecx { Zero ECX }
1894     mov cl, dl { Move Length(MatchString) to ECX }
1895     repe cmpsb { Compare MatchString to buffer }
1896     je @@BMSFound { If equal, string is found }
1897    
1898     mov al, dl { Move Length(MatchString) to AL }
1899     sub al, cl { Calculate offset that string didn't match }
1900     add esi, eax { Move ESI back to end of MatchString }
1901     add edi, eax { Move EDI to pre-string compare location }
1902     inc edi
1903     mov al, dh { Move character back to AL }
1904     pop ecx { Restore ECX }
1905     jmp @@BMSNext { Do another compare }
1906    
1907     @@BMSFound: { EDI points to start of match }
1908     mov edx, BufPtr { Move pointer to buffer into EDX }
1909     mov esi, Pos
1910     sub edi, edx { Calculate position of match }
1911     mov eax, edi
1912     inc eax
1913     inc eax
1914     mov [esi], eax { Set Pos to position of match }
1915     mov eax, 1 { Set result to True }
1916     pop ecx { Restore ESP }
1917     jmp @@BMSDone
1918    
1919     @@BMSNotFound:
1920     xor eax, eax { Set result to False }
1921    
1922     @@BMSDone:
1923     cld { Restore direction flag }
1924     pop ebx { Restore registers }
1925     pop esi
1926     pop edi
1927     end;
1928     {$ENDIF}
1929    
1930     {$IFDEF UNICODE}
1931     function BMSearchUCL(var Buffer; BufLength : Cardinal; var BT : BTable; // TODO-UNICODE
1932     const MatchString : String ; var Pos : Cardinal) : Boolean; register;
1933     var
1934     BufPtr: PChar;
1935     begin
1936     BufPtr := PChar(@Buffer);
1937     Pos := System.Pos(AnsiUpperCase(MatchString), AnsiUpperCase(BufPtr));
1938     Exit(Pos <> 0);
1939     end;
1940     {$ELSE}
1941    
1942     function BMSearchUCL(var Buffer; BufLength : Cardinal; var BT : BTable;
1943     const MatchString : AnsiString ; var Pos : Cardinal) : Boolean; register;
1944     {-Use the Boyer-Moore search method to search a buffer for a string. This
1945     search is not case sensitive.}
1946     var
1947     BufPtr : Pointer;
1948     asm
1949     push edi { Save registers since we will be changing }
1950     push esi
1951     push ebx
1952    
1953     mov BufPtr, eax { Copy Buffer to local variable and ESI }
1954     mov esi, MatchString { Set ESI to beginning of MatchString }
1955     or esi, esi
1956     jz @@BMSNotFound
1957     mov edi, eax
1958     mov ebx, ecx { Copy BT ptr to EBX }
1959     mov ecx, edx { Length of buffer to ECX }
1960     xor eax, eax { Zero EAX }
1961    
1962     mov edx, [esi-StrOffset].LStrRec.Length
1963     cmp edx, 0FFh { If EDX > 255, force to 255 }
1964     jbe @@1
1965     mov edx, 0FFh
1966    
1967     @@1:
1968     or dl, dl { Check to see if we have a trivial case }
1969     jz @@BMSNotFound { If Length(MatchString) = 0 we're done }
1970    
1971     @@BMSInit:
1972     dec edx { Set up for BM Search }
1973     add esi, edx { Set ESI to end of MatchString }
1974     add ecx, edi { Set ECX to end of buffer }
1975     add edi, edx { Set EDI to first check point }
1976     mov dh, [esi] { Set DH to character we'll be looking for }
1977     dec esi { Dec ESI in prep for BMSFound loop }
1978     jmp @@BMSComp { Jump to first comparison }
1979    
1980     @@BMSNext:
1981     mov al, [ebx+eax] { Look up skip distance from table }
1982     add edi, eax { Skip EDI ahead to next check point }
1983    
1984     @@BMSComp:
1985     cmp edi, ecx { Have we reached end of buffer? }
1986     jae @@BMSNotFound { If so, we're done }
1987    
1988     push ebx { Save registers }
1989     push ecx
1990     push edx
1991     mov al, [edi] { Move character from buffer into AL for comparison }
1992     push eax { Push Char onto stack for CharUpper }
1993     call CharUpper
1994     pop edx { Restore registers }
1995     pop ecx
1996     pop ebx
1997    
1998     cmp dh, al { Compare }
1999     jne @@BMSNext { If not equal, go to next checkpoint }
2000    
2001     push ecx { Save ECX }
2002     dec edi
2003     xor ecx, ecx { Zero ECX }
2004     mov cl, dl { Move Length(MatchString) to ECX }
2005     jecxz @@BMSFound { If ECX is zero, string is found }
2006    
2007     @@StringComp:
2008     xor eax, eax
2009     mov al, [edi] { Get char from buffer }
2010     dec edi { Dec buffer index }
2011    
2012     push ebx { Save registers }
2013     push ecx
2014     push edx
2015     push eax { Push Char onto stack for CharUpper }
2016     call CharUpper
2017     pop edx { Restore registers }
2018     pop ecx
2019     pop ebx
2020    
2021     mov ah, al { Move buffer char to AH }
2022     mov al, [esi] { Get MatchString char }
2023     dec esi
2024     cmp ah, al { Compare }
2025     loope @@StringComp { OK? Get next character }
2026     je @@BMSFound { Matched! }
2027    
2028     xor ah, ah { Zero AH }
2029     mov al, dl { Move Length(MatchString) to AL }
2030     sub al, cl { Calculate offset that string didn't match }
2031     add esi, eax { Move ESI back to end of MatchString }
2032     add edi, eax { Move EDI to pre-string compare location }
2033     inc edi
2034     mov al, dh { Move character back to AL }
2035     pop ecx { Restore ECX }
2036     jmp @@BMSNext { Do another compare }
2037    
2038     @@BMSFound: { EDI points to start of match }
2039     mov edx, BufPtr { Move pointer to buffer into EDX }
2040     mov esi, Pos
2041     sub edi, edx { Calculate position of match }
2042     mov eax, edi
2043     inc eax
2044     inc eax
2045     mov [esi], eax { Set Pos to position of match }
2046     mov eax, 1 { Set result to True }
2047     pop ecx { Restore ESP }
2048     jmp @@BMSDone
2049    
2050     @@BMSNotFound:
2051     xor eax, eax { Set result to False }
2052    
2053     @@BMSDone:
2054     pop ebx { Restore registers }
2055     pop esi
2056     pop edi
2057     end;
2058     {$ENDIF}
2059    
2060     {--------------- DOS pathname parsing -----------------}
2061    
2062     function DefaultExtensionL(const Name, Ext : String) : String;
2063     {-Return a file name with a default extension attached.}
2064     var
2065     DotPos : Cardinal;
2066     begin
2067     if HasExtensionL(Name, DotPos) then
2068     Result := Name
2069     else if Name = '' then
2070     Result := ''
2071     else
2072     Result := Name + '.' + Ext;
2073     end;
2074    
2075     function ForceExtensionL(const Name, Ext : String) : String;
2076     {-Force the specified extension onto the file name.}
2077     var
2078     DotPos : Cardinal;
2079     begin
2080     if HasExtensionL(Name, DotPos) then
2081     Result := System.Copy(Name, 1, DotPos) + Ext
2082     else if Name = '' then
2083     Result := ''
2084     else
2085     Result := Name + '.' + Ext;
2086     end;
2087    
2088     function JustFilenameL(const PathName : String) : String;
2089     {-Return just the filename and extension of a pathname.}
2090     var
2091     I : Cardinal;
2092     begin
2093     Result := '';
2094     if PathName = '' then Exit;
2095     I := Succ(Cardinal(Length(PathName)));
2096     repeat
2097     Dec(I);
2098     until (I = 0) or (PathName[I] in DosDelimSet); {!!.01}
2099     Result := System.Copy(PathName, Succ(I), StMaxFileLen);
2100     end;
2101    
2102     function JustNameL(const PathName : String) : String;
2103     {-Return just the filename (no extension, path, or drive) of a pathname.}
2104     var
2105     DotPos : Cardinal;
2106     S : AnsiString;
2107     begin
2108     S := JustFileNameL(PathName);
2109     if HasExtensionL(S, DotPos) then
2110     S := System.Copy(S, 1, DotPos-1);
2111     Result := S;
2112     end;
2113    
2114     function JustExtensionL(const Name : String) : String;
2115     {-Return just the extension of a pathname.}
2116     var
2117     DotPos : Cardinal;
2118     begin
2119     if HasExtensionL(Name, DotPos) then
2120     Result := System.Copy(Name, Succ(DotPos), StMaxFileLen)
2121     else
2122     Result := '';
2123     end;
2124    
2125     function JustPathnameL(const PathName : String) : String;
2126     {-Return just the drive and directory portion of a pathname.}
2127     var
2128     I : Cardinal;
2129     begin
2130     if PathName = '' then Exit;
2131    
2132     I := Succ(Cardinal(Length(PathName)));
2133     repeat
2134     Dec(I);
2135     until (I = 0) or (PathName[I] in DosDelimSet); {!!.01}
2136    
2137     if I = 0 then
2138     {Had no drive or directory name}
2139     SetLength(Result, 0)
2140     else if I = 1 then
2141     {Either the root directory of default drive or invalid pathname}
2142     Result := PathName[1]
2143     else if (PathName[I] = '\') then begin
2144     if PathName[Pred(I)] = ':' then
2145     {Root directory of a drive, leave trailing backslash}
2146     Result := System.Copy(PathName, 1, I)
2147     else
2148     {Subdirectory, remove the trailing backslash}
2149     Result := System.Copy(PathName, 1, Pred(I));
2150     end else
2151     {Either the default directory of a drive or invalid pathname}
2152     Result := System.Copy(PathName, 1, I);
2153     end;
2154    
2155     function AddBackSlashL(const DirName : String) : String;
2156     {-Add a default backslash to a directory name}
2157     begin
2158     Result := DirName;
2159     if (Length(Result) = 0) then
2160     Exit;
2161     if ((Length(Result) = 2) and (Result[2] = ':')) or
2162     ((Length(Result) > 2) and (Result[Length(Result)] <> '\')) then
2163     Result := Result + '\';
2164     end;
2165    
2166     function CleanFileNameL(const FileName : AnsiString) : AnsiString;
2167     {-Return filename with at most 8 chars of name and 3 of extension}
2168     var
2169     DotPos : Cardinal;
2170     NameLen : Word;
2171     begin
2172     if HasExtensionL(FileName, DotPos) then begin
2173     {Take the first 8 chars of name and first 3 chars of extension}
2174     NameLen := Pred(DotPos);
2175     if NameLen > 8 then
2176     NameLen := 8;
2177     Result := System.Copy(FileName, 1, NameLen)+System.Copy(FileName, DotPos, 4);
2178     end else
2179     {Take the first 8 chars of name}
2180     Result := System.Copy(FileName, 1, 8);
2181     end;
2182    
2183     function CleanPathNameL(const PathName : String) : String;
2184     {-Return a pathname cleaned up as DOS does it.}
2185     var
2186     I : Cardinal;
2187     S : String;
2188     begin
2189     SetLength(Result, 0);
2190     S := PathName;
2191    
2192     I := Succ(Cardinal(Length(S)));
2193     repeat
2194     dec(I);
2195     if I > 2 then
2196     if (S[I] = '\') and (S[I-1] = '\') then
2197     if (S[I-2] <> ':') then
2198     System.Delete(S, I, 1);
2199     until I <= 0;
2200    
2201     I := Succ(Cardinal(Length(S)));
2202     repeat
2203     {Get the next directory or drive portion of pathname}
2204     repeat
2205     Dec(I);
2206     until (I = 0) or (S[I] in DosDelimSet); {!!.02}
2207    
2208     {Clean it up and prepend it to output string}
2209     Result := CleanFileNameL(System.Copy(S, Succ(I), StMaxFileLen)) + Result;
2210     if I > 0 then begin
2211     Result := S[I] + Result;
2212     System.Delete(S, I, 255);
2213     end;
2214     until I <= 0;
2215    
2216     end;
2217    
2218     function HasExtensionL(const Name : String; var DotPos : Cardinal) : Boolean;
2219     {-Determine if a pathname contains an extension and, if so, return the
2220     position of the dot in front of the extension.}
2221     var
2222     I : Cardinal;
2223     begin
2224     DotPos := 0;
2225     for I := Length(Name) downto 1 do
2226     if (Name[I] = '.') and (DotPos = 0) then
2227     DotPos := I;
2228     Result := (DotPos > 0)
2229     and not CharExistsL(System.Copy(Name, Succ(DotPos), StMaxFileLen), '\');
2230     end;
2231    
2232     {------------------ Formatting routines --------------------}
2233    
2234    
2235     function CommaizeChL(L : Longint; Ch : Char) : String;
2236     {-Convert a long integer to a string with Ch in comma positions}
2237     var
2238     Temp : string;
2239     NumCommas, I, Len : Cardinal;
2240     Neg : Boolean;
2241     begin
2242     SetLength(Temp, 1);
2243     Temp[1] := Ch;
2244     if L < 0 then begin
2245     Neg := True;
2246     L := Abs(L);
2247     end else
2248     Neg := False;
2249     Result := Long2StrL(L);
2250     Len := Length(Result);
2251     NumCommas := (Pred(Len)) div 3;
2252     for I := 1 to NumCommas do
2253     System.Insert(Temp, Result, Succ(Len-(I * 3)));
2254     if Neg then
2255     System.Insert('-', Result, 1);
2256     end;
2257    
2258     function CommaizeL(L : LongInt) : String;
2259     {-Convert a long integer to a string with commas}
2260     begin
2261     Result := CommaizeChL(L, ',');
2262     end;
2263    
2264     function FormPrimL(const Mask : String; R : TstFloat; const LtCurr, RtCurr : String;
2265     Sep, DecPt : Char; AssumeDP : Boolean) : String;
2266     {-Returns a formatted string with digits from R merged into the Mask}
2267     const
2268     Blank = 0;
2269     Asterisk = 1;
2270     Zero = 2;
2271     const
2272     {$IFOPT N+}
2273     MaxPlaces = 18;
2274     {$ELSE}
2275     MaxPlaces = 11;
2276     {$ENDIF}
2277     FormChars : string = '#@*$-+,.';
2278     PlusArray : array[Boolean] of Char = ('+', '-');
2279     MinusArray : array[Boolean] of Char = (' ', '-');
2280     FillArray : array[Blank..Zero] of Char = (' ', '*', '0');
2281     var
2282     S : string; {temporary string}
2283     Filler : Integer; {char for unused digit slots: ' ', '*', '0'}
2284     WontFit, {true if number won't fit in the mask}
2285     AddMinus, {true if minus sign needs to be added}
2286     Dollar, {true if floating dollar sign is desired}
2287     Negative : Boolean; {true if B is negative}
2288     StartF, {starting point of the numeric field}
2289     EndF : Longint; {end of numeric field}
2290     RtChars, {# of chars to add to right}
2291     LtChars, {# of chars to add to left}
2292     DotPos, {position of '.' in Mask}
2293     Digits, {total # of digits}
2294     Blanks, {# of blanks returned by Str}
2295     Places, {# of digits after the '.'}
2296     FirstDigit, {pos. of first digit returned by Str}
2297     Extras, {# of extra digits needed for special cases}
2298     DigitPtr : Byte; {pointer into temporary string of digits}
2299     I : Cardinal;
2300     label
2301     EndFound,
2302     RedoCase,
2303     Done;
2304     begin
2305     {assume decimal point at end?}
2306     Result := Mask;
2307     if (not AssumeDP) and (not CharExistsL(Result, '.')) then
2308     AssumeDP := true;
2309     if AssumeDP and (Result <> '') then begin
2310     SetLength(Result, Succ(Length(Result)));
2311     Result[Length(Result)] := '.';
2312     end;
2313    
2314     RtChars := 0;
2315     LtChars := 0;
2316    
2317     {check for empty string}
2318     if Length(Result) = 0 then
2319     goto Done;
2320    
2321     {initialize variables}
2322     Filler := Blank;
2323     DotPos := 0;
2324     Places := 0;
2325     Digits := 0;
2326     Dollar := False;
2327     AddMinus := True;
2328     StartF := 1;
2329    
2330     {store the sign of the real and make it positive}
2331     Negative := (R < 0);
2332     R := Abs(R);
2333    
2334     {strip and count c's}
2335     for I := Length(Result) downto 1 do begin
2336     if Result[I] = 'C' then begin
2337     Inc(RtChars);
2338     System.Delete(Result, I, 1);
2339     end else if Result[I] = 'c' then begin
2340     Inc(LtChars);
2341     System.Delete(Result, I, 1);
2342     end;
2343     end;
2344    
2345     {find the starting point for the field}
2346     while (StartF <= Length(Result))
2347     {and (System.Pos(Result[StartF], FormChars) = 0) do}
2348     and not CharExistsL(FormChars, Result[StartF]) do
2349     Inc(StartF);
2350     if StartF > Length(Result) then
2351     goto Done;
2352    
2353     {find the end point for the field}
2354     EndF := StartF;
2355     for I := StartF to Length(Result) do begin
2356     EndF := I;
2357     case Result[EndF] of
2358     '*' : Filler := Asterisk;
2359     '@' : Filler := Zero;
2360     '$' : Dollar := True;
2361     '-',
2362     '+' : AddMinus := False;
2363     '#' : {ignore} ;
2364     ',',
2365     '.' : DotPos := EndF;
2366     else
2367     goto EndFound;
2368     end;
2369     {Inc(EndF);}
2370     end;
2371    
2372     {if we get here at all, the last char was part of the field}
2373     Inc(EndF);
2374    
2375     EndFound:
2376     {if we jumped to here instead, it wasn't}
2377     Dec(EndF);
2378    
2379     {disallow Dollar if Filler is Zero}
2380     if Filler = Zero then
2381     Dollar := False;
2382    
2383     {we need an extra slot if Dollar is True}
2384     Extras := Ord(Dollar);
2385    
2386     {get total # of digits and # after the decimal point}
2387     for I := StartF to EndF do
2388     case Result[I] of
2389     '#', '@',
2390     '*', '$' :
2391     begin
2392     Inc(Digits);
2393     if (I > DotPos) and (DotPos <> 0) then
2394     Inc(Places);
2395     end;
2396     end;
2397    
2398     {need one more 'digit' if Places > 0}
2399     Inc(Digits, Ord(Places > 0));
2400    
2401     {also need an extra blank if (1) Negative is true, and (2) Filler is Blank,
2402     and (3) AddMinus is true}
2403     if Negative and AddMinus and (Filler = Blank) then
2404     Inc(Extras)
2405     else
2406     AddMinus := False;
2407    
2408     {translate the real to a string}
2409     Str(R:Digits:Places, S);
2410    
2411     {add zeros that Str may have left out}
2412     if Places > MaxPlaces then begin
2413     I := Length(S);
2414     // SetLength(S, LongInt(I) + (Places-MaxPlaces));
2415     // FillChar(S[Succ(I)], Places-MaxPlaces, '0');
2416     S := StringOfChar('0', Places-MaxPlaces) + S;
2417     while (Length(S) > Digits) and (S[1] = ' ') do
2418     System.Delete(S, 1, 1);
2419     end;
2420    
2421     {count number of initial blanks}
2422     Blanks := 1;
2423     while S[Blanks] = ' ' do
2424     Inc(Blanks);
2425     FirstDigit := Blanks;
2426     Dec(Blanks);
2427    
2428     {the number won't fit if (a) S is longer than Digits or (b) the number of
2429     initial blanks is less than Extras}
2430     WontFit := (Length(S) > Digits) or (Blanks < Extras);
2431    
2432     {if it won't fit, fill decimal slots with '*'}
2433     if WontFit then begin
2434     for I := StartF to EndF do
2435     case Result[I] of
2436     '#', '@', '*', '$' : Result[I] := '*';
2437     '+' : Result[I] := PlusArray[Negative];
2438     '-' : Result[I] := MinusArray[Negative];
2439     end;
2440     goto Done;
2441     end;
2442    
2443     {fill initial blanks in S with Filler; insert floating dollar sign}
2444     if Blanks > 0 then begin
2445     //FillChar(S[1], Blanks, FillArray[Filler]);
2446     Delete(S, 1, Blanks);
2447     S := StringOfChar(FillArray[Filler], Blanks) + S;
2448    
2449     {put floating dollar sign in last blank slot if necessary}
2450     if Dollar then begin
2451     S[Blanks] := LtCurr[1];
2452     Dec(Blanks);
2453     end;
2454    
2455     {insert a minus sign if necessary}
2456     if AddMinus then
2457     S[Blanks] := '-';
2458     end;
2459    
2460     {put in the digits / signs}
2461     DigitPtr := Length(S);
2462     for I := EndF downto StartF do begin
2463     RedoCase:
2464     case Result[I] of
2465     '#', '@', '*', '$' :
2466     if DigitPtr <> 0 then begin
2467     Result[I] := S[DigitPtr];
2468     Dec(DigitPtr);
2469     if (DigitPtr <> 0) and (S[DigitPtr] = '.') then {!!.01}
2470     Dec(DigitPtr);
2471     end
2472     else
2473     Result[I] := FillArray[Filler];
2474     ',' :
2475     begin
2476     Result[I] := Sep;
2477     if (I < DotPos) and (DigitPtr < FirstDigit) then begin
2478     Result[I] := '#';
2479     goto RedoCase;
2480     end;
2481     end;
2482     '.' :
2483     begin
2484     Result[I] := DecPt;
2485     if (I < DotPos) and (DigitPtr < FirstDigit) then begin
2486     Result[I] := '#';
2487     goto RedoCase;
2488     end;
2489     end;
2490     '+' : Result[I] := PlusArray[Negative];
2491     '-' : Result[I] := MinusArray[Negative];
2492     end;
2493     end;
2494    
2495     Done:
2496     if AssumeDP then
2497     SetLength(Result, Pred(Length(Result)));
2498     if RtChars > 0 then begin
2499     S := RtCurr;
2500     if Length(S) > RtChars then
2501     SetLength(S, RtChars)
2502     else
2503     S := LeftPadL(S, RtChars);
2504     Result := Result + S;
2505     end;
2506     if LtChars > 0 then begin
2507     S := LtCurr;
2508     if Length(S) > LtChars then
2509     SetLength(S, LtChars)
2510     else
2511     S := PadL(S, LtChars);
2512     Result := S + Result;
2513     end;
2514     end;
2515    
2516     function FloatFormL(const Mask : String ; R : TstFloat ; const LtCurr,
2517     RtCurr : String ; Sep, DecPt : Char) : String;
2518     {-Return a formatted string with digits from R merged into mask.}
2519     begin
2520     Result := FormPrimL(Mask, R, LtCurr, RtCurr, Sep, DecPt, False);
2521     end;
2522    
2523     function LongIntFormL(const Mask : String ; L : LongInt ; const LtCurr,
2524     RtCurr : String ; Sep : Char) : String;
2525     {-Return a formatted string with digits from L merged into mask.}
2526     begin
2527     Result := FormPrimL(Mask, L, LtCurr, RtCurr, Sep, '.', True);
2528     end;
2529    
2530     function StrChPosL(const P : String; C : Char; var Pos : Cardinal) : Boolean;
2531     {-Return the position of a specified character within a string.}
2532     {$IFDEF UNICODE}
2533     begin
2534     Pos := System.Pos(C, P);
2535     Result := Pos <> 0;
2536     end;
2537     {$ELSE}
2538     asm
2539     push ebx { Save registers }
2540     push edi
2541    
2542     or eax, eax { Protect against null string }
2543     jz @@NotFound
2544    
2545     xor edi, edi { Zero counter }
2546     mov ebx, [eax-StrOffset].LStrRec.Length { Get input length }
2547    
2548     @@Loop:
2549     inc edi { Increment counter }
2550     cmp [eax], dl { Did we find it? }
2551     jz @@Found
2552     inc eax { Increment pointer }
2553    
2554     cmp edi, ebx { End of string? }
2555     jnz @@Loop { If not, loop }
2556    
2557     @@NotFound:
2558     xor eax, eax { Not found, zero EAX for False }
2559     mov [ecx], eax
2560     jmp @@Done
2561    
2562     @@Found:
2563     mov [ecx], edi { Set Pos }
2564     mov eax, 1 { Set EAX to True }
2565    
2566     @@Done:
2567     pop edi { Restore registers }
2568     pop ebx
2569     end;
2570     {$ENDIF}
2571    
2572     function StrStPosL(const P, S : String; var Pos : Cardinal) : Boolean;
2573     {-Return the position of a specified substring within a string.}
2574     begin
2575     Pos := System.Pos(S, P);
2576     Result := Pos <> 0;
2577     end;
2578    
2579     function StrStCopyL(const S : String; Pos, Count : Cardinal) : String;
2580     {-Copy characters at a specified position in a string.}
2581     begin
2582     Result := System.Copy(S, Pos, Count);
2583     end;
2584    
2585     function StrChInsertL(const S : String; C : Char; Pos : Cardinal) : String;
2586     var
2587     Temp : string;
2588     begin
2589     SetLength(Temp, 1);
2590     Temp[1] := C;
2591     Result := S;
2592     System.Insert(Temp, Result, Pos);
2593     end;
2594    
2595     function StrStInsertL(const S1, S2 : String; Pos : Cardinal) : String;
2596     {-Insert a string into another string at a specified position.}
2597     begin
2598     Result := S1;
2599     System.Insert(S2, Result, Pos);
2600     end;
2601    
2602     function StrChDeleteL(const S : String; Pos : Cardinal) : String;
2603     {-Delete the character at a specified position in a string.}
2604     begin
2605     Result := S;
2606     System.Delete(Result, Pos, 1);
2607     end;
2608    
2609     function StrStDeleteL(const S : String; Pos, Count : Cardinal) : String;
2610     {-Delete characters at a specified position in a string.}
2611     begin
2612     Result := S;
2613     System.Delete(Result, Pos, Count);
2614     end;
2615    
2616    
2617     {----------------------------------------------------------------------------}
2618    
2619     function CopyLeftL(const S : String; Len : Cardinal) : String;
2620     {-Return the left Len characters of a string}
2621     begin
2622     if (Len < 1) or (S = '') then
2623     Result := ''
2624     else
2625     Result := Copy(S, 1, Len);
2626     end;
2627    
2628     {----------------------------------------------------------------------------}
2629    
2630     function CopyMidL(const S : String; First, Len : Cardinal) : String;
2631     {-Return the mid part of a string}
2632     begin
2633     if (LongInt(First) > Length(S)) or (LongInt(Len) < 1) or (S = '') then
2634     Result := ''
2635     else
2636     Result := Copy(S, First, Len);
2637     end;
2638    
2639     {----------------------------------------------------------------------------}
2640    
2641     function CopyRightL(const S : String; First : Cardinal) : String;
2642     {-Return the right Len characters of a string}
2643     begin
2644     if (LongInt(First) > Length(S)) or (First < 1) or (S = '') then
2645     Result := ''
2646     else
2647     Result := Copy(S, First, Length(S));
2648     end;
2649    
2650     {----------------------------------------------------------------------------}
2651    
2652     function CopyRightAbsL(const S : String; NumChars : Cardinal) : String;
2653     {-Return NumChar characters starting from end}
2654     begin
2655     if (Cardinal(Length(S)) > NumChars) then
2656     Result := Copy(S, (Cardinal(Length(S)) - NumChars)+1, NumChars)
2657     else
2658     Result := S;
2659     end;
2660    
2661     {----------------------------------------------------------------------------}
2662    
2663     function WordPosL(const S, WordDelims, AWord : String;
2664     N : Cardinal; var Position : Cardinal) : Boolean;
2665     {-returns the Nth instance of a given word within a string}
2666     var
2667     TmpStr : String;
2668     Len,
2669     I,
2670     P1,
2671     P2 : Cardinal;
2672     begin
2673     if (S = '') or (AWord = '') or (pos(AWord, S) = 0) or (N < 1) then begin
2674     Result := False;
2675     Position := 0;
2676     Exit;
2677     end;
2678    
2679     Result := False;
2680     Position := 0;
2681    
2682     TmpStr := S;
2683     I := 0;
2684     Len := Length(AWord);
2685     P1 := pos(AWord, TmpStr);
2686    
2687     while (P1 > 0) and (Length(TmpStr) > 0) do begin
2688     P2 := P1 + pred(Len);
2689     if (P1 = 1) then begin
2690     if (pos(TmpStr[P2+1], WordDelims) > 0) then begin
2691     Inc(I);
2692     end else
2693     System.Delete(TmpStr, 1, P2);
2694     end else if (pos(TmpStr[P1-1], WordDelims) > 0) and
2695     // ((pos(TmpStr[P2+1], WordDelims) > 0) or {!!.02}
2696     // (LongInt(P2+1) = Length(TmpStr))) then begin {!!.02}
2697     ((LongInt(P2+1) >= Length(TmpStr)) or {!!.02}
2698     (pos(TmpStr[P2+1], WordDelims) > 0)) then begin {!!.02}
2699     Inc(I);
2700     end else if ((LongInt(P1 + pred(Len))) = Length(TmpStr)) then begin
2701     if (P1 > 1) and (pos(TmpStr[P1-1], WordDelims) > 0) then
2702     Inc(I);
2703     end;
2704    
2705     if (I = N) then begin
2706     Result := True;
2707     Position := Position + P1;
2708     Exit;
2709     end;
2710     System.Delete(TmpStr, 1, P2);
2711     Position := Position + P2;
2712     P1 := pos(AWord, TmpStr);
2713     end;
2714     end;
2715    
2716    
2717     {----------------------------------------------------------------------------}
2718    
2719     function CopyFromNthWordL(const S, WordDelims : String;
2720     const AWord : String; N : Cardinal; {!!.02}
2721     var SubString : String) : Boolean;
2722     var
2723     P : Cardinal;
2724     begin
2725     if (WordPosL(S, WordDelims, AWord, N, P)) then begin
2726     SubString := Copy(S, P, Length(S));
2727     Result := True;
2728     end else begin
2729     SubString := '';
2730     Result := False;
2731     end;
2732     end;
2733    
2734     {----------------------------------------------------------------------------}
2735    
2736     function DeleteFromNthWordL(const S, WordDelims : String;
2737     const AWord : String; N : Cardinal; {!!.02}
2738     var SubString : String) : Boolean;
2739     var
2740     P : Cardinal;
2741     begin
2742     SubString := S;
2743     if (WordPosL(S, WordDelims, AWord, N, P)) then begin
2744     Result := True;
2745     SubString := Copy(S, 1, P-1);
2746     end else begin
2747     Result := False;
2748     SubString := '';
2749     end;
2750     end;
2751    
2752     {----------------------------------------------------------------------------}
2753    
2754     function CopyFromToWordL(const S, WordDelims, Word1, Word2 : String;
2755     N1, N2 : Cardinal;
2756     var SubString : String) : Boolean;
2757     var
2758     P1,
2759     P2 : Cardinal;
2760     begin
2761     if (WordPosL(S, WordDelims, Word1, N1, P1)) then begin
2762     if (WordPosL(S, WordDelims, Word2, N2, P2)) then begin
2763     Dec(P2);
2764     if (P2 > P1) then begin
2765     Result := True;
2766     SubString := Copy(S, P1, P2-P1);
2767     end else begin
2768     Result := False;
2769     SubString := '';
2770     end;
2771     end else begin
2772     Result := False;
2773     SubString := '';
2774     end;
2775     end else begin
2776     Result := False;
2777     SubString := '';
2778     end;
2779     end;
2780    
2781     {----------------------------------------------------------------------------}
2782    
2783     function DeleteFromToWordL(const S, WordDelims, Word1, Word2 : String;
2784     N1, N2 : Cardinal;
2785     var SubString : String) : Boolean;
2786     var
2787     P1,
2788     P2 : Cardinal;
2789     begin
2790     SubString := S;
2791     if (WordPosL(S, WordDelims, Word1, N1, P1)) then begin
2792     if (WordPosL(S, WordDelims, Word2, N2, P2)) then begin
2793     Dec(P2);
2794     if (P2 > P1) then begin
2795     Result := True;
2796     System.Delete(SubString, P1, P2-P1+1);
2797     end else begin
2798     Result := False;
2799     SubString := '';
2800     end;
2801     end else begin
2802     Result := False;
2803     SubString := '';
2804     end;
2805     end else begin
2806     Result := False;
2807     SubString := '';
2808     end;
2809     end;
2810    
2811     {----------------------------------------------------------------------------}
2812    
2813     function CopyWithinL(const S, Delimiter : String;
2814     Strip : Boolean) : String;
2815     var
2816     P1,
2817     P2 : Cardinal;
2818     TmpStr : String;
2819     begin
2820     if (S = '') or (Delimiter = '') or (pos(Delimiter, S) = 0) then
2821     Result := ''
2822     else begin
2823     if (StrStPosL(S, Delimiter, P1)) then begin
2824     TmpStr := Copy(S, LongInt(P1) + Length(Delimiter), Length(S));
2825     if StrStPosL(TmpStr, Delimiter, P2) then begin
2826     Result := Copy(TmpStr, 1, P2-1);
2827     if (not Strip) then
2828     Result := Delimiter + Result + Delimiter;
2829     end else begin
2830     Result := TmpStr;
2831     if (not Strip) then
2832     Result := Delimiter + Result;
2833     end;
2834     end;
2835     end;
2836     end;
2837    
2838     {----------------------------------------------------------------------------}
2839    
2840     function DeleteWithinL(const S, Delimiter : String) : String;
2841     var
2842     P1,
2843     P2 : Cardinal;
2844     TmpStr : String;
2845     begin
2846     if (S = '') or (Delimiter = '') or (pos(Delimiter, S) = 0) then
2847     Result := ''
2848     else begin
2849     if (StrStPosL(S, Delimiter, P1)) then begin
2850     TmpStr := Copy(S, LongInt(P1) + Length(Delimiter), Length(S));
2851     if (pos(Delimiter, TmpStr) = 0) then
2852     Result := Copy(S, 1, P1-1)
2853     else begin
2854     if (StrStPosL(TmpStr, Delimiter, P2)) then begin
2855     Result := S;
2856     P2 := LongInt(P2) + (2*Length(Delimiter));
2857     System.Delete(Result, P1, P2);
2858     end;
2859     end;
2860     end;
2861     end;
2862     end;
2863    
2864     {----------------------------------------------------------------------------}
2865    
2866     function ReplaceWordL(const S, WordDelims, OldWord, NewWord : String;
2867     N : Cardinal;
2868     var Replacements : Cardinal) : String;
2869     var
2870     I,
2871     C,
2872     P1 : Cardinal;
2873     begin
2874     if (S = '') or (WordDelims = '') or (OldWord = '') or
2875     (pos(OldWord, S) = 0) then begin
2876     Result := S;
2877     Replacements := 0;
2878     Exit;
2879     end;
2880    
2881     if (WordPosL(S, WordDelims, OldWord, N, P1)) then begin
2882     Result := S;
2883     System.Delete(Result, P1, Length(OldWord));
2884    
2885     C := 0;
2886     for I := 1 to Replacements do begin
2887     if ((Length(NewWord)) + Length(Result)) < MaxLongInt then begin
2888     Inc(C);
2889     System.Insert(NewWord, Result, P1);
2890     Inc(P1, Length(NewWord) + 1);
2891     end else begin
2892     Replacements := C;
2893     Exit;
2894     end;
2895     end;
2896     end else begin
2897     Result := S;
2898     Replacements := 0;
2899     end;
2900     end;
2901    
2902    
2903     function ReplaceWordAllL(const S, WordDelims, OldWord, NewWord : String;
2904     var Replacements : Cardinal) : String;
2905     var
2906     I,
2907     C,
2908     P1 : Cardinal;
2909     begin
2910     if (S = '') or (WordDelims = '') or (OldWord = '') or
2911     (Pos(OldWord, S) = 0) then begin
2912     Result := S;
2913     Replacements := 0;
2914     end else begin
2915     Result := S;
2916     C := 0;
2917     while (WordPosL(Result, WordDelims, OldWord, 1, P1)) do begin
2918     System.Delete(Result, P1, Length(OldWord));
2919     for I := 1 to Replacements do begin
2920     if ((Length(NewWord) + Length(Result)) < MaxLongInt) then begin
2921     Inc(C);
2922     System.Insert(NewWord, Result, P1);
2923     end else begin
2924     Replacements := C;
2925     Exit;
2926     end;
2927     end;
2928     end;
2929     Replacements := C;
2930     end;
2931     end;
2932    
2933    
2934     {----------------------------------------------------------------------------}
2935    
2936     function ReplaceStringL(const S, OldString, NewString : String;
2937     N : Cardinal;
2938     var Replacements : Cardinal) : String;
2939     var
2940     I,
2941     C,
2942     P1 : Cardinal;
2943     TmpStr : String;
2944     begin
2945     if (S = '') or (OldString = '') or (pos(OldString, S) = 0) then begin
2946     Result := S;
2947     Replacements := 0;
2948     Exit;
2949     end;
2950     TmpStr := S;
2951    
2952     I := 1;
2953     P1 := pos(OldString, TmpStr);
2954     C := P1;
2955     while (I < N) and (LongInt(C) < Length(TmpStr)) do begin
2956     Inc(I);
2957     System.Delete(TmpStr, 1, LongInt(P1) + Length(OldString));
2958     Inc(C, LongInt(P1) + Length(OldString));
2959     end;
2960     Result := S;
2961     System.Delete(Result, C, Length(OldString));
2962    
2963     C := 0;
2964     for I := 1 to Replacements do begin
2965     if (((Length(NewString)) + Length(Result)) < MaxLongInt) then begin
2966     Inc(C);
2967     System.Insert(NewString, Result, P1);
2968     Inc(P1, Length(NewString) + 1);
2969     end else begin
2970     Replacements := C;
2971     Exit;
2972     end;
2973     end;
2974     end;
2975    
2976    
2977     function ReplaceStringAllL(const S, OldString, NewString : String;
2978     var Replacements : Cardinal) : String;
2979     var
2980     I,
2981     C : Cardinal;
2982     P1 : longint;
2983     Tmp: String;
2984     begin
2985     if (S = '') or (OldString = '') or (Pos(OldString, S) = 0) then
2986     begin
2987     Result := S;
2988     Replacements := 0;
2989     end
2990     else begin
2991     Tmp := S;
2992     P1 := AnsiPos(OldString, S);
2993     if (P1 > 0) then begin
2994     Result := Copy(Tmp, 1, P1-1);
2995     C := 0;
2996     while (P1 > 0) do begin
2997     for I := 1 to Replacements do begin
2998     Inc(C);
2999     Result := Result + NewString;
3000     end;
3001     Tmp := Copy(Tmp, P1+Length(OldString), MaxLongInt);
3002     P1 := AnsiPos(OldString, Tmp);
3003     if (P1 > 0) then begin
3004     Result := Result + Copy(Tmp, 1, P1-1);
3005     end else
3006     Result := Result + Tmp;
3007     end;
3008     Replacements := C;
3009     end else begin
3010     Result := S;
3011     Replacements := 0;
3012     end;
3013     end;
3014     end;
3015    
3016    
3017     function LastWordL(const S, WordDelims, AWord : String;
3018     var Position : Cardinal) : Boolean;
3019     var
3020     TmpStr : String;
3021     I : Cardinal;
3022     begin
3023     if (S = '') or (WordDelims = '') or
3024     (AWord = '') or (pos(AWord, S) = 0) then begin
3025     Result := False;
3026     Position := 0;
3027     Exit;
3028     end;
3029    
3030     TmpStr := S;
3031     I := Length(TmpStr);
3032     while (pos(TmpStr[I], WordDelims) > 0) do begin
3033     System.Delete(TmpStr, I, 1);
3034     I := Length(TmpStr);
3035     end;
3036    
3037     Position := Length(TmpStr);
3038     repeat
3039     while (pos(TmpStr[Position], WordDelims) = 0) and (Position > 1) do
3040     Dec(Position);
3041     if (Copy(TmpStr, Position + 1, Length(AWord)) = AWord) then begin
3042     Inc(Position);
3043     Result := True;
3044     Exit;
3045     end;
3046     System.Delete(TmpStr, Position, Length(TmpStr));
3047     Position := Length(TmpStr);
3048     until (Length(TmpStr) = 0);
3049     Result := False;
3050     Position := 0;
3051     end;
3052    
3053    
3054    
3055     function LastWordAbsL(const S, WordDelims : String;
3056     var Position : Cardinal) : Boolean;
3057     begin
3058     if (S = '') or (WordDelims = '') then begin
3059     Result := False;
3060     Position := 0;
3061     Exit;
3062     end;
3063    
3064     {find first non-delimiter character, if any. If not a "one-word wonder"}
3065     Position := Length(S);
3066     while (Position > 0) and (pos(S[Position], WordDelims) > 0) do
3067     Dec(Position);
3068    
3069     if (Position = 0) then begin
3070     Result := True;
3071     Position := 1;
3072     Exit;
3073     end;
3074    
3075     {find next delimiter character}
3076     while (Position > 0) and (pos(S[Position], WordDelims) = 0) do
3077     Dec(Position);
3078     Inc(Position);
3079     Result := True;
3080     end;
3081    
3082    
3083    
3084     function LastStringL(const S, AString : String;
3085     var Position : Cardinal) : Boolean;
3086     var
3087     TmpStr : String;
3088     I, C : Cardinal;
3089     begin
3090     if (S = '') or (AString = '') or (pos(AString, S) = 0) then begin
3091     Result := False;
3092     Position := 0;
3093     Exit;
3094     end;
3095    
3096     TmpStr := S;
3097     C := 0;
3098     I := pos(AString, TmpStr);
3099     while (I > 0) do begin
3100     Inc(C, LongInt(I) + Length(AString));
3101     System.Delete(TmpStr, 1, LongInt(I) + Length(AString));
3102     I := pos(AString, TmpStr);
3103     end;
3104     {Go back the length of AString since the while loop deletes the last instance}
3105     Dec(C, Length(AString));
3106     Position := C;
3107     Result := True;
3108     end;
3109    
3110    
3111    
3112     function KeepCharsL(const S, Chars : String) : String;
3113     var
3114     FromInx : Cardinal;
3115     ToInx : Cardinal;
3116     begin
3117     {if either the input string or the list of acceptable chars is empty
3118     the destination string will also be empty}
3119     if (S = '') or (Chars = '') then begin
3120     Result := '';
3121     Exit;
3122     end;
3123    
3124     {set the maximum length of the result string (it could be less than
3125     this, of course}
3126     SetLength(Result, length(S));
3127    
3128     {start off the to index}
3129     ToInx := 0;
3130    
3131     {in a loop, copy over the chars that match the list}
3132     for FromInx := 1 to length(S) do
3133     if CharExistsL(Chars, S[FromInx]) then begin
3134     inc(ToInx);
3135     Result[ToInx] := S[FromInx];
3136     end;
3137    
3138     {make sure that the length of the result string is correct}
3139     SetLength(Result, ToInx);
3140     end;
3141    
3142    
3143    
3144     function RepeatStringL(const RepeatString : String;
3145     var Repetitions : Cardinal;
3146     MaxLen : Cardinal) : String;
3147     var
3148     i : Cardinal;
3149     Len : Cardinal;
3150     ActualReps : Cardinal;
3151     begin
3152     Result := '';
3153     if (MaxLen <> 0) and
3154     (Repetitions <> 0) and
3155     (RepeatString <> '') then begin
3156     Len := length(RepeatString);
3157     ActualReps := MaxLen div Len;
3158     if (ActualReps > Repetitions) then
3159     ActualReps := Repetitions
3160     else
3161     Repetitions := ActualReps;
3162     if (ActualReps > 0) then begin
3163     SetLength(Result, ActualReps * Len);
3164     for i := 0 to pred(ActualReps) do
3165     Move(RepeatString[1], Result[i * Len + 1], Len * SizeOf(Char));
3166     end;
3167     end;
3168     end;
3169    
3170    
3171    
3172     function TrimCharsL(const S, Chars : String) : String;
3173     begin
3174     Result := RightTrimCharsL(S, Chars);
3175     Result := LeftTrimCharsL(Result, Chars);
3176     end;
3177    
3178    
3179    
3180     function RightTrimCharsL(const S, Chars : String) : String;
3181     var
3182     CutOff : integer;
3183     begin
3184     CutOff := length(S);
3185     while (CutOff > 0) do begin
3186     if not CharExistsL(Chars, S[CutOff]) then
3187     Break;
3188     dec(CutOff);
3189     end;
3190     if (CutOff = 0) then
3191     Result := ''
3192     else
3193     Result := Copy(S, 1, CutOff);
3194     end;
3195    
3196    
3197    
3198     function LeftTrimCharsL(const S, Chars : String) : String;
3199     var
3200     CutOff : integer;
3201     LenS : integer;
3202     begin
3203     LenS := length(S);
3204     CutOff := 1;
3205     while (CutOff <= LenS) do begin
3206     if not CharExistsL(Chars, S[CutOff]) then
3207     Break;
3208     inc(CutOff);
3209     end;
3210     if (CutOff > LenS) then
3211     Result := ''
3212     else
3213     Result := Copy(S, CutOff, LenS - CutOff + 1);
3214     end;
3215    
3216    
3217    
3218     function ExtractTokensL(const S, Delims: String;
3219     QuoteChar : Char;
3220     AllowNulls : Boolean;
3221     Tokens : TStrings) : Cardinal; overload;
3222     var
3223     State : (ScanStart,
3224     ScanQuotedToken,
3225     ScanQuotedTokenEnd,
3226     ScanNormalToken,
3227     ScanNormalTokenWithQuote);
3228     CurChar : Char;
3229     TokenStart : integer;
3230     Inx : integer;
3231     begin
3232     {Notes: this routine implements the following state machine
3233     start ----> ScanStart
3234     ScanStart-----quote----->ScanQuotedToken
3235     ScanStart-----delim----->ScanStart (1)
3236     ScanStart-----other----->ScanNormalToken
3237     ScanQuotedToken-----quote----->ScanQuotedTokenEnd
3238     ScanQuotedToken-----other----->ScanQuotedToken
3239     ScanQuotedTokenEnd-----quote----->ScanNormalTokenWithQuote
3240     ScanQuotedTokenEnd-----delim----->ScanStart (2)
3241     ScanQuotedTokenEnd-----other----->ScanNormalToken
3242     ScanNormalToken-----quote----->ScanNormalTokenWithQuote
3243     ScanNormalToken-----delim----->ScanStart (3)
3244     ScanNormalToken-----other----->ScanNormalToken
3245     ScanNormalTokenWithQuote-----quote----->ScanNormalTokenWithQuote
3246     ScanNormalTokenWithQuote-----other----->ScanNormalToken
3247    
3248     (1) output a null token if allowed
3249     (2) output a token, stripping quotes (if the dequoted token is
3250     empty, output a null token if allowed)
3251     (3) output a token; no quote stripping
3252    
3253     If the quote character is #0, it's taken to mean that the routine
3254     should not check for quoted substrings.}
3255    
3256     {clear the tokens string list, set the return value to zero}
3257     Tokens.Clear;
3258     Result := 0;
3259    
3260     {if the input string is empty or the delimiter list is empty or
3261     the quote character is found in the delimiter list, return zero
3262     tokens found}
3263     if (S = '') or
3264     (Delims = '') or
3265     CharExistsL(Delims, QuoteChar) then
3266     Exit;
3267    
3268     {start off in the normal scanning state}
3269     State := ScanStart;
3270    
3271     {the first token starts at position 1}
3272     TokenStart := 1;
3273    
3274     {read through the entire string}
3275     for Inx := 1 to length(S) do begin
3276    
3277     {get the current character}
3278     CurChar := S[Inx];
3279    
3280     {process the character according to the current state}
3281     case State of
3282     ScanStart :
3283     begin
3284     {if the current char is the quote character, switch states}
3285     if (QuoteChar <> #0) and (CurChar = QuoteChar) then
3286     State := ScanQuotedToken
3287    
3288     {if the current char is a delimiter, output a null token}
3289     else if CharExistsL(Delims, CurChar) then begin
3290    
3291     {if allowed to, output a null token}
3292     if AllowNulls then begin
3293     Tokens.Add('');
3294     inc(Result);
3295     end;
3296    
3297     {set the start of the next token to be one character after
3298     this delimiter}
3299     TokenStart := succ(Inx);
3300     end
3301    
3302     {otherwise, the current char is starting a normal token, so
3303     switch states}
3304     else
3305     State := ScanNormalToken
3306     end;
3307    
3308     ScanQuotedToken :
3309     begin
3310     {if the current char is the quote character, switch states}
3311     if (CurChar = QuoteChar) then
3312     State := ScanQuotedTokenEnd
3313     end;
3314    
3315     ScanQuotedTokenEnd :
3316     begin
3317     {if the current char is the quote character, we have a token
3318     consisting of two (or more) quoted substrings, so switch
3319     states}
3320     if (CurChar = QuoteChar) then
3321     State := ScanNormalTokenWithQuote
3322    
3323     {if the current char is a delimiter, output the token
3324     without the quotes}
3325     else if CharExistsL(Delims, CurChar) then begin
3326    
3327     {if the token is empty without the quotes, output a null
3328     token only if allowed to}
3329     if ((Inx - TokenStart) = 2) then begin
3330     if AllowNulls then begin
3331     Tokens.Add('');
3332     inc(Result);
3333     end
3334     end
3335    
3336     {else output the token without the quotes}
3337     else begin
3338     Tokens.Add(Copy(S, succ(TokenStart), Inx - TokenStart - 2));
3339     inc(Result);
3340     end;
3341    
3342     {set the start of the next token to be one character after
3343     this delimiter}
3344     TokenStart := succ(Inx);
3345    
3346     {switch states back to the start state}
3347     State := ScanStart;
3348     end
3349    
3350     {otherwise it's a (complex) normal token, so switch states}
3351     else
3352     State := ScanNormalToken
3353     end;
3354    
3355     ScanNormalToken :
3356     begin
3357     {if the current char is the quote character, we have a
3358     complex token with at least one quoted substring, so switch
3359     states}
3360     if (QuoteChar <> #0) and (CurChar = QuoteChar) then
3361     State := ScanNormalTokenWithQuote
3362    
3363     {if the current char is a delimiter, output the token}
3364     else if CharExistsL(Delims, CurChar) then begin
3365     Tokens.Add(Copy(S, TokenStart, Inx - TokenStart));
3366     inc(Result);
3367    
3368     {set the start of the next token to be one character after
3369     this delimiter}
3370     TokenStart := succ(Inx);
3371    
3372     {switch states back to the start state}
3373     State := ScanStart;
3374     end;
3375     end;
3376    
3377     ScanNormalTokenWithQuote :
3378     begin
3379     {if the current char is the quote character, switch states
3380     back to scanning a normal token}
3381     if (CurChar = QuoteChar) then
3382     State := ScanNormalToken;
3383     end;
3384    
3385     end;
3386     end;
3387    
3388     {we need to process the (possible) final token: first assume that
3389     the current character index is just beyond the end of the string}
3390     Inx := succ(length(S));
3391    
3392     {if we are in the scanning quoted token state, we've read an opening
3393     quote, but no closing one; increment the token start value}
3394     if (State = ScanQuotedToken) then
3395     inc(TokenStart)
3396    
3397     {if we've finished scanning a quoted token, we've read both quotes;
3398     increment the token start value, and decrement the current index}
3399     else if (State = ScanQuotedTokenEnd) then begin
3400     inc(TokenStart);
3401     dec(Inx);
3402     end;
3403    
3404     {if the final token is not empty, output the token}
3405     if (TokenStart < Inx) then begin
3406     Tokens.Add(Copy(S, TokenStart, Inx - TokenStart));
3407     inc(Result);
3408     end
3409     {otherwise the final token is empty, so output a null token if
3410     allowed to}
3411     else if AllowNulls then begin
3412     Tokens.Add('');
3413     inc(Result);
3414     end;
3415     end;
3416    
3417     function ContainsOnlyL(const S, Chars : String;
3418     var BadPos : Cardinal) : Boolean;
3419     var
3420     I : Cardinal;
3421     begin
3422     if (S = '') then begin
3423     Result := False;
3424     BadPos := 0;
3425     end else begin
3426     for I := 1 to Length(S) do begin
3427     if (not CharExistsL(Chars, S[I])) then begin
3428     BadPos := I;
3429     Result := False;
3430     Exit;
3431     end;
3432     end;
3433     Result := True;
3434     BadPos := 0;
3435     end;
3436     end;
3437    
3438    
3439    
3440     function ContainsOtherThanL(const S, Chars : String;
3441     var BadPos : Cardinal) : Boolean;
3442     var
3443     I : Cardinal;
3444     begin
3445     if (S = '') then begin
3446     Result := False;
3447     BadPos := 0;
3448     end else begin
3449     for I := 1 to Length(S) do begin
3450     if (CharExistsL(Chars, S[I])) then begin
3451     BadPos := I;
3452     Result := True;
3453     Exit;
3454     end;
3455     end;
3456     Result := False;
3457     BadPos := 0;
3458     end;
3459     end;
3460    
3461    
3462    
3463     function IsChAlphaL(C : Char) : Boolean;
3464     {-Returns true if Ch is an alpha}
3465     begin
3466     Result := Windows.IsCharAlpha(C);
3467     end;
3468    
3469    
3470    
3471     function IsChNumericL(C : Char; const Numbers : String) : Boolean; {!!.02}
3472     {-Returns true if Ch in numeric set}
3473     begin
3474     Result := CharExistsL(Numbers, C);
3475     end;
3476    
3477    
3478    
3479     function IsChAlphaNumericL(C : Char; const Numbers : String) : Boolean; {!!.02}
3480     {-Returns true if Ch is an alpha or numeric}
3481     begin
3482     Result := Windows.IsCharAlpha(C) or CharExistsL(Numbers, C);
3483     end;
3484    
3485    
3486    
3487     function IsStrAlphaL(const S : String) : Boolean;
3488     {-Returns true if all characters in string are an alpha}
3489     var
3490     I : Cardinal;
3491     begin
3492     Result := false;
3493     if (length(S) > 0) then begin
3494     for I := 1 to Length(S) do
3495     if not Windows.IsCharAlpha(S[I]) then
3496     Exit;
3497     Result := true;
3498     end;
3499     end;
3500    
3501    
3502    
3503     function IsStrNumericL(const S, Numbers : String) : Boolean;
3504     {-Returns true if all characters in string are in numeric set}
3505     var
3506     i : Cardinal;
3507     begin
3508     Result := false;
3509     if (length(S) > 0) then begin
3510     for i := 1 to Length(S) do
3511     if not CharExistsL(Numbers, S[i]) then
3512     Exit;
3513     Result := true;
3514     end;
3515     end;
3516    
3517    
3518    
3519     function IsStrAlphaNumericL(const S, Numbers : String) : Boolean;
3520     {-Returns true if all characters in string are alpha or numeric}
3521     var
3522     i : Cardinal;
3523     begin
3524     Result := false;
3525     if (length(S) > 0) then begin
3526     for I := 1 to Length(S) do
3527     if (not Windows.IsCharAlpha(S[i])) and
3528     (not CharExistsL(Numbers, S[i])) then
3529     Exit;
3530     Result := true;
3531     end;
3532     end;
3533    
3534    
3535     function StrWithinL(const S, SearchStr : string;
3536     Start : Cardinal;
3537     var Position : Cardinal) : boolean;
3538     var
3539     TmpStr : string;
3540     begin
3541     TmpStr := S;
3542     if (Start > 1) then
3543     System.Delete(TmpStr, 1, Start-1);
3544     Position := pos(SearchStr, TmpStr);
3545     if (Position > 0) then begin
3546     Position := Position + Start - 1;
3547     Result := True;
3548     end else
3549     Result := False;
3550     end;
3551    
3552    
3553     end.

  ViewVC Help
Powered by ViewVC 1.1.20