cutils.pas 40 KB

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