ustrings.inc 68 KB

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