ustrings.inc 65 KB

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