xapian-core  1.4.27
byte_length_strings.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Olly Betts
5  *
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.
10  *
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.
15  *
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
20  */
21 
22 #ifndef XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
23 #define XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
24 
25 #include <xapian/error.h>
26 
27 #include <string>
28 
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
34 
36  const unsigned char * p;
37  size_t left;
38 
39  ByteLengthPrefixedStringItor(const unsigned char * p_, size_t left_)
40  : p(p_), left(left_) { }
41 
42  public:
43  explicit ByteLengthPrefixedStringItor(const std::string & s)
44  : p(reinterpret_cast<const unsigned char *>(s.data())),
45  left(s.size()) { }
46 
47  std::string operator*() const {
48  size_t len = *p ^ MAGIC_XOR_VALUE;
49  return std::string(reinterpret_cast<const char *>(p + 1), len);
50  }
51 
53  const unsigned char * old_p = p;
54  size_t old_left = left;
55  operator++();
56  return ByteLengthPrefixedStringItor(old_p, old_left);
57  }
58 
60  if (!left) {
61  throw Xapian::DatabaseCorruptError("Bad synonym data (none left)");
62  }
63  size_t add = (*p ^ MAGIC_XOR_VALUE) + 1;
64  if (left < add) {
65  throw Xapian::DatabaseCorruptError("Bad synonym data (too little left)");
66  }
67  p += add;
68  left -= add;
69  return *this;
70  }
71 
72  bool at_end() const {
73  return left == 0;
74  }
75 };
76 
80  const ByteLengthPrefixedStringItor *b) const {
81  return (**a > **b);
82  }
83 };
84 
85 #endif // XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
bool operator()(const ByteLengthPrefixedStringItor *a, const ByteLengthPrefixedStringItor *b) const
Return true if and only if a&#39;s string is strictly greater than b&#39;s.
ByteLengthPrefixedStringItor(const std::string &s)
ByteLengthPrefixedStringItor(const unsigned char *p_, size_t left_)
Hierarchy of classes which Xapian can throw as exceptions.
ByteLengthPrefixedStringItor & operator++()
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:409
#define MAGIC_XOR_VALUE
ByteLengthPrefixedStringItor operator++(int)