LCOV - code coverage report
Current view: top level - backends - byte_length_strings.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 4ba52dacf4fb Lines: 17 19 89.5 %
Date: 2019-05-20 14:58:19 Functions: 5 5 100.0 %
Branches: 5 22 22.7 %

           Branch data     Line data    Source code
       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
       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                 :            : 
      35                 :            : class ByteLengthPrefixedStringItor {
      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                 :          4 :     explicit ByteLengthPrefixedStringItor(const std::string & s)
      44                 :          4 :         : p(reinterpret_cast<const unsigned char *>(s.data())),
      45                 :          4 :           left(s.size()) { }
      46                 :            : 
      47                 :         12 :     std::string operator*() const {
      48                 :         12 :         size_t len = *p ^ MAGIC_XOR_VALUE;
      49         [ +  - ]:         12 :         return std::string(reinterpret_cast<const char *>(p + 1), len);
      50                 :            :     }
      51                 :            : 
      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);
      57                 :            :     }
      58                 :            : 
      59                 :          4 :     ByteLengthPrefixedStringItor & operator++() {
      60         [ -  + ]:          4 :         if (!left) {
      61 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseCorruptError("Bad synonym data (none left)");
                 [ #  # ]
      62                 :            :         }
      63                 :          4 :         size_t add = (*p ^ MAGIC_XOR_VALUE) + 1;
      64         [ -  + ]:          4 :         if (left < add) {
      65 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseCorruptError("Bad synonym data (too little left)");
                 [ #  # ]
      66                 :            :         }
      67                 :          4 :         p += add;
      68                 :          4 :         left -= add;
      69                 :          4 :         return *this;
      70                 :            :     }
      71                 :            : 
      72                 :          4 :     bool at_end() const {
      73                 :          4 :         return left == 0;
      74                 :            :     }
      75                 :            : };
      76                 :            : 
      77                 :            : struct ByteLengthPrefixedStringItorGt {
      78                 :            :     /// Return true if and only if a's string is strictly greater than b's.
      79                 :          2 :     bool operator()(const ByteLengthPrefixedStringItor *a,
      80                 :            :                     const ByteLengthPrefixedStringItor *b) const {
      81 [ +  - ][ +  - ]:          2 :         return (**a > **b);
      82                 :            :     }
      83                 :            : };
      84                 :            : 
      85                 :            : #endif // XAPIAN_INCLUDED_BYTE_LENGTH_STRINGS_H

Generated by: LCOV version 1.11