wstrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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_ZeroBased_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_WIDESTR_ZEROBASED_RANGECHECK']; compilerproc;
  428. begin
  429. if (p=nil) or (index>=PWideRec(p-WideFirstOff)^.len div 2) or (Index<0) then
  430. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  431. end;
  432. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  433. {
  434. Sets The length of string S to L.
  435. Makes sure S is unique, and contains enough room.
  436. }
  437. Var
  438. Temp : Pointer;
  439. movelen: SizeInt;
  440. begin
  441. if (l>0) then
  442. begin
  443. if Pointer(S)=nil then
  444. begin
  445. { Need a complete new string...}
  446. Pointer(s):=NewWideString(l);
  447. end
  448. { windows doesn't support reallocing widestrings, this code
  449. is anyways subject to be removed because widestrings shouldn't be
  450. ref. counted anymore (FK) }
  451. else
  452. if
  453. {$ifdef MSWINDOWS}
  454. not winwidestringalloc and
  455. {$endif MSWINDOWS}
  456. True
  457. then
  458. begin
  459. Dec(Pointer(S),WideFirstOff);
  460. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  461. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  462. Inc(Pointer(S), WideFirstOff);
  463. end
  464. else
  465. begin
  466. { Reallocation is needed... }
  467. Temp:=Pointer(NewWideString(L));
  468. if Length(S)>0 then
  469. begin
  470. if l < succ(length(s)) then
  471. movelen := l
  472. { also move terminating null }
  473. else
  474. movelen := succ(length(s));
  475. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  476. end;
  477. fpc_widestr_decr_ref(Pointer(S));
  478. Pointer(S):=Temp;
  479. end;
  480. { Force nil termination in case it gets shorter }
  481. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  482. {$ifdef MSWINDOWS}
  483. if not winwidestringalloc then
  484. {$endif MSWINDOWS}
  485. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  486. end
  487. else // length=0, deallocate the string
  488. fpc_widestr_decr_ref (Pointer(S));
  489. end;
  490. {*****************************************************************************
  491. Public functions, In interface.
  492. *****************************************************************************}
  493. Function fpc_widestr_Unique_func(Var S : WideString): Pointer; external name 'FPC_WIDESTR_UNIQUE';
  494. Procedure UniqueString (Var S : WideString);{$ifdef SYSTEMINLINE}inline;{$endif}
  495. begin
  496. fpc_widestr_Unique_func(S);
  497. end;
  498. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  499. begin
  500. pointer(result) := pointer(s);
  501. end;
  502. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  503. var
  504. ResultAddress : Pointer;
  505. begin
  506. ResultAddress:=Nil;
  507. dec(index);
  508. if Index < 0 then
  509. Index := 0;
  510. { Check Size. Accounts for Zero-length S, the double check is needed because
  511. Size can be maxint and will get <0 when adding index }
  512. if (Size>Length(S)) or
  513. (Index+Size>Length(S)) then
  514. Size:=Length(S)-Index;
  515. If Size>0 then
  516. begin
  517. { NewWideString won't return a nil pointer }
  518. ResultAddress:=NewWideString(Size);
  519. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  520. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  521. end;
  522. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  523. Pointer(fpc_widestr_Copy):=ResultAddress;
  524. end;
  525. Function Pos (Const Substr : WideString; Const Source : WideString; Offset : SizeInt = 1) : SizeInt;
  526. var
  527. i,MaxLen : SizeInt;
  528. pc : pwidechar;
  529. begin
  530. Pos:=0;
  531. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  532. begin
  533. MaxLen:=Length(source)-Length(SubStr)-(Offset-1);
  534. i:=0;
  535. pc:=@source[Offset];
  536. while (i<=MaxLen) do
  537. begin
  538. inc(i);
  539. if (SubStr[1]=pc^) and
  540. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  541. begin
  542. Pos:=Offset+i-1;
  543. exit;
  544. end;
  545. inc(pc);
  546. end;
  547. end;
  548. end;
  549. { Faster version for a widechar alone }
  550. Function Pos (c : WideChar; Const s : WideString; Offset : Sizeint = 1) : SizeInt;
  551. var
  552. i: SizeInt;
  553. pc : pwidechar;
  554. begin
  555. if (Offset>0) and (Offset<=length(s)) then
  556. begin
  557. pc:=@s[OffSet];
  558. for i:=OffSet to length(s) do
  559. begin
  560. if pc^=c then
  561. begin
  562. pos:=i;
  563. exit;
  564. end;
  565. inc(pc);
  566. end;
  567. end;
  568. pos:=0;
  569. end;
  570. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  571. block, which is significant bloat without any sensible speed improvement. }
  572. Function Pos (c : WideChar; Const s : RawByteString; Offset : SizeInt = 1) : SizeInt;
  573. begin
  574. result:=Pos(c,WideString(s),Offset);
  575. end;
  576. Function Pos (const c : RawByteString; Const s : WideString;Offset : SizeInt = 1) : SizeInt;
  577. begin
  578. result:=Pos(WideString(c),s,Offset);
  579. end;
  580. Function Pos (const c : ShortString; Const s : WideString;Offset : SizeInt = 1) : SizeInt;
  581. begin
  582. result:=Pos(WideString(c),s,Offset);
  583. end;
  584. Function Pos (const c : WideString; Const s : RawByteString;Offset : SizeInt = 1) : SizeInt;
  585. begin
  586. result:=Pos(c,WideString(s),Offset);
  587. end;
  588. { Faster version for a char alone. Must be implemented because }
  589. { pos(c: char; const s: shortstring) also exists, so otherwise }
  590. { using pos(char,pchar) will always call the shortstring version }
  591. { (exact match for first argument), also with $h+ (JM) }
  592. Function Pos (c : Char; Const s : WideString; Offset : SizeInt = 1) : SizeInt;
  593. var
  594. i: SizeInt;
  595. wc : widechar;
  596. pc : pwidechar;
  597. begin
  598. if (Offset>0) and (Offset<=Length(S)) then
  599. begin
  600. wc:=c;
  601. pc:=@s[OffSet];
  602. for i:=OffSet to length(s) do
  603. begin
  604. if pc^=wc then
  605. begin
  606. pos:=i;
  607. exit;
  608. end;
  609. inc(pc);
  610. end;
  611. end;
  612. pos:=0;
  613. end;
  614. Procedure {$ifdef VER3_0}Delete{$else}fpc_widestr_delete{$endif}(Var S : WideString; Index,Size: SizeInt);
  615. Var
  616. LS : SizeInt;
  617. begin
  618. LS:=Length(S);
  619. if (Index>LS) or (Index<=0) or (Size<=0) then
  620. exit;
  621. { (Size+Index) will overflow if Size=MaxInt. }
  622. if Size>LS-Index then
  623. Size:=LS-Index+1;
  624. if Size<=LS-Index then
  625. begin
  626. Dec(Index);
  627. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  628. end;
  629. Setlength(s,LS-Size);
  630. end;
  631. Procedure {$ifdef VER3_0}Insert{$else}fpc_widestr_insert{$endif}(Const Source : WideString; Var S : WideString; Index : SizeInt);
  632. var
  633. Temp : WideString;
  634. LS : SizeInt;
  635. begin
  636. If Length(Source)=0 then
  637. exit;
  638. if index <= 0 then
  639. index := 1;
  640. Ls:=Length(S);
  641. if index > LS then
  642. index := LS+1;
  643. Dec(Index);
  644. SetLength(Temp,Length(Source)+LS);
  645. If Index>0 then
  646. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  647. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  648. If (LS-Index)>0 then
  649. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  650. S:=Temp;
  651. end;
  652. function UpCase(const s : WideString) : WideString;
  653. begin
  654. result:=widestringmanager.UpperWideStringProc(s);
  655. end;
  656. 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}
  657. begin
  658. SetLength(S,Len);
  659. If (Buf<>Nil) and (Len>0) then
  660. Move (Buf[0],S[1],Len*sizeof(WideChar));
  661. end;
  662. 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}
  663. begin
  664. If (Buf<>Nil) and (Len>0) then
  665. widestringmanager.Ansi2WideMoveProc(Buf,DefaultSystemCodePage,S,Len)
  666. else
  667. SetLength(s,len);
  668. end;
  669. {$ifndef FPUNONE}
  670. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  671. Var
  672. SS: ShortString;
  673. begin
  674. fpc_Val_Real_WideStr := 0;
  675. if length(S)>255 then
  676. code:=256
  677. else
  678. begin
  679. SS:=ShortString(S);
  680. Val(SS,fpc_Val_Real_WideStr,code);
  681. end;
  682. end;
  683. {$endif}
  684. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  685. var
  686. ss: ShortString;
  687. begin
  688. if length(s)>255 then
  689. code:=256
  690. else
  691. begin
  692. ss:=ShortString(s);
  693. val(ss,fpc_val_enum_widestr,code);
  694. end;
  695. end;
  696. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  697. Var
  698. SS: ShortString;
  699. begin
  700. if length(S)>255 then
  701. begin
  702. fpc_Val_Currency_WideStr:=0;
  703. code:=256;
  704. end
  705. else
  706. begin
  707. SS:=ShortString(S);
  708. Val(SS,fpc_Val_Currency_WideStr,code);
  709. end;
  710. end;
  711. 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;
  712. Var
  713. SS: ShortString;
  714. begin
  715. fpc_Val_UInt_WideStr:=0;
  716. if length(S)>255 then
  717. code:=256
  718. else
  719. begin
  720. SS:=ShortString(S);
  721. Val(SS,fpc_Val_UInt_WideStr,code);
  722. end;
  723. end;
  724. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  725. Var
  726. SS: ShortString;
  727. begin
  728. fpc_Val_SInt_WideStr:=0;
  729. if length(S)>255 then
  730. code:=256
  731. else
  732. begin
  733. SS:=ShortString(S);
  734. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  735. end;
  736. end;
  737. {$ifndef CPU64}
  738. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  739. Var
  740. SS: ShortString;
  741. begin
  742. fpc_Val_qword_WideStr:=0;
  743. if length(S)>255 then
  744. code:=256
  745. else
  746. begin
  747. SS:=ShortString(S);
  748. Val(SS,fpc_Val_qword_WideStr,Code);
  749. end;
  750. end;
  751. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  752. Var
  753. SS: ShortString;
  754. begin
  755. fpc_Val_int64_WideStr:=0;
  756. if length(S)>255 then
  757. code:=256
  758. else
  759. begin
  760. SS:=ShortString(S);
  761. Val(SS,fpc_Val_int64_WideStr,Code);
  762. end;
  763. end;
  764. {$endif CPU64}
  765. {$ifndef FPUNONE}
  766. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  767. var
  768. ss: ShortString;
  769. begin
  770. str_real(len,fr,d,treal_type(rt),ss);
  771. s:=WideString(ss);
  772. end;
  773. {$endif}
  774. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  775. var
  776. ss: ShortString;
  777. begin
  778. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  779. s:=WideString(ss);
  780. end;
  781. procedure fpc_widestr_bool(b : boolean;len:sizeint;out s:widestring);compilerproc;
  782. var
  783. ss: ShortString;
  784. begin
  785. fpc_shortstr_bool(b,len,ss);
  786. s:=WideString(ss);
  787. end;
  788. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  789. var
  790. ss: ShortString;
  791. begin
  792. str(c:len:fr,ss);
  793. s:=WideString(ss);
  794. end;
  795. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  796. Var
  797. SS: ShortString;
  798. begin
  799. Str (v:Len,SS);
  800. S:=WideString(SS);
  801. end;
  802. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  803. Var
  804. SS: ShortString;
  805. begin
  806. str(v:Len,SS);
  807. S:=WideString(SS);
  808. end;
  809. {$ifndef CPU64}
  810. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  811. Var
  812. SS: ShortString;
  813. begin
  814. Str(v:Len,SS);
  815. S:=WideString(SS);
  816. end;
  817. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  818. Var
  819. SS: ShortString;
  820. begin
  821. str(v:Len,SS);
  822. S:=WideString(SS);
  823. end;
  824. {$endif CPU64}
  825. function UTF8Encode(const s : WideString) : RawByteString;
  826. var
  827. i : SizeInt;
  828. hs : UTF8String;
  829. begin
  830. result:='';
  831. if s='' then
  832. exit;
  833. SetLength(hs,length(s)*3);
  834. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  835. if i>0 then
  836. begin
  837. SetLength(hs,i-1);
  838. result:=hs;
  839. end;
  840. end;