xapian-core  2.0.0
byte_length_strings.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2024 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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
22 #define XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
23 
24 #include <xapian/error.h>
25 
26 #include <string>
27 
28 // We XOR the length values with this so that they are more likely to coincide
29 // with lower case ASCII letters, which are likely to be common. This means
30 // that zlib should do a better job of compressing tag values - in tests, this
31 // gave 5% better compression.
32 #define MAGIC_XOR_VALUE 96
33 
35  const unsigned char * p;
36  size_t left;
37 
38  ByteLengthPrefixedStringItor(const unsigned char * p_, size_t left_)
39  : p(p_), left(left_) { }
40 
41  public:
42  explicit ByteLengthPrefixedStringItor(const std::string & s)
43  : p(reinterpret_cast<const unsigned char *>(s.data())),
44  left(s.size()) { }
45 
51  std::string_view operator*() const {
52  size_t len = *p ^ MAGIC_XOR_VALUE;
53  return std::string_view(reinterpret_cast<const char *>(p + 1), len);
54  }
55 
57  const unsigned char * old_p = p;
58  size_t old_left = left;
59  operator++();
60  return ByteLengthPrefixedStringItor(old_p, old_left);
61  }
62 
64  if (!left) {
65  throw Xapian::DatabaseCorruptError("Bad synonym data (none left)");
66  }
67  size_t add = (*p ^ MAGIC_XOR_VALUE) + 1;
68  if (left < add) {
69  throw Xapian::DatabaseCorruptError("Bad synonym data (too little left)");
70  }
71  p += add;
72  left -= add;
73  return *this;
74  }
75 
76  bool at_end() const {
77  return left == 0;
78  }
79 };
80 
84  const ByteLengthPrefixedStringItor *b) const {
85  return (**a > **b);
86  }
87 };
88 
89 #endif // XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
#define MAGIC_XOR_VALUE
std::string_view operator*() const
Get the current entry.
ByteLengthPrefixedStringItor operator++(int)
ByteLengthPrefixedStringItor(const std::string &s)
ByteLengthPrefixedStringItor & operator++()
ByteLengthPrefixedStringItor(const unsigned char *p_, size_t left_)
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:397
Hierarchy of classes which Xapian can throw as exceptions.
bool operator()(const ByteLengthPrefixedStringItor *a, const ByteLengthPrefixedStringItor *b) const
Return true if and only if a's string is strictly greater than b's.