tustvold commented on code in PR #4975:
URL: https://github.com/apache/arrow-rs/pull/4975#discussion_r1368606520


##########
arrow-cast/src/base64.rs:
##########
@@ -0,0 +1,117 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Functions for Base64 encoding/decoding
+
+use arrow_array::{Array, GenericBinaryArray, GenericStringArray, 
OffsetSizeTrait};
+use arrow_buffer::OffsetBuffer;
+use arrow_schema::ArrowError;
+use base64::encoded_len;
+use base64::engine::Config;
+
+pub use base64::prelude::*;
+
+/// Bas64 encode each element of `array` with the provided `engine`
+pub fn b64_encode<E: Engine, O: OffsetSizeTrait>(
+    engine: &E,
+    array: &GenericBinaryArray<O>,
+) -> GenericStringArray<O> {
+    let lengths = array.offsets().windows(2).map(|w| {
+        let len = w[1].as_usize() - w[0].as_usize();
+        encoded_len(len, engine.config().encode_padding()).unwrap()
+    });
+    let offsets = OffsetBuffer::<O>::from_lengths(lengths);
+    let buffer_len = offsets.last().unwrap().as_usize();
+    let mut buffer = vec![0_u8; buffer_len];
+    let mut offset = 0;
+
+    for i in 0..array.len() {
+        let len = engine
+            .encode_slice(array.value(i), &mut buffer[offset..])
+            .unwrap();
+        offset += len;
+    }
+    assert_eq!(offset, buffer_len);
+
+    // Safety: Base64 is valid UTF-8
+    unsafe { GenericStringArray::new_unchecked(offsets, buffer.into(), 
array.nulls().cloned()) }
+}
+
+/// Base64 decode each element of `array` with the provided `engine`
+pub fn b64_decode<E: Engine, O: OffsetSizeTrait>(
+    engine: &E,
+    array: &GenericBinaryArray<O>,

Review Comment:
   It would perhaps be more symmetrical to take a `GenericStringArray` but it 
seems unnecessary to mandate UTF-8 data, when the decoding process effectively 
verifies it anyway



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to