Auto merge of #17640 - emilio:starts-with-ignore-ascii-case, r=stshine

style: Fix starts_with_ignore_ascii_case.

In particular, fix a panic when the input is not ASCII and we happen to index in
something that is not a char boundary.

This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1379380

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17640)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-07-08 12:59:29 -07:00 committed by GitHub
commit 104e0b4730
2 changed files with 14 additions and 3 deletions

View file

@ -148,6 +148,6 @@ pub fn str_join<I, T>(strs: I, join: &str) -> String
/// Returns true if a given string has a given prefix with case-insensitive match.
pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool {
string.len() > prefix.len() &&
string[0..prefix.len()].eq_ignore_ascii_case(prefix)
string.len() >= prefix.len() &&
string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
}