ustrings.inc 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  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. lens, lena : SizeUInt;
  859. begin
  860. if (l>0) then
  861. begin
  862. if Pointer(S)=nil then
  863. begin
  864. { Need a complete new string...}
  865. Pointer(s):=NewUnicodeString(l);
  866. end
  867. else
  868. if (PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref = 1) then
  869. begin
  870. Temp:=Pointer(s)-UnicodeFirstOff;
  871. lens:=MemSize(Temp);
  872. lena:=SizeUInt(L*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar)));
  873. if (lena>lens) or ((lens>32) and (lena<=SizeInt(SizeUint(lens) div 2))) then
  874. Pointer(S):=reallocmem(Temp, lena)+UnicodeFirstOff;
  875. end
  876. else
  877. begin
  878. { Reallocation is needed... }
  879. Temp:=NewUnicodeString(l);
  880. { also move terminating null }
  881. lens:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len+1;
  882. if l<lens then
  883. lens:=l;
  884. Move(Pointer(S)^,Temp^,lens * Sizeof(UnicodeChar));
  885. fpc_unicodestr_decr_ref(Pointer(S));
  886. Pointer(S):=Temp;
  887. end;
  888. { Force nil termination in case it gets shorter }
  889. PWord(Pointer(S)+l*sizeof(UnicodeChar))^:=0;
  890. PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len:=l;
  891. end
  892. else { length=0, deallocate the string }
  893. fpc_unicodestr_decr_ref (Pointer(S));
  894. end;
  895. {$endif FPC_HAS_UNICODESTR_SETLENGTH}
  896. {*****************************************************************************
  897. Public functions, In interface.
  898. *****************************************************************************}
  899. function UnicodeCharToString(S : PUnicodeChar) : UnicodeString;
  900. begin
  901. result:=UnicodeCharLenToString(s,Length(UnicodeString(s)));
  902. end;
  903. {$ifndef FPC_HAS_STRING_TO_UNICODECHAR}
  904. {$define FPC_HAS_STRING_TO_UNICODECHAR}
  905. function StringToUnicodeChar(const Src : RawByteString;Dest : PUnicodeChar;DestSize : SizeInt) : PUnicodeChar;
  906. begin
  907. result:=StringToWideChar(Src,Dest,DestSize);
  908. end;
  909. {$endif FPC_HAS_STRING_TO_UNICODECHAR}
  910. function WideCharToString(S : PWideChar) : UnicodeString;
  911. begin
  912. result:=WideCharLenToString(s,Length(WideString(s)));
  913. end;
  914. {$ifndef FPC_HAS_STRING_LEN_TO_WIDECHAR}
  915. {$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
  916. function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  917. var
  918. temp: widestring;
  919. Len: SizeInt;
  920. begin
  921. widestringmanager.Ansi2WideMoveProc(PAnsiChar(Src),StringCodePage(Src),temp,Length(Src));
  922. Len:=Length(temp);
  923. if DestSize<=Len then
  924. Len:=Destsize-1;
  925. move(temp[1],Dest^,Len*SizeOf(WideChar));
  926. Dest[Len]:=#0;
  927. result:=Dest;
  928. end;
  929. {$endif FPC_HAS_STRING_LEN_TO_WIDECHAR}
  930. {$ifndef FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  931. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  932. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  933. begin
  934. SetLength(result,Len);
  935. Move(S^,Pointer(Result)^,Len*2);
  936. end;
  937. {$endif FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  938. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : UnicodeString);
  939. begin
  940. Dest:=UnicodeCharLenToString(Src,Len);
  941. end;
  942. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : AnsiString);
  943. begin
  944. Dest:=AnsiString(UnicodeCharLenToString(Src,Len));
  945. end;
  946. procedure UnicodeCharToStrVar(S : PUnicodeChar;out Dest : AnsiString);
  947. begin
  948. Dest:=AnsiString(UnicodeCharToString(S));
  949. end;
  950. {$ifndef FPC_HAS_WIDECHAR_LEN_TO_STRING}
  951. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  952. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  953. begin
  954. SetLength(result,Len);
  955. Move(S^,Pointer(Result)^,Len*2);
  956. end;
  957. {$endif FPC_HAS_WIDECHAR_LEN_TO_STRING}
  958. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : UnicodeString);
  959. begin
  960. Dest:=WideCharLenToString(Src,Len);
  961. end;
  962. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  963. begin
  964. Dest:=AnsiString(WideCharLenToString(Src,Len));
  965. end;
  966. procedure WideCharToStrVar(S : PWideChar;out Dest : UnicodeString);
  967. begin
  968. Dest:=WideCharToString(S);
  969. end;
  970. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  971. begin
  972. Dest:=AnsiString(WideCharToString(S));
  973. end;
  974. Function fpc_unicodestr_Unique_func(Var S : UnicodeString): Pointer; external name 'FPC_UNICODESTR_UNIQUE';
  975. Procedure UniqueString (Var S : UnicodeString);{$ifdef SYSTEMINLINE}inline;{$endif}
  976. begin
  977. fpc_unicodestr_Unique_func(S);
  978. end;
  979. {$ifndef FPC_HAS_UNICODESTR_UNIQUE}
  980. {$define FPC_HAS_UNICODESTR_UNIQUE}
  981. Function fpc_unicodestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_UNICODESTR_UNIQUE']; compilerproc;
  982. {
  983. Make sure reference count of S is 1,
  984. using copy-on-write semantics.
  985. }
  986. Var
  987. SNew : Pointer;
  988. L : SizeInt;
  989. begin
  990. pointer(result) := pointer(s);
  991. If (result<>nil) and (PUnicodeRec(result-UnicodeFirstOff)^.Ref<>1) then
  992. begin
  993. L:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.len;
  994. SNew:=NewUnicodeString (L);
  995. Move (PUnicodeChar(S)^,SNew^,(L+1)*sizeof(UnicodeChar));
  996. PUnicodeRec(SNew-UnicodeFirstOff)^.len:=L;
  997. fpc_unicodestr_decr_ref (Pointer(S)); { Thread safe }
  998. pointer(S):=SNew;
  999. pointer(result):=SNew;
  1000. end;
  1001. end;
  1002. {$endif FPC_HAS_UNICODESTR_UNIQUE}
  1003. {$ifndef FPC_HAS_UNICODESTR_COPY}
  1004. {$define FPC_HAS_UNICODESTR_COPY}
  1005. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  1006. var
  1007. Lim : SizeInt;
  1008. ResultAddress : Pointer;
  1009. begin
  1010. ResultAddress:=Nil;
  1011. dec(index);
  1012. if Index < 0 then
  1013. Index := 0;
  1014. Lim:=Length(S)-Index; { Cannot overflow as both Length(S) and Index are non-negative. }
  1015. if Size>Lim then
  1016. Size:=Lim;
  1017. If Size>0 then
  1018. begin
  1019. ResultAddress:=NewUnicodeString(Size);
  1020. Move (PUnicodeChar(S)[Index],ResultAddress^,Size*sizeof(UnicodeChar));
  1021. PUnicodeRec(ResultAddress-UnicodeFirstOff)^.Len:=Size;
  1022. PUnicodeChar(ResultAddress+Size*sizeof(UnicodeChar))^:=#0;
  1023. end;
  1024. fpc_unicodestr_decr_ref(Pointer(fpc_unicodestr_copy));
  1025. Pointer(fpc_unicodestr_Copy):=ResultAddress;
  1026. end;
  1027. {$endif FPC_HAS_UNICODESTR_COPY}
  1028. {$ifndef FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1029. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1030. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  1031. var
  1032. i,MaxLen,nsource,nsub,d : SizeInt;
  1033. begin
  1034. Pos:=0;
  1035. nsource:=Length(Source);
  1036. nsub:=Length(Substr);
  1037. if (nsub>0) and (Offset>0) and (Offset<=nsource) then
  1038. begin
  1039. MaxLen:=nsource-nsub+1;
  1040. i:=Offset;
  1041. while (i<=MaxLen) do
  1042. begin
  1043. d:=IndexWord(Source[i],MaxLen-i+1,word(Substr[1]));
  1044. if d<0 then
  1045. exit;
  1046. if CompareWord(Substr[1],Source[i+d],nsub)=0 then
  1047. exit(i+d);
  1048. i:=i+d+1;
  1049. end;
  1050. end;
  1051. end;
  1052. {$endif FPC_HAS_POS_UNICODESTR_UNICODESTR}
  1053. {$ifndef FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1054. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1055. { Faster version for a unicodechar alone }
  1056. Function Pos (c : UnicodeChar; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  1057. var
  1058. ns,idx: SizeInt;
  1059. begin
  1060. pos:=0;
  1061. ns:=length(s);
  1062. if (Offset>0) and (Offset<=ns) then
  1063. begin
  1064. idx:=IndexWord(s[Offset],ns-Offset+1,word(c));
  1065. if idx>=0 then
  1066. pos:=Offset+idx;
  1067. end;
  1068. end;
  1069. {$endif FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  1070. { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
  1071. block, which is significant bloat without any sensible speed improvement. }
  1072. Function Pos (const c : RawByteString; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  1073. begin
  1074. result:=Pos(UnicodeString(c),s,offset);
  1075. end;
  1076. Function Pos (const c : ShortString; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  1077. begin
  1078. result:=Pos(UnicodeString(c),s,OffSet);
  1079. end;
  1080. Function Pos (const c : UnicodeString; Const s : RawByteString; Offset: Sizeint = 1) : SizeInt;
  1081. begin
  1082. result:=Pos(c,UnicodeString(s),OffSet);
  1083. end;
  1084. {$ifndef FPC_HAS_UNICODESTR_OF_CHAR}
  1085. {$define FPC_HAS_UNICODESTR_OF_CHAR}
  1086. Function StringOfChar(c : Unicodechar;l : SizeInt) : UnicodeString;
  1087. begin
  1088. SetLength(StringOfChar,l);
  1089. FillWord(Pointer(StringOfChar)^,Length(StringOfChar),word(c));
  1090. end;
  1091. {$endif}
  1092. {$ifndef FPC_HAS_POS_CHAR_UNICODESTR}
  1093. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  1094. { Faster version for a AnsiChar alone. Must be implemented because }
  1095. { pos(c: AnsiChar; const s: shortstring) also exists, so otherwise }
  1096. { using pos(AnsiChar,pansichar) will always call the shortstring version }
  1097. { (exact match for first argument), also with $h+ (JM) }
  1098. Function Pos (c : AnsiChar; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  1099. var
  1100. ns,idx: SizeInt;
  1101. begin
  1102. pos:=0;
  1103. ns:=length(s);
  1104. if (Offset>0) and (Offset<=ns) then
  1105. begin
  1106. idx:=IndexWord(s[Offset],ns-Offset+1,word(unicodechar(c)));
  1107. if idx>=0 then
  1108. pos:=Offset+idx;
  1109. end;
  1110. end;
  1111. {$endif FPC_HAS_POS_CHAR_UNICODESTR}
  1112. {$ifndef FPC_HAS_DELETE_UNICODESTR}
  1113. {$define FPC_HAS_DELETE_UNICODESTR}
  1114. Procedure {$ifdef VER3_0}Delete{$else}fpc_unicodestr_delete{$endif}(Var S : UnicodeString; Index,Size: SizeInt);
  1115. Var
  1116. LS : SizeInt;
  1117. begin
  1118. LS:=Length(S);
  1119. if (Index>LS) or (Index<=0) or (Size<=0) then
  1120. exit;
  1121. UniqueString (S);
  1122. { (Size+Index) will overflow if Size=MaxInt. }
  1123. if Size>LS-Index then
  1124. Size:=LS-Index+1;
  1125. if Size<=LS-Index then
  1126. begin
  1127. Dec(Index);
  1128. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1129. end;
  1130. Setlength(s,LS-Size);
  1131. end;
  1132. {$endif FPC_HAS_DELETE_UNICODESTR}
  1133. {$ifndef FPC_HAS_INSERT_UNICODESTR}
  1134. {$define FPC_HAS_INSERT_UNICODESTR}
  1135. Procedure {$ifdef VER3_0}Insert{$else}fpc_unicodestr_insert{$endif}(Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1136. var
  1137. Temp : UnicodeString;
  1138. LS : SizeInt;
  1139. begin
  1140. If Length(Source)=0 then
  1141. exit;
  1142. if index <= 0 then
  1143. index := 1;
  1144. Ls:=Length(S);
  1145. if index > LS then
  1146. index := LS+1;
  1147. Dec(Index);
  1148. SetLength(Temp,Length(Source)+LS);
  1149. If Index>0 then
  1150. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1151. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1152. If (LS-Index)>0 then
  1153. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1154. S:=Temp;
  1155. end;
  1156. {$endif FPC_HAS_INSERT_UNICODESTR}
  1157. {$ifndef FPC_HAS_UPCASE_UNICODECHAR}
  1158. {$define FPC_HAS_UPCASE_UNICODECHAR}
  1159. Function UpCase(c:UnicodeChar):UnicodeChar;
  1160. begin
  1161. Result:= widestringmanager.UpperUnicodeStringProc(UnicodeString(c))[1]
  1162. end;
  1163. {$endif FPC_HAS_UPCASE_UNICODECHAR}
  1164. {$ifndef FPC_HAS_UPCASE_UNICODESTR}
  1165. {$define FPC_HAS_UPCASE_UNICODESTR}
  1166. function UpCase(const s : UnicodeString) : UnicodeString;
  1167. begin
  1168. result:=widestringmanager.UpperUnicodeStringProc(s);
  1169. end;
  1170. {$endif FPC_HAS_UPCASE_UNICODESTR}
  1171. {$ifndef FPC_HAS_LOWERCASE_UNICODECHAR}
  1172. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  1173. Function LowerCase(c:UnicodeChar):UnicodeChar;
  1174. begin
  1175. Result:= widestringmanager.LowerUnicodeStringProc(UnicodeString(c))[1]
  1176. end;
  1177. {$endif FPC_HAS_LOWERCASE_UNICODECHAR}
  1178. {$ifndef FPC_HAS_LOWERCASE_UNICODESTR}
  1179. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  1180. function LowerCase(const s : UnicodeString) : UnicodeString;
  1181. begin
  1182. result:=widestringmanager.LowerUnicodeStringProc(s);
  1183. end;
  1184. {$endif FPC_HAS_LOWERCASE_UNICODESTR}
  1185. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1186. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1187. 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}
  1188. begin
  1189. SetLength(S,Len);
  1190. If (Buf<>Nil) and (Len>0) then
  1191. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1192. end;
  1193. {$endif FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  1194. {$ifndef FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1195. {$define FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1196. 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}
  1197. begin
  1198. If (Buf<>Nil) and (Len>0) then
  1199. widestringmanager.Ansi2UnicodeMoveProc(Buf,DefaultSystemCodePage,S,Len)
  1200. else
  1201. SetLength(S,Len);
  1202. end;
  1203. {$endif FPC_HAS_SETSTRING_UNICODESTR_PCHAR}
  1204. {$ifndef FPUNONE}
  1205. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1206. Var
  1207. SS: ShortString;
  1208. begin
  1209. fpc_Val_Real_UnicodeStr:=0;
  1210. if length(S)>255 then
  1211. code:=256
  1212. else
  1213. begin
  1214. SS:=ShortString(S);
  1215. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1216. end;
  1217. end;
  1218. {$endif}
  1219. {$ifndef FPC_STR_ENUM_INTERN}
  1220. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1221. var
  1222. ss: ShortString;
  1223. begin
  1224. if length(s)>255 then
  1225. code:=256
  1226. else
  1227. begin
  1228. ss:=ShortString(s);
  1229. val(ss,fpc_val_enum_unicodestr,code);
  1230. end;
  1231. end;
  1232. {$endif FPC_STR_ENUM_INTERN}
  1233. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1234. Var
  1235. SS: ShortString;
  1236. begin
  1237. if length(S)>255 then
  1238. begin
  1239. fpc_Val_Currency_UnicodeStr:=0;
  1240. code:=256;
  1241. end
  1242. else
  1243. begin
  1244. SS:=ShortString(S);
  1245. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1246. end;
  1247. end;
  1248. 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;
  1249. Var
  1250. SS: ShortString;
  1251. begin
  1252. fpc_Val_UInt_UnicodeStr:=0;
  1253. if length(S)>255 then
  1254. code:=256
  1255. else
  1256. begin
  1257. SS:=ShortString(S);
  1258. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1259. end;
  1260. end;
  1261. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1262. Var
  1263. SS: ShortString;
  1264. begin
  1265. fpc_Val_SInt_UnicodeStr:=0;
  1266. if length(S)>255 then
  1267. code:=256
  1268. else
  1269. begin
  1270. SS:=ShortString(S);
  1271. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1272. end;
  1273. end;
  1274. {$ifndef CPU64}
  1275. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1276. Var
  1277. SS: ShortString;
  1278. begin
  1279. fpc_Val_qword_UnicodeStr:=0;
  1280. if length(S)>255 then
  1281. code:=256
  1282. else
  1283. begin
  1284. SS:=ShortString(S);
  1285. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1286. end;
  1287. end;
  1288. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1289. Var
  1290. SS: ShortString;
  1291. begin
  1292. fpc_Val_int64_UnicodeStr:=0;
  1293. if length(S)>255 then
  1294. code:=256
  1295. else
  1296. begin
  1297. SS:=ShortString(S);
  1298. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1299. end;
  1300. end;
  1301. {$endif CPU64}
  1302. {$if defined(CPU16) or defined(CPU8)}
  1303. Function fpc_Val_longword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_UNICODESTR']; compilerproc;
  1304. Var
  1305. SS: ShortString;
  1306. begin
  1307. fpc_Val_longword_UnicodeStr:=0;
  1308. if length(S)>255 then
  1309. code:=256
  1310. else
  1311. begin
  1312. SS:=ShortString(S);
  1313. Val(SS,fpc_Val_longword_UnicodeStr,Code);
  1314. end;
  1315. end;
  1316. Function fpc_Val_longint_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_UNICODESTR']; compilerproc;
  1317. Var
  1318. SS: ShortString;
  1319. begin
  1320. fpc_Val_longint_UnicodeStr:=0;
  1321. if length(S)>255 then
  1322. code:=256
  1323. else
  1324. begin
  1325. SS:=ShortString(S);
  1326. Val(SS,fpc_Val_longint_UnicodeStr,Code);
  1327. end;
  1328. end;
  1329. Function fpc_Val_word_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): word; [public, alias:'FPC_VAL_WORD_UNICODESTR']; compilerproc;
  1330. Var
  1331. SS: ShortString;
  1332. begin
  1333. fpc_Val_word_UnicodeStr:=0;
  1334. if length(S)>255 then
  1335. code:=256
  1336. else
  1337. begin
  1338. SS:=ShortString(S);
  1339. Val(SS,fpc_Val_word_UnicodeStr,Code);
  1340. end;
  1341. end;
  1342. Function fpc_Val_smallint_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): SmallInt; [public, alias:'FPC_VAL_SMALLINT_UNICODESTR']; compilerproc;
  1343. Var
  1344. SS: ShortString;
  1345. begin
  1346. fpc_Val_smallint_UnicodeStr:=0;
  1347. if length(S)>255 then
  1348. code:=256
  1349. else
  1350. begin
  1351. SS:=ShortString(S);
  1352. Val(SS,fpc_Val_smallint_UnicodeStr,Code);
  1353. end;
  1354. end;
  1355. {$endif CPU16 or CPU8}
  1356. {$ifndef FPUNONE}
  1357. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1358. var
  1359. ss: shortstring;
  1360. begin
  1361. str_real(len,fr,d,treal_type(rt),ss);
  1362. s:=UnicodeString(ss);
  1363. end;
  1364. {$endif}
  1365. {$ifndef FPC_STR_ENUM_INTERN}
  1366. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1367. var
  1368. ss: ShortString;
  1369. begin
  1370. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1371. s:=UnicodeString(ss);
  1372. end;
  1373. {$endif FPC_STR_ENUM_INTERN}
  1374. procedure fpc_unicodestr_bool(b : boolean;len:sizeint;out s:unicodestring);compilerproc;
  1375. var
  1376. ss: ShortString;
  1377. begin
  1378. fpc_shortstr_bool(b,len,ss);
  1379. s:=UnicodeString(ss);
  1380. end;
  1381. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1382. var
  1383. ss: shortstring;
  1384. begin
  1385. str(c:len:fr,ss);
  1386. s:=UnicodeString(ss);
  1387. end;
  1388. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1389. Var
  1390. SS: ShortString;
  1391. begin
  1392. Str (v:Len,SS);
  1393. S:=UnicodeString(SS);
  1394. end;
  1395. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1396. Var
  1397. SS: ShortString;
  1398. begin
  1399. str(v:Len,SS);
  1400. S:=UnicodeString(SS);
  1401. end;
  1402. {$ifndef CPU64}
  1403. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1404. Var
  1405. SS: ShortString;
  1406. begin
  1407. Str (v:Len,SS);
  1408. S:=UnicodeString(SS);
  1409. end;
  1410. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1411. Var
  1412. SS: ShortString;
  1413. begin
  1414. str(v:Len,SS);
  1415. S:=UnicodeString(SS);
  1416. end;
  1417. {$endif CPU64}
  1418. {$if defined(CPU16) or defined(CPU8)}
  1419. Procedure fpc_UnicodeStr_LongInt(v : LongInt; Len : SizeInt; out S : UnicodeString);compilerproc;
  1420. Var
  1421. SS: ShortString;
  1422. begin
  1423. Str (v:Len,SS);
  1424. S:=UnicodeString(SS);
  1425. end;
  1426. Procedure fpc_UnicodeStr_LongWord(v : LongWord;Len : SizeInt; out S : UnicodeString);compilerproc;
  1427. Var
  1428. SS: ShortString;
  1429. begin
  1430. str(v:Len,SS);
  1431. S:=UnicodeString(SS);
  1432. end;
  1433. Procedure fpc_UnicodeStr_SmallInt(v : SmallInt; Len : SizeInt; out S : UnicodeString);compilerproc;
  1434. Var
  1435. SS: ShortString;
  1436. begin
  1437. Str (v:Len,SS);
  1438. S:=UnicodeString(SS);
  1439. end;
  1440. Procedure fpc_UnicodeStr_Word(v : Word;Len : SizeInt; out S : UnicodeString);compilerproc;
  1441. Var
  1442. SS: ShortString;
  1443. begin
  1444. str(v:Len,SS);
  1445. S:=UnicodeString(SS);
  1446. end;
  1447. {$endif CPU16 or CPU8}
  1448. function UnicodeToUtf8(Dest: PAnsiChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1449. begin
  1450. if assigned(Source) then
  1451. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,Length(Source))
  1452. else
  1453. Result:=0;
  1454. end;
  1455. function UnicodeToUtf8(Dest: PAnsiChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1456. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1457. begin
  1458. runerror(217);
  1459. end;
  1460. {$else EXCLUDE_COMPLEX_PROCS}
  1461. var
  1462. i,j : SizeUInt;
  1463. lw : longword;
  1464. begin
  1465. result:=0;
  1466. if source=nil then
  1467. exit;
  1468. i:=0;
  1469. j:=0;
  1470. if assigned(Dest) then
  1471. begin
  1472. while (i<SourceChars) and (j<MaxDestBytes) do
  1473. begin
  1474. lw:=ord(Source[i]);
  1475. case lw of
  1476. 0..$7f:
  1477. begin
  1478. Dest[j]:=AnsiChar(lw);
  1479. inc(j);
  1480. end;
  1481. $80..$7ff:
  1482. begin
  1483. if j+1>=MaxDestBytes then
  1484. break;
  1485. Dest[j]:=AnsiChar($c0 or (lw shr 6));
  1486. Dest[j+1]:=AnsiChar($80 or (lw and $3f));
  1487. inc(j,2);
  1488. end;
  1489. $800..$d7ff,$e000..$ffff:
  1490. begin
  1491. if j+2>=MaxDestBytes then
  1492. break;
  1493. Dest[j]:=AnsiChar($e0 or (lw shr 12));
  1494. Dest[j+1]:=AnsiChar($80 or ((lw shr 6) and $3f));
  1495. Dest[j+2]:=AnsiChar($80 or (lw and $3f));
  1496. inc(j,3);
  1497. end;
  1498. $d800..$dbff:
  1499. {High Surrogates}
  1500. begin
  1501. if j+3>=MaxDestBytes then
  1502. break;
  1503. if (i+1<sourcechars) and
  1504. (word(Source[i+1]) >= $dc00) and
  1505. (word(Source[i+1]) <= $dfff) then
  1506. begin
  1507. { $d7c0 is ($d800 - ($10000 shr 10)) }
  1508. lw:=(longword(lw-$d7c0) shl 10) + (ord(source[i+1]) xor $dc00);
  1509. Dest[j]:=AnsiChar($f0 or (lw shr 18));
  1510. Dest[j+1]:=AnsiChar($80 or ((lw shr 12) and $3f));
  1511. Dest[j+2]:=AnsiChar($80 or ((lw shr 6) and $3f));
  1512. Dest[j+3]:=AnsiChar($80 or (lw and $3f));
  1513. inc(j,4);
  1514. inc(i);
  1515. end;
  1516. end;
  1517. end;
  1518. inc(i);
  1519. end;
  1520. if j>SizeUInt(MaxDestBytes-1) then
  1521. j:=MaxDestBytes-1;
  1522. Dest[j]:=#0;
  1523. end
  1524. else
  1525. begin
  1526. while i<SourceChars do
  1527. begin
  1528. case word(Source[i]) of
  1529. $0..$7f:
  1530. inc(j);
  1531. $80..$7ff:
  1532. inc(j,2);
  1533. $800..$d7ff,$e000..$ffff:
  1534. inc(j,3);
  1535. $d800..$dbff:
  1536. begin
  1537. if (i+1<sourcechars) and
  1538. (word(Source[i+1]) >= $dc00) and
  1539. (word(Source[i+1]) <= $dfff) then
  1540. begin
  1541. inc(j,4);
  1542. inc(i);
  1543. end;
  1544. end;
  1545. end;
  1546. inc(i);
  1547. end;
  1548. end;
  1549. result:=j+1;
  1550. end;
  1551. {$endif EXCLUDE_COMPLEX_PROCS}
  1552. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PAnsiChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1553. begin
  1554. if assigned(Source) then
  1555. Result:=Utf8ToUnicode(Dest,MaxChars,Source,length(Source),True)
  1556. else
  1557. Result:=0;
  1558. end;
  1559. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PAnsiChar; SourceBytes: SizeUInt): SizeUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1560. begin
  1561. Result:=Utf8ToUnicode(Dest,MaxDestChars,Source,SourceBytes,True);
  1562. end;
  1563. function Utf8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PAnsiChar; SourceBytes: SizeUInt; IgnoreInvalid : Boolean): SizeUInt;
  1564. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1565. begin
  1566. runerror(217);
  1567. end;
  1568. {$else EXCLUDE_COMPLEX_PROCS}
  1569. const
  1570. UNICODE_INVALID=63;
  1571. var
  1572. InputUTF8: SizeUInt;
  1573. IBYTE: BYTE;
  1574. OutputUnicode: SizeUInt;
  1575. PRECHAR: SizeUInt;
  1576. TempBYTE: BYTE;
  1577. CharLen: SizeUint;
  1578. LookAhead: SizeUInt;
  1579. UC: SizeUInt;
  1580. begin
  1581. if not assigned(Source) then
  1582. begin
  1583. result:=0;
  1584. exit;
  1585. end;
  1586. result:=SizeUInt(-1);
  1587. InputUTF8:=0;
  1588. OutputUnicode:=0;
  1589. PreChar:=0;
  1590. if Assigned(Dest) Then
  1591. begin
  1592. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1593. begin
  1594. IBYTE:=byte(Source[InputUTF8]);
  1595. if (IBYTE and $80) = 0 then
  1596. begin
  1597. // One character US-ASCII, convert it to unicode
  1598. // Commented code to convert LF to CRLF has been removed
  1599. Dest[OutputUnicode]:=WideChar(IBYTE);
  1600. inc(OutputUnicode);
  1601. PreChar:=IBYTE;
  1602. inc(InputUTF8);
  1603. end
  1604. else
  1605. begin
  1606. TempByte:=IBYTE;
  1607. CharLen:=0;
  1608. while (TempBYTE and $80)<>0 do
  1609. begin
  1610. TempBYTE:=(TempBYTE shl 1) and $FE;
  1611. inc(CharLen);
  1612. end;
  1613. //Test for the "CharLen" conforms UTF-8 string
  1614. //This means the 10xxxxxx pattern.
  1615. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1616. begin
  1617. //Insuficient chars in string to decode
  1618. //UTF-8 array. Fallback to single AnsiChar.
  1619. CharLen:= 1;
  1620. end;
  1621. for LookAhead := 1 to CharLen-1 do
  1622. begin
  1623. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1624. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1625. begin
  1626. //Invalid UTF-8 sequence, fallback.
  1627. CharLen:= LookAhead;
  1628. break;
  1629. end;
  1630. end;
  1631. UC:=$FFFF;
  1632. case CharLen of
  1633. 1: begin
  1634. //Not valid UTF-8 sequence
  1635. UC:=UNICODE_INVALID;
  1636. end;
  1637. 2: begin
  1638. //Two bytes UTF, convert it
  1639. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1640. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1641. if UC <= $7F then
  1642. begin
  1643. //Invalid UTF sequence.
  1644. UC:=UNICODE_INVALID;
  1645. end;
  1646. end;
  1647. 3: begin
  1648. //Three bytes, convert it to unicode
  1649. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1650. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1651. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1652. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1653. begin
  1654. //Invalid UTF-8 sequence
  1655. UC:= UNICODE_INVALID;
  1656. End;
  1657. end;
  1658. 4: begin
  1659. //Four bytes, convert it to two unicode characters
  1660. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1661. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1662. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1663. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1664. if (UC < $10000) or (UC > $10FFFF) then
  1665. begin
  1666. UC:= UNICODE_INVALID;
  1667. end
  1668. else
  1669. begin
  1670. { only store pair if room }
  1671. dec(UC,$10000);
  1672. if (OutputUnicode<MaxDestChars-1) then
  1673. begin
  1674. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1675. inc(OutputUnicode);
  1676. UC:=(UC and $3ff) + $DC00;
  1677. end
  1678. else
  1679. begin
  1680. InputUTF8:= InputUTF8 + CharLen;
  1681. { don't store anything }
  1682. CharLen:=0;
  1683. end;
  1684. end;
  1685. end;
  1686. 5,6,7: begin
  1687. //Invalid UTF8 to unicode conversion,
  1688. //mask it as invalid UNICODE too.
  1689. UC:=UNICODE_INVALID;
  1690. end;
  1691. end;
  1692. if CharLen > 0 then
  1693. begin
  1694. if (UC=UNICODE_INVALID) and Not IgnoreInvalid then
  1695. HandleError(231); // Will be converted to EConversionError in sysutils
  1696. PreChar:=UC;
  1697. Dest[OutputUnicode]:=WideChar(UC);
  1698. inc(OutputUnicode);
  1699. end;
  1700. InputUTF8:= InputUTF8 + CharLen;
  1701. end;
  1702. end;
  1703. Result:=OutputUnicode+1;
  1704. end
  1705. else
  1706. begin
  1707. while (InputUTF8<SourceBytes) do
  1708. begin
  1709. IBYTE:=byte(Source[InputUTF8]);
  1710. if (IBYTE and $80) = 0 then
  1711. begin
  1712. // One character US-ASCII, convert it to unicode
  1713. // Commented code to convert LF to CRLF has been removed
  1714. inc(OutputUnicode);
  1715. PreChar:=IBYTE;
  1716. inc(InputUTF8);
  1717. end
  1718. else
  1719. begin
  1720. TempByte:=IBYTE;
  1721. CharLen:=0;
  1722. while (TempBYTE and $80)<>0 do
  1723. begin
  1724. TempBYTE:=(TempBYTE shl 1) and $FE;
  1725. inc(CharLen);
  1726. end;
  1727. //Test for the "CharLen" conforms UTF-8 string
  1728. //This means the 10xxxxxx pattern.
  1729. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1730. begin
  1731. //Insuficient chars in string to decode
  1732. //UTF-8 array. Fallback to single AnsiChar.
  1733. CharLen:= 1;
  1734. end;
  1735. for LookAhead := 1 to CharLen-1 do
  1736. begin
  1737. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1738. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1739. begin
  1740. //Invalid UTF-8 sequence, fallback.
  1741. CharLen:= LookAhead;
  1742. break;
  1743. end;
  1744. end;
  1745. UC:=$FFFF;
  1746. case CharLen of
  1747. 1: begin
  1748. //Not valid UTF-8 sequence
  1749. UC:=UNICODE_INVALID;
  1750. end;
  1751. 2: begin
  1752. //Two bytes UTF, convert it
  1753. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1754. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1755. if UC <= $7F then
  1756. begin
  1757. //Invalid UTF sequence.
  1758. UC:=UNICODE_INVALID;
  1759. end;
  1760. end;
  1761. 3: begin
  1762. //Three bytes, convert it to unicode
  1763. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1764. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1765. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1766. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1767. begin
  1768. //Invalid UTF-8 sequence
  1769. UC:= UNICODE_INVALID;
  1770. end;
  1771. end;
  1772. 4: begin
  1773. //Four bytes, convert it to two unicode characters
  1774. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1775. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1776. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1777. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1778. if (UC < $10000) or (UC > $10FFFF) then
  1779. UC:= UNICODE_INVALID
  1780. else
  1781. { extra character character }
  1782. inc(OutputUnicode);
  1783. end;
  1784. 5,6,7: begin
  1785. //Invalid UTF8 to unicode conversion,
  1786. //mask it as invalid UNICODE too.
  1787. UC:=UNICODE_INVALID;
  1788. end;
  1789. end;
  1790. if CharLen > 0 then
  1791. begin
  1792. if (UC=UNICODE_INVALID) and Not IgnoreInvalid then
  1793. HandleError(231); // Will be converted to EConversionError in sysutils
  1794. PreChar:=UC;
  1795. inc(OutputUnicode);
  1796. end;
  1797. InputUTF8:= InputUTF8 + CharLen;
  1798. end;
  1799. end;
  1800. Result:=OutputUnicode+1;
  1801. end;
  1802. end;
  1803. {$endif EXCLUDE_COMPLEX_PROCS}
  1804. function UTF8Encode(const s : RawByteString) : RawByteString; inline;
  1805. begin
  1806. Result:=UTF8Encode(UnicodeString(s));
  1807. end;
  1808. {$ifndef FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1809. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1810. function UTF8Encode(const s : UnicodeString) : RawByteString;
  1811. var
  1812. i : SizeInt;
  1813. hs : UTF8String;
  1814. begin
  1815. result:='';
  1816. if Length(s)=0 then
  1817. exit;
  1818. SetLength(hs,length(s)*3);
  1819. i:=UnicodeToUtf8(pansichar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  1820. if i>0 then
  1821. begin
  1822. SetLength(hs,i-1);
  1823. result:=hs;
  1824. end;
  1825. end;
  1826. {$endif FPC_HAS_UTF8ENCODE_UNICODESTRING}
  1827. {$ifndef FPC_HAS_UTF8DECODE_UNICODESTRING}
  1828. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  1829. function UTF8Decode(const s : RawByteString): UnicodeString;
  1830. var
  1831. i : SizeInt;
  1832. hs : UnicodeString;
  1833. begin
  1834. result:='';
  1835. if Length(s)=0 then
  1836. exit;
  1837. SetLength(hs,length(s));
  1838. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pansichar(s),length(s));
  1839. if i>0 then
  1840. begin
  1841. SetLength(hs,i-1);
  1842. result:=hs;
  1843. end;
  1844. end;
  1845. {$endif FPC_HAS_UTF8DECODE_UNICODESTRING}
  1846. function AnsiToUtf8(const s : RawByteString): RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1847. begin
  1848. Result:=Utf8Encode(s);
  1849. end;
  1850. function Utf8ToAnsi(const s : RawByteString) : RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
  1851. begin
  1852. Result:=RawByteString(Utf8Decode(s));
  1853. end;
  1854. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  1855. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String);
  1856. var
  1857. i, reslen: sizeint;
  1858. w: longint;
  1859. begin
  1860. reslen:=0;
  1861. i:=0;
  1862. { calculate required length }
  1863. while (i<len) do
  1864. begin
  1865. if (p[i]<=#$d7ff) or (p[i]>=#$e000) then
  1866. inc(i)
  1867. else if (p[i]<=#$dbff) and
  1868. (i+1<len) and
  1869. (p[i+1]>=#$dc00) and
  1870. (p[i+1]<=#$dfff) then
  1871. inc(i,2)
  1872. else
  1873. inc(i);
  1874. inc(reslen);
  1875. end;
  1876. SetLength(res,reslen+1); { +1 for null termination }
  1877. reslen:=0;
  1878. i:=0;
  1879. { do conversion }
  1880. while (i<len) do
  1881. begin
  1882. w:=ord(p[i]);
  1883. if (w<=$d7ff) or (w>=$e000) then
  1884. res[reslen]:=w
  1885. else if (w<=$dbff) and
  1886. (i+1<len) and
  1887. (p[i+1]>=#$dc00) and
  1888. (p[i+1]<=#$dfff) then
  1889. begin
  1890. res[reslen]:=(UCS4Char(w-$d7c0) shl 10)+(UCS4Char(p[i+1]) xor $dc00);
  1891. inc(i);
  1892. end
  1893. else { invalid surrogate pair }
  1894. res[reslen]:=w;
  1895. inc(i);
  1896. inc(reslen);
  1897. end;
  1898. res[reslen]:=0;
  1899. end;
  1900. {$ifndef FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1901. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1902. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  1903. begin
  1904. UCS4Encode(PWideChar(s),Length(s),result);
  1905. end;
  1906. {$endif FPC_HAS_UCS4STRING_TO_UNICODESTR}
  1907. {$ifndef FPC_HAS_WIDESTR_TO_UCS4STRING}
  1908. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  1909. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1910. begin
  1911. UCS4Encode(PWideChar(s),Length(s),result);
  1912. end;
  1913. {$endif FPC_HAS_WIDESTR_TO_UCS4STRING}
  1914. {$ifndef FPC_HAS_UCS4STRING_TO_WIDESTR}
  1915. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  1916. { dest should point to previously allocated wide/unicodestring }
  1917. procedure UCS4Decode(const s: UCS4String; dest: PWideChar);
  1918. var
  1919. i: sizeint;
  1920. nc: UCS4Char;
  1921. begin
  1922. for i:=0 to length(s)-2 do { -2 because s contains explicit terminating #0 }
  1923. begin
  1924. nc:=s[i];
  1925. if (nc<=$ffff) then
  1926. dest^:=widechar(nc)
  1927. else if (dword(nc)<=$10ffff) then
  1928. begin
  1929. dest^:=widechar(nc shr 10 + $d7c0);
  1930. { subtracting $10000 doesn't change low 10 bits }
  1931. dest[1]:=widechar(nc and $3ff + $dc00);
  1932. inc(dest);
  1933. end
  1934. else { invalid code point }
  1935. dest^:='?';
  1936. inc(dest);
  1937. end;
  1938. end;
  1939. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  1940. var
  1941. i : SizeInt;
  1942. reslen : SizeInt;
  1943. begin
  1944. reslen:=0;
  1945. for i:=0 to length(s)-2 do { skip terminating #0 }
  1946. Inc(reslen,1+ord((s[i]>$ffff) and (cardinal(s[i])<=$10ffff)));
  1947. SetLength(result,reslen);
  1948. UCS4Decode(s,pointer(result));
  1949. end;
  1950. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1951. var
  1952. i : SizeInt;
  1953. reslen : SizeInt;
  1954. begin
  1955. reslen:=0;
  1956. for i:=0 to length(s)-2 do { skip terminating #0 }
  1957. Inc(reslen,1+ord((s[i]>$ffff) and (cardinal(s[i])<=$10ffff)));
  1958. SetLength(result,reslen);
  1959. UCS4Decode(s,pointer(result));
  1960. end;
  1961. {$endif FPC_HAS_UCS4STRING_TO_WIDESTR}
  1962. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  1963. {$ifndef FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  1964. const
  1965. SNoUnicodestrings = 'This binary has no string conversion support compiled in.';
  1966. SRecompileWithUnicodestrings = 'Recompile the application with a unit that installs a unicodestring manager in the program uses clause.';
  1967. procedure unimplementedunicodestring;
  1968. begin
  1969. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1970. {$ifndef HAS_WIDESTRINGMANAGER}
  1971. If IsConsole then
  1972. begin
  1973. Writeln(StdErr,SNoUnicodestrings);
  1974. Writeln(StdErr,SRecompileWithUnicodestrings);
  1975. end;
  1976. {$endif HAS_WIDESTRINGMANAGER}
  1977. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1978. HandleErrorAddrFrameInd(234{RuntimeErrorExitCodes[reCodesetConversion]},get_pc_addr,get_frame);
  1979. end;
  1980. function StringElementSize(const S: UnicodeString): Word; overload;
  1981. begin
  1982. if assigned(Pointer(S)) then
  1983. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.ElementSize
  1984. else
  1985. Result:=SizeOf(UnicodeChar);
  1986. end;
  1987. function StringRefCount(const S: UnicodeString): SizeInt; overload;
  1988. begin
  1989. if assigned(Pointer(S)) then
  1990. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.Ref
  1991. else
  1992. Result:=0;
  1993. end;
  1994. function StringCodePage(const S: UnicodeString): TSystemCodePage; overload;
  1995. begin
  1996. {$ifdef FPC_HAS_CPSTRING}
  1997. if assigned(Pointer(S)) then
  1998. Result:=PUnicodeRec(pointer(S)-UnicodeFirstOff)^.CodePage
  1999. else
  2000. {$endif FPC_HAS_CPSTRING}
  2001. Result:=DefaultUnicodeCodePage;
  2002. end;
  2003. {$push}
  2004. {$warnings off}
  2005. function StubUnicodeCase(const s : UnicodeString) : UnicodeString;
  2006. begin
  2007. unimplementedunicodestring;
  2008. end;
  2009. function StubCompareUnicodeString(const s1, s2 : UnicodeString; Options : TCompareOptions) : PtrInt;
  2010. begin
  2011. unimplementedunicodestring;
  2012. end;
  2013. function StubWideCase(const s: WideString): WideString;
  2014. begin
  2015. unimplementedunicodestring;
  2016. end;
  2017. function StubCompareWideString(const s1, s2 : WideString; Options : TCompareOptions) : PtrInt;
  2018. begin
  2019. unimplementedunicodestring;
  2020. end;
  2021. {$pop}
  2022. procedure initunicodestringmanager;
  2023. begin
  2024. {$ifndef HAS_WIDESTRINGMANAGER}
  2025. widestringmanager:=Default(TUnicodeStringManager);
  2026. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2027. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2028. {$else FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2029. widestringmanager.Ansi2WideMoveProc:=@DefaultAnsi2WideMove;
  2030. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2031. widestringmanager.Wide2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  2032. widestringmanager.UpperWideStringProc:=@StubWideCase;
  2033. widestringmanager.LowerWideStringProc:=@StubWideCase;
  2034. widestringmanager.Unicode2AnsiMoveProc:=@DefaultUnicode2AnsiMove;
  2035. widestringmanager.Ansi2UnicodeMoveProc:=@DefaultAnsi2UnicodeMove;
  2036. widestringmanager.UpperUnicodeStringProc:=@StubUnicodeCase;
  2037. widestringmanager.LowerUnicodeStringProc:=@StubUnicodeCase;
  2038. widestringmanager.GetStandardCodePageProc:=@DefaultGetStandardCodePage;
  2039. {$endif HAS_WIDESTRINGMANAGER}
  2040. widestringmanager.CompareWideStringProc:=@StubCompareWideString;
  2041. // widestringmanager.CompareTextWideStringProc:=@StubCompareWideString;
  2042. widestringmanager.CompareUnicodeStringProc:=@StubCompareUnicodeString;
  2043. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  2044. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  2045. end;
  2046. {$endif FPC_HAS_BUILTIN_WIDESTR_MANAGER}
  2047. {$ifndef FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2048. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2049. Function ToSingleByteFileSystemEncodedFileName(const Str: UnicodeString): RawByteString;
  2050. Begin
  2051. widestringmanager.Unicode2AnsiMoveProc(punicodechar(Str),Result,
  2052. DefaultFileSystemCodePage,Length(Str));
  2053. End;
  2054. {$endif FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  2055. {$ifndef FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2056. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2057. Function ToSingleByteFileSystemEncodedFileName(const arr: array of widechar): RawByteString;
  2058. Begin
  2059. widestringmanager.Unicode2AnsiMoveProc(@arr[0],Result,
  2060. DefaultFileSystemCodePage,length(pwidechar(@arr[0])));
  2061. End;
  2062. {$endif FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  2063. Function ToSingleByteFileSystemEncodedFileName(const Str: RawByteString): RawByteString;
  2064. Begin
  2065. Result:=Str;
  2066. SetCodePage(Result,DefaultFileSystemCodePage,True);
  2067. End;
  2068. { Delphi compatibility: always interpret the data in the string as UTF-8,
  2069. ignore any codepage }
  2070. function UTF8ToString(const S: RawByteString): UnicodeString; inline;
  2071. begin
  2072. Result := UTF8Decode(S);
  2073. end;
  2074. function UTF8ToUnicodeString(const s : RawByteString): UnicodeString;
  2075. begin
  2076. Result := UTF8Decode(S);
  2077. end;
  2078. function UTF8ToString(const S: ShortString): UnicodeString;
  2079. Var
  2080. rs: RawByteString;
  2081. begin
  2082. rs:=S;
  2083. Result := UTF8Decode(rs);
  2084. end;
  2085. function UTF8ToUnicodeString(const S: ShortString): unicodestring;
  2086. begin
  2087. Result:=UTF8ToString(S);
  2088. end;
  2089. function UTF8ToString(const S: PAnsiChar): UnicodeString;
  2090. var
  2091. rs: RawByteString;
  2092. Count: Integer;
  2093. begin
  2094. Count := length(S);
  2095. SetLength(rs, Count);
  2096. if Count > 0 then
  2097. fpc_pchar_ansistr_intern_charmove(S,0,rs,0,Count);
  2098. Result := UTF8ToString(rs);
  2099. end;
  2100. function UTF8ToUnicodeString(const S: PAnsiChar): unicodestring;
  2101. begin
  2102. Result:=UTF8ToString(S);
  2103. end;
  2104. { byte and ansichar are the same on the JVM, and "array of" and "pointer to"
  2105. are as well }
  2106. {$ifndef CPUJVM}
  2107. function UTF8ToString(const S: array of AnsiChar): UnicodeString;
  2108. var
  2109. rs: RawByteString;
  2110. Count: Integer;
  2111. begin
  2112. Count := Length(S);
  2113. SetLength(rs, Count);
  2114. if Count > 0 then
  2115. fpc_pchar_ansistr_intern_charmove(@S,Low(S),rs,0,Count);
  2116. Result := UTF8ToString(rs);
  2117. end;
  2118. function UTF8ToString(const S: array of Byte): 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(pansichar(@S),Low(S),rs,0,Count);
  2127. Result := UTF8ToString(rs);
  2128. end;
  2129. {$endif not CPUJVM}