| 10 | namespace detail { |
| 11 | |
| 12 | fl::string hex(u64 value, HexIntWidth width, bool is_negative, bool uppercase, bool pad_to_width) { |
| 13 | // Determine target width in hex characters based on integer bit width |
| 14 | size_t target_width = 0; |
| 15 | switch (width) { |
| 16 | case HexIntWidth::Width8: target_width = 2; break; // 8 bits = 2 hex chars |
| 17 | case HexIntWidth::Width16: target_width = 4; break; // 16 bits = 4 hex chars |
| 18 | case HexIntWidth::Width32: target_width = 8; break; // 32 bits = 8 hex chars |
| 19 | case HexIntWidth::Width64: target_width = 16; break; // 64 bits = 16 hex chars |
| 20 | } |
| 21 | |
| 22 | fl::string result; |
| 23 | const char* digits = uppercase ? "0123456789ABCDEF" : "0123456789abcdef"; |
| 24 | |
| 25 | // Convert value to hex string (minimal representation) |
| 26 | u64 temp_value = value; |
| 27 | if (temp_value == 0) { |
| 28 | // Special case: zero should be "0" not empty string |
| 29 | result = fl::string("0"); |
| 30 | } else { |
| 31 | while (temp_value > 0) { |
| 32 | char ch = digits[temp_value % 16]; |
| 33 | // Convert char to string since fl::string::append treats char as number |
| 34 | char temp_ch_str[2] = {ch, '\0'}; |
| 35 | fl::string digit_str(temp_ch_str); |
| 36 | // Use += since + operator is not defined for fl::string |
| 37 | fl::string temp = digit_str; |
| 38 | temp += result; |
| 39 | result = temp; |
| 40 | temp_value /= 16; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Pad with leading zeros to target width (only if padding is requested) |
| 45 | if (pad_to_width) { |
| 46 | while (result.size() < target_width) { |
| 47 | fl::string zero_str("0"); |
| 48 | zero_str += result; |
| 49 | result = zero_str; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Add negative sign if needed |
| 54 | if (is_negative) { |
| 55 | fl::string minus_str("-"); |
| 56 | minus_str += result; |
| 57 | result = minus_str; |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | } // namespace detail |
| 64 | |