ArenaZ

Форк
0
/
GLZStringUtils.pas 
804 строки · 31.2 Кб
1
(*====< GLZStringUtils.pas >====================================================@br
2
  @created(2017-06-11)
3
  @author(J.Delauney (BeanzMaster))
4
  Historique : @br
5
  @unorderedList(
6
    @item(11/06/2017 : Creation  )
7
  )
8
--------------------------------------------------------------------------------@br
9

10
  @bold(Description :)@br
11
  Initialise un objet StrOps regroupant plusieur fonctions utiles à la manipulation de
12
  chaine de caractères
13

14
  ------------------------------------------------------------------------------@br
15

16
  Notes : @br
17
  ------------------------------------------------------------------------------@br
18

19
  Credits :
20

21
  ------------------------------------------------------------------------------@br
22
  LICENCE : MPL / GPL @br
23
  @br
24
 *==============================================================================*)
25
unit GLZStringUtils;
26

27
interface
28

29
{.$i ..\glzscene_options.inc}
30

31
uses
32
  Classes, SysUtils, Dialogs;
33

34
Const
35
  {$IFDEF MSWINDOWS}
36
  PathSeparator='\';
37
  {$ELSE}
38
  PathSeparator='/';
39
  {$ENDIF}
40
  WhiteSpaces = [#8, #9, #10, #13, #32];
41
{const
42
  gcsAll           = [#0..#255];
43
  gcsAZHi          = ['A'..'Z'];
44
  gcsAZLo          = ['a'..'z'];
45
  gcsAZAlpha       = ['A'..'Z','a'..'z'];
46
  gcsDigit         = ['0'..'9'];
47
  gcsInt           = ['0'..'9','-','+'];
48
  gcsNumeric       = ['0'..'9',',','.','-','+'];
49
  gcsFloat         = ['0'..'9',',','.','-','+','E','e'];
50
  gcsControl       = [#0..#31];
51
  gcCRSet          = [#13,#10];
52
  gcCR             = #13#10;
53
  gcBaseBinary     = '01';
54
  gcBaseHex        = '0123456789ABCDEF';
55
  gcWordDelims     = [#1..#47,#58..#64,#91..#96,#123..#126]-['`'];
56
  gcBooleanTrueSet   : TCharSet = ['1','T','t','Y','y','J','j']; }
57

58
Type
59
  TStrReplaceOptions = set of (roIgnoreCase, roWholeWords, roReplaceOnce, roPreserveCase, roRemoveDelim); //Delphi's is TReplaceFlags
60

61
Type
62
  TGLZStringHelper = class
63
  Public
64
    //Trim - Remove characters depending on the position in a string
65
    function  Trim(const s : String) : String; overload;
66
    function  TrimLeft(const s : String) : String;
67
    function  TrimRight(const s : String) : String;
68
    //Informational functions-----------------------------------------------------
69
    function  IsEmpty(const s : String) : Boolean;
70
    //Case------------------------------------------------------------------------
71
    function  UpCase(const s : String) : String; //overload;
72
    function  LoCase(const s : String) : String; //overload;
73
    //Delete----------------------------------------------------------------------
74
    //function  Del(const SubStr : String; const s : String): String;
75
    //Manipulation----------------------------------------------------------------
76
    //function  Cut(var s : String; Index, Count : Integer) : String;
77
    //function  Ins(const SubStr : String; const s : String; Index : Integer = 1) : String;
78
    //function  SetAt(const s : String; const c : Char; Index : Integer) : String;
79
    //function  EnsurePrefix(const SubStr : String; const s : String) : String;
80
    //function  EnsureSuffix(const SubStr : String; const s : String) : String;
81
	  //function  EnsureNoPrefix(const SubStr : String; const s : String) : String;
82
    //function  EnsureNoSuffix(const SubStr : String; const s : String) : String;
83
    function  PadCenter(const s: String; Len: Integer; PadChar : Char = ' '): String;
84
    function  PadRight(const s : String; PadLen : SmallInt; c : Char = ' ') : String;
85
    function  PadLeft(const s : String; PadLen : Word; c : Char = ' ') : String;
86
    //Replacement-----------------------------------------------------------------
87
    {function  ReplaceChars(const s : String; const OldChars : TCharSet; NewChar : Char) : String;
88
    function  Replace(OldChar, NewChar : Char; const InStr : String; const IgnoreCase : Boolean = False) : string; overload;
89
    function  Replace(
90
              const FindStr      : String;
91
              const ReplaceStr   : String;
92
              const InStr        : String;
93
              const IgnoreCase   : Boolean = False;
94
              const WholeWord    : Boolean = False;
95
              const PreserveCase : Boolean = False;
96
              const Delims       : TCharSet = gcWordDelims) : String; overload; }
97
    //Positional------------------------------------------------------------------
98
    function  Pos(const SubStr, s : String;StartPos :integer = 1) : Integer; //overload;
99
    function  PosBetween(const SubStr1, SubStr2 : String; const s : String; var StartPos, EndPos : Integer):Boolean;
100
    //function  Pos(c : Char; const s : String; StartPos : Integer = 1) : Integer; overload;
101
    //Return part of a string-----------------------------------------------------
102
    function  After(const SubStr : String; const s : String; Position : Integer = 1) : String;
103
    function  Before(const SubStr : String; const s : String; Position : Integer = 1) : String;
104
    function  Between(const SubStr1, SubStr2 : String; const s : String) : String;
105
    function  Mid(const SubStr1, SubStr2 : String; const s : String; Position : Integer = 1) : String;
106
    //Copy functions--------------------------------------------------------------
107
    function Copy(const aSourceString : string; aStart, aLength : Integer) : string;
108
    function CopyPos(const s: String; StartPos, EndPos: Integer): String;
109
    function LeftOf(const s : String; Position : Integer) : String;
110
    function RightOf(const s : String; Position : Integer) : String;
111
    //Numeric To String conversions-----------------------------------------------
112
    function ToStr(Value : Boolean) : String; overload;
113
    function ToStr(Value : Integer) : String; overload;
114
    function ToStr(Value : Extended) : String; overload;
115
{    function  ToStr(Value : Integer) : String; overload;
116

117
    function  ToStr(Value : Byte; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
118
    function  ToStr(Value : SmallInt; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
119
    function  ToStr(Value : Word; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
120
    function  ToStr(Value : Integer; PadLen : SmallInt; PadChar : Char = '0') : String; overload;
121
    function  ToStr(Value : Cardinal; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
122
    function  ToStr(Value : Int64; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
123
    function  ToStr(Value : Pointer) : String; overload;
124
    function  ToStr(Value : Double; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
125
    function  ToStr(Value : Extended; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
126
    function  ToStr(Value : TCurrency; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload; }
127
	//String to Numeric conversions-----------------------------------------------
128
{	function ToVal(const s : String) : Byte; overload;
129
	function ToVal(const s : String) : SmallInt; overload;
130
	function ToVal(const s : String) : Word; overload;
131
	function ToVal(const s : String) : Integer; overload;
132
	function ToVal(const s : String) : Cardinal; overload;
133
	function ToVal(const s : String) : Int64; overload;
134
	function ToVal(const s : String) : Double; overload;
135
	function ToVal(const s : String) : Extended; overload;
136
	function ToVal(const s : String) : TCurrency; overload; }
137
    //Miscellaneous-------------------------------------------------------------
138

139
    procedure SkipWhiteSpace(var Line: string);
140
    function ReadString(var Line: string): string;
141
    function ReadInt(var Line: string): Integer;
142

143
    function RepeatChar(c : Char; Count : Integer) : String;
144
    function RepeatStr(const s : String; Count : Integer) : String;
145
    function Surround(S: string; chs: string): string;  overload;
146
    function Surround(S: string; chsL, chsR: string): string; //overload;
147
    function Implode(lst:TStringList;sep : string =';'):string;
148
    function Explode(ch : string;sep: string = ';'):TStringList;
149
    function Find(const Substr, S: string; const Index: Integer =1): Integer;
150

151
//    function TokenAt(const S: string; Seperator: Char; At: Integer): string;
152
  end;
153

154
Var StrOps : TGLZStringHelper;
155

156
implementation
157

158
//Uses StrUtils;
159

160
{//////////////////////////////////////////////////////////////////////////////}
161
{                      GLOBAL FUNCTIONS, VARS                                  }
162
{//////////////////////////////////////////////////////////////////////////////}
163

164
//CaseArray's for fast conversion - see FillANSIArrays
165
{var
166
  gaANSIUpperArray   : array[Char] of Char;
167
  gaANSILowerArray   : array[Char] of Char;
168
  gaANSICharType     : array[Char] of TCharTypeFlags; }
169

170
{-------------------------------------------------------------------------------
171
*Copy - Same as the standard Copy function except Count is by default 2GB.
172
 Example: Copy('ABCDEF', 2) > 'BCDEF'
173
 Example: Copy('ABCDEF', 2, 3) > 'BCD'
174
-------------------------------------------------------------------------------}
175
function TGLZStringHelper.Copy(const aSourceString : string; aStart, aLength : Integer) : string;
176
var
177
  L                           : Integer;
178
begin
179
  L := Length(aSourceString);
180
  if L=0 then Exit;
181
  if (aStart < 1) or (aLength < 1) then Exit;
182

183
  if aStart + (aLength) > L then aLength := L - (aStart-2);
184

185
  if (aStart <1) then exit;
186

187
  SetLength(Result,aLength);
188
  //FastCharMove(aSourceString[aStart], Result[1], aLength);
189
  Move(aSourceString[aStart], Result[1], aLength);
190
end;
191

192
function TGLZStringHelper.CopyPos(const s: String; StartPos, EndPos: Integer): String;
193
var Len:integer;
194
begin
195

196
	if EndPos<StartPos then
197
	begin
198
		Len:=StartPos+EndPos;
199
	end
200
	else
201
	begin
202
		Len:=EndPos-StartPos;
203
	end;
204
	result:=Copy(s,StartPos, Len);
205
end;
206

207
{-------------------------------------------------------------------------------
208
*Trim - Strips leading/trailing spaces and control characters.
209
  Same as standard Trim function but with optional DoUpcase.
210
Example: Trim('  hello   world  ') > 'hello   world'
211
-------------------------------------------------------------------------------}
212
function TGLZStringHelper.Trim(const s : String) : String;
213
var i, l: Integer;
214
begin
215
  l := Length(s);
216
  i := 1;
217
  while (i <= l) and (s[i] <= ' ') do Inc(i);
218
  if i > l then Result := '' else
219
  begin
220
    while s[l] <= ' ' do System.Dec(l);
221
    Result := Copy(s, i, l - i + 1);
222
  end;
223
End;
224

225
{-------------------------------------------------------------------------------
226
*TrimLeft - Strips leading spaces and control characters from a string.
227
  Same as standard TrimLeft function but with optional DoUpcase.
228
Example: TrimLeft('  hello   world  ') > 'hello   world  '
229
-------------------------------------------------------------------------------}
230
function TGLZStringHelper.TrimLeft(const s : String) : String;
231
var i, l : Integer;
232
begin
233
  l := Length(s);
234
  i := 1;
235
  while (i <= l) and (s[i] <= ' ') do Inc(i);
236
  Result := Copy(s, i, Maxint);
237
end;
238

239
{-------------------------------------------------------------------------------
240
*TrimRight - Strips trailing spaces and control characters from a string.
241
  Same as standard TrimRight function but with optional DoUpcase.
242
Example: TrimRight('  hello   world  ') > '  hello   world'
243
-------------------------------------------------------------------------------}
244
function TGLZStringHelper.TrimRight(const s : String) : String;
245
var i : Integer;
246
begin
247
  i := Length(s);
248
  while (i > 0) and (S[i] <= ' ') do Dec(i);
249
  Result := Copy(s, 1, i);
250
end;
251

252
{-------------------------------------------------------------------------------
253
*IsEmpty - Returns true if a string has no characters above ord(' ').
254
 If basically means that it returns true if the string is empty or only contains
255
 control characters or spaces.
256
 Much faster than "if Trim(s)='' then..."
257
 Example:
258
   IsEmpty('   '+#9) > True
259
   IsEmpty(' a  ') > False
260
-------------------------------------------------------------------------------}
261
function TGLZStringHelper.IsEmpty(const s : String) : Boolean;
262
var i : Integer;
263
begin
264
  Result := False;
265
  for i := 1 to Length(s) do if s[i]>' ' then Exit;
266
  Result := True;
267
end;
268

269
{-------------------------------------------------------------------------------
270
*UpCase - AnsiUpperCase converts all characters in the given string to uppercase.
271
 The conversion uses the current Windows locale.
272
 Faster than SysUtils.AnsiUppercase but does not support MBCS.
273
 Example: UpCase('polé') > 'POLÉ'
274
Note - should be 3-4 times faster than SysUtils.AnsiUppercase and 20% faster than SysUtils.UpperCase
275
-------------------------------------------------------------------------------}
276
function TGLZStringHelper.UpCase(const s : String) : String;
277
Begin
278
  result:=UpperCase(S);
279
end;
280

281
{-------------------------------------------------------------------------------
282
*LoCase - AnsiLowerCase converts all characters in the given string to lowercase.
283
 The conversion uses the current Windows locale.
284
 Faster than SysUtils.AnsiLowerCase but does not support MBCS.
285
 Example: LoCase('POLÉ') > 'polé'
286
-------------------------------------------------------------------------------}          
287
function TGLZStringHelper.LoCase(const s : String) : String;
288
Begin
289
  result:=LowerCase(S);
290
end;
291

292
{-------------------------------------------------------------------------------
293
*Pos - Overloaded - Same as the standard Pos function except that:
294
  - if you use enable StrManEnableASM then it is faster - thanks to Peter Morris
295
     (see notes and credits at FastMemPos)
296
  - an optional starting position can be specified.
297
 Example: Pos('the','the man there') > 1
298
 Example: Pos('the','the man there',2) > 9
299
 Example: Pos('THE','the man there',2) > 0
300
-------------------------------------------------------------------------------}
301
function TGLZStringHelper.Pos(const SubStr, s : String; StartPos :integer = 1) : Integer;
302
var tmp:String;
303
    L:Integer;
304
begin
305
  if StartPos>1 then
306
  begin
307
    L:=Length(S)-StartPos;
308
    tmp:=Copy(S,StartPos, L);
309
  end
310
  else tmp:=S;
311

312
  Result := System.Pos(SubStr,tmp);
313
end;
314

315
function TGLZStringHelper.PosBetween(const SubStr1, SubStr2 : String; const s : String; var StartPos, EndPos : Integer):Boolean;
316
var REndPos, RStartPos :Integer;
317
Begin
318
  Result:=False;
319
  StartPos:=-1;
320
  EndPos:=-1;
321
  RStartPos:=Pos(Substr1,S);
322
  REndPos:=Pos(Substr2,S);
323
  if (RStartPos>0) And (REndPos>0) then
324
  begin
325
    result:=True;
326
    StartPos:=RStartPos;
327
    EndPos:=REndpos;
328
  end;
329
End;
330

331
{-------------------------------------------------------------------------------
332
*After
333
 Scans for SubStr in s - if found the characters after SubStr is returned else
334
 an empty string is returned.
335
 Examples:
336
   After('Funny','Those Funny People') > ' People';
337
   After('land','Your land is my land ok',1) > ' is my land ok'
338
   After('land','Your land is my land ok',7) > ' ok'
339
   After('not there','Your land is my land ok') > ''
340
-------------------------------------------------------------------------------}
341
function TGLZStringHelper.After(const SubStr : String; const s : String; Position : Integer = 1) : String;
342
var p,L,Start : Integer;
343
begin
344
  p := Pos(SubStr, s,Position);
345
  Result := '';
346
  if p>=0 then
347
  begin
348
    Start:=p+Length(SubStr);
349
    L:=Length(s)-(Start-1);
350
    Result := Copy(s, Start, L);
351
  end;
352
end;
353
{-------------------------------------------------------------------------------
354
*Before
355
 Scans for SubStr in s - if found the characters before SubStr is returned else
356
 the complete string is returned.
357
 Example: Before(' People','Those Funny People') > 'Those Funny';
358
 Example: Before('land','Your land is my land',1) > 'Your';
359
 Example: Before('land','Your land is my land',7) > 'Your land is my ';
360
 Example: Before('not there','Your land is my land') > 'Your land is my land'
361
-------------------------------------------------------------------------------}
362
function TGLZStringHelper.Before(const SubStr : String; const s : String; Position : Integer = 1) : String;
363
var p , L: Integer;
364
begin
365
  p := Pos(SubStr, s,Position);
366
  Result := '';
367
  if p>=0 then
368
  begin
369
    //Start:=p+Length(SubStr);
370
    L:=p-1;
371
    Result := Copy(s, position, L);
372
  end;
373
end;
374
{-------------------------------------------------------------------------------
375
*Between - Scans s for the start/end combination of SubStr1, SubStr2 and
376
 returns the text between them.
377
 If SubStr2 is empty SubStr2 will be regarded as identical to SubStr1.
378
 If SubStr2 is not found, then an empty string is returned (as opposed to function Mid).
379

380
Example:
381
  Between('<-','->','<-a->  <-b->') > 'a'
382
  Between('(',')','(a(b(c)))  (d(e))') > 'a(b(c))'
383
  Between('(',')','(a(b(c))  (d(e') > ''
384
  Between('\','\','c:\winnt\system\util.dll') > 'winnt'
385
  Between('\','\','c:\winnt') > ''
386
-------------------------------------------------------------------------------}
387
function TGLZStringHelper.Between(const SubStr1, SubStr2 : String; const s : String) : String;
388
var StartPos,EndPos : Integer;
389
begin
390
  StartPos:=0;
391
  EndPos:=0;
392
  if PosBetween(SubStr1, SubStr2, s, StartPos, EndPos) then
393
  begin
394
    StartPos:=StartPos+1;
395
    Result := Copy(s, StartPos, (EndPos-StartPos));
396
  end
397
  else Result := '';
398
end;
399
{-------------------------------------------------------------------------------
400
*Mid - Scans s and returns text after the first SubStr1 and before SubStr2 if found.
401
 If SubStr2 is empty SubStr2 will be regarded as identical to SubStr1.
402
 If SubStr2 is not found the complete string after SubStr1 is returned (as opposed to function Between).
403
 This function is the equivalent of a Before(After combination, but faster.
404
Example:
405
  Mid('(',')','(a) (b) (c)') > a
406
  Mid('(',')','(a(b)(c)  ((d)e)') > a(b
407
  Mid('(',')','(a(b(c)))') > a(b(c
408
  Mid('(',')','(a(b(c)') > a(b(c
409
  Mid('\','','c:\winnt\system\util.dll') > winnt
410
  Mid('\','','c:\winnt') > winnt
411
Note that function Between will return the first innermost text:
412
  Between('(',')','(a(b)(c)' > 'b'
413
-------------------------------------------------------------------------------}
414
function TGLZStringHelper.Mid(const SubStr1, SubStr2 : String; const s : String; Position : Integer = 1) : String;
415
var p1,p2 : Integer;
416
begin
417
  p1 := Pos(SubStr1, s, Position);
418
  if p1<=0
419
  then Result := '' else
420
  begin
421
    if SubStr2=''
422
    then p2 := Pos(SubStr1, s, p1+Length(SubStr1))
423
    else p2 := Pos(SubStr2, s, p1+Length(SubStr1));
424
    if p2<=0
425
      then Result := Copy(s, p1+Length(SubStr1), Length(s))
426
      else Result := CopyPos(s, p1+Length(SubStr1), p2-1);
427
  end;
428
end;
429
{-------------------------------------------------------------------------------
430
*LeftAt
431
 Simple function that returns all chars to the left from a specific (including) Position
432
 Example: LeftAt('hello',2) > 'he'
433
See also RightAt, Copy, First, Last
434
-------------------------------------------------------------------------------}
435
function TGLZStringHelper.LeftOf(const s : String; Position : Integer) : String;
436
begin
437
  Result := Copy(s, 1, Position);
438
end;
439

440
{-------------------------------------------------------------------------------
441
*RightAt
442
 Simple function that returns all chars to the right from a specific (including) Position
443
 Example: RightAt('hello',2) > 'ello'
444
See also LeftAt, Copy, First, Last
445
-------------------------------------------------------------------------------}
446
function TGLZStringHelper.RightOf(const s : String; Position : Integer) : String;
447
begin
448
  Result := Copy(s, Position, Length(s));
449
end;
450

451
{-------------------------------------------------------------------------------
452
*Pad
453
 Ads a character at the end of the string until the length is equal to PadLen.
454
 If PadLen is negative the character will be inserted to the left.
455
 Example: Pad('hello',7)  > 'hello  '
456
 Example: Pad('hello',7,' ')  > 'hello  '
457
 Example: Pad('hello',-7,'.') > '..hello'
458
 Example: Pad('hello',1,'.') > 'hello'
459
-------------------------------------------------------------------------------}
460
function TGLZStringHelper.PadRight(const s : String; PadLen : SmallInt; c : Char = ' ') : String;
461
var  i : Integer;
462
  More : Integer;
463
  Slen : Integer;
464
begin
465
  SLen := Length(s);
466
  More := Abs(PadLen) - Slen;
467
  if More>0 then
468
  begin
469
    if PadLen<0 then
470
    begin
471
      SetLength(Result, Abs(PadLen));
472
      System.Move(s[1], Result[More+1], Slen);
473
      for i := 1 to More do Result[i] := c;
474
    end else
475
    begin
476
      Result := s;
477
      SetLength(Result, Abs(PadLen));
478
      for i := SLen+1 to Slen+More do Result[i] := c;
479
    end;
480
  end else Result := s;
481
end;
482

483
{-------------------------------------------------------------------------------
484
*Center - Centers text in a string of length Len.
485
 Example: Center('ok',6) > '  ok  '
486
-------------------------------------------------------------------------------}
487
function TGLZStringHelper.PadCenter(const s: String; Len: Integer; PadChar : Char = ' '): String;
488
begin
489
  if Length(s) < Len then
490
  begin
491
    Result := RepeatChar(PadChar, (Len div 2) - (Length(s) div 2))+s;
492
    Result := Result + RepeatChar(PadChar, Len - Length(Result));
493
  end else Result := s;
494
end;
495
{-------------------------------------------------------------------------------
496
*PadLeft - Pads a character to the left while Length(s)<PadLen.
497
 PadLeft('hello',7)  > '  hello'
498
 PadLeft('hello',7,'.')  > '..hello'
499
Note that function Pad with a negative PadLen is functionally then same.
500
See also Pad
501
-------------------------------------------------------------------------------}
502
function TGLZStringHelper.PadLeft(const s : String; PadLen : Word; c : Char = ' ') : String;
503
begin
504
  Result := PadRight(s, -PadLen, c);
505
end;
506

507
{-------------------------------------------------------------------------------
508
*RepeatChar - Sets a string with a certain character and length.
509
 Example: RepeatChar('a',3) > 'aaa'
510
See also RepeatStr
511
-------------------------------------------------------------------------------}
512
function TGLZStringHelper.RepeatChar(c : Char; Count : Integer) : String;
513
begin
514
  SetLength(Result, Count);
515
  if Count>0
516
    then FillChar(Result[1], Count, c);
517
end;
518

519
{-------------------------------------------------------------------------------
520
*RepeatStr - Repeats a string count times.
521
 Example: RepeatStr('hello',3) > 'hellohellohello'
522
-------------------------------------------------------------------------------}
523
function TGLZStringHelper.RepeatStr(const s : String; Count : Integer) : String;
524
var  p : PChar;
525
  Slen : Integer;
526
begin
527
  SLen := Length(s);
528
  SetLength(Result, Count*SLen);
529
  p := Pointer(Result);
530
  while Count > 0 do
531
  begin
532
    Move(Pointer(s)^, p^, SLen);
533
    Inc(p, SLen);
534
    Dec(Count);
535
  end;
536
end;
537

538
function TGLZStringHelper.Find(const Substr, S: string; const Index: Integer=1): Integer;
539
var
540
  apos: Integer;
541
begin
542
  if (SubStr <> '') and (S <> '') then
543
  begin
544
    apos := Pos(Substr, Copy(S, Index, Length(S) - Index + 1));
545
    if apos = 0 then
546
      Result := 0
547
    else
548
      Result := Index + aPos - 1;
549
  end
550
  else
551
    Result := 0;
552
end;
553

554

555

556
{-------------------------------------------------------------------------------
557
*Str - Converts Boolean to a string.
558
 Example: Str(False) > 'False'
559
See also ToBool
560
-------------------------------------------------------------------------------}
561
function TGLZStringHelper.ToStr(Value : Boolean) : String;
562
begin
563
  if Value then Result := 'True' else Result := 'False';
564
end;
565
{-------------------------------------------------------------------------------
566
*Str - Converts an Integer to a string.
567
 Identical to standard function IntToStr, but slightly faster.
568
 Example: Str(12) > '12'
569
See also the overloaded Integer Str version with padding.
570
-------------------------------------------------------------------------------}
571
function TGLZStringHelper.ToStr(Value : Integer) : String;
572
begin
573
  System.Str(Value:0, Result);
574
end;
575

576
{-------------------------------------------------------------------------------
577
*Str - Converts an Integer to a string.
578
 Identical to standard function IntToStr, but slightly faster.
579
 Example: Str(12) > '12'
580
See also the overloaded Integer Str version with padding.
581
-------------------------------------------------------------------------------}
582
function TGLZStringHelper.ToStr(Value : Extended) : String;
583
begin
584
  result:=SysUtils.FloatToStr(value);
585
  (* StrToFloat
586
A new set of format settings which set a fixed decimal separator can be created with the following code:
587
// in your .lpr project file
588
uses
589
...
590
{$IFDEF UNIX}
591
clocale
592
{ required on Linux/Unix for formatsettings support. Should be one of the first (probably after cthreads?}
593
{$ENDIF}
594
and:
595
// in your code:
596
var
597
  FPointSeparator, FCommaSeparator: TFormatSettings;
598
begin
599
  // Format settings to convert a string to a float
600
  FPointSeparator := DefaultFormatSettings;
601
  FPointSeparator.DecimalSeparator := '.';
602
  FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
603
  FCommaSeparator := DefaultFormatSettings;
604
  FCommaSeparator.DecimalSeparator := ',';
605
  FCommaSeparator.ThousandSeparator := '#';// disable the thousand separator
606
Later on you can use this format settings when calling StrToFloat, like this:
607
// This function works like StrToFloat, but simply tries two possible decimal separator
608
// This will avoid an exception when the string format doesn't match the locale
609
function AnSemantico.StringToFloat(AStr: string): Double;
610
begin
611
  if Pos('.', AStr) > 0 then Result := StrToFloat(AStr, FPointSeparator)
612
  else Result := StrToFloat(AStr, FCommaSeparator);
613
end; *)
614

615
end;
616

617
{-------------------------------------------------------------------------------
618
*Del - returns s with all occurences of SubStr removed.
619
 Example: Del('the','the moon is therefore the light') > ' moon is refore  light'
620
See also DelWord
621
--------------------------------------------------------------------------------}
622
{function TStringManager.Del(const SubStr : String; const s : String): String;
623
var StartPos : Integer;
624
begin
625
  Result := s;
626
  StartPos := 1;
627
  PReplace(SubStr, '', Result, StartPos, [], []);
628
end; }
629

630

631

632

633
{-------------------------------------------------------------------------------
634
*Ins - function version of standard insert procedure.
635
 Example: Ins('a','1234',2) > '1a234'
636
 Example: Ins('a','1234',200) > '1234a'
637
-------------------------------------------------------------------------------}
638
{function TStringManager.Ins(const SubStr : String; const s : String; Index : Integer = 1) : String;
639
begin
640
  Result := s;
641
  System.Insert(SubStr, Result, Index);
642
end; }
643
{-------------------------------------------------------------------------------
644
*SlashAdd - add's a slash (typically a file/path name) at the end if needed
645
 Example: SlashAdd('c:\windows') > 'c:\windows\'
646
 Example: SlashAdd('c:\windows\') > 'c:\windows\'
647
See also SlashDel
648
-------------------------------------------------------------------------------}
649
{function TStringManager.AddSlash(const s : String) : String;
650
begin
651
  if (Length(s) > 0) and (s[Length(s)] <> '\')
652
    then Result := s+'\'
653
    else Result := s;
654
end; }
655

656
{-------------------------------------------------------------------------------
657
*SlashDel- reMoves a slash (typically a file/path name) if at end.
658
 Example: SlashDel('c:\windows') > 'c:\windows'
659
 Example: SlashDel('c:\windows\') > 'c:\windows'
660
See also SlashAdd
661
-------------------------------------------------------------------------------}
662
{function TStringManager.RemoveSlash(const s : String) : String;
663
begin
664
  if (Length(s) > 0) and (s[Length(s)] = '\')
665
    then Result := System.Copy(s, 1, Pred(Length(s)))
666
    else Result := s;
667
end; }
668
//--------------------------------------------------------------------------------------
669
{function TokenAt(const S: string; Seperator: Char; At: Integer): string;
670
var
671
  J, I: Integer;
672
begin
673
  Result := '';
674
  J := 1;
675
  I := 0;
676
  while (I <= At) and (J <= Length(S)) do
677
  begin
678
    if (S[J] = Seperator) then
679
      Inc(I)
680
    else if (I = At) then
681
      Result := Result + S[J];
682
    Inc(J);
683
  end;
684
  Result := Trim(Result);
685
end; }
686

687
{Function SubString(Str: String ; Substr: String ; n: Integer): String;
688
var
689
  z: Integer;
690
begin
691
  Str := Str + Substr;
692
  For z := 1 to n do
693
  begin
694
    Str := copy(Str, pos(Substr, Str)+length(Substr), length(Str)-pos(Substr, Str)+Length(Substr));
695
  end;
696
  Result := Trim(Copy(Str, 1, pos(Substr, Str) -1));
697
end;  }
698

699
procedure TGLZStringHelper.SkipWhiteSpace(var Line: string);
700
begin
701
  while (Length(Line) > 0) and (Line[1] in WhiteSpaces) do
702
    Delete(Line, 1, 1);
703
end;
704

705
function TGLZStringHelper.ReadString(var Line: string): string;
706
begin
707
  Result := '';
708
  SkipWhiteSpace(Line);
709
  while (Length(Line) > 0) and not(Line[1] in WhiteSpaces) do
710
  begin
711
    SetLength(Result, Length(Result) + 1);
712
    Result[Length(Result)] := Line[1];
713
    Delete(Line, 1, 1);
714
  end;
715
end;
716

717
function TGLZStringHelper.ReadInt(var Line: string): Integer;
718
begin
719
  Result := StrToInt(ReadString(Line));
720
end;
721
function TGLZStringHelper.Surround(S: string; chs: string): string;
722
begin
723
  Result := chs + S + chs;
724
end;
725

726
function TGLZStringHelper.Surround(S: string; chsL, chsR: string): string;
727
begin
728
  Result := chsL + S + chsR;
729
end;
730

731

732
function TGLZStringHelper.Explode(ch : string;sep: string = ';'):TStringList;
733
var
734
 p : integer;
735
begin
736
  p := pos(sep,ch);
737
  explode := TStringList.Create;
738
  while p > 0 do
739
  begin
740
    explode.Add(copy(ch,1,p-1));
741
    //showMessage(copy(ch,1,p-1));
742
    if p <= length(ch) then ch := copy(ch,p+ length(sep),length(ch));
743
    p := pos(sep,ch);
744
  end;
745
  explode.Add(ch);
746
end;
747

748
function TGLZStringHelper.Implode(lst:TStringList;sep : string =';'):string;
749
var
750
  i : integer;
751
  s : string;
752
begin
753
 i:= 0;
754
 while i < lst.Count - 1 do begin
755
   s := s + lst[i] + sep;
756
   i := i + 1;
757
 end;
758
 if i < lst.Count then s := s + lst[i]; //Ne mets pas de séparateur sur le dernier élément
759
 result := s;
760
end;
761

762
(* StrToFloat
763
A new set of format settings which set a fixed decimal separator can be created with the following code:
764
// in your .lpr project file
765
uses
766
...
767
{$IFDEF UNIX}
768
clocale
769
{ required on Linux/Unix for formatsettings support. Should be one of the first (probably after cthreads?}
770
{$ENDIF}
771
and:
772
// in your code:
773
var
774
  FPointSeparator, FCommaSeparator: TFormatSettings;
775
begin
776
  // Format settings to convert a string to a float
777
  FPointSeparator := DefaultFormatSettings;
778
  FPointSeparator.DecimalSeparator := '.';
779
  FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
780
  FCommaSeparator := DefaultFormatSettings;
781
  FCommaSeparator.DecimalSeparator := ',';
782
  FCommaSeparator.ThousandSeparator := '#';// disable the thousand separator
783
Later on you can use this format settings when calling StrToFloat, like this:
784
// This function works like StrToFloat, but simply tries two possible decimal separator
785
// This will avoid an exception when the string format doesn't match the locale
786
function AnSemantico.StringToFloat(AStr: string): Double;
787
begin
788
  if Pos('.', AStr) > 0 then Result := StrToFloat(AStr, FPointSeparator)
789
  else Result := StrToFloat(AStr, FCommaSeparator);
790
end; *)
791

792
//==============================================================================
793
{//////////////////////////////////////////////////////////////////////////////}
794
{                         INITIALIZATION / FINALIZATION                        }
795
{//////////////////////////////////////////////////////////////////////////////}
796

797

798
Initialization
799
 // FillANSIArrays;
800
  StrOps:=TGLZStringHelper.Create;
801
Finalization
802
  StrOps.Free;
803
//==============================================================================
804
end.
805

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.