diff --git a/.gitignore b/.gitignore index be20a11..24984c8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,8 @@ *.dSYM *.dll *.rlib -*.dummy *.exe build -Makefile *-test doc +target diff --git a/.travis.yml b/.travis.yml index 6883c15..9ae7308 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,14 @@ env: global: - secure: WVwPS+infwl0FS4/zbCOzp8TZ2WOz6CgZPHgEw6+5IN4sqgabWSRH4rnxYQPDg5G1xxNlUslOmxJ9TvlFwXxaWutIg0FFfrev1qTvRVYuXsmN6pXwIQwmRP6tUKCPx/5oAefa/oQAIjzjILQ4qPVADxuxlIAycX5PCN29Jl1xIk= -#before_install: +before_install: # - yes | sudo add-apt-repository ppa:hansjorg/rust # - sudo apt-get update -install: +# - sudo apt-get install rust-nightly - curl -O http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz - tar xfz rust-nightly-x86_64-unknown-linux-gnu.tar.gz - - (cd rust-nightly-x86_64-unknown-linux-gnu/ && sudo ./install.sh) -# - sudo apt-get install rust-nightly + - (cd rust-nightly-x86_64-unknown-linux-gnu/ && sudo ./install.sh --prefix=/usr) script: - - ./configure - make check RUSTFLAGS= - make doc branches: diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d815a6b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] + +name = "encoding" +version = "0.1.0" +authors = ["Kang Seonghoon "] + +[[lib]] + +name = "encoding" +path = "src/encoding/lib.rs" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..812f893 --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +CC ?= gcc +CXX ?= g++ +CXXFLAGS ?= +AR ?= ar +RUSTC ?= rustc +RUSTDOC ?= rustdoc +RUSTFLAGS ?= -O +EXT_DEPS ?= + +LIB_RS = src/encoding/lib.rs +LIB = ./libencoding.rlib +TEST_BIN = ./rustencoding-test +RUST_SRC = $(shell find src/encoding/. -type f -name '*.rs') + +.PHONY: all +all: $(LIB) + +$(LIB): $(LIB_RS) $(RUST_SRC) $(EXT_DEPS) + $(RUSTC) $(RUSTFLAGS) $< --out-dir $(dir $@) + +$(TEST_BIN): $(LIB_RS) $(RUST_SRC) + $(RUSTC) $(RUSTFLAGS) $< -o $@ --test + +.PHONY: doctest +doctest: $(LIB_RS) $(LIB) + $(RUSTDOC) $< -L . --test + +.PHONY: check +check: doctest $(TEST_BIN) + $(TEST_BIN) + +.PHONY: doc +doc: $(LIB_RS) $(RUST_SRC) + $(RUSTDOC) $< + +.PHONY: clean +clean: + rm -f *.o *.a *.so *.dylib *.rlib *.dll *.exe *-test + diff --git a/Makefile.in b/Makefile.in deleted file mode 100644 index afcab6a..0000000 --- a/Makefile.in +++ /dev/null @@ -1,40 +0,0 @@ -VPATH=%VPATH% - -CC ?= gcc -CXX ?= g++ -CXXFLAGS ?= -AR ?= ar -RUSTC ?= rustc -RUSTDOC ?= rustdoc -RUSTFLAGS ?= -O -EXT_DEPS ?= - -LIB_RS = src/encoding/lib.rs -RUST_SRC = $(shell find $(VPATH)/src/encoding/. -type f -name '*.rs') - -.PHONY: all -all: libencoding.dummy - -libencoding.dummy: $(LIB_RS) $(RUST_SRC) $(EXT_DEPS) - $(RUSTC) $(RUSTFLAGS) $< --out-dir . - touch $@ - -rustencoding-test: $(LIB_RS) $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< -o $@ --test - -.PHONY: doctest -doctest: $(LIB_RS) $(RUST_SRC) libencoding.dummy - $(RUSTDOC) $< -L . --test - -.PHONY: check -check: doctest rustencoding-test - ./rustencoding-test - -.PHONY: doc -doc: $(LIB_RS) $(RUST_SRC) - $(RUSTDOC) $< - -.PHONY: clean -clean: - rm -f *.o *.a *.so *.dylib *.rlib *.dll *.dummy *.exe *-test - diff --git a/configure b/configure deleted file mode 100755 index ed6c06e..0000000 --- a/configure +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -SRCDIR="$(cd $(dirname $0) && pwd)" -sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile - diff --git a/src/encoding/codec/japanese.rs b/src/encoding/codec/japanese.rs index 42a6e9c..d757bb1 100644 --- a/src/encoding/codec/japanese.rs +++ b/src/encoding/codec/japanese.rs @@ -512,13 +512,13 @@ impl Encoder for ISO2022JPEncoder { let mut st = self.st; macro_rules! ensure_ASCII( - () => (if st != ASCII { output.write_bytes(bytes!("\x1b(B")); st = ASCII; }) + () => (if st != ASCII { output.write_bytes(b"\x1b(B"); st = ASCII; }) ) macro_rules! ensure_Katakana( - () => (if st != Katakana { output.write_bytes(bytes!("\x1b(I")); st = Katakana; }) + () => (if st != Katakana { output.write_bytes(b"\x1b(I"); st = Katakana; }) ) macro_rules! ensure_Lead( - () => (if st != Lead { output.write_bytes(bytes!("\x1b$B")); st = Lead; }) + () => (if st != Lead { output.write_bytes(b"\x1b$B"); st = Lead; }) ) for ((i,j), ch) in input.index_iter() { diff --git a/src/encoding/codec/korean.rs b/src/encoding/codec/korean.rs index 80d8f2c..88fdf05 100644 --- a/src/encoding/codec/korean.rs +++ b/src/encoding/codec/korean.rs @@ -122,6 +122,7 @@ ascii_compatible_stateful_decoder! { mod windows949_tests { extern crate test; use super::Windows949Encoding; + use std::iter::range_inclusive; use testutils; use types::*; @@ -170,8 +171,7 @@ mod windows949_tests { #[test] fn test_decoder_invalid_lone_lead_immediate_test_finish() { - for i in range(0x81, 0xff) { - let i = i as u8; + for i in range_inclusive(0x81u8, 0xfe) { let mut d = Windows949Encoding.decoder(); assert_feed_ok!(d, [], [i], ""); // wait for a trail assert_finish_err!(d, ""); @@ -186,8 +186,7 @@ mod windows949_tests { #[test] fn test_decoder_invalid_lone_lead_followed_by_space() { - for i in range(0x80, 0x100) { - let i = i as u8; + for i in range_inclusive(0x80u8, 0xff) { let mut d = Windows949Encoding.decoder(); assert_feed_err!(d, [], [i], [0x20], ""); assert_finish_ok!(d, ""); @@ -196,8 +195,7 @@ mod windows949_tests { #[test] fn test_decoder_invalid_lead_followed_by_invalid_trail() { - for i in range(0x80u16, 0x100) { - let i = i as u8; + for i in range_inclusive(0x80u8, 0xff) { let mut d = Windows949Encoding.decoder(); assert_feed_err!(d, [], [i], [0x80], ""); assert_feed_err!(d, [], [i], [0xff], ""); diff --git a/src/encoding/codec/simpchinese.rs b/src/encoding/codec/simpchinese.rs index adbf0c2..e4af507 100644 --- a/src/encoding/codec/simpchinese.rs +++ b/src/encoding/codec/simpchinese.rs @@ -298,10 +298,10 @@ impl Encoder for HZEncoder { let mut escaped = self.escaped; macro_rules! ensure_escaped( - () => (if !escaped { output.write_bytes(bytes!("~{")); escaped = true; }) + () => (if !escaped { output.write_bytes(b"~{"); escaped = true; }) ) macro_rules! ensure_unescaped( - () => (if escaped { output.write_bytes(bytes!("~}")); escaped = false; }) + () => (if escaped { output.write_bytes(b"~}"); escaped = false; }) ) for ((i,j), ch) in input.index_iter() { @@ -423,14 +423,13 @@ mod hz_tests { #[test] fn test_encoder_valid() { let mut e = HZEncoding.encoder(); - assert_feed_ok!(e, "A", "", bytes!("A")); - assert_feed_ok!(e, "BC", "", bytes!("BC")); - assert_feed_ok!(e, "", "", bytes!("")); - assert_feed_ok!(e, "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd", "", - bytes!("~{VP;*HKCq92:M9z")); - assert_feed_ok!(e, "\uff21\uff22\uff23", "", bytes!("#A#B#C")); - assert_feed_ok!(e, "1\u20ac/m", "", bytes!("~}1~{\"c~}/m")); - assert_feed_ok!(e, "~<\u00a4~\u00a4>~", "", bytes!("~~<~{!h~}~~~{!h~}>~~")); + assert_feed_ok!(e, "A", "", b"A"); + assert_feed_ok!(e, "BC", "", b"BC"); + assert_feed_ok!(e, "", "", b""); + assert_feed_ok!(e, "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd", "", b"~{VP;*HKCq92:M9z"); + assert_feed_ok!(e, "\uff21\uff22\uff23", "", b"#A#B#C"); + assert_feed_ok!(e, "1\u20ac/m", "", b"~}1~{\"c~}/m"); + assert_feed_ok!(e, "~<\u00a4~\u00a4>~", "", b"~~<~{!h~}~~~{!h~}>~~"); assert_finish_ok!(e, []); } @@ -447,20 +446,20 @@ mod hz_tests { #[test] fn test_decoder_valid() { let mut d = HZEncoding.decoder(); - assert_feed_ok!(d, bytes!("A"), bytes!(""), "A"); - assert_feed_ok!(d, bytes!("BC"), bytes!(""), "BC"); - assert_feed_ok!(d, bytes!("D~~E"), bytes!("~"), "D~E"); - assert_feed_ok!(d, bytes!("~F~\nG"), bytes!("~"), "~FG"); - assert_feed_ok!(d, bytes!(""), bytes!(""), ""); - assert_feed_ok!(d, bytes!("\nH"), bytes!("~"), "H"); - assert_feed_ok!(d, bytes!("{VP~}~{;*HKCq92:M9z"), bytes!(""), + assert_feed_ok!(d, b"A", b"", "A"); + assert_feed_ok!(d, b"BC", b"", "BC"); + assert_feed_ok!(d, b"D~~E", b"~", "D~E"); + assert_feed_ok!(d, b"~F~\nG", b"~", "~FG"); + assert_feed_ok!(d, b"", b"", ""); + assert_feed_ok!(d, b"\nH", b"~", "H"); + assert_feed_ok!(d, b"{VP~}~{;*HKCq92:M9z", b"", "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd"); - assert_feed_ok!(d, bytes!(""), bytes!("#"), ""); - assert_feed_ok!(d, bytes!("A"), bytes!("~"), "\uff21"); - assert_feed_ok!(d, bytes!("~#B~~#C"), bytes!("~"), "~\uff22~\uff23"); - assert_feed_ok!(d, bytes!(""), bytes!(""), ""); - assert_feed_ok!(d, bytes!("\n#D~{#E~\n#F~{#G"), bytes!("~"), "#D\uff25#F\uff27"); - assert_feed_ok!(d, bytes!("}X~}YZ"), bytes!(""), "XYZ"); + assert_feed_ok!(d, b"", b"#", ""); + assert_feed_ok!(d, b"A", b"~", "\uff21"); + assert_feed_ok!(d, b"~#B~~#C", b"~", "~\uff22~\uff23"); + assert_feed_ok!(d, b"", b"", ""); + assert_feed_ok!(d, b"\n#D~{#E~\n#F~{#G", b"~", "#D\uff25#F\uff27"); + assert_feed_ok!(d, b"}X~}YZ", b"", "XYZ"); assert_finish_ok!(d, ""); } @@ -469,9 +468,9 @@ mod hz_tests { #[test] fn test_decoder_feed_after_finish() { let mut d = HZEncoding.decoder(); - assert_feed_ok!(d, bytes!("R;~{R;"), bytes!("R"), "R;\u4e00"); + assert_feed_ok!(d, b"R;~{R;", b"R", "R;\u4e00"); assert_finish_err!(d, ""); - assert_feed_ok!(d, bytes!("R;~{R;"), bytes!(""), "R;\u4e00"); + assert_feed_ok!(d, b"R;~{R;", b"", "R;\u4e00"); assert_finish_ok!(d, ""); } diff --git a/src/encoding/codec/utf_8.rs b/src/encoding/codec/utf_8.rs index 877d97e..0a23b40 100644 --- a/src/encoding/codec/utf_8.rs +++ b/src/encoding/codec/utf_8.rs @@ -682,7 +682,7 @@ mod tests { let s = testutils::ASCII_TEXT.as_bytes(); bencher.bytes = s.len() as u64; bencher.iter(|| test::black_box({ - str::from_utf8_lossy(s) + String::from_utf8_lossy(s) })) } } @@ -739,7 +739,7 @@ mod tests { let s = testutils::KOREAN_TEXT.as_bytes(); bencher.bytes = s.len() as u64; bencher.iter(|| test::black_box({ - str::from_utf8_lossy(s) + String::from_utf8_lossy(s) })) } } @@ -784,7 +784,7 @@ mod tests { let s = testutils::INVALID_UTF8_TEXT; bencher.bytes = s.len() as u64; bencher.iter(|| test::black_box({ - str::from_utf8_lossy(s) + String::from_utf8_lossy(s) })) } } @@ -829,7 +829,7 @@ mod tests { let s = testutils::get_external_bench_data(); bencher.bytes = s.len() as u64; bencher.iter(|| test::black_box({ - str::from_utf8_lossy(s.as_slice()) + String::from_utf8_lossy(s.as_slice()) })) } } diff --git a/src/encoding/lib.rs b/src/encoding/lib.rs index 631c41a..c1f7fa3 100644 --- a/src/encoding/lib.rs +++ b/src/encoding/lib.rs @@ -171,7 +171,7 @@ Whenever in doubt, look at the source code and specifications for detailed expla */ -#![crate_id = "encoding#0.1.0"] +#![crate_name = "encoding"] #![crate_type = "lib"] #![crate_type = "dylib"] #![crate_type = "rlib"] diff --git a/src/encoding/testutils.rs b/src/encoding/testutils.rs index 017143c..5439272 100644 --- a/src/encoding/testutils.rs +++ b/src/encoding/testutils.rs @@ -65,8 +65,8 @@ macro_rules! assert_finish_err( let output = $this.test_norm_output(output); let (err, buf) = $this.test_finish(); let upto = err.map(|e| e.upto); - assert!(Some(0) == upto, - "raw_finish should return {}, but instead returned {}", Some(0), upto); + assert!(Some(0u) == upto, + "raw_finish should return {}, but instead returned {}", Some(0u), upto); assert!(output == buf.as_slice(), "raw_finish should push {}, but instead pushed {}", output, buf.as_slice()); }) @@ -178,12 +178,12 @@ macro_rules! single_byte_tests( () => ( mod tests { use super::{forward, backward}; + use std::iter::range_inclusive; use test; #[test] fn test_correct_table() { - for i in range(128, 256) { - let i = i as u8; + for i in range_inclusive(0x80u8, 0xff) { let j = forward(i); if j != 0xffff { assert_eq!(backward(j as u32), i); } } @@ -192,8 +192,8 @@ macro_rules! single_byte_tests( #[bench] fn bench_forward_sequential_128(bencher: &mut test::Bencher) { bencher.iter(|| { - for i in range(0x80, 0x100) { - test::black_box(forward(i as u8)); + for i in range_inclusive(0x80u8, 0xff) { + test::black_box(forward(i)); } }) } @@ -217,9 +217,9 @@ macro_rules! multi_byte_tests( (make shared tests and benches with dups = $dups:expr) => ( // internal macro #[test] fn test_correct_table() { + use std::iter::range_inclusive; static DUPS: &'static [u16] = &$dups; - for i in range(0u32, 0x10000) { - let i = i as u16; + for i in range_inclusive(0u16, 0xffff) { if DUPS.contains(&i) { continue; } let j = forward(i); if j != 0xffff { assert_eq!(backward(j), i); } @@ -271,9 +271,12 @@ macro_rules! multi_byte_tests( multi_byte_tests!(make shared tests and benches with dups = $dups) + static REMAP_MIN: u16 = $remap_min; + static REMAP_MAX: u16 = $remap_max; + #[test] fn test_correct_remapping() { - for i in range::($remap_min, $remap_max+1) { + for i in range::(REMAP_MIN, REMAP_MAX+1) { let j = forward(i); if j != 0xffff { let ii = backward_remapped(j); @@ -309,24 +312,33 @@ macro_rules! multi_byte_range_tests( use super::{forward, backward}; use test; + static MIN_KEY: u32 = $minkey; + static MAX_KEY: u32 = $maxkey; + static KEY_UBOUND: u32 = $keyubound; + static MIN_VALUE: u32 = $minvalue; + static MAX_VALUE: u32 = $maxvalue; + static VALUE_UBOUND: u32 = $valueubound; + #[test] + #[allow(type_limits)] fn test_no_failure() { - for i in range::(if $minkey>0 {$minkey-1} else {0}, $maxkey+2) { + for i in range::(if MIN_KEY>0 {MIN_KEY-1} else {0}, MAX_KEY+2) { forward(i); } - for j in range::(if $minvalue>0 {$minvalue-1} else {0}, $maxvalue+2) { + for j in range::(if MIN_VALUE>0 {MIN_VALUE-1} else {0}, MAX_VALUE+2) { backward(j); } } #[test] fn test_correct_table() { - for i in range::($minkey, $maxkey+2) { + for i in range::(MIN_KEY, MAX_KEY+2) { let j = forward(i); if j == 0xffffffff { continue; } let i_ = backward(j); if i_ == 0xffffffff { continue; } - assert!(i_ == i, "backward(forward({})) = backward({}) = {} != {}", i, j, i_, i); + assert!(i_ == i, + "backward(forward({})) = backward({}) = {} != {}", i, j, i_, i); } } @@ -338,7 +350,7 @@ macro_rules! multi_byte_range_tests( test::black_box(forward(i)); } start += 0x80; - if start >= $keyubound { start = 0; } + if start >= KEY_UBOUND { start = 0; } }) } @@ -350,7 +362,7 @@ macro_rules! multi_byte_range_tests( test::black_box(backward(i)); } start += 0x80; - if start >= $valueubound { start = 0; } + if start >= VALUE_UBOUND { start = 0; } }) } } diff --git a/src/encoding/types.rs b/src/encoding/types.rs index bcc034d..52c896d 100644 --- a/src/encoding/types.rs +++ b/src/encoding/types.rs @@ -521,9 +521,9 @@ mod tests { &MyEncoding { flag: false, prohibit: '\u0080', prepend: "" }; assert_eq!(COMPAT.encode("Hello\u203d I'm fine.", EncodeNcrEscape), - Ok(Vec::from_slice(bytes!("Hello‽ I'm fine.")))); + Ok(Vec::from_slice(b"Hello‽ I'm fine."))); assert_eq!(INCOMPAT.encode("Hello\u203d I'm fine.", EncodeNcrEscape), - Ok(Vec::from_slice(bytes!("Hello‽ I'm fine.")))); + Ok(Vec::from_slice(b"Hello‽ I'm fine."))); } #[test] @@ -535,9 +535,9 @@ mod tests { // this should behave incorrectly as the encoding broke the assumption. assert_eq!(COMPAT.encode("Hello\u203d I'm fine.", EncodeNcrEscape), - Ok(Vec::from_slice(bytes!("He*l*l*o‽* *I*'*m* *f*i*n*e.")))); + Ok(Vec::from_slice(b"He*l*l*o‽* *I*'*m* *f*i*n*e."))); assert_eq!(INCOMPAT.encode("Hello\u203d I'm fine.", EncodeNcrEscape), - Ok(Vec::from_slice(bytes!("He*l*l*o*&*#*8*2*5*3*;* *I*'*m* *f*i*n*e.")))); + Ok(Vec::from_slice(b"He*l*l*o*&*#*8*2*5*3*;* *I*'*m* *f*i*n*e."))); } #[test]