mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Fix logic bug in transform_text; convert do to for; add some simple tests for each compression mode.
This commit is contained in:
parent
f5a5f35314
commit
afb8f9493b
1 changed files with 102 additions and 9 deletions
|
@ -31,7 +31,7 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str {
|
||||||
let out_str: ~str = ~"";
|
let out_str: ~str = ~"";
|
||||||
match mode {
|
match mode {
|
||||||
CompressNone | DiscardNewline => {
|
CompressNone | DiscardNewline => {
|
||||||
do str::each_char(text) |ch: char| {
|
for str::each_char(text) |ch: char| {
|
||||||
if is_discardable_char(ch, mode) {
|
if is_discardable_char(ch, mode) {
|
||||||
// TODO: record skipped char
|
// TODO: record skipped char
|
||||||
} else {
|
} else {
|
||||||
|
@ -41,14 +41,12 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str {
|
||||||
}
|
}
|
||||||
str::push_char(&out_str, ch);
|
str::push_char(&out_str, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
CompressWhitespace | CompressWhitespaceNewline => {
|
CompressWhitespace | CompressWhitespaceNewline => {
|
||||||
let mut in_whitespace: bool = false;
|
let mut in_whitespace: bool = false;
|
||||||
do str::each_char(text) |ch: char| {
|
for str::each_char(text) |ch: char| {
|
||||||
// TODO: discard newlines between CJK chars
|
// TODO: discard newlines between CJK chars
|
||||||
let mut next_in_whitespace: bool = match (ch, mode) {
|
let mut next_in_whitespace: bool = match (ch, mode) {
|
||||||
// TODO: check for following char that may create
|
// TODO: check for following char that may create
|
||||||
|
@ -60,7 +58,7 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str {
|
||||||
(_, _) => false
|
(_, _) => false
|
||||||
};
|
};
|
||||||
|
|
||||||
if next_in_whitespace {
|
if !next_in_whitespace {
|
||||||
if is_always_discardable_char(ch) {
|
if is_always_discardable_char(ch) {
|
||||||
// revert whitespace setting, since this char was discarded
|
// revert whitespace setting, since this char was discarded
|
||||||
next_in_whitespace = in_whitespace;
|
next_in_whitespace = in_whitespace;
|
||||||
|
@ -69,18 +67,17 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str {
|
||||||
// TODO: record kept char
|
// TODO: record kept char
|
||||||
str::push_char(&out_str, ch);
|
str::push_char(&out_str, ch);
|
||||||
}
|
}
|
||||||
} else {
|
} else { /* next_in_whitespace; possibly add a space char */
|
||||||
if in_whitespace {
|
if in_whitespace {
|
||||||
// TODO: record skipped char
|
// TODO: record skipped char
|
||||||
} else {
|
} else {
|
||||||
// TODO: record kept char
|
// TODO: record kept char
|
||||||
str::push_char(&out_str, ch);
|
str::push_char(&out_str, ' ');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// save whitespace context for next char
|
// save whitespace context for next char
|
||||||
in_whitespace = next_in_whitespace;
|
in_whitespace = next_in_whitespace;
|
||||||
true
|
} /* /for str::each_char */
|
||||||
} /* /do str::each_chari */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,3 +125,99 @@ pub fn true_type_tag(a: char, b: char, c: char, d: char) -> u32 {
|
||||||
fn test_true_type_tag() {
|
fn test_true_type_tag() {
|
||||||
assert true_type_tag('c', 'm', 'a', 'p') == 0x_63_6D_61_70_u32;
|
assert true_type_tag('c', 'm', 'a', 'p') == 0x_63_6D_61_70_u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_compress_none() {
|
||||||
|
|
||||||
|
let test_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo\n bar",
|
||||||
|
~"foo \nbar",
|
||||||
|
~" foo bar \nbaz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz\n\n"];
|
||||||
|
let mode = CompressNone;
|
||||||
|
|
||||||
|
for uint::range(0, test_strs.len()) |i| {
|
||||||
|
assert transform_text(test_strs[i], mode) == test_strs[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_discard_newline() {
|
||||||
|
|
||||||
|
let test_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo\n bar",
|
||||||
|
~"foo \nbar",
|
||||||
|
~" foo bar \nbaz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz\n\n"];
|
||||||
|
|
||||||
|
let oracle_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo bar",
|
||||||
|
~"foo bar",
|
||||||
|
~" foo bar baz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz"];
|
||||||
|
|
||||||
|
assert vec::same_length(test_strs, oracle_strs);
|
||||||
|
let mode = DiscardNewline;
|
||||||
|
|
||||||
|
for uint::range(0, test_strs.len()) |i| {
|
||||||
|
assert transform_text(test_strs[i], mode) == oracle_strs[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_compress_whitespace() {
|
||||||
|
let test_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo\n bar",
|
||||||
|
~"foo \nbar",
|
||||||
|
~" foo bar \nbaz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz\n\n"];
|
||||||
|
|
||||||
|
let oracle_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo\n bar",
|
||||||
|
~"foo \nbar",
|
||||||
|
~" foo bar \nbaz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz\n\n"];
|
||||||
|
|
||||||
|
assert vec::same_length(test_strs, oracle_strs);
|
||||||
|
let mode = CompressWhitespace;
|
||||||
|
|
||||||
|
for uint::range(0, test_strs.len()) |i| {
|
||||||
|
assert transform_text(test_strs[i], mode) == oracle_strs[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transform_compress_whitespace_newline() {
|
||||||
|
let test_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo\n bar",
|
||||||
|
~"foo \nbar",
|
||||||
|
~" foo bar \nbaz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz\n\n"];
|
||||||
|
|
||||||
|
let oracle_strs : ~[~str] = ~[~" foo bar",
|
||||||
|
~"foo bar ",
|
||||||
|
~"foo bar",
|
||||||
|
~"foo bar",
|
||||||
|
~" foo bar baz",
|
||||||
|
~"foo bar baz",
|
||||||
|
~"foobarbaz "];
|
||||||
|
|
||||||
|
assert vec::same_length(test_strs, oracle_strs);
|
||||||
|
let mode = CompressWhitespaceNewline;
|
||||||
|
|
||||||
|
for uint::range(0, test_strs.len()) |i| {
|
||||||
|
assert transform_text(test_strs[i], mode) == oracle_strs[i];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue