-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathustrings.pas
682 lines (595 loc) · 17.8 KB
/
ustrings.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
{
*********************************************************************
* Unidade Strings *
* --------------- *
* *
* Autor : Paulo Sergio Cardoso *
* Data : 03-11-1991 *
* *
* Contem rotinas gerais de transformacao de numeros reias em *
* cadeias string, alem de rotinas gerais de manipulacao de strings. *
* *
* Alteracoes *
* ------------------------------------------ *
* Alteracao *
* Autor *
* Data *
* ------------------------------------------ *
* *
*********************************************************************
}
{
Grundy NewBrain Emulator Pro Made by Despsoft
Copyright (c) 2004, Despoinidis Chris
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A
NY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}
Unit
UStrings;
Interface
Function NumToStr (X:Real;N:Byte):String;
Function Replicate (C:Char;N:Byte):String;
Function Space (N:Byte):String;
Function RealToStr (X:Real;N:Byte;M:Byte):String;
Function ScientToStr (X:Real;N:Byte):String;
Function sStr (X:LongInt; N:Byte):String;
Function sStrZero (X:LongInt; N:Byte):String;
Function AllTrim (S:String):String;
Function Fixa (Frase:String;Qtd:Byte):String;
Function Upper (S:String):String;
Function XorText (Frase:String; XorByte:Byte):String;
Function XorNumeroSerie (Frase:String):String;
function ValNum (cStr:String; var nError: Integer):LongInt;
Function ValReal (cStr:String):Real;
Function TodaysDate:String;
Function TodaysDateTime:String;
Function FormatDate (S:String):String;
Function UnFormatDate (S:String):String;
Function FormatTime (S:String):String;
Procedure CalculaDiaSemana (nDia,nMes,nAno:Integer; Var nDiaS,nTotDias:Integer);
Function CompDateTime (S1,S2:String):Integer;
Function ExtractDate (S:String):String;
Function ExtractTime (S:String):String;
Function ComputeDTime (Date,Time:String):String;
function HexToInt(S: String): LongInt;
function ToHex(nNumber, nSize: LongInt): String;
function IsValidHex(Var s:String): Boolean;
function InBetween(s:String;chs:String='()'): Boolean;
function IsValidBin(Var s:String): Boolean;
function IsValidOkt(Var s:String): Boolean;
function BinToInt(s:String): Longint;
function OktToInt(s:String): Longint;
function IsValidInt(var s: String): Boolean;
function GetValidInteger(S:String;Var res:Integer): Boolean;
function GetFirstPart(var S, part1: String; Delimeters: string; const AsWord:
Boolean = False): Boolean;
Implementation
uses sysutils,classes;
{
***************************************************************************
* Funcao NumToStr *
* --------------- *
* Entradas : X - Numero Real *
* N - Tamanho da String de Saida *
* A funcao devolve uma string contendo o numero X convertido. *
* Se for um numero inteiro, devolve a string sem valores apos *
* a virgula. Se for menor que 1e-5 devolve em notacao cientifica.*
***************************************************************************
}
Function NumToStr (X:Real;N:Byte):String;
Var
S:String;
Begin
If X=0 then Begin
Str (0:N,S);
NumToStr := S
End;
If (Frac(X)<1e-6) and (Int(X)<>0)
then Begin
Str (X:N:0,S);
NumToStr := S;
Exit;
End;
If (Abs(X)>1e-3) and (Abs(X)<1e+3)
then Begin
Str (X:N:5,S);
NumToStr := S;
Exit
End;
Str (X:N,S);
NumToStr := S
End;
{
********************************************************************
* Funcao Replicate *
* ---------------- *
* Entradas : C - Caractere *
* N - Byte *
* Devolve um string contendo N caracteres, todos eles iguais a C. *
********************************************************************
}
Function Replicate;
Var
S : String;
I : Byte;
Begin
S := '';
For I := 1 to N do
S := S + C;
Replicate := S
End;
Function Space(N:Byte):String;
Begin
Space := Replicate (' ',N);
End;
{
*********************************************************************
* Funcao Fixa *
* ----------- *
* Entradas : Frase - String *
* Qtd - Byte *
* Devolve uma string que contem a Frase truncada em Qtd caracteres. *
*********************************************************************
}
Function Fixa (Frase:String;Qtd:Byte):String;
Var
Aux : String;
Begin
Aux := Frase;
While Length(Aux)<Qtd do
Aux := #32+Aux;
Fixa := Aux
End;
{
**************************************************************************
* Funcao RealToStr *
* ---------------- *
* Entradas : X - Real *
* N,M - Byte *
* Devolve uma string contendo a transformacao do numero X em um string. *
* A string contera o numero com N caracteres, com M bytes decimais (con- *
* tando o ponto flutuante). *
**************************************************************************
}
Function RealToStr (X:Real;N:Byte;M:Byte):String;
Var
Aux : String;
Begin
Str (X:N:M,Aux);
RealToStr := Aux
End;
{
**********************************************************************
* Funcao ScientToStr *
* ------------------ *
* Entradas : X - Numero Real *
* N - Byte *
* Devolve uma string de N caracteres contendo o numero X em notacao *
* cientifica (9.999E-99). *
**********************************************************************
}
Function ScientToStr (X:Real;N:Byte):String;
Var
Aux : String;
Begin
Str (X:N,Aux);
ScientToStr := Aux
End;
{
*************************************************************
* Funcao sStr *
* ---------------- *
* Entradas : X - Inteiro Longo (32 bits) *
* N - Byte *
* Devolve uma string de N bytes contendo o numero X *
*************************************************************
}
Function sStr (X:LongInt;N:Byte):String;
Var
Aux : String;
Begin
Str (X:N,Aux);
sStr := Aux
End;
{
*************************************************************
* Funcao sStrZero *
* ----------------- *
* Entradas : X - Inteiro Longo (32 bits) *
* N - Byte *
* Devolve uma string de N bytes contendo o numero X, colo- *
* cando zeros a esquerda se n for maior que o numero de *
* digitos do numero inteiro X *
*************************************************************
}
Function sStrZero (X:LongInt;N:Byte):String;
Var
Aux : String;
I : Byte;
Begin
Str (X:N,Aux);
For i := 1 to Length(Aux) do
If Aux[i]=' ' then Aux[i] := '0';
sStrZero := Aux
End;
{
*****************************************************************
* Funcao AllTrim *
* -------------- *
* Entradas : S - String *
* Devolve a String S, retirando todos os espacos a esquerda e a *
* direita. *
*****************************************************************
}
Function AllTrim (S:String):String;
Var
I : Byte;
Begin
While (Length(S)>0) and (S[1]=#32) do
Delete (S,1,1);
While (Length(S)>0) and (S[Length(S)]=#32) do
Delete (S,Length(S),1);
AllTrim := S
End;
////
Function Upper (S:String):String;
Var
X : String;
I : Byte;
Begin
X := S;
For i := 1 to Length(S) do
X[i] := UpCase(S[i]);
Upper := X
End;
Function XorText (Frase:String; XorByte:Byte):String;
Var
I : Byte;
Begin
For i := 1 to Length(Frase) do
if (i mod 2)=0 then
Frase[i] := Char(Byte(Frase[i]) xor XorByte)
else
Frase[i] := Char(Byte(Frase[i]) xor (XorByte+1));
XorText := Frase
End;
Function XorNumeroSerie (Frase:String):String;
Var
I : Byte;
Begin
For i := 1 to Length(Frase) do
Frase[i] := Char(Byte(Frase[i]) xor ($70+i));
XorNumeroSerie := Frase
End;
Function ValNum (cStr:String; var nError: Integer):LongInt;
Var
nAux : LongInt;
Begin
Val (AllTrim(cStr),nAux,nError);
ValNum := nAux
End;
Function ValReal (cStr:String):Real;
Var
nError : Integer;
nAux : Real;
Begin
Val (AllTrim(cStr),nAux,nError);
ValReal := nAux
End;
Function FormatDate (S:String):String;
Begin
FormatDate := Copy(S,7,2)+'/'+Copy(S,5,2)+'/'+Copy(S,1,4);
End;
Function TodaysDate:String;
Var
nDia,nMes,nAno,nDiaS : Word;
Begin
{GetDate (nAno,nMes,nDia,nDiaS);}
TodaysDate := sStrZero(nDia,2)+'/'+sStrZero(nMes,2)+'/'+sStrZero(nAno,4)
End;
Function TodaysDateTime:String;
Var
nDia,nMes,nAno,nDiaS : Word;
nHora,nMin,nSeg,nSeg100 : Word;
Begin
{
GetDate (nAno,nMes,nDia,nDiaS);
GetTime (nHora,nMin,nSeg,nSeg100);
}
TodaysDateTime := sStrZero(nAno,4)+sStrZero(nMes,2)+sStrZero(nDia,2)+
sStrZero(nHora,2)+sStrZero(nMin,2)+sStrZero(nSeg,2);
End;
Function CompDateTime (S1,S2:String):Integer;
Begin
If S1>S2 then CompDateTime := 1
else If S1<S2 then CompDateTime := -1
else CompDateTime := 0;
End;
Function UnFormatDate (S:String):String;
Begin
UnFormatDate := Copy(S,7,4)+Copy(S,4,2)+Copy(S,1,2);
End;
Function FormatTime (S:String):String;
Begin
FormatTime := Copy(S,1,2)+'h'+Copy(S,3,2)+'min'
End;
Procedure CalculaDiaSemana (nDia,nMes,nAno:Integer; Var nDiaS,nTotDias:Integer);
Const
ZELLER_OFFSET = -1;
aMeses : Array [1..12] Of Byte = (31,28,31,30,31,30,31,31,30,31,30,31);
Var
Yr,Mn,n1,n2 : Integer;
aMes:Array [1..12] Of Byte;
i:integer;
Begin
for i:= 1 to 12 do
aMes[i]:=Ameses[i];
yr := nAno;
mn := nMes;
If (mn<3) then
Begin
Inc (mn,12);
Dec (yr)
End;
n1 := (26*(mn + 1)) div 10 ;
n2 := ((125*LongInt(yr)) div 100) ;
nDiaS := ((nDia + n1 + n2 - (yr div 100) + (yr div 400) + ZELLER_OFFSET) mod 7) ;
If ((nAno mod 4)=0) and ((nAno mod 40)<>0) then aMes[2] := 29
else aMes[2] := 28;
nTotDias := aMes[nMes]
End;
Function ExtractDate (S:String):String;
Begin
ExtractDate := Copy(S,7,2)+'/'+Copy(S,5,2)+'/'+Copy(S,1,4);
End;
Function ExtractTime (S:String):String;
Begin
ExtractTime := Copy(S,9,2)+'h'+Copy(S,11,2)+'min'+Copy(S,13,2)+'s';
End;
Function ComputeDTime (Date,Time:String):String;
Begin
ComputeDTime := Copy(Date,7,4)+Copy(Date,4,2)+Copy(Date,1,2)+Copy(Time,1,2)+Copy(Time,4,2)+Copy(Time,9,2);
End;
//// Various functions
function IsValidHex(Var s:String): Boolean;
Const ValidHex=['0'..'9','A'..'F'];
STR_OPERATION= '+-*/^!@#$<>%&*\|?{}';
Var l,i:Integer;
r:boolean;
Begin
s:=Uppercase(s);
Result:=False;
r:=false;
for i := 1 to Length(STR_OPERATION) do
r:=r or (pos(STR_OPERATION[i],s)>1);//operator not at start i.e. +2000
if r then exit;
l:=Length(s);
if (l>1) then
Begin
if s[1]='$' then
s:=Copy(s,2,maxint)+'h';
if (s[l]='h') or (s[l]='H') then
Begin
Result:=True;
for i := 1 to l-1 do
Result:=Result and (s[i] in ValidHex) ;
end;
end;
end;
function IsValidBin(Var s:String): Boolean;
Const ValidBin=['0','1'];
Var l,i:Integer;
Begin
s:=Uppercase(s);
Result:=False;
l:=Length(s);
if (l>1) then
Begin
if (s[l]='b') or (s[l]='B') then
Begin
Result:=True;
for i := 1 to l-1 do
Result:=Result and (s[i] in ValidBin) ;
end;
end;
end;
function IsValidOkt(Var s:String): Boolean;
Const ValidOkt=['0','1','2','3','4','5','6','7'];
Var l,i:Integer;
Begin
s:=Uppercase(s);
Result:=False;
l:=Length(s);
if (l>1) then
Begin
if (s[l]='q') or (s[l]='Q') then
Begin
Result:=True;
for i := 1 to l-1 do
Result:=Result and (s[i] in ValidOkt) ;
end;
end;
end;
function HexToInt(S: String): LongInt;
const
cHexDig: String[16] = '0123456789ABCDEF';
var
nMult,
nAux: LongInt;
I, nPos: Integer;
begin
S := AllTrim(UPPERCASE(S));
nAux := 0;
nMult := 1;
for i := Length(S) downto 1 do begin
nPos := Pos(S[i], cHexDig);
if nPos<>0 then
begin
nAux := nAux+(nPos-1)*nMult;
nMult := nMult*16;
end;
end;
HexToInt := nAux;
end;
Function BinToInt(s:String): Longint;
Const ValidBin=['0','1'];
Var j,pow:Integer;
Begin
Result:=0;
pow:=1;
for j := Length(s) downto 1 do
Begin
if S[j] in ValidBin then
Begin
if s[j]='1' then
Result:=Result+Pow;
Pow:=Pow*2;
end;
End;
end;
Function OktToInt(s:String): Longint;
Const ValidOkt=['0','1','2','3','4','5','6','7'];
Var j,pow:Integer;
Begin
Result:=0;
pow:=1;
for j := Length(s) downto 1 do
Begin
if S[j] in ValidOkt then
Begin
Result:=Result+Pow*strtoint(s[j]);
Pow:=Pow*8;
end;
End;
end;
function ToHex(nNumber, nSize: LongInt): String;
const
cCharHex: String[16] = '0123456789ABCDEF';
var
cAux: String;
I: Integer;
begin
cAux := '';
for i := 1 to nSize do begin
cAux := cCharHex[nNumber mod 16+1]+cAux;
nNumber := nNumber div 16;
end;
ToHex := cAux;
end;
function InBetween(s:String;chs:String='()'): Boolean;
Begin
Result:=False;
s:=Trim(s);
if Length(s)<2 then
Exit;
Result:= (s[1]=chs[1]) and (s[Length(s)]=chs[2]) ;
end;
Function GetValidInteger(S:String;Var res:Integer):Boolean;
Begin
Result:=False;
if InBetween(s,'''''') or InBetween(s,'''''') then
Begin
s:=StringReplace(s,'''','',[rfReplaceAll]);
s:=StringReplace(s,'"','',[rfReplaceAll]);
if Length(s)=1 then //just a char
Begin
Res:=Ord(s[1]);
Result:=True;
end
Else ;//A string
end
Else
if IsValidHex(s) then
Begin
Res:=HexToInt(s);
result:=True;
end
Else
if IsValidBin(s) then
Begin
Res:=BinToInt(s);
result:=True;
end
Else
if IsValidOkt(s) then
Begin
Res:=OktToInt(s);
result:=True;
end
Else
if IsValidInt(s) then
Begin
Res:=Strtoint(s);
result:=True;
end;
end;
function IsValidInt(Var s:String): Boolean;
Const ValidInt=['0'..'9'];
Var l,i:Integer;
k:Integer;
Begin
Result:=False;
l:=Length(s);
if (l>=1) then
Begin
Result:=True;
if (s[1]='+') or (s[1]='-') then
k:=2
Else
k:=1;
for i := k to l do
Result:=Result and (s[i] in ValidInt) ;
end;
end;
function GetFirstPart(var S, part1: String; Delimeters: string; const AsWord:
Boolean = False): Boolean;
Var delims:TStringlist;
lastp,p,i:Integer;
Begin
Result:=False;
Part1:='';
if Not AsWord then
Begin
lastp:=999;
for i := 1 to Length(Delimeters) do
Begin
p:=Pos(Delimeters[i],s);
if (p>0) and (p<lastp) then
lastp:=p
end;
end
Else
Begin
p:=Pos(Delimeters,s);
if p>0 then
lastp:=p;
end;
p:=lastp;
if p=999 then Exit; //no delimeter found
Part1:=Copy(s,1,p-1); //1st part
if not AsWord then
s:=Copy(s,p+1,maxint) //Rest of string
Else
s:=Copy(s,p+Length(Delimeters),maxint); //Rest of string
Result:=True;
End;
End.