Coverage Report

Created: 2025-07-18 07:03

/src/qpdf/libqpdf/qpdf/Util.hh
Line
Count
Source (jump to first uncovered line)
1
#ifndef UTIL_HH
2
#define UTIL_HH
3
4
#include <string>
5
6
namespace qpdf::util
7
{
8
    // This is a collection of useful utility functions for qpdf internal use. They include inline
9
    // functions, some of which are exposed as regular functions in QUtil. Implementations are in
10
    // QUtil.cc.
11
12
    inline constexpr char
13
    hex_decode_char(char digit)
14
882k
    {
15
882k
        return digit <= '9' && digit >= '0'
16
882k
            ? char(digit - '0')
17
882k
            : (digit >= 'a' ? char(digit - 'a' + 10)
18
320k
                            : (digit >= 'A' ? char(digit - 'A' + 10) : '\20'));
19
882k
    }
20
21
    inline constexpr bool
22
    is_hex_digit(char ch)
23
0
    {
24
0
        return hex_decode_char(ch) < '\20';
25
0
    }
26
27
    inline constexpr bool
28
    is_space(char ch)
29
21.6M
    {
30
21.6M
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f' || ch == '\v';
31
21.6M
    }
32
33
    inline bool
34
    is_digit(char ch)
35
8.16M
    {
36
8.16M
        return (ch >= '0' && ch <= '9');
37
8.16M
    }
38
39
    // Returns lower-case hex-encoded version of the char including a leading "#".
40
    inline std::string
41
    hex_encode_char(char c)
42
30.3M
    {
43
30.3M
        static auto constexpr hexchars = "0123456789abcdef";
44
30.3M
        return {'#', hexchars[static_cast<unsigned char>(c) >> 4], hexchars[c & 0x0f]};
45
30.3M
    }
46
47
    // Numerically increment a digit string. Ignore the last 'tail' characters.
48
    inline void
49
    increment(std::string& s, int tail = 0)
50
181k
    {
51
181k
        auto end = s.rend();
52
201k
        for (auto it = s.rbegin() + tail; it != end; ++it) {
53
200k
            ++*it;
54
200k
            if (*it != ':') {
55
181k
                return;
56
181k
            }
57
19.8k
            *it = '0';
58
19.8k
        }
59
732
        s.insert(0, 1, '1');
60
732
    }
61
62
    std::string random_string(size_t len);
63
64
} // namespace qpdf::util
65
66
#endif // UTIL_HH