wstrings.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. This file implements support routines for WideStrings with FPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. This file contains the implementation of the WideString type,
  14. and all things that are needed for it.
  15. WideString is defined as a 'silent' pwidechar :
  16. a pwidechar that points to:
  17. @-4 : DWord for size; size=number of bytes, not the number of chars. Divide or multiply
  18. with sizeof(WideChar) to convert. This is needed to be compatible with Delphi and
  19. Windows COM BSTR.
  20. @ : String + Terminating #0;
  21. Pwidechar(Widestring) is a valid typecast.
  22. So WS[i] is converted to the address @WS+i-1.
  23. Constants should be assigned a reference count of -1
  24. Meaning that they can't be disposed of.
  25. }
  26. Type
  27. PWideRec = ^TWideRec;
  28. TWideRec = Packed Record
  29. Len : DWord;
  30. end;
  31. Const
  32. WideRecLen = SizeOf(TWideRec);
  33. WideFirstOff = SizeOf(TWideRec);
  34. {
  35. Default WideChar <-> AnsiChar conversion is to only convert the
  36. lower 127 chars, all others are translated to spaces.
  37. These routines can be overwritten for the Current Locale
  38. }
  39. procedure DefaultAnsi2WideMove(source:PAnsiChar;cp : TSystemCodePage;var dest:widestring;len:SizeInt);
  40. var
  41. i : SizeInt;
  42. begin
  43. setlength(dest,len);
  44. for i:=1 to len do
  45. begin
  46. dest[i]:=widechar(byte(source^));
  47. inc(source);
  48. end;
  49. end;
  50. {****************************************************************************
  51. Internal functions, not in interface.
  52. ****************************************************************************}
  53. procedure WideStringError;
  54. begin
  55. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  56. end;
  57. Function NewWideString(Len : SizeInt) : Pointer;
  58. {
  59. Allocate a new WideString on the heap.
  60. initialize it to zero length and reference count 1.
  61. }
  62. Var
  63. P : Pointer;
  64. begin
  65. {$ifdef MSWINDOWS}
  66. if winwidestringalloc then
  67. begin
  68. P:=SysAllocStringLen(nil,Len);
  69. if P=nil then
  70. WideStringError;
  71. end
  72. else
  73. {$endif MSWINDOWS}
  74. begin
  75. GetMem(P,Len*sizeof(WideChar)+WideRecLen);
  76. If P<>Nil then
  77. begin
  78. PWideRec(P)^.Len:=Len*2; { Initial length }
  79. inc(p,WideFirstOff); { Points to widestring now }
  80. PWideChar(P)^:=#0; { Terminating #0 }
  81. end
  82. else
  83. WideStringError;
  84. end;
  85. NewWideString:=P;
  86. end;
  87. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_DECR_REF']; compilerproc;
  88. {
  89. Decreases the ReferenceCount of a non constant widestring;
  90. If the reference count is zero, deallocate the widestring;
  91. }
  92. Begin
  93. If S=Nil then
  94. exit;
  95. {$ifdef MSWINDOWS}
  96. if winwidestringalloc then
  97. SysFreeString(S)
  98. else
  99. {$endif MSWINDOWS}
  100. begin
  101. Dec (S,WideFirstOff);
  102. Freemem(S);
  103. end;
  104. S:=Nil;
  105. end;
  106. { alias for internal use }
  107. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_DECR_REF'];
  108. Procedure fpc_WideStr_Incr_Ref(Var S : Pointer);[Public,Alias:'FPC_WIDESTR_INCR_REF']; compilerproc;
  109. var
  110. p : pointer;
  111. Begin
  112. If S=Nil then
  113. exit;
  114. p:=NewWidestring(length(WideString(S)));
  115. move(s^,p^,(length(WideString(s))+1)*sizeof(widechar)); // double #0 too
  116. s:=p;
  117. end;
  118. { alias for internal use }
  119. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_INCR_REF'];
  120. procedure fpc_WideStr_To_ShortStr (out res: ShortString;const S2 : WideString); [Public, alias: 'FPC_WIDESTR_TO_SHORTSTR'];compilerproc;
  121. {
  122. Converts a WideString to a ShortString;
  123. }
  124. Var
  125. Size : SizeInt;
  126. temp : ansistring;
  127. begin
  128. res:='';
  129. Size:=Length(S2);
  130. if Size>0 then
  131. begin
  132. If Size>high(res) then
  133. Size:=high(res);
  134. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,DefaultSystemCodePage,Size);
  135. res:=temp;
  136. end;
  137. end;
  138. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  139. {
  140. Converts a ShortString to a WideString;
  141. }
  142. Var
  143. Size : SizeInt;
  144. begin
  145. result:='';
  146. Size:=Length(S2);
  147. if Size>0 then
  148. widestringmanager.Ansi2WideMoveProc(PAnsiChar(@S2[1]),DefaultSystemCodePage,result,Size);
  149. end;
  150. Function fpc_WideStr_To_AnsiStr (const S2 : WideString;cp : TSystemCodePage): AnsiString; compilerproc;
  151. {
  152. Converts a WideString to an AnsiString
  153. }
  154. Var
  155. Size : SizeInt;
  156. begin
  157. result:='';
  158. Size:=Length(S2);
  159. if Size>0 then
  160. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,TranslatePlaceholderCP(cp),Size);
  161. end;
  162. Function fpc_AnsiStr_To_WideStr(Const S2 : RawByteString): WideString; compilerproc;
  163. {
  164. Converts an AnsiString to a WideString;
  165. }
  166. Var
  167. Size : SizeInt;
  168. cp: TSystemCodePage;
  169. begin
  170. result:='';
  171. Size:=Length(S2);
  172. if Size>0 then
  173. begin
  174. cp:=TranslatePlaceholderCP(StringCodePage(S2));
  175. widestringmanager.Ansi2WideMoveProc(PAnsiChar(S2),cp,result,Size);
  176. end;
  177. end;
  178. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  179. var
  180. Size : SizeInt;
  181. begin
  182. result:='';
  183. if p=nil then
  184. exit;
  185. Size := IndexWord(p^, -1, 0);
  186. Setlength(result,Size); // zero-terminates
  187. if Size>0 then
  188. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  189. end;
  190. { checked against the ansistring routine, 2001-05-27 (FK) }
  191. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  192. {
  193. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  194. }
  195. begin
  196. if S1=S2 then exit;
  197. if S2<>nil then
  198. begin
  199. {$ifdef MSWINDOWS}
  200. if winwidestringalloc then
  201. begin
  202. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  203. WideStringError;
  204. end
  205. else
  206. {$endif MSWINDOWS}
  207. begin
  208. SetLength(WideString(S1),length(WideString(S2)));
  209. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  210. end;
  211. end
  212. else
  213. fpc_widestr_decr_ref (S1); // will set S1 to nil
  214. end;
  215. { alias for internal use }
  216. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  217. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  218. Var
  219. Size,Location : SizeInt;
  220. same : boolean;
  221. begin
  222. { only assign if s1 or s2 is empty }
  223. if Length(S1)=0 then
  224. begin
  225. DestS:=s2;
  226. exit;
  227. end;
  228. if Length(S2)=0 then
  229. begin
  230. DestS:=s1;
  231. exit;
  232. end;
  233. Location:=Length(S1);
  234. Size:=length(S2);
  235. { Use Pointer() typecasts to prevent extra conversion code }
  236. if Pointer(DestS)=Pointer(S1) then
  237. begin
  238. same:=Pointer(S1)=Pointer(S2);
  239. SetLength(DestS,Size+Location);
  240. if same then
  241. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  242. else
  243. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  244. end
  245. else if Pointer(DestS)=Pointer(S2) then
  246. begin
  247. SetLength(DestS,Size+Location);
  248. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  249. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  250. end
  251. else
  252. begin
  253. DestS:='';
  254. SetLength(DestS,Size+Location);
  255. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  256. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  257. end;
  258. end;
  259. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  260. Var
  261. i : Longint;
  262. p,pc : pointer;
  263. Size,NewLen : SizeInt;
  264. DestTmp : Widestring;
  265. begin
  266. if high(sarr)=0 then
  267. begin
  268. DestS:='';
  269. exit;
  270. end;
  271. { First calculate size of the result so we can do
  272. a single call to SetLength() }
  273. NewLen:=0;
  274. for i:=low(sarr) to high(sarr) do
  275. inc(NewLen,length(sarr[i]));
  276. SetLength(DestTmp,NewLen);
  277. pc:=pwidechar(DestTmp);
  278. for i:=low(sarr) to high(sarr) do
  279. begin
  280. p:=pointer(sarr[i]);
  281. if assigned(p) then
  282. begin
  283. Size:=length(widestring(p));
  284. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  285. inc(pc,size*sizeof(WideChar));
  286. end;
  287. end;
  288. DestS:=DestTmp;
  289. end;
  290. Function fpc_Char_To_WideStr(const c : AnsiChar): WideString; compilerproc;
  291. {
  292. Converts a AnsiChar to a WideString;
  293. }
  294. begin
  295. widestringmanager.Ansi2WideMoveProc(@c,DefaultSystemCodePage,fpc_char_To_WideStr,1);
  296. end;
  297. Function fpc_UChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  298. {
  299. Converts a WideChar to a WideString;
  300. }
  301. begin
  302. Setlength (fpc_UChar_To_WideStr,1);
  303. fpc_UChar_To_WideStr[1]:= c;
  304. end;
  305. Function fpc_PChar_To_WideStr(const p : PAnsiChar): WideString; compilerproc;
  306. Var
  307. L : SizeInt;
  308. begin
  309. if (not assigned(p)) or (p[0]=#0) Then
  310. begin
  311. fpc_pchar_to_widestr := '';
  312. exit;
  313. end;
  314. l:=IndexChar(p^,-1,#0);
  315. widestringmanager.Ansi2WideMoveProc(P,DefaultSystemCodePage,fpc_PChar_To_WideStr,l);
  316. end;
  317. Function fpc_CharArray_To_WideStr(const arr: array of AnsiChar; zerobased: boolean = true): WideString; compilerproc;
  318. var
  319. i : SizeInt;
  320. begin
  321. if (zerobased) then
  322. begin
  323. if (arr[0]=#0) Then
  324. begin
  325. fpc_chararray_to_widestr := '';
  326. exit;
  327. end;
  328. i:=IndexChar(arr,high(arr)+1,#0);
  329. if i = -1 then
  330. i := high(arr)+1;
  331. end
  332. else
  333. i := high(arr)+1;
  334. widestringmanager.Ansi2WideMoveProc(pansichar(@arr),DefaultSystemCodePage,fpc_CharArray_To_WideStr,i);
  335. end;
  336. procedure fpc_widestr_to_chararray(out res: array of AnsiChar; const src: WideString); compilerproc;
  337. var
  338. len: SizeInt;
  339. temp: ansistring;
  340. begin
  341. len := length(src);
  342. { make sure we don't dereference src if it can be nil (JM) }
  343. if len > 0 then
  344. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,DefaultSystemCodePage,len);
  345. len := length(temp);
  346. if len > length(res) then
  347. len := length(res);
  348. {$push}
  349. {$r-}
  350. move(temp[1],res[0],len);
  351. fillchar(res[len],length(res)-len,0);
  352. {$pop}
  353. end;
  354. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  355. var
  356. len: SizeInt;
  357. begin
  358. len := length(src);
  359. if len > length(res) then
  360. len := length(res);
  361. {$push}
  362. {$r-}
  363. { make sure we don't try to access element 1 of the ansistring if it's nil }
  364. if len > 0 then
  365. move(src[1],res[0],len*SizeOf(WideChar));
  366. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  367. {$pop}
  368. end;
  369. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  370. {
  371. Compares 2 WideStrings;
  372. The result is
  373. <0 if S1<S2
  374. 0 if S1=S2
  375. >0 if S1>S2
  376. }
  377. Var
  378. MaxI,Temp : SizeInt;
  379. begin
  380. if pointer(S1)=pointer(S2) then
  381. begin
  382. fpc_WideStr_Compare:=0;
  383. exit;
  384. end;
  385. Maxi:=Length(S1);
  386. temp:=Length(S2);
  387. If MaxI>Temp then
  388. MaxI:=Temp;
  389. Temp:=CompareWord(S1[1],S2[1],MaxI);
  390. if temp=0 then
  391. temp:=Length(S1)-Length(S2);
  392. fpc_WideStr_Compare:=Temp;
  393. end;
  394. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  395. {
  396. Compares 2 WideStrings for equality only;
  397. The result is
  398. 0 if S1=S2
  399. <>0 if S1<>S2
  400. }
  401. Var
  402. MaxI : SizeInt;
  403. begin
  404. if pointer(S1)=pointer(S2) then
  405. exit(0);
  406. Maxi:=Length(S1);
  407. If MaxI<>Length(S2) then
  408. exit(-1)
  409. else
  410. exit(CompareWord(S1[1],S2[1],MaxI));
  411. end;
  412. Procedure fpc_WideStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  413. begin
  414. if (p=nil) or (index>PWideRec(p-WideFirstOff)^.len div 2) or (Index<1) then
  415. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  416. end;
  417. Procedure fpc_WideStr_ZeroBased_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_WIDESTR_ZEROBASED_RANGECHECK']; compilerproc;
  418. begin
  419. if (p=nil) or (index>=PWideRec(p-WideFirstOff)^.len div 2) or (Index<0) then
  420. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  421. end;
  422. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  423. {
  424. Sets The length of WideString S to L.
  425. Makes sure S is unique, and contains enough room.
  426. }
  427. Var
  428. Temp : Pointer;
  429. movelen: SizeInt;
  430. begin
  431. if (l>0) then
  432. begin
  433. if Pointer(S)=nil then
  434. begin
  435. { Need a complete new widestring...}
  436. Pointer(s):=NewWideString(l);
  437. end
  438. { windows doesn't support reallocing widestrings, this code
  439. is anyways subject to be removed because widestrings shouldn't be
  440. ref. counted anymore (FK) }
  441. else
  442. if
  443. {$ifdef MSWINDOWS}
  444. not winwidestringalloc and
  445. {$endif MSWINDOWS}
  446. True
  447. then
  448. begin
  449. Dec(Pointer(S),WideFirstOff);
  450. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  451. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  452. Inc(Pointer(S), WideFirstOff);
  453. end
  454. else
  455. begin
  456. { Reallocation is needed... }
  457. Temp:=Pointer(NewWideString(L));
  458. if Length(S)>0 then
  459. begin
  460. if l < succ(length(s)) then
  461. movelen := l
  462. { also move terminating null }
  463. else
  464. movelen := succ(length(s));
  465. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  466. end;
  467. fpc_widestr_decr_ref(Pointer(S));
  468. Pointer(S):=Temp;
  469. end;
  470. { Force nil termination in case it gets shorter }
  471. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  472. {$ifdef MSWINDOWS}
  473. if not winwidestringalloc then
  474. {$endif MSWINDOWS}
  475. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  476. end
  477. else // length=0, deallocate the widestring
  478. fpc_widestr_decr_ref (Pointer(S));
  479. end;
  480. {*****************************************************************************
  481. Public functions, In interface.
  482. *****************************************************************************}
  483. Function fpc_widestr_Unique_func(Var S : WideString): Pointer; external name 'FPC_WIDESTR_UNIQUE';
  484. Procedure UniqueString (Var S : WideString);{$ifdef SYSTEMINLINE}inline;{$endif}
  485. begin
  486. fpc_widestr_Unique_func(S);
  487. end;
  488. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  489. begin
  490. pointer(result) := pointer(s);
  491. end;
  492. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  493. var
  494. ResultAddress : Pointer;
  495. begin
  496. ResultAddress:=Nil;
  497. dec(index);
  498. if Index < 0 then
  499. Index := 0;
  500. { Check Size. Accounts for Zero-length S, the double check is needed because
  501. Size can be maxint and will get <0 when adding index }
  502. if (Size>Length(S)) or
  503. (Index+Size>Length(S)) then
  504. Size:=Length(S)-Index;
  505. If Size>0 then
  506. begin
  507. { NewWideString won't return a nil pointer }
  508. ResultAddress:=NewWideString(Size);
  509. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  510. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  511. end;
  512. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  513. Pointer(fpc_widestr_Copy):=ResultAddress;
  514. end;
  515. Function Pos (Const Substr : WideString; Const Source : WideString; Offset : SizeInt = 1) : SizeInt;
  516. var
  517. i,MaxLen : SizeInt;
  518. pc : pwidechar;
  519. begin
  520. Pos:=0;
  521. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  522. begin
  523. MaxLen:=Length(source)-Length(SubStr)-(Offset-1);
  524. i:=0;
  525. pc:=@source[Offset];
  526. while (i<=MaxLen) do
  527. begin
  528. inc(i);
  529. if (SubStr[1]=pc^) and
  530. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  531. begin
  532. Pos:=Offset+i-1;
  533. exit;
  534. end;
  535. inc(pc);
  536. end;
  537. end;
  538. end;
  539. { Faster version for a widechar alone }
  540. Function Pos (c : WideChar; Const s : WideString; Offset : Sizeint = 1) : SizeInt;
  541. var
  542. i: SizeInt;
  543. pc : pwidechar;
  544. begin
  545. if (Offset>0) and (Offset<=length(s)) then
  546. begin
  547. pc:=@s[OffSet];
  548. for i:=OffSet to length(s) do
  549. begin
  550. if pc^=c then
  551. begin
  552. pos:=i;
  553. exit;
  554. end;
  555. inc(pc);
  556. end;
  557. end;
  558. pos:=0;
  559. end;
  560. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  561. block, which is significant bloat without any sensible speed improvement. }
  562. Function Pos (c : WideChar; Const s : RawByteString; Offset : SizeInt = 1) : SizeInt;
  563. begin
  564. result:=Pos(c,WideString(s),Offset);
  565. end;
  566. Function Pos (const c : RawByteString; Const s : WideString;Offset : SizeInt = 1) : SizeInt;
  567. begin
  568. result:=Pos(WideString(c),s,Offset);
  569. end;
  570. Function Pos (const c : ShortString; Const s : WideString;Offset : SizeInt = 1) : SizeInt;
  571. begin
  572. result:=Pos(WideString(c),s,Offset);
  573. end;
  574. Function Pos (const c : WideString; Const s : RawByteString;Offset : SizeInt = 1) : SizeInt;
  575. begin
  576. result:=Pos(c,WideString(s),Offset);
  577. end;
  578. { Faster version for a AnsiChar alone. Must be implemented because }
  579. { pos(c: ansichar; const s: shortstring) also exists, so otherwise }
  580. { using pos(AnsiChar,pansichar) will always call the shortstring version }
  581. { (exact match for first argument), also with $h+ (JM) }
  582. Function Pos (c : AnsiChar; Const s : WideString; Offset : SizeInt = 1) : SizeInt;
  583. var
  584. i: SizeInt;
  585. wc : widechar;
  586. pc : pwidechar;
  587. begin
  588. if (Offset>0) and (Offset<=Length(S)) then
  589. begin
  590. wc:=c;
  591. pc:=@s[OffSet];
  592. for i:=OffSet to length(s) do
  593. begin
  594. if pc^=wc then
  595. begin
  596. pos:=i;
  597. exit;
  598. end;
  599. inc(pc);
  600. end;
  601. end;
  602. pos:=0;
  603. end;
  604. Procedure fpc_widestr_delete(Var S : WideString; Index,Size: SizeInt);
  605. Var
  606. LS : SizeInt;
  607. begin
  608. LS:=Length(S);
  609. if (Index>LS) or (Index<=0) or (Size<=0) then
  610. exit;
  611. { (Size+Index) will overflow if Size=MaxInt. }
  612. if Size>LS-Index then
  613. Size:=LS-Index+1;
  614. if Size<=LS-Index then
  615. begin
  616. Dec(Index);
  617. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  618. end;
  619. Setlength(s,LS-Size);
  620. end;
  621. Procedure fpc_widestr_insert(Const Source : WideString; Var S : WideString; Index : SizeInt);
  622. var
  623. Temp : WideString;
  624. LS : SizeInt;
  625. begin
  626. If Length(Source)=0 then
  627. exit;
  628. if index <= 0 then
  629. index := 1;
  630. Ls:=Length(S);
  631. if index > LS then
  632. index := LS+1;
  633. Dec(Index);
  634. SetLength(Temp,Length(Source)+LS);
  635. If Index>0 then
  636. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  637. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  638. If (LS-Index)>0 then
  639. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  640. S:=Temp;
  641. end;
  642. function UpCase(const s : WideString) : WideString;
  643. begin
  644. result:=widestringmanager.UpperWideStringProc(s);
  645. end;
  646. Procedure fpc_setstring_widestr_pwidechar(Out S : WideString; Buf : PWideChar; Len : SizeInt); compilerproc;
  647. begin
  648. SetLength(S,Len);
  649. If (Buf<>Nil) and (Len>0) then
  650. Move (Buf[0],S[1],Len*sizeof(WideChar));
  651. end;
  652. Procedure fpc_setstring_widestr_pansichar(Out S : WideString; Buf : PAnsiChar; Len : SizeInt); compilerproc;
  653. begin
  654. If (Buf<>Nil) and (Len>0) then
  655. widestringmanager.Ansi2WideMoveProc(Buf,DefaultSystemCodePage,S,Len)
  656. else
  657. SetLength(s,len);
  658. end;
  659. {$ifndef FPUNONE}
  660. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  661. Var
  662. SS: ShortString;
  663. begin
  664. fpc_Val_Real_WideStr := 0;
  665. if length(S)>255 then
  666. code:=256
  667. else
  668. begin
  669. SS:=ShortString(S);
  670. Val(SS,fpc_Val_Real_WideStr,code);
  671. end;
  672. end;
  673. {$endif}
  674. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  675. var
  676. ss: ShortString;
  677. begin
  678. if length(s)>255 then
  679. code:=256
  680. else
  681. begin
  682. ss:=ShortString(s);
  683. val(ss,fpc_val_enum_widestr,code);
  684. end;
  685. end;
  686. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  687. Var
  688. SS: ShortString;
  689. begin
  690. if length(S)>255 then
  691. begin
  692. fpc_Val_Currency_WideStr:=0;
  693. code:=256;
  694. end
  695. else
  696. begin
  697. SS:=ShortString(S);
  698. Val(SS,fpc_Val_Currency_WideStr,code);
  699. end;
  700. end;
  701. Function fpc_Val_UInt_WideStr ({$ifndef VER3_2}DestSize: SizeInt;{$endif VER3_2} Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  702. Var
  703. SS: ShortString;
  704. begin
  705. fpc_Val_UInt_WideStr:=0;
  706. if length(S)>255 then
  707. code:=256
  708. else
  709. begin
  710. SS:=ShortString(S);
  711. Val(SS,fpc_Val_UInt_WideStr,code);
  712. end;
  713. end;
  714. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  715. Var
  716. SS: ShortString;
  717. begin
  718. fpc_Val_SInt_WideStr:=0;
  719. if length(S)>255 then
  720. code:=256
  721. else
  722. begin
  723. SS:=ShortString(S);
  724. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  725. end;
  726. end;
  727. {$ifndef CPU64}
  728. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  729. Var
  730. SS: ShortString;
  731. begin
  732. fpc_Val_qword_WideStr:=0;
  733. if length(S)>255 then
  734. code:=256
  735. else
  736. begin
  737. SS:=ShortString(S);
  738. Val(SS,fpc_Val_qword_WideStr,Code);
  739. end;
  740. end;
  741. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  742. Var
  743. SS: ShortString;
  744. begin
  745. fpc_Val_int64_WideStr:=0;
  746. if length(S)>255 then
  747. code:=256
  748. else
  749. begin
  750. SS:=ShortString(S);
  751. Val(SS,fpc_Val_int64_WideStr,Code);
  752. end;
  753. end;
  754. {$endif CPU64}
  755. {$ifndef FPUNONE}
  756. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  757. var
  758. ss: ShortString;
  759. begin
  760. str_real(len,fr,d,treal_type(rt),ss);
  761. s:=WideString(ss);
  762. end;
  763. {$endif}
  764. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  765. var
  766. ss: ShortString;
  767. begin
  768. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  769. s:=WideString(ss);
  770. end;
  771. procedure fpc_widestr_bool(b : boolean;len:sizeint;out s:widestring);compilerproc;
  772. var
  773. ss: ShortString;
  774. begin
  775. fpc_shortstr_bool(b,len,ss);
  776. s:=WideString(ss);
  777. end;
  778. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  779. var
  780. ss: ShortString;
  781. begin
  782. str(c:len:fr,ss);
  783. s:=WideString(ss);
  784. end;
  785. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  786. Var
  787. SS: ShortString;
  788. begin
  789. Str (v:Len,SS);
  790. S:=WideString(SS);
  791. end;
  792. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  793. Var
  794. SS: ShortString;
  795. begin
  796. str(v:Len,SS);
  797. S:=WideString(SS);
  798. end;
  799. {$ifndef CPU64}
  800. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  801. Var
  802. SS: ShortString;
  803. begin
  804. Str(v:Len,SS);
  805. S:=WideString(SS);
  806. end;
  807. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  808. Var
  809. SS: ShortString;
  810. begin
  811. str(v:Len,SS);
  812. S:=WideString(SS);
  813. end;
  814. {$endif CPU64}
  815. function UTF8Encode(const s : WideString) : RawByteString;
  816. var
  817. i : SizeInt;
  818. hs : UTF8String;
  819. begin
  820. result:='';
  821. if Length(s)=0 then
  822. exit;
  823. SetLength(hs,length(s)*3);
  824. i:=UnicodeToUtf8(pansichar(hs),length(hs)+1,PWideChar(s),length(s));
  825. if i>0 then
  826. begin
  827. SetLength(hs,i-1);
  828. result:=hs;
  829. end;
  830. end;