ustrings.inc 63 KB

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