ustrings.inc 64 KB

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