GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 45 / 0 / 45
Functions: 100.0% 10 / 0 / 10
Branches: 64.3% 36 / 0 / 56

include/DetourModKit/format.hpp
Line Branch Exec Source
1 #ifndef DETOURMODKIT_FORMAT_HPP
2 #define DETOURMODKIT_FORMAT_HPP
3
4 /**
5 * @file format.hpp
6 * @brief String and format utilities for DetourModKit.
7 * @details Provides string manipulation (trimming) and formatting utilities for common game modding types like memory
8 * addresses, byte values, and virtual key codes.
9 */
10
11 #include <concepts>
12 #include <cstddef>
13 #include <cstdint>
14 #include <format>
15 #include <string>
16 #include <string_view>
17 #include <vector>
18
19 namespace DetourModKit
20 {
21 namespace String
22 {
23 /**
24 * @brief Trims leading and trailing whitespace characters from a string.
25 * @details Whitespace characters considered are space, tab, newline, carriage return, form feed, and vertical
26 * tab.
27 * @param s The string_view to trim.
28 * @return std::string A new string with leading/trailing whitespace removed. Returns an empty string if the
29 * input string is empty or contains only whitespace.
30 */
31 793 [[nodiscard]] inline std::string trim(std::string_view s)
32 {
33 793 const char *whitespace_chars = " \t\n\r\f\v";
34
35 793 const size_t first_non_whitespace = s.find_first_not_of(whitespace_chars);
36
2/2
✓ Branch 3 → 4 taken 46 times.
✓ Branch 3 → 9 taken 747 times.
793 if (std::string_view::npos == first_non_whitespace)
37 {
38
1/2
✓ Branch 6 → 7 taken 46 times.
✗ Branch 6 → 18 not taken.
92 return "";
39 }
40
41 747 const size_t last_non_whitespace = s.find_last_not_of(whitespace_chars);
42
2/4
✓ Branch 12 → 13 taken 747 times.
✗ Branch 12 → 21 not taken.
✓ Branch 13 → 14 taken 747 times.
✗ Branch 13 → 21 not taken.
747 return std::string(s.substr(first_non_whitespace, (last_non_whitespace - first_non_whitespace + 1)));
43 }
44 } // namespace String
45
46 namespace Format
47 {
48 /**
49 * @brief Formats a memory address as a hexadecimal string.
50 * @param address The memory address to format.
51 * @return std::string Formatted address (e.g., "0x00007FFE12345678").
52 */
53 336 [[nodiscard]] inline std::string format_address(uintptr_t address)
54 {
55
1/2
✓ Branch 2 → 3 taken 336 times.
✗ Branch 2 → 5 not taken.
336 return std::format("0x{:0{}X}", address, sizeof(uintptr_t) * 2);
56 }
57
58 /**
59 * @brief Formats a signed integer as an unsigned hexadecimal string.
60 * @details Prints the unsigned two's-complement bit pattern, so a negative value widens to its unsigned
61 * representation (e.g. -1 -> "0xFFFFFFFF"). Use the ptrdiff_t overload when a leading '-' and the
62 * signed magnitude are wanted, or the unsigned-integral overload for size_t / unsigned values.
63 * @param value The integer value to format.
64 * @param width Minimum width of the hex part (0 for no padding).
65 * @return std::string Formatted hex string (e.g., "0xFF").
66 */
67 28 [[nodiscard]] inline std::string format_hex(int value, int width = 0)
68 {
69
2/2
✓ Branch 2 → 3 taken 23 times.
✓ Branch 2 → 5 taken 5 times.
28 if (width > 0)
70
1/2
✓ Branch 3 → 4 taken 23 times.
✗ Branch 3 → 8 not taken.
23 return std::format("0x{:0{}X}", static_cast<unsigned int>(value), width);
71
1/2
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 10 not taken.
5 return std::format("0x{:X}", static_cast<unsigned int>(value));
72 }
73
74 /**
75 * @brief Formats any unsigned integer as a hexadecimal string.
76 * @details Constrained to std::unsigned_integral so a size_t / unsigned / uint64_t argument binds here exactly
77 * instead of being ambiguous between the int and ptrdiff_t overloads. The full value is preserved
78 * with no narrowing. The signed int and ptrdiff_t overloads are unaffected because a signed argument
79 * does not satisfy the constraint.
80 * @tparam T The unsigned integral type of the value.
81 * @param value The unsigned value to format.
82 * @param width Minimum width of the hex part (0 for no padding).
83 * @return std::string Formatted hex string (e.g., "0xDEADBEEF").
84 */
85 6 template <std::unsigned_integral T> [[nodiscard]] inline std::string format_hex(T value, int width = 0)
86 {
87
4/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned int>(unsigned int, int):
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 1 time.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned long long>(unsigned long long, int):
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 3 times.
6 if (width > 0)
88
2/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned int>(unsigned int, int):
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 8 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned long long>(unsigned long long, int):
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 8 not taken.
2 return std::format("0x{:0{}X}", value, width);
89
2/4
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned int>(unsigned int, int):
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 9 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > DetourModKit::Format::format_hex<unsigned long long>(unsigned long long, int):
✓ Branch 5 → 6 taken 3 times.
✗ Branch 5 → 9 not taken.
4 return std::format("0x{:X}", value);
90 }
91
92 /**
93 * @brief Formats a ptrdiff_t as a signed hexadecimal string.
94 * @param value The value to format.
95 * @return std::string Formatted hex string (e.g., "0xFF" or "-0x10").
96 */
97 23 [[nodiscard]] inline std::string format_hex(ptrdiff_t value)
98 {
99
2/2
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 5 taken 20 times.
23 if (value < 0)
100 {
101 // Two's complement negation via unsigned cast avoids UB on PTRDIFF_MIN
102 3 const auto magnitude = static_cast<size_t>(~static_cast<size_t>(value) + 1u);
103
1/2
✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 8 not taken.
3 return std::format("-0x{:X}", magnitude);
104 }
105
1/2
✓ Branch 5 → 6 taken 20 times.
✗ Branch 5 → 10 not taken.
20 return std::format("0x{:X}", static_cast<size_t>(value));
106 }
107
108 /**
109 * @brief Formats a byte value as a two-digit hexadecimal string.
110 * @param b The byte value to format.
111 * @return std::string Formatted byte (e.g., "0xCC").
112 */
113 3 [[nodiscard]] inline std::string format_byte(std::byte b)
114 {
115
1/2
✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 5 not taken.
3 return std::format("0x{:02X}", static_cast<unsigned int>(b));
116 }
117
118 /**
119 * @brief Formats a vector of integers as a comma-separated hex list.
120 * @param values The vector of integer values.
121 * @return std::string Formatted list (e.g., "[0x72, 0xA0, 0x20]").
122 */
123 7 [[nodiscard]] inline std::string format_int_vector(const std::vector<int> &values)
124 {
125
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 9 taken 5 times.
7 if (values.empty())
126 {
127
1/2
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 28 not taken.
4 return "[]";
128 }
129
130 // "0x" + 2+ hex digits ~4 chars per entry, plus ", " separator
131 5 std::string result;
132
1/2
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 34 not taken.
5 result.reserve(1 + values.size() * 6 + 1);
133
1/2
✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 34 not taken.
5 result += '[';
134
2/2
✓ Branch 22 → 14 taken 12 times.
✓ Branch 22 → 23 taken 5 times.
17 for (size_t i = 0; i < values.size(); ++i)
135 {
136
2/2
✓ Branch 14 → 15 taken 7 times.
✓ Branch 14 → 16 taken 5 times.
12 if (i > 0)
137 {
138
1/2
✓ Branch 15 → 16 taken 7 times.
✗ Branch 15 → 34 not taken.
7 result += ", ";
139 }
140
2/4
✓ Branch 17 → 18 taken 12 times.
✗ Branch 17 → 33 not taken.
✓ Branch 18 → 19 taken 12 times.
✗ Branch 18 → 31 not taken.
12 result += format_hex(values[i], 2);
141 }
142
1/2
✓ Branch 23 → 24 taken 5 times.
✗ Branch 23 → 34 not taken.
5 result += ']';
143 5 return result;
144 5 }
145
146 /**
147 * @brief Formats a Virtual Key code as a two-digit hexadecimal string.
148 * @param vk_code The virtual key code.
149 * @return std::string Formatted VK code (e.g., "0x72").
150 */
151 3 [[nodiscard]] inline std::string format_vkcode(int vk_code)
152 {
153 3 return format_hex(vk_code, 2);
154 }
155
156 /**
157 * @brief Formats a vector of Virtual Key codes.
158 * @param keys The vector of VK codes.
159 * @return std::string Formatted VK code list.
160 */
161 3 [[nodiscard]] inline std::string format_vkcode_list(const std::vector<int> &keys)
162 {
163 3 return format_int_vector(keys);
164 }
165
166 } // namespace Format
167 } // namespace DetourModKit
168
169 #endif // DETOURMODKIT_FORMAT_HPP
170