-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.pas
615 lines (533 loc) · 20.1 KB
/
strings.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
(* Misc collection of string routines from all over the place... *)
UNIT Strings;
INTERFACE
{$DEFINE DELPHI}
{$IFDEF DELPHI}
uses sysutils;
{$ENDIF}
TYPE
TDir = (L,R);
tCharSet = set of char;
FUNCTION Str2Int(Str: String; (* Converts String to Integer *)
VAR Code: Integer): Integer;
FUNCTION Int2Str(I: Integer): String; (* Converts Integer to String *)
FUNCTION StripSlash(Str: String): String; (* Strip trailing '\' *)
FUNCTION AddSlash(Str: String): String; (* Add trailing '\' *)
FUNCTION PadStr(Str: String; (* Pad String with characters *)
Ch: Char; (* Character to pad with *)
Num: Byte; (* Number of places to pad to *)
Dir: TDir): String; (* Direction to pad in (L,R)*)
FUNCTION UpCaseStr(Str: String): String; (* Convert string to uppercase *)
FUNCTION LowCaseStr(Str: String): String; (* Convert string to lowercase *)
FUNCTION NameForm(Str: String): String; (* Convert string to Name format *)
FUNCTION StripExt(Str: String): String; (* Strip Extension from filename *)
FUNCTION AddExt(Str,Ext: String): String; (* Add Extension to filename *)
function BackwardPos (const SubString, Data : string; (* Searches backward, returns position *)
StartPosition : byte) : byte;
function CountPos (var SubString, Data : string; (* Returns the position of a numbered substring*)
Count : byte) : byte;
function ForwardPos (var SubString, Data : string; (* Searches forward, returns position *)
StartPosition : byte) : byte;
function IsCharactersInString (Chars : tCharSet; Data : string) : boolean;
function IsInString (SubString : string; Data : string) : boolean;
function Occurrence (var SubString, Data : string) : byte;
function Replace (var Data, SubString1, SubString2 : string) : string;
function SearchPos (var SubString, Data : string; StartPosition : byte) : byte;
function StringBackwardPos (SubString, Data : string; StartPosition : byte) : byte;
function StringCentered (Data : string) : string;
function StringCountPos (SubString, Data : string; Count : byte) : byte;
function StringDoubled (Data : string) : string;
function StringFill (Count : byte; (* Returns string consisting of Count copies of substring *)
SubString : string) : string;
function StringFixed (Data : string; FixedLength : byte) : string;
function StringFixedCentered (Data : string; FixedLength : byte) : string;
function StringFixedRight (Data : string; FixedLength : byte) : string;
function StringForwardPos (SubString, Data : string; StartPosition : byte) : byte;
function StringOccurrence (SubString, Data : string) : byte;
function StringReplace1 (Data, SubString1, SubString2 : string) : string; (* Replaces all occurances of substring *)
function StringSearchPos (SubString, Data : string; StartPosition : byte) : byte;
function StringSpace (Count : byte) : string;
function StringStripped (Data : string) : string;
function StringZeroStripped (Data : string) : string;
procedure Fill (var Data : string; Count : byte; SubString : string);
procedure StringRemoveLead (var Data : string; Character : char); (* Left Trim of specified Char *)
procedure StringRemoveTrail (var Data : string; Character : char); (* Right trim of specified char *)
function StripChar(var Data : string; (* Removes all instances of Char from string *)
OneChar : Char) : string;
Function sLeft(cString : String; iChars : Integer) : string;
Function sRight(cString : String; iChars : Integer) : string;
FUNCTION ParseCount( cString: STRING; cChar: CHAR ): INTEGER;
FUNCTION Parse( cString: STRING; nIndex: INTEGER; cChar: CHAR ): STRING;
FUNCTION LastCharPos(tStr : string; tChar : char) : integer;
function Encrypt(const S: String; Key: Word): String;
function Decrypt(const S: String; Key: Word): String;
Function StrReverse(Const astr : string) : string;
IMPLEMENTATION
function Encrypt(const S: String; Key: Word): String;
var
I: byte;
const
C1 = 52845;
C2 = 22719;
begin
//Result[0] := S[0];
setlength(result,length(s));
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(result[I]) + Key) * C1 + C2;
end;
end;
function Decrypt(const S: String; Key: Word): String;
var
I: byte;
const
C1 = 52845;
C2 = 22719;
begin
//Result[0] := S[0];
setlength(result,length(s));
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(S[I]) + Key) * C1 + C2;
end;
//setlength(result,length(s));
end;
FUNCTION Str2Int(Str: String;
VAR Code: Integer): Integer;
VAR I: Integer;
BEGIN
VAL(Str,I,Code);
Str2Int := I;
END;
FUNCTION Int2Str(I: Integer): String;
VAR S: String;
BEGIN
STR(I,S);
Int2Str := S;
END;
FUNCTION StripSlash(Str: String): String;
BEGIN
IF Str[Length(Str)] = '\' THEN
StripSlash := COPY(Str,1,Length(Str)-1);
END;
FUNCTION AddSlash(Str: String): String;
BEGIN
IF Str[Length(Str)] <> '\' THEN
AddSlash := Str + '\';
END;
FUNCTION PadStr(Str: String;
Ch: Char;
Num: Byte;
Dir: TDir): String;
VAR
TempStr: String;
{$IFDEF DELPHI}
B: Longint;
{$ELSE}
B:Byte;
{$ENDIF}
BEGIN
TempStr := '';
IF Length(Str) < Num THEN
BEGIN
FOR B := Length(Str) TO Num DO TempStr := TempStr + Ch;
CASE Dir OF
L: PadStr := TempStr + Str;
R: PadStr := Str + TempStr;
END;
END
ELSE
BEGIN
FOR B := 1 TO Num DO TempStr := TempStr + Str[B];
PadStr := TempStr;
END;
END;
FUNCTION UpCaseStr(Str: String): String;
VAR
TempStr: String;
{$IFDEF DELPHI}
B: Longint;
{$ELSE}
B:Byte;
{$ENDIF}
BEGIN
TempStr := Str;
FOR B := 1 TO Length(Str) DO TempStr[B] := UpCase(TempStr[B]);
UpCaseStr := TempStr;
END;
FUNCTION LowCaseStr(Str: String): String;
VAR
TempStr: String;
{$IFDEF DELPHI}
B: Longint;
{$ELSE}
B:Byte;
{$ENDIF}
BEGIN
TempStr := Str;
FOR B := 1 TO Length(Str) DO IF TempStr[B] IN ['A'..'Z'] THEN
TempStr[B] := CHR(ORD(TempStr[B])+32);
LowCaseStr := TempStr;
END;
FUNCTION NameForm(Str: String): String;
VAR
TempStr: String;
{$IFDEF DELPHI}
Pos: Longint;
{$ELSE}
Pos:Byte;
{$ENDIF}
BEGIN
TempStr := lowercase(Str); // lowercase the entire thing to begin
TempStr[1] := UpCase(TempStr[1]); // Upper the first one
FOR Pos := 2 TO Length(TempStr) DO
IF TempStr[Pos] = ' ' THEN
TempStr[Pos+1] := UpCase(TempStr[Pos+1]);
{
ELSE
IF TempStr[Pos] IN ['A'..'Z'] THEN
TempStr[Pos] := CHR(ORD(TempStr[Pos])+32);
}
NameForm := TempStr;
END;
FUNCTION StripExt(Str: String): String;
VAR DotPos: Byte;
BEGIN
DotPos := POS('.',Str);
IF DotPos > 1 THEN StripExt := COPY(Str,1,DotPos-1)
ELSE StripExt := Str;
END;
FUNCTION AddExt(Str,Ext: String): String;
VAR DotPos: Byte;
BEGIN
DotPos := POS('.',Str);
IF (DotPos > 1) AND (DotPos < 10) THEN AddExt := COPY(Str,1,DotPos) + Ext
ELSE IF DotPos = 0 THEN AddExt := Str + '.' + Ext;
END;
{ *****************************************
* Procedures *
***************************************** }
{ Returns the position of a substring, beginning at specified position and
searching forward }
function ForwardPos (var SubString, Data : string; StartPosition : byte) : byte;
var Position : integer;
begin
if StartPosition < 1 then StartPosition := 1;
Position := Pos (SubString, Copy (Data, StartPosition, Length(Data) - StartPosition + 1));
if Position > 0 then ForwardPos := Pred(Position + StartPosition)
else ForwardPos := 0; { Not found }
end;
{ Returns the position of a substring, beginning at specified position and
searching forward }
function StringForwardPos (SubString, Data : string; StartPosition : byte) : byte;
begin
StringForwardPos := ForwardPos (SubString, Data, StartPosition);
end;
{ Returns the position of a substring, beginning at specified position and
searching backward }
function BackwardPos (const SubString, Data : string; StartPosition : byte) : byte;
begin
if StartPosition > Length(Data) then StartPosition := Length(Data);
BackwardPos := Pos (SubString, Copy (Data, 1, StartPosition));
end;
{ Returns the position of a substring, beginning at specified position and
searching backward }
function StringBackwardPos (SubString, Data : string; StartPosition : byte) : byte;
begin
StringBackwardPos := BackwardPos (SubString, Data, StartPosition);
end;
{ Returns the position of a character closests to a certain position }
function SearchPos (var SubString, Data : string; StartPosition : byte) : byte;
var ForwardIndex, BackwardIndex : byte;
begin
ForwardIndex := ForwardPos (SubString, Data, StartPosition);
BackwardIndex := BackwardPos (SubString, Data, StartPosition);
{ Return the position to the closest found substring }
if StartPosition-ForwardIndex >= BackwardIndex-StartPosition then
SearchPos := ForwardIndex else SearchPos := BackwardIndex;
end;
{ Returns the position of a character closest to a certain position }
function StringSearchPos (SubString, Data : string; StartPosition : byte) : byte;
begin
StringSearchPos := SearchPos (SubString, Data, StartPosition);
end;
{ Returns the position of a numbered substring in a string (for example,
if Count is set to two, the position of the second location of the substring
is returned). }
function CountPos (var SubString, Data : string; Count : byte) : byte;
var Index : integer; Position : byte;
begin
Position := 0; { Reset search position }
for Index := 1 to Count do
Position := ForwardPos (SubString, Data, Succ(Position));
CountPos := Position; { Zero if not found }
end;
{ Returns the position of a numbered substring in a string (for example,
if Count is set to two, the position of the second location of the substring
is returned). }
function StringCountPos (SubString, Data : string; Count : byte) : byte;
begin
StringCountPos := CountPos (SubString, Data, Count);
end;
{ Returns the number of occurrences of a substring inside a string }
function Occurrence (var SubString, Data : string) : byte;
var Count, Position : integer;
begin
{ Reset position and counter }
Position := 0; Count := 0;
{ Search for substring and go forward if one is found }
repeat
Position := ForwardPos (SubString, Data, Succ(Position));
if Position <> 0 then Inc (Count);
until (Position = 0);
Occurrence := Count; { Return number of occurrences }
end;
{ Returns the number of occurrences of a substring inside a string }
function StringOccurrence (SubString, Data : string) : byte;
begin
StringOccurrence := Occurrence (SubString, Data);
end;
{ Returns TRUE if substring is found inside string }
function IsInString (SubString : string; Data : string) : boolean;
begin
IsInString := (Pos (SubString, Data) <> 0);
end;
{ Returns TRUE if any character in specified set is found inside string }
function IsCharactersInString (Chars : tCharSet; Data : string) : boolean;
var Index : integer;
begin
IsCharactersInString := FALSE; { Assume that no character is found }
for Index := 1 to Length(Data) do
if Data[Index] in Chars then begin
{ Character in set found - break }
IsCharactersInString := TRUE;
Exit;
end;
end;
{ Replaces a sub-string in a string with another sub-string }
function Replace (var Data, SubString1, SubString2 : string) : string;
var Index, Position : integer; TempString : string;
begin
{ Reset index variable and clear temporary string }
Index := 0; TempString := '';
while Index < Length(Data) do begin
Inc (Index); { Go forward one step }
Position := ForwardPos (SubString1, Data, Index);
{ Copy data if not search string found }
if Index <> Position then TempString := TempString + Data[Index]
else begin
{ Replace SubString1 with SubString2 }
TempString := TempString + SubString2;
Index := Position + Pred(Length (SubString1));
end;
end;
Replace := TempString; { Return replaced string }
end;
{ Replaces a sub-string in a string with another sub-string }
function StringReplace1 (Data, SubString1, SubString2 : string) : string;
begin
StringReplace1 := Replace (Data, SubString1, SubString2);
end;
{ Generates a string filled with specified number of copies of the
specified substring. }
procedure Fill (var Data : string; Count : byte; SubString : string);
var Index : byte;
begin
Data := ''; { Clear data string }
if Length(SubString) = 1 then begin
{ Use memory filling if data is a character }
if Count > 0 then FillChar (Data[1], Count, Ord(SubString[1]));
{$IFDEF DELPHI}
setlength(Data,Count); { Set string length }
{$ELSE}
data[0] := chr(count);
{$ENDIF}
end else for Index := 1 to Count do Data := Data + SubString;
end;
{ Generates a string filled with specified number of copies of the
specified substring. }
function StringFill (Count : byte; SubString : string) : string;
var TempString : string;
begin
{ Avoid overlap of filling - use temporary string for filling }
Fill (TempString, Count, SubString);
StringFill := TempString;
end;
{ Generates a string containing specified number of blank spaces }
function StringSpace (Count : byte) : string;
begin
StringSpace := StringFill (Count, #32);
end;
{ Removes all leading characters in string }
procedure StringRemoveLead (var Data : string; Character : char);
var Length : byte absolute Data;
begin
{ Delete all leading characters }
while (Data[1] = Character) and (Length > 1) do Delete (Data, 1, 1);
end;
{ Removes all trailing characters in string }
procedure StringRemoveTrail (var Data : string; Character : char);
var Length : byte absolute Data;
begin
{ Delete all trailing characters }
while (Data[Length] = Character) and (Length > 1) do Dec (Length);
end;
{ Removes blank spaces before and after text inside a string }
(* NOTE: USE TRIM! *)
function StringStripped (Data : string) : string;
begin
{ Delete all leading and trailing spaces }
StringRemoveLead (Data, #32);
StringRemoveTrail (Data, #32);
{ Return stripped string }
StringStripped := Data;
end;
{ Removes leading and trailing zeroes from a text string }
function StringZeroStripped (Data : string) : string;
begin
{ Delete all leading and trailing zeroes }
StringRemoveLead (Data, '0');
StringRemoveTrail (Data, '0');
{ Remove comma signs or add one leading zero if comma first in string }
if (Data[Length(Data)] = '.') then Delete (Data, Length(Data), 1); { Delete last character }
if (Data[1] = '.') then Data := '0' + Data;
StringZeroStripped := Data;
end;
{ Centers text inside a string }
function StringCentered (Data : string) : string;
var FixSpace, StrippedStr : string; StartLength : byte;
begin
{$IFDEF DELPHI}
StrippedStr := Trim(Data); { Removes leading and trailing spaces }
{$ELSE}
StrippedStr := stringstripped(Data);
{$ENDIF}
{ Calculate number of spaces available for centering }
FixSpace := StringSpace((Length(Data) - Length(StrippedStr)) div 2);
StartLength := Length(Data);
Data := FixSpace + StrippedStr + FixSpace;
if Length(Data) < StartLength then Data := Chr(32) + Data;
StringCentered := Data;
end;
{ Fixes the length of a string by inserting spaces or removing characters }
function StringFixed (Data : string; FixedLength : byte) : string;
var Length : byte absolute Data;
begin
if (Data <> '') then Data := Copy (Data, 1, FixedLength);
while (Length < FixedLength) do Insert (#32, Data, Succ(Length));
StringFixed := Data;
end;
{ Centers and fixes the length of a string }
function StringFixedCentered (Data : string; FixedLength : byte) : string;
begin
StringFixedCentered := StringCentered(StringFixed(Data, FixedLength));
end;
{ Right justifies and fixes the length of a string }
function StringFixedRight (Data : string; FixedLength : byte) : string;
begin
if FixedLength >= Length(Data) then
StringFixedRight := StringSpace (FixedLength - Length(Data)) + Data
else StringFixedRight := StringFixed (Data, FixedLength);
end;
{ Inserts one blank space between each character inside string }
function StringDoubled (Data : string) : string;
var Index : byte; TempString : string;
begin
TempString := '';
for Index := 1 to Length(Data) do TempString := TempString + Data[Index] + #32;
StringDoubled := Copy (TempString, 1, Pred(Length(TempString)));
end;
function StripChar(var Data : string; OneChar : Char) : string;
begin
repeat
delete(data,pos(Onechar,data),1);
until pos(Onechar,data) = 0;
StripChar := Data;
end;
Function sLeft(cString : String; iChars : Integer) : string;
begin
sLeft := copy(cString,1,iChars);
end;
Function sRight(cString : String; iChars : Integer) : string;
begin
sright := copy(cString,Length(cString)-ichars+1,iChars);
end;
{*****************************************************************************
* Function ...... ParseCount()
* Purpose ....... To count the number of tokens in a string
* Parameters .... cString String to count tokens in
* cChar Token separator
* Returns ....... Number of tokens in <cString>
* Notes ......... Uses function StripChar
*****************************************************************************}
FUNCTION ParseCount( cString: STRING; cChar: CHAR ): INTEGER;
var oldlen,newlen : integer;
BEGIN
oldlen := length(cString);
newlen := length(stripchar(cString,cChar));
ParseCount := oldlen - newlen;
END;
{*****************************************************************************
* Function ...... Parse()
* Purpose ....... To parse out tokens from a string
* Parameters .... cString String to parse
* nIndex Token number to return
* cChar Token separator
* Returns ....... Token <nIndex> extracted from <cString>
* Notes ......... If <nIndex> is greater than the number of tokens in
* <cString> then a null string is returned.
* . Uses function sLeft, sRight, and ParseCount
* Author ........ Martin Richardson
* Date .......... September 30, 1992
*****************************************************************************}
FUNCTION Parse( cString: STRING; nIndex: INTEGER; cChar: CHAR ): STRING;
VAR
i: INTEGER;
cResult: STRING;
BEGIN
cString := cString + cChar;
IF nIndex > (ParseCount( cString, cChar )) THEN
cResult := ''
ELSE BEGIN
FOR i := 1 TO nIndex DO BEGIN
cResult := sLeft( cString, POS( cChar, cString ) - 1 );
cString := sRight(cString, LENGTH(cString) - POS(cChar, cString));
END { Next I };
END { IF };
Parse := cResult;
END;
{**************************************************************}
{* LastCharPos returns the last occurance of tChar in tStr *}
{* 4/23/98 JDG *}
{**************************************************************}
FUNCTION LastCharPos(tStr : string;
tChar : char) : integer;
var count :integer;
begin
// Author's note: would be more efficient to start at the end of the string
// and work your way to the beginning, stopping after you find the first
// occurance of tChar, which would be the last occurance in the string.
count := length(tstr)+1;
repeat
dec(count)
until (tStr[count] = tChar) or (count =1);
if tchar = tstr[count] then LastCharPos := count
else count := 0; // return 0 if not found
END; {LastCharPos}
{**************************************************************}
{* StrReverse returns the reverse of a string *}
{* 9/06/98 DG *}
{**************************************************************}
Function StrReverse(Const astr : string) : string;
// Reverses a string
var x,y : integer;
begin
y := length(astr);
setlength(result,y);
for x := 1 to y do
begin
result[y-(x-1)] := astr[x];
end;
end;
BEGIN
END.