ustrings.inc 69 KB

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