ustrings.inc 66 KB

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