sysuni.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. {
  2. *********************************************************************
  3. Copyright (C) 2002-2005 by Florian Klaempfl
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *********************************************************************
  10. }
  11. function Trim(const S: unicodestring): unicodestring;
  12. var
  13. Ofs, Len: sizeint;
  14. begin
  15. len := Length(S);
  16. while (Len>0) and (S[Len]<=' ') do
  17. dec(Len);
  18. Ofs := 1;
  19. while (Ofs<=Len) and (S[Ofs]<=' ') do
  20. Inc(Ofs);
  21. result := Copy(S, Ofs, 1 + Len - Ofs);
  22. end;
  23. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  24. function TrimLeft(const S: unicodestring): unicodestring;
  25. var
  26. i,l:sizeint;
  27. begin
  28. l := length(s);
  29. i := 1;
  30. while (i<=l) and (s[i]<=' ') do
  31. inc(i);
  32. Result := copy(s, i, l);
  33. end;
  34. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  35. function TrimRight(const S: unicodestring): unicodestring;
  36. var
  37. l:sizeint;
  38. begin
  39. l := length(s);
  40. while (l>0) and (s[l]<=' ') do
  41. dec(l);
  42. result := copy(s,1,l);
  43. end;
  44. function UnicodeUpperCase(const s : UnicodeString) : UnicodeString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  45. begin
  46. result:=widestringmanager.UpperUnicodeStringProc(s);
  47. end;
  48. function UnicodeLowerCase(const s : UnicodeString) : UnicodeString;{$ifdef SYSUTILSINLINE}inline;{$endif}
  49. begin
  50. result:=widestringmanager.LowerUnicodeStringProc(s);
  51. end;
  52. function UnicodeCompareStr(const s1, s2 : UnicodeString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  53. begin
  54. result:=widestringmanager.CompareUnicodeStringProc(s1,s2);
  55. end;
  56. function UnicodeSameStr(const s1, s2 : UnicodeString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  57. begin
  58. result:=widestringmanager.CompareUnicodeStringProc(s1,s2)=0;
  59. end;
  60. function UnicodeCompareText(const s1, s2 : UnicodeString) : PtrInt;{$ifdef SYSUTILSINLINE}inline;{$endif}
  61. begin
  62. result:=widestringmanager.CompareTextUnicodeStringProc(s1,s2);
  63. end;
  64. function UnicodeSameText(const s1, s2 : UnicodeString) : Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  65. begin
  66. result:=widestringmanager.CompareTextUnicodeStringProc(s1,s2)=0;
  67. end;
  68. { we've no templates, but with includes we can simulate this :) }
  69. {$macro on}
  70. {$define INWIDEFORMAT}
  71. {$define TFormatString:=unicodestring}
  72. {$define TFormatChar:=unicodechar}
  73. Function UnicodeFormat (Const Fmt : UnicodeString; const Args : Array of const; Const FormatSettings: TFormatSettings) : UnicodeString;
  74. {$i sysformt.inc}
  75. {$undef TFormatString}
  76. {$undef TFormatChar}
  77. {$undef INWIDEFORMAT}
  78. {$macro off}
  79. Function UnicodeFormat (Const Fmt : UnicodeString; const Args : Array of const) : UnicodeString;
  80. begin
  81. Result:=UnicodeFormat(Fmt,Args,DefaultFormatSettings);
  82. end;
  83. Function UnicodeFormatBuf (Var Buffer; BufLen : Cardinal;
  84. Const Fmt; fmtLen : Cardinal;
  85. Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  86. Var
  87. S,F : UnicodeString;
  88. begin
  89. Setlength(F,fmtlen);
  90. if fmtlen > 0 then
  91. Move(fmt,F[1],fmtlen*sizeof(Unicodechar));
  92. S:=UnicodeFormat (F,Args);
  93. If Cardinal(Length(S))<Buflen then
  94. Result:=Length(S)
  95. else
  96. Result:=Buflen;
  97. Move(S[1],Buffer,Result);
  98. end;
  99. Function UnicodeFormatBuf (Var Buffer; BufLen : Cardinal;
  100. Const Fmt; fmtLen : Cardinal;
  101. Const Args : Array of const) : Cardinal;
  102. begin
  103. Result:=UnicodeFormatBuf(Buffer,BufLEn,Fmt,FmtLen,Args,DefaultFormatSettings);
  104. end;
  105. Procedure UnicodeFmtStr(Var Res: UnicodeString; Const Fmt : UnicodeString; Const args: Array of const; Const FormatSettings: TFormatSettings);
  106. begin
  107. Res:=UnicodeFormat(fmt,Args);
  108. end;
  109. Procedure UnicodeFmtStr(Var Res: UnicodeString; Const Fmt : UnicodeString; Const args: Array of const);
  110. begin
  111. UnicodeFmtStr(Res,Fmt,Args,DefaultFormatSettings);
  112. end;
  113. function StrMove(dest,source : PWideChar;l : SizeInt) : PWideChar; overload;
  114. begin
  115. move(source^,dest^,l*2);
  116. Result:=dest;
  117. end;
  118. function StrPLCopy(Dest: PWideChar; const Source: UnicodeString; MaxLen: SizeInt): PWideChar; overload;
  119. var Len: SizeInt;
  120. begin
  121. Len := length(Source);
  122. if Len > MaxLen then
  123. Len := MaxLen;
  124. Move(Source[1], Dest^, Len*sizeof(WideChar));
  125. Dest[Len] := #0;
  126. StrPLCopy := Dest;
  127. end;
  128. function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
  129. begin
  130. StrPCopy := StrPLCopy(Dest, Source, length(Source));
  131. end;
  132. function StrScan(P: PWideChar; C: WideChar): PWideChar;
  133. Var
  134. count: SizeInt;
  135. Begin
  136. count := 0;
  137. { As in Borland Pascal, if looking for NULL return null }
  138. if C = #0 then
  139. begin
  140. StrScan := @(P[StrLen(P)]);
  141. exit;
  142. end;
  143. { Find first matching character of Ch in Str }
  144. while P[count] <> #0 do
  145. begin
  146. if C = P[count] then
  147. begin
  148. StrScan := @(P[count]);
  149. exit;
  150. end;
  151. Inc(count);
  152. end;
  153. { nothing found. }
  154. StrScan := nil;
  155. end;
  156. function strnew(p : PWideChar) : PWideChar; overload;
  157. var
  158. len : SizeInt;
  159. begin
  160. Result:=nil;
  161. if (p=nil) or (p^=#0) then
  162. exit;
  163. len:=strlen(p)+1;
  164. Result:=PWideChar(StrAlloc(Len*2));
  165. if Result<>nil then
  166. strmove(Result,p,len);
  167. end;
  168. function StrPas(Str: PWideChar): UnicodeString;overload;
  169. begin
  170. Result:=Str;
  171. end;
  172. function BytesOf(const Val: UnicodeString): TBytes;
  173. begin
  174. Result:=TEncoding.Default.GetBytes(Val);
  175. end;
  176. function BytesOf(const Val: WideChar): TBytes; overload;
  177. begin
  178. Result:=TEncoding.Default.GetBytes(Val);
  179. end;
  180. function StringOf(const Bytes: TBytes): UnicodeString;
  181. begin
  182. Result:=TEncoding.Default.GetString(Bytes);
  183. end;
  184. function WideBytesOf(const Value: UnicodeString): TBytes;
  185. var
  186. Len:Integer;
  187. begin
  188. Len:=Length(Value)*SizeOf(UnicodeChar);
  189. SetLength(Result,Len);
  190. if Len>0 then
  191. Move(Value[1],Result[0],Len);
  192. end;
  193. function WideStringOf(const Value: TBytes): UnicodeString;
  194. var
  195. Len:Integer;
  196. begin
  197. Len:=Length(Value) div SizeOf(UnicodeChar);
  198. SetLength(Result,Len);
  199. if Len>0 then
  200. Move(Value[0],Result[1],Len*SizeOf(UnicodeChar));
  201. end;
  202. function ByteLength(const S: UnicodeString): Integer;
  203. begin
  204. Result:=Length(S)*SizeOf(UnicodeChar);
  205. end;