ustrings.inc 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  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. Procedure fpc_UnicodeStr_CheckZero(p : pointer);[Public,Alias : 'FPC_UNICODESTR_CHECKZERO']; compilerproc;
  1166. begin
  1167. if p=nil then
  1168. HandleErrorFrame(201,get_frame);
  1169. end;
  1170. Procedure fpc_UnicodeStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_UNICODESTR_RANGECHECK']; compilerproc;
  1171. begin
  1172. if (index>len div 2) or (Index<1) then
  1173. HandleErrorFrame(201,get_frame);
  1174. end;
  1175. Procedure fpc_UnicodeStr_SetLength(Var S : UnicodeString; l : SizeInt);[Public,Alias : 'FPC_UNICODESTR_SETLENGTH']; compilerproc;
  1176. {
  1177. Sets The length of string S to L.
  1178. Makes sure S is unique, and contains enough room.
  1179. }
  1180. Var
  1181. Temp : Pointer;
  1182. movelen: SizeInt;
  1183. begin
  1184. if (l>0) then
  1185. begin
  1186. if Pointer(S)=nil then
  1187. begin
  1188. { Need a complete new string...}
  1189. Pointer(s):=NewUnicodeString(l);
  1190. end
  1191. { windows doesn't support reallocing unicodestrings, this code
  1192. is anyways subject to be removed because unicodestrings shouldn't be
  1193. ref. counted anymore (FK) }
  1194. else
  1195. if (PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref = 1) then
  1196. begin
  1197. Dec(Pointer(S),UnicodeFirstOff);
  1198. if SizeUInt(L*sizeof(UnicodeChar)+UnicodeRecLen)>MemSize(Pointer(S)) then
  1199. reallocmem(pointer(S), L*sizeof(UnicodeChar)+UnicodeRecLen);
  1200. Inc(Pointer(S), UnicodeFirstOff);
  1201. end
  1202. else
  1203. begin
  1204. { Reallocation is needed... }
  1205. Temp:=Pointer(NewUnicodeString(L));
  1206. if Length(S)>0 then
  1207. begin
  1208. if l < succ(length(s)) then
  1209. movelen := l
  1210. { also move terminating null }
  1211. else
  1212. movelen := succ(length(s));
  1213. Move(Pointer(S)^,Temp^,movelen * Sizeof(UnicodeChar));
  1214. end;
  1215. fpc_unicodestr_decr_ref(Pointer(S));
  1216. Pointer(S):=Temp;
  1217. end;
  1218. { Force nil termination in case it gets shorter }
  1219. PWord(Pointer(S)+l*sizeof(UnicodeChar))^:=0;
  1220. PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Len:=l*sizeof(UnicodeChar);
  1221. end
  1222. else
  1223. begin
  1224. { Length=0 }
  1225. if Pointer(S)<>nil then
  1226. fpc_unicodestr_decr_ref (Pointer(S));
  1227. Pointer(S):=Nil;
  1228. end;
  1229. end;
  1230. {*****************************************************************************
  1231. Public functions, In interface.
  1232. *****************************************************************************}
  1233. function UnicodeCharToString(S : PUnicodeChar) : AnsiString;
  1234. begin
  1235. result:=UnicodeCharLenToString(s,Length(UnicodeString(s)));
  1236. end;
  1237. function StringToUnicodeChar(const Src : AnsiString;Dest : PUnicodeChar;DestSize : SizeInt) : PUnicodeChar;
  1238. var
  1239. temp:unicodestring;
  1240. begin
  1241. widestringmanager.Ansi2UnicodeMoveProc(PChar(Src),temp,Length(Src));
  1242. if Length(temp)<DestSize then
  1243. move(temp[1],Dest^,Length(temp)*SizeOf(UnicodeChar))
  1244. else
  1245. move(temp[1],Dest^,(DestSize-1)*SizeOf(UnicodeChar));
  1246. Dest[DestSize-1]:=#0;
  1247. result:=Dest;
  1248. end;
  1249. function WideCharToString(S : PWideChar) : AnsiString;
  1250. begin
  1251. result:=WideCharLenToString(s,Length(WideString(s)));
  1252. end;
  1253. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1254. var
  1255. temp:widestring;
  1256. begin
  1257. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1258. if Length(temp)<DestSize then
  1259. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1260. else
  1261. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1262. Dest[DestSize-1]:=#0;
  1263. result:=Dest;
  1264. end;
  1265. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : AnsiString;
  1266. begin
  1267. //SetLength(result,Len);
  1268. widestringmanager.Unicode2AnsiMoveproc(S,result,Len);
  1269. end;
  1270. procedure UnicodeCharLenToStrVar(Src : PUnicodeChar;Len : SizeInt;out Dest : AnsiString);
  1271. begin
  1272. Dest:=UnicodeCharLenToString(Src,Len);
  1273. end;
  1274. procedure UnicodeCharToStrVar(S : PUnicodeChar;out Dest : AnsiString);
  1275. begin
  1276. Dest:=UnicodeCharToString(S);
  1277. end;
  1278. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1279. begin
  1280. //SetLength(result,Len);
  1281. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1282. end;
  1283. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1284. begin
  1285. Dest:=WideCharLenToString(Src,Len);
  1286. end;
  1287. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1288. begin
  1289. Dest:=WideCharToString(S);
  1290. end;
  1291. Function fpc_unicodestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_UNICODESTR_UNIQUE']; compilerproc;
  1292. {
  1293. Make sure reference count of S is 1,
  1294. using copy-on-write semantics.
  1295. }
  1296. Var
  1297. SNew : Pointer;
  1298. L : SizeInt;
  1299. begin
  1300. pointer(result) := pointer(s);
  1301. If Pointer(S)=Nil then
  1302. exit;
  1303. if PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.Ref<>1 then
  1304. begin
  1305. L:=PUnicodeRec(Pointer(S)-UnicodeFirstOff)^.len div sizeof(UnicodeChar);
  1306. SNew:=NewUnicodeString (L);
  1307. Move (PUnicodeChar(S)^,SNew^,(L+1)*sizeof(UnicodeChar));
  1308. PUnicodeRec(SNew-UnicodeFirstOff)^.len:=L * sizeof(UnicodeChar);
  1309. fpc_unicodestr_decr_ref (Pointer(S)); { Thread safe }
  1310. pointer(S):=SNew;
  1311. pointer(result):=SNew;
  1312. end;
  1313. end;
  1314. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  1315. var
  1316. ResultAddress : Pointer;
  1317. begin
  1318. ResultAddress:=Nil;
  1319. dec(index);
  1320. if Index < 0 then
  1321. Index := 0;
  1322. { Check Size. Accounts for Zero-length S, the double check is needed because
  1323. Size can be maxint and will get <0 when adding index }
  1324. if (Size>Length(S)) or
  1325. (Index+Size>Length(S)) then
  1326. Size:=Length(S)-Index;
  1327. If Size>0 then
  1328. begin
  1329. If Index<0 Then
  1330. Index:=0;
  1331. ResultAddress:=Pointer(NewUnicodeString (Size));
  1332. if ResultAddress<>Nil then
  1333. begin
  1334. Move (PUnicodeChar(S)[Index],ResultAddress^,Size*sizeof(UnicodeChar));
  1335. PUnicodeRec(ResultAddress-UnicodeFirstOff)^.Len:=Size*sizeof(UnicodeChar);
  1336. PUnicodeChar(ResultAddress+Size*sizeof(UnicodeChar))^:=#0;
  1337. end;
  1338. end;
  1339. fpc_unicodestr_decr_ref(Pointer(fpc_unicodestr_copy));
  1340. Pointer(fpc_unicodestr_Copy):=ResultAddress;
  1341. end;
  1342. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  1343. var
  1344. i,MaxLen : SizeInt;
  1345. pc : punicodechar;
  1346. begin
  1347. Pos:=0;
  1348. if Length(SubStr)>0 then
  1349. begin
  1350. MaxLen:=Length(source)-Length(SubStr);
  1351. i:=0;
  1352. pc:=@source[1];
  1353. while (i<=MaxLen) do
  1354. begin
  1355. inc(i);
  1356. if (SubStr[1]=pc^) and
  1357. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1358. begin
  1359. Pos:=i;
  1360. exit;
  1361. end;
  1362. inc(pc);
  1363. end;
  1364. end;
  1365. end;
  1366. { Faster version for a unicodechar alone }
  1367. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  1368. var
  1369. i: SizeInt;
  1370. pc : punicodechar;
  1371. begin
  1372. pc:=@s[1];
  1373. for i:=1 to length(s) do
  1374. begin
  1375. if pc^=c then
  1376. begin
  1377. pos:=i;
  1378. exit;
  1379. end;
  1380. inc(pc);
  1381. end;
  1382. pos:=0;
  1383. end;
  1384. Function Pos (c : AnsiString; Const s : UnicodeString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1385. begin
  1386. result:=Pos(UnicodeString(c),s);
  1387. end;
  1388. Function Pos (c : ShortString; Const s : UnicodeString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1389. begin
  1390. result:=Pos(UnicodeString(c),s);
  1391. end;
  1392. Function Pos (c : UnicodeString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1393. begin
  1394. result:=Pos(c,UnicodeString(s));
  1395. end;
  1396. { Faster version for a char alone. Must be implemented because }
  1397. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1398. { using pos(char,pchar) will always call the shortstring version }
  1399. { (exact match for first argument), also with $h+ (JM) }
  1400. Function Pos (c : Char; Const s : UnicodeString) : SizeInt;
  1401. var
  1402. i: SizeInt;
  1403. wc : unicodechar;
  1404. pc : punicodechar;
  1405. begin
  1406. wc:=c;
  1407. pc:=@s[1];
  1408. for i:=1 to length(s) do
  1409. begin
  1410. if pc^=wc then
  1411. begin
  1412. pos:=i;
  1413. exit;
  1414. end;
  1415. inc(pc);
  1416. end;
  1417. pos:=0;
  1418. end;
  1419. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  1420. Var
  1421. LS : SizeInt;
  1422. begin
  1423. LS:=Length(S);
  1424. if (Index>LS) or (Index<=0) or (Size<=0) then
  1425. exit;
  1426. UniqueString (S);
  1427. { (Size+Index) will overflow if Size=MaxInt. }
  1428. if Size>LS-Index then
  1429. Size:=LS-Index+1;
  1430. if Size<=LS-Index then
  1431. begin
  1432. Dec(Index);
  1433. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1434. end;
  1435. Setlength(s,LS-Size);
  1436. end;
  1437. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1438. var
  1439. Temp : UnicodeString;
  1440. LS : SizeInt;
  1441. begin
  1442. If Length(Source)=0 then
  1443. exit;
  1444. if index <= 0 then
  1445. index := 1;
  1446. Ls:=Length(S);
  1447. if index > LS then
  1448. index := LS+1;
  1449. Dec(Index);
  1450. Pointer(Temp) := NewUnicodeString(Length(Source)+LS);
  1451. SetLength(Temp,Length(Source)+LS);
  1452. If Index>0 then
  1453. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1454. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1455. If (LS-Index)>0 then
  1456. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1457. S:=Temp;
  1458. end;
  1459. Function UpCase(c:UnicodeChar):UnicodeChar;
  1460. var
  1461. s : UnicodeString;
  1462. begin
  1463. s:=c;
  1464. result:=widestringmanager.UpperUnicodeStringProc(s)[1];
  1465. end;
  1466. function UpCase(const s : UnicodeString) : UnicodeString;
  1467. begin
  1468. result:=widestringmanager.UpperUnicodeStringProc(s);
  1469. end;
  1470. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  1471. begin
  1472. SetLength(S,Len);
  1473. If (Buf<>Nil) and (Len>0) then
  1474. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1475. end;
  1476. Procedure SetString (Out S : UnicodeString; Buf : PChar; Len : SizeInt);
  1477. var
  1478. BufLen: SizeInt;
  1479. begin
  1480. SetLength(S,Len);
  1481. If (Buf<>Nil) and (Len>0) then
  1482. widestringmanager.Ansi2UnicodeMoveProc(Buf,S,Len);
  1483. end;
  1484. {$ifndef FPUNONE}
  1485. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1486. Var
  1487. SS : String;
  1488. begin
  1489. fpc_Val_Real_UnicodeStr := 0;
  1490. if length(S) > 255 then
  1491. code := 256
  1492. else
  1493. begin
  1494. SS := S;
  1495. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1496. end;
  1497. end;
  1498. {$endif}
  1499. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1500. var ss:shortstring;
  1501. begin
  1502. if length(s)>255 then
  1503. code:=256
  1504. else
  1505. begin
  1506. ss:=s;
  1507. val(ss,fpc_val_enum_unicodestr,code);
  1508. end;
  1509. end;
  1510. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1511. Var
  1512. SS : String;
  1513. begin
  1514. if length(S) > 255 then
  1515. begin
  1516. fpc_Val_Currency_UnicodeStr:=0;
  1517. code := 256;
  1518. end
  1519. else
  1520. begin
  1521. SS := S;
  1522. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1523. end;
  1524. end;
  1525. Function fpc_Val_UInt_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_UNICODESTR']; compilerproc;
  1526. Var
  1527. SS : ShortString;
  1528. begin
  1529. fpc_Val_UInt_UnicodeStr := 0;
  1530. if length(S) > 255 then
  1531. code := 256
  1532. else
  1533. begin
  1534. SS := S;
  1535. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1536. end;
  1537. end;
  1538. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1539. Var
  1540. SS : ShortString;
  1541. begin
  1542. fpc_Val_SInt_UnicodeStr:=0;
  1543. if length(S)>255 then
  1544. code:=256
  1545. else
  1546. begin
  1547. SS := S;
  1548. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1549. end;
  1550. end;
  1551. {$ifndef CPU64}
  1552. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1553. Var
  1554. SS : ShortString;
  1555. begin
  1556. fpc_Val_qword_UnicodeStr:=0;
  1557. if length(S)>255 then
  1558. code:=256
  1559. else
  1560. begin
  1561. SS := S;
  1562. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1563. end;
  1564. end;
  1565. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1566. Var
  1567. SS : ShortString;
  1568. begin
  1569. fpc_Val_int64_UnicodeStr:=0;
  1570. if length(S)>255 then
  1571. code:=256
  1572. else
  1573. begin
  1574. SS := S;
  1575. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1576. end;
  1577. end;
  1578. {$endif CPU64}
  1579. {$ifndef FPUNONE}
  1580. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1581. var
  1582. ss : shortstring;
  1583. begin
  1584. str_real(len,fr,d,treal_type(rt),ss);
  1585. s:=ss;
  1586. end;
  1587. {$endif}
  1588. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1589. var ss:shortstring;
  1590. begin
  1591. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1592. s:=ss;
  1593. end;
  1594. procedure fpc_unicodestr_bool(b : boolean;len:sizeint;out s:unicodestring);compilerproc;
  1595. var ss:shortstring;
  1596. begin
  1597. fpc_shortstr_bool(b,len,ss);
  1598. s:=ss;
  1599. end;
  1600. {$ifdef FPC_HAS_STR_CURRENCY}
  1601. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1602. var
  1603. ss : shortstring;
  1604. begin
  1605. str(c:len:fr,ss);
  1606. s:=ss;
  1607. end;
  1608. {$endif FPC_HAS_STR_CURRENCY}
  1609. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1610. Var
  1611. SS : ShortString;
  1612. begin
  1613. Str (v:Len,SS);
  1614. S:=SS;
  1615. end;
  1616. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1617. Var
  1618. SS : ShortString;
  1619. begin
  1620. str(v:Len,SS);
  1621. S:=SS;
  1622. end;
  1623. {$ifndef CPU64}
  1624. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1625. Var
  1626. SS : ShortString;
  1627. begin
  1628. Str (v:Len,SS);
  1629. S:=SS;
  1630. end;
  1631. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1632. Var
  1633. SS : ShortString;
  1634. begin
  1635. str(v:Len,SS);
  1636. S:=SS;
  1637. end;
  1638. {$endif CPU64}
  1639. { converts an utf-16 code point or surrogate pair to utf-32 }
  1640. function utf16toutf32(const S: UnicodeString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1641. var
  1642. w: unicodechar;
  1643. begin
  1644. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1645. { are the same in UTF-32 }
  1646. w:=s[index];
  1647. if (w<=#$d7ff) or
  1648. (w>=#$e000) then
  1649. begin
  1650. result:=UCS4Char(w);
  1651. len:=1;
  1652. end
  1653. { valid surrogate pair? }
  1654. else if (w<=#$dbff) and
  1655. { w>=#$d7ff check not needed, checked above }
  1656. (index<length(s)) and
  1657. (s[index+1]>=#$dc00) and
  1658. (s[index+1]<=#$dfff) then
  1659. { convert the surrogate pair to UTF-32 }
  1660. begin
  1661. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1662. len:=2;
  1663. end
  1664. else
  1665. { invalid surrogate -> do nothing }
  1666. begin
  1667. result:=UCS4Char(w);
  1668. len:=1;
  1669. end;
  1670. end;
  1671. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1672. begin
  1673. if assigned(Source) then
  1674. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1675. else
  1676. Result:=0;
  1677. end;
  1678. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1679. var
  1680. i,j : SizeUInt;
  1681. w : word;
  1682. lw : longword;
  1683. len : longint;
  1684. begin
  1685. result:=0;
  1686. if source=nil then
  1687. exit;
  1688. i:=0;
  1689. j:=0;
  1690. if assigned(Dest) then
  1691. begin
  1692. while (i<SourceChars) and (j<MaxDestBytes) do
  1693. begin
  1694. w:=word(Source[i]);
  1695. case w of
  1696. 0..$7f:
  1697. begin
  1698. Dest[j]:=char(w);
  1699. inc(j);
  1700. end;
  1701. $80..$7ff:
  1702. begin
  1703. if j+1>=MaxDestBytes then
  1704. break;
  1705. Dest[j]:=char($c0 or (w shr 6));
  1706. Dest[j+1]:=char($80 or (w and $3f));
  1707. inc(j,2);
  1708. end;
  1709. $800..$d7ff,$e000..$ffff:
  1710. begin
  1711. if j+2>=MaxDestBytes then
  1712. break;
  1713. Dest[j]:=char($e0 or (w shr 12));
  1714. Dest[j+1]:=char($80 or ((w shr 6) and $3f));
  1715. Dest[j+2]:=char($80 or (w and $3f));
  1716. inc(j,3);
  1717. end;
  1718. $d800..$dbff:
  1719. {High Surrogates}
  1720. begin
  1721. if j+3>=MaxDestBytes then
  1722. break;
  1723. if (i<sourcechars-1) and
  1724. (word(Source[i+1]) >= $dc00) and
  1725. (word(Source[i+1]) <= $dfff) then
  1726. begin
  1727. lw:=longword(utf16toutf32(Source[i] + Source[i+1], 1, len));
  1728. Dest[j]:=char($f0 or (lw shr 18));
  1729. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1730. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1731. Dest[j+3]:=char($80 or (lw and $3f));
  1732. inc(j,4);
  1733. inc(i);
  1734. end;
  1735. end;
  1736. end;
  1737. inc(i);
  1738. end;
  1739. if j>SizeUInt(MaxDestBytes-1) then
  1740. j:=MaxDestBytes-1;
  1741. Dest[j]:=#0;
  1742. end
  1743. else
  1744. begin
  1745. while i<SourceChars do
  1746. begin
  1747. case word(Source[i]) of
  1748. $0..$7f:
  1749. inc(j);
  1750. $80..$7ff:
  1751. inc(j,2);
  1752. $800..$d7ff,$e000..$ffff:
  1753. inc(j,3);
  1754. $d800..$dbff:
  1755. begin
  1756. if (i<sourcechars-1) and
  1757. (word(Source[i+1]) >= $dc00) and
  1758. (word(Source[i+1]) <= $dfff) then
  1759. begin
  1760. inc(j,4);
  1761. inc(i);
  1762. end;
  1763. end;
  1764. end;
  1765. inc(i);
  1766. end;
  1767. end;
  1768. result:=j+1;
  1769. end;
  1770. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1771. begin
  1772. if assigned(Source) then
  1773. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1774. else
  1775. Result:=0;
  1776. end;
  1777. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1778. const
  1779. UNICODE_INVALID=63;
  1780. var
  1781. InputUTF8: SizeUInt;
  1782. IBYTE: BYTE;
  1783. OutputUnicode: SizeUInt;
  1784. PRECHAR: SizeUInt;
  1785. TempBYTE: BYTE;
  1786. CharLen: SizeUint;
  1787. LookAhead: SizeUInt;
  1788. UC: SizeUInt;
  1789. begin
  1790. if not assigned(Source) then
  1791. begin
  1792. result:=0;
  1793. exit;
  1794. end;
  1795. result:=SizeUInt(-1);
  1796. InputUTF8:=0;
  1797. OutputUnicode:=0;
  1798. PreChar:=0;
  1799. if Assigned(Dest) Then
  1800. begin
  1801. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1802. begin
  1803. IBYTE:=byte(Source[InputUTF8]);
  1804. if (IBYTE and $80) = 0 then
  1805. begin
  1806. //One character US-ASCII, convert it to unicode
  1807. if IBYTE = 10 then
  1808. begin
  1809. If (PreChar<>13) and FALSE then
  1810. begin
  1811. //Expand to crlf, conform UTF-8.
  1812. //This procedure will break the memory alocation by
  1813. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1814. if OutputUnicode+1<MaxDestChars then
  1815. begin
  1816. Dest[OutputUnicode]:=WideChar(13);
  1817. inc(OutputUnicode);
  1818. Dest[OutputUnicode]:=WideChar(10);
  1819. inc(OutputUnicode);
  1820. PreChar:=10;
  1821. end
  1822. else
  1823. begin
  1824. Dest[OutputUnicode]:=WideChar(13);
  1825. inc(OutputUnicode);
  1826. end;
  1827. end
  1828. else
  1829. begin
  1830. Dest[OutputUnicode]:=WideChar(IBYTE);
  1831. inc(OutputUnicode);
  1832. PreChar:=IBYTE;
  1833. end;
  1834. end
  1835. else
  1836. begin
  1837. Dest[OutputUnicode]:=WideChar(IBYTE);
  1838. inc(OutputUnicode);
  1839. PreChar:=IBYTE;
  1840. end;
  1841. inc(InputUTF8);
  1842. end
  1843. else
  1844. begin
  1845. TempByte:=IBYTE;
  1846. CharLen:=0;
  1847. while (TempBYTE and $80)<>0 do
  1848. begin
  1849. TempBYTE:=(TempBYTE shl 1) and $FE;
  1850. inc(CharLen);
  1851. end;
  1852. //Test for the "CharLen" conforms UTF-8 string
  1853. //This means the 10xxxxxx pattern.
  1854. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1855. begin
  1856. //Insuficient chars in string to decode
  1857. //UTF-8 array. Fallback to single char.
  1858. CharLen:= 1;
  1859. end;
  1860. for LookAhead := 1 to CharLen-1 do
  1861. begin
  1862. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1863. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1864. begin
  1865. //Invalid UTF-8 sequence, fallback.
  1866. CharLen:= LookAhead;
  1867. break;
  1868. end;
  1869. end;
  1870. UC:=$FFFF;
  1871. case CharLen of
  1872. 1: begin
  1873. //Not valid UTF-8 sequence
  1874. UC:=UNICODE_INVALID;
  1875. end;
  1876. 2: begin
  1877. //Two bytes UTF, convert it
  1878. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1879. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1880. if UC <= $7F then
  1881. begin
  1882. //Invalid UTF sequence.
  1883. UC:=UNICODE_INVALID;
  1884. end;
  1885. end;
  1886. 3: begin
  1887. //Three bytes, convert it to unicode
  1888. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1889. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1890. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1891. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1892. begin
  1893. //Invalid UTF-8 sequence
  1894. UC:= UNICODE_INVALID;
  1895. End;
  1896. end;
  1897. 4: begin
  1898. //Four bytes, convert it to two unicode characters
  1899. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1900. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1901. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1902. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1903. if (UC < $10000) or (UC > $10FFFF) then
  1904. begin
  1905. UC:= UNICODE_INVALID;
  1906. end
  1907. else
  1908. begin
  1909. { only store pair if room }
  1910. dec(UC,$10000);
  1911. if (OutputUnicode<MaxDestChars-1) then
  1912. begin
  1913. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1914. inc(OutputUnicode);
  1915. UC:=(UC and $3ff) + $DC00;
  1916. end
  1917. else
  1918. begin
  1919. InputUTF8:= InputUTF8 + CharLen;
  1920. { don't store anything }
  1921. CharLen:=0;
  1922. end;
  1923. end;
  1924. end;
  1925. 5,6,7: begin
  1926. //Invalid UTF8 to unicode conversion,
  1927. //mask it as invalid UNICODE too.
  1928. UC:=UNICODE_INVALID;
  1929. end;
  1930. end;
  1931. if CharLen > 0 then
  1932. begin
  1933. PreChar:=UC;
  1934. Dest[OutputUnicode]:=WideChar(UC);
  1935. inc(OutputUnicode);
  1936. end;
  1937. InputUTF8:= InputUTF8 + CharLen;
  1938. end;
  1939. end;
  1940. Result:=OutputUnicode+1;
  1941. end
  1942. else
  1943. begin
  1944. while (InputUTF8<SourceBytes) do
  1945. begin
  1946. IBYTE:=byte(Source[InputUTF8]);
  1947. if (IBYTE and $80) = 0 then
  1948. begin
  1949. //One character US-ASCII, convert it to unicode
  1950. if IBYTE = 10 then
  1951. begin
  1952. if (PreChar<>13) and FALSE then
  1953. begin
  1954. //Expand to crlf, conform UTF-8.
  1955. //This procedure will break the memory alocation by
  1956. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1957. inc(OutputUnicode,2);
  1958. PreChar:=10;
  1959. end
  1960. else
  1961. begin
  1962. inc(OutputUnicode);
  1963. PreChar:=IBYTE;
  1964. end;
  1965. end
  1966. else
  1967. begin
  1968. inc(OutputUnicode);
  1969. PreChar:=IBYTE;
  1970. end;
  1971. inc(InputUTF8);
  1972. end
  1973. else
  1974. begin
  1975. TempByte:=IBYTE;
  1976. CharLen:=0;
  1977. while (TempBYTE and $80)<>0 do
  1978. begin
  1979. TempBYTE:=(TempBYTE shl 1) and $FE;
  1980. inc(CharLen);
  1981. end;
  1982. //Test for the "CharLen" conforms UTF-8 string
  1983. //This means the 10xxxxxx pattern.
  1984. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1985. begin
  1986. //Insuficient chars in string to decode
  1987. //UTF-8 array. Fallback to single char.
  1988. CharLen:= 1;
  1989. end;
  1990. for LookAhead := 1 to CharLen-1 do
  1991. begin
  1992. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1993. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1994. begin
  1995. //Invalid UTF-8 sequence, fallback.
  1996. CharLen:= LookAhead;
  1997. break;
  1998. end;
  1999. end;
  2000. UC:=$FFFF;
  2001. case CharLen of
  2002. 1: begin
  2003. //Not valid UTF-8 sequence
  2004. UC:=UNICODE_INVALID;
  2005. end;
  2006. 2: begin
  2007. //Two bytes UTF, convert it
  2008. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  2009. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  2010. if UC <= $7F then
  2011. begin
  2012. //Invalid UTF sequence.
  2013. UC:=UNICODE_INVALID;
  2014. end;
  2015. end;
  2016. 3: begin
  2017. //Three bytes, convert it to unicode
  2018. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  2019. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  2020. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  2021. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  2022. begin
  2023. //Invalid UTF-8 sequence
  2024. UC:= UNICODE_INVALID;
  2025. end;
  2026. end;
  2027. 4: begin
  2028. //Four bytes, convert it to two unicode characters
  2029. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  2030. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  2031. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  2032. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  2033. if (UC < $10000) or (UC > $10FFFF) then
  2034. UC:= UNICODE_INVALID
  2035. else
  2036. { extra character character }
  2037. inc(OutputUnicode);
  2038. end;
  2039. 5,6,7: begin
  2040. //Invalid UTF8 to unicode conversion,
  2041. //mask it as invalid UNICODE too.
  2042. UC:=UNICODE_INVALID;
  2043. end;
  2044. end;
  2045. if CharLen > 0 then
  2046. begin
  2047. PreChar:=UC;
  2048. inc(OutputUnicode);
  2049. end;
  2050. InputUTF8:= InputUTF8 + CharLen;
  2051. end;
  2052. end;
  2053. Result:=OutputUnicode+1;
  2054. end;
  2055. end;
  2056. function UTF8Encode(const s : Ansistring) : UTF8String; inline;
  2057. begin
  2058. Result:=UTF8Encode(UnicodeString(s));
  2059. end;
  2060. function UTF8Encode(const s : UnicodeString) : UTF8String;
  2061. var
  2062. i : SizeInt;
  2063. hs : UTF8String;
  2064. begin
  2065. result:='';
  2066. if s='' then
  2067. exit;
  2068. SetLength(hs,length(s)*3);
  2069. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  2070. if i>0 then
  2071. begin
  2072. SetLength(hs,i-1);
  2073. result:=hs;
  2074. end;
  2075. end;
  2076. function UTF8Decode(const s : UTF8String): UnicodeString;
  2077. var
  2078. i : SizeInt;
  2079. hs : UnicodeString;
  2080. begin
  2081. result:='';
  2082. if s='' then
  2083. exit;
  2084. SetLength(hs,length(s));
  2085. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  2086. if i>0 then
  2087. begin
  2088. SetLength(hs,i-1);
  2089. result:=hs;
  2090. end;
  2091. end;
  2092. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  2093. begin
  2094. Result:=Utf8Encode(s);
  2095. end;
  2096. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  2097. begin
  2098. Result:=Utf8Decode(s);
  2099. end;
  2100. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  2101. var
  2102. i, slen,
  2103. destindex : SizeInt;
  2104. len : longint;
  2105. begin
  2106. slen:=length(s);
  2107. setlength(result,slen+1);
  2108. i:=1;
  2109. destindex:=0;
  2110. while (i<=slen) do
  2111. begin
  2112. result[destindex]:=utf16toutf32(s,i,len);
  2113. inc(destindex);
  2114. inc(i,len);
  2115. end;
  2116. { destindex <= slen (surrogate pairs may have been merged) }
  2117. { destindex+1 for terminating #0 (dynamic arrays are }
  2118. { implicitely filled with zero) }
  2119. setlength(result,destindex+1);
  2120. end;
  2121. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  2122. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: UnicodeString; var index: SizeInt);
  2123. var
  2124. p : PUnicodeChar;
  2125. begin
  2126. { if nc > $ffff, we need two places }
  2127. if (index+ord(nc > $ffff)>length(s)) then
  2128. if (length(s) < 10*256) then
  2129. setlength(s,length(s)+10)
  2130. else
  2131. setlength(s,length(s)+length(s) shr 8);
  2132. { we know that s is unique -> avoid uniquestring calls}
  2133. p:=@s[index];
  2134. if (nc<$ffff) then
  2135. begin
  2136. p^:=unicodechar(nc);
  2137. inc(index);
  2138. end
  2139. else if (dword(nc)<=$10ffff) then
  2140. begin
  2141. p^:=unicodechar((nc - $10000) shr 10 + $d800);
  2142. (p+1)^:=unicodechar((nc - $10000) and $3ff + $dc00);
  2143. inc(index,2);
  2144. end
  2145. else
  2146. { invalid code point }
  2147. begin
  2148. p^:='?';
  2149. inc(index);
  2150. end;
  2151. end;
  2152. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  2153. var
  2154. i : SizeInt;
  2155. resindex : SizeInt;
  2156. begin
  2157. { skip terminating #0 }
  2158. SetLength(result,length(s)-1);
  2159. resindex:=1;
  2160. for i:=0 to high(s)-1 do
  2161. ConcatUTF32ToUnicodeStr(s[i],result,resindex);
  2162. { adjust result length (may be too big due to growing }
  2163. { for surrogate pairs) }
  2164. setlength(result,resindex-1);
  2165. end;
  2166. function WideStringToUCS4String(const s : WideString) : UCS4String;
  2167. var
  2168. i, slen,
  2169. destindex : SizeInt;
  2170. len : longint;
  2171. begin
  2172. slen:=length(s);
  2173. setlength(result,slen+1);
  2174. i:=1;
  2175. destindex:=0;
  2176. while (i<=slen) do
  2177. begin
  2178. result[destindex]:=utf16toutf32(s,i,len);
  2179. inc(destindex);
  2180. inc(i,len);
  2181. end;
  2182. { destindex <= slen (surrogate pairs may have been merged) }
  2183. { destindex+1 for terminating #0 (dynamic arrays are }
  2184. { implicitely filled with zero) }
  2185. setlength(result,destindex+1);
  2186. end;
  2187. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  2188. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  2189. var
  2190. p : PWideChar;
  2191. begin
  2192. { if nc > $ffff, we need two places }
  2193. if (index+ord(nc > $ffff)>length(s)) then
  2194. if (length(s) < 10*256) then
  2195. setlength(s,length(s)+10)
  2196. else
  2197. setlength(s,length(s)+length(s) shr 8);
  2198. { we know that s is unique -> avoid uniquestring calls}
  2199. p:=@s[index];
  2200. if (nc<$ffff) then
  2201. begin
  2202. p^:=widechar(nc);
  2203. inc(index);
  2204. end
  2205. else if (dword(nc)<=$10ffff) then
  2206. begin
  2207. p^:=widechar((nc - $10000) shr 10 + $d800);
  2208. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  2209. inc(index,2);
  2210. end
  2211. else
  2212. { invalid code point }
  2213. begin
  2214. p^:='?';
  2215. inc(index);
  2216. end;
  2217. end;
  2218. function UCS4StringToWideString(const s : UCS4String) : WideString;
  2219. var
  2220. i : SizeInt;
  2221. resindex : SizeInt;
  2222. begin
  2223. { skip terminating #0 }
  2224. SetLength(result,length(s)-1);
  2225. resindex:=1;
  2226. for i:=0 to high(s)-1 do
  2227. ConcatUTF32ToWideStr(s[i],result,resindex);
  2228. { adjust result length (may be too big due to growing }
  2229. { for surrogate pairs) }
  2230. setlength(result,resindex-1);
  2231. end;
  2232. const
  2233. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  2234. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  2235. procedure unimplementedunicodestring;
  2236. begin
  2237. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  2238. If IsConsole then
  2239. begin
  2240. Writeln(StdErr,SNoUnicodestrings);
  2241. Writeln(StdErr,SRecompileWithUnicodestrings);
  2242. end;
  2243. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  2244. HandleErrorFrame(233,get_frame);
  2245. end;
  2246. {$warnings off}
  2247. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  2248. begin
  2249. unimplementedunicodestring;
  2250. end;
  2251. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  2252. begin
  2253. unimplementedunicodestring;
  2254. end;
  2255. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  2256. begin
  2257. unimplementedunicodestring;
  2258. end;
  2259. {$warnings on}
  2260. procedure initunicodestringmanager;
  2261. begin
  2262. {$ifndef HAS_WIDESTRINGMANAGER}
  2263. widestringmanager.Unicode2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2264. widestringmanager.Ansi2UnicodeMoveProc:=@defaultAnsi2UnicodeMove;
  2265. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  2266. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  2267. {$endif HAS_WIDESTRINGMANAGER}
  2268. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  2269. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  2270. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2271. {$ifndef HAS_WIDESTRINGMANAGER}
  2272. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2273. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2274. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  2275. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  2276. {$endif HAS_WIDESTRINGMANAGER}
  2277. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  2278. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  2279. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  2280. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  2281. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2282. end;