sstrings.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines for string handling
  12. ****************************************************************************}
  13. procedure fpc_Shortstr_SetLength(var s:shortstring;len:SizeInt);[Public,Alias : 'FPC_SHORTSTR_SETLENGTH']; compilerproc;
  14. begin
  15. if Len>255 then
  16. Len:=255;
  17. s[0]:=chr(len);
  18. end;
  19. function fpc_shortstr_copy(const s : shortstring;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  20. begin
  21. if count<0 then
  22. count:=0;
  23. if index>1 then
  24. dec(index)
  25. else
  26. index:=0;
  27. if index>length(s) then
  28. count:=0
  29. else
  30. if count>length(s)-index then
  31. count:=length(s)-index;
  32. fpc_shortstr_Copy[0]:=chr(Count);
  33. Move(s[Index+1],fpc_shortstr_Copy[1],Count);
  34. end;
  35. procedure delete(var s : shortstring;index : SizeInt;count : SizeInt);
  36. begin
  37. if index<=0 then
  38. exit;
  39. if (Index<=Length(s)) and (Count>0) then
  40. begin
  41. if Count>length(s)-Index then
  42. Count:=length(s)-Index+1;
  43. s[0]:=Chr(length(s)-Count);
  44. if Index<=Length(s) then
  45. Move(s[Index+Count],s[Index],Length(s)-Index+1);
  46. end;
  47. end;
  48. procedure insert(const source : shortstring;var s : shortstring;index : SizeInt);
  49. var
  50. cut,srclen,indexlen : SizeInt;
  51. begin
  52. if index<1 then
  53. index:=1;
  54. if index>length(s) then
  55. index:=length(s)+1;
  56. indexlen:=Length(s)-Index+1;
  57. srclen:=length(Source);
  58. if sizeInt(length(source))+sizeint(length(s))>=sizeof(s) then
  59. begin
  60. cut:=sizeInt(length(source))+sizeint(length(s))-sizeof(s)+1;
  61. if cut>indexlen then
  62. begin
  63. dec(srclen,cut-indexlen);
  64. indexlen:=0;
  65. end
  66. else
  67. dec(indexlen,cut);
  68. end;
  69. move(s[Index],s[Index+srclen],indexlen);
  70. move(Source[1],s[Index],srclen);
  71. s[0]:=chr(index+srclen+indexlen-1);
  72. end;
  73. procedure insert(source : Char;var s : shortstring;index : SizeInt);
  74. var
  75. indexlen : SizeInt;
  76. begin
  77. if index<1 then
  78. index:=1;
  79. if index>length(s) then
  80. index:=length(s)+1;
  81. indexlen:=Length(s)-Index+1;
  82. if (sizeint(length(s))+1=sizeof(s)) and (indexlen>0) then
  83. dec(indexlen);
  84. move(s[Index],s[Index+1],indexlen);
  85. s[Index]:=Source;
  86. s[0]:=chr(index+indexlen);
  87. end;
  88. function pos(const substr : shortstring;const s : shortstring):SizeInt;
  89. var
  90. i,MaxLen : SizeInt;
  91. pc : pchar;
  92. begin
  93. Pos:=0;
  94. if Length(SubStr)>0 then
  95. begin
  96. MaxLen:=sizeint(Length(s))-Length(SubStr);
  97. i:=0;
  98. pc:=@s[1];
  99. while (i<=MaxLen) do
  100. begin
  101. inc(i);
  102. if (SubStr[1]=pc^) and
  103. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  104. begin
  105. Pos:=i;
  106. exit;
  107. end;
  108. inc(pc);
  109. end;
  110. end;
  111. end;
  112. {Faster when looking for a single char...}
  113. function pos(c:char;const s:shortstring):SizeInt;
  114. var
  115. i : SizeInt;
  116. pc : pchar;
  117. begin
  118. pc:=@s[1];
  119. for i:=1 to length(s) do
  120. begin
  121. if pc^=c then
  122. begin
  123. pos:=i;
  124. exit;
  125. end;
  126. inc(pc);
  127. end;
  128. pos:=0;
  129. end;
  130. function fpc_char_copy(c:char;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  131. begin
  132. if (index=1) and (Count>0) then
  133. fpc_char_Copy:=c
  134. else
  135. fpc_char_Copy:='';
  136. end;
  137. function pos(const substr : shortstring;c:char): SizeInt;
  138. begin
  139. if (length(substr)=1) and (substr[1]=c) then
  140. Pos:=1
  141. else
  142. Pos:=0;
  143. end;
  144. {$ifdef IBM_CHAR_SET}
  145. const
  146. UpCaseTbl : shortstring[7]=#154#142#153#144#128#143#165;
  147. LoCaseTbl : shortstring[7]=#129#132#148#130#135#134#164;
  148. {$endif}
  149. function upcase(c : char) : char;
  150. {$IFDEF IBM_CHAR_SET}
  151. var
  152. i : longint;
  153. {$ENDIF}
  154. begin
  155. if (c in ['a'..'z']) then
  156. upcase:=char(byte(c)-32)
  157. else
  158. {$IFDEF IBM_CHAR_SET}
  159. begin
  160. i:=Pos(c,LoCaseTbl);
  161. if i>0 then
  162. upcase:=UpCaseTbl[i]
  163. else
  164. upcase:=c;
  165. end;
  166. {$ELSE}
  167. upcase:=c;
  168. {$ENDIF}
  169. end;
  170. function upcase(const s : shortstring) : shortstring;
  171. var
  172. i : longint;
  173. begin
  174. upcase[0]:=s[0];
  175. for i := 1 to length (s) do
  176. upcase[i] := upcase (s[i]);
  177. end;
  178. function lowercase(c : char) : char;overload;
  179. {$IFDEF IBM_CHAR_SET}
  180. var
  181. i : longint;
  182. {$ENDIF}
  183. begin
  184. if (c in ['A'..'Z']) then
  185. lowercase:=char(byte(c)+32)
  186. else
  187. {$IFDEF IBM_CHAR_SET}
  188. begin
  189. i:=Pos(c,UpCaseTbl);
  190. if i>0 then
  191. lowercase:=LoCaseTbl[i]
  192. else
  193. lowercase:=c;
  194. end;
  195. {$ELSE}
  196. lowercase:=c;
  197. {$ENDIF}
  198. end;
  199. function lowercase(const s : shortstring) : shortstring; overload;
  200. var
  201. i : longint;
  202. begin
  203. lowercase [0]:=s[0];
  204. for i:=1 to length(s) do
  205. lowercase[i]:=lowercase (s[i]);
  206. end;
  207. const
  208. HexTbl : array[0..15] of char='0123456789ABCDEF';
  209. function hexstr(val : longint;cnt : byte) : shortstring;
  210. var
  211. i : longint;
  212. begin
  213. hexstr[0]:=char(cnt);
  214. for i:=cnt downto 1 do
  215. begin
  216. hexstr[i]:=hextbl[val and $f];
  217. val:=val shr 4;
  218. end;
  219. end;
  220. function octstr(val : longint;cnt : byte) : shortstring;
  221. var
  222. i : longint;
  223. begin
  224. octstr[0]:=char(cnt);
  225. for i:=cnt downto 1 do
  226. begin
  227. octstr[i]:=hextbl[val and 7];
  228. val:=val shr 3;
  229. end;
  230. end;
  231. function binstr(val : longint;cnt : byte) : shortstring;
  232. var
  233. i : longint;
  234. begin
  235. binstr[0]:=char(cnt);
  236. for i:=cnt downto 1 do
  237. begin
  238. binstr[i]:=char(48+val and 1);
  239. val:=val shr 1;
  240. end;
  241. end;
  242. function hexstr(val : int64;cnt : byte) : shortstring;
  243. var
  244. i : longint;
  245. begin
  246. hexstr[0]:=char(cnt);
  247. for i:=cnt downto 1 do
  248. begin
  249. hexstr[i]:=hextbl[val and $f];
  250. val:=val shr 4;
  251. end;
  252. end;
  253. function octstr(val : int64;cnt : byte) : shortstring;
  254. var
  255. i : longint;
  256. begin
  257. octstr[0]:=char(cnt);
  258. for i:=cnt downto 1 do
  259. begin
  260. octstr[i]:=hextbl[val and 7];
  261. val:=val shr 3;
  262. end;
  263. end;
  264. function binstr(val : int64;cnt : byte) : shortstring;
  265. var
  266. i : longint;
  267. begin
  268. binstr[0]:=char(cnt);
  269. for i:=cnt downto 1 do
  270. begin
  271. binstr[i]:=char(48+val and 1);
  272. val:=val shr 1;
  273. end;
  274. end;
  275. Function hexStr(Val:qword;cnt:byte):shortstring;
  276. begin
  277. hexStr:=hexStr(int64(Val),cnt);
  278. end;
  279. Function OctStr(Val:qword;cnt:byte):shortstring;
  280. begin
  281. OctStr:=OctStr(int64(Val),cnt);
  282. end;
  283. Function binStr(Val:qword;cnt:byte):shortstring;
  284. begin
  285. binStr:=binStr(int64(Val),cnt);
  286. end;
  287. function hexstr(val : pointer) : shortstring;
  288. var
  289. i : longint;
  290. v : ptruint;
  291. begin
  292. v:=ptruint(val);
  293. hexstr[0]:=chr(sizeof(pointer)*2);
  294. for i:=sizeof(pointer)*2 downto 1 do
  295. begin
  296. hexstr[i]:=hextbl[v and $f];
  297. v:=v shr 4;
  298. end;
  299. end;
  300. function space (b : byte): shortstring;
  301. begin
  302. space[0] := chr(b);
  303. FillChar (Space[1],b,' ');
  304. end;
  305. {*****************************************************************************
  306. Str() Helpers
  307. *****************************************************************************}
  308. procedure fpc_shortstr_SInt(v : valSInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_SINT']; compilerproc;
  309. begin
  310. int_str(v,s);
  311. if length(s)<len then
  312. s:=space(len-length(s))+s;
  313. end;
  314. procedure fpc_shortstr_UInt(v : valUInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_UINT']; compilerproc;
  315. begin
  316. int_str(v,s);
  317. if length(s)<len then
  318. s:=space(len-length(s))+s;
  319. end;
  320. {$ifndef CPU64}
  321. procedure fpc_shortstr_qword(v : qword;len : longint;out s : shortstring);[public,alias:'FPC_SHORTSTR_QWORD']; compilerproc;
  322. begin
  323. int_str(v,s);
  324. if length(s)<len then
  325. s:=space(len-length(s))+s;
  326. end;
  327. procedure fpc_shortstr_int64(v : int64;len : longint;out s : shortstring);[public,alias:'FPC_SHORTSTR_INT64']; compilerproc;
  328. begin
  329. int_str(v,s);
  330. if length(s)<len then
  331. s:=space(len-length(s))+s;
  332. end;
  333. {$endif CPU64}
  334. { fpc_shortstr_sInt must appear before this file is included, because }
  335. { it's used inside real2str.inc and otherwise the searching via the }
  336. { compilerproc name will fail (JM) }
  337. {$I real2str.inc}
  338. procedure fpc_shortstr_float(d : ValReal;len,fr,rt : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT']; compilerproc;
  339. begin
  340. str_real(len,fr,d,treal_type(rt),s);
  341. end;
  342. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_SHORTSTR_ENUM'];compilerproc;
  343. type
  344. Ptypeinfo=^Ttypeinfo;
  345. Ttypeinfo=record
  346. kind:byte;
  347. name:shortstring;
  348. end;
  349. Penuminfo=^Tenuminfo;
  350. Tenuminfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  351. ordtype:byte;
  352. minvalue,maxvalue:longint;
  353. basetype:pointer;
  354. namelist:shortstring;
  355. end;
  356. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  357. o:longint;
  358. s:Pstring;
  359. end;
  360. var
  361. p:Pstring;
  362. l,h,m:cardinal;
  363. sorted_array:^Tsorted_array;
  364. i,spaces:byte;
  365. label
  366. error;
  367. begin
  368. if Pcardinal(ord2strindex)^=0 then
  369. begin
  370. {The compiler did generate a lookup table.}
  371. with Penuminfo(Pbyte(typinfo)+2+length(Ptypeinfo(typinfo)^.name))^ do
  372. begin
  373. if (ordinal<minvalue) or (ordinal>maxvalue) then
  374. goto error; {Invalid ordinal value for this enum.}
  375. dec(ordinal,minvalue);
  376. end;
  377. {Get the address of the string.}
  378. p:=Pshortstring((PPpointer(ord2strindex)+1+ordinal)^);
  379. if p=nil then
  380. goto error; {Invalid ordinal value for this enum.}
  381. s:=p^;
  382. end
  383. else
  384. begin
  385. {The compiler did generate a sorted array of (ordvalue,Pstring) tuples.}
  386. sorted_array:=pointer(Pcardinal(ord2strindex)+2);
  387. {Use a binary search to get the string.}
  388. l:=0;
  389. h:=(Pcardinal(ord2strindex)+1)^-1;
  390. repeat
  391. m:=(l+h) div 2;
  392. if ordinal>sorted_array[m].o then
  393. l:=m+1
  394. else if ordinal<sorted_array[m].o then
  395. h:=m-1
  396. else
  397. break;
  398. if l>h then
  399. goto error; {Ordinal value not found? Kaboom.}
  400. until false;
  401. s:=sorted_array[m].s^;
  402. end;
  403. {Pad the string with spaces if necessary.}
  404. if len>length(s) then
  405. begin
  406. spaces:=len-length(s);
  407. for i:=1 to spaces do
  408. s[length(s)+i]:=' ';
  409. inc(byte(s[0]),spaces);
  410. end;
  411. exit;
  412. error:
  413. {Call runtime error in a central place, this saves space.}
  414. runerror(107);
  415. end;
  416. { also define alias for internal use in the system unit }
  417. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);external name 'FPC_SHORTSTR_ENUM';
  418. procedure fpc_shortstr_currency(c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
  419. const
  420. MinLen = 8; { Minimal string length in scientific format }
  421. var
  422. buf : array[1..19] of char;
  423. i,j,k,reslen,tlen,sign,r,point : longint;
  424. ic : qword;
  425. begin
  426. { default value for length is -32767 }
  427. if len=-32767 then
  428. len:=25;
  429. if PInt64(@c)^ >= 0 then
  430. begin
  431. ic:=QWord(PInt64(@c)^);
  432. sign:=0;
  433. end
  434. else
  435. begin
  436. sign:=1;
  437. ic:=QWord(-PInt64(@c)^);
  438. end;
  439. { converting to integer string }
  440. tlen:=0;
  441. repeat
  442. Inc(tlen);
  443. buf[tlen]:=Chr(ic mod 10 + $30);
  444. ic:=ic div 10;
  445. until ic = 0;
  446. { calculating:
  447. reslen - length of result string,
  448. r - rounding or appending zeroes,
  449. point - place of decimal point }
  450. reslen:=tlen;
  451. if f <> 0 then
  452. Inc(reslen); { adding decimal point length }
  453. if f < 0 then
  454. begin
  455. { scientific format }
  456. Inc(reslen,5); { adding length of sign and exponent }
  457. if len < MinLen then
  458. len:=MinLen;
  459. r:=reslen-len;
  460. if reslen < len then
  461. reslen:=len;
  462. if r > 0 then
  463. begin
  464. reslen:=len;
  465. point:=tlen - r;
  466. end
  467. else
  468. point:=tlen;
  469. end
  470. else
  471. begin
  472. { fixed format }
  473. Inc(reslen, sign);
  474. { prepending fractional part with zeroes }
  475. while tlen < 5 do
  476. begin
  477. Inc(reslen);
  478. Inc(tlen);
  479. buf[tlen]:='0';
  480. end;
  481. { Currency have 4 digits in fractional part }
  482. r:=4 - f;
  483. point:=f;
  484. if point <> 0 then
  485. begin
  486. if point > 4 then
  487. point:=4;
  488. Inc(point);
  489. end;
  490. Dec(reslen,r);
  491. end;
  492. { rounding string if r > 0 }
  493. if r > 0 then
  494. begin
  495. i:=1;
  496. k:=0;
  497. for j:=0 to r do
  498. begin
  499. buf[i]:=chr(ord(buf[i]) + k);
  500. if buf[i] >= '5' then
  501. k:=1
  502. else
  503. k:=0;
  504. Inc(i);
  505. if i>tlen then
  506. break;
  507. end;
  508. end;
  509. { preparing result string }
  510. if reslen<len then
  511. reslen:=len;
  512. if reslen>High(s) then
  513. begin
  514. if r < 0 then
  515. Inc(r, reslen - High(s));
  516. reslen:=High(s);
  517. end;
  518. SetLength(s,reslen);
  519. j:=reslen;
  520. if f<0 then
  521. begin
  522. { writing power of 10 part }
  523. if PInt64(@c)^ = 0 then
  524. k:=0
  525. else
  526. k:=tlen-5;
  527. if k >= 0 then
  528. s[j-2]:='+'
  529. else
  530. begin
  531. s[j-2]:='-';
  532. k:=-k;
  533. end;
  534. s[j]:=Chr(k mod 10 + $30);
  535. Dec(j);
  536. s[j]:=Chr(k div 10 + $30);
  537. Dec(j,2);
  538. s[j]:='E';
  539. Dec(j);
  540. end;
  541. { writing extra zeroes if r < 0 }
  542. while r < 0 do
  543. begin
  544. s[j]:='0';
  545. Dec(j);
  546. Inc(r);
  547. end;
  548. { writing digits and decimal point }
  549. for i:=r + 1 to tlen do
  550. begin
  551. Dec(point);
  552. if point = 0 then
  553. begin
  554. s[j]:='.';
  555. Dec(j);
  556. end;
  557. s[j]:=buf[i];
  558. Dec(j);
  559. end;
  560. { writing sign }
  561. if sign = 1 then
  562. begin
  563. s[j]:='-';
  564. Dec(j);
  565. end;
  566. { writing spaces }
  567. while j > 0 do
  568. begin
  569. s[j]:=' ';
  570. Dec(j);
  571. end;
  572. end;
  573. {
  574. Array Of Char Str() helpers
  575. }
  576. procedure fpc_chararray_sint(v : valsint;len : SizeInt;out a:array of char);compilerproc;
  577. var
  578. ss : shortstring;
  579. maxlen : SizeInt;
  580. begin
  581. int_str(v,ss);
  582. if length(ss)<len then
  583. ss:=space(len-length(ss))+ss;
  584. if length(ss)<high(a)+1 then
  585. maxlen:=length(ss)
  586. else
  587. maxlen:=high(a)+1;
  588. move(ss[1],pchar(@a)^,maxlen);
  589. end;
  590. procedure fpc_chararray_uint(v : valuint;len : SizeInt;out a : array of char);compilerproc;
  591. var
  592. ss : shortstring;
  593. maxlen : SizeInt;
  594. begin
  595. int_str(v,ss);
  596. if length(ss)<len then
  597. ss:=space(len-length(ss))+ss;
  598. if length(ss)<high(a)+1 then
  599. maxlen:=length(ss)
  600. else
  601. maxlen:=high(a)+1;
  602. move(ss[1],pchar(@a)^,maxlen);
  603. end;
  604. {$ifndef CPU64}
  605. procedure fpc_chararray_qword(v : qword;len : SizeInt;out a : array of char);compilerproc;
  606. var
  607. ss : shortstring;
  608. maxlen : SizeInt;
  609. begin
  610. int_str(v,ss);
  611. if length(ss)<len then
  612. ss:=space(len-length(ss))+ss;
  613. if length(ss)<high(a)+1 then
  614. maxlen:=length(ss)
  615. else
  616. maxlen:=high(a)+1;
  617. move(ss[1],pchar(@a)^,maxlen);
  618. end;
  619. procedure fpc_chararray_int64(v : int64;len : SizeInt;out a : array of char);compilerproc;
  620. var
  621. ss : shortstring;
  622. maxlen : SizeInt;
  623. begin
  624. int_str(v,ss);
  625. if length(ss)<len then
  626. ss:=space(len-length(ss))+ss;
  627. if length(ss)<high(a)+1 then
  628. maxlen:=length(ss)
  629. else
  630. maxlen:=high(a)+1;
  631. move(ss[1],pchar(@a)^,maxlen);
  632. end;
  633. {$endif CPU64}
  634. procedure fpc_chararray_Float(d : ValReal;len,fr,rt : SizeInt;out a : array of char);compilerproc;
  635. var
  636. ss : shortstring;
  637. maxlen : SizeInt;
  638. begin
  639. str_real(len,fr,d,treal_type(rt),ss);
  640. if length(ss)<high(a)+1 then
  641. maxlen:=length(ss)
  642. else
  643. maxlen:=high(a)+1;
  644. move(ss[1],pchar(@a)^,maxlen);
  645. end;
  646. {$ifdef FPC_HAS_STR_CURRENCY}
  647. procedure fpc_chararray_Currency(c : Currency;len,fr : SizeInt;out a : array of char);compilerproc;
  648. var
  649. ss : shortstring;
  650. maxlen : SizeInt;
  651. begin
  652. str(c:len:fr,ss);
  653. if length(ss)<high(a)+1 then
  654. maxlen:=length(ss)
  655. else
  656. maxlen:=high(a)+1;
  657. move(ss[1],pchar(@a)^,maxlen);
  658. end;
  659. {$endif FPC_HAS_STR_CURRENCY}
  660. {*****************************************************************************
  661. Val() Functions
  662. *****************************************************************************}
  663. Function InitVal(const s:shortstring;out negativ:boolean;out base:byte):ValSInt;
  664. var
  665. Code : SizeInt;
  666. begin
  667. {Skip Spaces and Tab}
  668. code:=1;
  669. while (code<=length(s)) and (s[code] in [' ',#9]) do
  670. inc(code);
  671. {Sign}
  672. negativ:=false;
  673. case s[code] of
  674. '-' : begin
  675. negativ:=true;
  676. inc(code);
  677. end;
  678. '+' : inc(code);
  679. end;
  680. {Base}
  681. base:=10;
  682. if code<=length(s) then
  683. begin
  684. case s[code] of
  685. '$',
  686. 'X',
  687. 'x' : begin
  688. base:=16;
  689. inc(code);
  690. end;
  691. '%' : begin
  692. base:=2;
  693. inc(code);
  694. end;
  695. '&' : begin
  696. Base:=8;
  697. inc(code);
  698. end;
  699. '0' : begin
  700. if (code < length(s)) and (s[code+1] in ['x', 'X']) then
  701. begin
  702. inc(code, 2);
  703. base := 16;
  704. end;
  705. end;
  706. end;
  707. end;
  708. { strip leading zeros }
  709. while ((code < length(s)) and (s[code] = '0')) do begin
  710. inc(code);
  711. end;
  712. InitVal:=code;
  713. end;
  714. Function fpc_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR']; compilerproc;
  715. var
  716. temp, prev, maxPrevValue, maxNewValue: ValUInt;
  717. base,u : byte;
  718. negative : boolean;
  719. begin
  720. fpc_Val_SInt_ShortStr := 0;
  721. Temp:=0;
  722. Code:=InitVal(s,negative,base);
  723. if Code>length(s) then
  724. exit;
  725. if (s[Code]=#0) then
  726. begin
  727. if (Code>1) and (s[Code-1]='0') then
  728. Code:=0;
  729. exit;
  730. end;
  731. maxPrevValue := ValUInt(MaxUIntValue) div ValUInt(Base);
  732. if (base = 10) then
  733. maxNewValue := MaxSIntValue + ord(negative)
  734. else
  735. maxNewValue := MaxUIntValue;
  736. while Code<=Length(s) do
  737. begin
  738. case s[Code] of
  739. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  740. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  741. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  742. #0 : break;
  743. else
  744. u:=16;
  745. end;
  746. Prev := Temp;
  747. Temp := Temp*ValUInt(base);
  748. If (u >= base) or
  749. (ValUInt(maxNewValue-u) < Temp) or
  750. (prev > maxPrevValue) Then
  751. Begin
  752. fpc_Val_SInt_ShortStr := 0;
  753. Exit
  754. End;
  755. Temp:=Temp+u;
  756. inc(code);
  757. end;
  758. code := 0;
  759. fpc_Val_SInt_ShortStr := ValSInt(Temp);
  760. If Negative Then
  761. fpc_Val_SInt_ShortStr := -fpc_Val_SInt_ShortStr;
  762. If Not(Negative) and (base <> 10) Then
  763. {sign extend the result to allow proper range checking}
  764. Case DestSize of
  765. 1: fpc_Val_SInt_ShortStr := shortint(fpc_Val_SInt_ShortStr);
  766. 2: fpc_Val_SInt_ShortStr := smallint(fpc_Val_SInt_ShortStr);
  767. {$ifdef cpu64}
  768. 4: fpc_Val_SInt_ShortStr := longint(fpc_Val_SInt_ShortStr);
  769. {$endif cpu64}
  770. End;
  771. end;
  772. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  773. { we have to pass the DestSize parameter on (JM) }
  774. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'FPC_VAL_SINT_SHORTSTR'];
  775. Function fpc_Val_UInt_Shortstr(Const S: ShortString; out Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR']; compilerproc;
  776. var
  777. prev : ValUInt;
  778. base,u : byte;
  779. negative : boolean;
  780. begin
  781. fpc_Val_UInt_Shortstr:=0;
  782. Code:=InitVal(s,negative,base);
  783. If Negative or (Code>length(s)) Then
  784. Exit;
  785. if (s[Code]=#0) then
  786. begin
  787. if (Code>1) and (s[Code-1]='0') then
  788. Code:=0;
  789. exit;
  790. end;
  791. while Code<=Length(s) do
  792. begin
  793. case s[Code] of
  794. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  795. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  796. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  797. #0 : break;
  798. else
  799. u:=16;
  800. end;
  801. prev := fpc_Val_UInt_Shortstr;
  802. If (u>=base) or
  803. (ValUInt(MaxUIntValue-u) div ValUInt(Base)<prev) then
  804. begin
  805. fpc_Val_UInt_Shortstr:=0;
  806. exit;
  807. end;
  808. fpc_Val_UInt_Shortstr:=fpc_Val_UInt_Shortstr*ValUInt(base) + u;
  809. inc(code);
  810. end;
  811. code := 0;
  812. end;
  813. {$ifndef CPU64}
  814. Function fpc_val_int64_shortstr(Const S: ShortString; out Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR']; compilerproc;
  815. var u, temp, prev, maxprevvalue, maxnewvalue : qword;
  816. base : byte;
  817. negative : boolean;
  818. const maxint64=qword($7fffffffffffffff);
  819. maxqword=qword($ffffffffffffffff);
  820. begin
  821. fpc_val_int64_shortstr := 0;
  822. Temp:=0;
  823. Code:=InitVal(s,negative,base);
  824. if Code>length(s) then
  825. exit;
  826. if (s[Code]=#0) then
  827. begin
  828. if (Code>1) and (s[Code-1]='0') then
  829. Code:=0;
  830. exit;
  831. end;
  832. maxprevvalue := maxqword div base;
  833. if (base = 10) then
  834. maxnewvalue := maxint64 + ord(negative)
  835. else
  836. maxnewvalue := maxqword;
  837. while Code<=Length(s) do
  838. begin
  839. case s[Code] of
  840. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  841. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  842. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  843. #0 : break;
  844. else
  845. u:=16;
  846. end;
  847. Prev:=Temp;
  848. Temp:=Temp*qword(base);
  849. If (u >= base) or
  850. (qword(maxnewvalue-u) < temp) or
  851. (prev > maxprevvalue) Then
  852. Begin
  853. fpc_val_int64_shortstr := 0;
  854. Exit
  855. End;
  856. Temp:=Temp+u;
  857. inc(code);
  858. end;
  859. code:=0;
  860. fpc_val_int64_shortstr:=int64(Temp);
  861. If Negative Then
  862. fpc_val_int64_shortstr:=-fpc_val_int64_shortstr;
  863. end;
  864. Function fpc_val_qword_shortstr(Const S: ShortString; out Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR']; compilerproc;
  865. var u, prev: QWord;
  866. base : byte;
  867. negative : boolean;
  868. const maxqword=qword($ffffffffffffffff);
  869. begin
  870. fpc_val_qword_shortstr:=0;
  871. Code:=InitVal(s,negative,base);
  872. If Negative or (Code>length(s)) Then
  873. Exit;
  874. if (s[Code]=#0) then
  875. begin
  876. if (Code>1) and (s[Code-1]='0') then
  877. Code:=0;
  878. exit;
  879. end;
  880. while Code<=Length(s) do
  881. begin
  882. case s[Code] of
  883. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  884. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  885. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  886. #0 : break;
  887. else
  888. u:=16;
  889. end;
  890. prev := fpc_val_qword_shortstr;
  891. If (u>=base) or
  892. ((QWord(maxqword-u) div QWord(base))<prev) then
  893. Begin
  894. fpc_val_qword_shortstr := 0;
  895. Exit
  896. End;
  897. fpc_val_qword_shortstr:=fpc_val_qword_shortstr*QWord(base) + u;
  898. inc(code);
  899. end;
  900. code := 0;
  901. end;
  902. {$endif CPU64}
  903. const
  904. {$ifdef FPC_HAS_TYPE_EXTENDED}
  905. valmaxexpnorm=4932;
  906. {$else}
  907. {$ifdef FPC_HAS_TYPE_DOUBLE}
  908. valmaxexpnorm=308;
  909. {$else}
  910. {$ifdef FPC_HAS_TYPE_SINGLE}
  911. valmaxexpnorm=38;
  912. {$else}
  913. {$error Unknown floating point precision }
  914. {$endif}
  915. {$endif}
  916. {$endif}
  917. Function fpc_Val_Real_ShortStr(const s : shortstring; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR']; compilerproc;
  918. var
  919. hd,
  920. esign,sign : valreal;
  921. exponent,i : SizeInt;
  922. flags : byte;
  923. begin
  924. fpc_Val_Real_ShortStr:=0.0;
  925. code:=1;
  926. exponent:=0;
  927. esign:=1;
  928. flags:=0;
  929. sign:=1;
  930. while (code<=length(s)) and (s[code] in [' ',#9]) do
  931. inc(code);
  932. if code<=length(s) then
  933. case s[code] of
  934. '+' : inc(code);
  935. '-' : begin
  936. sign:=-1;
  937. inc(code);
  938. end;
  939. end;
  940. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  941. begin
  942. { Read integer part }
  943. flags:=flags or 1;
  944. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  945. inc(code);
  946. end;
  947. { Decimal ? }
  948. if (length(s)>=code) and (s[code]='.') then
  949. begin
  950. hd:=1.0;
  951. inc(code);
  952. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  953. begin
  954. { Read fractional part. }
  955. flags:=flags or 2;
  956. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  957. hd:=hd*10.0;
  958. inc(code);
  959. end;
  960. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  961. end;
  962. { Again, read integer and fractional part}
  963. if flags=0 then
  964. begin
  965. fpc_Val_Real_ShortStr:=0.0;
  966. exit;
  967. end;
  968. { Exponent ? }
  969. if (length(s)>=code) and (upcase(s[code])='E') then
  970. begin
  971. inc(code);
  972. if Length(s) >= code then
  973. if s[code]='+' then
  974. inc(code)
  975. else
  976. if s[code]='-' then
  977. begin
  978. esign:=-1;
  979. inc(code);
  980. end;
  981. if (length(s)<code) or not(s[code] in ['0'..'9']) then
  982. begin
  983. fpc_Val_Real_ShortStr:=0.0;
  984. exit;
  985. end;
  986. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  987. begin
  988. exponent:=exponent*10;
  989. exponent:=exponent+ord(s[code])-ord('0');
  990. inc(code);
  991. end;
  992. end;
  993. { evaluate sign }
  994. { (before exponent, because the exponent may turn it into a denormal) }
  995. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*sign;
  996. { Calculate Exponent }
  997. hd:=1.0;
  998. { the magnitude range maximum (normal) is lower in absolute value than the }
  999. { the magnitude range minimum (denormal). E.g. an extended value can go }
  1000. { up to 1E4932, but "down" to 1E-4951. So make sure that we don't try to }
  1001. { calculate 1E4951 as factor, since that would overflow and result in 0. }
  1002. if (exponent>valmaxexpnorm-2) then
  1003. begin
  1004. for i:=1 to valmaxexpnorm-2 do
  1005. hd:=hd*10.0;
  1006. if esign>0 then
  1007. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1008. else
  1009. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1010. dec(exponent,valmaxexpnorm-2);
  1011. hd:=1.0;
  1012. end;
  1013. for i:=1 to exponent do
  1014. hd:=hd*10.0;
  1015. if esign>0 then
  1016. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1017. else
  1018. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1019. { Not all characters are read ? }
  1020. if length(s)>=code then
  1021. begin
  1022. fpc_Val_Real_ShortStr:=0.0;
  1023. exit;
  1024. end;
  1025. { success ! }
  1026. code:=0;
  1027. end;
  1028. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_SHORTSTR']; compilerproc;
  1029. type Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1030. o:longint;
  1031. s:Pstring;
  1032. end;
  1033. var l,h,m:cardinal;
  1034. sorted_array:^Tsorted_array;
  1035. spaces:byte;
  1036. t:shortstring;
  1037. label error;
  1038. begin
  1039. {Val for numbers accepts spaces at the start, so lets do the same
  1040. for enums. Skip spaces at the start of the string.}
  1041. spaces:=1;
  1042. while (spaces<=length(s)) and (s[spaces]=' ') do
  1043. inc(spaces);
  1044. t:=upcase(copy(s,spaces,255));
  1045. sorted_array:=pointer(Pcardinal(str2ordindex)+1);
  1046. {Use a binary search to get the string.}
  1047. l:=1;
  1048. h:=Pcardinal(str2ordindex)^;
  1049. repeat
  1050. m:=(l+h) div 2;
  1051. if t>upcase(sorted_array[m-1].s^) then
  1052. l:=m+1
  1053. else if t<upcase(sorted_array[m-1].s^) then
  1054. h:=m-1
  1055. else
  1056. break;
  1057. if l>h then
  1058. goto error;
  1059. until false;
  1060. fpc_val_enum_shortstr:=sorted_array[m-1].o;
  1061. exit;
  1062. error:
  1063. {Not found. Find first error position. Take care of the string length.}
  1064. code:=1;
  1065. while (code<=length(s)) and (s[code]=sorted_array[m].s^[code]) do
  1066. inc(code);
  1067. if code>length(s) then
  1068. code:=length(s)+1;
  1069. inc(code,spaces-1); {Add skipped spaces again.}
  1070. {The result of val in case of error is undefined, don't assign a function
  1071. result.}
  1072. end;
  1073. {Redeclare fpc_val_enum_shortstr for internal use in the system unit.}
  1074. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint;external name 'FPC_VAL_ENUM_SHORTSTR';
  1075. function fpc_Val_Currency_ShortStr(const s : shortstring; out Code : ValSInt): currency; [public, alias:'FPC_VAL_CURRENCY_SHORTSTR']; compilerproc;
  1076. const
  1077. MaxInt64 : Int64 = $7FFFFFFFFFFFFFFF;
  1078. Int64Edge : Int64 = ($7FFFFFFFFFFFFFFF - 10) div 10;
  1079. Int64Edge2 : Int64 = $7FFFFFFFFFFFFFFF div 10;
  1080. var
  1081. res : Int64;
  1082. i,j,power,sign,len : longint;
  1083. FracOverflow : boolean;
  1084. begin
  1085. fpc_Val_Currency_ShortStr:=0;
  1086. res:=0;
  1087. len:=Length(s);
  1088. Code:=1;
  1089. sign:=1;
  1090. power:=0;
  1091. while True do
  1092. if Code > len then
  1093. exit
  1094. else
  1095. if s[Code] in [' ', #9] then
  1096. Inc(Code)
  1097. else
  1098. break;
  1099. { Read sign }
  1100. case s[Code] of
  1101. '+' : Inc(Code);
  1102. '-' : begin
  1103. sign:=-1;
  1104. inc(code);
  1105. end;
  1106. end;
  1107. { Read digits }
  1108. FracOverflow:=False;
  1109. i:=0;
  1110. while Code <= len do
  1111. begin
  1112. case s[Code] of
  1113. '0'..'9':
  1114. begin
  1115. j:=Ord(s[code])-Ord('0');
  1116. { check overflow }
  1117. if (res <= Int64Edge) or (res <= (MaxInt64 - j) div 10) then
  1118. begin
  1119. res:=res*10 + j;
  1120. Inc(i);
  1121. end
  1122. else
  1123. if power = 0 then
  1124. { exit if integer part overflow }
  1125. exit
  1126. else
  1127. begin
  1128. if not FracOverflow and (j >= 5) and (res < MaxInt64) then
  1129. { round if first digit of fractional part overflow }
  1130. Inc(res);
  1131. FracOverflow:=True;
  1132. end;
  1133. end;
  1134. '.':
  1135. begin
  1136. if power = 0 then
  1137. begin
  1138. power:=1;
  1139. i:=0;
  1140. end
  1141. else
  1142. exit;
  1143. end;
  1144. else
  1145. break;
  1146. end;
  1147. Inc(Code);
  1148. end;
  1149. if (i = 0) and (power = 0) then
  1150. exit;
  1151. if power <> 0 then
  1152. power:=i;
  1153. power:=4 - power;
  1154. { Exponent? }
  1155. if Code <= len then
  1156. if s[Code] in ['E', 'e'] then
  1157. begin
  1158. Inc(Code);
  1159. if Code > len then
  1160. exit;
  1161. i:=1;
  1162. case s[Code] of
  1163. '+':
  1164. Inc(Code);
  1165. '-':
  1166. begin
  1167. i:=-1;
  1168. Inc(Code);
  1169. end;
  1170. end;
  1171. { read exponent }
  1172. j:=0;
  1173. while Code <= len do
  1174. if s[Code] in ['0'..'9'] then
  1175. begin
  1176. if j > 4951 then
  1177. exit;
  1178. j:=j*10 + (Ord(s[code])-Ord('0'));
  1179. Inc(Code);
  1180. end
  1181. else
  1182. exit;
  1183. power:=power + j*i;
  1184. end
  1185. else
  1186. exit;
  1187. if power > 0 then
  1188. begin
  1189. for i:=1 to power do
  1190. if res <= Int64Edge2 then
  1191. res:=res*10
  1192. else
  1193. exit;
  1194. end
  1195. else
  1196. for i:=1 to -power do
  1197. begin
  1198. if res <= MaxInt64 - 5 then
  1199. Inc(res, 5);
  1200. res:=res div 10;
  1201. end;
  1202. res:=res*sign;
  1203. fpc_Val_Currency_ShortStr:=PCurrency(@res)^;
  1204. Code:=0;
  1205. end;
  1206. Procedure SetString (Out S : Shortstring; Buf : PChar; Len : SizeInt);
  1207. begin
  1208. If Len > High(S) then
  1209. Len := High(S);
  1210. SetLength(S,Len);
  1211. If Buf<>Nil then
  1212. begin
  1213. Move (Buf[0],S[1],Len);
  1214. end;
  1215. end;