ustrings.inc 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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. PAnsiRec(dest)^.CodePage:=cp;
  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. procedure fpc_UChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  524. {
  525. Converts a WideChar to a ShortString;
  526. }
  527. var
  528. s: ansistring;
  529. begin
  530. widestringmanager.Wide2AnsiMoveProc(@c,s,DefaultSystemCodePage,1);
  531. res:=s;
  532. end;
  533. {$endif FPC_HAS_UCHAR_TO_SHORTSTR}
  534. {$ifndef FPC_HAS_UCHAR_TO_UNICODESTR}
  535. {$define FPC_HAS_UCHAR_TO_UNICODESTR}
  536. Function fpc_UChar_To_UnicodeStr(const c : UnicodeChar): UnicodeString; compilerproc;
  537. {
  538. Converts a UnicodeChar to a UnicodeString;
  539. }
  540. begin
  541. Setlength (fpc_UChar_To_UnicodeStr,1);
  542. fpc_UChar_To_UnicodeStr[1]:= c;
  543. end;
  544. {$endif FPC_HAS_UCHAR_TO_UNICODESTR}
  545. {$ifndef FPC_HAS_UCHAR_TO_ANSISTR}
  546. {$define FPC_HAS_UCHAR_TO_ANSISTR}
  547. Function fpc_UChar_To_AnsiStr(const c : UnicodeChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  548. {
  549. Converts a UnicodeChar to a AnsiString;
  550. }
  551. {$ifndef FPC_HAS_CPSTRING}
  552. var
  553. cp : TSystemCodePage;
  554. {$endif FPC_HAS_CPSTRING}
  555. begin
  556. {$ifndef FPC_HAS_CPSTRING}
  557. cp:=DefaultSystemCodePage;
  558. {$endif FPC_HAS_CPSTRING}
  559. cp:=TranslatePlaceholderCP(cp);
  560. widestringmanager.Unicode2AnsiMoveProc(@c, fpc_UChar_To_AnsiStr, cp, 1);
  561. end;
  562. {$endif FPC_HAS_UCHAR_TO_ANSISTR}
  563. {$ifndef FPC_HAS_PCHAR_TO_UNICODESTR}
  564. {$define FPC_HAS_PCHAR_TO_UNICODESTR}
  565. Function fpc_PChar_To_UnicodeStr(const p : pchar): UnicodeString; compilerproc;
  566. Var
  567. L : SizeInt;
  568. begin
  569. if (not assigned(p)) or (p[0]=#0) Then
  570. begin
  571. fpc_pchar_to_unicodestr := '';
  572. exit;
  573. end;
  574. l:=IndexChar(p^,-1,#0);
  575. widestringmanager.Ansi2UnicodeMoveProc(P,DefaultSystemCodePage,fpc_PChar_To_UnicodeStr,l);
  576. end;
  577. {$endif FPC_HAS_PCHAR_TO_UNICODESTR}
  578. {$ifndef FPC_HAS_CHARARRAY_TO_UNICODESTR}
  579. {$define FPC_HAS_CHARARRAY_TO_UNICODESTR}
  580. Function fpc_CharArray_To_UnicodeStr(const arr: array of char; zerobased: boolean = true): UnicodeString; compilerproc;
  581. var
  582. i : SizeInt;
  583. begin
  584. if zerobased then
  585. begin
  586. if arr[0]=#0 Then
  587. begin
  588. fpc_chararray_to_unicodestr:='';
  589. exit;
  590. end;
  591. i:=IndexChar(arr,high(arr)+1,#0);
  592. if i=-1 then
  593. i:=high(arr)+1;
  594. end
  595. else
  596. i:=high(arr)+1;
  597. widestringmanager.Ansi2UnicodeMoveProc(pchar(@arr),DefaultSystemCodePage,fpc_CharArray_To_UnicodeStr,i);
  598. end;
  599. {$endif FPC_HAS_CHARARRAY_TO_UNICODESTR}
  600. {$ifndef FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  601. {$define FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  602. Function fpc_WideCharArray_To_UnicodeStr(const arr: array of widechar; zerobased: boolean = true): UnicodeString; compilerproc;
  603. var
  604. i : SizeInt;
  605. begin
  606. if (zerobased) then
  607. begin
  608. i:=IndexWord(arr,high(arr)+1,0);
  609. if i = -1 then
  610. i := high(arr)+1;
  611. end
  612. else
  613. i := high(arr)+1;
  614. SetLength(fpc_WideCharArray_To_UnicodeStr,i);
  615. Move(arr[0], Pointer(fpc_WideCharArray_To_UnicodeStr)^,i*sizeof(WideChar));
  616. end;
  617. {$endif FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  618. {$ifndef FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  619. {$define FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  620. { due to their names, the following procedures should be in wstrings.inc,
  621. however, the compiler generates code using this functions on all platforms }
  622. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  623. var
  624. l: longint;
  625. index: ptrint;
  626. len: byte;
  627. temp: ansistring;
  628. begin
  629. l := high(arr)+1;
  630. if l>=high(res)+1 then
  631. l:=high(res)
  632. else if l<0 then
  633. l:=0;
  634. if zerobased then
  635. begin
  636. index:=IndexWord(arr[0],l,0);
  637. if index<0 then
  638. len:=l
  639. else
  640. len:=index;
  641. end
  642. else
  643. len:=l;
  644. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,DefaultSystemCodePage,len);
  645. res:=temp;
  646. end;
  647. {$endif FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  648. {$ifndef FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  649. {$define FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  650. 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;
  651. var
  652. i : SizeInt;
  653. {$ifndef FPC_HAS_CPSTRING}
  654. cp : TSystemCodePage;
  655. {$endif FPC_HAS_CPSTRING}
  656. begin
  657. {$ifndef FPC_HAS_CPSTRING}
  658. cp:=DefaultSystemCodePage;
  659. {$endif FPC_HAS_CPSTRING}
  660. if (zerobased) then
  661. begin
  662. i:=IndexWord(arr,high(arr)+1,0);
  663. if i = -1 then
  664. i := high(arr)+1;
  665. end
  666. else
  667. i := high(arr)+1;
  668. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),RawByteString(fpc_WideCharArray_To_AnsiStr),cp,i);
  669. end;
  670. {$endif FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
  671. {$ifndef FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  672. {$define FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  673. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  674. var
  675. i : SizeInt;
  676. begin
  677. if (zerobased) then
  678. begin
  679. i:=IndexWord(arr,high(arr)+1,0);
  680. if i = -1 then
  681. i := high(arr)+1;
  682. end
  683. else
  684. i := high(arr)+1;
  685. SetLength(fpc_WideCharArray_To_WideStr,i);
  686. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  687. end;
  688. {$endif FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  689. {$ifndef FPC_HAS_UNICODESTR_TO_CHARARRAY}
  690. {$define FPC_HAS_UNICODESTR_TO_CHARARRAY}
  691. procedure fpc_unicodestr_to_chararray(out res: array of char; const src: UnicodeString); compilerproc;
  692. var
  693. len: SizeInt;
  694. temp: ansistring;
  695. begin
  696. len := length(src);
  697. { make sure we don't dereference src if it can be nil (JM) }
  698. if len > 0 then
  699. widestringmanager.unicode2ansimoveproc(punicodechar(@src[1]),temp,DefaultSystemCodePage,len);
  700. len := length(temp);
  701. if len > length(res) then
  702. len := length(res);
  703. {$push}
  704. {$r-}
  705. move(temp[1],res[0],len);
  706. fillchar(res[len],length(res)-len,0);
  707. {$pop}
  708. end;
  709. {$endif FPC_HAS_UNICODESTR_TO_UNICODECHARARRAY}
  710. {$ifndef FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  711. {$define FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  712. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: RawByteString); compilerproc;
  713. var
  714. len: SizeInt;
  715. temp: widestring;
  716. begin
  717. len := length(src);
  718. { make sure we don't dereference src if it can be nil (JM) }
  719. if len > 0 then
  720. widestringmanager.ansi2widemoveproc(pchar(@src[1]),StringCodePage(src),temp,len);
  721. len := length(temp);
  722. if len > length(res) then
  723. len := length(res);
  724. {$push}
  725. {$r-}
  726. move(temp[1],res[0],len*sizeof(widechar));
  727. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  728. {$pop}
  729. end;
  730. {$endif FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  731. {$ifndef FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  732. {$define FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  733. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  734. var
  735. len: longint;
  736. temp : widestring;
  737. begin
  738. len := length(src);
  739. { make sure we don't access char 1 if length is 0 (JM) }
  740. if len > 0 then
  741. widestringmanager.ansi2widemoveproc(pchar(@src[1]),DefaultSystemCodePage,temp,len);
  742. len := length(temp);
  743. if len > length(res) then
  744. len := length(res);
  745. {$push}
  746. {$r-}
  747. move(temp[1],res[0],len*sizeof(widechar));
  748. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  749. {$pop}
  750. end;
  751. {$endif FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  752. {$ifndef FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  753. {$define FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  754. procedure fpc_unicodestr_to_widechararray(out res: array of widechar; const src: UnicodeString); compilerproc;
  755. var
  756. len: SizeInt;
  757. begin
  758. len := length(src);
  759. if len > length(res) then
  760. len := length(res);
  761. {$push}
  762. {$r-}
  763. { make sure we don't try to access element 1 of the widestring if it's nil }
  764. if len > 0 then
  765. move(src[1],res[0],len*SizeOf(WideChar));
  766. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  767. {$pop}
  768. end;
  769. {$endif FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  770. {$ifndef FPC_HAS_UNICODESTR_COMPARE}
  771. {$define FPC_HAS_UNICODESTR_COMPARE}
  772. Function fpc_UnicodeStr_Compare(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE']; compilerproc;
  773. {
  774. Compares 2 UnicodeStrings;
  775. The result is
  776. <0 if S1<S2
  777. 0 if S1=S2
  778. >0 if S1>S2
  779. }
  780. Var
  781. MaxI,Temp : SizeInt;
  782. begin
  783. if pointer(S1)=pointer(S2) then
  784. begin
  785. fpc_UnicodeStr_Compare:=0;
  786. exit;
  787. end;
  788. Maxi:=Length(S1);
  789. temp:=Length(S2);
  790. If MaxI>Temp then
  791. MaxI:=Temp;
  792. Temp:=CompareWord(S1[1],S2[1],MaxI);
  793. if temp=0 then
  794. temp:=Length(S1)-Length(S2);
  795. fpc_UnicodeStr_Compare:=Temp;
  796. end;
  797. {$endif FPC_HAS_UNICODESTR_COMPARE}
  798. {$ifndef FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  799. {$define FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  800. Function fpc_UnicodeStr_Compare_Equal(const S1,S2 : UnicodeString): SizeInt;[Public,Alias : 'FPC_UNICODESTR_COMPARE_EQUAL']; compilerproc;
  801. {
  802. Compares 2 UnicodeStrings for equality only;
  803. The result is
  804. 0 if S1=S2
  805. <>0 if S1<>S2
  806. }
  807. Var
  808. MaxI : SizeInt;
  809. begin
  810. if pointer(S1)=pointer(S2) then
  811. exit(0);
  812. Maxi:=Length(S1);
  813. If MaxI<>Length(S2) then
  814. exit(-1)
  815. else
  816. exit(CompareWord(S1[1],S2[1],MaxI));
  817. end;
  818. {$endif FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  819. {$ifndef FPC_HAS_UNICODESTR_RANGECHECK}
  820. {$define FPC_HAS_UNICODESTR_RANGECHECK}
  821. Procedure fpc_UnicodeStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_UNICODESTR_RANGECHECK']; compilerproc;
  822. begin
  823. if (p=nil) or (index>PUnicodeRec(p-UnicodeFirstOff)^.len) or (Index<1) then
  824. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  825. end;
  826. {$endif FPC_HAS_UNICODESTR_RANGECHECK}
  827. {$ifndef FPC_HAS_UNICODESTR_SETLENGTH}
  828. {$define FPC_HAS_UNICODESTR_SETLENGTH}
  829. Procedure fpc_UnicodeStr_SetLength(Var S : UnicodeString; l : SizeInt);[Public,Alias : 'FPC_UNICODESTR_SETLENGTH']; compilerproc;
  830. {
  831. Sets The length of string S to L.
  832. Makes sure S is unique, and contains enough room.
  833. }
  834. Var
  835. Temp : Pointer;
  836. movelen: SizeInt;
  837. nl,lens, lena : SizeUInt;
  838. begin
  839. nl:=l;
  840. {$IFDEF VER2_6}
  841. nl:=nl*2;
  842. {$ENDIF}
  843. if (l>0) then
  844. begin
  845. if Pointer(S)=nil then
  846. begin
  847. { Need a complete new string...}
  848. Pointer(s):=NewUnicodeString(nl);
  849. end
  850. else
  851. if (PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref = 1) then
  852. begin
  853. Temp:=Pointer(s)-UnicodeFirstOff;
  854. lens:=MemSize(Temp);
  855. lena:=SizeUInt(L*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar)));
  856. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  857. begin
  858. reallocmem(Temp, lena);
  859. Pointer(S):=Temp+UnicodeFirstOff;
  860. end;
  861. end
  862. else
  863. begin
  864. { Reallocation is needed... }
  865. Temp:=NewUnicodeString(nL);
  866. if Length(S)>0 then
  867. begin
  868. if l < succ(length(s)) then
  869. movelen := l
  870. { also move terminating null }
  871. else
  872. movelen := succ(length(s));
  873. Move(Pointer(S)^,Temp^,movelen * Sizeof(UnicodeChar));
  874. end;
  875. fpc_unicodestr_decr_ref(Pointer(S));
  876. Pointer(S):=Temp;
  877. end;
  878. { Force nil termination in case it gets shorter }
  879. PWord(Pointer(S)+l*sizeof(UnicodeChar))^:=0;
  880. PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len:=nl;
  881. end
  882. else { length=0, deallocate the string }
  883. fpc_unicodestr_decr_ref (Pointer(S));
  884. end;
  885. {$endif FPC_HAS_UNICODESTR_SETLENGTH}
  886. {*****************************************************************************
  887. Public functions, In interface.
  888. *****************************************************************************}
  889. function UnicodeCharToString(S : PUnicodeChar) : UnicodeString;
  890. begin
  891. result:=UnicodeCharLenToString(s,Length(UnicodeString(s)));
  892. end;
  893. {$ifndef FPC_HAS_STRING_TO_UNICODECHAR}
  894. {$define FPC_HAS_STRING_TO_UNICODECHAR}
  895. function StringToUnicodeChar(const Src : RawByteString;Dest : PUnicodeChar;DestSize : SizeInt) : PUnicodeChar;
  896. begin
  897. result:=StringToWideChar(Src,Dest,DestSize);
  898. end;
  899. {$endif FPC_HAS_STRING_TO_UNICODECHAR}
  900. function WideCharToString(S : PWideChar) : UnicodeString;
  901. begin
  902. result:=WideCharLenToString(s,Length(WideString(s)));
  903. end;
  904. {$ifndef FPC_HAS_STRING_LEN_TO_WIDECHAR}
  905. {$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
  906. function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  907. var
  908. temp: widestring;
  909. Len: SizeInt;
  910. begin
  911. widestringmanager.Ansi2WideMoveProc(PChar(Src),StringCodePage(Src),temp,Length(Src));
  912. Len:=Length(temp);
  913. if DestSize<=Len then
  914. Len:=Destsize-1;
  915. move(temp[1],Dest^,Len*SizeOf(WideChar));
  916. Dest[Len]:=#0;
  917. result:=Dest;
  918. end;
  919. {$endif FPC_HAS_STRING_LEN_TO_WIDECHAR}
  920. {$ifndef FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  921. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  922. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  923. begin
  924. SetLength(result,Len);
  925. Move(S^,Pointer(Result)^,Len*2);
  926. end;
  927. {$endif FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  928. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : UnicodeString);
  929. begin
  930. Dest:=UnicodeCharLenToString(Src,Len);
  931. end;
  932. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : AnsiString);
  933. begin
  934. Dest:=AnsiString(UnicodeCharLenToString(Src,Len));
  935. end;
  936. procedure UnicodeCharToStrVar(S : PUnicodeChar;out Dest : AnsiString);
  937. begin
  938. Dest:=AnsiString(UnicodeCharToString(S));
  939. end;
  940. {$ifndef FPC_HAS_WIDECHAR_LEN_TO_STRING}
  941. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  942. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  943. begin
  944. SetLength(result,Len);
  945. Move(S^,Pointer(Result)^,Len*2);
  946. end;
  947. {$endif FPC_HAS_WIDECHAR_LEN_TO_STRING}
  948. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : UnicodeString);
  949. begin
  950. Dest:=WideCharLenToString(Src,Len);
  951. end;
  952. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  953. begin
  954. Dest:=AnsiString(WideCharLenToString(Src,Len));
  955. end;
  956. procedure WideCharToStrVar(S : PWideChar;out Dest : UnicodeString);
  957. begin
  958. Dest:=WideCharToString(S);
  959. end;
  960. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  961. begin
  962. Dest:=AnsiString(WideCharToString(S));
  963. end;
  964. {$ifndef FPC_HAS_UNICODESTR_UNIQUE}
  965. {$define FPC_HAS_UNICODESTR_UNIQUE}
  966. Function fpc_unicodestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_UNICODESTR_UNIQUE']; compilerproc;
  967. {
  968. Make sure reference count of S is 1,
  969. using copy-on-write semantics.
  970. }
  971. Var
  972. SNew : Pointer;
  973. L : SizeInt;
  974. begin
  975. pointer(result) := pointer(s);
  976. If Pointer(S)=Nil then
  977. exit;
  978. if PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref<>1 then
  979. begin
  980. L:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.len;
  981. SNew:=NewUnicodeString (L);
  982. Move (PUnicodeChar(S)^,SNew^,(L+1)*sizeof(UnicodeChar));
  983. PUnicodeRec(SNew-UnicodeFirstOff)^.len:=L;
  984. fpc_unicodestr_decr_ref (Pointer(S)); { Thread safe }
  985. pointer(S):=SNew;
  986. pointer(result):=SNew;
  987. end;
  988. end;
  989. {$endif FPC_HAS_UNICODESTR_UNIQUE}
  990. {$ifndef FPC_HAS_UNICODESTR_COPY}
  991. {$define FPC_HAS_UNICODESTR_COPY}
  992. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  993. var
  994. ResultAddress : Pointer;
  995. begin
  996. ResultAddress:=Nil;
  997. dec(index);
  998. if Index < 0 then
  999. Index := 0;
  1000. { Check Size. Accounts for Zero-length S, the double check is needed because
  1001. Size can be maxint and will get <0 when adding index }
  1002. if (Size>Length(S)) or
  1003. (Index+Size>Length(S)) then
  1004. Size:=Length(S)-Index;
  1005. If Size>0 then
  1006. begin
  1007. ResultAddress:=NewUnicodeString(Size);
  1008. Move (PUnicodeChar(S)[Index],ResultAddress^,Size*sizeof(UnicodeChar));
  1009. PUnicodeRec(ResultAddress-UnicodeFirstOff)^.Len:=Size;
  1010. PUnicodeChar(ResultAddress+Size*sizeof(UnicodeChar))^:=#0;
  1011. end;
  1012. fpc_unicodestr_decr_ref(Pointer(fpc_unicodestr_copy));
  1013. Pointer(fpc_unicodestr_Copy):=ResultAddress;
  1014. end;
  1015. {$endif FPC_HAS_UNICODESTR_COPY}
  1016. {$ifndef FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1017. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1018. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  1019. var
  1020. i,MaxLen : SizeInt;
  1021. pc : punicodechar;
  1022. begin
  1023. Pos:=0;
  1024. if Length(SubStr)>0 then
  1025. begin
  1026. MaxLen:=Length(source)-Length(SubStr);
  1027. i:=0;
  1028. pc:=@source[1];
  1029. while (i<=MaxLen) do
  1030. begin
  1031. inc(i);
  1032. if (SubStr[1]=pc^) and
  1033. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1034. begin
  1035. Pos:=i;
  1036. exit;
  1037. end;
  1038. inc(pc);
  1039. end;
  1040. end;
  1041. end;
  1042. {$endif FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1043. {$ifndef FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1044. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1045. { Faster version for a unicodechar alone }
  1046. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  1047. var
  1048. i: SizeInt;
  1049. pc : punicodechar;
  1050. begin
  1051. pc:=@s[1];
  1052. for i:=1 to length(s) do
  1053. begin
  1054. if pc^=c then
  1055. begin
  1056. pos:=i;
  1057. exit;
  1058. end;
  1059. inc(pc);
  1060. end;
  1061. pos:=0;
  1062. end;
  1063. {$endif FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1064. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  1065. block, which is significant bloat without any sensible speed improvement. }
  1066. Function Pos (const c : RawByteString; Const s : UnicodeString) : SizeInt;
  1067. begin
  1068. result:=Pos(UnicodeString(c),s);
  1069. end;
  1070. Function Pos (const c : ShortString; Const s : UnicodeString) : SizeInt;
  1071. begin
  1072. result:=Pos(UnicodeString(c),s);
  1073. end;
  1074. Function Pos (const c : UnicodeString; Const s : RawByteString) : SizeInt;
  1075. begin
  1076. result:=Pos(c,UnicodeString(s));
  1077. end;
  1078. {$ifndef FPC_HAS_POS_CHAR_UNICODESTR}
  1079. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  1080. { Faster version for a char alone. Must be implemented because }
  1081. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1082. { using pos(char,pchar) will always call the shortstring version }
  1083. { (exact match for first argument), also with $h+ (JM) }
  1084. Function Pos (c : Char; Const s : UnicodeString) : SizeInt;
  1085. var
  1086. i: SizeInt;
  1087. wc : unicodechar;
  1088. pc : punicodechar;
  1089. begin
  1090. wc:=c;
  1091. pc:=@s[1];
  1092. for i:=1 to length(s) do
  1093. begin
  1094. if pc^=wc then
  1095. begin
  1096. pos:=i;
  1097. exit;
  1098. end;
  1099. inc(pc);
  1100. end;
  1101. pos:=0;
  1102. end;
  1103. {$endif FPC_HAS_POS_CHAR_UNICODESTR}
  1104. {$ifndef FPC_HAS_DELETE_UNICODESTR}
  1105. {$define FPC_HAS_DELETE_UNICODESTR}
  1106. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  1107. Var
  1108. LS : SizeInt;
  1109. begin
  1110. LS:=Length(S);
  1111. if (Index>LS) or (Index<=0) or (Size<=0) then
  1112. exit;
  1113. UniqueString (S);
  1114. { (Size+Index) will overflow if Size=MaxInt. }
  1115. if Size>LS-Index then
  1116. Size:=LS-Index+1;
  1117. if Size<=LS-Index then
  1118. begin
  1119. Dec(Index);
  1120. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1121. end;
  1122. Setlength(s,LS-Size);
  1123. end;
  1124. {$endif FPC_HAS_DELETE_UNICODESTR}
  1125. {$ifndef FPC_HAS_INSERT_UNICODESTR}
  1126. {$define FPC_HAS_INSERT_UNICODESTR}
  1127. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1128. var
  1129. Temp : UnicodeString;
  1130. LS : SizeInt;
  1131. begin
  1132. If Length(Source)=0 then
  1133. exit;
  1134. if index <= 0 then
  1135. index := 1;
  1136. Ls:=Length(S);
  1137. if index > LS then
  1138. index := LS+1;
  1139. Dec(Index);
  1140. SetLength(Temp,Length(Source)+LS);
  1141. If Index>0 then
  1142. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1143. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1144. If (LS-Index)>0 then
  1145. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1146. S:=Temp;
  1147. end;
  1148. {$endif FPC_HAS_INSERT_UNICODESTR}
  1149. {$ifndef FPC_HAS_UPCASE_UNICODECHAR}
  1150. {$define FPC_HAS_UPCASE_UNICODECHAR}
  1151. Function UpCase(c:UnicodeChar):UnicodeChar;
  1152. var
  1153. s : UnicodeString;
  1154. begin
  1155. s:=c;
  1156. result:=widestringmanager.UpperUnicodeStringProc(s)[1];
  1157. end;
  1158. {$endif FPC_HAS_UPCASE_UNICODECHAR}
  1159. {$ifndef FPC_HAS_UPCASE_UNICODESTR}
  1160. {$define FPC_HAS_UPCASE_UNICODESTR}
  1161. function UpCase(const s : UnicodeString) : UnicodeString;
  1162. begin
  1163. result:=widestringmanager.UpperUnicodeStringProc(s);
  1164. end;
  1165. {$endif FPC_HAS_UPCASE_UNICODESTR}
  1166. {$ifndef FPC_HAS_LOWERCASE_UNICODECHAR}
  1167. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  1168. Function LowerCase(c:UnicodeChar):UnicodeChar;
  1169. var
  1170. s : UnicodeString;
  1171. begin
  1172. s:=c;
  1173. result:=widestringmanager.LowerUnicodeStringProc(s)[1];
  1174. end;
  1175. {$endif FPC_HAS_LOWERCASE_UNICODECHAR}
  1176. {$ifndef FPC_HAS_LOWERCASE_UNICODESTR}
  1177. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  1178. function LowerCase(const s : UnicodeString) : UnicodeString;
  1179. begin
  1180. result:=widestringmanager.LowerUnicodeStringProc(s);
  1181. end;
  1182. {$endif FPC_HAS_LOWERCASE_UNICODESTR}
  1183. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1184. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1185. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  1186. begin
  1187. SetLength(S,Len);
  1188. If (Buf<>Nil) and (Len>0) then
  1189. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1190. end;
  1191. {$endif FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1192. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1193. {$define FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1194. Procedure SetString (Out S : UnicodeString; Buf : PChar; Len : SizeInt);
  1195. begin
  1196. If (Buf<>Nil) and (Len>0) then
  1197. widestringmanager.Ansi2UnicodeMoveProc(Buf,DefaultSystemCodePage,S,Len)
  1198. else
  1199. SetLength(S,Len);
  1200. end;
  1201. {$endif FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1202. {$ifndef FPUNONE}
  1203. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1204. Var
  1205. SS: ShortString;
  1206. begin
  1207. fpc_Val_Real_UnicodeStr:=0;
  1208. if length(S)>255 then
  1209. code:=256
  1210. else
  1211. begin
  1212. SS:=ShortString(S);
  1213. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1214. end;
  1215. end;
  1216. {$endif}
  1217. {$ifndef FPC_STR_ENUM_INTERN}
  1218. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1219. var
  1220. ss: ShortString;
  1221. begin
  1222. if length(s)>255 then
  1223. code:=256
  1224. else
  1225. begin
  1226. ss:=ShortString(s);
  1227. val(ss,fpc_val_enum_unicodestr,code);
  1228. end;
  1229. end;
  1230. {$endif FPC_STR_ENUM_INTERN}
  1231. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1232. Var
  1233. SS: ShortString;
  1234. begin
  1235. if length(S)>255 then
  1236. begin
  1237. fpc_Val_Currency_UnicodeStr:=0;
  1238. code:=256;
  1239. end
  1240. else
  1241. begin
  1242. SS:=ShortString(S);
  1243. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1244. end;
  1245. end;
  1246. Function fpc_Val_UInt_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_UNICODESTR']; compilerproc;
  1247. Var
  1248. SS: ShortString;
  1249. begin
  1250. fpc_Val_UInt_UnicodeStr:=0;
  1251. if length(S)>255 then
  1252. code:=256
  1253. else
  1254. begin
  1255. SS:=ShortString(S);
  1256. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1257. end;
  1258. end;
  1259. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1260. Var
  1261. SS: ShortString;
  1262. begin
  1263. fpc_Val_SInt_UnicodeStr:=0;
  1264. if length(S)>255 then
  1265. code:=256
  1266. else
  1267. begin
  1268. SS:=ShortString(S);
  1269. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1270. end;
  1271. end;
  1272. {$ifndef CPU64}
  1273. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1274. Var
  1275. SS: ShortString;
  1276. begin
  1277. fpc_Val_qword_UnicodeStr:=0;
  1278. if length(S)>255 then
  1279. code:=256
  1280. else
  1281. begin
  1282. SS:=ShortString(S);
  1283. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1284. end;
  1285. end;
  1286. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1287. Var
  1288. SS: ShortString;
  1289. begin
  1290. fpc_Val_int64_UnicodeStr:=0;
  1291. if length(S)>255 then
  1292. code:=256
  1293. else
  1294. begin
  1295. SS:=ShortString(S);
  1296. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1297. end;
  1298. end;
  1299. {$endif CPU64}
  1300. {$if defined(CPU16) or defined(CPU8)}
  1301. Function fpc_Val_longword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_UNICODESTR']; compilerproc;
  1302. Var
  1303. SS: ShortString;
  1304. begin
  1305. fpc_Val_longword_UnicodeStr:=0;
  1306. if length(S)>255 then
  1307. code:=256
  1308. else
  1309. begin
  1310. SS:=ShortString(S);
  1311. Val(SS,fpc_Val_longword_UnicodeStr,Code);
  1312. end;
  1313. end;
  1314. Function fpc_Val_longint_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_UNICODESTR']; compilerproc;
  1315. Var
  1316. SS: ShortString;
  1317. begin
  1318. fpc_Val_longint_UnicodeStr:=0;
  1319. if length(S)>255 then
  1320. code:=256
  1321. else
  1322. begin
  1323. SS:=ShortString(S);
  1324. Val(SS,fpc_Val_longint_UnicodeStr,Code);
  1325. end;
  1326. end;
  1327. {$endif CPU16 or CPU8}
  1328. {$ifndef FPUNONE}
  1329. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1330. var
  1331. ss: shortstring;
  1332. begin
  1333. str_real(len,fr,d,treal_type(rt),ss);
  1334. s:=UnicodeString(ss);
  1335. end;
  1336. {$endif}
  1337. {$ifndef FPC_STR_ENUM_INTERN}
  1338. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1339. var
  1340. ss: ShortString;
  1341. begin
  1342. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1343. s:=UnicodeString(ss);
  1344. end;
  1345. {$endif FPC_STR_ENUM_INTERN}
  1346. procedure fpc_unicodestr_bool(b : boolean;len:sizeint;out s:unicodestring);compilerproc;
  1347. var
  1348. ss: ShortString;
  1349. begin
  1350. fpc_shortstr_bool(b,len,ss);
  1351. s:=UnicodeString(ss);
  1352. end;
  1353. {$ifdef FPC_HAS_STR_CURRENCY}
  1354. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1355. var
  1356. ss: shortstring;
  1357. begin
  1358. str(c:len:fr,ss);
  1359. s:=UnicodeString(ss);
  1360. end;
  1361. {$endif FPC_HAS_STR_CURRENCY}
  1362. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1363. Var
  1364. SS: ShortString;
  1365. begin
  1366. Str (v:Len,SS);
  1367. S:=UnicodeString(SS);
  1368. end;
  1369. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1370. Var
  1371. SS: ShortString;
  1372. begin
  1373. str(v:Len,SS);
  1374. S:=UnicodeString(SS);
  1375. end;
  1376. {$ifndef CPU64}
  1377. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1378. Var
  1379. SS: ShortString;
  1380. begin
  1381. Str (v:Len,SS);
  1382. S:=UnicodeString(SS);
  1383. end;
  1384. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1385. Var
  1386. SS: ShortString;
  1387. begin
  1388. str(v:Len,SS);
  1389. S:=UnicodeString(SS);
  1390. end;
  1391. {$endif CPU64}
  1392. {$if defined(CPU16) or defined(CPU8)}
  1393. Procedure fpc_UnicodeStr_LongInt(v : LongInt; Len : SizeInt; out S : UnicodeString);compilerproc;
  1394. Var
  1395. SS: ShortString;
  1396. begin
  1397. Str (v:Len,SS);
  1398. S:=UnicodeString(SS);
  1399. end;
  1400. Procedure fpc_UnicodeStr_LongWord(v : LongWord;Len : SizeInt; out S : UnicodeString);compilerproc;
  1401. Var
  1402. SS: ShortString;
  1403. begin
  1404. str(v:Len,SS);
  1405. S:=UnicodeString(SS);
  1406. end;
  1407. {$endif CPU16 or CPU8}
  1408. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1409. begin
  1410. if assigned(Source) then
  1411. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,Length(Source))
  1412. else
  1413. Result:=0;
  1414. end;
  1415. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1416. var
  1417. i,j : SizeUInt;
  1418. lw : longword;
  1419. begin
  1420. result:=0;
  1421. if source=nil then
  1422. exit;
  1423. i:=0;
  1424. j:=0;
  1425. if assigned(Dest) then
  1426. begin
  1427. while (i<SourceChars) and (j<MaxDestBytes) do
  1428. begin
  1429. lw:=ord(Source[i]);
  1430. case lw of
  1431. 0..$7f:
  1432. begin
  1433. Dest[j]:=char(lw);
  1434. inc(j);
  1435. end;
  1436. $80..$7ff:
  1437. begin
  1438. if j+1>=MaxDestBytes then
  1439. break;
  1440. Dest[j]:=char($c0 or (lw shr 6));
  1441. Dest[j+1]:=char($80 or (lw and $3f));
  1442. inc(j,2);
  1443. end;
  1444. $800..$d7ff,$e000..$ffff:
  1445. begin
  1446. if j+2>=MaxDestBytes then
  1447. break;
  1448. Dest[j]:=char($e0 or (lw shr 12));
  1449. Dest[j+1]:=char($80 or ((lw shr 6) and $3f));
  1450. Dest[j+2]:=char($80 or (lw and $3f));
  1451. inc(j,3);
  1452. end;
  1453. $d800..$dbff:
  1454. {High Surrogates}
  1455. begin
  1456. if j+3>=MaxDestBytes then
  1457. break;
  1458. if (i+1<sourcechars) and
  1459. (word(Source[i+1]) >= $dc00) and
  1460. (word(Source[i+1]) <= $dfff) then
  1461. begin
  1462. { $d7c0 is ($d800 - ($10000 shr 10)) }
  1463. lw:=(longword(lw-$d7c0) shl 10) + (ord(source[i+1]) xor $dc00);
  1464. Dest[j]:=char($f0 or (lw shr 18));
  1465. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1466. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1467. Dest[j+3]:=char($80 or (lw and $3f));
  1468. inc(j,4);
  1469. inc(i);
  1470. end;
  1471. end;
  1472. end;
  1473. inc(i);
  1474. end;
  1475. if j>SizeUInt(MaxDestBytes-1) then
  1476. j:=MaxDestBytes-1;
  1477. Dest[j]:=#0;
  1478. end
  1479. else
  1480. begin
  1481. while i<SourceChars do
  1482. begin
  1483. case word(Source[i]) of
  1484. $0..$7f:
  1485. inc(j);
  1486. $80..$7ff:
  1487. inc(j,2);
  1488. $800..$d7ff,$e000..$ffff:
  1489. inc(j,3);
  1490. $d800..$dbff:
  1491. begin
  1492. if (i+1<sourcechars) and
  1493. (word(Source[i+1]) >= $dc00) and
  1494. (word(Source[i+1]) <= $dfff) then
  1495. begin
  1496. inc(j,4);
  1497. inc(i);
  1498. end;
  1499. end;
  1500. end;
  1501. inc(i);
  1502. end;
  1503. end;
  1504. result:=j+1;
  1505. end;
  1506. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1507. begin
  1508. if assigned(Source) then
  1509. Result:=Utf8ToUnicode(Dest,MaxChars,Source,length(Source))
  1510. else
  1511. Result:=0;
  1512. end;
  1513. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1514. const
  1515. UNICODE_INVALID=63;
  1516. var
  1517. InputUTF8: SizeUInt;
  1518. IBYTE: BYTE;
  1519. OutputUnicode: SizeUInt;
  1520. PRECHAR: SizeUInt;
  1521. TempBYTE: BYTE;
  1522. CharLen: SizeUint;
  1523. LookAhead: SizeUInt;
  1524. UC: SizeUInt;
  1525. begin
  1526. if not assigned(Source) then
  1527. begin
  1528. result:=0;
  1529. exit;
  1530. end;
  1531. result:=SizeUInt(-1);
  1532. InputUTF8:=0;
  1533. OutputUnicode:=0;
  1534. PreChar:=0;
  1535. if Assigned(Dest) Then
  1536. begin
  1537. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1538. begin
  1539. IBYTE:=byte(Source[InputUTF8]);
  1540. if (IBYTE and $80) = 0 then
  1541. begin
  1542. //One character US-ASCII, convert it to unicode
  1543. if IBYTE = 10 then
  1544. begin
  1545. If (PreChar<>13) and FALSE then
  1546. begin
  1547. //Expand to crlf, conform UTF-8.
  1548. //This procedure will break the memory alocation by
  1549. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1550. if OutputUnicode+1<MaxDestChars then
  1551. begin
  1552. Dest[OutputUnicode]:=WideChar(13);
  1553. inc(OutputUnicode);
  1554. Dest[OutputUnicode]:=WideChar(10);
  1555. inc(OutputUnicode);
  1556. PreChar:=10;
  1557. end
  1558. else
  1559. begin
  1560. Dest[OutputUnicode]:=WideChar(13);
  1561. inc(OutputUnicode);
  1562. end;
  1563. end
  1564. else
  1565. begin
  1566. Dest[OutputUnicode]:=WideChar(IBYTE);
  1567. inc(OutputUnicode);
  1568. PreChar:=IBYTE;
  1569. end;
  1570. end
  1571. else
  1572. begin
  1573. Dest[OutputUnicode]:=WideChar(IBYTE);
  1574. inc(OutputUnicode);
  1575. PreChar:=IBYTE;
  1576. end;
  1577. inc(InputUTF8);
  1578. end
  1579. else
  1580. begin
  1581. TempByte:=IBYTE;
  1582. CharLen:=0;
  1583. while (TempBYTE and $80)<>0 do
  1584. begin
  1585. TempBYTE:=(TempBYTE shl 1) and $FE;
  1586. inc(CharLen);
  1587. end;
  1588. //Test for the "CharLen" conforms UTF-8 string
  1589. //This means the 10xxxxxx pattern.
  1590. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1591. begin
  1592. //Insuficient chars in string to decode
  1593. //UTF-8 array. Fallback to single char.
  1594. CharLen:= 1;
  1595. end;
  1596. for LookAhead := 1 to CharLen-1 do
  1597. begin
  1598. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1599. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1600. begin
  1601. //Invalid UTF-8 sequence, fallback.
  1602. CharLen:= LookAhead;
  1603. break;
  1604. end;
  1605. end;
  1606. UC:=$FFFF;
  1607. case CharLen of
  1608. 1: begin
  1609. //Not valid UTF-8 sequence
  1610. UC:=UNICODE_INVALID;
  1611. end;
  1612. 2: begin
  1613. //Two bytes UTF, convert it
  1614. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1615. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1616. if UC <= $7F then
  1617. begin
  1618. //Invalid UTF sequence.
  1619. UC:=UNICODE_INVALID;
  1620. end;
  1621. end;
  1622. 3: begin
  1623. //Three bytes, convert it to unicode
  1624. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1625. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1626. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1627. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1628. begin
  1629. //Invalid UTF-8 sequence
  1630. UC:= UNICODE_INVALID;
  1631. End;
  1632. end;
  1633. 4: begin
  1634. //Four bytes, convert it to two unicode characters
  1635. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1636. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1637. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1638. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1639. if (UC < $10000) or (UC > $10FFFF) then
  1640. begin
  1641. UC:= UNICODE_INVALID;
  1642. end
  1643. else
  1644. begin
  1645. { only store pair if room }
  1646. dec(UC,$10000);
  1647. if (OutputUnicode<MaxDestChars-1) then
  1648. begin
  1649. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1650. inc(OutputUnicode);
  1651. UC:=(UC and $3ff) + $DC00;
  1652. end
  1653. else
  1654. begin
  1655. InputUTF8:= InputUTF8 + CharLen;
  1656. { don't store anything }
  1657. CharLen:=0;
  1658. end;
  1659. end;
  1660. end;
  1661. 5,6,7: begin
  1662. //Invalid UTF8 to unicode conversion,
  1663. //mask it as invalid UNICODE too.
  1664. UC:=UNICODE_INVALID;
  1665. end;
  1666. end;
  1667. if CharLen > 0 then
  1668. begin
  1669. PreChar:=UC;
  1670. Dest[OutputUnicode]:=WideChar(UC);
  1671. inc(OutputUnicode);
  1672. end;
  1673. InputUTF8:= InputUTF8 + CharLen;
  1674. end;
  1675. end;
  1676. Result:=OutputUnicode+1;
  1677. end
  1678. else
  1679. begin
  1680. while (InputUTF8<SourceBytes) do
  1681. begin
  1682. IBYTE:=byte(Source[InputUTF8]);
  1683. if (IBYTE and $80) = 0 then
  1684. begin
  1685. //One character US-ASCII, convert it to unicode
  1686. if IBYTE = 10 then
  1687. begin
  1688. if (PreChar<>13) and FALSE then
  1689. begin
  1690. //Expand to crlf, conform UTF-8.
  1691. //This procedure will break the memory alocation by
  1692. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1693. inc(OutputUnicode,2);
  1694. PreChar:=10;
  1695. end
  1696. else
  1697. begin
  1698. inc(OutputUnicode);
  1699. PreChar:=IBYTE;
  1700. end;
  1701. end
  1702. else
  1703. begin
  1704. inc(OutputUnicode);
  1705. PreChar:=IBYTE;
  1706. end;
  1707. inc(InputUTF8);
  1708. end
  1709. else
  1710. begin
  1711. TempByte:=IBYTE;
  1712. CharLen:=0;
  1713. while (TempBYTE and $80)<>0 do
  1714. begin
  1715. TempBYTE:=(TempBYTE shl 1) and $FE;
  1716. inc(CharLen);
  1717. end;
  1718. //Test for the "CharLen" conforms UTF-8 string
  1719. //This means the 10xxxxxx pattern.
  1720. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1721. begin
  1722. //Insuficient chars in string to decode
  1723. //UTF-8 array. Fallback to single char.
  1724. CharLen:= 1;
  1725. end;
  1726. for LookAhead := 1 to CharLen-1 do
  1727. begin
  1728. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1729. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1730. begin
  1731. //Invalid UTF-8 sequence, fallback.
  1732. CharLen:= LookAhead;
  1733. break;
  1734. end;
  1735. end;
  1736. UC:=$FFFF;
  1737. case CharLen of
  1738. 1: begin
  1739. //Not valid UTF-8 sequence
  1740. UC:=UNICODE_INVALID;
  1741. end;
  1742. 2: begin
  1743. //Two bytes UTF, convert it
  1744. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1745. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1746. if UC <= $7F then
  1747. begin
  1748. //Invalid UTF sequence.
  1749. UC:=UNICODE_INVALID;
  1750. end;
  1751. end;
  1752. 3: begin
  1753. //Three bytes, convert it to unicode
  1754. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1755. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1756. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1757. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1758. begin
  1759. //Invalid UTF-8 sequence
  1760. UC:= UNICODE_INVALID;
  1761. end;
  1762. end;
  1763. 4: begin
  1764. //Four bytes, convert it to two unicode characters
  1765. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1766. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1767. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1768. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1769. if (UC < $10000) or (UC > $10FFFF) then
  1770. UC:= UNICODE_INVALID
  1771. else
  1772. { extra character character }
  1773. inc(OutputUnicode);
  1774. end;
  1775. 5,6,7: begin
  1776. //Invalid UTF8 to unicode conversion,
  1777. //mask it as invalid UNICODE too.
  1778. UC:=UNICODE_INVALID;
  1779. end;
  1780. end;
  1781. if CharLen > 0 then
  1782. begin
  1783. PreChar:=UC;
  1784. inc(OutputUnicode);
  1785. end;
  1786. InputUTF8:= InputUTF8 + CharLen;
  1787. end;
  1788. end;
  1789. Result:=OutputUnicode+1;
  1790. end;
  1791. end;
  1792. function UTF8Encode(const s : RawByteString) : RawByteString; inline;
  1793. begin
  1794. Result:=UTF8Encode(UnicodeString(s));
  1795. end;
  1796. {$ifndef FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1797. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1798. function UTF8Encode(const s : UnicodeString) : RawByteString;
  1799. var
  1800. i : SizeInt;
  1801. hs : UTF8String;
  1802. begin
  1803. result:='';
  1804. if s='' then
  1805. exit;
  1806. SetLength(hs,length(s)*3);
  1807. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  1808. if i>0 then
  1809. begin
  1810. SetLength(hs,i-1);
  1811. result:=hs;
  1812. end;
  1813. end;
  1814. {$endif FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1815. {$ifndef FPC_HAS_UTF8DECODE_UNICODESTRING}
  1816. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  1817. function UTF8Decode(const s : RawByteString): UnicodeString;
  1818. var
  1819. i : SizeInt;
  1820. hs : UnicodeString;
  1821. begin
  1822. result:='';
  1823. if s='' then
  1824. exit;
  1825. SetLength(hs,length(s));
  1826. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  1827. if i>0 then
  1828. begin
  1829. SetLength(hs,i-1);
  1830. result:=hs;
  1831. end;
  1832. end;
  1833. {$endif FPC_HAS_UTF8DECODE_UNICODESTRING}
  1834. function AnsiToUtf8(const s : RawByteString): RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1835. begin
  1836. Result:=Utf8Encode(s);
  1837. end;
  1838. function Utf8ToAnsi(const s : RawByteString) : RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1839. begin
  1840. Result:=RawByteString(Utf8Decode(s));
  1841. end;
  1842. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String);
  1843. var
  1844. i, reslen: sizeint;
  1845. w: longint;
  1846. begin
  1847. reslen:=0;
  1848. i:=0;
  1849. { calculate required length }
  1850. while (i<len) do
  1851. begin
  1852. if (p[i]<=#$d7ff) or (p[i]>=#$e000) then
  1853. inc(i)
  1854. else if (p[i]<=#$dbff) and
  1855. (i+1<len) and
  1856. (p[i+1]>=#$dc00) and
  1857. (p[i+1]<=#$dfff) then
  1858. inc(i,2)
  1859. else
  1860. inc(i);
  1861. inc(reslen);
  1862. end;
  1863. SetLength(res,reslen+1); { +1 for null termination }
  1864. reslen:=0;
  1865. i:=0;
  1866. { do conversion }
  1867. while (i<len) do
  1868. begin
  1869. w:=ord(p[i]);
  1870. if (w<=$d7ff) or (w>=$e000) then
  1871. res[reslen]:=w
  1872. else if (w<=$dbff) and
  1873. (i+1<len) and
  1874. (p[i+1]>=#$dc00) and
  1875. (p[i+1]<=#$dfff) then
  1876. begin
  1877. res[reslen]:=(UCS4Char(w-$d7c0) shl 10)+(UCS4Char(p[i+1]) xor $dc00);
  1878. inc(i);
  1879. end
  1880. else { invalid surrogate pair }
  1881. res[reslen]:=w;
  1882. inc(i);
  1883. inc(reslen);
  1884. end;
  1885. res[reslen]:=0;
  1886. end;
  1887. {$ifndef FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1888. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1889. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  1890. begin
  1891. UCS4Encode(PWideChar(s),Length(s),result);
  1892. end;
  1893. {$endif FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1894. {$ifndef FPC_HAS_WIDESTR_TO_UCS4STRING}
  1895. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  1896. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1897. begin
  1898. UCS4Encode(PWideChar(s),Length(s),result);
  1899. end;
  1900. {$endif FPC_HAS_WIDESTR_TO_UCS4STRING}
  1901. {$ifndef FPC_HAS_UCS4STRING_TO_WIDESTR}
  1902. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  1903. { dest should point to previously allocated wide/unicodestring }
  1904. procedure UCS4Decode(const s: UCS4String; dest: PWideChar);
  1905. var
  1906. i: sizeint;
  1907. nc: UCS4Char;
  1908. begin
  1909. for i:=0 to length(s)-2 do { -2 because s contains explicit terminating #0 }
  1910. begin
  1911. nc:=s[i];
  1912. if (nc<$ffff) then
  1913. dest^:=widechar(nc)
  1914. else if (dword(nc)<=$10ffff) then
  1915. begin
  1916. dest^:=widechar(nc shr 10 + $d7c0);
  1917. { subtracting $10000 doesn't change low 10 bits }
  1918. dest[1]:=widechar(nc and $3ff + $dc00);
  1919. inc(dest);
  1920. end
  1921. else { invalid code point }
  1922. dest^:='?';
  1923. inc(dest);
  1924. end;
  1925. end;
  1926. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  1927. var
  1928. i : SizeInt;
  1929. reslen : SizeInt;
  1930. begin
  1931. reslen:=0;
  1932. for i:=0 to length(s)-2 do { skip terminating #0 }
  1933. Inc(reslen,1+ord((s[i]>$ffff) and (s[i]<=$10ffff)));
  1934. SetLength(result,reslen);
  1935. UCS4Decode(s,pointer(result));
  1936. end;
  1937. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1938. var
  1939. i : SizeInt;
  1940. reslen : SizeInt;
  1941. begin
  1942. reslen:=0;
  1943. for i:=0 to length(s)-2 do { skip terminating #0 }
  1944. Inc(reslen,1+ord((s[i]>$ffff) and (s[i]<=$10ffff)));
  1945. SetLength(result,reslen);
  1946. UCS4Decode(s,pointer(result));
  1947. end;
  1948. {$endif FPC_HAS_UCS4STRING_TO_WIDESTR}
  1949. {$ifndef FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  1950. const
  1951. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  1952. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  1953. procedure unimplementedunicodestring;
  1954. begin
  1955. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1956. If IsConsole then
  1957. begin
  1958. Writeln(StdErr,SNoUnicodestrings);
  1959. Writeln(StdErr,SRecompileWithUnicodestrings);
  1960. end;
  1961. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1962. HandleErrorAddrFrameInd(233,get_pc_addr,get_frame);
  1963. end;
  1964. function StringElementSize(const S: UnicodeString): Word; overload;
  1965. begin
  1966. if assigned(Pointer(S)) then
  1967. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.ElementSize
  1968. else
  1969. Result:=SizeOf(UnicodeChar);
  1970. end;
  1971. function StringRefCount(const S: UnicodeString): SizeInt; overload;
  1972. begin
  1973. if assigned(Pointer(S)) then
  1974. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.Ref
  1975. else
  1976. Result:=0;
  1977. end;
  1978. function StringCodePage(const S: UnicodeString): TSystemCodePage; overload;
  1979. begin
  1980. {$ifdef FPC_HAS_CPSTRING}
  1981. if assigned(Pointer(S)) then
  1982. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.CodePage
  1983. else
  1984. {$endif FPC_HAS_CPSTRING}
  1985. Result:=DefaultUnicodeCodePage;
  1986. end;
  1987. {$warnings off}
  1988. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  1989. begin
  1990. unimplementedunicodestring;
  1991. end;
  1992. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  1993. begin
  1994. unimplementedunicodestring;
  1995. end;
  1996. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  1997. begin
  1998. unimplementedunicodestring;
  1999. end;
  2000. {$warnings on}
  2001. procedure initunicodestringmanager;
  2002. begin
  2003. {$ifndef HAS_WIDESTRINGMANAGER}
  2004. widestringmanager.Unicode2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  2005. widestringmanager.Ansi2UnicodeMoveProc:=@DefaultAnsi2UnicodeMove;
  2006. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  2007. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  2008. {$endif HAS_WIDESTRINGMANAGER}
  2009. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  2010. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  2011. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2012. {$ifndef HAS_WIDESTRINGMANAGER}
  2013. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2014. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2015. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  2016. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  2017. {$endif HAS_WIDESTRINGMANAGER}
  2018. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  2019. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  2020. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  2021. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  2022. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2023. widestringmanager.GetStandardCodePageProc:=@DefaultGetStandardCodePage;
  2024. end;
  2025. {$endif FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  2026. {$ifndef FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2027. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2028. Function ToSingleByteFileSystemEncodedFileName(const Str: UnicodeString): RawByteString;
  2029. Begin
  2030. widestringmanager.Unicode2AnsiMoveProc(punicodechar(Str),Result,
  2031. DefaultFileSystemCodePage,Length(Str));
  2032. End;
  2033. {$endif FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2034. {$ifndef FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2035. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2036. Function ToSingleByteFileSystemEncodedFileName(const arr: array of widechar): RawByteString;
  2037. Begin
  2038. widestringmanager.Unicode2AnsiMoveProc(@arr[0],Result,
  2039. DefaultFileSystemCodePage,length(pwidechar(@arr[0])));
  2040. End;
  2041. {$endif FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2042. Function ToSingleByteFileSystemEncodedFileName(const Str: RawByteString): RawByteString;
  2043. Begin
  2044. Result:=Str;
  2045. SetCodePage(Result,DefaultFileSystemCodePage,True);
  2046. End;