sstrings.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. { fpc_shortstr_sInt must appear before this file is included, because }
  393. { it's used inside real2str.inc and otherwise the searching via the }
  394. { compilerproc name will fail (JM) }
  395. {$ifndef FPUNONE}
  396. {$I real2str.inc}
  397. {$endif}
  398. {$ifndef FPUNONE}
  399. procedure fpc_shortstr_float(d : ValReal;len,fr,rt : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT']; compilerproc;
  400. begin
  401. str_real(len,fr,d,treal_type(rt),s);
  402. end;
  403. {$endif}
  404. {$ifndef FPC_STR_ENUM_INTERN}
  405. function fpc_shortstr_enum_intern(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring): longint;
  406. { The following contains the TTypeInfo/TTypeData records from typinfo.pp
  407. specialized for the tkEnumeration case (and stripped of unused things). }
  408. type
  409. PPstring=^Pstring;
  410. Penum_typeinfo=^Tenum_typeinfo;
  411. Tenum_typeinfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  412. kind:byte; { always tkEnumeration }
  413. num_chars:byte;
  414. chars:array[0..0] of char; { variable length with size of num_chars }
  415. end;
  416. Penum_typedata=^Tenum_typedata;
  417. Tenum_typedata={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  418. ordtype:byte;
  419. { this seemingly extraneous inner record is here for alignment purposes, so
  420. that its data gets aligned properly (if FPC_REQUIRES_PROPER_ALIGNMENT is
  421. set }
  422. inner: {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  423. minvalue,maxvalue:longint;
  424. basetype:pointer; { required for alignment }
  425. end;
  426. { more data here, but not needed }
  427. end;
  428. { Pascal data types for the ordinal enum value to string table. It consists of a header
  429. that indicates what type of data the table stores, either a direct lookup table (when
  430. o = lookup) or a set of ordered (ordinal value, string) tuples (when o = search). }
  431. { A single entry in the set of ordered tuples }
  432. Psearch_data=^Tsearch_data;
  433. Tsearch_data={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  434. value:longint;
  435. name:Pstring;
  436. end;
  437. Penum_ord_to_string=^Tenum_ord_to_string;
  438. Tenum_ord_to_string={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  439. o:(lookup,search);
  440. case integer of
  441. 0: (lookup_data:array[0..0] of Pstring);
  442. 1: (num_entries:longint;
  443. search_data:array[0..0] of Tsearch_data);
  444. end;
  445. var
  446. enum_o2s : Penum_ord_to_string;
  447. header:Penum_typeinfo;
  448. body:Penum_typedata;
  449. res:Pshortstring;
  450. sorted_data:Psearch_data;
  451. spaces,i,m,h,l:longint;
  452. begin
  453. { set default return value }
  454. fpc_shortstr_enum_intern:=107;
  455. enum_o2s:=Penum_ord_to_string(ord2strindex);
  456. { depending on the type of table in ord2strindex retrieve the data }
  457. if (enum_o2s^.o=lookup) then
  458. begin
  459. { direct lookup table }
  460. header:=Penum_typeinfo(typinfo);
  461. { calculate address of enum rtti body: add the actual size of the
  462. enum_rtti_header, and then align. Use an alignment of 1 (which
  463. does nothing) in case FPC_REQUIRES_PROPER_ALIGNMENT is not set
  464. to avoid the need for an if in this situation }
  465. body:=Penum_typedata(align(ptruint(header) + 2 * sizeof(byte) { kind, num_chars } + header^.num_chars,
  466. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT} 1 {$else} sizeof(pointer) {$endif}));
  467. with (body^.inner) do
  468. begin
  469. { Bounds check for the ordinal value for this enum }
  470. if (ordinal<minvalue) or (ordinal>maxvalue) then
  471. exit;
  472. { make the ordinal index for lookup zero-based }
  473. dec(ordinal,minvalue);
  474. end;
  475. { temporarily disable range checking because of the access to the array[0..0]
  476. member of Tenum_ord_to_string_lookup }
  477. {$push}{$R-}
  478. res:=enum_o2s^.lookup_data[ordinal];
  479. {$pop}
  480. if (not assigned(res)) then
  481. exit;
  482. s:=res^;
  483. end
  484. else
  485. begin
  486. { The compiler did generate a sorted array of (ordvalue,Pstring) tuples }
  487. sorted_data:=@enum_o2s^.search_data;
  488. { Use a binary search to get the string }
  489. l:=0;
  490. { temporarily disable range checking because of the access to the array[0..0]
  491. member of Tenum_ord_to_string_search }
  492. {$push}{$R-}
  493. h:=enum_o2s^.num_entries-1;
  494. repeat
  495. m:=(l+h) div 2;
  496. if ordinal>sorted_data[m].value then
  497. l:=m+1
  498. else if ordinal<sorted_data[m].value then
  499. h:=m-1
  500. else
  501. break;
  502. if l>h then
  503. exit; { Ordinal value not found? Exit }
  504. until false;
  505. {$pop}
  506. s:=sorted_data[m].name^;
  507. end;
  508. { Pad the string with spaces if necessary }
  509. if (len>length(s)) then
  510. begin
  511. spaces:=len-length(s);
  512. for i:=1 to spaces do
  513. s[length(s)+i]:=' ';
  514. inc(byte(s[0]),spaces);
  515. end;
  516. fpc_shortstr_enum_intern:=0;
  517. end;
  518. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_SHORTSTR_ENUM'];compilerproc;
  519. var
  520. res: longint;
  521. begin
  522. res:=fpc_shortstr_enum_intern(ordinal,len,typinfo,ord2strindex,s);
  523. if (res<>0) then
  524. runerror(107);
  525. end;
  526. { also define alias for internal use in the system unit }
  527. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);external name 'FPC_SHORTSTR_ENUM';
  528. {$endif FPC_SHORTSTR_ENUM_INTERN}
  529. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);[public,alias:'FPC_SHORTSTR_BOOL'];compilerproc;
  530. begin
  531. if b then
  532. s:='TRUE'
  533. else
  534. s:='FALSE';
  535. if length(s)<len then
  536. s:=space(len-length(s))+s;
  537. end;
  538. { also define alias for internal use in the system unit }
  539. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);external {$ifndef cpujvm}name 'FPC_SHORTSTR_BOOL'{$endif};
  540. procedure fpc_shortstr_currency({$ifdef cpujvm}constref{$endif} c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
  541. const
  542. MinLen = 8; { Minimal string length in scientific format }
  543. var
  544. buf : array[1..19] of char;
  545. i,j,k,reslen,tlen,sign,r,point : longint;
  546. ic : qword;
  547. begin
  548. fillchar(buf,length(buf),'0');
  549. { default value for length is -32767 }
  550. if len=-32767 then
  551. len:=25;
  552. if PInt64(@c)^ >= 0 then
  553. begin
  554. ic:=QWord(PInt64(@c)^);
  555. sign:=0;
  556. end
  557. else
  558. begin
  559. sign:=1;
  560. ic:=QWord(-PInt64(@c)^);
  561. end;
  562. { converting to integer string }
  563. tlen:=0;
  564. repeat
  565. Inc(tlen);
  566. buf[tlen]:=Chr(ic mod 10 + $30);
  567. ic:=ic div 10;
  568. until ic = 0;
  569. { calculating:
  570. reslen - length of result string,
  571. r - rounding or appending zeroes,
  572. point - place of decimal point }
  573. reslen:=tlen;
  574. if f <> 0 then
  575. Inc(reslen); { adding decimal point length }
  576. if f < 0 then
  577. begin
  578. { scientific format }
  579. Inc(reslen,5); { adding length of sign and exponent }
  580. if len < MinLen then
  581. len:=MinLen;
  582. r:=reslen-len;
  583. if reslen < len then
  584. reslen:=len;
  585. if r > 0 then
  586. begin
  587. reslen:=len;
  588. point:=tlen - r;
  589. end
  590. else
  591. point:=tlen;
  592. end
  593. else
  594. begin
  595. { fixed format }
  596. Inc(reslen, sign);
  597. { prepending fractional part with zeroes }
  598. while tlen < 5 do
  599. begin
  600. Inc(reslen);
  601. Inc(tlen);
  602. buf[tlen]:='0';
  603. end;
  604. { Currency have 4 digits in fractional part }
  605. r:=4 - f;
  606. point:=f;
  607. if point <> 0 then
  608. begin
  609. if point > 4 then
  610. point:=4;
  611. Inc(point);
  612. end;
  613. Dec(reslen,r);
  614. end;
  615. { rounding string if r > 0 }
  616. if r > 0 then
  617. begin
  618. k := 0;
  619. i := r+2;
  620. if i > tlen then
  621. i := tlen+1;
  622. if buf[i-2] >= '5' then
  623. begin
  624. if buf[i-1] < '9' then
  625. buf[i-1] := chr(ord(buf[i-1])+1)
  626. else
  627. begin
  628. buf[i-1] := '0';
  629. k := 1;
  630. end;
  631. end;
  632. If (k=1) and (buf[i-1]='0') then
  633. begin
  634. { 1.9996 rounded to two decimal digits after the decimal separator must result in
  635. 2.00, i.e. the rounding is propagated
  636. }
  637. while buf[i]='9' do
  638. begin
  639. buf[i]:='0';
  640. inc(i);
  641. end;
  642. buf[i]:=chr(Ord(buf[i])+1);
  643. { did we add another digit? This happens when rounding
  644. e.g. 99.9996 to two decimal digits after the decimal separator which should result in
  645. 100.00
  646. }
  647. if i>tlen then
  648. begin
  649. inc(reslen);
  650. inc(tlen);
  651. end;
  652. end;
  653. end;
  654. { preparing result string }
  655. if reslen<len then
  656. reslen:=len;
  657. if reslen>High(s) then
  658. begin
  659. if r < 0 then
  660. Inc(r, reslen - High(s));
  661. reslen:=High(s);
  662. end;
  663. SetLength(s,reslen);
  664. j:=reslen;
  665. if f<0 then
  666. begin
  667. { writing power of 10 part }
  668. if PInt64(@c)^ = 0 then
  669. k:=0
  670. else
  671. k:=tlen-5;
  672. if k >= 0 then
  673. s[j-2]:='+'
  674. else
  675. begin
  676. s[j-2]:='-';
  677. k:=-k;
  678. end;
  679. s[j]:=Chr(k mod 10 + $30);
  680. Dec(j);
  681. s[j]:=Chr(k div 10 + $30);
  682. Dec(j,2);
  683. s[j]:='E';
  684. Dec(j);
  685. end;
  686. { writing extra zeroes if r < 0 }
  687. while r < 0 do
  688. begin
  689. s[j]:='0';
  690. Dec(j);
  691. Inc(r);
  692. end;
  693. { writing digits and decimal point }
  694. for i:=r + 1 to tlen do
  695. begin
  696. Dec(point);
  697. if point = 0 then
  698. begin
  699. s[j]:='.';
  700. Dec(j);
  701. end;
  702. s[j]:=buf[i];
  703. Dec(j);
  704. end;
  705. { writing sign }
  706. if sign = 1 then
  707. begin
  708. s[j]:='-';
  709. Dec(j);
  710. end;
  711. { writing spaces }
  712. while j > 0 do
  713. begin
  714. s[j]:=' ';
  715. Dec(j);
  716. end;
  717. end;
  718. {
  719. Array Of Char Str() helpers
  720. }
  721. procedure fpc_chararray_sint(v : valsint;len : SizeInt;out a:array of char);compilerproc;
  722. var
  723. ss : shortstring;
  724. maxlen : SizeInt;
  725. begin
  726. int_str(v,ss);
  727. if length(ss)<len then
  728. ss:=space(len-length(ss))+ss;
  729. if length(ss)<high(a)+1 then
  730. maxlen:=length(ss)
  731. else
  732. maxlen:=high(a)+1;
  733. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  734. end;
  735. procedure fpc_chararray_uint(v : valuint;len : SizeInt;out a : array of char);compilerproc;
  736. var
  737. ss : shortstring;
  738. maxlen : SizeInt;
  739. begin
  740. int_str_unsigned(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. {$ifndef CPU64}
  750. procedure fpc_chararray_qword(v : qword;len : SizeInt;out a : array of char);compilerproc;
  751. var
  752. ss : shortstring;
  753. maxlen : SizeInt;
  754. begin
  755. int_str_unsigned(v,ss);
  756. if length(ss)<len then
  757. ss:=space(len-length(ss))+ss;
  758. if length(ss)<high(a)+1 then
  759. maxlen:=length(ss)
  760. else
  761. maxlen:=high(a)+1;
  762. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  763. end;
  764. procedure fpc_chararray_int64(v : int64;len : SizeInt;out a : array of char);compilerproc;
  765. var
  766. ss : shortstring;
  767. maxlen : SizeInt;
  768. begin
  769. int_str(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. {$endif CPU64}
  779. {$ifndef FPUNONE}
  780. procedure fpc_chararray_Float(d : ValReal;len,fr,rt : SizeInt;out a : array of char);compilerproc;
  781. var
  782. ss : shortstring;
  783. maxlen : SizeInt;
  784. begin
  785. str_real(len,fr,d,treal_type(rt),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}
  793. {$ifndef FPC_STR_ENUM_INTERN}
  794. procedure fpc_chararray_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out a : array of char);compilerproc;
  795. var
  796. ss : shortstring;
  797. maxlen : SizeInt;
  798. begin
  799. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  800. if length(ss)<high(a)+1 then
  801. maxlen:=length(ss)
  802. else
  803. maxlen:=high(a)+1;
  804. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  805. end;
  806. {$endif not FPC_STR_ENUM_INTERN}
  807. procedure fpc_chararray_bool(b : boolean;len:sizeint;out a : array of char);compilerproc;
  808. var
  809. ss : shortstring;
  810. maxlen : SizeInt;
  811. begin
  812. fpc_shortstr_bool(b,len,ss);
  813. if length(ss)<high(a)+1 then
  814. maxlen:=length(ss)
  815. else
  816. maxlen:=high(a)+1;
  817. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  818. end;
  819. {$ifndef FPC_HAS_CHARARRAY_CURRENCY}
  820. {$define FPC_HAS_CHARARRAY_CURRENCY}
  821. procedure fpc_chararray_Currency(c : Currency;len,fr : SizeInt;out a : array of char);compilerproc;
  822. var
  823. ss : shortstring;
  824. maxlen : SizeInt;
  825. begin
  826. str(c:len:fr,ss);
  827. if length(ss)<high(a)+1 then
  828. maxlen:=length(ss)
  829. else
  830. maxlen:=high(a)+1;
  831. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  832. end;
  833. {$endif FPC_HAS_STR_CURRENCY}
  834. {*****************************************************************************
  835. Val() Functions
  836. *****************************************************************************}
  837. Function InitVal(const s:shortstring;out negativ:boolean;out base:byte):ValSInt;
  838. var
  839. Code : SizeInt;
  840. begin
  841. code:=1;
  842. negativ:=false;
  843. base:=10;
  844. if length(s)=0 then
  845. begin
  846. InitVal:=code;
  847. Exit;
  848. end;
  849. {Skip Spaces and Tab}
  850. while (code<=length(s)) and (s[code] in [' ',#9]) do
  851. inc(code);
  852. {Sign}
  853. case s[code] of
  854. '-' : begin
  855. negativ:=true;
  856. inc(code);
  857. end;
  858. '+' : inc(code);
  859. end;
  860. {Base}
  861. if code<=length(s) then
  862. begin
  863. case s[code] of
  864. '$',
  865. 'X',
  866. 'x' : begin
  867. base:=16;
  868. inc(code);
  869. end;
  870. '%' : begin
  871. base:=2;
  872. inc(code);
  873. end;
  874. '&' : begin
  875. Base:=8;
  876. inc(code);
  877. end;
  878. '0' : begin
  879. if (code < length(s)) and (s[code+1] in ['x', 'X']) then
  880. begin
  881. inc(code, 2);
  882. base := 16;
  883. end;
  884. end;
  885. end;
  886. end;
  887. { strip leading zeros }
  888. while ((code < length(s)) and (s[code] = '0')) do begin
  889. inc(code);
  890. end;
  891. InitVal:=code;
  892. end;
  893. Function fpc_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR']; compilerproc;
  894. var
  895. temp, prev, maxPrevValue, maxNewValue: ValUInt;
  896. base,u : byte;
  897. negative : boolean;
  898. begin
  899. fpc_Val_SInt_ShortStr := 0;
  900. Temp:=0;
  901. Code:=InitVal(s,negative,base);
  902. if Code>length(s) then
  903. exit;
  904. if (s[Code]=#0) then
  905. begin
  906. if (Code>1) and (s[Code-1]='0') then
  907. Code:=0;
  908. exit;
  909. end;
  910. maxPrevValue := ValUInt(MaxUIntValue) div ValUInt(Base);
  911. if (base = 10) then
  912. maxNewValue := MaxSIntValue + ord(negative)
  913. else
  914. maxNewValue := MaxUIntValue;
  915. while Code<=Length(s) do
  916. begin
  917. case s[Code] of
  918. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  919. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  920. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  921. #0 : break;
  922. else
  923. u:=16;
  924. end;
  925. Prev := Temp;
  926. Temp := Temp*ValUInt(base);
  927. If (u >= base) or
  928. (ValUInt(maxNewValue-u) < Temp) or
  929. (prev > maxPrevValue) Then
  930. Begin
  931. fpc_Val_SInt_ShortStr := 0;
  932. Exit
  933. End;
  934. Temp:=Temp+u;
  935. inc(code);
  936. end;
  937. code := 0;
  938. fpc_Val_SInt_ShortStr := ValSInt(Temp);
  939. If Negative Then
  940. fpc_Val_SInt_ShortStr := -fpc_Val_SInt_ShortStr;
  941. If Not(Negative) and (base <> 10) Then
  942. {sign extend the result to allow proper range checking}
  943. Case DestSize of
  944. 1: fpc_Val_SInt_ShortStr := shortint(fpc_Val_SInt_ShortStr);
  945. 2: fpc_Val_SInt_ShortStr := smallint(fpc_Val_SInt_ShortStr);
  946. {$ifdef cpu64}
  947. 4: fpc_Val_SInt_ShortStr := longint(fpc_Val_SInt_ShortStr);
  948. {$endif cpu64}
  949. End;
  950. end;
  951. {$ifndef FPC_HAS_INT_VAL_SINT_SHORTSTR}
  952. {$define FPC_HAS_INT_VAL_SINT_SHORTSTR}
  953. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  954. { we have to pass the DestSize parameter on (JM) }
  955. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'FPC_VAL_SINT_SHORTSTR'];
  956. {$endif FPC_HAS_INT_VAL_SINT_SHORTSTR}
  957. Function fpc_Val_UInt_Shortstr(Const S: ShortString; out Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR']; compilerproc;
  958. var
  959. prev : ValUInt;
  960. base,u : byte;
  961. negative : boolean;
  962. begin
  963. fpc_Val_UInt_Shortstr:=0;
  964. Code:=InitVal(s,negative,base);
  965. If Negative or (Code>length(s)) Then
  966. Exit;
  967. if (s[Code]=#0) then
  968. begin
  969. if (Code>1) and (s[Code-1]='0') then
  970. Code:=0;
  971. exit;
  972. end;
  973. while Code<=Length(s) do
  974. begin
  975. case s[Code] of
  976. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  977. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  978. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  979. #0 : break;
  980. else
  981. u:=16;
  982. end;
  983. prev := fpc_Val_UInt_Shortstr;
  984. If (u>=base) or
  985. (ValUInt(MaxUIntValue-u) div ValUInt(Base)<prev) then
  986. begin
  987. fpc_Val_UInt_Shortstr:=0;
  988. exit;
  989. end;
  990. fpc_Val_UInt_Shortstr:=fpc_Val_UInt_Shortstr*ValUInt(base) + u;
  991. inc(code);
  992. end;
  993. code := 0;
  994. end;
  995. {$ifndef CPU64}
  996. Function fpc_val_int64_shortstr(Const S: ShortString; out Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR']; compilerproc;
  997. var u, temp, prev, maxprevvalue, maxnewvalue : qword;
  998. base : byte;
  999. negative : boolean;
  1000. const maxint64=qword($7fffffffffffffff);
  1001. maxqword=qword($ffffffffffffffff);
  1002. begin
  1003. fpc_val_int64_shortstr := 0;
  1004. Temp:=0;
  1005. Code:=InitVal(s,negative,base);
  1006. if Code>length(s) then
  1007. exit;
  1008. if (s[Code]=#0) then
  1009. begin
  1010. if (Code>1) and (s[Code-1]='0') then
  1011. Code:=0;
  1012. exit;
  1013. end;
  1014. maxprevvalue := maxqword div base;
  1015. if (base = 10) then
  1016. maxnewvalue := maxint64 + ord(negative)
  1017. else
  1018. maxnewvalue := maxqword;
  1019. while Code<=Length(s) do
  1020. begin
  1021. case s[Code] of
  1022. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1023. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1024. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1025. #0 : break;
  1026. else
  1027. u:=16;
  1028. end;
  1029. Prev:=Temp;
  1030. Temp:=Temp*qword(base);
  1031. If (u >= base) or
  1032. (qword(maxnewvalue-u) < temp) or
  1033. (prev > maxprevvalue) Then
  1034. Begin
  1035. fpc_val_int64_shortstr := 0;
  1036. Exit
  1037. End;
  1038. Temp:=Temp+u;
  1039. inc(code);
  1040. end;
  1041. code:=0;
  1042. fpc_val_int64_shortstr:=int64(Temp);
  1043. If Negative Then
  1044. fpc_val_int64_shortstr:=-fpc_val_int64_shortstr;
  1045. end;
  1046. Function fpc_val_qword_shortstr(Const S: ShortString; out Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR']; compilerproc;
  1047. var u, prev: QWord;
  1048. base : byte;
  1049. negative : boolean;
  1050. const maxqword=qword($ffffffffffffffff);
  1051. begin
  1052. fpc_val_qword_shortstr:=0;
  1053. Code:=InitVal(s,negative,base);
  1054. If Negative or (Code>length(s)) Then
  1055. Exit;
  1056. if (s[Code]=#0) then
  1057. begin
  1058. if (Code>1) and (s[Code-1]='0') then
  1059. Code:=0;
  1060. exit;
  1061. end;
  1062. while Code<=Length(s) do
  1063. begin
  1064. case s[Code] of
  1065. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1066. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1067. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1068. #0 : break;
  1069. else
  1070. u:=16;
  1071. end;
  1072. prev := fpc_val_qword_shortstr;
  1073. If (u>=base) or
  1074. ((QWord(maxqword-u) div QWord(base))<prev) then
  1075. Begin
  1076. fpc_val_qword_shortstr := 0;
  1077. Exit
  1078. End;
  1079. fpc_val_qword_shortstr:=fpc_val_qword_shortstr*QWord(base) + u;
  1080. inc(code);
  1081. end;
  1082. code := 0;
  1083. end;
  1084. {$endif CPU64}
  1085. {$ifndef FPUNONE}
  1086. const
  1087. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1088. valmaxexpnorm=4932;
  1089. {$else}
  1090. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1091. valmaxexpnorm=308;
  1092. {$else}
  1093. {$ifdef FPC_HAS_TYPE_SINGLE}
  1094. valmaxexpnorm=38;
  1095. {$else}
  1096. {$error Unknown floating point precision }
  1097. {$endif}
  1098. {$endif}
  1099. {$endif}
  1100. {$endif}
  1101. {$ifndef FPUNONE}
  1102. (******************
  1103. Derived from: ".\Free Pascal\source\rtl\inc\genmath.inc"
  1104. Origin: "fast 10^n routine"
  1105. function FPower10(val: Extended; Power: Longint): Extended;
  1106. Changes:
  1107. > adapted to "ValReal", so float can be single/double/extended
  1108. > slightly changed arrays [redundant 58+2 float constants gone away]
  1109. > added some checks etc..
  1110. Notes:
  1111. > denormalization and overflow should go smooth if corresponding
  1112. FPU exceptions are masked [no external care needed by now]
  1113. > adaption to real48 and real128 is not hard if one needed
  1114. ******************)
  1115. //
  1116. function mul_by_power10(x:ValReal;power:integer):ValReal;
  1117. //
  1118. // result:=X*(10^power)
  1119. //
  1120. // Routine achieves result with no more than 3 floating point mul/div's.
  1121. // Up to ABS(power)=31, only 1 floating point mul/div is needed.
  1122. //
  1123. // Limitations:
  1124. // for ValReal=extended : power=-5119..+5119
  1125. // for ValReal=double : power=-319..+319
  1126. // for ValReal=single : power=-63..+63
  1127. //
  1128. // If "power" is beyond this limits, routine gives up and returns 0/+INF/-INF.
  1129. // This is not generally correct, but should be ok when routine is used only
  1130. // as "VAL"-helper, since "x" exponent is reasonably close to 0 in this case.
  1131. //
  1132. //==================================
  1133. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1134. {$ERROR C_HIGH_EXPBITS_5TO8 declared somewhere in scope}
  1135. {$ENDIF}
  1136. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1137. {$ERROR C_HIGH_EXPBITS_9ANDUP declared somewhere in scope}
  1138. {$ENDIF}
  1139. {$IF SIZEOF(ValReal)=10}
  1140. //==================================
  1141. // assuming "type ValReal=extended;"
  1142. //
  1143. const
  1144. C_MAX_POWER = 5119;
  1145. C_HIGH_EXPBITS_5TO8 = 15;
  1146. C_HIGH_EXPBITS_9ANDUP = 9;
  1147. {$ELSEIF SIZEOF(ValReal)=8}
  1148. //==================================
  1149. // assuming "type ValReal=double;"
  1150. //
  1151. const
  1152. C_MAX_POWER = 319;
  1153. C_HIGH_EXPBITS_5TO8 = 9;
  1154. {$ELSEIF SIZEOF(ValReal)=4}
  1155. //==================================
  1156. // assuming "type ValReal=single;"
  1157. //
  1158. const
  1159. C_MAX_POWER = 63;
  1160. {$ELSE}
  1161. //==================================
  1162. // assuming "ValReal=?"
  1163. //
  1164. {$ERROR Unsupported ValReal type}
  1165. {$ENDIF}
  1166. //==================================
  1167. const
  1168. C_INFTYP = ValReal( 1.0/0.0);
  1169. C_INFTYM = ValReal(-1.0/0.0);
  1170. mul_expbits_0_to_4:packed array[0..31]of ValReal=(
  1171. 1E0, 1E1, 1E2, 1E3,
  1172. 1E4, 1E5, 1E6, 1E7,
  1173. 1E8, 1E9, 1E10, 1E11,
  1174. 1E12, 1E13, 1E14, 1E15,
  1175. 1E16, 1E17, 1E18, 1E19,
  1176. 1E20, 1E21, 1E22, 1E23,
  1177. 1E24, 1E25, 1E26, 1E27,
  1178. 1E28, 1E29, 1E30, 1E31);
  1179. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1180. mul_expbits_5_to_8:packed array[1..C_HIGH_EXPBITS_5TO8] of ValReal=(
  1181. 1E32, 1E64, 1E96, 1E128,
  1182. 1E160, 1E192, 1E224, 1E256, 1E288
  1183. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)},
  1184. 1E320, 1E352, 1E384, 1E416, 1E448, 1E480
  1185. {$ENDIF});
  1186. {$ELSE}
  1187. mul_expbits_5_to_8:ValReal=1E32;
  1188. {$ENDIF}
  1189. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1190. mul_expbits_9_and_up:packed array[1..C_HIGH_EXPBITS_9ANDUP] of ValReal=(
  1191. 1E512, 1E1024, 1E1536, 1E2048,
  1192. 1E2560, 1E3072, 1E3584, 1E4096,
  1193. 1E4608);
  1194. {$ENDIF}
  1195. begin
  1196. if power=0 then mul_by_power10:=x else
  1197. if power<-C_MAX_POWER then mul_by_power10:=0 else
  1198. if power>C_MAX_POWER then
  1199. if x<0 then mul_by_power10:=C_INFTYM else
  1200. if x>0 then mul_by_power10:=C_INFTYP else mul_by_power10:=0
  1201. else
  1202. if power<0 then
  1203. begin
  1204. power:=-power;
  1205. mul_by_power10:=x/mul_expbits_0_to_4[power and $1F];
  1206. power:=(power shr 5);
  1207. if power=0 then exit;
  1208. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1209. if power and $F<>0 then
  1210. mul_by_power10:=
  1211. mul_by_power10/mul_expbits_5_to_8[power and $F];
  1212. {$ELSE} // "single", power<>0, so always div
  1213. mul_by_power10:=mul_by_power10/mul_expbits_5_to_8;
  1214. {$ENDIF}
  1215. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1216. power:=(power shr 4);
  1217. if power<>0 then
  1218. mul_by_power10:=
  1219. mul_by_power10/mul_expbits_9_and_up[power];
  1220. {$ENDIF}
  1221. end
  1222. else
  1223. begin
  1224. mul_by_power10:=x*mul_expbits_0_to_4[power and $1F];
  1225. power:=(power shr 5);
  1226. if power=0 then exit;
  1227. {$IF DECLARED(C_HIGH_EXPBITS_5TO8)}
  1228. if power and $F<>0 then
  1229. mul_by_power10:=
  1230. mul_by_power10*mul_expbits_5_to_8[power and $F];
  1231. {$ELSE} // "single", power<>0, so always mul
  1232. mul_by_power10:=mul_by_power10*mul_expbits_5_to_8;
  1233. {$ENDIF}
  1234. {$IF DECLARED(C_HIGH_EXPBITS_9ANDUP)}
  1235. power:=(power shr 4);
  1236. if power<>0 then
  1237. mul_by_power10:=
  1238. mul_by_power10*mul_expbits_9_and_up[power];
  1239. {$ENDIF}
  1240. end;
  1241. end;
  1242. Function fpc_Val_Real_ShortStr(const s : shortstring; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR']; compilerproc;
  1243. var
  1244. hd,
  1245. sign : valreal;
  1246. esign,
  1247. exponent,
  1248. decpoint : SizeInt;
  1249. flags : byte;
  1250. begin
  1251. fpc_Val_Real_ShortStr:=0.0;
  1252. code:=1;
  1253. exponent:=0;
  1254. decpoint:=0;
  1255. esign:=1;
  1256. flags:=0;
  1257. sign:=1;
  1258. while (code<=length(s)) and (s[code] in [' ',#9]) do
  1259. inc(code);
  1260. if code<=length(s) then
  1261. case s[code] of
  1262. '+' : inc(code);
  1263. '-' : begin
  1264. sign:=-1;
  1265. inc(code);
  1266. end;
  1267. end;
  1268. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  1269. begin
  1270. { Read integer part }
  1271. flags:=flags or 1;
  1272. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  1273. inc(code);
  1274. end;
  1275. { Decimal ? }
  1276. if (length(s)>=code) and (s[code]='.') then
  1277. begin
  1278. inc(code);
  1279. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  1280. begin
  1281. { Read fractional part. }
  1282. flags:=flags or 2;
  1283. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  1284. inc(decpoint);
  1285. inc(code);
  1286. end;
  1287. end;
  1288. { Again, read integer and fractional part}
  1289. if flags=0 then
  1290. begin
  1291. fpc_Val_Real_ShortStr:=0.0;
  1292. exit;
  1293. end;
  1294. { Exponent ? }
  1295. if (length(s)>=code) and (s[code] in ['e','E']) then
  1296. begin
  1297. inc(code);
  1298. if Length(s) >= code then
  1299. if s[code]='+' then
  1300. inc(code)
  1301. else
  1302. if s[code]='-' then
  1303. begin
  1304. esign:=-1;
  1305. inc(code);
  1306. end;
  1307. if (length(s)<code) or not(s[code] in ['0'..'9']) then
  1308. begin
  1309. fpc_Val_Real_ShortStr:=0.0;
  1310. exit;
  1311. end;
  1312. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  1313. begin
  1314. exponent:=exponent*10;
  1315. exponent:=exponent+ord(s[code])-ord('0');
  1316. inc(code);
  1317. end;
  1318. end;
  1319. { adjust exponent based on decimal point }
  1320. if esign>0 then
  1321. begin
  1322. dec(exponent,decpoint);
  1323. if (exponent<0) then
  1324. begin
  1325. esign:=-1;
  1326. exponent:=-exponent;
  1327. end
  1328. end
  1329. else
  1330. inc(exponent,decpoint);
  1331. { evaluate sign }
  1332. { (before exponent, because the exponent may turn it into a denormal) }
  1333. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*sign;
  1334. { Calculate Exponent }
  1335. hd:=1.0;
  1336. { the magnitude range maximum (normal) is lower in absolute value than the }
  1337. { the magnitude range minimum (denormal). E.g. an extended value can go }
  1338. { up to 1E4932, but "down" to 1E-4951. So make sure that we don't try to }
  1339. { calculate 1E4951 as factor, since that would overflow and result in 0. }
  1340. if (exponent>valmaxexpnorm-2) then
  1341. begin
  1342. hd:=mul_by_power10(hd,valmaxexpnorm-2);
  1343. if esign>0 then
  1344. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1345. else
  1346. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1347. dec(exponent,valmaxexpnorm-2);
  1348. hd:=1.0;
  1349. end;
  1350. hd:=mul_by_power10(hd,exponent);
  1351. if esign>0 then
  1352. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  1353. else
  1354. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  1355. { Not all characters are read ? }
  1356. if length(s)>=code then
  1357. begin
  1358. fpc_Val_Real_ShortStr:=0.0;
  1359. exit;
  1360. end;
  1361. { success ! }
  1362. code:=0;
  1363. end;
  1364. {$endif}
  1365. {$ifndef FPC_STR_ENUM_INTERN}
  1366. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_SHORTSTR']; compilerproc;
  1367. function string_compare(const s1,s2:shortstring):sizeint;
  1368. {We cannot use the > and < operators to compare a string here, because we if the string is
  1369. not found in the enum, we need to return the position of error in "code". Code equals the
  1370. highest matching character of all string compares, which is only known inside the string
  1371. comparison.}
  1372. var i,l:byte;
  1373. c1,c2:char;
  1374. begin
  1375. l:=length(s1);
  1376. if length(s1)>length(s2) then
  1377. l:=length(s2);
  1378. i:=1;
  1379. while i<=l do
  1380. begin
  1381. c1:=s1[i];
  1382. c2:=s2[i];
  1383. if c1<>c2 then
  1384. break;
  1385. inc(i);
  1386. end;
  1387. if i>code then
  1388. code:=i;
  1389. if i<=l then
  1390. string_compare:=byte(c1)-byte(c2)
  1391. else
  1392. string_compare:=length(s1)-length(s2);
  1393. end;
  1394. type Psorted_array=^Tsorted_array;
  1395. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1396. o:longint;
  1397. s:Pstring;
  1398. end;
  1399. Pstring_to_ord=^Tstring_to_ord;
  1400. Tstring_to_ord={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1401. count:longint;
  1402. data:array[0..0] of Tsorted_array;
  1403. end;
  1404. var l,h,m:cardinal;
  1405. c:sizeint;
  1406. sorted_array:^Tsorted_array;
  1407. spaces:byte;
  1408. t:shortstring;
  1409. begin
  1410. {Val for numbers accepts spaces at the start, so lets do the same
  1411. for enums. Skip spaces at the start of the string.}
  1412. spaces:=1;
  1413. code:=1;
  1414. while (spaces<=length(s)) and (s[spaces]=' ') do
  1415. inc(spaces);
  1416. t:=upcase(copy(s,spaces,255));
  1417. sorted_array:=pointer(@Pstring_to_ord(str2ordindex)^.data);
  1418. {Use a binary search to get the string.}
  1419. l:=1;
  1420. h:=Pstring_to_ord(str2ordindex)^.count;
  1421. repeat
  1422. m:=(l+h) div 2;
  1423. c:=string_compare(t,upcase(sorted_array[m-1].s^));
  1424. if c>0 then
  1425. l:=m+1
  1426. else if c<0 then
  1427. h:=m-1
  1428. else
  1429. break;
  1430. if l>h then
  1431. begin
  1432. {Not found...}
  1433. inc(code,spaces-1); {Add skipped spaces again.}
  1434. {The result of val in case of error is undefined, don't assign a function result.}
  1435. exit;
  1436. end;
  1437. until false;
  1438. code:=0;
  1439. fpc_val_enum_shortstr:=sorted_array[m-1].o;
  1440. end;
  1441. {Redeclare fpc_val_enum_shortstr for internal use in the system unit.}
  1442. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint;external name 'FPC_VAL_ENUM_SHORTSTR';
  1443. {$endif FPC_STR_ENUM_INTERN}
  1444. function fpc_Val_Currency_ShortStr(const s : shortstring; out Code : ValSInt): currency; [public, alias:'FPC_VAL_CURRENCY_SHORTSTR']; compilerproc;
  1445. const
  1446. MaxInt64 : Int64 = $7FFFFFFFFFFFFFFF;
  1447. Int64Edge : Int64 = ($7FFFFFFFFFFFFFFF - 10) div 10;
  1448. Int64Edge2 : Int64 = $7FFFFFFFFFFFFFFF div 10;
  1449. var
  1450. { to enable taking the address on the JVM target }
  1451. res : array[0..0] of Int64;
  1452. i,j,power,sign,len : longint;
  1453. FracOverflow : boolean;
  1454. begin
  1455. fpc_Val_Currency_ShortStr:=0;
  1456. res[0]:=0;
  1457. len:=Length(s);
  1458. Code:=1;
  1459. sign:=1;
  1460. power:=0;
  1461. while True do
  1462. if Code > len then
  1463. exit
  1464. else
  1465. if s[Code] in [' ', #9] then
  1466. Inc(Code)
  1467. else
  1468. break;
  1469. { Read sign }
  1470. case s[Code] of
  1471. '+' : Inc(Code);
  1472. '-' : begin
  1473. sign:=-1;
  1474. inc(code);
  1475. end;
  1476. end;
  1477. { Read digits }
  1478. FracOverflow:=False;
  1479. i:=0;
  1480. while Code <= len do
  1481. begin
  1482. case s[Code] of
  1483. '0'..'9':
  1484. begin
  1485. j:=Ord(s[code])-Ord('0');
  1486. { check overflow }
  1487. if (res[0] <= Int64Edge) or (res[0] <= (MaxInt64 - j) div 10) then
  1488. begin
  1489. res[0]:=res[0]*10 + j;
  1490. Inc(i);
  1491. end
  1492. else
  1493. if power = 0 then
  1494. { exit if integer part overflow }
  1495. exit
  1496. else
  1497. begin
  1498. if not FracOverflow and (j >= 5) and (res[0] < MaxInt64) then
  1499. { round if first digit of fractional part overflow }
  1500. Inc(res[0]);
  1501. FracOverflow:=True;
  1502. end;
  1503. end;
  1504. '.':
  1505. begin
  1506. if power = 0 then
  1507. begin
  1508. power:=1;
  1509. i:=0;
  1510. end
  1511. else
  1512. exit;
  1513. end;
  1514. else
  1515. break;
  1516. end;
  1517. Inc(Code);
  1518. end;
  1519. if (i = 0) and (power = 0) then
  1520. exit;
  1521. if power <> 0 then
  1522. power:=i;
  1523. power:=4 - power;
  1524. { Exponent? }
  1525. if Code <= len then
  1526. if s[Code] in ['E', 'e'] then
  1527. begin
  1528. Inc(Code);
  1529. if Code > len then
  1530. exit;
  1531. i:=1;
  1532. case s[Code] of
  1533. '+':
  1534. Inc(Code);
  1535. '-':
  1536. begin
  1537. i:=-1;
  1538. Inc(Code);
  1539. end;
  1540. end;
  1541. { read exponent }
  1542. j:=0;
  1543. while Code <= len do
  1544. if s[Code] in ['0'..'9'] then
  1545. begin
  1546. if j > 4951 then
  1547. exit;
  1548. j:=j*10 + (Ord(s[code])-Ord('0'));
  1549. Inc(Code);
  1550. end
  1551. else
  1552. exit;
  1553. power:=power + j*i;
  1554. end
  1555. else
  1556. exit;
  1557. if power > 0 then
  1558. begin
  1559. for i:=1 to power do
  1560. if res[0] <= Int64Edge2 then
  1561. res[0]:=res[0]*10
  1562. else
  1563. exit;
  1564. end
  1565. else
  1566. for i:=1 to -power do
  1567. begin
  1568. if res[0] <= MaxInt64 - 5 then
  1569. Inc(res[0], 5);
  1570. res[0]:=res[0] div 10;
  1571. end;
  1572. res[0]:=res[0]*sign;
  1573. fpc_Val_Currency_ShortStr:=PCurrency(@res[0])^;
  1574. Code:=0;
  1575. end;
  1576. {$ifndef FPC_HAS_SETSTRING_SHORTSTR}
  1577. {$define FPC_HAS_SETSTRING_SHORTSTR}
  1578. Procedure SetString (Out S : Shortstring; Buf : PChar; Len : SizeInt);
  1579. begin
  1580. If Len > High(S) then
  1581. Len := High(S);
  1582. SetLength(S,Len);
  1583. If Buf<>Nil then
  1584. begin
  1585. Move (Buf[0],S[1],Len);
  1586. end;
  1587. end;
  1588. {$endif FPC_HAS_SETSTRING_SHORTSTR}
  1589. {$ifndef FPC_HAS_COMPARETEXT_SHORTSTR}
  1590. {$define FPC_HAS_COMPARETEXT_SHORTSTR}
  1591. function ShortCompareText(const S1, S2: shortstring): SizeInt;
  1592. var
  1593. c1, c2: Byte;
  1594. i: Integer;
  1595. L1, L2, Count: SizeInt;
  1596. P1, P2: PChar;
  1597. begin
  1598. L1 := Length(S1);
  1599. L2 := Length(S2);
  1600. if L1 > L2 then
  1601. Count := L2
  1602. else
  1603. Count := L1;
  1604. i := 0;
  1605. P1 := @S1[1];
  1606. P2 := @S2[1];
  1607. while i < count do
  1608. begin
  1609. c1 := byte(p1^);
  1610. c2 := byte(p2^);
  1611. if c1 <> c2 then
  1612. begin
  1613. if c1 in [97..122] then
  1614. Dec(c1, 32);
  1615. if c2 in [97..122] then
  1616. Dec(c2, 32);
  1617. if c1 <> c2 then
  1618. Break;
  1619. end;
  1620. Inc(P1); Inc(P2); Inc(I);
  1621. end;
  1622. if i < count then
  1623. ShortCompareText := c1 - c2
  1624. else
  1625. ShortCompareText := L1 - L2;
  1626. end;
  1627. {$endif FPC_HAS_COMPARETEXT_SHORTSTR}