xapian-core  1.4.26
length.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2009,2012,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 USA
19  */
20 
21 #ifndef XAPIAN_INCLUDED_LENGTH_H
22 #define XAPIAN_INCLUDED_LENGTH_H
23 
24 #include <string>
25 
34 template<class T>
35 std::string
37 {
38  std::string result;
39  if (len < 255) {
40  result += static_cast<unsigned char>(len);
41  } else {
42  result += '\xff';
43  len -= 255;
44  while (true) {
45  unsigned char b = static_cast<unsigned char>(len & 0x7f);
46  len >>= 7;
47  if (!len) {
48  result += char(b | static_cast<unsigned char>(0x80));
49  break;
50  }
51  result += b;
52  }
53  }
54  return result;
55 }
56 
64 void decode_length(const char ** p, const char *end, unsigned & out);
65 
66 void decode_length(const char ** p, const char *end, unsigned long & out);
67 
68 void decode_length(const char ** p, const char *end, unsigned long long & out);
69 
80 void decode_length_and_check(const char ** p, const char *end, unsigned & out);
81 
82 void decode_length_and_check(const char ** p, const char *end,
83  unsigned long & out);
84 
85 void decode_length_and_check(const char ** p, const char *end,
86  unsigned long long & out);
87 
88 #endif // XAPIAN_INCLUDED_LENGTH_H
void decode_length_and_check(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
Definition: length.cc:112
std::string encode_length(T len)
Encode a length as a variable-length string.
Definition: length.h:36
void decode_length(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
Definition: length.cc:94