wstrings.inc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2001 by Florian Klaempfl,
  5. member of the Free Pascal development team.
  6. This file implements support routines for WideStrings/Unicode with FPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {
  14. This file contains the implementation of the WideString type,
  15. and all things that are needed for it.
  16. WideString is defined as a 'silent' pwidechar :
  17. a pwidechar that points to :
  18. @-8 : SizeInt for reference count;
  19. @-4 : SizeInt for size;
  20. @ : String + Terminating #0;
  21. Pwidechar(Widestring) is a valid typecast.
  22. So WS[i] is converted to the address @WS+i-1.
  23. Constants should be assigned a reference count of -1
  24. Meaning that they can't be disposed of.
  25. }
  26. Type
  27. PWideRec = ^TWideRec;
  28. TWideRec = Packed Record
  29. Ref,
  30. Len : SizeInt;
  31. First : WideChar;
  32. end;
  33. Const
  34. WideRecLen = SizeOf(TWideRec);
  35. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  36. {
  37. Default WideChar <-> Char conversion is to only convert the
  38. lower 127 chars, all others are translated to spaces.
  39. These routines can be overwritten for the Current Locale
  40. }
  41. procedure Wide2AnsiMove(source:pwidechar;dest:pchar;len:SizeInt);
  42. var
  43. i : SizeInt;
  44. begin
  45. for i:=1 to len do
  46. begin
  47. if word(source^)<128 then
  48. dest^:=char(word(source^))
  49. else
  50. dest^:=' ';
  51. inc(dest);
  52. inc(source);
  53. end;
  54. end;
  55. procedure Ansi2WideMove(source:pchar;dest:pwidechar;len:SizeInt);
  56. var
  57. i : SizeInt;
  58. begin
  59. for i:=1 to len do
  60. begin
  61. if byte(source^)<128 then
  62. dest^:=widechar(byte(source^))
  63. else
  64. dest^:=' ';
  65. inc(dest);
  66. inc(source);
  67. end;
  68. end;
  69. Const
  70. Wide2AnsiMoveProc:TWide2AnsiMove=@Wide2AnsiMove;
  71. Ansi2WideMoveProc:TAnsi2WideMove=@Ansi2WideMove;
  72. Procedure GetWideStringManager (Var Manager : TWideStringManager);
  73. begin
  74. Manager.Wide2AnsiMove:=Wide2AnsiMoveProc;
  75. Manager.Ansi2WideMove:=Ansi2WideMoveProc;
  76. end;
  77. Procedure SetWideStringManager (Const New : TWideStringManager; Var Old: TWideStringManager);
  78. begin
  79. GetWideStringManager(Old);
  80. SetWideStringManager(New);
  81. end;
  82. Procedure SetWideStringManager (Const New : TWideStringManager);
  83. begin
  84. Wide2AnsiMoveProc:=New.Wide2AnsiMove;
  85. Ansi2WideMoveProc:=New.Ansi2WideMove;
  86. end;
  87. (*
  88. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  89. {
  90. Make sure reference count of S is 1,
  91. using copy-on-write semantics.
  92. }
  93. begin
  94. end;
  95. *)
  96. {****************************************************************************
  97. Internal functions, not in interface.
  98. ****************************************************************************}
  99. {$ifdef WideStrDebug}
  100. Procedure DumpWideRec(S : Pointer);
  101. begin
  102. If S=Nil then
  103. Writeln ('String is nil')
  104. Else
  105. Begin
  106. With PWideRec(S-WideFirstOff)^ do
  107. begin
  108. Write ('(Maxlen: ',maxlen);
  109. Write (' Len:',len);
  110. Writeln (' Ref: ',ref,')');
  111. end;
  112. end;
  113. end;
  114. {$endif}
  115. Function NewWideString(Len : SizeInt) : Pointer;
  116. {
  117. Allocate a new WideString on the heap.
  118. initialize it to zero length and reference count 1.
  119. }
  120. Var
  121. P : Pointer;
  122. begin
  123. GetMem(P,Len*sizeof(WideChar)+WideRecLen);
  124. If P<>Nil then
  125. begin
  126. PWideRec(P)^.Len:=0; { Initial length }
  127. PWideRec(P)^.Ref:=1; { Set reference count }
  128. PWideRec(P)^.First:=#0; { Terminating #0 }
  129. inc(p,WideFirstOff); { Points to string now }
  130. end;
  131. NewWideString:=P;
  132. end;
  133. Procedure DisposeWideString(Var S : Pointer);
  134. {
  135. Deallocates a WideString From the heap.
  136. }
  137. begin
  138. If S=Nil then
  139. exit;
  140. Dec (S,WideFirstOff);
  141. FreeMem (S);
  142. S:=Nil;
  143. end;
  144. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_WIDESTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  145. {
  146. Decreases the ReferenceCount of a non constant widestring;
  147. If the reference count is zero, deallocate the string;
  148. }
  149. Type
  150. pSizeInt = ^SizeInt;
  151. Var
  152. l : pSizeInt;
  153. Begin
  154. { Zero string }
  155. If S=Nil then exit;
  156. { check for constant strings ...}
  157. l:=@PWIDEREC(S-WideFirstOff)^.Ref;
  158. If l^<0 then exit;
  159. { declocked does a MT safe dec and returns true, if the counter is 0 }
  160. If declocked(l^) then
  161. { Ref count dropped to zero }
  162. DisposeWideString (S); { Remove...}
  163. {$ifndef decrrefnotnil}
  164. s:=nil;
  165. {$endif}
  166. end;
  167. {$ifdef hascompilerproc}
  168. { alias for internal use }
  169. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_WIDESTR_DECR_REF'];
  170. {$endif compilerproc}
  171. {$ifdef hascompilerproc}
  172. Procedure fpc_WideStr_Incr_Ref (S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  173. {$else}
  174. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_WIDESTR_INCR_REF'];
  175. {$endif compilerproc}
  176. Begin
  177. If S=Nil then
  178. exit;
  179. { Let's be paranoid : Constant string ??}
  180. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  181. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  182. end;
  183. {$ifdef hascompilerproc}
  184. { alias for internal use }
  185. Procedure fpc_WideStr_Incr_Ref (S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_WIDESTR_INCR_REF'];
  186. {$endif compilerproc}
  187. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  188. {
  189. Converts a WideString to a ShortString;
  190. }
  191. Var
  192. Size : SizeInt;
  193. begin
  194. if S2='' then
  195. fpc_WideStr_To_ShortStr:=''
  196. else
  197. begin
  198. Size:=Length(S2);
  199. If Size>high_of_res then
  200. Size:=high_of_res;
  201. Wide2AnsiMoveProc(PWideChar(S2),PChar(@fpc_WideStr_To_ShortStr[1]),Size);
  202. byte(fpc_WideStr_To_ShortStr[0]):=byte(Size);
  203. end;
  204. end;
  205. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  206. {
  207. Converts a ShortString to a WideString;
  208. }
  209. Var
  210. Size : SizeInt;
  211. begin
  212. Size:=Length(S2);
  213. Setlength (fpc_ShortStr_To_WideStr,Size);
  214. if Size>0 then
  215. begin
  216. Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(Pointer(fpc_ShortStr_To_WideStr)),Size);
  217. { Terminating Zero }
  218. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  219. end;
  220. end;
  221. { old style helper }
  222. {$ifndef hascompilerproc}
  223. Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR'];
  224. begin
  225. s1 := pointer(fpc_ShortStr_To_WideStr(s2));
  226. end;
  227. {$endif hascompilerproc}
  228. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  229. {
  230. Converts a WideString to an AnsiString
  231. }
  232. Var
  233. Size : SizeInt;
  234. begin
  235. if s2='' then
  236. exit;
  237. Size:=Length(WideString(S2));
  238. Setlength (fpc_WideStr_To_AnsiStr,Size);
  239. if Size>0 then
  240. begin
  241. Wide2AnsiMoveProc(PWideChar(Pointer(S2)),PChar(Pointer(fpc_WideStr_To_AnsiStr)),Size);
  242. { Terminating Zero }
  243. PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
  244. end;
  245. end;
  246. { old style helper }
  247. {$ifndef hascompilerproc}
  248. Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;const S2 : WideString);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR'];
  249. begin
  250. s1 := pointer(fpc_WideStr_To_AnsiStr(s2));
  251. end;
  252. {$endif hascompilerproc}
  253. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  254. {
  255. Converts an AnsiString to a WideString;
  256. }
  257. Var
  258. Size : SizeInt;
  259. begin
  260. if s2='' then
  261. exit;
  262. Size:=Length(S2);
  263. Setlength (result,Size);
  264. if Size>0 then
  265. begin
  266. Ansi2WideMoveProc(PChar(S2),PWideChar(Pointer(result)),Size);
  267. { Terminating Zero }
  268. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  269. end;
  270. end;
  271. { compilers with widestrings should have compiler procs }
  272. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  273. var
  274. Size : SizeInt;
  275. begin
  276. if p=nil then
  277. exit;
  278. Size := IndexWord(p^, -1, 0);
  279. Setlength (result,Size);
  280. if Size>0 then
  281. begin
  282. Wide2AnsiMoveProc(P,PChar(Pointer(result)),Size);
  283. { Terminating Zero }
  284. PChar(Pointer(result)+Size)^:=#0;
  285. end;
  286. end;
  287. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  288. var
  289. Size : SizeInt;
  290. begin
  291. if p=nil then
  292. exit;
  293. Size := IndexWord(p^, -1, 0);
  294. Setlength (result,Size);
  295. if Size>0 then
  296. begin
  297. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  298. { Terminating Zero }
  299. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  300. end;
  301. end;
  302. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  303. var
  304. Size : SizeInt;
  305. begin
  306. if p=nil then
  307. begin
  308. fpc_PWideChar_To_ShortStr:='';
  309. exit;
  310. end;
  311. Size := IndexWord(p^, $7fffffff, 0);
  312. Setlength (result,Size+1);
  313. if Size>0 then
  314. begin
  315. If Size>255 then
  316. Size:=255;
  317. Wide2AnsiMoveProc(p,PChar(@result[1]),Size);
  318. byte(result[0]):=byte(Size);
  319. end;
  320. end;
  321. { old style helper }
  322. {$ifndef hascompilerproc}
  323. Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : AnsiString);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR'];
  324. begin
  325. s1 := pointer(fpc_AnsiStr_To_WideStr(s2));
  326. end;
  327. {$endif hascompilerproc}
  328. { checked against the ansistring routine, 2001-05-27 (FK) }
  329. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  330. {
  331. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  332. }
  333. begin
  334. If S2<>nil then
  335. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  336. Inc(PWideRec(S2-WideFirstOff)^.ref);
  337. { Decrease the reference count on the old S1 }
  338. fpc_widestr_decr_ref (S1);
  339. { And finally, have S1 pointing to S2 (or its copy) }
  340. S1:=S2;
  341. end;
  342. {$ifdef hascompilerproc}
  343. { alias for internal use }
  344. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  345. {$endif hascompilerproc}
  346. { checked against the ansistring routine, 2001-05-27 (FK) }
  347. {$ifdef hascompilerproc}
  348. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  349. var
  350. S3: WideString absolute result;
  351. {$else hascompilerproc}
  352. Procedure fpc_WideStr_Concat (S1,S2 : WideString;var S3 : WideString);[Public, alias: 'FPC_WIDESTR_CONCAT'];
  353. {$endif hascompilerproc}
  354. {
  355. Concatenates 2 WideStrings : S1+S2.
  356. Result Goes to S3;
  357. }
  358. Var
  359. Size,Location : SizeInt;
  360. begin
  361. { only assign if s1 or s2 is empty }
  362. if (S1='') then
  363. S3 := S2
  364. else
  365. if (S2='') then
  366. S3 := S1
  367. else
  368. begin
  369. { create new result }
  370. Size:=Length(S2);
  371. Location:=Length(S1);
  372. SetLength (S3,Size+Location);
  373. Move (S1[1],S3[1],Location*sizeof(WideChar));
  374. Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
  375. end;
  376. end;
  377. Function fpc_Char_To_WideStr(const c : WideChar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  378. {
  379. Converts a Char to a WideString;
  380. }
  381. begin
  382. if c = #0 then
  383. { result is automatically set to '' }
  384. exit;
  385. Setlength (fpc_Char_To_WideStr,1);
  386. fpc_Char_To_WideStr[1]:=c;
  387. { Terminating Zero }
  388. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  389. end;
  390. { old style helper }
  391. {$ifndef hascompilerproc}
  392. Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : WideChar);[Public, alias: 'FPC_CHAR_TO_WIDESTR'];
  393. begin
  394. s1 := pointer(fpc_Char_To_WideStr(c));
  395. end;
  396. {$endif hascompilerproc}
  397. Function fpc_PChar_To_WideStr(const p : pchar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  398. Var
  399. L : SizeInt;
  400. begin
  401. if (not assigned(p)) or (p[0]=#0) Then
  402. { result is automatically set to '' }
  403. exit;
  404. l:=IndexChar(p^,-1,#0);
  405. SetLength(fpc_PChar_To_WideStr,L);
  406. Ansi2WideMoveProc(P,PWideChar(Pointer(fpc_PChar_To_WideStr)),l);
  407. end;
  408. { old style helper }
  409. {$ifndef hascompilerproc}
  410. Procedure fpc_PChar_To_WideStr(var a : WideString;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  411. begin
  412. pointer(a) := pointer(fpc_PChar_To_WideStr(p));
  413. end;
  414. {$endif hascompilerproc}
  415. Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  416. var
  417. i : SizeInt;
  418. begin
  419. if arr[0]=#0 Then
  420. { result is automatically set to '' }
  421. exit;
  422. i:=IndexChar(arr,high(arr)+1,#0);
  423. if i = -1 then
  424. i := high(arr)+1;
  425. SetLength(fpc_CharArray_To_WideStr,i);
  426. Ansi2WideMoveProc (pchar(@arr),PWideChar(Pointer(fpc_CharArray_To_WideStr)),i);
  427. end;
  428. { old style helper }
  429. {$ifndef hascompilerproc}
  430. Procedure fpc_CharArray_To_WideStr(var a : WideString; p: pointer; len: SizeInt); [Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  431. var
  432. src: pchar;
  433. i: SizeInt;
  434. begin
  435. src := pchar(p);
  436. if src[0]=#0 Then
  437. begin
  438. pointer(a) := nil;
  439. exit;
  440. end;
  441. i:=IndexChar(src^,len,#0);
  442. if i = -1 then
  443. i := len;
  444. pointer(a) := NewWideString(i);
  445. Ansi2WideMoveProc (src,PWideChar(Pointer(@a[1])),i);
  446. end;
  447. {$endif not hascompilerproc}
  448. {$ifdef hascompilerproc}
  449. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  450. var
  451. l: longint;
  452. {$else hascompilerproc}
  453. function fpc_WideCharArray_To_ShortStr(arr:pwidechar; l : longint):shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR'];
  454. var
  455. {$endif hascompilerproc}
  456. index: longint;
  457. len: byte;
  458. begin
  459. {$ifdef hascompilerproc}
  460. l := high(arr)+1;
  461. {$endif hascompilerproc}
  462. if l>=256 then
  463. l:=255
  464. else if l<0 then
  465. l:=0;
  466. index:=IndexWord(arr[0],l,0);
  467. if (index < 0) then
  468. len := l
  469. else
  470. len := index;
  471. {$ifdef hascompilerproc}
  472. Wide2AnsiMoveProc (pwidechar(@arr),PAnsiChar(@(fpc_WideCharArray_To_ShortStr[1])),len);
  473. {$else}
  474. Wide2AnsiMoveProc (arr, PAnsiChar(@(fpc_WideCharArray_To_ShortStr[1])),len);
  475. {$endif}
  476. fpc_WideCharArray_To_ShortStr[0]:=chr(len);
  477. end;
  478. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  479. var
  480. i : SizeInt;
  481. begin
  482. if arr[0]=#0 Then
  483. { result is automatically set to '' }
  484. exit;
  485. i:=IndexWord(arr,high(arr)+1,0);
  486. if i = -1 then
  487. i := high(arr)+1;
  488. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  489. Wide2AnsiMoveProc (pwidechar(@arr),PAnsiChar(Pointer(fpc_WideCharArray_To_AnsiStr)),i);
  490. end;
  491. { old style helper }
  492. {$ifndef hascompilerproc}
  493. Procedure fpc_WideCharArray_To_AnsiStr(var a : AnsiString; p: pointer; len: SizeInt); [Public,Alias : 'FPC_WIDECHARARRAY_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  494. var
  495. src: pwidechar;
  496. i: SizeInt;
  497. begin
  498. src := pwidechar(p);
  499. if src[0]=#0 Then
  500. begin
  501. pointer(a) := nil;
  502. exit;
  503. end;
  504. i:=IndexWord(src^,len,0);
  505. if i = -1 then
  506. i := len;
  507. pointer(a) := NewAnsiString(i);
  508. Wide2AnsiMoveProc (src,PAnsiChar(Pointer(@a[1])),i);
  509. end;
  510. {$endif not hascompilerproc}
  511. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  512. var
  513. i : SizeInt;
  514. begin
  515. if arr[0]=#0 Then
  516. { result is automatically set to '' }
  517. exit;
  518. i:=IndexWord(arr,high(arr)+1,0);
  519. if i = -1 then
  520. i := high(arr)+1;
  521. SetLength(fpc_WideCharArray_To_WideStr,i);
  522. Move(pwidechar(@arr)^, PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1]))^,i*sizeof(WideChar));
  523. { Terminating Zero }
  524. PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1])+i*sizeof(WideChar))^:=#0;
  525. end;
  526. { old style helper }
  527. {$ifndef hascompilerproc}
  528. Procedure fpc_WideCharArray_To_WideStr(var a : WideString; p: pointer; len: SizeInt); [Public,Alias : 'FPC_WIDECHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  529. var
  530. src: pwidechar;
  531. i: SizeInt;
  532. begin
  533. src := pwidechar(p);
  534. if src[0]=#0 Then
  535. begin
  536. pointer(a) := nil;
  537. exit;
  538. end;
  539. i:=IndexWord(src^,len,#0);
  540. if i = -1 then
  541. i := len;
  542. pointer(a) := NewWideString(i);
  543. Move(p^, PWideChar(Pointer(@a[1]))^,i*sizeof(WideChar));
  544. { Terminating Zero }
  545. PWideChar(Pointer(@a[1])+i*sizeof(WideChar))^:=#0;
  546. end;
  547. {$endif not hascompilerproc}
  548. {$ifdef hascompilerproc}
  549. { inside the compiler, the resulttype is modified to that of the actual }
  550. { chararray we're converting to (JM) }
  551. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  552. var
  553. len: SizeInt;
  554. begin
  555. len := length(src);
  556. if len > arraysize then
  557. len := arraysize;
  558. { make sure we don't dereference src if it can be nil (JM) }
  559. if len > 0 then
  560. wide2ansimoveproc(pwidechar(@src[1]),pchar(@fpc_widestr_to_chararray[0]),len);
  561. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  562. end;
  563. {$endif hascompilerproc}
  564. {$ifdef hascompilerproc}
  565. { inside the compiler, the resulttype is modified to that of the actual }
  566. { widechararray we're converting to (JM) }
  567. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  568. var
  569. len: SizeInt;
  570. begin
  571. len := length(src);
  572. if len > arraysize then
  573. len := arraysize;
  574. { make sure we don't try to access element 1 of the ansistring if it's nil }
  575. if len > 0 then
  576. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  577. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  578. end;
  579. {$endif hascompilerproc}
  580. {$ifdef hascompilerproc}
  581. { inside the compiler, the resulttype is modified to that of the actual }
  582. { chararray we're converting to (JM) }
  583. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  584. var
  585. len: SizeInt;
  586. begin
  587. len := length(src);
  588. if len > arraysize then
  589. len := arraysize;
  590. { make sure we don't dereference src if it can be nil (JM) }
  591. if len > 0 then
  592. ansi2widemoveproc(pchar(@src[1]),pwidechar(@fpc_ansistr_to_widechararray[0]),len);
  593. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  594. end;
  595. {$endif hascompilerproc}
  596. {$ifdef hascompilerproc}
  597. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  598. var
  599. len: longint;
  600. begin
  601. len := length(src);
  602. if len > arraysize then
  603. len := arraysize;
  604. { make sure we don't access char 1 if length is 0 (JM) }
  605. if len > 0 then
  606. ansi2widemoveproc(pchar(@src[1]),pwidechar(@fpc_shortstr_to_widechararray[0]),len);
  607. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  608. end;
  609. {$endif hascompilerproc}
  610. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  611. {
  612. Compares 2 WideStrings;
  613. The result is
  614. <0 if S1<S2
  615. 0 if S1=S2
  616. >0 if S1>S2
  617. }
  618. Var
  619. MaxI,Temp : SizeInt;
  620. begin
  621. if pointer(S1)=pointer(S2) then
  622. begin
  623. fpc_WideStr_Compare:=0;
  624. exit;
  625. end;
  626. Maxi:=Length(S1);
  627. temp:=Length(S2);
  628. If MaxI>Temp then
  629. MaxI:=Temp;
  630. Temp:=CompareWord(S1[1],S2[1],MaxI);
  631. if temp=0 then
  632. temp:=Length(S1)-Length(S2);
  633. fpc_WideStr_Compare:=Temp;
  634. end;
  635. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  636. begin
  637. if p=nil then
  638. HandleErrorFrame(201,get_frame);
  639. end;
  640. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  641. begin
  642. if (index>len) or (Index<1) then
  643. HandleErrorFrame(201,get_frame);
  644. end;
  645. {$ifndef INTERNSETLENGTH}
  646. Procedure SetLength (Var S : WideString; l : SizeInt);
  647. {$else INTERNSETLENGTH}
  648. Procedure fpc_WideStr_SetLength (Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  649. {$endif INTERNSETLENGTH}
  650. {
  651. Sets The length of string S to L.
  652. Makes sure S is unique, and contains enough room.
  653. }
  654. Var
  655. Temp : Pointer;
  656. movelen: SizeInt;
  657. begin
  658. if (l>0) then
  659. begin
  660. if Pointer(S)=nil then
  661. begin
  662. { Need a complete new string...}
  663. Pointer(s):=NewWideString(l);
  664. end
  665. else if (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1) then
  666. begin
  667. Dec(Pointer(S),WideFirstOff);
  668. if L*sizeof(WideChar)+WideRecLen>MemSize(Pointer(S)) then
  669. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  670. Inc(Pointer(S), WideFirstOff);
  671. end
  672. else
  673. begin
  674. { Reallocation is needed... }
  675. Temp:=Pointer(NewWideString(L));
  676. if Length(S)>0 then
  677. begin
  678. if l < succ(length(s)) then
  679. movelen := l
  680. { also move terminating null }
  681. else movelen := succ(length(s));
  682. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  683. end;
  684. fpc_widestr_decr_ref(Pointer(S));
  685. Pointer(S):=Temp;
  686. end;
  687. { Force nil termination in case it gets shorter }
  688. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  689. PWideRec(Pointer(S)-FirstOff)^.Len:=l;
  690. end
  691. else
  692. begin
  693. { Length=0 }
  694. if Pointer(S)<>nil then
  695. fpc_widestr_decr_ref (Pointer(S));
  696. Pointer(S):=Nil;
  697. end;
  698. end;
  699. {*****************************************************************************
  700. Public functions, In interface.
  701. *****************************************************************************}
  702. function WideCharToString(S : PWideChar) : AnsiString;
  703. begin
  704. result:=WideCharLenToString(s,Length(WideString(s)));
  705. end;
  706. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  707. begin
  708. if Length(Src)<DestSize then
  709. Ansi2WideMoveProc(PChar(Src),Dest,Length(Src))
  710. else
  711. Ansi2WideMoveProc(PChar(Src),Dest,DestSize);
  712. result:=Dest;
  713. end;
  714. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  715. begin
  716. SetLength(result,Len);
  717. Wide2AnsiMove(S,PChar(result),Len);
  718. end;
  719. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;var Dest : AnsiString);
  720. begin
  721. Dest:=WideCharLenToString(Src,Len);
  722. end;
  723. procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
  724. begin
  725. Dest:=WideCharToString(S);
  726. end;
  727. {$ifndef INTERNLENGTH}
  728. Function Length (Const S : WideString) : SizeInt;
  729. {
  730. Returns the length of an WideString.
  731. Takes in acount that zero strings are NIL;
  732. }
  733. begin
  734. If Pointer(S)=Nil then
  735. Length:=0
  736. else
  737. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  738. end;
  739. {$endif INTERNLENGTH}
  740. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  741. {
  742. Make sure reference count of S is 1,
  743. using copy-on-write semantics.
  744. }
  745. Var
  746. SNew : Pointer;
  747. L : SizeInt;
  748. begin
  749. pointer(result) := pointer(s);
  750. If Pointer(S)=Nil then
  751. exit;
  752. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  753. begin
  754. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  755. SNew:=NewWideString (L);
  756. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  757. PWideRec(SNew-WideFirstOff)^.len:=L;
  758. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  759. pointer(S):=SNew;
  760. pointer(result):=SNew;
  761. end;
  762. end;
  763. {$ifdef interncopy}
  764. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  765. {$else}
  766. Function Copy (Const S : WideString; Index,Size : SizeInt) : WideString;
  767. {$endif}
  768. var
  769. ResultAddress : Pointer;
  770. begin
  771. ResultAddress:=Nil;
  772. dec(index);
  773. if Index < 0 then
  774. Index := 0;
  775. { Check Size. Accounts for Zero-length S, the double check is needed because
  776. Size can be maxint and will get <0 when adding index }
  777. if (Size>Length(S)) or
  778. (Index+Size>Length(S)) then
  779. Size:=Length(S)-Index;
  780. If Size>0 then
  781. begin
  782. If Index<0 Then
  783. Index:=0;
  784. ResultAddress:=Pointer(NewWideString (Size));
  785. if ResultAddress<>Nil then
  786. begin
  787. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  788. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  789. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  790. end;
  791. end;
  792. {$ifdef interncopy}
  793. Pointer(fpc_widestr_Copy):=ResultAddress;
  794. {$else}
  795. Pointer(Copy):=ResultAddress;
  796. {$endif}
  797. end;
  798. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  799. var
  800. i,MaxLen : SizeInt;
  801. pc : pwidechar;
  802. begin
  803. Pos:=0;
  804. if Length(SubStr)>0 then
  805. begin
  806. MaxLen:=Length(source)-Length(SubStr);
  807. i:=0;
  808. pc:=@source[1];
  809. while (i<=MaxLen) do
  810. begin
  811. inc(i);
  812. if (SubStr[1]=pc^) and
  813. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  814. begin
  815. Pos:=i;
  816. exit;
  817. end;
  818. inc(pc);
  819. end;
  820. end;
  821. end;
  822. { Faster version for a widechar alone }
  823. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  824. var
  825. i: SizeInt;
  826. pc : pwidechar;
  827. begin
  828. pc:=@s[1];
  829. for i:=1 to length(s) do
  830. begin
  831. if pc^=c then
  832. begin
  833. pos:=i;
  834. exit;
  835. end;
  836. inc(pc);
  837. end;
  838. pos:=0;
  839. end;
  840. { Faster version for a char alone. Must be implemented because }
  841. { pos(c: char; const s: shortstring) also exists, so otherwise }
  842. { using pos(char,pchar) will always call the shortstring version }
  843. { (exact match for first argument), also with $h+ (JM) }
  844. Function Pos (c : Char; Const s : WideString) : SizeInt;
  845. var
  846. i: SizeInt;
  847. wc : widechar;
  848. pc : pwidechar;
  849. begin
  850. wc:=c;
  851. pc:=@s[1];
  852. for i:=1 to length(s) do
  853. begin
  854. if pc^=wc then
  855. begin
  856. pos:=i;
  857. exit;
  858. end;
  859. inc(pc);
  860. end;
  861. pos:=0;
  862. end;
  863. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  864. Var
  865. LS : SizeInt;
  866. begin
  867. If Length(S)=0 then
  868. exit;
  869. if index<=0 then
  870. exit;
  871. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  872. if (Index<=LS) and (Size>0) then
  873. begin
  874. UniqueString (S);
  875. if Size+Index>LS then
  876. Size:=LS-Index+1;
  877. if Index+Size<=LS then
  878. begin
  879. Dec(Index);
  880. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  881. end;
  882. Setlength(s,LS-Size);
  883. end;
  884. end;
  885. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  886. var
  887. Temp : WideString;
  888. LS : SizeInt;
  889. begin
  890. If Length(Source)=0 then
  891. exit;
  892. if index <= 0 then
  893. index := 1;
  894. Ls:=Length(S);
  895. if index > LS then
  896. index := LS+1;
  897. Dec(Index);
  898. Pointer(Temp) := NewWideString(Length(Source)+LS);
  899. SetLength(Temp,Length(Source)+LS);
  900. If Index>0 then
  901. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  902. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  903. If (LS-Index)>0 then
  904. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  905. S:=Temp;
  906. end;
  907. Procedure SetString (Var S : WideString; Buf : PWideChar; Len : SizeInt);
  908. var
  909. BufLen: SizeInt;
  910. begin
  911. SetLength(S,Len);
  912. If (Buf<>Nil) and (Len>0) then
  913. begin
  914. BufLen := IndexWord(Buf^, Len+1, 0);
  915. If (BufLen>0) and (BufLen < Len) then
  916. Len := BufLen;
  917. Move (Buf[0],S[1],Len*sizeof(WideChar));
  918. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  919. end;
  920. end;
  921. Procedure SetString (Var S : WideString; Buf : PChar; Len : SizeInt);
  922. var
  923. BufLen: SizeInt;
  924. begin
  925. SetLength(S,Len);
  926. If (Buf<>Nil) and (Len>0) then
  927. begin
  928. BufLen := IndexByte(Buf^, Len+1, 0);
  929. If (BufLen>0) and (BufLen < Len) then
  930. Len := BufLen;
  931. Ansi2WideMoveProc(Buf,PWideChar(S),Len);
  932. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  933. end;
  934. end;
  935. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  936. Var
  937. SS : String;
  938. begin
  939. fpc_Val_Real_WideStr := 0;
  940. if length(S) > 255 then
  941. code := 256
  942. else
  943. begin
  944. SS := S;
  945. Val(SS,fpc_Val_Real_WideStr,code);
  946. end;
  947. end;
  948. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  949. Var
  950. SS : ShortString;
  951. begin
  952. fpc_Val_UInt_WideStr := 0;
  953. if length(S) > 255 then
  954. code := 256
  955. else
  956. begin
  957. SS := S;
  958. Val(SS,fpc_Val_UInt_WideStr,code);
  959. end;
  960. end;
  961. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  962. Var
  963. SS : ShortString;
  964. begin
  965. fpc_Val_SInt_WideStr:=0;
  966. if length(S)>255 then
  967. code:=256
  968. else
  969. begin
  970. SS := S;
  971. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  972. end;
  973. end;
  974. {$ifndef CPU64}
  975. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  976. Var
  977. SS : ShortString;
  978. begin
  979. fpc_Val_qword_WideStr:=0;
  980. if length(S)>255 then
  981. code:=256
  982. else
  983. begin
  984. SS := S;
  985. Val(SS,fpc_Val_qword_WideStr,Code);
  986. end;
  987. end;
  988. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  989. Var
  990. SS : ShortString;
  991. begin
  992. fpc_Val_int64_WideStr:=0;
  993. if length(S)>255 then
  994. code:=256
  995. else
  996. begin
  997. SS := S;
  998. Val(SS,fpc_Val_int64_WideStr,Code);
  999. end;
  1000. end;
  1001. {$endif CPU64}
  1002. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;var s : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1003. var
  1004. ss : shortstring;
  1005. begin
  1006. str_real(len,fr,d,treal_type(rt),ss);
  1007. s:=ss;
  1008. end;
  1009. {$ifdef STR_USES_VALINT}
  1010. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1011. {$else}
  1012. Procedure fpc_WideStr_Longint(v : Longint; Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1013. {$endif}
  1014. Var
  1015. SS : ShortString;
  1016. begin
  1017. Str (v:Len,SS);
  1018. S:=SS;
  1019. end;
  1020. {$ifdef STR_USES_VALINT}
  1021. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1022. {$else}
  1023. Procedure fpc_WideStr_Longword(v : Longword;Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1024. {$endif}
  1025. Var
  1026. SS : ShortString;
  1027. begin
  1028. str(v:Len,SS);
  1029. S:=SS;
  1030. end;
  1031. {$ifndef CPU64}
  1032. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1033. Var
  1034. SS : ShortString;
  1035. begin
  1036. Str (v:Len,SS);
  1037. S:=SS;
  1038. end;
  1039. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
  1040. Var
  1041. SS : ShortString;
  1042. begin
  1043. str(v:Len,SS);
  1044. S:=SS;
  1045. end;
  1046. {$endif CPU64}
  1047. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1048. begin
  1049. if assigned(Source) then
  1050. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1051. else
  1052. Result:=0;
  1053. end;
  1054. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1055. var
  1056. i,j : SizeUInt;
  1057. w : word;
  1058. begin
  1059. result:=0;
  1060. if source=nil then
  1061. exit;
  1062. i:=0;
  1063. j:=0;
  1064. if assigned(Dest) then
  1065. begin
  1066. while (i<SourceChars) and (j<MaxDestBytes) do
  1067. begin
  1068. w:=word(Source[i]);
  1069. case w of
  1070. 0..$7f:
  1071. begin
  1072. Dest[j]:=char(w);
  1073. inc(j);
  1074. end;
  1075. $80..$7ff:
  1076. begin
  1077. if j+1>=MaxDestBytes then
  1078. break;
  1079. Dest[j]:=char($c0 or (w shr 6));
  1080. Dest[j+1]:=char($80 or (w and $3f));
  1081. inc(j,2);
  1082. end;
  1083. else
  1084. begin
  1085. if j+2>=MaxDestBytes then
  1086. break;
  1087. Dest[j]:=char($e0 or (w shr 12));
  1088. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1089. Dest[j+2]:=char($80 or (w and $3f));
  1090. inc(j,3);
  1091. end;
  1092. end;
  1093. inc(i);
  1094. end;
  1095. if j>MaxDestBytes-1 then
  1096. j:=MaxDestBytes-1;
  1097. Dest[j]:=#0;
  1098. end
  1099. else
  1100. begin
  1101. while i<SourceChars do
  1102. begin
  1103. case word(Source[i]) of
  1104. $0..$7f:
  1105. inc(j);
  1106. $80..$7ff:
  1107. inc(j,2);
  1108. else
  1109. inc(j,3);
  1110. end;
  1111. end;
  1112. end;
  1113. result:=j+1;
  1114. end;
  1115. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1116. begin
  1117. if assigned(Source) then
  1118. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1119. else
  1120. Result:=0;
  1121. end;
  1122. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1123. var
  1124. i,j : SizeUInt;
  1125. w : word;
  1126. b : byte;
  1127. begin
  1128. result:=0;
  1129. i:=0;
  1130. j:=0;
  1131. if assigned(Dest) then
  1132. begin
  1133. while (j<MaxDestChars) and (i<SourceBytes) do
  1134. begin
  1135. b:=byte(Source[i]);
  1136. inc(i);
  1137. // 2 or 3 bytes?
  1138. if b>=$80 then
  1139. begin
  1140. w:=b and $3c;
  1141. if i>=SourceBytes then
  1142. exit;
  1143. // 3 bytes?
  1144. if (b and $20)<>0 then
  1145. begin
  1146. b:=byte(Source[i]);
  1147. inc(i);
  1148. if i>=SourceBytes then
  1149. exit;
  1150. if (b and $c0)<>$80 then
  1151. exit;
  1152. w:=(w shl 6) or (b and $3c);
  1153. end;
  1154. b:=byte(Source[i]);
  1155. w:=(w shl 6) or (b and $3c);
  1156. if (b and $c0)<>$80 then
  1157. exit;
  1158. inc(i);
  1159. end;
  1160. Dest[j]:=WideChar(w);
  1161. inc(j);
  1162. end;
  1163. end
  1164. else
  1165. begin
  1166. while i<SourceBytes do
  1167. begin
  1168. b:=byte(Source[i]);
  1169. inc(i);
  1170. // 2 or 3 bytes?
  1171. if b>=$80 then
  1172. begin
  1173. if i>=SourceBytes then
  1174. exit;
  1175. // 3 bytes?
  1176. if (b and $20)<>0 then
  1177. begin
  1178. b:=byte(Source[i]);
  1179. inc(i);
  1180. if i>=SourceBytes then
  1181. exit;
  1182. if (b and $c0)<>$80 then
  1183. exit;
  1184. end;
  1185. if (byte(Source[i]) and $c0)<>$80 then
  1186. exit;
  1187. inc(i);
  1188. end;
  1189. inc(j);
  1190. end;
  1191. end;
  1192. result:=j+1;
  1193. end;
  1194. function UTF8Encode(const s : WideString) : UTF8String;
  1195. var
  1196. i : SizeInt;
  1197. hs : UTF8String;
  1198. begin
  1199. result:='';
  1200. if s='' then
  1201. exit;
  1202. SetLength(hs,length(s)*3);
  1203. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1204. if i>0 then
  1205. begin
  1206. SetLength(hs,i-1);
  1207. result:=hs;
  1208. end;
  1209. end;
  1210. function UTF8Decode(const s : UTF8String): WideString;
  1211. var
  1212. i : SizeInt;
  1213. hs : WideString;
  1214. begin
  1215. result:='';
  1216. if s='' then
  1217. exit;
  1218. SetLength(hs,length(s));
  1219. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1220. if i>0 then
  1221. begin
  1222. SetLength(hs,i-1);
  1223. result:=hs;
  1224. end;
  1225. end;
  1226. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1227. begin
  1228. Result:=Utf8Encode(s);
  1229. end;
  1230. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1231. begin
  1232. Result:=Utf8Decode(s);
  1233. end;
  1234. {
  1235. $Log$
  1236. Revision 1.47 2005-01-06 13:31:06 florian
  1237. * widecharray patch from Peter
  1238. Revision 1.46 2004/11/17 22:19:04 peter
  1239. internconst, internproc and some external declarations moved to interface
  1240. Revision 1.45 2004/10/24 20:01:42 peter
  1241. * saveregisters calling convention is obsolete
  1242. Revision 1.44 2004/09/29 14:55:49 mazen
  1243. * widestring conversion routines takes a widechar so no
  1244. information is lost
  1245. Revision 1.43 2004/09/18 10:23:08 jonas
  1246. - removed unused local variable
  1247. * fixed bug in Utf8ToUnicode (use of uninitialised variable)
  1248. Revision 1.42 2004/07/18 20:21:44 florian
  1249. + several unicode (to/from utf-8 conversion) stuff added
  1250. * some longint -> SizeInt changes
  1251. Revision 1.41 2004/07/12 17:58:19 peter
  1252. * remove maxlen field from ansistring/widestrings
  1253. Revision 1.40 2004/07/02 21:21:09 peter
  1254. * decr ref doesn't reset pointer
  1255. * finalize resets pointer for astring,wstring
  1256. Revision 1.39 2004/05/31 14:31:57 peter
  1257. * remove comment warnings
  1258. Revision 1.38 2004/05/29 15:39:08 florian
  1259. * the decr functions set the data now to nil
  1260. Revision 1.37 2004/05/01 23:55:18 peter
  1261. * replace strlenint with sizeint
  1262. Revision 1.36 2004/04/29 18:59:43 peter
  1263. * str() helpers now also use valint/valuint
  1264. * int64/qword helpers disabled for cpu64
  1265. Revision 1.35 2004/01/22 22:09:05 peter
  1266. * finalize needs to reset to nil after decr_ref
  1267. Revision 1.34 2003/11/29 17:27:05 michael
  1268. + Added overloaded version of SetWideStringManager without old parameter
  1269. Revision 1.33 2003/11/28 20:36:13 michael
  1270. + Added WideStringManager
  1271. Revision 1.32 2003/11/05 15:33:51 florian
  1272. * made Index* usage consistent with astrings.inc
  1273. Revision 1.31 2003/06/17 19:24:08 jonas
  1274. * fixed conversion of fpc_*str_unique to compilerproc
  1275. Revision 1.30 2003/06/17 16:38:53 jonas
  1276. * fpc_ansistr|widestr_unique is now a function so it can be used as
  1277. compilerproc
  1278. Revision 1.29 2003/05/01 08:05:23 florian
  1279. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  1280. Revision 1.28 2002/12/29 16:59:17 peter
  1281. * implemented some more conversions
  1282. Revision 1.27 2002/12/15 22:33:12 peter
  1283. * SetString(WideString,[PChar|PWideChar],Len) added
  1284. Revision 1.26 2002/12/14 19:16:45 sg
  1285. * Ported improvements from the AnsiString equivalents to NewWideString and
  1286. fpc_WideStr_SetLength
  1287. Revision 1.25 2002/12/07 14:35:34 carl
  1288. - avoid warnings (add typecast)
  1289. Revision 1.24 2002/10/10 16:08:50 florian
  1290. + several widestring/pwidechar related helpers added
  1291. Revision 1.23 2002/10/02 18:21:52 peter
  1292. * Copy() changed to internal function calling compilerprocs
  1293. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  1294. new copy functions
  1295. Revision 1.22 2002/09/26 21:50:38 florian
  1296. + some WideString<->AnsiString conversion functions added
  1297. Revision 1.21 2002/09/14 11:20:50 carl
  1298. * Delphi compatibility fix (with string routines)
  1299. Revision 1.20 2002/09/07 21:16:45 carl
  1300. * cardinal -> longword
  1301. Revision 1.19 2002/09/07 15:07:46 peter
  1302. * old logs removed and tabs fixed
  1303. Revision 1.18 2002/07/29 21:28:17 florian
  1304. * several fixes to get further with linux/ppc system unit compilation
  1305. Revision 1.17 2002/04/26 15:19:05 peter
  1306. * use saveregisters for incr routines, saves also problems with
  1307. the optimizer
  1308. Revision 1.16 2002/04/25 20:14:57 peter
  1309. * updated compilerprocs
  1310. * incr ref count has now a value argument instead of var
  1311. }