Qt 4.8
qpatternmatchingfns.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QStringList>
43 
44 #include "qboolean_p.h"
45 #include "qcommonvalues_p.h"
46 #include "qitemmappingiterator_p.h"
47 #include "qpatternistlocale_p.h"
48 #include "qatomicstring_p.h"
49 
50 #include "qpatternmatchingfns_p.h"
51 
53 
54 using namespace QPatternist;
55 
57 {
58 }
59 
61 {
62  const QRegExp regexp(pattern(context));
63  QString input;
64 
65  const Item arg(m_operands.first()->evaluateSingleton(context));
66  if(arg)
67  input = arg.stringValue();
68 
69  return Boolean::fromValue(input.contains(regexp));
70 }
71 
73 {
74 }
75 
77 {
78  const QRegExp regexp(pattern(context));
79  QString input;
80 
81  const Item arg(m_operands.first()->evaluateSingleton(context));
82  if(arg)
83  input = arg.stringValue();
84 
85  const QString replacement(m_replacementString.isNull() ? parseReplacement(regexp.captureCount(), context)
87 
88 
89  return AtomicString::fromValue(input.replace(regexp, replacement));
90 }
91 
93 {
94  return QtXmlPatterns::tr("%1 must be followed by %2 or %3, not at "
95  "the end of the replacement string.")
99 }
100 
102  const DynamicContext::Ptr &context) const
103 {
104  // TODO what if there is no groups, can one rewrite to the replacement then?
105  const QString input(m_operands.at(2)->evaluateSingleton(context).stringValue());
106 
107  QString retval;
108  retval.reserve(input.size());
109  const int len = input.length();
110 
111  for(int i = 0; i < len; ++i)
112  {
113  const QChar ch(input.at(i));
114  switch(ch.toAscii())
115  {
116  case '$':
117  {
118  /* QRegExp uses '\' as opposed to '$' for marking sub groups. */
119  retval.append(QLatin1Char('\\'));
120 
121  ++i;
122  if(i == len)
123  {
124  context->error(errorAtEnd('$'), ReportContext::FORX0004, this);
125  return QString();
126  }
127 
128  const QChar nextCh(input.at(i));
129  if(nextCh.isDigit())
130  retval.append(nextCh);
131  else
132  {
133  context->error(QtXmlPatterns::tr("In the replacement string, %1 must be "
134  "followed by at least one digit when not escaped.")
135  .arg(formatKeyword(QLatin1Char('$'))),
137  return QString();
138  }
139 
140  break;
141  }
142  case '\\':
143  {
144  ++i;
145  if(i == len)
146  {
147  /* error, we've reached the end. */;
148  context->error(errorAtEnd('\\'), ReportContext::FORX0004, this);
149  }
150 
151  const QChar nextCh(input.at(i));
152  if(nextCh == QLatin1Char('\\') || nextCh == QLatin1Char('$'))
153  {
154  retval.append(ch);
155  break;
156  }
157  else
158  {
159  context->error(QtXmlPatterns::tr("In the replacement string, %1 can only be used to "
160  "escape itself or %2, not %3")
161  .arg(formatKeyword(QLatin1Char('\\')))
162  .arg(formatKeyword(QLatin1Char('$')))
163  .arg(formatKeyword(nextCh)),
165  return QString();
166  }
167  }
168  default:
169  retval.append(ch);
170  }
171  }
172 
173  return retval;
174 }
175 
177 {
178  const Expression::Ptr me(PatternPlatform::compress(context));
179 
180  if(me != this)
181  return me;
182 
183  if(m_operands.at(2)->is(IDStringValue))
184  {
185  const int capt = captureCount();
186  if(capt == -1)
187  return me;
188  else
190  }
191 
192  return me;
193 }
194 
196 {
197 }
198 
202 static inline bool qIsForwardIteratorEnd(const QString &item)
203 {
204  return item.isNull();
205 }
206 
208 {
209  return AtomicString::fromValue(subject);
210 }
211 
213 {
214  const Item arg(m_operands.first()->evaluateSingleton(context));
215  if(!arg)
217 
218  const QString input(arg.stringValue());
219  if(input.isEmpty())
221 
222  const QRegExp regExp(pattern(context));
223  const QStringList result(input.split(regExp, QString::KeepEmptyParts));
224 
225  return makeItemMappingIterator<Item>(ConstPtr(this),
226  makeListIterator(result),
228 }
229 
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QString formatKeyword(const QString &keyword)
QExplicitlySharedDataPointer< const TokenizeFN > ConstPtr
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
static QString errorAtEnd(const char ch)
Centralizes the translation string.
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const EmptyIterator< Item >::Ptr emptyIterator
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static AtomicString::Ptr fromValue(const QString &value)
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The namespace for the internal API of QtXmlPatterns.
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
static Boolean::Ptr fromValue(const bool value)
Definition: qboolean.cpp:115
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
Contains functionality for functions and expressions that uses regular expressions.
Item mapToItem(const QString &subject, const DynamicContext::Ptr &) const
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
static bool qIsForwardIteratorEnd(const QString &item)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
int captureCount() const
Returns the number of captures contained in the regular expression.
Definition: qregexp.cpp:4223
virtual QExplicitlySharedDataPointer< DynamicContext > dynamicContext() const =0
QExplicitlySharedDataPointer< DynamicContext > Ptr
QString parseReplacement(const int captureCount, const DynamicContext::Ptr &context) const
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
bool is(const ID id) const
const QRegExp pattern(const DynamicContext::Ptr &context) const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
virtual Expression::Ptr compress(const StaticContext::Ptr &context)