Snap for 10453563 from 47b2dc9b133f7f6fd9727e8a11cd41deef5fe360 to mainline-art-release

Change-Id: I5d75f540dc573ca59b1fd3ba94b17544d58b3f0c
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 8dc2ce5..3b16060 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,6 @@
 {
   "git": {
-    "sha1": "2811a1ad6f7c8bead2ef3671e4fdc10de1553e96"
-  }
-}
+    "sha1": "69fb82bd9c2da8b8977809e4d5d0c03109519831"
+  },
+  "path_in_vcs": ""
+}
\ No newline at end of file
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 6ae8d0c..4397394 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -14,6 +14,7 @@
       run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
     - run: cargo build --all
     - run: cargo test --all
+    - run: cargo build --features std
 
   fuzz_targets:
     name: Fuzz Targets
@@ -23,7 +24,7 @@
     # Note that building with fuzzers requires nightly since it uses unstable
     # flags to rustc.
     - run: rustup update nightly && rustup default nightly
-    - run: cargo install cargo-fuzz --vers "^0.10"
+    - run: cargo install cargo-fuzz --vers "^0.11"
     - run: cargo fuzz build --dev
 
   rustfmt:
diff --git a/Android.bp b/Android.bp
index d98d15c..d7efeea 100644
--- a/Android.bp
+++ b/Android.bp
@@ -44,7 +44,7 @@
     host_supported: true,
     crate_name: "rustc_demangle",
     cargo_env_compat: true,
-    cargo_pkg_version: "0.1.21",
+    cargo_pkg_version: "0.1.22",
     srcs: ["src/lib.rs"],
     edition: "2015",
     apex_available: [
@@ -67,7 +67,7 @@
     host_supported: true,
     crate_name: "rustc_demangle",
     cargo_env_compat: true,
-    cargo_pkg_version: "0.1.21",
+    cargo_pkg_version: "0.1.22",
     srcs: ["src/lib.rs"],
     test_suites: ["general-tests"],
     auto_gen_config: true,
diff --git a/Cargo.toml b/Cargo.toml
index 2253b63..60ef533 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,25 +3,35 @@
 # When uploading crates to the registry Cargo will automatically
 # "normalize" Cargo.toml files for maximal compatibility
 # with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies
+# to registry (e.g., crates.io) dependencies.
 #
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
 
 [package]
 name = "rustc-demangle"
-version = "0.1.21"
+version = "0.1.22"
 authors = ["Alex Crichton <alex@alexcrichton.com>"]
-description = "Rust compiler symbol demangling.\n"
+description = """
+Rust compiler symbol demangling.
+"""
 homepage = "https://github.com/alexcrichton/rustc-demangle"
 documentation = "https://docs.rs/rustc-demangle"
 readme = "README.md"
 license = "MIT/Apache-2.0"
 repository = "https://github.com/alexcrichton/rustc-demangle"
+
+[package.metadata.docs.rs]
+features = ["std"]
+rustdoc-args = [
+    "--cfg",
+    "docsrs",
+]
+
 [profile.release]
 lto = true
+
 [dependencies.compiler_builtins]
 version = "0.1.2"
 optional = true
@@ -32,4 +42,8 @@
 package = "rustc-std-workspace-core"
 
 [features]
-rustc-dep-of-std = ["core", "compiler_builtins"]
+rustc-dep-of-std = [
+    "core",
+    "compiler_builtins",
+]
+std = []
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 552e069..1deb42f 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "rustc-demangle"
-version = "0.1.21"
+version = "0.1.22"
 authors = ["Alex Crichton <alex@alexcrichton.com>"]
 license = "MIT/Apache-2.0"
 readme = "README.md"
@@ -20,6 +20,11 @@
 
 [features]
 rustc-dep-of-std = ['core', 'compiler_builtins']
+std = []
 
 [profile.release]
 lto = true
+
+[package.metadata.docs.rs]
+features = ["std"]
+rustdoc-args = ["--cfg", "docsrs"]
diff --git a/METADATA b/METADATA
index 0018b05..bc073d8 100644
--- a/METADATA
+++ b/METADATA
@@ -1,3 +1,7 @@
+# This project was upgraded with external_updater.
+# Usage: tools/external_updater/updater.sh update rust/crates/rustc-demangle
+# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md
+
 name: "rustc-demangle"
 description: "Rust compiler symbol demangling."
 third_party {
@@ -7,13 +11,13 @@
   }
   url {
     type: ARCHIVE
-    value: "https://static.crates.io/crates/rustc-demangle/rustc-demangle-0.1.21.crate"
+    value: "https://static.crates.io/crates/rustc-demangle/rustc-demangle-0.1.22.crate"
   }
-  version: "0.1.21"
+  version: "0.1.22"
   license_type: NOTICE
   last_upgrade_date {
-    year: 2021
-    month: 9
-    day: 22
+    year: 2023
+    month: 4
+    day: 3
   }
 }
