ustrings.inc 58 KB

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