ustrings.inc 68 KB

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