ustrings.inc 72 KB

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