Merge mozilla-central to mozilla-inbound. a=merge CLOSED TREE
authorCiure Andrei <aciure@mozilla.com>
Wed, 14 Aug 2019 19:37:05 +0300
changeset 488059 99d4d3837b3ba32d26ece9fe77e337b968199283
parent 488058 b18e834a1dafe9e1b8e36585239027030e3d07e3 (current diff)
parent 487851 62094e9b468a0903e56b7af177d7590d616a8d5b (diff)
child 488060 d513683e1fdbfdb79f5112e9745f2fc0fb4e92fe
push id36435
push usercbrindusan@mozilla.com
push dateThu, 15 Aug 2019 09:46:49 +0000
treeherdermozilla-central@0db07ff50ab5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmerge
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Merge mozilla-central to mozilla-inbound. a=merge CLOSED TREE
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1004,16 +1004,24 @@ source = "registry+https://github.com/ru
 name = "encoding_c"
 version = "0.9.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
+name = "encoding_c_mem"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "encoding_glue"
 version = "0.1.0"
 dependencies = [
  "encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
  "nserror 0.1.0",
  "nsstring 0.1.0",
 ]
 
@@ -1255,16 +1263,17 @@ dependencies = [
  "bitsdownload 0.1.0",
  "bookmark_sync 0.1.0",
  "cert_storage 0.0.1",
  "cose-c 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "cubeb-coreaudio 0.1.0",
  "cubeb-pulse 0.2.0",
  "cubeb-sys 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "encoding_c 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding_c_mem 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "encoding_glue 0.1.0",
  "env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "geckoservo 0.0.1",
  "gkrust_utils 0.1.0",
  "jsrust_shared 0.1.0",
  "kvstore 0.1.0",
  "lmdb-rkv-sys 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3990,16 +3999,17 @@ dependencies = [
 "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c"
 "checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a"
 "checksum dogear 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "251a15c9a597d70eb53cbb0c5473d8d8c6241aef615c092030ebab27fb5b26ef"
 "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
 "checksum dtoa-short 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "068d4026697c1a18f0b0bb8cfcad1b0c151b90d8edb9bf4c235ad68128920d1d"
 "checksum dwrote 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0bd1369e02db5e9b842a9b67bce8a2fcc043beafb2ae8a799dd482d46ea1ff0d"
 "checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
 "checksum encoding_c 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "769ecb8b33323998e482b218c0d13cd64c267609023b4b7ec3ee740714c318ee"
+"checksum encoding_c_mem 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9516d857aa9e070e02890ed3906f678ffca7fac8d82bd53c3567d96337f256"
 "checksum encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "4155785c79f2f6701f185eb2e6b4caf0555ec03477cb4c70db67b465311620ed"
 "checksum enum-display-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "53f76eb63c4bfc6fce5000f106254701b741fc9a65ee08445fde0ff39e583f1c"
 "checksum env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0561146661ae44c579e993456bc76d11ce1e0c7d745e57b2fa7146b6e49fa2ad"
 "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
 "checksum euclid 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2c4786521fec183792e755bf32cd0188e4e7628c6d0fcfd51426435b9081a106"
 "checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7"
 "checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
 "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
--- a/accessible/html/HTMLElementAccessibles.cpp
+++ b/accessible/html/HTMLElementAccessibles.cpp
@@ -160,21 +160,21 @@ bool HTMLSummaryAccessible::IsWidget() c
 // HTMLHeaderOrFooterAccessible
 ////////////////////////////////////////////////////////////////////////////////
 
 role HTMLHeaderOrFooterAccessible::NativeRole() const {
   // Only map header and footer if they are direct descendants of the body tag.
   // If other sectioning or sectioning root elements, they become sections.
   nsIContent* parent = mContent->GetParent();
   while (parent) {
-    if (parent->IsAnyOfHTMLElements(nsGkAtoms::article, nsGkAtoms::aside,
-                                    nsGkAtoms::nav, nsGkAtoms::section,
-                                    nsGkAtoms::blockquote, nsGkAtoms::details,
-                                    nsGkAtoms::dialog, nsGkAtoms::fieldset,
-                                    nsGkAtoms::figure, nsGkAtoms::td)) {
+    if (parent->IsAnyOfHTMLElements(
+            nsGkAtoms::article, nsGkAtoms::aside, nsGkAtoms::nav,
+            nsGkAtoms::section, nsGkAtoms::main, nsGkAtoms::blockquote,
+            nsGkAtoms::details, nsGkAtoms::dialog, nsGkAtoms::fieldset,
+            nsGkAtoms::figure, nsGkAtoms::td)) {
       break;
     }
     parent = parent->GetParent();
   }
 
   // No sectioning or sectioning root elements found.
   if (!parent) {
     return roles::LANDMARK;
--- a/accessible/tests/mochitest/attributes/test_xml-roles.html
+++ b/accessible/tests/mochitest/attributes/test_xml-roles.html
@@ -16,19 +16,21 @@
 
   <script type="application/javascript">
 
     function doTest() {
       // Some AT may look for this
       testAttrs("nav", {"xml-roles": "navigation"}, true);
       testAttrs("header", {"xml-roles": "banner"}, true);
       testAbsentAttrs("article_header", {"xml-roles": "banner"});
+      testAbsentAttrs("main_header", {"xml-roles": "banner"});
       testAbsentAttrs("section_header", {"xml-roles": "banner"});
       testAttrs("footer", {"xml-roles": "contentinfo"}, true);
       testAbsentAttrs("article_footer", {"xml-roles": "contentinfo"});
+      testAbsentAttrs("main_footer", {"xml-roles": "contentinfo"});
       testAbsentAttrs("section_footer", {"xml-roles": "contentinfo"});
       testAttrs("aside", {"xml-roles": "complementary"}, true);
       testAbsentAttrs("section", {"xml-roles": "region"}, true);
       testAttrs("main", {"xml-roles": "main"}, true); // // ARIA override
       testAttrs("form", {"xml-roles": "form"}, true);
       testAttrs("feed", {"xml-roles": "feed"}, true);
       testAttrs("article", {"xml-roles": "article"}, true);
       testAttrs("main_element", {"xml-roles": "main"}, true);
@@ -152,16 +154,20 @@
 
   <nav id="nav">a nav</nav>
   <header id="header">a header</header>
   <footer id="footer">a footer</footer>
   <article id="article_with_header_and_footer">
     <header id="article_header">a header within an article</header>
     <footer id="article_footer">a footer within an article</footer>
   </article>
+  <main id="main_with_header_and_footer">
+    <header id="main_header">a header within a main</header>
+    <footer id="main_footer">a footer within a main</footer>
+  </main>
   <section id="section_with_header_and_footer">
     <header id="section_header">a header within an section</header>
     <footer id="section_footer">a footer within an section</footer>
   </section>
   <aside id="aside">by the way I am an aside</aside>
   <section id="section">a section</section>
   <article id="main" role="main">a main area</article>
   <article id="form" role="form">a form area</article>
--- a/accessible/tests/mochitest/elm/test_HTMLSpec.html
+++ b/accessible/tests/mochitest/elm/test_HTMLSpec.html
@@ -521,16 +521,17 @@
 
       obj = {
         role: ROLE_SECTION,
         absentAttributes: { "xml-roles": "contentinfo" },
         interfaces: [ nsIAccessibleText, nsIAccessibleHyperText ],
       };
       testElm("footer_in_article", obj);
       testElm("footer_in_aside", obj);
+      testElm("footer_in_main", obj);
       testElm("footer_in_nav", obj);
       testElm("footer_in_section", obj);
       testElm("footer_in_blockquote", obj);
       testElm("footer_in_details", obj);
       testElm("footer_in_dialog", obj);
       testElm("footer_in_fieldset", obj);
       testElm("footer_in_figure", obj);
       testElm("footer_in_td", obj);
@@ -623,16 +624,17 @@
 
       obj = {
         role: ROLE_SECTION,
         absentAttributes: { "xml-roles": "banner" },
         interfaces: [ nsIAccessibleText, nsIAccessibleHyperText ],
       };
       testElm("header_in_article", obj);
       testElm("header_in_aside", obj);
+      testElm("header_in_main", obj);
       testElm("header_in_nav", obj);
       testElm("header_in_section", obj);
       testElm("header_in_blockquote", obj);
       testElm("header_in_details", obj);
       testElm("header_in_dialog", obj);
       testElm("header_in_fieldset", obj);
       testElm("header_in_figure", obj);
       testElm("header_in_td", obj);
@@ -1549,16 +1551,19 @@
 
   <footer id="footer">Some copyright info</footer>
   <article>
     <footer id="footer_in_article">Some copyright info</footer>
   </article>
   <aside>
     <footer id="footer_in_aside">Some copyright info</footer>
   </aside>
+  <main>
+    <footer id="footer_in_main">Some copyright info</footer>
+  </main>
   <nav>
     <footer id="footer_in_nav">Some copyright info</footer>
   </nav>
   <section>
     <footer id="footer_in_section">Some copyright info</footer>
   </section>
   <blockquote>
     <footer id="footer_in_blockquote">Some copyright info</footer>
@@ -1595,16 +1600,19 @@
 
   <header id="header">A logo</header>
   <article>
     <header id="header_in_article">Not logo</header>
   </article>
   <aside>
     <header id="header_in_aside">Not logo</header>
   </aside>
+  <main>
+    <header id="header_in_main">Not logo</header>
+  </main>
   <nav>
     <header id="header_in_nav">Not logo</header>
   </nav>
   <section>
     <header id="header_in_section">Not logo</header>
   </section>
   <blockquote>
     <header id="header_in_blockquote">Not logo</header>
--- a/accessible/tests/mochitest/jsat/test_landmarks.html
+++ b/accessible/tests/mochitest/jsat/test_landmarks.html
@@ -55,16 +55,31 @@
         accOrElmOrID: "article_footer",
         expectedUtterance: [
           ["a footer within an article"],
           ["a footer within an article"]],
         expectedBraille: [
           ["a footer within an article"],
           ["a footer within an article"]],
       }, {
+        accOrElmOrID: "main_header",
+        expectedUtterance: [["a header within main content"],
+                            ["a header within main content"]],
+        expectedBraille: [
+          ["a header within main content"],
+          ["a header within main content"]],
+      }, {
+        accOrElmOrID: "main_footer",
+        expectedUtterance: [
+          ["a footer within main content"],
+          ["a footer within main content"]],
+        expectedBraille: [
+          ["a footer within main content"],
+          ["a footer within main content"]],
+      }, {
         accOrElmOrID: "section_header",
         expectedUtterance: [["a header within a section"],
                             ["a header within a section"]],
         expectedBraille: [
           ["a header within a section"],
           ["a header within a section"]],
       }, {
         accOrElmOrID: "section_footer",
@@ -154,16 +169,20 @@
     <pre id="test"></pre>
     <nav id="nav">a nav</nav>
     <header id="header">a header</header>
     <footer id="footer">a footer</footer>
     <article id="article_with_header_and_footer">
       <header id="article_header">a header within an article</header>
       <footer id="article_footer">a footer within an article</footer>
     </article>
+    <main id="main_with_header_and_footer">
+      <header id="main_header">a header within main content</header>
+      <footer id="main_footer">a footer within main content</footer>
+    </main>
     <section id="section_with_header_and_footer">
       <header id="section_header">a header within a section</header>
       <footer id="section_footer">a footer within a section</footer>
     </section>
     <aside id="aside">by the way I am an aside</aside>
     <article id="main" role="main">a main area</article>
     <main id="main_element">another main area</main>
     <ul style="list-style-type: none;">
--- a/build/build-clang/README
+++ b/build/build-clang/README
@@ -11,52 +11,46 @@ optional arguments:
   -c CONFIG, --config CONFIG
                         Clang configuration file
   --clean               Clean the build directory
 ```
 
 Pre-requisites
 --------------
 * Working build toolchain.
-* Subversion
+* git
 * CMake
 * Ninja
-* Python 2.7
+* Python 2.7 and 3
 
 Please use the latest available CMake for your platform to avoid surprises.
 
 Config file format
 ------------------
 
 build-clang.py accepts a JSON config format with the following fields:
 
-* llvm_revision: The LLVM SVN revision to build.
 * stages: Use 1, 2, or 3 to select different compiler stages.  The default is 3.
-* llvm_repo: SVN path to the LLVM repo.
-* clang_repo: SVN path to the Clang repo.
-* extra_repo: SVN path to the clang-tools-extra repo.
-* lld_repo: SVN path to the lld repo.
-* compiler_repo: SVN path to the compiler-rt repo.
-* libcxx_repo: SVN path to the libcxx repo.
-* libcxxabi_repo: SVN path to the libcxxabi repo.
 * python_path: Path to the Python 2.7 installation on the machine building clang.
 * gcc_dir: Path to the gcc toolchain installation, only required on Linux.
 * cc: Path to the bootsraping C Compiler.
 * cxx: Path to the bootsraping C++ Compiler.
 * as: Path to the assembler tool.
 * ar: Path to the library archiver tool.
 * ranlib: Path to the ranlib tool (optional).
 * libtool: Path to the libtool tool (optional).
 * ld: Path to the linker.
 * patches: Optional list of patches to apply.
 * build_type: The type of build to make.  Supported types: Release, Debug, RelWithDebInfo or MinSizeRel.
 * build_libcxx: Whether to build with libcxx.  The default is false.
 * build_clang_tidy: Whether to build clang-tidy with the Mozilla checks imported.  The default is false.
 * osx_cross_compile: Whether to invoke CMake for OS X cross compile builds.
 * assertions: Whether to enable LLVM assertions.  The default is false.
 
+The revisions are defined in taskcluster/ci/fetch/toolchains.yml. They are usually commit sha1s corresponding to upstream tags.
+
 Environment Variables
 ---------------------
 
 The following environment variables are used for cross-compile builds targeting OS X on Linux.
 
 * CROSS_CCTOOLS_PATH: Path to the cctools directory where the cross compiler toolchain is located.
 * CROSS_SYSROOT: Path to the OS X SDK directory for cross compile builds.
--- a/intl/encoding_glue/src/lib.rs
+++ b/intl/encoding_glue/src/lib.rs
@@ -500,132 +500,8 @@ pub fn encode_from_nscstring(encoding: &
 #[inline(always)]
 fn checked_add(num: usize, opt: Option<usize>) -> Option<usize> {
     if let Some(n) = opt {
         n.checked_add(num)
     } else {
         None
     }
 }
-
-// Bindings for encoding_rs::mem. These may move to a separate crate in the future.
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_utf16_bidi(buffer: *const u16, len: usize) -> bool {
-    encoding_rs::mem::is_utf16_bidi(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_ascii(buffer: *const u8, len: usize) -> bool {
-    encoding_rs::mem::is_ascii(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_basic_latin(buffer: *const u16, len: usize) -> bool {
-    encoding_rs::mem::is_basic_latin(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_utf8_latin1(buffer: *const u8, len: usize) -> bool {
-    encoding_rs::mem::is_utf8_latin1(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_utf16_latin1(buffer: *const u16, len: usize) -> bool {
-    encoding_rs::mem::is_utf16_latin1(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_is_str_latin1(buffer: *const u8, len: usize) -> bool {
-    encoding_rs::mem::is_str_latin1(::std::str::from_utf8_unchecked(
-        ::std::slice::from_raw_parts(buffer, len),
-    ))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_utf16_valid_up_to(buffer: *const u16, len: usize) -> usize {
-    encoding_rs::mem::utf16_valid_up_to(::std::slice::from_raw_parts(buffer, len))
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_ensure_utf16_validity(buffer: *mut u16, len: usize) {
-    encoding_rs::mem::ensure_utf16_validity(::std::slice::from_raw_parts_mut(buffer, len));
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_utf16_to_latin1_lossy(
-    src: *const u16,
-    src_len: usize,
-    dst: *mut u8,
-    dst_len: usize,
-) {
-    encoding_rs::mem::convert_utf16_to_latin1_lossy(
-        ::std::slice::from_raw_parts(src, src_len),
-        ::std::slice::from_raw_parts_mut(dst, dst_len),
-    );
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_utf8_to_latin1_lossy(
-    src: *const u8,
-    src_len: usize,
-    dst: *mut u8,
-    dst_len: usize,
-) -> usize {
-    encoding_rs::mem::convert_utf8_to_latin1_lossy(
-        ::std::slice::from_raw_parts(src, src_len),
-        ::std::slice::from_raw_parts_mut(dst, dst_len),
-    )
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_latin1_to_utf16(
-    src: *const u8,
-    src_len: usize,
-    dst: *mut u16,
-    dst_len: usize,
-) {
-    encoding_rs::mem::convert_latin1_to_utf16(
-        ::std::slice::from_raw_parts(src, src_len),
-        ::std::slice::from_raw_parts_mut(dst, dst_len),
-    );
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_utf16_to_utf8(
-    src: *const u16,
-    src_len: usize,
-    dst: *mut u8,
-    dst_len: usize,
-) -> usize {
-    encoding_rs::mem::convert_utf16_to_utf8(
-        ::std::slice::from_raw_parts(src, src_len),
-        ::std::slice::from_raw_parts_mut(dst, dst_len),
-    )
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_utf16_to_utf8_partial(
-    src: *const u16,
-    src_len: *mut usize,
-    dst: *mut u8,
-    dst_len: *mut usize,
-) {
-    let (read, written) = encoding_rs::mem::convert_utf16_to_utf8_partial(
-        ::std::slice::from_raw_parts(src, *src_len),
-        ::std::slice::from_raw_parts_mut(dst, *dst_len),
-    );
-    *src_len = read;
-    *dst_len = written;
-}
-
-#[no_mangle]
-pub unsafe extern "C" fn encoding_mem_convert_utf8_to_utf16(
-    src: *const u8,
-    src_len: usize,
-    dst: *mut u16,
-    dst_len: usize,
-) -> usize {
-    encoding_rs::mem::convert_utf8_to_utf16(
-        ::std::slice::from_raw_parts(src, src_len),
-        ::std::slice::from_raw_parts_mut(dst, dst_len),
-    )
-}
--- a/intl/moz.build
+++ b/intl/moz.build
@@ -27,16 +27,17 @@ DIRS += [
 EXPORTS.mozilla += [
     'Encoding.h',
     'JapaneseDetector.h',
 ]
 
 EXPORTS += [
     '../third_party/rust/encoding_c/include/encoding_rs.h',
     '../third_party/rust/encoding_c/include/encoding_rs_statics.h',
+    '../third_party/rust/encoding_c_mem/include/encoding_rs_mem.h',
     '../third_party/rust/shift_or_euc_c/include/shift_or_euc.h',
 ]
 
 with Files("**"):
     BUG_COMPONENT = ("Core", "Internationalization")
 
 with Files("icu/**"):
     BUG_COMPONENT = ("Core", "JavaScript: Internationalization API")
--- a/intl/unicharutil/util/nsBidiUtils.h
+++ b/intl/unicharutil/util/nsBidiUtils.h
@@ -2,21 +2,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsBidiUtils_h__
 #define nsBidiUtils_h__
 
 #include "nsString.h"
-
-extern "C" {
-
-bool encoding_mem_is_utf16_bidi(char16_t const* buffer, size_t len);
-}
+#include "encoding_rs_mem.h"
 
 /**
  *  Read ftp://ftp.unicode.org/Public/UNIDATA/ReadMe-Latest.txt
  *  section BIDIRECTIONAL PROPERTIES
  *  for the detailed definition of the following categories
  *
  *  The values here must match the equivalents in %bidicategorycode in
  *  mozilla/intl/unicharutil/tools/genUnicodePropertyData.pl,
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/.cargo-checksum.json
@@ -0,0 +1,1 @@
+{"files":{"CONTRIBUTING.md":"d393951002340c3d98011f7b654e8133408f3f0e13b9f6470f4cb5d251e3afed","COPYRIGHT":"e9fbb44e03be7c959792ef8a3472bebdddffdafe44c87c923c039e09e1145813","Cargo.toml":"5f74d35f7f6a26aa3eb284e96e344be1d3ad1d40503171ed204f1bf727356319","LICENSE-APACHE":"cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30","LICENSE-MIT":"90df74ddb43e7f5aa5068890eacc151ecca7c997c9515cf17aea30b8734075bf","README.md":"e0119e9e226c787be0707ffb575a3309405be145bd1c9cbe79f528b5d891cd14","include/encoding_rs_mem.h":"3a3db57b6607e353f23679af6771426b11feb2a73bdd9577a9dfdd0cd11c950a","include/encoding_rs_mem_cpp.h":"407747e4e0ed5ace7014920cb536188f74d0675c91185f1e1878f9ec314de136","src/lib.rs":"4dfe017e10458810b323a94584b75475999e9c4a64c6c10cbba8df009c163d86"},"package":"2f9516d857aa9e070e02890ed3906f678ffca7fac8d82bd53c3567d96337f256"}
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/CONTRIBUTING.md
@@ -0,0 +1,33 @@
+If you send a pull request / patch, please observe the following.
+
+## Licensing
+
+Since this crate is dual-licensed,
+[section 5 of the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0#contributions)
+is considered to apply in the sense of Contributions being automatically
+under the Apache License 2.0 or MIT dual license (see the `COPYRIGHT` file).
+That is, by the act of offering a Contribution, you place your Contribution
+under the Apache License 2.0 or MIT dual license stated in the `COPYRIGHT`
+file. Please do not contribute if you aren't willing or allowed to license your
+contributions in this manner.
+
+You are encouraged to dedicate test code that you contribute to the Public
+Domain using the CC0 dedication. If you contribute test code that is not
+dedicated to the Public Domain, please be sure not to put it in a part of
+source code that the comments designate as being dedicated to the Public
+Domain.
+
+## Copyright Notices
+
+If you require the addition of your copyright notice, it's up to you to edit in
+your notice as part of your Contribution. Not adding a copyright notice is
+taken as a waiver of copyright notice.
+
+## Compatibility with Stable Rust
+
+Please ensure that your Contribution compiles with the latest stable-channel
+rustc.
+
+## rustfmt
+
+Please run `cargo fmt` before creating a pull.
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/COPYRIGHT
@@ -0,0 +1,9 @@
+encoding_c_mem is copyright 2015-2016 Mozilla Foundation.
+
+Licensed under the Apache License, Version 2.0
+<LICENSE-APACHE or
+https://www.apache.org/licenses/LICENSE-2.0> or the MIT
+license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
+at your option. All files in the project carrying such
+notice may not be copied, modified, or distributed except
+according to those terms.
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/Cargo.toml
@@ -0,0 +1,26 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# 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
+#
+# 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)
+
+[package]
+edition = "2018"
+name = "encoding_c_mem"
+version = "0.2.1"
+authors = ["Henri Sivonen <hsivonen@hsivonen.fi>"]
+description = "C API for encoding_rs::mem"
+homepage = "https://docs.rs/encoding_c_mem/"
+documentation = "https://docs.rs/encoding_c_mem/"
+readme = "README.md"
+keywords = ["ffi", "capi", "encoding", "unicode", "charset"]
+license = "MIT/Apache-2.0"
+repository = "https://github.com/hsivonen/encoding_c_mem"
+[dependencies.encoding_rs]
+version = "0.8.17"
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/LICENSE-APACHE
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright (c) 2015-2016 Mozilla Foundation
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/README.md
@@ -0,0 +1,37 @@
+# encoding_c_mem
+
+[![crates.io](https://meritbadge.herokuapp.com/encoding_c_mem)](https://crates.io/crates/encoding_c_mem)
+[![docs.rs](https://docs.rs/encoding_c_mem/badge.svg)](https://docs.rs/encoding_c_mem/)
+[![Apache 2 / MIT dual-licensed](https://img.shields.io/badge/license-Apache%202%20%2F%20MIT-blue.svg)](https://github.com/hsivonen/encoding_c_mem/blob/master/COPYRIGHT)
+
+encoding_c_mem is an FFI wrapper for the `mem` module of [encoding_rs](https://github.com/hsivonen/encoding_rs).
+
+## Licensing
+
+Please see the file named
+[COPYRIGHT](https://github.com/hsivonen/encoding_c_mem/blob/master/COPYRIGHT).
+
+## No Unwinding Support!
+
+This crate is meant for use in binaries compiled with `panic = 'abort'`, which
+is _required_ for correctness! Unwinding across FFI is Undefined Behavior, and
+this crate does nothing to try to prevent unwinding across the FFI if
+compiled with unwinding enabled.
+
+## Release Notes
+
+### 0.2.1
+
+* Fix a typo in README.
+
+### 0.2.0
+
+* Use `char` instead of `uint8_t` for 8-bit-unit text in C and C++.
+
+### 0.1.1
+
+* Add include guard to the C header.
+
+### 0.1.0
+
+* Initial release of encoding_c_mem.
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/include/encoding_rs_mem.h
@@ -0,0 +1,655 @@
+// Copyright 2015-2016 Mozilla Foundation. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#ifndef encoding_rs_mem_h_
+#define encoding_rs_mem_h_
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * _Note:_ "Latin1" in this header refers to the Unicode range from U+0000 to
+ * U+00FF, inclusive, and does not refer to the windows-1252 range. This
+ * in-memory encoding is sometimes used as a storage optimization of text
+ * when UTF-16 indexing and length semantics are exposed.
+ */
+
+/**
+ * Classification of text as Latin1 (all code points are below U+0100),
+ * left-to-right with some non-Latin1 characters or as containing at least
+ * some right-to-left characters.
+ */
+typedef enum {
+  /**
+   * Every character is below U+0100.
+   */
+  Latin1 = 0,
+  /**
+   * There is at least one character that's U+0100 or higher, but there
+   * are no right-to-left characters.
+   */
+  LeftToRight = 1,
+  /**
+   * There is at least one right-to-left character.
+   */
+  Bidi = 2,
+} Latin1Bidi;
+
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+/**
+ * Checks whether a valid UTF-8 buffer contains code points
+ * that trigger right-to-left processing or is all-Latin1.
+ *
+ * Possibly more efficient than performing the checks separately.
+ *
+ * Returns `Latin1Bidi::Latin1` if `is_str_latin1()` would return `true`.
+ * Otherwise, returns `Latin1Bidi::Bidi` if `is_str_bidi()` would return
+ * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+ * if `buffer` is `NULL`, or if the memory designated by `buffer` and
+ * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
+ * may be bogus but still has to be non-`NULL`.)
+ */
+Latin1Bidi encoding_mem_check_str_for_latin1_and_bidi(const char* buffer,
+                                                      size_t len);
+
+/**
+ * Checks whether a potentially invalid UTF-16 buffer contains code points
+ * that trigger right-to-left processing or is all-Latin1.
+ *
+ * Possibly more efficient than performing the checks separately.
+ *
+ * Returns `Latin1Bidi::Latin1` if `is_utf16_latin1()` would return `true`.
+ * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf16_bidi()` would return
+ * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+Latin1Bidi encoding_mem_check_utf16_for_latin1_and_bidi(const char16_t* buffer,
+                                                        size_t len);
+
+/**
+ * Checks whether a potentially invalid UTF-8 buffer contains code points
+ * that trigger right-to-left processing or is all-Latin1.
+ *
+ * Possibly more efficient than performing the checks separately.
+ *
+ * Returns `Latin1Bidi::Latin1` if `is_utf8_latin1()` would return `true`.
+ *
+ * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf8_bidi()` would return
+ * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL`.)
+ */
+Latin1Bidi encoding_mem_check_utf8_for_latin1_and_bidi(const char* buffer,
+                                                       size_t len);
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-16.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * The number of `char16_t`s written equals the length of the source buffer.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+void encoding_mem_convert_latin1_to_utf16(const char* src, size_t src_len,
+                                          char16_t* dst, size_t dst_len);
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-8.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer times two.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Safety
+ *
+ * Note that this function may write garbage beyond the number of bytes
+ * indicated by the return value.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_convert_latin1_to_utf8(const char* src, size_t src_len,
+                                           char* dst, size_t dst_len);
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-8 with potentially insufficient
+ * output space.
+ *
+ * Returns the number of bytes read and the number of bytes written.
+ *
+ * If the output isn't large enough, not all input is consumed.
+ *
+ * # Safety
+ *
+ * Note that this function may write garbage beyond the number of bytes
+ * indicated by the return value, so using a `&mut str` interpreted as
+ * `&mut [u8]` as the destination is not safe. If you want to convert into
+ * a `&mut str`, use `convert_utf16_to_str()` instead of this function.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+void encoding_mem_convert_latin1_to_utf8_partial(const char* src,
+                                                 size_t* src_len, char* dst,
+                                                 size_t* dst_len);
+
+/**
+ * Converts valid UTF-8 to valid UTF-16.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL`, if the two memory blocks overlap, of if the
+ * buffer designated by `src` and `src_len` does not contain valid UTF-8. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_convert_str_to_utf16(const char* src, size_t src_len,
+                                         char16_t* dst, size_t dst_len);
+
+/**
+ * If the input is valid UTF-16 representing only Unicode code points from
+ * U+0000 to U+00FF, inclusive, converts the input into output that
+ * represents the value of each code point as the unsigned byte value of
+ * each output byte.
+ *
+ * If the input does not fulfill the condition stated above, does something
+ * that is memory-safe without any promises about any properties of the
+ * output and will probably assert in debug builds in future versions.
+ * In particular, callers shouldn't assume the output to be the same across
+ * crate versions or CPU architectures and should not assume that non-ASCII
+ * input can't map to ASCII output.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * The number of bytes written equals the length of the source buffer.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ * (Probably in future versions if debug assertions are enabled (and not
+ * fuzzing) and the input is not in the range U+0000 to U+00FF, inclusive.)
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+void encoding_mem_convert_utf16_to_latin1_lossy(const char16_t* src,
+                                                size_t src_len, char* dst,
+                                                size_t dst_len);
+
+/**
+ * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+ * with the REPLACEMENT CHARACTER.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer times three.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_convert_utf16_to_utf8(const char16_t* src, size_t src_len,
+                                          char* dst, size_t dst_len);
+
+/**
+ * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+ * with the REPLACEMENT CHARACTER with potentially insufficient output
+ * space.
+ *
+ * Returns the number of code units read and the number of bytes written.
+ *
+ * Guarantees that the bytes in the destination beyond the number of
+ * bytes claimed as written by the second item of the return tuple
+ * are left unmodified.
+ *
+ * Not all code units are read if there isn't enough output space.
+ * Note  that this method isn't designed for general streamability but for
+ * not allocating memory for the worst case up front. Specifically,
+ * if the input starts with or ends with an unpaired surrogate, those are
+ * replaced with the REPLACEMENT CHARACTER.
+ *
+ * Matches the semantics of `TextEncoder.encodeInto()` from the
+ * Encoding Standard.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+void encoding_mem_convert_utf16_to_utf8_partial(const char16_t* src,
+                                                size_t* src_len, char* dst,
+                                                size_t* dst_len);
+
+/**
+ * If the input is valid UTF-8 representing only Unicode code points from
+ * U+0000 to U+00FF, inclusive, converts the input into output that
+ * represents the value of each code point as the unsigned byte value of
+ * each output byte.
+ *
+ * If the input does not fulfill the condition stated above, this function
+ * panics if debug assertions are enabled (and fuzzing isn't) and otherwise
+ * does something that is memory-safe without any promises about any
+ * properties of the output. In particular, callers shouldn't assume the
+ * output to be the same across crate versions or CPU architectures and
+ * should not assume that non-ASCII input can't map to ASCII output.
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ * If debug assertions are enabled (and not fuzzing) and the input is
+ * not in the range U+0000 to U+00FF, inclusive.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_convert_utf8_to_latin1_lossy(const char* src,
+                                                 size_t src_len, char* dst,
+                                                 size_t dst_len);
+
+/**
+ * Converts potentially-invalid UTF-8 to valid UTF-16 with errors replaced
+ * with the REPLACEMENT CHARACTER.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer _plus one_.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_convert_utf8_to_utf16(const char* src, size_t src_len,
+                                          char16_t* dst, size_t dst_len);
+
+/**
+ * Copies ASCII from source to destination up to the first non-ASCII byte
+ * (or the end of the input if it is ASCII in its entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_copy_ascii_to_ascii(const char* src, size_t src_len,
+                                        char* dst, size_t dst_len);
+
+/**
+ * Copies ASCII from source to destination zero-extending it to UTF-16 up to
+ * the first non-ASCII byte (or the end of the input if it is ASCII in its
+ * entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_copy_ascii_to_basic_latin(const char* src, size_t src_len,
+                                              char16_t* dst, size_t dst_len);
+
+/**
+ * Copies Basic Latin from source to destination narrowing it to ASCII up to
+ * the first non-Basic Latin code unit (or the end of the input if it is
+ * Basic Latin in its entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `src_len` don't designate a valid memory block, if
+ * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+ * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+ * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+ * aligned. Likewise for `dst` and `dst_len`.)
+ */
+size_t encoding_mem_copy_basic_latin_to_ascii(const char16_t* src,
+                                              size_t src_len, char* dst,
+                                              size_t dst_len);
+
+/**
+ * Replaces unpaired surrogates in the input with the REPLACEMENT CHARACTER.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+void encoding_mem_ensure_utf16_validity(char16_t* buffer, size_t len);
+
+/**
+ * Checks whether the buffer is all-ASCII.
+ *
+ * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+ * is not guaranteed to fail fast.)
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL`.)
+ */
+bool encoding_mem_is_ascii(const char* buffer, size_t len);
+
+/**
+ * Checks whether the buffer is all-Basic Latin (i.e. UTF-16 representing
+ * only ASCII characters).
+ *
+ * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+ * is not guaranteed to fail fast.)
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+bool encoding_mem_is_basic_latin(const char16_t* buffer, size_t len);
+
+/**
+ * Checks whether a scalar value triggers right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ *
+ * # Undefined behavior
+ *
+ * Undefined behavior ensues if `c` is not a valid Unicode Scalar Value.
+ */
+bool encoding_mem_is_char_bidi(char32_t c);
+
+/**
+ * Checks whether a valid UTF-8 buffer contains code points that trigger
+ * right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+ * if `buffer` is `NULL`, or if the memory designated by `buffer` and
+ * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
+ * may be bogus but still has to be non-`NULL`.)
+ */
+bool encoding_mem_is_str_bidi(const char* buffer, size_t len);
+
+/**
+ * Checks whether the buffer represents only code point less than or equal
+ * to U+00FF.
+ *
+ * Fails fast. (I.e. returns before having read the whole buffer if code
+ * points above U+00FF are discovered.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+ * if `buffer` is `NULL`, or if the memory designated by `buffer` and
+ * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
+ * may be bogus but still has to be non-`NULL`.)
+ */
+bool encoding_mem_is_str_latin1(const char* buffer, size_t len);
+
+/**
+ * Checks whether a UTF-16 buffer contains code points that trigger
+ * right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Returns `true` if the input contains an RTL character or an unpaired
+ * high surrogate that could be the high half of an RTL character.
+ * Returns `false` if the input contains neither RTL characters nor
+ * unpaired high surrogates that could be higher halves of RTL characters.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+bool encoding_mem_is_utf16_bidi(const char16_t* buffer, size_t len);
+
+/**
+ * Checks whether a UTF-16 code unit triggers right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Since supplementary-plane right-to-left blocks are identifiable from the
+ * high surrogate without examining the low surrogate, this function returns
+ * `true` for such high surrogates making the function suitable for handling
+ * supplementary-plane text without decoding surrogate pairs to scalar
+ * values. Obviously, such high surrogates are then reported as right-to-left
+ * even if actually unpaired.
+ */
+bool encoding_mem_is_utf16_code_unit_bidi(char16_t u);
+
+/**
+ * Checks whether the buffer represents only code point less than or equal
+ * to U+00FF.
+ *
+ * May read the entire buffer even if it isn't all-Latin1. (I.e. the function
+ * is not guaranteed to fail fast.)
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+bool encoding_mem_is_utf16_latin1(const char16_t* buffer, size_t len);
+
+/**
+ * Checks whether a potentially-invalid UTF-8 buffer contains code points
+ * that trigger right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Returns `true` if the input is invalid UTF-8 or the input contains an
+ * RTL character. Returns `false` if the input is valid UTF-8 and contains
+ * no RTL characters.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL`.)
+ */
+bool encoding_mem_is_utf8_bidi(const char* buffer, size_t len);
+
+/**
+ * Checks whether the buffer is valid UTF-8 representing only code points
+ * less than or equal to U+00FF.
+ *
+ * Fails fast. (I.e. returns before having read the whole buffer if UTF-8
+ * invalidity or code points above U+00FF are discovered.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL`.)
+ */
+bool encoding_mem_is_utf8_latin1(const char* buffer, size_t len);
+
+/**
+ * Returns the index of the first unpaired surrogate or, if the input is
+ * valid UTF-16 in its entirety, the length of the input.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+ * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+ * still has to be non-`NULL` and aligned.)
+ */
+size_t encoding_mem_utf16_valid_up_to(const char16_t* buffer, size_t len);
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif  // __cplusplus
+
+#endif  // encoding_rs_mem_h_
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/include/encoding_rs_mem_cpp.h
@@ -0,0 +1,546 @@
+// Copyright 2015-2016 Mozilla Foundation. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#pragma once
+
+#ifndef encoding_rs_mem_cpp_h_
+#define encoding_rs_mem_cpp_h_
+
+#include <string_view>
+#include <tuple>
+#include "gsl/gsl"
+
+#include "encoding_rs_mem.h"
+
+namespace encoding_rs {
+namespace mem {
+
+namespace detail {
+/**
+ * Replaces `nullptr` with a bogus pointer suitable for use as part of a
+ * zero-length Rust slice.
+ */
+template <class T>
+static inline T* null_to_bogus(T* ptr) {
+  return ptr ? ptr : reinterpret_cast<T*>(alignof(T));
+}
+};  // namespace detail
+
+/**
+ * Checks whether a potentially invalid UTF-16 buffer contains code points
+ * that trigger right-to-left processing or is all-Latin1.
+ *
+ * Possibly more efficient than performing the checks separately.
+ *
+ * Returns `Latin1Bidi::Latin1` if `is_utf16_latin1()` would return `true`.
+ * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf16_bidi()` would return
+ * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+ */
+inline Latin1Bidi check_for_latin1_and_bidi(std::u16string_view buffer) {
+  return encoding_mem_check_utf16_for_latin1_and_bidi(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether a potentially invalid UTF-8 buffer contains code points
+ * that trigger right-to-left processing or is all-Latin1.
+ *
+ * Possibly more efficient than performing the checks separately.
+ *
+ * Returns `Latin1Bidi::Latin1` if `is_utf8_latin1()` would return `true`.
+ *
+ * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf8_bidi()` would return
+ * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+ */
+inline Latin1Bidi check_for_latin1_and_bidi(std::string_view buffer) {
+  return encoding_mem_check_utf8_for_latin1_and_bidi(
+      encoding_rs::mem::detail::null_to_bogus<const char>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-16.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * The number of `char16_t`s written equals the length of the source buffer.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline void convert_latin1_to_utf16(gsl::span<const char> src,
+                                    gsl::span<char16_t> dst) {
+  encoding_mem_convert_latin1_to_utf16(
+      encoding_rs::mem::detail::null_to_bogus<const char>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char16_t>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-8.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer times two.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Safety
+ *
+ * Note that this function may write garbage beyond the number of bytes
+ * indicated by the return value.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `dst` overlap.
+ */
+inline size_t convert_latin1_to_utf8(gsl::span<const char> src,
+                                     gsl::span<char> dst) {
+  return encoding_mem_convert_latin1_to_utf8(
+      encoding_rs::mem::detail::null_to_bogus<const char>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Converts bytes whose unsigned value is interpreted as Unicode code point
+ * (i.e. U+0000 to U+00FF, inclusive) to UTF-8 with potentially insufficient
+ * output space.
+ *
+ * Returns the number of bytes read and the number of bytes written.
+ *
+ * If the output isn't large enough, not all input is consumed.
+ *
+ * # Safety
+ *
+ * Note that this function may write garbage beyond the number of bytes
+ * indicated by the return value, so using a `&mut str` interpreted as
+ * `&mut [u8]` as the destination is not safe. If you want to convert into
+ * a `&mut str`, use `convert_utf16_to_str()` instead of this function.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `dst` overlap.
+ */
+inline std::tuple<size_t, size_t> convert_latin1_to_utf8_partial(
+    gsl::span<const char> src, gsl::span<char> dst) {
+  size_t src_read = src.size();
+  size_t dst_written = dst.size();
+  encoding_mem_convert_latin1_to_utf8_partial(
+      encoding_rs::mem::detail::null_to_bogus<const char>(src.data()),
+      &src_read, encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      &dst_written);
+  return {src_read, dst_written};
+}
+
+/**
+ * Converts valid UTF-8 to valid UTF-16.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline size_t convert_str_to_utf16(std::string_view src,
+                                   gsl::span<char16_t> dst) {
+  return encoding_mem_convert_str_to_utf16(
+      encoding_rs::mem::detail::null_to_bogus<const char>(
+          reinterpret_cast<const char*>(src.data())),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char16_t>(dst.data()),
+      dst.size());
+}
+
+/**
+ * If the input is valid UTF-16 representing only Unicode code points from
+ * U+0000 to U+00FF, inclusive, converts the input into output that
+ * represents the value of each code point as the unsigned byte value of
+ * each output byte.
+ *
+ * If the input does not fulfill the condition stated above, does something
+ * that is memory-safe without any promises about any properties of the
+ * output and will probably assert in debug builds in future versions.
+ * In particular, callers shouldn't assume the output to be the same across
+ * crate versions or CPU architectures and should not assume that non-ASCII
+ * input can't map to ASCII output.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * The number of bytes written equals the length of the source buffer.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ * (Probably in future versions if debug assertions are enabled (and not
+ * fuzzing) and the input is not in the range U+0000 to U+00FF, inclusive.)
+ */
+inline void convert_utf16_to_latin1_lossy(std::u16string_view src,
+                                          gsl::span<char> dst) {
+  encoding_mem_convert_utf16_to_latin1_lossy(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+ * with the REPLACEMENT CHARACTER.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer times three.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline size_t convert_utf16_to_utf8(std::u16string_view src,
+                                    gsl::span<char> dst) {
+  return encoding_mem_convert_utf16_to_utf8(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+ * with the REPLACEMENT CHARACTER with potentially insufficient output
+ * space.
+ *
+ * Returns the number of code units read and the number of bytes written.
+ *
+ * Guarantees that the bytes in the destination beyond the number of
+ * bytes claimed as written by the second item of the return tuple
+ * are left unmodified.
+ *
+ * Not all code units are read if there isn't enough output space.
+ * Note  that this method isn't designed for general streamability but for
+ * not allocating memory for the worst case up front. Specifically,
+ * if the input starts with or ends with an unpaired surrogate, those are
+ * replaced with the REPLACEMENT CHARACTER.
+ *
+ * Matches the semantics of `TextEncoder.encodeInto()` from the
+ * Encoding Standard.
+ */
+inline std::tuple<size_t, size_t> convert_utf16_to_utf8_partial(
+    std::u16string_view src, gsl::span<char> dst) {
+  size_t src_read = src.size();
+  size_t dst_written = dst.size();
+  encoding_mem_convert_utf16_to_utf8_partial(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(src.data()),
+      &src_read, encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      &dst_written);
+  return {src_read, dst_written};
+}
+
+/**
+ * If the input is valid UTF-8 representing only Unicode code points from
+ * U+0000 to U+00FF, inclusive, converts the input into output that
+ * represents the value of each code point as the unsigned byte value of
+ * each output byte.
+ *
+ * If the input does not fulfill the condition stated above, this function
+ * panics if debug assertions are enabled (and fuzzing isn't) and otherwise
+ * does something that is memory-safe without any promises about any
+ * properties of the output. In particular, callers shouldn't assume the
+ * output to be the same across crate versions or CPU architectures and
+ * should not assume that non-ASCII input can't map to ASCII output.
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ * If debug assertions are enabled (and not fuzzing) and the input is
+ * not in the range U+0000 to U+00FF, inclusive.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `dst` overlap.
+ */
+inline size_t convert_utf8_to_latin1_lossy(std::string_view src,
+                                           gsl::span<char> dst) {
+  return encoding_mem_convert_utf8_to_latin1_lossy(
+      encoding_rs::mem::detail::null_to_bogus<const char>(
+          reinterpret_cast<const char*>(src.data())),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Converts potentially-invalid UTF-8 to valid UTF-16 with errors replaced
+ * with the REPLACEMENT CHARACTER.
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer _plus one_.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline size_t convert_utf8_to_utf16(std::string_view src,
+                                    gsl::span<char16_t> dst) {
+  return encoding_mem_convert_utf8_to_utf16(
+      encoding_rs::mem::detail::null_to_bogus<const char>(
+          reinterpret_cast<const char*>(src.data())),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char16_t>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Copies ASCII from source to destination up to the first non-ASCII byte
+ * (or the end of the input if it is ASCII in its entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ *
+ * # Undefined behavior
+ *
+ * UB ensues if `src` and `dst` overlap.
+ */
+inline size_t copy_ascii_to_ascii(gsl::span<const char> src,
+                                  gsl::span<char> dst) {
+  return encoding_mem_copy_ascii_to_ascii(
+      encoding_rs::mem::detail::null_to_bogus<const char>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Copies ASCII from source to destination zero-extending it to UTF-16 up to
+ * the first non-ASCII byte (or the end of the input if it is ASCII in its
+ * entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of `char16_t`s written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline size_t copy_ascii_to_basic_latin(gsl::span<const char> src,
+                                        gsl::span<char16_t> dst) {
+  return encoding_mem_copy_ascii_to_basic_latin(
+      encoding_rs::mem::detail::null_to_bogus<const char>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char16_t>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Copies Basic Latin from source to destination narrowing it to ASCII up to
+ * the first non-Basic Latin code unit (or the end of the input if it is
+ * Basic Latin in its entirety).
+ *
+ * The length of the destination buffer must be at least the length of the
+ * source buffer.
+ *
+ * Returns the number of bytes written.
+ *
+ * # Panics
+ *
+ * Panics if the destination buffer is shorter than stated above.
+ */
+inline size_t copy_basic_latin_to_ascii(gsl::span<const char16_t> src,
+                                        gsl::span<char> dst) {
+  return encoding_mem_copy_basic_latin_to_ascii(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(src.data()),
+      src.size(), encoding_rs::mem::detail::null_to_bogus<char>(dst.data()),
+      dst.size());
+}
+
+/**
+ * Replaces unpaired surrogates in the input with the REPLACEMENT CHARACTER.
+ */
+inline void ensure_utf16_validity(gsl::span<char16_t> buffer) {
+  encoding_mem_ensure_utf16_validity(
+      encoding_rs::mem::detail::null_to_bogus<char16_t>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether the buffer is all-ASCII.
+ *
+ * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+ * is not guaranteed to fail fast.)
+ */
+inline bool is_ascii(std::string_view buffer) {
+  return encoding_mem_is_ascii(
+      encoding_rs::mem::detail::null_to_bogus<const char>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether the buffer is all-Basic Latin (i.e. UTF-16 representing
+ * only ASCII characters).
+ *
+ * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+ * is not guaranteed to fail fast.)
+ */
+inline bool is_ascii(std::u16string_view buffer) {
+  return encoding_mem_is_basic_latin(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether a scalar value triggers right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ *
+ * # Undefined behavior
+ *
+ * Undefined behavior ensues if `c` is not a valid Unicode Scalar Value.
+ */
+inline bool is_scalar_value_bidi(char32_t c) {
+  return encoding_mem_is_char_bidi(c);
+}
+
+/**
+ * Checks whether a UTF-16 buffer contains code points that trigger
+ * right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Returns `true` if the input contains an RTL character or an unpaired
+ * high surrogate that could be the high half of an RTL character.
+ * Returns `false` if the input contains neither RTL characters nor
+ * unpaired high surrogates that could be higher halves of RTL characters.
+ */
+inline bool is_bidi(std::u16string_view buffer) {
+  return encoding_mem_is_utf16_bidi(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether a UTF-16 code unit triggers right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Since supplementary-plane right-to-left blocks are identifiable from the
+ * high surrogate without examining the low surrogate, this function returns
+ * `true` for such high surrogates making the function suitable for handling
+ * supplementary-plane text without decoding surrogate pairs to scalar
+ * values. Obviously, such high surrogates are then reported as right-to-left
+ * even if actually unpaired.
+ */
+inline bool is_utf16_code_unit_bidi(char16_t u) {
+  return encoding_mem_is_utf16_code_unit_bidi(u);
+}
+
+/**
+ * Checks whether the buffer represents only code point less than or equal
+ * to U+00FF.
+ *
+ * May read the entire buffer even if it isn't all-Latin1. (I.e. the function
+ * is not guaranteed to fail fast.)
+ */
+inline bool is_utf16_latin1(std::u16string_view buffer) {
+  return encoding_mem_is_utf16_latin1(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether a potentially-invalid UTF-8 buffer contains code points
+ * that trigger right-to-left processing.
+ *
+ * The check is done on a Unicode block basis without regard to assigned
+ * vs. unassigned code points in the block. Hebrew presentation forms in
+ * the Alphabetic Presentation Forms block are treated as if they formed
+ * a block on their own (i.e. it treated as right-to-left). Additionally,
+ * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+ * for. Control characters that are technically bidi controls but do not
+ * cause right-to-left behavior without the presence of right-to-left
+ * characters or right-to-left controls are not checked for. As a special
+ * case, U+FEFF is excluded from Arabic Presentation Forms-B.
+ * Returns `true` if the input is invalid UTF-8 or the input contains an
+ * RTL character. Returns `false` if the input is valid UTF-8 and contains
+ * no RTL characters.
+ */
+inline bool is_bidi(std::string_view buffer) {
+  return encoding_mem_is_utf8_bidi(
+      encoding_rs::mem::detail::null_to_bogus<const char>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Checks whether the buffer is valid UTF-8 representing only code points
+ * less than or equal to U+00FF.
+ *
+ * Fails fast. (I.e. returns before having read the whole buffer if UTF-8
+ * invalidity or code points above U+00FF are discovered.
+ */
+inline bool is_utf8_latin1(std::string_view buffer) {
+  return encoding_mem_is_utf8_latin1(
+      encoding_rs::mem::detail::null_to_bogus<const char>(buffer.data()),
+      buffer.size());
+}
+
+/**
+ * Returns the index of the first unpaired surrogate or, if the input is
+ * valid UTF-16 in its entirety, the length of the input.
+ */
+inline size_t utf16_valid_up_to(std::u16string_view buffer) {
+  return encoding_mem_utf16_valid_up_to(
+      encoding_rs::mem::detail::null_to_bogus<const char16_t>(buffer.data()),
+      buffer.size());
+}
+
+};  // namespace mem
+};  // namespace encoding_rs
+
+#endif  // encoding_rs_mem_cpp_h_
new file mode 100644
--- /dev/null
+++ b/third_party/rust/encoding_c_mem/src/lib.rs
@@ -0,0 +1,761 @@
+// Copyright 2015-2016 Mozilla Foundation. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! FFI bindings for `encoding_rs::mem`.
+//!
+//! _Note:_ "Latin1" in this module refers to the Unicode range from U+0000 to
+//! U+00FF, inclusive, and does not refer to the windows-1252 range. This
+//! in-memory encoding is sometimes used as a storage optimization of text
+//! when UTF-16 indexing and length semantics are exposed.
+
+use encoding_rs::mem::Latin1Bidi;
+
+/// Checks whether the buffer is all-ASCII.
+///
+/// May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+/// is not guaranteed to fail fast.)
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_ascii(buffer: *const u8, len: usize) -> bool {
+    encoding_rs::mem::is_ascii(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether the buffer is all-Basic Latin (i.e. UTF-16 representing
+/// only ASCII characters).
+///
+/// May read the entire buffer even if it isn't all-ASCII. (I.e. the function
+/// is not guaranteed to fail fast.)
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_basic_latin(buffer: *const u16, len: usize) -> bool {
+    encoding_rs::mem::is_basic_latin(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether the buffer is valid UTF-8 representing only code points
+/// less than or equal to U+00FF.
+///
+/// Fails fast. (I.e. returns before having read the whole buffer if UTF-8
+/// invalidity or code points above U+00FF are discovered.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_utf8_latin1(buffer: *const u8, len: usize) -> bool {
+    encoding_rs::mem::is_utf8_latin1(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether the buffer represents only code point less than or equal
+/// to U+00FF.
+///
+/// Fails fast. (I.e. returns before having read the whole buffer if code
+/// points above U+00FF are discovered.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+/// if `buffer` is `NULL`, or if the memory designated by `buffer` and `buffer_len`
+/// does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_str_latin1(buffer: *const u8, len: usize) -> bool {
+    encoding_rs::mem::is_str_latin1(::std::str::from_utf8_unchecked(
+        ::std::slice::from_raw_parts(buffer, len),
+    ))
+}
+
+/// Checks whether the buffer represents only code point less than or equal
+/// to U+00FF.
+///
+/// May read the entire buffer even if it isn't all-Latin1. (I.e. the function
+/// is not guaranteed to fail fast.)
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_utf16_latin1(buffer: *const u16, len: usize) -> bool {
+    encoding_rs::mem::is_utf16_latin1(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether a potentially-invalid UTF-8 buffer contains code points
+/// that trigger right-to-left processing.
+///
+/// The check is done on a Unicode block basis without regard to assigned
+/// vs. unassigned code points in the block. Hebrew presentation forms in
+/// the Alphabetic Presentation Forms block are treated as if they formed
+/// a block on their own (i.e. it treated as right-to-left). Additionally,
+/// the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+/// for. Control characters that are technically bidi controls but do not
+/// cause right-to-left behavior without the presence of right-to-left
+/// characters or right-to-left controls are not checked for. As a special
+/// case, U+FEFF is excluded from Arabic Presentation Forms-B.
+///
+/// Returns `true` if the input is invalid UTF-8 or the input contains an
+/// RTL character. Returns `false` if the input is valid UTF-8 and contains
+/// no RTL characters.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_utf8_bidi(buffer: *const u8, len: usize) -> bool {
+    encoding_rs::mem::is_utf8_bidi(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether a valid UTF-8 buffer contains code points that trigger
+/// right-to-left processing.
+///
+/// The check is done on a Unicode block basis without regard to assigned
+/// vs. unassigned code points in the block. Hebrew presentation forms in
+/// the Alphabetic Presentation Forms block are treated as if they formed
+/// a block on their own (i.e. it treated as right-to-left). Additionally,
+/// the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+/// for. Control characters that are technically bidi controls but do not
+/// cause right-to-left behavior without the presence of right-to-left
+/// characters or right-to-left controls are not checked for. As a special
+/// case, U+FEFF is excluded from Arabic Presentation Forms-B.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+/// if `buffer` is `NULL`, or if the memory designated by `buffer` and `buffer_len`
+/// does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_str_bidi(buffer: *const u8, len: usize) -> bool {
+    encoding_rs::mem::is_str_bidi(::std::str::from_utf8_unchecked(
+        ::std::slice::from_raw_parts(buffer, len),
+    ))
+}
+
+/// Checks whether a UTF-16 buffer contains code points that trigger
+/// right-to-left processing.
+///
+/// The check is done on a Unicode block basis without regard to assigned
+/// vs. unassigned code points in the block. Hebrew presentation forms in
+/// the Alphabetic Presentation Forms block are treated as if they formed
+/// a block on their own (i.e. it treated as right-to-left). Additionally,
+/// the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+/// for. Control characters that are technically bidi controls but do not
+/// cause right-to-left behavior without the presence of right-to-left
+/// characters or right-to-left controls are not checked for. As a special
+/// case, U+FEFF is excluded from Arabic Presentation Forms-B.
+///
+/// Returns `true` if the input contains an RTL character or an unpaired
+/// high surrogate that could be the high half of an RTL character.
+/// Returns `false` if the input contains neither RTL characters nor
+/// unpaired high surrogates that could be higher halves of RTL characters.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_utf16_bidi(buffer: *const u16, len: usize) -> bool {
+    encoding_rs::mem::is_utf16_bidi(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether a scalar value triggers right-to-left processing.
+///
+/// The check is done on a Unicode block basis without regard to assigned
+/// vs. unassigned code points in the block. Hebrew presentation forms in
+/// the Alphabetic Presentation Forms block are treated as if they formed
+/// a block on their own (i.e. it treated as right-to-left). Additionally,
+/// the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+/// for. Control characters that are technically bidi controls but do not
+/// cause right-to-left behavior without the presence of right-to-left
+/// characters or right-to-left controls are not checked for. As a special
+/// case, U+FEFF is excluded from Arabic Presentation Forms-B.
+///
+/// # Undefined behavior
+///
+/// Undefined behavior ensues if `c` is not a valid Unicode Scalar Value.
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_char_bidi(c: char) -> bool {
+    encoding_rs::mem::is_char_bidi(c)
+}
+
+/// Checks whether a UTF-16 code unit triggers right-to-left processing.
+///
+/// The check is done on a Unicode block basis without regard to assigned
+/// vs. unassigned code points in the block. Hebrew presentation forms in
+/// the Alphabetic Presentation Forms block are treated as if they formed
+/// a block on their own (i.e. it treated as right-to-left). Additionally,
+/// the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
+/// for. Control characters that are technically bidi controls but do not
+/// cause right-to-left behavior without the presence of right-to-left
+/// characters or right-to-left controls are not checked for. As a special
+/// case, U+FEFF is excluded from Arabic Presentation Forms-B.
+///
+/// Since supplementary-plane right-to-left blocks are identifiable from the
+/// high surrogate without examining the low surrogate, this function returns
+/// `true` for such high surrogates making the function suitable for handling
+/// supplementary-plane text without decoding surrogate pairs to scalar
+/// values. Obviously, such high surrogates are then reported as right-to-left
+/// even if actually unpaired.
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_is_utf16_code_unit_bidi(u: u16) -> bool {
+    encoding_rs::mem::is_utf16_code_unit_bidi(u)
+}
+
+/// Checks whether a potentially invalid UTF-8 buffer contains code points
+/// that trigger right-to-left processing or is all-Latin1.
+///
+/// Possibly more efficient than performing the checks separately.
+///
+/// Returns `Latin1Bidi::Latin1` if `is_utf8_latin1()` would return `true`.
+/// Otherwise, returns `Latin1Bidi::Bidi` if `is_utf8_bidi()` would return
+/// `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_check_utf8_for_latin1_and_bidi(
+    buffer: *const u8,
+    len: usize,
+) -> Latin1Bidi {
+    encoding_rs::mem::check_utf8_for_latin1_and_bidi(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Checks whether a valid UTF-8 buffer contains code points
+/// that trigger right-to-left processing or is all-Latin1.
+///
+/// Possibly more efficient than performing the checks separately.
+///
+/// Returns `Latin1Bidi::Latin1` if `is_str_latin1()` would return `true`.
+/// Otherwise, returns `Latin1Bidi::Bidi` if `is_str_bidi()` would return
+/// `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
+/// if `buffer` is `NULL`, or if the memory designated by `buffer` and `buffer_len`
+/// does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_check_str_for_latin1_and_bidi(
+    buffer: *const u8,
+    len: usize,
+) -> Latin1Bidi {
+    encoding_rs::mem::check_str_for_latin1_and_bidi(::std::str::from_utf8_unchecked(
+        ::std::slice::from_raw_parts(buffer, len),
+    ))
+}
+
+/// Checks whether a potentially invalid UTF-16 buffer contains code points
+/// that trigger right-to-left processing or is all-Latin1.
+///
+/// Possibly more efficient than performing the checks separately.
+///
+/// Returns `Latin1Bidi::Latin1` if `is_utf16_latin1()` would return `true`.
+/// Otherwise, returns `Latin1Bidi::Bidi` if `is_utf16_bidi()` would return
+/// `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_check_utf16_for_latin1_and_bidi(
+    buffer: *const u16,
+    len: usize,
+) -> Latin1Bidi {
+    encoding_rs::mem::check_utf16_for_latin1_and_bidi(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Converts potentially-invalid UTF-8 to valid UTF-16 with errors replaced
+/// with the REPLACEMENT CHARACTER.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer _plus one_.
+///
+/// Returns the number of `u16`s written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_utf8_to_utf16(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u16,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::convert_utf8_to_utf16(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// Converts valid UTF-8 to valid UTF-16.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// Returns the number of `u16`s written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL`, if the two memory blocks overlap, of if the
+/// buffer designated by `src` and `src_len` does not contain valid UTF-8. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_str_to_utf16(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u16,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::convert_str_to_utf16(
+        ::std::str::from_utf8_unchecked(::std::slice::from_raw_parts(src, src_len)),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+/// with the REPLACEMENT CHARACTER with potentially insufficient output
+/// space.
+///
+/// Returns the number of code units read and the number of bytes written.
+///
+/// Guarantees that the bytes in the destination beyond the number of
+/// bytes claimed as written by the second item of the return tuple
+/// are left unmodified.
+///
+/// Not all code units are read if there isn't enough output space.
+///
+/// Note  that this method isn't designed for general streamability but for
+/// not allocating memory for the worst case up front. Specifically,
+/// if the input starts with or ends with an unpaired surrogate, those are
+/// replaced with the REPLACEMENT CHARACTER.
+///
+/// Matches the semantics of `TextEncoder.encodeInto()` from the
+/// Encoding Standard.
+///
+/// # Safety
+///
+/// If you want to convert into a `&mut str`, use
+/// `convert_utf16_to_str_partial()` instead of using this function
+/// together with the `unsafe` method `as_bytes_mut()` on `&mut str`.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_utf16_to_utf8_partial(
+    src: *const u16,
+    src_len: *mut usize,
+    dst: *mut u8,
+    dst_len: *mut usize,
+) {
+    let (read, written) = encoding_rs::mem::convert_utf16_to_utf8_partial(
+        ::std::slice::from_raw_parts(src, *src_len),
+        ::std::slice::from_raw_parts_mut(dst, *dst_len),
+    );
+    *src_len = read;
+    *dst_len = written;
+}
+
+/// Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
+/// with the REPLACEMENT CHARACTER.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer times three.
+///
+/// Returns the number of bytes written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Safety
+///
+/// If you want to convert into a `&mut str`, use `convert_utf16_to_str()`
+/// instead of using this function together with the `unsafe` method
+/// `as_bytes_mut()` on `&mut str`.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_utf16_to_utf8(
+    src: *const u16,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::convert_utf16_to_utf8(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// Converts bytes whose unsigned value is interpreted as Unicode code point
+/// (i.e. U+0000 to U+00FF, inclusive) to UTF-16.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// The number of `u16`s written equals the length of the source buffer.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_latin1_to_utf16(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u16,
+    dst_len: usize,
+) {
+    encoding_rs::mem::convert_latin1_to_utf16(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    );
+}
+
+/// Converts bytes whose unsigned value is interpreted as Unicode code point
+/// (i.e. U+0000 to U+00FF, inclusive) to UTF-8 with potentially insufficient
+/// output space.
+///
+/// Returns the number of bytes read and the number of bytes written.
+///
+/// If the output isn't large enough, not all input is consumed.
+///
+/// # Safety
+///
+/// Note that this function may write garbage beyond the number of bytes
+/// indicated by the return value, so using a `&mut str` interpreted as
+/// `&mut [u8]` as the destination is not safe. If you want to convert into
+/// a `&mut str`, use `convert_utf16_to_str()` instead of this function.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_latin1_to_utf8_partial(
+    src: *const u8,
+    src_len: *mut usize,
+    dst: *mut u8,
+    dst_len: *mut usize,
+) {
+    let (read, written) = encoding_rs::mem::convert_latin1_to_utf8_partial(
+        ::std::slice::from_raw_parts(src, *src_len),
+        ::std::slice::from_raw_parts_mut(dst, *dst_len),
+    );
+    *src_len = read;
+    *dst_len = written;
+}
+
+/// Converts bytes whose unsigned value is interpreted as Unicode code point
+/// (i.e. U+0000 to U+00FF, inclusive) to UTF-8.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer times two.
+///
+/// Returns the number of bytes written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Safety
+///
+/// Note that this function may write garbage beyond the number of bytes
+/// indicated by the return value, so using a `&mut str` interpreted as
+/// `&mut [u8]` as the destination is not safe. If you want to convert into
+/// a `&mut str`, use `convert_utf16_to_str()` instead of this function.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_latin1_to_utf8(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::convert_latin1_to_utf8(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// If the input is valid UTF-8 representing only Unicode code points from
+/// U+0000 to U+00FF, inclusive, converts the input into output that
+/// represents the value of each code point as the unsigned byte value of
+/// each output byte.
+///
+/// If the input does not fulfill the condition stated above, this function
+/// panics if debug assertions are enabled (and fuzzing isn't) and otherwise
+/// does something that is memory-safe without any promises about any
+/// properties of the output. In particular, callers shouldn't assume the
+/// output to be the same across crate versions or CPU architectures and
+/// should not assume that non-ASCII input can't map to ASCII output.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// Returns the number of bytes written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// If debug assertions are enabled (and not fuzzing) and the input is
+/// not in the range U+0000 to U+00FF, inclusive.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_utf8_to_latin1_lossy(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::convert_utf8_to_latin1_lossy(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// If the input is valid UTF-16 representing only Unicode code points from
+/// U+0000 to U+00FF, inclusive, converts the input into output that
+/// represents the value of each code point as the unsigned byte value of
+/// each output byte.
+///
+/// If the input does not fulfill the condition stated above, does something
+/// that is memory-safe without any promises about any properties of the
+/// output and will probably assert in debug builds in future versions.
+/// In particular, callers shouldn't assume the output to be the same across
+/// crate versions or CPU architectures and should not assume that non-ASCII
+/// input can't map to ASCII output.
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// The number of bytes written equals the length of the source buffer.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// (Probably in future versions if debug assertions are enabled (and not
+/// fuzzing) and the input is not in the range U+0000 to U+00FF, inclusive.)
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_convert_utf16_to_latin1_lossy(
+    src: *const u16,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) {
+    encoding_rs::mem::convert_utf16_to_latin1_lossy(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    );
+}
+
+/// Returns the index of the first unpaired surrogate or, if the input is
+/// valid UTF-16 in its entirety, the length of the input.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_utf16_valid_up_to(buffer: *const u16, len: usize) -> usize {
+    encoding_rs::mem::utf16_valid_up_to(::std::slice::from_raw_parts(buffer, len))
+}
+
+/// Replaces unpaired surrogates in the input with the REPLACEMENT CHARACTER.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
+/// or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
+/// still has to be non-`NULL` and aligned.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_ensure_utf16_validity(buffer: *mut u16, len: usize) {
+    encoding_rs::mem::ensure_utf16_validity(::std::slice::from_raw_parts_mut(buffer, len));
+}
+
+/// Copies ASCII from source to destination up to the first non-ASCII byte
+/// (or the end of the input if it is ASCII in its entirety).
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// Returns the number of bytes written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_copy_ascii_to_ascii(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::copy_ascii_to_ascii(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// Copies ASCII from source to destination zero-extending it to UTF-16 up to
+/// the first non-ASCII byte (or the end of the input if it is ASCII in its
+/// entirety).
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// Returns the number of `u16`s written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_copy_ascii_to_basic_latin(
+    src: *const u8,
+    src_len: usize,
+    dst: *mut u16,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::copy_ascii_to_basic_latin(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
+
+/// Copies Basic Latin from source to destination narrowing it to ASCII up to
+/// the first non-Basic Latin code unit (or the end of the input if it is
+/// Basic Latin in its entirety).
+///
+/// The length of the destination buffer must be at least the length of the
+/// source buffer.
+///
+/// Returns the number of bytes written.
+///
+/// # Panics
+///
+/// Panics if the destination buffer is shorter than stated above.
+///
+/// # Undefined behavior
+///
+/// UB ensues if `src` and `src_len` don't designate a valid memory block, if
+/// `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
+/// block, if `dst` is `NULL` or if the two memory blocks overlap. (If
+/// `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
+/// aligned. Likewise for `dst` and `dst_len`.)
+#[no_mangle]
+pub unsafe extern "C" fn encoding_mem_copy_basic_latin_to_ascii(
+    src: *const u16,
+    src_len: usize,
+    dst: *mut u8,
+    dst_len: usize,
+) -> usize {
+    encoding_rs::mem::copy_basic_latin_to_ascii(
+        ::std::slice::from_raw_parts(src, src_len),
+        ::std::slice::from_raw_parts_mut(dst, dst_len),
+    )
+}
--- a/toolkit/library/rust/shared/Cargo.toml
+++ b/toolkit/library/rust/shared/Cargo.toml
@@ -18,16 +18,17 @@ prefs_parser = { path = "../../../../mod
 static_prefs = { path = "../../../../modules/libpref/init/static_prefs" }
 profiler_helper = { path = "../../../../tools/profiler/rust-helper", optional = true }
 mozurl = { path = "../../../../netwerk/base/mozurl" }
 webrender_bindings = { path = "../../../../gfx/webrender_bindings", optional = true }
 cubeb-coreaudio = { path = "../../../../media/libcubeb/cubeb-coreaudio-rs", optional = true }
 cubeb-pulse = { path = "../../../../media/libcubeb/cubeb-pulse-rs", optional = true, features=["pulse-dlopen"] }
 cubeb-sys = { version = "0.5.0", optional = true, features=["gecko-in-tree"] }
 encoding_c = "0.9.0"
+encoding_c_mem = "0.2.0"
 encoding_glue = { path = "../../../../intl/encoding_glue" }
 audioipc-client = { path = "../../../../media/audioipc/client", optional = true }
 audioipc-server = { path = "../../../../media/audioipc/server", optional = true }
 authenticator = "0.2.6"
 gkrust_utils = { path = "../../../../xpcom/rust/gkrust_utils" }
 rsdparsa_capi = { path = "../../../../media/webrtc/signaling/src/sdp/rsdparsa_capi" }
 xulstore = { path = "../../../components/xulstore", optional = true }
 # We have these to enforce common feature sets for said crates.
--- a/toolkit/library/rust/shared/lib.rs
+++ b/toolkit/library/rust/shared/lib.rs
@@ -19,16 +19,17 @@ extern crate profiler_helper;
 extern crate mozurl;
 #[cfg(feature = "quantum_render")]
 extern crate webrender_bindings;
 #[cfg(feature = "cubeb_coreaudio_rust")]
 extern crate cubeb_coreaudio;
 #[cfg(feature = "cubeb_pulse_rust")]
 extern crate cubeb_pulse;
 extern crate encoding_c;
+extern crate encoding_c_mem;
 extern crate encoding_glue;
 #[cfg(feature = "cubeb-remoting")]
 extern crate audioipc_client;
 #[cfg(feature = "cubeb-remoting")]
 extern crate audioipc_server;
 extern crate env_logger;
 extern crate authenticator;
 extern crate gkrust_utils;
--- a/xpcom/string/nsReadableUtils.h
+++ b/xpcom/string/nsReadableUtils.h
@@ -11,59 +11,25 @@
 /**
  * I guess all the routines in this file are all mis-named.
  * According to our conventions, they should be |NS_xxx|.
  */
 
 #include "mozilla/Assertions.h"
 #include "nsAString.h"
 #include "mozilla/Tuple.h"
+#include "encoding_rs_mem.h"
 
 #include "nsTArrayForwardDeclare.h"
 
-// Can't include mozilla/Encoding.h here. The implementations are in
-// the encoding_rs and encoding_glue crates.
+// Can't include mozilla/Encoding.h here. The implementation is in
+// the encoding_rs crate.
 extern "C" {
+// Declared as uint8_t instead of char to match declaration in another header.
 size_t encoding_utf8_valid_up_to(uint8_t const* buffer, size_t buffer_len);
-
-bool encoding_mem_is_ascii(uint8_t const* buffer, size_t buffer_len);
-
-bool encoding_mem_is_basic_latin(char16_t const* buffer, size_t buffer_len);
-
-bool encoding_mem_is_utf8_latin1(uint8_t const* buffer, size_t buffer_len);
-
-bool encoding_mem_is_str_latin1(uint8_t const* buffer, size_t buffer_len);
-
-bool encoding_mem_is_utf16_latin1(char16_t const* buffer, size_t buffer_len);
-
-size_t encoding_mem_utf16_valid_up_to(char16_t const* buffer,
-                                      size_t buffer_len);
-
-void encoding_mem_ensure_utf16_validity(char16_t* buffer, size_t buffer_len);
-
-void encoding_mem_convert_utf16_to_latin1_lossy(const char16_t* src,
-                                                size_t src_len, char* dst,
-                                                size_t dst_len);
-
-size_t encoding_mem_convert_utf8_to_latin1_lossy(const char* src,
-                                                 size_t src_len, char* dst,
-                                                 size_t dst_len);
-
-void encoding_mem_convert_latin1_to_utf16(const char* src, size_t src_len,
-                                          char16_t* dst, size_t dst_len);
-
-size_t encoding_mem_convert_utf16_to_utf8(const char16_t* src, size_t src_len,
-                                          char* dst, size_t dst_len);
-
-void encoding_mem_convert_utf16_to_utf8_partial(const char16_t* src,
-                                                size_t* src_len, char* dst,
-                                                size_t* dst_len);
-
-size_t encoding_mem_convert_utf8_to_utf16(const char* src, size_t src_len,
-                                          char16_t* dst, size_t dst_len);
 }
 
 // From the nsstring crate
 extern "C" {
 bool nsstring_fallible_append_utf8_impl(nsAString* aThis, const char* aOther,
                                         size_t aOtherLen, size_t aOldLen);
 
 bool nsstring_fallible_append_latin1_impl(nsAString* aThis, const char* aOther,
@@ -509,143 +475,89 @@ char16_t* CopyUnicodeTo(const nsAString&
 
 /**
  * Returns |true| if |aString| contains only ASCII characters, that is,
  * characters in the range (0x00, 0x7F).
  *
  * @param aString a 16-bit wide string to scan
  */
 inline bool IsASCII(mozilla::Span<const char16_t> aString) {
-  size_t length = aString.Length();
-  const char16_t* ptr = aString.Elements();
-  // For short strings, calling into Rust is a pessimization, and the SIMD
-  // code won't have a chance to kick in anyway.
-  if (length < 16) {
-    char16_t accu = 0;
-    for (size_t i = 0; i < length; i++) {
-      accu |= ptr[i];
-    }
-    return accu < 0x80U;
-  }
-  return encoding_mem_is_basic_latin(ptr, length);
+  return encoding_mem_is_basic_latin(aString.Elements(), aString.Length());
 }
 
 /**
  * Returns |true| if |aString| contains only ASCII characters, that is,
  * characters in the range (0x00, 0x7F).
  *
  * @param aString a 8-bit wide string to scan
  */
 inline bool IsASCII(mozilla::Span<const char> aString) {
   size_t length = aString.Length();
-  const uint8_t* ptr = reinterpret_cast<const uint8_t*>(aString.Elements());
-  // For short strings, calling into Rust is a pessimization, and the SIMD
+  const char* ptr = aString.Elements();
+  // For short strings, avoid the function call, since, the SIMD
   // code won't have a chance to kick in anyway.
   if (length < 16) {
+    const uint8_t* uptr = reinterpret_cast<const uint8_t*>(ptr);
     uint8_t accu = 0;
     for (size_t i = 0; i < length; i++) {
-      accu |= ptr[i];
+      accu |= uptr[i];
     }
     return accu < 0x80U;
   }
   return encoding_mem_is_ascii(ptr, length);
 }
 
 /**
  * Returns |true| if |aString| contains only Latin1 characters, that is,
  * characters in the range (U+0000, U+00FF).
  *
  * @param aString a potentially-invalid UTF-16 string to scan
  */
 inline bool IsUTF16Latin1(mozilla::Span<const char16_t> aString) {
-  size_t length = aString.Length();
-  const char16_t* ptr = aString.Elements();
-  // For short strings, calling into Rust is a pessimization, and the SIMD
-  // code won't have a chance to kick in anyway.
-  if (length < 16) {
-    char16_t accu = 0;
-    for (size_t i = 0; i < length; i++) {
-      accu |= ptr[i];
-    }
-    return accu < 0x100U;
-  }
-  return encoding_mem_is_utf16_latin1(ptr, length);
+  return encoding_mem_is_utf16_latin1(aString.Elements(), aString.Length());
 }
 
 /**
  * Returns |true| if |aString| contains only Latin1 characters, that is,
  * characters in the range (U+0000, U+00FF).
  *
  * If you know that the argument is always absolutely guaranteed to be valid
  * UTF-8, use the faster UnsafeIsValidUTF8Latin1() instead.
  *
  * @param aString potentially-invalid UTF-8 string to scan
  */
 inline bool IsUTF8Latin1(mozilla::Span<const char> aString) {
-  size_t length = aString.Length();
-  const uint8_t* ptr = reinterpret_cast<const uint8_t*>(aString.Elements());
-  // For short strings, calling into Rust is a pessimization, and the SIMD
-  // code won't have a chance to kick in anyway.
-  if (length < 16) {
-    for (size_t i = 0; i < length; i++) {
-      if (ptr[i] >= 0x80U) {
-        ptr += i;
-        length -= i;
-        // This loop can't handle non-ASCII, but the Rust code can, so
-        // upon seeing non-ASCII, break the loop and let the Rust code
-        // handle the rest of the buffer (including the non-ASCII byte).
-        goto end;
-      }
-    }
-    return true;
-  }
-end:
-  return encoding_mem_is_utf8_latin1(ptr, length);
+  return encoding_mem_is_utf8_latin1(aString.Elements(), aString.Length());
 }
 
 /**
  * Returns |true| if |aString| contains only Latin1 characters, that is,
  * characters in the range (U+0000, U+00FF).
  *
  * The argument MUST be valid UTF-8. If you are at all unsure, use IsUTF8Latin1
  * instead!
  *
  * @param aString known-valid UTF-8 string to scan
  */
 inline bool UnsafeIsValidUTF8Latin1(mozilla::Span<const char> aString) {
-  size_t length = aString.Length();
-  const uint8_t* ptr = reinterpret_cast<const uint8_t*>(aString.Elements());
-  // For short strings, calling into Rust is a pessimization, and the SIMD
-  // code won't have a chance to kick in anyway.
-  if (length < 16) {
-    for (size_t i = 0; i < length; i++) {
-      if (ptr[i] >= 0x80U) {
-        ptr += i;
-        length -= i;
-        goto end;
-      }
-    }
-    return true;
-  }
-end:
-  return encoding_mem_is_str_latin1(ptr, length);
+  return encoding_mem_is_str_latin1(aString.Elements(), aString.Length());
 }
 
 /**
  * Returns |true| if |aString| is a valid UTF-8 string.
  *
  * Note that this doesn't check whether the string might look like a valid
  * string in another encoding, too, e.g. ISO-2022-JP.
  *
  * @param aString an 8-bit wide string to scan
  */
 inline bool IsUTF8(mozilla::Span<const char> aString) {
   size_t length = aString.Length();
   const uint8_t* ptr = reinterpret_cast<const uint8_t*>(aString.Elements());
-  // For short strings, calling into Rust is a pessimization, and the SIMD
+  // For short strings, the function call is a pessimization, and the SIMD
   // code won't have a chance to kick in anyway.
   if (length < 16) {
     for (size_t i = 0; i < length; i++) {
       if (ptr[i] >= 0x80U) {
         ptr += i;
         length -= i;
         goto end;
       }