Debian Codegraph @

src/compat.rs

raw symbols SCIP
1 //! The `compat` API flavor provides full compatibility with [`std::str::from_utf8()`] and detailed validation errors.
2 //!
3 //! In particular, [`from_utf8()`]
4 //! returns an [`Utf8Error`], which has the [`valid_up_to()`](Utf8Error#method.valid_up_to) and
5 //! [`error_len()`](Utf8Error#method.error_len) methods. The first is useful for verification of streamed data. The
6 //! second is useful e.g. for replacing invalid byte sequences with a replacement character.
7 //!
8 //! The functions in this module also fail early: errors are checked on-the-fly as the string is processed and once
9 //! an invalid UTF-8 sequence is encountered, it returns without processing the rest of the data.
10 //! This comes at a slight performance penalty compared to the [`crate::basic`] module if the input is valid UTF-8.
11
12 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#;
13 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#;
14
15 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::{from_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked()., from_utf8_unchecked_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked_mut().};
16
17 use craterust-analyzer cargo simdutf8 0.1.5 crate/::implementationrust-analyzer cargo simdutf8 0.1.5 implementation/::validate_utf8_compatrust-analyzer cargo simdutf8 0.1.5 implementation/validate_utf8_compat().;
18
19 /// UTF-8 error information compatible with [`std::str::Utf8Error`].
20 ///
21 /// Contains information on the location of the encountered validation error and the length of the
22 /// invalid UTF-8 sequence.
23 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
24 pub struct Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error# {
25 pub(craterust-analyzer cargo simdutf8 0.1.5 crate/) valid_up_torust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#valid_up_to.: usize,
26 pub(craterust-analyzer cargo simdutf8 0.1.5 crate/) error_lenrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#error_len.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>,
27 }
28
29 impl Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error# {
30 /// Analogue to [`std::str::Utf8Error::valid_up_to()`](std::str::Utf8Error#method.valid_up_to).
31 ///
32 /// ...
33 #[inline]
34 #[must_use]
35 pub fn valid_up_torust-analyzer cargo simdutf8 0.1.5 compat/impl#[Utf8Error]valid_up_to().(&self) -> usize {
36 self.valid_up_torust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#valid_up_to.
37 }
38
39 /// Analogue to [`std::str::Utf8Error::error_len()`](std::str::Utf8Error#method.error_len).
40 ///
41 /// ...
42 #[inline]
43 #[must_use]
44 pub fn error_lenrust-analyzer cargo simdutf8 0.1.5 compat/impl#[Utf8Error]error_len().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
45 self.error_lenrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#error_len..maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|len| len as usize)
46 }
47 }
48
49 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error# {
50 fn fmtrust-analyzer cargo simdutf8 0.1.5 compat/impl#[Utf8Error][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
51 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(error_len) = self.error_lenrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#error_len. {
52 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
53 f,
54 "invalid utf-8 sequence of {} bytes from index {}",
55 error_len, self.valid_up_torust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#valid_up_to.
56 )
57 } else {
58 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
59 f,
60 "incomplete utf-8 byte sequence from index {}",
61 self.valid_up_torust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#valid_up_to.
62 )
63 }
64 }
65 }
66
67 #[cfg(feature = "std")]
68 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error# {}
69
70 /// Analogue to [`std::str::from_utf8()`].
71 ///
72 /// Checks if the passed byte sequence is valid UTF-8 and returns an
73 /// [`std::str`] reference to the passed byte slice wrapped in `Ok()` if it is.
74 ///
75 /// # Errors
76 /// Will return Err([`Utf8Error`]) on if the input contains invalid UTF-8 with
77 /// detailed error information.
78 #[inline]
79 pub fn from_utf8rust-analyzer cargo simdutf8 0.1.5 compat/from_utf8().(input: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#> {
80 unsafe {
81 validate_utf8_compatrust-analyzer cargo simdutf8 0.1.5 implementation/validate_utf8_compat().(input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
82 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(from_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(input))
83 }
84 }
85
86 /// Analogue to [`std::str::from_utf8_mut()`].
87 ///
88 /// Checks if the passed mutable byte sequence is valid UTF-8 and returns a mutable
89 /// [`std::str`] reference to the passed byte slice wrapped in `Ok()` if it is.
90 ///
91 /// # Errors
92 /// Will return Err([`Utf8Error`]) on if the input contains invalid UTF-8 with
93 /// detailed error information.
94 #[inline]
95 pub fn from_utf8_mutrust-analyzer cargo simdutf8 0.1.5 compat/from_utf8_mut().(input: &mut [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut str, Utf8Errorrust-analyzer cargo simdutf8 0.1.5 compat/Utf8Error#> {
96 unsafe {
97 validate_utf8_compatrust-analyzer cargo simdutf8 0.1.5 implementation/validate_utf8_compat().(input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(from_utf8_unchecked_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked_mut().(input))
99 }
100 }
101
102 /// Allows direct access to the platform-specific unsafe validation implementations.
103 #[cfg(feature = "public_imp")]
104 pub mod imprust-analyzer cargo simdutf8 0.1.5 compat/imp/ {
105 /// Includes the x86/x86-64 SIMD implementations.
106 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
107 pub mod x86rust-analyzer cargo simdutf8 0.1.5 compat/imp/x86/ {
108 /// Includes the validation implementation for AVX 2-compatible CPUs.
109 pub mod avx2rust-analyzer cargo simdutf8 0.1.5 compat/imp/x86/avx2/ {
110 pub use craterust-analyzer cargo simdutf8 0.1.5 crate/::implementationrust-analyzer cargo simdutf8 0.1.5 implementation/::x86rust-analyzer cargo simdutf8 0.1.5 implementation/x86/::avx2rust-analyzer cargo simdutf8 0.1.5 implementation/x86/avx2/::validate_utf8_compatrust-analyzer cargo simdutf8 0.1.5 implementation/x86/avx2/validate_utf8_compat(). as validate_utf8rust-analyzer cargo simdutf8 0.1.5 implementation/x86/avx2/validate_utf8_compat().;
111 }
112 /// Includes the validation implementation for SSE 4.2-compatible CPUs.
113 pub mod sse42rust-analyzer cargo simdutf8 0.1.5 compat/imp/x86/sse42/ {
114 pub use craterust-analyzer cargo simdutf8 0.1.5 crate/::implementationrust-analyzer cargo simdutf8 0.1.5 implementation/::x86rust-analyzer cargo simdutf8 0.1.5 implementation/x86/::sse42rust-analyzer cargo simdutf8 0.1.5 implementation/x86/sse42/::validate_utf8_compatrust-analyzer cargo simdutf8 0.1.5 implementation/x86/sse42/validate_utf8_compat(). as validate_utf8rust-analyzer cargo simdutf8 0.1.5 implementation/x86/sse42/validate_utf8_compat().;
115 }
116 }
117
118 /// Includes the aarch64 SIMD implementations.
119 #[cfg(all(feature = "aarch64_neon", target_arch = "aarch64"))]
120 pub mod aarch64 {
121 /// Includes the validation implementation for Neon SIMD.
122 pub mod neon {
123 pub use craterust-analyzer cargo simdutf8 0.1.5 crate/::implementationrust-analyzer cargo simdutf8 0.1.5 implementation/::aarch64::neon::validate_utf8_compat as validate_utf8;
124 }
125 }
126
127 /// Includes the wasm32 SIMD implementations.
128 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
129 pub mod wasm32 {
130 /// Includes the validation implementation for WASM simd128.
131 pub mod simd128 {
132 pub use craterust-analyzer cargo simdutf8 0.1.5 crate/::implementationrust-analyzer cargo simdutf8 0.1.5 implementation/::wasm32::simd128::validate_utf8_compat as validate_utf8;
133 }
134 }
135 }