ustrings.inc 67 KB

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