syswide.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {%MainUnit sysutils.pp}
  2. {
  3. *********************************************************************
  4. Copyright (C) 2002-2005 by Florian Klaempfl
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. *********************************************************************
  11. }
  12. function IsLeadChar(Ch: WideChar): Boolean;
  13. begin
  14. Result := (Ch >= #$D800) and (Ch <= #$DFFF);
  15. end;
  16. function Trim(const S: widestring): widestring;
  17. var
  18. Ofs, Len: sizeint;
  19. begin
  20. len := Length(S);
  21. while (Len>0) and (S[Len]<=' ') do
  22. dec(Len);
  23. Ofs := 1;
  24. while (Ofs<=Len) and (S[Ofs]<=' ') do
  25. Inc(Ofs);
  26. result := Copy(S, Ofs, 1 + Len - Ofs);
  27. end;
  28. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  29. function TrimLeft(const S: widestring): widestring;
  30. var
  31. i,l:sizeint;
  32. begin
  33. l := length(s);
  34. i := 1;
  35. while (i<=l) and (s[i]<=' ') do
  36. inc(i);
  37. Result := copy(s, i, l);
  38. end;
  39. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  40. function TrimRight(const S: widestring): widestring;
  41. var
  42. l:sizeint;
  43. begin
  44. l := length(s);
  45. while (l>0) and (s[l]<=' ') do
  46. dec(l);
  47. result := copy(s,1,l);
  48. end;
  49. function WideUpperCase(const s : WideString) : WideString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  50. begin
  51. result:=widestringmanager.UpperWideStringProc(s);
  52. end;
  53. function WideLowerCase(const s : WideString) : WideString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  54. begin
  55. result:=widestringmanager.LowerWideStringProc(s);
  56. end;
  57. function WideCompareStr(const s1, s2 : WideString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  58. begin
  59. result:=widestringmanager.CompareWideStringProc(s1,s2,[]);
  60. end;
  61. function WideSameStr(const s1, s2 : WideString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  62. begin
  63. result:=widestringmanager.CompareWideStringProc(s1,s2,[])=0;
  64. end;
  65. function WideCompareText(const s1, s2 : WideString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  66. begin
  67. result:=widestringmanager.CompareWideStringProc(s1,s2,[coIgnoreCase]);
  68. end;
  69. function WideSameText(const s1, s2 : WideString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  70. begin
  71. result:=widestringmanager.CompareWideStringProc(s1,s2,[coIgnoreCase])=0;
  72. end;
  73. { we've no templates, but with includes we can simulate this :) }
  74. {$macro on}
  75. {$define INWIDEFORMAT}
  76. {$define TFormatString:=widestring}
  77. {$define TFormatChar:=widechar}
  78. Function WideFormat (Const Fmt : WideString; const Args : Array of const; Const FormatSettings: TFormatSettings) : WideString;
  79. {$i sysformt.inc}
  80. {$undef TFormatString}
  81. {$undef TFormatChar}
  82. {$undef INWIDEFORMAT}
  83. {$macro off}
  84. Function WideFormat (Const Fmt : WideString; const Args : Array of const) : WideString;
  85. begin
  86. Result:=WideFormat(Fmt,Args,DefaultFormatSettings);
  87. end;
  88. Function WideFormatBuf (Var Buffer; BufLen : Cardinal;
  89. Const Fmt; fmtLen : Cardinal;
  90. Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  91. Var
  92. S,F : WideString;
  93. begin
  94. Setlength(F,fmtlen);
  95. if fmtlen > 0 then
  96. Move(fmt,F[1],fmtlen*sizeof(Widechar));
  97. S:=WideFormat (F,Args);
  98. If Cardinal(Length(S))<Buflen then
  99. Result:=Length(S)
  100. else
  101. Result:=Buflen;
  102. Move(S[1],Buffer,Result);
  103. end;
  104. Function WideFormatBuf (Var Buffer; BufLen : Cardinal;
  105. Const Fmt; fmtLen : Cardinal;
  106. Const Args : Array of const) : Cardinal;
  107. begin
  108. Result:=WideFormatBuf(Buffer,BufLEn,Fmt,FmtLen,Args,DefaultFormatSettings);
  109. end;
  110. Procedure WideFmtStr(Var Res: WideString; Const Fmt : WideString; Const args: Array of const; Const FormatSettings: TFormatSettings);
  111. begin
  112. Res:=WideFormat(fmt,Args);
  113. end;
  114. Procedure WideFmtStr(Var Res: WideString; Const Fmt : WideString; Const args: Array of const);
  115. begin
  116. WideFmtStr(Res,Fmt,Args,DefaultFormatSettings);
  117. end;
  118. function StrCopy(Dest, Source: PWideChar): PWideChar; overload;
  119. var
  120. counter : SizeInt;
  121. begin
  122. counter := 0;
  123. while Source[counter] <> #0 do
  124. begin
  125. Dest[counter] := widechar(Source[counter]);
  126. Inc(counter);
  127. end;
  128. { terminate the string }
  129. Dest[counter] := #0;
  130. StrCopy := Dest;
  131. end;
  132. function StrLCopy(Dest,Source: PWideChar; MaxLen: SizeInt): PWideChar; overload;
  133. var
  134. counter: SizeInt;
  135. begin
  136. counter := 0;
  137. while (Source[counter] <> #0) and (counter < MaxLen) do
  138. begin
  139. Dest[counter] := widechar(Source[counter]);
  140. Inc(counter);
  141. end;
  142. { terminate the string }
  143. Dest[counter] := #0;
  144. StrLCopy := Dest;
  145. end;
  146. Function CharInSet(Ch:WideChar;Const CSet : TSysCharSet) : Boolean;
  147. begin
  148. result:=(Ch<=#$FF) and (ansichar(byte(ch)) in CSet);
  149. end;
  150. {$macro on}
  151. {$define INWIDESTRINGREPLACE}
  152. {$define SRString:=WideString}
  153. {$define SRUpperCase:=WideUppercase}
  154. {$define SRPChar:=PWideChar}
  155. {$define SRChar:=WideChar}
  156. Function WideStringReplace(const S, OldPattern, NewPattern: Widestring; Flags: TReplaceFlags): Widestring;
  157. Var
  158. C : Integer;
  159. begin
  160. Result:=WideStringReplace(S,OldPattern,NewPattern,Flags,C);
  161. end;
  162. function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
  163. {$i syssr.inc}
  164. {$undef INWIDESTRINGREPLACE}
  165. {$undef SRString}
  166. {$undef SRUpperCase}
  167. {$undef SRPChar}
  168. {$undef SRChar}