wstrings.inc 32 KB

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