wstrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2001 by Florian Klaempfl,
  5. member of the Free Pascal development team.
  6. This file implements support routines for WideStrings with FPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {
  14. This file contains the implementation of the WideString type,
  15. and all things that are needed for it.
  16. WideString is defined as a 'silent' pwidechar :
  17. a pwidechar that points to :
  18. @-12 : Longint for maximum size;
  19. @-8 : Longint for size;
  20. @-4 : Longint for reference count;
  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. Maxlen,
  31. len,
  32. ref : Longint;
  33. First : WideChar;
  34. end;
  35. Const
  36. WideRecLen = SizeOf(TWideRec);
  37. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  38. {
  39. Default WideChar <-> Char conversion is to only convert the
  40. lower 127 chars, all others are translated to spaces.
  41. These routines can be overwritten for the Current Locale
  42. }
  43. procedure Wide2AnsiMove(source:pwidechar;dest:pchar;len:longint);
  44. var
  45. i : longint;
  46. begin
  47. for i:=1to len do
  48. begin
  49. if word(source^)<128 then
  50. dest^:=char(word(source^))
  51. else
  52. dest^:=' ';
  53. inc(dest);
  54. inc(source);
  55. end;
  56. end;
  57. procedure Ansi2WideMove(source:pchar;dest:pwidechar;len:longint);
  58. var
  59. i : longint;
  60. begin
  61. for i:=1to len do
  62. begin
  63. if byte(source^)<128 then
  64. dest^:=widechar(byte(source^))
  65. else
  66. dest^:=' ';
  67. inc(dest);
  68. inc(source);
  69. end;
  70. end;
  71. Type
  72. TWide2AnsiMove=procedure(source:pwidechar;dest:pchar;len:longint);
  73. TAnsi2WideMove=procedure(source:pchar;dest:pwidechar;len:longint);
  74. Const
  75. Wide2AnsiMoveProc:TWide2AnsiMove=@Wide2AnsiMove;
  76. Ansi2WideMoveProc:TAnsi2WideMove=@Ansi2WideMove;
  77. (*
  78. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  79. {
  80. Make sure reference count of S is 1,
  81. using copy-on-write semantics.
  82. }
  83. begin
  84. end;
  85. *)
  86. {****************************************************************************
  87. Internal functions, not in interface.
  88. ****************************************************************************}
  89. {$ifdef WideStrDebug}
  90. Procedure DumpWideRec(S : Pointer);
  91. begin
  92. If S=Nil then
  93. Writeln ('String is nil')
  94. Else
  95. Begin
  96. With PWideRec(S-WideFirstOff)^ do
  97. begin
  98. Write ('(Maxlen: ',maxlen);
  99. Write (' Len:',len);
  100. Writeln (' Ref: ',ref,')');
  101. end;
  102. end;
  103. end;
  104. {$endif}
  105. Function NewWideString(Len : Longint) : Pointer;
  106. {
  107. Allocate a new WideString on the heap.
  108. initialize it to zero length and reference count 1.
  109. }
  110. Var
  111. P : Pointer;
  112. begin
  113. { Also add +1 for a terminating zero }
  114. GetMem(P,Len+Len+WideRecLen);
  115. If P<>Nil then
  116. begin
  117. PWideRec(P)^.Maxlen:=Len; { Maximal length }
  118. PWideRec(P)^.Len:=0; { Initial length }
  119. PWideRec(P)^.Ref:=1; { Set reference count }
  120. PWideRec(P)^.First:=#0; { Terminating #0 }
  121. inc(p,WideFirstOff); { Points to string now }
  122. end;
  123. NewWideString:=P;
  124. end;
  125. Procedure DisposeWideString(Var S : Pointer);
  126. {
  127. Deallocates a WideString From the heap.
  128. }
  129. begin
  130. If S=Nil then
  131. exit;
  132. Dec (Longint(S),WideFirstOff);
  133. FreeMem (S);
  134. S:=Nil;
  135. end;
  136. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  137. {
  138. Decreases the ReferenceCount of a non constant widestring;
  139. If the reference count is zero, deallocate the string;
  140. }
  141. Type
  142. plongint = ^longint;
  143. Var
  144. l : plongint;
  145. Begin
  146. { Zero string }
  147. If S=Nil then exit;
  148. { check for constant strings ...}
  149. l:=@PWIDEREC(S-WideFirstOff)^.Ref;
  150. If l^<0 then exit;
  151. { declocked does a MT safe dec and returns true, if the counter is 0 }
  152. If declocked(l^) then
  153. { Ref count dropped to zero }
  154. DisposeWideString (S); { Remove...}
  155. { this pointer is not valid anymore, so set it to zero }
  156. S:=nil;
  157. end;
  158. {$ifdef hascompilerproc}
  159. { alias for internal use }
  160. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_DECR_REF'];
  161. {$endif compilerproc}
  162. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  163. Begin
  164. If S=Nil then
  165. exit;
  166. { Let's be paranoid : Constant string ??}
  167. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  168. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  169. end;
  170. function fpc_WideStr_To_ShortStr (high_of_res: longint;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  171. {
  172. Converts a WideString to a ShortString;
  173. }
  174. Var
  175. Size : Longint;
  176. begin
  177. if S2='' then
  178. fpc_WideStr_To_ShortStr:=''
  179. else
  180. begin
  181. Size:=Length(S2);
  182. If Size>high_of_res then
  183. Size:=high_of_res;
  184. Wide2AnsiMoveProc(PWideChar(S2),PChar(@fpc_WideStr_To_ShortStr[1]),Size);
  185. byte(fpc_WideStr_To_ShortStr[0]):=Size;
  186. end;
  187. end;
  188. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  189. {
  190. Converts a ShortString to a WideString;
  191. }
  192. Var
  193. Size : Longint;
  194. begin
  195. Size:=Length(S2);
  196. Setlength (fpc_ShortStr_To_WideStr,Size);
  197. if Size>0 then
  198. begin
  199. Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(Pointer(fpc_ShortStr_To_WideStr)),Size);
  200. { Terminating Zero }
  201. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  202. end;
  203. end;
  204. { old style helper }
  205. {$ifndef hascompilerproc}
  206. Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR'];
  207. begin
  208. s1 := pointer(fpc_ShortStr_To_WideStr(s2));
  209. end;
  210. {$endif hascompilerproc}
  211. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  212. {
  213. Converts a WideString to an AnsiString
  214. }
  215. Var
  216. Size : Longint;
  217. begin
  218. if s2='' then
  219. exit;
  220. Size:=Length(WideString(S2));
  221. Setlength (fpc_WideStr_To_AnsiStr,Size);
  222. if Size>0 then
  223. begin
  224. Wide2AnsiMoveProc(PWideChar(Pointer(S2)),PChar(Pointer(fpc_WideStr_To_AnsiStr)),Size);
  225. { Terminating Zero }
  226. PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
  227. end;
  228. end;
  229. { old style helper }
  230. {$ifndef hascompilerproc}
  231. Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;const S2 : WideString);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR'];
  232. begin
  233. s1 := pointer(fpc_WideStr_To_AnsiStr(s2));
  234. end;
  235. {$endif hascompilerproc}
  236. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  237. {
  238. Converts an AnsiString to a WideString;
  239. }
  240. Var
  241. Size : Longint;
  242. begin
  243. if s2='' then
  244. exit;
  245. Size:=Length(S2);
  246. Setlength (fpc_AnsiStr_To_WideStr,Size);
  247. if Size>0 then
  248. begin
  249. Ansi2WideMoveProc(PChar(S2),PWideChar(Pointer(fpc_AnsiStr_To_WideStr)),Size);
  250. { Terminating Zero }
  251. PWideChar(Pointer(fpc_AnsiStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  252. end;
  253. end;
  254. { old style helper }
  255. {$ifndef hascompilerproc}
  256. Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : AnsiString);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR'];
  257. begin
  258. s1 := pointer(fpc_AnsiStr_To_WideStr(s2));
  259. end;
  260. {$endif hascompilerproc}
  261. { checked against the ansistring routine, 2001-05-27 (FK) }
  262. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  263. {
  264. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  265. }
  266. begin
  267. If S2<>nil then
  268. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  269. Inc(PWideRec(S2-WideFirstOff)^.ref);
  270. { Decrease the reference count on the old S1 }
  271. fpc_widestr_decr_ref (S1);
  272. { And finally, have S1 pointing to S2 (or its copy) }
  273. S1:=S2;
  274. end;
  275. {$ifdef hascompilerproc}
  276. { alias for internal use }
  277. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  278. {$endif hascompilerproc}
  279. { checked against the ansistring routine, 2001-05-27 (FK) }
  280. {$ifdef hascompilerproc}
  281. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  282. var
  283. S3: WideString absolute result;
  284. {$else hascompilerproc}
  285. Procedure fpc_WideStr_Concat (S1,S2 : WideString;var S3 : WideString);[Public, alias: 'FPC_WIDESTR_CONCAT'];
  286. {$endif hascompilerproc}
  287. {
  288. Concatenates 2 WideStrings : S1+S2.
  289. Result Goes to S3;
  290. }
  291. Var
  292. Size,Location : Longint;
  293. begin
  294. { only assign if s1 or s2 is empty }
  295. if (S1='') then
  296. S3 := S2
  297. else
  298. if (S2='') then
  299. S3 := S1
  300. else
  301. begin
  302. { create new result }
  303. Size:=Length(S2);
  304. Location:=Length(S1);
  305. SetLength (S3,Size+Location);
  306. Move (S1[1],S3[1],Location*sizeof(WideChar));
  307. Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
  308. end;
  309. end;
  310. Function fpc_Char_To_WideStr(const c : Char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  311. {
  312. Converts a Char to a WideString;
  313. }
  314. begin
  315. if c = #0 then
  316. { result is automatically set to '' }
  317. exit;
  318. Setlength (fpc_Char_To_WideStr,1);
  319. fpc_Char_To_WideStr[1]:=c;
  320. { Terminating Zero }
  321. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  322. end;
  323. { old style helper }
  324. {$ifndef hascompilerproc}
  325. Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_WIDESTR'];
  326. begin
  327. s1 := pointer(fpc_Char_To_WideStr(c));
  328. end;
  329. {$endif hascompilerproc}
  330. Function fpc_PChar_To_WideStr(const p : pchar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  331. Var
  332. L : Longint;
  333. begin
  334. if (not assigned(p)) or (p[0]=#0) Then
  335. { result is automatically set to '' }
  336. exit;
  337. l:=IndexChar(p^,-1,#0);
  338. SetLength(fpc_PChar_To_WideStr,L);
  339. Ansi2WideMoveProc(P,PWideChar(Pointer(fpc_PChar_To_WideStr)),l);
  340. end;
  341. { old style helper }
  342. {$ifndef hascompilerproc}
  343. Procedure fpc_PChar_To_WideStr(var a : WideString;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  344. begin
  345. pointer(a) := pointer(fpc_PChar_To_WideStr(p));
  346. end;
  347. {$endif hascompilerproc}
  348. Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  349. var
  350. i : longint;
  351. begin
  352. if arr[0]=#0 Then
  353. { result is automatically set to '' }
  354. exit;
  355. i:=IndexChar(arr,high(arr)+1,#0);
  356. if i = -1 then
  357. i := high(arr)+1;
  358. SetLength(fpc_CharArray_To_WideStr,i);
  359. Ansi2WideMoveProc (pchar(@arr),PWideChar(Pointer(fpc_CharArray_To_WideStr)),i);
  360. end;
  361. { old style helper }
  362. {$ifndef hascompilerproc}
  363. Procedure fpc_CharArray_To_WideStr(var a : WideString; p: pointer; len: longint); [Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  364. var
  365. src: pchar;
  366. i: longint;
  367. begin
  368. src := pchar(p);
  369. if src[0]=#0 Then
  370. begin
  371. pointer(a) := nil;
  372. exit;
  373. end;
  374. i:=IndexChar(src^,len,#0);
  375. if i = -1 then
  376. i := len;
  377. pointer(a) := NewWideString(i);
  378. Ansi2WideMoveProc (src,PWideChar(Pointer(@a[1])),i);
  379. end;
  380. {$endif not hascompilerproc}
  381. {$ifdef hascompilerproc}
  382. { inside the compiler, the resulttype is modified to that of the actual }
  383. { chararray we're converting to (JM) }
  384. function fpc_widestr_to_chararray(arraysize: longint; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  385. var
  386. len: longint;
  387. begin
  388. len := length(src);
  389. if len > arraysize then
  390. len := arraysize;
  391. { make sure we don't dereference src if it can be nil (JM) }
  392. if len > 0 then
  393. wide2ansimoveproc(pwidechar(@src[1]),pchar(@fpc_widestr_to_chararray[0]),len);
  394. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  395. end;
  396. {$endif hascompilerproc}
  397. Function fpc_WideStr_Compare(const S1,S2 : WideString): Longint;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  398. {
  399. Compares 2 WideStrings;
  400. The result is
  401. <0 if S1<S2
  402. 0 if S1=S2
  403. >0 if S1>S2
  404. }
  405. Var
  406. MaxI,Temp : Longint;
  407. begin
  408. if pointer(S1)=pointer(S2) then
  409. begin
  410. fpc_WideStr_Compare:=0;
  411. exit;
  412. end;
  413. Maxi:=Length(S1);
  414. temp:=Length(S2);
  415. If MaxI>Temp then
  416. MaxI:=Temp;
  417. Temp:=CompareWord(S1[1],S2[1],MaxI);
  418. if temp=0 then
  419. temp:=Length(S1)-Length(S2);
  420. fpc_WideStr_Compare:=Temp;
  421. end;
  422. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  423. begin
  424. if p=nil then
  425. HandleErrorFrame(201,get_frame);
  426. end;
  427. Procedure fpc_WideStr_CheckRange(len,index : longint);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  428. begin
  429. if (index>len) or (Index<1) then
  430. HandleErrorFrame(201,get_frame);
  431. end;
  432. {$ifndef INTERNSETLENGTH}
  433. Procedure SetLength (Var S : WideString; l : Longint);
  434. {$else INTERNSETLENGTH}
  435. Procedure fpc_WideStr_SetLength (Var S : WideString; l : Longint);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  436. {$endif INTERNSETLENGTH}
  437. {
  438. Sets The length of string S to L.
  439. Makes sure S is unique, and contains enough room.
  440. }
  441. Var
  442. Temp : Pointer;
  443. begin
  444. if (l>0) then
  445. begin
  446. if Pointer(S)=nil then
  447. begin
  448. { Need a complete new string...}
  449. Pointer(s):=NewWideString(l);
  450. end
  451. else
  452. If (PWideRec(Pointer(S)-WideFirstOff)^.Maxlen < L) or
  453. (PWideRec(Pointer(S)-WideFirstOff)^.Ref <> 1) then
  454. begin
  455. { Reallocation is needed... }
  456. Temp:=Pointer(NewWideString(L));
  457. if Length(S)>0 then
  458. Move(Pointer(S)^,Temp^,L*sizeof(WideChar));
  459. fpc_WideStr_decr_ref(Pointer(S));
  460. Pointer(S):=Temp;
  461. end;
  462. { Force nil termination in case it gets shorter }
  463. PWideChar(Pointer(S)+l*sizeof(WideChar))^:=#0;
  464. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l;
  465. end
  466. else
  467. begin
  468. { Length=0 }
  469. if Pointer(S)<>nil then
  470. fpc_WideStr_decr_ref (Pointer(S));
  471. Pointer(S):=Nil;
  472. end;
  473. end;
  474. {*****************************************************************************
  475. Public functions, In interface.
  476. *****************************************************************************}
  477. {$ifndef INTERNLENGTH}
  478. Function Length (Const S : WideString) : Longint;
  479. {
  480. Returns the length of an WideString.
  481. Takes in acount that zero strings are NIL;
  482. }
  483. begin
  484. If Pointer(S)=Nil then
  485. Length:=0
  486. else
  487. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  488. end;
  489. {$endif INTERNLENGTH}
  490. { overloaded version of UniqueString for interface }
  491. procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
  492. Procedure fpc_widestr_Unique(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  493. {
  494. Make sure reference count of S is 1,
  495. using copy-on-write semantics.
  496. }
  497. Var
  498. SNew : Pointer;
  499. L : Longint;
  500. begin
  501. If Pointer(S)=Nil then
  502. exit;
  503. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  504. begin
  505. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  506. SNew:=NewWideString (L);
  507. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  508. PWideRec(SNew-WideFirstOff)^.len:=L;
  509. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  510. Pointer(S):=SNew;
  511. end;
  512. end;
  513. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  514. var
  515. ResultAddress : Pointer;
  516. begin
  517. ResultAddress:=Nil;
  518. dec(index);
  519. if Index < 0 then
  520. Index := 0;
  521. { Check Size. Accounts for Zero-length S, the double check is needed because
  522. Size can be maxint and will get <0 when adding index }
  523. if (Size>Length(S)) or
  524. (Index+Size>Length(S)) then
  525. Size:=Length(S)-Index;
  526. If Size>0 then
  527. begin
  528. If Index<0 Then
  529. Index:=0;
  530. ResultAddress:=Pointer(NewWideString (Size));
  531. if ResultAddress<>Nil then
  532. begin
  533. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  534. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  535. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  536. end;
  537. end;
  538. Pointer(Copy):=ResultAddress;
  539. end;
  540. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  541. var
  542. i,MaxLen : StrLenInt;
  543. pc : pwidechar;
  544. begin
  545. Pos:=0;
  546. if Length(SubStr)>0 then
  547. begin
  548. MaxLen:=Length(source)-Length(SubStr);
  549. i:=0;
  550. pc:=@source[1];
  551. while (i<=MaxLen) do
  552. begin
  553. inc(i);
  554. if (SubStr[1]=pc^) and
  555. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  556. begin
  557. Pos:=i;
  558. exit;
  559. end;
  560. inc(pc);
  561. end;
  562. end;
  563. end;
  564. { Faster version for a widechar alone }
  565. Function Pos (c : WideChar; Const s : WideString) : Longint;
  566. var
  567. i: longint;
  568. pc : pwidechar;
  569. begin
  570. pc:=@s[1];
  571. for i:=1 to length(s) do
  572. begin
  573. if pc^=c then
  574. begin
  575. pos:=i;
  576. exit;
  577. end;
  578. inc(pc);
  579. end;
  580. pos:=0;
  581. end;
  582. { Faster version for a char alone. Must be implemented because }
  583. { pos(c: char; const s: shortstring) also exists, so otherwise }
  584. { using pos(char,pchar) will always call the shortstring version }
  585. { (exact match for first argument), also with $h+ (JM) }
  586. Function Pos (c : Char; Const s : WideString) : Longint;
  587. var
  588. i: longint;
  589. wc : widechar;
  590. pc : pwidechar;
  591. begin
  592. wc:=c;
  593. pc:=@s[1];
  594. for i:=1 to length(s) do
  595. begin
  596. if pc^=wc then
  597. begin
  598. pos:=i;
  599. exit;
  600. end;
  601. inc(pc);
  602. end;
  603. pos:=0;
  604. end;
  605. Procedure Delete (Var S : WideString; Index,Size: Longint);
  606. Var
  607. LS : Longint;
  608. begin
  609. If Length(S)=0 then
  610. exit;
  611. if index<=0 then
  612. begin
  613. inc(Size,index-1);
  614. index:=1;
  615. end;
  616. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  617. if (Index<=LS) and (Size>0) then
  618. begin
  619. UniqueString (S);
  620. if Size+Index>LS then
  621. Size:=LS-Index+1;
  622. if Index+Size<=LS then
  623. begin
  624. Dec(Index);
  625. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  626. end;
  627. Setlength(s,LS-Size);
  628. end;
  629. end;
  630. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  631. var
  632. Temp : WideString;
  633. LS : Longint;
  634. begin
  635. If Length(Source)=0 then
  636. exit;
  637. if index <= 0 then
  638. index := 1;
  639. Ls:=Length(S);
  640. if index > LS then
  641. index := LS+1;
  642. Dec(Index);
  643. Pointer(Temp) := NewWideString(Length(Source)+LS);
  644. SetLength(Temp,Length(Source)+LS);
  645. If Index>0 then
  646. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  647. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  648. If (LS-Index)>0 then
  649. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  650. S:=Temp;
  651. end;
  652. {!!!:Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  653. begin
  654. SetLength(S,Len);
  655. Move (Buf[0],S[1],Len*2);
  656. end;}
  657. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  658. Var
  659. SS : String;
  660. begin
  661. fpc_Val_Real_WideStr := 0;
  662. if length(S) > 255 then
  663. code := 256
  664. else
  665. begin
  666. SS := S;
  667. Val(SS,fpc_Val_Real_WideStr,code);
  668. end;
  669. end;
  670. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  671. Var
  672. SS : ShortString;
  673. begin
  674. fpc_Val_UInt_WideStr := 0;
  675. if length(S) > 255 then
  676. code := 256
  677. else
  678. begin
  679. SS := S;
  680. Val(SS,fpc_Val_UInt_WideStr,code);
  681. end;
  682. end;
  683. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  684. Var
  685. SS : ShortString;
  686. begin
  687. fpc_Val_SInt_WideStr:=0;
  688. if length(S)>255 then
  689. code:=256
  690. else
  691. begin
  692. SS := S;
  693. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  694. end;
  695. end;
  696. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  697. Var
  698. SS : ShortString;
  699. begin
  700. fpc_Val_qword_WideStr:=0;
  701. if length(S)>255 then
  702. code:=256
  703. else
  704. begin
  705. SS := S;
  706. Val(SS,fpc_Val_qword_WideStr,Code);
  707. end;
  708. end;
  709. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  710. Var
  711. SS : ShortString;
  712. begin
  713. fpc_Val_int64_WideStr:=0;
  714. if length(S)>255 then
  715. code:=256
  716. else
  717. begin
  718. SS := S;
  719. Val(SS,fpc_Val_int64_WideStr,Code);
  720. end;
  721. end;
  722. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  723. var
  724. ss : shortstring;
  725. begin
  726. str_real(len,fr,d,treal_type(rt),ss);
  727. s:=ss;
  728. end;
  729. Procedure fpc_WideStr_Cardinal(C : Cardinal;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  730. Var
  731. SS : ShortString;
  732. begin
  733. str(C:Len,SS);
  734. S:=SS;
  735. end;
  736. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  737. Var
  738. SS : ShortString;
  739. begin
  740. Str (L:Len,SS);
  741. S:=SS;
  742. end;
  743. {
  744. $Log$
  745. Revision 1.15 2001-08-30 15:43:15 jonas
  746. * converted adding/comparing of strings to compileproc. Note that due
  747. to the way the shortstring helpers for i386 are written, they are
  748. still handled by the old code (reason: fpc_shortstr_compare returns
  749. results in the flags instead of in eax and fpc_shortstr_concat
  750. has wierd parameter conventions). The compilerproc stuff should work
  751. fine with the generic implementations though.
  752. * removed some nested comments warnings
  753. Revision 1.14 2001/08/29 19:49:04 jonas
  754. * some fixes in compilerprocs for chararray to string conversions
  755. * conversion from string to chararray is now also done via compilerprocs
  756. Revision 1.13 2001/08/28 13:24:47 jonas
  757. + compilerproc implementation of most string-related type conversions
  758. - removed all code from the compiler which has been replaced by
  759. compilerproc implementations (using (ifdef hascompilerproc) is not
  760. necessary in the compiler)
  761. Revision 1.12 2001/08/13 12:40:16 jonas
  762. * renamed some str(x,y) and val(x,y) helpers so the naming scheme is the
  763. same for all string types
  764. + added the str(x,y) and val(x,y,z) helpers for int64/qword to
  765. compproc.inc
  766. Revision 1.11 2001/08/01 15:00:11 jonas
  767. + "compproc" helpers
  768. * renamed several helpers so that their name is the same as their
  769. "public alias", which should facilitate the conversion of processor
  770. specific code in the code generator to processor independent code
  771. * some small fixes to the val_ansistring and val_widestring helpers
  772. (always immediately exit if the source string is longer than 255
  773. chars)
  774. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  775. still nil (used to crash, now return resp -1 and 0)
  776. Revision 1.10 2001/07/16 12:33:08 jonas
  777. * fixed wrong public alieases for val(widestring,...)
  778. Revision 1.9 2001/07/09 21:15:41 peter
  779. * Length made internal
  780. * Add array support for Length
  781. Revision 1.8 2001/07/08 21:00:18 peter
  782. * various widestring updates, it works now mostly without charset
  783. mapping supported
  784. Revision 1.7 2001/05/27 14:28:03 florian
  785. + some procedures added
  786. Revision 1.6 2000/11/06 23:17:15 peter
  787. * removed some warnings
  788. Revision 1.5 2000/11/06 20:34:24 peter
  789. * changed ver1_0 defines to temporary defs
  790. Revision 1.4 2000/10/21 18:20:17 florian
  791. * a lot of small changes:
  792. - setlength is internal
  793. - win32 graph unit extended
  794. ....
  795. Revision 1.3 2000/08/08 22:12:36 sg
  796. * Implemented WideString helper functions (but they are not tested yet
  797. due to the lack of full compiler support for WideString/WideChar!)
  798. Revision 1.2 2000/07/13 11:33:46 michael
  799. + removed logs
  800. }