sstrings.inc 54 KB

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