wstrings.inc 24 KB

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