[honey] Clean up unused values
[xapian.git] / xapian-core / backends / byte_length_strings.h
blobe3647a46b5e400d2f3c6ef23c87c4fce1700fdac
1 /** @file byte_length_strings.h
2 * @brief Handle decoding lists of strings with byte lengths
3 */
4 /* Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #ifndef XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
23 #define XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
25 #include <xapian/error.h>
27 #include <string>
29 // We XOR the length values with this so that they are more likely to coincide
30 // with lower case ASCII letters, which are likely to be common. This means
31 // that zlib should do a better job of compressing tag values - in tests, this
32 // gave 5% better compression.
33 #define MAGIC_XOR_VALUE 96
35 class ByteLengthPrefixedStringItor {
36 const unsigned char * p;
37 size_t left;
39 ByteLengthPrefixedStringItor(const unsigned char * p_, size_t left_)
40 : p(p_), left(left_) { }
42 public:
43 explicit ByteLengthPrefixedStringItor(const std::string & s)
44 : p(reinterpret_cast<const unsigned char *>(s.data())),
45 left(s.size()) { }
47 std::string operator*() const {
48 size_t len = *p ^ MAGIC_XOR_VALUE;
49 return std::string(reinterpret_cast<const char *>(p + 1), len);
52 ByteLengthPrefixedStringItor operator++(int) {
53 const unsigned char * old_p = p;
54 size_t old_left = left;
55 operator++();
56 return ByteLengthPrefixedStringItor(old_p, old_left);
59 ByteLengthPrefixedStringItor & operator++() {
60 if (!left) {
61 throw Xapian::DatabaseCorruptError("Bad synonym data (none left)");
63 size_t add = (*p ^ MAGIC_XOR_VALUE) + 1;
64 if (left < add) {
65 throw Xapian::DatabaseCorruptError("Bad synonym data (too little left)");
67 p += add;
68 left -= add;
69 return *this;
72 bool at_end() const {
73 return left == 0;
77 struct ByteLengthPrefixedStringItorGt {
78 /// Return true if and only if a's string is strictly greater than b's.
79 bool operator()(const ByteLengthPrefixedStringItor *a,
80 const ByteLengthPrefixedStringItor *b) const {
81 return (**a > **b);
85 #endif // XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H