wstrings.inc 25 KB

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