ArenaZ
804 строки · 31.2 Кб
1(*====< GLZStringUtils.pas >====================================================@br
2@created(2017-06-11)
3@author(J.Delauney (BeanzMaster))
4Historique : @br
5@unorderedList(
6@item(11/06/2017 : Creation )
7)
8--------------------------------------------------------------------------------@br
9
10@bold(Description :)@br
11Initialise un objet StrOps regroupant plusieur fonctions utiles à la manipulation de
12chaine de caractères
13
14------------------------------------------------------------------------------@br
15
16Notes : @br
17------------------------------------------------------------------------------@br
18
19Credits :
20
21------------------------------------------------------------------------------@br
22LICENCE : MPL / GPL @br
23@br
24*==============================================================================*)
25unit GLZStringUtils;
26
27interface
28
29{.$i ..\glzscene_options.inc}
30
31uses
32Classes, SysUtils, Dialogs;
33
34Const
35{$IFDEF MSWINDOWS}
36PathSeparator='\';
37{$ELSE}
38PathSeparator='/';
39{$ENDIF}
40WhiteSpaces = [#8, #9, #10, #13, #32];
41{const
42gcsAll = [#0..#255];
43gcsAZHi = ['A'..'Z'];
44gcsAZLo = ['a'..'z'];
45gcsAZAlpha = ['A'..'Z','a'..'z'];
46gcsDigit = ['0'..'9'];
47gcsInt = ['0'..'9','-','+'];
48gcsNumeric = ['0'..'9',',','.','-','+'];
49gcsFloat = ['0'..'9',',','.','-','+','E','e'];
50gcsControl = [#0..#31];
51gcCRSet = [#13,#10];
52gcCR = #13#10;
53gcBaseBinary = '01';
54gcBaseHex = '0123456789ABCDEF';
55gcWordDelims = [#1..#47,#58..#64,#91..#96,#123..#126]-['`'];
56gcBooleanTrueSet : TCharSet = ['1','T','t','Y','y','J','j']; }
57
58Type
59TStrReplaceOptions = set of (roIgnoreCase, roWholeWords, roReplaceOnce, roPreserveCase, roRemoveDelim); //Delphi's is TReplaceFlags
60
61Type
62TGLZStringHelper = class
63Public
64//Trim - Remove characters depending on the position in a string
65function Trim(const s : String) : String; overload;
66function TrimLeft(const s : String) : String;
67function TrimRight(const s : String) : String;
68//Informational functions-----------------------------------------------------
69function IsEmpty(const s : String) : Boolean;
70//Case------------------------------------------------------------------------
71function UpCase(const s : String) : String; //overload;
72function 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;
83function PadCenter(const s: String; Len: Integer; PadChar : Char = ' '): String;
84function PadRight(const s : String; PadLen : SmallInt; c : Char = ' ') : String;
85function PadLeft(const s : String; PadLen : Word; c : Char = ' ') : String;
86//Replacement-----------------------------------------------------------------
87{function ReplaceChars(const s : String; const OldChars : TCharSet; NewChar : Char) : String;
88function Replace(OldChar, NewChar : Char; const InStr : String; const IgnoreCase : Boolean = False) : string; overload;
89function Replace(
90const FindStr : String;
91const ReplaceStr : String;
92const InStr : String;
93const IgnoreCase : Boolean = False;
94const WholeWord : Boolean = False;
95const PreserveCase : Boolean = False;
96const Delims : TCharSet = gcWordDelims) : String; overload; }
97//Positional------------------------------------------------------------------
98function Pos(const SubStr, s : String;StartPos :integer = 1) : Integer; //overload;
99function 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-----------------------------------------------------
102function After(const SubStr : String; const s : String; Position : Integer = 1) : String;
103function Before(const SubStr : String; const s : String; Position : Integer = 1) : String;
104function Between(const SubStr1, SubStr2 : String; const s : String) : String;
105function Mid(const SubStr1, SubStr2 : String; const s : String; Position : Integer = 1) : String;
106//Copy functions--------------------------------------------------------------
107function Copy(const aSourceString : string; aStart, aLength : Integer) : string;
108function CopyPos(const s: String; StartPos, EndPos: Integer): String;
109function LeftOf(const s : String; Position : Integer) : String;
110function RightOf(const s : String; Position : Integer) : String;
111//Numeric To String conversions-----------------------------------------------
112function ToStr(Value : Boolean) : String; overload;
113function ToStr(Value : Integer) : String; overload;
114function ToStr(Value : Extended) : String; overload;
115{ function ToStr(Value : Integer) : String; overload;
116
117function ToStr(Value : Byte; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
118function ToStr(Value : SmallInt; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
119function ToStr(Value : Word; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
120function ToStr(Value : Integer; PadLen : SmallInt; PadChar : Char = '0') : String; overload;
121function ToStr(Value : Cardinal; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
122function ToStr(Value : Int64; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
123function ToStr(Value : Pointer) : String; overload;
124function ToStr(Value : Double; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
125function ToStr(Value : Extended; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload;
126function ToStr(Value : TCurrency; PadLen : SmallInt = 0; PadChar : Char = '0') : String; overload; }
127//String to Numeric conversions-----------------------------------------------
128{ function ToVal(const s : String) : Byte; overload;
129function ToVal(const s : String) : SmallInt; overload;
130function ToVal(const s : String) : Word; overload;
131function ToVal(const s : String) : Integer; overload;
132function ToVal(const s : String) : Cardinal; overload;
133function ToVal(const s : String) : Int64; overload;
134function ToVal(const s : String) : Double; overload;
135function ToVal(const s : String) : Extended; overload;
136function ToVal(const s : String) : TCurrency; overload; }
137//Miscellaneous-------------------------------------------------------------
138
139procedure SkipWhiteSpace(var Line: string);
140function ReadString(var Line: string): string;
141function ReadInt(var Line: string): Integer;
142
143function RepeatChar(c : Char; Count : Integer) : String;
144function RepeatStr(const s : String; Count : Integer) : String;
145function Surround(S: string; chs: string): string; overload;
146function Surround(S: string; chsL, chsR: string): string; //overload;
147function Implode(lst:TStringList;sep : string =';'):string;
148function Explode(ch : string;sep: string = ';'):TStringList;
149function Find(const Substr, S: string; const Index: Integer =1): Integer;
150
151// function TokenAt(const S: string; Seperator: Char; At: Integer): string;
152end;
153
154Var StrOps : TGLZStringHelper;
155
156implementation
157
158//Uses StrUtils;
159
160{//////////////////////////////////////////////////////////////////////////////}
161{ GLOBAL FUNCTIONS, VARS }
162{//////////////////////////////////////////////////////////////////////////////}
163
164//CaseArray's for fast conversion - see FillANSIArrays
165{var
166gaANSIUpperArray : array[Char] of Char;
167gaANSILowerArray : array[Char] of Char;
168gaANSICharType : array[Char] of TCharTypeFlags; }
169
170{-------------------------------------------------------------------------------
171*Copy - Same as the standard Copy function except Count is by default 2GB.
172Example: Copy('ABCDEF', 2) > 'BCDEF'
173Example: Copy('ABCDEF', 2, 3) > 'BCD'
174-------------------------------------------------------------------------------}
175function TGLZStringHelper.Copy(const aSourceString : string; aStart, aLength : Integer) : string;
176var
177L : Integer;
178begin
179L := Length(aSourceString);
180if L=0 then Exit;
181if (aStart < 1) or (aLength < 1) then Exit;
182
183if aStart + (aLength) > L then aLength := L - (aStart-2);
184
185if (aStart <1) then exit;
186
187SetLength(Result,aLength);
188//FastCharMove(aSourceString[aStart], Result[1], aLength);
189Move(aSourceString[aStart], Result[1], aLength);
190end;
191
192function TGLZStringHelper.CopyPos(const s: String; StartPos, EndPos: Integer): String;
193var Len:integer;
194begin
195
196if EndPos<StartPos then
197begin
198Len:=StartPos+EndPos;
199end
200else
201begin
202Len:=EndPos-StartPos;
203end;
204result:=Copy(s,StartPos, Len);
205end;
206
207{-------------------------------------------------------------------------------
208*Trim - Strips leading/trailing spaces and control characters.
209Same as standard Trim function but with optional DoUpcase.
210Example: Trim(' hello world ') > 'hello world'
211-------------------------------------------------------------------------------}
212function TGLZStringHelper.Trim(const s : String) : String;
213var i, l: Integer;
214begin
215l := Length(s);
216i := 1;
217while (i <= l) and (s[i] <= ' ') do Inc(i);
218if i > l then Result := '' else
219begin
220while s[l] <= ' ' do System.Dec(l);
221Result := Copy(s, i, l - i + 1);
222end;
223End;
224
225{-------------------------------------------------------------------------------
226*TrimLeft - Strips leading spaces and control characters from a string.
227Same as standard TrimLeft function but with optional DoUpcase.
228Example: TrimLeft(' hello world ') > 'hello world '
229-------------------------------------------------------------------------------}
230function TGLZStringHelper.TrimLeft(const s : String) : String;
231var i, l : Integer;
232begin
233l := Length(s);
234i := 1;
235while (i <= l) and (s[i] <= ' ') do Inc(i);
236Result := Copy(s, i, Maxint);
237end;
238
239{-------------------------------------------------------------------------------
240*TrimRight - Strips trailing spaces and control characters from a string.
241Same as standard TrimRight function but with optional DoUpcase.
242Example: TrimRight(' hello world ') > ' hello world'
243-------------------------------------------------------------------------------}
244function TGLZStringHelper.TrimRight(const s : String) : String;
245var i : Integer;
246begin
247i := Length(s);
248while (i > 0) and (S[i] <= ' ') do Dec(i);
249Result := Copy(s, 1, i);
250end;
251
252{-------------------------------------------------------------------------------
253*IsEmpty - Returns true if a string has no characters above ord(' ').
254If basically means that it returns true if the string is empty or only contains
255control characters or spaces.
256Much faster than "if Trim(s)='' then..."
257Example:
258IsEmpty(' '+#9) > True
259IsEmpty(' a ') > False
260-------------------------------------------------------------------------------}
261function TGLZStringHelper.IsEmpty(const s : String) : Boolean;
262var i : Integer;
263begin
264Result := False;
265for i := 1 to Length(s) do if s[i]>' ' then Exit;
266Result := True;
267end;
268
269{-------------------------------------------------------------------------------
270*UpCase - AnsiUpperCase converts all characters in the given string to uppercase.
271The conversion uses the current Windows locale.
272Faster than SysUtils.AnsiUppercase but does not support MBCS.
273Example: UpCase('polé') > 'POLÉ'
274Note - should be 3-4 times faster than SysUtils.AnsiUppercase and 20% faster than SysUtils.UpperCase
275-------------------------------------------------------------------------------}
276function TGLZStringHelper.UpCase(const s : String) : String;
277Begin
278result:=UpperCase(S);
279end;
280
281{-------------------------------------------------------------------------------
282*LoCase - AnsiLowerCase converts all characters in the given string to lowercase.
283The conversion uses the current Windows locale.
284Faster than SysUtils.AnsiLowerCase but does not support MBCS.
285Example: LoCase('POLÉ') > 'polé'
286-------------------------------------------------------------------------------}
287function TGLZStringHelper.LoCase(const s : String) : String;
288Begin
289result:=LowerCase(S);
290end;
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.
297Example: Pos('the','the man there') > 1
298Example: Pos('the','the man there',2) > 9
299Example: Pos('THE','the man there',2) > 0
300-------------------------------------------------------------------------------}
301function TGLZStringHelper.Pos(const SubStr, s : String; StartPos :integer = 1) : Integer;
302var tmp:String;
303L:Integer;
304begin
305if StartPos>1 then
306begin
307L:=Length(S)-StartPos;
308tmp:=Copy(S,StartPos, L);
309end
310else tmp:=S;
311
312Result := System.Pos(SubStr,tmp);
313end;
314
315function TGLZStringHelper.PosBetween(const SubStr1, SubStr2 : String; const s : String; var StartPos, EndPos : Integer):Boolean;
316var REndPos, RStartPos :Integer;
317Begin
318Result:=False;
319StartPos:=-1;
320EndPos:=-1;
321RStartPos:=Pos(Substr1,S);
322REndPos:=Pos(Substr2,S);
323if (RStartPos>0) And (REndPos>0) then
324begin
325result:=True;
326StartPos:=RStartPos;
327EndPos:=REndpos;
328end;
329End;
330
331{-------------------------------------------------------------------------------
332*After
333Scans for SubStr in s - if found the characters after SubStr is returned else
334an empty string is returned.
335Examples:
336After('Funny','Those Funny People') > ' People';
337After('land','Your land is my land ok',1) > ' is my land ok'
338After('land','Your land is my land ok',7) > ' ok'
339After('not there','Your land is my land ok') > ''
340-------------------------------------------------------------------------------}
341function TGLZStringHelper.After(const SubStr : String; const s : String; Position : Integer = 1) : String;
342var p,L,Start : Integer;
343begin
344p := Pos(SubStr, s,Position);
345Result := '';
346if p>=0 then
347begin
348Start:=p+Length(SubStr);
349L:=Length(s)-(Start-1);
350Result := Copy(s, Start, L);
351end;
352end;
353{-------------------------------------------------------------------------------
354*Before
355Scans for SubStr in s - if found the characters before SubStr is returned else
356the complete string is returned.
357Example: Before(' People','Those Funny People') > 'Those Funny';
358Example: Before('land','Your land is my land',1) > 'Your';
359Example: Before('land','Your land is my land',7) > 'Your land is my ';
360Example: Before('not there','Your land is my land') > 'Your land is my land'
361-------------------------------------------------------------------------------}
362function TGLZStringHelper.Before(const SubStr : String; const s : String; Position : Integer = 1) : String;
363var p , L: Integer;
364begin
365p := Pos(SubStr, s,Position);
366Result := '';
367if p>=0 then
368begin
369//Start:=p+Length(SubStr);
370L:=p-1;
371Result := Copy(s, position, L);
372end;
373end;
374{-------------------------------------------------------------------------------
375*Between - Scans s for the start/end combination of SubStr1, SubStr2 and
376returns the text between them.
377If SubStr2 is empty SubStr2 will be regarded as identical to SubStr1.
378If SubStr2 is not found, then an empty string is returned (as opposed to function Mid).
379
380Example:
381Between('<-','->','<-a-> <-b->') > 'a'
382Between('(',')','(a(b(c))) (d(e))') > 'a(b(c))'
383Between('(',')','(a(b(c)) (d(e') > ''
384Between('\','\','c:\winnt\system\util.dll') > 'winnt'
385Between('\','\','c:\winnt') > ''
386-------------------------------------------------------------------------------}
387function TGLZStringHelper.Between(const SubStr1, SubStr2 : String; const s : String) : String;
388var StartPos,EndPos : Integer;
389begin
390StartPos:=0;
391EndPos:=0;
392if PosBetween(SubStr1, SubStr2, s, StartPos, EndPos) then
393begin
394StartPos:=StartPos+1;
395Result := Copy(s, StartPos, (EndPos-StartPos));
396end
397else Result := '';
398end;
399{-------------------------------------------------------------------------------
400*Mid - Scans s and returns text after the first SubStr1 and before SubStr2 if found.
401If SubStr2 is empty SubStr2 will be regarded as identical to SubStr1.
402If SubStr2 is not found the complete string after SubStr1 is returned (as opposed to function Between).
403This function is the equivalent of a Before(After combination, but faster.
404Example:
405Mid('(',')','(a) (b) (c)') > a
406Mid('(',')','(a(b)(c) ((d)e)') > a(b
407Mid('(',')','(a(b(c)))') > a(b(c
408Mid('(',')','(a(b(c)') > a(b(c
409Mid('\','','c:\winnt\system\util.dll') > winnt
410Mid('\','','c:\winnt') > winnt
411Note that function Between will return the first innermost text:
412Between('(',')','(a(b)(c)' > 'b'
413-------------------------------------------------------------------------------}
414function TGLZStringHelper.Mid(const SubStr1, SubStr2 : String; const s : String; Position : Integer = 1) : String;
415var p1,p2 : Integer;
416begin
417p1 := Pos(SubStr1, s, Position);
418if p1<=0
419then Result := '' else
420begin
421if SubStr2=''
422then p2 := Pos(SubStr1, s, p1+Length(SubStr1))
423else p2 := Pos(SubStr2, s, p1+Length(SubStr1));
424if p2<=0
425then Result := Copy(s, p1+Length(SubStr1), Length(s))
426else Result := CopyPos(s, p1+Length(SubStr1), p2-1);
427end;
428end;
429{-------------------------------------------------------------------------------
430*LeftAt
431Simple function that returns all chars to the left from a specific (including) Position
432Example: LeftAt('hello',2) > 'he'
433See also RightAt, Copy, First, Last
434-------------------------------------------------------------------------------}
435function TGLZStringHelper.LeftOf(const s : String; Position : Integer) : String;
436begin
437Result := Copy(s, 1, Position);
438end;
439
440{-------------------------------------------------------------------------------
441*RightAt
442Simple function that returns all chars to the right from a specific (including) Position
443Example: RightAt('hello',2) > 'ello'
444See also LeftAt, Copy, First, Last
445-------------------------------------------------------------------------------}
446function TGLZStringHelper.RightOf(const s : String; Position : Integer) : String;
447begin
448Result := Copy(s, Position, Length(s));
449end;
450
451{-------------------------------------------------------------------------------
452*Pad
453Ads a character at the end of the string until the length is equal to PadLen.
454If PadLen is negative the character will be inserted to the left.
455Example: Pad('hello',7) > 'hello '
456Example: Pad('hello',7,' ') > 'hello '
457Example: Pad('hello',-7,'.') > '..hello'
458Example: Pad('hello',1,'.') > 'hello'
459-------------------------------------------------------------------------------}
460function TGLZStringHelper.PadRight(const s : String; PadLen : SmallInt; c : Char = ' ') : String;
461var i : Integer;
462More : Integer;
463Slen : Integer;
464begin
465SLen := Length(s);
466More := Abs(PadLen) - Slen;
467if More>0 then
468begin
469if PadLen<0 then
470begin
471SetLength(Result, Abs(PadLen));
472System.Move(s[1], Result[More+1], Slen);
473for i := 1 to More do Result[i] := c;
474end else
475begin
476Result := s;
477SetLength(Result, Abs(PadLen));
478for i := SLen+1 to Slen+More do Result[i] := c;
479end;
480end else Result := s;
481end;
482
483{-------------------------------------------------------------------------------
484*Center - Centers text in a string of length Len.
485Example: Center('ok',6) > ' ok '
486-------------------------------------------------------------------------------}
487function TGLZStringHelper.PadCenter(const s: String; Len: Integer; PadChar : Char = ' '): String;
488begin
489if Length(s) < Len then
490begin
491Result := RepeatChar(PadChar, (Len div 2) - (Length(s) div 2))+s;
492Result := Result + RepeatChar(PadChar, Len - Length(Result));
493end else Result := s;
494end;
495{-------------------------------------------------------------------------------
496*PadLeft - Pads a character to the left while Length(s)<PadLen.
497PadLeft('hello',7) > ' hello'
498PadLeft('hello',7,'.') > '..hello'
499Note that function Pad with a negative PadLen is functionally then same.
500See also Pad
501-------------------------------------------------------------------------------}
502function TGLZStringHelper.PadLeft(const s : String; PadLen : Word; c : Char = ' ') : String;
503begin
504Result := PadRight(s, -PadLen, c);
505end;
506
507{-------------------------------------------------------------------------------
508*RepeatChar - Sets a string with a certain character and length.
509Example: RepeatChar('a',3) > 'aaa'
510See also RepeatStr
511-------------------------------------------------------------------------------}
512function TGLZStringHelper.RepeatChar(c : Char; Count : Integer) : String;
513begin
514SetLength(Result, Count);
515if Count>0
516then FillChar(Result[1], Count, c);
517end;
518
519{-------------------------------------------------------------------------------
520*RepeatStr - Repeats a string count times.
521Example: RepeatStr('hello',3) > 'hellohellohello'
522-------------------------------------------------------------------------------}
523function TGLZStringHelper.RepeatStr(const s : String; Count : Integer) : String;
524var p : PChar;
525Slen : Integer;
526begin
527SLen := Length(s);
528SetLength(Result, Count*SLen);
529p := Pointer(Result);
530while Count > 0 do
531begin
532Move(Pointer(s)^, p^, SLen);
533Inc(p, SLen);
534Dec(Count);
535end;
536end;
537
538function TGLZStringHelper.Find(const Substr, S: string; const Index: Integer=1): Integer;
539var
540apos: Integer;
541begin
542if (SubStr <> '') and (S <> '') then
543begin
544apos := Pos(Substr, Copy(S, Index, Length(S) - Index + 1));
545if apos = 0 then
546Result := 0
547else
548Result := Index + aPos - 1;
549end
550else
551Result := 0;
552end;
553
554
555
556{-------------------------------------------------------------------------------
557*Str - Converts Boolean to a string.
558Example: Str(False) > 'False'
559See also ToBool
560-------------------------------------------------------------------------------}
561function TGLZStringHelper.ToStr(Value : Boolean) : String;
562begin
563if Value then Result := 'True' else Result := 'False';
564end;
565{-------------------------------------------------------------------------------
566*Str - Converts an Integer to a string.
567Identical to standard function IntToStr, but slightly faster.
568Example: Str(12) > '12'
569See also the overloaded Integer Str version with padding.
570-------------------------------------------------------------------------------}
571function TGLZStringHelper.ToStr(Value : Integer) : String;
572begin
573System.Str(Value:0, Result);
574end;
575
576{-------------------------------------------------------------------------------
577*Str - Converts an Integer to a string.
578Identical to standard function IntToStr, but slightly faster.
579Example: Str(12) > '12'
580See also the overloaded Integer Str version with padding.
581-------------------------------------------------------------------------------}
582function TGLZStringHelper.ToStr(Value : Extended) : String;
583begin
584result:=SysUtils.FloatToStr(value);
585(* StrToFloat
586A new set of format settings which set a fixed decimal separator can be created with the following code:
587// in your .lpr project file
588uses
589...
590{$IFDEF UNIX}
591clocale
592{ required on Linux/Unix for formatsettings support. Should be one of the first (probably after cthreads?}
593{$ENDIF}
594and:
595// in your code:
596var
597FPointSeparator, FCommaSeparator: TFormatSettings;
598begin
599// Format settings to convert a string to a float
600FPointSeparator := DefaultFormatSettings;
601FPointSeparator.DecimalSeparator := '.';
602FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
603FCommaSeparator := DefaultFormatSettings;
604FCommaSeparator.DecimalSeparator := ',';
605FCommaSeparator.ThousandSeparator := '#';// disable the thousand separator
606Later 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
609function AnSemantico.StringToFloat(AStr: string): Double;
610begin
611if Pos('.', AStr) > 0 then Result := StrToFloat(AStr, FPointSeparator)
612else Result := StrToFloat(AStr, FCommaSeparator);
613end; *)
614
615end;
616
617{-------------------------------------------------------------------------------
618*Del - returns s with all occurences of SubStr removed.
619Example: Del('the','the moon is therefore the light') > ' moon is refore light'
620See also DelWord
621--------------------------------------------------------------------------------}
622{function TStringManager.Del(const SubStr : String; const s : String): String;
623var StartPos : Integer;
624begin
625Result := s;
626StartPos := 1;
627PReplace(SubStr, '', Result, StartPos, [], []);
628end; }
629
630
631
632
633{-------------------------------------------------------------------------------
634*Ins - function version of standard insert procedure.
635Example: Ins('a','1234',2) > '1a234'
636Example: Ins('a','1234',200) > '1234a'
637-------------------------------------------------------------------------------}
638{function TStringManager.Ins(const SubStr : String; const s : String; Index : Integer = 1) : String;
639begin
640Result := s;
641System.Insert(SubStr, Result, Index);
642end; }
643{-------------------------------------------------------------------------------
644*SlashAdd - add's a slash (typically a file/path name) at the end if needed
645Example: SlashAdd('c:\windows') > 'c:\windows\'
646Example: SlashAdd('c:\windows\') > 'c:\windows\'
647See also SlashDel
648-------------------------------------------------------------------------------}
649{function TStringManager.AddSlash(const s : String) : String;
650begin
651if (Length(s) > 0) and (s[Length(s)] <> '\')
652then Result := s+'\'
653else Result := s;
654end; }
655
656{-------------------------------------------------------------------------------
657*SlashDel- reMoves a slash (typically a file/path name) if at end.
658Example: SlashDel('c:\windows') > 'c:\windows'
659Example: SlashDel('c:\windows\') > 'c:\windows'
660See also SlashAdd
661-------------------------------------------------------------------------------}
662{function TStringManager.RemoveSlash(const s : String) : String;
663begin
664if (Length(s) > 0) and (s[Length(s)] = '\')
665then Result := System.Copy(s, 1, Pred(Length(s)))
666else Result := s;
667end; }
668//--------------------------------------------------------------------------------------
669{function TokenAt(const S: string; Seperator: Char; At: Integer): string;
670var
671J, I: Integer;
672begin
673Result := '';
674J := 1;
675I := 0;
676while (I <= At) and (J <= Length(S)) do
677begin
678if (S[J] = Seperator) then
679Inc(I)
680else if (I = At) then
681Result := Result + S[J];
682Inc(J);
683end;
684Result := Trim(Result);
685end; }
686
687{Function SubString(Str: String ; Substr: String ; n: Integer): String;
688var
689z: Integer;
690begin
691Str := Str + Substr;
692For z := 1 to n do
693begin
694Str := copy(Str, pos(Substr, Str)+length(Substr), length(Str)-pos(Substr, Str)+Length(Substr));
695end;
696Result := Trim(Copy(Str, 1, pos(Substr, Str) -1));
697end; }
698
699procedure TGLZStringHelper.SkipWhiteSpace(var Line: string);
700begin
701while (Length(Line) > 0) and (Line[1] in WhiteSpaces) do
702Delete(Line, 1, 1);
703end;
704
705function TGLZStringHelper.ReadString(var Line: string): string;
706begin
707Result := '';
708SkipWhiteSpace(Line);
709while (Length(Line) > 0) and not(Line[1] in WhiteSpaces) do
710begin
711SetLength(Result, Length(Result) + 1);
712Result[Length(Result)] := Line[1];
713Delete(Line, 1, 1);
714end;
715end;
716
717function TGLZStringHelper.ReadInt(var Line: string): Integer;
718begin
719Result := StrToInt(ReadString(Line));
720end;
721function TGLZStringHelper.Surround(S: string; chs: string): string;
722begin
723Result := chs + S + chs;
724end;
725
726function TGLZStringHelper.Surround(S: string; chsL, chsR: string): string;
727begin
728Result := chsL + S + chsR;
729end;
730
731
732function TGLZStringHelper.Explode(ch : string;sep: string = ';'):TStringList;
733var
734p : integer;
735begin
736p := pos(sep,ch);
737explode := TStringList.Create;
738while p > 0 do
739begin
740explode.Add(copy(ch,1,p-1));
741//showMessage(copy(ch,1,p-1));
742if p <= length(ch) then ch := copy(ch,p+ length(sep),length(ch));
743p := pos(sep,ch);
744end;
745explode.Add(ch);
746end;
747
748function TGLZStringHelper.Implode(lst:TStringList;sep : string =';'):string;
749var
750i : integer;
751s : string;
752begin
753i:= 0;
754while i < lst.Count - 1 do begin
755s := s + lst[i] + sep;
756i := i + 1;
757end;
758if i < lst.Count then s := s + lst[i]; //Ne mets pas de séparateur sur le dernier élément
759result := s;
760end;
761
762(* StrToFloat
763A new set of format settings which set a fixed decimal separator can be created with the following code:
764// in your .lpr project file
765uses
766...
767{$IFDEF UNIX}
768clocale
769{ required on Linux/Unix for formatsettings support. Should be one of the first (probably after cthreads?}
770{$ENDIF}
771and:
772// in your code:
773var
774FPointSeparator, FCommaSeparator: TFormatSettings;
775begin
776// Format settings to convert a string to a float
777FPointSeparator := DefaultFormatSettings;
778FPointSeparator.DecimalSeparator := '.';
779FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
780FCommaSeparator := DefaultFormatSettings;
781FCommaSeparator.DecimalSeparator := ',';
782FCommaSeparator.ThousandSeparator := '#';// disable the thousand separator
783Later 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
786function AnSemantico.StringToFloat(AStr: string): Double;
787begin
788if Pos('.', AStr) > 0 then Result := StrToFloat(AStr, FPointSeparator)
789else Result := StrToFloat(AStr, FCommaSeparator);
790end; *)
791
792//==============================================================================
793{//////////////////////////////////////////////////////////////////////////////}
794{ INITIALIZATION / FINALIZATION }
795{//////////////////////////////////////////////////////////////////////////////}
796
797
798Initialization
799// FillANSIArrays;
800StrOps:=TGLZStringHelper.Create;
801Finalization
802StrOps.Free;
803//==============================================================================
804end.
805