wstrings.inc 23 KB

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