syswide.inc 5.5 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; mode: TTrimMode): widestring;
  17. var
  18. start, ed, ns: SizeInt;
  19. begin
  20. start := 1;
  21. ns := Length(S);
  22. ed := ns;
  23. if mode <> TTrimMode.Right then
  24. while (start <= ed) and (S[start] <= ' ') do
  25. inc(start);
  26. if mode <> TTrimMode.Left then
  27. while (start <= ed) and (S[ed] <= ' ') do
  28. dec(ed);
  29. if (start = 1) and (ed = ns) then
  30. Result := S
  31. else
  32. Result := Copy(S, start, ed - start + 1);
  33. end;
  34. function Trim(const S: widestring): widestring;
  35. begin
  36. Result := Trim(S, TTrimMode.Both);
  37. end;
  38. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  39. function TrimLeft(const S: widestring): widestring;
  40. begin
  41. Result := Trim(S, TTrimMode.Left);
  42. end;
  43. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  44. function TrimRight(const S: widestring): widestring;
  45. begin
  46. Result := Trim(S, TTrimMode.Right);
  47. end;
  48. function WideUpperCase(const s : WideString) : WideString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  49. begin
  50. result:=widestringmanager.UpperWideStringProc(s);
  51. end;
  52. function WideLowerCase(const s : WideString) : WideString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  53. begin
  54. result:=widestringmanager.LowerWideStringProc(s);
  55. end;
  56. function WideCompareStr(const s1, s2 : WideString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  57. begin
  58. result:=widestringmanager.CompareWideStringProc(s1,s2,[]);
  59. end;
  60. function WideSameStr(const s1, s2 : WideString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  61. begin
  62. result:=widestringmanager.CompareWideStringProc(s1,s2,[])=0;
  63. end;
  64. function WideCompareText(const s1, s2 : WideString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  65. begin
  66. result:=widestringmanager.CompareWideStringProc(s1,s2,[coIgnoreCase]);
  67. end;
  68. function WideSameText(const s1, s2 : WideString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  69. begin
  70. result:=widestringmanager.CompareWideStringProc(s1,s2,[coIgnoreCase])=0;
  71. end;
  72. { we've no templates, but with includes we can simulate this :) }
  73. {$macro on}
  74. {$define INWIDEFORMAT}
  75. {$define TFormatString:=widestring}
  76. {$define TFormatChar:=widechar}
  77. Function WideFormat (Const Fmt : WideString; const Args : Array of const; Const FormatSettings: TFormatSettings) : WideString;
  78. {$i sysformt.inc}
  79. {$undef TFormatString}
  80. {$undef TFormatChar}
  81. {$undef INWIDEFORMAT}
  82. {$macro off}
  83. Function WideFormat (Const Fmt : WideString; const Args : Array of const) : WideString;
  84. begin
  85. Result:=WideFormat(Fmt,Args,DefaultFormatSettings);
  86. end;
  87. Function WideFormatBuf (Var Buffer; BufLen : Cardinal;
  88. Const Fmt; fmtLen : Cardinal;
  89. Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  90. Var
  91. S,F : WideString;
  92. begin
  93. Setlength(F,fmtlen);
  94. if fmtlen > 0 then
  95. Move(fmt,F[1],fmtlen*sizeof(Widechar));
  96. S:=WideFormat (F,Args);
  97. If Cardinal(Length(S))<Buflen then
  98. Result:=Length(S)
  99. else
  100. Result:=Buflen;
  101. Move(S[1],Buffer,Result);
  102. end;
  103. Function WideFormatBuf (Var Buffer; BufLen : Cardinal;
  104. Const Fmt; fmtLen : Cardinal;
  105. Const Args : Array of const) : Cardinal;
  106. begin
  107. Result:=WideFormatBuf(Buffer,BufLEn,Fmt,FmtLen,Args,DefaultFormatSettings);
  108. end;
  109. Procedure WideFmtStr(Var Res: WideString; Const Fmt : WideString; Const args: Array of const; Const FormatSettings: TFormatSettings);
  110. begin
  111. Res:=WideFormat(fmt,Args);
  112. end;
  113. Procedure WideFmtStr(Var Res: WideString; Const Fmt : WideString; Const args: Array of const);
  114. begin
  115. WideFmtStr(Res,Fmt,Args,DefaultFormatSettings);
  116. end;
  117. function StrCopy(Dest, Source: PWideChar): PWideChar; overload;
  118. var
  119. counter : SizeInt;
  120. begin
  121. counter := 0;
  122. while Source[counter] <> #0 do
  123. begin
  124. Dest[counter] := widechar(Source[counter]);
  125. Inc(counter);
  126. end;
  127. { terminate the string }
  128. Dest[counter] := #0;
  129. StrCopy := Dest;
  130. end;
  131. function StrLCopy(Dest,Source: PWideChar; MaxLen: SizeInt): PWideChar; overload;
  132. var
  133. counter: SizeInt;
  134. begin
  135. counter := 0;
  136. while (Source[counter] <> #0) and (counter < MaxLen) do
  137. begin
  138. Dest[counter] := widechar(Source[counter]);
  139. Inc(counter);
  140. end;
  141. { terminate the string }
  142. Dest[counter] := #0;
  143. StrLCopy := Dest;
  144. end;
  145. Function CharInSet(Ch:WideChar;Const CSet : TSysCharSet) : Boolean;
  146. begin
  147. result:=(Ch<=#$FF) and (ansichar(byte(ch)) in CSet);
  148. end;
  149. {$macro on}
  150. {$define INWIDESTRINGREPLACE}
  151. {$define SRString:=WideString}
  152. {$define SRUpperCase:=WideUppercase}
  153. {$define SRPChar:=PWideChar}
  154. {$define SRChar:=WideChar}
  155. Function WideStringReplace(const S, OldPattern, NewPattern: Widestring; Flags: TReplaceFlags): Widestring;
  156. Var
  157. C : Integer;
  158. begin
  159. Result:=WideStringReplace(S,OldPattern,NewPattern,Flags,C);
  160. end;
  161. function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
  162. {$i syssr.inc}
  163. {$undef INWIDESTRINGREPLACE}
  164. {$undef SRString}
  165. {$undef SRUpperCase}
  166. {$undef SRPChar}
  167. {$undef SRChar}