cutils.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# This unit contains some generic support functions which are used
  18. in the different parts of the compiler.
  19. }
  20. unit cutils;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. constexp;
  25. type
  26. Tcharset=set of char;
  27. var
  28. internalerrorproc : procedure(i:longint);
  29. {# Returns the minimal value between @var(a) and @var(b) }
  30. function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  31. function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  32. function min(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  33. {# Returns the maximum value between @var(a) and @var(b) }
  34. function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  35. function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  36. function max(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  37. { These functions are intenionally put here and not in the constexp unit.
  38. Since Tconstexprint may be automatically converted to int, which causes
  39. loss of data and since there are already min and max functions for ints in
  40. this unit, we put min and max for Tconstexprint as well. This way we avoid
  41. potential bugs, caused by code unintentionally calling the int versions of
  42. min/max on Tconstexprint, because of only including cutils and forgetting
  43. the constexp unit in the uses clause. }
  44. function min(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  45. function max(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  46. {# Return value @var(i) aligned on @var(a) boundary }
  47. function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
  48. function align(i,a:int64):int64;{$ifdef USEINLINE}inline;{$endif}
  49. function align(i,a:qword):qword;{$ifdef USEINLINE}inline;{$endif}
  50. { if you have an address aligned using "oldalignment" and add an
  51. offset of (a multiple of) offset to it, this function calculates
  52. the new minimally guaranteed alignment
  53. }
  54. function newalignment(oldalignment: longint; offset: int64): longint;
  55. {# Return @var(b) with the bit order reversed }
  56. function reverse_byte(b: byte): byte;
  57. {# Return @var(w) with the bit order reversed }
  58. function reverse_word(w: word): word;
  59. {# Return @var(l) with the bit order reversed }
  60. function reverse_longword(l: longword): longword;
  61. function next_prime(l: longint): longint;
  62. function used_align(varalign,minalign,maxalign:longint):longint;
  63. function isbetteralignedthan(new, org, limit: cardinal): boolean;
  64. function packedbitsloadsize(bitlen: int64) : int64;
  65. procedure Replace(var s:string;s1:string;const s2:string);
  66. procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
  67. procedure ReplaceCase(var s:string;const s1,s2:string);
  68. procedure ReplaceCase(var s:ansistring;const s1,s2:ansistring);
  69. Function MatchPattern(const pattern,what:string):boolean;
  70. function upper(const c : char) : char;
  71. function upper(const s : string) : string;
  72. function upper(const s : ansistring) : ansistring;
  73. function lower(const c : char) : char;
  74. function lower(const s : string) : string;
  75. function lower(const s : ansistring) : ansistring;
  76. function rpos(const needle: char; const haystack: shortstring): longint; overload;
  77. function rpos(const needle: shortstring; const haystack: shortstring): longint; overload;
  78. function trimspace(const s:string):string;
  79. function trimspace(const s:AnsiString):AnsiString;
  80. function space (b : longint): string;
  81. { returns the position of the first char of the set cs in s, if there is none, then it returns 0 }
  82. function PosCharset(const cs : TCharSet;const s : ansistring) : integer;
  83. function PadSpace(const s:string;len:longint):string;
  84. function PadSpace(const s:AnsiString;len:longint):AnsiString;
  85. function GetToken(var s:string;endchar:char):string;
  86. function GetToken(var s:ansistring;endchar:char):ansistring;
  87. procedure uppervar(var s : string);
  88. function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
  89. function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  90. function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  91. function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  92. function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
  93. function DStr(l:longint):string;
  94. {# Returns true if the string s is a number }
  95. function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
  96. {# Returns true if value is a power of 2, the actual
  97. exponent value is returned in power.
  98. }
  99. function ispowerof2(value : int64;out power : longint) : boolean;
  100. function ispowerof2(const value : Tconstexprint;out power : longint) : boolean;
  101. {# Returns true if abs(value) is a power of 2, the actual
  102. exponent value is returned in power.
  103. }
  104. function isabspowerof2(const value : Tconstexprint; out power : longint) : boolean;
  105. { # Returns the power of 2 >= value }
  106. function nextpowerof2(value : qword; out power: longint) : qword;
  107. function backspace_quote(const s:string;const qchars:Tcharset):string;
  108. function octal_quote(const s:string;const qchars:Tcharset):string;
  109. {# If the string is quoted, in accordance with pascal, it is
  110. dequoted and returned in s, and the function returns true.
  111. If it is not quoted, or if the quoting is bad, s is not touched,
  112. and false is returned.
  113. }
  114. function DePascalQuote(var s: ansistring): Boolean;
  115. function CompareStr(const S1, S2: string): Integer;
  116. function CompareText(S1, S2: string): integer;
  117. { releases the string p and assignes nil to p }
  118. { if p=nil then freemem isn't called }
  119. procedure stringdispose(var p : pshortstring);{$ifdef USEINLINE}inline;{$endif}
  120. { allocates mem for a copy of s, copies s to this mem and returns }
  121. { a pointer to this mem }
  122. function stringdup(const s : shortstring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  123. function stringdup(const s : ansistring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  124. {# Allocates memory for the string @var(s) and copies s as zero
  125. terminated string to that allocated memory and returns a pointer
  126. to that mem
  127. }
  128. function strpnew(const s : string) : pchar;
  129. function strpnew(const s : ansistring) : pchar;
  130. {# makes the character @var(c) lowercase, with spanish, french and german
  131. character set
  132. }
  133. function lowercase(c : char) : char;
  134. { makes zero terminated string to a pascal string }
  135. { the data in p is modified and p is returned }
  136. function pchar2pshortstring(p : pchar) : pshortstring;
  137. { inverse of pchar2pshortstring }
  138. function pshortstring2pchar(p : pshortstring) : pchar;
  139. { allocate a new pchar with the contents of a}
  140. function ansistring2pchar(const a: ansistring) : pchar;
  141. { Ansistring (pchar+length) support }
  142. procedure ansistringdispose(var p : pchar;length : longint);
  143. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  144. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  145. {Lzw encode/decode to compress strings -> save memory.}
  146. function minilzw_encode(const s:string):string;
  147. function minilzw_decode(const s:string):string;
  148. Function nextafter(x,y:double):double;
  149. function LengthUleb128(a: qword) : byte;
  150. function LengthSleb128(a: int64) : byte;
  151. function EncodeUleb128(a: qword;out buf;len: byte) : byte;
  152. function EncodeSleb128(a: int64;out buf;len: byte) : byte;
  153. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  154. const
  155. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  156. implementation
  157. uses
  158. SysUtils;
  159. var
  160. uppertbl,
  161. lowertbl : array[char] of char;
  162. function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  163. {
  164. return the minimal of a and b
  165. }
  166. begin
  167. if a<=b then
  168. min:=a
  169. else
  170. min:=b;
  171. end;
  172. function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  173. {
  174. return the minimal of a and b
  175. }
  176. begin
  177. if a<=b then
  178. min:=a
  179. else
  180. min:=b;
  181. end;
  182. function min(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  183. {
  184. return the minimal of a and b
  185. }
  186. begin
  187. if a<=b then
  188. min:=a
  189. else
  190. min:=b;
  191. end;
  192. function min(a,b : qword) : qword;
  193. {
  194. return the minimal of a and b
  195. }
  196. begin
  197. if a<=b then
  198. min:=a
  199. else
  200. min:=b;
  201. end;
  202. function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  203. {
  204. return the maximum of a and b
  205. }
  206. begin
  207. if a>=b then
  208. max:=a
  209. else
  210. max:=b;
  211. end;
  212. function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  213. {
  214. return the maximum of a and b
  215. }
  216. begin
  217. if a>=b then
  218. max:=a
  219. else
  220. max:=b;
  221. end;
  222. function max(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  223. {
  224. return the maximum of a and b
  225. }
  226. begin
  227. if a>=b then
  228. max:=a
  229. else
  230. max:=b;
  231. end;
  232. function max(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  233. {
  234. return the maximum of a and b
  235. }
  236. begin
  237. if a>=b then
  238. max:=a
  239. else
  240. max:=b;
  241. end;
  242. function newalignment(oldalignment: longint; offset: int64): longint;
  243. begin
  244. { oldalignment must be power of two.
  245. Negating two's complement number keeps its tail '100...000' and complements all bits above.
  246. "x and -x" extracts this tail of 'x'.
  247. Said tail of "oldalignment or offset" is the desired answer. }
  248. result:=oldalignment or longint(offset); { high part of offset won't matter as long as alignment is 32-bit }
  249. result:=result and -result;
  250. end;
  251. function reverse_byte(b: byte): byte;
  252. const
  253. reverse_nible:array[0..15] of 0..15 =
  254. (%0000,%1000,%0100,%1100,%0010,%1010,%0110,%1110,
  255. %0001,%1001,%0101,%1101,%0011,%1011,%0111,%1111);
  256. begin
  257. reverse_byte:=(reverse_nible[b and $f] shl 4) or reverse_nible[b shr 4];
  258. end;
  259. function reverse_word(w: word): word;
  260. type
  261. TWordRec = packed record
  262. hi, lo: Byte;
  263. end;
  264. begin
  265. TWordRec(reverse_word).hi := reverse_byte(TWordRec(w).lo);
  266. TWordRec(reverse_word).lo := reverse_byte(TWordRec(w).hi);
  267. end;
  268. function reverse_longword(l: longword): longword;
  269. type
  270. TLongWordRec = packed record
  271. b: array[0..3] of Byte;
  272. end;
  273. begin
  274. TLongWordRec(reverse_longword).b[0] := reverse_byte(TLongWordRec(l).b[3]);
  275. TLongWordRec(reverse_longword).b[1] := reverse_byte(TLongWordRec(l).b[2]);
  276. TLongWordRec(reverse_longword).b[2] := reverse_byte(TLongWordRec(l).b[1]);
  277. TLongWordRec(reverse_longword).b[3] := reverse_byte(TLongWordRec(l).b[0]);
  278. end;
  279. function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
  280. {
  281. return value <i> aligned <a> boundary. <a> must be power of two.
  282. }
  283. begin
  284. { One-line formula for i >= 0 is
  285. >>> (i + a - 1) and not (a - 1),
  286. and for i < 0 is
  287. >>> i and not (a - 1). }
  288. if a>0 then
  289. a:=a-1; { 'a' is decremented beforehand, this also allows a=0 as a synonym for a=1. }
  290. if i>=0 then
  291. i:=i+a;
  292. result:=i and not a;
  293. end;
  294. function align(i,a:int64):int64;{$ifdef USEINLINE}inline;{$endif}
  295. {
  296. return value <i> aligned <a> boundary. <a> must be power of two.
  297. }
  298. begin
  299. { Copy of 'longint' version. }
  300. if a>0 then
  301. a:=a-1;
  302. if i>=0 then
  303. i:=i+a;
  304. result:=i and not a;
  305. end;
  306. function align(i,a:qword):qword;{$ifdef USEINLINE}inline;{$endif}
  307. {
  308. return value <i> aligned <a> boundary. <a> must be power of two.
  309. }
  310. begin
  311. { No i < 0 case here. }
  312. if a>0 then
  313. a:=a-1;
  314. result:=(i+a) and not a;
  315. end;
  316. function packedbitsloadsize(bitlen: int64) : int64;
  317. begin
  318. case bitlen of
  319. 1,2,4,8:
  320. result := 1;
  321. { 10 bits can never be split over 3 bytes via 1-8-1, because it }
  322. { always starts at a multiple of 10 bits. Same for the others. }
  323. 3,5,6,7,9,10,12,16:
  324. result := 2;
  325. {$ifdef cpu64bitalu}
  326. { performance penalty for unaligned 8 byte access is much }
  327. { higher than for unaligned 4 byte access, at least on ppc, }
  328. { so use 4 bytes even in some cases where a value could }
  329. { always loaded using a single 8 byte load (e.g. in case of }
  330. { 28 bit values) }
  331. 11,13,14,15,17..32:
  332. result := 4;
  333. else
  334. result := 8;
  335. {$else cpu64bitalu}
  336. else
  337. result := 4;
  338. {$endif cpu64bitalu}
  339. end;
  340. end;
  341. function isbetteralignedthan(new, org, limit: cardinal): boolean;
  342. var
  343. cnt: cardinal;
  344. begin
  345. cnt:=2;
  346. while (cnt <= limit) do
  347. begin
  348. if (org and (cnt-1)) > (new and (cnt-1)) then
  349. begin
  350. result:=true;
  351. exit;
  352. end
  353. else if (org and (cnt-1)) < (new and (cnt-1)) then
  354. begin
  355. result:=false;
  356. exit;
  357. end;
  358. cnt:=cnt*2;
  359. end;
  360. result:=false;
  361. end;
  362. function next_prime(l: longint): longint;
  363. var
  364. check, checkbound: longint;
  365. ok: boolean;
  366. begin
  367. result:=l or 1;
  368. while l<high(longint) do
  369. begin
  370. ok:=true;
  371. checkbound:=trunc(sqrt(l));
  372. check:=3;
  373. while check<checkbound do
  374. begin
  375. if (l mod check) = 0 then
  376. begin
  377. ok:=false;
  378. break;
  379. end;
  380. inc(check,2);
  381. end;
  382. if ok then
  383. exit;
  384. inc(l);
  385. end;
  386. end;
  387. function used_align(varalign,minalign,maxalign:longint):longint;
  388. begin
  389. { varalign : minimum alignment required for the variable
  390. minalign : Minimum alignment of this structure, 0 = undefined
  391. maxalign : Maximum alignment of this structure, 0 = undefined }
  392. if (minalign>0) and
  393. (varalign<=minalign) then
  394. used_align:=minalign
  395. else
  396. begin
  397. if (maxalign>0) and
  398. (varalign>maxalign) then
  399. used_align:=maxalign
  400. else
  401. used_align:=varalign;
  402. end;
  403. end;
  404. procedure Replace(var s:string;s1:string;const s2:string);
  405. var
  406. last,
  407. i : longint;
  408. begin
  409. s1:=upper(s1);
  410. last:=0;
  411. repeat
  412. i:=pos(s1,upper(s));
  413. if i=last then
  414. i:=0;
  415. if (i>0) then
  416. begin
  417. Delete(s,i,length(s1));
  418. Insert(s2,s,i);
  419. last:=i;
  420. end;
  421. until (i=0);
  422. end;
  423. procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
  424. var
  425. last,
  426. i : longint;
  427. begin
  428. s1:=upper(s1);
  429. last:=0;
  430. repeat
  431. i:=pos(s1,upper(s));
  432. if i=last then
  433. i:=0;
  434. if (i>0) then
  435. begin
  436. Delete(s,i,length(s1));
  437. Insert(s2,s,i);
  438. last:=i;
  439. end;
  440. until (i=0);
  441. end;
  442. procedure ReplaceCase(var s:string;const s1,s2:string);
  443. var
  444. last,
  445. i : longint;
  446. begin
  447. last:=0;
  448. repeat
  449. i:=pos(s1,s);
  450. if i=last then
  451. i:=0;
  452. if (i>0) then
  453. begin
  454. Delete(s,i,length(s1));
  455. Insert(s2,s,i);
  456. last:=i;
  457. end;
  458. until (i=0);
  459. end;
  460. procedure ReplaceCase(var s: ansistring; const s1, s2: ansistring);
  461. var
  462. last,
  463. i : longint;
  464. begin
  465. last:=0;
  466. repeat
  467. i:=pos(s1,s);
  468. if i=last then
  469. i:=0;
  470. if (i>0) then
  471. begin
  472. Delete(s,i,length(s1));
  473. Insert(s2,s,i);
  474. last:=i;
  475. end;
  476. until (i=0);
  477. end;
  478. Function MatchPattern(const pattern,what:string):boolean;
  479. var
  480. found : boolean;
  481. i1,i2 : longint;
  482. begin
  483. i1:=0;
  484. i2:=0;
  485. if pattern='' then
  486. begin
  487. result:=(what='');
  488. exit;
  489. end;
  490. found:=true;
  491. repeat
  492. inc(i1);
  493. if (i1>length(pattern)) then
  494. break;
  495. inc(i2);
  496. if (i2>length(what)) then
  497. break;
  498. case pattern[i1] of
  499. '?' :
  500. found:=true;
  501. '*' :
  502. begin
  503. found:=true;
  504. if (i1=length(pattern)) then
  505. i2:=length(what)
  506. else
  507. if (i1<length(pattern)) and (pattern[i1+1]<>what[i2]) then
  508. begin
  509. if i2<length(what) then
  510. dec(i1)
  511. end
  512. else
  513. if i2>1 then
  514. dec(i2);
  515. end;
  516. else
  517. found:=(pattern[i1]=what[i2]) or (what[i2]='?');
  518. end;
  519. until not found;
  520. if found then
  521. begin
  522. found:=(i2>=length(what)) and
  523. (
  524. (i1>length(pattern)) or
  525. ((i1=length(pattern)) and
  526. (pattern[i1]='*'))
  527. );
  528. end;
  529. result:=found;
  530. end;
  531. function upper(const c : char) : char;
  532. {
  533. return uppercase of c
  534. }
  535. begin
  536. upper:=uppertbl[c];
  537. end;
  538. function upper(const s : string) : string;
  539. {
  540. return uppercased string of s
  541. }
  542. var
  543. i : longint;
  544. begin
  545. for i:=1 to length(s) do
  546. upper[i]:=uppertbl[s[i]];
  547. upper[0]:=s[0];
  548. end;
  549. function upper(const s : ansistring) : ansistring;
  550. {
  551. return uppercased string of s
  552. }
  553. var
  554. i : longint;
  555. begin
  556. Result:='';
  557. setlength(upper,length(s));
  558. for i:=1 to length(s) do
  559. upper[i]:=uppertbl[s[i]];
  560. end;
  561. function lower(const c : char) : char;
  562. {
  563. return lowercase of c
  564. }
  565. begin
  566. lower:=lowertbl[c];
  567. end;
  568. function lower(const s : string) : string;
  569. {
  570. return lowercased string of s
  571. }
  572. var
  573. i : longint;
  574. begin
  575. for i:=1 to length(s) do
  576. lower[i]:=lowertbl[s[i]];
  577. lower[0]:=s[0];
  578. end;
  579. function lower(const s : ansistring) : ansistring;
  580. {
  581. return lowercased string of s
  582. }
  583. var
  584. i : longint;
  585. begin
  586. Result:='';
  587. setlength(lower,length(s));
  588. for i:=1 to length(s) do
  589. lower[i]:=lowertbl[s[i]];
  590. end;
  591. procedure uppervar(var s : string);
  592. {
  593. uppercase string s
  594. }
  595. var
  596. i : longint;
  597. begin
  598. for i:=1 to length(s) do
  599. s[i]:=uppertbl[s[i]];
  600. end;
  601. procedure initupperlower;
  602. var
  603. c : char;
  604. begin
  605. for c:=#0 to #255 do
  606. begin
  607. lowertbl[c]:=c;
  608. uppertbl[c]:=c;
  609. case c of
  610. 'A'..'Z' :
  611. lowertbl[c]:=char(byte(c)+32);
  612. 'a'..'z' :
  613. uppertbl[c]:=char(byte(c)-32);
  614. end;
  615. end;
  616. end;
  617. function DStr(l:longint):string;
  618. var
  619. TmpStr : string[32];
  620. i : longint;
  621. begin
  622. Str(l,TmpStr);
  623. i:=Length(TmpStr);
  624. while (i>3) do
  625. begin
  626. dec(i,3);
  627. if TmpStr[i]<>'-' then
  628. insert('.',TmpStr,i+1);
  629. end;
  630. DStr:=TmpStr;
  631. end;
  632. function rpos(const needle: char; const haystack: shortstring): longint;
  633. begin
  634. result:=length(haystack);
  635. while (result>0) do
  636. begin
  637. if haystack[result]=needle then
  638. exit;
  639. dec(result);
  640. end;
  641. end;
  642. function rpos(const needle: shortstring; const haystack: shortstring): longint;
  643. begin
  644. result:=0;
  645. if (length(needle)=0) or
  646. (length(needle)>length(haystack)) then
  647. exit;
  648. result:=length(haystack)-length(needle);
  649. repeat
  650. if (haystack[result]=needle[1]) and
  651. (copy(haystack,result,length(needle))=needle) then
  652. exit;
  653. dec(result);
  654. until result=0;
  655. end;
  656. function trimspace(const s:string):string;
  657. {
  658. return s with all leading and ending spaces and tabs removed
  659. }
  660. var
  661. i,j : longint;
  662. begin
  663. i:=length(s);
  664. while (i>0) and (s[i] in [#9,' ']) do
  665. dec(i);
  666. j:=1;
  667. while (j<i) and (s[j] in [#9,' ']) do
  668. inc(j);
  669. trimspace:=Copy(s,j,i-j+1);
  670. end;
  671. function trimspace(const s:AnsiString):AnsiString;
  672. {
  673. return s with all leading and ending spaces and tabs removed
  674. }
  675. var
  676. i,j : longint;
  677. begin
  678. i:=length(s);
  679. while (i>0) and (s[i] in [#9,' ']) do
  680. dec(i);
  681. j:=1;
  682. while (j<i) and (s[j] in [#9,' ']) do
  683. inc(j);
  684. trimspace:=Copy(s,j,i-j+1);
  685. end;
  686. function space (b : longint): string;
  687. var
  688. s: string;
  689. begin
  690. space[0] := chr(b);
  691. s[0] := chr(b);
  692. FillChar (S[1],b,' ');
  693. space:=s;
  694. end;
  695. function PadSpace(const s:string;len:longint):string;
  696. {
  697. return s with spaces add to the end
  698. }
  699. begin
  700. if length(s)<len then
  701. PadSpace:=s+Space(len-length(s))
  702. else
  703. PadSpace:=s;
  704. end;
  705. function PadSpace(const s:AnsiString;len:longint):AnsiString;
  706. {
  707. return s with spaces add to the end
  708. }
  709. begin
  710. if length(s)<len then
  711. PadSpace:=s+Space(len-length(s))
  712. else
  713. PadSpace:=s;
  714. end;
  715. function GetToken(var s:string;endchar:char):string;
  716. var
  717. i : longint;
  718. quote : char;
  719. begin
  720. GetToken:='';
  721. s:=TrimSpace(s);
  722. if (length(s)>0) and
  723. (s[1] in ['''','"']) then
  724. begin
  725. quote:=s[1];
  726. i:=1;
  727. while (i<length(s)) do
  728. begin
  729. inc(i);
  730. if s[i]=quote then
  731. begin
  732. { Remove double quote }
  733. if (i<length(s)) and
  734. (s[i+1]=quote) then
  735. begin
  736. Delete(s,i,1);
  737. inc(i);
  738. end
  739. else
  740. begin
  741. GetToken:=Copy(s,2,i-2);
  742. Delete(s,1,i);
  743. exit;
  744. end;
  745. end;
  746. end;
  747. GetToken:=s;
  748. s:='';
  749. end
  750. else
  751. begin
  752. i:=pos(EndChar,s);
  753. if i=0 then
  754. begin
  755. GetToken:=s;
  756. s:='';
  757. exit;
  758. end
  759. else
  760. begin
  761. GetToken:=Copy(s,1,i-1);
  762. Delete(s,1,i);
  763. exit;
  764. end;
  765. end;
  766. end;
  767. function GetToken(var s:ansistring;endchar:char):ansistring;
  768. var
  769. i : longint;
  770. quote : char;
  771. begin
  772. GetToken:='';
  773. s:=TrimSpace(s);
  774. if (length(s)>0) and
  775. (s[1] in ['''','"']) then
  776. begin
  777. quote:=s[1];
  778. i:=1;
  779. while (i<length(s)) do
  780. begin
  781. inc(i);
  782. if s[i]=quote then
  783. begin
  784. { Remove double quote }
  785. if (i<length(s)) and
  786. (s[i+1]=quote) then
  787. begin
  788. Delete(s,i,1);
  789. inc(i);
  790. end
  791. else
  792. begin
  793. GetToken:=Copy(s,2,i-2);
  794. Delete(s,1,i);
  795. exit;
  796. end;
  797. end;
  798. end;
  799. GetToken:=s;
  800. s:='';
  801. end
  802. else
  803. begin
  804. i:=pos(EndChar,s);
  805. if i=0 then
  806. begin
  807. GetToken:=s;
  808. s:='';
  809. exit;
  810. end
  811. else
  812. begin
  813. GetToken:=Copy(s,1,i-1);
  814. Delete(s,1,i);
  815. exit;
  816. end;
  817. end;
  818. end;
  819. function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
  820. begin
  821. str(e,result);
  822. end;
  823. function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  824. {
  825. return string of value i
  826. }
  827. begin
  828. str(i,result);
  829. end;
  830. function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  831. {
  832. return string of value i
  833. }
  834. begin
  835. str(i,result);
  836. end;
  837. function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  838. {
  839. return string of value i
  840. }
  841. begin
  842. str(i,result);
  843. end;
  844. function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
  845. {
  846. return string of value i, but always include a + when i>=0
  847. }
  848. begin
  849. str(i,result);
  850. if i>=0 then
  851. result:='+'+result;
  852. end;
  853. function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
  854. {
  855. is string a correct number ?
  856. }
  857. var
  858. w : integer;
  859. l : longint;
  860. begin
  861. val(s,l,w);
  862. // remove warning
  863. l:=l;
  864. is_number:=(w=0);
  865. end;
  866. function ispowerof2(value : int64;out power : longint) : boolean;
  867. {
  868. return if value is a power of 2. And if correct return the power
  869. }
  870. begin
  871. if (value <= 0) or (value and (value - 1) <> 0) then
  872. exit(false);
  873. power:=BsfQWord(value);
  874. result:=true;
  875. end;
  876. function ispowerof2(const value: Tconstexprint; out power: longint): boolean;
  877. begin
  878. if value.signed or
  879. (value.uvalue<=high(int64)) then
  880. result:=ispowerof2(value.svalue,power)
  881. else if not value.signed and
  882. (value.svalue=low(int64)) then
  883. begin
  884. result:=true;
  885. power:=63;
  886. end
  887. else
  888. result:=false;
  889. end;
  890. function isabspowerof2(const value : Tconstexprint;out power : longint) : boolean;
  891. begin
  892. if ispowerof2(value,power) then
  893. result:=true
  894. else if value.signed and (value.svalue<0) and (value.svalue<>low(int64)) and ispowerof2(-value.svalue,power) then
  895. result:=true
  896. else
  897. result:=false;
  898. end;
  899. function nextpowerof2(value : qword; out power: longint) : qword;
  900. begin
  901. power:=-1;
  902. result:=0;
  903. if (value=0) or (value>qword($8000000000000000)) then
  904. exit;
  905. power:=BsrQWord(value);
  906. result:=qword(1) shl power;
  907. if (value and (value-1))<>0 then
  908. begin
  909. inc(power);
  910. result:=result shl 1;
  911. end;
  912. end;
  913. function backspace_quote(const s:string;const qchars:Tcharset):string;
  914. var i:byte;
  915. begin
  916. backspace_quote:='';
  917. for i:=1 to length(s) do
  918. begin
  919. if (s[i]=#10) and (#10 in qchars) then
  920. backspace_quote:=backspace_quote+'\n'
  921. else if (s[i]=#13) and (#13 in qchars) then
  922. backspace_quote:=backspace_quote+'\r'
  923. else
  924. begin
  925. if s[i] in qchars then
  926. backspace_quote:=backspace_quote+'\';
  927. backspace_quote:=backspace_quote+s[i];
  928. end;
  929. end;
  930. end;
  931. function octal_quote(const s:string;const qchars:Tcharset):string;
  932. var i:byte;
  933. begin
  934. octal_quote:='';
  935. for i:=1 to length(s) do
  936. begin
  937. if s[i] in qchars then
  938. begin
  939. if ord(s[i])<64 then
  940. octal_quote:=octal_quote+'\'+octstr(ord(s[i]),3)
  941. else
  942. octal_quote:=octal_quote+'\'+octstr(ord(s[i]),4);
  943. end
  944. else
  945. octal_quote:=octal_quote+s[i];
  946. end;
  947. end;
  948. function DePascalQuote(var s: ansistring): Boolean;
  949. var
  950. destPos, sourcePos, len: Integer;
  951. t: string;
  952. ch: Char;
  953. begin
  954. t:='';
  955. DePascalQuote:= false;
  956. len:= length(s);
  957. if (len >= 1) and (s[1] = '''') then
  958. begin
  959. {Remove quotes, exchange '' against ' }
  960. destPos := 0;
  961. sourcepos:=1;
  962. while (sourcepos<len) do
  963. begin
  964. inc(sourcePos);
  965. ch := s[sourcePos];
  966. if ch = '''' then
  967. begin
  968. inc(sourcePos);
  969. if (sourcePos <= len) and (s[sourcePos] = '''') then
  970. {Add the quote as part of string}
  971. else
  972. begin
  973. SetLength(t, destPos);
  974. s:= t;
  975. Exit(true);
  976. end;
  977. end;
  978. inc(destPos);
  979. t[destPos] := ch;
  980. end;
  981. end;
  982. end;
  983. function pchar2pshortstring(p : pchar) : pshortstring;
  984. var
  985. w,i : longint;
  986. begin
  987. w:=strlen(p);
  988. for i:=w-1 downto 0 do
  989. p[i+1]:=p[i];
  990. p[0]:=chr(w);
  991. pchar2pshortstring:=pshortstring(p);
  992. end;
  993. function pshortstring2pchar(p : pshortstring) : pchar;
  994. var
  995. w,i : longint;
  996. begin
  997. w:=length(p^);
  998. for i:=1 to w do
  999. p^[i-1]:=p^[i];
  1000. p^[w]:=#0;
  1001. pshortstring2pchar:=pchar(p);
  1002. end;
  1003. function ansistring2pchar(const a: ansistring) : pchar;
  1004. var
  1005. len: ptrint;
  1006. begin
  1007. len:=length(a);
  1008. getmem(result,len+1);
  1009. if (len<>0) then
  1010. move(a[1],result[0],len);
  1011. result[len]:=#0;
  1012. end;
  1013. function lowercase(c : char) : char;
  1014. begin
  1015. case c of
  1016. #65..#90 : c := chr(ord (c) + 32);
  1017. #154 : c:=#129; { german }
  1018. #142 : c:=#132; { german }
  1019. #153 : c:=#148; { german }
  1020. #144 : c:=#130; { french }
  1021. #128 : c:=#135; { french }
  1022. #143 : c:=#134; { swedish/norge (?) }
  1023. #165 : c:=#164; { spanish }
  1024. #228 : c:=#229; { greek }
  1025. #226 : c:=#231; { greek }
  1026. #232 : c:=#227; { greek }
  1027. end;
  1028. lowercase := c;
  1029. end;
  1030. function strpnew(const s : string) : pchar;
  1031. var
  1032. p : pchar;
  1033. begin
  1034. getmem(p,length(s)+1);
  1035. move(s[1],p^,length(s));
  1036. p[length(s)]:=#0;
  1037. result:=p;
  1038. end;
  1039. function strpnew(const s: ansistring): pchar;
  1040. var
  1041. p : pchar;
  1042. begin
  1043. getmem(p,length(s)+1);
  1044. move(s[1],p^,length(s)+1);
  1045. result:=p;
  1046. end;
  1047. procedure stringdispose(var p : pshortstring);{$ifdef USEINLINE}inline;{$endif}
  1048. begin
  1049. if assigned(p) then
  1050. begin
  1051. freemem(p);
  1052. p:=nil;
  1053. end;
  1054. end;
  1055. function stringdup(const s : shortstring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  1056. begin
  1057. getmem(result,length(s)+1);
  1058. result^:=s;
  1059. end;
  1060. function stringdup(const s : ansistring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  1061. begin
  1062. getmem(result,length(s)+1);
  1063. result^:=s;
  1064. end;
  1065. function PosCharset(const cs : TCharSet;const s : ansistring) : integer;
  1066. var
  1067. i : integer;
  1068. begin
  1069. result:=0;
  1070. for i:=1 to length(s) do
  1071. if s[i] in cs then
  1072. begin
  1073. result:=i;
  1074. exit;
  1075. end;
  1076. end;
  1077. function CompareStr(const S1, S2: string): Integer;
  1078. var
  1079. count, count1, count2: integer;
  1080. begin
  1081. result := 0;
  1082. Count1 := Length(S1);
  1083. Count2 := Length(S2);
  1084. if Count1>Count2 then
  1085. Count:=Count2
  1086. else
  1087. Count:=Count1;
  1088. result := CompareChar(S1[1],S2[1], Count);
  1089. if result=0 then
  1090. result:=Count1-Count2;
  1091. end;
  1092. function CompareText(S1, S2: string): integer;
  1093. begin
  1094. UpperVar(S1);
  1095. UpperVar(S2);
  1096. Result:=CompareStr(S1,S2);
  1097. end;
  1098. {*****************************************************************************
  1099. Ansistring (PChar+Length)
  1100. *****************************************************************************}
  1101. procedure ansistringdispose(var p : pchar;length : longint);
  1102. begin
  1103. if assigned(p) then
  1104. begin
  1105. freemem(p);
  1106. p:=nil;
  1107. end;
  1108. end;
  1109. { enable ansistring comparison }
  1110. { 0 means equal }
  1111. { 1 means p1 > p2 }
  1112. { -1 means p1 < p2 }
  1113. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  1114. var
  1115. i,j : longint;
  1116. begin
  1117. compareansistrings:=0;
  1118. j:=min(length1,length2);
  1119. i:=0;
  1120. while (i<j) do
  1121. begin
  1122. if p1[i]>p2[i] then
  1123. begin
  1124. compareansistrings:=1;
  1125. exit;
  1126. end
  1127. else
  1128. if p1[i]<p2[i] then
  1129. begin
  1130. compareansistrings:=-1;
  1131. exit;
  1132. end;
  1133. inc(i);
  1134. end;
  1135. if length1>length2 then
  1136. compareansistrings:=1
  1137. else
  1138. if length1<length2 then
  1139. compareansistrings:=-1;
  1140. end;
  1141. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  1142. var
  1143. p : pchar;
  1144. begin
  1145. getmem(p,length1+length2+1);
  1146. move(p1[0],p[0],length1);
  1147. move(p2[0],p[length1],length2+1);
  1148. concatansistrings:=p;
  1149. end;
  1150. {*****************************************************************************
  1151. Ultra basic KISS Lzw (de)compressor
  1152. *****************************************************************************}
  1153. {This is an extremely basic implementation of the Lzw algorithm. It
  1154. compresses 7-bit ASCII strings into 8-bit compressed strings.
  1155. The Lzw dictionary is preinitialized with 0..127, therefore this
  1156. part of the dictionary does not need to be stored in the arrays.
  1157. The Lzw code size is allways 8 bit, so we do not need complex code
  1158. that can write partial bytes.}
  1159. function minilzw_encode(const s:string):string;
  1160. var t,u,i:byte;
  1161. c:char;
  1162. data:array[128..255] of char;
  1163. previous:array[128..255] of byte;
  1164. lzwptr:byte;
  1165. next_avail:set of 0..255;
  1166. label l1;
  1167. begin
  1168. minilzw_encode:='';
  1169. fillchar(data,sizeof(data),#0);
  1170. fillchar(previous,sizeof(previous),#0);
  1171. if s<>'' then
  1172. begin
  1173. lzwptr:=127;
  1174. t:=byte(s[1]);
  1175. i:=2;
  1176. u:=128;
  1177. next_avail:=[];
  1178. while i<=length(s) do
  1179. begin
  1180. c:=s[i];
  1181. if not(t in next_avail) or (u>lzwptr) then goto l1;
  1182. while (previous[u]<>t) or (data[u]<>c) do
  1183. begin
  1184. inc(u);
  1185. if u>lzwptr then goto l1;
  1186. end;
  1187. t:=u;
  1188. inc(i);
  1189. continue;
  1190. l1:
  1191. {It's a pity that we still need those awfull tricks
  1192. with this modern compiler. Without this performance
  1193. of the entire procedure drops about 3 times.}
  1194. inc(minilzw_encode[0]);
  1195. minilzw_encode[length(minilzw_encode)]:=char(t);
  1196. if lzwptr=255 then
  1197. begin
  1198. lzwptr:=127;
  1199. next_avail:=[];
  1200. end
  1201. else
  1202. begin
  1203. inc(lzwptr);
  1204. data[lzwptr]:=c;
  1205. previous[lzwptr]:=t;
  1206. include(next_avail,t);
  1207. end;
  1208. t:=byte(c);
  1209. u:=128;
  1210. inc(i);
  1211. end;
  1212. inc(minilzw_encode[0]);
  1213. minilzw_encode[length(minilzw_encode)]:=char(t);
  1214. end;
  1215. end;
  1216. function minilzw_decode(const s:string):string;
  1217. var oldc,newc,c:char;
  1218. i,j:byte;
  1219. data:array[128..255] of char;
  1220. previous:array[128..255] of byte;
  1221. lzwptr:byte;
  1222. t:string;
  1223. begin
  1224. minilzw_decode:='';
  1225. fillchar(data,sizeof(data),#0);
  1226. fillchar(previous,sizeof(previous),#0);
  1227. if s<>'' then
  1228. begin
  1229. lzwptr:=127;
  1230. oldc:=s[1];
  1231. c:=oldc;
  1232. i:=2;
  1233. minilzw_decode:=oldc;
  1234. while i<=length(s) do
  1235. begin
  1236. newc:=s[i];
  1237. if byte(newc)>lzwptr then
  1238. begin
  1239. t:=c;
  1240. c:=oldc;
  1241. end
  1242. else
  1243. begin
  1244. c:=newc;
  1245. t:='';
  1246. end;
  1247. while c>=#128 do
  1248. begin
  1249. inc(t[0]);
  1250. t[length(t)]:=data[byte(c)];
  1251. byte(c):=previous[byte(c)];
  1252. end;
  1253. inc(minilzw_decode[0]);
  1254. minilzw_decode[length(minilzw_decode)]:=c;
  1255. for j:=length(t) downto 1 do
  1256. begin
  1257. inc(minilzw_decode[0]);
  1258. minilzw_decode[length(minilzw_decode)]:=t[j];
  1259. end;
  1260. if lzwptr=255 then
  1261. lzwptr:=127
  1262. else
  1263. begin
  1264. inc(lzwptr);
  1265. previous[lzwptr]:=byte(oldc);
  1266. data[lzwptr]:=c;
  1267. end;
  1268. oldc:=newc;
  1269. inc(i);
  1270. end;
  1271. end;
  1272. end;
  1273. procedure defaulterror(i:longint);
  1274. begin
  1275. writeln('Internal error ',i);
  1276. runerror(255);
  1277. end;
  1278. Function Nextafter(x,y:double):double;
  1279. // Returns the double precision number closest to x in
  1280. // the direction toward y.
  1281. // Initial direct translation by Soeren Haastrup from
  1282. // www.netlib.org/fdlibm/s_nextafter.c according to
  1283. // ====================================================
  1284. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1285. // Developed at SunSoft, a Sun Microsystems, Inc. business.
  1286. // Permission to use, copy, modify, and distribute this
  1287. // software is freely granted, provided that this notice
  1288. // is preserved.
  1289. // ====================================================
  1290. // and with all signaling policies preserved as is.
  1291. type
  1292. {$if defined(ENDIAN_LITTLE) and not defined(FPC_DOUBLE_HILO_SWAPPED)}
  1293. twoword=record
  1294. lo,hi:longword; // Little Endian split of a double.
  1295. end;
  1296. {$else}
  1297. twoword=record
  1298. hi,lo:longword; // Big Endian split of a double.
  1299. end;
  1300. {$endif}
  1301. var
  1302. hx,hy,ix,iy:longint;
  1303. lx,ly:longword;
  1304. Begin
  1305. hx:=twoword(x).hi; // high and low words of x and y
  1306. lx:=twoword(x).lo;
  1307. hy:=twoword(y).hi;
  1308. ly:=twoword(y).lo;
  1309. ix:=hx and $7fffffff; // absolute values
  1310. iy:=hy and $7fffffff;
  1311. // Case x=NAN or y=NAN
  1312. if ( (ix>=$7ff00000) and ((longword(ix-$7ff00000) or lx) <> 0) )
  1313. or ( (iy>=$7ff00000) and ((longword(iy-$7ff00000) OR ly) <> 0) )
  1314. then exit(x+y);
  1315. // Case x=y
  1316. if x=y then exit(x); // (implies Nextafter(0,-0) is 0 and not -0...)
  1317. // Case x=0
  1318. if (longword(ix) or lx)=0
  1319. then begin
  1320. twoword(x).hi:=hy and $80000000; // return +-minimalSubnormal
  1321. twoword(x).lo:=1;
  1322. y:=x*x; // set underflow flag (ignored in FPC as default)
  1323. if y=x
  1324. then exit(y)
  1325. else exit(x);
  1326. end;
  1327. // all other cases
  1328. if hx>=0 // x>0
  1329. then begin
  1330. if (hx>hy) or ( (hx=hy) and (lx>ly) ) // x>y , return x-ulp
  1331. then begin
  1332. if (lx=0) then hx:=hx-1;
  1333. lx:=lx-1;
  1334. end
  1335. else begin // x<y, return x+ulp
  1336. lx:=lx+1;
  1337. if lx=0 then hx:=hx+1;
  1338. end
  1339. end
  1340. else begin // x<0
  1341. if (hy>=0) or (hx>=hy) or ( (hx=hy) and (lx>ly)) // x<y, return x-ulp
  1342. then begin
  1343. if (lx=0) then hx:=hx-1;
  1344. lx:=lx-1;
  1345. end
  1346. else begin // x>y , return x+ulp
  1347. lx:=lx+1;
  1348. if lx=0 then hx:=hx+1;
  1349. end
  1350. end;
  1351. // finally check if overflow or underflow just happend
  1352. hy:=hx and $7ff00000;
  1353. if (hy>= $7ff00000) then exit(x+x); // overflow and signal
  1354. if (hy<$0010000) // underflow
  1355. then begin
  1356. y:=x*x; // raise underflow flag
  1357. if y<>x
  1358. then begin
  1359. twoword(y).hi:=hx;
  1360. twoword(y).lo:=lx;
  1361. exit(y);
  1362. end
  1363. end;
  1364. twoword(x).hi:=hx;
  1365. twoword(x).lo:=lx;
  1366. nextafter:=x;
  1367. end;
  1368. function LengthUleb128(a: qword) : byte;
  1369. begin
  1370. result:=0;
  1371. repeat
  1372. inc(result);
  1373. a := a shr 7;
  1374. until a=0;
  1375. end;
  1376. function LengthSleb128(a: int64) : byte;
  1377. begin
  1378. { 'a xor SarInt64(a,63)' has upper bits 0...01 where '0's symbolize sign bits of 'a' and 1 symbolizes its most significant non-sign bit.
  1379. 'shl 1' ensures storing the sign bit. }
  1380. result:=LengthUleb128(qword(a xor SarInt64(a,63)) shl 1);
  1381. end;
  1382. function EncodeUleb128(a: qword;out buf;len : byte) : byte;
  1383. var
  1384. b: byte;
  1385. pbuf : pbyte;
  1386. begin
  1387. result:=0;
  1388. pbuf:=@buf;
  1389. repeat
  1390. b := a and $7f;
  1391. a := a shr 7;
  1392. if a<>0 then
  1393. b := b or $80;
  1394. pbuf^:=b;
  1395. inc(pbuf);
  1396. inc(result);
  1397. until (a=0) and (result>=len);
  1398. end;
  1399. function EncodeSleb128(a: int64;out buf;len : byte) : byte;
  1400. var
  1401. b: byte;
  1402. more: boolean;
  1403. pbuf : pbyte;
  1404. begin
  1405. result:=0;
  1406. pbuf:=@buf;
  1407. repeat
  1408. b := a and $7f;
  1409. a := SarInt64(a, 7);
  1410. inc(result);
  1411. more:=(result<len) or (a<>-(b shr 6));
  1412. pbuf^:=b or byte(more) shl 7;
  1413. inc(pbuf);
  1414. until not more;
  1415. end;
  1416. initialization
  1417. internalerrorproc:=@defaulterror;
  1418. initupperlower;
  1419. end.