sstrings.inc 43 KB

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