wstrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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) : SizeInt;
  517. var
  518. i,MaxLen : SizeInt;
  519. pc : pwidechar;
  520. begin
  521. Pos:=0;
  522. if Length(SubStr)>0 then
  523. begin
  524. MaxLen:=Length(source)-Length(SubStr);
  525. i:=0;
  526. pc:=@source[1];
  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) : SizeInt;
  542. var
  543. i: SizeInt;
  544. pc : pwidechar;
  545. begin
  546. pc:=@s[1];
  547. for i:=1 to length(s) do
  548. begin
  549. if pc^=c then
  550. begin
  551. pos:=i;
  552. exit;
  553. end;
  554. inc(pc);
  555. end;
  556. pos:=0;
  557. end;
  558. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  559. block, which is significant bloat without any sensible speed improvement. }
  560. Function Pos (c : WideChar; Const s : RawByteString) : SizeInt;
  561. begin
  562. result:=Pos(c,WideString(s));
  563. end;
  564. Function Pos (const c : RawByteString; Const s : WideString) : SizeInt;
  565. begin
  566. result:=Pos(WideString(c),s);
  567. end;
  568. Function Pos (const c : ShortString; Const s : WideString) : SizeInt;
  569. begin
  570. result:=Pos(WideString(c),s);
  571. end;
  572. Function Pos (const c : WideString; Const s : RawByteString) : SizeInt;
  573. begin
  574. result:=Pos(c,WideString(s));
  575. end;
  576. { Faster version for a char alone. Must be implemented because }
  577. { pos(c: char; const s: shortstring) also exists, so otherwise }
  578. { using pos(char,pchar) will always call the shortstring version }
  579. { (exact match for first argument), also with $h+ (JM) }
  580. Function Pos (c : Char; Const s : WideString) : SizeInt;
  581. var
  582. i: SizeInt;
  583. wc : widechar;
  584. pc : pwidechar;
  585. begin
  586. wc:=c;
  587. pc:=@s[1];
  588. for i:=1 to length(s) do
  589. begin
  590. if pc^=wc then
  591. begin
  592. pos:=i;
  593. exit;
  594. end;
  595. inc(pc);
  596. end;
  597. pos:=0;
  598. end;
  599. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  600. Var
  601. LS : SizeInt;
  602. begin
  603. LS:=Length(S);
  604. if (Index>LS) or (Index<=0) or (Size<=0) then
  605. exit;
  606. { (Size+Index) will overflow if Size=MaxInt. }
  607. if Size>LS-Index then
  608. Size:=LS-Index+1;
  609. if Size<=LS-Index then
  610. begin
  611. Dec(Index);
  612. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  613. end;
  614. Setlength(s,LS-Size);
  615. end;
  616. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  617. var
  618. Temp : WideString;
  619. LS : SizeInt;
  620. begin
  621. If Length(Source)=0 then
  622. exit;
  623. if index <= 0 then
  624. index := 1;
  625. Ls:=Length(S);
  626. if index > LS then
  627. index := LS+1;
  628. Dec(Index);
  629. SetLength(Temp,Length(Source)+LS);
  630. If Index>0 then
  631. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  632. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  633. If (LS-Index)>0 then
  634. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  635. S:=Temp;
  636. end;
  637. function UpCase(const s : WideString) : WideString;
  638. begin
  639. result:=widestringmanager.UpperWideStringProc(s);
  640. end;
  641. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  642. begin
  643. SetLength(S,Len);
  644. If (Buf<>Nil) and (Len>0) then
  645. Move (Buf[0],S[1],Len*sizeof(WideChar));
  646. end;
  647. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  648. begin
  649. If (Buf<>Nil) and (Len>0) then
  650. widestringmanager.Ansi2WideMoveProc(Buf,DefaultSystemCodePage,S,Len)
  651. else
  652. SetLength(s,len);
  653. end;
  654. {$ifndef FPUNONE}
  655. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  656. Var
  657. SS: ShortString;
  658. begin
  659. fpc_Val_Real_WideStr := 0;
  660. if length(S)>255 then
  661. code:=256
  662. else
  663. begin
  664. SS:=ShortString(S);
  665. Val(SS,fpc_Val_Real_WideStr,code);
  666. end;
  667. end;
  668. {$endif}
  669. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  670. var
  671. ss: ShortString;
  672. begin
  673. if length(s)>255 then
  674. code:=256
  675. else
  676. begin
  677. ss:=ShortString(s);
  678. val(ss,fpc_val_enum_widestr,code);
  679. end;
  680. end;
  681. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  682. Var
  683. SS: ShortString;
  684. begin
  685. if length(S)>255 then
  686. begin
  687. fpc_Val_Currency_WideStr:=0;
  688. code:=256;
  689. end
  690. else
  691. begin
  692. SS:=ShortString(S);
  693. Val(SS,fpc_Val_Currency_WideStr,code);
  694. end;
  695. end;
  696. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  697. Var
  698. SS: ShortString;
  699. begin
  700. fpc_Val_UInt_WideStr:=0;
  701. if length(S)>255 then
  702. code:=256
  703. else
  704. begin
  705. SS:=ShortString(S);
  706. Val(SS,fpc_Val_UInt_WideStr,code);
  707. end;
  708. end;
  709. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  710. Var
  711. SS: ShortString;
  712. begin
  713. fpc_Val_SInt_WideStr:=0;
  714. if length(S)>255 then
  715. code:=256
  716. else
  717. begin
  718. SS:=ShortString(S);
  719. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  720. end;
  721. end;
  722. {$ifndef CPU64}
  723. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  724. Var
  725. SS: ShortString;
  726. begin
  727. fpc_Val_qword_WideStr:=0;
  728. if length(S)>255 then
  729. code:=256
  730. else
  731. begin
  732. SS:=ShortString(S);
  733. Val(SS,fpc_Val_qword_WideStr,Code);
  734. end;
  735. end;
  736. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  737. Var
  738. SS: ShortString;
  739. begin
  740. fpc_Val_int64_WideStr:=0;
  741. if length(S)>255 then
  742. code:=256
  743. else
  744. begin
  745. SS:=ShortString(S);
  746. Val(SS,fpc_Val_int64_WideStr,Code);
  747. end;
  748. end;
  749. {$endif CPU64}
  750. {$ifndef FPUNONE}
  751. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  752. var
  753. ss: ShortString;
  754. begin
  755. str_real(len,fr,d,treal_type(rt),ss);
  756. s:=WideString(ss);
  757. end;
  758. {$endif}
  759. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  760. var
  761. ss: ShortString;
  762. begin
  763. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  764. s:=WideString(ss);
  765. end;
  766. procedure fpc_widestr_bool(b : boolean;len:sizeint;out s:widestring);compilerproc;
  767. var
  768. ss: ShortString;
  769. begin
  770. fpc_shortstr_bool(b,len,ss);
  771. s:=WideString(ss);
  772. end;
  773. {$ifdef FPC_HAS_STR_CURRENCY}
  774. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  775. var
  776. ss: ShortString;
  777. begin
  778. str(c:len:fr,ss);
  779. s:=WideString(ss);
  780. end;
  781. {$endif FPC_HAS_STR_CURRENCY}
  782. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  783. Var
  784. SS: ShortString;
  785. begin
  786. Str (v:Len,SS);
  787. S:=WideString(SS);
  788. end;
  789. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  790. Var
  791. SS: ShortString;
  792. begin
  793. str(v:Len,SS);
  794. S:=WideString(SS);
  795. end;
  796. {$ifndef CPU64}
  797. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  798. Var
  799. SS: ShortString;
  800. begin
  801. Str(v:Len,SS);
  802. S:=WideString(SS);
  803. end;
  804. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  805. Var
  806. SS: ShortString;
  807. begin
  808. str(v:Len,SS);
  809. S:=WideString(SS);
  810. end;
  811. {$endif CPU64}
  812. function UTF8Encode(const s : WideString) : RawByteString;
  813. var
  814. i : SizeInt;
  815. hs : UTF8String;
  816. begin
  817. result:='';
  818. if s='' then
  819. exit;
  820. SetLength(hs,length(s)*3);
  821. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  822. if i>0 then
  823. begin
  824. SetLength(hs,i-1);
  825. result:=hs;
  826. end;
  827. end;
  828. const
  829. SNoWidestrings = 'This binary has no widestrings support compiled in.';
  830. SRecompileWithWidestrings = 'Recompile the application with a widestrings-manager in the program uses clause.';
  831. procedure unimplementedwidestring;
  832. begin
  833. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  834. If IsConsole then
  835. begin
  836. Writeln(StdErr,SNoWidestrings);
  837. Writeln(StdErr,SRecompileWithWidestrings);
  838. end;
  839. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  840. HandleErrorAddrFrameInd(233,get_pc_addr,get_frame);
  841. end;
  842. {$warnings off}
  843. function GenericWideCase(const s : WideString) : WideString;
  844. begin
  845. unimplementedwidestring;
  846. end;
  847. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  848. begin
  849. unimplementedwidestring;
  850. end;
  851. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  852. begin
  853. unimplementedwidestring;
  854. end;
  855. {$warnings on}
  856. function DefaultCharLengthPChar(const Str: PChar): PtrInt;forward;
  857. function DefaultCodePointLength(const Str: PChar; MaxLookAead: PtrInt): Ptrint;forward;
  858. procedure initwidestringmanager;
  859. begin
  860. fillchar(widestringmanager,sizeof(widestringmanager),0);
  861. {$ifndef HAS_WIDESTRINGMANAGER}
  862. widestringmanager.Wide2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  863. widestringmanager.Ansi2WideMoveProc:=@DefaultAnsi2WideMove;
  864. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  865. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  866. {$endif HAS_WIDESTRINGMANAGER}
  867. widestringmanager.CompareWideStringProc:=@CompareWideString;
  868. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  869. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  870. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  871. end;