ustrings.inc 48 KB

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