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. @-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(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 char alone. Must be implemented because }
  579. { pos(c: char; const s: shortstring) also exists, so otherwise }
  580. { using pos(char,pchar) will always call the shortstring version }
  581. { (exact match for first argument), also with $h+ (JM) }
  582. Function Pos (c : Char; 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 {$ifdef VER3_0}Delete{$else}fpc_widestr_delete{$endif}(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 {$ifdef VER3_0}Insert{$else}fpc_widestr_insert{$endif}(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 {$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}
  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 {$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}
  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 (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 s='' then
  822. exit;
  823. SetLength(hs,length(s)*3);
  824. i:=UnicodeToUtf8(pchar(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;