sstrings.inc 39 KB

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