1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! IPv4 hosts.
use Write;
use Ipv4Addr;
use crate*;
/// Make an [`Ipv4Host`].
///
/// Please note that you can also make an [`Ipv4Host`] directly from an existing [`Ipv4Addr`].
/// # Errors
/// If [`try_percent_decode`] returns an error, returns the error [`InvalidIpv4Host`].
///
/// If [`parse_ipv4_addr`] returns an error, returns the error [`InvalidIpv4Host`].
/// # Examples
/// ```
/// use std::assert_matches;
/// use std::borrow::Cow;
///
/// use better_url::util::*;
///
/// assert_matches!(make_ipv4_host( "127.0.0.1").unwrap(), (false, Cow::Borrowed(x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host( "127.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host( "0177.0x1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host( "0x7f.0X1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host( "0x7f000001").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host( "0x7%46.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host("%E2%91%a027.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// ```
/// Make an [`Ipv4Host`] from bytes.
///
/// Please note that you can also make an [`Ipv4Host`] directly from an existing [`Ipv4Addr`].
/// # Errors
/// If [`try_percent_decode_bytes`] returns an error, returns the error [`InvalidIpv4Host`].
///
/// If [`parse_ipv4_addr`] returns an error, returns the error [`InvalidIpv4Host`].
/// # Examples
/// ```
/// use std::assert_matches;
/// use std::borrow::Cow;
///
/// use better_url::util::*;
///
/// assert_matches!(make_ipv4_host_bytes( b"127.0.0.1").unwrap(), (false, Cow::Borrowed(x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes( b"127.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes( b"0177.0x1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes( b"0x7f.0X1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes( b"0x7f000001").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes( b"0x7%46.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// assert_matches!(make_ipv4_host_bytes(b"%E2%91%a027.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
///
/// // Not valid UTF-8, but when percent decoded it becomes valid.
/// assert_matches!(make_ipv4_host_bytes(b"%E2\x91%a027.1").unwrap(), (true , Cow::Owned (x), _) if x == "127.0.0.1");
/// ```
/// [The IPv4 parser](https://url.spec.whatwg.org/#concept-ipv4-parser).
///
/// Expects the input to be percent decoded and UTS46 mapped and normalized, but not necessarily an [`Ipv4Host`] literal.
///
/// See [`make_ipv4_host`] to make an [`Ipv4Host`].
/// [Parse an IPv4 number](https://url.spec.whatwg.org/#ipv4-number-parser).
///
/// For segments other than the last one, see [`parse_ipv4_u8`].
/// # Examples
/// ```
/// use better_url::util::*;
///
/// assert_eq!(parse_ipv4_u32( "0" ), Some( 0));
/// assert_eq!(parse_ipv4_u32( "9" ), Some( 9));
/// assert_eq!(parse_ipv4_u32("+9" ), None );
///
/// assert_eq!(parse_ipv4_u32("a" ), None );
/// assert_eq!(parse_ipv4_u32("A" ), None );
/// assert_eq!(parse_ipv4_u32("0x" ), Some( 0));
/// assert_eq!(parse_ipv4_u32("0X" ), Some( 0));
/// assert_eq!(parse_ipv4_u32("0xa" ), Some(10));
/// assert_eq!(parse_ipv4_u32("0xA" ), Some(10));
/// assert_eq!(parse_ipv4_u32("+0xA"), None );
/// assert_eq!(parse_ipv4_u32("0x+A"), None );
///
/// assert_eq!(parse_ipv4_u32("07" ), Some( 7));
/// assert_eq!(parse_ipv4_u32("08" ), None );
/// assert_eq!(parse_ipv4_u32("+07" ), None );
/// assert_eq!(parse_ipv4_u32("0+7" ), None );
/// ```
/// [`parse_ipv4_u32`] but returns a [`u8`] for the non-last segments.
/// # Examples
/// ```
/// use better_url::util::*;
///
/// assert_eq!(parse_ipv4_u8( "0" ), Some( 0));
/// assert_eq!(parse_ipv4_u8( "9" ), Some( 9));
/// assert_eq!(parse_ipv4_u8("+9" ), None );
///
/// assert_eq!(parse_ipv4_u8("a" ), None );
/// assert_eq!(parse_ipv4_u8("A" ), None );
/// assert_eq!(parse_ipv4_u8("0x" ), Some( 0));
/// assert_eq!(parse_ipv4_u8("0X" ), Some( 0));
/// assert_eq!(parse_ipv4_u8("0xa" ), Some(10));
/// assert_eq!(parse_ipv4_u8("0xA" ), Some(10));
/// assert_eq!(parse_ipv4_u8("+0xA"), None );
/// assert_eq!(parse_ipv4_u8("0x+A"), None );
///
/// assert_eq!(parse_ipv4_u8("07" ), Some( 7));
/// assert_eq!(parse_ipv4_u8("08" ), None );
/// assert_eq!(parse_ipv4_u8("+07" ), None );
/// assert_eq!(parse_ipv4_u8("0+7" ), None );
/// ```