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 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Globalization;
using System.Runtime;
using System.Xml.XPath;
internal enum AxisDirection : byte
{
Forward,
Reverse
}
internal enum QueryNodeType : byte
{
Any = 0x00,
Root = 0x01,
Attribute = 0x02,
Element = 0x04,
Text = 0x08,
Comment = 0x10,
Processing = 0x20,
Namespace = 0x40,
Multiple = 0x80,
ChildNodes = (QueryNodeType.Multiple | QueryNodeType.Element | QueryNodeType.Comment | QueryNodeType.Text | QueryNodeType.Processing),
Ancestor = (QueryNodeType.Multiple | QueryNodeType.Element | QueryNodeType.Root),
All = (QueryNodeType.Multiple | QueryNodeType.Element | QueryNodeType.Attribute | QueryNodeType.Namespace | QueryNodeType.Root | QueryNodeType.Comment | QueryNodeType.Text | QueryNodeType.Processing)
}
internal enum QueryAxisType : byte
{
None = 0,
Ancestor = 1,
AncestorOrSelf = 2,
Attribute = 3,
Child = 4,
Descendant = 5,
DescendantOrSelf = 6,
Following = 7,
FollowingSibling = 8,
Namespace = 9,
Parent = 10,
Preceding = 11,
PrecedingSibling = 12,
Self = 13
}
// 4 bytes - each element is a byte
internal struct QueryAxis
{
AxisDirection direction;
QueryNodeType principalNode;
QueryAxisType type;
QueryNodeType validNodeTypes;
internal QueryAxis(QueryAxisType type, AxisDirection direction, QueryNodeType principalNode, QueryNodeType validNodeTypes)
{
this.direction = direction;
this.principalNode = principalNode;
this.type = type;
this.validNodeTypes = validNodeTypes;
}
#if NO
internal AxisDirection Direction
{
get
{
return this.direction;
}
}
#endif
internal QueryNodeType PrincipalNodeType
{
get
{
return this.principalNode;
}
}
internal QueryAxisType Type
{
get
{
return this.type;
}
}
internal QueryNodeType ValidNodeTypes
{
get
{
return this.validNodeTypes;
}
}
internal bool IsSupported()
{
switch (this.type)
{
default:
return false;
case QueryAxisType.DescendantOrSelf:
case QueryAxisType.Descendant:
case QueryAxisType.Attribute:
case QueryAxisType.Child:
case QueryAxisType.Self:
break;
}
return true;
}
}
/// <summary>
/// Information about a qname
/// </summary>
internal enum NodeQNameType : byte
{
// QName has neither name nor namespace. Entirely empty
Empty = 0x00,
// QName has a regular name
Name = 0x01,
// QName has regular namespace
Namespace = 0x02,
// QName has both name and namespace
Standard = NodeQNameType.Name | NodeQNameType.Namespace,
// QName has a wildcard name
NameWildcard = 0x04,
// QName has a wildcard namespace
NamespaceWildcard = 0x08,
// QName is entirely wildcard
Wildcard = NodeQNameType.NameWildcard | NodeQNameType.NamespaceWildcard
}
/// <summary>
/// We'll use our own class to store QNames instead of XmlQualifiedName because:
/// 1. Our is a struct. No allocations required. We have to dynamically create QNames in several places and
/// and don't want to do allocations
/// 2. Our equality tests are frequently faster. XmlQualifiedName implements .Equal with the assumption that
/// strings are atomized using a shared name table, which in the case of arbitrary object graphs, they almost
/// never will be.
/// </summary>
internal struct NodeQName
{
internal static NodeQName Empty = new NodeQName(string.Empty, string.Empty);
internal string name;
internal string ns;
internal NodeQName(string name)
: this(name, string.Empty)
{
}
internal NodeQName(string name, string ns)
{
this.name = (null == name) ? string.Empty : name;
this.ns = (null == ns) ? string.Empty : ns;
}
#if NO
internal NodeQName(string name, string ns, string defaultNS)
{
Fx.Assert(null != defaultNS, "");
this.name = (null == name) ? string.Empty : name;
this.ns = (null == ns) ? defaultNS : ns;
}
internal NodeQName(XmlQualifiedName qname)
{
this.name = qname.Name;
this.ns = qname.Namespace;
}
internal bool HasWildcard
{
get
{
return (this.IsNameWildcard || this.IsNamespaceWildcard);
}
}
#endif
internal bool IsEmpty
{
get
{
return (this.name.Length == 0 && this.ns.Length == 0);
}
}
internal bool IsNameDefined
{
get
{
return (this.name.Length > 0);
}
}
internal bool IsNameWildcard
{
get
{
return object.ReferenceEquals(this.name, QueryDataModel.Wildcard);
}
}
internal bool IsNamespaceDefined
{
get
{
return (this.ns.Length > 0);
}
}
internal bool IsNamespaceWildcard
{
get
{
return object.ReferenceEquals(this.ns, QueryDataModel.Wildcard);
}
}
internal string Name
{
get
{
return this.name;
}
#if NO
set
{
Fx.Assert(null != value, "");
this.name = value;
}
#endif
}
internal string Namespace
{
get
{
return this.ns;
}
#if NO
set
{
Fx.Assert(null != value, "");
this.ns = value;
}
#endif
}
/// <summary>
/// If this qname's strings are == to the constants defined in NodeQName, replace the strings with the
/// constants
/// </summary>
#if NO
internal bool Atomize()
{
return false;
}
#endif
internal bool EqualsName(string name)
{
return (name == this.name);
}
#if NO
internal bool Equals(string name)
{
return this.EqualsName(name);
}
internal bool Equals(string name, string ns)
{
return ( (name.Length == this.name.Length && name == this.name) && (ns.Length == this.ns.Length && ns == this.ns));
}
#endif
internal bool Equals(NodeQName qname)
{
return ((qname.name.Length == this.name.Length && qname.name == this.name) && (qname.ns.Length == this.ns.Length && qname.ns == this.ns));
}
#if NO
internal bool Equals(SeekableXPathNavigator navigator)
{
string str = navigator.LocalName;
if (this.name.Length == str.Length && this.name == str)
{
str = navigator.NamespaceURI;
return (this.ns.Length == str.Length && this.ns == str);
}
return false;
}
#endif
internal bool EqualsNamespace(string ns)
{
return (ns == this.ns);
}
#if NO
internal bool EqualsReference(NodeQName qname)
{
return (object.ReferenceEquals(qname.name, this.name) && object.ReferenceEquals(qname.ns, this.ns));
}
internal string QName()
{
return this.ns + ':' + this.name;
}
#endif
/// <summary>
/// Return this qname's type - whether the name is defined, whether the name is a wildcard etc
/// </summary>
internal NodeQNameType GetQNameType()
{
NodeQNameType type = NodeQNameType.Empty;
if (this.IsNameDefined)
{
if (this.IsNameWildcard)
{
type |= NodeQNameType.NameWildcard;
}
else
{
type |= NodeQNameType.Name;
}
}
if (this.IsNamespaceDefined)
{
if (this.IsNamespaceWildcard)
{
type |= NodeQNameType.NamespaceWildcard;
}
else
{
type |= NodeQNameType.Namespace;
}
}
return type;
}
}
internal static class QueryDataModel
{
internal static QueryAxis[] axes;
internal static string Wildcard = "*";
static QueryDataModel()
{
// Init axes table
QueryDataModel.axes = new QueryAxis[] {
new QueryAxis(QueryAxisType.None, AxisDirection.Forward, QueryNodeType.Any, QueryNodeType.Any),
new QueryAxis(QueryAxisType.Ancestor, AxisDirection.Reverse, QueryNodeType.Element, QueryNodeType.Ancestor),
new QueryAxis(QueryAxisType.AncestorOrSelf, AxisDirection.Reverse, QueryNodeType.Element, QueryNodeType.All),
new QueryAxis(QueryAxisType.Attribute, AxisDirection.Forward, QueryNodeType.Attribute, QueryNodeType.Attribute),
new QueryAxis(QueryAxisType.Child, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.ChildNodes),
new QueryAxis(QueryAxisType.Descendant, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.ChildNodes),
new QueryAxis(QueryAxisType.DescendantOrSelf, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.All),
new QueryAxis(QueryAxisType.Following, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.ChildNodes),
new QueryAxis(QueryAxisType.FollowingSibling, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.ChildNodes),
new QueryAxis(QueryAxisType.Namespace, AxisDirection.Forward, QueryNodeType.Namespace, QueryNodeType.Namespace),
new QueryAxis(QueryAxisType.Parent, AxisDirection.Reverse, QueryNodeType.Element, QueryNodeType.Ancestor),
new QueryAxis(QueryAxisType.Preceding, AxisDirection.Reverse, QueryNodeType.Element, QueryNodeType.ChildNodes),
new QueryAxis(QueryAxisType.PrecedingSibling, AxisDirection.Reverse, QueryNodeType.Element, QueryNodeType.All),
new QueryAxis(QueryAxisType.Self, AxisDirection.Forward, QueryNodeType.Element, QueryNodeType.All),
};
}
/// <summary>
/// XPath does not interpret namespace declarations as attributes
/// Any attributes that not qualified by the XmlNamespaces namespaces is therefore kosher
/// </summary>
internal static bool IsAttribute(string ns)
{
return (0 != string.CompareOrdinal("http://www.w3.org/2000/xmlns/", ns));
}
#if NO
internal static bool IsDigit(char ch)
{
return char.IsDigit(ch);
}
internal static bool IsLetter(char ch)
{
return char.IsLetter(ch);
}
internal static bool IsLetterOrDigit(char ch)
{
return char.IsLetterOrDigit(ch);
}
internal static bool IsWhitespace(char ch)
{
return char.IsWhiteSpace(ch);
}
#endif
internal static QueryAxis GetAxis(QueryAxisType type)
{
return QueryDataModel.axes[(int)type];
}
#if NO
internal static QueryNodeType GetNodeType(XPathNodeType type)
{
QueryNodeType nodeType;
switch (type)
{
default:
nodeType = QueryNodeType.Any;
break;
case XPathNodeType.Root:
nodeType = QueryNodeType.Root;
break;
case XPathNodeType.Attribute:
nodeType = QueryNodeType.Attribute;
break;
case XPathNodeType.Element:
nodeType = QueryNodeType.Element;
break;
case XPathNodeType.Comment:
nodeType = QueryNodeType.Comment;
break;
case XPathNodeType.Text:
case XPathNodeType.Whitespace:
case XPathNodeType.SignificantWhitespace:
nodeType = QueryNodeType.Text;
break;
case XPathNodeType.ProcessingInstruction:
nodeType = QueryNodeType.Processing;
break;
}
return nodeType;
}
internal static XPathNodeType GetXPathNodeType(QueryNodeType type)
{
XPathNodeType nodeType = XPathNodeType.All;
switch(type)
{
default:
break;
case QueryNodeType.Attribute:
nodeType = XPathNodeType.Attribute;
break;
case QueryNodeType.Root:
nodeType = XPathNodeType.Root;
break;
case QueryNodeType.Namespace:
nodeType = XPathNodeType.Namespace;
break;
case QueryNodeType.Element:
nodeType = XPathNodeType.Element;
break;
case QueryNodeType.Comment:
nodeType = XPathNodeType.Comment;
break;
case QueryNodeType.Text:
nodeType = XPathNodeType.Text;
break;
case QueryNodeType.Processing:
nodeType = XPathNodeType.ProcessingInstruction;
break;
}
return nodeType;
}
// Is it possible to select nodes matching the given criteria from nodes of the given type
internal static bool IsSelectPossible(QueryNodeType nodeType, NodeSelectCriteria desc)
{
if (0 != (nodeType & QueryNodeType.Attribute))
{
switch(desc.Axis.Type)
{
default:
return false;
// Navigation is possible from attributes on these axes
case QueryAxisType.Self:
case QueryAxisType.Ancestor:
case QueryAxisType.AncestorOrSelf:
case QueryAxisType.Parent:
return true;
}
}
else if (0 != (nodeType & QueryNodeType.Root))
{
if (AxisDirection.Reverse == desc.Axis.Direction)
{
return false;
}
switch(desc.Axis.Type)
{
default:
return true;
// Navigation is possible from attributes on these axes
case QueryAxisType.Attribute:
case QueryAxisType.Namespace:
return false;
}
}
return true;
}
#endif
}
internal static class QueryValueModel
{
/*
Conversions
The following EXACTLY follow the XPath 1.0 spec. Some conversions may seem ----/inefficient, but
we prefer to adhere to the spec and shall leave them be unless performance becomes an issue.
*/
internal static bool Boolean(string val)
{
Fx.Assert(null != val, "");
return (val.Length > 0);
}
internal static bool Boolean(double dblVal)
{
return (dblVal != 0 && !double.IsNaN(dblVal));
}
internal static bool Boolean(NodeSequence sequence)
{
Fx.Assert(null != sequence, "");
return sequence.IsNotEmpty;
}
internal static bool Boolean(XPathNodeIterator iterator)
{
Fx.Assert(null != iterator, "");
return (iterator.Count > 0);
}
internal static double Double(bool val)
{
return (val ? 1 : 0);
}
internal static double Double(string val)
{
// XPath does not convert numbers the same way .NET does. A string preceeded by + is actually converted
// to NAN! Go figure.. Anyway, we have to do this manually.
val = val.TrimStart();
if (val.Length > 0 && val[0] != '+')
{
double dblVal;
if (double.TryParse(val,
NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingWhite,
NumberFormatInfo.InvariantInfo,
out dblVal))
{
return dblVal;
}
}
return double.NaN;
}
internal static double Double(NodeSequence sequence)
{
Fx.Assert(null != sequence, "");
return QueryValueModel.Double(sequence.StringValue());
}
internal static double Double(XPathNodeIterator iterator)
{
Fx.Assert(null != iterator, "");
return QueryValueModel.Double(QueryValueModel.String(iterator));
}
#if NO
internal static string String(object val)
{
return val.ToString();
}
#endif
internal static string String(bool val)
{
return val ? "true" : "false"; // XPath requires all lower case. bool.ToString() returns 'False' and 'True'
}
internal static string String(double val)
{
return val.ToString(CultureInfo.InvariantCulture);
}
internal static string String(NodeSequence sequence)
{
Fx.Assert(null != sequence, "");
return sequence.StringValue();
}
internal static string String(XPathNodeIterator iterator)
{
Fx.Assert(null != iterator, "");
if (iterator.Count == 0)
{
return string.Empty;
}
else if (iterator.CurrentPosition == 0)
{
iterator.MoveNext();
return iterator.Current.Value;
}
else if (iterator.CurrentPosition == 1)
{
return iterator.Current.Value;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new InvalidOperationException(SR.GetString(SR.QueryCantGetStringForMovedIterator)));
}
}
// OPTIMIZE Comparisons in general!!
internal static bool Compare(bool x, bool y, RelationOperator op)
{
switch (op)
{
default:
return QueryValueModel.Compare(QueryValueModel.Double(x), QueryValueModel.Double(y), op);
case RelationOperator.Eq:
return (x == y);
case RelationOperator.Ne:
return (x != y);
}
}
internal static bool Compare(bool x, double y, RelationOperator op)
{
switch (op)
{
default:
return QueryValueModel.Compare(QueryValueModel.Double(x), y, op);
case RelationOperator.Eq:
return (x == QueryValueModel.Boolean(y));
case RelationOperator.Ne:
return (x != QueryValueModel.Boolean(y));
}
}
internal static bool Compare(bool x, string y, RelationOperator op)
{
Fx.Assert(null != y, "");
switch (op)
{
default:
return QueryValueModel.Compare(QueryValueModel.Double(x), QueryValueModel.Double(y), op);
case RelationOperator.Eq:
return (x == QueryValueModel.Boolean(y));
case RelationOperator.Ne:
return (x != QueryValueModel.Boolean(y));
}
}
internal static bool Compare(bool x, NodeSequence y, RelationOperator op)
{
Fx.Assert(null != y, "");
return QueryValueModel.Compare(x, QueryValueModel.Boolean(y), op);
}
internal static bool Compare(double x, bool y, RelationOperator op)
{
switch (op)
{
default:
return QueryValueModel.Compare(x, QueryValueModel.Double(y), op);
case RelationOperator.Eq:
return (QueryValueModel.Boolean(x) == y);
case RelationOperator.Ne:
return (QueryValueModel.Boolean(x) != y);
}
}
internal static bool Compare(double x, double y, RelationOperator op)
{
switch (op)
{
default:
throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new QueryProcessingException(QueryProcessingError.TypeMismatch));
case RelationOperator.Eq:
return (x == y);
case RelationOperator.Ge:
return (x >= y);
case RelationOperator.Gt:
return (x > y);
case RelationOperator.Le:
return (x <= y);
case RelationOperator.Lt:
return (x < y);
case RelationOperator.Ne:
return (x != y);
}
}
internal static bool Compare(double x, string y, RelationOperator op)
{
Fx.Assert(null != y, "");
return QueryValueModel.Compare(x, QueryValueModel.Double(y), op);
}
internal static bool Compare(double x, NodeSequence y, RelationOperator op)
{
Fx.Assert(null != y, "");
switch (op)
{
default:
return y.Compare(x, op);
case RelationOperator.Ge:
return y.Compare(x, RelationOperator.Le);
case RelationOperator.Gt:
return y.Compare(x, RelationOperator.Lt);
case RelationOperator.Le:
return y.Compare(x, RelationOperator.Ge);
case RelationOperator.Lt:
return y.Compare(x, RelationOperator.Gt);
}
}
internal static bool Compare(string x, bool y, RelationOperator op)
{
Fx.Assert(null != x, "");
switch (op)
{
default:
return QueryValueModel.Compare(QueryValueModel.Double(x), QueryValueModel.Double(y), op);
case RelationOperator.Eq:
return (y == QueryValueModel.Boolean(x));
case RelationOperator.Ne:
return (y != QueryValueModel.Boolean(x));
}
}
internal static bool Compare(string x, double y, RelationOperator op)
{
Fx.Assert(null != x, "");
return QueryValueModel.Compare(QueryValueModel.Double(x), y, op);
}
internal static bool Compare(string x, string y, RelationOperator op)
{
Fx.Assert(null != x && null != y, "");
switch (op)
{
default:
Fx.Assert("Invalid RelationOperator");
break;
case RelationOperator.Eq:
return QueryValueModel.Equals(x, y);
case RelationOperator.Ge:
case RelationOperator.Gt:
case RelationOperator.Le:
case RelationOperator.Lt:
return QueryValueModel.Compare(QueryValueModel.Double(x), QueryValueModel.Double(y), op);
case RelationOperator.Ne:
return (x.Length != y.Length || 0 != string.CompareOrdinal(x, y));
}
return false;
}
internal static bool Compare(string x, NodeSequence y, RelationOperator op)
{
Fx.Assert(null != y, "");
switch (op)
{
default:
return y.Compare(x, op);
case RelationOperator.Ge:
return y.Compare(x, RelationOperator.Le);
case RelationOperator.Gt:
return y.Compare(x, RelationOperator.Lt);
case RelationOperator.Le:
return y.Compare(x, RelationOperator.Ge);
case RelationOperator.Lt:
return y.Compare(x, RelationOperator.Gt);
}
}
internal static bool Compare(NodeSequence x, bool y, RelationOperator op)
{
Fx.Assert(null != x, "");
return QueryValueModel.Compare(QueryValueModel.Boolean(x), y, op);
}
internal static bool Compare(NodeSequence x, double y, RelationOperator op)
{
Fx.Assert(null != x, "");
return x.Compare(y, op);
}
internal static bool Compare(NodeSequence x, string y, RelationOperator op)
{
Fx.Assert(null != x, "");
return x.Compare(y, op);
}
internal static bool Compare(NodeSequence x, NodeSequence y, RelationOperator op)
{
Fx.Assert(null != x, "");
return x.Compare(y, op);
}
internal static bool CompileTimeCompare(object x, object y, RelationOperator op)
{
Fx.Assert(null != x && null != y, "");
if (x is string)
{
if (y is double)
{
return QueryValueModel.Compare((string)x, (double)y, op);
}
else if (y is string)
{
return QueryValueModel.Compare((string)x, (string)y, op);
}
}
else if (x is double)
{
if (y is double)
{
return QueryValueModel.Compare((double)x, (double)y, op);
}
else if (y is string)
{
return QueryValueModel.Compare((double)x, (string)y, op);
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QueryCompileException(QueryCompileError.InvalidComparison));
}
internal static bool Equals(bool x, string y)
{
return (x == QueryValueModel.Boolean(y));
}
internal static bool Equals(double x, string y)
{
return (x == QueryValueModel.Double(y));
}
internal static bool Equals(string x, string y)
{
return (x.Length == y.Length && x == y);
}
internal static bool Equals(NodeSequence x, string y)
{
return x.Equals(y);
}
internal static bool Equals(bool x, double y)
{
return (x == QueryValueModel.Boolean(y));
}
internal static bool Equals(double x, double y)
{
return (x == y);
}
internal static bool Equals(NodeSequence x, double y)
{
return x.Equals(y);
}
internal static double Round(double val)
{
// Math.Round does bankers rounding, which is IEEE 754, section 4.
// If a is halfway between two whole numbers, one of which by definition is even and the other odd, then
// the even number is returned. Thus Round(3.5) == Round(4.5) == 4.0
// XPath has different rules.. which is Math.Floor(a + 0.5)... with two exceptions (see below)
// The round function returns the number that is closest to the argument and that is an integer.
// If there are two such numbers, then the one that is closest to positive infinity is returned.
// If the argument is NaN, then NaN is returned.
// If the argument is positive infinity, then positive infinity is returned.
// If the argument is negative infinity, then negative infinity is returned.
// If the argument is positive zero, then positive zero is returned.
// If the argument is negative zero, then negative zero is returned.
// If the argument is less than zero, but greater than or equal to -0.5, then negative zero is returned.
// For these last two cases, the result of calling the round function is not the same as the result of
// adding 0.5 and then calling the floor function.
// Note: .NET has no positive or negative zero... so we give up and use Math.Round...
// For all other cases, we use Floor to Round...
return (-0.5 <= val && val <= 0.0) ? Math.Round(val) : Math.Floor(val + 0.5);
}
#if NO
internal static XPathResultType ResultType(ValueDataType dataType)
{
switch (dataType)
{
default:
break;
case ValueDataType.Boolean:
return XPathResultType.Boolean;
case ValueDataType.Double:
return XPathResultType.Number;
case ValueDataType.Sequence:
return XPathResultType.NodeSet;
case ValueDataType.String:
return XPathResultType.String;
}
return XPathResultType.Any;
}
#endif
}
}
|