* Use ... instead of .. when matching a range, in preparation for
  rust-lang/rust#17584 (may not be complete).
* Use the new slicing syntax (a[], a[x..y]) if possible. (Currently
  String::as_slice() is blocked by rust-lang/rust#17502.)
* Change deprecated String::push_char() to String::push().
This commit is contained in:
kennytm 2014-09-28 00:59:20 +08:00
parent fa40eec147
commit bca618fe12
6 changed files with 46 additions and 47 deletions

View file

@ -40,17 +40,17 @@ impl Bits {
let b = self.bit_offset + n;
match (self.bit_offset, b) {
(0, 0..8) => {
(0, 0...8) => {
self.data.push((number << (8-b)) as u8);
}
(0, _) => {
self.data.push((number >> (b-8)) as u8);
self.data.push((number << (16-b)) as u8);
}
(_, 0..8) => {
(_, 0...8) => {
*self.data.last_mut().unwrap() |= (number << (8-b)) as u8;
}
(_, 9..16) => {
(_, 9...16) => {
*self.data.last_mut().unwrap() |= (number >> (b-8)) as u8;
self.data.push((number << (16-b)) as u8);
}
@ -234,14 +234,14 @@ impl Bits {
self.reserve_additional(12); // assume the common case that eci_designator <= 127.
try!(self.push_mode_indicator(Eci));
match eci_designator {
0..127 => {
0...127 => {
self.push_number(8, eci_designator as u16);
}
128..16383 => {
128...16383 => {
self.push_number(2, 0b10);
self.push_number(14, eci_designator as u16);
}
16384..999999 => {
16384...999999 => {
self.push_number(3, 0b110);
self.push_number(5, (eci_designator >> 16) as u16);
self.push_number(16, (eci_designator & 0xffff) as u16);
@ -387,8 +387,8 @@ mod numeric_tests {
#[inline]
fn alphanumeric_digit(character: u8) -> u16 {
match character {
b'0' .. b'9' => (character - b'0') as u16,
b'A' .. b'Z' => (character - b'A') as u16 + 10,
b'0' ... b'9' => (character - b'0') as u16,
b'A' ... b'Z' => (character - b'A') as u16 + 10,
b' ' => 36,
b'$' => 37,
b'%' => 38,
@ -666,7 +666,7 @@ impl Bits {
};
let cur_length = self.len();
let data_length = try!(self.version.fetch(ec_level, DATA_LENGTHS.as_slice()));
let data_length = try!(self.version.fetch(ec_level, DATA_LENGTHS[]));
if cur_length > data_length {
return Err(DataTooLong);
}
@ -759,7 +759,7 @@ impl Bits {
data: &[u8],
mut segments_iter: I) -> QrResult<()> {
for segment in segments_iter {
let slice = data.slice(segment.begin, segment.end);
let slice = data[segment.begin..segment.end];
try!(match segment.mode {
Numeric => self.push_numeric_data(slice),
Alphanumeric => self.push_alphanumeric_data(slice),
@ -829,8 +829,8 @@ pub fn encode_auto(data: &[u8], ec_level: ErrorCorrectionLevel) -> QrResult<Bits
let segments = Parser::new(data).collect::<Vec<Segment>>();
for version in [Version(9), Version(26), Version(40)].iter() {
let opt_segments = Optimizer::new(segments.iter().map(|s| *s), *version).collect::<Vec<Segment>>();
let total_len = total_encoded_len(opt_segments.as_slice(), *version);
let data_capacity = version.fetch(ec_level, DATA_LENGTHS.as_slice()).unwrap();
let total_len = total_encoded_len(opt_segments[], *version);
let data_capacity = version.fetch(ec_level, DATA_LENGTHS[]).unwrap();
if total_len <= data_capacity {
let min_version = find_min_version(total_len, ec_level);
let mut bits = Bits::new(min_version);

View file

@ -112,9 +112,9 @@ impl Canvas {
let width = self.width;
let mut res = String::with_capacity((width * (width + 1)) as uint);
for y in range(0, width) {
res.push_char('\n');
res.push('\n');
for x in range(0, width) {
res.push_char(match self.get(x, y) {
res.push(match self.get(x, y) {
Empty => '?',
Light => '.',
Dark => '#',
@ -654,7 +654,7 @@ impl Canvas {
/// Draws the version information patterns.
fn draw_version_info_patterns(&mut self) {
match self.version {
MicroVersion(_) | Version(1..6) => { return; }
MicroVersion(_) | Version(1...6) => { return; }
Version(a) => {
let version_info = VERSION_INFOS[(a - 7) as uint] << 14;
self.draw_number(version_info, Dark, Light, VERSION_INFO_COORDS_BL);

View file

@ -28,7 +28,7 @@ pub fn create_error_correction_code(data: &[u8], ec_code_size: uint) -> Vec<u8>
}
let log_lead_coeff = LOG_TABLE[lead_coeff] as uint;
for (u, v) in res.slice_from_mut(i+1).iter_mut().zip(log_den.iter()) {
for (u, v) in res[mut i+1..].iter_mut().zip(log_den.iter()) {
*u ^= EXP_TABLE[((*v as uint + log_lead_coeff) % 255) as uint];
}
}
@ -43,19 +43,19 @@ mod ec_tests {
#[test]
fn test_poly_mod_1() {
let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11", 10);
assert_eq!(res.as_slice(), b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
assert_eq!(res[], b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
}
#[test]
fn test_poly_mod_2() {
let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec", 13);
assert_eq!(res.as_slice(), b"\xa8H\x16R\xd96\x9c\x00.\x0f\xb4z\x10");
assert_eq!(res[], b"\xa8H\x16R\xd96\x9c\x00.\x0f\xb4z\x10");
}
#[test]
fn test_poly_mod_3() {
let res = create_error_correction_code(b"CUF\x86W&U\xc2w2\x06\x12\x06g&", 18);
assert_eq!(res.as_slice(), b"\xd5\xc7\x0b-s\xf7\xf1\xdf\xe5\xf8\x9au\x9aoV\xa1o'");
assert_eq!(res[], b"\xd5\xc7\x0b-s\xf7\xf1\xdf\xe5\xf8\x9au\x9aoV\xa1o'");
}
}
@ -118,7 +118,7 @@ fn interleave<T: Copy, V>(blocks: &Vec<V>) -> Vec<T>
#[test]
fn test_interleave() {
let res = interleave(&vec![b"1234", b"5678", b"abcdef", b"ghijkl"]);
assert_eq!(res.as_slice(), b"15ag26bh37ci48djekfl");
assert_eq!(res[], b"15ag26bh37ci48djekfl");
}
//}}}
@ -131,7 +131,7 @@ pub fn construct_codewords(rawbits: &[u8],
version: QrVersion,
ec_level: ErrorCorrectionLevel) -> QrResult<(Vec<u8>, Vec<u8>)> {
let (block_1_size, block_1_count, block_2_size, block_2_count) =
try!(version.fetch(ec_level, DATA_BYTES_PER_BLOCK.as_slice()));
try!(version.fetch(ec_level, DATA_BYTES_PER_BLOCK[]));
let blocks_count = block_1_count + block_2_count;
let block_1_end = block_1_size * block_1_count;
@ -141,13 +141,13 @@ pub fn construct_codewords(rawbits: &[u8],
// Divide the data into blocks.
let mut blocks = Vec::with_capacity(blocks_count);
blocks.extend(rawbits.slice_to(block_1_end).chunks(block_1_size));
blocks.extend(rawbits[..block_1_end].chunks(block_1_size));
if block_2_size > 0 {
blocks.extend(rawbits.slice_from(block_1_end).chunks(block_2_size));
blocks.extend(rawbits[block_1_end..].chunks(block_2_size));
}
// Generate EC codes.
let ec_bytes = try!(version.fetch(ec_level, EC_BYTES_PER_BLOCK.as_slice()));
let ec_bytes = try!(version.fetch(ec_level, EC_BYTES_PER_BLOCK[]));
let ec_codes = blocks.iter()
.map(|block| create_error_correction_code(*block, ec_bytes))
.collect::<Vec<Vec<u8>>>();
@ -167,17 +167,17 @@ mod construct_codewords_test {
fn test_add_ec_simple() {
let msg = b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11";
let (blocks_vec, ec_vec) = construct_codewords(msg, Version(1), M).unwrap();
assert_eq!(blocks_vec.as_slice(), msg);
assert_eq!(ec_vec.as_slice(), b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
assert_eq!(blocks_vec[], msg);
assert_eq!(ec_vec[], b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
}
#[test]
fn test_add_ec_complex() {
let msg = b"CUF\x86W&U\xc2w2\x06\x12\x06g&\xf6\xf6B\x07v\x86\xf2\x07&V\x16\xc6\xc7\x92\x06\xb6\xe6\xf7w2\x07v\x86W&R\x06\x86\x972\x07F\xf7vV\xc2\x06\x972\x10\xec\x11\xec\x11\xec\x11\xec";
let (blocks_vec, ec_vec) = construct_codewords(msg, Version(5), Q).unwrap();
assert_eq!(blocks_vec.as_slice(),
assert_eq!(blocks_vec[],
b"C\xf6\xb6FU\xf6\xe6\xf7FB\xf7v\x86\x07wVWv2\xc2&\x86\x07\x06U\xf2v\x97\xc2\x07\x862w&W\x102V&\xec\x06\x16R\x11\x12\xc6\x06\xec\x06\xc7\x86\x11g\x92\x97\xec&\x062\x11\x07\xec");
assert_eq!(ec_vec.as_slice(),
assert_eq!(ec_vec[],
b"\xd5W\x94\xeb\xc7\xcct\x9f\x0b`\xb1\x05-<\xd4\xads\xcaL\x18\xf7\xb6\x85\x93\xf1|K;\xdf\x9d\xf2!\xe5\xc8\xeej\xf8\x86L(\x9a\x1b\xc3\xffu\x81\xe6\xac\x9a\xd1\xbdRo\x11\n\x02V\xa3l\x83\xa1\xa3\xf0 ox\xc0\xb2'\x85\x8d\xec");
}
}

View file

@ -117,11 +117,10 @@ impl QrCode {
ec_level: ErrorCorrectionLevel) -> QrResult<QrCode> {
let version = bits.version();
let data = bits.into_bytes();
let (encoded_data, ec_data) = try!(ec::construct_codewords(data.as_slice(),
version, ec_level));
let (encoded_data, ec_data) = try!(ec::construct_codewords(data[], version, ec_level));
let mut canvas = canvas::Canvas::new(version, ec_level);
canvas.draw_all_functional_patterns();
canvas.draw_data(encoded_data.as_slice(), ec_data.as_slice());
canvas.draw_data(encoded_data[], ec_data[]);
let canvas = canvas.apply_best_mask();
Ok(QrCode {
content: canvas.to_bools(),
@ -155,9 +154,9 @@ impl QrCode {
let mut k = 0;
let mut res = String::with_capacity(width * (width + 1));
for _ in range(0, width) {
res.push_char('\n');
res.push('\n');
for _ in range(0, width) {
res.push_char(if self.content[k] { on_char } else { off_char });
res.push(if self.content[k] { on_char } else { off_char });
k += 1;
}
}

View file

@ -314,15 +314,15 @@ mod optimize_tests {
use types::{Numeric, Alphanumeric, Byte, Kanji, QrVersion, Version, MicroVersion};
fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: QrVersion) {
let prev_len = total_encoded_len(given.as_slice(), version);
let prev_len = total_encoded_len(given[], version);
let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::<Vec<Segment>>();
let new_len = total_encoded_len(opt_segs.as_slice(), version);
let new_len = total_encoded_len(opt_segs[], version);
if given != opt_segs {
assert!(prev_len > new_len, "{} > {}", prev_len, new_len);
}
assert!(opt_segs == expected,
"Optimization gave something better: {} < {} ({})",
new_len, total_encoded_len(expected.as_slice(), version), opt_segs);
new_len, total_encoded_len(expected[], version), opt_segs);
}
#[test]
@ -500,14 +500,14 @@ impl ExclusiveCharacterSet {
/// Determines which character set a byte is in.
fn from_u8(c: u8) -> ExclusiveCharacterSet {
match c {
0x20 | 0x24 | 0x25 | 0x2a | 0x2b | 0x2d .. 0x2f | 0x3a => ESymbol,
0x30 .. 0x39 => ENumeric,
0x41 .. 0x5a => EAlpha,
0x81 .. 0x9f => EKanjiHi1,
0xe0 .. 0xea => EKanjiHi2,
0x20 | 0x24 | 0x25 | 0x2a | 0x2b | 0x2d ... 0x2f | 0x3a => ESymbol,
0x30 ... 0x39 => ENumeric,
0x41 ... 0x5a => EAlpha,
0x81 ... 0x9f => EKanjiHi1,
0xe0 ... 0xea => EKanjiHi2,
0xeb => EKanjiHi3,
0x40 | 0x5b .. 0x7e | 0x80 | 0xa0 .. 0xbf => EKanjiLo1,
0xc0 .. 0xdf | 0xec .. 0xfc => EKanjiLo2,
0x40 | 0x5b ... 0x7e | 0x80 | 0xa0 ... 0xbf => EKanjiLo1,
0xc0 ... 0xdf | 0xec ... 0xfc => EKanjiLo2,
_ => EByte
}
}

View file

@ -99,8 +99,8 @@ impl QrVersion {
where T: PartialEq + Default + Copy
{
match *self {
Version(v @ 1..40) => Ok(table[v as uint - 1][ec_level as uint]),
MicroVersion(v @ 1..4) => {
Version(v @ 1...40) => Ok(table[v as uint - 1][ec_level as uint]),
MicroVersion(v @ 1...4) => {
let obj = table[v as uint + 39][ec_level as uint];
if obj != Default::default() {
Ok(obj)
@ -173,13 +173,13 @@ impl Mode {
Kanji => a,
}
}
Version(1..9) => match *self {
Version(1...9) => match *self {
Numeric => 10,
Alphanumeric => 9,
Byte => 8,
Kanji => 8,
},
Version(10..26) => match *self {
Version(10...26) => match *self {
Numeric => 12,
Alphanumeric => 11,
Byte => 16,