ustrings.inc 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  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. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  349. var
  350. Size : SizeInt;
  351. begin
  352. result:='';
  353. if p=nil then
  354. exit;
  355. Size := IndexWord(p^, -1, 0);
  356. if Size>0 then
  357. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  358. end;
  359. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  360. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  361. var
  362. Size : SizeInt;
  363. temp: ansistring;
  364. begin
  365. result:='';
  366. if p=nil then
  367. exit;
  368. Size := IndexWord(p^, $7fffffff, 0);
  369. if Size>0 then
  370. begin
  371. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  372. result:=temp;
  373. end;
  374. end;
  375. {$else FPC_STRTOSHORTSTRINGPROC}
  376. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  377. var
  378. Size : SizeInt;
  379. temp: ansistring;
  380. begin
  381. res:='';
  382. if p=nil then
  383. exit;
  384. Size:=IndexWord(p^, high(PtrInt), 0);
  385. if Size>0 then
  386. begin
  387. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  388. res:=temp;
  389. end;
  390. end;
  391. {$endif FPC_STRTOSHORTSTRINGPROC}
  392. { checked against the ansistring routine, 2001-05-27 (FK) }
  393. Procedure fpc_UnicodeStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_UNICODESTR_ASSIGN']; compilerproc;
  394. {
  395. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  396. }
  397. begin
  398. If S2<>nil then
  399. If PUnicodeRec(S2-UnicodeFirstOff)^.Ref>0 then
  400. inclocked(PUnicodeRec(S2-UnicodeFirstOff)^.ref);
  401. { Decrease the reference count on the old S1 }
  402. fpc_unicodestr_decr_ref (S1);
  403. s1:=s2;
  404. end;
  405. { alias for internal use }
  406. Procedure fpc_UnicodeStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_UNICODESTR_ASSIGN'];
  407. {$ifndef STR_CONCAT_PROCS}
  408. function fpc_UnicodeStr_Concat (const S1,S2 : UnicodeString): UnicodeString; compilerproc;
  409. Var
  410. Size,Location : SizeInt;
  411. pc : punicodechar;
  412. begin
  413. { only assign if s1 or s2 is empty }
  414. if (S1='') then
  415. begin
  416. result:=s2;
  417. exit;
  418. end;
  419. if (S2='') then
  420. begin
  421. result:=s1;
  422. exit;
  423. end;
  424. Location:=Length(S1);
  425. Size:=length(S2);
  426. SetLength(result,Size+Location);
  427. pc:=punicodechar(result);
  428. Move(S1[1],pc^,Location*sizeof(UnicodeChar));
  429. inc(pc,location);
  430. Move(S2[1],pc^,(Size+1)*sizeof(UnicodeChar));
  431. end;
  432. function fpc_UnicodeStr_Concat_multi (const sarr:array of Unicodestring): unicodestring; compilerproc;
  433. Var
  434. i : Longint;
  435. p : pointer;
  436. pc : punicodechar;
  437. Size,NewSize : SizeInt;
  438. begin
  439. { First calculate size of the result so we can do
  440. a single call to SetLength() }
  441. NewSize:=0;
  442. for i:=low(sarr) to high(sarr) do
  443. inc(Newsize,length(sarr[i]));
  444. SetLength(result,NewSize);
  445. pc:=punicodechar(result);
  446. for i:=low(sarr) to high(sarr) do
  447. begin
  448. p:=pointer(sarr[i]);
  449. if assigned(p) then
  450. begin
  451. Size:=length(unicodestring(p));
  452. Move(punicodechar(p)^,pc^,(Size+1)*sizeof(UnicodeChar));
  453. inc(pc,size);
  454. end;
  455. end;
  456. end;
  457. {$else STR_CONCAT_PROCS}
  458. procedure fpc_UnicodeStr_Concat (var DestS:Unicodestring;const S1,S2 : UnicodeString); compilerproc;
  459. Var
  460. Size,Location : SizeInt;
  461. same : boolean;
  462. begin
  463. { only assign if s1 or s2 is empty }
  464. if (S1='') then
  465. begin
  466. DestS:=s2;
  467. exit;
  468. end;
  469. if (S2='') then
  470. begin
  471. DestS:=s1;
  472. exit;
  473. end;
  474. Location:=Length(S1);
  475. Size:=length(S2);
  476. { Use Pointer() typecasts to prevent extra conversion code }
  477. if Pointer(DestS)=Pointer(S1) then
  478. begin
  479. same:=Pointer(S1)=Pointer(S2);
  480. SetLength(DestS,Size+Location);
  481. if same then
  482. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size)*sizeof(UnicodeChar))
  483. else
  484. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  485. end
  486. else if Pointer(DestS)=Pointer(S2) then
  487. begin
  488. SetLength(DestS,Size+Location);
  489. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  490. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(UnicodeChar));
  491. end
  492. else
  493. begin
  494. DestS:='';
  495. SetLength(DestS,Size+Location);
  496. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(UnicodeChar));
  497. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  498. end;
  499. end;
  500. procedure fpc_UnicodeStr_Concat_multi (var DestS:Unicodestring;const sarr:array of Unicodestring); compilerproc;
  501. Var
  502. i : Longint;
  503. p,pc : pointer;
  504. Size,NewLen : SizeInt;
  505. lowstart : longint;
  506. destcopy : pointer;
  507. OldDestLen : SizeInt;
  508. begin
  509. if high(sarr)=0 then
  510. begin
  511. DestS:='';
  512. exit;
  513. end;
  514. destcopy:=nil;
  515. lowstart:=low(sarr);
  516. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  517. inc(lowstart);
  518. { Check for another reuse, then we can't use
  519. the append optimization }
  520. for i:=lowstart to high(sarr) do
  521. begin
  522. if Pointer(DestS)=Pointer(sarr[i]) then
  523. begin
  524. { if DestS is used somewhere in the middle of the expression,
  525. we need to make sure the original string still exists after
  526. we empty/modify DestS.
  527. This trick only works with reference counted strings. Therefor
  528. this optimization is disabled for WINLIKEUNICODESTRING }
  529. destcopy:=pointer(dests);
  530. fpc_UnicodeStr_Incr_Ref(destcopy);
  531. lowstart:=low(sarr);
  532. break;
  533. end;
  534. end;
  535. { Start with empty DestS if we start with concatting
  536. the first array element }
  537. if lowstart=low(sarr) then
  538. DestS:='';
  539. OldDestLen:=length(DestS);
  540. { Calculate size of the result so we can do
  541. a single call to SetLength() }
  542. NewLen:=0;
  543. for i:=low(sarr) to high(sarr) do
  544. inc(NewLen,length(sarr[i]));
  545. SetLength(DestS,NewLen);
  546. { Concat all strings, except the string we already
  547. copied in DestS }
  548. pc:=Pointer(DestS)+OldDestLen*sizeof(UnicodeChar);
  549. for i:=lowstart to high(sarr) do
  550. begin
  551. p:=pointer(sarr[i]);
  552. if assigned(p) then
  553. begin
  554. Size:=length(unicodestring(p));
  555. Move(p^,pc^,(Size+1)*sizeof(UnicodeChar));
  556. inc(pc,size*sizeof(UnicodeChar));
  557. end;
  558. end;
  559. fpc_UnicodeStr_Decr_Ref(destcopy);
  560. end;
  561. {$endif STR_CONCAT_PROCS}
  562. Function fpc_Char_To_UChar(const c : Char): UnicodeChar; compilerproc;
  563. var
  564. w: unicodestring;
  565. begin
  566. widestringmanager.Ansi2UnicodeMoveProc(@c, w, 1);
  567. fpc_Char_To_UChar:= w[1];
  568. end;
  569. Function fpc_Char_To_UnicodeStr(const c : Char): UnicodeString; compilerproc;
  570. {
  571. Converts a Char to a UnicodeString;
  572. }
  573. begin
  574. Setlength(fpc_Char_To_UnicodeStr,1);
  575. fpc_Char_To_UnicodeStr[1]:=c;
  576. { Terminating Zero }
  577. PUnicodeChar(Pointer(fpc_Char_To_UnicodeStr)+sizeof(UnicodeChar))^:=#0;
  578. end;
  579. Function fpc_UChar_To_Char(const c : UnicodeChar): Char; compilerproc;
  580. {
  581. Converts a UnicodeChar to a Char;
  582. }
  583. var
  584. s: ansistring;
  585. begin
  586. widestringmanager.Unicode2AnsiMoveProc(@c, s, 1);
  587. if length(s)=1 then
  588. fpc_UChar_To_Char:= s[1]
  589. else
  590. fpc_UChar_To_Char:='?';
  591. end;
  592. Function fpc_WChar_To_UnicodeStr(const c : WideChar): UnicodeString; compilerproc;
  593. {
  594. Converts a WideChar to a UnicodeString;
  595. }
  596. begin
  597. Setlength (Result,1);
  598. Result[1]:= c;
  599. end;
  600. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  601. var
  602. w: widestring;
  603. begin
  604. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  605. fpc_Char_To_WChar:= w[1];
  606. end;
  607. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  608. {
  609. Converts a WideChar to a Char;
  610. }
  611. var
  612. s: ansistring;
  613. begin
  614. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  615. if length(s)=1 then
  616. fpc_WChar_To_Char:= s[1]
  617. else
  618. fpc_WChar_To_Char:='?';
  619. end;
  620. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  621. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  622. {
  623. Converts a WideChar to a ShortString;
  624. }
  625. var
  626. s: ansistring;
  627. begin
  628. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  629. fpc_WChar_To_ShortStr:= s;
  630. end;
  631. {$else FPC_STRTOSHORTSTRINGPROC}
  632. procedure fpc_WChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  633. {
  634. Converts a WideChar to a ShortString;
  635. }
  636. var
  637. s: ansistring;
  638. begin
  639. widestringmanager.Wide2AnsiMoveProc(@c,s,1);
  640. res:=s;
  641. end;
  642. {$endif FPC_STRTOSHORTSTRINGPROC}
  643. Function fpc_UChar_To_UnicodeStr(const c : UnicodeChar): UnicodeString; compilerproc;
  644. {
  645. Converts a UnicodeChar to a UnicodeString;
  646. }
  647. begin
  648. Setlength (fpc_UChar_To_UnicodeStr,1);
  649. fpc_UChar_To_UnicodeStr[1]:= c;
  650. end;
  651. Function fpc_UChar_To_AnsiStr(const c : UnicodeChar): AnsiString; compilerproc;
  652. {
  653. Converts a UnicodeChar to a AnsiString;
  654. }
  655. begin
  656. widestringmanager.Unicode2AnsiMoveProc(@c, fpc_UChar_To_AnsiStr, 1);
  657. end;
  658. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  659. Function fpc_UChar_To_ShortStr(const c : UnicodeChar): ShortString; compilerproc;
  660. {
  661. Converts a UnicodeChar to a ShortString;
  662. }
  663. var
  664. s: ansistring;
  665. begin
  666. widestringmanager.Unicode2AnsiMoveProc(@c, s, 1);
  667. fpc_UChar_To_ShortStr:= s;
  668. end;
  669. {$else FPC_STRTOSHORTSTRINGPROC}
  670. procedure fpc_UChar_To_ShortStr(out res : shortstring;const c : UnicodeChar) compilerproc;
  671. {
  672. Converts a UnicodeChar to a ShortString;
  673. }
  674. var
  675. s: ansistring;
  676. begin
  677. widestringmanager.Unicode2AnsiMoveProc(@c,s,1);
  678. res:=s;
  679. end;
  680. {$endif FPC_STRTOSHORTSTRINGPROC}
  681. Function fpc_PChar_To_UnicodeStr(const p : pchar): UnicodeString; compilerproc;
  682. Var
  683. L : SizeInt;
  684. begin
  685. if (not assigned(p)) or (p[0]=#0) Then
  686. begin
  687. fpc_pchar_to_unicodestr := '';
  688. exit;
  689. end;
  690. l:=IndexChar(p^,-1,#0);
  691. widestringmanager.Ansi2UnicodeMoveProc(P,fpc_PChar_To_UnicodeStr,l);
  692. end;
  693. Function fpc_CharArray_To_UnicodeStr(const arr: array of char; zerobased: boolean = true): UnicodeString; compilerproc;
  694. var
  695. i : SizeInt;
  696. begin
  697. if (zerobased) then
  698. begin
  699. if (arr[0]=#0) Then
  700. begin
  701. fpc_chararray_to_unicodestr := '';
  702. exit;
  703. end;
  704. i:=IndexChar(arr,high(arr)+1,#0);
  705. if i = -1 then
  706. i := high(arr)+1;
  707. end
  708. else
  709. i := high(arr)+1;
  710. SetLength(fpc_CharArray_To_UnicodeStr,i);
  711. widestringmanager.Ansi2UnicodeMoveProc (pchar(@arr),fpc_CharArray_To_UnicodeStr,i);
  712. end;
  713. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  714. function fpc_UnicodeCharArray_To_ShortStr(const arr: array of unicodechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_UNICODECHARARRAY_TO_SHORTSTR']; compilerproc;
  715. var
  716. l: longint;
  717. index: longint;
  718. len: byte;
  719. temp: ansistring;
  720. begin
  721. l := high(arr)+1;
  722. if l>=256 then
  723. l:=255
  724. else if l<0 then
  725. l:=0;
  726. if zerobased then
  727. begin
  728. index:=IndexWord(arr[0],l,0);
  729. if (index < 0) then
  730. len := l
  731. else
  732. len := index;
  733. end
  734. else
  735. len := l;
  736. widestringmanager.Unicode2AnsiMoveProc (punicodechar(@arr),temp,len);
  737. fpc_UnicodeCharArray_To_ShortStr := temp;
  738. end;
  739. {$else FPC_STRTOSHORTSTRINGPROC}
  740. procedure fpc_UnicodeCharArray_To_ShortStr(out res : shortstring;const arr: array of unicodechar; zerobased: boolean = true);[public,alias:'FPC_UNICODECHARARRAY_TO_SHORTSTR']; compilerproc;
  741. var
  742. l: longint;
  743. index: ptrint;
  744. len: byte;
  745. temp: ansistring;
  746. begin
  747. l := high(arr)+1;
  748. if l>=high(res)+1 then
  749. l:=high(res)
  750. else if l<0 then
  751. l:=0;
  752. if zerobased then
  753. begin
  754. index:=IndexWord(arr[0],l,0);
  755. if index<0 then
  756. len:=l
  757. else
  758. len:=index;
  759. end
  760. else
  761. len:=l;
  762. widestringmanager.Unicode2AnsiMoveProc (punicodechar(@arr),temp,len);
  763. res:=temp;
  764. end;
  765. {$endif FPC_STRTOSHORTSTRINGPROC}
  766. Function fpc_UnicodeCharArray_To_AnsiStr(const arr: array of unicodechar; zerobased: boolean = true): AnsiString; compilerproc;
  767. var
  768. i : SizeInt;
  769. begin
  770. if (zerobased) then
  771. begin
  772. i:=IndexWord(arr,high(arr)+1,0);
  773. if i = -1 then
  774. i := high(arr)+1;
  775. end
  776. else
  777. i := high(arr)+1;
  778. SetLength(fpc_UnicodeCharArray_To_AnsiStr,i);
  779. widestringmanager.Unicode2AnsiMoveProc (punicodechar(@arr),fpc_UnicodeCharArray_To_AnsiStr,i);
  780. end;
  781. Function fpc_UnicodeCharArray_To_UnicodeStr(const arr: array of unicodechar; zerobased: boolean = true): UnicodeString; compilerproc;
  782. var
  783. i : SizeInt;
  784. begin
  785. if (zerobased) then
  786. begin
  787. i:=IndexWord(arr,high(arr)+1,0);
  788. if i = -1 then
  789. i := high(arr)+1;
  790. end
  791. else
  792. i := high(arr)+1;
  793. SetLength(fpc_UnicodeCharArray_To_UnicodeStr,i);
  794. Move(arr[0], Pointer(fpc_UnicodeCharArray_To_UnicodeStr)^,i*sizeof(UnicodeChar));
  795. end;
  796. Function fpc_WideCharArray_To_UnicodeStr(const arr: array of widechar; zerobased: boolean = true): UnicodeString; compilerproc;
  797. var
  798. i : SizeInt;
  799. begin
  800. if (zerobased) then
  801. begin
  802. i:=IndexWord(arr,high(arr)+1,0);
  803. if i = -1 then
  804. i := high(arr)+1;
  805. end
  806. else
  807. i := high(arr)+1;
  808. SetLength(fpc_WideCharArray_To_UnicodeStr,i);
  809. Move(arr[0], Pointer(fpc_WideCharArray_To_UnicodeStr)^,i*sizeof(WideChar));
  810. end;
  811. { due to their names, the following procedures should be in wstrings.inc,
  812. however, the compiler generates code using this functions on all platforms }
  813. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  814. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  815. var
  816. l: longint;
  817. index: longint;
  818. len: byte;
  819. temp: ansistring;
  820. begin
  821. l := high(arr)+1;
  822. if l>=256 then
  823. l:=255
  824. else if l<0 then
  825. l:=0;
  826. if zerobased then
  827. begin
  828. index:=IndexWord(arr[0],l,0);
  829. if (index < 0) then
  830. len := l
  831. else
  832. len := index;
  833. end
  834. else
  835. len := l;
  836. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  837. fpc_WideCharArray_To_ShortStr := temp;
  838. end;
  839. {$else FPC_STRTOSHORTSTRINGPROC}
  840. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  841. var
  842. l: longint;
  843. index: ptrint;
  844. len: byte;
  845. temp: ansistring;
  846. begin
  847. l := high(arr)+1;
  848. if l>=high(res)+1 then
  849. l:=high(res)
  850. else if l<0 then
  851. l:=0;
  852. if zerobased then
  853. begin
  854. index:=IndexWord(arr[0],l,0);
  855. if index<0 then
  856. len:=l
  857. else
  858. len:=index;
  859. end
  860. else
  861. len:=l;
  862. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  863. res:=temp;
  864. end;
  865. {$endif FPC_STRTOSHORTSTRINGPROC}
  866. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  867. var
  868. i : SizeInt;
  869. begin
  870. if (zerobased) then
  871. begin
  872. i:=IndexWord(arr,high(arr)+1,0);
  873. if i = -1 then
  874. i := high(arr)+1;
  875. end
  876. else
  877. i := high(arr)+1;
  878. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  879. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  880. end;
  881. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  882. var
  883. i : SizeInt;
  884. begin
  885. if (zerobased) then
  886. begin
  887. i:=IndexWord(arr,high(arr)+1,0);
  888. if i = -1 then
  889. i := high(arr)+1;
  890. end
  891. else
  892. i := high(arr)+1;
  893. SetLength(fpc_WideCharArray_To_WideStr,i);
  894. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  895. end;
  896. {$ifndef FPC_STRTOCHARARRAYPROC}
  897. { inside the compiler, the resulttype is modified to that of the actual }
  898. { chararray we're converting to (JM) }
  899. function fpc_unicodestr_to_chararray(arraysize: SizeInt; const src: UnicodeString): fpc_big_chararray;[public,alias: 'FPC_UNICODESTR_TO_CHARARRAY']; compilerproc;
  900. var
  901. len: SizeInt;
  902. temp: ansistring;
  903. begin
  904. len := length(src);
  905. { make sure we don't dereference src if it can be nil (JM) }
  906. if len > 0 then
  907. widestringmanager.unicode2ansimoveproc(punicodechar(@src[1]),temp,len);
  908. len := length(temp);
  909. if len > arraysize then
  910. len := arraysize;
  911. {$r-}
  912. move(temp[1],fpc_unicodestr_to_chararray[0],len);
  913. fillchar(fpc_unicodestr_to_chararray[len],arraysize-len,0);
  914. {$ifdef RangeCheckWasOn}
  915. {$r+}
  916. {$endif}
  917. end;
  918. { inside the compiler, the resulttype is modified to that of the actual }
  919. { unicodechararray we're converting to (JM) }
  920. function fpc_unicodestr_to_unicodechararray(arraysize: SizeInt; const src: UnicodeString): fpc_big_unicodechararray;[public,alias: 'FPC_UNICODESTR_TO_UNICODECHARARRAY']; compilerproc;
  921. var
  922. len: SizeInt;
  923. begin
  924. len := length(src);
  925. if len > arraysize then
  926. len := arraysize;
  927. {$r-}
  928. { make sure we don't try to access element 1 of the ansistring if it's nil }
  929. if len > 0 then
  930. move(src[1],fpc_unicodestr_to_unicodechararray[0],len*SizeOf(UnicodeChar));
  931. fillchar(fpc_unicodestr_to_unicodechararray[len],(arraysize-len)*SizeOf(UnicodeChar),0);
  932. {$ifdef RangeCheckWasOn}
  933. {$r+}
  934. {$endif}
  935. end;
  936. { inside the compiler, the resulttype is modified to that of the actual }
  937. { chararray we're converting to (JM) }
  938. function fpc_ansistr_to_unicodechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_unicodechararray;[public,alias: 'FPC_ANSISTR_TO_UNICODECHARARRAY']; compilerproc;
  939. var
  940. len: SizeInt;
  941. temp: unicodestring;
  942. begin
  943. len := length(src);
  944. { make sure we don't dereference src if it can be nil (JM) }
  945. if len > 0 then
  946. widestringmanager.ansi2unicodemoveproc(pchar(@src[1]),temp,len);
  947. len := length(temp);
  948. if len > arraysize then
  949. len := arraysize;
  950. {$r-}
  951. move(temp[1],fpc_ansistr_to_unicodechararray[0],len*sizeof(unicodechar));
  952. fillchar(fpc_ansistr_to_unicodechararray[len],(arraysize-len)*SizeOf(UnicodeChar),0);
  953. {$ifdef RangeCheckWasOn}
  954. {$r+}
  955. {$endif}
  956. end;
  957. function fpc_shortstr_to_unicodechararray(arraysize: SizeInt; const src: ShortString): fpc_big_unicodechararray;[public,alias: 'FPC_SHORTSTR_TO_UNICODECHARARRAY']; compilerproc;
  958. var
  959. len: longint;
  960. temp : unicodestring;
  961. begin
  962. len := length(src);
  963. { make sure we don't access char 1 if length is 0 (JM) }
  964. if len > 0 then
  965. widestringmanager.ansi2unicodemoveproc(pchar(@src[1]),temp,len);
  966. len := length(temp);
  967. if len > arraysize then
  968. len := arraysize;
  969. {$r-}
  970. move(temp[1],fpc_shortstr_to_unicodechararray[0],len*sizeof(unicodechar));
  971. fillchar(fpc_shortstr_to_unicodechararray[len],(arraysize-len)*SizeOf(UnicodeChar),0);
  972. {$ifdef RangeCheckWasOn}
  973. {$r+}
  974. {$endif}
  975. end;
  976. {$else ndef FPC_STRTOCHARARRAYPROC}
  977. procedure fpc_unicodestr_to_chararray(out res: array of char; const src: UnicodeString); compilerproc;
  978. var
  979. len: SizeInt;
  980. temp: ansistring;
  981. begin
  982. len := length(src);
  983. { make sure we don't dereference src if it can be nil (JM) }
  984. if len > 0 then
  985. widestringmanager.unicode2ansimoveproc(punicodechar(@src[1]),temp,len);
  986. len := length(temp);
  987. if len > length(res) then
  988. len := length(res);
  989. {$r-}
  990. move(temp[1],res[0],len);
  991. fillchar(res[len],length(res)-len,0);
  992. {$ifdef RangeCheckWasOn}
  993. {$r+}
  994. {$endif}
  995. end;
  996. procedure fpc_unicodestr_to_unicodechararray(out res: array of unicodechar; const src: UnicodeString); compilerproc;
  997. var
  998. len: SizeInt;
  999. begin
  1000. len := length(src);
  1001. if len > length(res) then
  1002. len := length(res);
  1003. {$r-}
  1004. { make sure we don't try to access element 1 of the ansistring if it's nil }
  1005. if len > 0 then
  1006. move(src[1],res[0],len*SizeOf(UnicodeChar));
  1007. fillchar(res[len],(length(res)-len)*SizeOf(UnicodeChar),0);
  1008. {$ifdef RangeCheckWasOn}
  1009. {$r+}
  1010. {$endif}
  1011. end;
  1012. procedure fpc_ansistr_to_unicodechararray(out res: array of unicodechar; const src: AnsiString); compilerproc;
  1013. var
  1014. len: SizeInt;
  1015. temp: unicodestring;
  1016. begin
  1017. len := length(src);
  1018. { make sure we don't dereference src if it can be nil (JM) }
  1019. if len > 0 then
  1020. widestringmanager.ansi2unicodemoveproc(pchar(@src[1]),temp,len);
  1021. len := length(temp);
  1022. if len > length(res) then
  1023. len := length(res);
  1024. {$r-}
  1025. move(temp[1],res[0],len*sizeof(unicodechar));
  1026. fillchar(res[len],(length(res)-len)*SizeOf(UnicodeChar),0);
  1027. {$ifdef RangeCheckWasOn}
  1028. {$r+}
  1029. {$endif}
  1030. end;
  1031. procedure fpc_shortstr_to_unicodechararray(out res: array of unicodechar; const src: ShortString); compilerproc;
  1032. var
  1033. len: longint;
  1034. temp : unicodestring;
  1035. begin
  1036. len := length(src);
  1037. { make sure we don't access char 1 if length is 0 (JM) }
  1038. if len > 0 then
  1039. widestringmanager.ansi2unicodemoveproc(pchar(@src[1]),temp,len);
  1040. len := length(temp);
  1041. if len > length(res) then
  1042. len := length(res);
  1043. {$r-}
  1044. move(temp[1],res[0],len*sizeof(unicodechar));
  1045. fillchar(res[len],(length(res)-len)*SizeOf(UnicodeChar),0);
  1046. {$ifdef RangeCheckWasOn}
  1047. {$r+}
  1048. {$endif}
  1049. end;
  1050. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  1051. var
  1052. len: SizeInt;
  1053. temp: widestring;
  1054. begin
  1055. len := length(src);
  1056. { make sure we don't dereference src if it can be nil (JM) }
  1057. if len > 0 then
  1058. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  1059. len := length(temp);
  1060. if len > length(res) then
  1061. len := length(res);
  1062. {$r-}
  1063. move(temp[1],res[0],len*sizeof(widechar));
  1064. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  1065. {$ifdef RangeCheckWasOn}
  1066. {$r+}
  1067. {$endif}
  1068. end;
  1069. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  1070. var
  1071. len: longint;
  1072. temp : widestring;
  1073. begin
  1074. len := length(src);
  1075. { make sure we don't access char 1 if length is 0 (JM) }
  1076. if len > 0 then
  1077. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  1078. len := length(temp);
  1079. if len > length(res) then
  1080. len := length(res);
  1081. {$r-}
  1082. move(temp[1],res[0],len*sizeof(widechar));
  1083. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  1084. {$ifdef RangeCheckWasOn}
  1085. {$r+}
  1086. {$endif}
  1087. end;
  1088. procedure fpc_unicodestr_to_widechararray(out res: array of widechar; const src: UnicodeString); compilerproc;
  1089. var
  1090. len: SizeInt;
  1091. begin
  1092. len := length(src);
  1093. if len > length(res) then
  1094. len := length(res);
  1095. {$r-}
  1096. { make sure we don't try to access element 1 of the widestring if it's nil }
  1097. if len > 0 then
  1098. move(src[1],res[0],len*SizeOf(WideChar));
  1099. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  1100. {$ifdef RangeCheckWasOn}
  1101. {$r+}
  1102. {$endif}
  1103. end;
  1104. {$endif ndef FPC_STRTOCHARARRAYPROC}
  1105. Function fpc_UnicodeStr_Compare(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE']; compilerproc;
  1106. {
  1107. Compares 2 UnicodeStrings;
  1108. The result is
  1109. <0 if S1<S2
  1110. 0 if S1=S2
  1111. >0 if S1>S2
  1112. }
  1113. Var
  1114. MaxI,Temp : SizeInt;
  1115. begin
  1116. if pointer(S1)=pointer(S2) then
  1117. begin
  1118. fpc_UnicodeStr_Compare:=0;
  1119. exit;
  1120. end;
  1121. Maxi:=Length(S1);
  1122. temp:=Length(S2);
  1123. If MaxI>Temp then
  1124. MaxI:=Temp;
  1125. Temp:=CompareWord(S1[1],S2[1],MaxI);
  1126. if temp=0 then
  1127. temp:=Length(S1)-Length(S2);
  1128. fpc_UnicodeStr_Compare:=Temp;
  1129. end;
  1130. Function fpc_UnicodeStr_Compare_Equal(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE_EQUAL']; compilerproc;
  1131. {
  1132. Compares 2 UnicodeStrings for equality only;
  1133. The result is
  1134. 0 if S1=S2
  1135. <>0 if S1<>S2
  1136. }
  1137. Var
  1138. MaxI : SizeInt;
  1139. begin
  1140. if pointer(S1)=pointer(S2) then
  1141. exit(0);
  1142. Maxi:=Length(S1);
  1143. If MaxI<>Length(S2) then
  1144. exit(-1)
  1145. else
  1146. exit(CompareWord(S1[1],S2[1],MaxI));
  1147. end;
  1148. Procedure fpc_UnicodeStr_CheckZero(p : pointer);[Public,Alias : 'FPC_UNICODESTR_CHECKZERO']; compilerproc;
  1149. begin
  1150. if p=nil then
  1151. HandleErrorFrame(201,get_frame);
  1152. end;
  1153. Procedure fpc_UnicodeStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_UNICODESTR_RANGECHECK']; compilerproc;
  1154. begin
  1155. if (index>len div 2) or (Index<1) then
  1156. HandleErrorFrame(201,get_frame);
  1157. end;
  1158. Procedure fpc_UnicodeStr_SetLength(Var S : UnicodeString; l : SizeInt);[Public,Alias : 'FPC_UNICODESTR_SETLENGTH']; compilerproc;
  1159. {
  1160. Sets The length of string S to L.
  1161. Makes sure S is unique, and contains enough room.
  1162. }
  1163. Var
  1164. Temp : Pointer;
  1165. movelen: SizeInt;
  1166. begin
  1167. if (l>0) then
  1168. begin
  1169. if Pointer(S)=nil then
  1170. begin
  1171. { Need a complete new string...}
  1172. Pointer(s):=NewUnicodeString(l);
  1173. end
  1174. { windows doesn't support reallocing unicodestrings, this code
  1175. is anyways subject to be removed because unicodestrings shouldn't be
  1176. ref. counted anymore (FK) }
  1177. else
  1178. if (PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref = 1) then
  1179. begin
  1180. Dec(Pointer(S),UnicodeFirstOff);
  1181. if SizeUInt(L*sizeof(UnicodeChar)+UnicodeRecLen)>MemSize(Pointer(S)) then
  1182. reallocmem(pointer(S), L*sizeof(UnicodeChar)+UnicodeRecLen);
  1183. Inc(Pointer(S), UnicodeFirstOff);
  1184. end
  1185. else
  1186. begin
  1187. { Reallocation is needed... }
  1188. Temp:=Pointer(NewUnicodeString(L));
  1189. if Length(S)>0 then
  1190. begin
  1191. if l < succ(length(s)) then
  1192. movelen := l
  1193. { also move terminating null }
  1194. else
  1195. movelen := succ(length(s));
  1196. Move(Pointer(S)^,Temp^,movelen * Sizeof(UnicodeChar));
  1197. end;
  1198. fpc_unicodestr_decr_ref(Pointer(S));
  1199. Pointer(S):=Temp;
  1200. end;
  1201. { Force nil termination in case it gets shorter }
  1202. PWord(Pointer(S)+l*sizeof(UnicodeChar))^:=0;
  1203. PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len:=l*sizeof(UnicodeChar);
  1204. end
  1205. else
  1206. begin
  1207. { Length=0 }
  1208. if Pointer(S)<>nil then
  1209. fpc_unicodestr_decr_ref (Pointer(S));
  1210. Pointer(S):=Nil;
  1211. end;
  1212. end;
  1213. {*****************************************************************************
  1214. Public functions, In interface.
  1215. *****************************************************************************}
  1216. function UnicodeCharToString(S : PUnicodeChar) : AnsiString;
  1217. begin
  1218. result:=UnicodeCharLenToString(s,Length(UnicodeString(s)));
  1219. end;
  1220. function StringToUnicodeChar(const Src : AnsiString;Dest : PUnicodeChar;DestSize : SizeInt) : PUnicodeChar;
  1221. var
  1222. temp:unicodestring;
  1223. begin
  1224. widestringmanager.Ansi2UnicodeMoveProc(PChar(Src),temp,Length(Src));
  1225. if Length(temp)<DestSize then
  1226. move(temp[1],Dest^,Length(temp)*SizeOf(UnicodeChar))
  1227. else
  1228. move(temp[1],Dest^,(DestSize-1)*SizeOf(UnicodeChar));
  1229. Dest[DestSize-1]:=#0;
  1230. result:=Dest;
  1231. end;
  1232. function WideCharToString(S : PWideChar) : AnsiString;
  1233. begin
  1234. result:=WideCharLenToString(s,Length(WideString(s)));
  1235. end;
  1236. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1237. var
  1238. temp:widestring;
  1239. begin
  1240. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1241. if Length(temp)<DestSize then
  1242. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1243. else
  1244. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1245. Dest[DestSize-1]:=#0;
  1246. result:=Dest;
  1247. end;
  1248. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : AnsiString;
  1249. begin
  1250. //SetLength(result,Len);
  1251. widestringmanager.Unicode2AnsiMoveproc(S,result,Len);
  1252. end;
  1253. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : AnsiString);
  1254. begin
  1255. Dest:=UnicodeCharLenToString(Src,Len);
  1256. end;
  1257. procedure UnicodeCharToStrVar(S : PUnicodeChar;out Dest : AnsiString);
  1258. begin
  1259. Dest:=UnicodeCharToString(S);
  1260. end;
  1261. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1262. begin
  1263. //SetLength(result,Len);
  1264. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1265. end;
  1266. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1267. begin
  1268. Dest:=WideCharLenToString(Src,Len);
  1269. end;
  1270. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1271. begin
  1272. Dest:=WideCharToString(S);
  1273. end;
  1274. Function fpc_unicodestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_UNICODESTR_UNIQUE']; compilerproc;
  1275. {
  1276. Make sure reference count of S is 1,
  1277. using copy-on-write semantics.
  1278. }
  1279. Var
  1280. SNew : Pointer;
  1281. L : SizeInt;
  1282. begin
  1283. pointer(result) := pointer(s);
  1284. If Pointer(S)=Nil then
  1285. exit;
  1286. if PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref<>1 then
  1287. begin
  1288. L:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.len div sizeof(UnicodeChar);
  1289. SNew:=NewUnicodeString (L);
  1290. Move (PUnicodeChar(S)^,SNew^,(L+1)*sizeof(UnicodeChar));
  1291. PUnicodeRec(SNew-UnicodeFirstOff)^.len:=L * sizeof(UnicodeChar);
  1292. fpc_unicodestr_decr_ref (Pointer(S)); { Thread safe }
  1293. pointer(S):=SNew;
  1294. pointer(result):=SNew;
  1295. end;
  1296. end;
  1297. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  1298. var
  1299. ResultAddress : Pointer;
  1300. begin
  1301. ResultAddress:=Nil;
  1302. dec(index);
  1303. if Index < 0 then
  1304. Index := 0;
  1305. { Check Size. Accounts for Zero-length S, the double check is needed because
  1306. Size can be maxint and will get <0 when adding index }
  1307. if (Size>Length(S)) or
  1308. (Index+Size>Length(S)) then
  1309. Size:=Length(S)-Index;
  1310. If Size>0 then
  1311. begin
  1312. If Index<0 Then
  1313. Index:=0;
  1314. ResultAddress:=Pointer(NewUnicodeString (Size));
  1315. if ResultAddress<>Nil then
  1316. begin
  1317. Move (PUnicodeChar(S)[Index],ResultAddress^,Size*sizeof(UnicodeChar));
  1318. PUnicodeRec(ResultAddress-UnicodeFirstOff)^.Len:=Size*sizeof(UnicodeChar);
  1319. PUnicodeChar(ResultAddress+Size*sizeof(UnicodeChar))^:=#0;
  1320. end;
  1321. end;
  1322. fpc_unicodestr_decr_ref(Pointer(fpc_unicodestr_copy));
  1323. Pointer(fpc_unicodestr_Copy):=ResultAddress;
  1324. end;
  1325. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  1326. var
  1327. i,MaxLen : SizeInt;
  1328. pc : punicodechar;
  1329. begin
  1330. Pos:=0;
  1331. if Length(SubStr)>0 then
  1332. begin
  1333. MaxLen:=Length(source)-Length(SubStr);
  1334. i:=0;
  1335. pc:=@source[1];
  1336. while (i<=MaxLen) do
  1337. begin
  1338. inc(i);
  1339. if (SubStr[1]=pc^) and
  1340. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1341. begin
  1342. Pos:=i;
  1343. exit;
  1344. end;
  1345. inc(pc);
  1346. end;
  1347. end;
  1348. end;
  1349. { Faster version for a unicodechar alone }
  1350. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  1351. var
  1352. i: SizeInt;
  1353. pc : punicodechar;
  1354. begin
  1355. pc:=@s[1];
  1356. for i:=1 to length(s) do
  1357. begin
  1358. if pc^=c then
  1359. begin
  1360. pos:=i;
  1361. exit;
  1362. end;
  1363. inc(pc);
  1364. end;
  1365. pos:=0;
  1366. end;
  1367. Function Pos (c : AnsiString; Const s : UnicodeString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1368. begin
  1369. result:=Pos(UnicodeString(c),s);
  1370. end;
  1371. Function Pos (c : ShortString; Const s : UnicodeString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1372. begin
  1373. result:=Pos(UnicodeString(c),s);
  1374. end;
  1375. Function Pos (c : UnicodeString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1376. begin
  1377. result:=Pos(c,UnicodeString(s));
  1378. end;
  1379. { Faster version for a char alone. Must be implemented because }
  1380. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1381. { using pos(char,pchar) will always call the shortstring version }
  1382. { (exact match for first argument), also with $h+ (JM) }
  1383. Function Pos (c : Char; Const s : UnicodeString) : SizeInt;
  1384. var
  1385. i: SizeInt;
  1386. wc : unicodechar;
  1387. pc : punicodechar;
  1388. begin
  1389. wc:=c;
  1390. pc:=@s[1];
  1391. for i:=1 to length(s) do
  1392. begin
  1393. if pc^=wc then
  1394. begin
  1395. pos:=i;
  1396. exit;
  1397. end;
  1398. inc(pc);
  1399. end;
  1400. pos:=0;
  1401. end;
  1402. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  1403. Var
  1404. LS : SizeInt;
  1405. begin
  1406. If Length(S)=0 then
  1407. exit;
  1408. if index<=0 then
  1409. exit;
  1410. LS:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len div sizeof(UnicodeChar);
  1411. if (Index<=LS) and (Size>0) then
  1412. begin
  1413. UniqueString (S);
  1414. if Size+Index>LS then
  1415. Size:=LS-Index+1;
  1416. if Index+Size<=LS then
  1417. begin
  1418. Dec(Index);
  1419. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1420. end;
  1421. Setlength(s,LS-Size);
  1422. end;
  1423. end;
  1424. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1425. var
  1426. Temp : UnicodeString;
  1427. LS : SizeInt;
  1428. begin
  1429. If Length(Source)=0 then
  1430. exit;
  1431. if index <= 0 then
  1432. index := 1;
  1433. Ls:=Length(S);
  1434. if index > LS then
  1435. index := LS+1;
  1436. Dec(Index);
  1437. Pointer(Temp) := NewUnicodeString(Length(Source)+LS);
  1438. SetLength(Temp,Length(Source)+LS);
  1439. If Index>0 then
  1440. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1441. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1442. If (LS-Index)>0 then
  1443. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1444. S:=Temp;
  1445. end;
  1446. Function UpCase(c:UnicodeChar):UnicodeChar;
  1447. var
  1448. s : UnicodeString;
  1449. begin
  1450. s:=c;
  1451. result:=widestringmanager.UpperUnicodeStringProc(s)[1];
  1452. end;
  1453. function UpCase(const s : UnicodeString) : UnicodeString;
  1454. begin
  1455. result:=widestringmanager.UpperUnicodeStringProc(s);
  1456. end;
  1457. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  1458. var
  1459. BufLen: SizeInt;
  1460. begin
  1461. SetLength(S,Len);
  1462. If (Buf<>Nil) and (Len>0) then
  1463. begin
  1464. BufLen := IndexWord(Buf^, Len+1, 0);
  1465. If (BufLen>0) and (BufLen < Len) then
  1466. Len := BufLen;
  1467. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1468. PUnicodeChar(Pointer(S)+Len*sizeof(UnicodeChar))^:=#0;
  1469. end;
  1470. end;
  1471. Procedure SetString (Out S : UnicodeString; Buf : PChar; Len : SizeInt);
  1472. var
  1473. BufLen: SizeInt;
  1474. begin
  1475. SetLength(S,Len);
  1476. If (Buf<>Nil) and (Len>0) then
  1477. begin
  1478. BufLen := IndexByte(Buf^, Len+1, 0);
  1479. If (BufLen>0) and (BufLen < Len) then
  1480. Len := BufLen;
  1481. widestringmanager.Ansi2UnicodeMoveProc(Buf,S,Len);
  1482. //PUnicodeChar(Pointer(S)+Len*sizeof(UnicodeChar))^:=#0;
  1483. end;
  1484. end;
  1485. {$ifndef FPUNONE}
  1486. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1487. Var
  1488. SS : String;
  1489. begin
  1490. fpc_Val_Real_UnicodeStr := 0;
  1491. if length(S) > 255 then
  1492. code := 256
  1493. else
  1494. begin
  1495. SS := S;
  1496. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1497. end;
  1498. end;
  1499. {$endif}
  1500. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1501. var ss:shortstring;
  1502. begin
  1503. if length(s)>255 then
  1504. code:=256
  1505. else
  1506. begin
  1507. ss:=s;
  1508. val(ss,fpc_val_enum_unicodestr,code);
  1509. end;
  1510. end;
  1511. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1512. Var
  1513. SS : String;
  1514. begin
  1515. if length(S) > 255 then
  1516. begin
  1517. fpc_Val_Currency_UnicodeStr:=0;
  1518. code := 256;
  1519. end
  1520. else
  1521. begin
  1522. SS := S;
  1523. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1524. end;
  1525. end;
  1526. Function fpc_Val_UInt_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_UNICODESTR']; compilerproc;
  1527. Var
  1528. SS : ShortString;
  1529. begin
  1530. fpc_Val_UInt_UnicodeStr := 0;
  1531. if length(S) > 255 then
  1532. code := 256
  1533. else
  1534. begin
  1535. SS := S;
  1536. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1537. end;
  1538. end;
  1539. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1540. Var
  1541. SS : ShortString;
  1542. begin
  1543. fpc_Val_SInt_UnicodeStr:=0;
  1544. if length(S)>255 then
  1545. code:=256
  1546. else
  1547. begin
  1548. SS := S;
  1549. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1550. end;
  1551. end;
  1552. {$ifndef CPU64}
  1553. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1554. Var
  1555. SS : ShortString;
  1556. begin
  1557. fpc_Val_qword_UnicodeStr:=0;
  1558. if length(S)>255 then
  1559. code:=256
  1560. else
  1561. begin
  1562. SS := S;
  1563. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1564. end;
  1565. end;
  1566. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1567. Var
  1568. SS : ShortString;
  1569. begin
  1570. fpc_Val_int64_UnicodeStr:=0;
  1571. if length(S)>255 then
  1572. code:=256
  1573. else
  1574. begin
  1575. SS := S;
  1576. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1577. end;
  1578. end;
  1579. {$endif CPU64}
  1580. {$ifndef FPUNONE}
  1581. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1582. var
  1583. ss : shortstring;
  1584. begin
  1585. str_real(len,fr,d,treal_type(rt),ss);
  1586. s:=ss;
  1587. end;
  1588. {$endif}
  1589. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1590. var ss:shortstring;
  1591. begin
  1592. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1593. s:=ss;
  1594. end;
  1595. {$ifdef FPC_HAS_STR_CURRENCY}
  1596. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1597. var
  1598. ss : shortstring;
  1599. begin
  1600. str(c:len:fr,ss);
  1601. s:=ss;
  1602. end;
  1603. {$endif FPC_HAS_STR_CURRENCY}
  1604. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1605. Var
  1606. SS : ShortString;
  1607. begin
  1608. Str (v:Len,SS);
  1609. S:=SS;
  1610. end;
  1611. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1612. Var
  1613. SS : ShortString;
  1614. begin
  1615. str(v:Len,SS);
  1616. S:=SS;
  1617. end;
  1618. {$ifndef CPU64}
  1619. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1620. Var
  1621. SS : ShortString;
  1622. begin
  1623. Str (v:Len,SS);
  1624. S:=SS;
  1625. end;
  1626. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1627. Var
  1628. SS : ShortString;
  1629. begin
  1630. str(v:Len,SS);
  1631. S:=SS;
  1632. end;
  1633. {$endif CPU64}
  1634. { converts an utf-16 code point or surrogate pair to utf-32 }
  1635. function utf16toutf32(const S: UnicodeString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1636. var
  1637. w: unicodechar;
  1638. begin
  1639. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1640. { are the same in UTF-32 }
  1641. w:=s[index];
  1642. if (w<=#$d7ff) or
  1643. (w>=#$e000) then
  1644. begin
  1645. result:=UCS4Char(w);
  1646. len:=1;
  1647. end
  1648. { valid surrogate pair? }
  1649. else if (w<=#$dbff) and
  1650. { w>=#$d7ff check not needed, checked above }
  1651. (index<length(s)) and
  1652. (s[index+1]>=#$dc00) and
  1653. (s[index+1]<=#$dfff) then
  1654. { convert the surrogate pair to UTF-32 }
  1655. begin
  1656. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1657. len:=2;
  1658. end
  1659. else
  1660. { invalid surrogate -> do nothing }
  1661. begin
  1662. result:=UCS4Char(w);
  1663. len:=1;
  1664. end;
  1665. end;
  1666. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1667. begin
  1668. if assigned(Source) then
  1669. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1670. else
  1671. Result:=0;
  1672. end;
  1673. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1674. var
  1675. i,j : SizeUInt;
  1676. w : word;
  1677. lw : longword;
  1678. len : longint;
  1679. begin
  1680. result:=0;
  1681. if source=nil then
  1682. exit;
  1683. i:=0;
  1684. j:=0;
  1685. if assigned(Dest) then
  1686. begin
  1687. while (i<SourceChars) and (j<MaxDestBytes) do
  1688. begin
  1689. w:=word(Source[i]);
  1690. case w of
  1691. 0..$7f:
  1692. begin
  1693. Dest[j]:=char(w);
  1694. inc(j);
  1695. end;
  1696. $80..$7ff:
  1697. begin
  1698. if j+1>=MaxDestBytes then
  1699. break;
  1700. Dest[j]:=char($c0 or (w shr 6));
  1701. Dest[j+1]:=char($80 or (w and $3f));
  1702. inc(j,2);
  1703. end;
  1704. $800..$d7ff,$e000..$ffff:
  1705. begin
  1706. if j+2>=MaxDestBytes then
  1707. break;
  1708. Dest[j]:=char($e0 or (w shr 12));
  1709. Dest[j+1]:=char($80 or ((w shr 6) and $3f));
  1710. Dest[j+2]:=char($80 or (w and $3f));
  1711. inc(j,3);
  1712. end;
  1713. $d800..$dbff:
  1714. {High Surrogates}
  1715. begin
  1716. if j+3>=MaxDestBytes then
  1717. break;
  1718. if (i<sourcechars-1) and
  1719. (word(Source[i+1]) >= $dc00) and
  1720. (word(Source[i+1]) <= $dfff) then
  1721. begin
  1722. lw:=longword(utf16toutf32(Source[i] + Source[i+1], 1, len));
  1723. Dest[j]:=char($f0 or (lw shr 18));
  1724. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1725. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1726. Dest[j+3]:=char($80 or (lw and $3f));
  1727. inc(j,4);
  1728. inc(i);
  1729. end;
  1730. end;
  1731. end;
  1732. inc(i);
  1733. end;
  1734. if j>SizeUInt(MaxDestBytes-1) then
  1735. j:=MaxDestBytes-1;
  1736. Dest[j]:=#0;
  1737. end
  1738. else
  1739. begin
  1740. while i<SourceChars do
  1741. begin
  1742. case word(Source[i]) of
  1743. $0..$7f:
  1744. inc(j);
  1745. $80..$7ff:
  1746. inc(j,2);
  1747. $800..$d7ff,$e000..$ffff:
  1748. inc(j,3);
  1749. $d800..$dbff:
  1750. begin
  1751. if (i<sourcechars-1) and
  1752. (word(Source[i+1]) >= $dc00) and
  1753. (word(Source[i+1]) <= $dfff) then
  1754. begin
  1755. inc(j,4);
  1756. inc(i);
  1757. end;
  1758. end;
  1759. end;
  1760. inc(i);
  1761. end;
  1762. end;
  1763. result:=j+1;
  1764. end;
  1765. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1766. begin
  1767. if assigned(Source) then
  1768. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1769. else
  1770. Result:=0;
  1771. end;
  1772. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1773. const
  1774. UNICODE_INVALID=63;
  1775. var
  1776. InputUTF8: SizeUInt;
  1777. IBYTE: BYTE;
  1778. OutputUnicode: SizeUInt;
  1779. PRECHAR: SizeUInt;
  1780. TempBYTE: BYTE;
  1781. CharLen: SizeUint;
  1782. LookAhead: SizeUInt;
  1783. UC: SizeUInt;
  1784. begin
  1785. if not assigned(Source) then
  1786. begin
  1787. result:=0;
  1788. exit;
  1789. end;
  1790. result:=SizeUInt(-1);
  1791. InputUTF8:=0;
  1792. OutputUnicode:=0;
  1793. PreChar:=0;
  1794. if Assigned(Dest) Then
  1795. begin
  1796. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1797. begin
  1798. IBYTE:=byte(Source[InputUTF8]);
  1799. if (IBYTE and $80) = 0 then
  1800. begin
  1801. //One character US-ASCII, convert it to unicode
  1802. if IBYTE = 10 then
  1803. begin
  1804. If (PreChar<>13) and FALSE then
  1805. begin
  1806. //Expand to crlf, conform UTF-8.
  1807. //This procedure will break the memory alocation by
  1808. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1809. if OutputUnicode+1<MaxDestChars then
  1810. begin
  1811. Dest[OutputUnicode]:=WideChar(13);
  1812. inc(OutputUnicode);
  1813. Dest[OutputUnicode]:=WideChar(10);
  1814. inc(OutputUnicode);
  1815. PreChar:=10;
  1816. end
  1817. else
  1818. begin
  1819. Dest[OutputUnicode]:=WideChar(13);
  1820. inc(OutputUnicode);
  1821. end;
  1822. end
  1823. else
  1824. begin
  1825. Dest[OutputUnicode]:=WideChar(IBYTE);
  1826. inc(OutputUnicode);
  1827. PreChar:=IBYTE;
  1828. end;
  1829. end
  1830. else
  1831. begin
  1832. Dest[OutputUnicode]:=WideChar(IBYTE);
  1833. inc(OutputUnicode);
  1834. PreChar:=IBYTE;
  1835. end;
  1836. inc(InputUTF8);
  1837. end
  1838. else
  1839. begin
  1840. TempByte:=IBYTE;
  1841. CharLen:=0;
  1842. while (TempBYTE and $80)<>0 do
  1843. begin
  1844. TempBYTE:=(TempBYTE shl 1) and $FE;
  1845. inc(CharLen);
  1846. end;
  1847. //Test for the "CharLen" conforms UTF-8 string
  1848. //This means the 10xxxxxx pattern.
  1849. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1850. begin
  1851. //Insuficient chars in string to decode
  1852. //UTF-8 array. Fallback to single char.
  1853. CharLen:= 1;
  1854. end;
  1855. for LookAhead := 1 to CharLen-1 do
  1856. begin
  1857. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1858. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1859. begin
  1860. //Invalid UTF-8 sequence, fallback.
  1861. CharLen:= LookAhead;
  1862. break;
  1863. end;
  1864. end;
  1865. UC:=$FFFF;
  1866. case CharLen of
  1867. 1: begin
  1868. //Not valid UTF-8 sequence
  1869. UC:=UNICODE_INVALID;
  1870. end;
  1871. 2: begin
  1872. //Two bytes UTF, convert it
  1873. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1874. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1875. if UC <= $7F then
  1876. begin
  1877. //Invalid UTF sequence.
  1878. UC:=UNICODE_INVALID;
  1879. end;
  1880. end;
  1881. 3: begin
  1882. //Three bytes, convert it to unicode
  1883. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1884. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1885. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1886. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1887. begin
  1888. //Invalid UTF-8 sequence
  1889. UC:= UNICODE_INVALID;
  1890. End;
  1891. end;
  1892. 4: begin
  1893. //Four bytes, convert it to two unicode characters
  1894. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1895. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1896. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1897. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1898. if (UC < $10000) or (UC > $10FFFF) then
  1899. begin
  1900. UC:= UNICODE_INVALID;
  1901. end
  1902. else
  1903. begin
  1904. { only store pair if room }
  1905. dec(UC,$10000);
  1906. if (OutputUnicode<MaxDestChars-1) then
  1907. begin
  1908. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1909. inc(OutputUnicode);
  1910. UC:=(UC and $3ff) + $DC00;
  1911. end
  1912. else
  1913. begin
  1914. InputUTF8:= InputUTF8 + CharLen;
  1915. { don't store anything }
  1916. CharLen:=0;
  1917. end;
  1918. end;
  1919. end;
  1920. 5,6,7: begin
  1921. //Invalid UTF8 to unicode conversion,
  1922. //mask it as invalid UNICODE too.
  1923. UC:=UNICODE_INVALID;
  1924. end;
  1925. end;
  1926. if CharLen > 0 then
  1927. begin
  1928. PreChar:=UC;
  1929. Dest[OutputUnicode]:=WideChar(UC);
  1930. inc(OutputUnicode);
  1931. end;
  1932. InputUTF8:= InputUTF8 + CharLen;
  1933. end;
  1934. end;
  1935. Result:=OutputUnicode+1;
  1936. end
  1937. else
  1938. begin
  1939. while (InputUTF8<SourceBytes) do
  1940. begin
  1941. IBYTE:=byte(Source[InputUTF8]);
  1942. if (IBYTE and $80) = 0 then
  1943. begin
  1944. //One character US-ASCII, convert it to unicode
  1945. if IBYTE = 10 then
  1946. begin
  1947. if (PreChar<>13) and FALSE then
  1948. begin
  1949. //Expand to crlf, conform UTF-8.
  1950. //This procedure will break the memory alocation by
  1951. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1952. inc(OutputUnicode,2);
  1953. PreChar:=10;
  1954. end
  1955. else
  1956. begin
  1957. inc(OutputUnicode);
  1958. PreChar:=IBYTE;
  1959. end;
  1960. end
  1961. else
  1962. begin
  1963. inc(OutputUnicode);
  1964. PreChar:=IBYTE;
  1965. end;
  1966. inc(InputUTF8);
  1967. end
  1968. else
  1969. begin
  1970. TempByte:=IBYTE;
  1971. CharLen:=0;
  1972. while (TempBYTE and $80)<>0 do
  1973. begin
  1974. TempBYTE:=(TempBYTE shl 1) and $FE;
  1975. inc(CharLen);
  1976. end;
  1977. //Test for the "CharLen" conforms UTF-8 string
  1978. //This means the 10xxxxxx pattern.
  1979. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1980. begin
  1981. //Insuficient chars in string to decode
  1982. //UTF-8 array. Fallback to single char.
  1983. CharLen:= 1;
  1984. end;
  1985. for LookAhead := 1 to CharLen-1 do
  1986. begin
  1987. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1988. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1989. begin
  1990. //Invalid UTF-8 sequence, fallback.
  1991. CharLen:= LookAhead;
  1992. break;
  1993. end;
  1994. end;
  1995. UC:=$FFFF;
  1996. case CharLen of
  1997. 1: begin
  1998. //Not valid UTF-8 sequence
  1999. UC:=UNICODE_INVALID;
  2000. end;
  2001. 2: begin
  2002. //Two bytes UTF, convert it
  2003. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  2004. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  2005. if UC <= $7F then
  2006. begin
  2007. //Invalid UTF sequence.
  2008. UC:=UNICODE_INVALID;
  2009. end;
  2010. end;
  2011. 3: begin
  2012. //Three bytes, convert it to unicode
  2013. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  2014. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  2015. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  2016. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  2017. begin
  2018. //Invalid UTF-8 sequence
  2019. UC:= UNICODE_INVALID;
  2020. end;
  2021. end;
  2022. 4: begin
  2023. //Four bytes, convert it to two unicode characters
  2024. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  2025. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  2026. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  2027. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  2028. if (UC < $10000) or (UC > $10FFFF) then
  2029. UC:= UNICODE_INVALID
  2030. else
  2031. { extra character character }
  2032. inc(OutputUnicode);
  2033. end;
  2034. 5,6,7: begin
  2035. //Invalid UTF8 to unicode conversion,
  2036. //mask it as invalid UNICODE too.
  2037. UC:=UNICODE_INVALID;
  2038. end;
  2039. end;
  2040. if CharLen > 0 then
  2041. begin
  2042. PreChar:=UC;
  2043. inc(OutputUnicode);
  2044. end;
  2045. InputUTF8:= InputUTF8 + CharLen;
  2046. end;
  2047. end;
  2048. Result:=OutputUnicode+1;
  2049. end;
  2050. end;
  2051. function UTF8Encode(const s : Ansistring) : UTF8String; inline;
  2052. begin
  2053. Result:=UTF8Encode(UnicodeString(s));
  2054. end;
  2055. function UTF8Encode(const s : UnicodeString) : UTF8String;
  2056. var
  2057. i : SizeInt;
  2058. hs : UTF8String;
  2059. begin
  2060. result:='';
  2061. if s='' then
  2062. exit;
  2063. SetLength(hs,length(s)*3);
  2064. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  2065. if i>0 then
  2066. begin
  2067. SetLength(hs,i-1);
  2068. result:=hs;
  2069. end;
  2070. end;
  2071. function UTF8Decode(const s : UTF8String): UnicodeString;
  2072. var
  2073. i : SizeInt;
  2074. hs : UnicodeString;
  2075. begin
  2076. result:='';
  2077. if s='' then
  2078. exit;
  2079. SetLength(hs,length(s));
  2080. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  2081. if i>0 then
  2082. begin
  2083. SetLength(hs,i-1);
  2084. result:=hs;
  2085. end;
  2086. end;
  2087. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  2088. begin
  2089. Result:=Utf8Encode(s);
  2090. end;
  2091. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  2092. begin
  2093. Result:=Utf8Decode(s);
  2094. end;
  2095. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  2096. var
  2097. i, slen,
  2098. destindex : SizeInt;
  2099. len : longint;
  2100. begin
  2101. slen:=length(s);
  2102. setlength(result,slen+1);
  2103. i:=1;
  2104. destindex:=0;
  2105. while (i<=slen) do
  2106. begin
  2107. result[destindex]:=utf16toutf32(s,i,len);
  2108. inc(destindex);
  2109. inc(i,len);
  2110. end;
  2111. { destindex <= slen (surrogate pairs may have been merged) }
  2112. { destindex+1 for terminating #0 (dynamic arrays are }
  2113. { implicitely filled with zero) }
  2114. setlength(result,destindex+1);
  2115. end;
  2116. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  2117. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: UnicodeString; var index: SizeInt);
  2118. var
  2119. p : PUnicodeChar;
  2120. begin
  2121. { if nc > $ffff, we need two places }
  2122. if (index+ord(nc > $ffff)>length(s)) then
  2123. if (length(s) < 10*256) then
  2124. setlength(s,length(s)+10)
  2125. else
  2126. setlength(s,length(s)+length(s) shr 8);
  2127. { we know that s is unique -> avoid uniquestring calls}
  2128. p:=@s[index];
  2129. if (nc<$ffff) then
  2130. begin
  2131. p^:=unicodechar(nc);
  2132. inc(index);
  2133. end
  2134. else if (dword(nc)<=$10ffff) then
  2135. begin
  2136. p^:=unicodechar((nc - $10000) shr 10 + $d800);
  2137. (p+1)^:=unicodechar((nc - $10000) and $3ff + $dc00);
  2138. inc(index,2);
  2139. end
  2140. else
  2141. { invalid code point }
  2142. begin
  2143. p^:='?';
  2144. inc(index);
  2145. end;
  2146. end;
  2147. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  2148. var
  2149. i : SizeInt;
  2150. resindex : SizeInt;
  2151. begin
  2152. { skip terminating #0 }
  2153. SetLength(result,length(s)-1);
  2154. resindex:=1;
  2155. for i:=0 to high(s)-1 do
  2156. ConcatUTF32ToUnicodeStr(s[i],result,resindex);
  2157. { adjust result length (may be too big due to growing }
  2158. { for surrogate pairs) }
  2159. setlength(result,resindex-1);
  2160. end;
  2161. function WideStringToUCS4String(const s : WideString) : UCS4String;
  2162. var
  2163. i, slen,
  2164. destindex : SizeInt;
  2165. len : longint;
  2166. begin
  2167. slen:=length(s);
  2168. setlength(result,slen+1);
  2169. i:=1;
  2170. destindex:=0;
  2171. while (i<=slen) do
  2172. begin
  2173. result[destindex]:=utf16toutf32(s,i,len);
  2174. inc(destindex);
  2175. inc(i,len);
  2176. end;
  2177. { destindex <= slen (surrogate pairs may have been merged) }
  2178. { destindex+1 for terminating #0 (dynamic arrays are }
  2179. { implicitely filled with zero) }
  2180. setlength(result,destindex+1);
  2181. end;
  2182. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  2183. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  2184. var
  2185. p : PWideChar;
  2186. begin
  2187. { if nc > $ffff, we need two places }
  2188. if (index+ord(nc > $ffff)>length(s)) then
  2189. if (length(s) < 10*256) then
  2190. setlength(s,length(s)+10)
  2191. else
  2192. setlength(s,length(s)+length(s) shr 8);
  2193. { we know that s is unique -> avoid uniquestring calls}
  2194. p:=@s[index];
  2195. if (nc<$ffff) then
  2196. begin
  2197. p^:=widechar(nc);
  2198. inc(index);
  2199. end
  2200. else if (dword(nc)<=$10ffff) then
  2201. begin
  2202. p^:=widechar((nc - $10000) shr 10 + $d800);
  2203. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  2204. inc(index,2);
  2205. end
  2206. else
  2207. { invalid code point }
  2208. begin
  2209. p^:='?';
  2210. inc(index);
  2211. end;
  2212. end;
  2213. function UCS4StringToWideString(const s : UCS4String) : WideString;
  2214. var
  2215. i : SizeInt;
  2216. resindex : SizeInt;
  2217. begin
  2218. { skip terminating #0 }
  2219. SetLength(result,length(s)-1);
  2220. resindex:=1;
  2221. for i:=0 to high(s)-1 do
  2222. ConcatUTF32ToWideStr(s[i],result,resindex);
  2223. { adjust result length (may be too big due to growing }
  2224. { for surrogate pairs) }
  2225. setlength(result,resindex-1);
  2226. end;
  2227. const
  2228. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  2229. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  2230. procedure unimplementedunicodestring;
  2231. begin
  2232. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  2233. If IsConsole then
  2234. begin
  2235. Writeln(StdErr,SNoUnicodestrings);
  2236. Writeln(StdErr,SRecompileWithUnicodestrings);
  2237. end;
  2238. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  2239. HandleErrorFrame(233,get_frame);
  2240. end;
  2241. {$warnings off}
  2242. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  2243. begin
  2244. unimplementedunicodestring;
  2245. end;
  2246. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  2247. begin
  2248. unimplementedunicodestring;
  2249. end;
  2250. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  2251. begin
  2252. unimplementedunicodestring;
  2253. end;
  2254. function CharLengthPChar(const Str: PChar): PtrInt;
  2255. begin
  2256. unimplementedunicodestring;
  2257. end;
  2258. {$warnings on}
  2259. procedure initunicodestringmanager;
  2260. begin
  2261. {$ifndef HAS_WIDESTRINGMANAGER}
  2262. widestringmanager.Unicode2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2263. widestringmanager.Ansi2UnicodeMoveProc:=@defaultAnsi2UnicodeMove;
  2264. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  2265. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  2266. {$endif HAS_WIDESTRINGMANAGER}
  2267. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  2268. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  2269. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2270. {$ifndef HAS_WIDESTRINGMANAGER}
  2271. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2272. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2273. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  2274. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  2275. {$endif HAS_WIDESTRINGMANAGER}
  2276. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  2277. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  2278. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  2279. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2280. end;