-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathcutils.pas
1420 lines (1240 loc) · 37.3 KB
/
cutils.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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Copyright (c) 1998-2002 by Florian Klaempfl
This unit implements some support functions
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
{# This unit contains some generic support functions which are used
in the different parts of the compiler.
}
unit cutils;
{$i fpcdefs.inc}
interface
type
Tcharset=set of char;
var
internalerrorproc : procedure(i:longint);
{# Returns the minimal value between @var(a) and @var(b) }
function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
{# Returns the maximum value between @var(a) and @var(b) }
function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
{# Return value @var(i) aligned on @var(a) boundary }
function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
{ if you have an address aligned using "oldalignment" and add an
offset of (a multiple of) offset to it, this function calculates
the new minimally guaranteed alignment
}
function newalignment(oldalignment: longint; offset: int64): longint;
{# Return @var(b) with the bit order reversed }
function reverse_byte(b: byte): byte;
function used_align(varalign,minalign,maxalign:shortint):shortint;
function isbetteralignedthan(new, org, limit: cardinal): boolean;
function size_2_align(len : longint) : shortint;
function packedbitsloadsize(bitlen: int64) : int64;
procedure Replace(var s:string;s1:string;const s2:string);
procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
procedure ReplaceCase(var s:string;const s1,s2:string);
Function MatchPattern(const pattern,what:string):boolean;
function upper(const c : char) : char;
function upper(const s : string) : string;
function upper(const s : ansistring) : ansistring;
function lower(const c : char) : char;
function lower(const s : string) : string;
function lower(const s : ansistring) : ansistring;
function trimbspace(const s:string):string;
function trimspace(const s:string):string;
function space (b : longint): string;
function PadSpace(const s:string;len:longint):string;
function GetToken(var s:string;endchar:char):string;
procedure uppervar(var s : string);
function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
function DStr(l:longint):string;
{# Returns true if the string s is a number }
function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
{# Returns true if value is a power of 2, the actual
exponent value is returned in power.
}
function ispowerof2(value : int64;out power : longint) : boolean;
function nextpowerof2(value : int64; out power: longint) : int64;
function backspace_quote(const s:string;const qchars:Tcharset):string;
function octal_quote(const s:string;const qchars:Tcharset):string;
function maybequoted(const s:string):string;
function maybequoted(const s:ansistring):ansistring;
{# If the string is quoted, in accordance with pascal, it is
dequoted and returned in s, and the function returns true.
If it is not quoted, or if the quoting is bad, s is not touched,
and false is returned.
}
function DePascalQuote(var s: ansistring): Boolean;
function CompareStr(const S1, S2: string): Integer;
function CompareText(S1, S2: string): integer;
{ releases the string p and assignes nil to p }
{ if p=nil then freemem isn't called }
procedure stringdispose(var p : pshortstring);{$ifdef USEINLINE}inline;{$endif}
{ allocates mem for a copy of s, copies s to this mem and returns }
{ a pointer to this mem }
function stringdup(const s : string) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
{# Allocates memory for the string @var(s) and copies s as zero
terminated string to that allocated memory and returns a pointer
to that mem
}
function strpnew(const s : string) : pchar;
{# makes the character @var(c) lowercase, with spanish, french and german
character set
}
function lowercase(c : char) : char;
{ makes zero terminated string to a pascal string }
{ the data in p is modified and p is returned }
function pchar2pshortstring(p : pchar) : pshortstring;
{ inverse of pchar2pshortstring }
function pshortstring2pchar(p : pshortstring) : pchar;
{ Ansistring (pchar+length) support }
procedure ansistringdispose(var p : pchar;length : longint);
function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
{Lzw encode/decode to compress strings -> save memory.}
function minilzw_encode(const s:string):string;
function minilzw_decode(const s:string):string;
Function nextafter(x,y:double):double;
{$ifdef ver2_0}
{ RTL routines not available yet in 2.0.x }
function SwapEndian(const AValue: SmallInt): SmallInt;
function SwapEndian(const AValue: Word): Word;
function SwapEndian(const AValue: LongInt): LongInt;
function SwapEndian(const AValue: DWord): DWord;
function SwapEndian(const AValue: Int64): Int64;
function SwapEndian(const AValue: QWord): QWord;
{$endif ver2_0}
implementation
uses
SysUtils;
var
uppertbl,
lowertbl : array[char] of char;
function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
{
return the minimal of a and b
}
begin
if a<=b then
min:=a
else
min:=b;
end;
function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
{
return the minimal of a and b
}
begin
if a<=b then
min:=a
else
min:=b;
end;
function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
{
return the maximum of a and b
}
begin
if a>=b then
max:=a
else
max:=b;
end;
function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
{
return the maximum of a and b
}
begin
if a>=b then
max:=a
else
max:=b;
end;
function newalignment(oldalignment: longint; offset: int64): longint;
var
localoffset: longint;
begin
localoffset:=longint(offset);
while (localoffset mod oldalignment)<>0 do
oldalignment:=oldalignment div 2;
newalignment:=oldalignment;
end;
function reverse_byte(b: byte): byte;
const
reverse_nible:array[0..15] of 0..15 =
(%0000,%1000,%0100,%1100,%0010,%1010,%0110,%1110,
%0001,%1001,%0101,%1101,%0011,%1011,%0111,%1111);
begin
reverse_byte:=(reverse_nible[b and $f] shl 4) or reverse_nible[b shr 4];
end;
function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
{
return value <i> aligned <a> boundary
}
begin
{ for 0 and 1 no aligning is needed }
if a<=1 then
result:=i
else
begin
if i<0 then
result:=((i-a+1) div a) * a
else
result:=((i+a-1) div a) * a;
end;
end;
function size_2_align(len : longint) : shortint;
begin
if len>16 then
size_2_align:=32
else if len>8 then
size_2_align:=16
else if len>4 then
size_2_align:=8
else if len>2 then
size_2_align:=4
else if len>1 then
size_2_align:=2
else
size_2_align:=1;
end;
function packedbitsloadsize(bitlen: int64) : int64;
begin
case bitlen of
1,2,4,8:
result := 1;
{ 10 bits can never be split over 3 bytes via 1-8-1, because it }
{ always starts at a multiple of 10 bits. Same for the others. }
3,5,6,7,9,10,12,16:
result := 2;
{$ifdef cpu64bitalu}
{ performance penalty for unaligned 8 byte access is much }
{ higher than for unaligned 4 byte access, at least on ppc, }
{ so use 4 bytes even in some cases where a value could }
{ always loaded using a single 8 byte load (e.g. in case of }
{ 28 bit values) }
11,13,14,15,17..32:
result := 4;
else
result := 8;
{$else cpu64bitalu}
else
result := 4;
{$endif cpu64bitalu}
end;
end;
function isbetteralignedthan(new, org, limit: cardinal): boolean;
var
cnt: cardinal;
begin
cnt:=2;
while (cnt <= limit) do
begin
if (org and (cnt-1)) > (new and (cnt-1)) then
begin
result:=true;
exit;
end
else if (org and (cnt-1)) < (new and (cnt-1)) then
begin
result:=false;
exit;
end;
cnt:=cnt*2;
end;
result:=false;
end;
function used_align(varalign,minalign,maxalign:shortint):shortint;
begin
{ varalign : minimum alignment required for the variable
minalign : Minimum alignment of this structure, 0 = undefined
maxalign : Maximum alignment of this structure, 0 = undefined }
if (minalign>0) and
(varalign<minalign) then
used_align:=minalign
else
begin
if (maxalign>0) and
(varalign>maxalign) then
used_align:=maxalign
else
used_align:=varalign;
end;
end;
procedure Replace(var s:string;s1:string;const s2:string);
var
last,
i : longint;
begin
s1:=upper(s1);
last:=0;
repeat
i:=pos(s1,upper(s));
if i=last then
i:=0;
if (i>0) then
begin
Delete(s,i,length(s1));
Insert(s2,s,i);
last:=i;
end;
until (i=0);
end;
procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
var
last,
i : longint;
begin
s1:=upper(s1);
last:=0;
repeat
i:=pos(s1,upper(s));
if i=last then
i:=0;
if (i>0) then
begin
Delete(s,i,length(s1));
Insert(s2,s,i);
last:=i;
end;
until (i=0);
end;
procedure ReplaceCase(var s:string;const s1,s2:string);
var
last,
i : longint;
begin
last:=0;
repeat
i:=pos(s1,s);
if i=last then
i:=0;
if (i>0) then
begin
Delete(s,i,length(s1));
Insert(s2,s,i);
last:=i;
end;
until (i=0);
end;
Function MatchPattern(const pattern,what:string):boolean;
var
found : boolean;
i1,i2 : longint;
begin
i1:=0;
i2:=0;
if pattern='' then
begin
result:=(what='');
exit;
end;
found:=true;
repeat
inc(i1);
if (i1>length(pattern)) then
break;
inc(i2);
if (i2>length(what)) then
break;
case pattern[i1] of
'?' :
found:=true;
'*' :
begin
found:=true;
if (i1=length(pattern)) then
i2:=length(what)
else
if (i1<length(pattern)) and (pattern[i1+1]<>what[i2]) then
begin
if i2<length(what) then
dec(i1)
end
else
if i2>1 then
dec(i2);
end;
else
found:=(pattern[i1]=what[i2]) or (what[i2]='?');
end;
until not found;
if found then
begin
found:=(i2>=length(what)) and
(
(i1>length(pattern)) or
((i1=length(pattern)) and
(pattern[i1]='*'))
);
end;
result:=found;
end;
function upper(const c : char) : char;
{
return uppercase of c
}
begin
upper:=uppertbl[c];
end;
function upper(const s : string) : string;
{
return uppercased string of s
}
var
i : longint;
begin
for i:=1 to length(s) do
upper[i]:=uppertbl[s[i]];
upper[0]:=s[0];
end;
function upper(const s : ansistring) : ansistring;
{
return uppercased string of s
}
var
i : longint;
begin
setlength(upper,length(s));
for i:=1 to length(s) do
upper[i]:=uppertbl[s[i]];
end;
function lower(const c : char) : char;
{
return lowercase of c
}
begin
lower:=lowertbl[c];
end;
function lower(const s : string) : string;
{
return lowercased string of s
}
var
i : longint;
begin
for i:=1 to length(s) do
lower[i]:=lowertbl[s[i]];
lower[0]:=s[0];
end;
function lower(const s : ansistring) : ansistring;
{
return lowercased string of s
}
var
i : longint;
begin
setlength(lower,length(s));
for i:=1 to length(s) do
lower[i]:=lowertbl[s[i]];
end;
procedure uppervar(var s : string);
{
uppercase string s
}
var
i : longint;
begin
for i:=1 to length(s) do
s[i]:=uppertbl[s[i]];
end;
procedure initupperlower;
var
c : char;
begin
for c:=#0 to #255 do
begin
lowertbl[c]:=c;
uppertbl[c]:=c;
case c of
'A'..'Z' :
lowertbl[c]:=char(byte(c)+32);
'a'..'z' :
uppertbl[c]:=char(byte(c)-32);
end;
end;
end;
function DStr(l:longint):string;
var
TmpStr : string[32];
i : longint;
begin
Str(l,TmpStr);
i:=Length(TmpStr);
while (i>3) do
begin
dec(i,3);
if TmpStr[i]<>'-' then
insert('.',TmpStr,i+1);
end;
DStr:=TmpStr;
end;
function trimbspace(const s:string):string;
{
return s with all leading spaces and tabs removed
}
var
i,j : longint;
begin
j:=1;
i:=length(s);
while (j<i) and (s[j] in [#9,' ']) do
inc(j);
trimbspace:=Copy(s,j,i-j+1);
end;
function trimspace(const s:string):string;
{
return s with all leading and ending spaces and tabs removed
}
var
i,j : longint;
begin
i:=length(s);
while (i>0) and (s[i] in [#9,' ']) do
dec(i);
j:=1;
while (j<i) and (s[j] in [#9,' ']) do
inc(j);
trimspace:=Copy(s,j,i-j+1);
end;
function space (b : longint): string;
var
s: string;
begin
space[0] := chr(b);
s[0] := chr(b);
FillChar (S[1],b,' ');
space:=s;
end;
function PadSpace(const s:string;len:longint):string;
{
return s with spaces add to the end
}
begin
if length(s)<len then
PadSpace:=s+Space(len-length(s))
else
PadSpace:=s;
end;
function GetToken(var s:string;endchar:char):string;
var
i : longint;
quote : char;
begin
GetToken:='';
s:=TrimSpace(s);
if (length(s)>0) and
(s[1] in ['''','"']) then
begin
quote:=s[1];
i:=1;
while (i<length(s)) do
begin
inc(i);
if s[i]=quote then
begin
{ Remove double quote }
if (i<length(s)) and
(s[i+1]=quote) then
begin
Delete(s,i,1);
inc(i);
end
else
begin
GetToken:=Copy(s,2,i-2);
Delete(s,1,i);
exit;
end;
end;
end;
GetToken:=s;
s:='';
end
else
begin
i:=pos(EndChar,s);
if i=0 then
begin
GetToken:=s;
s:='';
exit;
end
else
begin
GetToken:=Copy(s,1,i-1);
Delete(s,1,i);
exit;
end;
end;
end;
function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
begin
str(e,result);
end;
function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
{
return string of value i
}
begin
str(i,result);
end;
function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
{
return string of value i
}
begin
str(i,result);
end;
function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
{
return string of value i
}
begin
str(i,result);
end;
function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
{
return string of value i, but always include a + when i>=0
}
begin
str(i,result);
if i>=0 then
result:='+'+result;
end;
function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
{
is string a correct number ?
}
var
w : integer;
l : longint;
begin
val(s,l,w);
// remove warning
l:=l;
is_number:=(w=0);
end;
function ispowerof2(value : int64;out power : longint) : boolean;
{
return if value is a power of 2. And if correct return the power
}
var
hl : int64;
i : longint;
begin
if value and (value - 1) <> 0 then
begin
ispowerof2 := false;
exit
end;
hl:=1;
ispowerof2:=true;
for i:=0 to 63 do
begin
if hl=value then
begin
power:=i;
exit;
end;
hl:=hl shl 1;
end;
ispowerof2:=false;
end;
function nextpowerof2(value : int64; out power: longint) : int64;
{
returns the power of 2 >= value
}
var
i : longint;
begin
result := 0;
power := -1;
if ((value <= 0) or
(value >= $4000000000000000)) then
exit;
result := 1;
for i:=0 to 63 do
begin
if result>=value then
begin
power := i;
exit;
end;
result:=result shl 1;
end;
end;
function backspace_quote(const s:string;const qchars:Tcharset):string;
var i:byte;
begin
backspace_quote:='';
for i:=1 to length(s) do
begin
if (s[i]=#10) and (#10 in qchars) then
backspace_quote:=backspace_quote+'\n'
else if (s[i]=#13) and (#13 in qchars) then
backspace_quote:=backspace_quote+'\r'
else
begin
if s[i] in qchars then
backspace_quote:=backspace_quote+'\';
backspace_quote:=backspace_quote+s[i];
end;
end;
end;
function octal_quote(const s:string;const qchars:Tcharset):string;
var i:byte;
begin
octal_quote:='';
for i:=1 to length(s) do
begin
if s[i] in qchars then
begin
if ord(s[i])<64 then
octal_quote:=octal_quote+'\'+octstr(ord(s[i]),3)
else
octal_quote:=octal_quote+'\'+octstr(ord(s[i]),4);
end
else
octal_quote:=octal_quote+s[i];
end;
end;
function maybequoted(const s:ansistring):ansistring;
const
{$IFDEF MSWINDOWS}
FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'{', '}', '''', '`', '~'];
{$ELSE}
FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'{', '}', '''', ':', '\', '`', '~'];
{$ENDIF}
var
s1 : ansistring;
i : integer;
quoted : boolean;
begin
quoted:=false;
s1:='"';
for i:=1 to length(s) do
begin
case s[i] of
'"' :
begin
quoted:=true;
s1:=s1+'\"';
end;
' ',
#128..#255 :
begin
quoted:=true;
s1:=s1+s[i];
end;
else begin
if s[i] in FORBIDDEN_CHARS then
quoted:=True;
s1:=s1+s[i];
end;
end;
end;
if quoted then
maybequoted:=s1+'"'
else
maybequoted:=s;
end;
function maybequoted(const s:string):string;
const
{$IFDEF MSWINDOWS}
FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'{', '}', '''', '`', '~'];
{$ELSE}
FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'{', '}', '''', ':', '\', '`', '~'];
{$ENDIF}
var
s1 : string;
i : integer;
quoted : boolean;
begin
quoted:=false;
s1:='"';
for i:=1 to length(s) do
begin
case s[i] of
'"' :
begin
quoted:=true;
s1:=s1+'\"';
end;
' ',
#128..#255 :
begin
quoted:=true;
s1:=s1+s[i];
end;
else begin
if s[i] in FORBIDDEN_CHARS then
quoted:=True;
s1:=s1+s[i];
end;
end;
end;
if quoted then
maybequoted:=s1+'"'
else
maybequoted:=s;
end;
function DePascalQuote(var s: ansistring): Boolean;
var
destPos, sourcePos, len: Integer;
t: string;
ch: Char;
begin
DePascalQuote:= false;
len:= length(s);
if (len >= 1) and (s[1] = '''') then
begin
{Remove quotes, exchange '' against ' }
destPos := 0;
sourcepos:=1;
while (sourcepos<len) do
begin
inc(sourcePos);
ch := s[sourcePos];
if ch = '''' then
begin
inc(sourcePos);
if (sourcePos <= len) and (s[sourcePos] = '''') then
{Add the quote as part of string}
else
begin
SetLength(t, destPos);
s:= t;
Exit(true);
end;
end;
inc(destPos);
t[destPos] := ch;
end;
end;
end;
function pchar2pshortstring(p : pchar) : pshortstring;
var
w,i : longint;
begin
w:=strlen(p);
for i:=w-1 downto 0 do
p[i+1]:=p[i];
p[0]:=chr(w);
pchar2pshortstring:=pshortstring(p);
end;
function pshortstring2pchar(p : pshortstring) : pchar;
var
w,i : longint;
begin
w:=length(p^);
for i:=1 to w do
p^[i-1]:=p^[i];
p^[w]:=#0;
pshortstring2pchar:=pchar(p);
end;
function lowercase(c : char) : char;
begin
case c of
#65..#90 : c := chr(ord (c) + 32);
#154 : c:=#129; { german }
#142 : c:=#132; { german }
#153 : c:=#148; { german }
#144 : c:=#130; { french }
#128 : c:=#135; { french }
#143 : c:=#134; { swedish/norge (?) }
#165 : c:=#164; { spanish }
#228 : c:=#229; { greek }
#226 : c:=#231; { greek }
#232 : c:=#227; { greek }
end;
lowercase := c;
end;
function strpnew(const s : string) : pchar;
var
p : pchar;
begin