Botan 3.14.1
Crypto and TLS for C&
telephone_number.h
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_TELEPHONE_NUMBER_H_
8#define BOTAN_TELEPHONE_NUMBER_H_
9
10#include <botan/types.h>
11#include <optional>
12#include <string>
13#include <string_view>
14
15namespace Botan {
16
17/**
18* A telephone number as used in RFC 8226 STIR certificates
19*
20* TelephoneNumber ::= IA5String (SIZE (1..15)) (FROM ("0123456789#*"))
21*
22* This is the dialed form rather than E.164: '*' and '#' may appear, and
23* leading zeros are significant, so "0123" and "123" are distinct numbers.
24*/
25class BOTAN_PUBLIC_API(3, 14) TelephoneNumber final {
26 public:
27 /**
28 * Parse a telephone number
29 * @return the number, or nullopt unless @p tn is 1 to 15 characters from "0123456789#*"
30 */
31 static std::optional<TelephoneNumber> from_string(std::string_view tn);
32
33 /**
34 * @return the number as it was given
35 */
36 const std::string& to_string() const { return m_number; }
37
38 /**
39 * @return true if the number contains '*' or '#'
40 */
41 bool is_wildcard() const;
42
43 /**
44 * @return the length of the number in characters
45 */
46 size_t length() const { return m_number.size(); }
47
48 /**
49 * @return the number as an integer, or nullopt if is_wildcard()
50 */
51 std::optional<uint64_t> numeric_value() const;
52
53 /**
54 * Numbers are ordered by their string form
55 */
56 auto operator<=>(const TelephoneNumber&) const = default;
57
58 private:
59 explicit TelephoneNumber(std::string number) : m_number(std::move(number)) {}
60
61 std::string m_number;
62};
63
64/**
65* A block of consecutive telephone numbers, all of the same length
66*/
67class BOTAN_PUBLIC_API(3, 14) TelephoneNumberRange final {
68 public:
69 /**
70 * Create the range of @p count numbers beginning at @p start
71 *
72 * @return the range, or nullopt if @p start is a wildcard, @p count is
73 * less than 2, or the range would run past the last number of the same length
74 * as @p start
75 */
76 static std::optional<TelephoneNumberRange> from(const TelephoneNumber& start, uint64_t count);
77
78 /**
79 * @return the first number in the range
80 */
81 const TelephoneNumber& start() const { return m_start; }
82
83 /**
84 * @return the number of numbers in the range
85 */
86 uint64_t count() const { return m_count; }
87
88 /**
89 * @return the final number in the range
90 */
91 TelephoneNumber last() const;
92
93 /**
94 * @return true if @p tn has the same length as this range and lies within it
95 */
96 bool contains(const TelephoneNumber& tn) const;
97
98 /**
99 * @return true if every number of @p other lies within this range
100 */
101 bool contains(const TelephoneNumberRange& other) const;
102
103 /**
104 * Ranges are ordered by start and then by count
105 */
106 auto operator<=>(const TelephoneNumberRange&) const = default;
107
108 private:
109 TelephoneNumberRange(TelephoneNumber start, uint64_t count) : m_start(std::move(start)), m_count(count) {}
110
111 TelephoneNumber m_start;
112 uint64_t m_count;
113};
114
115} // namespace Botan
116
117#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
static std::optional< TelephoneNumberRange > from(const TelephoneNumber &start, uint64_t count)
auto operator<=>(const TelephoneNumberRange &) const =default
const TelephoneNumber & start() const
static std::optional< TelephoneNumber > from_string(std::string_view tn)
const std::string & to_string() const
auto operator<=>(const TelephoneNumber &) const =default