ustrings.inc 70 KB

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