tustvold commented on code in PR #4389:
URL: https://github.com/apache/arrow-rs/pull/4389#discussion_r1225789070
##########
parquet/src/column/writer/mod.rs:
##########
@@ -1152,9 +1213,78 @@ fn compare_greater_byte_array_decimals(a: &[u8], b:
&[u8]) -> bool {
(a[1..]) > (b[1..])
}
+/// Truncate a UTF8 slice to the longest prefix that is still a valid UTF8
string, while being less than `length` bytes.
+fn truncate_utf8(data: &str, length: usize) -> Option<Vec<u8>> {
+ // We return values like that at an earlier stage in the process.
+ assert!(data.len() >= length);
+ let mut char_indices = data.char_indices();
+
+ // We know `data` is a valid UTF8 encoded string, which means it has at
least one valid UTF8 byte, which will make this loop exist.
+ while let Some((idx, c)) = char_indices.next_back() {
+ let split_point = idx + c.len_utf8();
+ if split_point <= length {
+ return data.as_bytes()[0..split_point].to_vec().into();
+ }
+ }
+
+ unreachable!()
+}
+
+/// Truncate a binary slice to make sure its length is less than `length`
+fn truncate_binary(data: &[u8], length: usize) -> Option<Vec<u8>> {
+ // We return values like that at an earlier stage in the process.
+ assert!(data.len() >= length);
+ // If all bytes are already maximal, no need to truncate
+ if data.iter().all(|b| *b == u8::MAX) {
+ None
+ } else {
+ data[0..length].to_vec().into()
+ }
+}
+
+/// Try and increment the bytes from right to left.
+///
+/// Returns `None` if all bytes are set to `u8::MAX`.
+fn increment(mut data: Vec<u8>) -> Option<Vec<u8>> {
+ for byte in data.iter_mut().rev() {
+ *byte = byte.checked_add(1).unwrap_or(0);
+
+ if *byte != 0 {
+ return Some(data);
+ }
Review Comment:
```suggestion
let (incremented, overflow) = byte.overflowing_add(1);
*byte = incremented;
if !overflow {
return Some(data);
}
```
--
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]