ustrings.inc 69 KB

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