2016-07-28 13:27:39 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-07-28 13:27:39 +10:00
|
|
|
*
|
2019-01-03 15:48:34 +11:00
|
|
|
* Copyright 2016-2019 Danny Robson <danny@nerdcruft.net>
|
2016-07-28 13:27:39 +10:00
|
|
|
*/
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
#pragma once
|
2016-07-28 13:27:39 +10:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "annotation.hpp"
|
2019-01-03 15:48:34 +11:00
|
|
|
#include "debug.hpp"
|
2016-08-02 18:50:35 +10:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::ascii {
|
2016-07-28 13:27:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Returns true if the supplied character is an ASCII digit.
|
2016-07-28 13:27:39 +10:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_digit (char c) noexcept
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Converts an ASCII character into a corresponding integer.
|
|
|
|
///
|
|
|
|
/// Specifically does not convert multiple characters.
|
2016-08-02 18:50:35 +10:00
|
|
|
constexpr inline
|
|
|
|
uint8_t
|
|
|
|
to_integer (char c)
|
|
|
|
{
|
|
|
|
return likely (is_digit (c)) ?
|
|
|
|
c - '0'
|
|
|
|
: throw std::invalid_argument ("character is not a digit");
|
|
|
|
}
|
|
|
|
|
2016-07-28 13:27:39 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns true if the supplied character is an upper case letter in the
|
|
|
|
/// "C" locale.
|
2016-07-28 13:27:39 +10:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_upper (char c) noexcept
|
|
|
|
{
|
|
|
|
return c >= 'A' && c <= 'Z';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns true if the supplied character is a lower case letter in the
|
|
|
|
/// "C" locale.
|
2017-05-22 15:57:40 +10:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_lower (char c) noexcept
|
|
|
|
{
|
|
|
|
return c >= 'a' && c <= 'z';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns true if the supplied character is an alphabetical character in
|
|
|
|
/// the "C" locale.
|
2018-10-31 09:29:28 +11:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_alpha (char c) noexcept
|
|
|
|
{
|
|
|
|
return is_lower (c) || is_upper (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns true if the supplied ASCII character is a hexadecimal digit.
|
2017-05-22 15:57:40 +10:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_hex (const char c) noexcept
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '0'...'9': return true;
|
|
|
|
case 'a'...'f': return true;
|
|
|
|
case 'A'...'F': return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Converts a supplied ASCII character from a hexadecimal digit into a
|
|
|
|
/// corresponding integer.
|
|
|
|
///
|
|
|
|
/// Explicitly does not cover any multi-character case.
|
2017-05-22 15:57:40 +10:00
|
|
|
constexpr inline
|
2018-01-10 17:19:39 +11:00
|
|
|
uint8_t
|
2017-05-22 15:57:40 +10:00
|
|
|
from_hex (char c)
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9' ? (c - '0' ) :
|
|
|
|
c >= 'a' && c <= 'f' ? (c - 'a' + 10) :
|
|
|
|
c >= 'A' && c <= 'F' ? (c - 'A' + 10) :
|
|
|
|
throw std::invalid_argument ("character is not hexademical");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-28 13:27:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Converts a lower case ASCII character into an upper case character.
|
|
|
|
///
|
|
|
|
/// The results are undefined if the character isn't a lowercase
|
|
|
|
/// alphabetical character in the "C" locale.
|
2016-07-28 13:27:39 +10:00
|
|
|
constexpr inline
|
|
|
|
char
|
|
|
|
to_upper (char c) noexcept
|
|
|
|
{
|
2018-12-26 15:10:21 +11:00
|
|
|
CHECK (is_lower (c));
|
2016-07-28 13:27:39 +10:00
|
|
|
return c - 'a' + 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Converts an upper case ASCII character into a lower case character.
|
|
|
|
///
|
|
|
|
/// The results are undefined if the character isn't an uppercase
|
|
|
|
/// alphabetical character in the "C" locale.
|
2016-07-28 13:27:39 +10:00
|
|
|
constexpr inline
|
|
|
|
char
|
|
|
|
to_lower (char c) noexcept
|
|
|
|
{
|
2018-12-26 15:10:21 +11:00
|
|
|
CHECK (is_upper (c));
|
2016-07-28 13:27:39 +10:00
|
|
|
return c - 'A' + 'a';
|
|
|
|
}
|
2016-08-02 18:50:26 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Returns true if the supplied ASCII character is whitespace.
|
2016-08-02 18:50:26 +10:00
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
is_space (char c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case ' ':
|
|
|
|
case '\f':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\v':
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2016-07-28 13:27:39 +10:00
|
|
|
|
2018-01-23 18:51:37 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include <vector>
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Converts a string of ASCII hex digits into a vector of u08 values.
|
|
|
|
///
|
|
|
|
/// The supplied string must have a length that is a multiple of 2.
|
2018-01-23 18:51:37 +11:00
|
|
|
std::vector<std::uint8_t>
|
|
|
|
inline
|
2019-02-09 14:57:02 +11:00
|
|
|
operator"" _hex2u08 (const char *str, size_t len)
|
2018-01-23 18:51:37 +11:00
|
|
|
{
|
|
|
|
CHECK_MOD (len, 2);
|
|
|
|
std::vector<uint8_t> res (len/2);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i += 2)
|
2018-08-05 14:42:02 +10:00
|
|
|
res[i/2] = cruft::ascii::from_hex (str[i]) << 4 | cruft::ascii::from_hex (str[i+1]);
|
2018-01-23 18:51:37 +11:00
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Converts a string of ASCII hex digits into an array of u08 values.
|
|
|
|
///
|
|
|
|
/// The supplied string must have a length that is a multiple of 2.
|
2018-01-23 18:51:37 +11:00
|
|
|
template <typename CharT, CharT ...StrV>
|
|
|
|
constexpr auto
|
|
|
|
operator"" _hex2array (void)
|
|
|
|
{
|
|
|
|
static_assert (sizeof ...(StrV) % 2 == 0);
|
|
|
|
|
|
|
|
std::array<std::uint8_t,sizeof...(StrV)/2> res {};
|
|
|
|
constexpr CharT literal[] = { StrV... };
|
|
|
|
|
|
|
|
for (size_t i = 0; i < res.size (); ++i)
|
2018-08-05 14:42:02 +10:00
|
|
|
res[i] = cruft::ascii::from_hex (literal[i*2+0]) << 4 |
|
|
|
|
cruft::ascii::from_hex (literal[i*2+1]);
|
2018-01-23 18:51:37 +11:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|