diff --git a/src/lib.rs b/src/lib.rs
index 1ecb13f..7eb1c42 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -25,8 +25,9 @@
 
 #![no_std]
 #![deny(missing_docs)]
+#![cfg_attr(docsrs, feature(doc_cfg))]
 
-#[cfg(test)]
+#[cfg(any(test, feature = "std"))]
 #[macro_use]
 extern crate std;
 
@@ -144,6 +145,74 @@
     }
 }
 
+#[cfg(feature = "std")]
+fn demangle_line(line: &str, include_hash: bool) -> std::borrow::Cow<str> {
+    let mut line = std::borrow::Cow::Borrowed(line);
+    let mut head = 0;
+    loop {
+        // Move to the next potential match
+        head = match (line[head..].find("_ZN"), line[head..].find("_R")) {
+            (Some(idx), None) | (None, Some(idx)) => head + idx,
+            (Some(idx1), Some(idx2)) => head + idx1.min(idx2),
+            (None, None) => {
+                // No more matches, we can return our line.
+                return line;
+            }
+        };
+        // Find the non-matching character.
+        //
+        // If we do not find a character, then until the end of the line is the
+        // thing to demangle.
+        let match_end = line[head..]
+            .find(|ch: char| !(ch == '$' || ch == '.' || ch == '_' || ch.is_ascii_alphanumeric()))
+            .map(|idx| head + idx)
+            .unwrap_or(line.len());
+
+        let mangled = &line[head..match_end];
+        if let Ok(demangled) = try_demangle(mangled) {
+            let demangled = if include_hash {
+                format!("{}", demangled)
+            } else {
+                format!("{:#}", demangled)
+            };
+            line.to_mut().replace_range(head..match_end, &demangled);
+            // Start again after the replacement.
+            head = head + demangled.len();
+        } else {
+            // Skip over the full symbol. We don't try to find a partial Rust symbol in the wider
+            // matched text today.
+            head = head + mangled.len();
+        }
+    }
+}
+
+/// Process a stream of data from `input` into the provided `output`, demangling any symbols found
+/// within.
+///
+/// This currently is implemented by buffering each line of input in memory, but that may be
+/// changed in the future. Symbols never cross line boundaries so this is just an implementation
+/// detail.
+#[cfg(feature = "std")]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub fn demangle_stream<R: std::io::BufRead, W: std::io::Write>(
+    input: &mut R,
+    output: &mut W,
+    include_hash: bool,
+) -> std::io::Result<()> {
+    let mut buf = std::string::String::new();
+    // We read in lines to reduce the memory usage at any time.
+    //
+    // demangle_line is also more efficient with relatively small buffers as it will copy around
+    // trailing data during demangling. In the future we might directly stream to the output but at
+    // least right now that seems to be less efficient.
+    while input.read_line(&mut buf)? > 0 {
+        let demangled_line = demangle_line(&buf, include_hash);
+        output.write_all(demangled_line.as_bytes())?;
+        buf.clear();
+    }
+    Ok(())
+}
+
 /// Error returned from the `try_demangle` function below when demangling fails.
 #[derive(Debug, Clone)]
 pub struct TryDemangleError {
@@ -490,4 +559,22 @@
             "{size limit reached}"
         );
     }
+
+    #[test]
+    #[cfg(feature = "std")]
+    fn find_multiple() {
+        assert_eq!(
+            super::demangle_line("_ZN3fooE.llvm moocow _ZN3fooE.llvm", false),
+            "foo.llvm moocow foo.llvm"
+        );
+    }
+
+    #[test]
+    #[cfg(feature = "std")]
+    fn interleaved_new_legacy() {
+        assert_eq!(
+            super::demangle_line("_ZN3fooE.llvm moocow _RNvMNtNtNtNtCs8a2262Dv4r_3mio3sys4unix8selector5epollNtB2_8Selector6select _ZN3fooE.llvm", false),
+            "foo.llvm moocow <mio::sys::unix::selector::epoll::Selector>::select foo.llvm"
+        );
+    }
 }