MCPcopy Create free account
hub / github.com/apache/arrow-rs / b64_encode

Function b64_encode

arrow-cast/src/base64.rs:31–56  ·  view source on GitHub ↗

Bas64 encode each element of `array` with the provided [`Engine`]

(
    engine: &E,
    array: &GenericBinaryArray<O>,
)

Source from the content-addressed store, hash-verified

29
30/// Bas64 encode each element of `array` with the provided [`Engine`]
31pub fn b64_encode<E: Engine, O: OffsetSizeTrait>(
32 engine: &E,
33 array: &GenericBinaryArray<O>,
34) -> GenericStringArray<O> {
35 let lengths = array.offsets().windows(2).map(|w| {
36 let len = w[1].as_usize() - w[0].as_usize();
37 encoded_len(len, engine.config().encode_padding()).unwrap()
38 });
39 let offsets = OffsetBuffer::<O>::from_lengths(lengths);
40 let buffer_len = offsets.last().unwrap().as_usize();
41 let mut buffer = vec![0_u8; buffer_len];
42 let mut offset = 0;
43
44 for i in 0..array.len() {
45 let len = engine
46 .encode_slice(array.value(i), &mut buffer[offset..])
47 .unwrap();
48 offset += len;
49 }
50 assert_eq!(offset, buffer_len);
51
52 // Safety: Base64 is valid UTF-8
53 unsafe {
54 GenericStringArray::new_unchecked(offsets, Buffer::from_vec(buffer), array.nulls().cloned())
55 }
56}
57
58/// Base64 decode each element of `array` with the provided [`Engine`]
59pub fn b64_decode<E: Engine, O: OffsetSizeTrait>(

Callers 1

test_engineFunction · 0.85

Calls 8

encoded_lenFunction · 0.85
from_lengthsFunction · 0.85
as_usizeMethod · 0.80
offsetsMethod · 0.45
lastMethod · 0.45
lenMethod · 0.45
valueMethod · 0.45
nullsMethod · 0.45

Tested by 1

test_engineFunction · 0.68