mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-09-27 22:11:54 +00:00
Minor performance improvements in AttributePairs parsing
This commit is contained in:
parent
91dc000b78
commit
5bba4b306a
1 changed files with 8 additions and 17 deletions
|
@ -21,11 +21,10 @@ impl<'a> Iterator for AttributePairs<'a> {
|
||||||
// the position in the string:
|
// the position in the string:
|
||||||
let start = self.index;
|
let start = self.index;
|
||||||
// the key ends at an `=`:
|
// the key ends at an `=`:
|
||||||
let end = self
|
let end = self.string[self.index..]
|
||||||
.string
|
|
||||||
.char_indices()
|
.char_indices()
|
||||||
.skip_while(|(i, _)| *i < self.index)
|
.find_map(|(i, c)| if c == '=' { Some(i) } else { None })?
|
||||||
.find_map(|(i, c)| if c == '=' { Some(i) } else { None })?;
|
+ self.index;
|
||||||
|
|
||||||
// advance the index to the char after the end of the key (to skip the `=`)
|
// advance the index to the char after the end of the key (to skip the `=`)
|
||||||
// NOTE: it is okay to add 1 to the index, because an `=` is exactly 1 byte.
|
// NOTE: it is okay to add 1 to the index, because an `=` is exactly 1 byte.
|
||||||
|
@ -44,11 +43,7 @@ impl<'a> Iterator for AttributePairs<'a> {
|
||||||
let end = {
|
let end = {
|
||||||
let mut result = self.string.len();
|
let mut result = self.string.len();
|
||||||
|
|
||||||
for (i, c) in self
|
for (i, c) in self.string[self.index..].char_indices() {
|
||||||
.string
|
|
||||||
.char_indices()
|
|
||||||
.skip_while(|(i, _)| *i < self.index)
|
|
||||||
{
|
|
||||||
// if a quote is encountered
|
// if a quote is encountered
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
// update variable
|
// update variable
|
||||||
|
@ -60,7 +55,7 @@ impl<'a> Iterator for AttributePairs<'a> {
|
||||||
self.index += 1;
|
self.index += 1;
|
||||||
// the result is the index of the comma (comma is not included in the
|
// the result is the index of the comma (comma is not included in the
|
||||||
// resulting string)
|
// resulting string)
|
||||||
result = i;
|
result = i + self.index - 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +69,7 @@ impl<'a> Iterator for AttributePairs<'a> {
|
||||||
::core::str::from_utf8(&self.string.as_bytes()[start..end]).unwrap()
|
::core::str::from_utf8(&self.string.as_bytes()[start..end]).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
Some((key.trim(), value.trim()))
|
Some((key, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
@ -84,11 +79,7 @@ impl<'a> Iterator for AttributePairs<'a> {
|
||||||
// this also ignores `=` inside quotes!
|
// this also ignores `=` inside quotes!
|
||||||
let mut inside_quotes = false;
|
let mut inside_quotes = false;
|
||||||
|
|
||||||
for (_, c) in self
|
for (_, c) in self.string[self.index..].char_indices() {
|
||||||
.string
|
|
||||||
.char_indices()
|
|
||||||
.skip_while(|(i, _)| *i < self.index)
|
|
||||||
{
|
|
||||||
if c == '=' && !inside_quotes {
|
if c == '=' && !inside_quotes {
|
||||||
remaining += 1;
|
remaining += 1;
|
||||||
} else if c == '"' {
|
} else if c == '"' {
|
||||||
|
@ -199,7 +190,7 @@ mod test {
|
||||||
assert_eq!((1, Some(1)), pairs.size_hint());
|
assert_eq!((1, Some(1)), pairs.size_hint());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
pairs.next(),
|
pairs.next(),
|
||||||
Some(("ध्वनि स्थिति और्४५० नीचे", "देखने लाभो द्वारा करके(विशेष"))
|
Some(("ध्वनि स्थिति और्४५० नीचे ", "देखने लाभो द्वारा करके(विशेष"))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!((0, Some(0)), pairs.size_hint());
|
assert_eq!((0, Some(0)), pairs.size_hint());
|
||||||
|
|
Loading…
Reference in a new issue