ustrings.inc 71 KB

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