yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
hex_util.h
Go to the documentation of this file.
1#ifndef YAZE_SRC_CLI_UTIL_HEX_UTIL_H_
2#define YAZE_SRC_CLI_UTIL_HEX_UTIL_H_
3
4#include <climits>
5#include <cstdint>
6#include <cstdlib>
7#include <string>
8
9#include "absl/strings/string_view.h"
10
11namespace yaze {
12namespace cli {
13namespace util {
14
15// Portable hex string parser - works across all abseil versions
16// Replaces absl::SimpleHexAtoi which may not be available in older versions
17inline bool ParseHexString(absl::string_view str, int* out) {
18 if (str.empty()) {
19 return false;
20 }
21
22 // Skip optional 0x prefix
23 if (str.size() >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
24 str = str.substr(2);
25 }
26 if (str.empty()) {
27 return false;
28 }
29
30 char* end = nullptr;
31 std::string str_copy(str.data(), str.size());
32 long result = std::strtol(str_copy.c_str(), &end, 16);
33
34 if (end == str_copy.c_str() || *end != '\0') {
35 return false;
36 }
37 if (result < INT_MIN || result > INT_MAX) {
38 return false;
39 }
40
41 *out = static_cast<int>(result);
42 return true;
43}
44
45inline bool ParseHexString(absl::string_view str, uint32_t* out) {
46 if (str.empty()) {
47 return false;
48 }
49
50 // Skip optional 0x prefix
51 if (str.size() >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
52 str = str.substr(2);
53 }
54 if (str.empty()) {
55 return false;
56 }
57
58 char* end = nullptr;
59 std::string str_copy(str.data(), str.size());
60 unsigned long result = std::strtoul(str_copy.c_str(), &end, 16);
61
62 if (end == str_copy.c_str() || *end != '\0') {
63 return false;
64 }
65 if (result > UINT32_MAX) {
66 return false;
67 }
68
69 *out = static_cast<uint32_t>(result);
70 return true;
71}
72
73} // namespace util
74} // namespace cli
75} // namespace yaze
76
77#endif // YAZE_SRC_CLI_UTIL_HEX_UTIL_H_
bool ParseHexString(absl::string_view str, int *out)
Definition hex_util.h:17