/usr/share/ada/adainclude/xmlada/schema-decimal.adb is in libxmlada4.1-dev 4.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | -----------------------------------------------------------------------
-- XML/Ada - An XML suite for Ada95 --
-- --
-- Copyright (C) 2005-2010, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public --
-- License as published by the Free Software Foundation; either --
-- version 2 of the License, or (at your option) any later version. --
-- --
-- This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. --
-- --
-- You should have received a copy of the GNU General Public --
-- License along with this library; if not, write to the --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330, --
-- Boston, MA 02111-1307, USA. --
-- --
-----------------------------------------------------------------------
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Sax.Encodings; use Sax.Encodings;
with Sax.Symbols; use Sax.Symbols;
with Sax.Utils; use Sax.Utils;
with Unicode.CES; use Unicode, Unicode.CES;
with Unicode.Names.Basic_Latin; use Unicode.Names.Basic_Latin;
package body Schema.Decimal is
type Compare_Result is (Less_Than, Equal, Greater_Than);
function Compare (Num1, Num2 : String) return Compare_Result;
-- Compare two numbers
function Get_Exp (Num : String) return Long_Long_Integer;
-- Return the exponential part of Num (ie the part after 'E'.
procedure Get_Fore (Num : String; First, Last : out Integer);
-- Return the position of the first and last digit in the integer part of
-- Num
procedure To_Next_Digit (Num : String; Pos : in out Integer);
-- Move Pos to the next digit in Num
procedure Internal_Value
(Ch : Unicode.CES.Byte_Sequence;
Symbols : Sax.Utils.Symbol_Table;
Allow_Exponent : Boolean;
Val : out Arbitrary_Precision_Number;
Error : out Symbol);
-- Internal implementation of Value
-----------
-- Image --
-----------
function Image
(Number : Arbitrary_Precision_Number) return Unicode.CES.Byte_Sequence is
begin
if Number.Value /= No_Symbol then
return Get (Number.Value).all;
else
return "0";
end if;
end Image;
-----------
-- Value --
-----------
function Value
(Val : Sax.Symbols.Symbol) return Arbitrary_Precision_Number
is
begin
return (Value => Val);
end Value;
-----------
-- Value --
-----------
procedure Value
(Symbols : Sax.Utils.Symbol_Table;
Ch : Unicode.CES.Byte_Sequence;
Val : out Arbitrary_Precision_Number;
Error : out Sax.Symbols.Symbol) is
begin
Internal_Value (Ch, Symbols, True, Val, Error);
end Value;
--------------------
-- Internal_Value --
--------------------
procedure Internal_Value
(Ch : Unicode.CES.Byte_Sequence;
Symbols : Sax.Utils.Symbol_Table;
Allow_Exponent : Boolean;
Val : out Arbitrary_Precision_Number;
Error : out Symbol)
is
Pos : Integer := Ch'First;
First, Last : Integer;
C : Unicode_Char;
Saw_Exponent : Boolean := False;
Saw_Point : Boolean := False;
begin
if Ch'Length = 0 then
Error := Find (Symbols, "Invalid: empty string used as a number");
Val := Undefined_Number;
return;
end if;
-- Skip leading spaces (because the "whitespace" facet is always
-- "collapse"
while Pos <= Ch'Last loop
First := Pos;
Encoding.Read (Ch, Pos, C);
exit when not Is_White_Space (C);
end loop;
-- Skip sign, if any
if C = Plus_Sign or C = Hyphen_Minus then
Encoding.Read (Ch, Pos, C);
end if;
Last := Pos - 1;
-- Check we only have digits from now on
loop
if C = Period then
if Saw_Point then
Error := Find
(Symbols, "Only one decimal separator allowed in " & Ch);
Val := Undefined_Number;
return;
end if;
Saw_Point := True;
elsif C = Latin_Capital_Letter_E
or else C = Latin_Small_Letter_E
then
if Saw_Exponent then
Error := Find (Symbols, "Only one exponent allowed in " & Ch);
Val := Undefined_Number;
return;
end if;
if not Allow_Exponent then
Error := Find
(Symbols, "Exponent parent not authorized in " & Ch);
Val := Undefined_Number;
return;
end if;
Saw_Exponent := True;
Saw_Point := False;
if Pos > Ch'Last then
Error := Find (Symbols, "No exponent specified in " & Ch);
Val := Undefined_Number;
return;
else
declare
Save : constant Integer := Pos;
begin
Encoding.Read (Ch, Pos, C);
if C /= Plus_Sign and C /= Hyphen_Minus then
Pos := Save;
end if;
end;
end if;
elsif not Is_Digit (C) then
-- Skip trailing spaces
if Is_White_Space (C) then
while Pos <= Ch'Last loop
Encoding.Read (Ch, Pos, C);
if not Is_White_Space (C) then
Error :=
Find (Symbols, "Invalid integer: """ & Ch & """");
Val := Undefined_Number;
return;
end if;
end loop;
exit;
else
Error := Find (Symbols, "Invalid integer: """ & Ch & """");
Val := Undefined_Number;
return;
end if;
end if;
Last := Pos - 1;
exit when Pos > Ch'Last;
Encoding.Read (Ch, Pos, C);
end loop;
Error := No_Symbol;
if Ch (First .. Last) = "-0" then
Val := (Value => Find (Symbols, "0"));
else
Val := (Value => Find (Symbols, Ch (First .. Last)));
end if;
end Internal_Value;
-----------------------
-- Value_No_Exponent --
-----------------------
procedure Value_No_Exponent
(Symbols : Sax.Utils.Symbol_Table;
Ch : Unicode.CES.Byte_Sequence;
Val : out Arbitrary_Precision_Number;
Error : out Sax.Symbols.Symbol) is
begin
Internal_Value (Ch, Symbols, False, Val, Error);
end Value_No_Exponent;
-------------
-- Get_Exp --
-------------
function Get_Exp (Num : String) return Long_Long_Integer is
Pos : Integer := Num'Last;
begin
while Pos >= Num'First
and then Num (Pos) /= 'E'
and then Num (Pos) /= 'e'
loop
Pos := Pos - 1;
end loop;
if Pos >= Num'First then
return Long_Long_Integer'Value (Num (Pos + 1 .. Num'Last));
else
return 0;
end if;
end Get_Exp;
--------------
-- Get_Fore --
--------------
procedure Get_Fore (Num : String; First, Last : out Integer) is
Pos : Integer;
begin
if Num (Num'First) = '-' or else Num (Num'First) = '+' then
First := Num'First + 1;
else
First := Num'First;
end if;
Pos := First;
while Pos <= Num'Last
and then Num (Pos) /= '.'
and then Num (Pos) /= 'E'
and then Num (Pos) /= 'e'
loop
Pos := Pos + 1;
end loop;
Last := Pos - 1;
end Get_Fore;
-------------------
-- To_Next_Digit --
-------------------
procedure To_Next_Digit (Num : String; Pos : in out Integer) is
begin
Pos := Pos + 1;
if Pos <= Num'Last then
if Num (Pos) = 'E' or Num (Pos) = 'e' then
Pos := Num'Last + 1;
elsif Num (Pos) = '.' then
Pos := Pos + 1;
end if;
end if;
end To_Next_Digit;
-------------
-- Compare --
-------------
function Compare (Num1, Num2 : String) return Compare_Result is
Num1_Negative : constant Boolean := Num1 (Num1'First) = '-';
Num2_Negative : constant Boolean := Num2 (Num2'First) = '-';
Exp1, Exp2 : Long_Long_Integer;
Pos1, Pos2 : Integer;
Fore_First1, Fore_Last1 : Integer;
Fore_First2, Fore_Last2 : Integer;
begin
-- We have to normalize the numbers (take care of exponents
if Num1_Negative and not Num2_Negative then
return Less_Than;
elsif not Num1_Negative and Num2_Negative then
return Greater_Than;
else
-- They have the same sign
Exp1 := Get_Exp (Num1);
Exp2 := Get_Exp (Num2);
Get_Fore (Num1, Fore_First1, Fore_Last1);
Get_Fore (Num2, Fore_First2, Fore_Last2);
-- Different lengths ?
if Long_Long_Integer (Fore_Last1 - Fore_First1) + Exp1 >
Long_Long_Integer (Fore_Last2 - Fore_First2) + Exp2
then
if Num1_Negative then
return Less_Than;
else
return Greater_Than;
end if;
elsif Long_Long_Integer (Fore_Last1 - Fore_First1) + Exp1 <
Long_Long_Integer (Fore_Last2 - Fore_First2) + Exp2
then
if Num1_Negative then
return Greater_Than;
else
return Less_Than;
end if;
end if;
-- Same length of fore parts, we need to compare the digits
Pos1 := Fore_First1;
Pos2 := Fore_First2;
loop
if Num1 (Pos1) > Num2 (Pos2) then
if Num1_Negative then
return Less_Than;
else
return Greater_Than;
end if;
elsif Num1 (Pos1) < Num2 (Pos2) then
if Num1_Negative then
return Greater_Than;
else
return Less_Than;
end if;
end if;
To_Next_Digit (Num1, Pos1);
To_Next_Digit (Num2, Pos2);
if Pos1 > Num1'Last
and then Pos2 > Num2'Last
then
return Equal;
elsif Pos1 > Num1'Last then
-- If only "0" remain (and because we are in the decimal part),
-- the two numbers are equal.
while Num2 (Pos2) = '0' loop
To_Next_Digit (Num2, Pos2);
if Pos2 > Num2'Last then
return Equal;
end if;
end loop;
if Num1_Negative then
return Greater_Than;
else
return Less_Than;
end if;
elsif Pos2 > Num2'Last then
-- If only "0" remain (and because we are in the decimal part),
-- the two numbers are equal.
while Num1 (Pos1) = '0' loop
To_Next_Digit (Num1, Pos1);
if Pos1 > Num1'Last then
return Equal;
end if;
end loop;
if Num1_Negative then
return Less_Than;
else
return Greater_Than;
end if;
end if;
end loop;
end if;
end Compare;
---------
-- "<" --
---------
function "<" (Num1, Num2 : Arbitrary_Precision_Number) return Boolean is
begin
return Compare (Get (Num1.Value).all, Get (Num2.Value).all) = Less_Than;
end "<";
----------
-- "<=" --
----------
function "<=" (Num1, Num2 : Arbitrary_Precision_Number) return Boolean is
begin
return Compare (Get (Num1.Value).all, Get (Num2.Value).all) /=
Greater_Than;
end "<=";
---------
-- "=" --
---------
function "=" (Num1, Num2 : Arbitrary_Precision_Number) return Boolean is
begin
if Num1.Value = No_Symbol then
return Num2.Value = No_Symbol;
elsif Num2.Value = No_Symbol then
return False;
else
return Compare (Get (Num1.Value).all, Get (Num2.Value).all) = Equal;
end if;
end "=";
----------
-- ">=" --
----------
function ">=" (Num1, Num2 : Arbitrary_Precision_Number) return Boolean is
begin
return Compare (Get (Num1.Value).all, Get (Num2.Value).all) /= Less_Than;
end ">=";
---------
-- ">" --
---------
function ">" (Num1, Num2 : Arbitrary_Precision_Number) return Boolean is
begin
return Compare (Get (Num1.Value).all, Get (Num2.Value).all) =
Greater_Than;
end ">";
------------------
-- Check_Digits --
------------------
function Check_Digits
(Symbols : Sax.Utils.Symbol_Table;
Num : Arbitrary_Precision_Number;
Fraction_Digits, Total_Digits : Integer := -1)
return Sax.Symbols.Symbol
is
Value : constant Cst_Byte_Sequence_Access := Get (Num.Value);
Exp : constant Long_Long_Integer := Get_Exp (Value.all);
Fore_First, Fore_Last : Integer;
Pos : Integer;
Digits_Count : Natural := 0;
Frac_Digits : Natural := 0;
begin
Get_Fore (Value.all, Fore_First, Fore_Last);
-- Now count the significant digits (including fractional part)
Pos := Value'First;
if Value (Pos) = '-' or Value (Pos) = '+' then
Pos := Pos + 1;
end if;
if Value (Pos) = '.' then
Pos := Pos + 1;
end if;
while Pos <= Value'Last loop
Digits_Count := Digits_Count + 1;
if Pos > Fore_Last then
Frac_Digits := Frac_Digits + 1;
end if;
To_Next_Digit (Value.all, Pos);
end loop;
if Total_Digits > 0 then
-- Gross estimation
if Long_Long_Integer (Fore_Last - Fore_First) + Exp >=
Long_Long_Integer (Total_Digits)
then
return Find
(Symbols, "Number " & Value.all
& " has too many digits (totalDigits is"
& Integer'Image (Total_Digits) & ')');
end if;
if Digits_Count > Total_Digits then
return Find
(Symbols, "Number " & Value.all
& " has too many digits (totalDigits is"
& Integer'Image (Total_Digits) & ")");
end if;
end if;
if Fraction_Digits >= 0 then
if Long_Long_Integer (Frac_Digits) - Exp >
Long_Long_Integer (Fraction_Digits)
then
return Find
(Symbols, "Number " & Value.all
& " has too many fractional digits (fractionDigits is"
& Integer'Image (Fraction_Digits) & ')');
end if;
end if;
return No_Symbol;
end Check_Digits;
----------
-- "<=" --
----------
function "<=" (F1, F2 : XML_Float) return Boolean is
begin
case F1.Kind is
when NaN =>
return False;
when Plus_Infinity =>
return False;
when Minus_Infinity =>
return True;
when Standard_Float =>
case F2.Kind is
when NaN =>
return False;
when Plus_Infinity =>
return True;
when Minus_Infinity =>
return False;
when Standard_Float =>
return F1.Value <= F2.Value;
end case;
end case;
end "<=";
----------
-- ">=" --
----------
function ">=" (F1, F2 : XML_Float) return Boolean is
begin
return not (F1 < F2);
end ">=";
---------
-- ">" --
---------
function ">" (F1, F2 : XML_Float) return Boolean is
begin
return not (F1 <= F2);
end ">";
---------
-- "<" --
---------
function "<" (F1, F2 : XML_Float) return Boolean is
begin
case F1.Kind is
when NaN =>
return False;
when Plus_Infinity =>
return False;
when Minus_Infinity =>
return True;
when Standard_Float =>
case F2.Kind is
when NaN =>
return False;
when Plus_Infinity =>
return True;
when Minus_Infinity =>
return False;
when Standard_Float =>
return F1.Value < F2.Value;
end case;
end case;
end "<";
-----------
-- Value --
-----------
function Value (Str : String) return XML_Float is
begin
if Str = "NaN" then
return XML_Float'(Kind => NaN);
elsif Str = "INF" then
return XML_Float'(Kind => Plus_Infinity);
elsif Str = "-INF" then
return XML_Float'(Kind => Minus_Infinity);
else
return XML_Float'(Kind => Standard_Float,
Value => Long_Long_Float'Value (Str));
end if;
end Value;
-----------
-- Image --
-----------
function Image (Value : XML_Float) return String is
begin
case Value.Kind is
when NaN =>
return "NaN";
when Plus_Infinity =>
return "INF";
when Minus_Infinity =>
return "-INF";
when Standard_Float =>
declare
Str : constant String := Long_Long_Float'Image (Value.Value);
E : constant Integer := Index (Str, "E");
begin
if E < Str'First then
for J in reverse Str'Range loop
if Str (J) /= '0' then
return Str (Str'First .. J);
end if;
end loop;
else
for J in reverse Str'First .. E - 1 loop
if Str (J) /= '0' then
return Str (Str'First .. J) & Str (E .. Str'Last);
end if;
end loop;
end if;
return Str;
end;
end case;
end Image;
end Schema.Decimal;
|