GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 37 / 0 / 37
Functions: 100.0% 8 / 0 / 8
Branches: 63.6% 28 / 0 / 44

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
8 * for common game modding types like memory addresses, byte values,
9 * and virtual key codes.
10 */
11
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,
26 * form feed, and vertical tab.
27 * @param s The string_view to trim.
28 * @return std::string A new string with leading/trailing whitespace removed.
29 * Returns an empty string if the input string is empty or contains only whitespace.
30 */
31 764 inline std::string trim(std::string_view s)
32 {
33 764 const char *whitespace_chars = " \t\n\r\f\v";
34
35 764 const size_t first_non_whitespace = s.find_first_not_of(whitespace_chars);
36
2/2
✓ Branch 3 → 4 taken 48 times.
✓ Branch 3 → 9 taken 716 times.
764 if (std::string_view::npos == first_non_whitespace)
37 {
38
1/2
✓ Branch 6 → 7 taken 48 times.
✗ Branch 6 → 18 not taken.
96 return "";
39 }
40
41 716 const size_t last_non_whitespace = s.find_last_not_of(whitespace_chars);
42
2/4
✓ Branch 12 → 13 taken 716 times.
✗ Branch 12 → 21 not taken.
✓ Branch 13 → 14 taken 716 times.
✗ Branch 13 → 21 not taken.
716 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 144 inline std::string format_address(uintptr_t address)
54 {
55
1/2
✓ Branch 2 → 3 taken 144 times.
✗ Branch 2 → 5 not taken.
144 return std::format("0x{:0{}X}", address, sizeof(uintptr_t) * 2);
56 }
57
58 /**
59 * @brief Formats an integer as a hexadecimal string.
60 * @param value The integer value to format.
61 * @param width Minimum width of the hex part (0 for no padding).
62 * @return std::string Formatted hex string (e.g., "0xFF").
63 */
64 24 inline std::string format_hex(int value, int width = 0)
65 {
66
2/2
✓ Branch 2 → 3 taken 19 times.
✓ Branch 2 → 5 taken 5 times.
24 if (width > 0)
67
1/2
✓ Branch 3 → 4 taken 19 times.
✗ Branch 3 → 8 not taken.
19 return std::format("0x{:0{}X}", static_cast<unsigned int>(value), width);
68
1/2
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 10 not taken.
5 return std::format("0x{:X}", static_cast<unsigned int>(value));
69 }
70
71 /**
72 * @brief Formats a ptrdiff_t as a signed hexadecimal string.
73 * @param value The value to format.
74 * @return std::string Formatted hex string (e.g., "0xFF" or "-0x10").
75 */
76 23 inline std::string format_hex(ptrdiff_t value)
77 {
78
2/2
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 5 taken 20 times.
23 if (value < 0)
79 {
80 // Two's complement negation via unsigned cast avoids UB on PTRDIFF_MIN
81 3 const auto magnitude = static_cast<size_t>(~static_cast<size_t>(value) + 1u);
82
1/2
✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 8 not taken.
3 return std::format("-0x{:X}", magnitude);
83 }
84
1/2
✓ Branch 5 → 6 taken 20 times.
✗ Branch 5 → 10 not taken.
20 return std::format("0x{:X}", static_cast<size_t>(value));
85 }
86
87 /**
88 * @brief Formats a byte value as a two-digit hexadecimal string.
89 * @param b The byte value to format.
90 * @return std::string Formatted byte (e.g., "0xCC").
91 */
92 3 inline std::string format_byte(std::byte b)
93 {
94
1/2
✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 5 not taken.
3 return std::format("0x{:02X}", static_cast<unsigned int>(b));
95 }
96
97 /**
98 * @brief Formats a vector of integers as a comma-separated hex list.
99 * @param values The vector of integer values.
100 * @return std::string Formatted list (e.g., "[0x72, 0xA0, 0x20]").
101 */
102 7 inline std::string format_int_vector(const std::vector<int> &values)
103 {
104
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 9 taken 5 times.
7 if (values.empty())
105 {
106
1/2
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 28 not taken.
4 return "[]";
107 }
108
109 // "0x" + 2+ hex digits ≈ 4 chars per entry, plus ", " separator
110 5 std::string result;
111
1/2
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 34 not taken.
5 result.reserve(1 + values.size() * 6 + 1);
112
1/2
✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 34 not taken.
5 result += '[';
113
2/2
✓ Branch 22 → 14 taken 12 times.
✓ Branch 22 → 23 taken 5 times.
17 for (size_t i = 0; i < values.size(); ++i)
114 {
115
2/2
✓ Branch 14 → 15 taken 7 times.
✓ Branch 14 → 16 taken 5 times.
12 if (i > 0)
116 {
117
1/2
✓ Branch 15 → 16 taken 7 times.
✗ Branch 15 → 34 not taken.
7 result += ", ";
118 }
119
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);
120 }
121
1/2
✓ Branch 23 → 24 taken 5 times.
✗ Branch 23 → 34 not taken.
5 result += ']';
122 5 return result;
123 5 }
124
125 /**
126 * @brief Formats a Virtual Key code as a two-digit hexadecimal string.
127 * @param vk_code The virtual key code.
128 * @return std::string Formatted VK code (e.g., "0x72").
129 */
130 3 inline std::string format_vkcode(int vk_code)
131 {
132 3 return format_hex(vk_code, 2);
133 }
134
135 /**
136 * @brief Formats a vector of Virtual Key codes.
137 * @param keys The vector of VK codes.
138 * @return std::string Formatted VK code list.
139 */
140 3 inline std::string format_vkcode_list(const std::vector<int> &keys)
141 {
142 3 return format_int_vector(keys);
143 }
144
145 } // namespace Format
146 } // namespace DetourModKit
147
148 #endif // DETOURMODKIT_FORMAT_HPP
149