Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BooleanFunctions.cs
Go to the documentation of this file.
1using System;
4
6
7internal sealed class BooleanFunctions : ValueQuery
8{
9 private readonly Query _arg;
10
12
13 public override XPathResultType StaticType => XPathResultType.Boolean;
14
16 {
17 _arg = arg;
18 _funcType = funcType;
19 }
20
22 : base(other)
23 {
24 _arg = Query.Clone(other._arg);
25 _funcType = other._funcType;
26 }
27
28 public override void SetXsltContext(XsltContext context)
29 {
30 if (_arg != null)
31 {
32 _arg.SetXsltContext(context);
33 }
34 }
35
36 public override object Evaluate(XPathNodeIterator nodeIterator)
37 {
38 return _funcType switch
39 {
40 Function.FunctionType.FuncBoolean => toBoolean(nodeIterator),
41 Function.FunctionType.FuncNot => Not(nodeIterator),
42 Function.FunctionType.FuncTrue => true,
43 Function.FunctionType.FuncFalse => false,
44 Function.FunctionType.FuncLang => Lang(nodeIterator),
45 _ => false,
46 };
47 }
48
49 internal static bool toBoolean(double number)
50 {
51 if (number != 0.0)
52 {
53 return !double.IsNaN(number);
54 }
55 return false;
56 }
57
58 internal static bool toBoolean(string str)
59 {
60 return str.Length > 0;
61 }
62
63 internal bool toBoolean(XPathNodeIterator nodeIterator)
64 {
65 object obj = _arg.Evaluate(nodeIterator);
67 {
68 return _arg.Advance() != null;
69 }
70 if (obj is string str)
71 {
72 return toBoolean(str);
73 }
74 if (obj is double)
75 {
76 return toBoolean((double)obj);
77 }
78 if (obj is bool)
79 {
80 return (bool)obj;
81 }
82 return true;
83 }
84
85 private bool Not(XPathNodeIterator nodeIterator)
86 {
87 return !(bool)_arg.Evaluate(nodeIterator);
88 }
89
90 private bool Lang(XPathNodeIterator nodeIterator)
91 {
92 string text = _arg.Evaluate(nodeIterator).ToString();
93 string xmlLang = nodeIterator.Current.XmlLang;
94 if (xmlLang.StartsWith(text, StringComparison.OrdinalIgnoreCase))
95 {
96 if (xmlLang.Length != text.Length)
97 {
98 return xmlLang[text.Length] == '-';
99 }
100 return true;
101 }
102 return false;
103 }
104
105 public override XPathNodeIterator Clone()
106 {
107 return new BooleanFunctions(this);
108 }
109}
bool Lang(XPathNodeIterator nodeIterator)
BooleanFunctions(Function.FunctionType funcType, Query arg)
readonly Function.FunctionType _funcType
bool toBoolean(XPathNodeIterator nodeIterator)
override XPathNodeIterator Clone()
static bool toBoolean(double number)
bool Not(XPathNodeIterator nodeIterator)
override void SetXsltContext(XsltContext context)
override object Evaluate(XPathNodeIterator nodeIterator)
object Evaluate(XPathNodeIterator nodeIterator)
virtual void SetXsltContext(XsltContext context)
Definition Query.cs:52
XPathNavigator Advance()
static Query Clone(Query input)
Definition Query.cs:66