ustrings.inc 69 KB

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