ustrings.inc 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. LS:=Length(S);
  1407. if (Index>LS) or (Index<=0) or (Size<=0) then
  1408. exit;
  1409. UniqueString (S);
  1410. { (Size+Index) will overflow if Size=MaxInt. }
  1411. if Size>LS-Index then
  1412. Size:=LS-Index+1;
  1413. if Size<=LS-Index then
  1414. begin
  1415. Dec(Index);
  1416. Move(PUnicodeChar(S)[Index+Size],PUnicodeChar(S)[Index],(LS-Index-Size+1)*sizeof(UnicodeChar));
  1417. end;
  1418. Setlength(s,LS-Size);
  1419. end;
  1420. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  1421. var
  1422. Temp : UnicodeString;
  1423. LS : SizeInt;
  1424. begin
  1425. If Length(Source)=0 then
  1426. exit;
  1427. if index <= 0 then
  1428. index := 1;
  1429. Ls:=Length(S);
  1430. if index > LS then
  1431. index := LS+1;
  1432. Dec(Index);
  1433. Pointer(Temp) := NewUnicodeString(Length(Source)+LS);
  1434. SetLength(Temp,Length(Source)+LS);
  1435. If Index>0 then
  1436. move (PUnicodeChar(S)^,PUnicodeChar(Temp)^,Index*sizeof(UnicodeChar));
  1437. Move (PUnicodeChar(Source)^,PUnicodeChar(Temp)[Index],Length(Source)*sizeof(UnicodeChar));
  1438. If (LS-Index)>0 then
  1439. Move(PUnicodeChar(S)[Index],PUnicodeChar(temp)[Length(Source)+index],(LS-Index)*sizeof(UnicodeChar));
  1440. S:=Temp;
  1441. end;
  1442. Function UpCase(c:UnicodeChar):UnicodeChar;
  1443. var
  1444. s : UnicodeString;
  1445. begin
  1446. s:=c;
  1447. result:=widestringmanager.UpperUnicodeStringProc(s)[1];
  1448. end;
  1449. function UpCase(const s : UnicodeString) : UnicodeString;
  1450. begin
  1451. result:=widestringmanager.UpperUnicodeStringProc(s);
  1452. end;
  1453. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  1454. begin
  1455. SetLength(S,Len);
  1456. If (Buf<>Nil) and (Len>0) then
  1457. Move (Buf[0],S[1],Len*sizeof(UnicodeChar));
  1458. end;
  1459. Procedure SetString (Out S : UnicodeString; Buf : PChar; Len : SizeInt);
  1460. var
  1461. BufLen: SizeInt;
  1462. begin
  1463. SetLength(S,Len);
  1464. If (Buf<>Nil) and (Len>0) then
  1465. widestringmanager.Ansi2UnicodeMoveProc(Buf,S,Len);
  1466. end;
  1467. {$ifndef FPUNONE}
  1468. Function fpc_Val_Real_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_UNICODESTR']; compilerproc;
  1469. Var
  1470. SS : String;
  1471. begin
  1472. fpc_Val_Real_UnicodeStr := 0;
  1473. if length(S) > 255 then
  1474. code := 256
  1475. else
  1476. begin
  1477. SS := S;
  1478. Val(SS,fpc_Val_Real_UnicodeStr,code);
  1479. end;
  1480. end;
  1481. {$endif}
  1482. function fpc_val_enum_unicodestr(str2ordindex:pointer;const s:unicodestring;out code:valsint):longint;compilerproc;
  1483. var ss:shortstring;
  1484. begin
  1485. if length(s)>255 then
  1486. code:=256
  1487. else
  1488. begin
  1489. ss:=s;
  1490. val(ss,fpc_val_enum_unicodestr,code);
  1491. end;
  1492. end;
  1493. Function fpc_Val_Currency_UnicodeStr(Const S : UnicodeString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_UNICODESTR']; compilerproc;
  1494. Var
  1495. SS : String;
  1496. begin
  1497. if length(S) > 255 then
  1498. begin
  1499. fpc_Val_Currency_UnicodeStr:=0;
  1500. code := 256;
  1501. end
  1502. else
  1503. begin
  1504. SS := S;
  1505. Val(SS,fpc_Val_Currency_UnicodeStr,code);
  1506. end;
  1507. end;
  1508. Function fpc_Val_UInt_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_UNICODESTR']; compilerproc;
  1509. Var
  1510. SS : ShortString;
  1511. begin
  1512. fpc_Val_UInt_UnicodeStr := 0;
  1513. if length(S) > 255 then
  1514. code := 256
  1515. else
  1516. begin
  1517. SS := S;
  1518. Val(SS,fpc_Val_UInt_UnicodeStr,code);
  1519. end;
  1520. end;
  1521. Function fpc_Val_SInt_UnicodeStr (DestSize: SizeInt; Const S : UnicodeString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_UNICODESTR']; compilerproc;
  1522. Var
  1523. SS : ShortString;
  1524. begin
  1525. fpc_Val_SInt_UnicodeStr:=0;
  1526. if length(S)>255 then
  1527. code:=256
  1528. else
  1529. begin
  1530. SS := S;
  1531. fpc_Val_SInt_UnicodeStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1532. end;
  1533. end;
  1534. {$ifndef CPU64}
  1535. Function fpc_Val_qword_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_UNICODESTR']; compilerproc;
  1536. Var
  1537. SS : ShortString;
  1538. begin
  1539. fpc_Val_qword_UnicodeStr:=0;
  1540. if length(S)>255 then
  1541. code:=256
  1542. else
  1543. begin
  1544. SS := S;
  1545. Val(SS,fpc_Val_qword_UnicodeStr,Code);
  1546. end;
  1547. end;
  1548. Function fpc_Val_int64_UnicodeStr (Const S : UnicodeString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_UNICODESTR']; compilerproc;
  1549. Var
  1550. SS : ShortString;
  1551. begin
  1552. fpc_Val_int64_UnicodeStr:=0;
  1553. if length(S)>255 then
  1554. code:=256
  1555. else
  1556. begin
  1557. SS := S;
  1558. Val(SS,fpc_Val_int64_UnicodeStr,Code);
  1559. end;
  1560. end;
  1561. {$endif CPU64}
  1562. {$ifndef FPUNONE}
  1563. procedure fpc_UnicodeStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : UnicodeString);compilerproc;
  1564. var
  1565. ss : shortstring;
  1566. begin
  1567. str_real(len,fr,d,treal_type(rt),ss);
  1568. s:=ss;
  1569. end;
  1570. {$endif}
  1571. procedure fpc_unicodestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:unicodestring);compilerproc;
  1572. var ss:shortstring;
  1573. begin
  1574. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1575. s:=ss;
  1576. end;
  1577. {$ifdef FPC_HAS_STR_CURRENCY}
  1578. procedure fpc_UnicodeStr_Currency(c : Currency;len,fr : SizeInt;out s : UnicodeString);compilerproc;
  1579. var
  1580. ss : shortstring;
  1581. begin
  1582. str(c:len:fr,ss);
  1583. s:=ss;
  1584. end;
  1585. {$endif FPC_HAS_STR_CURRENCY}
  1586. Procedure fpc_UnicodeStr_SInt(v : ValSint; Len : SizeInt; out S : UnicodeString);compilerproc;
  1587. Var
  1588. SS : ShortString;
  1589. begin
  1590. Str (v:Len,SS);
  1591. S:=SS;
  1592. end;
  1593. Procedure fpc_UnicodeStr_UInt(v : ValUInt;Len : SizeInt; out S : UnicodeString);compilerproc;
  1594. Var
  1595. SS : ShortString;
  1596. begin
  1597. str(v:Len,SS);
  1598. S:=SS;
  1599. end;
  1600. {$ifndef CPU64}
  1601. Procedure fpc_UnicodeStr_Int64(v : Int64; Len : SizeInt; out S : UnicodeString);compilerproc;
  1602. Var
  1603. SS : ShortString;
  1604. begin
  1605. Str (v:Len,SS);
  1606. S:=SS;
  1607. end;
  1608. Procedure fpc_UnicodeStr_Qword(v : Qword;Len : SizeInt; out S : UnicodeString);compilerproc;
  1609. Var
  1610. SS : ShortString;
  1611. begin
  1612. str(v:Len,SS);
  1613. S:=SS;
  1614. end;
  1615. {$endif CPU64}
  1616. { converts an utf-16 code point or surrogate pair to utf-32 }
  1617. function utf16toutf32(const S: UnicodeString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1618. var
  1619. w: unicodechar;
  1620. begin
  1621. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1622. { are the same in UTF-32 }
  1623. w:=s[index];
  1624. if (w<=#$d7ff) or
  1625. (w>=#$e000) then
  1626. begin
  1627. result:=UCS4Char(w);
  1628. len:=1;
  1629. end
  1630. { valid surrogate pair? }
  1631. else if (w<=#$dbff) and
  1632. { w>=#$d7ff check not needed, checked above }
  1633. (index<length(s)) and
  1634. (s[index+1]>=#$dc00) and
  1635. (s[index+1]<=#$dfff) then
  1636. { convert the surrogate pair to UTF-32 }
  1637. begin
  1638. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1639. len:=2;
  1640. end
  1641. else
  1642. { invalid surrogate -> do nothing }
  1643. begin
  1644. result:=UCS4Char(w);
  1645. len:=1;
  1646. end;
  1647. end;
  1648. function UnicodeToUtf8(Dest: PChar; Source: PUnicodeChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1649. begin
  1650. if assigned(Source) then
  1651. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1652. else
  1653. Result:=0;
  1654. end;
  1655. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
  1656. var
  1657. i,j : SizeUInt;
  1658. w : word;
  1659. lw : longword;
  1660. len : longint;
  1661. begin
  1662. result:=0;
  1663. if source=nil then
  1664. exit;
  1665. i:=0;
  1666. j:=0;
  1667. if assigned(Dest) then
  1668. begin
  1669. while (i<SourceChars) and (j<MaxDestBytes) do
  1670. begin
  1671. w:=word(Source[i]);
  1672. case w of
  1673. 0..$7f:
  1674. begin
  1675. Dest[j]:=char(w);
  1676. inc(j);
  1677. end;
  1678. $80..$7ff:
  1679. begin
  1680. if j+1>=MaxDestBytes then
  1681. break;
  1682. Dest[j]:=char($c0 or (w shr 6));
  1683. Dest[j+1]:=char($80 or (w and $3f));
  1684. inc(j,2);
  1685. end;
  1686. $800..$d7ff,$e000..$ffff:
  1687. begin
  1688. if j+2>=MaxDestBytes then
  1689. break;
  1690. Dest[j]:=char($e0 or (w shr 12));
  1691. Dest[j+1]:=char($80 or ((w shr 6) and $3f));
  1692. Dest[j+2]:=char($80 or (w and $3f));
  1693. inc(j,3);
  1694. end;
  1695. $d800..$dbff:
  1696. {High Surrogates}
  1697. begin
  1698. if j+3>=MaxDestBytes then
  1699. break;
  1700. if (i<sourcechars-1) and
  1701. (word(Source[i+1]) >= $dc00) and
  1702. (word(Source[i+1]) <= $dfff) then
  1703. begin
  1704. lw:=longword(utf16toutf32(Source[i] + Source[i+1], 1, len));
  1705. Dest[j]:=char($f0 or (lw shr 18));
  1706. Dest[j+1]:=char($80 or ((lw shr 12) and $3f));
  1707. Dest[j+2]:=char($80 or ((lw shr 6) and $3f));
  1708. Dest[j+3]:=char($80 or (lw and $3f));
  1709. inc(j,4);
  1710. inc(i);
  1711. end;
  1712. end;
  1713. end;
  1714. inc(i);
  1715. end;
  1716. if j>SizeUInt(MaxDestBytes-1) then
  1717. j:=MaxDestBytes-1;
  1718. Dest[j]:=#0;
  1719. end
  1720. else
  1721. begin
  1722. while i<SourceChars do
  1723. begin
  1724. case word(Source[i]) of
  1725. $0..$7f:
  1726. inc(j);
  1727. $80..$7ff:
  1728. inc(j,2);
  1729. $800..$d7ff,$e000..$ffff:
  1730. inc(j,3);
  1731. $d800..$dbff:
  1732. begin
  1733. if (i<sourcechars-1) and
  1734. (word(Source[i+1]) >= $dc00) and
  1735. (word(Source[i+1]) <= $dfff) then
  1736. begin
  1737. inc(j,4);
  1738. inc(i);
  1739. end;
  1740. end;
  1741. end;
  1742. inc(i);
  1743. end;
  1744. end;
  1745. result:=j+1;
  1746. end;
  1747. function Utf8ToUnicode(Dest: PUnicodeChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1748. begin
  1749. if assigned(Source) then
  1750. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1751. else
  1752. Result:=0;
  1753. end;
  1754. function UTF8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1755. const
  1756. UNICODE_INVALID=63;
  1757. var
  1758. InputUTF8: SizeUInt;
  1759. IBYTE: BYTE;
  1760. OutputUnicode: SizeUInt;
  1761. PRECHAR: SizeUInt;
  1762. TempBYTE: BYTE;
  1763. CharLen: SizeUint;
  1764. LookAhead: SizeUInt;
  1765. UC: SizeUInt;
  1766. begin
  1767. if not assigned(Source) then
  1768. begin
  1769. result:=0;
  1770. exit;
  1771. end;
  1772. result:=SizeUInt(-1);
  1773. InputUTF8:=0;
  1774. OutputUnicode:=0;
  1775. PreChar:=0;
  1776. if Assigned(Dest) Then
  1777. begin
  1778. while (OutputUnicode<MaxDestChars) and (InputUTF8<SourceBytes) do
  1779. begin
  1780. IBYTE:=byte(Source[InputUTF8]);
  1781. if (IBYTE and $80) = 0 then
  1782. begin
  1783. //One character US-ASCII, convert it to unicode
  1784. if IBYTE = 10 then
  1785. begin
  1786. If (PreChar<>13) and FALSE then
  1787. begin
  1788. //Expand to crlf, conform UTF-8.
  1789. //This procedure will break the memory alocation by
  1790. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1791. if OutputUnicode+1<MaxDestChars then
  1792. begin
  1793. Dest[OutputUnicode]:=WideChar(13);
  1794. inc(OutputUnicode);
  1795. Dest[OutputUnicode]:=WideChar(10);
  1796. inc(OutputUnicode);
  1797. PreChar:=10;
  1798. end
  1799. else
  1800. begin
  1801. Dest[OutputUnicode]:=WideChar(13);
  1802. inc(OutputUnicode);
  1803. end;
  1804. end
  1805. else
  1806. begin
  1807. Dest[OutputUnicode]:=WideChar(IBYTE);
  1808. inc(OutputUnicode);
  1809. PreChar:=IBYTE;
  1810. end;
  1811. end
  1812. else
  1813. begin
  1814. Dest[OutputUnicode]:=WideChar(IBYTE);
  1815. inc(OutputUnicode);
  1816. PreChar:=IBYTE;
  1817. end;
  1818. inc(InputUTF8);
  1819. end
  1820. else
  1821. begin
  1822. TempByte:=IBYTE;
  1823. CharLen:=0;
  1824. while (TempBYTE and $80)<>0 do
  1825. begin
  1826. TempBYTE:=(TempBYTE shl 1) and $FE;
  1827. inc(CharLen);
  1828. end;
  1829. //Test for the "CharLen" conforms UTF-8 string
  1830. //This means the 10xxxxxx pattern.
  1831. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1832. begin
  1833. //Insuficient chars in string to decode
  1834. //UTF-8 array. Fallback to single char.
  1835. CharLen:= 1;
  1836. end;
  1837. for LookAhead := 1 to CharLen-1 do
  1838. begin
  1839. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1840. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1841. begin
  1842. //Invalid UTF-8 sequence, fallback.
  1843. CharLen:= LookAhead;
  1844. break;
  1845. end;
  1846. end;
  1847. UC:=$FFFF;
  1848. case CharLen of
  1849. 1: begin
  1850. //Not valid UTF-8 sequence
  1851. UC:=UNICODE_INVALID;
  1852. end;
  1853. 2: begin
  1854. //Two bytes UTF, convert it
  1855. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1856. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1857. if UC <= $7F then
  1858. begin
  1859. //Invalid UTF sequence.
  1860. UC:=UNICODE_INVALID;
  1861. end;
  1862. end;
  1863. 3: begin
  1864. //Three bytes, convert it to unicode
  1865. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1866. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1867. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1868. if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1869. begin
  1870. //Invalid UTF-8 sequence
  1871. UC:= UNICODE_INVALID;
  1872. End;
  1873. end;
  1874. 4: begin
  1875. //Four bytes, convert it to two unicode characters
  1876. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  1877. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  1878. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  1879. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  1880. if (UC < $10000) or (UC > $10FFFF) then
  1881. begin
  1882. UC:= UNICODE_INVALID;
  1883. end
  1884. else
  1885. begin
  1886. { only store pair if room }
  1887. dec(UC,$10000);
  1888. if (OutputUnicode<MaxDestChars-1) then
  1889. begin
  1890. Dest[OutputUnicode]:=WideChar(UC shr 10 + $D800);
  1891. inc(OutputUnicode);
  1892. UC:=(UC and $3ff) + $DC00;
  1893. end
  1894. else
  1895. begin
  1896. InputUTF8:= InputUTF8 + CharLen;
  1897. { don't store anything }
  1898. CharLen:=0;
  1899. end;
  1900. end;
  1901. end;
  1902. 5,6,7: begin
  1903. //Invalid UTF8 to unicode conversion,
  1904. //mask it as invalid UNICODE too.
  1905. UC:=UNICODE_INVALID;
  1906. end;
  1907. end;
  1908. if CharLen > 0 then
  1909. begin
  1910. PreChar:=UC;
  1911. Dest[OutputUnicode]:=WideChar(UC);
  1912. inc(OutputUnicode);
  1913. end;
  1914. InputUTF8:= InputUTF8 + CharLen;
  1915. end;
  1916. end;
  1917. Result:=OutputUnicode+1;
  1918. end
  1919. else
  1920. begin
  1921. while (InputUTF8<SourceBytes) do
  1922. begin
  1923. IBYTE:=byte(Source[InputUTF8]);
  1924. if (IBYTE and $80) = 0 then
  1925. begin
  1926. //One character US-ASCII, convert it to unicode
  1927. if IBYTE = 10 then
  1928. begin
  1929. if (PreChar<>13) and FALSE then
  1930. begin
  1931. //Expand to crlf, conform UTF-8.
  1932. //This procedure will break the memory alocation by
  1933. //FPC for the widestring, so never use it. Condition never true due the "and FALSE".
  1934. inc(OutputUnicode,2);
  1935. PreChar:=10;
  1936. end
  1937. else
  1938. begin
  1939. inc(OutputUnicode);
  1940. PreChar:=IBYTE;
  1941. end;
  1942. end
  1943. else
  1944. begin
  1945. inc(OutputUnicode);
  1946. PreChar:=IBYTE;
  1947. end;
  1948. inc(InputUTF8);
  1949. end
  1950. else
  1951. begin
  1952. TempByte:=IBYTE;
  1953. CharLen:=0;
  1954. while (TempBYTE and $80)<>0 do
  1955. begin
  1956. TempBYTE:=(TempBYTE shl 1) and $FE;
  1957. inc(CharLen);
  1958. end;
  1959. //Test for the "CharLen" conforms UTF-8 string
  1960. //This means the 10xxxxxx pattern.
  1961. if SizeUInt(InputUTF8+CharLen-1)>SourceBytes then
  1962. begin
  1963. //Insuficient chars in string to decode
  1964. //UTF-8 array. Fallback to single char.
  1965. CharLen:= 1;
  1966. end;
  1967. for LookAhead := 1 to CharLen-1 do
  1968. begin
  1969. if ((byte(Source[InputUTF8+LookAhead]) and $80)<>$80) or
  1970. ((byte(Source[InputUTF8+LookAhead]) and $40)<>$00) then
  1971. begin
  1972. //Invalid UTF-8 sequence, fallback.
  1973. CharLen:= LookAhead;
  1974. break;
  1975. end;
  1976. end;
  1977. UC:=$FFFF;
  1978. case CharLen of
  1979. 1: begin
  1980. //Not valid UTF-8 sequence
  1981. UC:=UNICODE_INVALID;
  1982. end;
  1983. 2: begin
  1984. //Two bytes UTF, convert it
  1985. UC:=(byte(Source[InputUTF8]) and $1F) shl 6;
  1986. UC:=UC or (byte(Source[InputUTF8+1]) and $3F);
  1987. if UC <= $7F then
  1988. begin
  1989. //Invalid UTF sequence.
  1990. UC:=UNICODE_INVALID;
  1991. end;
  1992. end;
  1993. 3: begin
  1994. //Three bytes, convert it to unicode
  1995. UC:= (byte(Source[InputUTF8]) and $0F) shl 12;
  1996. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 6);
  1997. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F));
  1998. If (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
  1999. begin
  2000. //Invalid UTF-8 sequence
  2001. UC:= UNICODE_INVALID;
  2002. end;
  2003. end;
  2004. 4: begin
  2005. //Four bytes, convert it to two unicode characters
  2006. UC:= (byte(Source[InputUTF8]) and $07) shl 18;
  2007. UC:= UC or ((byte(Source[InputUTF8+1]) and $3F) shl 12);
  2008. UC:= UC or ((byte(Source[InputUTF8+2]) and $3F) shl 6);
  2009. UC:= UC or ((byte(Source[InputUTF8+3]) and $3F));
  2010. if (UC < $10000) or (UC > $10FFFF) then
  2011. UC:= UNICODE_INVALID
  2012. else
  2013. { extra character character }
  2014. inc(OutputUnicode);
  2015. end;
  2016. 5,6,7: begin
  2017. //Invalid UTF8 to unicode conversion,
  2018. //mask it as invalid UNICODE too.
  2019. UC:=UNICODE_INVALID;
  2020. end;
  2021. end;
  2022. if CharLen > 0 then
  2023. begin
  2024. PreChar:=UC;
  2025. inc(OutputUnicode);
  2026. end;
  2027. InputUTF8:= InputUTF8 + CharLen;
  2028. end;
  2029. end;
  2030. Result:=OutputUnicode+1;
  2031. end;
  2032. end;
  2033. function UTF8Encode(const s : Ansistring) : UTF8String; inline;
  2034. begin
  2035. Result:=UTF8Encode(UnicodeString(s));
  2036. end;
  2037. function UTF8Encode(const s : UnicodeString) : UTF8String;
  2038. var
  2039. i : SizeInt;
  2040. hs : UTF8String;
  2041. begin
  2042. result:='';
  2043. if s='' then
  2044. exit;
  2045. SetLength(hs,length(s)*3);
  2046. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PUnicodeChar(s),length(s));
  2047. if i>0 then
  2048. begin
  2049. SetLength(hs,i-1);
  2050. result:=hs;
  2051. end;
  2052. end;
  2053. function UTF8Decode(const s : UTF8String): UnicodeString;
  2054. var
  2055. i : SizeInt;
  2056. hs : UnicodeString;
  2057. begin
  2058. result:='';
  2059. if s='' then
  2060. exit;
  2061. SetLength(hs,length(s));
  2062. i:=Utf8ToUnicode(PUnicodeChar(hs),length(hs)+1,pchar(s),length(s));
  2063. if i>0 then
  2064. begin
  2065. SetLength(hs,i-1);
  2066. result:=hs;
  2067. end;
  2068. end;
  2069. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  2070. begin
  2071. Result:=Utf8Encode(s);
  2072. end;
  2073. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  2074. begin
  2075. Result:=Utf8Decode(s);
  2076. end;
  2077. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  2078. var
  2079. i, slen,
  2080. destindex : SizeInt;
  2081. len : longint;
  2082. begin
  2083. slen:=length(s);
  2084. setlength(result,slen+1);
  2085. i:=1;
  2086. destindex:=0;
  2087. while (i<=slen) do
  2088. begin
  2089. result[destindex]:=utf16toutf32(s,i,len);
  2090. inc(destindex);
  2091. inc(i,len);
  2092. end;
  2093. { destindex <= slen (surrogate pairs may have been merged) }
  2094. { destindex+1 for terminating #0 (dynamic arrays are }
  2095. { implicitely filled with zero) }
  2096. setlength(result,destindex+1);
  2097. end;
  2098. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  2099. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: UnicodeString; var index: SizeInt);
  2100. var
  2101. p : PUnicodeChar;
  2102. begin
  2103. { if nc > $ffff, we need two places }
  2104. if (index+ord(nc > $ffff)>length(s)) then
  2105. if (length(s) < 10*256) then
  2106. setlength(s,length(s)+10)
  2107. else
  2108. setlength(s,length(s)+length(s) shr 8);
  2109. { we know that s is unique -> avoid uniquestring calls}
  2110. p:=@s[index];
  2111. if (nc<$ffff) then
  2112. begin
  2113. p^:=unicodechar(nc);
  2114. inc(index);
  2115. end
  2116. else if (dword(nc)<=$10ffff) then
  2117. begin
  2118. p^:=unicodechar((nc - $10000) shr 10 + $d800);
  2119. (p+1)^:=unicodechar((nc - $10000) and $3ff + $dc00);
  2120. inc(index,2);
  2121. end
  2122. else
  2123. { invalid code point }
  2124. begin
  2125. p^:='?';
  2126. inc(index);
  2127. end;
  2128. end;
  2129. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  2130. var
  2131. i : SizeInt;
  2132. resindex : SizeInt;
  2133. begin
  2134. { skip terminating #0 }
  2135. SetLength(result,length(s)-1);
  2136. resindex:=1;
  2137. for i:=0 to high(s)-1 do
  2138. ConcatUTF32ToUnicodeStr(s[i],result,resindex);
  2139. { adjust result length (may be too big due to growing }
  2140. { for surrogate pairs) }
  2141. setlength(result,resindex-1);
  2142. end;
  2143. function WideStringToUCS4String(const s : WideString) : UCS4String;
  2144. var
  2145. i, slen,
  2146. destindex : SizeInt;
  2147. len : longint;
  2148. begin
  2149. slen:=length(s);
  2150. setlength(result,slen+1);
  2151. i:=1;
  2152. destindex:=0;
  2153. while (i<=slen) do
  2154. begin
  2155. result[destindex]:=utf16toutf32(s,i,len);
  2156. inc(destindex);
  2157. inc(i,len);
  2158. end;
  2159. { destindex <= slen (surrogate pairs may have been merged) }
  2160. { destindex+1 for terminating #0 (dynamic arrays are }
  2161. { implicitely filled with zero) }
  2162. setlength(result,destindex+1);
  2163. end;
  2164. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  2165. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  2166. var
  2167. p : PWideChar;
  2168. begin
  2169. { if nc > $ffff, we need two places }
  2170. if (index+ord(nc > $ffff)>length(s)) then
  2171. if (length(s) < 10*256) then
  2172. setlength(s,length(s)+10)
  2173. else
  2174. setlength(s,length(s)+length(s) shr 8);
  2175. { we know that s is unique -> avoid uniquestring calls}
  2176. p:=@s[index];
  2177. if (nc<$ffff) then
  2178. begin
  2179. p^:=widechar(nc);
  2180. inc(index);
  2181. end
  2182. else if (dword(nc)<=$10ffff) then
  2183. begin
  2184. p^:=widechar((nc - $10000) shr 10 + $d800);
  2185. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  2186. inc(index,2);
  2187. end
  2188. else
  2189. { invalid code point }
  2190. begin
  2191. p^:='?';
  2192. inc(index);
  2193. end;
  2194. end;
  2195. function UCS4StringToWideString(const s : UCS4String) : WideString;
  2196. var
  2197. i : SizeInt;
  2198. resindex : SizeInt;
  2199. begin
  2200. { skip terminating #0 }
  2201. SetLength(result,length(s)-1);
  2202. resindex:=1;
  2203. for i:=0 to high(s)-1 do
  2204. ConcatUTF32ToWideStr(s[i],result,resindex);
  2205. { adjust result length (may be too big due to growing }
  2206. { for surrogate pairs) }
  2207. setlength(result,resindex-1);
  2208. end;
  2209. const
  2210. SNoUnicodestrings = 'This binary has no unicodestrings support compiled in.';
  2211. SRecompileWithUnicodestrings = 'Recompile the application with a unicodestrings-manager in the program uses clause.';
  2212. procedure unimplementedunicodestring;
  2213. begin
  2214. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  2215. If IsConsole then
  2216. begin
  2217. Writeln(StdErr,SNoUnicodestrings);
  2218. Writeln(StdErr,SRecompileWithUnicodestrings);
  2219. end;
  2220. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  2221. HandleErrorFrame(233,get_frame);
  2222. end;
  2223. {$warnings off}
  2224. function GenericUnicodeCase(const s : UnicodeString) : UnicodeString;
  2225. begin
  2226. unimplementedunicodestring;
  2227. end;
  2228. function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
  2229. begin
  2230. unimplementedunicodestring;
  2231. end;
  2232. function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
  2233. begin
  2234. unimplementedunicodestring;
  2235. end;
  2236. function CharLengthPChar(const Str: PChar): PtrInt;
  2237. begin
  2238. unimplementedunicodestring;
  2239. end;
  2240. {$warnings on}
  2241. procedure initunicodestringmanager;
  2242. begin
  2243. {$ifndef HAS_WIDESTRINGMANAGER}
  2244. widestringmanager.Unicode2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2245. widestringmanager.Ansi2UnicodeMoveProc:=@defaultAnsi2UnicodeMove;
  2246. widestringmanager.UpperUnicodeStringProc:=@GenericUnicodeCase;
  2247. widestringmanager.LowerUnicodeStringProc:=@GenericUnicodeCase;
  2248. {$endif HAS_WIDESTRINGMANAGER}
  2249. widestringmanager.CompareUnicodeStringProc:=@CompareUnicodeString;
  2250. widestringmanager.CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
  2251. {$ifdef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2252. {$ifndef HAS_WIDESTRINGMANAGER}
  2253. widestringmanager.Wide2AnsiMoveProc:=@defaultUnicode2AnsiMove;
  2254. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2UnicodeMove;
  2255. widestringmanager.UpperWideStringProc:=@GenericUnicodeCase;
  2256. widestringmanager.LowerWideStringProc:=@GenericUnicodeCase;
  2257. {$endif HAS_WIDESTRINGMANAGER}
  2258. widestringmanager.CompareWideStringProc:=@CompareUnicodeString;
  2259. widestringmanager.CompareTextWideStringProc:=@CompareTextUnicodeString;
  2260. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  2261. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2262. end;