1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qboolean_p.h" |
41 | #include "qcommonvalues_p.h" |
42 | #include "qliteral_p.h" |
43 | #include "qatomicstring_p.h" |
44 | |
45 | #include "qsubstringfns_p.h" |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | using namespace QPatternist; |
50 | |
51 | Item ContainsFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
52 | { |
53 | const Item op1(m_operands.first()->evaluateSingleton(context)); |
54 | QString str1; |
55 | |
56 | if(op1) |
57 | str1 = op1.stringValue(); |
58 | |
59 | const Item op2(m_operands.at(i: 1)->evaluateSingleton(context)); |
60 | QString str2; |
61 | |
62 | if(op2) |
63 | str2 = op2.stringValue(); |
64 | |
65 | if(str2.isEmpty()) |
66 | return CommonValues::BooleanTrue; |
67 | |
68 | if(str1.isEmpty()) |
69 | return CommonValues::BooleanFalse; |
70 | |
71 | return Boolean::fromValue(value: str1.contains(s: str2, cs: caseSensitivity())); |
72 | } |
73 | |
74 | Item StartsWithFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
75 | { |
76 | const Item op1(m_operands.first()->evaluateSingleton(context)); |
77 | QString str1; |
78 | |
79 | if(op1) |
80 | str1 = op1.stringValue(); |
81 | |
82 | const Item op2(m_operands.at(i: 1)->evaluateSingleton(context)); |
83 | QString str2; |
84 | |
85 | if(op2) |
86 | str2 = op2.stringValue(); |
87 | |
88 | if(str2.isEmpty()) |
89 | return CommonValues::BooleanTrue; |
90 | |
91 | if(str1.isEmpty()) |
92 | return CommonValues::BooleanFalse; |
93 | |
94 | return Boolean::fromValue(value: str1.startsWith(s: str2, cs: caseSensitivity())); |
95 | } |
96 | |
97 | Item EndsWithFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
98 | { |
99 | const Item op1(m_operands.first()->evaluateSingleton(context)); |
100 | QString str1; |
101 | |
102 | if(op1) |
103 | str1 = op1.stringValue(); |
104 | |
105 | const Item op2(m_operands.at(i: 1)->evaluateSingleton(context)); |
106 | QString str2; |
107 | |
108 | if(op2) |
109 | str2 = op2.stringValue(); |
110 | |
111 | if(str2.isEmpty()) |
112 | return CommonValues::BooleanTrue; |
113 | |
114 | if(str1.isEmpty()) |
115 | return CommonValues::BooleanFalse; |
116 | |
117 | return Boolean::fromValue(value: str1.endsWith(s: str2, cs: caseSensitivity())); |
118 | } |
119 | |
120 | Item SubstringBeforeFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
121 | { |
122 | const Item op1(m_operands.first()->evaluateSingleton(context)); |
123 | QString str1; |
124 | |
125 | if(op1) |
126 | str1 = op1.stringValue(); |
127 | |
128 | const Item op2(m_operands.at(i: 1)->evaluateSingleton(context)); |
129 | QString str2; |
130 | |
131 | if(op2) |
132 | str2 = op2.stringValue(); |
133 | |
134 | const int pos = str1.indexOf(s: str2); |
135 | if(pos == -1) |
136 | return CommonValues::EmptyString; |
137 | |
138 | return AtomicString::fromValue(value: QString(str1.left(n: pos))); |
139 | } |
140 | |
141 | Item SubstringAfterFN::evaluateSingleton(const DynamicContext::Ptr &context) const |
142 | { |
143 | const Item op1(m_operands.first()->evaluateSingleton(context)); |
144 | QString str1; |
145 | |
146 | if(op1) |
147 | str1 = op1.stringValue(); |
148 | |
149 | const Item op2(m_operands.at(i: 1)->evaluateSingleton(context)); |
150 | QString str2; |
151 | |
152 | if(op2) |
153 | str2 = op2.stringValue(); |
154 | |
155 | if(str2.isEmpty()) |
156 | { |
157 | if(op1) |
158 | return op1; |
159 | else |
160 | return CommonValues::EmptyString; |
161 | } |
162 | |
163 | const int pos = str1.indexOf(s: str2); |
164 | |
165 | if(pos == -1) |
166 | return CommonValues::EmptyString; |
167 | |
168 | return AtomicString::fromValue(value: QString(str1.right(n: str1.length() - (pos + str2.length())))); |
169 | } |
170 | |
171 | QT_END_NAMESPACE |
172 |