astrings.inc 34 KB

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