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

Last change on this file since 256 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
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**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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.