api/valuerangeproccompat.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2007 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <string>
00024 #include "stringutils.h"
00025 
00026 using namespace std;
00027 
00028 #define XAPIAN_NO_V102_NUMBER_VRP
00029 
00030 #include <xapian/base.h>
00031 #include <xapian/queryparser.h>
00032 #include <xapian/types.h>
00033 #include <xapian/visibility.h>
00034 
00035 #include <string>
00036 
00037 // Xapian 1.0.0 and 1.0.1 had a conceptually broken implementation of
00038 // NumberValueRangeProcessor which we quickly told people to avoid using.  But to keep
00039 // ABI compatibility, we should keep it around until the next incompatible ABI change
00040 // (probably 1.1.0).  So we put the new NumberValueRangeProcessor in a subnamespace and
00041 // then pull it into namespace Xapian with "using v102::NumberValueRangeProcessor".
00042 //
00043 // This is the old NumberValueRangeProcessor implementation, which still exists
00044 // with default visibility, but isn't declared in an external or internal
00045 // header, so will only be used when dynamically linking with application code
00046 // built against Xapian 1.0.0 or 1.0.1.
00047 
00048 namespace Xapian {
00049 
00050 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00051     Xapian::valueno valno;
00052     bool prefix;
00053     std::string str;
00054 
00055   public:
00056     NumberValueRangeProcessor(Xapian::valueno valno_)
00057         : valno(valno_), prefix(false) { }
00058 
00059     NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00060                               bool prefix_ = true)
00061         : valno(valno_), prefix(prefix_), str(str_) { }
00062 
00063     Xapian::valueno operator()(std::string &begin, std::string &end);
00064 };
00065 
00066 }
00067 
00068 Xapian::valueno
00069 Xapian::NumberValueRangeProcessor::operator()(string &begin, string &end)
00070 {
00071     size_t b_b = 0, e_b = 0;
00072     size_t b_e = string::npos, e_e = string::npos;
00073 
00074     if (str.size()) {
00075         if (prefix) {
00076             // If there's a prefix, require it on the start.
00077             if (!startswith(begin, str)) {
00078                 // Prefix not given.
00079                 return Xapian::BAD_VALUENO;
00080             }
00081             b_b = str.size();
00082             // But it's optional on the end, e.g. $10..50
00083             if (startswith(end, str)) {
00084                 e_b = str.size();
00085             }
00086         } else {
00087             // If there's a suffix, require it on the end.
00088             if (!endswith(end, str)) {
00089                 // Prefix not given.
00090                 return Xapian::BAD_VALUENO;
00091             }
00092             e_e = end.size() - str.size();
00093             // But it's optional on the start, e.g. 10..50kg
00094             if (endswith(begin, str)) {
00095                 b_e = begin.size() - str.size();
00096             }
00097         }
00098     }
00099 
00100     if (begin.find_first_not_of("0123456789", b_b) != b_e)
00101         // Not a number.
00102         return Xapian::BAD_VALUENO;
00103 
00104     if (end.find_first_not_of("0123456789", e_b) != e_e)
00105         // Not a number.
00106         return Xapian::BAD_VALUENO;
00107 
00108     // Adjust begin string if necessary.
00109     if (b_b)
00110         begin.erase(0, b_b);
00111     else if (b_e != string::npos)
00112         begin.resize(b_e);
00113 
00114     // Adjust end string if necessary.
00115     if (e_b)
00116         end.erase(0, e_b);
00117     else if (e_e != string::npos)
00118         end.resize(e_e);
00119 
00120     return valno;
00121 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.