Debian Codegraph @

src/shared/util/utf8.rs

raw symbols SCIP
1 // auto-generated by: jiff-cli generate shared
2
3 /// Decodes the next UTF-8 encoded codepoint from the given byte slice.
4 ///
5 /// If no valid encoding of a codepoint exists at the beginning of the
6 /// given byte slice, then a 1-3 byte slice is returned (which is guaranteed
7 /// to be a prefix of `bytes`). That byte slice corresponds either to a single
8 /// invalid byte, or to a prefix of a valid UTF-8 encoding of a Unicode scalar
9 /// value (but which ultimately did not lead to a valid encoding).
10 ///
11 /// This returns `None` if and only if `bytes` is empty.
12 ///
13 /// This never panics.
14 ///
15 /// *WARNING*: This is not designed for performance. If you're looking for
16 /// a fast UTF-8 decoder, this is not it. If you feel like you need one in
17 /// this crate, then please file an issue and discuss your use case.
18 pub(craterust-analyzer cargo jiff-static 0.2.16 crate/) fn decoderust-analyzer cargo jiff-static 0.2.16 shared/util/utf8/decode().(bytes: &[u8]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<char, &[u8]>> {
19 if bytes.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
20 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
21 }
22 let string = match 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_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(4)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) {
23 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) => s,
24 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref err) if err.valid_up_torust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]valid_up_to().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 => {
25 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_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#err.valid_up_torust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]valid_up_to().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
26 }
27 // In this case, we want to return 1-3 bytes that make up a prefix of
28 // a potentially valid codepoint.
29 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
30 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(
31 &bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#err.error_lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]error_len().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
32 ))
33 }
34 };
35 // OK because we guaranteed above that `string`
36 // must be non-empty. And thus, `str::chars` must
37 // yield at least one Unicode scalar value.
38 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(string.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()))
39 }