ustrings.inc 67 KB

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