sysuni.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 StrPas(Str: PWideChar): UnicodeString;overload;
  157. begin
  158. Result:=Str;
  159. end;
  160. function strecopy(dest,source : pwidechar) : pwidechar;
  161. var
  162. counter: sizeint;
  163. begin
  164. counter := indexword(source^,-1,0);
  165. { counter+1 will move zero terminator }
  166. move(source^,dest^,(counter+1)*2);
  167. result:=dest+counter;
  168. end;
  169. function strend(p : pwidechar) : pwidechar;
  170. begin
  171. result:=p+indexword(p^,-1,0);
  172. end;
  173. function strcat(dest,source : pwidechar) : pwidechar;
  174. begin
  175. strcopy(strend(dest),source);
  176. strcat:=dest;
  177. end;
  178. function strcomp(str1,str2 : pwidechar) : SizeInt;
  179. var
  180. counter: sizeint;
  181. c1, c2: widechar;
  182. begin
  183. counter:=0;
  184. repeat
  185. c1:=str1[counter];
  186. c2:=str2[counter];
  187. inc(counter);
  188. until (c1<>c2) or
  189. (c1=#0) or
  190. (c2=#0);
  191. strcomp:=ord(c1)-ord(c2);
  192. end;
  193. function strlcomp(str1,str2 : pwidechar;l : SizeInt) : SizeInt;
  194. var
  195. counter: sizeint;
  196. c1, c2: widechar;
  197. begin
  198. if l = 0 then
  199. begin
  200. strlcomp := 0;
  201. exit;
  202. end;
  203. counter:=0;
  204. repeat
  205. c1:=str1[counter];
  206. c2:=str2[counter];
  207. inc(counter);
  208. until (c1<>c2) or (counter>=l) or
  209. (c1=#0) or (c2=#0);
  210. strlcomp:=ord(c1)-ord(c2);
  211. end;
  212. { the str* functions are not supposed to support internationalisation;
  213. system.upcase(widechar) does support it (although this is
  214. Delphi-incompatible) }
  215. function simplewideupcase(w: widechar): widechar;
  216. begin
  217. if w in ['a'..'z'] then
  218. result:=widechar(ord(w)-32)
  219. else
  220. result:=w;
  221. end;
  222. function stricomp(str1,str2 : pwidechar) : SizeInt;
  223. var
  224. counter: sizeint;
  225. c1, c2: widechar;
  226. begin
  227. counter := 0;
  228. c1:=simplewideupcase(str1[counter]);
  229. c2:=simplewideupcase(str2[counter]);
  230. while c1=c2 do
  231. begin
  232. if (c1=#0) or (c2=#0) then break;
  233. inc(counter);
  234. c1:=simplewideupcase(str1[counter]);
  235. c2:=simplewideupcase(str2[counter]);
  236. end;
  237. stricomp:=ord(c1)-ord(c2);
  238. end;
  239. function strlcat(dest,source : pwidechar;l : SizeInt) : pwidechar;
  240. var
  241. destend : pwidechar;
  242. begin
  243. destend:=strend(dest);
  244. dec(l,destend-dest);
  245. if l>0 then
  246. strlcopy(destend,source,l);
  247. strlcat:=dest;
  248. end;
  249. function strrscan(p : pwidechar;c : widechar) : pwidechar;
  250. var
  251. count: sizeint;
  252. index: sizeint;
  253. begin
  254. count:=strlen(p);
  255. { As in Borland Pascal , if looking for NULL return null }
  256. if c=#0 then
  257. begin
  258. strrscan:=@(p[count]);
  259. exit;
  260. end;
  261. dec(count);
  262. for index:=count downto 0 do
  263. begin
  264. if c=p[index] then
  265. begin
  266. strrscan:=@(p[index]);
  267. exit;
  268. end;
  269. end;
  270. { nothing found. }
  271. strrscan:=nil;
  272. end;
  273. function strlower(p : pwidechar) : pwidechar;
  274. var
  275. counter: SizeInt;
  276. c: widechar;
  277. begin
  278. counter:=0;
  279. repeat
  280. c:=p[counter];
  281. if c in [#65..#90] then
  282. p[counter]:=widechar(ord(c)+32);
  283. inc(counter);
  284. until c=#0;
  285. strlower:=p;
  286. end;
  287. function strupper(p : pwidechar) : pwidechar;
  288. var
  289. counter: SizeInt;
  290. c: widechar;
  291. begin
  292. counter:=0;
  293. repeat
  294. c:=p[counter];
  295. if c in [#97..#122] then
  296. p[counter]:=widechar(ord(c)-32);
  297. inc(counter);
  298. until c=#0;
  299. strupper:=p;
  300. end;
  301. function strlicomp(str1,str2 : pwidechar;l : SizeInt) : SizeInt;
  302. var
  303. counter: sizeint;
  304. c1, c2: char;
  305. begin
  306. counter := 0;
  307. if l=0 then
  308. begin
  309. strlicomp := 0;
  310. exit;
  311. end;
  312. repeat
  313. c1:=simplewideupcase(str1[counter]);
  314. c2:=simplewideupcase(str2[counter]);
  315. if (c1=#0) or (c2=#0) then break;
  316. inc(counter);
  317. until (c1<>c2) or (counter>=l);
  318. strlicomp:=ord(c1)-ord(c2);
  319. end;
  320. function strpos(str1,str2 : pwidechar) : pwidechar;
  321. var
  322. p : pwidechar;
  323. lstr2 : SizeInt;
  324. begin
  325. strpos:=nil;
  326. if (str1=nil) or (str2=nil) then
  327. exit;
  328. p:=strscan(str1,str2^);
  329. if p=nil then
  330. exit;
  331. lstr2:=strlen(str2);
  332. while p<>nil do
  333. begin
  334. if strlcomp(p,str2,lstr2)=0 then
  335. begin
  336. strpos:=p;
  337. exit;
  338. end;
  339. inc(p);
  340. p:=strscan(p,str2^);
  341. end;
  342. end;
  343. function strnew(p : pwidechar) : pwidechar; overload;
  344. var
  345. len: sizeint;
  346. begin
  347. len:=strlen(p)+1;
  348. result:=WideStrAlloc(Len);
  349. if result<>nil then
  350. strmove(result,p,len);
  351. end;
  352. function WideStrAlloc(Size: cardinal): PWideChar;
  353. begin
  354. getmem(result,size*2+sizeof(cardinal));
  355. cardinal(pointer(result)^):=size*2+sizeof(cardinal);
  356. inc(result,sizeof(cardinal));
  357. end;
  358. function StrBufSize(str: pwidechar): SizeUInt;
  359. begin
  360. if assigned(str) then
  361. result:=cardinal(pointer(str-sizeof(cardinal))^)-sizeof(cardinal)
  362. else
  363. result := 0;
  364. end;
  365. procedure StrDispose(str: pwidechar);
  366. begin
  367. if assigned(str) then
  368. begin
  369. dec(str,sizeof(cardinal));
  370. freemem(str,cardinal(pointer(str)^));
  371. end;
  372. end;
  373. function BytesOf(const Val: UnicodeString): TBytes;
  374. begin
  375. Result:=TEncoding.Default.GetBytes(Val);
  376. end;
  377. function BytesOf(const Val: WideChar): TBytes; overload;
  378. begin
  379. Result:=TEncoding.Default.GetBytes(Val);
  380. end;
  381. function StringOf(const Bytes: TBytes): UnicodeString;
  382. begin
  383. Result:=TEncoding.Default.GetString(Bytes);
  384. end;
  385. function WideBytesOf(const Value: UnicodeString): TBytes;
  386. var
  387. Len:Integer;
  388. begin
  389. Len:=Length(Value)*SizeOf(UnicodeChar);
  390. SetLength(Result,Len);
  391. if Len>0 then
  392. Move(Value[1],Result[0],Len);
  393. end;
  394. function WideStringOf(const Value: TBytes): UnicodeString;
  395. var
  396. Len:Integer;
  397. begin
  398. Len:=Length(Value) div SizeOf(UnicodeChar);
  399. SetLength(Result,Len);
  400. if Len>0 then
  401. Move(Value[0],Result[1],Len*SizeOf(UnicodeChar));
  402. end;
  403. function ByteLength(const S: UnicodeString): Integer;
  404. begin
  405. Result:=Length(S)*SizeOf(UnicodeChar);
  406. end;