wstrings.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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 WideStrings/Unicode 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. {
  13. This file contains the implementation of the WideString type,
  14. and all things that are needed for it.
  15. WideString is defined as a 'silent' pwidechar :
  16. a pwidechar that points to :
  17. @-8 : SizeInt for reference count;
  18. @-4 : SizeInt for size;
  19. @ : String + Terminating #0;
  20. Pwidechar(Widestring) is a valid typecast.
  21. So WS[i] is converted to the address @WS+i-1.
  22. Constants should be assigned a reference count of -1
  23. Meaning that they can't be disposed of.
  24. }
  25. Type
  26. PWideRec = ^TWideRec;
  27. TWideRec = Packed Record
  28. Ref,
  29. Len : SizeInt;
  30. First : WideChar;
  31. end;
  32. Const
  33. WideRecLen = SizeOf(TWideRec);
  34. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  35. {
  36. Default WideChar <-> Char conversion is to only convert the
  37. lower 127 chars, all others are translated to spaces.
  38. These routines can be overwritten for the Current Locale
  39. }
  40. procedure DefaultWide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  41. var
  42. i : SizeInt;
  43. begin
  44. //writeln('in widetoansimove');
  45. setlength(dest,len);
  46. for i:=1 to len do
  47. begin
  48. if word(source^)<256 then
  49. dest[i]:=char(word(source^))
  50. else
  51. dest[i]:='?';
  52. //inc(dest);
  53. inc(source);
  54. end;
  55. end;
  56. procedure DefaultAnsi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  57. var
  58. i : SizeInt;
  59. begin
  60. //writeln('in ansitowidemove');
  61. setlength(dest,len);
  62. for i:=1 to len do
  63. begin
  64. // if byte(source^)<128 then
  65. dest[i]:=widechar(byte(source^));
  66. // else
  67. // dest^:=' ';
  68. //inc(dest);
  69. inc(source);
  70. end;
  71. end;
  72. Procedure GetWideStringManager (Var Manager : TWideStringManager);
  73. begin
  74. manager:=widestringmanager;
  75. end;
  76. Procedure SetWideStringManager (Const New : TWideStringManager; Var Old: TWideStringManager);
  77. begin
  78. Old:=widestringmanager;
  79. widestringmanager:=New;
  80. end;
  81. Procedure SetWideStringManager (Const New : TWideStringManager);
  82. begin
  83. widestringmanager:=New;
  84. end;
  85. (*
  86. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  87. {
  88. Make sure reference count of S is 1,
  89. using copy-on-write semantics.
  90. }
  91. begin
  92. end;
  93. *)
  94. {****************************************************************************
  95. Internal functions, not in interface.
  96. ****************************************************************************}
  97. {$ifdef WideStrDebug}
  98. Procedure DumpWideRec(S : Pointer);
  99. begin
  100. If S=Nil then
  101. Writeln ('String is nil')
  102. Else
  103. Begin
  104. With PWideRec(S-WideFirstOff)^ do
  105. begin
  106. Write ('(Maxlen: ',maxlen);
  107. Write (' Len:',len);
  108. Writeln (' Ref: ',ref,')');
  109. end;
  110. end;
  111. end;
  112. {$endif}
  113. Function NewWideString(Len : SizeInt) : Pointer;
  114. {
  115. Allocate a new WideString on the heap.
  116. initialize it to zero length and reference count 1.
  117. }
  118. Var
  119. P : Pointer;
  120. begin
  121. GetMem(P,Len*sizeof(WideChar)+WideRecLen);
  122. If P<>Nil then
  123. begin
  124. PWideRec(P)^.Len:=0; { Initial length }
  125. PWideRec(P)^.Ref:=1; { Set reference count }
  126. PWideRec(P)^.First:=#0; { Terminating #0 }
  127. inc(p,WideFirstOff); { Points to string now }
  128. end;
  129. NewWideString:=P;
  130. end;
  131. Procedure DisposeWideString(Var S : Pointer);
  132. {
  133. Deallocates a WideString From the heap.
  134. }
  135. begin
  136. If S=Nil then
  137. exit;
  138. Dec (S,WideFirstOff);
  139. FreeMem (S);
  140. S:=Nil;
  141. end;
  142. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_WIDESTR_DECR_REF']; compilerproc;
  143. {
  144. Decreases the ReferenceCount of a non constant widestring;
  145. If the reference count is zero, deallocate the string;
  146. }
  147. Type
  148. pSizeInt = ^SizeInt;
  149. Var
  150. l : pSizeInt;
  151. Begin
  152. { Zero string }
  153. If S=Nil then exit;
  154. { check for constant strings ...}
  155. l:=@PWIDEREC(S-WideFirstOff)^.Ref;
  156. If l^<0 then exit;
  157. { declocked does a MT safe dec and returns true, if the counter is 0 }
  158. If declocked(l^) then
  159. { Ref count dropped to zero }
  160. DisposeWideString (S); { Remove...}
  161. end;
  162. { alias for internal use }
  163. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_WIDESTR_DECR_REF'];
  164. Procedure fpc_WideStr_Incr_Ref (S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[Public,Alias:'FPC_WIDESTR_INCR_REF']; compilerproc;
  165. Begin
  166. If S=Nil then
  167. exit;
  168. { Let's be paranoid : Constant string ??}
  169. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  170. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  171. end;
  172. { alias for internal use }
  173. Procedure fpc_WideStr_Incr_Ref (S : Pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[external name 'FPC_WIDESTR_INCR_REF'];
  174. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; compilerproc;
  175. {
  176. Converts a WideString to a ShortString;
  177. }
  178. Var
  179. Size : SizeInt;
  180. temp : ansistring;
  181. begin
  182. if S2='' then
  183. fpc_WideStr_To_ShortStr:=''
  184. else
  185. begin
  186. Size:=Length(S2);
  187. If Size>high_of_res then
  188. Size:=high_of_res;
  189. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  190. fpc_WideStr_To_ShortStr:=temp;
  191. end;
  192. end;
  193. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  194. {
  195. Converts a ShortString to a WideString;
  196. }
  197. Var
  198. Size : SizeInt;
  199. begin
  200. Size:=Length(S2);
  201. //Setlength (fpc_ShortStr_To_WideStr,Size);
  202. if Size>0 then
  203. begin
  204. widestringmanager.Ansi2WideMoveProc(PChar(@S2[1]),fpc_ShortStr_To_WideStr,Size);
  205. { Terminating Zero }
  206. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  207. end;
  208. end;
  209. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; compilerproc;
  210. {
  211. Converts a WideString to an AnsiString
  212. }
  213. Var
  214. Size : SizeInt;
  215. begin
  216. if s2='' then
  217. exit;
  218. Size:=Length(WideString(S2));
  219. // Setlength (fpc_WideStr_To_AnsiStr,Size);
  220. if Size>0 then
  221. begin
  222. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),fpc_WideStr_To_AnsiStr,Size);
  223. { Terminating Zero }
  224. // PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
  225. end;
  226. end;
  227. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; compilerproc;
  228. {
  229. Converts an AnsiString to a WideString;
  230. }
  231. Var
  232. Size : SizeInt;
  233. begin
  234. if s2='' then
  235. exit;
  236. Size:=Length(S2);
  237. // Setlength (result,Size);
  238. if Size>0 then
  239. begin
  240. widestringmanager.Ansi2WideMoveProc(PChar(S2),result,Size);
  241. { Terminating Zero }
  242. // PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  243. end;
  244. end;
  245. { compilers with widestrings should have compiler procs }
  246. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  247. var
  248. Size : SizeInt;
  249. begin
  250. if p=nil then
  251. exit;
  252. Size := IndexWord(p^, -1, 0);
  253. // Setlength (result,Size);
  254. if Size>0 then
  255. begin
  256. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  257. { Terminating Zero }
  258. // PChar(Pointer(result)+Size)^:=#0;
  259. end;
  260. end;
  261. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  262. var
  263. Size : SizeInt;
  264. begin
  265. if p=nil then
  266. exit;
  267. Size := IndexWord(p^, -1, 0);
  268. Setlength (result,Size);
  269. if Size>0 then
  270. begin
  271. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  272. { Terminating Zero }
  273. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  274. end;
  275. end;
  276. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  277. var
  278. Size : SizeInt;
  279. temp: ansistring;
  280. begin
  281. if p=nil then
  282. begin
  283. fpc_PWideChar_To_ShortStr:='';
  284. exit;
  285. end;
  286. Size := IndexWord(p^, $7fffffff, 0);
  287. // Setlength (result,Size+1);
  288. if Size>0 then
  289. begin
  290. // If Size>255 then
  291. // Size:=255;
  292. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  293. // byte(result[0]):=byte(Size);
  294. end;
  295. result := temp
  296. end;
  297. { checked against the ansistring routine, 2001-05-27 (FK) }
  298. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  299. {
  300. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  301. }
  302. begin
  303. If S2<>nil then
  304. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  305. Inc(PWideRec(S2-WideFirstOff)^.ref);
  306. { Decrease the reference count on the old S1 }
  307. fpc_widestr_decr_ref (S1);
  308. { And finally, have S1 pointing to S2 (or its copy) }
  309. S1:=S2;
  310. end;
  311. { alias for internal use }
  312. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  313. { checked against the ansistring routine, 2001-05-27 (FK) }
  314. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  315. var
  316. S3: WideString absolute result;
  317. {
  318. Concatenates 2 WideStrings : S1+S2.
  319. Result Goes to S3;
  320. }
  321. Var
  322. Size,Location : SizeInt;
  323. begin
  324. { only assign if s1 or s2 is empty }
  325. if (S1='') then
  326. S3 := S2
  327. else
  328. if (S2='') then
  329. S3 := S1
  330. else
  331. begin
  332. { create new result }
  333. Size:=Length(S2);
  334. Location:=Length(S1);
  335. SetLength (S3,Size+Location);
  336. Move (S1[1],S3[1],Location*sizeof(WideChar));
  337. Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
  338. end;
  339. end;
  340. Function fpc_Char_To_WideStr(const c : WideChar): WideString; compilerproc;
  341. {
  342. Converts a Char to a WideString;
  343. }
  344. begin
  345. if c = #0 then
  346. { result is automatically set to '' }
  347. exit;
  348. Setlength (fpc_Char_To_WideStr,1);
  349. fpc_Char_To_WideStr[1]:=c;
  350. { Terminating Zero }
  351. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  352. end;
  353. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  354. Var
  355. L : SizeInt;
  356. begin
  357. if (not assigned(p)) or (p[0]=#0) Then
  358. { result is automatically set to '' }
  359. exit;
  360. l:=IndexChar(p^,-1,#0);
  361. //SetLength(fpc_PChar_To_WideStr,L);
  362. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  363. end;
  364. Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; compilerproc;
  365. var
  366. i : SizeInt;
  367. begin
  368. if arr[0]=#0 Then
  369. { result is automatically set to '' }
  370. exit;
  371. i:=IndexChar(arr,high(arr)+1,#0);
  372. if i = -1 then
  373. i := high(arr)+1;
  374. SetLength(fpc_CharArray_To_WideStr,i);
  375. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  376. end;
  377. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  378. var
  379. l: longint;
  380. index: longint;
  381. len: byte;
  382. temp: ansistring;
  383. begin
  384. l := high(arr)+1;
  385. if l>=256 then
  386. l:=255
  387. else if l<0 then
  388. l:=0;
  389. index:=IndexWord(arr[0],l,0);
  390. if (index < 0) then
  391. len := l
  392. else
  393. len := index;
  394. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  395. fpc_WideCharArray_To_ShortStr := temp;
  396. //fpc_WideCharArray_To_ShortStr[0]:=chr(len);
  397. end;
  398. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar): AnsiString; compilerproc;
  399. var
  400. i : SizeInt;
  401. begin
  402. if arr[0]=#0 Then
  403. { result is automatically set to '' }
  404. exit;
  405. i:=IndexWord(arr,high(arr)+1,0);
  406. if i = -1 then
  407. i := high(arr)+1;
  408. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  409. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  410. end;
  411. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar): WideString; compilerproc;
  412. var
  413. i : SizeInt;
  414. begin
  415. if arr[0]=#0 Then
  416. { result is automatically set to '' }
  417. exit;
  418. i:=IndexWord(arr,high(arr)+1,0);
  419. if i = -1 then
  420. i := high(arr)+1;
  421. SetLength(fpc_WideCharArray_To_WideStr,i);
  422. Move(pwidechar(@arr)^, PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1]))^,i*sizeof(WideChar));
  423. { Terminating Zero }
  424. PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1])+i*sizeof(WideChar))^:=#0;
  425. end;
  426. { inside the compiler, the resulttype is modified to that of the actual }
  427. { chararray we're converting to (JM) }
  428. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  429. var
  430. len: SizeInt;
  431. temp: ansistring;
  432. begin
  433. len := length(src);
  434. { make sure we don't dereference src if it can be nil (JM) }
  435. if len > 0 then
  436. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  437. len := length(temp);
  438. if len > arraysize then
  439. len := arraysize;
  440. move(temp[1],fpc_widestr_to_chararray[0],len);
  441. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  442. end;
  443. { inside the compiler, the resulttype is modified to that of the actual }
  444. { widechararray we're converting to (JM) }
  445. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  446. var
  447. len: SizeInt;
  448. begin
  449. len := length(src);
  450. if len > arraysize then
  451. len := arraysize;
  452. { make sure we don't try to access element 1 of the ansistring if it's nil }
  453. if len > 0 then
  454. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  455. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  456. end;
  457. { inside the compiler, the resulttype is modified to that of the actual }
  458. { chararray we're converting to (JM) }
  459. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  460. var
  461. len: SizeInt;
  462. temp: widestring;
  463. begin
  464. len := length(src);
  465. { make sure we don't dereference src if it can be nil (JM) }
  466. if len > 0 then
  467. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  468. len := length(temp);
  469. if len > arraysize then
  470. len := arraysize;
  471. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  472. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  473. end;
  474. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  475. var
  476. len: longint;
  477. temp : widestring;
  478. begin
  479. len := length(src);
  480. { make sure we don't access char 1 if length is 0 (JM) }
  481. if len > 0 then
  482. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  483. len := length(temp);
  484. if len > arraysize then
  485. len := arraysize;
  486. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  487. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  488. end;
  489. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  490. {
  491. Compares 2 WideStrings;
  492. The result is
  493. <0 if S1<S2
  494. 0 if S1=S2
  495. >0 if S1>S2
  496. }
  497. Var
  498. MaxI,Temp : SizeInt;
  499. begin
  500. if pointer(S1)=pointer(S2) then
  501. begin
  502. fpc_WideStr_Compare:=0;
  503. exit;
  504. end;
  505. Maxi:=Length(S1);
  506. temp:=Length(S2);
  507. If MaxI>Temp then
  508. MaxI:=Temp;
  509. Temp:=CompareWord(S1[1],S2[1],MaxI);
  510. if temp=0 then
  511. temp:=Length(S1)-Length(S2);
  512. fpc_WideStr_Compare:=Temp;
  513. end;
  514. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  515. begin
  516. if p=nil then
  517. HandleErrorFrame(201,get_frame);
  518. end;
  519. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  520. begin
  521. if (index>len) or (Index<1) then
  522. HandleErrorFrame(201,get_frame);
  523. end;
  524. Procedure fpc_WideStr_SetLength (Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  525. {
  526. Sets The length of string S to L.
  527. Makes sure S is unique, and contains enough room.
  528. }
  529. Var
  530. Temp : Pointer;
  531. movelen: SizeInt;
  532. begin
  533. if (l>0) then
  534. begin
  535. if Pointer(S)=nil then
  536. begin
  537. { Need a complete new string...}
  538. Pointer(s):=NewWideString(l);
  539. end
  540. else if (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1) then
  541. begin
  542. Dec(Pointer(S),WideFirstOff);
  543. if L*sizeof(WideChar)+WideRecLen>MemSize(Pointer(S)) then
  544. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  545. Inc(Pointer(S), WideFirstOff);
  546. end
  547. else
  548. begin
  549. { Reallocation is needed... }
  550. Temp:=Pointer(NewWideString(L));
  551. if Length(S)>0 then
  552. begin
  553. if l < succ(length(s)) then
  554. movelen := l
  555. { also move terminating null }
  556. else movelen := succ(length(s));
  557. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  558. end;
  559. fpc_widestr_decr_ref(Pointer(S));
  560. Pointer(S):=Temp;
  561. end;
  562. { Force nil termination in case it gets shorter }
  563. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  564. PWideRec(Pointer(S)-FirstOff)^.Len:=l;
  565. end
  566. else
  567. begin
  568. { Length=0 }
  569. if Pointer(S)<>nil then
  570. fpc_widestr_decr_ref (Pointer(S));
  571. Pointer(S):=Nil;
  572. end;
  573. end;
  574. {*****************************************************************************
  575. Public functions, In interface.
  576. *****************************************************************************}
  577. function WideCharToString(S : PWideChar) : AnsiString;
  578. begin
  579. result:=WideCharLenToString(s,Length(WideString(s)));
  580. end;
  581. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  582. var
  583. temp:widestring;
  584. begin
  585. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  586. if Length(temp)<DestSize then
  587. move(temp[1],Dest^,Length(temp))
  588. else
  589. move(temp[1],Dest^,destsize);
  590. result:=Dest;
  591. end;
  592. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  593. begin
  594. //SetLength(result,Len);
  595. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  596. end;
  597. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;var Dest : AnsiString);
  598. begin
  599. Dest:=WideCharLenToString(Src,Len);
  600. end;
  601. procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
  602. begin
  603. Dest:=WideCharToString(S);
  604. end;
  605. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  606. {
  607. Make sure reference count of S is 1,
  608. using copy-on-write semantics.
  609. }
  610. Var
  611. SNew : Pointer;
  612. L : SizeInt;
  613. begin
  614. pointer(result) := pointer(s);
  615. If Pointer(S)=Nil then
  616. exit;
  617. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  618. begin
  619. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  620. SNew:=NewWideString (L);
  621. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  622. PWideRec(SNew-WideFirstOff)^.len:=L;
  623. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  624. pointer(S):=SNew;
  625. pointer(result):=SNew;
  626. end;
  627. end;
  628. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  629. var
  630. ResultAddress : Pointer;
  631. begin
  632. ResultAddress:=Nil;
  633. dec(index);
  634. if Index < 0 then
  635. Index := 0;
  636. { Check Size. Accounts for Zero-length S, the double check is needed because
  637. Size can be maxint and will get <0 when adding index }
  638. if (Size>Length(S)) or
  639. (Index+Size>Length(S)) then
  640. Size:=Length(S)-Index;
  641. If Size>0 then
  642. begin
  643. If Index<0 Then
  644. Index:=0;
  645. ResultAddress:=Pointer(NewWideString (Size));
  646. if ResultAddress<>Nil then
  647. begin
  648. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  649. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  650. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  651. end;
  652. end;
  653. Pointer(fpc_widestr_Copy):=ResultAddress;
  654. end;
  655. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  656. var
  657. i,MaxLen : SizeInt;
  658. pc : pwidechar;
  659. begin
  660. Pos:=0;
  661. if Length(SubStr)>0 then
  662. begin
  663. MaxLen:=Length(source)-Length(SubStr);
  664. i:=0;
  665. pc:=@source[1];
  666. while (i<=MaxLen) do
  667. begin
  668. inc(i);
  669. if (SubStr[1]=pc^) and
  670. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  671. begin
  672. Pos:=i;
  673. exit;
  674. end;
  675. inc(pc);
  676. end;
  677. end;
  678. end;
  679. { Faster version for a widechar alone }
  680. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  681. var
  682. i: SizeInt;
  683. pc : pwidechar;
  684. begin
  685. pc:=@s[1];
  686. for i:=1 to length(s) do
  687. begin
  688. if pc^=c then
  689. begin
  690. pos:=i;
  691. exit;
  692. end;
  693. inc(pc);
  694. end;
  695. pos:=0;
  696. end;
  697. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  698. var
  699. i: SizeInt;
  700. pc : pchar;
  701. begin
  702. pc:=@s[1];
  703. for i:=1 to length(s) do
  704. begin
  705. if widechar(pc^)=c then
  706. begin
  707. pos:=i;
  708. exit;
  709. end;
  710. inc(pc);
  711. end;
  712. pos:=0;
  713. end;
  714. { Faster version for a char alone. Must be implemented because }
  715. { pos(c: char; const s: shortstring) also exists, so otherwise }
  716. { using pos(char,pchar) will always call the shortstring version }
  717. { (exact match for first argument), also with $h+ (JM) }
  718. Function Pos (c : Char; Const s : WideString) : SizeInt;
  719. var
  720. i: SizeInt;
  721. wc : widechar;
  722. pc : pwidechar;
  723. begin
  724. wc:=c;
  725. pc:=@s[1];
  726. for i:=1 to length(s) do
  727. begin
  728. if pc^=wc then
  729. begin
  730. pos:=i;
  731. exit;
  732. end;
  733. inc(pc);
  734. end;
  735. pos:=0;
  736. end;
  737. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  738. Var
  739. LS : SizeInt;
  740. begin
  741. If Length(S)=0 then
  742. exit;
  743. if index<=0 then
  744. exit;
  745. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  746. if (Index<=LS) and (Size>0) then
  747. begin
  748. UniqueString (S);
  749. if Size+Index>LS then
  750. Size:=LS-Index+1;
  751. if Index+Size<=LS then
  752. begin
  753. Dec(Index);
  754. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  755. end;
  756. Setlength(s,LS-Size);
  757. end;
  758. end;
  759. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  760. var
  761. Temp : WideString;
  762. LS : SizeInt;
  763. begin
  764. If Length(Source)=0 then
  765. exit;
  766. if index <= 0 then
  767. index := 1;
  768. Ls:=Length(S);
  769. if index > LS then
  770. index := LS+1;
  771. Dec(Index);
  772. Pointer(Temp) := NewWideString(Length(Source)+LS);
  773. SetLength(Temp,Length(Source)+LS);
  774. If Index>0 then
  775. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  776. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  777. If (LS-Index)>0 then
  778. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  779. S:=Temp;
  780. end;
  781. function UpCase(const s : WideString) : WideString;
  782. begin
  783. result:=widestringmanager.UpperWideStringProc(s);
  784. end;
  785. Procedure SetString (Var S : WideString; Buf : PWideChar; Len : SizeInt);
  786. var
  787. BufLen: SizeInt;
  788. begin
  789. SetLength(S,Len);
  790. If (Buf<>Nil) and (Len>0) then
  791. begin
  792. BufLen := IndexWord(Buf^, Len+1, 0);
  793. If (BufLen>0) and (BufLen < Len) then
  794. Len := BufLen;
  795. Move (Buf[0],S[1],Len*sizeof(WideChar));
  796. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  797. end;
  798. end;
  799. Procedure SetString (Var S : WideString; Buf : PChar; Len : SizeInt);
  800. var
  801. BufLen: SizeInt;
  802. begin
  803. SetLength(S,Len);
  804. If (Buf<>Nil) and (Len>0) then
  805. begin
  806. BufLen := IndexByte(Buf^, Len+1, 0);
  807. If (BufLen>0) and (BufLen < Len) then
  808. Len := BufLen;
  809. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  810. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  811. end;
  812. end;
  813. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  814. Var
  815. SS : String;
  816. begin
  817. fpc_Val_Real_WideStr := 0;
  818. if length(S) > 255 then
  819. code := 256
  820. else
  821. begin
  822. SS := S;
  823. Val(SS,fpc_Val_Real_WideStr,code);
  824. end;
  825. end;
  826. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  827. Var
  828. SS : ShortString;
  829. begin
  830. fpc_Val_UInt_WideStr := 0;
  831. if length(S) > 255 then
  832. code := 256
  833. else
  834. begin
  835. SS := S;
  836. Val(SS,fpc_Val_UInt_WideStr,code);
  837. end;
  838. end;
  839. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  840. Var
  841. SS : ShortString;
  842. begin
  843. fpc_Val_SInt_WideStr:=0;
  844. if length(S)>255 then
  845. code:=256
  846. else
  847. begin
  848. SS := S;
  849. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  850. end;
  851. end;
  852. {$ifndef CPU64}
  853. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  854. Var
  855. SS : ShortString;
  856. begin
  857. fpc_Val_qword_WideStr:=0;
  858. if length(S)>255 then
  859. code:=256
  860. else
  861. begin
  862. SS := S;
  863. Val(SS,fpc_Val_qword_WideStr,Code);
  864. end;
  865. end;
  866. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  867. Var
  868. SS : ShortString;
  869. begin
  870. fpc_Val_int64_WideStr:=0;
  871. if length(S)>255 then
  872. code:=256
  873. else
  874. begin
  875. SS := S;
  876. Val(SS,fpc_Val_int64_WideStr,Code);
  877. end;
  878. end;
  879. {$endif CPU64}
  880. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;var s : WideString);compilerproc;
  881. var
  882. ss : shortstring;
  883. begin
  884. str_real(len,fr,d,treal_type(rt),ss);
  885. s:=ss;
  886. end;
  887. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; Var S : WideString);compilerproc;
  888. Var
  889. SS : ShortString;
  890. begin
  891. Str (v:Len,SS);
  892. S:=SS;
  893. end;
  894. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; Var S : WideString);compilerproc;
  895. Var
  896. SS : ShortString;
  897. begin
  898. str(v:Len,SS);
  899. S:=SS;
  900. end;
  901. {$ifndef CPU64}
  902. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; Var S : WideString);compilerproc;
  903. Var
  904. SS : ShortString;
  905. begin
  906. Str (v:Len,SS);
  907. S:=SS;
  908. end;
  909. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; Var S : WideString);compilerproc;
  910. Var
  911. SS : ShortString;
  912. begin
  913. str(v:Len,SS);
  914. S:=SS;
  915. end;
  916. {$endif CPU64}
  917. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  918. begin
  919. if assigned(Source) then
  920. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  921. else
  922. Result:=0;
  923. end;
  924. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  925. var
  926. i,j : SizeUInt;
  927. w : word;
  928. begin
  929. result:=0;
  930. if source=nil then
  931. exit;
  932. i:=0;
  933. j:=0;
  934. if assigned(Dest) then
  935. begin
  936. while (i<SourceChars) and (j<MaxDestBytes) do
  937. begin
  938. w:=word(Source[i]);
  939. case w of
  940. 0..$7f:
  941. begin
  942. Dest[j]:=char(w);
  943. inc(j);
  944. end;
  945. $80..$7ff:
  946. begin
  947. if j+1>=MaxDestBytes then
  948. break;
  949. Dest[j]:=char($c0 or (w shr 6));
  950. Dest[j+1]:=char($80 or (w and $3f));
  951. inc(j,2);
  952. end;
  953. else
  954. begin
  955. if j+2>=MaxDestBytes then
  956. break;
  957. Dest[j]:=char($e0 or (w shr 12));
  958. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  959. Dest[j+2]:=char($80 or (w and $3f));
  960. inc(j,3);
  961. end;
  962. end;
  963. inc(i);
  964. end;
  965. if j>MaxDestBytes-1 then
  966. j:=MaxDestBytes-1;
  967. Dest[j]:=#0;
  968. end
  969. else
  970. begin
  971. while i<SourceChars do
  972. begin
  973. case word(Source[i]) of
  974. $0..$7f:
  975. inc(j);
  976. $80..$7ff:
  977. inc(j,2);
  978. else
  979. inc(j,3);
  980. end;
  981. end;
  982. end;
  983. result:=j+1;
  984. end;
  985. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  986. begin
  987. if assigned(Source) then
  988. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  989. else
  990. Result:=0;
  991. end;
  992. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  993. var
  994. i,j : SizeUInt;
  995. w: SizeUInt;
  996. b : byte;
  997. begin
  998. if not assigned(Source) then
  999. begin
  1000. result:=0;
  1001. exit;
  1002. end;
  1003. result:=SizeUInt(-1);
  1004. i:=0;
  1005. j:=0;
  1006. if assigned(Dest) then
  1007. begin
  1008. while (j<MaxDestChars) and (i<SourceBytes) do
  1009. begin
  1010. b:=byte(Source[i]);
  1011. w:=b;
  1012. inc(i);
  1013. // 2 or 3 bytes?
  1014. if b>=$80 then
  1015. begin
  1016. w:=b and $3f;
  1017. if i>=SourceBytes then
  1018. exit;
  1019. // 3 bytes?
  1020. if (b and $20)<>0 then
  1021. begin
  1022. b:=byte(Source[i]);
  1023. inc(i);
  1024. if i>=SourceBytes then
  1025. exit;
  1026. if (b and $c0)<>$80 then
  1027. exit;
  1028. w:=(w shl 6) or (b and $3f);
  1029. end;
  1030. b:=byte(Source[i]);
  1031. w:=(w shl 6) or (b and $3f);
  1032. if (b and $c0)<>$80 then
  1033. exit;
  1034. inc(i);
  1035. end;
  1036. Dest[j]:=WideChar(w);
  1037. inc(j);
  1038. end;
  1039. if j>=MaxDestChars then j:=MaxDestChars-1;
  1040. Dest[j]:=#0;
  1041. end
  1042. else
  1043. begin
  1044. while i<SourceBytes do
  1045. begin
  1046. b:=byte(Source[i]);
  1047. inc(i);
  1048. // 2 or 3 bytes?
  1049. if b>=$80 then
  1050. begin
  1051. if i>=SourceBytes then
  1052. exit;
  1053. // 3 bytes?
  1054. b := b and $3f;
  1055. if (b and $20)<>0 then
  1056. begin
  1057. b:=byte(Source[i]);
  1058. inc(i);
  1059. if i>=SourceBytes then
  1060. exit;
  1061. if (b and $c0)<>$80 then
  1062. exit;
  1063. end;
  1064. if (byte(Source[i]) and $c0)<>$80 then
  1065. exit;
  1066. inc(i);
  1067. end;
  1068. inc(j);
  1069. end;
  1070. end;
  1071. result:=j+1;
  1072. end;
  1073. function UTF8Encode(const s : WideString) : UTF8String;
  1074. var
  1075. i : SizeInt;
  1076. hs : UTF8String;
  1077. begin
  1078. result:='';
  1079. if s='' then
  1080. exit;
  1081. SetLength(hs,length(s)*3);
  1082. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1083. if i>0 then
  1084. begin
  1085. SetLength(hs,i-1);
  1086. result:=hs;
  1087. end;
  1088. end;
  1089. function UTF8Decode(const s : UTF8String): WideString;
  1090. var
  1091. i : SizeInt;
  1092. hs : WideString;
  1093. begin
  1094. result:='';
  1095. if s='' then
  1096. exit;
  1097. SetLength(hs,length(s));
  1098. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1099. if i>0 then
  1100. begin
  1101. SetLength(hs,i-1);
  1102. result:=hs;
  1103. end;
  1104. end;
  1105. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1106. begin
  1107. Result:=Utf8Encode(s);
  1108. end;
  1109. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1110. begin
  1111. Result:=Utf8Decode(s);
  1112. end;
  1113. procedure unimplementedwidestring;
  1114. begin
  1115. HandleErrorFrame(215,get_frame);
  1116. end;
  1117. function GenericWideCase(const s : WideString) : WideString;
  1118. begin
  1119. unimplementedwidestring;
  1120. end;
  1121. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1122. begin
  1123. unimplementedwidestring;
  1124. end;
  1125. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1126. begin
  1127. unimplementedwidestring;
  1128. end;
  1129. function CharLengthPChar(const Str: PChar): PtrInt;
  1130. begin
  1131. unimplementedwidestring;
  1132. end;
  1133. procedure initwidestringmanager;
  1134. begin
  1135. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1136. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1137. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1138. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1139. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1140. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1141. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1142. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1143. end;