ustrings.inc 68 KB

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