iri_string/convert.rs
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
//! Conversion between URI/IRI types.
use core::fmt;
#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::string::String;
#[cfg(feature = "alloc")]
use crate::format::{ToDedicatedString, ToStringFallible};
use crate::spec::Spec;
use crate::types::{
RiAbsoluteStr, RiFragmentStr, RiQueryStr, RiReferenceStr, RiRelativeStr, RiStr,
};
#[cfg(feature = "alloc")]
use crate::types::{
RiAbsoluteString, RiFragmentString, RiQueryString, RiReferenceString, RiRelativeString,
RiString,
};
#[cfg(feature = "alloc")]
use crate::types::{
UriAbsoluteString, UriFragmentString, UriQueryString, UriReferenceString, UriRelativeString,
UriString,
};
/// Hexadecimal digits for a nibble.
const HEXDIGITS: [u8; 16] = [
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F',
];
/// A resource identifier mapped to a URI of some kind.
///
/// Supported `Src` type are:
///
/// * IRIs:
/// + [`IriAbsoluteStr`] (alias of `RiAbsoluteStr<IriSpec>`)
/// + [`IriReferenceStr`] (alias of `RiReferenceStr<IriSpec>`)
/// + [`IriRelativeStr`] (alias of `RiRelativeStr<IriSpec>`)
/// + [`IriStr`] (alias of `RiStr<IriSpec>`)
/// * URIs:
/// + [`UriAbsoluteStr`] (alias of `RiAbsoluteStr<UriSpec>`)
/// + [`UriReferenceStr`] (alias of `RiReferenceStr<UriSpec>`)
/// + [`UriRelativeStr`] (alias of `RiRelativeStr<UriSpec>`)
/// + [`UriStr`] (alias of `RiStr<UriSpec>`)
///
/// # Examples
///
/// ```
/// use iri_string::convert::MappedToUri;
/// use iri_string::types::{IriStr, UriStr};
///
/// let src = IriStr::new("http://example.com/?alpha=\u{03B1}")?;
/// // The type is `MappedToUri<IriStr>`, but you usually don't need to specify.
/// let mapped = MappedToUri::from(src).to_string();
/// assert_eq!(mapped, "http://example.com/?alpha=%CE%B1");
/// # Ok::<_, iri_string::validate::Error>(())
/// ```
///
/// [`IriAbsoluteStr`]: crate::types::IriAbsoluteStr
/// [`IriReferenceStr`]: crate::types::IriReferenceStr
/// [`IriRelativeStr`]: crate::types::IriRelativeStr
/// [`IriStr`]: crate::types::IriStr
/// [`UriAbsoluteStr`]: crate::types::UriAbsoluteStr
/// [`UriReferenceStr`]: crate::types::UriReferenceStr
/// [`UriRelativeStr`]: crate::types::UriRelativeStr
/// [`UriStr`]: crate::types::UriStr
#[derive(Debug, Clone, Copy)]
pub struct MappedToUri<'a, Src: ?Sized>(&'a Src);
/// Implement conversions for an IRI string type.
macro_rules! impl_for_iri {
($borrowed:ident, $owned:ident, $owned_uri:ident) => {
impl<S: Spec> fmt::Display for MappedToUri<'_, $borrowed<S>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write_percent_encoded(f, self.0.as_str())
}
}
#[cfg(feature = "alloc")]
impl<S: Spec> ToDedicatedString for MappedToUri<'_, $borrowed<S>> {
type Target = $owned_uri;
fn try_to_dedicated_string(&self) -> Result<Self::Target, TryReserveError> {
let s = self.try_to_string()?;
Ok(TryFrom::try_from(s)
.expect("[validity] the IRI must be encoded into a valid URI"))
}
}
impl<'a, S: Spec> From<&'a $borrowed<S>> for MappedToUri<'a, $borrowed<S>> {
#[inline]
fn from(iri: &'a $borrowed<S>) -> Self {
Self(iri)
}
}
#[cfg(feature = "alloc")]
impl<'a, S: Spec> From<&'a $owned<S>> for MappedToUri<'a, $borrowed<S>> {
#[inline]
fn from(iri: &'a $owned<S>) -> Self {
Self(iri.as_slice())
}
}
};
}
impl_for_iri!(RiReferenceStr, RiReferenceString, UriReferenceString);
impl_for_iri!(RiStr, RiString, UriString);
impl_for_iri!(RiAbsoluteStr, RiAbsoluteString, UriAbsoluteString);
impl_for_iri!(RiRelativeStr, RiRelativeString, UriRelativeString);
impl_for_iri!(RiQueryStr, RiQueryString, UriQueryString);
impl_for_iri!(RiFragmentStr, RiFragmentString, UriFragmentString);
/// Percent-encodes and writes the IRI string using the given buffer.
fn write_percent_encoded(f: &mut fmt::Formatter<'_>, mut s: &str) -> fmt::Result {
while !s.is_empty() {
// Skip ASCII characters.
let non_ascii_pos = s.bytes().position(|b| !b.is_ascii()).unwrap_or(s.len());
let (ascii, rest) = s.split_at(non_ascii_pos);
if !ascii.is_empty() {
f.write_str(ascii)?;
s = rest;
}
if s.is_empty() {
return Ok(());
}
// Search for the next ASCII character.
let nonascii_end = s.bytes().position(|b| b.is_ascii()).unwrap_or(s.len());
let (nonasciis, rest) = s.split_at(nonascii_end);
debug_assert!(
!nonasciis.is_empty(),
"string without non-ASCII characters should have caused early return"
);
s = rest;
// Escape non-ASCII characters as percent-encoded bytes.
//
// RFC 3987 (section 3.1 step 2) says "for each character in
// 'ucschar' or 'iprivate'", but this simply means "for each
// non-ASCII characters" since any non-ASCII characters that can
// appear in an IRI match `ucschar` or `iprivate`.
/// Number of source bytes to encode at once.
const NUM_BYTES_AT_ONCE: usize = 21;
percent_encode_bytes(f, nonasciis, &mut [0_u8; NUM_BYTES_AT_ONCE * 3])?;
}
Ok(())
}
/// Percent-encode the string and pass the encoded chunks to the given function.
///
/// `buf` is used as a temporary working buffer. It is initialized by this
/// function, so users can pass any mutable byte slice with enough size.
///
/// # Precondition
///
/// The length of `buf` must be 3 bytes or more.
fn percent_encode_bytes(f: &mut fmt::Formatter<'_>, s: &str, buf: &mut [u8]) -> fmt::Result {
/// Fill the buffer by percent-encoded bytes.
///
/// Note that this function applies percent-encoding to every characters,
/// even if it is ASCII alphabet.
///
/// # Precondition
///
/// * The length of `buf` must be 3 bytes or more.
/// * All of the `buf[i * 3]` elements should already be set to `b'%'`.
// This function have many preconditions and I don't want checks for them
// to be mandatory, so make this nested inner function.
fn fill_by_percent_encoded<'a>(buf: &'a mut [u8], bytes: &mut core::str::Bytes<'_>) -> &'a str {
let src_len = bytes.len();
// `<[u8; N]>::array_chunks_mut` is unstable as of Rust 1.58.1.
for (dest, byte) in buf.chunks_exact_mut(3).zip(bytes.by_ref()) {
debug_assert_eq!(
dest.len(),
3,
"[validity] `chunks_exact()` must return a slice with the exact length"
);
debug_assert_eq!(
dest[0], b'%',
"[precondition] the buffer must be properly initialized"
);
let upper = byte >> 4;
let lower = byte & 0b1111;
dest[1] = HEXDIGITS[usize::from(upper)];
dest[2] = HEXDIGITS[usize::from(lower)];
}
let num_dest_written = (src_len - bytes.len()) * 3;
let buf_filled = &buf[..num_dest_written];
// SAFETY: `b'%'` and `HEXDIGITS[_]` are all ASCII characters, so
// `buf_filled` is filled with ASCII characters and is valid UTF-8 bytes.
unsafe {
debug_assert!(core::str::from_utf8(buf_filled).is_ok());
core::str::from_utf8_unchecked(buf_filled)
}
}
assert!(
buf.len() >= 3,
"[precondition] length of `buf` must be 3 bytes or more"
);
// Drop the elements that will never be used.
// The length to be used is always a multiple of three.
let buf_len = buf.len() / 3 * 3;
let buf = &mut buf[..buf_len];
// Fill some bytes with `%`.
// This will be vectorized by optimization (especially for long buffers),
// so no need to selectively set `buf[i * 3]`.
buf.fill(b'%');
let mut bytes = s.bytes();
// `<core::str::Bytes as ExactSizeIterator>::is_empty` is unstable as of Rust 1.58.1.
while bytes.len() != 0 {
let encoded = fill_by_percent_encoded(buf, &mut bytes);
f.write_str(encoded)?;
}
Ok(())
}
/// Percent-encodes the given IRI using the given buffer.
#[cfg(feature = "alloc")]
pub(crate) fn try_percent_encode_iri_inline(
iri: &mut String,
) -> Result<(), alloc::collections::TryReserveError> {
// Calculate the result length and extend the buffer.
let num_nonascii = count_nonascii(iri);
if num_nonascii == 0 {
// No need to escape.
return Ok(());
}
let additional = num_nonascii * 2;
iri.try_reserve(additional)?;
let src_len = iri.len();
// Temporarily take the ownership of the internal buffer.
let mut buf = core::mem::take(iri).into_bytes();
// `b'\0'` cannot appear in a valid IRI, so this default value would be
// useful in case of debugging.
buf.extend(core::iter::repeat(b'\0').take(additional));
// Fill the buffer from the tail to the head.
let mut dest_end = buf.len();
let mut src_end = src_len;
let mut rest_nonascii = num_nonascii;
while rest_nonascii > 0 {
debug_assert!(
src_end > 0,
"[validity] the source position should not overrun"
);
debug_assert!(
dest_end > 0,
"[validity] the destination position should not overrun"
);
src_end -= 1;
dest_end -= 1;
let byte = buf[src_end];
if byte.is_ascii() {
buf[dest_end] = byte;
// Use the ASCII character directly.
} else {
// Percent-encode the byte.
dest_end -= 2;
buf[dest_end] = b'%';
let upper = byte >> 4;
let lower = byte & 0b1111;
buf[dest_end + 1] = HEXDIGITS[usize::from(upper)];
buf[dest_end + 2] = HEXDIGITS[usize::from(lower)];
rest_nonascii -= 1;
}
}
// Move the result from the temporary buffer to the destination.
let s = String::from_utf8(buf).expect("[consistency] the encoding result is an ASCII string");
*iri = s;
Ok(())
}
/// Returns the number of non-ASCII characters.
#[cfg(feature = "alloc")]
#[inline]
#[must_use]
fn count_nonascii(s: &str) -> usize {
s.bytes().filter(|b| !b.is_ascii()).count()
}