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