ustrings.inc 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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. {$ifdef FPC_HAS_STR_CURRENCY}
  1595. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1596. var
  1597. ss : shortstring;
  1598. begin
  1599. str(c:len:fr,ss);
  1600. s:=ss;
  1601. end;
  1602. {$endif FPC_HAS_STR_CURRENCY}
  1603. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1604. Var
  1605. SS : ShortString;
  1606. begin
  1607. Str (v:Len,SS);
  1608. S:=SS;
  1609. end;
  1610. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1611. Var
  1612. SS : ShortString;
  1613. begin
  1614. str(v:Len,SS);
  1615. S:=SS;
  1616. end;
  1617. {$ifndef CPU64}
  1618. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1619. Var
  1620. SS : ShortString;
  1621. begin
  1622. Str (v:Len,SS);
  1623. S:=SS;
  1624. end;
  1625. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1626. Var
  1627. SS : ShortString;
  1628. begin
  1629. str(v:Len,SS);
  1630. S:=SS;
  1631. end;
  1632. {$endif CPU64}
  1633. { converts an utf-16 code point or surrogate pair to utf-32 }
  1634. function utf16toutf32(const S: UnicodeString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1635. var
  1636. w: unicodechar;
  1637. begin
  1638. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1639. { are the same in UTF-32 }
  1640. w:=s[index];
  1641. if (w<=#$d7ff) or
  1642. (w>=#$e000) then
  1643. begin
  1644. result:=UCS4Char(w);
  1645. len:=1;
  1646. end
  1647. { valid surrogate pair? }
  1648. else if (w<=#$dbff) and
  1649. { w>=#$d7ff check not needed, checked above }
  1650. (index<length(s)) and
  1651. (s[index+1]>=#$dc00) and
  1652. (s[index+1]<=#$dfff) then
  1653. { convert the surrogate pair to UTF-32 }
  1654. begin
  1655. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1656. len:=2;
  1657. end
  1658. else
  1659. { invalid surrogate -> do nothing }
  1660. begin
  1661. result:=UCS4Char(w);
  1662. len:=1;
  1663. end;
  1664. end;
  1665. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1666. begin
  1667. if assigned(Source) then
  1668. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1669. else
  1670. Result:=0;
  1671. end;
  1672. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1673. var
  1674. i,j : SizeUInt;
  1675. w : word;
  1676. lw : longword;
  1677. len : longint;
  1678. begin
  1679. result:=0;
  1680. if source=nil then
  1681. exit;
  1682. i:=0;
  1683. j:=0;
  1684. if assigned(Dest) then
  1685. begin
  1686. while (i<SourceChars) and (j<MaxDestBytes) do
  1687. begin
  1688. w:=word(Source[i]);
  1689. case w of
  1690. 0..$7f:
  1691. begin
  1692. Dest[j]:=char(w);
  1693. inc(j);
  1694. end;
  1695. $80..$7ff:
  1696. begin
  1697. if j+1>=MaxDestBytes then
  1698. break;
  1699. Dest[j]:=char($c0 or (w shr 6));
  1700. Dest[j+1]:=char($80 or (w and $3f));
  1701. inc(j,2);
  1702. end;
  1703. $800..$d7ff,$e000..$ffff:
  1704. begin
  1705. if j+2>=MaxDestBytes then
  1706. break;
  1707. Dest[j]:=char($e0 or (w shr 12));
  1708. Dest[j+1]:=char($80 or ((w shr 6) and $3f));
  1709. Dest[j+2]:=char($80 or (w and $3f));
  1710. inc(j,3);
  1711. end;
  1712. $d800..$dbff:
  1713. {High Surrogates}
  1714. begin
  1715. if j+3>=MaxDestBytes then
  1716. break;
  1717. if (i<sourcechars-1) and
  1718. (word(Source[i+1]) >= $dc00) and
  1719. (word(Source[i+1]) <= $dfff) then
  1720. begin
  1721. lw:=longword(utf16toutf32(Source[i] + Source[i+1], 1, len));
  1722. Dest[j]:=char($f0 or (lw shr 18));
  1723. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1724. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1725. Dest[j+3]:=char($80 or (lw and $3f));
  1726. inc(j,4);
  1727. inc(i);
  1728. end;
  1729. end;
  1730. end;
  1731. inc(i);
  1732. end;
  1733. if j>SizeUInt(MaxDestBytes-1) then
  1734. j:=MaxDestBytes-1;
  1735. Dest[j]:=#0;
  1736. end
  1737. else
  1738. begin
  1739. while i<SourceChars do
  1740. begin
  1741. case word(Source[i]) of
  1742. $0..$7f:
  1743. inc(j);
  1744. $80..$7ff:
  1745. inc(j,2);
  1746. $800..$d7ff,$e000..$ffff:
  1747. inc(j,3);
  1748. $d800..$dbff:
  1749. begin
  1750. if (i<sourcechars-1) and
  1751. (word(Source[i+1]) >= $dc00) and
  1752. (word(Source[i+1]) <= $dfff) then
  1753. begin
  1754. inc(j,4);
  1755. inc(i);
  1756. end;
  1757. end;
  1758. end;
  1759. inc(i);
  1760. end;
  1761. end;
  1762. result:=j+1;
  1763. end;
  1764. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1765. begin
  1766. if assigned(Source) then
  1767. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1768. else
  1769. Result:=0;
  1770. end;
  1771. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1772. const
  1773. UNICODE_INVALID=63;
  1774. var
  1775. InputUTF8: SizeUInt;
  1776. IBYTE: BYTE;
  1777. OutputUnicode: SizeUInt;
  1778. PRECHAR: SizeUInt;
  1779. TempBYTE: BYTE;
  1780. CharLen: SizeUint;
  1781. LookAhead: SizeUInt;
  1782. UC: SizeUInt;
  1783. begin
  1784. if not assigned(Source) then
  1785. begin
  1786. result:=0;
  1787. exit;
  1788. end;
  1789. result:=SizeUInt(-1);
  1790. InputUTF8:=0;
  1791. OutputUnicode:=0;
  1792. PreChar:=0;
  1793. if Assigned(Dest) Then
  1794. begin
  1795. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1796. begin
  1797. IBYTE:=byte(Source[InputUTF8]);
  1798. if (IBYTE and $80) = 0 then
  1799. begin
  1800. //One character US-ASCII, convert it to unicode
  1801. if IBYTE = 10 then
  1802. begin
  1803. If (PreChar<>13) and FALSE then
  1804. begin
  1805. //Expand to crlf, conform UTF-8.
  1806. //This procedure will break the memory alocation by
  1807. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1808. if OutputUnicode+1<MaxDestChars then
  1809. begin
  1810. Dest[OutputUnicode]:=WideChar(13);
  1811. inc(OutputUnicode);
  1812. Dest[OutputUnicode]:=WideChar(10);
  1813. inc(OutputUnicode);
  1814. PreChar:=10;
  1815. end
  1816. else
  1817. begin
  1818. Dest[OutputUnicode]:=WideChar(13);
  1819. inc(OutputUnicode);
  1820. end;
  1821. end
  1822. else
  1823. begin
  1824. Dest[OutputUnicode]:=WideChar(IBYTE);
  1825. inc(OutputUnicode);
  1826. PreChar:=IBYTE;
  1827. end;
  1828. end
  1829. else
  1830. begin
  1831. Dest[OutputUnicode]:=WideChar(IBYTE);
  1832. inc(OutputUnicode);
  1833. PreChar:=IBYTE;
  1834. end;
  1835. inc(InputUTF8);
  1836. end
  1837. else
  1838. begin
  1839. TempByte:=IBYTE;
  1840. CharLen:=0;
  1841. while (TempBYTE and $80)<>0 do
  1842. begin
  1843. TempBYTE:=(TempBYTE shl 1) and $FE;
  1844. inc(CharLen);
  1845. end;
  1846. //Test for the "CharLen" conforms UTF-8 string
  1847. //This means the 10xxxxxx pattern.
  1848. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1849. begin
  1850. //Insuficient chars in string to decode
  1851. //UTF-8 array. Fallback to single char.
  1852. CharLen:= 1;
  1853. end;
  1854. for LookAhead := 1 to CharLen-1 do
  1855. begin
  1856. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1857. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1858. begin
  1859. //Invalid UTF-8 sequence, fallback.
  1860. CharLen:= LookAhead;
  1861. break;
  1862. end;
  1863. end;
  1864. UC:=$FFFF;
  1865. case CharLen of
  1866. 1: begin
  1867. //Not valid UTF-8 sequence
  1868. UC:=UNICODE_INVALID;
  1869. end;
  1870. 2: begin
  1871. //Two bytes UTF, convert it
  1872. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1873. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1874. if UC <= $7F then
  1875. begin
  1876. //Invalid UTF sequence.
  1877. UC:=UNICODE_INVALID;
  1878. end;
  1879. end;
  1880. 3: begin
  1881. //Three bytes, convert it to unicode
  1882. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1883. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1884. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1885. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1886. begin
  1887. //Invalid UTF-8 sequence
  1888. UC:= UNICODE_INVALID;
  1889. End;
  1890. end;
  1891. 4: begin
  1892. //Four bytes, convert it to two unicode characters
  1893. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1894. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1895. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1896. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1897. if (UC < $10000) or (UC > $10FFFF) then
  1898. begin
  1899. UC:= UNICODE_INVALID;
  1900. end
  1901. else
  1902. begin
  1903. { only store pair if room }
  1904. dec(UC,$10000);
  1905. if (OutputUnicode<MaxDestChars-1) then
  1906. begin
  1907. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1908. inc(OutputUnicode);
  1909. UC:=(UC and $3ff) + $DC00;
  1910. end
  1911. else
  1912. begin
  1913. InputUTF8:= InputUTF8 + CharLen;
  1914. { don't store anything }
  1915. CharLen:=0;
  1916. end;
  1917. end;
  1918. end;
  1919. 5,6,7: begin
  1920. //Invalid UTF8 to unicode conversion,
  1921. //mask it as invalid UNICODE too.
  1922. UC:=UNICODE_INVALID;
  1923. end;
  1924. end;
  1925. if CharLen > 0 then
  1926. begin
  1927. PreChar:=UC;
  1928. Dest[OutputUnicode]:=WideChar(UC);
  1929. inc(OutputUnicode);
  1930. end;
  1931. InputUTF8:= InputUTF8 + CharLen;
  1932. end;
  1933. end;
  1934. Result:=OutputUnicode+1;
  1935. end
  1936. else
  1937. begin
  1938. while (InputUTF8<SourceBytes) do
  1939. begin
  1940. IBYTE:=byte(Source[InputUTF8]);
  1941. if (IBYTE and $80) = 0 then
  1942. begin
  1943. //One character US-ASCII, convert it to unicode
  1944. if IBYTE = 10 then
  1945. begin
  1946. if (PreChar<>13) and FALSE then
  1947. begin
  1948. //Expand to crlf, conform UTF-8.
  1949. //This procedure will break the memory alocation by
  1950. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1951. inc(OutputUnicode,2);
  1952. PreChar:=10;
  1953. end
  1954. else
  1955. begin
  1956. inc(OutputUnicode);
  1957. PreChar:=IBYTE;
  1958. end;
  1959. end
  1960. else
  1961. begin
  1962. inc(OutputUnicode);
  1963. PreChar:=IBYTE;
  1964. end;
  1965. inc(InputUTF8);
  1966. end
  1967. else
  1968. begin
  1969. TempByte:=IBYTE;
  1970. CharLen:=0;
  1971. while (TempBYTE and $80)<>0 do
  1972. begin
  1973. TempBYTE:=(TempBYTE shl 1) and $FE;
  1974. inc(CharLen);
  1975. end;
  1976. //Test for the "CharLen" conforms UTF-8 string
  1977. //This means the 10xxxxxx pattern.
  1978. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1979. begin
  1980. //Insuficient chars in string to decode
  1981. //UTF-8 array. Fallback to single char.
  1982. CharLen:= 1;
  1983. end;
  1984. for LookAhead := 1 to CharLen-1 do
  1985. begin
  1986. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1987. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1988. begin
  1989. //Invalid UTF-8 sequence, fallback.
  1990. CharLen:= LookAhead;
  1991. break;
  1992. end;
  1993. end;
  1994. UC:=$FFFF;
  1995. case CharLen of
  1996. 1: begin
  1997. //Not valid UTF-8 sequence
  1998. UC:=UNICODE_INVALID;
  1999. end;
  2000. 2: begin
  2001. //Two bytes UTF, convert it
  2002. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  2003. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  2004. if UC <= $7F then
  2005. begin
  2006. //Invalid UTF sequence.
  2007. UC:=UNICODE_INVALID;
  2008. end;
  2009. end;
  2010. 3: begin
  2011. //Three bytes, convert it to unicode
  2012. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  2013. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  2014. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  2015. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  2016. begin
  2017. //Invalid UTF-8 sequence
  2018. UC:= UNICODE_INVALID;
  2019. end;
  2020. end;
  2021. 4: begin
  2022. //Four bytes, convert it to two unicode characters
  2023. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  2024. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  2025. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  2026. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  2027. if (UC < $10000) or (UC > $10FFFF) then
  2028. UC:= UNICODE_INVALID
  2029. else
  2030. { extra character character }
  2031. inc(OutputUnicode);
  2032. end;
  2033. 5,6,7: begin
  2034. //Invalid UTF8 to unicode conversion,
  2035. //mask it as invalid UNICODE too.
  2036. UC:=UNICODE_INVALID;
  2037. end;
  2038. end;
  2039. if CharLen > 0 then
  2040. begin
  2041. PreChar:=UC;
  2042. inc(OutputUnicode);
  2043. end;
  2044. InputUTF8:= InputUTF8 + CharLen;
  2045. end;
  2046. end;
  2047. Result:=OutputUnicode+1;
  2048. end;
  2049. end;
  2050. function UTF8Encode(const s : Ansistring) : UTF8String; inline;
  2051. begin
  2052. Result:=UTF8Encode(UnicodeString(s));
  2053. end;
  2054. function UTF8Encode(const s : UnicodeString) : UTF8String;
  2055. var
  2056. i : SizeInt;
  2057. hs : UTF8String;
  2058. begin
  2059. result:='';
  2060. if s='' then
  2061. exit;
  2062. SetLength(hs,length(s)*3);
  2063. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  2064. if i>0 then
  2065. begin
  2066. SetLength(hs,i-1);
  2067. result:=hs;
  2068. end;
  2069. end;
  2070. function UTF8Decode(const s : UTF8String): UnicodeString;
  2071. var
  2072. i : SizeInt;
  2073. hs : UnicodeString;
  2074. begin
  2075. result:='';
  2076. if s='' then
  2077. exit;
  2078. SetLength(hs,length(s));
  2079. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  2080. if i>0 then
  2081. begin
  2082. SetLength(hs,i-1);
  2083. result:=hs;
  2084. end;
  2085. end;
  2086. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  2087. begin
  2088. Result:=Utf8Encode(s);
  2089. end;
  2090. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  2091. begin
  2092. Result:=Utf8Decode(s);
  2093. end;
  2094. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  2095. var
  2096. i, slen,
  2097. destindex : SizeInt;
  2098. len : longint;
  2099. begin
  2100. slen:=length(s);
  2101. setlength(result,slen+1);
  2102. i:=1;
  2103. destindex:=0;
  2104. while (i<=slen) do
  2105. begin
  2106. result[destindex]:=utf16toutf32(s,i,len);
  2107. inc(destindex);
  2108. inc(i,len);
  2109. end;
  2110. { destindex <= slen (surrogate pairs may have been merged) }
  2111. { destindex+1 for terminating #0 (dynamic arrays are }
  2112. { implicitely filled with zero) }
  2113. setlength(result,destindex+1);
  2114. end;
  2115. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  2116. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: UnicodeString; var index: SizeInt);
  2117. var
  2118. p : PUnicodeChar;
  2119. begin
  2120. { if nc > $ffff, we need two places }
  2121. if (index+ord(nc > $ffff)>length(s)) then
  2122. if (length(s) < 10*256) then
  2123. setlength(s,length(s)+10)
  2124. else
  2125. setlength(s,length(s)+length(s) shr 8);
  2126. { we know that s is unique -> avoid uniquestring calls}
  2127. p:=@s[index];
  2128. if (nc<$ffff) then
  2129. begin
  2130. p^:=unicodechar(nc);
  2131. inc(index);
  2132. end
  2133. else if (dword(nc)<=$10ffff) then
  2134. begin
  2135. p^:=unicodechar((nc - $10000) shr 10 + $d800);
  2136. (p+1)^:=unicodechar((nc - $10000) and $3ff + $dc00);
  2137. inc(index,2);
  2138. end
  2139. else
  2140. { invalid code point }
  2141. begin
  2142. p^:='?';
  2143. inc(index);
  2144. end;
  2145. end;
  2146. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  2147. var
  2148. i : SizeInt;
  2149. resindex : SizeInt;
  2150. begin
  2151. { skip terminating #0 }
  2152. SetLength(result,length(s)-1);
  2153. resindex:=1;
  2154. for i:=0 to high(s)-1 do
  2155. ConcatUTF32ToUnicodeStr(s[i],result,resindex);
  2156. { adjust result length (may be too big due to growing }
  2157. { for surrogate pairs) }
  2158. setlength(result,resindex-1);
  2159. end;
  2160. function WideStringToUCS4String(const s : WideString) : UCS4String;
  2161. var
  2162. i, slen,
  2163. destindex : SizeInt;
  2164. len : longint;
  2165. begin
  2166. slen:=length(s);
  2167. setlength(result,slen+1);
  2168. i:=1;
  2169. destindex:=0;
  2170. while (i<=slen) do
  2171. begin
  2172. result[destindex]:=utf16toutf32(s,i,len);
  2173. inc(destindex);
  2174. inc(i,len);
  2175. end;
  2176. { destindex <= slen (surrogate pairs may have been merged) }
  2177. { destindex+1 for terminating #0 (dynamic arrays are }
  2178. { implicitely filled with zero) }
  2179. setlength(result,destindex+1);
  2180. end;
  2181. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  2182. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  2183. var
  2184. p : PWideChar;
  2185. begin
  2186. { if nc > $ffff, we need two places }
  2187. if (index+ord(nc > $ffff)>length(s)) then
  2188. if (length(s) < 10*256) then
  2189. setlength(s,length(s)+10)
  2190. else
  2191. setlength(s,length(s)+length(s) shr 8);
  2192. { we know that s is unique -> avoid uniquestring calls}
  2193. p:=@s[index];
  2194. if (nc<$ffff) then
  2195. begin
  2196. p^:=widechar(nc);
  2197. inc(index);
  2198. end
  2199. else if (dword(nc)<=$10ffff) then
  2200. begin
  2201. p^:=widechar((nc - $10000) shr 10 + $d800);
  2202. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  2203. inc(index,2);
  2204. end
  2205. else
  2206. { invalid code point }
  2207. begin
  2208. p^:='?';
  2209. inc(index);
  2210. end;
  2211. end;
  2212. function UCS4StringToWideString(const s : UCS4String) : WideString;
  2213. var
  2214. i : SizeInt;
  2215. resindex : SizeInt;
  2216. begin
  2217. { skip terminating #0 }
  2218. SetLength(result,length(s)-1);
  2219. resindex:=1;
  2220. for i:=0 to high(s)-1 do
  2221. ConcatUTF32ToWideStr(s[i],result,resindex);
  2222. { adjust result length (may be too big due to growing }
  2223. { for surrogate pairs) }
  2224. setlength(result,resindex-1);
  2225. end;
  2226. const
  2227. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  2228. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  2229. procedure unimplementedunicodestring;
  2230. begin
  2231. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  2232. If IsConsole then
  2233. begin
  2234. Writeln(StdErr,SNoUnicodestrings);
  2235. Writeln(StdErr,SRecompileWithUnicodestrings);
  2236. end;
  2237. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  2238. HandleErrorFrame(233,get_frame);
  2239. end;
  2240. {$warnings off}
  2241. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  2242. begin
  2243. unimplementedunicodestring;
  2244. end;
  2245. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  2246. begin
  2247. unimplementedunicodestring;
  2248. end;
  2249. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  2250. begin
  2251. unimplementedunicodestring;
  2252. end;
  2253. {$warnings on}
  2254. procedure initunicodestringmanager;
  2255. begin
  2256. {$ifndef HAS_WIDESTRINGMANAGER}
  2257. widestringmanager.Unicode2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2258. widestringmanager.Ansi2UnicodeMoveProc:=@defaultAnsi2UnicodeMove;
  2259. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  2260. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  2261. {$endif HAS_WIDESTRINGMANAGER}
  2262. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  2263. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  2264. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2265. {$ifndef HAS_WIDESTRINGMANAGER}
  2266. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2267. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2268. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  2269. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  2270. {$endif HAS_WIDESTRINGMANAGER}
  2271. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  2272. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  2273. widestringmanager.CharLengthPCharProc:=@DefaultCharLengthPChar;
  2274. widestringmanager.CodePointLengthProc:=@DefaultCodePointLength;
  2275. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2276. end;