sstrings.inc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. {$ifndef FPC_HAS_SHORTSTR_SETLENGTH}
  14. {$define FPC_HAS_SHORTSTR_SETLENGTH}
  15. procedure fpc_Shortstr_SetLength(var s:shortstring;len:SizeInt);[Public,Alias : 'FPC_SHORTSTR_SETLENGTH']; compilerproc;
  16. begin
  17. if Len>255 then
  18. Len:=255;
  19. s[0]:=chr(len);
  20. end;
  21. {$endif FPC_HAS_SHORTSTR_SETLENGTH}
  22. {$ifndef FPC_HAS_SHORTSTR_COPY}
  23. {$define FPC_HAS_SHORTSTR_COPY}
  24. function fpc_shortstr_copy(const s : shortstring;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  25. begin
  26. if count<0 then
  27. count:=0;
  28. if index>1 then
  29. dec(index)
  30. else
  31. index:=0;
  32. if index>length(s) then
  33. count:=0
  34. else
  35. if count>length(s)-index then
  36. count:=length(s)-index;
  37. fpc_shortstr_Copy[0]:=chr(Count);
  38. fpc_shortstr_shortstr_intern_charmove(s,Index+1,fpc_shortstr_Copy,1,Count);
  39. end;
  40. {$endif FPC_HAS_SHORTSTR_COPY}
  41. {$ifndef FPC_HAS_SHORTSTR_DELETE}
  42. {$define FPC_HAS_SHORTSTR_DELETE}
  43. procedure delete(var s : shortstring;index : SizeInt;count : SizeInt);
  44. begin
  45. if index<=0 then
  46. exit;
  47. if (Index<=Length(s)) and (Count>0) then
  48. begin
  49. if Count>length(s)-Index then
  50. Count:=length(s)-Index+1;
  51. s[0]:=Chr(length(s)-Count);
  52. if Index<=Length(s) then
  53. fpc_shortstr_shortstr_intern_charmove(s,Index+Count,s,Index,Length(s)-Index+1);
  54. end;
  55. end;
  56. {$endif FPC_HAS_SHORTSTR_DELETE}
  57. {$ifndef FPC_HAS_SHORTSTR_INSERT}
  58. {$define FPC_HAS_SHORTSTR_INSERT}
  59. procedure insert(const source : shortstring;var s : shortstring;index : SizeInt);
  60. var
  61. cut,srclen,indexlen : SizeInt;
  62. begin
  63. if index<1 then
  64. index:=1;
  65. if index>length(s) then
  66. begin
  67. index:=length(s)+1;
  68. if index>high(s) then
  69. exit;
  70. end;
  71. indexlen:=Length(s)-Index+1;
  72. srclen:=length(Source);
  73. if sizeInt(length(source))+sizeint(length(s))>=sizeof(s) then
  74. begin
  75. cut:=sizeInt(length(source))+sizeint(length(s))-sizeof(s)+1;
  76. if cut>indexlen then
  77. begin
  78. dec(srclen,cut-indexlen);
  79. indexlen:=0;
  80. end
  81. else
  82. dec(indexlen,cut);
  83. end;
  84. fpc_shortstr_shortstr_intern_charmove(s,Index,s,Index+srclen,indexlen);
  85. fpc_shortstr_shortstr_intern_charmove(Source,1,s,Index,srclen);
  86. s[0]:=chr(index+srclen+indexlen-1);
  87. end;
  88. {$endif FPC_HAS_SHORTSTR_INSERT}
  89. {$ifndef FPC_HAS_SHORTSTR_INSERT_CHAR}
  90. {$define FPC_HAS_SHORTSTR_INSERT_CHAR}
  91. procedure insert(source : Char;var s : shortstring;index : SizeInt);
  92. var
  93. indexlen : SizeInt;
  94. begin
  95. if index<1 then
  96. index:=1;
  97. if index>length(s) then
  98. begin
  99. index:=length(s)+1;
  100. if index>high(s) then
  101. exit;
  102. end;
  103. indexlen:=Length(s)-Index+1;
  104. if (sizeint(length(s))+1=sizeof(s)) and (indexlen>0) then
  105. dec(indexlen);
  106. fpc_shortstr_shortstr_intern_charmove(s,Index,s,Index+1,indexlen);
  107. s[Index]:=Source;
  108. s[0]:=chr(index+indexlen);
  109. end;
  110. {$endif FPC_HAS_SHORTSTR_INSERT_CHAR}
  111. {$ifndef FPC_HAS_SHORTSTR_POS_SHORTSTR}
  112. {$define FPC_HAS_SHORTSTR_POS_SHORTSTR}
  113. function pos(const substr : shortstring;const s : shortstring):SizeInt;
  114. var
  115. i,MaxLen : SizeInt;
  116. pc : pchar;
  117. begin
  118. Pos:=0;
  119. if Length(SubStr)>0 then
  120. begin
  121. MaxLen:=sizeint(Length(s))-Length(SubStr);
  122. i:=0;
  123. pc:=@s[1];
  124. while (i<=MaxLen) do
  125. begin
  126. inc(i);
  127. if (SubStr[1]=pc^) and
  128. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  129. begin
  130. Pos:=i;
  131. exit;
  132. end;
  133. inc(pc);
  134. end;
  135. end;
  136. end;
  137. {$endif FPC_HAS_SHORTSTR_POS_SHORTSTR}
  138. {$ifndef FPC_HAS_SHORTSTR_POS_CHAR}
  139. {$define FPC_HAS_SHORTSTR_POS_CHAR}
  140. {Faster when looking for a single char...}
  141. function pos(c:char;const s:shortstring):SizeInt;
  142. var
  143. i : SizeInt;
  144. pc : pchar;
  145. begin
  146. pc:=@s[1];
  147. for i:=1 to length(s) do
  148. begin
  149. if pc^=c then
  150. begin
  151. pos:=i;
  152. exit;
  153. end;
  154. inc(pc);
  155. end;
  156. pos:=0;
  157. end;
  158. {$endif FPC_HAS_SHORTSTR_POS_CHAR}
  159. function fpc_char_copy(c:char;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  160. begin
  161. if (index=1) and (Count>0) then
  162. fpc_char_Copy:=c
  163. else
  164. fpc_char_Copy:='';
  165. end;
  166. function pos(const substr : shortstring;c:char): SizeInt;
  167. begin
  168. if (length(substr)=1) and (substr[1]=c) then
  169. Pos:=1
  170. else
  171. Pos:=0;
  172. end;
  173. {$if not defined(FPC_UPCASE_CHAR) or not defined(FPC_LOWERCASE_CHAR)}
  174. {$ifdef IBM_CHAR_SET}
  175. const
  176. UpCaseTbl : shortstring[7]=#154#142#153#144#128#143#165;
  177. LoCaseTbl : shortstring[7]=#129#132#148#130#135#134#164;
  178. {$endif}
  179. {$endif}
  180. {$ifndef FPC_UPCASE_CHAR}
  181. {$define FPC_UPCASE_CHAR}
  182. function upcase(c : char) : char;
  183. {$IFDEF IBM_CHAR_SET}
  184. var
  185. i : longint;
  186. {$ENDIF}
  187. begin
  188. if (c in ['a'..'z']) then
  189. upcase:=char(byte(c)-32)
  190. else
  191. {$IFDEF IBM_CHAR_SET}
  192. begin
  193. i:=Pos(c,LoCaseTbl);
  194. if i>0 then
  195. upcase:=UpCaseTbl[i]
  196. else
  197. upcase:=c;
  198. end;
  199. {$ELSE}
  200. upcase:=c;
  201. {$ENDIF}
  202. end;
  203. {$endif FPC_UPCASE_CHAR}
  204. {$ifndef FPC_UPCASE_SHORTSTR}
  205. {$define FPC_UPCASE_SHORTSTR}
  206. function upcase(const s : shortstring) : shortstring;
  207. var
  208. i : longint;
  209. begin
  210. upcase[0]:=s[0];
  211. for i := 1 to length (s) do
  212. upcase[i] := upcase (s[i]);
  213. end;
  214. {$endif FPC_UPCASE_SHORTSTR}
  215. {$ifndef FPC_LOWERCASE_CHAR}
  216. {$define FPC_LOWERCASE_CHAR}
  217. function lowercase(c : char) : char;overload;
  218. {$IFDEF IBM_CHAR_SET}
  219. var
  220. i : longint;
  221. {$ENDIF}
  222. begin
  223. if (c in ['A'..'Z']) then
  224. lowercase:=char(byte(c)+32)
  225. else
  226. {$IFDEF IBM_CHAR_SET}
  227. begin
  228. i:=Pos(c,UpCaseTbl);
  229. if i>0 then
  230. lowercase:=LoCaseTbl[i]
  231. else
  232. lowercase:=c;
  233. end;
  234. {$ELSE}
  235. lowercase:=c;
  236. {$ENDIF}
  237. end;
  238. {$endif FPC_LOWERCASE_CHAR}
  239. {$ifndef FPC_LOWERCASE_SHORTSTR}
  240. {$define FPC_LOWERCASE_SHORTSTR}
  241. function lowercase(const s : shortstring) : shortstring; overload;
  242. var
  243. i : longint;
  244. begin
  245. lowercase [0]:=s[0];
  246. for i:=1 to length(s) do
  247. lowercase[i]:=lowercase (s[i]);
  248. end;
  249. {$endif FPC_LOWERCASE_SHORTSTR}
  250. const
  251. HexTbl : array[0..15] of char='0123456789ABCDEF';
  252. function hexstr(val : longint;cnt : byte) : shortstring;
  253. var
  254. i : longint;
  255. begin
  256. hexstr[0]:=char(cnt);
  257. for i:=cnt downto 1 do
  258. begin
  259. hexstr[i]:=hextbl[val and $f];
  260. val:=val shr 4;
  261. end;
  262. end;
  263. function octstr(val : longint;cnt : byte) : shortstring;
  264. var
  265. i : longint;
  266. begin
  267. octstr[0]:=char(cnt);
  268. for i:=cnt downto 1 do
  269. begin
  270. octstr[i]:=hextbl[val and 7];
  271. val:=val shr 3;
  272. end;
  273. end;
  274. function binstr(val : longint;cnt : byte) : shortstring;
  275. var
  276. i : longint;
  277. begin
  278. binstr[0]:=char(cnt);
  279. for i:=cnt downto 1 do
  280. begin
  281. binstr[i]:=char(48+val and 1);
  282. val:=val shr 1;
  283. end;
  284. end;
  285. function hexstr(val : int64;cnt : byte) : shortstring;
  286. var
  287. i : longint;
  288. begin
  289. hexstr[0]:=char(cnt);
  290. for i:=cnt downto 1 do
  291. begin
  292. hexstr[i]:=hextbl[val and $f];
  293. val:=val shr 4;
  294. end;
  295. end;
  296. function octstr(val : int64;cnt : byte) : shortstring;
  297. var
  298. i : longint;
  299. begin
  300. octstr[0]:=char(cnt);
  301. for i:=cnt downto 1 do
  302. begin
  303. octstr[i]:=hextbl[val and 7];
  304. val:=val shr 3;
  305. end;
  306. end;
  307. function binstr(val : int64;cnt : byte) : shortstring;
  308. var
  309. i : longint;
  310. begin
  311. binstr[0]:=char(cnt);
  312. for i:=cnt downto 1 do
  313. begin
  314. binstr[i]:=char(48+val and 1);
  315. val:=val shr 1;
  316. end;
  317. end;
  318. {$ifndef FPC_HAS_QWORD_HEX_SHORTSTR}
  319. {$define FPC_HAS_QWORD_HEX_SHORTSTR}
  320. Function hexStr(Val:qword;cnt:byte):shortstring;
  321. begin
  322. hexStr:=hexStr(int64(Val),cnt);
  323. end;
  324. {$endif FPC_HAS_QWORD_HEX_SHORTSTR}
  325. {$ifndef FPC_HAS_QWORD_OCT_SHORTSTR}
  326. {$define FPC_HAS_QWORD_OCT_SHORTSTR}
  327. Function OctStr(Val:qword;cnt:byte):shortstring;
  328. begin
  329. OctStr:=OctStr(int64(Val),cnt);
  330. end;
  331. {$endif FPC_HAS_QWORD_OCT_SHORTSTR}
  332. {$ifndef FPC_HAS_QWORD_BIN_SHORTSTR}
  333. {$define FPC_HAS_QWORD_BIN_SHORTSTR}
  334. Function binStr(Val:qword;cnt:byte):shortstring;
  335. begin
  336. binStr:=binStr(int64(Val),cnt);
  337. end;
  338. {$endif FPC_HAS_QWORD_BIN_SHORTSTR}
  339. {$ifndef FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  340. {$define FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  341. function hexstr(val : pointer) : shortstring;
  342. var
  343. i : longint;
  344. v : ptruint;
  345. begin
  346. v:=ptruint(val);
  347. hexstr[0]:=chr(sizeof(pointer)*2);
  348. for i:=sizeof(pointer)*2 downto 1 do
  349. begin
  350. hexstr[i]:=hextbl[v and $f];
  351. v:=v shr 4;
  352. end;
  353. end;
  354. {$endif FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  355. {$ifndef FPC_HAS_SPACE_SHORTSTR}
  356. {$define FPC_HAS_SPACE_SHORTSTR}
  357. function space (b : byte): shortstring;
  358. begin
  359. space[0] := chr(b);
  360. FillChar (Space[1],b,' ');
  361. end;
  362. {$endif FPC_HAS_SPACE_SHORTSTR}
  363. {*****************************************************************************
  364. Str() Helpers
  365. *****************************************************************************}
  366. procedure fpc_shortstr_SInt(v : valSInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_SINT']; compilerproc;
  367. begin
  368. int_str(v,s);
  369. if length(s)<len then
  370. s:=space(len-length(s))+s;
  371. end;
  372. procedure fpc_shortstr_UInt(v : valUInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_UINT']; compilerproc;
  373. begin
  374. int_str_unsigned(v,s);
  375. if length(s)<len then
  376. s:=space(len-length(s))+s;
  377. end;
  378. {$ifndef CPU64}
  379. procedure fpc_shortstr_qword(v : qword;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_QWORD']; compilerproc;
  380. begin
  381. int_str_unsigned(v,s);
  382. if length(s)<len then
  383. s:=space(len-length(s))+s;
  384. end;
  385. procedure fpc_shortstr_int64(v : int64;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_INT64']; compilerproc;
  386. begin
  387. int_str(v,s);
  388. if length(s)<len then
  389. s:=space(len-length(s))+s;
  390. end;
  391. {$endif CPU64}
  392. {$if defined(CPU16) or defined(CPU8)}
  393. procedure fpc_shortstr_longword(v : longword;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_LONGWORD']; compilerproc;
  394. begin
  395. int_str_unsigned(v,s);
  396. if length(s)<len then
  397. s:=space(len-length(s))+s;
  398. end;
  399. procedure fpc_shortstr_longint(v : longint;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_LONGINT']; compilerproc;
  400. begin
  401. int_str(v,s);
  402. if length(s)<len then
  403. s:=space(len-length(s))+s;
  404. end;
  405. {$endif CPU16 or CPU8}
  406. { fpc_shortstr_sInt must appear before this file is included, because }
  407. { it's used inside real2str.inc and otherwise the searching via the }
  408. { compilerproc name will fail (JM) }
  409. {$ifndef FPUNONE}
  410. {$I real2str.inc}
  411. {$endif}
  412. {$ifndef FPUNONE}
  413. procedure fpc_shortstr_float(d : ValReal;len,fr,rt : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT']; compilerproc;
  414. begin
  415. str_real(len,fr,d,treal_type(rt),s);
  416. end;
  417. {$endif}
  418. {$ifndef FPC_STR_ENUM_INTERN}
  419. function fpc_shortstr_enum_intern(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring): longint;
  420. { The following contains the TTypeInfo/TTypeData records from typinfo.pp
  421. specialized for the tkEnumeration case (and stripped of unused things). }
  422. type
  423. PPstring=^Pstring;
  424. Penum_typeinfo=^Tenum_typeinfo;
  425. Tenum_typeinfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  426. kind:byte; { always tkEnumeration }
  427. num_chars:byte;
  428. chars:array[0..0] of char; { variable length with size of num_chars }
  429. end;
  430. Penum_typedata=^Tenum_typedata;
  431. Tenum_typedata={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  432. ordtype:byte;
  433. { this seemingly extraneous inner record is here for alignment purposes, so
  434. that its data gets aligned properly (if FPC_REQUIRES_PROPER_ALIGNMENT is
  435. set }
  436. inner: {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  437. minvalue,maxvalue:longint;
  438. basetype:pointer; { required for alignment }
  439. end;
  440. { more data here, but not needed }
  441. end;
  442. { Pascal data types for the ordinal enum value to string table. It consists of a header
  443. that indicates what type of data the table stores, either a direct lookup table (when
  444. o = lookup) or a set of ordered (ordinal value, string) tuples (when o = search). }
  445. { A single entry in the set of ordered tuples }
  446. Psearch_data=^Tsearch_data;
  447. Tsearch_data={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  448. value:longint;
  449. name:Pstring;
  450. end;
  451. Penum_ord_to_string=^Tenum_ord_to_string;
  452. Tenum_ord_to_string={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  453. o:(lookup,search);
  454. case integer of
  455. 0: (lookup_data:array[0..0] of Pstring);
  456. 1: (num_entries:longint;
  457. search_data:array[0..0] of Tsearch_data);
  458. end;
  459. var
  460. enum_o2s : Penum_ord_to_string;
  461. header:Penum_typeinfo;
  462. body:Penum_typedata;
  463. res:Pshortstring;
  464. sorted_data:Psearch_data;
  465. spaces,i,m,h,l:longint;
  466. begin
  467. { set default return value }
  468. fpc_shortstr_enum_intern:=107;
  469. enum_o2s:=Penum_ord_to_string(ord2strindex);
  470. { depending on the type of table in ord2strindex retrieve the data }
  471. if (enum_o2s^.o=lookup) then
  472. begin
  473. { direct lookup table }
  474. header:=Penum_typeinfo(typinfo);
  475. { calculate address of enum rtti body: add the actual size of the
  476. enum_rtti_header, and then align. Use an alignment of 1 (which
  477. does nothing) in case FPC_REQUIRES_PROPER_ALIGNMENT is not set
  478. to avoid the need for an if in this situation }
  479. body:=Penum_typedata(align(ptruint(header) + 2 * sizeof(byte) { kind, num_chars } + header^.num_chars,
  480. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT} 1 {$else} sizeof(pointer) {$endif}));
  481. with (body^.inner) do
  482. begin
  483. { Bounds check for the ordinal value for this enum }
  484. if (ordinal<minvalue) or (ordinal>maxvalue) then
  485. exit;
  486. { make the ordinal index for lookup zero-based }
  487. dec(ordinal,minvalue);
  488. end;
  489. { temporarily disable range checking because of the access to the array[0..0]
  490. member of Tenum_ord_to_string_lookup }
  491. {$push}{$R-}
  492. res:=enum_o2s^.lookup_data[ordinal];
  493. {$pop}
  494. if (not assigned(res)) then
  495. exit;
  496. s:=res^;
  497. end
  498. else
  499. begin
  500. { The compiler did generate a sorted array of (ordvalue,Pstring) tuples }
  501. sorted_data:=@enum_o2s^.search_data;
  502. { Use a binary search to get the string }
  503. l:=0;
  504. { temporarily disable range checking because of the access to the array[0..0]
  505. member of Tenum_ord_to_string_search }
  506. {$push}{$R-}
  507. h:=enum_o2s^.num_entries-1;
  508. repeat
  509. m:=(l+h) div 2;
  510. if ordinal>sorted_data[m].value then
  511. l:=m+1
  512. else if ordinal<sorted_data[m].value then
  513. h:=m-1
  514. else
  515. break;
  516. if l>h then
  517. exit; { Ordinal value not found? Exit }
  518. until false;
  519. {$pop}
  520. s:=sorted_data[m].name^;
  521. end;
  522. { Pad the string with spaces if necessary }
  523. if (len>length(s)) then
  524. begin
  525. spaces:=len-length(s);
  526. for i:=1 to spaces do
  527. s[length(s)+i]:=' ';
  528. inc(byte(s[0]),spaces);
  529. end;
  530. fpc_shortstr_enum_intern:=0;
  531. end;
  532. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_SHORTSTR_ENUM'];compilerproc;
  533. var
  534. res: longint;
  535. begin
  536. res:=fpc_shortstr_enum_intern(ordinal,len,typinfo,ord2strindex,s);
  537. if (res<>0) then
  538. runerror(107);
  539. end;
  540. { also define alias for internal use in the system unit }
  541. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);external name 'FPC_SHORTSTR_ENUM';
  542. {$endif FPC_SHORTSTR_ENUM_INTERN}
  543. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);[public,alias:'FPC_SHORTSTR_BOOL'];compilerproc;
  544. begin
  545. if b then
  546. s:='TRUE'
  547. else
  548. s:='FALSE';
  549. if length(s)<len then
  550. s:=space(len-length(s))+s;
  551. end;
  552. { also define alias for internal use in the system unit }
  553. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);external {$ifndef cpujvm}name 'FPC_SHORTSTR_BOOL'{$endif};
  554. procedure fpc_shortstr_currency({$ifdef cpujvm}constref{$endif} c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
  555. const
  556. MinLen = 8; { Minimal string length in scientific format }
  557. var
  558. buf : array[1..19] of char;
  559. i,j,k,reslen,tlen,sign,r,point : longint;
  560. ic : qword;
  561. begin
  562. fillchar(buf,length(buf),'0');
  563. { default value for length is -32767 }
  564. if len=-32767 then
  565. len:=25;
  566. if PInt64(@c)^ >= 0 then
  567. begin
  568. ic:=QWord(PInt64(@c)^);
  569. sign:=0;
  570. end
  571. else
  572. begin
  573. sign:=1;
  574. ic:=QWord(-PInt64(@c)^);
  575. end;
  576. { converting to integer string }
  577. tlen:=0;
  578. repeat
  579. Inc(tlen);
  580. buf[tlen]:=Chr(ic mod 10 + $30);
  581. ic:=ic div 10;
  582. until ic = 0;
  583. { calculating:
  584. reslen - length of result string,
  585. r - rounding or appending zeroes,
  586. point - place of decimal point }
  587. reslen:=tlen;
  588. if f <> 0 then
  589. Inc(reslen); { adding decimal point length }
  590. if f < 0 then
  591. begin
  592. { scientific format }
  593. Inc(reslen,5); { adding length of sign and exponent }
  594. if len < MinLen then
  595. len:=MinLen;
  596. r:=reslen-len;
  597. if reslen < len then
  598. reslen:=len;
  599. if r > 0 then
  600. begin
  601. reslen:=len;
  602. point:=tlen - r;
  603. end
  604. else
  605. point:=tlen;
  606. end
  607. else
  608. begin
  609. { fixed format }
  610. Inc(reslen, sign);
  611. { prepending fractional part with zeroes }
  612. while tlen < 5 do
  613. begin
  614. Inc(reslen);
  615. Inc(tlen);
  616. buf[tlen]:='0';
  617. end;
  618. { Currency have 4 digits in fractional part }
  619. r:=4 - f;
  620. point:=f;
  621. if point <> 0 then
  622. begin
  623. if point > 4 then
  624. point:=4;
  625. Inc(point);
  626. end;
  627. Dec(reslen,r);
  628. end;
  629. { rounding string if r > 0 }
  630. if r > 0 then
  631. begin
  632. k := 0;
  633. i := r+2;
  634. if i > tlen then
  635. i := tlen+1;
  636. if buf[i-2] >= '5' then
  637. begin
  638. if buf[i-1] < '9' then
  639. buf[i-1] := chr(ord(buf[i-1])+1)
  640. else
  641. begin
  642. buf[i-1] := '0';
  643. k := 1;
  644. end;
  645. end;
  646. If (k=1) and (buf[i-1]='0') then
  647. begin
  648. { 1.9996 rounded to two decimal digits after the decimal separator must result in
  649. 2.00, i.e. the rounding is propagated
  650. }
  651. while buf[i]='9' do
  652. begin
  653. buf[i]:='0';
  654. inc(i);
  655. end;
  656. buf[i]:=chr(Ord(buf[i])+1);
  657. { did we add another digit? This happens when rounding
  658. e.g. 99.9996 to two decimal digits after the decimal separator which should result in
  659. 100.00
  660. }
  661. if i>tlen then
  662. begin
  663. inc(reslen);
  664. inc(tlen);
  665. end;
  666. end;
  667. end;
  668. { preparing result string }
  669. if reslen<len then
  670. reslen:=len;
  671. if reslen>High(s) then
  672. begin
  673. if r < 0 then
  674. Inc(r, reslen - High(s));
  675. reslen:=High(s);
  676. end;
  677. SetLength(s,reslen);
  678. j:=reslen;
  679. if f<0 then
  680. begin
  681. { writing power of 10 part }
  682. if PInt64(@c)^ = 0 then
  683. k:=0
  684. else
  685. k:=tlen-5;
  686. if k >= 0 then
  687. s[j-2]:='+'
  688. else
  689. begin
  690. s[j-2]:='-';
  691. k:=-k;
  692. end;
  693. s[j]:=Chr(k mod 10 + $30);
  694. Dec(j);
  695. s[j]:=Chr(k div 10 + $30);
  696. Dec(j,2);
  697. s[j]:='E';
  698. Dec(j);
  699. end;
  700. { writing extra zeroes if r < 0 }
  701. while r < 0 do
  702. begin
  703. s[j]:='0';
  704. Dec(j);
  705. Inc(r);
  706. end;
  707. { writing digits and decimal point }
  708. for i:=r + 1 to tlen do
  709. begin
  710. Dec(point);
  711. if point = 0 then
  712. begin
  713. s[j]:='.';
  714. Dec(j);
  715. end;
  716. s[j]:=buf[i];
  717. Dec(j);
  718. end;
  719. { writing sign }
  720. if sign = 1 then
  721. begin
  722. s[j]:='-';
  723. Dec(j);
  724. end;
  725. { writing spaces }
  726. while j > 0 do
  727. begin
  728. s[j]:=' ';
  729. Dec(j);
  730. end;
  731. end;
  732. {
  733. Array Of Char Str() helpers
  734. }
  735. procedure fpc_chararray_sint(v : valsint;len : SizeInt;out a:array of char);compilerproc;
  736. var
  737. ss : shortstring;
  738. maxlen : SizeInt;
  739. begin
  740. int_str(v,ss);
  741. if length(ss)<len then
  742. ss:=space(len-length(ss))+ss;
  743. if length(ss)<high(a)+1 then
  744. maxlen:=length(ss)
  745. else
  746. maxlen:=high(a)+1;
  747. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  748. end;
  749. procedure fpc_chararray_uint(v : valuint;len : SizeInt;out a : array of char);compilerproc;
  750. var
  751. ss : shortstring;
  752. maxlen : SizeInt;
  753. begin
  754. int_str_unsigned(v,ss);
  755. if length(ss)<len then
  756. ss:=space(len-length(ss))+ss;
  757. if length(ss)<high(a)+1 then
  758. maxlen:=length(ss)
  759. else
  760. maxlen:=high(a)+1;
  761. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  762. end;
  763. {$ifndef CPU64}
  764. procedure fpc_chararray_qword(v : qword;len : SizeInt;out a : array of char);compilerproc;
  765. var
  766. ss : shortstring;
  767. maxlen : SizeInt;
  768. begin
  769. int_str_unsigned(v,ss);
  770. if length(ss)<len then
  771. ss:=space(len-length(ss))+ss;
  772. if length(ss)<high(a)+1 then
  773. maxlen:=length(ss)
  774. else
  775. maxlen:=high(a)+1;
  776. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  777. end;
  778. procedure fpc_chararray_int64(v : int64;len : SizeInt;out a : array of char);compilerproc;
  779. var
  780. ss : shortstring;
  781. maxlen : SizeInt;
  782. begin
  783. int_str(v,ss);
  784. if length(ss)<len then
  785. ss:=space(len-length(ss))+ss;
  786. if length(ss)<high(a)+1 then
  787. maxlen:=length(ss)
  788. else
  789. maxlen:=high(a)+1;
  790. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  791. end;
  792. {$endif CPU64}
  793. {$if defined(CPU16) or defined(CPU8)}
  794. procedure fpc_chararray_longword(v : longword;len : SizeInt;out a : array of char);compilerproc;
  795. var
  796. ss : shortstring;
  797. maxlen : SizeInt;
  798. begin
  799. int_str_unsigned(v,ss);
  800. if length(ss)<len then
  801. ss:=space(len-length(ss))+ss;
  802. if length(ss)<high(a)+1 then
  803. maxlen:=length(ss)
  804. else
  805. maxlen:=high(a)+1;
  806. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  807. end;
  808. procedure fpc_chararray_longint(v : longint;len : SizeInt;out a : array of char);compilerproc;
  809. var
  810. ss : shortstring;
  811. maxlen : SizeInt;
  812. begin
  813. int_str(v,ss);
  814. if length(ss)<len then
  815. ss:=space(len-length(ss))+ss;
  816. if length(ss)<high(a)+1 then
  817. maxlen:=length(ss)
  818. else
  819. maxlen:=high(a)+1;
  820. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  821. end;
  822. {$endif CPU16 or CPU8}
  823. {$ifndef FPUNONE}
  824. procedure fpc_chararray_Float(d : ValReal;len,fr,rt : SizeInt;out a : array of char);compilerproc;
  825. var
  826. ss : shortstring;
  827. maxlen : SizeInt;
  828. begin
  829. str_real(len,fr,d,treal_type(rt),ss);
  830. if length(ss)<high(a)+1 then
  831. maxlen:=length(ss)
  832. else
  833. maxlen:=high(a)+1;
  834. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  835. end;
  836. {$endif}
  837. {$ifndef FPC_STR_ENUM_INTERN}
  838. procedure fpc_chararray_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out a : array of char);compilerproc;
  839. var
  840. ss : shortstring;
  841. maxlen : SizeInt;
  842. begin
  843. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  844. if length(ss)<high(a)+1 then
  845. maxlen:=length(ss)
  846. else
  847. maxlen:=high(a)+1;
  848. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  849. end;
  850. {$endif not FPC_STR_ENUM_INTERN}
  851. procedure fpc_chararray_bool(b : boolean;len:sizeint;out a : array of char);compilerproc;
  852. var
  853. ss : shortstring;
  854. maxlen : SizeInt;
  855. begin
  856. fpc_shortstr_bool(b,len,ss);
  857. if length(ss)<high(a)+1 then
  858. maxlen:=length(ss)
  859. else
  860. maxlen:=high(a)+1;
  861. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  862. end;
  863. {$ifndef FPC_HAS_CHARARRAY_CURRENCY}
  864. {$define FPC_HAS_CHARARRAY_CURRENCY}
  865. procedure fpc_chararray_Currency(c : Currency;len,fr : SizeInt;out a : array of char);compilerproc;
  866. var
  867. ss : shortstring;
  868. maxlen : SizeInt;
  869. begin
  870. str(c:len:fr,ss);
  871. if length(ss)<high(a)+1 then
  872. maxlen:=length(ss)
  873. else
  874. maxlen:=high(a)+1;
  875. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  876. end;
  877. {$endif FPC_HAS_STR_CURRENCY}
  878. {*****************************************************************************
  879. Val() Functions
  880. *****************************************************************************}
  881. Function InitVal(const s:shortstring;out negativ:boolean;out base:byte):ValSInt;
  882. var
  883. Code : SizeInt;
  884. begin
  885. code:=1;
  886. negativ:=false;
  887. base:=10;
  888. if length(s)=0 then
  889. begin
  890. InitVal:=code;
  891. Exit;
  892. end;
  893. {Skip Spaces and Tab}
  894. while (code<=length(s)) and (s[code] in [' ',#9]) do
  895. inc(code);
  896. {Sign}
  897. case s[code] of
  898. '-' : begin
  899. negativ:=true;
  900. inc(code);
  901. end;
  902. '+' : inc(code);
  903. end;
  904. {Base}
  905. if code<=length(s) then
  906. begin
  907. case s[code] of
  908. '$',
  909. 'X',
  910. 'x' : begin
  911. base:=16;
  912. inc(code);
  913. end;
  914. '%' : begin
  915. base:=2;
  916. inc(code);
  917. end;
  918. '&' : begin
  919. Base:=8;
  920. inc(code);
  921. end;
  922. '0' : begin
  923. if (code < length(s)) and (s[code+1] in ['x', 'X']) then
  924. begin
  925. inc(code, 2);
  926. base := 16;
  927. end;
  928. end;
  929. end;
  930. end;
  931. { strip leading zeros }
  932. while ((code < length(s)) and (s[code] = '0')) do begin
  933. inc(code);
  934. end;
  935. InitVal:=code;
  936. end;
  937. Function fpc_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR']; compilerproc;
  938. var
  939. temp, prev, maxPrevValue, maxNewValue: ValUInt;
  940. base,u : byte;
  941. negative : boolean;
  942. begin
  943. fpc_Val_SInt_ShortStr := 0;
  944. Temp:=0;
  945. Code:=InitVal(s,negative,base);
  946. if Code>length(s) then
  947. exit;
  948. if (s[Code]=#0) then
  949. begin
  950. if (Code>1) and (s[Code-1]='0') then
  951. Code:=0;
  952. exit;
  953. end;
  954. maxPrevValue := ValUInt(MaxUIntValue) div ValUInt(Base);
  955. if (base = 10) then
  956. maxNewValue := MaxSIntValue + ord(negative)
  957. else
  958. maxNewValue := MaxUIntValue;
  959. while Code<=Length(s) do
  960. begin
  961. case s[Code] of
  962. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  963. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  964. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  965. #0 : break;
  966. else
  967. u:=16;
  968. end;
  969. Prev := Temp;
  970. Temp := Temp*ValUInt(base);
  971. If (u >= base) or
  972. (ValUInt(maxNewValue-u) < Temp) or
  973. (prev > maxPrevValue) Then
  974. Begin
  975. fpc_Val_SInt_ShortStr := 0;
  976. Exit
  977. End;
  978. Temp:=Temp+u;
  979. inc(code);
  980. end;
  981. code := 0;
  982. fpc_Val_SInt_ShortStr := ValSInt(Temp);
  983. If Negative Then
  984. fpc_Val_SInt_ShortStr := -fpc_Val_SInt_ShortStr;
  985. If Not(Negative) and (base <> 10) Then
  986. {sign extend the result to allow proper range checking}
  987. Case DestSize of
  988. 1: fpc_Val_SInt_ShortStr := shortint(fpc_Val_SInt_ShortStr);
  989. 2: fpc_Val_SInt_ShortStr := smallint(fpc_Val_SInt_ShortStr);
  990. {$ifdef cpu64}
  991. 4: fpc_Val_SInt_ShortStr := longint(fpc_Val_SInt_ShortStr);
  992. {$endif cpu64}
  993. End;
  994. end;
  995. {$ifndef FPC_HAS_INT_VAL_SINT_SHORTSTR}
  996. {$define FPC_HAS_INT_VAL_SINT_SHORTSTR}
  997. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  998. { we have to pass the DestSize parameter on (JM) }
  999. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'FPC_VAL_SINT_SHORTSTR'];
  1000. {$endif FPC_HAS_INT_VAL_SINT_SHORTSTR}
  1001. Function fpc_Val_UInt_Shortstr(Const S: ShortString; out Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR']; compilerproc;
  1002. var
  1003. prev : ValUInt;
  1004. base,u : byte;
  1005. negative : boolean;
  1006. begin
  1007. fpc_Val_UInt_Shortstr:=0;
  1008. Code:=InitVal(s,negative,base);
  1009. If Negative or (Code>length(s)) Then
  1010. Exit;
  1011. if (s[Code]=#0) then
  1012. begin
  1013. if (Code>1) and (s[Code-1]='0') then
  1014. Code:=0;
  1015. exit;
  1016. end;
  1017. while Code<=Length(s) do
  1018. begin
  1019. case s[Code] of
  1020. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1021. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1022. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1023. #0 : break;
  1024. else
  1025. u:=16;
  1026. end;
  1027. prev := fpc_Val_UInt_Shortstr;
  1028. If (u>=base) or
  1029. (ValUInt(MaxUIntValue-u) div ValUInt(Base)<prev) then
  1030. begin
  1031. fpc_Val_UInt_Shortstr:=0;
  1032. exit;
  1033. end;
  1034. fpc_Val_UInt_Shortstr:=fpc_Val_UInt_Shortstr*ValUInt(base) + u;
  1035. inc(code);
  1036. end;
  1037. code := 0;
  1038. end;
  1039. {$ifndef CPU64}
  1040. Function fpc_val_int64_shortstr(Const S: ShortString; out Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR']; compilerproc;
  1041. var u, temp, prev, maxprevvalue, maxnewvalue : qword;
  1042. base : byte;
  1043. negative : boolean;
  1044. const maxint64=qword($7fffffffffffffff);
  1045. maxqword=qword($ffffffffffffffff);
  1046. begin
  1047. fpc_val_int64_shortstr := 0;
  1048. Temp:=0;
  1049. Code:=InitVal(s,negative,base);
  1050. if Code>length(s) then
  1051. exit;
  1052. if (s[Code]=#0) then
  1053. begin
  1054. if (Code>1) and (s[Code-1]='0') then
  1055. Code:=0;
  1056. exit;
  1057. end;
  1058. maxprevvalue := maxqword div base;
  1059. if (base = 10) then
  1060. maxnewvalue := maxint64 + ord(negative)
  1061. else
  1062. maxnewvalue := maxqword;
  1063. while Code<=Length(s) do
  1064. begin
  1065. case s[Code] of
  1066. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1067. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1068. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1069. #0 : break;
  1070. else
  1071. u:=16;
  1072. end;
  1073. Prev:=Temp;
  1074. Temp:=Temp*qword(base);
  1075. If (u >= base) or
  1076. (qword(maxnewvalue-u) < temp) or
  1077. (prev > maxprevvalue) Then
  1078. Begin
  1079. fpc_val_int64_shortstr := 0;
  1080. Exit
  1081. End;
  1082. Temp:=Temp+u;
  1083. inc(code);
  1084. end;
  1085. code:=0;
  1086. fpc_val_int64_shortstr:=int64(Temp);
  1087. If Negative Then
  1088. fpc_val_int64_shortstr:=-fpc_val_int64_shortstr;
  1089. end;
  1090. Function fpc_val_qword_shortstr(Const S: ShortString; out Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR']; compilerproc;
  1091. var u, prev: QWord;
  1092. base : byte;
  1093. negative : boolean;
  1094. const maxqword=qword($ffffffffffffffff);
  1095. begin
  1096. fpc_val_qword_shortstr:=0;
  1097. Code:=InitVal(s,negative,base);
  1098. If Negative or (Code>length(s)) Then
  1099. Exit;
  1100. if (s[Code]=#0) then
  1101. begin
  1102. if (Code>1) and (s[Code-1]='0') then
  1103. Code:=0;
  1104. exit;
  1105. end;
  1106. while Code<=Length(s) do
  1107. begin
  1108. case s[Code] of
  1109. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1110. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1111. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1112. #0 : break;
  1113. else
  1114. u:=16;
  1115. end;
  1116. prev := fpc_val_qword_shortstr;
  1117. If (u>=base) or
  1118. ((QWord(maxqword-u) div QWord(base))<prev) then
  1119. Begin
  1120. fpc_val_qword_shortstr := 0;
  1121. Exit
  1122. End;
  1123. fpc_val_qword_shortstr:=fpc_val_qword_shortstr*QWord(base) + u;
  1124. inc(code);
  1125. end;
  1126. code := 0;
  1127. end;
  1128. {$endif CPU64}
  1129. {$ifndef FPUNONE}
  1130. const
  1131. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1132. valmaxexpnorm=4932;
  1133. {$else}
  1134. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1135. valmaxexpnorm=308;
  1136. {$else}
  1137. {$ifdef FPC_HAS_TYPE_SINGLE}
  1138. valmaxexpnorm=38;
  1139. {$else}
  1140. {$error Unknown floating point precision }
  1141. {$endif}
  1142. {$endif}
  1143. {$endif}
  1144. {$endif}
  1145. {$ifndef FPUNONE}
  1146. (******************
  1147. Derived from: ".\Free Pascal\source\rtl\inc\genmath.inc"
  1148. Origin: "fast 10^n routine"
  1149. function FPower10(val: Extended; Power: Longint): Extended;
  1150. Changes:
  1151. > adapted to "ValReal", so float can be single/double/extended
  1152. > slightly changed arrays [redundant 58+2 float constants gone away]
  1153. > added some checks etc..
  1154. Notes:
  1155. > denormalization and overflow should go smooth if corresponding
  1156. FPU exceptions are masked [no external care needed by now]
  1157. > adaption to real48 and real128 is not hard if one needed
  1158. ******************)
  1159. //
  1160. function mul_by_power10(x:ValReal;power:integer):ValReal;
  1161. //
  1162. // result:=X*(10^power)
  1163. //
  1164. // Routine achieves result with no more than 3 floating point mul/div's.
  1165. // Up to ABS(power)=31, only 1 floating point mul/div is needed.
  1166. //
  1167. // Limitations:
  1168. // for ValReal=extended : power=-5119..+5119
  1169. // for ValReal=double : power=-319..+319
  1170. // for ValReal=single : power=-63..+63
  1171. //
  1172. // If "power" is beyond this limits, routine gives up and returns 0/+INF/-INF.
  1173. // This is not generally correct, but should be ok when routine is used only
  1174. // as "VAL"-helper, since "x" exponent is reasonably close to 0 in this case.
  1175. //
  1176. //==================================
  1177. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1178. {$ERROR C_HIGH_EXPBITS_5TO8 declared somewhere in scope}
  1179. {$ENDIF}
  1180. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1181. {$ERROR C_HIGH_EXPBITS_9ANDUP declared somewhere in scope}
  1182. {$ENDIF}
  1183. {$IF SIZEOF(ValReal)=10}
  1184. //==================================
  1185. // assuming "type ValReal=extended;"
  1186. //
  1187. const
  1188. C_MAX_POWER = 5119;
  1189. C_HIGH_EXPBITS_5TO8 = 15;
  1190. C_HIGH_EXPBITS_9ANDUP = 9;
  1191. {$ELSEIF SIZEOF(ValReal)=8}
  1192. //==================================
  1193. // assuming "type ValReal=double;"
  1194. //
  1195. const
  1196. C_MAX_POWER = 319;
  1197. C_HIGH_EXPBITS_5TO8 = 9;
  1198. {$ELSEIF SIZEOF(ValReal)=4}
  1199. //==================================
  1200. // assuming "type ValReal=single;"
  1201. //
  1202. const
  1203. C_MAX_POWER = 63;
  1204. {$ELSE}
  1205. //==================================
  1206. // assuming "ValReal=?"
  1207. //
  1208. {$ERROR Unsupported ValReal type}
  1209. {$ENDIF}
  1210. //==================================
  1211. const
  1212. C_INFTYP = ValReal( 1.0/0.0);
  1213. C_INFTYM = ValReal(-1.0/0.0);
  1214. mul_expbits_0_to_4:packed array[0..31]of ValReal=(
  1215. 1E0, 1E1, 1E2, 1E3,
  1216. 1E4, 1E5, 1E6, 1E7,
  1217. 1E8, 1E9, 1E10, 1E11,
  1218. 1E12, 1E13, 1E14, 1E15,
  1219. 1E16, 1E17, 1E18, 1E19,
  1220. 1E20, 1E21, 1E22, 1E23,
  1221. 1E24, 1E25, 1E26, 1E27,
  1222. 1E28, 1E29, 1E30, 1E31);
  1223. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1224. mul_expbits_5_to_8:packed array[1..C_HIGH_EXPBITS_5TO8] of ValReal=(
  1225. 1E32, 1E64, 1E96, 1E128,
  1226. 1E160, 1E192, 1E224, 1E256, 1E288
  1227. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)},
  1228. 1E320, 1E352, 1E384, 1E416, 1E448, 1E480
  1229. {$ENDIF});
  1230. {$ELSE}
  1231. mul_expbits_5_to_8:ValReal=1E32;
  1232. {$ENDIF}
  1233. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1234. mul_expbits_9_and_up:packed array[1..C_HIGH_EXPBITS_9ANDUP] of ValReal=(
  1235. 1E512, 1E1024, 1E1536, 1E2048,
  1236. 1E2560, 1E3072, 1E3584, 1E4096,
  1237. 1E4608);
  1238. {$ENDIF}
  1239. begin
  1240. if power=0 then mul_by_power10:=x else
  1241. if power<-C_MAX_POWER then mul_by_power10:=0 else
  1242. if power>C_MAX_POWER then
  1243. if x<0 then mul_by_power10:=C_INFTYM else
  1244. if x>0 then mul_by_power10:=C_INFTYP else mul_by_power10:=0
  1245. else
  1246. if power<0 then
  1247. begin
  1248. power:=-power;
  1249. mul_by_power10:=x/mul_expbits_0_to_4[power and $1F];
  1250. power:=(power shr 5);
  1251. if power=0 then exit;
  1252. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1253. if power and $F<>0 then
  1254. mul_by_power10:=
  1255. mul_by_power10/mul_expbits_5_to_8[power and $F];
  1256. {$ELSE} // "single", power<>0, so always div
  1257. mul_by_power10:=mul_by_power10/mul_expbits_5_to_8;
  1258. {$ENDIF}
  1259. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1260. power:=(power shr 4);
  1261. if power<>0 then
  1262. mul_by_power10:=
  1263. mul_by_power10/mul_expbits_9_and_up[power];
  1264. {$ENDIF}
  1265. end
  1266. else
  1267. begin
  1268. mul_by_power10:=x*mul_expbits_0_to_4[power and $1F];
  1269. power:=(power shr 5);
  1270. if power=0 then exit;
  1271. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1272. if power and $F<>0 then
  1273. mul_by_power10:=
  1274. mul_by_power10*mul_expbits_5_to_8[power and $F];
  1275. {$ELSE} // "single", power<>0, so always mul
  1276. mul_by_power10:=mul_by_power10*mul_expbits_5_to_8;
  1277. {$ENDIF}
  1278. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1279. power:=(power shr 4);
  1280. if power<>0 then
  1281. mul_by_power10:=
  1282. mul_by_power10*mul_expbits_9_and_up[power];
  1283. {$ENDIF}
  1284. end;
  1285. end;
  1286. Function fpc_Val_Real_ShortStr(const s : shortstring; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR']; compilerproc;
  1287. var
  1288. hd,
  1289. sign : valreal;
  1290. esign,
  1291. exponent,
  1292. decpoint : SizeInt;
  1293. flags : byte;
  1294. begin
  1295. fpc_Val_Real_ShortStr:=0.0;
  1296. code:=1;
  1297. exponent:=0;
  1298. decpoint:=0;
  1299. esign:=1;
  1300. flags:=0;
  1301. sign:=1;
  1302. while (code<=length(s)) and (s[code] in [' ',#9]) do
  1303. inc(code);
  1304. if code<=length(s) then
  1305. case s[code] of
  1306. '+' : inc(code);
  1307. '-' : begin
  1308. sign:=-1;
  1309. inc(code);
  1310. end;
  1311. end;
  1312. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  1313. begin
  1314. { Read integer part }
  1315. flags:=flags or 1;
  1316. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  1317. inc(code);
  1318. end;
  1319. { Decimal ? }
  1320. if (length(s)>=code) and (s[code]='.') then
  1321. begin
  1322. inc(code);
  1323. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  1324. begin
  1325. { Read fractional part. }
  1326. flags:=flags or 2;
  1327. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  1328. inc(decpoint);
  1329. inc(code);
  1330. end;
  1331. end;
  1332. { Again, read integer and fractional part}
  1333. if flags=0 then
  1334. begin
  1335. fpc_Val_Real_ShortStr:=0.0;
  1336. exit;
  1337. end;
  1338. { Exponent ? }
  1339. if (length(s)>=code) and (s[code] in ['e','E']) then
  1340. begin
  1341. inc(code);
  1342. if Length(s) >= code then
  1343. if s[code]='+' then
  1344. inc(code)
  1345. else
  1346. if s[code]='-' then
  1347. begin
  1348. esign:=-1;
  1349. inc(code);
  1350. end;
  1351. if (length(s)<code) or not(s[code] in ['0'..'9']) then
  1352. begin
  1353. fpc_Val_Real_ShortStr:=0.0;
  1354. exit;
  1355. end;
  1356. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  1357. begin
  1358. exponent:=exponent*10;
  1359. exponent:=exponent+ord(s[code])-ord('0');
  1360. inc(code);
  1361. end;
  1362. end;
  1363. { adjust exponent based on decimal point }
  1364. if esign>0 then
  1365. begin
  1366. dec(exponent,decpoint);
  1367. if (exponent<0) then
  1368. begin
  1369. esign:=-1;
  1370. exponent:=-exponent;
  1371. end
  1372. end
  1373. else
  1374. inc(exponent,decpoint);
  1375. { evaluate sign }
  1376. { (before exponent, because the exponent may turn it into a denormal) }
  1377. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*sign;
  1378. { Calculate Exponent }
  1379. hd:=1.0;
  1380. { the magnitude range maximum (normal) is lower in absolute value than the }
  1381. { the magnitude range minimum (denormal). E.g. an extended value can go }
  1382. { up to 1E4932, but "down" to 1E-4951. So make sure that we don't try to }
  1383. { calculate 1E4951 as factor, since that would overflow and result in 0. }
  1384. if (exponent>valmaxexpnorm-2) then
  1385. begin
  1386. hd:=mul_by_power10(hd,valmaxexpnorm-2);
  1387. if esign>0 then
  1388. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1389. else
  1390. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1391. dec(exponent,valmaxexpnorm-2);
  1392. hd:=1.0;
  1393. end;
  1394. hd:=mul_by_power10(hd,exponent);
  1395. if esign>0 then
  1396. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1397. else
  1398. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1399. { Not all characters are read ? }
  1400. if length(s)>=code then
  1401. begin
  1402. fpc_Val_Real_ShortStr:=0.0;
  1403. exit;
  1404. end;
  1405. { success ! }
  1406. code:=0;
  1407. end;
  1408. {$endif}
  1409. {$ifndef FPC_STR_ENUM_INTERN}
  1410. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_SHORTSTR']; compilerproc;
  1411. function string_compare(const s1,s2:shortstring):sizeint;
  1412. {We cannot use the > and < operators to compare a string here, because we if the string is
  1413. not found in the enum, we need to return the position of error in "code". Code equals the
  1414. highest matching character of all string compares, which is only known inside the string
  1415. comparison.}
  1416. var i,l:byte;
  1417. c1,c2:char;
  1418. begin
  1419. l:=length(s1);
  1420. if length(s1)>length(s2) then
  1421. l:=length(s2);
  1422. i:=1;
  1423. while i<=l do
  1424. begin
  1425. c1:=s1[i];
  1426. c2:=s2[i];
  1427. if c1<>c2 then
  1428. break;
  1429. inc(i);
  1430. end;
  1431. if i>code then
  1432. code:=i;
  1433. if i<=l then
  1434. string_compare:=byte(c1)-byte(c2)
  1435. else
  1436. string_compare:=length(s1)-length(s2);
  1437. end;
  1438. type Psorted_array=^Tsorted_array;
  1439. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1440. o:longint;
  1441. s:Pstring;
  1442. end;
  1443. Pstring_to_ord=^Tstring_to_ord;
  1444. Tstring_to_ord={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1445. count:longint;
  1446. data:array[0..0] of Tsorted_array;
  1447. end;
  1448. var l,h,m:cardinal;
  1449. c:sizeint;
  1450. sorted_array:^Tsorted_array;
  1451. spaces:byte;
  1452. t:shortstring;
  1453. begin
  1454. {Val for numbers accepts spaces at the start, so lets do the same
  1455. for enums. Skip spaces at the start of the string.}
  1456. spaces:=1;
  1457. code:=1;
  1458. while (spaces<=length(s)) and (s[spaces]=' ') do
  1459. inc(spaces);
  1460. t:=upcase(copy(s,spaces,255));
  1461. sorted_array:=pointer(@Pstring_to_ord(str2ordindex)^.data);
  1462. {Use a binary search to get the string.}
  1463. l:=1;
  1464. h:=Pstring_to_ord(str2ordindex)^.count;
  1465. repeat
  1466. m:=(l+h) div 2;
  1467. c:=string_compare(t,upcase(sorted_array[m-1].s^));
  1468. if c>0 then
  1469. l:=m+1
  1470. else if c<0 then
  1471. h:=m-1
  1472. else
  1473. break;
  1474. if l>h then
  1475. begin
  1476. {Not found...}
  1477. inc(code,spaces-1); {Add skipped spaces again.}
  1478. {The result of val in case of error is undefined, don't assign a function result.}
  1479. exit;
  1480. end;
  1481. until false;
  1482. code:=0;
  1483. fpc_val_enum_shortstr:=sorted_array[m-1].o;
  1484. end;
  1485. {Redeclare fpc_val_enum_shortstr for internal use in the system unit.}
  1486. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint;external name 'FPC_VAL_ENUM_SHORTSTR';
  1487. {$endif FPC_STR_ENUM_INTERN}
  1488. function fpc_Val_Currency_ShortStr(const s : shortstring; out Code : ValSInt): currency; [public, alias:'FPC_VAL_CURRENCY_SHORTSTR']; compilerproc;
  1489. const
  1490. MaxInt64 : Int64 = $7FFFFFFFFFFFFFFF;
  1491. Int64Edge : Int64 = ($7FFFFFFFFFFFFFFF - 10) div 10;
  1492. Int64Edge2 : Int64 = $7FFFFFFFFFFFFFFF div 10;
  1493. var
  1494. { to enable taking the address on the JVM target }
  1495. res : array[0..0] of Int64;
  1496. i,j,power,sign,len : longint;
  1497. FracOverflow : boolean;
  1498. begin
  1499. fpc_Val_Currency_ShortStr:=0;
  1500. res[0]:=0;
  1501. len:=Length(s);
  1502. Code:=1;
  1503. sign:=1;
  1504. power:=0;
  1505. while True do
  1506. if Code > len then
  1507. exit
  1508. else
  1509. if s[Code] in [' ', #9] then
  1510. Inc(Code)
  1511. else
  1512. break;
  1513. { Read sign }
  1514. case s[Code] of
  1515. '+' : Inc(Code);
  1516. '-' : begin
  1517. sign:=-1;
  1518. inc(code);
  1519. end;
  1520. end;
  1521. { Read digits }
  1522. FracOverflow:=False;
  1523. i:=0;
  1524. while Code <= len do
  1525. begin
  1526. case s[Code] of
  1527. '0'..'9':
  1528. begin
  1529. j:=Ord(s[code])-Ord('0');
  1530. { check overflow }
  1531. if (res[0] <= Int64Edge) or (res[0] <= (MaxInt64 - j) div 10) then
  1532. begin
  1533. res[0]:=res[0]*10 + j;
  1534. Inc(i);
  1535. end
  1536. else
  1537. if power = 0 then
  1538. { exit if integer part overflow }
  1539. exit
  1540. else
  1541. begin
  1542. if not FracOverflow and (j >= 5) and (res[0] < MaxInt64) then
  1543. { round if first digit of fractional part overflow }
  1544. Inc(res[0]);
  1545. FracOverflow:=True;
  1546. end;
  1547. end;
  1548. '.':
  1549. begin
  1550. if power = 0 then
  1551. begin
  1552. power:=1;
  1553. i:=0;
  1554. end
  1555. else
  1556. exit;
  1557. end;
  1558. else
  1559. break;
  1560. end;
  1561. Inc(Code);
  1562. end;
  1563. if (i = 0) and (power = 0) then
  1564. exit;
  1565. if power <> 0 then
  1566. power:=i;
  1567. power:=4 - power;
  1568. { Exponent? }
  1569. if Code <= len then
  1570. if s[Code] in ['E', 'e'] then
  1571. begin
  1572. Inc(Code);
  1573. if Code > len then
  1574. exit;
  1575. i:=1;
  1576. case s[Code] of
  1577. '+':
  1578. Inc(Code);
  1579. '-':
  1580. begin
  1581. i:=-1;
  1582. Inc(Code);
  1583. end;
  1584. end;
  1585. { read exponent }
  1586. j:=0;
  1587. while Code <= len do
  1588. if s[Code] in ['0'..'9'] then
  1589. begin
  1590. if j > 4951 then
  1591. exit;
  1592. j:=j*10 + (Ord(s[code])-Ord('0'));
  1593. Inc(Code);
  1594. end
  1595. else
  1596. exit;
  1597. power:=power + j*i;
  1598. end
  1599. else
  1600. exit;
  1601. if power > 0 then
  1602. begin
  1603. for i:=1 to power do
  1604. if res[0] <= Int64Edge2 then
  1605. res[0]:=res[0]*10
  1606. else
  1607. exit;
  1608. end
  1609. else
  1610. for i:=1 to -power do
  1611. begin
  1612. if res[0] <= MaxInt64 - 5 then
  1613. Inc(res[0], 5);
  1614. res[0]:=res[0] div 10;
  1615. end;
  1616. res[0]:=res[0]*sign;
  1617. fpc_Val_Currency_ShortStr:=PCurrency(@res[0])^;
  1618. Code:=0;
  1619. end;
  1620. {$ifndef FPC_HAS_SETSTRING_SHORTSTR}
  1621. {$define FPC_HAS_SETSTRING_SHORTSTR}
  1622. Procedure SetString (Out S : Shortstring; Buf : PChar; Len : SizeInt);
  1623. begin
  1624. If Len > High(S) then
  1625. Len := High(S);
  1626. SetLength(S,Len);
  1627. If Buf<>Nil then
  1628. begin
  1629. Move (Buf[0],S[1],Len);
  1630. end;
  1631. end;
  1632. {$endif FPC_HAS_SETSTRING_SHORTSTR}
  1633. {$ifndef FPC_HAS_COMPARETEXT_SHORTSTR}
  1634. {$define FPC_HAS_COMPARETEXT_SHORTSTR}
  1635. function ShortCompareText(const S1, S2: shortstring): SizeInt;
  1636. var
  1637. c1, c2: Byte;
  1638. i: Integer;
  1639. L1, L2, Count: SizeInt;
  1640. P1, P2: PChar;
  1641. begin
  1642. L1 := Length(S1);
  1643. L2 := Length(S2);
  1644. if L1 > L2 then
  1645. Count := L2
  1646. else
  1647. Count := L1;
  1648. i := 0;
  1649. P1 := @S1[1];
  1650. P2 := @S2[1];
  1651. while i < count do
  1652. begin
  1653. c1 := byte(p1^);
  1654. c2 := byte(p2^);
  1655. if c1 <> c2 then
  1656. begin
  1657. if c1 in [97..122] then
  1658. Dec(c1, 32);
  1659. if c2 in [97..122] then
  1660. Dec(c2, 32);
  1661. if c1 <> c2 then
  1662. Break;
  1663. end;
  1664. Inc(P1); Inc(P2); Inc(I);
  1665. end;
  1666. if i < count then
  1667. ShortCompareText := c1 - c2
  1668. else
  1669. ShortCompareText := L1 - L2;
  1670. end;
  1671. {$endif FPC_HAS_COMPARETEXT_SHORTSTR}