2
0

wustrings.inc 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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/Unicode 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. {$ifdef FPC_WINLIKEWIDESTRING}
  31. Len : DWord;
  32. {$else FPC_WINLIKEWIDESTRING}
  33. Ref : SizeInt;
  34. Len : SizeInt;
  35. {$endif FPC_WINLIKEWIDESTRING}
  36. First : WideChar;
  37. end;
  38. Const
  39. WideRecLen = SizeOf(TWideRec);
  40. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  41. {
  42. Default WideChar <-> Char conversion is to only convert the
  43. lower 127 chars, all others are translated to spaces.
  44. These routines can be overwritten for the Current Locale
  45. }
  46. procedure DefaultWide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  47. var
  48. i : SizeInt;
  49. begin
  50. setlength(dest,len);
  51. for i:=1 to len do
  52. begin
  53. if word(source^)<256 then
  54. dest[i]:=char(word(source^))
  55. else
  56. dest[i]:='?';
  57. inc(source);
  58. end;
  59. end;
  60. procedure DefaultAnsi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  61. var
  62. i : SizeInt;
  63. begin
  64. setlength(dest,len);
  65. for i:=1 to len do
  66. begin
  67. dest[i]:=widechar(byte(source^));
  68. inc(source);
  69. end;
  70. end;
  71. Procedure GetWideStringManager (Var Manager : TWideStringManager);
  72. begin
  73. manager:=widestringmanager;
  74. end;
  75. Procedure SetWideStringManager (Const New : TWideStringManager; Var Old: TWideStringManager);
  76. begin
  77. Old:=widestringmanager;
  78. widestringmanager:=New;
  79. end;
  80. Procedure SetWideStringManager (Const New : TWideStringManager);
  81. begin
  82. widestringmanager:=New;
  83. end;
  84. (*
  85. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  86. {
  87. Make sure reference count of S is 1,
  88. using copy-on-write semantics.
  89. }
  90. begin
  91. end;
  92. *)
  93. {****************************************************************************
  94. Internal functions, not in interface.
  95. ****************************************************************************}
  96. procedure WideStringError;
  97. begin
  98. HandleErrorFrame(204,get_frame);
  99. end;
  100. {$ifdef WideStrDebug}
  101. Procedure DumpWideRec(S : Pointer);
  102. begin
  103. If S=Nil then
  104. Writeln ('String is nil')
  105. Else
  106. Begin
  107. With PWideRec(S-WideFirstOff)^ do
  108. begin
  109. Write ('(Len:',len);
  110. Writeln (' Ref: ',ref,')');
  111. end;
  112. end;
  113. end;
  114. {$endif}
  115. Function NewWideString(Len : SizeInt) : Pointer;
  116. {
  117. Allocate a new WideString on the heap.
  118. initialize it to zero length and reference count 1.
  119. }
  120. Var
  121. P : Pointer;
  122. begin
  123. {$ifdef MSWINDOWS}
  124. if winwidestringalloc then
  125. begin
  126. P:=SysAllocStringLen(nil,Len);
  127. if P=nil then
  128. WideStringError;
  129. end
  130. else
  131. {$endif MSWINDOWS}
  132. begin
  133. GetMem(P,Len*sizeof(WideChar)+WideRecLen);
  134. If P<>Nil then
  135. begin
  136. PWideRec(P)^.Len:=Len*2; { Initial length }
  137. {$ifndef FPC_WINLIKEWIDESTRING}
  138. PWideRec(P)^.Ref:=1; { Initial Refcount }
  139. {$endif FPC_WINLIKEWIDESTRING}
  140. PWideRec(P)^.First:=#0; { Terminating #0 }
  141. inc(p,WideFirstOff); { Points to string now }
  142. end
  143. else
  144. WideStringError;
  145. end;
  146. NewWideString:=P;
  147. end;
  148. Procedure DisposeWideString(Var S : Pointer);
  149. {
  150. Deallocates a WideString From the heap.
  151. }
  152. begin
  153. If S=Nil then
  154. exit;
  155. {$ifndef MSWINDOWS}
  156. Dec (S,WideFirstOff);
  157. Freemem(S);
  158. {$else MSWINDOWS}
  159. if winwidestringalloc then
  160. SysFreeString(S)
  161. else
  162. begin
  163. Dec (S,WideFirstOff);
  164. Freemem(S);
  165. end;
  166. {$endif MSWINDOWS}
  167. S:=Nil;
  168. end;
  169. {$ifdef FPC_WINLIKEWIDESTRING}
  170. var
  171. __data_start: byte; external name '__data_start__';
  172. __data_end: byte; external name '__data_end__';
  173. function IsWideStringConstant(S: pointer): boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  174. {
  175. Returns True if widestring is constant (located in .data section);
  176. }
  177. begin
  178. Result:=(S>=@__data_start) and (S<@__data_end);
  179. end;
  180. {$endif FPC_WINLIKEWIDESTRING}
  181. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_DECR_REF']; compilerproc;
  182. {
  183. Decreases the ReferenceCount of a non constant widestring;
  184. If the reference count is zero, deallocate the string;
  185. }
  186. Type
  187. pSizeInt = ^SizeInt;
  188. {$ifndef FPC_WINLIKEWIDESTRING}
  189. Var
  190. l : pSizeInt;
  191. {$endif FPC_WINLIKEWIDESTRING}
  192. Begin
  193. { Zero string }
  194. if S=Nil then
  195. exit;
  196. {$ifndef FPC_WINLIKEWIDESTRING}
  197. { check for constant strings ...}
  198. l:=@PWideRec(S-WideFirstOff)^.Ref;
  199. if l^<0 then
  200. exit;
  201. { declocked does a MT safe dec and returns true, if the counter is 0 }
  202. if declocked(l^) then
  203. { Ref count dropped to zero ...
  204. ... remove }
  205. {$else}
  206. if not IsWideStringConstant(S) then
  207. {$endif FPC_WINLIKEWIDESTRING}
  208. DisposeWideString(S);
  209. end;
  210. { alias for internal use }
  211. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_DECR_REF'];
  212. Procedure fpc_WideStr_Incr_Ref(Var S : Pointer);[Public,Alias:'FPC_WIDESTR_INCR_REF']; compilerproc;
  213. {$ifdef FPC_WINLIKEWIDESTRING}
  214. var
  215. p : pointer;
  216. {$endif FPC_WINLIKEWIDESTRING}
  217. Begin
  218. If S=Nil then
  219. exit;
  220. {$ifdef FPC_WINLIKEWIDESTRING}
  221. p:=NewWidestring(length(WideString(S)));
  222. move(s^,p^,(length(WideString(s))+1)*sizeof(widechar)); // double #0 too
  223. s:=p;
  224. {$else FPC_WINLIKEWIDESTRING}
  225. { Let's be paranoid : Constant string ??}
  226. If PWideRec(S-WideFirstOff)^.Ref<0 then
  227. exit;
  228. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  229. {$endif FPC_WINLIKEWIDESTRING}
  230. end;
  231. { alias for internal use }
  232. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_INCR_REF'];
  233. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  234. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; compilerproc;
  235. {
  236. Converts a WideString to a ShortString;
  237. }
  238. Var
  239. Size : SizeInt;
  240. temp : ansistring;
  241. begin
  242. result:='';
  243. Size:=Length(S2);
  244. if Size>0 then
  245. begin
  246. If Size>high_of_res then
  247. Size:=high_of_res;
  248. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  249. result:=temp;
  250. end;
  251. end;
  252. {$else FPC_STRTOSHORTSTRINGPROC}
  253. procedure fpc_WideStr_To_ShortStr (out res: ShortString;const S2 : WideString); [Public, alias: 'FPC_WIDESTR_TO_SHORTSTR'];compilerproc;
  254. {
  255. Converts a WideString to a ShortString;
  256. }
  257. Var
  258. Size : SizeInt;
  259. temp : ansistring;
  260. begin
  261. res:='';
  262. Size:=Length(S2);
  263. if Size>0 then
  264. begin
  265. If Size>high(res) then
  266. Size:=high(res);
  267. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  268. res:=temp;
  269. end;
  270. end;
  271. {$endif FPC_STRTOSHORTSTRINGPROC}
  272. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  273. {
  274. Converts a ShortString to a WideString;
  275. }
  276. Var
  277. Size : SizeInt;
  278. begin
  279. result:='';
  280. Size:=Length(S2);
  281. if Size>0 then
  282. begin
  283. widestringmanager.Ansi2WideMoveProc(PChar(@S2[1]),result,Size);
  284. { Terminating Zero }
  285. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  286. end;
  287. end;
  288. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; compilerproc;
  289. {
  290. Converts a WideString to an AnsiString
  291. }
  292. Var
  293. Size : SizeInt;
  294. begin
  295. result:='';
  296. Size:=Length(S2);
  297. if Size>0 then
  298. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,Size);
  299. end;
  300. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; compilerproc;
  301. {
  302. Converts an AnsiString to a WideString;
  303. }
  304. Var
  305. Size : SizeInt;
  306. begin
  307. result:='';
  308. Size:=Length(S2);
  309. if Size>0 then
  310. widestringmanager.Ansi2WideMoveProc(PChar(S2),result,Size);
  311. end;
  312. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  313. var
  314. Size : SizeInt;
  315. begin
  316. result:='';
  317. if p=nil then
  318. exit;
  319. Size := IndexWord(p^, -1, 0);
  320. if Size>0 then
  321. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  322. end;
  323. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  324. var
  325. Size : SizeInt;
  326. begin
  327. result:='';
  328. if p=nil then
  329. exit;
  330. Size := IndexWord(p^, -1, 0);
  331. Setlength(result,Size);
  332. if Size>0 then
  333. begin
  334. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  335. { Terminating Zero }
  336. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  337. end;
  338. end;
  339. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  340. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  341. var
  342. Size : SizeInt;
  343. temp: ansistring;
  344. begin
  345. result:='';
  346. if p=nil then
  347. exit;
  348. Size := IndexWord(p^, $7fffffff, 0);
  349. if Size>0 then
  350. begin
  351. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  352. result:=temp;
  353. end;
  354. end;
  355. {$else FPC_STRTOSHORTSTRINGPROC}
  356. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  357. var
  358. Size : SizeInt;
  359. temp: ansistring;
  360. begin
  361. res:='';
  362. if p=nil then
  363. exit;
  364. Size:=IndexWord(p^, high(PtrInt), 0);
  365. if Size>0 then
  366. begin
  367. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  368. res:=temp;
  369. end;
  370. end;
  371. {$endif FPC_STRTOSHORTSTRINGPROC}
  372. { checked against the ansistring routine, 2001-05-27 (FK) }
  373. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  374. {
  375. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  376. }
  377. begin
  378. {$ifdef FPC_WINLIKEWIDESTRING}
  379. if S1=S2 then exit;
  380. if S2<>nil then
  381. begin
  382. if IsWideStringConstant(S1) then
  383. begin
  384. S1:=NewWidestring(length(WideString(S2)));
  385. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  386. end
  387. else
  388. {$ifdef MSWINDOWS}
  389. if winwidestringalloc then
  390. begin
  391. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  392. WideStringError;
  393. end
  394. else
  395. {$endif MSWINDOWS}
  396. begin
  397. SetLength(WideString(S1),length(WideString(S2)));
  398. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  399. end;
  400. end
  401. else
  402. begin
  403. { Free S1 }
  404. fpc_widestr_decr_ref (S1);
  405. S1:=nil;
  406. end;
  407. {$else FPC_WINLIKEWIDESTRING}
  408. If S2<>nil then
  409. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  410. inclocked(PWideRec(S2-WideFirstOff)^.ref);
  411. { Decrease the reference count on the old S1 }
  412. fpc_widestr_decr_ref (S1);
  413. s1:=s2;
  414. {$endif FPC_WINLIKEWIDESTRING}
  415. end;
  416. { alias for internal use }
  417. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  418. {$ifndef STR_CONCAT_PROCS}
  419. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  420. Var
  421. Size,Location : SizeInt;
  422. pc : pwidechar;
  423. begin
  424. { only assign if s1 or s2 is empty }
  425. if (S1='') then
  426. begin
  427. result:=s2;
  428. exit;
  429. end;
  430. if (S2='') then
  431. begin
  432. result:=s1;
  433. exit;
  434. end;
  435. Location:=Length(S1);
  436. Size:=length(S2);
  437. SetLength(result,Size+Location);
  438. pc:=pwidechar(result);
  439. Move(S1[1],pc^,Location*sizeof(WideChar));
  440. inc(pc,location);
  441. Move(S2[1],pc^,(Size+1)*sizeof(WideChar));
  442. end;
  443. function fpc_WideStr_Concat_multi (const sarr:array of Widestring): widestring; compilerproc;
  444. Var
  445. i : Longint;
  446. p : pointer;
  447. pc : pwidechar;
  448. Size,NewSize : SizeInt;
  449. begin
  450. { First calculate size of the result so we can do
  451. a single call to SetLength() }
  452. NewSize:=0;
  453. for i:=low(sarr) to high(sarr) do
  454. inc(Newsize,length(sarr[i]));
  455. SetLength(result,NewSize);
  456. pc:=pwidechar(result);
  457. for i:=low(sarr) to high(sarr) do
  458. begin
  459. p:=pointer(sarr[i]);
  460. if assigned(p) then
  461. begin
  462. Size:=length(widestring(p));
  463. Move(pwidechar(p)^,pc^,(Size+1)*sizeof(WideChar));
  464. inc(pc,size);
  465. end;
  466. end;
  467. end;
  468. {$else STR_CONCAT_PROCS}
  469. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  470. Var
  471. Size,Location : SizeInt;
  472. same : boolean;
  473. begin
  474. { only assign if s1 or s2 is empty }
  475. if (S1='') then
  476. begin
  477. DestS:=s2;
  478. exit;
  479. end;
  480. if (S2='') then
  481. begin
  482. DestS:=s1;
  483. exit;
  484. end;
  485. Location:=Length(S1);
  486. Size:=length(S2);
  487. { Use Pointer() typecasts to prevent extra conversion code }
  488. if Pointer(DestS)=Pointer(S1) then
  489. begin
  490. same:=Pointer(S1)=Pointer(S2);
  491. SetLength(DestS,Size+Location);
  492. if same then
  493. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  494. else
  495. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  496. end
  497. else if Pointer(DestS)=Pointer(S2) then
  498. begin
  499. SetLength(DestS,Size+Location);
  500. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  501. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  502. end
  503. else
  504. begin
  505. DestS:='';
  506. SetLength(DestS,Size+Location);
  507. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  508. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  509. end;
  510. end;
  511. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  512. Var
  513. lowstart,i : Longint;
  514. p,pc : pointer;
  515. Size,NewLen,
  516. OldDestLen : SizeInt;
  517. destcopy : widestring;
  518. begin
  519. if high(sarr)=0 then
  520. begin
  521. DestS:='';
  522. exit;
  523. end;
  524. lowstart:=low(sarr);
  525. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  526. inc(lowstart);
  527. { Check for another reuse, then we can't use
  528. the append optimization }
  529. for i:=lowstart to high(sarr) do
  530. begin
  531. if Pointer(DestS)=Pointer(sarr[i]) then
  532. begin
  533. { if DestS is used somewhere in the middle of the expression,
  534. we need to make sure the original string still exists after
  535. we empty/modify DestS }
  536. destcopy:=dests;
  537. lowstart:=low(sarr);
  538. break;
  539. end;
  540. end;
  541. { Start with empty DestS if we start with concatting
  542. the first array element }
  543. if lowstart=low(sarr) then
  544. DestS:='';
  545. OldDestLen:=length(DestS);
  546. { Calculate size of the result so we can do
  547. a single call to SetLength() }
  548. NewLen:=0;
  549. for i:=low(sarr) to high(sarr) do
  550. inc(NewLen,length(sarr[i]));
  551. SetLength(DestS,NewLen);
  552. { Concat all strings, except the string we already
  553. copied in DestS }
  554. pc:=Pointer(DestS)+OldDestLen*sizeof(WideChar);
  555. for i:=lowstart to high(sarr) do
  556. begin
  557. p:=pointer(sarr[i]);
  558. if assigned(p) then
  559. begin
  560. Size:=length(widestring(p));
  561. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  562. inc(pc,size*sizeof(WideChar));
  563. end;
  564. end;
  565. end;
  566. {$endif STR_CONCAT_PROCS}
  567. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  568. var
  569. w: widestring;
  570. begin
  571. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  572. fpc_Char_To_WChar:= w[1];
  573. end;
  574. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  575. {
  576. Converts a Char to a WideString;
  577. }
  578. begin
  579. Setlength(fpc_Char_To_WideStr,1);
  580. fpc_Char_To_WideStr[1]:=c;
  581. { Terminating Zero }
  582. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  583. end;
  584. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  585. {
  586. Converts a WideChar to a Char;
  587. }
  588. var
  589. s: ansistring;
  590. begin
  591. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  592. if length(s)=1 then
  593. fpc_WChar_To_Char:= s[1]
  594. else
  595. fpc_WChar_To_Char:='?';
  596. end;
  597. Function fpc_WChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  598. {
  599. Converts a WideChar to a WideString;
  600. }
  601. begin
  602. Setlength (fpc_WChar_To_WideStr,1);
  603. fpc_WChar_To_WideStr[1]:= c;
  604. end;
  605. Function fpc_WChar_To_AnsiStr(const c : WideChar): AnsiString; compilerproc;
  606. {
  607. Converts a WideChar to a AnsiString;
  608. }
  609. begin
  610. widestringmanager.Wide2AnsiMoveProc(@c, fpc_WChar_To_AnsiStr, 1);
  611. end;
  612. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  613. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  614. {
  615. Converts a WideChar to a ShortString;
  616. }
  617. var
  618. s: ansistring;
  619. begin
  620. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  621. fpc_WChar_To_ShortStr:= s;
  622. end;
  623. {$else FPC_STRTOSHORTSTRINGPROC}
  624. procedure fpc_WChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  625. {
  626. Converts a WideChar to a ShortString;
  627. }
  628. var
  629. s: ansistring;
  630. begin
  631. widestringmanager.Wide2AnsiMoveProc(@c,s,1);
  632. res:=s;
  633. end;
  634. {$endif FPC_STRTOSHORTSTRINGPROC}
  635. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  636. Var
  637. L : SizeInt;
  638. begin
  639. if (not assigned(p)) or (p[0]=#0) Then
  640. begin
  641. fpc_pchar_to_widestr := '';
  642. exit;
  643. end;
  644. l:=IndexChar(p^,-1,#0);
  645. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  646. end;
  647. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  648. var
  649. i : SizeInt;
  650. begin
  651. if (zerobased) then
  652. begin
  653. if (arr[0]=#0) Then
  654. begin
  655. fpc_chararray_to_widestr := '';
  656. exit;
  657. end;
  658. i:=IndexChar(arr,high(arr)+1,#0);
  659. if i = -1 then
  660. i := high(arr)+1;
  661. end
  662. else
  663. i := high(arr)+1;
  664. SetLength(fpc_CharArray_To_WideStr,i);
  665. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  666. end;
  667. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  668. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  669. var
  670. l: longint;
  671. index: longint;
  672. len: byte;
  673. temp: ansistring;
  674. begin
  675. l := high(arr)+1;
  676. if l>=256 then
  677. l:=255
  678. else if l<0 then
  679. l:=0;
  680. if zerobased then
  681. begin
  682. index:=IndexWord(arr[0],l,0);
  683. if (index < 0) then
  684. len := l
  685. else
  686. len := index;
  687. end
  688. else
  689. len := l;
  690. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  691. fpc_WideCharArray_To_ShortStr := temp;
  692. end;
  693. {$else FPC_STRTOSHORTSTRINGPROC}
  694. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  695. var
  696. l: longint;
  697. index: ptrint;
  698. len: byte;
  699. temp: ansistring;
  700. begin
  701. l := high(arr)+1;
  702. if l>=high(res)+1 then
  703. l:=high(res)
  704. else if l<0 then
  705. l:=0;
  706. if zerobased then
  707. begin
  708. index:=IndexWord(arr[0],l,0);
  709. if index<0 then
  710. len:=l
  711. else
  712. len:=index;
  713. end
  714. else
  715. len:=l;
  716. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  717. res:=temp;
  718. end;
  719. {$endif FPC_STRTOSHORTSTRINGPROC}
  720. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  721. var
  722. i : SizeInt;
  723. begin
  724. if (zerobased) then
  725. begin
  726. i:=IndexWord(arr,high(arr)+1,0);
  727. if i = -1 then
  728. i := high(arr)+1;
  729. end
  730. else
  731. i := high(arr)+1;
  732. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  733. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  734. end;
  735. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  736. var
  737. i : SizeInt;
  738. begin
  739. if (zerobased) then
  740. begin
  741. i:=IndexWord(arr,high(arr)+1,0);
  742. if i = -1 then
  743. i := high(arr)+1;
  744. end
  745. else
  746. i := high(arr)+1;
  747. SetLength(fpc_WideCharArray_To_WideStr,i);
  748. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  749. end;
  750. {$ifndef FPC_STRTOCHARARRAYPROC}
  751. { inside the compiler, the resulttype is modified to that of the actual }
  752. { chararray we're converting to (JM) }
  753. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  754. var
  755. len: SizeInt;
  756. temp: ansistring;
  757. begin
  758. len := length(src);
  759. { make sure we don't dereference src if it can be nil (JM) }
  760. if len > 0 then
  761. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  762. len := length(temp);
  763. if len > arraysize then
  764. len := arraysize;
  765. {$r-}
  766. move(temp[1],fpc_widestr_to_chararray[0],len);
  767. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  768. {$ifdef RangeCheckWasOn}
  769. {$r+}
  770. {$endif}
  771. end;
  772. { inside the compiler, the resulttype is modified to that of the actual }
  773. { widechararray we're converting to (JM) }
  774. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  775. var
  776. len: SizeInt;
  777. begin
  778. len := length(src);
  779. if len > arraysize then
  780. len := arraysize;
  781. {$r-}
  782. { make sure we don't try to access element 1 of the ansistring if it's nil }
  783. if len > 0 then
  784. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  785. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  786. {$ifdef RangeCheckWasOn}
  787. {$r+}
  788. {$endif}
  789. end;
  790. { inside the compiler, the resulttype is modified to that of the actual }
  791. { chararray we're converting to (JM) }
  792. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  793. var
  794. len: SizeInt;
  795. temp: widestring;
  796. begin
  797. len := length(src);
  798. { make sure we don't dereference src if it can be nil (JM) }
  799. if len > 0 then
  800. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  801. len := length(temp);
  802. if len > arraysize then
  803. len := arraysize;
  804. {$r-}
  805. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  806. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  807. {$ifdef RangeCheckWasOn}
  808. {$r+}
  809. {$endif}
  810. end;
  811. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  812. var
  813. len: longint;
  814. temp : widestring;
  815. begin
  816. len := length(src);
  817. { make sure we don't access char 1 if length is 0 (JM) }
  818. if len > 0 then
  819. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  820. len := length(temp);
  821. if len > arraysize then
  822. len := arraysize;
  823. {$r-}
  824. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  825. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  826. {$ifdef RangeCheckWasOn}
  827. {$r+}
  828. {$endif}
  829. end;
  830. {$else ndef FPC_STRTOCHARARRAYPROC}
  831. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  832. var
  833. len: SizeInt;
  834. temp: ansistring;
  835. begin
  836. len := length(src);
  837. { make sure we don't dereference src if it can be nil (JM) }
  838. if len > 0 then
  839. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  840. len := length(temp);
  841. if len > length(res) then
  842. len := length(res);
  843. {$r-}
  844. move(temp[1],res[0],len);
  845. fillchar(res[len],length(res)-len,0);
  846. {$ifdef RangeCheckWasOn}
  847. {$r+}
  848. {$endif}
  849. end;
  850. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  851. var
  852. len: SizeInt;
  853. begin
  854. len := length(src);
  855. if len > length(res) then
  856. len := length(res);
  857. {$r-}
  858. { make sure we don't try to access element 1 of the ansistring if it's nil }
  859. if len > 0 then
  860. move(src[1],res[0],len*SizeOf(WideChar));
  861. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  862. {$ifdef RangeCheckWasOn}
  863. {$r+}
  864. {$endif}
  865. end;
  866. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  867. var
  868. len: SizeInt;
  869. temp: widestring;
  870. begin
  871. len := length(src);
  872. { make sure we don't dereference src if it can be nil (JM) }
  873. if len > 0 then
  874. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  875. len := length(temp);
  876. if len > length(res) then
  877. len := length(res);
  878. {$r-}
  879. move(temp[1],res[0],len*sizeof(widechar));
  880. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  881. {$ifdef RangeCheckWasOn}
  882. {$r+}
  883. {$endif}
  884. end;
  885. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  886. var
  887. len: longint;
  888. temp : widestring;
  889. begin
  890. len := length(src);
  891. { make sure we don't access char 1 if length is 0 (JM) }
  892. if len > 0 then
  893. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  894. len := length(temp);
  895. if len > length(res) then
  896. len := length(res);
  897. {$r-}
  898. move(temp[1],res[0],len*sizeof(widechar));
  899. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  900. {$ifdef RangeCheckWasOn}
  901. {$r+}
  902. {$endif}
  903. end;
  904. {$endif ndef FPC_STRTOCHARARRAYPROC}
  905. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  906. {
  907. Compares 2 WideStrings;
  908. The result is
  909. <0 if S1<S2
  910. 0 if S1=S2
  911. >0 if S1>S2
  912. }
  913. Var
  914. MaxI,Temp : SizeInt;
  915. begin
  916. if pointer(S1)=pointer(S2) then
  917. begin
  918. fpc_WideStr_Compare:=0;
  919. exit;
  920. end;
  921. Maxi:=Length(S1);
  922. temp:=Length(S2);
  923. If MaxI>Temp then
  924. MaxI:=Temp;
  925. Temp:=CompareWord(S1[1],S2[1],MaxI);
  926. if temp=0 then
  927. temp:=Length(S1)-Length(S2);
  928. fpc_WideStr_Compare:=Temp;
  929. end;
  930. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  931. {
  932. Compares 2 WideStrings for equality only;
  933. The result is
  934. 0 if S1=S2
  935. <>0 if S1<>S2
  936. }
  937. Var
  938. MaxI : SizeInt;
  939. begin
  940. if pointer(S1)=pointer(S2) then
  941. exit(0);
  942. Maxi:=Length(S1);
  943. If MaxI<>Length(S2) then
  944. exit(-1)
  945. else
  946. exit(CompareWord(S1[1],S2[1],MaxI));
  947. end;
  948. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  949. begin
  950. if p=nil then
  951. HandleErrorFrame(201,get_frame);
  952. end;
  953. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  954. begin
  955. if (index>len div 2) or (Index<1) then
  956. HandleErrorFrame(201,get_frame);
  957. end;
  958. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  959. {
  960. Sets The length of string S to L.
  961. Makes sure S is unique, and contains enough room.
  962. }
  963. Var
  964. Temp : Pointer;
  965. movelen: SizeInt;
  966. begin
  967. if (l>0) then
  968. begin
  969. if Pointer(S)=nil then
  970. begin
  971. { Need a complete new string...}
  972. Pointer(s):=NewWideString(l);
  973. end
  974. { windows doesn't support reallocing widestrings, this code
  975. is anyways subject to be removed because widestrings shouldn't be
  976. ref. counted anymore (FK) }
  977. else
  978. if
  979. {$ifdef MSWINDOWS}
  980. not winwidestringalloc and
  981. {$endif MSWINDOWS}
  982. {$ifdef FPC_WINLIKEWIDESTRING}
  983. not IsWideStringConstant(pointer(S))
  984. {$else}
  985. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  986. {$endif FPC_WINLIKEWIDESTRING}
  987. then
  988. begin
  989. Dec(Pointer(S),WideFirstOff);
  990. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  991. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  992. Inc(Pointer(S), WideFirstOff);
  993. end
  994. else
  995. begin
  996. { Reallocation is needed... }
  997. Temp:=Pointer(NewWideString(L));
  998. if Length(S)>0 then
  999. begin
  1000. if l < succ(length(s)) then
  1001. movelen := l
  1002. { also move terminating null }
  1003. else
  1004. movelen := succ(length(s));
  1005. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  1006. end;
  1007. fpc_widestr_decr_ref(Pointer(S));
  1008. Pointer(S):=Temp;
  1009. end;
  1010. { Force nil termination in case it gets shorter }
  1011. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  1012. {$ifdef MSWINDOWS}
  1013. if not winwidestringalloc then
  1014. {$endif MSWINDOWS}
  1015. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  1016. end
  1017. else
  1018. begin
  1019. { Length=0 }
  1020. if Pointer(S)<>nil then
  1021. fpc_widestr_decr_ref (Pointer(S));
  1022. Pointer(S):=Nil;
  1023. end;
  1024. end;
  1025. {*****************************************************************************
  1026. Public functions, In interface.
  1027. *****************************************************************************}
  1028. function WideCharToString(S : PWideChar) : AnsiString;
  1029. begin
  1030. result:=WideCharLenToString(s,Length(WideString(s)));
  1031. end;
  1032. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1033. var
  1034. temp:widestring;
  1035. begin
  1036. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1037. if Length(temp)<DestSize then
  1038. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1039. else
  1040. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1041. Dest[DestSize-1]:=#0;
  1042. result:=Dest;
  1043. end;
  1044. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1045. begin
  1046. //SetLength(result,Len);
  1047. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1048. end;
  1049. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1050. begin
  1051. Dest:=WideCharLenToString(Src,Len);
  1052. end;
  1053. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1054. begin
  1055. Dest:=WideCharToString(S);
  1056. end;
  1057. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  1058. {$ifdef FPC_WINLIKEWIDESTRING}
  1059. begin
  1060. pointer(result) := pointer(s);
  1061. end;
  1062. {$else FPC_WINLIKEWIDESTRING}
  1063. {
  1064. Make sure reference count of S is 1,
  1065. using copy-on-write semantics.
  1066. }
  1067. Var
  1068. SNew : Pointer;
  1069. L : SizeInt;
  1070. begin
  1071. pointer(result) := pointer(s);
  1072. If Pointer(S)=Nil then
  1073. exit;
  1074. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  1075. begin
  1076. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  1077. SNew:=NewWideString (L);
  1078. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  1079. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  1080. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  1081. pointer(S):=SNew;
  1082. pointer(result):=SNew;
  1083. end;
  1084. end;
  1085. {$endif FPC_WINLIKEWIDESTRING}
  1086. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  1087. var
  1088. ResultAddress : Pointer;
  1089. begin
  1090. ResultAddress:=Nil;
  1091. dec(index);
  1092. if Index < 0 then
  1093. Index := 0;
  1094. { Check Size. Accounts for Zero-length S, the double check is needed because
  1095. Size can be maxint and will get <0 when adding index }
  1096. if (Size>Length(S)) or
  1097. (Index+Size>Length(S)) then
  1098. Size:=Length(S)-Index;
  1099. If Size>0 then
  1100. begin
  1101. If Index<0 Then
  1102. Index:=0;
  1103. ResultAddress:=Pointer(NewWideString (Size));
  1104. if ResultAddress<>Nil then
  1105. begin
  1106. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  1107. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  1108. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  1109. end;
  1110. end;
  1111. Pointer(fpc_widestr_Copy):=ResultAddress;
  1112. end;
  1113. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1114. var
  1115. i,MaxLen : SizeInt;
  1116. pc : pwidechar;
  1117. begin
  1118. Pos:=0;
  1119. if Length(SubStr)>0 then
  1120. begin
  1121. MaxLen:=Length(source)-Length(SubStr);
  1122. i:=0;
  1123. pc:=@source[1];
  1124. while (i<=MaxLen) do
  1125. begin
  1126. inc(i);
  1127. if (SubStr[1]=pc^) and
  1128. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1129. begin
  1130. Pos:=i;
  1131. exit;
  1132. end;
  1133. inc(pc);
  1134. end;
  1135. end;
  1136. end;
  1137. { Faster version for a widechar alone }
  1138. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1139. var
  1140. i: SizeInt;
  1141. pc : pwidechar;
  1142. begin
  1143. pc:=@s[1];
  1144. for i:=1 to length(s) do
  1145. begin
  1146. if pc^=c then
  1147. begin
  1148. pos:=i;
  1149. exit;
  1150. end;
  1151. inc(pc);
  1152. end;
  1153. pos:=0;
  1154. end;
  1155. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1156. var
  1157. i: SizeInt;
  1158. pc : pchar;
  1159. begin
  1160. pc:=@s[1];
  1161. for i:=1 to length(s) do
  1162. begin
  1163. if widechar(pc^)=c then
  1164. begin
  1165. pos:=i;
  1166. exit;
  1167. end;
  1168. inc(pc);
  1169. end;
  1170. pos:=0;
  1171. end;
  1172. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1173. begin
  1174. result:=Pos(WideString(c),s);
  1175. end;
  1176. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1177. begin
  1178. result:=Pos(WideString(c),s);
  1179. end;
  1180. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1181. begin
  1182. result:=Pos(c,WideString(s));
  1183. end;
  1184. { Faster version for a char alone. Must be implemented because }
  1185. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1186. { using pos(char,pchar) will always call the shortstring version }
  1187. { (exact match for first argument), also with $h+ (JM) }
  1188. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1189. var
  1190. i: SizeInt;
  1191. wc : widechar;
  1192. pc : pwidechar;
  1193. begin
  1194. wc:=c;
  1195. pc:=@s[1];
  1196. for i:=1 to length(s) do
  1197. begin
  1198. if pc^=wc then
  1199. begin
  1200. pos:=i;
  1201. exit;
  1202. end;
  1203. inc(pc);
  1204. end;
  1205. pos:=0;
  1206. end;
  1207. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1208. Var
  1209. LS : SizeInt;
  1210. begin
  1211. If Length(S)=0 then
  1212. exit;
  1213. if index<=0 then
  1214. exit;
  1215. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1216. if (Index<=LS) and (Size>0) then
  1217. begin
  1218. UniqueString (S);
  1219. if Size+Index>LS then
  1220. Size:=LS-Index+1;
  1221. if Index+Size<=LS then
  1222. begin
  1223. Dec(Index);
  1224. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  1225. end;
  1226. Setlength(s,LS-Size);
  1227. end;
  1228. end;
  1229. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1230. var
  1231. Temp : WideString;
  1232. LS : SizeInt;
  1233. begin
  1234. If Length(Source)=0 then
  1235. exit;
  1236. if index <= 0 then
  1237. index := 1;
  1238. Ls:=Length(S);
  1239. if index > LS then
  1240. index := LS+1;
  1241. Dec(Index);
  1242. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1243. SetLength(Temp,Length(Source)+LS);
  1244. If Index>0 then
  1245. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1246. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1247. If (LS-Index)>0 then
  1248. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1249. S:=Temp;
  1250. end;
  1251. function UpCase(const s : WideString) : WideString;
  1252. begin
  1253. result:=widestringmanager.UpperWideStringProc(s);
  1254. end;
  1255. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1256. var
  1257. BufLen: SizeInt;
  1258. begin
  1259. SetLength(S,Len);
  1260. If (Buf<>Nil) and (Len>0) then
  1261. begin
  1262. BufLen := IndexWord(Buf^, Len+1, 0);
  1263. If (BufLen>0) and (BufLen < Len) then
  1264. Len := BufLen;
  1265. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1266. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1267. end;
  1268. end;
  1269. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1270. var
  1271. BufLen: SizeInt;
  1272. begin
  1273. SetLength(S,Len);
  1274. If (Buf<>Nil) and (Len>0) then
  1275. begin
  1276. BufLen := IndexByte(Buf^, Len+1, 0);
  1277. If (BufLen>0) and (BufLen < Len) then
  1278. Len := BufLen;
  1279. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1280. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1281. end;
  1282. end;
  1283. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1284. Var
  1285. SS : String;
  1286. begin
  1287. fpc_Val_Real_WideStr := 0;
  1288. if length(S) > 255 then
  1289. code := 256
  1290. else
  1291. begin
  1292. SS := S;
  1293. Val(SS,fpc_Val_Real_WideStr,code);
  1294. end;
  1295. end;
  1296. {$ifdef FPC_HAS_VALSTR_ENUM}
  1297. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1298. var ss:shortstring;
  1299. begin
  1300. if length(s)>255 then
  1301. code:=256
  1302. else
  1303. begin
  1304. ss:=s;
  1305. val(ss,fpc_val_enum_widestr,code);
  1306. end;
  1307. end;
  1308. {$endif FPC_HAS_VALSTR_ENUM}
  1309. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1310. Var
  1311. SS : String;
  1312. begin
  1313. if length(S) > 255 then
  1314. begin
  1315. fpc_Val_Currency_WideStr:=0;
  1316. code := 256;
  1317. end
  1318. else
  1319. begin
  1320. SS := S;
  1321. Val(SS,fpc_Val_Currency_WideStr,code);
  1322. end;
  1323. end;
  1324. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1325. Var
  1326. SS : ShortString;
  1327. begin
  1328. fpc_Val_UInt_WideStr := 0;
  1329. if length(S) > 255 then
  1330. code := 256
  1331. else
  1332. begin
  1333. SS := S;
  1334. Val(SS,fpc_Val_UInt_WideStr,code);
  1335. end;
  1336. end;
  1337. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1338. Var
  1339. SS : ShortString;
  1340. begin
  1341. fpc_Val_SInt_WideStr:=0;
  1342. if length(S)>255 then
  1343. code:=256
  1344. else
  1345. begin
  1346. SS := S;
  1347. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1348. end;
  1349. end;
  1350. {$ifndef CPU64}
  1351. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1352. Var
  1353. SS : ShortString;
  1354. begin
  1355. fpc_Val_qword_WideStr:=0;
  1356. if length(S)>255 then
  1357. code:=256
  1358. else
  1359. begin
  1360. SS := S;
  1361. Val(SS,fpc_Val_qword_WideStr,Code);
  1362. end;
  1363. end;
  1364. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1365. Var
  1366. SS : ShortString;
  1367. begin
  1368. fpc_Val_int64_WideStr:=0;
  1369. if length(S)>255 then
  1370. code:=256
  1371. else
  1372. begin
  1373. SS := S;
  1374. Val(SS,fpc_Val_int64_WideStr,Code);
  1375. end;
  1376. end;
  1377. {$endif CPU64}
  1378. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1379. var
  1380. ss : shortstring;
  1381. begin
  1382. str_real(len,fr,d,treal_type(rt),ss);
  1383. s:=ss;
  1384. end;
  1385. {$ifdef FPC_HAS_VALSTR_ENUM}
  1386. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1387. var ss:shortstring;
  1388. begin
  1389. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1390. s:=ss;
  1391. end;
  1392. {$endif FPC_HAS_VALSTR_ENUM}
  1393. {$ifdef FPC_HAS_STR_CURRENCY}
  1394. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1395. var
  1396. ss : shortstring;
  1397. begin
  1398. str(c:len:fr,ss);
  1399. s:=ss;
  1400. end;
  1401. {$endif FPC_HAS_STR_CURRENCY}
  1402. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1403. Var
  1404. SS : ShortString;
  1405. begin
  1406. Str (v:Len,SS);
  1407. S:=SS;
  1408. end;
  1409. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1410. Var
  1411. SS : ShortString;
  1412. begin
  1413. str(v:Len,SS);
  1414. S:=SS;
  1415. end;
  1416. {$ifndef CPU64}
  1417. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1418. Var
  1419. SS : ShortString;
  1420. begin
  1421. Str (v:Len,SS);
  1422. S:=SS;
  1423. end;
  1424. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1425. Var
  1426. SS : ShortString;
  1427. begin
  1428. str(v:Len,SS);
  1429. S:=SS;
  1430. end;
  1431. {$endif CPU64}
  1432. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1433. begin
  1434. if assigned(Source) then
  1435. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1436. else
  1437. Result:=0;
  1438. end;
  1439. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1440. var
  1441. i,j : SizeUInt;
  1442. w : word;
  1443. begin
  1444. result:=0;
  1445. if source=nil then
  1446. exit;
  1447. i:=0;
  1448. j:=0;
  1449. if assigned(Dest) then
  1450. begin
  1451. while (i<SourceChars) and (j<MaxDestBytes) do
  1452. begin
  1453. w:=word(Source[i]);
  1454. case w of
  1455. 0..$7f:
  1456. begin
  1457. Dest[j]:=char(w);
  1458. inc(j);
  1459. end;
  1460. $80..$7ff:
  1461. begin
  1462. if j+1>=MaxDestBytes then
  1463. break;
  1464. Dest[j]:=char($c0 or (w shr 6));
  1465. Dest[j+1]:=char($80 or (w and $3f));
  1466. inc(j,2);
  1467. end;
  1468. else
  1469. begin
  1470. if j+2>=MaxDestBytes then
  1471. break;
  1472. Dest[j]:=char($e0 or (w shr 12));
  1473. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1474. Dest[j+2]:=char($80 or (w and $3f));
  1475. inc(j,3);
  1476. end;
  1477. end;
  1478. inc(i);
  1479. end;
  1480. if j>MaxDestBytes-1 then
  1481. j:=MaxDestBytes-1;
  1482. Dest[j]:=#0;
  1483. end
  1484. else
  1485. begin
  1486. while i<SourceChars do
  1487. begin
  1488. case word(Source[i]) of
  1489. $0..$7f:
  1490. inc(j);
  1491. $80..$7ff:
  1492. inc(j,2);
  1493. else
  1494. inc(j,3);
  1495. end;
  1496. inc(i);
  1497. end;
  1498. end;
  1499. result:=j+1;
  1500. end;
  1501. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1502. begin
  1503. if assigned(Source) then
  1504. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1505. else
  1506. Result:=0;
  1507. end;
  1508. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1509. var
  1510. i,j : SizeUInt;
  1511. w: SizeUInt;
  1512. b : byte;
  1513. begin
  1514. if not assigned(Source) then
  1515. begin
  1516. result:=0;
  1517. exit;
  1518. end;
  1519. result:=SizeUInt(-1);
  1520. i:=0;
  1521. j:=0;
  1522. if assigned(Dest) then
  1523. begin
  1524. while (j<MaxDestChars) and (i<SourceBytes) do
  1525. begin
  1526. b:=byte(Source[i]);
  1527. w:=b;
  1528. inc(i);
  1529. // 2 or 3 bytes?
  1530. if b>=$80 then
  1531. begin
  1532. w:=b and $3f;
  1533. if i>=SourceBytes then
  1534. exit;
  1535. // 3 bytes?
  1536. if (b and $20)<>0 then
  1537. begin
  1538. b:=byte(Source[i]);
  1539. inc(i);
  1540. if i>=SourceBytes then
  1541. exit;
  1542. if (b and $c0)<>$80 then
  1543. exit;
  1544. w:=(w shl 6) or (b and $3f);
  1545. end;
  1546. b:=byte(Source[i]);
  1547. w:=(w shl 6) or (b and $3f);
  1548. if (b and $c0)<>$80 then
  1549. exit;
  1550. inc(i);
  1551. end;
  1552. Dest[j]:=WideChar(w);
  1553. inc(j);
  1554. end;
  1555. if j>=MaxDestChars then j:=MaxDestChars-1;
  1556. Dest[j]:=#0;
  1557. end
  1558. else
  1559. begin
  1560. while i<SourceBytes do
  1561. begin
  1562. b:=byte(Source[i]);
  1563. inc(i);
  1564. // 2 or 3 bytes?
  1565. if b>=$80 then
  1566. begin
  1567. if i>=SourceBytes then
  1568. exit;
  1569. // 3 bytes?
  1570. b := b and $3f;
  1571. if (b and $20)<>0 then
  1572. begin
  1573. b:=byte(Source[i]);
  1574. inc(i);
  1575. if i>=SourceBytes then
  1576. exit;
  1577. if (b and $c0)<>$80 then
  1578. exit;
  1579. end;
  1580. if (byte(Source[i]) and $c0)<>$80 then
  1581. exit;
  1582. inc(i);
  1583. end;
  1584. inc(j);
  1585. end;
  1586. end;
  1587. result:=j+1;
  1588. end;
  1589. function UTF8Encode(const s : WideString) : UTF8String;
  1590. var
  1591. i : SizeInt;
  1592. hs : UTF8String;
  1593. begin
  1594. result:='';
  1595. if s='' then
  1596. exit;
  1597. SetLength(hs,length(s)*3);
  1598. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1599. if i>0 then
  1600. begin
  1601. SetLength(hs,i-1);
  1602. result:=hs;
  1603. end;
  1604. end;
  1605. function UTF8Decode(const s : UTF8String): WideString;
  1606. var
  1607. i : SizeInt;
  1608. hs : WideString;
  1609. begin
  1610. result:='';
  1611. if s='' then
  1612. exit;
  1613. SetLength(hs,length(s));
  1614. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1615. if i>0 then
  1616. begin
  1617. SetLength(hs,i-1);
  1618. result:=hs;
  1619. end;
  1620. end;
  1621. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1622. begin
  1623. Result:=Utf8Encode(s);
  1624. end;
  1625. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1626. begin
  1627. Result:=Utf8Decode(s);
  1628. end;
  1629. { converts an utf-16 code point or surrogate pair to utf-32 }
  1630. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1631. var
  1632. w: widechar;
  1633. begin
  1634. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1635. { are the same in UTF-32 }
  1636. w:=s[index];
  1637. if (w<=#$d7ff) or
  1638. (w>=#$e000) then
  1639. begin
  1640. result:=UCS4Char(w);
  1641. len:=1;
  1642. end
  1643. { valid surrogate pair? }
  1644. else if (w<=#$dbff) and
  1645. { w>=#$d7ff check not needed, checked above }
  1646. (index<length(s)) and
  1647. (s[index+1]>=#$dc00) and
  1648. (s[index+1]<=#$dfff) then
  1649. { convert the surrogate pair to UTF-32 }
  1650. begin
  1651. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1652. len:=2;
  1653. end
  1654. else
  1655. { invalid surrogate -> do nothing }
  1656. begin
  1657. result:=UCS4Char(w);
  1658. len:=1;
  1659. end;
  1660. end;
  1661. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1662. var
  1663. i, slen,
  1664. destindex : SizeInt;
  1665. len : longint;
  1666. begin
  1667. slen:=length(s);
  1668. setlength(result,slen+1);
  1669. i:=1;
  1670. destindex:=0;
  1671. while (i<=slen) do
  1672. begin
  1673. result[destindex]:=utf16toutf32(s,i,len);
  1674. inc(destindex);
  1675. inc(i,len);
  1676. end;
  1677. { destindex <= slen (surrogate pairs may have been merged) }
  1678. { destindex+1 for terminating #0 (dynamic arrays are }
  1679. { implicitely filled with zero) }
  1680. setlength(result,destindex+1);
  1681. end;
  1682. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1683. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1684. var
  1685. p : PWideChar;
  1686. begin
  1687. { if nc > $ffff, we need two places }
  1688. if (index+ord(nc > $ffff)>length(s)) then
  1689. if (length(s) < 10*256) then
  1690. setlength(s,length(s)+10)
  1691. else
  1692. setlength(s,length(s)+length(s) shr 8);
  1693. { we know that s is unique -> avoid uniquestring calls}
  1694. p:=@s[index];
  1695. if (nc<$ffff) then
  1696. begin
  1697. p^:=widechar(nc);
  1698. inc(index);
  1699. end
  1700. else if (dword(nc)<=$10ffff) then
  1701. begin
  1702. p^:=widechar((nc - $10000) shr 10 + $d800);
  1703. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1704. inc(index,2);
  1705. end
  1706. else
  1707. { invalid code point }
  1708. begin
  1709. p^:='?';
  1710. inc(index);
  1711. end;
  1712. end;
  1713. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1714. var
  1715. i : SizeInt;
  1716. resindex : SizeInt;
  1717. begin
  1718. { skip terminating #0 }
  1719. SetLength(result,length(s)-1);
  1720. resindex:=1;
  1721. for i:=0 to high(s)-1 do
  1722. ConcatUTF32ToWideStr(s[i],result,resindex);
  1723. { adjust result length (may be too big due to growing }
  1724. { for surrogate pairs) }
  1725. setlength(result,resindex-1);
  1726. end;
  1727. procedure unimplementedwidestring;
  1728. begin
  1729. HandleErrorFrame(215,get_frame);
  1730. end;
  1731. {$warnings off}
  1732. function GenericWideCase(const s : WideString) : WideString;
  1733. begin
  1734. unimplementedwidestring;
  1735. end;
  1736. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1737. begin
  1738. unimplementedwidestring;
  1739. end;
  1740. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1741. begin
  1742. unimplementedwidestring;
  1743. end;
  1744. function CharLengthPChar(const Str: PChar): PtrInt;
  1745. begin
  1746. unimplementedwidestring;
  1747. end;
  1748. {$warnings on}
  1749. procedure initwidestringmanager;
  1750. begin
  1751. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1752. {$ifndef HAS_WIDESTRINGMANAGER}
  1753. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1754. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1755. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1756. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1757. {$endif HAS_WIDESTRINGMANAGER}
  1758. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1759. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1760. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1761. end;