Skip to content

Commit 028333c

Browse files
committed
add byte_buf
1 parent 7e91bc8 commit 028333c

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

datafusion/functions/src/unicode/reverse.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,17 @@ fn reverse_impl<'a, T: OffsetSizeTrait, V: StringArrayType<'a>>(
120120
let mut builder = GenericStringBuilder::<T>::with_capacity(string_array.len(), 1024);
121121

122122
let mut string_buf = String::new();
123+
let mut byte_buf = Vec::<u8>::new();
123124
for string in string_array.iter() {
124125
if let Some(s) = string {
125126
if s.is_ascii() {
127+
// reverse bytes directly since ASCII characters are single bytes
128+
byte_buf.extend(s.as_bytes());
129+
byte_buf.reverse();
126130
// SAFETY: Since the original string was ASCII, reversing the bytes still results in valid UTF-8.
127-
let reversed = unsafe {
128-
// reverse bytes directly since ASCII characters are single bytes
129-
String::from_utf8_unchecked(s.bytes().rev().collect::<Vec<u8>>())
130-
};
131+
let reversed = unsafe { std::str::from_utf8_unchecked(&byte_buf) };
131132
builder.append_value(&reversed);
133+
byte_buf.clear();
132134
} else {
133135
string_buf.extend(s.chars().rev());
134136
builder.append_value(&string_buf);

0 commit comments

Comments
 (0)