wstrings.inc 34 KB

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