astrings.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. This file implements AnsiStrings for 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. { This will release some functions for special shortstring support }
  13. { define EXTRAANSISHORT}
  14. {$ifndef FPC_ANSISTRING_TYPE_DEFINED}
  15. {
  16. This file contains the implementation of the AnsiString type,
  17. and all things that are needed for it.
  18. AnsiString is defined as a 'silent' pchar :
  19. a pchar that points to :
  20. @-8 : SizeInt for reference count;
  21. @-4 : SizeInt for size;
  22. @ : String + Terminating #0;
  23. Pchar(Ansistring) is a valid typecast.
  24. So AS[i] is converted to the address @AS+i-1.
  25. Constants should be assigned a reference count of -1
  26. Meaning that they can't be disposed of.
  27. }
  28. Type
  29. PAnsiRec = ^TAnsiRec;
  30. TAnsiRec = Packed Record
  31. Ref,
  32. Len : SizeInt;
  33. First : Char;
  34. end;
  35. Const
  36. AnsiRecLen = SizeOf(TAnsiRec);
  37. FirstOff = SizeOf(TAnsiRec)-1;
  38. {$define FPC_ANSISTRING_TYPE_DEFINED}
  39. {****************************************************************************
  40. Internal functions, not in interface.
  41. ****************************************************************************}
  42. {$ifndef FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  43. {$define FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  44. procedure fpc_pchar_ansistr_intern_charmove(const src: pchar; const srcindex: sizeint; var dst: ansistring; const dstindex, len: sizeint); {$ifdef SYSTEMINLINE}inline;{$endif}
  45. begin
  46. move(src[srcindex],pbyte(pointer(dst))[dstindex],len);
  47. end;
  48. {$endif FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  49. {$ifndef FPC_HAS_NEWANSISTR}
  50. {$endif FPC_HAS_NEWANSISTR}
  51. Function NewAnsiString(Len : SizeInt) : Pointer;
  52. {
  53. Allocate a new AnsiString on the heap.
  54. initialize it to zero length and reference count 1.
  55. }
  56. Var
  57. P : Pointer;
  58. begin
  59. { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
  60. GetMem(P,Len+AnsiRecLen);
  61. If P<>Nil then
  62. begin
  63. PAnsiRec(P)^.Ref:=1; { Set reference count }
  64. PAnsiRec(P)^.Len:=0; { Initial length }
  65. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  66. inc(p,firstoff); { Points to string now }
  67. end;
  68. NewAnsiString:=P;
  69. end;
  70. {$endif FPC_HAS_NEWANSISTR}
  71. {$ifndef FPC_HAS_DISPOSE_ANSISTR}
  72. {$define FPC_HAS_DISPOSE_ANSISTR}
  73. Procedure DisposeAnsiString(Var S : Pointer); {$IFNDEF VER2_0} Inline; {$ENDIF}
  74. {
  75. Deallocates a AnsiString From the heap.
  76. }
  77. begin
  78. If S=Nil then
  79. exit;
  80. Dec (S,FirstOff);
  81. FreeMem (S);
  82. S:=Nil;
  83. end;
  84. {$endif FPC_HAS_DISPOSE_ANSISTR}
  85. {$ifndef FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  86. {$define FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  87. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF']; compilerproc;
  88. {
  89. Decreases the ReferenceCount of a non constant ansistring;
  90. If the reference count is zero, deallocate the string;
  91. }
  92. Type
  93. pSizeInt = ^SizeInt;
  94. Var
  95. l : pSizeInt;
  96. Begin
  97. { Zero string }
  98. If S=Nil then exit;
  99. { check for constant strings ...}
  100. l:=@PAnsiRec(S-FirstOff)^.Ref;
  101. If l^<0 then exit;
  102. { declocked does a MT safe dec and returns true, if the counter is 0 }
  103. If declocked(l^) then
  104. { Ref count dropped to zero }
  105. DisposeAnsiString (S); { Remove...}
  106. end;
  107. {$endif FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  108. { also define alias for internal use in the system unit }
  109. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [external name 'FPC_ANSISTR_DECR_REF'];
  110. {$ifndef FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  111. {$define FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  112. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  113. Begin
  114. If S=Nil then
  115. exit;
  116. { Let's be paranoid : Constant string ??}
  117. If PAnsiRec(S-FirstOff)^.Ref<0 then exit;
  118. inclocked(PAnsiRec(S-FirstOff)^.Ref);
  119. end;
  120. {$endif FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  121. { also define alias which can be used inside the system unit }
  122. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [external name 'FPC_ANSISTR_INCR_REF'];
  123. {$ifndef FPC_HAS_ANSISTR_ASSIGN}
  124. {$define FPC_HAS_ANSISTR_ASSIGN}
  125. Procedure fpc_AnsiStr_Assign (Var DestS : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; compilerproc;
  126. {
  127. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  128. }
  129. begin
  130. if DestS=S2 then
  131. exit;
  132. If S2<>nil then
  133. If PAnsiRec(S2-FirstOff)^.Ref>0 then
  134. inclocked(PAnsiRec(S2-FirstOff)^.ref);
  135. { Decrease the reference count on the old S1 }
  136. fpc_ansistr_decr_ref (DestS);
  137. { And finally, have DestS pointing to S2 (or its copy) }
  138. DestS:=S2;
  139. end;
  140. {$endif FPC_HAS_ANSISTR_ASSIGN}
  141. { alias for internal use }
  142. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  143. {$ifndef STR_CONCAT_PROCS}
  144. {$ifndef FPC_HAS_ANSISTR_CONCAT}
  145. {$define FPC_HAS_ANSISTR_CONCAT}
  146. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  147. Var
  148. Size,Location : SizeInt;
  149. pc : pchar;
  150. begin
  151. { only assign if s1 or s2 is empty }
  152. if (S1='') then
  153. begin
  154. result:=s2;
  155. exit;
  156. end;
  157. if (S2='') then
  158. begin
  159. result:=s1;
  160. exit;
  161. end;
  162. Location:=Length(S1);
  163. Size:=length(S2);
  164. SetLength(result,Size+Location);
  165. pc:=pchar(result);
  166. Move(S1[1],pc^,Location);
  167. inc(pc,location);
  168. Move(S2[1],pc^,Size+1);
  169. end;
  170. {$endif FPC_HAS_ANSISTR_CONCAT}
  171. {$ifndef FPC_HAS_ANSISTR_CONCAT_MULTI}
  172. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  173. function fpc_AnsiStr_Concat_multi (const sarr:array of Ansistring): ansistring; compilerproc;
  174. Var
  175. i : Longint;
  176. p : pointer;
  177. pc : pchar;
  178. Size,NewLen : SizeInt;
  179. begin
  180. { First calculate size of the result so we can do
  181. a single call to SetLength() }
  182. NewLen:=0;
  183. for i:=low(sarr) to high(sarr) do
  184. inc(NewLen,length(sarr[i]));
  185. SetLength(result,NewLen);
  186. pc:=pchar(result);
  187. for i:=low(sarr) to high(sarr) do
  188. begin
  189. p:=pointer(sarr[i]);
  190. if assigned(p) then
  191. begin
  192. Size:=length(ansistring(p));
  193. Move(pchar(p)^,pc^,Size+1);
  194. inc(pc,size);
  195. end;
  196. end;
  197. end;
  198. {$endif FPC_HAS_ANSISTR_CONCAT_MULTI}
  199. {$else STR_CONCAT_PROCS}
  200. {$ifndef FPC_HAS_ANSISTR_CONCAT}
  201. {$define FPC_HAS_ANSISTR_CONCAT}
  202. procedure fpc_AnsiStr_Concat (var DestS:ansistring;const S1,S2 : AnsiString); compilerproc;
  203. Var
  204. Size,Location : SizeInt;
  205. same : boolean;
  206. begin
  207. { only assign if s1 or s2 is empty }
  208. if (S1='') then
  209. begin
  210. DestS:=s2;
  211. exit;
  212. end;
  213. if (S2='') then
  214. begin
  215. DestS:=s1;
  216. exit;
  217. end;
  218. Location:=Length(S1);
  219. Size:=length(S2);
  220. { Use Pointer() typecasts to prevent extra conversion code }
  221. if Pointer(DestS)=Pointer(S1) then
  222. begin
  223. same:=Pointer(S1)=Pointer(S2);
  224. SetLength(DestS,Size+Location);
  225. if same then
  226. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size)
  227. else
  228. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  229. end
  230. else if Pointer(DestS)=Pointer(S2) then
  231. begin
  232. SetLength(DestS,Size+Location);
  233. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size+1);
  234. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  235. end
  236. else
  237. begin
  238. DestS:='';
  239. SetLength(DestS,Size+Location);
  240. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  241. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  242. end;
  243. end;
  244. {$endif FPC_HAS_ANSISTR_CONCAT}
  245. {$ifndef FPC_HAS_ANSISTR_CONCAT_MULTI}
  246. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  247. procedure fpc_AnsiStr_Concat_multi (var DestS:ansistring;const sarr:array of Ansistring); compilerproc;
  248. Var
  249. lowstart,i : Longint;
  250. p,pc : pointer;
  251. Size,NewLen,
  252. OldDestLen : SizeInt;
  253. destcopy : pointer;
  254. begin
  255. if high(sarr)=0 then
  256. begin
  257. DestS:='';
  258. exit;
  259. end;
  260. destcopy:=nil;
  261. lowstart:=low(sarr);
  262. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  263. inc(lowstart);
  264. { Check for another reuse, then we can't use
  265. the append optimization }
  266. for i:=lowstart to high(sarr) do
  267. begin
  268. if Pointer(DestS)=Pointer(sarr[i]) then
  269. begin
  270. { if DestS is used somewhere in the middle of the expression,
  271. we need to make sure the original string still exists after
  272. we empty/modify DestS }
  273. destcopy:=pointer(dests);
  274. fpc_AnsiStr_Incr_Ref(destcopy);
  275. lowstart:=low(sarr);
  276. break;
  277. end;
  278. end;
  279. { Start with empty DestS if we start with concatting
  280. the first array element }
  281. if lowstart=low(sarr) then
  282. DestS:='';
  283. OldDestLen:=length(DestS);
  284. { Calculate size of the result so we can do
  285. a single call to SetLength() }
  286. NewLen:=0;
  287. for i:=low(sarr) to high(sarr) do
  288. inc(NewLen,length(sarr[i]));
  289. SetLength(DestS,NewLen);
  290. { Concat all strings, except the string we already
  291. copied in DestS }
  292. pc:=Pointer(DestS)+OldDestLen;
  293. for i:=lowstart to high(sarr) do
  294. begin
  295. p:=pointer(sarr[i]);
  296. if assigned(p) then
  297. begin
  298. Size:=length(ansistring(p));
  299. Move(p^,pc^,Size+1);
  300. inc(pc,size);
  301. end;
  302. end;
  303. fpc_AnsiStr_Decr_Ref(destcopy);
  304. end;
  305. {$endif FPC_HAS_ANSISTR_CONCAT_MULTI}
  306. {$endif STR_CONCAT_PROCS}
  307. {$ifdef EXTRAANSISHORT}
  308. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  309. {
  310. Concatenates a Ansi with a short string; : S2 + S2
  311. }
  312. Var
  313. Size,Location : SizeInt;
  314. begin
  315. Size:=Length(S2);
  316. Location:=Length(S1);
  317. If Size=0 then
  318. exit;
  319. { Setlength takes case of uniqueness
  320. and alllocated memory. We need to use length,
  321. to take into account possibility of S1=Nil }
  322. SetLength (S1,Size+Length(S1));
  323. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  324. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  325. end;
  326. {$endif EXTRAANSISHORT}
  327. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  328. {$ifndef FPC_HAS_ANSISTR_TO_SHORTSTR}
  329. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  330. { the following declaration has exactly the same effect as }
  331. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  332. { which is what the old helper was, so we don't need an extra implementation }
  333. { of the old helper (JM) }
  334. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  335. {
  336. Converts a AnsiString to a ShortString;
  337. }
  338. Var
  339. Size : SizeInt;
  340. begin
  341. if S2='' then
  342. fpc_AnsiStr_To_ShortStr:=''
  343. else
  344. begin
  345. Size:=Length(S2);
  346. If Size>high_of_res then
  347. Size:=high_of_res;
  348. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  349. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  350. end;
  351. end;
  352. {$endif FPC_HAS_ANSISTR_TO_SHORTSTR}
  353. {$else FPC_STRTOSHORTSTRINGPROC}
  354. {$ifndef FPC_HAS_ANSISTR_TO_SHORTSTR}
  355. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  356. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : Ansistring);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  357. {
  358. Converts a AnsiString to a ShortString;
  359. }
  360. Var
  361. Size : SizeInt;
  362. begin
  363. if S2='' then
  364. res:=''
  365. else
  366. begin
  367. Size:=Length(S2);
  368. If Size>high(res) then
  369. Size:=high(res);
  370. Move (S2[1],res[1],Size);
  371. byte(res[0]):=byte(Size);
  372. end;
  373. end;
  374. {$endif FPC_HAS_ANSISTR_TO_SHORTSTR}
  375. {$endif FPC_STRTOSHORTSTRINGPROC}
  376. {$ifndef FPC_HAS_SHORTSTR_TO_ANSISTR}
  377. {$define FPC_HAS_SHORTSTR_TO_ANSISTR}
  378. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  379. {
  380. Converts a ShortString to a AnsiString;
  381. }
  382. Var
  383. Size : SizeInt;
  384. begin
  385. Size:=Length(S2);
  386. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  387. if Size>0 then
  388. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  389. end;
  390. {$endif FPC_HAS_SHORTSTR_TO_ANSISTR}
  391. {$ifndef FPC_HAS_CHAR_TO_ANSISTR}
  392. {$define FPC_HAS_CHAR_TO_ANSISTR}
  393. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  394. {
  395. Converts a Char to a AnsiString;
  396. }
  397. begin
  398. Setlength (fpc_Char_To_AnsiStr,1);
  399. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  400. { Terminating Zero }
  401. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  402. end;
  403. {$endif FPC_HAS_CHAR_TO_ANSISTR}
  404. {$ifndef FPC_HAS_PCHAR_TO_ANSISTR}
  405. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  406. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  407. Var
  408. L : SizeInt;
  409. begin
  410. if (not assigned(p)) or (p[0]=#0) Then
  411. L := 0
  412. else
  413. l:=IndexChar(p^,-1,#0);
  414. SetLength(fpc_PChar_To_AnsiStr,L);
  415. if L > 0 then
  416. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  417. end;
  418. {$endif FPC_HAS_PCHAR_TO_ANSISTR}
  419. {$ifndef FPC_HAS_CHARARRAY_TO_ANSISTR}
  420. {$define FPC_HAS_CHARARRAY_TO_ANSISTR}
  421. Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  422. var
  423. i : SizeInt;
  424. begin
  425. if (zerobased) then
  426. begin
  427. if (arr[0]=#0) Then
  428. i := 0
  429. else
  430. begin
  431. i:=IndexChar(arr,high(arr)+1,#0);
  432. if i = -1 then
  433. i := high(arr)+1;
  434. end;
  435. end
  436. else
  437. i := high(arr)+1;
  438. SetLength(fpc_CharArray_To_AnsiStr,i);
  439. if i > 0 then
  440. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  441. end;
  442. {$endif FPC_HAS_CHARARRAY_TO_ANSISTR}
  443. {$ifndef FPC_STRTOCHARARRAYPROC}
  444. {$ifndef FPC_HAS_ANSISTR_TO_CHARARRAY}
  445. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  446. { note: inside the compiler, the resulttype is modified to be the length }
  447. { of the actual chararray to which we convert (JM) }
  448. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  449. var
  450. len: SizeInt;
  451. begin
  452. len := length(src);
  453. if len > arraysize then
  454. len := arraysize;
  455. {$r-}
  456. { make sure we don't try to access element 1 of the ansistring if it's nil }
  457. if len > 0 then
  458. move(src[1],fpc_ansistr_to_chararray[0],len);
  459. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  460. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  461. {$ifdef RangeCheckWasOn}
  462. {$r+}
  463. {$endif}
  464. end;
  465. {$endif FPC_HAS_ANSISTR_TO_CHARARRAY}
  466. {$else ndef FPC_STRTOCHARARRAYPROC}
  467. {$ifndef FPC_HAS_ANSISTR_TO_CHARARRAY}
  468. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  469. procedure fpc_ansistr_to_chararray(out res: array of char; const src: ansistring); compilerproc;
  470. var
  471. len: SizeInt;
  472. begin
  473. len := length(src);
  474. if len > length(res) then
  475. len := length(res);
  476. {$r-}
  477. { make sure we don't try to access element 1 of the ansistring if it's nil }
  478. if len > 0 then
  479. move(src[1],res[0],len);
  480. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  481. fillchar(res[len],length(res)-len,0);
  482. {$ifdef RangeCheckWasOn}
  483. {$r+}
  484. {$endif}
  485. end;
  486. {$endif FPC_HAS_ANSISTR_TO_CHARARRAY}
  487. {$endif ndef FPC_STRTOCHARARRAYPROC}
  488. {$ifndef FPC_HAS_ANSISTR_COMPARE}
  489. {$define FPC_HAS_ANSISTR_COMPARE}
  490. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  491. {
  492. Compares 2 AnsiStrings;
  493. The result is
  494. <0 if S1<S2
  495. 0 if S1=S2
  496. >0 if S1>S2
  497. }
  498. Var
  499. MaxI,Temp : SizeInt;
  500. begin
  501. if pointer(S1)=pointer(S2) then
  502. begin
  503. result:=0;
  504. exit;
  505. end;
  506. Maxi:=Length(S1);
  507. temp:=Length(S2);
  508. If MaxI>Temp then
  509. MaxI:=Temp;
  510. if MaxI>0 then
  511. begin
  512. result:=CompareByte(S1[1],S2[1],MaxI);
  513. if result=0 then
  514. result:=Length(S1)-Length(S2);
  515. end
  516. else
  517. result:=Length(S1)-Length(S2);
  518. end;
  519. {$endif FPC_HAS_ANSISTR_COMPARE}
  520. {$ifndef FPC_HAS_ANSISTR_COMPARE_EQUAL}
  521. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  522. Function fpc_AnsiStr_Compare_equal(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  523. {
  524. Compares 2 AnsiStrings for equality/inequality only;
  525. The result is
  526. 0 if S1=S2
  527. <>0 if S1<>S2
  528. }
  529. Var
  530. MaxI,Temp : SizeInt;
  531. begin
  532. if pointer(S1)=pointer(S2) then
  533. begin
  534. result:=0;
  535. exit;
  536. end;
  537. Maxi:=Length(S1);
  538. temp:=Length(S2);
  539. Result := Maxi - temp;
  540. if Result = 0 then
  541. if MaxI>0 then
  542. result:=CompareByte(S1[1],S2[1],MaxI);
  543. end;
  544. {$endif FPC_HAS_ANSISTR_COMPARE_EQUAL}
  545. {$ifdef VER2_4}
  546. // obsolete but needed for boostrapping with 2.4
  547. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  548. begin
  549. if p=nil then
  550. HandleErrorFrame(201,get_frame);
  551. end;
  552. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  553. begin
  554. if (index>len) or (Index<1) then
  555. HandleErrorFrame(201,get_frame);
  556. end;
  557. {$else VER2_4}
  558. {$ifndef FPC_HAS_ANSISTR_CHECKRANGE}
  559. {$define FPC_HAS_ANSISTR_CHECKRANGE}
  560. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  561. begin
  562. if (p=nil) or (index>PAnsiRec(p-FirstOff)^.Len) or (Index<1) then
  563. HandleErrorFrame(201,get_frame);
  564. end;
  565. {$endif FPC_HAS_ANSISTR_CHECKRANGE}
  566. {$endif VER2_4}
  567. {$ifndef FPC_HAS_ANSISTR_SETLENGTH}
  568. {$define FPC_HAS_ANSISTR_SETLENGTH}
  569. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  570. {
  571. Sets The length of string S to L.
  572. Makes sure S is unique, and contains enough room.
  573. }
  574. Var
  575. Temp : Pointer;
  576. lens, lena,
  577. movelen : SizeInt;
  578. begin
  579. if (l>0) then
  580. begin
  581. if Pointer(S)=nil then
  582. begin
  583. GetMem(Pointer(S),AnsiRecLen+L);
  584. PAnsiRec(S)^.Ref:=1;
  585. inc(Pointer(S),firstoff);
  586. end
  587. else if PAnsiRec(Pointer(S)-FirstOff)^.Ref=1 then
  588. begin
  589. Dec(Pointer(S),FirstOff);
  590. lens:=MemSize(Pointer(s));
  591. lena:=AnsiRecLen+L;
  592. { allow shrinking string if that saves at least half of current size }
  593. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  594. reallocmem(pointer(S),AnsiRecLen+L);
  595. Inc(Pointer(S),FirstOff);
  596. end
  597. else
  598. begin
  599. { Reallocation is needed... }
  600. Temp:=Pointer(NewAnsiString(L));
  601. { also move terminating null }
  602. lens:=succ(length(s));
  603. if l < lens then
  604. movelen := l
  605. else
  606. movelen := lens;
  607. Move(Pointer(S)^,Temp^,movelen);
  608. { ref count dropped to zero in the mean time? }
  609. If (PAnsiRec(Pointer(S)-FirstOff)^.Ref > 0) and
  610. declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  611. freemem(PAnsiRec(Pointer(s)-FirstOff));
  612. Pointer(S):=Temp;
  613. end;
  614. { Force nil termination in case it gets shorter }
  615. PByte(Pointer(S)+l)^:=0;
  616. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  617. end
  618. else
  619. begin
  620. { Length=0 }
  621. if Pointer(S)<>nil then
  622. fpc_ansistr_decr_ref (Pointer(S));
  623. Pointer(S):=Nil;
  624. end;
  625. end;
  626. {$endif FPC_HAS_ANSISTR_SETLENGTH}
  627. {$ifdef EXTRAANSISHORT}
  628. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  629. {
  630. Compares a AnsiString with a ShortString;
  631. The result is
  632. <0 if S1<S2
  633. 0 if S1=S2
  634. >0 if S1>S2
  635. }
  636. Var
  637. i,MaxI,Temp : SizeInt;
  638. begin
  639. Temp:=0;
  640. i:=0;
  641. MaxI:=Length(AnsiString(S1));
  642. if MaxI>byte(S2[0]) then
  643. MaxI:=Byte(S2[0]);
  644. While (i<MaxI) and (Temp=0) do
  645. begin
  646. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  647. inc(i);
  648. end;
  649. AnsiStr_ShortStr_Compare:=Temp;
  650. end;
  651. {$endif EXTRAANSISHORT}
  652. {*****************************************************************************
  653. Public functions, In interface.
  654. *****************************************************************************}
  655. {$ifndef FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  656. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  657. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  658. Var
  659. SNew : Pointer;
  660. L : SizeInt;
  661. begin
  662. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  663. SNew:=NewAnsiString (L);
  664. Move (Pointer(S)^,SNew^,L+1);
  665. PAnsiRec(SNew-FirstOff)^.len:=L;
  666. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  667. pointer(S):=SNew;
  668. pointer(result):=SNew;
  669. end;
  670. {$endif FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  671. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  672. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  673. // MV: inline the basic checks for case that S is already unique.
  674. // Rest is too complex to inline, so factor that out as a call.
  675. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  676. {
  677. Make sure reference count of S is 1,
  678. using copy-on-write semantics.
  679. }
  680. begin
  681. pointer(result) := pointer(s);
  682. If Pointer(S)=Nil then
  683. exit;
  684. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  685. result:=fpc_truely_ansistr_unique(s);
  686. end;
  687. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  688. {$ifndef FPC_HAS_ANSISTR_APPEND_CHAR}
  689. {$define FPC_HAS_ANSISTR_APPEND_CHAR}
  690. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  691. begin
  692. SetLength(S,length(S)+1);
  693. // avoid unique call
  694. PChar(Pointer(S)+length(S)-1)^:=c;
  695. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  696. end;
  697. {$endif FPC_HAS_ANSISTR_APPEND_CHAR}
  698. {$ifndef FPC_HAS_ANSISTR_APPEND_SHORTSTR}
  699. {$define FPC_HAS_ANSISTR_APPEND_SHORTSTR}
  700. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  701. var
  702. ofs : SizeInt;
  703. begin
  704. if Str='' then
  705. exit;
  706. ofs:=Length(S);
  707. SetLength(S,ofs+length(Str));
  708. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  709. move(Str[1],(pointer(S)+ofs)^,length(Str));
  710. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  711. end;
  712. {$endif FPC_HAS_ANSISTR_APPEND_SHORTSTR}
  713. {$ifndef FPC_HAS_ANSISTR_APPEND_ANSISTR}
  714. {$define FPC_HAS_ANSISTR_APPEND_ANSISTR}
  715. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  716. var
  717. ofs, strlength: SizeInt;
  718. samestring: boolean;
  719. begin
  720. if Str='' then
  721. exit;
  722. samestring := pointer(s) = pointer(str);
  723. { needed in case s and str are the same string }
  724. strlength := length(str);
  725. ofs:=Length(S);
  726. SetLength(S,ofs+strlength);
  727. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  728. if not(samestring) then
  729. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  730. else
  731. { the setlength may have relocated the string, so str may no longer be valid }
  732. move(S[1],(pointer(S)+ofs)^,strlength+1)
  733. end;
  734. {$endif FPC_HAS_ANSISTR_APPEND_ANSISTR}
  735. {$ifndef FPC_HAS_ANSISTR_COPY}
  736. {$define FPC_HAS_ANSISTR_COPY}
  737. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  738. var
  739. ResultAddress : Pointer;
  740. begin
  741. ResultAddress:=Nil;
  742. dec(index);
  743. if Index < 0 then
  744. Index := 0;
  745. { Check Size. Accounts for Zero-length S, the double check is needed because
  746. Size can be maxint and will get <0 when adding index }
  747. if (Size>Length(S)) or
  748. (Index+Size>Length(S)) then
  749. Size:=Length(S)-Index;
  750. If Size>0 then
  751. begin
  752. If Index<0 Then
  753. Index:=0;
  754. ResultAddress:=Pointer(NewAnsiString (Size));
  755. if ResultAddress<>Nil then
  756. begin
  757. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  758. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  759. PByte(ResultAddress+Size)^:=0;
  760. end;
  761. end;
  762. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  763. Pointer(fpc_ansistr_Copy):=ResultAddress;
  764. end;
  765. {$endif FPC_HAS_ANSISTR_COPY}
  766. {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
  767. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  768. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  769. var
  770. i,MaxLen : SizeInt;
  771. pc : pchar;
  772. begin
  773. Pos:=0;
  774. if Length(SubStr)>0 then
  775. begin
  776. MaxLen:=Length(source)-Length(SubStr);
  777. i:=0;
  778. pc:=@source[1];
  779. while (i<=MaxLen) do
  780. begin
  781. inc(i);
  782. if (SubStr[1]=pc^) and
  783. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  784. begin
  785. Pos:=i;
  786. exit;
  787. end;
  788. inc(pc);
  789. end;
  790. end;
  791. end;
  792. {$endif FPC_HAS_POS_SHORTSTR_ANSISTR}
  793. {$ifndef FPC_HAS_POS_ANSISTR_ANSISTR}
  794. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  795. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  796. var
  797. i,MaxLen : SizeInt;
  798. pc : pchar;
  799. begin
  800. Pos:=0;
  801. if Length(SubStr)>0 then
  802. begin
  803. MaxLen:=Length(source)-Length(SubStr);
  804. i:=0;
  805. pc:=@source[1];
  806. while (i<=MaxLen) do
  807. begin
  808. inc(i);
  809. if (SubStr[1]=pc^) and
  810. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  811. begin
  812. Pos:=i;
  813. exit;
  814. end;
  815. inc(pc);
  816. end;
  817. end;
  818. end;
  819. {$endif FPC_HAS_POS_ANSISTR_ANSISTR}
  820. {$ifndef FPC_HAS_POS_ANSICHAR_ANSISTR}
  821. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  822. { Faster version for a char alone. Must be implemented because }
  823. { pos(c: char; const s: shortstring) also exists, so otherwise }
  824. { using pos(char,pchar) will always call the shortstring version }
  825. { (exact match for first argument), also with $h+ (JM) }
  826. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  827. var
  828. i: SizeInt;
  829. pc : pchar;
  830. begin
  831. pc:=@s[1];
  832. for i:=1 to length(s) do
  833. begin
  834. if pc^=c then
  835. begin
  836. pos:=i;
  837. exit;
  838. end;
  839. inc(pc);
  840. end;
  841. pos:=0;
  842. end;
  843. {$endif FPC_HAS_POS_ANSICHAR_ANSISTR}
  844. {$ifndef FPUNONE}
  845. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  846. Var
  847. SS : String;
  848. begin
  849. fpc_Val_Real_AnsiStr := 0;
  850. if length(S) > 255 then
  851. code := 256
  852. else
  853. begin
  854. SS := S;
  855. Val(SS,fpc_Val_Real_AnsiStr,code);
  856. end;
  857. end;
  858. {$endif}
  859. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  860. Var
  861. SS : String;
  862. begin
  863. if length(S) > 255 then
  864. begin
  865. fpc_Val_Currency_AnsiStr := 0;
  866. code := 256;
  867. end
  868. else
  869. begin
  870. SS := S;
  871. Val(SS,fpc_Val_Currency_AnsiStr,code);
  872. end;
  873. end;
  874. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  875. Var
  876. SS : ShortString;
  877. begin
  878. fpc_Val_UInt_AnsiStr := 0;
  879. if length(S) > 255 then
  880. code := 256
  881. else
  882. begin
  883. SS := S;
  884. Val(SS,fpc_Val_UInt_AnsiStr,code);
  885. end;
  886. end;
  887. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  888. Var
  889. SS : ShortString;
  890. begin
  891. fpc_Val_SInt_AnsiStr:=0;
  892. if length(S)>255 then
  893. code:=256
  894. else
  895. begin
  896. SS := S;
  897. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  898. end;
  899. end;
  900. {$ifndef CPU64}
  901. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  902. Var
  903. SS : ShortString;
  904. begin
  905. fpc_Val_qword_AnsiStr:=0;
  906. if length(S)>255 then
  907. code:=256
  908. else
  909. begin
  910. SS := S;
  911. Val(SS,fpc_Val_qword_AnsiStr,Code);
  912. end;
  913. end;
  914. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  915. Var
  916. SS : ShortString;
  917. begin
  918. fpc_Val_int64_AnsiStr:=0;
  919. if length(S)>255 then
  920. code:=256
  921. else
  922. begin
  923. SS := s;
  924. Val(SS,fpc_Val_int64_AnsiStr,Code);
  925. end;
  926. end;
  927. {$endif CPU64}
  928. {$ifndef FPUNONE}
  929. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  930. var
  931. ss: ShortString;
  932. begin
  933. str_real(len,fr,d,treal_type(rt),ss);
  934. s:=ss;
  935. end;
  936. {$endif}
  937. {$ifndef FPC_STR_ENUM_INTERN}
  938. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  939. var ss:shortstring;
  940. begin
  941. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  942. s:=ss;
  943. end;
  944. {$endif FPC_STR_ENUM_INTERN}
  945. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  946. var
  947. ss:shortstring;
  948. begin
  949. fpc_shortstr_bool(b,len,ss);
  950. s:=ss;
  951. end;
  952. {$ifndef FPC_STR_ENUM_INTERN}
  953. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  954. begin
  955. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  956. end;
  957. {$endif FPC_STR_ENUM_INTERN}
  958. {$ifdef FPC_HAS_STR_CURRENCY}
  959. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  960. var
  961. ss: ShortString;
  962. begin
  963. str(c:len:fr,ss);
  964. s:=ss;
  965. end;
  966. {$endif FPC_HAS_STR_CURRENCY}
  967. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  968. Var
  969. SS : ShortString;
  970. begin
  971. str(v:Len,SS);
  972. S:=SS;
  973. end;
  974. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  975. Var
  976. SS : ShortString;
  977. begin
  978. str (v:Len,SS);
  979. S:=SS;
  980. end;
  981. {$ifndef CPU64}
  982. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  983. Var
  984. SS : ShortString;
  985. begin
  986. str(v:Len,SS);
  987. S:=SS;
  988. end;
  989. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  990. Var
  991. SS : ShortString;
  992. begin
  993. str (v:Len,SS);
  994. S:=SS;
  995. end;
  996. {$endif CPU64}
  997. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  998. Var
  999. LS : SizeInt;
  1000. begin
  1001. ls:=Length(S);
  1002. If (Index>LS) or (Index<=0) or (Size<=0) then
  1003. exit;
  1004. UniqueString (S);
  1005. If (Size>LS-Index) then // Size+Index gives overflow ??
  1006. Size:=LS-Index+1;
  1007. If (Size<=LS-Index) then
  1008. begin
  1009. Dec(Index);
  1010. fpc_pchar_ansistr_intern_charmove(pchar(S),Index+Size,S,Index,LS-Index-Size+1);
  1011. end;
  1012. Setlength(S,LS-Size);
  1013. end;
  1014. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  1015. var
  1016. Temp : AnsiString;
  1017. LS : SizeInt;
  1018. begin
  1019. If Length(Source)=0 then
  1020. exit;
  1021. if index <= 0 then
  1022. index := 1;
  1023. Ls:=Length(S);
  1024. if index > LS then
  1025. index := LS+1;
  1026. Dec(Index);
  1027. SetLength(Temp,Length(Source)+LS);
  1028. If Index>0 then
  1029. fpc_pchar_ansistr_intern_charmove(pchar(S),0,Temp,0,Index);
  1030. fpc_pchar_ansistr_intern_charmove(pchar(Source),0,Temp,Index,Length(Source));
  1031. If (LS-Index)>0 then
  1032. fpc_pchar_ansistr_intern_charmove(pchar(S),Index,Temp,Length(Source)+Index,LS-Index);
  1033. S:=Temp;
  1034. end;
  1035. {$ifndef FPC_HAS_ANSISTR_OF_CHAR}
  1036. {$define FPC_HAS_ANSISTR_OF_CHAR}
  1037. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  1038. begin
  1039. SetLength(StringOfChar,l);
  1040. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1041. end;
  1042. {$endif FPC_HAS_ANSISTR_OF_CHAR}
  1043. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  1044. begin
  1045. SetLength(S,Len);
  1046. If (Buf<>Nil) then
  1047. fpc_pchar_ansistr_intern_charmove(Buf,0,S,0,Len);
  1048. end;
  1049. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1050. begin
  1051. if (Buf<>nil) and (Len>0) then
  1052. widestringmanager.Wide2AnsiMoveProc(Buf,S,Len)
  1053. else
  1054. SetLength(S, Len);
  1055. end;
  1056. {$ifndef FPC_HAS_UPCASE_ANSISTR}
  1057. {$define FPC_HAS_UPCASE_ANSISTR}
  1058. function upcase(const s : ansistring) : ansistring;
  1059. var
  1060. i : SizeInt;
  1061. begin
  1062. Setlength(result,length(s));
  1063. for i := 1 to length (s) do
  1064. result[i] := upcase(s[i]);
  1065. end;
  1066. {$endif FPC_HAS_UPCASE_ANSISTR}
  1067. {$ifndef FPC_HAS_LOWERCASE_ANSISTR}
  1068. {$define FPC_HAS_LOWERCASE_ANSISTR}
  1069. function lowercase(const s : ansistring) : ansistring;
  1070. var
  1071. i : SizeInt;
  1072. begin
  1073. Setlength(result,length(s));
  1074. for i := 1 to length (s) do
  1075. result[i] := lowercase(s[i]);
  1076. end;
  1077. {$endif FPC_HAS_LOWERCASE_ANSISTR}