-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgf_strings.pas
725 lines (631 loc) · 17.1 KB
/
gf_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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
unit gf_strings;
{$I gf_base.inc}
(* ************************************************************
MOZILLA PUBLIC LICENSE STATEMENT
-----------------------------------------------------------
The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is "gf_strings.pas".
The Initial Developer of the Original Code is Marek Jedlinski
<eristic@lodz.pdi.net> (Poland).
Portions created by Marek Jedlinski are
Copyright (C) 2000, 2001. All Rights Reserved.
-----------------------------------------------------------
Contributor(s):
-----------------------------------------------------------
History:
-----------------------------------------------------------
To do:
-----------------------------------------------------------
Released: 20 August 2001
-----------------------------------------------------------
URLs:
- original author's software site:
http://www.lodz.pdi.net/~eristic/free/index.html
http://go.to/generalfrenetics
Email addresses (at least one should be valid)
<eristic@lodz.pdi.net>
<cicho@polbox.com>
<cicho@tenbit.pl>
************************************************************ *)
interface
uses SysUtils, Windows, Classes,
TntClasses, WideStrUtils, WideStrings;
type
TCaseCycle = (
ccLower, ccMixed, ccUpper
);
function GetLetterCase( const aStr : string ) : TCaseCycle;
procedure DelimTextToStrs(
AStrs: TStrings;
const Value: string;
const AchDelim : Char );
procedure DelimTextToWStrs( AStrs: TTntStrings;
const Value: WideString ;
const AchDelim : WideChar );
function StrToCSVText( const aStr : string; const aDelim : char; const QuoteAll : boolean ) : string;
procedure CSVTextToStrs(
aList : TStrings;
const aStr : string;
const aDelim : char );
procedure SplitString( aList : TStrings; aStr : string; const aDelim : char );
function CountChars( const ch : char; const s : string ) : integer;
procedure CharToChar( var s : wideString; const oldchar, newchar : char );
function RemoveAccelChar( const s : wideString ) : wideString;
procedure CollapseSpaces( var s : string );
function TrimPunct( s : string ) : string;
function TailMatch( const LongerString, ShorterString : string ) : boolean;
procedure UnquoteString( var s : string );
function StringsToString( const AStrs : TStrings; const aDelim : char; const WrapWithDelim : boolean ) : string;
function MatchMask(source, pattern: String): Boolean;
procedure StripControlChars( var s : string );
function GetWordChars : string;
function ExpandMetaChars( line : wideString ) : wideString;
function GetIndentOfLine (const S: string): integer;
function TryUTF8ToWideString(const s: string): wideString;
function FirstLineFromString( const str: WideString; const MaxLen : integer ) : WideString;
implementation
uses gf_misc, TntSystem;
function GetLetterCase( const aStr : string ) : TCaseCycle;
const
_max_check_len = 255; // do not waste time checking multi-kilobyte strings
var
i, len : integer;
HadUpper, HadLower : boolean;
begin
result := ccLower;
HadUpper := false;
HadLower := false;
len := length( aStr );
if ( len > _max_check_len ) then
len := _max_check_len;
for i := 1 to len do
begin
if IsCharAlpha( aStr[i] ) then
begin
if IsCharUpper( aStr[i] ) then
HadUpper := true
else
HadLower := true;
if ( HadUpper and HadLower ) then
begin
result := ccMixed;
exit;
end;
end;
end;
if HadUpper then
result := ccUpper
else
result := ccLower;
end; // GetLetterCase
function ExpandMetaChars( line : wideString ) : wideString;
var
i, linelen : integer;
wasmeta : boolean;
ch : wideChar;
begin
result := '';
if ( line = '' ) then exit;
linelen := length( line );
wasmeta := false;
if ( linelen > 1 ) then
begin
if (( line[1] = '"' ) and ( line[linelen] = '"' )) then
begin
delete( line, linelen, 1 );
delete( line, 1, 1 );
dec( linelen, 2 );
end;
end;
for i := 1 to linelen do
begin
ch := line[i];
case ch of
'\' : begin
if wasmeta then
begin
result := result + ch;
wasmeta := false;
end
else
wasmeta := true;
end;
'n' : begin
if wasmeta then
result := result + #13
else
result := result + ch;
wasmeta := false;
end;
't' : begin
if wasmeta then
result := result + #9
else
result := result + ch;
wasmeta := false;
end;
else
begin
wasmeta := false;
result := result + ch;
end;
end;
end;
end; // ExpandMetaChars
procedure StripControlChars( var s : string );
var
i, l : integer;
begin
l := length( s );
for i := l downto 1 do
begin
if ( s[i] < #32 ) then
delete( s, i, 1 );
end;
end; // StripControlChars
function TailMatch( const LongerString, ShorterString : string ) : boolean;
var
llonger, lshorter : integer;
begin
llonger := length( LongerString );
lshorter := length( shorterstring );
if ( lshorter > llonger ) then
begin
result := false;
exit;
end;
{
'abcdxxx' 7
' xxx' 3, pos 5
}
result := ( copy( LongerString, succ( llonger - lshorter ), lshorter ) = ShorterString );
end; // TailMatch
function TrimPunct( s : string ) : string;
var
slen : integer;
begin
while ( s <> '' ) do
begin
if IsCharAlphaNumericA( s[1] ) then
break
else
delete( s, 1, 1 );
end;
slen := length( s );
while ( slen > 0 ) do
begin
if IsCharAlphaNumericA( s[slen] ) then
break
else
begin
delete( s, slen, 1 );
dec( slen );
end;
end;
result := s;
end; // TrimPunct
function StrToCSVText(
const aStr : string;
const aDelim : char;
const QuoteAll : boolean ) : string;
// aDelim MUST NOT be double quote '"'
var
qp : integer;
MustQuote : boolean;
begin
result := aStr;
if ( aStr = '' ) then
begin
if QuoteAll then
result := '""';
exit;
end;
MustQuote := ( pos( aDelim, aStr ) > 0 );
if ( pos( '"', result ) > 0 ) then
begin
MustQuote := true;
qp := 1;
repeat
if ( result[qp] <> '"' ) then
begin
inc( qp );
end
else
begin
insert( '"', result, qp );
inc( qp, 2 );
end;
until ( qp > length( result ));
end;
if ( MustQuote or QuoteAll ) then
result := '"' + result + '"';
end; // StrToCSVText
procedure CSVTextToStrs(
aList : TStrings;
const aStr : string;
const aDelim : char );
// DelimTextToStrs relies on all strings being wrapped in quotes,
// but this is not a requirement for delimited fields in a CSV file.
// (Otherwise, DelimTextToStrs breaks on every space, in addition to
// breaking on delimiter characters)
// This routine does what DelimTextToStrs does but ONLY breaks on
// the delimiter char. Only those strings must be quoted which contain
// quotes or contain the delimiter character.
var
s : string;
InQuotes : boolean;
p, l : integer;
ch, prevch : char;
begin
if ( aStr = '' ) then exit;
s := '';
p := 0;
prevch := #0;
l := length( aStr );
InQuotes := false;
aList.BeginUpdate; // so that we can feed e.g. ListBox.Items to this routine
try
while ( p < l ) do
begin
inc( p );
ch := aStr[p];
if ( ch = aDelim ) then
begin
if ( InQuotes and ( prevch <> '"' )) then
begin
s := s + ch;
end
else
begin
aList.Add( s );
s := '';
InQuotes := false;
// prevch := #0;
end;
end
else
if ( ch = '"' ) then
begin
if ( prevch = '"' ) then
begin
// dooubled quotes are "escaped",
// i.e. they're real quote characters
// rather than group words
s := s + '"';
ch := #0;
end
else
begin
if ( InQuotes or ( s <> '' )) then
begin
if InQuotes then
begin
// nothing; we will see what to do
// when we get the next char
if ( p = l ) then
Ch := aDelim; // otherwise we'll lose the last field if it is a blank string
end
else
begin
// IMPOSSIBLE: the string was not
// quoted, so it can't contain
// embedded quote characters
raise EConvertError.Create( 'Unmatched double quote in string at pos ' + inttostr( p ));
end;
end
else
begin
InQuotes := true;
ch := #0;
end;
end;
end
else
begin
s := s + ch;
end;
prevch := ch;
end;
if (( s <> '' ) or ( prevch = aDelim )) then
aList.Add( s );
finally
aList.EndUpdate;
end;
end; // CSVTextToStrs
procedure SplitString( aList : TStrings; aStr : string; const aDelim : char );
var
p : integer;
s : string;
begin
// the simplest split routine of all:
// just splits at delimiter, does not care about anything else.
// DO NOT USE FOR PROPERLY FORMATTED CSV DATA!
p := pos( aDelim, aStr );
while ( p > 0 ) do
begin
s := copy( aStr, 1, pred( p ));
delete( aStr, 1, p );
if ( s <> '' ) then
aList.Add( s );
p := pos( aDelim, aStr );
end;
if ( aStr <> '' ) then
aList.Add( aStr );
end; // SplitString
procedure DelimTextToStrs( AStrs: TStrings;
const Value: string ;
const AchDelim : Char );
var
P, P1 : PChar;
S : string;
chDelim : char ;
begin
chDelim := AchDelim ;
AStrs.BeginUpdate;
try
// AStrs.Clear;
P := PChar(Value);
while P^ in [#1..' '] do
P := CharNext(P);
while P^ <> #0 do
begin
if ( P^ = '"' ) then
S := AnsiExtractQuotedStr(P, '"')
else
begin
P1 := P;
while (P^ > ' ') and ( P^ <> chDelim ) do
P := CharNext(P);
SetString(S, P1, P - P1);
end;
AStrs.Add(S);
while P^ in [#1..' '] do
P := CharNext(P);
if P^ = chDelim then // P^ = ',' then
repeat
P := CharNext(P);
until not (P^ in [#1..' ']);
end; // while
finally
AStrs.EndUpdate;
end;
end; // DelimTextToStrs
procedure DelimTextToWStrs( AStrs: TTntStrings;
const Value: WideString ;
const AchDelim : WideChar );
var
P, P1 : PWideChar;
S : WideString;
chDelim : WideChar ;
begin
chDelim := AchDelim ;
AStrs.BeginUpdate;
try
// AStrs.Clear;
P := PWideChar(Value);
while P^ in [#1..' '] do
P := CharNextW(P);
while P^ <> #0 do
begin
if ( P^ = '"' ) then
S := WideExtractQuotedStr(P, '"')
else
begin
P1 := P;
while (P^ > ' ') and ( P^ <> chDelim ) do
P := CharNextW(P);
SetString(S, P1, P - P1);
end;
AStrs.Add(S);
while P^ in [#1..' '] do
P := CharNextW(P);
if P^ = chDelim then // P^ = ',' then
repeat
P := CharNextW(P);
until not (P^ in [#1..' ']);
end; // while
finally
AStrs.EndUpdate;
end;
end; // DelimTextToStrsUnicode
function CountChars( const ch : char; const s : string ) : integer;
var
i : integer;
begin
result := 0;
for i := 1 to length( s ) do
if s[i] = ch then inc( result );
end; // CountChars
procedure CharToChar( var s : wideString; const oldchar, newchar : char );
var
p : integer;
begin
if ( oldchar = newchar ) then exit;
p := pos( oldchar, s );
while ( p > 0 ) do
begin
s[p] := wideChar(newchar);
p := pos( oldchar, s );
end;
end; // CharToChar
function RemoveAccelChar( const s : widestring ) : wideString;
var
p : integer;
begin
result := s;
p := pos( '&', result );
if ( p > 0 ) then
delete( result, p, 1 );
end; // RemoveAccelChar
procedure CollapseSpaces( var s : string );
const
SPCARRAY : array[1..2] of string = ( #32#32, #9#9 );
var
i, p : integer;
begin
for i := 1 to 2 do
begin
p := pos( SPCARRAY[i], s );
while ( p > 0 ) do
begin
delete( s, p, 1 );
p := pos( SPCARRAY[i], s );
end;
end;
end; // CollapseSpaces
procedure UnquoteString( var s : string );
var
l : integer;
begin
l := length( s );
if ( l > 2 ) then
begin
if (( s[1] = '"' ) and ( s[l] = '"' )) then
begin
delete( s, l, 1 );
delete( s, 1, 1 );
end;
end;
end; // UnquoteString
function StringsToString(
const AStrs : TStrings;
const aDelim : char;
const WrapWithDelim : boolean ) : string;
var
i : integer;
s : string;
begin
result := '';
for i := 1 to AStrs.Count do
begin
s := AStrs[pred( i )];
if ( s <> '' ) then
begin
if ( i = 1 ) then
result := s
else
result := result + aDelim + s;
end;
end;
if (( result <> '' ) and WrapWithDelim ) then
result := Format( '%s%s%s', [aDelim, result, aDelim] );
end; // StringsToString
function MatchMask(source, pattern: String): Boolean;
{
From: stidolph@magnet.com (David Stidolph)
Subject: [delphi] String Pattern matching
Date: Tue, 27 Jun 1995 10:01:18 -0400
There are many times when you need to compare two strings, but want to use
wild cards in the match - all last names that begin with 'St', etc. The
following is a piece of code I got from Sean Stanley in Tallahassee Florida
in C. I translated it into Delphi an am uploading it here for all to use.
I have not tested it extensivly, but the original function has been tested
quite thoughly.
I would love feedback on this routine - or peoples changes to it. I want to
forward them to Sean to get him to release more tidbits like this.
This function takes two strings and compares them. The first string
can be anything, but should not contain pattern characters (* or ?).
The pattern string can have as many of these pattern characters as you want.
For example: MatchStrings('David Stidolph','*St*') would return True.
Orignal code by Sean Stanley in C
Rewritten in Delphi by David Stidolph
}
var
pSource: Array [0..255] of Char;
pPattern: Array [0..255] of Char;
function MatchPattern(element, pattern: PChar): Boolean;
function IsPatternWild(pattern: PChar): Boolean;
begin
Result := StrScan(pattern,'*') <> nil;
if not Result then Result := StrScan(pattern,'?') <> nil;
end;
begin
if 0 = StrComp(pattern,'*') then
Result := True
else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
Result := False
else if element^ = Chr(0) then
Result := True
else begin
case pattern^ of
'*': if MatchPattern(element,@pattern[1]) then
Result := True
else
Result := MatchPattern(@element[1],pattern);
'?': Result := MatchPattern(@element[1],@pattern[1]);
else
if element^ = pattern^ then
Result := MatchPattern(@element[1],@pattern[1])
else
Result := False;
end;
end;
end;
begin
StrPCopy(pSource,source);
StrPCopy(pPattern,pattern);
Result := MatchPattern(pSource,pPattern);
end; // MatchMask
function GetWordChars : string;
var
i : integer;
ch : char;
begin
result := '';
for i := 33 to 255 do
begin
ch := chr( i );
if IsCharAlphaA( ch ) then
result := result + ch;
end;
end; // GetWordChars
function GetIndentOfLine (const S: string): integer;
begin
// count blanks and tabs in this string
Result := 0;
while (Result < length( S )) and
(S[Result+1] In [' ',#9])
do
Inc( Result );
end;
function TryUTF8ToWideString(const s: string): wideString;
begin
Result:= UTF8ToWideString(s);
if Result= '' then
Result:= s;
end;
function FirstLineFromString( const str: WideString; const MaxLen : integer ) : WideString;
var
i, l, max, lineBreakAt : integer;
begin
result:= str;
l := length( str );
if ( l > 0 ) then
begin
if ( MaxLen < l ) then
max := MaxLen
else
max := l;
lineBreakAt:= max+1;
for i := 1 to max do
begin
if ( result[i] < #32 ) then
begin
lineBreakAt:= i;
break;
end;
end;
delete( result, lineBreakAt, l );
if (lineBreakAt > max) and (l > max) then begin
result:= trimright(result) + '...';
end;
end;
end; // FirstLineFromString
end.