1 /** @file byte_length_strings.h
2 * @brief Handle decoding lists of strings with byte lengths
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
22 #ifndef XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
23 #define XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H
25 #include <xapian/error.h>
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
;
39 ByteLengthPrefixedStringItor(const unsigned char * p_
, size_t left_
)
40 : p(p_
), left(left_
) { }
43 explicit ByteLengthPrefixedStringItor(const std::string
& s
)
44 : p(reinterpret_cast<const unsigned char *>(s
.data())),
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
;
56 return ByteLengthPrefixedStringItor(old_p
, old_left
);
59 ByteLengthPrefixedStringItor
& operator++() {
61 throw Xapian::DatabaseCorruptError("Bad synonym data (none left)");
63 size_t add
= (*p
^ MAGIC_XOR_VALUE
) + 1;
65 throw Xapian::DatabaseCorruptError("Bad synonym data (too little left)");
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 {
85 #endif // XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H