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
use crate::alloc::string::String;
#[inline]
pub fn encode<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}
#[inline]
pub fn encode_and_store_to_slice<T: ?Sized + AsRef<[u8]>>(input: &T, output: &mut [u8]) -> usize {
base64::encode_config_slice(input, base64::URL_SAFE_NO_PAD, output)
}
#[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_and_push_to_string_mut(input, &mut output);
output
}
pub fn encode_and_push_to_string_mut<T: ?Sized + AsRef<[u8]>>(input: &T, output: &mut String) {
let bytes = input.as_ref();
let buffer = unsafe { output.as_mut_vec() };
let current_length = buffer.len();
let base64_length = ((bytes.len() << 2) + 2) / 3;
let min_capacity = current_length + base64_length;
let capacity = buffer.capacity();
if capacity < min_capacity {
buffer.reserve(min_capacity - capacity);
}
unsafe {
buffer.set_len(min_capacity);
}
let base64_len = encode_and_store_to_slice(bytes, &mut buffer[current_length..min_capacity]);
unsafe {
buffer.set_len(current_length + base64_len);
}
}