Skip to content

Commit

Permalink
make has_hex_prefix_unsafe constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 2, 2024
1 parent 5849d6a commit 3681799
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
27 changes: 11 additions & 16 deletions include/ada/checkers-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
#ifndef ADA_CHECKERS_INL_H
#define ADA_CHECKERS_INL_H

#include "ada/common_defs.h"

#include <algorithm>
#include <string_view>
#include <cstring>

namespace ada::checkers {

inline bool has_hex_prefix_unsafe(std::string_view input) {
constexpr bool has_hex_prefix_unsafe(std::string_view input) {
// This is actually efficient code, see has_hex_prefix for the assembly.
uint32_t value_one = 1;
bool is_little_endian = (reinterpret_cast<char*>(&value_one)[0] == 1);
uint16_t word0x{};
std::memcpy(&word0x, "0x", 2); // we would use bit_cast in C++20 and the
// function could be constexpr.
uint16_t two_first_bytes{};
std::memcpy(&two_first_bytes, input.data(), 2);
if (is_little_endian) {
constexpr bool is_little_endian = std::endian::native == std::endian::little;
constexpr auto word0x =
std::bit_cast<uint16_t>(static_cast<uint16_t>(0x7830));
uint16_t two_first_bytes =
static_cast<uint16_t>(input[0]) |
static_cast<uint16_t>((static_cast<uint16_t>(input[1]) << 8));
if constexpr (is_little_endian) {
two_first_bytes |= 0x2000;
} else {
two_first_bytes |= 0x020;
}
return two_first_bytes == word0x;
}

inline bool has_hex_prefix(std::string_view input) {
constexpr bool has_hex_prefix(std::string_view input) {
return input.size() >= 2 && has_hex_prefix_unsafe(input);
}

Expand All @@ -42,14 +37,14 @@ constexpr bool is_alpha(char x) noexcept {
return (to_lower(x) >= 'a') && (to_lower(x) <= 'z');
}

inline constexpr bool is_windows_drive_letter(std::string_view input) noexcept {
constexpr bool is_windows_drive_letter(std::string_view input) noexcept {
return input.size() >= 2 &&
(is_alpha(input[0]) && ((input[1] == ':') || (input[1] == '|'))) &&
((input.size() == 2) || (input[2] == '/' || input[2] == '\\' ||
input[2] == '?' || input[2] == '#'));
}

inline constexpr bool is_normalized_windows_drive_letter(
constexpr bool is_normalized_windows_drive_letter(
std::string_view input) noexcept {
return input.size() >= 2 && (is_alpha(input[0]) && (input[1] == ':'));
}
Expand Down
4 changes: 2 additions & 2 deletions include/ada/checkers.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ constexpr bool is_alpha(char x) noexcept;
*
* @see has_hex_prefix
*/
inline bool has_hex_prefix_unsafe(std::string_view input);
constexpr bool has_hex_prefix_unsafe(std::string_view input);
/**
* @private
* Check whether a string starts with 0x or 0X.
*/
inline bool has_hex_prefix(std::string_view input);
constexpr bool has_hex_prefix(std::string_view input);

/**
* @private
Expand Down

0 comments on commit 3681799

Please sign in to comment.