servo: Merge #17640 - style: Fix starts_with_ignore_ascii_case (from emilio:starts-with-ignore-ascii-case); r=stshine

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

Source-Repo: https://github.com/servo/servo
Source-Revision: 104e0b473073340689b4bca5c128c61632d4e58d

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 14e052469d419b4fd24721fd275047a20527297f
This commit is contained in:
Emilio Cobos Álvarez 2017-07-08 12:59:29 -07:00
parent 39fc6ece65
commit 168589b832
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())
}

View File

@ -2,7 +2,7 @@
* 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/. */
use style::str::{split_html_space_chars, str_join};
use style::str::{split_html_space_chars, str_join, starts_with_ignore_ascii_case};
#[test]
pub fn split_html_space_chars_whitespace() {
@ -33,3 +33,14 @@ pub fn test_str_join_many() {
let expected = "-alpha--beta-gamma-";
assert_eq!(actual, expected);
}
#[test]
pub fn test_starts_with_ignore_ascii_case_basic() {
assert!(starts_with_ignore_ascii_case("-webkit-", "-webkit-"));
assert!(starts_with_ignore_ascii_case("-webkit-foo", "-webkit-"));
}
#[test]
pub fn test_starts_with_ignore_ascii_case_char_boundary() {
assert!(!starts_with_ignore_ascii_case("aaaaa💩", "-webkit-"));
}