• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.9
/src/base/math_util.hh
1
/**
2
 * Copyright (c) 2020, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#ifndef lnav_math_util_hh
31
#define lnav_math_util_hh
32

33
#include <cmath>
34

35
#include <sys/types.h>
36

37
#undef rounddown
38
#undef roundup
39

40
/**
41
 * Round down a number based on a given granularity.
42
 *
43
 * @param
44
 * @param step The granularity.
45
 */
46
template<typename Size, typename Step>
47
auto
48
rounddown(Size size, Step step)
10,864✔
49
{
50
    return size - (size % step);
10,864✔
51
}
52

53
template<typename Size, typename Step>
54
auto
55
roundup(Size size, Step step)
864✔
56
{
57
    auto retval = size + (step - 1);
864✔
58

59
    retval -= (retval % step);
864✔
60

61
    return retval;
864✔
62
}
63

64
inline int
65
rounddown_offset(size_t size, int step, int offset)
66
{
67
    return size - ((size - offset) % step);
68
}
69

70
template<typename Size, typename Step>
71
auto
72
roundup_size(Size size, Step step)
8,402,831✔
73
{
74
    auto retval = size + step;
8,402,831✔
75

76
    retval -= (retval % step);
8,402,831✔
77

78
    return retval;
8,402,831✔
79
}
80

81
template<typename T>
82
T
83
abs_diff(T a, T b)
66✔
84
{
85
    return a > b ? a - b : b - a;
66✔
86
}
87

88
template<typename T>
89
class clamped {
90
public:
91
    static clamped from(T value, T min, T max) { return {value, min, max}; }
92

93
    clamped& operator+=(T rhs)
×
94
    {
UNCOV
95
        if (rhs < 0) {
×
96
            return this->operator-=(-rhs);
×
97
        }
98

99
        if (this->c_value + rhs < this->c_max) {
×
UNCOV
100
            this->c_value += rhs;
×
101
        } else {
102
            this->c_value = this->c_max;
×
103
        }
104

UNCOV
105
        return *this;
×
106
    }
107

108
    clamped& operator-=(T rhs)
×
109
    {
UNCOV
110
        if (rhs < 0) {
×
UNCOV
111
            return this->operator+=(-rhs);
×
112
        }
113

114
        if (this->c_value - rhs > this->c_min) {
×
UNCOV
115
            this->c_value -= rhs;
×
116
        } else {
UNCOV
117
            this->c_value = this->c_min;
×
118
        }
119

UNCOV
120
        return *this;
×
121
    }
122

UNCOV
123
    bool available_to_consume(T rhs) const
×
124
    {
UNCOV
125
        return (this->c_value - rhs > this->c_min);
×
126
    }
127

UNCOV
128
    bool try_consume(T rhs)
×
129
    {
UNCOV
130
        if (rhs == 0) {
×
UNCOV
131
            return false;
×
132
        }
133

UNCOV
134
        if (this->c_value - rhs > this->c_min) {
×
UNCOV
135
            this->c_value -= rhs;
×
UNCOV
136
            return true;
×
137
        }
138

UNCOV
139
        return false;
×
140
    }
141

UNCOV
142
    operator T() const { return this->c_value; }
×
143

144
    bool is_min() const { return this->c_value == this->c_min; }
145

146
    T get_min() const { return this->c_min; }
147

148
    T get_max() const { return this->c_max; }
149

150
private:
UNCOV
151
    clamped(T value, T min, T max) : c_value(value), c_min(min), c_max(max) {}
×
152

153
    T c_value;
154
    T c_min;
155
    T c_max;
156
};
157

158
template<typename T>
159
std::enable_if_t<std::is_integral_v<T>, size_t>
160
count_digits(T n)
2,540✔
161
{
162
    return n == 0 ? 1 : 1 + std::floor(std::log10(std::abs(n)));
2,540✔
163
}
164

165
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc