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
use core::str::from_utf8_unchecked;
use crate::alloc::string::String;
use crate::alloc::vec::Vec;
#[inline]
pub fn encode<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}
#[inline]
pub fn encode_to_string<'a, T: ?Sized + AsRef<[u8]>>(input: &T, output: &'a mut String) -> &'a str {
let base64_url = encode_to_vec(input, unsafe { output.as_mut_vec() });
unsafe { from_utf8_unchecked(base64_url) }
}
#[inline]
pub fn encode_to_vec<'a, T: ?Sized + AsRef<[u8]>>(input: &T, output: &'a mut Vec<u8>) -> &'a [u8] {
let bytes = input.as_ref();
let current_length = output.len();
let base64_length = ((bytes.len() << 2) + 2) / 3;
output.reserve(base64_length);
unsafe {
output.set_len(current_length + base64_length);
}
let base64_length = encode_to_slice(bytes, &mut output[current_length..]).len();
unsafe {
output.set_len(current_length + base64_length);
}
&output[current_length..]
}
#[inline]
pub fn encode_to_slice<'a, T: ?Sized + AsRef<[u8]>>(input: &T, output: &'a mut [u8]) -> &'a [u8] {
let length = base64::encode_config_slice(input, base64::URL_SAFE_NO_PAD, output);
&output[..length]
}
#[deprecated(since = "1.4.0", note = "Please use the `encode_to_slice` function instead")]
#[inline]
pub fn encode_and_store_to_slice<T: ?Sized + AsRef<[u8]>>(input: &T, output: &mut [u8]) -> usize {
encode_to_slice(input, output).len()
}
#[deprecated(since = "1.4.0", note = "Please use the `encode_to_string` function instead")]
#[inline]
pub fn encode_and_push_to_string<T: ?Sized + AsRef<[u8]>, S: Into<String>>(
input: &T,
output: S,
) -> String {
let mut output = output.into();
encode_to_string(input, &mut output);
output
}
#[deprecated(since = "1.4.0", note = "Please use the `encode_to_string` function instead")]
pub fn encode_and_push_to_string_mut<T: ?Sized + AsRef<[u8]>>(input: &T, output: &mut String) {
encode_to_string(input, output);
}