Initial import from the monolithic Qt.
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / functions / qsubstringfns.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qboolean_p.h"
43 #include "qcommonvalues_p.h"
44 #include "qliteral_p.h"
45 #include "qatomicstring_p.h"
46
47 #include "qsubstringfns_p.h"
48
49 QT_BEGIN_NAMESPACE
50
51 using namespace QPatternist;
52
53 Item ContainsFN::evaluateSingleton(const DynamicContext::Ptr &context) const
54 {
55     const Item op1(m_operands.first()->evaluateSingleton(context));
56     QString str1;
57
58     if(op1)
59         str1 = op1.stringValue();
60
61     const Item op2(m_operands.at(1)->evaluateSingleton(context));
62     QString str2;
63
64     if(op2)
65         str2 = op2.stringValue();
66
67     if(str2.isEmpty())
68         return CommonValues::BooleanTrue;
69
70     if(str1.isEmpty())
71         return CommonValues::BooleanFalse;
72
73     return Boolean::fromValue(str1.contains(str2, caseSensitivity()));
74 }
75
76 Item StartsWithFN::evaluateSingleton(const DynamicContext::Ptr &context) const
77 {
78     const Item op1(m_operands.first()->evaluateSingleton(context));
79     QString str1;
80
81     if(op1)
82         str1 = op1.stringValue();
83
84     const Item op2(m_operands.at(1)->evaluateSingleton(context));
85     QString str2;
86
87     if(op2)
88         str2 = op2.stringValue();
89
90     if(str2.isEmpty())
91         return CommonValues::BooleanTrue;
92
93     if(str1.isEmpty())
94         return CommonValues::BooleanFalse;
95
96     return Boolean::fromValue(str1.startsWith(str2, caseSensitivity()));
97 }
98
99 Item EndsWithFN::evaluateSingleton(const DynamicContext::Ptr &context) const
100 {
101     const Item op1(m_operands.first()->evaluateSingleton(context));
102     QString str1;
103
104     if(op1)
105         str1 = op1.stringValue();
106
107     const Item op2(m_operands.at(1)->evaluateSingleton(context));
108     QString str2;
109
110     if(op2)
111         str2 = op2.stringValue();
112
113     if(str2.isEmpty())
114         return CommonValues::BooleanTrue;
115
116     if(str1.isEmpty())
117         return CommonValues::BooleanFalse;
118
119     return Boolean::fromValue(str1.endsWith(str2, caseSensitivity()));
120 }
121
122 Item SubstringBeforeFN::evaluateSingleton(const DynamicContext::Ptr &context) const
123 {
124     const Item op1(m_operands.first()->evaluateSingleton(context));
125     QString str1;
126
127     if(op1)
128         str1 = op1.stringValue();
129
130     const Item op2(m_operands.at(1)->evaluateSingleton(context));
131     QString str2;
132
133     if(op2)
134         str2 = op2.stringValue();
135
136     const int pos = str1.indexOf(str2);
137     if(pos == -1)
138         return CommonValues::EmptyString;
139
140     return AtomicString::fromValue(QString(str1.left(pos)));
141 }
142
143 Item SubstringAfterFN::evaluateSingleton(const DynamicContext::Ptr &context) const
144 {
145     const Item op1(m_operands.first()->evaluateSingleton(context));
146     QString str1;
147
148     if(op1)
149         str1 = op1.stringValue();
150
151     const Item op2(m_operands.at(1)->evaluateSingleton(context));
152     QString str2;
153
154     if(op2)
155         str2 = op2.stringValue();
156
157     if(str2.isEmpty())
158     {
159         if(op1)
160             return op1;
161         else
162             return CommonValues::EmptyString;
163     }
164
165     const int pos = str1.indexOf(str2);
166
167     if(pos == -1)
168         return CommonValues::EmptyString;
169
170     return AtomicString::fromValue(QString(str1.right(str1.length() - (pos + str2.length()))));
171 }
172
173 QT_END_NAMESPACE