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 | 949 | [[nodiscard]] inline std::string trim(std::string_view s) | |
| 32 | { | ||
| 33 | 949 | const char *whitespace_chars = " \t\n\r\f\v"; | |
| 34 | |||
| 35 | 949 | 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 903 times.
|
949 | 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 | 903 | const size_t last_non_whitespace = s.find_last_not_of(whitespace_chars); | |
| 42 |
2/4✓ Branch 12 → 13 taken 903 times.
✗ Branch 12 → 21 not taken.
✓ Branch 13 → 14 taken 903 times.
✗ Branch 13 → 21 not taken.
|
903 | 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 | 971 | [[nodiscard]] inline std::string format_address(uintptr_t address) | |
| 54 | { | ||
| 55 |
2/2✓ Branch 2 → 3 taken 970 times.
✓ Branch 2 → 5 taken 1 time.
|
971 | 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 | 30 | [[nodiscard]] inline std::string format_hex(int value, int width = 0) | |
| 68 | { | ||
| 69 |
2/2✓ Branch 2 → 3 taken 24 times.
✓ Branch 2 → 5 taken 6 times.
|
30 | if (width > 0) |
| 70 |
1/2✓ Branch 3 → 4 taken 24 times.
✗ Branch 3 → 8 not taken.
|
24 | return std::format("0x{:0{}X}", static_cast<unsigned int>(value), width); |
| 71 |
1/2✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 10 not taken.
|
6 | return std::format("0x{:X}", static_cast<unsigned int>(value)); |
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * @brief Formats a signed long as an unsigned hexadecimal string. | ||
| 76 | * @details Exact match for the 32-bit Win32 LONG family (HRESULT, LONG, LSTATUS, NTSTATUS). Negative values | ||
| 77 | * print the unsigned two's-complement bit pattern like the int overload. | ||
| 78 | * @param value The long value to format. | ||
| 79 | * @param width Minimum width of the hex part (0 for no padding). | ||
| 80 | * @return std::string Formatted hex string (e.g., "0x80004005"). | ||
| 81 | */ | ||
| 82 | 9 | [[nodiscard]] inline std::string format_hex(long value, int width = 0) | |
| 83 | { | ||
| 84 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 5 taken 7 times.
|
9 | if (width > 0) |
| 85 |
1/2✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 8 not taken.
|
2 | return std::format("0x{:0{}X}", static_cast<unsigned long>(value), width); |
| 86 |
1/2✓ Branch 5 → 6 taken 7 times.
✗ Branch 5 → 10 not taken.
|
7 | return std::format("0x{:X}", static_cast<unsigned long>(value)); |
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * @brief Formats any unsigned integer as a hexadecimal string. | ||
| 91 | * @details Constrained to std::unsigned_integral so a size_t / unsigned / uint64_t argument binds here exactly | ||
| 92 | * instead of being ambiguous between the int and ptrdiff_t overloads. The full value is preserved | ||
| 93 | * with no narrowing. The signed int and ptrdiff_t overloads are unaffected because a signed argument | ||
| 94 | * does not satisfy the constraint. | ||
| 95 | * @tparam T The unsigned integral type of the value. | ||
| 96 | * @param value The unsigned value to format. | ||
| 97 | * @param width Minimum width of the hex part (0 for no padding). | ||
| 98 | * @return std::string Formatted hex string (e.g., "0xDEADBEEF"). | ||
| 99 | */ | ||
| 100 | 10 | template <std::unsigned_integral T> [[nodiscard]] inline std::string format_hex(T value, int width = 0) | |
| 101 | { | ||
| 102 |
5/6std::__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>(unsigned long, int):
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 2 times.
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 5 times.
|
10 | if (width > 0) |
| 103 |
2/6std::__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>(unsigned long, int):
✗ Branch 3 → 4 not taken.
✗ 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); |
| 104 |
3/6std::__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>(unsigned long, int):
✓ Branch 5 → 6 taken 2 times.
✗ 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 5 times.
✗ Branch 5 → 9 not taken.
|
8 | return std::format("0x{:X}", value); |
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * @brief Formats a ptrdiff_t as a signed hexadecimal string. | ||
| 109 | * @details The signed 64-bit path (LONG_PTR / SSIZE_T / long long on LLP64), and the one signed overload that | ||
| 110 | * prints a leading '-' with the magnitude rather than the two's-complement bit pattern. A pointer | ||
| 111 | * difference is a distance, not a register image. The pad count applies to the hex digits only; the | ||
| 112 | * '-' and the "0x" prefix sit outside the padded field, matching the other overloads. | ||
| 113 | * @param value The value to format. | ||
| 114 | * @param width Minimum width of the hex part (0 for no padding). | ||
| 115 | * @return std::string Formatted hex string (e.g., "0xFF" or "-0x10"). | ||
| 116 | */ | ||
| 117 | 14 | [[nodiscard]] inline std::string format_hex(ptrdiff_t value, int width = 0) | |
| 118 | { | ||
| 119 |
2/2✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 9 taken 6 times.
|
14 | if (value < 0) |
| 120 | { | ||
| 121 | // Two's complement negation via unsigned cast avoids UB on PTRDIFF_MIN | ||
| 122 | 8 | const auto magnitude = static_cast<size_t>(~static_cast<size_t>(value) + 1u); | |
| 123 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 6 taken 6 times.
|
8 | if (width > 0) |
| 124 |
1/2✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 15 not taken.
|
2 | return std::format("-0x{:0{}X}", magnitude, width); |
| 125 |
1/2✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 16 not taken.
|
6 | return std::format("-0x{:X}", magnitude); |
| 126 | } | ||
| 127 |
2/2✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 12 taken 4 times.
|
6 | if (width > 0) |
| 128 |
1/2✓ Branch 10 → 11 taken 2 times.
✗ Branch 10 → 18 not taken.
|
2 | return std::format("0x{:0{}X}", static_cast<size_t>(value), width); |
| 129 |
1/2✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 20 not taken.
|
4 | return std::format("0x{:X}", static_cast<size_t>(value)); |
| 130 | } | ||
| 131 | |||
| 132 | /** | ||
| 133 | * @brief Formats a byte value as a two-digit hexadecimal string. | ||
| 134 | * @param b The byte value to format. | ||
| 135 | * @return std::string Formatted byte (e.g., "0xCC"). | ||
| 136 | */ | ||
| 137 | 3 | [[nodiscard]] inline std::string format_byte(std::byte b) | |
| 138 | { | ||
| 139 |
1/2✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 5 not taken.
|
3 | return std::format("0x{:02X}", static_cast<unsigned int>(b)); |
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * @brief Formats a vector of integers as a comma-separated hex list. | ||
| 144 | * @param values The vector of integer values. | ||
| 145 | * @return std::string Formatted list (e.g., "[0x72, 0xA0, 0x20]"). | ||
| 146 | */ | ||
| 147 | 7 | [[nodiscard]] inline std::string format_int_vector(const std::vector<int> &values) | |
| 148 | { | ||
| 149 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 9 taken 5 times.
|
7 | if (values.empty()) |
| 150 | { | ||
| 151 |
1/2✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 28 not taken.
|
4 | return "[]"; |
| 152 | } | ||
| 153 | |||
| 154 | // "0x" + 2+ hex digits ~4 chars per entry, plus ", " separator | ||
| 155 | 5 | std::string result; | |
| 156 |
1/2✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 34 not taken.
|
5 | result.reserve(1 + values.size() * 6 + 1); |
| 157 |
1/2✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 34 not taken.
|
5 | result += '['; |
| 158 |
2/2✓ Branch 22 → 14 taken 12 times.
✓ Branch 22 → 23 taken 5 times.
|
17 | for (size_t i = 0; i < values.size(); ++i) |
| 159 | { | ||
| 160 |
2/2✓ Branch 14 → 15 taken 7 times.
✓ Branch 14 → 16 taken 5 times.
|
12 | if (i > 0) |
| 161 | { | ||
| 162 |
1/2✓ Branch 15 → 16 taken 7 times.
✗ Branch 15 → 34 not taken.
|
7 | result += ", "; |
| 163 | } | ||
| 164 |
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); |
| 165 | } | ||
| 166 |
1/2✓ Branch 23 → 24 taken 5 times.
✗ Branch 23 → 34 not taken.
|
5 | result += ']'; |
| 167 | 5 | return result; | |
| 168 | 5 | } | |
| 169 | |||
| 170 | /** | ||
| 171 | * @brief Formats a Virtual Key code as a two-digit hexadecimal string. | ||
| 172 | * @param vk_code The virtual key code. | ||
| 173 | * @return std::string Formatted VK code (e.g., "0x72"). | ||
| 174 | */ | ||
| 175 | 3 | [[nodiscard]] inline std::string format_vkcode(int vk_code) | |
| 176 | { | ||
| 177 | 3 | return format_hex(vk_code, 2); | |
| 178 | } | ||
| 179 | |||
| 180 | /** | ||
| 181 | * @brief Formats a vector of Virtual Key codes. | ||
| 182 | * @param keys The vector of VK codes. | ||
| 183 | * @return std::string Formatted VK code list. | ||
| 184 | */ | ||
| 185 | 3 | [[nodiscard]] inline std::string format_vkcode_list(const std::vector<int> &keys) | |
| 186 | { | ||
| 187 | 3 | return format_int_vector(keys); | |
| 188 | } | ||
| 189 | |||
| 190 | } // namespace format | ||
| 191 | } // namespace DetourModKit | ||
| 192 | |||
| 193 | #endif // DETOURMODKIT_FORMAT_HPP | ||
| 194 |