2
0

wstrings.inc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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. end;
  564. Inc(Pointer(S), WideFirstOff);
  565. end
  566. {$endif MSWINDOWS}
  567. else
  568. begin
  569. { Reallocation is needed... }
  570. Temp:=Pointer(NewWideString(L));
  571. if Length(S)>0 then
  572. begin
  573. if l < succ(length(s)) then
  574. movelen := l
  575. { also move terminating null }
  576. else movelen := succ(length(s));
  577. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  578. end;
  579. fpc_widestr_decr_ref(Pointer(S));
  580. Pointer(S):=Temp;
  581. end;
  582. { Force nil termination in case it gets shorter }
  583. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  584. PWideRec(Pointer(S)-FirstOff)^.Len:=l;
  585. end
  586. else
  587. begin
  588. { Length=0 }
  589. if Pointer(S)<>nil then
  590. fpc_widestr_decr_ref (Pointer(S));
  591. Pointer(S):=Nil;
  592. end;
  593. end;
  594. {*****************************************************************************
  595. Public functions, In interface.
  596. *****************************************************************************}
  597. function WideCharToString(S : PWideChar) : AnsiString;
  598. begin
  599. result:=WideCharLenToString(s,Length(WideString(s)));
  600. end;
  601. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  602. var
  603. temp:widestring;
  604. begin
  605. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  606. if Length(temp)<DestSize then
  607. move(temp[1],Dest^,Length(temp))
  608. else
  609. move(temp[1],Dest^,destsize);
  610. result:=Dest;
  611. end;
  612. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  613. begin
  614. //SetLength(result,Len);
  615. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  616. end;
  617. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;var Dest : AnsiString);
  618. begin
  619. Dest:=WideCharLenToString(Src,Len);
  620. end;
  621. procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
  622. begin
  623. Dest:=WideCharToString(S);
  624. end;
  625. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  626. {
  627. Make sure reference count of S is 1,
  628. using copy-on-write semantics.
  629. }
  630. Var
  631. SNew : Pointer;
  632. L : SizeInt;
  633. begin
  634. pointer(result) := pointer(s);
  635. If Pointer(S)=Nil then
  636. exit;
  637. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  638. begin
  639. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  640. SNew:=NewWideString (L);
  641. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  642. PWideRec(SNew-WideFirstOff)^.len:=L;
  643. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  644. pointer(S):=SNew;
  645. pointer(result):=SNew;
  646. end;
  647. end;
  648. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  649. var
  650. ResultAddress : Pointer;
  651. begin
  652. ResultAddress:=Nil;
  653. dec(index);
  654. if Index < 0 then
  655. Index := 0;
  656. { Check Size. Accounts for Zero-length S, the double check is needed because
  657. Size can be maxint and will get <0 when adding index }
  658. if (Size>Length(S)) or
  659. (Index+Size>Length(S)) then
  660. Size:=Length(S)-Index;
  661. If Size>0 then
  662. begin
  663. If Index<0 Then
  664. Index:=0;
  665. ResultAddress:=Pointer(NewWideString (Size));
  666. if ResultAddress<>Nil then
  667. begin
  668. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  669. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  670. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  671. end;
  672. end;
  673. Pointer(fpc_widestr_Copy):=ResultAddress;
  674. end;
  675. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  676. var
  677. i,MaxLen : SizeInt;
  678. pc : pwidechar;
  679. begin
  680. Pos:=0;
  681. if Length(SubStr)>0 then
  682. begin
  683. MaxLen:=Length(source)-Length(SubStr);
  684. i:=0;
  685. pc:=@source[1];
  686. while (i<=MaxLen) do
  687. begin
  688. inc(i);
  689. if (SubStr[1]=pc^) and
  690. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  691. begin
  692. Pos:=i;
  693. exit;
  694. end;
  695. inc(pc);
  696. end;
  697. end;
  698. end;
  699. { Faster version for a widechar alone }
  700. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  701. var
  702. i: SizeInt;
  703. pc : pwidechar;
  704. begin
  705. pc:=@s[1];
  706. for i:=1 to length(s) do
  707. begin
  708. if pc^=c then
  709. begin
  710. pos:=i;
  711. exit;
  712. end;
  713. inc(pc);
  714. end;
  715. pos:=0;
  716. end;
  717. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  718. var
  719. i: SizeInt;
  720. pc : pchar;
  721. begin
  722. pc:=@s[1];
  723. for i:=1 to length(s) do
  724. begin
  725. if widechar(pc^)=c then
  726. begin
  727. pos:=i;
  728. exit;
  729. end;
  730. inc(pc);
  731. end;
  732. pos:=0;
  733. end;
  734. { Faster version for a char alone. Must be implemented because }
  735. { pos(c: char; const s: shortstring) also exists, so otherwise }
  736. { using pos(char,pchar) will always call the shortstring version }
  737. { (exact match for first argument), also with $h+ (JM) }
  738. Function Pos (c : Char; Const s : WideString) : SizeInt;
  739. var
  740. i: SizeInt;
  741. wc : widechar;
  742. pc : pwidechar;
  743. begin
  744. wc:=c;
  745. pc:=@s[1];
  746. for i:=1 to length(s) do
  747. begin
  748. if pc^=wc then
  749. begin
  750. pos:=i;
  751. exit;
  752. end;
  753. inc(pc);
  754. end;
  755. pos:=0;
  756. end;
  757. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  758. Var
  759. LS : SizeInt;
  760. begin
  761. If Length(S)=0 then
  762. exit;
  763. if index<=0 then
  764. exit;
  765. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  766. if (Index<=LS) and (Size>0) then
  767. begin
  768. UniqueString (S);
  769. if Size+Index>LS then
  770. Size:=LS-Index+1;
  771. if Index+Size<=LS then
  772. begin
  773. Dec(Index);
  774. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  775. end;
  776. Setlength(s,LS-Size);
  777. end;
  778. end;
  779. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  780. var
  781. Temp : WideString;
  782. LS : SizeInt;
  783. begin
  784. If Length(Source)=0 then
  785. exit;
  786. if index <= 0 then
  787. index := 1;
  788. Ls:=Length(S);
  789. if index > LS then
  790. index := LS+1;
  791. Dec(Index);
  792. Pointer(Temp) := NewWideString(Length(Source)+LS);
  793. SetLength(Temp,Length(Source)+LS);
  794. If Index>0 then
  795. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  796. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  797. If (LS-Index)>0 then
  798. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  799. S:=Temp;
  800. end;
  801. function UpCase(const s : WideString) : WideString;
  802. begin
  803. result:=widestringmanager.UpperWideStringProc(s);
  804. end;
  805. Procedure SetString (Var S : WideString; Buf : PWideChar; Len : SizeInt);
  806. var
  807. BufLen: SizeInt;
  808. begin
  809. SetLength(S,Len);
  810. If (Buf<>Nil) and (Len>0) then
  811. begin
  812. BufLen := IndexWord(Buf^, Len+1, 0);
  813. If (BufLen>0) and (BufLen < Len) then
  814. Len := BufLen;
  815. Move (Buf[0],S[1],Len*sizeof(WideChar));
  816. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  817. end;
  818. end;
  819. Procedure SetString (Var S : WideString; Buf : PChar; Len : SizeInt);
  820. var
  821. BufLen: SizeInt;
  822. begin
  823. SetLength(S,Len);
  824. If (Buf<>Nil) and (Len>0) then
  825. begin
  826. BufLen := IndexByte(Buf^, Len+1, 0);
  827. If (BufLen>0) and (BufLen < Len) then
  828. Len := BufLen;
  829. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  830. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  831. end;
  832. end;
  833. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  834. Var
  835. SS : String;
  836. begin
  837. fpc_Val_Real_WideStr := 0;
  838. if length(S) > 255 then
  839. code := 256
  840. else
  841. begin
  842. SS := S;
  843. Val(SS,fpc_Val_Real_WideStr,code);
  844. end;
  845. end;
  846. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  847. Var
  848. SS : ShortString;
  849. begin
  850. fpc_Val_UInt_WideStr := 0;
  851. if length(S) > 255 then
  852. code := 256
  853. else
  854. begin
  855. SS := S;
  856. Val(SS,fpc_Val_UInt_WideStr,code);
  857. end;
  858. end;
  859. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  860. Var
  861. SS : ShortString;
  862. begin
  863. fpc_Val_SInt_WideStr:=0;
  864. if length(S)>255 then
  865. code:=256
  866. else
  867. begin
  868. SS := S;
  869. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  870. end;
  871. end;
  872. {$ifndef CPU64}
  873. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  874. Var
  875. SS : ShortString;
  876. begin
  877. fpc_Val_qword_WideStr:=0;
  878. if length(S)>255 then
  879. code:=256
  880. else
  881. begin
  882. SS := S;
  883. Val(SS,fpc_Val_qword_WideStr,Code);
  884. end;
  885. end;
  886. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  887. Var
  888. SS : ShortString;
  889. begin
  890. fpc_Val_int64_WideStr:=0;
  891. if length(S)>255 then
  892. code:=256
  893. else
  894. begin
  895. SS := S;
  896. Val(SS,fpc_Val_int64_WideStr,Code);
  897. end;
  898. end;
  899. {$endif CPU64}
  900. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;var s : WideString);compilerproc;
  901. var
  902. ss : shortstring;
  903. begin
  904. str_real(len,fr,d,treal_type(rt),ss);
  905. s:=ss;
  906. end;
  907. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; Var S : WideString);compilerproc;
  908. Var
  909. SS : ShortString;
  910. begin
  911. Str (v:Len,SS);
  912. S:=SS;
  913. end;
  914. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; Var S : WideString);compilerproc;
  915. Var
  916. SS : ShortString;
  917. begin
  918. str(v:Len,SS);
  919. S:=SS;
  920. end;
  921. {$ifndef CPU64}
  922. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; Var S : WideString);compilerproc;
  923. Var
  924. SS : ShortString;
  925. begin
  926. Str (v:Len,SS);
  927. S:=SS;
  928. end;
  929. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; Var S : WideString);compilerproc;
  930. Var
  931. SS : ShortString;
  932. begin
  933. str(v:Len,SS);
  934. S:=SS;
  935. end;
  936. {$endif CPU64}
  937. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  938. begin
  939. if assigned(Source) then
  940. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  941. else
  942. Result:=0;
  943. end;
  944. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  945. var
  946. i,j : SizeUInt;
  947. w : word;
  948. begin
  949. result:=0;
  950. if source=nil then
  951. exit;
  952. i:=0;
  953. j:=0;
  954. if assigned(Dest) then
  955. begin
  956. while (i<SourceChars) and (j<MaxDestBytes) do
  957. begin
  958. w:=word(Source[i]);
  959. case w of
  960. 0..$7f:
  961. begin
  962. Dest[j]:=char(w);
  963. inc(j);
  964. end;
  965. $80..$7ff:
  966. begin
  967. if j+1>=MaxDestBytes then
  968. break;
  969. Dest[j]:=char($c0 or (w shr 6));
  970. Dest[j+1]:=char($80 or (w and $3f));
  971. inc(j,2);
  972. end;
  973. else
  974. begin
  975. if j+2>=MaxDestBytes then
  976. break;
  977. Dest[j]:=char($e0 or (w shr 12));
  978. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  979. Dest[j+2]:=char($80 or (w and $3f));
  980. inc(j,3);
  981. end;
  982. end;
  983. inc(i);
  984. end;
  985. if j>MaxDestBytes-1 then
  986. j:=MaxDestBytes-1;
  987. Dest[j]:=#0;
  988. end
  989. else
  990. begin
  991. while i<SourceChars do
  992. begin
  993. case word(Source[i]) of
  994. $0..$7f:
  995. inc(j);
  996. $80..$7ff:
  997. inc(j,2);
  998. else
  999. inc(j,3);
  1000. end;
  1001. end;
  1002. end;
  1003. result:=j+1;
  1004. end;
  1005. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1006. begin
  1007. if assigned(Source) then
  1008. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1009. else
  1010. Result:=0;
  1011. end;
  1012. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1013. var
  1014. i,j : SizeUInt;
  1015. w: SizeUInt;
  1016. b : byte;
  1017. begin
  1018. if not assigned(Source) then
  1019. begin
  1020. result:=0;
  1021. exit;
  1022. end;
  1023. result:=SizeUInt(-1);
  1024. i:=0;
  1025. j:=0;
  1026. if assigned(Dest) then
  1027. begin
  1028. while (j<MaxDestChars) and (i<SourceBytes) do
  1029. begin
  1030. b:=byte(Source[i]);
  1031. w:=b;
  1032. inc(i);
  1033. // 2 or 3 bytes?
  1034. if b>=$80 then
  1035. begin
  1036. w:=b and $3f;
  1037. if i>=SourceBytes then
  1038. exit;
  1039. // 3 bytes?
  1040. if (b and $20)<>0 then
  1041. begin
  1042. b:=byte(Source[i]);
  1043. inc(i);
  1044. if i>=SourceBytes then
  1045. exit;
  1046. if (b and $c0)<>$80 then
  1047. exit;
  1048. w:=(w shl 6) or (b and $3f);
  1049. end;
  1050. b:=byte(Source[i]);
  1051. w:=(w shl 6) or (b and $3f);
  1052. if (b and $c0)<>$80 then
  1053. exit;
  1054. inc(i);
  1055. end;
  1056. Dest[j]:=WideChar(w);
  1057. inc(j);
  1058. end;
  1059. if j>=MaxDestChars then j:=MaxDestChars-1;
  1060. Dest[j]:=#0;
  1061. end
  1062. else
  1063. begin
  1064. while i<SourceBytes do
  1065. begin
  1066. b:=byte(Source[i]);
  1067. inc(i);
  1068. // 2 or 3 bytes?
  1069. if b>=$80 then
  1070. begin
  1071. if i>=SourceBytes then
  1072. exit;
  1073. // 3 bytes?
  1074. b := b and $3f;
  1075. if (b and $20)<>0 then
  1076. begin
  1077. b:=byte(Source[i]);
  1078. inc(i);
  1079. if i>=SourceBytes then
  1080. exit;
  1081. if (b and $c0)<>$80 then
  1082. exit;
  1083. end;
  1084. if (byte(Source[i]) and $c0)<>$80 then
  1085. exit;
  1086. inc(i);
  1087. end;
  1088. inc(j);
  1089. end;
  1090. end;
  1091. result:=j+1;
  1092. end;
  1093. function UTF8Encode(const s : WideString) : UTF8String;
  1094. var
  1095. i : SizeInt;
  1096. hs : UTF8String;
  1097. begin
  1098. result:='';
  1099. if s='' then
  1100. exit;
  1101. SetLength(hs,length(s)*3);
  1102. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1103. if i>0 then
  1104. begin
  1105. SetLength(hs,i-1);
  1106. result:=hs;
  1107. end;
  1108. end;
  1109. function UTF8Decode(const s : UTF8String): WideString;
  1110. var
  1111. i : SizeInt;
  1112. hs : WideString;
  1113. begin
  1114. result:='';
  1115. if s='' then
  1116. exit;
  1117. SetLength(hs,length(s));
  1118. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1119. if i>0 then
  1120. begin
  1121. SetLength(hs,i-1);
  1122. result:=hs;
  1123. end;
  1124. end;
  1125. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1126. begin
  1127. Result:=Utf8Encode(s);
  1128. end;
  1129. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1130. begin
  1131. Result:=Utf8Decode(s);
  1132. end;
  1133. procedure unimplementedwidestring;
  1134. begin
  1135. HandleErrorFrame(215,get_frame);
  1136. end;
  1137. function GenericWideCase(const s : WideString) : WideString;
  1138. begin
  1139. unimplementedwidestring;
  1140. end;
  1141. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1142. begin
  1143. unimplementedwidestring;
  1144. end;
  1145. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1146. begin
  1147. unimplementedwidestring;
  1148. end;
  1149. function CharLengthPChar(const Str: PChar): PtrInt;
  1150. begin
  1151. unimplementedwidestring;
  1152. end;
  1153. procedure initwidestringmanager;
  1154. begin
  1155. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1156. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1157. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1158. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1159. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1160. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1161. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1162. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1163. end;