ustrings.inc 69 KB

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