ustrings.inc 63 KB

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