Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add starts_with and ends_with to OsStr #26499

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ impl OsStr {
self.to_bytes().and_then(|b| CString::new(b).ok())
}

/// Returns true if the `other` is a prefix of the `OsStr`.
#[unstable(feature = "os_str_compare", reason = "recently added")]
pub fn starts_with<S: AsRef<OsStr>>(&self, other: S) -> bool {
self.bytes().starts_with(other.as_ref().bytes())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct for plain bytes and UTF-8, but what about other encodings? If OsStr is WTF-8, does that allow false positives (say for example that a single byte encoded sequence for a codepoint exists that's a prefix of a multibyte sequence. UTF-8 doesn't have this).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonSapin? I think these should be fine. If I'm reading the "spec" correctly, WTF-8 is a strict subset of "generalized UTF-8" (https://simonsapin.github.io/wtf-8/#generalized-utf_8) and, from what I can tell, generalized UTF-8 is a prefix-free code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, ok that was simple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, WTF-8 preserves the nice properties of UTF-8 like self-synchronization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Just a note for people from the future: No this is not fine because if self contains a non-BMP code point (4-byte sequence), and other contains just a high-surrogate (3-byte sequence), this implementation will always return false, contradicting the result when given two WTF-16 sequences.

}

/// Returns true if the `other` is a suffix of the `OsStr`.
#[unstable(feature = "os_str_compare", reason = "recently added")]
pub fn ends_with<S: AsRef<OsStr>>(&self, other: S) -> bool {
self.bytes().ends_with(other.as_ref().bytes())
}

/// Gets the underlying byte representation.
///
/// Note: it is *crucial* that this API is private, to avoid
Expand Down Expand Up @@ -414,3 +426,35 @@ impl AsInner<Slice> for OsStr {
&self.inner
}
}

#[cfg(test)]
mod test {
use super::OsStr;
#[test]
fn starts_with() {
assert!(OsStr::new("abcde").starts_with(OsStr::new("abcde")));
assert!(OsStr::new("abcde").starts_with(OsStr::new("abc")));
assert!(!OsStr::new("abcde").starts_with(OsStr::new("abde")));
assert!(!OsStr::new("abcde").starts_with(OsStr::new("abcdef")));
assert!(OsStr::new("").starts_with(OsStr::new("")));
assert!(OsStr::new("anything").starts_with(OsStr::new("")));
assert!(!OsStr::new("").starts_with(OsStr::new("anything")));

// test as_ref
assert!(OsStr::new("abcde").starts_with("abc"));
}

#[test]
fn ends_with() {
assert!(OsStr::new("abcde").ends_with(OsStr::new("abcde")));
assert!(OsStr::new("abcde").ends_with(OsStr::new("cde")));
assert!(!OsStr::new("abcde").ends_with(OsStr::new("abde")));
assert!(!OsStr::new("bcde").ends_with(OsStr::new("abcde")));
assert!(OsStr::new("").ends_with(OsStr::new("")));
assert!(OsStr::new("anything").ends_with(OsStr::new("")));
assert!(!OsStr::new("").ends_with(OsStr::new("anything")));

// test as_ref
assert!(OsStr::new("abcde").ends_with("cde"));
}
}