ustrings.inc 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  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. {$ifndef FPC_UNICODESTRING_TYPE_DEFINED}
  14. {
  15. This file contains the implementation of the UnicodeString type,
  16. and all things that are needed for it.
  17. UnicodeString is defined as a 'silent' punicodechar :
  18. a punicodechar that points to :
  19. @-8 : SizeInt for reference count;
  20. @-4 : SizeInt for size; size=number of chars. Multiply with
  21. sizeof(UnicodeChar) to get the number of bytes. This is compatible with Delphi.
  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 = Record
  31. CodePage : TSystemCodePage;
  32. ElementSize : Word;
  33. {$ifdef CPU64}
  34. { align fields }
  35. Dummy : DWord;
  36. {$endif CPU64}
  37. Ref : SizeInt;
  38. Len : SizeInt;
  39. end;
  40. Const
  41. UnicodeFirstOff = SizeOf(TUnicodeRec);
  42. {$endif FPC_UNICODESTRING_TYPE_DEFINED}
  43. {
  44. Default UnicodeChar <-> Char conversion is to only convert the
  45. lower 127 chars, all others are translated to '?'.
  46. These routines can be overridden for the Current Locale
  47. }
  48. {$ifndef FPC_HAS_DEFAULT_UNICODE_2_ANSI_MOVE}
  49. {$define FPC_HAS_DEFAULT_UNICODE_2_ANSI_MOVE}
  50. procedure DefaultUnicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  51. var
  52. i : SizeInt;
  53. p : PAnsiChar;
  54. begin
  55. setlength(dest,len);
  56. if not assigned(pointer(dest)) then
  57. exit;
  58. PAnsiRec(dest)^.CodePage:=cp;
  59. p:=pointer(dest); {SetLength guarantees that dest is unique}
  60. for i:=1 to len do
  61. begin
  62. if word(source^)<256 then
  63. p^:=char(word(source^))
  64. else
  65. p^:='?';
  66. inc(source);
  67. inc(p);
  68. end;
  69. end;
  70. {$endif FPC_HAS_DEFAULT_UNICODE_2_ANSI_MOVE}
  71. {$ifndef FPC_HAS_DEFAULT_ANSI_2_UNICODE}
  72. {$define FPC_HAS_DEFAULT_ANSI_2_UNICODE}
  73. procedure DefaultAnsi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
  74. var
  75. i : SizeInt;
  76. p : PUnicodeChar;
  77. begin
  78. setlength(dest,len);
  79. p:=pointer(dest); {SetLength guarantees that dest is unique}
  80. for i:=1 to len do
  81. begin
  82. p^:=unicodechar(byte(source^));
  83. inc(source);
  84. inc(p);
  85. end;
  86. end;
  87. {$endif FPC_HAS_DEFAULT_ANSI_2_UNICODE}
  88. {$ifndef FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  89. function DefaultCharLengthPChar(const Str: PChar): PtrInt;
  90. begin
  91. DefaultCharLengthPChar:=length(Str);
  92. end;
  93. function DefaultCodePointLength(const Str: PChar; MaxLookAead: PtrInt): Ptrint;
  94. begin
  95. if str[0]<>#0 then
  96. DefaultCodePointLength:=1
  97. else
  98. DefaultCodePointLength:=0;
  99. end;
  100. {$endif FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  101. function DefaultGetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
  102. begin
  103. { don't raise an exception here. We need this for text file handling }
  104. Result:=DefaultSystemCodePage;
  105. end;
  106. Procedure GetUnicodeStringManager (Var Manager : TUnicodeStringManager);
  107. begin
  108. manager:=widestringmanager;
  109. end;
  110. Procedure SetUnicodeStringManager (Const New : TUnicodeStringManager; Var Old: TUnicodeStringManager);
  111. begin
  112. Old:=widestringmanager;
  113. widestringmanager:=New;
  114. end;
  115. Procedure SetUnicodeStringManager (Const New : TUnicodeStringManager);
  116. begin
  117. widestringmanager:=New;
  118. end;
  119. Procedure GetWideStringManager (Var Manager : TUnicodeStringManager);
  120. begin
  121. manager:=widestringmanager;
  122. end;
  123. Procedure SetWideStringManager (Const New : TUnicodeStringManager; Var Old: TUnicodeStringManager);
  124. begin
  125. Old:=widestringmanager;
  126. widestringmanager:=New;
  127. end;
  128. Procedure SetWideStringManager (Const New : TUnicodeStringManager);
  129. begin
  130. widestringmanager:=New;
  131. end;
  132. {****************************************************************************
  133. Internal functions, not in interface.
  134. ****************************************************************************}
  135. procedure UnicodeStringError;
  136. begin
  137. HandleErrorAddrFrameInd(204,get_pc_addr,get_frame);
  138. end;
  139. {$ifndef FPC_HAS_NEW_UNICODESTRING}
  140. {$define FPC_HAS_NEW_UNICODESTRING}
  141. Function NewUnicodeString(Len : SizeInt) : Pointer;
  142. {
  143. Allocate a new UnicodeString on the heap.
  144. initialize it to zero length and reference count 1.
  145. }
  146. Var
  147. P : Pointer;
  148. begin
  149. GetMem(P,Len*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar)));
  150. If P<>Nil then
  151. begin
  152. PUnicodeRec(P)^.Len:=Len; { Initial length }
  153. PUnicodeRec(P)^.Ref:=1; { Initial Refcount }
  154. PUnicodeRec(P)^.CodePage:=DefaultUnicodeCodePage;
  155. PUnicodeRec(P)^.ElementSize:=SizeOf(UnicodeChar);
  156. inc(p,UnicodeFirstOff); { Points to string now }
  157. PUnicodeChar(P)^:=#0; { Terminating #0 }
  158. end
  159. else
  160. UnicodeStringError;
  161. NewUnicodeString:=P;
  162. end;
  163. {$endif FPC_HAS_NEW_UNICODESTRING}
  164. {$ifndef FPC_HAS_UNICODESTR_DECR_REF}
  165. {$define FPC_HAS_UNICODESTR_DECR_REF}
  166. Procedure fpc_UnicodeStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_UNICODESTR_DECR_REF']; compilerproc;
  167. {
  168. Decreases the ReferenceCount of a non constant unicodestring;
  169. If the reference count is zero, deallocate the string;
  170. }
  171. Var
  172. p: PUnicodeRec;
  173. Begin
  174. { Zero string }
  175. if S=Nil then
  176. exit;
  177. { check for constant strings ...}
  178. p:=PUnicodeRec(S-UnicodeFirstOff);
  179. S:=nil;
  180. if p^.Ref<0 then
  181. exit;
  182. { declocked does a MT safe dec and returns true, if the counter is 0 }
  183. if declocked(p^.Ref) then
  184. FreeMem(p);
  185. end;
  186. { alias for internal use }
  187. Procedure fpc_UnicodeStr_Decr_Ref (Var S : Pointer);[external name 'FPC_UNICODESTR_DECR_REF'];
  188. {$endif FPC_HAS_UNICODESTR_DECR_REF}
  189. {$ifndef FPC_HAS_UNICODESTR_INCR_REF}
  190. {$define FPC_HAS_UNICODESTR_INCR_REF}
  191. Procedure fpc_UnicodeStr_Incr_Ref(S : Pointer);[Public,Alias:'FPC_UNICODESTR_INCR_REF']; compilerproc;
  192. Begin
  193. If S=Nil then
  194. exit;
  195. { constant string ? }
  196. If PUnicodeRec(S-UnicodeFirstOff)^.Ref<0 then
  197. exit;
  198. inclocked(PUnicodeRec(S-UnicodeFirstOff)^.Ref);
  199. end;
  200. { alias for internal use }
  201. Procedure fpc_UnicodeStr_Incr_Ref (S : Pointer);[external name 'FPC_UNICODESTR_INCR_REF'];
  202. {$endif FPC_HAS_UNICODESTR_INCR_REF}
  203. {$ifndef FPC_HAS_UNICODESTR_TO_SHORTSTR}
  204. {$define FPC_HAS_UNICODESTR_TO_SHORTSTR}
  205. procedure fpc_UnicodeStr_To_ShortStr (out res: ShortString;const S2 : UnicodeString); [Public, alias: 'FPC_UNICODESTR_TO_SHORTSTR'];compilerproc;
  206. {
  207. Converts a UnicodeString to a ShortString;
  208. }
  209. Var
  210. Size : SizeInt;
  211. temp : ansistring;
  212. begin
  213. res:='';
  214. Size:=Length(S2);
  215. if Size>0 then
  216. begin
  217. If Size>high(res) then
  218. Size:=high(res);
  219. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(S2),temp,DefaultSystemCodePage,Size);
  220. res:=temp;
  221. end;
  222. end;
  223. {$endif FPC_HAS_UNICODESTR_TO_SHORTSTR}
  224. {$ifndef FPC_HAS_SHORTSTR_TO_UNICODESTR}
  225. {$define FPC_HAS_SHORTSTR_TO_UNICODESTR}
  226. Function fpc_ShortStr_To_UnicodeStr (Const S2 : ShortString): UnicodeString;compilerproc;
  227. {
  228. Converts a ShortString to a UnicodeString;
  229. }
  230. Var
  231. Size : SizeInt;
  232. begin
  233. result:='';
  234. Size:=Length(S2);
  235. if Size>0 then
  236. widestringmanager.Ansi2UnicodeMoveProc(PChar(@S2[1]),DefaultSystemCodePage,result,Size);
  237. end;
  238. {$endif FPC_HAS_SHORTSTR_TO_UNICODESTR}
  239. {$ifndef FPC_HAS_UNICODESTR_TO_ANSISTR}
  240. {$define FPC_HAS_UNICODESTR_TO_ANSISTR}
  241. Function fpc_UnicodeStr_To_AnsiStr (const S2 : UnicodeString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  242. {
  243. Converts a UnicodeString to an AnsiString
  244. }
  245. Var
  246. Size : SizeInt;
  247. {$ifndef FPC_HAS_CPSTRING}
  248. cp : TSystemCodePage;
  249. {$endif FPC_HAS_CPSTRING}
  250. begin
  251. {$ifndef FPC_HAS_CPSTRING}
  252. cp:=DefaultSystemCodePage;
  253. {$endif FPC_HAS_CPSTRING}
  254. result:='';
  255. Size:=Length(S2);
  256. if Size>0 then
  257. begin
  258. if (cp=CP_ACP) then
  259. cp:=DefaultSystemCodePage;
  260. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(S2)),result,cp,Size);
  261. end;
  262. end;
  263. {$endif FPC_HAS_UNICODESTR_TO_ANSISTR}
  264. {$ifndef FPC_HAS_ANSISTR_TO_UNICODESTR}
  265. {$define FPC_HAS_ANSISTR_TO_UNICODESTR}
  266. Function fpc_AnsiStr_To_UnicodeStr (Const S2 : RawByteString): UnicodeString; compilerproc;
  267. {
  268. Converts an AnsiString to a UnicodeString;
  269. }
  270. Var
  271. Size : SizeInt;
  272. cp: TSystemCodePage;
  273. begin
  274. result:='';
  275. Size:=Length(S2);
  276. if Size>0 then
  277. begin
  278. cp:=StringCodePage(S2);
  279. if (cp=CP_ACP) then
  280. cp:=DefaultSystemCodePage;
  281. widestringmanager.Ansi2UnicodeMoveProc(PChar(S2),cp,result,Size);
  282. end;
  283. end;
  284. {$endif FPC_HAS_ANSISTR_TO_UNICODESTR}
  285. {$ifndef FPC_HAS_UNICODESTR_TO_WIDESTR}
  286. {$define FPC_HAS_UNICODESTR_TO_WIDESTR}
  287. Function fpc_UnicodeStr_To_WideStr (const S2 : UnicodeString): WideString; compilerproc;
  288. begin
  289. SetLength(Result,Length(S2));
  290. Move(pointer(S2)^,Pointer(Result)^,Length(S2)*sizeof(WideChar));
  291. end;
  292. {$endif FPC_HAS_UNICODESTR_TO_WIDESTR}
  293. {$ifndef FPC_HAS_WIDESTR_TO_UNICODESTR}
  294. {$define FPC_HAS_WIDESTR_TO_UNICODESTR}
  295. Function fpc_WideStr_To_UnicodeStr (Const S2 : WideString): UnicodeString; compilerproc;
  296. begin
  297. SetLength(Result,Length(S2));
  298. Move(pointer(S2)^,Pointer(Result)^,Length(S2)*sizeof(WideChar));
  299. end;
  300. {$endif FPC_HAS_WIDESTR_TO_UNICODESTR}
  301. {$ifndef FPC_HAS_PWIDECHAR_TO_UNICODESTR}
  302. {$define FPC_HAS_PWIDECHAR_TO_UNICODESTR}
  303. Function fpc_PWideChar_To_UnicodeStr(const p : pwidechar): unicodestring; compilerproc;
  304. var
  305. Size : SizeInt;
  306. begin
  307. result:='';
  308. if p=nil then
  309. exit;
  310. Size := IndexWord(p^, -1, 0);
  311. Setlength(result,Size);
  312. if Size>0 then
  313. Move(p^,PUnicodeChar(Pointer(result))^,Size*sizeof(UnicodeChar));
  314. end;
  315. {$endif FPC_HAS_PWIDECHAR_TO_UNICODESTR}
  316. {$ifndef FPC_HAS_PWIDECHAR_TO_ANSISTR}
  317. {$define FPC_HAS_PWIDECHAR_TO_ANSISTR}
  318. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): ansistring; compilerproc;
  319. var
  320. Size : SizeInt;
  321. {$ifndef FPC_HAS_CPSTRING}
  322. cp : TSystemCodePage;
  323. {$endif FPC_HAS_CPSTRING}
  324. begin
  325. {$ifndef FPC_HAS_CPSTRING}
  326. cp:=DefaultSystemCodePage;
  327. {$endif FPC_HAS_CPSTRING}
  328. result:='';
  329. if p=nil then
  330. exit;
  331. Size := IndexWord(p^, -1, 0);
  332. if Size>0 then
  333. widestringmanager.Wide2AnsiMoveProc(P,result,cp,Size);
  334. end;
  335. {$endif FPC_HAS_PWIDECHAR_TO_ANSISTR}
  336. {$ifndef FPC_HAS_PWIDECHAR_TO_SHORTSTR}
  337. {$define FPC_HAS_PWIDECHAR_TO_SHORTSTR}
  338. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  339. var
  340. Size : SizeInt;
  341. temp: ansistring;
  342. begin
  343. res:='';
  344. if p=nil then
  345. exit;
  346. Size:=IndexWord(p^, high(PtrInt), 0);
  347. if Size>0 then
  348. begin
  349. widestringmanager.Wide2AnsiMoveProc(p,temp,DefaultSystemCodePage,Size);
  350. res:=temp;
  351. end;
  352. end;
  353. {$endif FPC_HAS_PWIDECHAR_TO_SHORTSTR}
  354. {$ifndef FPC_HAS_UNICODESTR_ASSIGN}
  355. {$define FPC_UNICODESTR_ASSIGN}
  356. { checked against the ansistring routine, 2001-05-27 (FK) }
  357. Procedure fpc_UnicodeStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_UNICODESTR_ASSIGN']; compilerproc;
  358. {
  359. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  360. }
  361. begin
  362. If S2<>nil then
  363. If PUnicodeRec(S2-UnicodeFirstOff)^.Ref>0 then
  364. inclocked(PUnicodeRec(S2-UnicodeFirstOff)^.ref);
  365. { Decrease the reference count on the old S1 }
  366. fpc_unicodestr_decr_ref (S1);
  367. s1:=s2;
  368. end;
  369. { alias for internal use }
  370. Procedure fpc_UnicodeStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_UNICODESTR_ASSIGN'];
  371. {$endif FPC_UNICODESTR_ASSIGN}
  372. {$ifndef FPC_HAS_UNICODESTR_CONCAT}
  373. {$define FPC_HAS_UNICODESTR_CONCAT}
  374. procedure fpc_UnicodeStr_Concat (var DestS:Unicodestring;const S1,S2 : UnicodeString); compilerproc;
  375. Var
  376. Size,Location : SizeInt;
  377. same : boolean;
  378. begin
  379. { only assign if s1 or s2 is empty }
  380. if (S1='') then
  381. begin
  382. DestS:=s2;
  383. exit;
  384. end;
  385. if (S2='') then
  386. begin
  387. DestS:=s1;
  388. exit;
  389. end;
  390. Location:=Length(S1);
  391. Size:=length(S2);
  392. { Use Pointer() typecasts to prevent extra conversion code }
  393. if Pointer(DestS)=Pointer(S1) then
  394. begin
  395. same:=Pointer(S1)=Pointer(S2);
  396. SetLength(DestS,Size+Location);
  397. if same then
  398. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size)*sizeof(UnicodeChar))
  399. else
  400. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  401. end
  402. else if Pointer(DestS)=Pointer(S2) then
  403. begin
  404. SetLength(DestS,Size+Location);
  405. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  406. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(UnicodeChar));
  407. end
  408. else
  409. begin
  410. DestS:='';
  411. SetLength(DestS,Size+Location);
  412. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(UnicodeChar));
  413. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(UnicodeChar))^,(Size+1)*sizeof(UnicodeChar));
  414. end;
  415. end;
  416. {$endif FPC_HAS_UNICODESTR_CONCAT}
  417. {$ifndef FPC_HAS_UNICODESTR_CONCAT_MULTI}
  418. {$define FPC_HAS_UNICODESTR_CONCAT_MULTI}
  419. procedure fpc_UnicodeStr_Concat_multi (var DestS:Unicodestring;const sarr:array of Unicodestring); compilerproc;
  420. Var
  421. i : Longint;
  422. p,pc : pointer;
  423. Size,NewLen : SizeInt;
  424. lowstart : longint;
  425. destcopy : pointer;
  426. OldDestLen : SizeInt;
  427. begin
  428. if high(sarr)=0 then
  429. begin
  430. DestS:='';
  431. exit;
  432. end;
  433. destcopy:=nil;
  434. lowstart:=low(sarr);
  435. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  436. inc(lowstart);
  437. { Check for another reuse, then we can't use
  438. the append optimization }
  439. for i:=lowstart to high(sarr) do
  440. begin
  441. if Pointer(DestS)=Pointer(sarr[i]) then
  442. begin
  443. { if DestS is used somewhere in the middle of the expression,
  444. we need to make sure the original string still exists after
  445. we empty/modify DestS.
  446. This trick only works with reference counted strings. Therefor
  447. this optimization is disabled for WINLIKEUNICODESTRING }
  448. destcopy:=pointer(dests);
  449. fpc_UnicodeStr_Incr_Ref(destcopy);
  450. lowstart:=low(sarr);
  451. break;
  452. end;
  453. end;
  454. { Start with empty DestS if we start with concatting
  455. the first array element }
  456. if lowstart=low(sarr) then
  457. DestS:='';
  458. OldDestLen:=length(DestS);
  459. { Calculate size of the result so we can do
  460. a single call to SetLength() }
  461. NewLen:=0;
  462. for i:=low(sarr) to high(sarr) do
  463. inc(NewLen,length(sarr[i]));
  464. SetLength(DestS,NewLen);
  465. { Concat all strings, except the string we already
  466. copied in DestS }
  467. pc:=Pointer(DestS)+OldDestLen*sizeof(UnicodeChar);
  468. for i:=lowstart to high(sarr) do
  469. begin
  470. p:=pointer(sarr[i]);
  471. if assigned(p) then
  472. begin
  473. Size:=length(unicodestring(p));
  474. Move(p^,pc^,(Size+1)*sizeof(UnicodeChar));
  475. inc(pc,size*sizeof(UnicodeChar));
  476. end;
  477. end;
  478. fpc_UnicodeStr_Decr_Ref(destcopy);
  479. end;
  480. {$endif FPC_HAS_UNICODESTR_CONCAT_MULTI}
  481. {$ifndef FPC_HAS_CHAR_TO_UCHAR}
  482. {$define FPC_HAS_CHAR_TO_UCHAR}
  483. Function fpc_Char_To_UChar(const c : Char): UnicodeChar; compilerproc;
  484. var
  485. w: unicodestring;
  486. begin
  487. widestringmanager.Ansi2UnicodeMoveProc(@c,DefaultSystemCodePage,w,1);
  488. fpc_Char_To_UChar:=w[1];
  489. end;
  490. {$endif FPC_HAS_CHAR_TO_UCHAR}
  491. {$ifndef FPC_HAS_CHAR_TO_UNICODESTR}
  492. {$define FPC_HAS_CHAR_TO_UNICODESTR}
  493. Function fpc_Char_To_UnicodeStr(const c : Char): UnicodeString; compilerproc;
  494. {
  495. Converts a Char to a UnicodeString;
  496. }
  497. begin
  498. widestringmanager.Ansi2UnicodeMoveProc(@c,DefaultSystemCodePage,result,1);
  499. end;
  500. {$endif FPC_HAS_CHAR_TO_UNICODESTR}
  501. {$ifndef FPC_HAS_UCHAR_TO_CHAR}
  502. {$define FPC_HAS_UCHAR_TO_CHAR}
  503. Function fpc_UChar_To_Char(const c : UnicodeChar): Char; compilerproc;
  504. {
  505. Converts a UnicodeChar to a Char;
  506. }
  507. var
  508. s: ansistring;
  509. begin
  510. widestringmanager.Unicode2AnsiMoveProc(@c, s, DefaultSystemCodePage, 1);
  511. if length(s)=1 then
  512. fpc_UChar_To_Char:= s[1]
  513. else
  514. fpc_UChar_To_Char:='?';
  515. end;
  516. {$endif FPC_HAS_UCHAR_TO_CHAR}
  517. {$ifndef FPC_HAS_UCHAR_TO_SHORTSTR}
  518. {$define FPC_HAS_UCHAR_TO_SHORTSTR}
  519. procedure fpc_UChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  520. {
  521. Converts a WideChar to a ShortString;
  522. }
  523. var
  524. s: ansistring;
  525. begin
  526. widestringmanager.Wide2AnsiMoveProc(@c,s,DefaultSystemCodePage,1);
  527. res:=s;
  528. end;
  529. {$endif FPC_HAS_UCHAR_TO_SHORTSTR}
  530. {$ifndef FPC_HAS_UCHAR_TO_UNICODESTR}
  531. {$define FPC_HAS_UCHAR_TO_UNICODESTR}
  532. Function fpc_UChar_To_UnicodeStr(const c : UnicodeChar): UnicodeString; compilerproc;
  533. {
  534. Converts a UnicodeChar to a UnicodeString;
  535. }
  536. begin
  537. Setlength (fpc_UChar_To_UnicodeStr,1);
  538. fpc_UChar_To_UnicodeStr[1]:= c;
  539. end;
  540. {$endif FPC_HAS_UCHAR_TO_UNICODESTR}
  541. {$ifndef FPC_HAS_UCHAR_TO_ANSISTR}
  542. {$define FPC_HAS_UCHAR_TO_ANSISTR}
  543. Function fpc_UChar_To_AnsiStr(const c : UnicodeChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  544. {
  545. Converts a UnicodeChar to a AnsiString;
  546. }
  547. {$ifndef FPC_HAS_CPSTRING}
  548. var
  549. cp : TSystemCodePage;
  550. {$endif FPC_HAS_CPSTRING}
  551. begin
  552. {$ifndef FPC_HAS_CPSTRING}
  553. cp:=DefaultSystemCodePage;
  554. {$endif FPC_HAS_CPSTRING}
  555. if (cp=CP_ACP) then
  556. cp:=DefaultSystemCodePage;
  557. widestringmanager.Unicode2AnsiMoveProc(@c, fpc_UChar_To_AnsiStr, cp, 1);
  558. end;
  559. {$endif FPC_HAS_UCHAR_TO_ANSISTR}
  560. {$ifndef FPC_HAS_PCHAR_TO_UNICODESTR}
  561. {$define FPC_HAS_PCHAR_TO_UNICODESTR}
  562. Function fpc_PChar_To_UnicodeStr(const p : pchar): UnicodeString; compilerproc;
  563. Var
  564. L : SizeInt;
  565. begin
  566. if (not assigned(p)) or (p[0]=#0) Then
  567. begin
  568. fpc_pchar_to_unicodestr := '';
  569. exit;
  570. end;
  571. l:=IndexChar(p^,-1,#0);
  572. widestringmanager.Ansi2UnicodeMoveProc(P,DefaultSystemCodePage,fpc_PChar_To_UnicodeStr,l);
  573. end;
  574. {$endif FPC_HAS_PCHAR_TO_UNICODESTR}
  575. {$ifndef FPC_HAS_CHARARRAY_TO_UNICODESTR}
  576. {$define FPC_HAS_CHARARRAY_TO_UNICODESTR}
  577. Function fpc_CharArray_To_UnicodeStr(const arr: array of char; zerobased: boolean = true): UnicodeString; compilerproc;
  578. var
  579. i : SizeInt;
  580. begin
  581. if zerobased then
  582. begin
  583. if arr[0]=#0 Then
  584. begin
  585. fpc_chararray_to_unicodestr:='';
  586. exit;
  587. end;
  588. i:=IndexChar(arr,high(arr)+1,#0);
  589. if i=-1 then
  590. i:=high(arr)+1;
  591. end
  592. else
  593. i:=high(arr)+1;
  594. widestringmanager.Ansi2UnicodeMoveProc(pchar(@arr),DefaultSystemCodePage,fpc_CharArray_To_UnicodeStr,i);
  595. end;
  596. {$endif FPC_HAS_CHARARRAY_TO_UNICODESTR}
  597. {$ifndef FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  598. {$define FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  599. Function fpc_WideCharArray_To_UnicodeStr(const arr: array of widechar; zerobased: boolean = true): UnicodeString; compilerproc;
  600. var
  601. i : SizeInt;
  602. begin
  603. if (zerobased) then
  604. begin
  605. i:=IndexWord(arr,high(arr)+1,0);
  606. if i = -1 then
  607. i := high(arr)+1;
  608. end
  609. else
  610. i := high(arr)+1;
  611. SetLength(fpc_WideCharArray_To_UnicodeStr,i);
  612. Move(arr[0], Pointer(fpc_WideCharArray_To_UnicodeStr)^,i*sizeof(WideChar));
  613. end;
  614. {$endif FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  615. {$ifndef FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  616. {$define FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  617. { due to their names, the following procedures should be in wstrings.inc,
  618. however, the compiler generates code using this functions on all platforms }
  619. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  620. var
  621. l: longint;
  622. index: ptrint;
  623. len: byte;
  624. temp: ansistring;
  625. begin
  626. l := high(arr)+1;
  627. if l>=high(res)+1 then
  628. l:=high(res)
  629. else if l<0 then
  630. l:=0;
  631. if zerobased then
  632. begin
  633. index:=IndexWord(arr[0],l,0);
  634. if index<0 then
  635. len:=l
  636. else
  637. len:=index;
  638. end
  639. else
  640. len:=l;
  641. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,DefaultSystemCodePage,len);
  642. res:=temp;
  643. end;
  644. {$endif FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  645. {$ifndef FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  646. {$define FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  647. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; {$ifdef FPC_HAS_CPSTRING}cp : TSystemCodePage;{$endif FPC_HAS_CPSTRING} zerobased: boolean = true): AnsiString; compilerproc;
  648. var
  649. i : SizeInt;
  650. {$ifndef FPC_HAS_CPSTRING}
  651. cp : TSystemCodePage;
  652. {$endif FPC_HAS_CPSTRING}
  653. begin
  654. {$ifndef FPC_HAS_CPSTRING}
  655. cp:=DefaultSystemCodePage;
  656. {$endif FPC_HAS_CPSTRING}
  657. if (zerobased) then
  658. begin
  659. i:=IndexWord(arr,high(arr)+1,0);
  660. if i = -1 then
  661. i := high(arr)+1;
  662. end
  663. else
  664. i := high(arr)+1;
  665. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),RawByteString(fpc_WideCharArray_To_AnsiStr),cp,i);
  666. end;
  667. {$endif FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  668. {$ifndef FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  669. {$define FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  670. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  671. var
  672. i : SizeInt;
  673. begin
  674. if (zerobased) then
  675. begin
  676. i:=IndexWord(arr,high(arr)+1,0);
  677. if i = -1 then
  678. i := high(arr)+1;
  679. end
  680. else
  681. i := high(arr)+1;
  682. SetLength(fpc_WideCharArray_To_WideStr,i);
  683. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  684. end;
  685. {$endif FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  686. {$ifndef FPC_HAS_UNICODESTR_TO_CHARARRAY}
  687. {$define FPC_HAS_UNICODESTR_TO_CHARARRAY}
  688. procedure fpc_unicodestr_to_chararray(out res: array of char; const src: UnicodeString); compilerproc;
  689. var
  690. len: SizeInt;
  691. temp: ansistring;
  692. begin
  693. len := length(src);
  694. { make sure we don't dereference src if it can be nil (JM) }
  695. if len > 0 then
  696. widestringmanager.unicode2ansimoveproc(punicodechar(@src[1]),temp,DefaultSystemCodePage,len);
  697. len := length(temp);
  698. if len > length(res) then
  699. len := length(res);
  700. {$push}
  701. {$r-}
  702. move(temp[1],res[0],len);
  703. fillchar(res[len],length(res)-len,0);
  704. {$pop}
  705. end;
  706. {$endif FPC_HAS_UNICODESTR_TO_UNICODECHARARRAY}
  707. {$ifndef FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  708. {$define FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  709. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: RawByteString); compilerproc;
  710. var
  711. len: SizeInt;
  712. temp: widestring;
  713. begin
  714. len := length(src);
  715. { make sure we don't dereference src if it can be nil (JM) }
  716. if len > 0 then
  717. widestringmanager.ansi2widemoveproc(pchar(@src[1]),StringCodePage(src),temp,len);
  718. len := length(temp);
  719. if len > length(res) then
  720. len := length(res);
  721. {$push}
  722. {$r-}
  723. move(temp[1],res[0],len*sizeof(widechar));
  724. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  725. {$pop}
  726. end;
  727. {$endif FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  728. {$ifndef FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  729. {$define FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  730. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  731. var
  732. len: longint;
  733. temp : widestring;
  734. begin
  735. len := length(src);
  736. { make sure we don't access char 1 if length is 0 (JM) }
  737. if len > 0 then
  738. widestringmanager.ansi2widemoveproc(pchar(@src[1]),DefaultSystemCodePage,temp,len);
  739. len := length(temp);
  740. if len > length(res) then
  741. len := length(res);
  742. {$push}
  743. {$r-}
  744. move(temp[1],res[0],len*sizeof(widechar));
  745. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  746. {$pop}
  747. end;
  748. {$endif FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  749. {$ifndef FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  750. {$define FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  751. procedure fpc_unicodestr_to_widechararray(out res: array of widechar; const src: UnicodeString); compilerproc;
  752. var
  753. len: SizeInt;
  754. begin
  755. len := length(src);
  756. if len > length(res) then
  757. len := length(res);
  758. {$push}
  759. {$r-}
  760. { make sure we don't try to access element 1 of the widestring if it's nil }
  761. if len > 0 then
  762. move(src[1],res[0],len*SizeOf(WideChar));
  763. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  764. {$pop}
  765. end;
  766. {$endif FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  767. {$ifndef FPC_HAS_UNICODESTR_COMPARE}
  768. {$define FPC_HAS_UNICODESTR_COMPARE}
  769. Function fpc_UnicodeStr_Compare(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE']; compilerproc;
  770. {
  771. Compares 2 UnicodeStrings;
  772. The result is
  773. <0 if S1<S2
  774. 0 if S1=S2
  775. >0 if S1>S2
  776. }
  777. Var
  778. MaxI,Temp : SizeInt;
  779. begin
  780. if pointer(S1)=pointer(S2) then
  781. begin
  782. fpc_UnicodeStr_Compare:=0;
  783. exit;
  784. end;
  785. Maxi:=Length(S1);
  786. temp:=Length(S2);
  787. If MaxI>Temp then
  788. MaxI:=Temp;
  789. Temp:=CompareWord(S1[1],S2[1],MaxI);
  790. if temp=0 then
  791. temp:=Length(S1)-Length(S2);
  792. fpc_UnicodeStr_Compare:=Temp;
  793. end;
  794. {$endif FPC_HAS_UNICODESTR_COMPARE}
  795. {$ifndef FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  796. {$define FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  797. Function fpc_UnicodeStr_Compare_Equal(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE_EQUAL']; compilerproc;
  798. {
  799. Compares 2 UnicodeStrings for equality only;
  800. The result is
  801. 0 if S1=S2
  802. <>0 if S1<>S2
  803. }
  804. Var
  805. MaxI : SizeInt;
  806. begin
  807. if pointer(S1)=pointer(S2) then
  808. exit(0);
  809. Maxi:=Length(S1);
  810. If MaxI<>Length(S2) then
  811. exit(-1)
  812. else
  813. exit(CompareWord(S1[1],S2[1],MaxI));
  814. end;
  815. {$endif FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  816. {$ifndef FPC_HAS_UNICODESTR_RANGECHECK}
  817. {$define FPC_HAS_UNICODESTR_RANGECHECK}
  818. Procedure fpc_UnicodeStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_UNICODESTR_RANGECHECK']; compilerproc;
  819. begin
  820. if (p=nil) or (index>PUnicodeRec(p-UnicodeFirstOff)^.len) or (Index<1) then
  821. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  822. end;
  823. {$endif FPC_HAS_UNICODESTR_RANGECHECK}
  824. {$ifndef FPC_HAS_UNICODESTR_SETLENGTH}
  825. {$define FPC_HAS_UNICODESTR_SETLENGTH}
  826. Procedure fpc_UnicodeStr_SetLength(Var S : UnicodeString; l : SizeInt);[Public,Alias : 'FPC_UNICODESTR_SETLENGTH']; compilerproc;
  827. {
  828. Sets The length of string S to L.
  829. Makes sure S is unique, and contains enough room.
  830. }
  831. Var
  832. Temp : Pointer;
  833. movelen: SizeInt;
  834. lens, lena : SizeUInt;
  835. begin
  836. if (l>0) then
  837. begin
  838. if Pointer(S)=nil then
  839. begin
  840. { Need a complete new string...}
  841. Pointer(s):=NewUnicodeString(l);
  842. end
  843. else
  844. if (PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref = 1) then
  845. begin
  846. Temp:=Pointer(s)-UnicodeFirstOff;
  847. lens:=MemSize(Temp);
  848. lena:=SizeUInt(L*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar)));
  849. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  850. begin
  851. reallocmem(Temp, lena);
  852. Pointer(S):=Temp+UnicodeFirstOff;
  853. end;
  854. end
  855. else
  856. begin
  857. { Reallocation is needed... }
  858. Temp:=NewUnicodeString(L);
  859. if Length(S)>0 then
  860. begin
  861. if l < succ(length(s)) then
  862. movelen := l
  863. { also move terminating null }
  864. else
  865. movelen := succ(length(s));
  866. Move(Pointer(S)^,Temp^,movelen * Sizeof(UnicodeChar));
  867. end;
  868. fpc_unicodestr_decr_ref(Pointer(S));
  869. Pointer(S):=Temp;
  870. end;
  871. { Force nil termination in case it gets shorter }
  872. PWord(Pointer(S)+l*sizeof(UnicodeChar))^:=0;
  873. PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len:=l;
  874. end
  875. else { length=0, deallocate the string }
  876. fpc_unicodestr_decr_ref (Pointer(S));
  877. end;
  878. {$endif FPC_HAS_UNICODESTR_SETLENGTH}
  879. {*****************************************************************************
  880. Public functions, In interface.
  881. *****************************************************************************}
  882. function UnicodeCharToString(S : PUnicodeChar) : UnicodeString;
  883. begin
  884. result:=UnicodeCharLenToString(s,Length(UnicodeString(s)));
  885. end;
  886. {$ifndef FPC_HAS_STRING_TO_UNICODECHAR}
  887. {$define FPC_HAS_STRING_TO_UNICODECHAR}
  888. function StringToUnicodeChar(const Src : RawByteString;Dest : PUnicodeChar;DestSize : SizeInt) : PUnicodeChar;
  889. begin
  890. result:=StringToWideChar(Src,Dest,DestSize);
  891. end;
  892. {$endif FPC_HAS_STRING_TO_UNICODECHAR}
  893. function WideCharToString(S : PWideChar) : UnicodeString;
  894. begin
  895. result:=WideCharLenToString(s,Length(WideString(s)));
  896. end;
  897. {$ifndef FPC_HAS_STRING_LEN_TO_WIDECHAR}
  898. {$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
  899. function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  900. var
  901. temp: widestring;
  902. Len: SizeInt;
  903. begin
  904. widestringmanager.Ansi2WideMoveProc(PChar(Src),StringCodePage(Src),temp,Length(Src));
  905. Len:=Length(temp);
  906. if DestSize<=Len then
  907. Len:=Destsize-1;
  908. move(temp[1],Dest^,Len*SizeOf(WideChar));
  909. Dest[Len]:=#0;
  910. result:=Dest;
  911. end;
  912. {$endif FPC_HAS_STRING_LEN_TO_WIDECHAR}
  913. {$ifndef FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  914. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  915. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  916. begin
  917. SetLength(result,Len);
  918. Move(S^,Pointer(Result)^,Len*2);
  919. end;
  920. {$endif FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  921. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : UnicodeString);
  922. begin
  923. Dest:=UnicodeCharLenToString(Src,Len);
  924. end;
  925. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : AnsiString);
  926. begin
  927. Dest:=AnsiString(UnicodeCharLenToString(Src,Len));
  928. end;
  929. procedure UnicodeCharToStrVar(S : PUnicodeChar;out Dest : AnsiString);
  930. begin
  931. Dest:=AnsiString(UnicodeCharToString(S));
  932. end;
  933. {$ifndef FPC_HAS_WIDECHAR_LEN_TO_STRING}
  934. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  935. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  936. begin
  937. SetLength(result,Len);
  938. Move(S^,Pointer(Result)^,Len*2);
  939. end;
  940. {$endif FPC_HAS_WIDECHAR_LEN_TO_STRING}
  941. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : UnicodeString);
  942. begin
  943. Dest:=WideCharLenToString(Src,Len);
  944. end;
  945. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  946. begin
  947. Dest:=AnsiString(WideCharLenToString(Src,Len));
  948. end;
  949. procedure WideCharToStrVar(S : PWideChar;out Dest : UnicodeString);
  950. begin
  951. Dest:=WideCharToString(S);
  952. end;
  953. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  954. begin
  955. Dest:=AnsiString(WideCharToString(S));
  956. end;
  957. {$ifndef FPC_HAS_UNICODESTR_UNIQUE}
  958. {$define FPC_HAS_UNICODESTR_UNIQUE}
  959. Function fpc_unicodestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_UNICODESTR_UNIQUE']; compilerproc;
  960. {
  961. Make sure reference count of S is 1,
  962. using copy-on-write semantics.
  963. }
  964. Var
  965. SNew : Pointer;
  966. L : SizeInt;
  967. begin
  968. pointer(result) := pointer(s);
  969. If Pointer(S)=Nil then
  970. exit;
  971. if PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref<>1 then
  972. begin
  973. L:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.len;
  974. SNew:=NewUnicodeString (L);
  975. Move (PUnicodeChar(S)^,SNew^,(L+1)*sizeof(UnicodeChar));
  976. PUnicodeRec(SNew-UnicodeFirstOff)^.len:=L;
  977. fpc_unicodestr_decr_ref (Pointer(S)); { Thread safe }
  978. pointer(S):=SNew;
  979. pointer(result):=SNew;
  980. end;
  981. end;
  982. {$endif FPC_HAS_UNICODESTR_UNIQUE}
  983. {$ifndef FPC_HAS_UNICODESTR_COPY}
  984. {$define FPC_HAS_UNICODESTR_COPY}
  985. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  986. var
  987. ResultAddress : Pointer;
  988. begin
  989. ResultAddress:=Nil;
  990. dec(index);
  991. if Index < 0 then
  992. Index := 0;
  993. { Check Size. Accounts for Zero-length S, the double check is needed because
  994. Size can be maxint and will get <0 when adding index }
  995. if (Size>Length(S)) or
  996. (Index+Size>Length(S)) then
  997. Size:=Length(S)-Index;
  998. If Size>0 then
  999. begin
  1000. ResultAddress:=NewUnicodeString(Size);
  1001. Move (PUnicodeChar(S)[Index],ResultAddress^,Size*sizeof(UnicodeChar));
  1002. PUnicodeRec(ResultAddress-UnicodeFirstOff)^.Len:=Size;
  1003. PUnicodeChar(ResultAddress+Size*sizeof(UnicodeChar))^:=#0;
  1004. end;
  1005. fpc_unicodestr_decr_ref(Pointer(fpc_unicodestr_copy));
  1006. Pointer(fpc_unicodestr_Copy):=ResultAddress;
  1007. end;
  1008. {$endif FPC_HAS_UNICODESTR_COPY}
  1009. {$ifndef FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1010. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1011. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  1012. var
  1013. i,MaxLen : SizeInt;
  1014. pc : punicodechar;
  1015. begin
  1016. Pos:=0;
  1017. if Length(SubStr)>0 then
  1018. begin
  1019. MaxLen:=Length(source)-Length(SubStr);
  1020. i:=0;
  1021. pc:=@source[1];
  1022. while (i<=MaxLen) do
  1023. begin
  1024. inc(i);
  1025. if (SubStr[1]=pc^) and
  1026. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1027. begin
  1028. Pos:=i;
  1029. exit;
  1030. end;
  1031. inc(pc);
  1032. end;
  1033. end;
  1034. end;
  1035. {$endif FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1036. {$ifndef FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1037. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1038. { Faster version for a unicodechar alone }
  1039. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  1040. var
  1041. i: SizeInt;
  1042. pc : punicodechar;
  1043. begin
  1044. pc:=@s[1];
  1045. for i:=1 to length(s) do
  1046. begin
  1047. if pc^=c then
  1048. begin
  1049. pos:=i;
  1050. exit;
  1051. end;
  1052. inc(pc);
  1053. end;
  1054. pos:=0;
  1055. end;
  1056. {$endif FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1057. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  1058. block, which is significant bloat without any sensible speed improvement. }
  1059. Function Pos (const c : RawByteString; Const s : UnicodeString) : SizeInt;
  1060. begin
  1061. result:=Pos(UnicodeString(c),s);
  1062. end;
  1063. Function Pos (const c : ShortString; Const s : UnicodeString) : SizeInt;
  1064. begin
  1065. result:=Pos(UnicodeString(c),s);
  1066. end;
  1067. Function Pos (const c : UnicodeString; Const s : RawByteString) : SizeInt;
  1068. begin
  1069. result:=Pos(c,UnicodeString(s));
  1070. end;
  1071. {$ifndef FPC_HAS_POS_CHAR_UNICODESTR}
  1072. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  1073. { Faster version for a char alone. Must be implemented because }
  1074. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1075. { using pos(char,pchar) will always call the shortstring version }
  1076. { (exact match for first argument), also with $h+ (JM) }
  1077. Function Pos (c : Char; Const s : UnicodeString) : SizeInt;
  1078. var
  1079. i: SizeInt;
  1080. wc : unicodechar;
  1081. pc : punicodechar;
  1082. begin
  1083. wc:=c;
  1084. pc:=@s[1];
  1085. for i:=1 to length(s) do
  1086. begin
  1087. if pc^=wc then
  1088. begin
  1089. pos:=i;
  1090. exit;
  1091. end;
  1092. inc(pc);
  1093. end;
  1094. pos:=0;
  1095. end;
  1096. {$endif FPC_HAS_POS_CHAR_UNICODESTR}
  1097. {$ifndef FPC_HAS_DELETE_UNICODESTR}
  1098. {$define FPC_HAS_DELETE_UNICODESTR}
  1099. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  1100. Var
  1101. LS : SizeInt;
  1102. begin
  1103. LS:=Length(S);
  1104. if (Index>LS) or (Index<=0) or (Size<=0) then
  1105. exit;
  1106. UniqueString (S);
  1107. { (Size+Index) will overflow if Size=MaxInt. }
  1108. if Size>LS-Index then
  1109. Size:=LS-Index+1;
  1110. if Size<=LS-Index then
  1111. begin
  1112. Dec(Index);
  1113. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1114. end;
  1115. Setlength(s,LS-Size);
  1116. end;
  1117. {$endif FPC_HAS_DELETE_UNICODESTR}
  1118. {$ifndef FPC_HAS_INSERT_UNICODESTR}
  1119. {$define FPC_HAS_INSERT_UNICODESTR}
  1120. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1121. var
  1122. Temp : UnicodeString;
  1123. LS : SizeInt;
  1124. begin
  1125. If Length(Source)=0 then
  1126. exit;
  1127. if index <= 0 then
  1128. index := 1;
  1129. Ls:=Length(S);
  1130. if index > LS then
  1131. index := LS+1;
  1132. Dec(Index);
  1133. SetLength(Temp,Length(Source)+LS);
  1134. If Index>0 then
  1135. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1136. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1137. If (LS-Index)>0 then
  1138. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1139. S:=Temp;
  1140. end;
  1141. {$endif FPC_HAS_INSERT_UNICODESTR}
  1142. {$ifndef FPC_HAS_UPCASE_UNICODECHAR}
  1143. {$define FPC_HAS_UPCASE_UNICODECHAR}
  1144. Function UpCase(c:UnicodeChar):UnicodeChar;
  1145. var
  1146. s : UnicodeString;
  1147. begin
  1148. s:=c;
  1149. result:=widestringmanager.UpperUnicodeStringProc(s)[1];
  1150. end;
  1151. {$endif FPC_HAS_UPCASE_UNICODECHAR}
  1152. {$ifndef FPC_HAS_UPCASE_UNICODESTR}
  1153. {$define FPC_HAS_UPCASE_UNICODESTR}
  1154. function UpCase(const s : UnicodeString) : UnicodeString;
  1155. begin
  1156. result:=widestringmanager.UpperUnicodeStringProc(s);
  1157. end;
  1158. {$endif FPC_HAS_UPCASE_UNICODESTR}
  1159. {$ifndef FPC_HAS_LOWERCASE_UNICODECHAR}
  1160. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  1161. Function LowerCase(c:UnicodeChar):UnicodeChar;
  1162. var
  1163. s : UnicodeString;
  1164. begin
  1165. s:=c;
  1166. result:=widestringmanager.LowerUnicodeStringProc(s)[1];
  1167. end;
  1168. {$endif FPC_HAS_LOWERCASE_UNICODECHAR}
  1169. {$ifndef FPC_HAS_LOWERCASE_UNICODESTR}
  1170. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  1171. function LowerCase(const s : UnicodeString) : UnicodeString;
  1172. begin
  1173. result:=widestringmanager.LowerUnicodeStringProc(s);
  1174. end;
  1175. {$endif FPC_HAS_LOWERCASE_UNICODESTR}
  1176. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1177. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1178. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  1179. begin
  1180. SetLength(S,Len);
  1181. If (Buf<>Nil) and (Len>0) then
  1182. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1183. end;
  1184. {$endif FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1185. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1186. {$define FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1187. Procedure SetString (Out S : UnicodeString; Buf : PChar; Len : SizeInt);
  1188. begin
  1189. If (Buf<>Nil) and (Len>0) then
  1190. widestringmanager.Ansi2UnicodeMoveProc(Buf,DefaultSystemCodePage,S,Len)
  1191. else
  1192. SetLength(S,Len);
  1193. end;
  1194. {$endif FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1195. {$ifndef FPUNONE}
  1196. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1197. Var
  1198. SS: ShortString;
  1199. begin
  1200. fpc_Val_Real_UnicodeStr:=0;
  1201. if length(S)>255 then
  1202. code:=256
  1203. else
  1204. begin
  1205. SS:=ShortString(S);
  1206. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1207. end;
  1208. end;
  1209. {$endif}
  1210. {$ifndef FPC_STR_ENUM_INTERN}
  1211. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1212. var
  1213. ss: ShortString;
  1214. begin
  1215. if length(s)>255 then
  1216. code:=256
  1217. else
  1218. begin
  1219. ss:=ShortString(s);
  1220. val(ss,fpc_val_enum_unicodestr,code);
  1221. end;
  1222. end;
  1223. {$endif FPC_STR_ENUM_INTERN}
  1224. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1225. Var
  1226. SS: ShortString;
  1227. begin
  1228. if length(S)>255 then
  1229. begin
  1230. fpc_Val_Currency_UnicodeStr:=0;
  1231. code:=256;
  1232. end
  1233. else
  1234. begin
  1235. SS:=ShortString(S);
  1236. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1237. end;
  1238. end;
  1239. Function fpc_Val_UInt_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_UNICODESTR']; compilerproc;
  1240. Var
  1241. SS: ShortString;
  1242. begin
  1243. fpc_Val_UInt_UnicodeStr:=0;
  1244. if length(S)>255 then
  1245. code:=256
  1246. else
  1247. begin
  1248. SS:=ShortString(S);
  1249. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1250. end;
  1251. end;
  1252. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1253. Var
  1254. SS: ShortString;
  1255. begin
  1256. fpc_Val_SInt_UnicodeStr:=0;
  1257. if length(S)>255 then
  1258. code:=256
  1259. else
  1260. begin
  1261. SS:=ShortString(S);
  1262. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1263. end;
  1264. end;
  1265. {$ifndef CPU64}
  1266. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1267. Var
  1268. SS: ShortString;
  1269. begin
  1270. fpc_Val_qword_UnicodeStr:=0;
  1271. if length(S)>255 then
  1272. code:=256
  1273. else
  1274. begin
  1275. SS:=ShortString(S);
  1276. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1277. end;
  1278. end;
  1279. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1280. Var
  1281. SS: ShortString;
  1282. begin
  1283. fpc_Val_int64_UnicodeStr:=0;
  1284. if length(S)>255 then
  1285. code:=256
  1286. else
  1287. begin
  1288. SS:=ShortString(S);
  1289. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1290. end;
  1291. end;
  1292. {$endif CPU64}
  1293. {$ifndef FPUNONE}
  1294. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1295. var
  1296. ss: shortstring;
  1297. begin
  1298. str_real(len,fr,d,treal_type(rt),ss);
  1299. s:=UnicodeString(ss);
  1300. end;
  1301. {$endif}
  1302. {$ifndef FPC_STR_ENUM_INTERN}
  1303. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1304. var
  1305. ss: ShortString;
  1306. begin
  1307. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1308. s:=UnicodeString(ss);
  1309. end;
  1310. {$endif FPC_STR_ENUM_INTERN}
  1311. procedure fpc_unicodestr_bool(b : boolean;len:sizeint;out s:unicodestring);compilerproc;
  1312. var
  1313. ss: ShortString;
  1314. begin
  1315. fpc_shortstr_bool(b,len,ss);
  1316. s:=UnicodeString(ss);
  1317. end;
  1318. {$ifdef FPC_HAS_STR_CURRENCY}
  1319. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1320. var
  1321. ss: shortstring;
  1322. begin
  1323. str(c:len:fr,ss);
  1324. s:=UnicodeString(ss);
  1325. end;
  1326. {$endif FPC_HAS_STR_CURRENCY}
  1327. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1328. Var
  1329. SS: ShortString;
  1330. begin
  1331. Str (v:Len,SS);
  1332. S:=UnicodeString(SS);
  1333. end;
  1334. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1335. Var
  1336. SS: ShortString;
  1337. begin
  1338. str(v:Len,SS);
  1339. S:=UnicodeString(SS);
  1340. end;
  1341. {$ifndef CPU64}
  1342. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1343. Var
  1344. SS: ShortString;
  1345. begin
  1346. Str (v:Len,SS);
  1347. S:=UnicodeString(SS);
  1348. end;
  1349. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1350. Var
  1351. SS: ShortString;
  1352. begin
  1353. str(v:Len,SS);
  1354. S:=UnicodeString(SS);
  1355. end;
  1356. {$endif CPU64}
  1357. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1358. begin
  1359. if assigned(Source) then
  1360. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,Length(Source))
  1361. else
  1362. Result:=0;
  1363. end;
  1364. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1365. var
  1366. i,j : SizeUInt;
  1367. lw : longword;
  1368. begin
  1369. result:=0;
  1370. if source=nil then
  1371. exit;
  1372. i:=0;
  1373. j:=0;
  1374. if assigned(Dest) then
  1375. begin
  1376. while (i<SourceChars) and (j<MaxDestBytes) do
  1377. begin
  1378. lw:=ord(Source[i]);
  1379. case lw of
  1380. 0..$7f:
  1381. begin
  1382. Dest[j]:=char(lw);
  1383. inc(j);
  1384. end;
  1385. $80..$7ff:
  1386. begin
  1387. if j+1>=MaxDestBytes then
  1388. break;
  1389. Dest[j]:=char($c0 or (lw shr 6));
  1390. Dest[j+1]:=char($80 or (lw and $3f));
  1391. inc(j,2);
  1392. end;
  1393. $800..$d7ff,$e000..$ffff:
  1394. begin
  1395. if j+2>=MaxDestBytes then
  1396. break;
  1397. Dest[j]:=char($e0 or (lw shr 12));
  1398. Dest[j+1]:=char($80 or ((lw shr 6) and $3f));
  1399. Dest[j+2]:=char($80 or (lw and $3f));
  1400. inc(j,3);
  1401. end;
  1402. $d800..$dbff:
  1403. {High Surrogates}
  1404. begin
  1405. if j+3>=MaxDestBytes then
  1406. break;
  1407. if (i+1<sourcechars) and
  1408. (word(Source[i+1]) >= $dc00) and
  1409. (word(Source[i+1]) <= $dfff) then
  1410. begin
  1411. { $d7c0 is ($d800 - ($10000 shr 10)) }
  1412. lw:=(longword(lw-$d7c0) shl 10) + (ord(source[i+1]) xor $dc00);
  1413. Dest[j]:=char($f0 or (lw shr 18));
  1414. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1415. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1416. Dest[j+3]:=char($80 or (lw and $3f));
  1417. inc(j,4);
  1418. inc(i);
  1419. end;
  1420. end;
  1421. end;
  1422. inc(i);
  1423. end;
  1424. if j>SizeUInt(MaxDestBytes-1) then
  1425. j:=MaxDestBytes-1;
  1426. Dest[j]:=#0;
  1427. end
  1428. else
  1429. begin
  1430. while i<SourceChars do
  1431. begin
  1432. case word(Source[i]) of
  1433. $0..$7f:
  1434. inc(j);
  1435. $80..$7ff:
  1436. inc(j,2);
  1437. $800..$d7ff,$e000..$ffff:
  1438. inc(j,3);
  1439. $d800..$dbff:
  1440. begin
  1441. if (i+1<sourcechars) and
  1442. (word(Source[i+1]) >= $dc00) and
  1443. (word(Source[i+1]) <= $dfff) then
  1444. begin
  1445. inc(j,4);
  1446. inc(i);
  1447. end;
  1448. end;
  1449. end;
  1450. inc(i);
  1451. end;
  1452. end;
  1453. result:=j+1;
  1454. end;
  1455. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1456. begin
  1457. if assigned(Source) then
  1458. Result:=Utf8ToUnicode(Dest,MaxChars,Source,length(Source))
  1459. else
  1460. Result:=0;
  1461. end;
  1462. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1463. const
  1464. UNICODE_INVALID=63;
  1465. var
  1466. InputUTF8: SizeUInt;
  1467. IBYTE: BYTE;
  1468. OutputUnicode: SizeUInt;
  1469. PRECHAR: SizeUInt;
  1470. TempBYTE: BYTE;
  1471. CharLen: SizeUint;
  1472. LookAhead: SizeUInt;
  1473. UC: SizeUInt;
  1474. begin
  1475. if not assigned(Source) then
  1476. begin
  1477. result:=0;
  1478. exit;
  1479. end;
  1480. result:=SizeUInt(-1);
  1481. InputUTF8:=0;
  1482. OutputUnicode:=0;
  1483. PreChar:=0;
  1484. if Assigned(Dest) Then
  1485. begin
  1486. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1487. begin
  1488. IBYTE:=byte(Source[InputUTF8]);
  1489. if (IBYTE and $80) = 0 then
  1490. begin
  1491. //One character US-ASCII, convert it to unicode
  1492. if IBYTE = 10 then
  1493. begin
  1494. If (PreChar<>13) and FALSE then
  1495. begin
  1496. //Expand to crlf, conform UTF-8.
  1497. //This procedure will break the memory alocation by
  1498. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1499. if OutputUnicode+1<MaxDestChars then
  1500. begin
  1501. Dest[OutputUnicode]:=WideChar(13);
  1502. inc(OutputUnicode);
  1503. Dest[OutputUnicode]:=WideChar(10);
  1504. inc(OutputUnicode);
  1505. PreChar:=10;
  1506. end
  1507. else
  1508. begin
  1509. Dest[OutputUnicode]:=WideChar(13);
  1510. inc(OutputUnicode);
  1511. end;
  1512. end
  1513. else
  1514. begin
  1515. Dest[OutputUnicode]:=WideChar(IBYTE);
  1516. inc(OutputUnicode);
  1517. PreChar:=IBYTE;
  1518. end;
  1519. end
  1520. else
  1521. begin
  1522. Dest[OutputUnicode]:=WideChar(IBYTE);
  1523. inc(OutputUnicode);
  1524. PreChar:=IBYTE;
  1525. end;
  1526. inc(InputUTF8);
  1527. end
  1528. else
  1529. begin
  1530. TempByte:=IBYTE;
  1531. CharLen:=0;
  1532. while (TempBYTE and $80)<>0 do
  1533. begin
  1534. TempBYTE:=(TempBYTE shl 1) and $FE;
  1535. inc(CharLen);
  1536. end;
  1537. //Test for the "CharLen" conforms UTF-8 string
  1538. //This means the 10xxxxxx pattern.
  1539. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1540. begin
  1541. //Insuficient chars in string to decode
  1542. //UTF-8 array. Fallback to single char.
  1543. CharLen:= 1;
  1544. end;
  1545. for LookAhead := 1 to CharLen-1 do
  1546. begin
  1547. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1548. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1549. begin
  1550. //Invalid UTF-8 sequence, fallback.
  1551. CharLen:= LookAhead;
  1552. break;
  1553. end;
  1554. end;
  1555. UC:=$FFFF;
  1556. case CharLen of
  1557. 1: begin
  1558. //Not valid UTF-8 sequence
  1559. UC:=UNICODE_INVALID;
  1560. end;
  1561. 2: begin
  1562. //Two bytes UTF, convert it
  1563. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1564. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1565. if UC <= $7F then
  1566. begin
  1567. //Invalid UTF sequence.
  1568. UC:=UNICODE_INVALID;
  1569. end;
  1570. end;
  1571. 3: begin
  1572. //Three bytes, convert it to unicode
  1573. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1574. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1575. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1576. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1577. begin
  1578. //Invalid UTF-8 sequence
  1579. UC:= UNICODE_INVALID;
  1580. End;
  1581. end;
  1582. 4: begin
  1583. //Four bytes, convert it to two unicode characters
  1584. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1585. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1586. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1587. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1588. if (UC < $10000) or (UC > $10FFFF) then
  1589. begin
  1590. UC:= UNICODE_INVALID;
  1591. end
  1592. else
  1593. begin
  1594. { only store pair if room }
  1595. dec(UC,$10000);
  1596. if (OutputUnicode<MaxDestChars-1) then
  1597. begin
  1598. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1599. inc(OutputUnicode);
  1600. UC:=(UC and $3ff) + $DC00;
  1601. end
  1602. else
  1603. begin
  1604. InputUTF8:= InputUTF8 + CharLen;
  1605. { don't store anything }
  1606. CharLen:=0;
  1607. end;
  1608. end;
  1609. end;
  1610. 5,6,7: begin
  1611. //Invalid UTF8 to unicode conversion,
  1612. //mask it as invalid UNICODE too.
  1613. UC:=UNICODE_INVALID;
  1614. end;
  1615. end;
  1616. if CharLen > 0 then
  1617. begin
  1618. PreChar:=UC;
  1619. Dest[OutputUnicode]:=WideChar(UC);
  1620. inc(OutputUnicode);
  1621. end;
  1622. InputUTF8:= InputUTF8 + CharLen;
  1623. end;
  1624. end;
  1625. Result:=OutputUnicode+1;
  1626. end
  1627. else
  1628. begin
  1629. while (InputUTF8<SourceBytes) do
  1630. begin
  1631. IBYTE:=byte(Source[InputUTF8]);
  1632. if (IBYTE and $80) = 0 then
  1633. begin
  1634. //One character US-ASCII, convert it to unicode
  1635. if IBYTE = 10 then
  1636. begin
  1637. if (PreChar<>13) and FALSE then
  1638. begin
  1639. //Expand to crlf, conform UTF-8.
  1640. //This procedure will break the memory alocation by
  1641. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1642. inc(OutputUnicode,2);
  1643. PreChar:=10;
  1644. end
  1645. else
  1646. begin
  1647. inc(OutputUnicode);
  1648. PreChar:=IBYTE;
  1649. end;
  1650. end
  1651. else
  1652. begin
  1653. inc(OutputUnicode);
  1654. PreChar:=IBYTE;
  1655. end;
  1656. inc(InputUTF8);
  1657. end
  1658. else
  1659. begin
  1660. TempByte:=IBYTE;
  1661. CharLen:=0;
  1662. while (TempBYTE and $80)<>0 do
  1663. begin
  1664. TempBYTE:=(TempBYTE shl 1) and $FE;
  1665. inc(CharLen);
  1666. end;
  1667. //Test for the "CharLen" conforms UTF-8 string
  1668. //This means the 10xxxxxx pattern.
  1669. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1670. begin
  1671. //Insuficient chars in string to decode
  1672. //UTF-8 array. Fallback to single char.
  1673. CharLen:= 1;
  1674. end;
  1675. for LookAhead := 1 to CharLen-1 do
  1676. begin
  1677. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1678. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1679. begin
  1680. //Invalid UTF-8 sequence, fallback.
  1681. CharLen:= LookAhead;
  1682. break;
  1683. end;
  1684. end;
  1685. UC:=$FFFF;
  1686. case CharLen of
  1687. 1: begin
  1688. //Not valid UTF-8 sequence
  1689. UC:=UNICODE_INVALID;
  1690. end;
  1691. 2: begin
  1692. //Two bytes UTF, convert it
  1693. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1694. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1695. if UC <= $7F then
  1696. begin
  1697. //Invalid UTF sequence.
  1698. UC:=UNICODE_INVALID;
  1699. end;
  1700. end;
  1701. 3: begin
  1702. //Three bytes, convert it to unicode
  1703. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1704. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1705. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1706. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1707. begin
  1708. //Invalid UTF-8 sequence
  1709. UC:= UNICODE_INVALID;
  1710. end;
  1711. end;
  1712. 4: begin
  1713. //Four bytes, convert it to two unicode characters
  1714. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1715. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1716. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1717. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1718. if (UC < $10000) or (UC > $10FFFF) then
  1719. UC:= UNICODE_INVALID
  1720. else
  1721. { extra character character }
  1722. inc(OutputUnicode);
  1723. end;
  1724. 5,6,7: begin
  1725. //Invalid UTF8 to unicode conversion,
  1726. //mask it as invalid UNICODE too.
  1727. UC:=UNICODE_INVALID;
  1728. end;
  1729. end;
  1730. if CharLen > 0 then
  1731. begin
  1732. PreChar:=UC;
  1733. inc(OutputUnicode);
  1734. end;
  1735. InputUTF8:= InputUTF8 + CharLen;
  1736. end;
  1737. end;
  1738. Result:=OutputUnicode+1;
  1739. end;
  1740. end;
  1741. function UTF8Encode(const s : RawByteString) : RawByteString; inline;
  1742. begin
  1743. Result:=UTF8Encode(UnicodeString(s));
  1744. end;
  1745. {$ifndef FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1746. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1747. function UTF8Encode(const s : UnicodeString) : RawByteString;
  1748. var
  1749. i : SizeInt;
  1750. hs : UTF8String;
  1751. begin
  1752. result:='';
  1753. if s='' then
  1754. exit;
  1755. SetLength(hs,length(s)*3);
  1756. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  1757. if i>0 then
  1758. begin
  1759. SetLength(hs,i-1);
  1760. result:=hs;
  1761. end;
  1762. end;
  1763. {$endif FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1764. {$ifndef FPC_HAS_UTF8DECODE_UNICODESTRING}
  1765. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  1766. function UTF8Decode(const s : RawByteString): UnicodeString;
  1767. var
  1768. i : SizeInt;
  1769. hs : UnicodeString;
  1770. begin
  1771. result:='';
  1772. if s='' then
  1773. exit;
  1774. SetLength(hs,length(s));
  1775. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  1776. if i>0 then
  1777. begin
  1778. SetLength(hs,i-1);
  1779. result:=hs;
  1780. end;
  1781. end;
  1782. {$endif FPC_HAS_UTF8DECODE_UNICODESTRING}
  1783. function AnsiToUtf8(const s : RawByteString): RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1784. begin
  1785. Result:=Utf8Encode(s);
  1786. end;
  1787. function Utf8ToAnsi(const s : RawByteString) : RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1788. begin
  1789. Result:=RawByteString(Utf8Decode(s));
  1790. end;
  1791. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String);
  1792. var
  1793. i, reslen: sizeint;
  1794. w: longint;
  1795. begin
  1796. reslen:=0;
  1797. i:=0;
  1798. { calculate required length }
  1799. while (i<len) do
  1800. begin
  1801. if (p[i]<=#$d7ff) or (p[i]>=#$e000) then
  1802. inc(i)
  1803. else if (p[i]<=#$dbff) and
  1804. (i+1<len) and
  1805. (p[i+1]>=#$dc00) and
  1806. (p[i+1]<=#$dfff) then
  1807. inc(i,2)
  1808. else
  1809. inc(i);
  1810. inc(reslen);
  1811. end;
  1812. SetLength(res,reslen+1); { +1 for null termination }
  1813. reslen:=0;
  1814. i:=0;
  1815. { do conversion }
  1816. while (i<len) do
  1817. begin
  1818. w:=ord(p[i]);
  1819. if (w<=$d7ff) or (w>=$e000) then
  1820. res[reslen]:=w
  1821. else if (w<=$dbff) and
  1822. (i+1<len) and
  1823. (p[i+1]>=#$dc00) and
  1824. (p[i+1]<=#$dfff) then
  1825. begin
  1826. res[reslen]:=(UCS4Char(w-$d7c0) shl 10)+(UCS4Char(p[i+1]) xor $dc00);
  1827. inc(i);
  1828. end
  1829. else { invalid surrogate pair }
  1830. res[reslen]:=w;
  1831. inc(i);
  1832. inc(reslen);
  1833. end;
  1834. res[reslen]:=0;
  1835. end;
  1836. {$ifndef FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1837. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1838. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  1839. begin
  1840. UCS4Encode(PWideChar(s),Length(s),result);
  1841. end;
  1842. {$endif FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1843. {$ifndef FPC_HAS_WIDESTR_TO_UCS4STRING}
  1844. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  1845. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1846. begin
  1847. UCS4Encode(PWideChar(s),Length(s),result);
  1848. end;
  1849. {$endif FPC_HAS_WIDESTR_TO_UCS4STRING}
  1850. {$ifndef FPC_HAS_UCS4STRING_TO_WIDESTR}
  1851. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  1852. { dest should point to previously allocated wide/unicodestring }
  1853. procedure UCS4Decode(const s: UCS4String; dest: PWideChar);
  1854. var
  1855. i: sizeint;
  1856. nc: UCS4Char;
  1857. begin
  1858. for i:=0 to length(s)-2 do { -2 because s contains explicit terminating #0 }
  1859. begin
  1860. nc:=s[i];
  1861. if (nc<$ffff) then
  1862. dest^:=widechar(nc)
  1863. else if (dword(nc)<=$10ffff) then
  1864. begin
  1865. dest^:=widechar(nc shr 10 + $d7c0);
  1866. { subtracting $10000 doesn't change low 10 bits }
  1867. dest[1]:=widechar(nc and $3ff + $dc00);
  1868. inc(dest);
  1869. end
  1870. else { invalid code point }
  1871. dest^:='?';
  1872. inc(dest);
  1873. end;
  1874. end;
  1875. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  1876. var
  1877. i : SizeInt;
  1878. reslen : SizeInt;
  1879. begin
  1880. reslen:=0;
  1881. for i:=0 to length(s)-2 do { skip terminating #0 }
  1882. Inc(reslen,1+ord((s[i]>$ffff) and (s[i]<=$10ffff)));
  1883. SetLength(result,reslen);
  1884. UCS4Decode(s,pointer(result));
  1885. end;
  1886. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1887. var
  1888. i : SizeInt;
  1889. reslen : SizeInt;
  1890. begin
  1891. reslen:=0;
  1892. for i:=0 to length(s)-2 do { skip terminating #0 }
  1893. Inc(reslen,1+ord((s[i]>$ffff) and (s[i]<=$10ffff)));
  1894. SetLength(result,reslen);
  1895. UCS4Decode(s,pointer(result));
  1896. end;
  1897. {$endif FPC_HAS_UCS4STRING_TO_WIDESTR}
  1898. {$ifndef FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  1899. const
  1900. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  1901. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  1902. procedure unimplementedunicodestring;
  1903. begin
  1904. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1905. If IsConsole then
  1906. begin
  1907. Writeln(StdErr,SNoUnicodestrings);
  1908. Writeln(StdErr,SRecompileWithUnicodestrings);
  1909. end;
  1910. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1911. HandleErrorAddrFrameInd(233,get_pc_addr,get_frame);
  1912. end;
  1913. function StringElementSize(const S: UnicodeString): Word; overload;
  1914. begin
  1915. if assigned(Pointer(S)) then
  1916. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.ElementSize
  1917. else
  1918. Result:=SizeOf(UnicodeChar);
  1919. end;
  1920. function StringRefCount(const S: UnicodeString): SizeInt; overload;
  1921. begin
  1922. if assigned(Pointer(S)) then
  1923. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.Ref
  1924. else
  1925. Result:=0;
  1926. end;
  1927. function StringCodePage(const S: UnicodeString): TSystemCodePage; overload;
  1928. begin
  1929. {$ifdef FPC_HAS_CPSTRING}
  1930. if assigned(Pointer(S)) then
  1931. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.CodePage
  1932. else
  1933. {$endif FPC_HAS_CPSTRING}
  1934. Result:=DefaultUnicodeCodePage;
  1935. end;
  1936. {$warnings off}
  1937. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  1938. begin
  1939. unimplementedunicodestring;
  1940. end;
  1941. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  1942. begin
  1943. unimplementedunicodestring;
  1944. end;
  1945. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  1946. begin
  1947. unimplementedunicodestring;
  1948. end;
  1949. {$warnings on}
  1950. procedure initunicodestringmanager;
  1951. begin
  1952. {$ifndef HAS_WIDESTRINGMANAGER}
  1953. widestringmanager.Unicode2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  1954. widestringmanager.Ansi2UnicodeMoveProc:=@DefaultAnsi2UnicodeMove;
  1955. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  1956. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  1957. {$endif HAS_WIDESTRINGMANAGER}
  1958. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  1959. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  1960. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1961. {$ifndef HAS_WIDESTRINGMANAGER}
  1962. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  1963. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  1964. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  1965. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  1966. {$endif HAS_WIDESTRINGMANAGER}
  1967. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  1968. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  1969. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  1970. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  1971. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1972. widestringmanager.GetStandardCodePageProc:=@DefaultGetStandardCodePage;
  1973. end;
  1974. {$endif FPC_HAS_BUILTIN_WIDESTR_MANAGER}