Skip to content

Truly independent web browser

AK: Compare and borrow Utf16Strings in Rust

Shannon Booth13b95e0c9c0b
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index 14801e32dbb..04d4215a7bb 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -221,6 +221,19 @@ macro_rules! impl_utf16_string_owner {
Self(unsafe { OwnedUtf16String::from_raw(raw) })
}
+ /// Retains a borrowed raw `AK::Utf16String` reference.
+ ///
+ /// # Safety
+ ///
+ /// `raw` must be a valid, nonzero raw representation that remains live for this call.
+ #[inline]
+ pub unsafe fn from_raw_borrowed(raw: usize) -> Self {
+ // SAFETY: The caller keeps the string alive while adding a reference.
+ unsafe { reference_utf16_string(raw) };
+ // SAFETY: The new reference is transferred to the returned owner.
+ unsafe { Self::from_raw_owned(raw) }
+ }
+
/// Transfers this owner's existing reference to the caller.
#[inline]
pub fn into_raw(self) -> usize {
@@ -736,19 +749,6 @@ pub fn encode_base64(input: &[u8], alphabet: Base64Alphabet, omit_padding: bool)
}
impl Utf16FlyString {
- /// Retains a borrowed raw `AK::Utf16FlyString` reference.
- ///
- /// # Safety
- ///
- /// `raw` must be a valid, nonzero fly-string representation that remains live for this call.
- #[inline]
- pub unsafe fn from_raw_borrowed(raw: usize) -> Self {
- // SAFETY: The caller keeps the string alive while adding a reference.
- unsafe { reference_utf16_string(raw) };
- // SAFETY: The new reference is transferred to the returned owner.
- unsafe { Self::from_raw_owned(raw) }
- }
-
/// Creates a string through AK's authoritative UTF-8 fly-string table.
pub fn from_utf8(string: &str) -> Self {
if let Some(raw) = utf16_short_string_raw(string) {
@@ -834,6 +834,23 @@ impl PartialEq for Utf16String {
impl Eq for Utf16String {}
+impl PartialEq<str> for Utf16String {
+ fn eq(&self, other: &str) -> bool {
+ match self.as_units() {
+ // NB: The UTF-8 of an ASCII string is its ASCII bytes.
+ Utf16StringUnits::Ascii(units) => units == other.as_bytes(),
+ Utf16StringUnits::Latin1(units) => units.iter().map(|&unit| u16::from(unit)).eq(other.encode_utf16()),
+ Utf16StringUnits::Utf16(units) => units.iter().copied().eq(other.encode_utf16()),
+ }
+ }
+}
+
+impl PartialEq<&str> for Utf16String {
+ fn eq(&self, other: &&str) -> bool {
+ *self == **other
+ }
+}
+
impl std::fmt::Debug for Utf16String {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let string = match self.as_units() {