sysuni.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 StrPLCopy(Dest: PWideChar; const Source: UnicodeString; MaxLen: SizeInt): PWideChar; overload;
  114. var Len: SizeInt;
  115. begin
  116. Len := length(Source);
  117. if Len > MaxLen then
  118. Len := MaxLen;
  119. Move(Source[1], Dest^, Len*sizeof(WideChar));
  120. Dest[Len] := #0;
  121. StrPLCopy := Dest;
  122. end;
  123. function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
  124. begin
  125. StrPCopy := StrPLCopy(Dest, Source, length(Source));
  126. end;
  127. {$IFNDEF VER2_4}
  128. function BytesOf(const Val: UnicodeString): TBytes;
  129. begin
  130. Result:=TEncoding.Default.GetBytes(Val);
  131. end;
  132. function BytesOf(const Val: WideChar): TBytes; overload;
  133. begin
  134. Result:=TEncoding.Default.GetBytes(Val);
  135. end;
  136. function StringOf(const Bytes: TBytes): UnicodeString;
  137. begin
  138. Result:=TEncoding.Default.GetString(Bytes);
  139. end;
  140. {$ENDIF VER2_4}
  141. function WideBytesOf(const Value: UnicodeString): TBytes;
  142. var
  143. Len:Integer;
  144. begin
  145. Len:=Length(Value)*SizeOf(UnicodeChar);
  146. SetLength(Result,Len);
  147. if Len>0 then
  148. Move(Value[1],Result[0],Len);
  149. end;
  150. function WideStringOf(const Value: TBytes): UnicodeString;
  151. var
  152. Len:Integer;
  153. begin
  154. Len:=Length(Value) div SizeOf(UnicodeChar);
  155. SetLength(Result,Len);
  156. if Len>0 then
  157. Move(Value[0],Result[1],Len*SizeOf(UnicodeChar));
  158. end;
  159. function ByteLength(const S: UnicodeString): Integer;
  160. begin
  161. Result:=Length(S)*SizeOf(UnicodeChar);
  162. end;