LCOV - code coverage report
Current view: top level - common - parseint.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7822d31adece Lines: 20 20 100.0 %
Date: 2019-05-23 11:15:29 Functions: 10 10 100.0 %
Branches: 123 140 87.9 %

           Branch data     Line data    Source code
       1                 :            : /** @file parseint.h
       2                 :            :  * @brief Parse signed and unsigned type from string and check for trailing characters.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2019 Olly Betts
       5                 :            :  * Copyright (C) 2019 Vaibhav Kansagara
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : #ifndef XAPIAN_INCLUDED_PARSEINT_H
      22                 :            : #define XAPIAN_INCLUDED_PARSEINT_H
      23                 :            : 
      24                 :            : #include "overflow.h"
      25                 :            : #include <limits>
      26                 :            : 
      27                 :            : template<typename T>
      28                 :        123 : bool parse_unsigned(const char* p, T& res)
      29                 :            : {
      30                 :        123 :     res = 0;
      31 [ +  + ][ +  + ]:        423 :     do {
         [ +  + ][ +  + ]
                 [ +  + ]
      32                 :        491 :         unsigned char digit = *p - '0';
      33   [ +  +  +  -  :       1343 :         if (digit > 9 ||
           -  + ][ +  + ]
           [ +  +  +  -  
           -  + ][ +  + ]
           [ +  +  +  -  
           +  + ][ +  + ]
           [ +  +  +  -  
           +  + ][ +  + ]
           [ +  +  +  -  
           +  + ][ +  + ]
      34                 :        426 :             mul_overflows(res, (unsigned int)10, res) ||
      35                 :        426 :             add_overflows(res, digit, res)) {
      36                 :         68 :             return false;
      37                 :            :         }
      38                 :            :     } while (*++p);
      39                 :         55 :     return true;
      40                 :            : }
      41                 :            : 
      42                 :            : template<typename T>
      43                 :         65 : bool parse_signed(const char* p, T& res)
      44                 :            : {
      45                 :            :     typedef typename std::make_unsigned<T>::type unsigned_type;
      46                 :         65 :     unsigned_type temp = 0;
      47 [ +  + ][ +  - ]:         80 :     if (*p == '-' && parse_unsigned(++p, temp) &&
           [ +  +  +  + ]
         [ +  + ][ +  + ]
                 [ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  + ]
                 [ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  + ]
                 [ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  + ]
                 [ +  - ]
           [ +  +  +  + ]
                 [ +  + ]
      48                 :            :         // casting the min signed value to unsigned gives us its absolute value.
      49                 :         15 :         temp <= unsigned_type(std::numeric_limits<T>::min())) {
      50                 :         10 :         res = -temp;
      51                 :         10 :         return true;
      52         [ +  - ]:         80 :     } else if (parse_unsigned(p, temp) &&
           [ +  +  +  + ]
         [ +  + ][ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  - ]
           [ +  +  +  + ]
         [ +  + ][ +  - ]
           [ +  +  +  + ]
                 [ +  + ]
      53                 :         25 :                temp <= std::numeric_limits<T>::max()) {
      54                 :         15 :         res = temp;
      55                 :         15 :         return true;
      56                 :            :     }
      57                 :         65 :     return false;
      58                 :            : }
      59                 :            : 
      60                 :            : #endif

Generated by: LCOV version 1.11