Coverage Report

Created: 2025-10-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/qpdf/Util.hh
Line
Count
Source
1
#ifndef UTIL_HH
2
#define UTIL_HH
3
4
#include <qpdf/assert_debug.h>
5
6
#include <stdexcept>
7
#include <string>
8
#include <utility>
9
10
namespace qpdf::util
11
{
12
    // qpdf::util is a collection of useful utility functions for qpdf internal use. It includes
13
    // inline functions, some of which are exposed as regular functions in QUtil. Implementations
14
    // are in QUtil.cc.
15
16
    // Throw a logic_error if 'cond' does not hold.
17
    //
18
    // DO NOT USE unless it is impractical or unnecessary to cover violations during CI Testing.
19
    inline void
20
    assertion(bool cond, std::string const msg)
21
0
    {
22
0
        if (!cond) {
23
0
            throw std::logic_error(msg);
24
0
        }
25
0
    }
26
27
    inline constexpr char
28
    hex_decode_char(char digit)
29
0
    {
30
0
        return digit <= '9' && digit >= '0'
31
0
            ? char(digit - '0')
32
0
            : (digit >= 'a' ? char(digit - 'a' + 10)
33
0
                            : (digit >= 'A' ? char(digit - 'A' + 10) : '\20'));
34
0
    }
35
36
    inline constexpr bool
37
    is_hex_digit(char ch)
38
0
    {
39
0
        return hex_decode_char(ch) < '\20';
40
0
    }
41
42
    inline constexpr bool
43
    is_space(char ch)
44
0
    {
45
0
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f' || ch == '\v';
46
0
    }
47
48
    inline bool
49
    is_digit(char ch)
50
0
    {
51
0
        return (ch >= '0' && ch <= '9');
52
0
    }
53
54
    // Returns lower-case hex-encoded version of the char including a leading "#".
55
    inline std::string
56
    hex_encode_char(char c)
57
0
    {
58
0
        static auto constexpr hexchars = "0123456789abcdef";
59
0
        return {'#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]};
60
0
    }
61
62
    // Numerically increment a digit string. Ignore the last 'tail' characters.
63
    inline void
64
    increment(std::string& s, int tail = 0)
65
0
    {
66
0
        auto end = s.rend();
67
0
        for (auto it = s.rbegin() + tail; it != end; ++it) {
68
0
            ++*it;
69
0
            if (*it != ':') {
70
0
                return;
71
0
            }
72
0
            *it = '0';
73
0
        }
74
0
        s.insert(0, 1, '1');
75
0
    }
76
77
    std::string random_string(size_t len);
78
79
} // namespace qpdf::util
80
81
#endif // UTIL_HH