Skip to content

Commit 14b5a4c

Browse files
committed
Make the tokenizer borrow rather than own its input.
1 parent 98c1e58 commit 14b5a4c

File tree

4 files changed

+7
-30
lines changed

4 files changed

+7
-30
lines changed

src/from_bytes.rs

-23
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use encoding::label::encoding_from_whatwg_label;
88
use encoding::all::UTF_8;
99
use encoding::{EncodingRef, DecoderTrap, decode};
1010

11-
use tokenizer::{tokenize, Tokenizer};
12-
use parser::{parse_stylesheet_rules, StylesheetParser};
13-
1411

1512
/// Determine the character encoding of a CSS stylesheet and decode it.
1613
///
@@ -68,23 +65,3 @@ fn decode_replace(input: &[u8], fallback_encoding: EncodingRef)-> (String, Encod
6865
let (result, used_encoding) = decode(input, DecoderTrap::Replace, fallback_encoding);
6966
(result.unwrap(), used_encoding)
7067
}
71-
72-
73-
/// Parse stylesheet from bytes.
74-
///
75-
/// * `css_bytes`: A byte string.
76-
/// * `protocol_encoding`: The encoding label, if any, defined by HTTP or equivalent protocol.
77-
/// (e.g. via the `charset` parameter of the `Content-Type` header.)
78-
/// * `environment_encoding`: An optional `Encoding` object for the [environment encoding]
79-
/// (http://www.w3.org/TR/css-syntax/#environment-encoding), if any.
80-
///
81-
/// Returns a 2-tuple of a `Iterator<Result<Rule, SyntaxError>>`
82-
/// and the `Encoding` object that was used.
83-
pub fn parse_stylesheet_rules_from_bytes(
84-
css_bytes: &[u8], protocol_encoding_label: Option<&str>,
85-
environment_encoding: Option<EncodingRef>)
86-
-> (StylesheetParser<Tokenizer>, EncodingRef) {
87-
let (css_unicode, encoding) = decode_stylesheet_bytes(
88-
css_bytes, protocol_encoding_label, environment_encoding);
89-
(parse_stylesheet_rules(tokenize(css_unicode.as_slice())), encoding)
90-
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use parser::{parse_stylesheet_rules, StylesheetParser,
2222
parse_rule_list, RuleListParser,
2323
parse_declaration_list, DeclarationListParser,
2424
parse_one_rule, parse_one_declaration, parse_one_component_value};
25-
pub use from_bytes::{decode_stylesheet_bytes, parse_stylesheet_rules_from_bytes};
25+
pub use from_bytes::decode_stylesheet_bytes;
2626
pub use color::{RGBA, Color};
2727
pub use nth::parse_nth;
2828
pub use serializer::{ToCss, CssStringWriter, serialize_identifier, serialize_string};

src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ fn stylesheet_from_bytes() {
185185
let environment_encoding = get_string(&map, &"environment_encoding".to_string())
186186
.and_then(encoding_from_whatwg_label);
187187

188-
let (mut rules, used_encoding) = parse_stylesheet_rules_from_bytes(
188+
let (css_unicode, used_encoding) = decode_stylesheet_bytes(
189189
css.as_slice(), protocol_encoding_label, environment_encoding);
190+
let mut rules = parse_stylesheet_rules(tokenize(css_unicode.as_slice()));
190191

191192
(rules.collect::<Vec<Result<Rule, SyntaxError>>>(), used_encoding.name().to_string()).to_json()
192193
};

src/tokenizer.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ use ast::ComponentValue::*;
1313

1414
/// Returns a `Iterator<(ComponentValue, SourceLocation)>`
1515
pub fn tokenize(input: &str) -> Tokenizer {
16-
let input = input.into_string();
1716
Tokenizer {
1817
length: input.len(),
1918
input: input,
2019
position: 0,
2120
}
2221
}
2322

24-
impl Iterator<Node> for Tokenizer {
23+
impl<'a> Iterator<Node> for Tokenizer<'a> {
2524
#[inline]
2625
fn next(&mut self) -> Option<Node> { next_component_value(self) }
2726
}
@@ -30,8 +29,8 @@ impl Iterator<Node> for Tokenizer {
3029
// *********** End of public API ***********
3130

3231

33-
pub struct Tokenizer {
34-
input: String,
32+
pub struct Tokenizer<'a> {
33+
input: &'a str,
3534
length: uint, // All counted in bytes, not characters
3635
position: uint, // All counted in bytes, not characters
3736
}
@@ -43,7 +42,7 @@ macro_rules! is_match(
4342
)
4443

4544

46-
impl Tokenizer {
45+
impl<'a> Tokenizer<'a> {
4746
#[inline]
4847
fn is_eof(&self) -> bool { self.position >= self.length }
4948

0 commit comments

Comments
 (0)