parquet/util/utf8.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! [`check_valid_utf8`] validation function
6use crate::errors::{ParquetError, Result};
7
8/// Check that `val` is a valid UTF-8 sequence.
9///
10/// If the `simdutf8` feature is enabled, this function will use
11/// SIMD-accelerated validation from the [`simdutf8`] crate. Otherwise, it will use
12/// [`std::str::from_utf8`].
13///
14/// # Errors
15///
16/// Returns `Err::General` with a message compatible with [`std::str::from_utf8`] on failure.
17///
18/// # Example
19/// ```
20/// use parquet::utf8::check_valid_utf8;
21/// assert!(check_valid_utf8(b"hello").is_ok());
22/// assert!(check_valid_utf8(b"hello \xF0\x9F\x98\x8E").is_ok());
23/// // invalid UTF-8
24/// assert!(check_valid_utf8(b"hello \xF0\x9F\x98").is_err());
25/// ```
26///
27/// [`simdutf8`]: https://crates.io/crates/simdutf8
28#[inline(always)]
29pub fn check_valid_utf8(val: &[u8]) -> Result<()> {
30 #[cfg(feature = "simdutf8")]
31 match simdutf8::basic::from_utf8(val) {
32 Ok(_) => Ok(()),
33 Err(_) => {
34 // Use simdutf8::compat to return details about the decoding error
35 let e = simdutf8::compat::from_utf8(val).unwrap_err();
36 Err(general_err!("encountered non UTF-8 data: {}", e))
37 }
38 }
39 #[cfg(not(feature = "simdutf8"))]
40 match std::str::from_utf8(val) {
41 Ok(_) => Ok(()),
42 Err(e) => Err(general_err!("encountered non UTF-8 data: {}", e)),
43 }
44}
45
46/// Check that `value` does not start in the middle of a UTF-8 character.
47///
48/// [`check_valid_utf8`] over values stored back to back misses a character
49/// split across two of them, so each value's start must be checked as well.
50#[cfg(feature = "arrow")]
51#[inline(always)]
52pub(crate) fn check_utf8_value_start(value: &[u8]) -> Result<()> {
53 match value.first() {
54 // A continuation byte has the form 0b10xxxxxx, bit majic from `str::is_char_boundary`
55 // see https://github.com/rust-lang/rust/blob/1.100.0/library/core/src/num/mod.rs#L1232-L1235
56 Some(&b) if (b as i8) < -0x40 => Err(general_err!("encountered non UTF-8 data")),
57 _ => Ok(()),
58 }
59}