wstrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. if (cp=CP_ACP) then
  170. cp:=DefaultSystemCodePage;
  171. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,cp,Size);
  172. end;
  173. end;
  174. Function fpc_AnsiStr_To_WideStr(Const S2 : RawByteString): WideString; compilerproc;
  175. {
  176. Converts an AnsiString to a WideString;
  177. }
  178. Var
  179. Size : SizeInt;
  180. cp: TSystemCodePage;
  181. begin
  182. result:='';
  183. Size:=Length(S2);
  184. if Size>0 then
  185. begin
  186. cp:=StringCodePage(S2);
  187. if (cp=CP_ACP) then
  188. cp:=DefaultSystemCodePage;
  189. widestringmanager.Ansi2WideMoveProc(PChar(S2),cp,result,Size);
  190. end;
  191. end;
  192. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  193. var
  194. Size : SizeInt;
  195. begin
  196. result:='';
  197. if p=nil then
  198. exit;
  199. Size := IndexWord(p^, -1, 0);
  200. Setlength(result,Size); // zero-terminates
  201. if Size>0 then
  202. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  203. end;
  204. { checked against the ansistring routine, 2001-05-27 (FK) }
  205. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  206. {
  207. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  208. }
  209. begin
  210. if S1=S2 then exit;
  211. if S2<>nil then
  212. begin
  213. {$ifdef MSWINDOWS}
  214. if winwidestringalloc then
  215. begin
  216. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  217. WideStringError;
  218. end
  219. else
  220. {$endif MSWINDOWS}
  221. begin
  222. SetLength(WideString(S1),length(WideString(S2)));
  223. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  224. end;
  225. end
  226. else
  227. fpc_widestr_decr_ref (S1); // will set S1 to nil
  228. end;
  229. { alias for internal use }
  230. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  231. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  232. Var
  233. Size,Location : SizeInt;
  234. same : boolean;
  235. begin
  236. { only assign if s1 or s2 is empty }
  237. if (S1='') then
  238. begin
  239. DestS:=s2;
  240. exit;
  241. end;
  242. if (S2='') then
  243. begin
  244. DestS:=s1;
  245. exit;
  246. end;
  247. Location:=Length(S1);
  248. Size:=length(S2);
  249. { Use Pointer() typecasts to prevent extra conversion code }
  250. if Pointer(DestS)=Pointer(S1) then
  251. begin
  252. same:=Pointer(S1)=Pointer(S2);
  253. SetLength(DestS,Size+Location);
  254. if same then
  255. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  256. else
  257. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  258. end
  259. else if Pointer(DestS)=Pointer(S2) then
  260. begin
  261. SetLength(DestS,Size+Location);
  262. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  263. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  264. end
  265. else
  266. begin
  267. DestS:='';
  268. SetLength(DestS,Size+Location);
  269. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  270. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  271. end;
  272. end;
  273. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  274. Var
  275. i : Longint;
  276. p,pc : pointer;
  277. Size,NewLen : SizeInt;
  278. DestTmp : Widestring;
  279. begin
  280. if high(sarr)=0 then
  281. begin
  282. DestS:='';
  283. exit;
  284. end;
  285. { First calculate size of the result so we can do
  286. a single call to SetLength() }
  287. NewLen:=0;
  288. for i:=low(sarr) to high(sarr) do
  289. inc(NewLen,length(sarr[i]));
  290. SetLength(DestTmp,NewLen);
  291. pc:=pwidechar(DestTmp);
  292. for i:=low(sarr) to high(sarr) do
  293. begin
  294. p:=pointer(sarr[i]);
  295. if assigned(p) then
  296. begin
  297. Size:=length(widestring(p));
  298. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  299. inc(pc,size*sizeof(WideChar));
  300. end;
  301. end;
  302. DestS:=DestTmp;
  303. end;
  304. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  305. {
  306. Converts a Char to a WideString;
  307. }
  308. begin
  309. widestringmanager.Ansi2WideMoveProc(@c,DefaultSystemCodePage,fpc_char_To_WideStr,1);
  310. end;
  311. Function fpc_UChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  312. {
  313. Converts a WideChar to a WideString;
  314. }
  315. begin
  316. Setlength (fpc_UChar_To_WideStr,1);
  317. fpc_UChar_To_WideStr[1]:= c;
  318. end;
  319. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  320. Var
  321. L : SizeInt;
  322. begin
  323. if (not assigned(p)) or (p[0]=#0) Then
  324. begin
  325. fpc_pchar_to_widestr := '';
  326. exit;
  327. end;
  328. l:=IndexChar(p^,-1,#0);
  329. widestringmanager.Ansi2WideMoveProc(P,DefaultSystemCodePage,fpc_PChar_To_WideStr,l);
  330. end;
  331. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  332. var
  333. i : SizeInt;
  334. begin
  335. if (zerobased) then
  336. begin
  337. if (arr[0]=#0) Then
  338. begin
  339. fpc_chararray_to_widestr := '';
  340. exit;
  341. end;
  342. i:=IndexChar(arr,high(arr)+1,#0);
  343. if i = -1 then
  344. i := high(arr)+1;
  345. end
  346. else
  347. i := high(arr)+1;
  348. widestringmanager.Ansi2WideMoveProc(pchar(@arr),DefaultSystemCodePage,fpc_CharArray_To_WideStr,i);
  349. end;
  350. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  351. var
  352. len: SizeInt;
  353. temp: ansistring;
  354. begin
  355. len := length(src);
  356. { make sure we don't dereference src if it can be nil (JM) }
  357. if len > 0 then
  358. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,DefaultSystemCodePage,len);
  359. len := length(temp);
  360. if len > length(res) then
  361. len := length(res);
  362. {$push}
  363. {$r-}
  364. move(temp[1],res[0],len);
  365. fillchar(res[len],length(res)-len,0);
  366. {$pop}
  367. end;
  368. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  369. var
  370. len: SizeInt;
  371. begin
  372. len := length(src);
  373. if len > length(res) then
  374. len := length(res);
  375. {$push}
  376. {$r-}
  377. { make sure we don't try to access element 1 of the ansistring if it's nil }
  378. if len > 0 then
  379. move(src[1],res[0],len*SizeOf(WideChar));
  380. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  381. {$pop}
  382. end;
  383. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  384. {
  385. Compares 2 WideStrings;
  386. The result is
  387. <0 if S1<S2
  388. 0 if S1=S2
  389. >0 if S1>S2
  390. }
  391. Var
  392. MaxI,Temp : SizeInt;
  393. begin
  394. if pointer(S1)=pointer(S2) then
  395. begin
  396. fpc_WideStr_Compare:=0;
  397. exit;
  398. end;
  399. Maxi:=Length(S1);
  400. temp:=Length(S2);
  401. If MaxI>Temp then
  402. MaxI:=Temp;
  403. Temp:=CompareWord(S1[1],S2[1],MaxI);
  404. if temp=0 then
  405. temp:=Length(S1)-Length(S2);
  406. fpc_WideStr_Compare:=Temp;
  407. end;
  408. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  409. {
  410. Compares 2 WideStrings for equality only;
  411. The result is
  412. 0 if S1=S2
  413. <>0 if S1<>S2
  414. }
  415. Var
  416. MaxI : SizeInt;
  417. begin
  418. if pointer(S1)=pointer(S2) then
  419. exit(0);
  420. Maxi:=Length(S1);
  421. If MaxI<>Length(S2) then
  422. exit(-1)
  423. else
  424. exit(CompareWord(S1[1],S2[1],MaxI));
  425. end;
  426. Procedure fpc_WideStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  427. begin
  428. if (p=nil) or (index>PWideRec(p-WideFirstOff)^.len div 2) or (Index<1) then
  429. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  430. end;
  431. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  432. {
  433. Sets The length of string S to L.
  434. Makes sure S is unique, and contains enough room.
  435. }
  436. Var
  437. Temp : Pointer;
  438. movelen: SizeInt;
  439. begin
  440. if (l>0) then
  441. begin
  442. if Pointer(S)=nil then
  443. begin
  444. { Need a complete new string...}
  445. Pointer(s):=NewWideString(l);
  446. end
  447. { windows doesn't support reallocing widestrings, this code
  448. is anyways subject to be removed because widestrings shouldn't be
  449. ref. counted anymore (FK) }
  450. else
  451. if
  452. {$ifdef MSWINDOWS}
  453. not winwidestringalloc and
  454. {$endif MSWINDOWS}
  455. True
  456. then
  457. begin
  458. Dec(Pointer(S),WideFirstOff);
  459. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  460. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  461. Inc(Pointer(S), WideFirstOff);
  462. end
  463. else
  464. begin
  465. { Reallocation is needed... }
  466. Temp:=Pointer(NewWideString(L));
  467. if Length(S)>0 then
  468. begin
  469. if l < succ(length(s)) then
  470. movelen := l
  471. { also move terminating null }
  472. else
  473. movelen := succ(length(s));
  474. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  475. end;
  476. fpc_widestr_decr_ref(Pointer(S));
  477. Pointer(S):=Temp;
  478. end;
  479. { Force nil termination in case it gets shorter }
  480. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  481. {$ifdef MSWINDOWS}
  482. if not winwidestringalloc then
  483. {$endif MSWINDOWS}
  484. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  485. end
  486. else // length=0, deallocate the string
  487. fpc_widestr_decr_ref (Pointer(S));
  488. end;
  489. {*****************************************************************************
  490. Public functions, In interface.
  491. *****************************************************************************}
  492. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  493. begin
  494. pointer(result) := pointer(s);
  495. end;
  496. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  497. var
  498. ResultAddress : Pointer;
  499. begin
  500. ResultAddress:=Nil;
  501. dec(index);
  502. if Index < 0 then
  503. Index := 0;
  504. { Check Size. Accounts for Zero-length S, the double check is needed because
  505. Size can be maxint and will get <0 when adding index }
  506. if (Size>Length(S)) or
  507. (Index+Size>Length(S)) then
  508. Size:=Length(S)-Index;
  509. If Size>0 then
  510. begin
  511. { NewWideString won't return a nil pointer }
  512. ResultAddress:=NewWideString(Size);
  513. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  514. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  515. end;
  516. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  517. Pointer(fpc_widestr_Copy):=ResultAddress;
  518. end;
  519. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  520. var
  521. i,MaxLen : SizeInt;
  522. pc : pwidechar;
  523. begin
  524. Pos:=0;
  525. if Length(SubStr)>0 then
  526. begin
  527. MaxLen:=Length(source)-Length(SubStr);
  528. i:=0;
  529. pc:=@source[1];
  530. while (i<=MaxLen) do
  531. begin
  532. inc(i);
  533. if (SubStr[1]=pc^) and
  534. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  535. begin
  536. Pos:=i;
  537. exit;
  538. end;
  539. inc(pc);
  540. end;
  541. end;
  542. end;
  543. { Faster version for a widechar alone }
  544. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  545. var
  546. i: SizeInt;
  547. pc : pwidechar;
  548. begin
  549. pc:=@s[1];
  550. for i:=1 to length(s) do
  551. begin
  552. if pc^=c then
  553. begin
  554. pos:=i;
  555. exit;
  556. end;
  557. inc(pc);
  558. end;
  559. pos:=0;
  560. end;
  561. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  562. block, which is significant bloat without any sensible speed improvement. }
  563. Function Pos (c : WideChar; Const s : RawByteString) : SizeInt;
  564. begin
  565. result:=Pos(c,WideString(s));
  566. end;
  567. Function Pos (const c : RawByteString; Const s : WideString) : SizeInt;
  568. begin
  569. result:=Pos(WideString(c),s);
  570. end;
  571. Function Pos (const c : ShortString; Const s : WideString) : SizeInt;
  572. begin
  573. result:=Pos(WideString(c),s);
  574. end;
  575. Function Pos (const c : WideString; Const s : RawByteString) : SizeInt;
  576. begin
  577. result:=Pos(c,WideString(s));
  578. end;
  579. { Faster version for a char alone. Must be implemented because }
  580. { pos(c: char; const s: shortstring) also exists, so otherwise }
  581. { using pos(char,pchar) will always call the shortstring version }
  582. { (exact match for first argument), also with $h+ (JM) }
  583. Function Pos (c : Char; Const s : WideString) : SizeInt;
  584. var
  585. i: SizeInt;
  586. wc : widechar;
  587. pc : pwidechar;
  588. begin
  589. wc:=c;
  590. pc:=@s[1];
  591. for i:=1 to length(s) do
  592. begin
  593. if pc^=wc then
  594. begin
  595. pos:=i;
  596. exit;
  597. end;
  598. inc(pc);
  599. end;
  600. pos:=0;
  601. end;
  602. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  603. Var
  604. LS : SizeInt;
  605. begin
  606. LS:=Length(S);
  607. if (Index>LS) or (Index<=0) or (Size<=0) then
  608. exit;
  609. { (Size+Index) will overflow if Size=MaxInt. }
  610. if Size>LS-Index then
  611. Size:=LS-Index+1;
  612. if Size<=LS-Index then
  613. begin
  614. Dec(Index);
  615. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  616. end;
  617. Setlength(s,LS-Size);
  618. end;
  619. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  620. var
  621. Temp : WideString;
  622. LS : SizeInt;
  623. begin
  624. If Length(Source)=0 then
  625. exit;
  626. if index <= 0 then
  627. index := 1;
  628. Ls:=Length(S);
  629. if index > LS then
  630. index := LS+1;
  631. Dec(Index);
  632. SetLength(Temp,Length(Source)+LS);
  633. If Index>0 then
  634. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  635. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  636. If (LS-Index)>0 then
  637. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  638. S:=Temp;
  639. end;
  640. function UpCase(const s : WideString) : WideString;
  641. begin
  642. result:=widestringmanager.UpperWideStringProc(s);
  643. end;
  644. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  645. begin
  646. SetLength(S,Len);
  647. If (Buf<>Nil) and (Len>0) then
  648. Move (Buf[0],S[1],Len*sizeof(WideChar));
  649. end;
  650. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  651. begin
  652. If (Buf<>Nil) and (Len>0) then
  653. widestringmanager.Ansi2WideMoveProc(Buf,DefaultSystemCodePage,S,Len)
  654. else
  655. SetLength(s,len);
  656. end;
  657. {$ifndef FPUNONE}
  658. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  659. Var
  660. SS: ShortString;
  661. begin
  662. fpc_Val_Real_WideStr := 0;
  663. if length(S)>255 then
  664. code:=256
  665. else
  666. begin
  667. SS:=ShortString(S);
  668. Val(SS,fpc_Val_Real_WideStr,code);
  669. end;
  670. end;
  671. {$endif}
  672. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  673. var
  674. ss: ShortString;
  675. begin
  676. if length(s)>255 then
  677. code:=256
  678. else
  679. begin
  680. ss:=ShortString(s);
  681. val(ss,fpc_val_enum_widestr,code);
  682. end;
  683. end;
  684. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  685. Var
  686. SS: ShortString;
  687. begin
  688. if length(S)>255 then
  689. begin
  690. fpc_Val_Currency_WideStr:=0;
  691. code:=256;
  692. end
  693. else
  694. begin
  695. SS:=ShortString(S);
  696. Val(SS,fpc_Val_Currency_WideStr,code);
  697. end;
  698. end;
  699. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  700. Var
  701. SS: ShortString;
  702. begin
  703. fpc_Val_UInt_WideStr:=0;
  704. if length(S)>255 then
  705. code:=256
  706. else
  707. begin
  708. SS:=ShortString(S);
  709. Val(SS,fpc_Val_UInt_WideStr,code);
  710. end;
  711. end;
  712. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  713. Var
  714. SS: ShortString;
  715. begin
  716. fpc_Val_SInt_WideStr:=0;
  717. if length(S)>255 then
  718. code:=256
  719. else
  720. begin
  721. SS:=ShortString(S);
  722. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  723. end;
  724. end;
  725. {$ifndef CPU64}
  726. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  727. Var
  728. SS: ShortString;
  729. begin
  730. fpc_Val_qword_WideStr:=0;
  731. if length(S)>255 then
  732. code:=256
  733. else
  734. begin
  735. SS:=ShortString(S);
  736. Val(SS,fpc_Val_qword_WideStr,Code);
  737. end;
  738. end;
  739. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  740. Var
  741. SS: ShortString;
  742. begin
  743. fpc_Val_int64_WideStr:=0;
  744. if length(S)>255 then
  745. code:=256
  746. else
  747. begin
  748. SS:=ShortString(S);
  749. Val(SS,fpc_Val_int64_WideStr,Code);
  750. end;
  751. end;
  752. {$endif CPU64}
  753. {$ifndef FPUNONE}
  754. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  755. var
  756. ss: ShortString;
  757. begin
  758. str_real(len,fr,d,treal_type(rt),ss);
  759. s:=WideString(ss);
  760. end;
  761. {$endif}
  762. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  763. var
  764. ss: ShortString;
  765. begin
  766. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  767. s:=WideString(ss);
  768. end;
  769. procedure fpc_widestr_bool(b : boolean;len:sizeint;out s:widestring);compilerproc;
  770. var
  771. ss: ShortString;
  772. begin
  773. fpc_shortstr_bool(b,len,ss);
  774. s:=WideString(ss);
  775. end;
  776. {$ifdef FPC_HAS_STR_CURRENCY}
  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. {$endif FPC_HAS_STR_CURRENCY}
  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;
  831. const
  832. SNoWidestrings = 'This binary has no widestrings support compiled in.';
  833. SRecompileWithWidestrings = 'Recompile the application with a widestrings-manager in the program uses clause.';
  834. procedure unimplementedwidestring;
  835. begin
  836. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  837. If IsConsole then
  838. begin
  839. Writeln(StdErr,SNoWidestrings);
  840. Writeln(StdErr,SRecompileWithWidestrings);
  841. end;
  842. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  843. HandleErrorAddrFrameInd(233,get_pc_addr,get_frame);
  844. end;
  845. {$warnings off}
  846. function GenericWideCase(const s : WideString) : WideString;
  847. begin
  848. unimplementedwidestring;
  849. end;
  850. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  851. begin
  852. unimplementedwidestring;
  853. end;
  854. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  855. begin
  856. unimplementedwidestring;
  857. end;
  858. {$warnings on}
  859. function DefaultCharLengthPChar(const Str: PChar): PtrInt;forward;
  860. function DefaultCodePointLength(const Str: PChar; MaxLookAead: PtrInt): Ptrint;forward;
  861. procedure initwidestringmanager;
  862. begin
  863. fillchar(widestringmanager,sizeof(widestringmanager),0);
  864. {$ifndef HAS_WIDESTRINGMANAGER}
  865. widestringmanager.Wide2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  866. widestringmanager.Ansi2WideMoveProc:=@DefaultAnsi2WideMove;
  867. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  868. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  869. {$endif HAS_WIDESTRINGMANAGER}
  870. widestringmanager.CompareWideStringProc:=@CompareWideString;
  871. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  872. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  873. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  874. end;