source: trunk/src/xmlpatterns/functions/qsubstringfns.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.9 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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**
[561]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.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
49QT_BEGIN_NAMESPACE
50
51using namespace QPatternist;
52
53Item 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
76Item 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
99Item 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
122Item 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
143Item 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
173QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.