wstrings.inc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. {$ifdef STR_USES_VALINT}
  888. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; Var S : WideString);compilerproc;
  889. {$else}
  890. Procedure fpc_WideStr_Longint(v : Longint; Len : SizeInt; Var S : WideString);compilerproc;
  891. {$endif}
  892. Var
  893. SS : ShortString;
  894. begin
  895. Str (v:Len,SS);
  896. S:=SS;
  897. end;
  898. {$ifdef STR_USES_VALINT}
  899. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; Var S : WideString);compilerproc;
  900. {$else}
  901. Procedure fpc_WideStr_Longword(v : Longword;Len : SizeInt; Var S : WideString);compilerproc;
  902. {$endif}
  903. Var
  904. SS : ShortString;
  905. begin
  906. str(v:Len,SS);
  907. S:=SS;
  908. end;
  909. {$ifndef CPU64}
  910. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; Var S : WideString);compilerproc;
  911. Var
  912. SS : ShortString;
  913. begin
  914. Str (v:Len,SS);
  915. S:=SS;
  916. end;
  917. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; Var S : WideString);compilerproc;
  918. Var
  919. SS : ShortString;
  920. begin
  921. str(v:Len,SS);
  922. S:=SS;
  923. end;
  924. {$endif CPU64}
  925. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  926. begin
  927. if assigned(Source) then
  928. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  929. else
  930. Result:=0;
  931. end;
  932. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  933. var
  934. i,j : SizeUInt;
  935. w : word;
  936. begin
  937. result:=0;
  938. if source=nil then
  939. exit;
  940. i:=0;
  941. j:=0;
  942. if assigned(Dest) then
  943. begin
  944. while (i<SourceChars) and (j<MaxDestBytes) do
  945. begin
  946. w:=word(Source[i]);
  947. case w of
  948. 0..$7f:
  949. begin
  950. Dest[j]:=char(w);
  951. inc(j);
  952. end;
  953. $80..$7ff:
  954. begin
  955. if j+1>=MaxDestBytes then
  956. break;
  957. Dest[j]:=char($c0 or (w shr 6));
  958. Dest[j+1]:=char($80 or (w and $3f));
  959. inc(j,2);
  960. end;
  961. else
  962. begin
  963. if j+2>=MaxDestBytes then
  964. break;
  965. Dest[j]:=char($e0 or (w shr 12));
  966. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  967. Dest[j+2]:=char($80 or (w and $3f));
  968. inc(j,3);
  969. end;
  970. end;
  971. inc(i);
  972. end;
  973. if j>MaxDestBytes-1 then
  974. j:=MaxDestBytes-1;
  975. Dest[j]:=#0;
  976. end
  977. else
  978. begin
  979. while i<SourceChars do
  980. begin
  981. case word(Source[i]) of
  982. $0..$7f:
  983. inc(j);
  984. $80..$7ff:
  985. inc(j,2);
  986. else
  987. inc(j,3);
  988. end;
  989. end;
  990. end;
  991. result:=j+1;
  992. end;
  993. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  994. begin
  995. if assigned(Source) then
  996. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  997. else
  998. Result:=0;
  999. end;
  1000. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1001. var
  1002. i,j : SizeUInt;
  1003. w: SizeUInt;
  1004. b : byte;
  1005. begin
  1006. if not assigned(Source) then
  1007. begin
  1008. result:=0;
  1009. exit;
  1010. end;
  1011. result:=SizeUInt(-1);
  1012. i:=0;
  1013. j:=0;
  1014. if assigned(Dest) then
  1015. begin
  1016. while (j<MaxDestChars) and (i<SourceBytes) do
  1017. begin
  1018. b:=byte(Source[i]);
  1019. w:=b;
  1020. inc(i);
  1021. // 2 or 3 bytes?
  1022. if b>=$80 then
  1023. begin
  1024. w:=b and $3f;
  1025. if i>=SourceBytes then
  1026. exit;
  1027. // 3 bytes?
  1028. if (b and $20)<>0 then
  1029. begin
  1030. b:=byte(Source[i]);
  1031. inc(i);
  1032. if i>=SourceBytes then
  1033. exit;
  1034. if (b and $c0)<>$80 then
  1035. exit;
  1036. w:=(w shl 6) or (b and $3f);
  1037. end;
  1038. b:=byte(Source[i]);
  1039. w:=(w shl 6) or (b and $3f);
  1040. if (b and $c0)<>$80 then
  1041. exit;
  1042. inc(i);
  1043. end;
  1044. Dest[j]:=WideChar(w);
  1045. inc(j);
  1046. end;
  1047. if j>=MaxDestChars then j:=MaxDestChars-1;
  1048. Dest[j]:=#0;
  1049. end
  1050. else
  1051. begin
  1052. while i<SourceBytes do
  1053. begin
  1054. b:=byte(Source[i]);
  1055. inc(i);
  1056. // 2 or 3 bytes?
  1057. if b>=$80 then
  1058. begin
  1059. if i>=SourceBytes then
  1060. exit;
  1061. // 3 bytes?
  1062. b := b and $3f;
  1063. if (b and $20)<>0 then
  1064. begin
  1065. b:=byte(Source[i]);
  1066. inc(i);
  1067. if i>=SourceBytes then
  1068. exit;
  1069. if (b and $c0)<>$80 then
  1070. exit;
  1071. end;
  1072. if (byte(Source[i]) and $c0)<>$80 then
  1073. exit;
  1074. inc(i);
  1075. end;
  1076. inc(j);
  1077. end;
  1078. end;
  1079. result:=j+1;
  1080. end;
  1081. function UTF8Encode(const s : WideString) : UTF8String;
  1082. var
  1083. i : SizeInt;
  1084. hs : UTF8String;
  1085. begin
  1086. result:='';
  1087. if s='' then
  1088. exit;
  1089. SetLength(hs,length(s)*3);
  1090. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1091. if i>0 then
  1092. begin
  1093. SetLength(hs,i-1);
  1094. result:=hs;
  1095. end;
  1096. end;
  1097. function UTF8Decode(const s : UTF8String): WideString;
  1098. var
  1099. i : SizeInt;
  1100. hs : WideString;
  1101. begin
  1102. result:='';
  1103. if s='' then
  1104. exit;
  1105. SetLength(hs,length(s));
  1106. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1107. if i>0 then
  1108. begin
  1109. SetLength(hs,i-1);
  1110. result:=hs;
  1111. end;
  1112. end;
  1113. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1114. begin
  1115. Result:=Utf8Encode(s);
  1116. end;
  1117. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1118. begin
  1119. Result:=Utf8Decode(s);
  1120. end;
  1121. procedure unimplementedwidestring;
  1122. begin
  1123. HandleErrorFrame(215,get_frame);
  1124. end;
  1125. function GenericWideCase(const s : WideString) : WideString;
  1126. begin
  1127. unimplementedwidestring;
  1128. end;
  1129. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1130. begin
  1131. unimplementedwidestring;
  1132. end;
  1133. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1134. begin
  1135. unimplementedwidestring;
  1136. end;
  1137. function CharLengthPChar(const Str: PChar): PtrInt;
  1138. begin
  1139. unimplementedwidestring;
  1140. end;
  1141. procedure initwidestringmanager;
  1142. begin
  1143. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1144. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1145. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1146. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1147. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1148. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1149. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1150. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1151. end;