Botan 3.14.1
Crypto and TLS for C&
Botan::TelephoneNumberRange Class Referencefinal

#include <telephone_number.h>

Public Member Functions

bool contains (const TelephoneNumber &tn) const
bool contains (const TelephoneNumberRange &other) const
uint64_t count () const
TelephoneNumber last () const
auto operator<=> (const TelephoneNumberRange &) const =default
const TelephoneNumber & start () const

Static Public Member Functions

static std::optional< TelephoneNumberRange > from (const TelephoneNumber &start, uint64_t count)

Detailed Description

A block of consecutive telephone numbers, all of the same length

Definition at line 67 of file telephone_number.h.

Member Function Documentation

◆ contains() [1/2]

bool Botan::TelephoneNumberRange::contains ( const TelephoneNumber & tn) const
Returns
true if tn has the same length as this range and lies within it

Definition at line 80 of file telephone_number.cpp.

80 {
81 const auto value = tn.numeric_value();
82 if(!value.has_value() || tn.length() != m_start.length()) {
83 return false;
84 }
85 const uint64_t first = m_start.numeric_value().value();
86 return *value >= first && (*value - first) < m_count;
87}

References Botan::TelephoneNumber::length(), and Botan::TelephoneNumber::numeric_value().

◆ contains() [2/2]

bool Botan::TelephoneNumberRange::contains ( const TelephoneNumberRange & other) const
Returns
true if every number of other lies within this range

Definition at line 89 of file telephone_number.cpp.

89 {
90 if(other.m_start.length() != m_start.length()) {
91 return false;
92 }
93 const uint64_t first = m_start.numeric_value().value();
94 const uint64_t other_first = other.m_start.numeric_value().value();
95 return other_first >= first && (other_first - first) + other.m_count <= m_count;
96}

References Botan::TelephoneNumber::length(), and Botan::TelephoneNumber::numeric_value().

◆ count()

uint64_t Botan::TelephoneNumberRange::count ( ) const
inline
Returns
the number of numbers in the range

Definition at line 86 of file telephone_number.h.

86{ return m_count; }

Referenced by Botan::Cert_Extension::TNAuthList::encompasses(), from(), and operator<=>().

◆ from()

std::optional< TelephoneNumberRange > Botan::TelephoneNumberRange::from ( const TelephoneNumber & start,
uint64_t count )
static

Create the range of count numbers beginning at start

Returns
the range, or nullopt if start is a wildcard, count is less than 2, or the range would run past the last number of the same length as start

Definition at line 48 of file telephone_number.cpp.

48 {
49 const auto pow10 = [](size_t n) -> uint64_t {
50 // Always in range since the input length is at most 15
51 uint64_t v = 1;
52 for(size_t i = 0; i != n; ++i) {
53 v *= 10;
54 }
55 return v;
56 };
57
58 const auto first = start.numeric_value();
59 if(!first.has_value() || count <= 1) {
60 return std::nullopt;
61 }
62 if(count > pow10(start.length()) - *first) {
63 return std::nullopt;
64 }
65 return TelephoneNumberRange(start, count);
66}
const TelephoneNumber & start() const

References count(), and start().

Referenced by Botan::Cert_Extension::TNAuthList::Entry::decode_from().

◆ last()

TelephoneNumber Botan::TelephoneNumberRange::last ( ) const
Returns
the final number in the range

Definition at line 68 of file telephone_number.cpp.

68 {
69 uint64_t value = m_start.numeric_value().value() + m_count - 1;
70 std::string digits(m_start.length(), '0');
71 for(size_t i = digits.size(); i > 0; --i) {
72 digits[i - 1] = static_cast<char>('0' + (value % 10));
73 value /= 10;
74 }
75 auto tn = TelephoneNumber::from_string(digits);
76 BOTAN_ASSERT_NOMSG(tn.has_value());
77 return *tn;
78}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::optional< TelephoneNumber > from_string(std::string_view tn)

References BOTAN_ASSERT_NOMSG, Botan::TelephoneNumber::from_string(), and Botan::TelephoneNumber::numeric_value().

◆ operator<=>()

auto Botan::TelephoneNumberRange::operator<=> ( const TelephoneNumberRange & ) const
default

Ranges are ordered by start and then by count

References count(), and start().

◆ start()

const TelephoneNumber & Botan::TelephoneNumberRange::start ( ) const
inline
Returns
the first number in the range

Definition at line 81 of file telephone_number.h.

81{ return m_start; }

Referenced by Botan::Cert_Extension::TNAuthList::encompasses(), from(), and operator<=>().


The documentation for this class was generated from the following files: