diff --git a/mk/crates.mk b/mk/crates.mk index fd41666275b50..00c00b3d3595b 100644 --- a/mk/crates.mk +++ b/mk/crates.mk @@ -49,11 +49,11 @@ # automatically generated for all stage/host/target combinations. ################################################################################ -TARGET_CRATES := libc std green native flate arena glob term semver \ - uuid serialize sync getopts collections num test time rand \ - url log regex graphviz core rbml rlibc alloc rustrt \ +TARGET_CRATES := libc std green native flate arena term \ + serialize sync getopts collections test time rand \ + log regex graphviz core rbml rlibc alloc rustrt \ unicode -HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros \ +HOST_CRATES := syntax rustc rustdoc regex_macros fmt_macros \ rustc_llvm rustc_back CRATES := $(TARGET_CRATES) $(HOST_CRATES) TOOLS := compiletest rustdoc rustc @@ -83,18 +83,13 @@ DEPS_glob := std DEPS_serialize := std log DEPS_rbml := std log serialize DEPS_term := std log -DEPS_semver := std -DEPS_uuid := std serialize DEPS_sync := core alloc rustrt collections DEPS_getopts := std DEPS_collections := core alloc unicode -DEPS_fourcc := rustc syntax std -DEPS_hexfloat := rustc syntax std DEPS_num := std DEPS_test := std getopts serialize rbml term time regex native:rust_test_helpers DEPS_time := std serialize DEPS_rand := core -DEPS_url := std DEPS_log := std regex DEPS_regex := std DEPS_regex_macros = rustc syntax std regex diff --git a/src/libfourcc/lib.rs b/src/libfourcc/lib.rs deleted file mode 100644 index ffa72e6d7958e..0000000000000 --- a/src/libfourcc/lib.rs +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! -Syntax extension to generate FourCCs. - -Once loaded, fourcc!() is called with a single 4-character string, -and an optional ident that is either `big`, `little`, or `target`. -The ident represents endianness, and specifies in which direction -the characters should be read. If the ident is omitted, it is assumed -to be `big`, i.e. left-to-right order. It returns a u32. - -# Examples - -To load the extension and use it: - -```rust,ignore -#[phase(plugin)] -extern crate fourcc; - -fn main() { - let val = fourcc!("\xC0\xFF\xEE!"); - assert_eq!(val, 0xC0FFEE21u32); - let little_val = fourcc!("foo ", little); - assert_eq!(little_val, 0x21EEFFC0u32); -} -``` - -# References - -* [Wikipedia: FourCC](http://en.wikipedia.org/wiki/FourCC) - -*/ - -#![crate_name = "fourcc"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/fourcc"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/")] - -#![feature(plugin_registrar)] - -extern crate syntax; -extern crate rustc; - -use syntax::ast; -use syntax::attr::contains; -use syntax::codemap::{Span, mk_sp}; -use syntax::ext::base; -use syntax::ext::base::{ExtCtxt, MacExpr}; -use syntax::ext::build::AstBuilder; -use syntax::parse::token; -use syntax::parse::token::InternedString; -use syntax::ptr::P; -use rustc::plugin::Registry; - -#[plugin_registrar] -pub fn plugin_registrar(reg: &mut Registry) { - reg.register_macro("fourcc", expand_syntax_ext); -} - -pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) - -> Box { - let (expr, endian) = parse_tts(cx, tts); - - let little = match endian { - None => false, - Some(Ident{ident, span}) => match token::get_ident(ident).get() { - "little" => true, - "big" => false, - "target" => target_endian_little(cx, sp), - _ => { - cx.span_err(span, "invalid endian directive in fourcc!"); - target_endian_little(cx, sp) - } - } - }; - - let s = match expr.node { - // expression is a literal - ast::ExprLit(ref lit) => match lit.node { - // string literal - ast::LitStr(ref s, _) => { - if s.get().char_len() != 4 { - cx.span_err(expr.span, "string literal with len != 4 in fourcc!"); - } - s - } - _ => { - cx.span_err(expr.span, "unsupported literal in fourcc!"); - return base::DummyResult::expr(sp) - } - }, - _ => { - cx.span_err(expr.span, "non-literal in fourcc!"); - return base::DummyResult::expr(sp) - } - }; - - let mut val = 0u32; - for codepoint in s.get().chars().take(4) { - let byte = if codepoint as u32 > 0xFF { - cx.span_err(expr.span, "fourcc! literal character out of range 0-255"); - 0u8 - } else { - codepoint as u8 - }; - - val = if little { - (val >> 8) | ((byte as u32) << 24) - } else { - (val << 8) | (byte as u32) - }; - } - let e = cx.expr_lit(sp, ast::LitInt(val as u64, ast::UnsignedIntLit(ast::TyU32))); - MacExpr::new(e) -} - -struct Ident { - ident: ast::Ident, - span: Span -} - -fn parse_tts(cx: &ExtCtxt, - tts: &[ast::TokenTree]) -> (P, Option) { - let p = &mut cx.new_parser_from_tts(tts); - let ex = p.parse_expr(); - let id = if p.token == token::EOF { - None - } else { - p.expect(&token::COMMA); - let lo = p.span.lo; - let ident = p.parse_ident(); - let hi = p.last_span.hi; - Some(Ident{ident: ident, span: mk_sp(lo, hi)}) - }; - if p.token != token::EOF { - p.unexpected(); - } - (ex, id) -} - -fn target_endian_little(cx: &ExtCtxt, sp: Span) -> bool { - let meta = cx.meta_name_value(sp, InternedString::new("target_endian"), - ast::LitStr(InternedString::new("little"), ast::CookedStr)); - contains(cx.cfg().as_slice(), &*meta) -} - -// FIXME (10872): This is required to prevent an LLVM assert on Windows -#[test] -fn dummy_test() { } diff --git a/src/libglob/lib.rs b/src/libglob/lib.rs deleted file mode 100644 index 0313c22933cfa..0000000000000 --- a/src/libglob/lib.rs +++ /dev/null @@ -1,893 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! - * Support for matching file paths against Unix shell style patterns. - * - * The `glob` and `glob_with` functions, in concert with the `Paths` - * type, allow querying the filesystem for all files that match a particular - * pattern - just like the libc `glob` function (for an example see the `glob` - * documentation). The methods on the `Pattern` type provide functionality - * for checking if individual paths match a particular pattern - in a similar - * manner to the libc `fnmatch` function - * - * For consistency across platforms, and for Windows support, this module - * is implemented entirely in Rust rather than deferring to the libc - * `glob`/`fnmatch` functions. - */ - -#![crate_name = "glob"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/glob"] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/", - html_playground_url = "http://play.rust-lang.org/")] -#![allow(deprecated)] - -use std::cell::Cell; -use std::{cmp, os, path}; -use std::io::fs::PathExtensions; -use std::io::fs; -use std::path::is_sep; -use std::string::String; - -/** - * An iterator that yields Paths from the filesystem that match a particular - * pattern - see the `glob` function for more details. - */ -pub struct Paths { - dir_patterns: Vec, - require_dir: bool, - options: MatchOptions, - todo: Vec<(Path,uint)>, -} - -/// -/// Return an iterator that produces all the Paths that match the given pattern, -/// which may be absolute or relative to the current working directory. -/// -/// This method uses the default match options and is equivalent to calling -/// `glob_with(pattern, MatchOptions::new())`. Use `glob_with` directly if you -/// want to use non-default match options. -/// -/// # Example -/// -/// Consider a directory `/media/pictures` containing only the files `kittens.jpg`, -/// `puppies.jpg` and `hamsters.gif`: -/// -/// ```rust -/// # #![allow(deprecated)] -/// use glob::glob; -/// -/// for path in glob("/media/pictures/*.jpg") { -/// println!("{}", path.display()); -/// } -/// ``` -/// -/// The above code will print: -/// -/// ```ignore -/// /media/pictures/kittens.jpg -/// /media/pictures/puppies.jpg -/// ``` -/// -pub fn glob(pattern: &str) -> Paths { - glob_with(pattern, MatchOptions::new()) -} - -/** - * Return an iterator that produces all the Paths that match the given pattern, - * which may be absolute or relative to the current working directory. - * - * This function accepts Unix shell style patterns as described by `Pattern::new(..)`. - * The options given are passed through unchanged to `Pattern::matches_with(..)` with - * the exception that `require_literal_separator` is always set to `true` regardless of the - * value passed to this function. - * - * Paths are yielded in alphabetical order, as absolute paths. - */ -pub fn glob_with(pattern: &str, options: MatchOptions) -> Paths { - #[cfg(windows)] - fn check_windows_verbatim(p: &Path) -> bool { path::windows::is_verbatim(p) } - #[cfg(not(windows))] - fn check_windows_verbatim(_: &Path) -> bool { false } - - // calculate root this way to handle volume-relative Windows paths correctly - let mut root = os::getcwd(); - let pat_root = Path::new(pattern).root_path(); - if pat_root.is_some() { - if check_windows_verbatim(pat_root.as_ref().unwrap()) { - // FIXME: How do we want to handle verbatim paths? I'm inclined to return nothing, - // since we can't very well find all UNC shares with a 1-letter server name. - return Paths { - dir_patterns: Vec::new(), - require_dir: false, - options: options, - todo: Vec::new(), - }; - } - root.push(pat_root.as_ref().unwrap()); - } - - let root_len = pat_root.map_or(0u, |p| p.as_vec().len()); - let dir_patterns = pattern.slice_from(cmp::min(root_len, pattern.len())) - .split_terminator(is_sep) - .map(|s| Pattern::new(s)) - .collect::>(); - let require_dir = pattern.chars().next_back().map(is_sep) == Some(true); - - let mut todo = Vec::new(); - if dir_patterns.len() > 0 { - // Shouldn't happen, but we're using -1 as a special index. - assert!(dir_patterns.len() < -1 as uint); - - fill_todo(&mut todo, dir_patterns.as_slice(), 0, &root, options); - } - - Paths { - dir_patterns: dir_patterns, - require_dir: require_dir, - options: options, - todo: todo, - } -} - -impl Iterator for Paths { - - fn next(&mut self) -> Option { - loop { - if self.dir_patterns.is_empty() || self.todo.is_empty() { - return None; - } - - let (path,idx) = self.todo.pop().unwrap(); - // idx -1: was already checked by fill_todo, maybe path was '.' or - // '..' that we can't match here because of normalization. - if idx == -1 as uint { - if self.require_dir && !path.is_dir() { continue; } - return Some(path); - } - let ref pattern = self.dir_patterns[idx]; - - if pattern.matches_with(match path.filename_str() { - // this ugly match needs to go here to avoid a borrowck error - None => { - // FIXME (#9639): How do we handle non-utf8 filenames? Ignore them for now - // Ideally we'd still match them against a * - continue; - } - Some(x) => x - }, self.options) { - if idx == self.dir_patterns.len() - 1 { - // it is not possible for a pattern to match a directory *AND* its children - // so we don't need to check the children - - if !self.require_dir || path.is_dir() { - return Some(path); - } - } else { - fill_todo(&mut self.todo, self.dir_patterns.as_slice(), - idx + 1, &path, self.options); - } - } - } - } - -} - -fn list_dir_sorted(path: &Path) -> Option> { - match fs::readdir(path) { - Ok(mut children) => { - children.sort_by(|p1, p2| p2.filename().cmp(&p1.filename())); - Some(children.into_iter().collect()) - } - Err(..) => None - } -} - -/** - * A compiled Unix shell style pattern. - */ -#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -pub struct Pattern { - tokens: Vec, -} - -#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -enum PatternToken { - Char(char), - AnyChar, - AnySequence, - AnyWithin(Vec ), - AnyExcept(Vec ) -} - -#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -enum CharSpecifier { - SingleChar(char), - CharRange(char, char) -} - -#[deriving(PartialEq)] -enum MatchResult { - Match, - SubPatternDoesntMatch, - EntirePatternDoesntMatch -} - -impl Pattern { - - /** - * This function compiles Unix shell style patterns: `?` matches any single - * character, `*` matches any (possibly empty) sequence of characters and - * `[...]` matches any character inside the brackets, unless the first - * character is `!` in which case it matches any character except those - * between the `!` and the `]`. Character sequences can also specify ranges - * of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any - * character between 0 and 9 inclusive. - * - * The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets - * (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then - * it is interpreted as being part of, rather then ending, the character - * set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively. - * The `-` character can be specified inside a character sequence pattern by - * placing it at the start or the end, e.g. `[abc-]`. - * - * When a `[` does not have a closing `]` before the end of the string then - * the `[` will be treated literally. - */ - pub fn new(pattern: &str) -> Pattern { - - let chars = pattern.chars().collect::>(); - let mut tokens = Vec::new(); - let mut i = 0; - - while i < chars.len() { - match chars[i] { - '?' => { - tokens.push(AnyChar); - i += 1; - } - '*' => { - // *, **, ***, ****, ... are all equivalent - while i < chars.len() && chars[i] == '*' { - i += 1; - } - tokens.push(AnySequence); - } - '[' => { - - if i <= chars.len() - 4 && chars[i + 1] == '!' { - match chars.slice_from(i + 3).position_elem(&']') { - None => (), - Some(j) => { - let chars = chars.slice(i + 2, i + 3 + j); - let cs = parse_char_specifiers(chars); - tokens.push(AnyExcept(cs)); - i += j + 4; - continue; - } - } - } - else if i <= chars.len() - 3 && chars[i + 1] != '!' { - match chars.slice_from(i + 2).position_elem(&']') { - None => (), - Some(j) => { - let cs = parse_char_specifiers(chars.slice(i + 1, i + 2 + j)); - tokens.push(AnyWithin(cs)); - i += j + 3; - continue; - } - } - } - - // if we get here then this is not a valid range pattern - tokens.push(Char('[')); - i += 1; - } - c => { - tokens.push(Char(c)); - i += 1; - } - } - } - - Pattern { tokens: tokens } - } - - /** - * Escape metacharacters within the given string by surrounding them in - * brackets. The resulting string will, when compiled into a `Pattern`, - * match the input string and nothing else. - */ - pub fn escape(s: &str) -> String { - let mut escaped = String::new(); - for c in s.chars() { - match c { - // note that ! does not need escaping because it is only special inside brackets - '?' | '*' | '[' | ']' => { - escaped.push_char('['); - escaped.push_char(c); - escaped.push_char(']'); - } - c => { - escaped.push_char(c); - } - } - } - escaped - } - - /** - * Return if the given `str` matches this `Pattern` using the default - * match options (i.e. `MatchOptions::new()`). - * - * # Example - * - * ```rust - * #![allow(deprecated)] - * use glob::Pattern; - * - * assert!(Pattern::new("c?t").matches("cat")); - * assert!(Pattern::new("k[!e]tteh").matches("kitteh")); - * assert!(Pattern::new("d*g").matches("doog")); - * ``` - */ - pub fn matches(&self, str: &str) -> bool { - self.matches_with(str, MatchOptions::new()) - } - - /** - * Return if the given `Path`, when converted to a `str`, matches this `Pattern` - * using the default match options (i.e. `MatchOptions::new()`). - */ - pub fn matches_path(&self, path: &Path) -> bool { - // FIXME (#9639): This needs to handle non-utf8 paths - path.as_str().map_or(false, |s| { - self.matches(s) - }) - } - - /** - * Return if the given `str` matches this `Pattern` using the specified match options. - */ - pub fn matches_with(&self, str: &str, options: MatchOptions) -> bool { - self.matches_from(None, str, 0, options) == Match - } - - /** - * Return if the given `Path`, when converted to a `str`, matches this `Pattern` - * using the specified match options. - */ - pub fn matches_path_with(&self, path: &Path, options: MatchOptions) -> bool { - // FIXME (#9639): This needs to handle non-utf8 paths - path.as_str().map_or(false, |s| { - self.matches_with(s, options) - }) - } - - fn matches_from(&self, - prev_char: Option, - mut file: &str, - i: uint, - options: MatchOptions) -> MatchResult { - - let prev_char = Cell::new(prev_char); - - let require_literal = |c| { - (options.require_literal_separator && is_sep(c)) || - (options.require_literal_leading_dot && c == '.' - && is_sep(prev_char.get().unwrap_or('/'))) - }; - - for (ti, token) in self.tokens.slice_from(i).iter().enumerate() { - match *token { - AnySequence => { - loop { - match self.matches_from(prev_char.get(), file, i + ti + 1, options) { - SubPatternDoesntMatch => (), // keep trying - m => return m, - } - - if file.is_empty() { - return EntirePatternDoesntMatch; - } - - let (some_c, next) = file.slice_shift_char(); - if require_literal(some_c.unwrap()) { - return SubPatternDoesntMatch; - } - prev_char.set(some_c); - file = next; - } - } - _ => { - if file.is_empty() { - return EntirePatternDoesntMatch; - } - - let (some_c, next) = file.slice_shift_char(); - let c = some_c.unwrap(); - let matches = match *token { - AnyChar => { - !require_literal(c) - } - AnyWithin(ref specifiers) => { - !require_literal(c) && - in_char_specifiers(specifiers.as_slice(), - c, - options) - } - AnyExcept(ref specifiers) => { - !require_literal(c) && - !in_char_specifiers(specifiers.as_slice(), - c, - options) - } - Char(c2) => { - chars_eq(c, c2, options.case_sensitive) - } - AnySequence => { - unreachable!() - } - }; - if !matches { - return SubPatternDoesntMatch; - } - prev_char.set(some_c); - file = next; - } - } - } - - if file.is_empty() { - Match - } else { - SubPatternDoesntMatch - } - } - -} - -// Fills `todo` with paths under `path` to be matched by `patterns[idx]`, -// special-casing patterns to match `.` and `..`, and avoiding `readdir()` -// calls when there are no metacharacters in the pattern. -fn fill_todo(todo: &mut Vec<(Path, uint)>, patterns: &[Pattern], idx: uint, path: &Path, - options: MatchOptions) { - // convert a pattern that's just many Char(_) to a string - fn pattern_as_str(pattern: &Pattern) -> Option { - let mut s = String::new(); - for token in pattern.tokens.iter() { - match *token { - Char(c) => s.push_char(c), - _ => return None - } - } - return Some(s); - } - - let add = |todo: &mut Vec<_>, next_path: Path| { - if idx + 1 == patterns.len() { - // We know it's good, so don't make the iterator match this path - // against the pattern again. In particular, it can't match - // . or .. globs since these never show up as path components. - todo.push((next_path, -1 as uint)); - } else { - fill_todo(todo, patterns, idx + 1, &next_path, options); - } - }; - - let pattern = &patterns[idx]; - - match pattern_as_str(pattern) { - Some(s) => { - // This pattern component doesn't have any metacharacters, so we - // don't need to read the current directory to know where to - // continue. So instead of passing control back to the iterator, - // we can just check for that one entry and potentially recurse - // right away. - let special = "." == s.as_slice() || ".." == s.as_slice(); - let next_path = path.join(s.as_slice()); - if (special && path.is_dir()) || (!special && next_path.exists()) { - add(todo, next_path); - } - }, - None => { - match list_dir_sorted(path) { - Some(entries) => { - todo.extend(entries.into_iter().map(|x|(x, idx))); - - // Matching the special directory entries . and .. that refer to - // the current and parent directory respectively requires that - // the pattern has a leading dot, even if the `MatchOptions` field - // `require_literal_leading_dot` is not set. - if pattern.tokens.len() > 0 && pattern.tokens[0] == Char('.') { - for &special in [".", ".."].iter() { - if pattern.matches_with(special, options) { - add(todo, path.join(special)); - } - } - } - } - None => {} - } - } - } -} - -fn parse_char_specifiers(s: &[char]) -> Vec { - let mut cs = Vec::new(); - let mut i = 0; - while i < s.len() { - if i + 3 <= s.len() && s[i + 1] == '-' { - cs.push(CharRange(s[i], s[i + 2])); - i += 3; - } else { - cs.push(SingleChar(s[i])); - i += 1; - } - } - cs -} - -fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: MatchOptions) -> bool { - - for &specifier in specifiers.iter() { - match specifier { - SingleChar(sc) => { - if chars_eq(c, sc, options.case_sensitive) { - return true; - } - } - CharRange(start, end) => { - - // FIXME: work with non-ascii chars properly (issue #1347) - if !options.case_sensitive && c.is_ascii() && start.is_ascii() && end.is_ascii() { - - let start = start.to_ascii().to_lowercase(); - let end = end.to_ascii().to_lowercase(); - - let start_up = start.to_uppercase(); - let end_up = end.to_uppercase(); - - // only allow case insensitive matching when - // both start and end are within a-z or A-Z - if start != start_up && end != end_up { - let start = start.to_char(); - let end = end.to_char(); - let c = c.to_ascii().to_lowercase().to_char(); - if c >= start && c <= end { - return true; - } - } - } - - if c >= start && c <= end { - return true; - } - } - } - } - - false -} - -/// A helper function to determine if two chars are (possibly case-insensitively) equal. -fn chars_eq(a: char, b: char, case_sensitive: bool) -> bool { - if cfg!(windows) && path::windows::is_sep(a) && path::windows::is_sep(b) { - true - } else if !case_sensitive && a.is_ascii() && b.is_ascii() { - // FIXME: work with non-ascii chars properly (issue #1347) - a.to_ascii().eq_ignore_case(b.to_ascii()) - } else { - a == b - } -} - -/** - * Configuration options to modify the behaviour of `Pattern::matches_with(..)` - */ -#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -pub struct MatchOptions { - - /** - * Whether or not patterns should be matched in a case-sensitive manner. This - * currently only considers upper/lower case relationships between ASCII characters, - * but in future this might be extended to work with Unicode. - */ - pub case_sensitive: bool, - - /** - * If this is true then path-component separator characters (e.g. `/` on Posix) - * must be matched by a literal `/`, rather than by `*` or `?` or `[...]` - */ - pub require_literal_separator: bool, - - /** - * If this is true then paths that contain components that start with a `.` will - * not match unless the `.` appears literally in the pattern: `*`, `?` or `[...]` - * will not match. This is useful because such files are conventionally considered - * hidden on Unix systems and it might be desirable to skip them when listing files. - */ - pub require_literal_leading_dot: bool -} - -impl MatchOptions { - - /** - * Constructs a new `MatchOptions` with default field values. This is used - * when calling functions that do not take an explicit `MatchOptions` parameter. - * - * This function always returns this value: - * - * ```rust,ignore - * MatchOptions { - * case_sensitive: true, - * require_literal_separator: false. - * require_literal_leading_dot: false - * } - * ``` - */ - pub fn new() -> MatchOptions { - MatchOptions { - case_sensitive: true, - require_literal_separator: false, - require_literal_leading_dot: false - } - } - -} - -#[cfg(test)] -mod test { - use std::os; - use super::{glob, Pattern, MatchOptions}; - - #[test] - fn test_absolute_pattern() { - // assume that the filesystem is not empty! - assert!(glob("/*").next().is_some()); - assert!(glob("//").next().is_some()); - - // check windows absolute paths with host/device components - let root_with_device = os::getcwd().root_path().unwrap().join("*"); - // FIXME (#9639): This needs to handle non-utf8 paths - assert!(glob(root_with_device.as_str().unwrap()).next().is_some()); - } - - #[test] - fn test_wildcard_optimizations() { - assert!(Pattern::new("a*b").matches("a___b")); - assert!(Pattern::new("a**b").matches("a___b")); - assert!(Pattern::new("a***b").matches("a___b")); - assert!(Pattern::new("a*b*c").matches("abc")); - assert!(!Pattern::new("a*b*c").matches("abcd")); - assert!(Pattern::new("a*b*c").matches("a_b_c")); - assert!(Pattern::new("a*b*c").matches("a___b___c")); - assert!(Pattern::new("abc*abc*abc").matches("abcabcabcabcabcabcabc")); - assert!(!Pattern::new("abc*abc*abc").matches("abcabcabcabcabcabcabca")); - assert!(Pattern::new("a*a*a*a*a*a*a*a*a").matches("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); - assert!(Pattern::new("a*b[xyz]c*d").matches("abxcdbxcddd")); - } - - #[test] - #[cfg_attr(windows, ignore)] // FIXME (#9406) - fn test_lots_of_files() { - // this is a good test because it touches lots of differently named files - glob("/*/*/*/*").skip(10000).next(); - } - - #[test] - fn test_range_pattern() { - - let pat = Pattern::new("a[0-9]b"); - for i in range(0u, 10) { - assert!(pat.matches(format!("a{}b", i).as_slice())); - } - assert!(!pat.matches("a_b")); - - let pat = Pattern::new("a[!0-9]b"); - for i in range(0u, 10) { - assert!(!pat.matches(format!("a{}b", i).as_slice())); - } - assert!(pat.matches("a_b")); - - let pats = ["[a-z123]", "[1a-z23]", "[123a-z]"]; - for &p in pats.iter() { - let pat = Pattern::new(p); - for c in "abcdefghijklmnopqrstuvwxyz".chars() { - assert!(pat.matches(c.to_string().as_slice())); - } - for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars() { - let options = MatchOptions {case_sensitive: false, .. MatchOptions::new()}; - assert!(pat.matches_with(c.to_string().as_slice(), options)); - } - assert!(pat.matches("1")); - assert!(pat.matches("2")); - assert!(pat.matches("3")); - } - - let pats = ["[abc-]", "[-abc]", "[a-c-]"]; - for &p in pats.iter() { - let pat = Pattern::new(p); - assert!(pat.matches("a")); - assert!(pat.matches("b")); - assert!(pat.matches("c")); - assert!(pat.matches("-")); - assert!(!pat.matches("d")); - } - - let pat = Pattern::new("[2-1]"); - assert!(!pat.matches("1")); - assert!(!pat.matches("2")); - - assert!(Pattern::new("[-]").matches("-")); - assert!(!Pattern::new("[!-]").matches("-")); - } - - #[test] - fn test_unclosed_bracket() { - // unclosed `[` should be treated literally - assert!(Pattern::new("abc[def").matches("abc[def")); - assert!(Pattern::new("abc[!def").matches("abc[!def")); - assert!(Pattern::new("abc[").matches("abc[")); - assert!(Pattern::new("abc[!").matches("abc[!")); - assert!(Pattern::new("abc[d").matches("abc[d")); - assert!(Pattern::new("abc[!d").matches("abc[!d")); - assert!(Pattern::new("abc[]").matches("abc[]")); - assert!(Pattern::new("abc[!]").matches("abc[!]")); - } - - #[test] - fn test_pattern_matches() { - let txt_pat = Pattern::new("*hello.txt"); - assert!(txt_pat.matches("hello.txt")); - assert!(txt_pat.matches("gareth_says_hello.txt")); - assert!(txt_pat.matches("some/path/to/hello.txt")); - assert!(txt_pat.matches("some\\path\\to\\hello.txt")); - assert!(txt_pat.matches("/an/absolute/path/to/hello.txt")); - assert!(!txt_pat.matches("hello.txt-and-then-some")); - assert!(!txt_pat.matches("goodbye.txt")); - - let dir_pat = Pattern::new("*some/path/to/hello.txt"); - assert!(dir_pat.matches("some/path/to/hello.txt")); - assert!(dir_pat.matches("a/bigger/some/path/to/hello.txt")); - assert!(!dir_pat.matches("some/path/to/hello.txt-and-then-some")); - assert!(!dir_pat.matches("some/other/path/to/hello.txt")); - } - - #[test] - fn test_pattern_escape() { - let s = "_[_]_?_*_!_"; - assert_eq!(Pattern::escape(s), "_[[]_[]]_[?]_[*]_!_".to_string()); - assert!(Pattern::new(Pattern::escape(s).as_slice()).matches(s)); - } - - #[test] - fn test_pattern_matches_case_insensitive() { - - let pat = Pattern::new("aBcDeFg"); - let options = MatchOptions { - case_sensitive: false, - require_literal_separator: false, - require_literal_leading_dot: false - }; - - assert!(pat.matches_with("aBcDeFg", options)); - assert!(pat.matches_with("abcdefg", options)); - assert!(pat.matches_with("ABCDEFG", options)); - assert!(pat.matches_with("AbCdEfG", options)); - } - - #[test] - fn test_pattern_matches_case_insensitive_range() { - - let pat_within = Pattern::new("[a]"); - let pat_except = Pattern::new("[!a]"); - - let options_case_insensitive = MatchOptions { - case_sensitive: false, - require_literal_separator: false, - require_literal_leading_dot: false - }; - let options_case_sensitive = MatchOptions { - case_sensitive: true, - require_literal_separator: false, - require_literal_leading_dot: false - }; - - assert!(pat_within.matches_with("a", options_case_insensitive)); - assert!(pat_within.matches_with("A", options_case_insensitive)); - assert!(!pat_within.matches_with("A", options_case_sensitive)); - - assert!(!pat_except.matches_with("a", options_case_insensitive)); - assert!(!pat_except.matches_with("A", options_case_insensitive)); - assert!(pat_except.matches_with("A", options_case_sensitive)); - } - - #[test] - fn test_pattern_matches_require_literal_separator() { - - let options_require_literal = MatchOptions { - case_sensitive: true, - require_literal_separator: true, - require_literal_leading_dot: false - }; - let options_not_require_literal = MatchOptions { - case_sensitive: true, - require_literal_separator: false, - require_literal_leading_dot: false - }; - - assert!(Pattern::new("abc/def").matches_with("abc/def", options_require_literal)); - assert!(!Pattern::new("abc?def").matches_with("abc/def", options_require_literal)); - assert!(!Pattern::new("abc*def").matches_with("abc/def", options_require_literal)); - assert!(!Pattern::new("abc[/]def").matches_with("abc/def", options_require_literal)); - - assert!(Pattern::new("abc/def").matches_with("abc/def", options_not_require_literal)); - assert!(Pattern::new("abc?def").matches_with("abc/def", options_not_require_literal)); - assert!(Pattern::new("abc*def").matches_with("abc/def", options_not_require_literal)); - assert!(Pattern::new("abc[/]def").matches_with("abc/def", options_not_require_literal)); - } - - #[test] - fn test_pattern_matches_require_literal_leading_dot() { - - let options_require_literal_leading_dot = MatchOptions { - case_sensitive: true, - require_literal_separator: false, - require_literal_leading_dot: true - }; - let options_not_require_literal_leading_dot = MatchOptions { - case_sensitive: true, - require_literal_separator: false, - require_literal_leading_dot: false - }; - - let f = |options| Pattern::new("*.txt").matches_with(".hello.txt", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(!f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new(".*.*").matches_with(".hello.txt", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new("aaa/bbb/*").matches_with("aaa/bbb/.ccc", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(!f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new("aaa/bbb/*").matches_with("aaa/bbb/c.c.c.", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new("aaa/bbb/.*").matches_with("aaa/bbb/.ccc", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new("aaa/?bbb").matches_with("aaa/.bbb", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(!f(options_require_literal_leading_dot)); - - let f = |options| Pattern::new("aaa/[.]bbb").matches_with("aaa/.bbb", options); - assert!(f(options_not_require_literal_leading_dot)); - assert!(!f(options_require_literal_leading_dot)); - } - - #[test] - fn test_matches_path() { - // on windows, (Path::new("a/b").as_str().unwrap() == "a\\b"), so this - // tests that / and \ are considered equivalent on windows - assert!(Pattern::new("a/b").matches_path(&Path::new("a/b"))); - } -} diff --git a/src/libhexfloat/lib.rs b/src/libhexfloat/lib.rs deleted file mode 100644 index 8335cc16d649f..0000000000000 --- a/src/libhexfloat/lib.rs +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! -Syntax extension to create floating point literals from hexadecimal strings - -Once loaded, hexfloat!() is called with a string containing the hexadecimal -floating-point literal, and an optional type (f32 or f64). -If the type is omitted, the literal is treated the same as a normal unsuffixed -literal. - -# Examples - -To load the extension and use it: - -```rust,ignore -#[phase(plugin)] -extern crate hexfloat; - -fn main() { - let val = hexfloat!("0x1.ffffb4", f32); -} -``` - -# References - -* [ExploringBinary: hexadecimal floating point constants] - (http://www.exploringbinary.com/hexadecimal-floating-point-constants/) - -*/ - -#![crate_name = "hexfloat"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/hexfloat"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(plugin_registrar)] - -extern crate syntax; -extern crate rustc; - -use syntax::ast; -use syntax::codemap::{Span, mk_sp}; -use syntax::ext::base; -use syntax::ext::base::{ExtCtxt, MacExpr}; -use syntax::ext::build::AstBuilder; -use syntax::parse::token; -use syntax::ptr::P; -use rustc::plugin::Registry; - -#[plugin_registrar] -pub fn plugin_registrar(reg: &mut Registry) { - reg.register_macro("hexfloat", expand_syntax_ext); -} - -//Check if the literal is valid (as LLVM expects), -//and return a descriptive error if not. -fn hex_float_lit_err(s: &str) -> Option<(uint, String)> { - let mut chars = s.chars().peekable(); - let mut i = 0; - if chars.peek() == Some(&'-') { chars.next(); i+= 1 } - if chars.next() != Some('0') { - return Some((i, "Expected '0'".to_string())); - } i+=1; - if chars.next() != Some('x') { - return Some((i, "Expected 'x'".to_string())); - } i+=1; - let mut d_len = 0i; - for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; d_len += 1;} - if chars.next() != Some('.') { - return Some((i, "Expected '.'".to_string())); - } i+=1; - let mut f_len = 0i; - for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; f_len += 1;} - if d_len == 0 && f_len == 0 { - return Some((i, "Expected digits before or after decimal \ - point".to_string())); - } - if chars.next() != Some('p') { - return Some((i, "Expected 'p'".to_string())); - } i+=1; - if chars.peek() == Some(&'-') { chars.next(); i+= 1 } - let mut e_len = 0i; - for _ in chars.take_while(|c| c.is_digit()) { chars.next(); i+=1; e_len += 1} - if e_len == 0 { - return Some((i, "Expected exponent digits".to_string())); - } - match chars.next() { - None => None, - Some(_) => Some((i, "Expected end of string".to_string())) - } -} - -pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) - -> Box { - let (expr, ty_lit) = parse_tts(cx, tts); - - let ty = match ty_lit { - None => None, - Some(Ident{ident, span}) => match token::get_ident(ident).get() { - "f32" => Some(ast::TyF32), - "f64" => Some(ast::TyF64), - _ => { - cx.span_err(span, "invalid floating point type in hexfloat!"); - None - } - } - }; - - let s = match expr.node { - // expression is a literal - ast::ExprLit(ref lit) => match lit.node { - // string literal - ast::LitStr(ref s, _) => { - s.clone() - } - _ => { - cx.span_err(expr.span, "unsupported literal in hexfloat!"); - return base::DummyResult::expr(sp); - } - }, - _ => { - cx.span_err(expr.span, "non-literal in hexfloat!"); - return base::DummyResult::expr(sp); - } - }; - - { - let err = hex_float_lit_err(s.get()); - match err { - Some((err_pos, err_str)) => { - let pos = expr.span.lo + syntax::codemap::Pos::from_uint(err_pos + 1); - let span = syntax::codemap::mk_sp(pos,pos); - cx.span_err(span, - format!("invalid hex float literal in hexfloat!: \ - {}", - err_str).as_slice()); - return base::DummyResult::expr(sp); - } - _ => () - } - } - - let lit = match ty { - None => ast::LitFloatUnsuffixed(s), - Some (ty) => ast::LitFloat(s, ty) - }; - MacExpr::new(cx.expr_lit(sp, lit)) -} - -struct Ident { - ident: ast::Ident, - span: Span -} - -fn parse_tts(cx: &ExtCtxt, - tts: &[ast::TokenTree]) -> (P, Option) { - let p = &mut cx.new_parser_from_tts(tts); - let ex = p.parse_expr(); - let id = if p.token == token::EOF { - None - } else { - p.expect(&token::COMMA); - let lo = p.span.lo; - let ident = p.parse_ident(); - let hi = p.last_span.hi; - Some(Ident{ident: ident, span: mk_sp(lo, hi)}) - }; - if p.token != token::EOF { - p.unexpected(); - } - (ex, id) -} - -// FIXME (10872): This is required to prevent an LLVM assert on Windows -#[test] -fn dummy_test() { } diff --git a/src/libnum/bigint.rs b/src/libnum/bigint.rs deleted file mode 100644 index e52d62a040be8..0000000000000 --- a/src/libnum/bigint.rs +++ /dev/null @@ -1,2961 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A Big integer (signed version: `BigInt`, unsigned version: `BigUint`). -//! -//! A `BigUint` is represented as an array of `BigDigit`s. -//! A `BigInt` is a combination of `BigUint` and `Sign`. -//! -//! Common numerical operations are overloaded, so we can treat them -//! the same way we treat other numbers. -//! -//! ## Example -//! -//! ```rust -//! # #![allow(deprecated)] -//! use num::bigint::BigUint; -//! use std::num::{Zero, One}; -//! use std::mem::replace; -//! -//! // Calculate large fibonacci numbers. -//! fn fib(n: uint) -> BigUint { -//! let mut f0: BigUint = Zero::zero(); -//! let mut f1: BigUint = One::one(); -//! for _ in range(0, n) { -//! let f2 = f0 + f1; -//! // This is a low cost way of swapping f0 with f1 and f1 with f2. -//! f0 = replace(&mut f1, f2); -//! } -//! f0 -//! } -//! -//! // This is a very large number. -//! println!("fib(1000) = {}", fib(1000)); -//! ``` -//! -//! It's easy to generate large random numbers: -//! -//! ```rust -//! # #![allow(deprecated)] -//! use num::bigint::{ToBigInt, RandBigInt}; -//! use std::rand; -//! -//! let mut rng = rand::task_rng(); -//! let a = rng.gen_bigint(1000u); -//! -//! let low = -10000i.to_bigint().unwrap(); -//! let high = 10000i.to_bigint().unwrap(); -//! let b = rng.gen_bigint_range(&low, &high); -//! -//! // Probably an even larger number. -//! println!("{}", a * b); -//! ``` - -use Integer; -use rand::Rng; - -use std::{cmp, fmt, hash}; -use std::default::Default; -use std::from_str::FromStr; -use std::num::CheckedDiv; -use std::num::{ToPrimitive, FromPrimitive}; -use std::num::{Zero, One, ToStrRadix, FromStrRadix}; -use std::string::String; -use std::{uint, i64, u64}; - -/// A `BigDigit` is a `BigUint`'s composing element. -pub type BigDigit = u32; - -/// A `DoubleBigDigit` is the internal type used to do the computations. Its -/// size is the double of the size of `BigDigit`. -pub type DoubleBigDigit = u64; - -pub const ZERO_BIG_DIGIT: BigDigit = 0; -static ZERO_VEC: [BigDigit, ..1] = [ZERO_BIG_DIGIT]; - -#[allow(non_snake_case)] -pub mod BigDigit { - use super::BigDigit; - use super::DoubleBigDigit; - - // `DoubleBigDigit` size dependent - #[allow(non_uppercase_statics)] - pub const bits: uint = 32; - - #[allow(non_uppercase_statics)] - pub const base: DoubleBigDigit = 1 << bits; - #[allow(non_uppercase_statics)] - static lo_mask: DoubleBigDigit = (-1 as DoubleBigDigit) >> bits; - - #[inline] - fn get_hi(n: DoubleBigDigit) -> BigDigit { (n >> bits) as BigDigit } - #[inline] - fn get_lo(n: DoubleBigDigit) -> BigDigit { (n & lo_mask) as BigDigit } - - /// Split one `DoubleBigDigit` into two `BigDigit`s. - #[inline] - pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) { - (get_hi(n), get_lo(n)) - } - - /// Join two `BigDigit`s into one `DoubleBigDigit` - #[inline] - pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit { - (lo as DoubleBigDigit) | ((hi as DoubleBigDigit) << bits) - } -} - -/// A big unsigned integer type. -/// -/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number -/// `(a + b * BigDigit::base + c * BigDigit::base^2)`. -#[deriving(Clone)] -pub struct BigUint { - data: Vec -} - -impl PartialEq for BigUint { - #[inline] - fn eq(&self, other: &BigUint) -> bool { - match self.cmp(other) { Equal => true, _ => false } - } -} -impl Eq for BigUint {} - -impl PartialOrd for BigUint { - #[inline] - fn partial_cmp(&self, other: &BigUint) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for BigUint { - #[inline] - fn cmp(&self, other: &BigUint) -> Ordering { - let (s_len, o_len) = (self.data.len(), other.data.len()); - if s_len < o_len { return Less; } - if s_len > o_len { return Greater; } - - for (&self_i, &other_i) in self.data.iter().rev().zip(other.data.iter().rev()) { - if self_i < other_i { return Less; } - if self_i > other_i { return Greater; } - } - return Equal; - } -} - -impl Default for BigUint { - #[inline] - fn default() -> BigUint { Zero::zero() } -} - -impl hash::Hash for BigUint { - fn hash(&self, state: &mut S) { - // hash 0 in case it's all 0's - 0u32.hash(state); - - let mut found_first_value = false; - for elem in self.data.iter().rev() { - // don't hash any leading 0's, they shouldn't affect the hash - if found_first_value || *elem != 0 { - found_first_value = true; - elem.hash(state); - } - } - } -} - -impl fmt::Show for BigUint { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_str_radix(10)) - } -} - -impl FromStr for BigUint { - #[inline] - fn from_str(s: &str) -> Option { - FromStrRadix::from_str_radix(s, 10) - } -} - -impl Num for BigUint {} - -impl BitAnd for BigUint { - fn bitand(&self, other: &BigUint) -> BigUint { - BigUint::new(self.data.iter().zip(other.data.iter()).map(|(ai, bi)| *ai & *bi).collect()) - } -} - -impl BitOr for BigUint { - fn bitor(&self, other: &BigUint) -> BigUint { - let zeros = ZERO_VEC.iter().cycle(); - let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; - let ored = a.data.iter().zip(b.data.iter().chain(zeros)).map( - |(ai, bi)| *ai | *bi - ).collect(); - return BigUint::new(ored); - } -} - -impl BitXor for BigUint { - fn bitxor(&self, other: &BigUint) -> BigUint { - let zeros = ZERO_VEC.iter().cycle(); - let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; - let xored = a.data.iter().zip(b.data.iter().chain(zeros)).map( - |(ai, bi)| *ai ^ *bi - ).collect(); - return BigUint::new(xored); - } -} - -impl Shl for BigUint { - #[inline] - fn shl(&self, rhs: &uint) -> BigUint { - let n_unit = *rhs / BigDigit::bits; - let n_bits = *rhs % BigDigit::bits; - return self.shl_unit(n_unit).shl_bits(n_bits); - } -} - -impl Shr for BigUint { - #[inline] - fn shr(&self, rhs: &uint) -> BigUint { - let n_unit = *rhs / BigDigit::bits; - let n_bits = *rhs % BigDigit::bits; - return self.shr_unit(n_unit).shr_bits(n_bits); - } -} - -impl Zero for BigUint { - #[inline] - fn zero() -> BigUint { BigUint::new(Vec::new()) } - - #[inline] - fn is_zero(&self) -> bool { self.data.is_empty() } -} - -impl One for BigUint { - #[inline] - fn one() -> BigUint { BigUint::new(vec!(1)) } -} - -impl Unsigned for BigUint {} - -impl Add for BigUint { - fn add(&self, other: &BigUint) -> BigUint { - let zeros = ZERO_VEC.iter().cycle(); - let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; - - let mut carry = 0; - let mut sum: Vec = a.data.iter().zip(b.data.iter().chain(zeros)).map(|(ai, bi)| { - let (hi, lo) = BigDigit::from_doublebigdigit( - (*ai as DoubleBigDigit) + (*bi as DoubleBigDigit) + (carry as DoubleBigDigit)); - carry = hi; - lo - }).collect(); - if carry != 0 { sum.push(carry); } - return BigUint::new(sum); - } -} - -impl Sub for BigUint { - fn sub(&self, other: &BigUint) -> BigUint { - let new_len = cmp::max(self.data.len(), other.data.len()); - let zeros = ZERO_VEC.iter().cycle(); - let (a, b) = (self.data.iter().chain(zeros.clone()), other.data.iter().chain(zeros)); - - let mut borrow = 0i; - let diff: Vec = a.take(new_len).zip(b).map(|(ai, bi)| { - let (hi, lo) = BigDigit::from_doublebigdigit( - BigDigit::base - + (*ai as DoubleBigDigit) - - (*bi as DoubleBigDigit) - - (borrow as DoubleBigDigit) - ); - /* - hi * (base) + lo == 1*(base) + ai - bi - borrow - => ai - bi - borrow < 0 <=> hi == 0 - */ - borrow = if hi == 0 { 1 } else { 0 }; - lo - }).collect(); - - assert!(borrow == 0, - "Cannot subtract other from self because other is larger than self."); - return BigUint::new(diff); - } -} - -impl Mul for BigUint { - fn mul(&self, other: &BigUint) -> BigUint { - if self.is_zero() || other.is_zero() { return Zero::zero(); } - - let (s_len, o_len) = (self.data.len(), other.data.len()); - if s_len == 1 { return mul_digit(other, self.data.as_slice()[0]); } - if o_len == 1 { return mul_digit(self, other.data.as_slice()[0]); } - - // Using Karatsuba multiplication - // (a1 * base + a0) * (b1 * base + b0) - // = a1*b1 * base^2 + - // (a1*b1 + a0*b0 - (a1-b0)*(b1-a0)) * base + - // a0*b0 - let half_len = cmp::max(s_len, o_len) / 2; - let (s_hi, s_lo) = cut_at(self, half_len); - let (o_hi, o_lo) = cut_at(other, half_len); - - let ll = s_lo * o_lo; - let hh = s_hi * o_hi; - let mm = { - let (s1, n1) = sub_sign(s_hi, s_lo); - let (s2, n2) = sub_sign(o_hi, o_lo); - match (s1, s2) { - (Equal, _) | (_, Equal) => hh + ll, - (Less, Greater) | (Greater, Less) => hh + ll + (n1 * n2), - (Less, Less) | (Greater, Greater) => hh + ll - (n1 * n2) - } - }; - - return ll + mm.shl_unit(half_len) + hh.shl_unit(half_len * 2); - - - fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint { - if n == 0 { return Zero::zero(); } - if n == 1 { return (*a).clone(); } - - let mut carry = 0; - let mut prod: Vec = a.data.iter().map(|ai| { - let (hi, lo) = BigDigit::from_doublebigdigit( - (*ai as DoubleBigDigit) * (n as DoubleBigDigit) + (carry as DoubleBigDigit) - ); - carry = hi; - lo - }).collect(); - if carry != 0 { prod.push(carry); } - return BigUint::new(prod); - } - - #[inline] - fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { - let mid = cmp::min(a.data.len(), n); - return (BigUint::from_slice(a.data.slice(mid, a.data.len())), - BigUint::from_slice(a.data.slice(0, mid))); - } - - #[inline] - fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) { - match a.cmp(&b) { - Less => (Less, b - a), - Greater => (Greater, a - b), - _ => (Equal, Zero::zero()) - } - } - } -} - -impl Div for BigUint { - #[inline] - fn div(&self, other: &BigUint) -> BigUint { - let (q, _) = self.div_rem(other); - return q; - } -} - -impl Rem for BigUint { - #[inline] - fn rem(&self, other: &BigUint) -> BigUint { - let (_, r) = self.div_rem(other); - return r; - } -} - -impl Neg for BigUint { - #[inline] - fn neg(&self) -> BigUint { fail!() } -} - -impl CheckedAdd for BigUint { - #[inline] - fn checked_add(&self, v: &BigUint) -> Option { - return Some(self.add(v)); - } -} - -impl CheckedSub for BigUint { - #[inline] - fn checked_sub(&self, v: &BigUint) -> Option { - if *self < *v { - return None; - } - return Some(self.sub(v)); - } -} - -impl CheckedMul for BigUint { - #[inline] - fn checked_mul(&self, v: &BigUint) -> Option { - return Some(self.mul(v)); - } -} - -impl CheckedDiv for BigUint { - #[inline] - fn checked_div(&self, v: &BigUint) -> Option { - if v.is_zero() { - return None; - } - return Some(self.div(v)); - } -} - -impl Integer for BigUint { - #[inline] - fn div_rem(&self, other: &BigUint) -> (BigUint, BigUint) { - self.div_mod_floor(other) - } - - #[inline] - fn div_floor(&self, other: &BigUint) -> BigUint { - let (d, _) = self.div_mod_floor(other); - return d; - } - - #[inline] - fn mod_floor(&self, other: &BigUint) -> BigUint { - let (_, m) = self.div_mod_floor(other); - return m; - } - - fn div_mod_floor(&self, other: &BigUint) -> (BigUint, BigUint) { - if other.is_zero() { fail!() } - if self.is_zero() { return (Zero::zero(), Zero::zero()); } - if *other == One::one() { return ((*self).clone(), Zero::zero()); } - - match self.cmp(other) { - Less => return (Zero::zero(), (*self).clone()), - Equal => return (One::one(), Zero::zero()), - Greater => {} // Do nothing - } - - let mut shift = 0; - let mut n = *other.data.last().unwrap(); - while n < (1 << BigDigit::bits - 2) { - n <<= 1; - shift += 1; - } - assert!(shift < BigDigit::bits); - let (d, m) = div_mod_floor_inner(self << shift, other << shift); - return (d, m >> shift); - - - fn div_mod_floor_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) { - let mut m = a; - let mut d: BigUint = Zero::zero(); - let mut n = 1; - while m >= b { - let (d0, d_unit, b_unit) = div_estimate(&m, &b, n); - let mut d0 = d0; - let mut prod = b * d0; - while prod > m { - // FIXME(#5992): assignment operator overloads - // d0 -= d_unit - d0 = d0 - d_unit; - // FIXME(#5992): assignment operator overloads - // prod -= b_unit; - prod = prod - b_unit - } - if d0.is_zero() { - n = 2; - continue; - } - n = 1; - // FIXME(#5992): assignment operator overloads - // d += d0; - d = d + d0; - // FIXME(#5992): assignment operator overloads - // m -= prod; - m = m - prod; - } - return (d, m); - } - - - fn div_estimate(a: &BigUint, b: &BigUint, n: uint) - -> (BigUint, BigUint, BigUint) { - if a.data.len() < n { - return (Zero::zero(), Zero::zero(), (*a).clone()); - } - - let an = a.data.tailn(a.data.len() - n); - let bn = *b.data.last().unwrap(); - let mut d = Vec::with_capacity(an.len()); - let mut carry = 0; - for elt in an.iter().rev() { - let ai = BigDigit::to_doublebigdigit(carry, *elt); - let di = ai / (bn as DoubleBigDigit); - assert!(di < BigDigit::base); - carry = (ai % (bn as DoubleBigDigit)) as BigDigit; - d.push(di as BigDigit) - } - d.reverse(); - - let shift = (a.data.len() - an.len()) - (b.data.len() - 1); - if shift == 0 { - return (BigUint::new(d), One::one(), (*b).clone()); - } - let one: BigUint = One::one(); - return (BigUint::new(d).shl_unit(shift), - one.shl_unit(shift), - b.shl_unit(shift)); - } - } - - /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. - /// - /// The result is always positive. - #[inline] - fn gcd(&self, other: &BigUint) -> BigUint { - // Use Euclid's algorithm - let mut m = (*self).clone(); - let mut n = (*other).clone(); - while !m.is_zero() { - let temp = m; - m = n % temp; - n = temp; - } - return n; - } - - /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. - #[inline] - fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other)) } - - /// Deprecated, use `is_multiple_of` instead. - #[deprecated = "function renamed to `is_multiple_of`"] - #[inline] - fn divides(&self, other: &BigUint) -> bool { return self.is_multiple_of(other); } - - /// Returns `true` if the number is a multiple of `other`. - #[inline] - fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() } - - /// Returns `true` if the number is divisible by `2`. - #[inline] - fn is_even(&self) -> bool { - // Considering only the last digit. - match self.data.as_slice().head() { - Some(x) => x.is_even(), - None => true - } - } - - /// Returns `true` if the number is not divisible by `2`. - #[inline] - fn is_odd(&self) -> bool { !self.is_even() } -} - -impl ToPrimitive for BigUint { - #[inline] - fn to_i64(&self) -> Option { - self.to_u64().and_then(|n| { - // If top bit of u64 is set, it's too large to convert to i64. - if n >> 63 == 0 { - Some(n as i64) - } else { - None - } - }) - } - - // `DoubleBigDigit` size dependent - #[inline] - fn to_u64(&self) -> Option { - match self.data.len() { - 0 => Some(0), - 1 => Some(self.data.as_slice()[0] as u64), - 2 => Some(BigDigit::to_doublebigdigit(self.data.as_slice()[1], self.data.as_slice()[0]) - as u64), - _ => None - } - } -} - -impl FromPrimitive for BigUint { - #[inline] - fn from_i64(n: i64) -> Option { - if n > 0 { - FromPrimitive::from_u64(n as u64) - } else if n == 0 { - Some(Zero::zero()) - } else { - None - } - } - - // `DoubleBigDigit` size dependent - #[inline] - fn from_u64(n: u64) -> Option { - let n = match BigDigit::from_doublebigdigit(n) { - (0, 0) => Zero::zero(), - (0, n0) => BigUint::new(vec!(n0)), - (n1, n0) => BigUint::new(vec!(n0, n1)) - }; - Some(n) - } -} - -/// A generic trait for converting a value to a `BigUint`. -pub trait ToBigUint { - /// Converts the value of `self` to a `BigUint`. - fn to_biguint(&self) -> Option; -} - -impl ToBigUint for BigInt { - #[inline] - fn to_biguint(&self) -> Option { - if self.sign == Plus { - Some(self.data.clone()) - } else if self.sign == NoSign { - Some(Zero::zero()) - } else { - None - } - } -} - -impl ToBigUint for BigUint { - #[inline] - fn to_biguint(&self) -> Option { - Some(self.clone()) - } -} - -macro_rules! impl_to_biguint( - ($T:ty, $from_ty:path) => { - impl ToBigUint for $T { - #[inline] - fn to_biguint(&self) -> Option { - $from_ty(*self) - } - } - } -) - -impl_to_biguint!(int, FromPrimitive::from_int) -impl_to_biguint!(i8, FromPrimitive::from_i8) -impl_to_biguint!(i16, FromPrimitive::from_i16) -impl_to_biguint!(i32, FromPrimitive::from_i32) -impl_to_biguint!(i64, FromPrimitive::from_i64) -impl_to_biguint!(uint, FromPrimitive::from_uint) -impl_to_biguint!(u8, FromPrimitive::from_u8) -impl_to_biguint!(u16, FromPrimitive::from_u16) -impl_to_biguint!(u32, FromPrimitive::from_u32) -impl_to_biguint!(u64, FromPrimitive::from_u64) - -impl ToStrRadix for BigUint { - fn to_str_radix(&self, radix: uint) -> String { - assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]"); - let (base, max_len) = get_radix_base(radix); - if base == BigDigit::base { - return fill_concat(self.data.as_slice(), radix, max_len) - } - return fill_concat(convert_base(self, base).as_slice(), radix, max_len); - - fn convert_base(n: &BigUint, base: DoubleBigDigit) -> Vec { - let divider = base.to_biguint().unwrap(); - let mut result = Vec::new(); - let mut m = n.clone(); - while m >= divider { - let (d, m0) = m.div_mod_floor(÷r); - result.push(m0.to_uint().unwrap() as BigDigit); - m = d; - } - if !m.is_zero() { - result.push(m.to_uint().unwrap() as BigDigit); - } - return result; - } - - fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> String { - if v.is_empty() { - return "0".to_string() - } - let mut s = String::with_capacity(v.len() * l); - for n in v.iter().rev() { - let ss = (*n as uint).to_str_radix(radix); - s.push_str("0".repeat(l - ss.len()).as_slice()); - s.push_str(ss.as_slice()); - } - s.as_slice().trim_left_chars('0').to_string() - } - } -} - -impl FromStrRadix for BigUint { - /// Creates and initializes a `BigUint`. - #[inline] - fn from_str_radix(s: &str, radix: uint) -> Option { - BigUint::parse_bytes(s.as_bytes(), radix) - } -} - -impl BigUint { - /// Creates and initializes a `BigUint`. - /// - /// The digits are be in base 2^32. - #[inline] - pub fn new(mut digits: Vec) -> BigUint { - // omit trailing zeros - let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1); - digits.truncate(new_len); - BigUint { data: digits } - } - - /// Creates and initializes a `BigUint`. - /// - /// The digits are be in base 2^32. - #[inline] - pub fn from_slice(slice: &[BigDigit]) -> BigUint { - BigUint::new(Vec::from_slice(slice)) - } - - /// Creates and initializes a `BigUint`. - pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { - let (base, unit_len) = get_radix_base(radix); - let base_num = match base.to_biguint() { - Some(base_num) => base_num, - None => { return None; } - }; - - let mut end = buf.len(); - let mut n: BigUint = Zero::zero(); - let mut power: BigUint = One::one(); - loop { - let start = cmp::max(end, unit_len) - unit_len; - match uint::parse_bytes(buf[start..end], radix) { - Some(d) => { - let d: Option = FromPrimitive::from_uint(d); - match d { - Some(d) => { - // FIXME(#5992): assignment operator overloads - // n += d * power; - n = n + d * power; - } - None => { return None; } - } - } - None => { return None; } - } - if end <= unit_len { - return Some(n); - } - end -= unit_len; - // FIXME(#5992): assignment operator overloads - // power *= base_num; - power = power * base_num; - } - } - - #[inline] - fn shl_unit(&self, n_unit: uint) -> BigUint { - if n_unit == 0 || self.is_zero() { return (*self).clone(); } - - BigUint::new(Vec::from_elem(n_unit, ZERO_BIG_DIGIT).append(self.data.as_slice())) - } - - #[inline] - fn shl_bits(&self, n_bits: uint) -> BigUint { - if n_bits == 0 || self.is_zero() { return (*self).clone(); } - - let mut carry = 0; - let mut shifted: Vec = self.data.iter().map(|elem| { - let (hi, lo) = BigDigit::from_doublebigdigit( - (*elem as DoubleBigDigit) << n_bits | (carry as DoubleBigDigit) - ); - carry = hi; - lo - }).collect(); - if carry != 0 { shifted.push(carry); } - return BigUint::new(shifted); - } - - #[inline] - fn shr_unit(&self, n_unit: uint) -> BigUint { - if n_unit == 0 { return (*self).clone(); } - if self.data.len() < n_unit { return Zero::zero(); } - return BigUint::from_slice( - self.data.slice(n_unit, self.data.len()) - ); - } - - #[inline] - fn shr_bits(&self, n_bits: uint) -> BigUint { - if n_bits == 0 || self.data.is_empty() { return (*self).clone(); } - - let mut borrow = 0; - let mut shifted_rev = Vec::with_capacity(self.data.len()); - for elem in self.data.iter().rev() { - shifted_rev.push((*elem >> n_bits) | borrow); - borrow = *elem << (BigDigit::bits - n_bits); - } - let shifted = { shifted_rev.reverse(); shifted_rev }; - return BigUint::new(shifted); - } - - /// Determines the fewest bits necessary to express the `BigUint`. - pub fn bits(&self) -> uint { - if self.is_zero() { return 0; } - let zeros = self.data.last().unwrap().leading_zeros(); - return self.data.len()*BigDigit::bits - zeros; - } -} - -// `DoubleBigDigit` size dependent -#[inline] -fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) { - match radix { - 2 => (4294967296, 32), - 3 => (3486784401, 20), - 4 => (4294967296, 16), - 5 => (1220703125, 13), - 6 => (2176782336, 12), - 7 => (1977326743, 11), - 8 => (1073741824, 10), - 9 => (3486784401, 10), - 10 => (1000000000, 9), - 11 => (2357947691, 9), - 12 => (429981696, 8), - 13 => (815730721, 8), - 14 => (1475789056, 8), - 15 => (2562890625, 8), - 16 => (4294967296, 8), - _ => fail!("The radix must be within (1, 16]") - } -} - -/// A Sign is a `BigInt`'s composing element. -#[deriving(PartialEq, PartialOrd, Eq, Ord, Clone, Show)] -pub enum Sign { Minus, NoSign, Plus } - -impl Neg for Sign { - /// Negate Sign value. - #[inline] - fn neg(&self) -> Sign { - match *self { - Minus => Plus, - NoSign => NoSign, - Plus => Minus - } - } -} - -/// A big signed integer type. -#[deriving(Clone)] -pub struct BigInt { - sign: Sign, - data: BigUint -} - -impl PartialEq for BigInt { - #[inline] - fn eq(&self, other: &BigInt) -> bool { - self.cmp(other) == Equal - } -} - -impl Eq for BigInt {} - -impl PartialOrd for BigInt { - #[inline] - fn partial_cmp(&self, other: &BigInt) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for BigInt { - #[inline] - fn cmp(&self, other: &BigInt) -> Ordering { - let scmp = self.sign.cmp(&other.sign); - if scmp != Equal { return scmp; } - - match self.sign { - NoSign => Equal, - Plus => self.data.cmp(&other.data), - Minus => other.data.cmp(&self.data), - } - } -} - -impl Default for BigInt { - #[inline] - fn default() -> BigInt { Zero::zero() } -} - -impl fmt::Show for BigInt { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_str_radix(10)) - } -} - -impl hash::Hash for BigInt { - fn hash(&self, state: &mut S) { - (self.sign == Plus).hash(state); - self.data.hash(state); - } -} - -impl FromStr for BigInt { - #[inline] - fn from_str(s: &str) -> Option { - FromStrRadix::from_str_radix(s, 10) - } -} - -impl Num for BigInt {} - -impl Shl for BigInt { - #[inline] - fn shl(&self, rhs: &uint) -> BigInt { - BigInt::from_biguint(self.sign, self.data << *rhs) - } -} - -impl Shr for BigInt { - #[inline] - fn shr(&self, rhs: &uint) -> BigInt { - BigInt::from_biguint(self.sign, self.data >> *rhs) - } -} - -impl Zero for BigInt { - #[inline] - fn zero() -> BigInt { - BigInt::from_biguint(NoSign, Zero::zero()) - } - - #[inline] - fn is_zero(&self) -> bool { self.sign == NoSign } -} - -impl One for BigInt { - #[inline] - fn one() -> BigInt { - BigInt::from_biguint(Plus, One::one()) - } -} - -impl Signed for BigInt { - #[inline] - fn abs(&self) -> BigInt { - match self.sign { - Plus | NoSign => self.clone(), - Minus => BigInt::from_biguint(Plus, self.data.clone()) - } - } - - #[inline] - fn abs_sub(&self, other: &BigInt) -> BigInt { - if *self <= *other { Zero::zero() } else { *self - *other } - } - - #[inline] - fn signum(&self) -> BigInt { - match self.sign { - Plus => BigInt::from_biguint(Plus, One::one()), - Minus => BigInt::from_biguint(Minus, One::one()), - NoSign => Zero::zero(), - } - } - - #[inline] - fn is_positive(&self) -> bool { self.sign == Plus } - - #[inline] - fn is_negative(&self) -> bool { self.sign == Minus } -} - -impl Add for BigInt { - #[inline] - fn add(&self, other: &BigInt) -> BigInt { - match (self.sign, other.sign) { - (NoSign, _) => other.clone(), - (_, NoSign) => self.clone(), - (Plus, Plus) => BigInt::from_biguint(Plus, self.data + other.data), - (Plus, Minus) => self - (-*other), - (Minus, Plus) => other - (-*self), - (Minus, Minus) => -((-self) + (-*other)) - } - } -} - -impl Sub for BigInt { - #[inline] - fn sub(&self, other: &BigInt) -> BigInt { - match (self.sign, other.sign) { - (NoSign, _) => -other, - (_, NoSign) => self.clone(), - (Plus, Plus) => match self.data.cmp(&other.data) { - Less => BigInt::from_biguint(Minus, other.data - self.data), - Greater => BigInt::from_biguint(Plus, self.data - other.data), - Equal => Zero::zero() - }, - (Plus, Minus) => self + (-*other), - (Minus, Plus) => -((-self) + *other), - (Minus, Minus) => (-other) - (-*self) - } - } -} - -impl Mul for BigInt { - #[inline] - fn mul(&self, other: &BigInt) -> BigInt { - match (self.sign, other.sign) { - (NoSign, _) | (_, NoSign) => Zero::zero(), - (Plus, Plus) | (Minus, Minus) => { - BigInt::from_biguint(Plus, self.data * other.data) - }, - (Plus, Minus) | (Minus, Plus) => { - BigInt::from_biguint(Minus, self.data * other.data) - } - } - } -} - -impl Div for BigInt { - #[inline] - fn div(&self, other: &BigInt) -> BigInt { - let (q, _) = self.div_rem(other); - q - } -} - -impl Rem for BigInt { - #[inline] - fn rem(&self, other: &BigInt) -> BigInt { - let (_, r) = self.div_rem(other); - r - } -} - -impl Neg for BigInt { - #[inline] - fn neg(&self) -> BigInt { - BigInt::from_biguint(self.sign.neg(), self.data.clone()) - } -} - -impl CheckedAdd for BigInt { - #[inline] - fn checked_add(&self, v: &BigInt) -> Option { - return Some(self.add(v)); - } -} - -impl CheckedSub for BigInt { - #[inline] - fn checked_sub(&self, v: &BigInt) -> Option { - return Some(self.sub(v)); - } -} - -impl CheckedMul for BigInt { - #[inline] - fn checked_mul(&self, v: &BigInt) -> Option { - return Some(self.mul(v)); - } -} - -impl CheckedDiv for BigInt { - #[inline] - fn checked_div(&self, v: &BigInt) -> Option { - if v.is_zero() { - return None; - } - return Some(self.div(v)); - } -} - - -impl Integer for BigInt { - #[inline] - fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) { - // r.sign == self.sign - let (d_ui, r_ui) = self.data.div_mod_floor(&other.data); - let d = BigInt::from_biguint(Plus, d_ui); - let r = BigInt::from_biguint(Plus, r_ui); - match (self.sign, other.sign) { - (_, NoSign) => fail!(), - (Plus, Plus) | (NoSign, Plus) => ( d, r), - (Plus, Minus) | (NoSign, Minus) => (-d, r), - (Minus, Plus) => (-d, -r), - (Minus, Minus) => ( d, -r) - } - } - - #[inline] - fn div_floor(&self, other: &BigInt) -> BigInt { - let (d, _) = self.div_mod_floor(other); - d - } - - #[inline] - fn mod_floor(&self, other: &BigInt) -> BigInt { - let (_, m) = self.div_mod_floor(other); - m - } - - fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) { - // m.sign == other.sign - let (d_ui, m_ui) = self.data.div_rem(&other.data); - let d = BigInt::from_biguint(Plus, d_ui); - let m = BigInt::from_biguint(Plus, m_ui); - match (self.sign, other.sign) { - (_, NoSign) => fail!(), - (Plus, Plus) | (NoSign, Plus) => (d, m), - (Plus, Minus) | (NoSign, Minus) => if m.is_zero() { - (-d, Zero::zero()) - } else { - (-d - One::one(), m + *other) - }, - (Minus, Plus) => if m.is_zero() { - (-d, Zero::zero()) - } else { - (-d - One::one(), other - m) - }, - (Minus, Minus) => (d, -m) - } - } - - /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. - /// - /// The result is always positive. - #[inline] - fn gcd(&self, other: &BigInt) -> BigInt { - BigInt::from_biguint(Plus, self.data.gcd(&other.data)) - } - - /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. - #[inline] - fn lcm(&self, other: &BigInt) -> BigInt { - BigInt::from_biguint(Plus, self.data.lcm(&other.data)) - } - - /// Deprecated, use `is_multiple_of` instead. - #[deprecated = "function renamed to `is_multiple_of`"] - #[inline] - fn divides(&self, other: &BigInt) -> bool { return self.is_multiple_of(other); } - - /// Returns `true` if the number is a multiple of `other`. - #[inline] - fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) } - - /// Returns `true` if the number is divisible by `2`. - #[inline] - fn is_even(&self) -> bool { self.data.is_even() } - - /// Returns `true` if the number is not divisible by `2`. - #[inline] - fn is_odd(&self) -> bool { self.data.is_odd() } -} - -impl ToPrimitive for BigInt { - #[inline] - fn to_i64(&self) -> Option { - match self.sign { - Plus => self.data.to_i64(), - NoSign => Some(0), - Minus => { - self.data.to_u64().and_then(|n| { - let m: u64 = 1 << 63; - if n < m { - Some(-(n as i64)) - } else if n == m { - Some(i64::MIN) - } else { - None - } - }) - } - } - } - - #[inline] - fn to_u64(&self) -> Option { - match self.sign { - Plus => self.data.to_u64(), - NoSign => Some(0), - Minus => None - } - } -} - -impl FromPrimitive for BigInt { - #[inline] - fn from_i64(n: i64) -> Option { - if n > 0 { - FromPrimitive::from_u64(n as u64).and_then(|n| { - Some(BigInt::from_biguint(Plus, n)) - }) - } else if n < 0 { - FromPrimitive::from_u64(u64::MAX - (n as u64) + 1).and_then( - |n| { - Some(BigInt::from_biguint(Minus, n)) - }) - } else { - Some(Zero::zero()) - } - } - - #[inline] - fn from_u64(n: u64) -> Option { - if n == 0 { - Some(Zero::zero()) - } else { - FromPrimitive::from_u64(n).and_then(|n| { - Some(BigInt::from_biguint(Plus, n)) - }) - } - } -} - -/// A generic trait for converting a value to a `BigInt`. -pub trait ToBigInt { - /// Converts the value of `self` to a `BigInt`. - fn to_bigint(&self) -> Option; -} - -impl ToBigInt for BigInt { - #[inline] - fn to_bigint(&self) -> Option { - Some(self.clone()) - } -} - -impl ToBigInt for BigUint { - #[inline] - fn to_bigint(&self) -> Option { - if self.is_zero() { - Some(Zero::zero()) - } else { - Some(BigInt { sign: Plus, data: self.clone() }) - } - } -} - -macro_rules! impl_to_bigint( - ($T:ty, $from_ty:path) => { - impl ToBigInt for $T { - #[inline] - fn to_bigint(&self) -> Option { - $from_ty(*self) - } - } - } -) - -impl_to_bigint!(int, FromPrimitive::from_int) -impl_to_bigint!(i8, FromPrimitive::from_i8) -impl_to_bigint!(i16, FromPrimitive::from_i16) -impl_to_bigint!(i32, FromPrimitive::from_i32) -impl_to_bigint!(i64, FromPrimitive::from_i64) -impl_to_bigint!(uint, FromPrimitive::from_uint) -impl_to_bigint!(u8, FromPrimitive::from_u8) -impl_to_bigint!(u16, FromPrimitive::from_u16) -impl_to_bigint!(u32, FromPrimitive::from_u32) -impl_to_bigint!(u64, FromPrimitive::from_u64) - -impl ToStrRadix for BigInt { - #[inline] - fn to_str_radix(&self, radix: uint) -> String { - match self.sign { - Plus => self.data.to_str_radix(radix), - NoSign => "0".to_string(), - Minus => format!("-{}", self.data.to_str_radix(radix)), - } - } -} - -impl FromStrRadix for BigInt { - /// Creates and initializes a BigInt. - #[inline] - fn from_str_radix(s: &str, radix: uint) -> Option { - BigInt::parse_bytes(s.as_bytes(), radix) - } -} - -pub trait RandBigInt { - /// Generate a random `BigUint` of the given bit size. - fn gen_biguint(&mut self, bit_size: uint) -> BigUint; - - /// Generate a random BigInt of the given bit size. - fn gen_bigint(&mut self, bit_size: uint) -> BigInt; - - /// Generate a random `BigUint` less than the given bound. Fails - /// when the bound is zero. - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint; - - /// Generate a random `BigUint` within the given range. The lower - /// bound is inclusive; the upper bound is exclusive. Fails when - /// the upper bound is not greater than the lower bound. - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; - - /// Generate a random `BigInt` within the given range. The lower - /// bound is inclusive; the upper bound is exclusive. Fails when - /// the upper bound is not greater than the lower bound. - fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; -} - -impl RandBigInt for R { - fn gen_biguint(&mut self, bit_size: uint) -> BigUint { - let (digits, rem) = bit_size.div_rem(&BigDigit::bits); - let mut data = Vec::with_capacity(digits+1); - for _ in range(0, digits) { - data.push(self.gen()); - } - if rem > 0 { - let final_digit: BigDigit = self.gen(); - data.push(final_digit >> (BigDigit::bits - rem)); - } - BigUint::new(data) - } - - fn gen_bigint(&mut self, bit_size: uint) -> BigInt { - // Generate a random BigUint... - let biguint = self.gen_biguint(bit_size); - // ...and then randomly assign it a Sign... - let sign = if biguint.is_zero() { - // ...except that if the BigUint is zero, we need to try - // again with probability 0.5. This is because otherwise, - // the probability of generating a zero BigInt would be - // double that of any other number. - if self.gen() { - return self.gen_bigint(bit_size); - } else { - NoSign - } - } else if self.gen() { - Plus - } else { - Minus - }; - BigInt::from_biguint(sign, biguint) - } - - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint { - assert!(!bound.is_zero()); - let bits = bound.bits(); - loop { - let n = self.gen_biguint(bits); - if n < *bound { return n; } - } - } - - fn gen_biguint_range(&mut self, - lbound: &BigUint, - ubound: &BigUint) - -> BigUint { - assert!(*lbound < *ubound); - return *lbound + self.gen_biguint_below(&(*ubound - *lbound)); - } - - fn gen_bigint_range(&mut self, - lbound: &BigInt, - ubound: &BigInt) - -> BigInt { - assert!(*lbound < *ubound); - let delta = (*ubound - *lbound).to_biguint().unwrap(); - return *lbound + self.gen_biguint_below(&delta).to_bigint().unwrap(); - } -} - -impl BigInt { - /// Creates and initializes a BigInt. - /// - /// The digits are be in base 2^32. - #[inline] - pub fn new(sign: Sign, digits: Vec) -> BigInt { - BigInt::from_biguint(sign, BigUint::new(digits)) - } - - /// Creates and initializes a `BigInt`. - /// - /// The digits are be in base 2^32. - #[inline] - pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt { - if sign == NoSign || data.is_zero() { - return BigInt { sign: NoSign, data: Zero::zero() }; - } - BigInt { sign: sign, data: data } - } - - /// Creates and initializes a `BigInt`. - #[inline] - pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt { - BigInt::from_biguint(sign, BigUint::from_slice(slice)) - } - - /// Creates and initializes a `BigInt`. - pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { - if buf.is_empty() { return None; } - let mut sign = Plus; - let mut start = 0; - if buf[0] == b'-' { - sign = Minus; - start = 1; - } - return BigUint::parse_bytes(buf[start..], radix) - .map(|bu| BigInt::from_biguint(sign, bu)); - } - - /// Converts this `BigInt` into a `BigUint`, if it's not negative. - #[inline] - pub fn to_biguint(&self) -> Option { - match self.sign { - Plus => Some(self.data.clone()), - NoSign => Some(Zero::zero()), - Minus => None - } - } -} - -#[cfg(test)] -mod biguint_tests { - use Integer; - use super::{BigDigit, BigUint, ToBigUint}; - use super::{Plus, BigInt, RandBigInt, ToBigInt}; - - use std::cmp::{Less, Equal, Greater}; - use std::from_str::FromStr; - use std::i64; - use std::num::{Zero, One, FromStrRadix, ToStrRadix}; - use std::num::{ToPrimitive, FromPrimitive}; - use std::num::CheckedDiv; - use std::rand::task_rng; - use std::u64; - use std::hash::hash; - - #[test] - fn test_from_slice() { - fn check(slice: &[BigDigit], data: &[BigDigit]) { - assert!(data == BigUint::from_slice(slice).data.as_slice()); - } - check([1], [1]); - check([0, 0, 0], []); - check([1, 2, 0, 0], [1, 2]); - check([0, 0, 1, 2], [0, 0, 1, 2]); - check([0, 0, 1, 2, 0, 0], [0, 0, 1, 2]); - check([-1], [-1]); - } - - #[test] - fn test_cmp() { - let data: [&[_], ..7] = [ &[], &[1], &[2], &[-1], &[0, 1], &[2, 1], &[1, 1, 1] ]; - let data: Vec = data.iter().map(|v| BigUint::from_slice(*v)).collect(); - for (i, ni) in data.iter().enumerate() { - for (j0, nj) in data.slice(i, data.len()).iter().enumerate() { - let j = j0 + i; - if i == j { - assert_eq!(ni.cmp(nj), Equal); - assert_eq!(nj.cmp(ni), Equal); - assert_eq!(ni, nj); - assert!(!(ni != nj)); - assert!(ni <= nj); - assert!(ni >= nj); - assert!(!(ni < nj)); - assert!(!(ni > nj)); - } else { - assert_eq!(ni.cmp(nj), Less); - assert_eq!(nj.cmp(ni), Greater); - - assert!(!(ni == nj)); - assert!(ni != nj); - - assert!(ni <= nj); - assert!(!(ni >= nj)); - assert!(ni < nj); - assert!(!(ni > nj)); - - assert!(!(nj <= ni)); - assert!(nj >= ni); - assert!(!(nj < ni)); - assert!(nj > ni); - } - } - } - } - - #[test] - fn test_hash() { - let a = BigUint::new(vec!()); - let b = BigUint::new(vec!(0)); - let c = BigUint::new(vec!(1)); - let d = BigUint::new(vec!(1,0,0,0,0,0)); - let e = BigUint::new(vec!(0,0,0,0,0,1)); - assert!(hash(&a) == hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) == hash(&d)); - assert!(hash(&d) != hash(&e)); - } - - #[test] - fn test_bitand() { - fn check(left: &[BigDigit], - right: &[BigDigit], - expected: &[BigDigit]) { - assert_eq!(BigUint::from_slice(left) & BigUint::from_slice(right), - BigUint::from_slice(expected)); - } - check([], [], []); - check([268, 482, 17], - [964, 54], - [260, 34]); - } - - #[test] - fn test_bitor() { - fn check(left: &[BigDigit], - right: &[BigDigit], - expected: &[BigDigit]) { - assert_eq!(BigUint::from_slice(left) | BigUint::from_slice(right), - BigUint::from_slice(expected)); - } - check([], [], []); - check([268, 482, 17], - [964, 54], - [972, 502, 17]); - } - - #[test] - fn test_bitxor() { - fn check(left: &[BigDigit], - right: &[BigDigit], - expected: &[BigDigit]) { - assert_eq!(BigUint::from_slice(left) ^ BigUint::from_slice(right), - BigUint::from_slice(expected)); - } - check([], [], []); - check([268, 482, 17], - [964, 54], - [712, 468, 17]); - } - - #[test] - fn test_shl() { - fn check(s: &str, shift: uint, ans: &str) { - let opt_biguint: Option = FromStrRadix::from_str_radix(s, 16); - let bu = (opt_biguint.unwrap() << shift).to_str_radix(16); - assert_eq!(bu.as_slice(), ans); - } - - check("0", 3, "0"); - check("1", 3, "8"); - - check("1\ - 0000\ - 0000\ - 0000\ - 0001\ - 0000\ - 0000\ - 0000\ - 0001", - 3, - "8\ - 0000\ - 0000\ - 0000\ - 0008\ - 0000\ - 0000\ - 0000\ - 0008"); - check("1\ - 0000\ - 0001\ - 0000\ - 0001", - 2, - "4\ - 0000\ - 0004\ - 0000\ - 0004"); - check("1\ - 0001\ - 0001", - 1, - "2\ - 0002\ - 0002"); - - check("\ - 4000\ - 0000\ - 0000\ - 0000", - 3, - "2\ - 0000\ - 0000\ - 0000\ - 0000"); - check("4000\ - 0000", - 2, - "1\ - 0000\ - 0000"); - check("4000", - 2, - "1\ - 0000"); - - check("4000\ - 0000\ - 0000\ - 0000", - 67, - "2\ - 0000\ - 0000\ - 0000\ - 0000\ - 0000\ - 0000\ - 0000\ - 0000"); - check("4000\ - 0000", - 35, - "2\ - 0000\ - 0000\ - 0000\ - 0000"); - check("4000", - 19, - "2\ - 0000\ - 0000"); - - check("fedc\ - ba98\ - 7654\ - 3210\ - fedc\ - ba98\ - 7654\ - 3210", - 4, - "f\ - edcb\ - a987\ - 6543\ - 210f\ - edcb\ - a987\ - 6543\ - 2100"); - check("88887777666655554444333322221111", 16, - "888877776666555544443333222211110000"); - } - - #[test] - fn test_shr() { - fn check(s: &str, shift: uint, ans: &str) { - let opt_biguint: Option = - FromStrRadix::from_str_radix(s, 16); - let bu = (opt_biguint.unwrap() >> shift).to_str_radix(16); - assert_eq!(bu.as_slice(), ans); - } - - check("0", 3, "0"); - check("f", 3, "1"); - - check("1\ - 0000\ - 0000\ - 0000\ - 0001\ - 0000\ - 0000\ - 0000\ - 0001", - 3, - "2000\ - 0000\ - 0000\ - 0000\ - 2000\ - 0000\ - 0000\ - 0000"); - check("1\ - 0000\ - 0001\ - 0000\ - 0001", - 2, - "4000\ - 0000\ - 4000\ - 0000"); - check("1\ - 0001\ - 0001", - 1, - "8000\ - 8000"); - - check("2\ - 0000\ - 0000\ - 0000\ - 0001\ - 0000\ - 0000\ - 0000\ - 0001", - 67, - "4000\ - 0000\ - 0000\ - 0000"); - check("2\ - 0000\ - 0001\ - 0000\ - 0001", - 35, - "4000\ - 0000"); - check("2\ - 0001\ - 0001", - 19, - "4000"); - - check("1\ - 0000\ - 0000\ - 0000\ - 0000", - 1, - "8000\ - 0000\ - 0000\ - 0000"); - check("1\ - 0000\ - 0000", - 1, - "8000\ - 0000"); - check("1\ - 0000", - 1, - "8000"); - check("f\ - edcb\ - a987\ - 6543\ - 210f\ - edcb\ - a987\ - 6543\ - 2100", - 4, - "fedc\ - ba98\ - 7654\ - 3210\ - fedc\ - ba98\ - 7654\ - 3210"); - - check("888877776666555544443333222211110000", 16, - "88887777666655554444333322221111"); - } - - // `DoubleBigDigit` size dependent - #[test] - fn test_convert_i64() { - fn check(b1: BigUint, i: i64) { - let b2: BigUint = FromPrimitive::from_i64(i).unwrap(); - assert!(b1 == b2); - assert!(b1.to_i64().unwrap() == i); - } - - check(Zero::zero(), 0); - check(One::one(), 1); - check(i64::MAX.to_biguint().unwrap(), i64::MAX); - - check(BigUint::new(vec!( )), 0); - check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::bits))); - check(BigUint::new(vec!(-1 )), (1 << (1*BigDigit::bits)) - 1); - check(BigUint::new(vec!( 0, 1 )), (1 << (1*BigDigit::bits))); - check(BigUint::new(vec!(-1, -1 >> 1)), i64::MAX); - - assert_eq!(i64::MIN.to_biguint(), None); - assert_eq!(BigUint::new(vec!(-1, -1 )).to_i64(), None); - assert_eq!(BigUint::new(vec!( 0, 0, 1)).to_i64(), None); - assert_eq!(BigUint::new(vec!(-1, -1, -1)).to_i64(), None); - } - - // `DoubleBigDigit` size dependent - #[test] - fn test_convert_u64() { - fn check(b1: BigUint, u: u64) { - let b2: BigUint = FromPrimitive::from_u64(u).unwrap(); - assert!(b1 == b2); - assert!(b1.to_u64().unwrap() == u); - } - - check(Zero::zero(), 0); - check(One::one(), 1); - check(u64::MIN.to_biguint().unwrap(), u64::MIN); - check(u64::MAX.to_biguint().unwrap(), u64::MAX); - - check(BigUint::new(vec!( )), 0); - check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::bits))); - check(BigUint::new(vec!(-1 )), (1 << (1*BigDigit::bits)) - 1); - check(BigUint::new(vec!( 0, 1)), (1 << (1*BigDigit::bits))); - check(BigUint::new(vec!(-1, -1)), u64::MAX); - - assert_eq!(BigUint::new(vec!( 0, 0, 1)).to_u64(), None); - assert_eq!(BigUint::new(vec!(-1, -1, -1)).to_u64(), None); - } - - #[test] - fn test_convert_to_bigint() { - fn check(n: BigUint, ans: BigInt) { - assert_eq!(n.to_bigint().unwrap(), ans); - assert_eq!(n.to_bigint().unwrap().to_biguint().unwrap(), n); - } - check(Zero::zero(), Zero::zero()); - check(BigUint::new(vec!(1,2,3)), - BigInt::from_biguint(Plus, BigUint::new(vec!(1,2,3)))); - } - - static SUM_TRIPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] = &[ - (&[], &[], &[]), - (&[], &[ 1], &[ 1]), - (&[ 1], &[ 1], &[ 2]), - (&[ 1], &[ 1, 1], &[ 2, 1]), - (&[ 1], &[-1], &[ 0, 1]), - (&[ 1], &[-1, -1], &[ 0, 0, 1]), - (&[-1, -1], &[-1, -1], &[-2, -1, 1]), - (&[ 1, 1, 1], &[-1, -1], &[ 0, 1, 2]), - (&[ 2, 2, 1], &[-1, -2], &[ 1, 1, 2]) - ]; - - #[test] - fn test_add() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(a + b == c); - assert!(b + a == c); - } - } - - #[test] - fn test_sub() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(c - a == b); - assert!(c - b == a); - } - } - - #[test] - #[should_fail] - fn test_sub_fail_on_underflow() { - let (a, b) : (BigUint, BigUint) = (Zero::zero(), One::one()); - a - b; - } - - static MUL_TRIPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] = &[ - (&[], &[], &[]), - (&[], &[ 1], &[]), - (&[ 2], &[], &[]), - (&[ 1], &[ 1], &[1]), - (&[ 2], &[ 3], &[ 6]), - (&[ 1], &[ 1, 1, 1], &[1, 1, 1]), - (&[ 1, 2, 3], &[ 3], &[ 3, 6, 9]), - (&[ 1, 1, 1], &[-1], &[-1, -1, -1]), - (&[ 1, 2, 3], &[-1], &[-1, -2, -2, 2]), - (&[ 1, 2, 3, 4], &[-1], &[-1, -2, -2, -2, 3]), - (&[-1], &[-1], &[ 1, -2]), - (&[-1, -1], &[-1], &[ 1, -1, -2]), - (&[-1, -1, -1], &[-1], &[ 1, -1, -1, -2]), - (&[-1, -1, -1, -1], &[-1], &[ 1, -1, -1, -1, -2]), - (&[-1/2 + 1], &[ 2], &[ 0, 1]), - (&[0, -1/2 + 1], &[ 2], &[ 0, 0, 1]), - (&[ 1, 2], &[ 1, 2, 3], &[1, 4, 7, 6]), - (&[-1, -1], &[-1, -1, -1], &[1, 0, -1, -2, -1]), - (&[-1, -1, -1], &[-1, -1, -1, -1], &[1, 0, 0, -1, -2, -1, -1]), - (&[ 0, 0, 1], &[ 1, 2, 3], &[0, 0, 1, 2, 3]), - (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) - ]; - - static DIV_REM_QUADRUPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] - = &[ - (&[ 1], &[ 2], &[], &[1]), - (&[ 1, 1], &[ 2], &[-1/2+1], &[1]), - (&[ 1, 1, 1], &[ 2], &[-1/2+1, -1/2+1], &[1]), - (&[ 0, 1], &[-1], &[1], &[1]), - (&[-1, -1], &[-2], &[2, 1], &[3]) - ]; - - #[test] - fn test_mul() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(a * b == c); - assert!(b * a == c); - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - let d = BigUint::from_slice(d_vec); - - assert!(a == b * c + d); - assert!(a == c * b + d); - } - } - - #[test] - fn test_div_rem() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - if !a.is_zero() { - assert_eq!(c.div_rem(&a), (b.clone(), Zero::zero())); - } - if !b.is_zero() { - assert_eq!(c.div_rem(&b), (a.clone(), Zero::zero())); - } - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - let d = BigUint::from_slice(d_vec); - - if !b.is_zero() { assert!(a.div_rem(&b) == (c, d)); } - } - } - - #[test] - fn test_checked_add() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(a.checked_add(&b).unwrap() == c); - assert!(b.checked_add(&a).unwrap() == c); - } - } - - #[test] - fn test_checked_sub() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(c.checked_sub(&a).unwrap() == b); - assert!(c.checked_sub(&b).unwrap() == a); - - if a > c { - assert!(a.checked_sub(&c).is_none()); - } - if b > c { - assert!(b.checked_sub(&c).is_none()); - } - } - } - - #[test] - fn test_checked_mul() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - assert!(a.checked_mul(&b).unwrap() == c); - assert!(b.checked_mul(&a).unwrap() == c); - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - let d = BigUint::from_slice(d_vec); - - assert!(a == b.checked_mul(&c).unwrap() + d); - assert!(a == c.checked_mul(&b).unwrap() + d); - } - } - - #[test] - fn test_checked_div() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigUint::from_slice(a_vec); - let b = BigUint::from_slice(b_vec); - let c = BigUint::from_slice(c_vec); - - if !a.is_zero() { - assert!(c.checked_div(&a).unwrap() == b); - } - if !b.is_zero() { - assert!(c.checked_div(&b).unwrap() == a); - } - - assert!(c.checked_div(&Zero::zero()).is_none()); - } - } - - #[test] - fn test_gcd() { - fn check(a: uint, b: uint, c: uint) { - let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); - let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); - let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); - - assert_eq!(big_a.gcd(&big_b), big_c); - } - - check(10, 2, 2); - check(10, 3, 1); - check(0, 3, 3); - check(3, 3, 3); - check(56, 42, 14); - } - - #[test] - fn test_lcm() { - fn check(a: uint, b: uint, c: uint) { - let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); - let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); - let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); - - assert_eq!(big_a.lcm(&big_b), big_c); - } - - check(1, 0, 0); - check(0, 1, 0); - check(1, 1, 1); - check(8, 9, 72); - check(11, 5, 55); - check(99, 17, 1683); - } - - #[test] - fn test_is_even() { - let one: BigUint = FromStr::from_str("1").unwrap(); - let two: BigUint = FromStr::from_str("2").unwrap(); - let thousand: BigUint = FromStr::from_str("1000").unwrap(); - let big: BigUint = FromStr::from_str("1000000000000000000000").unwrap(); - let bigger: BigUint = FromStr::from_str("1000000000000000000001").unwrap(); - assert!(one.is_odd()); - assert!(two.is_even()); - assert!(thousand.is_even()); - assert!(big.is_even()); - assert!(bigger.is_odd()); - assert!((one << 64).is_even()); - assert!(((one << 64) + one).is_odd()); - } - - fn to_str_pairs() -> Vec<(BigUint, Vec<(uint, String)>)> { - let bits = BigDigit::bits; - vec!(( Zero::zero(), vec!( - (2, "0".to_string()), (3, "0".to_string()) - )), ( BigUint::from_slice([ 0xff ]), vec!( - (2, "11111111".to_string()), - (3, "100110".to_string()), - (4, "3333".to_string()), - (5, "2010".to_string()), - (6, "1103".to_string()), - (7, "513".to_string()), - (8, "377".to_string()), - (9, "313".to_string()), - (10, "255".to_string()), - (11, "212".to_string()), - (12, "193".to_string()), - (13, "168".to_string()), - (14, "143".to_string()), - (15, "120".to_string()), - (16, "ff".to_string()) - )), ( BigUint::from_slice([ 0xfff ]), vec!( - (2, "111111111111".to_string()), - (4, "333333".to_string()), - (16, "fff".to_string()) - )), ( BigUint::from_slice([ 1, 2 ]), vec!( - (2, - format!("10{}1", "0".repeat(bits - 1))), - (4, - format!("2{}1", "0".repeat(bits / 2 - 1))), - (10, match bits { - 32 => "8589934593".to_string(), - 16 => "131073".to_string(), - _ => fail!() - }), - (16, - format!("2{}1", "0".repeat(bits / 4 - 1))) - )), ( BigUint::from_slice([ 1, 2, 3 ]), vec!( - (2, - format!("11{}10{}1", - "0".repeat(bits - 2), - "0".repeat(bits - 1))), - (4, - format!("3{}2{}1", - "0".repeat(bits / 2 - 1), - "0".repeat(bits / 2 - 1))), - (10, match bits { - 32 => "55340232229718589441".to_string(), - 16 => "12885032961".to_string(), - _ => fail!() - }), - (16, - format!("3{}2{}1", - "0".repeat(bits / 4 - 1), - "0".repeat(bits / 4 - 1))) - )) ) - } - - #[test] - fn test_to_str_radix() { - let r = to_str_pairs(); - for num_pair in r.iter() { - let &(ref n, ref rs) = num_pair; - for str_pair in rs.iter() { - let &(ref radix, ref str) = str_pair; - assert_eq!(n.to_str_radix(*radix).as_slice(), - str.as_slice()); - } - } - } - - #[test] - fn test_from_str_radix() { - let r = to_str_pairs(); - for num_pair in r.iter() { - let &(ref n, ref rs) = num_pair; - for str_pair in rs.iter() { - let &(ref radix, ref str) = str_pair; - assert_eq!(n, - &FromStrRadix::from_str_radix(str.as_slice(), - *radix).unwrap()); - } - } - - let zed: Option = FromStrRadix::from_str_radix("Z", 10); - assert_eq!(zed, None); - let blank: Option = FromStrRadix::from_str_radix("_", 2); - assert_eq!(blank, None); - let minus_one: Option = FromStrRadix::from_str_radix("-1", - 10); - assert_eq!(minus_one, None); - } - - #[test] - fn test_factor() { - fn factor(n: uint) -> BigUint { - let mut f: BigUint = One::one(); - for i in range(2, n + 1) { - // FIXME(#5992): assignment operator overloads - // f *= FromPrimitive::from_uint(i); - f = f * FromPrimitive::from_uint(i).unwrap(); - } - return f; - } - - fn check(n: uint, s: &str) { - let n = factor(n); - let ans = match FromStrRadix::from_str_radix(s, 10) { - Some(x) => x, None => fail!() - }; - assert_eq!(n, ans); - } - - check(3, "6"); - check(10, "3628800"); - check(20, "2432902008176640000"); - check(30, "265252859812191058636308480000000"); - } - - #[test] - fn test_bits() { - assert_eq!(BigUint::new(vec!(0,0,0,0)).bits(), 0); - let n: BigUint = FromPrimitive::from_uint(0).unwrap(); - assert_eq!(n.bits(), 0); - let n: BigUint = FromPrimitive::from_uint(1).unwrap(); - assert_eq!(n.bits(), 1); - let n: BigUint = FromPrimitive::from_uint(3).unwrap(); - assert_eq!(n.bits(), 2); - let n: BigUint = FromStrRadix::from_str_radix("4000000000", 16).unwrap(); - assert_eq!(n.bits(), 39); - let one: BigUint = One::one(); - assert_eq!((one << 426).bits(), 427); - } - - #[test] - fn test_rand() { - let mut rng = task_rng(); - let _n: BigUint = rng.gen_biguint(137); - assert!(rng.gen_biguint(0).is_zero()); - } - - #[test] - fn test_rand_range() { - let mut rng = task_rng(); - - for _ in range(0u, 10) { - assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), - &FromPrimitive::from_uint(237).unwrap()), - FromPrimitive::from_uint(236).unwrap()); - } - - let l = FromPrimitive::from_uint(403469000 + 2352).unwrap(); - let u = FromPrimitive::from_uint(403469000 + 3513).unwrap(); - for _ in range(0u, 1000) { - let n: BigUint = rng.gen_biguint_below(&u); - assert!(n < u); - - let n: BigUint = rng.gen_biguint_range(&l, &u); - assert!(n >= l); - assert!(n < u); - } - } - - #[test] - #[should_fail] - fn test_zero_rand_range() { - task_rng().gen_biguint_range(&FromPrimitive::from_uint(54).unwrap(), - &FromPrimitive::from_uint(54).unwrap()); - } - - #[test] - #[should_fail] - fn test_negative_rand_range() { - let mut rng = task_rng(); - let l = FromPrimitive::from_uint(2352).unwrap(); - let u = FromPrimitive::from_uint(3513).unwrap(); - // Switching u and l should fail: - let _n: BigUint = rng.gen_biguint_range(&u, &l); - } -} - -#[cfg(test)] -mod bigint_tests { - use Integer; - use super::{BigDigit, BigUint, ToBigUint}; - use super::{Sign, Minus, NoSign, Plus, BigInt, RandBigInt, ToBigInt}; - - use std::cmp::{Less, Equal, Greater}; - use std::i64; - use std::num::CheckedDiv; - use std::num::{Zero, One, FromStrRadix, ToStrRadix}; - use std::num::{ToPrimitive, FromPrimitive}; - use std::rand::task_rng; - use std::u64; - use std::hash::hash; - - #[test] - fn test_from_biguint() { - fn check(inp_s: Sign, inp_n: uint, ans_s: Sign, ans_n: uint) { - let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_uint(inp_n).unwrap()); - let ans = BigInt { sign: ans_s, data: FromPrimitive::from_uint(ans_n).unwrap()}; - assert_eq!(inp, ans); - } - check(Plus, 1, Plus, 1); - check(Plus, 0, NoSign, 0); - check(Minus, 1, Minus, 1); - check(NoSign, 1, NoSign, 0); - } - - #[test] - fn test_cmp() { - let vs: [&[BigDigit], ..4] = [ &[2 as BigDigit], &[1, 1], &[2, 1], &[1, 1, 1] ]; - let mut nums = Vec::new(); - for s in vs.iter().rev() { - nums.push(BigInt::from_slice(Minus, *s)); - } - nums.push(Zero::zero()); - nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, *s))); - - for (i, ni) in nums.iter().enumerate() { - for (j0, nj) in nums.slice(i, nums.len()).iter().enumerate() { - let j = i + j0; - if i == j { - assert_eq!(ni.cmp(nj), Equal); - assert_eq!(nj.cmp(ni), Equal); - assert_eq!(ni, nj); - assert!(!(ni != nj)); - assert!(ni <= nj); - assert!(ni >= nj); - assert!(!(ni < nj)); - assert!(!(ni > nj)); - } else { - assert_eq!(ni.cmp(nj), Less); - assert_eq!(nj.cmp(ni), Greater); - - assert!(!(ni == nj)); - assert!(ni != nj); - - assert!(ni <= nj); - assert!(!(ni >= nj)); - assert!(ni < nj); - assert!(!(ni > nj)); - - assert!(!(nj <= ni)); - assert!(nj >= ni); - assert!(!(nj < ni)); - assert!(nj > ni); - } - } - } - } - - #[test] - fn test_hash() { - let a = BigInt::new(NoSign, vec!()); - let b = BigInt::new(NoSign, vec!(0)); - let c = BigInt::new(Plus, vec!(1)); - let d = BigInt::new(Plus, vec!(1,0,0,0,0,0)); - let e = BigInt::new(Plus, vec!(0,0,0,0,0,1)); - let f = BigInt::new(Minus, vec!(1)); - assert!(hash(&a) == hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) == hash(&d)); - assert!(hash(&d) != hash(&e)); - assert!(hash(&c) != hash(&f)); - } - - #[test] - fn test_convert_i64() { - fn check(b1: BigInt, i: i64) { - let b2: BigInt = FromPrimitive::from_i64(i).unwrap(); - assert!(b1 == b2); - assert!(b1.to_i64().unwrap() == i); - } - - check(Zero::zero(), 0); - check(One::one(), 1); - check(i64::MIN.to_bigint().unwrap(), i64::MIN); - check(i64::MAX.to_bigint().unwrap(), i64::MAX); - - assert_eq!( - (i64::MAX as u64 + 1).to_bigint().unwrap().to_i64(), - None); - - assert_eq!( - BigInt::from_biguint(Plus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_i64(), - None); - - assert_eq!( - BigInt::from_biguint(Minus, BigUint::new(vec!(1,0,0,1<<(BigDigit::bits-1)))).to_i64(), - None); - - assert_eq!( - BigInt::from_biguint(Minus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_i64(), - None); - } - - #[test] - fn test_convert_u64() { - fn check(b1: BigInt, u: u64) { - let b2: BigInt = FromPrimitive::from_u64(u).unwrap(); - assert!(b1 == b2); - assert!(b1.to_u64().unwrap() == u); - } - - check(Zero::zero(), 0); - check(One::one(), 1); - check(u64::MIN.to_bigint().unwrap(), u64::MIN); - check(u64::MAX.to_bigint().unwrap(), u64::MAX); - - assert_eq!( - BigInt::from_biguint(Plus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_u64(), - None); - - let max_value: BigUint = FromPrimitive::from_u64(u64::MAX).unwrap(); - assert_eq!(BigInt::from_biguint(Minus, max_value).to_u64(), None); - assert_eq!(BigInt::from_biguint(Minus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_u64(), None); - } - - #[test] - fn test_convert_to_biguint() { - fn check(n: BigInt, ans_1: BigUint) { - assert_eq!(n.to_biguint().unwrap(), ans_1); - assert_eq!(n.to_biguint().unwrap().to_bigint().unwrap(), n); - } - let zero: BigInt = Zero::zero(); - let unsigned_zero: BigUint = Zero::zero(); - let positive = BigInt::from_biguint( - Plus, BigUint::new(vec!(1,2,3))); - let negative = -positive; - - check(zero, unsigned_zero); - check(positive, BigUint::new(vec!(1,2,3))); - - assert_eq!(negative.to_biguint(), None); - } - - static SUM_TRIPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] = &[ - (&[], &[], &[]), - (&[], &[ 1], &[ 1]), - (&[ 1], &[ 1], &[ 2]), - (&[ 1], &[ 1, 1], &[ 2, 1]), - (&[ 1], &[-1], &[ 0, 1]), - (&[ 1], &[-1, -1], &[ 0, 0, 1]), - (&[-1, -1], &[-1, -1], &[-2, -1, 1]), - (&[ 1, 1, 1], &[-1, -1], &[ 0, 1, 2]), - (&[ 2, 2, 1], &[-1, -2], &[ 1, 1, 2]) - ]; - - #[test] - fn test_add() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(a + b == c); - assert!(b + a == c); - assert!(c + (-a) == b); - assert!(c + (-b) == a); - assert!(a + (-c) == (-b)); - assert!(b + (-c) == (-a)); - assert!((-a) + (-b) == (-c)) - assert!(a + (-a) == Zero::zero()); - } - } - - #[test] - fn test_sub() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(c - a == b); - assert!(c - b == a); - assert!((-b) - a == (-c)) - assert!((-a) - b == (-c)) - assert!(b - (-a) == c); - assert!(a - (-b) == c); - assert!((-c) - (-a) == (-b)); - assert!(a - a == Zero::zero()); - } - } - - static MUL_TRIPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] = &[ - (&[], &[], &[]), - (&[], &[ 1], &[]), - (&[ 2], &[], &[]), - (&[ 1], &[ 1], &[1]), - (&[ 2], &[ 3], &[ 6]), - (&[ 1], &[ 1, 1, 1], &[1, 1, 1]), - (&[ 1, 2, 3], &[ 3], &[ 3, 6, 9]), - (&[ 1, 1, 1], &[-1], &[-1, -1, -1]), - (&[ 1, 2, 3], &[-1], &[-1, -2, -2, 2]), - (&[ 1, 2, 3, 4], &[-1], &[-1, -2, -2, -2, 3]), - (&[-1], &[-1], &[ 1, -2]), - (&[-1, -1], &[-1], &[ 1, -1, -2]), - (&[-1, -1, -1], &[-1], &[ 1, -1, -1, -2]), - (&[-1, -1, -1, -1], &[-1], &[ 1, -1, -1, -1, -2]), - (&[-1/2 + 1], &[ 2], &[ 0, 1]), - (&[0, -1/2 + 1], &[ 2], &[ 0, 0, 1]), - (&[ 1, 2], &[ 1, 2, 3], &[1, 4, 7, 6]), - (&[-1, -1], &[-1, -1, -1], &[1, 0, -1, -2, -1]), - (&[-1, -1, -1], &[-1, -1, -1, -1], &[1, 0, 0, -1, -2, -1, -1]), - (&[ 0, 0, 1], &[ 1, 2, 3], &[0, 0, 1, 2, 3]), - (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) - ]; - - static DIV_REM_QUADRUPLES: &'static [(&'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit], - &'static [BigDigit])] - = &[ - (&[ 1], &[ 2], &[], &[1]), - (&[ 1, 1], &[ 2], &[-1/2+1], &[1]), - (&[ 1, 1, 1], &[ 2], &[-1/2+1, -1/2+1], &[1]), - (&[ 0, 1], &[-1], &[1], &[1]), - (&[-1, -1], &[-2], &[2, 1], &[3]) - ]; - - #[test] - fn test_mul() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(a * b == c); - assert!(b * a == c); - - assert!((-a) * b == -c); - assert!((-b) * a == -c); - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - let d = BigInt::from_slice(Plus, d_vec); - - assert!(a == b * c + d); - assert!(a == c * b + d); - } - } - - #[test] - fn test_div_mod_floor() { - fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) { - let (d, m) = a.div_mod_floor(b); - if !m.is_zero() { - assert_eq!(m.sign, b.sign); - } - assert!(m.abs() <= b.abs()); - assert!(*a == b * d + m); - assert!(d == *ans_d); - assert!(m == *ans_m); - } - - fn check(a: &BigInt, b: &BigInt, d: &BigInt, m: &BigInt) { - if m.is_zero() { - check_sub(a, b, d, m); - check_sub(a, &b.neg(), &d.neg(), m); - check_sub(&a.neg(), b, &d.neg(), m); - check_sub(&a.neg(), &b.neg(), d, m); - } else { - check_sub(a, b, d, m); - check_sub(a, &b.neg(), &(d.neg() - One::one()), &(m - *b)); - check_sub(&a.neg(), b, &(d.neg() - One::one()), &(b - *m)); - check_sub(&a.neg(), &b.neg(), d, &m.neg()); - } - } - - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); } - if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); } - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - let d = BigInt::from_slice(Plus, d_vec); - - if !b.is_zero() { - check(&a, &b, &c, &d); - } - } - } - - - #[test] - fn test_div_rem() { - fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) { - let (q, r) = a.div_rem(b); - if !r.is_zero() { - assert_eq!(r.sign, a.sign); - } - assert!(r.abs() <= b.abs()); - assert!(*a == b * q + r); - assert!(q == *ans_q); - assert!(r == *ans_r); - } - - fn check(a: &BigInt, b: &BigInt, q: &BigInt, r: &BigInt) { - check_sub(a, b, q, r); - check_sub(a, &b.neg(), &q.neg(), r); - check_sub(&a.neg(), b, &q.neg(), &r.neg()); - check_sub(&a.neg(), &b.neg(), q, &r.neg()); - } - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); } - if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); } - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - let d = BigInt::from_slice(Plus, d_vec); - - if !b.is_zero() { - check(&a, &b, &c, &d); - } - } - } - - #[test] - fn test_checked_add() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(a.checked_add(&b).unwrap() == c); - assert!(b.checked_add(&a).unwrap() == c); - assert!(c.checked_add(&(-a)).unwrap() == b); - assert!(c.checked_add(&(-b)).unwrap() == a); - assert!(a.checked_add(&(-c)).unwrap() == (-b)); - assert!(b.checked_add(&(-c)).unwrap() == (-a)); - assert!((-a).checked_add(&(-b)).unwrap() == (-c)) - assert!(a.checked_add(&(-a)).unwrap() == Zero::zero()); - } - } - - #[test] - fn test_checked_sub() { - for elm in SUM_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(c.checked_sub(&a).unwrap() == b); - assert!(c.checked_sub(&b).unwrap() == a); - assert!((-b).checked_sub(&a).unwrap() == (-c)) - assert!((-a).checked_sub(&b).unwrap() == (-c)) - assert!(b.checked_sub(&(-a)).unwrap() == c); - assert!(a.checked_sub(&(-b)).unwrap() == c); - assert!((-c).checked_sub(&(-a)).unwrap() == (-b)); - assert!(a.checked_sub(&a).unwrap() == Zero::zero()); - } - } - - #[test] - fn test_checked_mul() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - assert!(a.checked_mul(&b).unwrap() == c); - assert!(b.checked_mul(&a).unwrap() == c); - - assert!((-a).checked_mul(&b).unwrap() == -c); - assert!((-b).checked_mul(&a).unwrap() == -c); - } - - for elm in DIV_REM_QUADRUPLES.iter() { - let (a_vec, b_vec, c_vec, d_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - let d = BigInt::from_slice(Plus, d_vec); - - assert!(a == b.checked_mul(&c).unwrap() + d); - assert!(a == c.checked_mul(&b).unwrap() + d); - } - } - #[test] - fn test_checked_div() { - for elm in MUL_TRIPLES.iter() { - let (a_vec, b_vec, c_vec) = *elm; - let a = BigInt::from_slice(Plus, a_vec); - let b = BigInt::from_slice(Plus, b_vec); - let c = BigInt::from_slice(Plus, c_vec); - - if !a.is_zero() { - assert!(c.checked_div(&a).unwrap() == b); - assert!((-c).checked_div(&(-a)).unwrap() == b); - assert!((-c).checked_div(&a).unwrap() == -b); - } - if !b.is_zero() { - assert!(c.checked_div(&b).unwrap() == a); - assert!((-c).checked_div(&(-b)).unwrap() == a); - assert!((-c).checked_div(&b).unwrap() == -a); - } - - assert!(c.checked_div(&Zero::zero()).is_none()); - assert!((-c).checked_div(&Zero::zero()).is_none()); - } - } - - #[test] - fn test_gcd() { - fn check(a: int, b: int, c: int) { - let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); - let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); - let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); - - assert_eq!(big_a.gcd(&big_b), big_c); - } - - check(10, 2, 2); - check(10, 3, 1); - check(0, 3, 3); - check(3, 3, 3); - check(56, 42, 14); - check(3, -3, 3); - check(-6, 3, 3); - check(-4, -2, 2); - } - - #[test] - fn test_lcm() { - fn check(a: int, b: int, c: int) { - let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); - let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); - let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); - - assert_eq!(big_a.lcm(&big_b), big_c); - } - - check(1, 0, 0); - check(0, 1, 0); - check(1, 1, 1); - check(-1, 1, 1); - check(1, -1, 1); - check(-1, -1, 1); - check(8, 9, 72); - check(11, 5, 55); - } - - #[test] - fn test_abs_sub() { - let zero: BigInt = Zero::zero(); - let one: BigInt = One::one(); - assert_eq!((-one).abs_sub(&one), zero); - let one: BigInt = One::one(); - let zero: BigInt = Zero::zero(); - assert_eq!(one.abs_sub(&one), zero); - let one: BigInt = One::one(); - let zero: BigInt = Zero::zero(); - assert_eq!(one.abs_sub(&zero), one); - let one: BigInt = One::one(); - let two: BigInt = FromPrimitive::from_int(2).unwrap(); - assert_eq!(one.abs_sub(&-one), two); - } - - #[test] - fn test_to_str_radix() { - fn check(n: int, ans: &str) { - let n: BigInt = FromPrimitive::from_int(n).unwrap(); - assert!(ans == n.to_str_radix(10).as_slice()); - } - check(10, "10"); - check(1, "1"); - check(0, "0"); - check(-1, "-1"); - check(-10, "-10"); - } - - - #[test] - fn test_from_str_radix() { - fn check(s: &str, ans: Option) { - let ans = ans.map(|n| { - let x: BigInt = FromPrimitive::from_int(n).unwrap(); - x - }); - assert_eq!(FromStrRadix::from_str_radix(s, 10), ans); - } - check("10", Some(10)); - check("1", Some(1)); - check("0", Some(0)); - check("-1", Some(-1)); - check("-10", Some(-10)); - check("Z", None); - check("_", None); - - // issue 10522, this hit an edge case that caused it to - // attempt to allocate a vector of size (-1u) == huge. - let x: BigInt = - from_str(format!("1{}", "0".repeat(36)).as_slice()).unwrap(); - let _y = x.to_string(); - } - - #[test] - fn test_neg() { - assert!(-BigInt::new(Plus, vec!(1, 1, 1)) == - BigInt::new(Minus, vec!(1, 1, 1))); - assert!(-BigInt::new(Minus, vec!(1, 1, 1)) == - BigInt::new(Plus, vec!(1, 1, 1))); - let zero: BigInt = Zero::zero(); - assert_eq!(-zero, zero); - } - - #[test] - fn test_rand() { - let mut rng = task_rng(); - let _n: BigInt = rng.gen_bigint(137); - assert!(rng.gen_bigint(0).is_zero()); - } - - #[test] - fn test_rand_range() { - let mut rng = task_rng(); - - for _ in range(0u, 10) { - assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), - &FromPrimitive::from_uint(237).unwrap()), - FromPrimitive::from_uint(236).unwrap()); - } - - fn check(l: BigInt, u: BigInt) { - let mut rng = task_rng(); - for _ in range(0u, 1000) { - let n: BigInt = rng.gen_bigint_range(&l, &u); - assert!(n >= l); - assert!(n < u); - } - } - let l: BigInt = FromPrimitive::from_uint(403469000 + 2352).unwrap(); - let u: BigInt = FromPrimitive::from_uint(403469000 + 3513).unwrap(); - check( l.clone(), u.clone()); - check(-l.clone(), u.clone()); - check(-u.clone(), -l.clone()); - } - - #[test] - #[should_fail] - fn test_zero_rand_range() { - task_rng().gen_bigint_range(&FromPrimitive::from_int(54).unwrap(), - &FromPrimitive::from_int(54).unwrap()); - } - - #[test] - #[should_fail] - fn test_negative_rand_range() { - let mut rng = task_rng(); - let l = FromPrimitive::from_uint(2352).unwrap(); - let u = FromPrimitive::from_uint(3513).unwrap(); - // Switching u and l should fail: - let _n: BigInt = rng.gen_bigint_range(&u, &l); - } -} - -#[cfg(test)] -mod bench { - extern crate test; - use self::test::Bencher; - use super::BigUint; - use std::iter; - use std::mem::replace; - use std::num::{FromPrimitive, Zero, One}; - - fn factorial(n: uint) -> BigUint { - let mut f: BigUint = One::one(); - for i in iter::range_inclusive(1, n) { - f = f * FromPrimitive::from_uint(i).unwrap(); - } - f - } - - fn fib(n: uint) -> BigUint { - let mut f0: BigUint = Zero::zero(); - let mut f1: BigUint = One::one(); - for _ in range(0, n) { - let f2 = f0 + f1; - f0 = replace(&mut f1, f2); - } - f0 - } - - #[bench] - fn factorial_100(b: &mut Bencher) { - b.iter(|| { - factorial(100); - }); - } - - #[bench] - fn fib_100(b: &mut Bencher) { - b.iter(|| { - fib(100); - }); - } - - #[bench] - fn to_string(b: &mut Bencher) { - let fac = factorial(100); - let fib = fib(100); - b.iter(|| { - fac.to_string(); - }); - b.iter(|| { - fib.to_string(); - }); - } - - #[bench] - fn shr(b: &mut Bencher) { - let n = { let one : BigUint = One::one(); one << 1000 }; - b.iter(|| { - let mut m = n.clone(); - for _ in range(0u, 10) { - m = m >> 1; - } - }) - } -} diff --git a/src/libnum/complex.rs b/src/libnum/complex.rs deleted file mode 100644 index 6690b1d5ddc7a..0000000000000 --- a/src/libnum/complex.rs +++ /dev/null @@ -1,379 +0,0 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - - -//! Complex numbers. - -use std::fmt; -use std::num::{Zero, One, ToStrRadix}; - -// FIXME #1284: handle complex NaN & infinity etc. This -// probably doesn't map to C's _Complex correctly. - -/// A complex number in Cartesian form. -#[deriving(PartialEq, Clone, Hash)] -pub struct Complex { - /// Real portion of the complex number - pub re: T, - /// Imaginary portion of the complex number - pub im: T -} - -pub type Complex32 = Complex; -pub type Complex64 = Complex; - -impl Complex { - /// Create a new Complex - #[inline] - pub fn new(re: T, im: T) -> Complex { - Complex { re: re, im: im } - } - - /// Returns the square of the norm (since `T` doesn't necessarily - /// have a sqrt function), i.e. `re^2 + im^2`. - #[inline] - pub fn norm_sqr(&self) -> T { - self.re * self.re + self.im * self.im - } - - - /// Returns the complex conjugate. i.e. `re - i im` - #[inline] - pub fn conj(&self) -> Complex { - Complex::new(self.re.clone(), -self.im) - } - - - /// Multiplies `self` by the scalar `t`. - #[inline] - pub fn scale(&self, t: T) -> Complex { - Complex::new(self.re * t, self.im * t) - } - - /// Divides `self` by the scalar `t`. - #[inline] - pub fn unscale(&self, t: T) -> Complex { - Complex::new(self.re / t, self.im / t) - } - - /// Returns `1/self` - #[inline] - pub fn inv(&self) -> Complex { - let norm_sqr = self.norm_sqr(); - Complex::new(self.re / norm_sqr, - -self.im / norm_sqr) - } -} - -impl Complex { - /// Calculate |self| - #[inline] - pub fn norm(&self) -> T { - self.re.hypot(self.im) - } -} - -impl Complex { - /// Calculate the principal Arg of self. - #[inline] - pub fn arg(&self) -> T { - self.im.atan2(self.re) - } - /// Convert to polar form (r, theta), such that `self = r * exp(i - /// * theta)` - #[inline] - pub fn to_polar(&self) -> (T, T) { - (self.norm(), self.arg()) - } - /// Convert a polar representation into a complex number. - #[inline] - pub fn from_polar(r: &T, theta: &T) -> Complex { - Complex::new(*r * theta.cos(), *r * theta.sin()) - } -} - -/* arithmetic */ -// (a + i b) + (c + i d) == (a + c) + i (b + d) -impl Add, Complex> for Complex { - #[inline] - fn add(&self, other: &Complex) -> Complex { - Complex::new(self.re + other.re, self.im + other.im) - } -} -// (a + i b) - (c + i d) == (a - c) + i (b - d) -impl Sub, Complex> for Complex { - #[inline] - fn sub(&self, other: &Complex) -> Complex { - Complex::new(self.re - other.re, self.im - other.im) - } -} -// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) -impl Mul, Complex> for Complex { - #[inline] - fn mul(&self, other: &Complex) -> Complex { - Complex::new(self.re*other.re - self.im*other.im, - self.re*other.im + self.im*other.re) - } -} - -// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) -// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] -impl Div, Complex> for Complex { - #[inline] - fn div(&self, other: &Complex) -> Complex { - let norm_sqr = other.norm_sqr(); - Complex::new((self.re*other.re + self.im*other.im) / norm_sqr, - (self.im*other.re - self.re*other.im) / norm_sqr) - } -} - -impl Neg> for Complex { - #[inline] - fn neg(&self) -> Complex { - Complex::new(-self.re, -self.im) - } -} - -/* constants */ -impl Zero for Complex { - #[inline] - fn zero() -> Complex { - Complex::new(Zero::zero(), Zero::zero()) - } - - #[inline] - fn is_zero(&self) -> bool { - self.re.is_zero() && self.im.is_zero() - } -} - -impl One for Complex { - #[inline] - fn one() -> Complex { - Complex::new(One::one(), Zero::zero()) - } -} - -/* string conversions */ -impl fmt::Show for Complex { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.im < Zero::zero() { - write!(f, "{}-{}i", self.re, -self.im) - } else { - write!(f, "{}+{}i", self.re, self.im) - } - } -} - -impl ToStrRadix for Complex { - fn to_str_radix(&self, radix: uint) -> String { - if self.im < Zero::zero() { - format!("{}-{}i", - self.re.to_str_radix(radix), - (-self.im).to_str_radix(radix)) - } else { - format!("{}+{}i", - self.re.to_str_radix(radix), - self.im.to_str_radix(radix)) - } - } -} - -#[cfg(test)] -mod test { - #![allow(non_uppercase_statics)] - - use super::{Complex64, Complex}; - use std::num::{Zero, One, Float}; - use std::hash::hash; - - pub const _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 }; - pub const _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 }; - pub const _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 }; - pub const _0_1i : Complex64 = Complex { re: 0.0, im: 1.0 }; - pub const _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 }; - pub const _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 }; - pub const all_consts : [Complex64, .. 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i]; - - #[test] - fn test_consts() { - // check our constants are what Complex::new creates - fn test(c : Complex64, r : f64, i: f64) { - assert_eq!(c, Complex::new(r,i)); - } - test(_0_0i, 0.0, 0.0); - test(_1_0i, 1.0, 0.0); - test(_1_1i, 1.0, 1.0); - test(_neg1_1i, -1.0, 1.0); - test(_05_05i, 0.5, 0.5); - - assert_eq!(_0_0i, Zero::zero()); - assert_eq!(_1_0i, One::one()); - } - - #[test] - #[cfg_attr(target_arch = "x86", ignore)] - // FIXME #7158: (maybe?) currently failing on x86. - fn test_norm() { - fn test(c: Complex64, ns: f64) { - assert_eq!(c.norm_sqr(), ns); - assert_eq!(c.norm(), ns.sqrt()) - } - test(_0_0i, 0.0); - test(_1_0i, 1.0); - test(_1_1i, 2.0); - test(_neg1_1i, 2.0); - test(_05_05i, 0.5); - } - - #[test] - fn test_scale_unscale() { - assert_eq!(_05_05i.scale(2.0), _1_1i); - assert_eq!(_1_1i.unscale(2.0), _05_05i); - for &c in all_consts.iter() { - assert_eq!(c.scale(2.0).unscale(2.0), c); - } - } - - #[test] - fn test_conj() { - for &c in all_consts.iter() { - assert_eq!(c.conj(), Complex::new(c.re, -c.im)); - assert_eq!(c.conj().conj(), c); - } - } - - #[test] - fn test_inv() { - assert_eq!(_1_1i.inv(), _05_05i.conj()); - assert_eq!(_1_0i.inv(), _1_0i.inv()); - } - - #[test] - #[should_fail] - fn test_divide_by_zero_natural() { - let n = Complex::new(2i, 3i); - let d = Complex::new(0, 0); - let _x = n / d; - } - - #[test] - #[should_fail] - #[ignore] - fn test_inv_zero() { - // FIXME #5736: should this really fail, or just NaN? - _0_0i.inv(); - } - - #[test] - fn test_arg() { - fn test(c: Complex64, arg: f64) { - assert!((c.arg() - arg).abs() < 1.0e-6) - } - test(_1_0i, 0.0); - test(_1_1i, 0.25 * Float::pi()); - test(_neg1_1i, 0.75 * Float::pi()); - test(_05_05i, 0.25 * Float::pi()); - } - - #[test] - fn test_polar_conv() { - fn test(c: Complex64) { - let (r, theta) = c.to_polar(); - assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6); - } - for &c in all_consts.iter() { test(c); } - } - - mod arith { - use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts}; - use std::num::Zero; - - #[test] - fn test_add() { - assert_eq!(_05_05i + _05_05i, _1_1i); - assert_eq!(_0_1i + _1_0i, _1_1i); - assert_eq!(_1_0i + _neg1_1i, _0_1i); - - for &c in all_consts.iter() { - assert_eq!(_0_0i + c, c); - assert_eq!(c + _0_0i, c); - } - } - - #[test] - fn test_sub() { - assert_eq!(_05_05i - _05_05i, _0_0i); - assert_eq!(_0_1i - _1_0i, _neg1_1i); - assert_eq!(_0_1i - _neg1_1i, _1_0i); - - for &c in all_consts.iter() { - assert_eq!(c - _0_0i, c); - assert_eq!(c - c, _0_0i); - } - } - - #[test] - fn test_mul() { - assert_eq!(_05_05i * _05_05i, _0_1i.unscale(2.0)); - assert_eq!(_1_1i * _0_1i, _neg1_1i); - - // i^2 & i^4 - assert_eq!(_0_1i * _0_1i, -_1_0i); - assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i); - - for &c in all_consts.iter() { - assert_eq!(c * _1_0i, c); - assert_eq!(_1_0i * c, c); - } - } - #[test] - fn test_div() { - assert_eq!(_neg1_1i / _0_1i, _1_1i); - for &c in all_consts.iter() { - if c != Zero::zero() { - assert_eq!(c / c, _1_0i); - } - } - } - #[test] - fn test_neg() { - assert_eq!(-_1_0i + _0_1i, _neg1_1i); - assert_eq!((-_0_1i) * _0_1i, _1_0i); - for &c in all_consts.iter() { - assert_eq!(-(-c), c); - } - } - } - - #[test] - fn test_to_string() { - fn test(c : Complex64, s: String) { - assert_eq!(c.to_string(), s); - } - test(_0_0i, "0+0i".to_string()); - test(_1_0i, "1+0i".to_string()); - test(_0_1i, "0+1i".to_string()); - test(_1_1i, "1+1i".to_string()); - test(_neg1_1i, "-1+1i".to_string()); - test(-_neg1_1i, "1-1i".to_string()); - test(_05_05i, "0.5+0.5i".to_string()); - } - - #[test] - fn test_hash() { - let a = Complex::new(0i32, 0i32); - let b = Complex::new(1i32, 0i32); - let c = Complex::new(0i32, 1i32); - assert!(hash(&a) != hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) != hash(&a)); - } -} diff --git a/src/libnum/integer.rs b/src/libnum/integer.rs deleted file mode 100644 index c5d076a70b582..0000000000000 --- a/src/libnum/integer.rs +++ /dev/null @@ -1,507 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Integer trait and functions. - -pub trait Integer: Num + PartialOrd - + Div - + Rem { - /// Floored integer division. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert!(( 8i).div_floor(& 3) == 2); - /// assert!(( 8i).div_floor(&-3) == -3); - /// assert!((-8i).div_floor(& 3) == -3); - /// assert!((-8i).div_floor(&-3) == 2); - /// - /// assert!(( 1i).div_floor(& 2) == 0); - /// assert!(( 1i).div_floor(&-2) == -1); - /// assert!((-1i).div_floor(& 2) == -1); - /// assert!((-1i).div_floor(&-2) == 0); - /// ``` - fn div_floor(&self, other: &Self) -> Self; - - /// Floored integer modulo, satisfying: - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// # let n = 1i; let d = 1i; - /// assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n) - /// ``` - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert!(( 8i).mod_floor(& 3) == 2); - /// assert!(( 8i).mod_floor(&-3) == -1); - /// assert!((-8i).mod_floor(& 3) == 1); - /// assert!((-8i).mod_floor(&-3) == -2); - /// - /// assert!(( 1i).mod_floor(& 2) == 1); - /// assert!(( 1i).mod_floor(&-2) == -1); - /// assert!((-1i).mod_floor(& 2) == 1); - /// assert!((-1i).mod_floor(&-2) == -1); - /// ``` - fn mod_floor(&self, other: &Self) -> Self; - - /// Greatest Common Divisor (GCD). - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(6i.gcd(&8), 2); - /// assert_eq!(7i.gcd(&3), 1); - /// ``` - fn gcd(&self, other: &Self) -> Self; - - /// Lowest Common Multiple (LCM). - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(7i.lcm(&3), 21); - /// assert_eq!(2i.lcm(&4), 4); - /// ``` - fn lcm(&self, other: &Self) -> Self; - - /// Deprecated, use `is_multiple_of` instead. - #[deprecated = "function renamed to `is_multiple_of`"] - fn divides(&self, other: &Self) -> bool; - - /// Returns `true` if `other` is a multiple of `self`. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(9i.is_multiple_of(&3), true); - /// assert_eq!(3i.is_multiple_of(&9), false); - /// ``` - fn is_multiple_of(&self, other: &Self) -> bool; - - /// Returns `true` if the number is even. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(3i.is_even(), false); - /// assert_eq!(4i.is_even(), true); - /// ``` - fn is_even(&self) -> bool; - - /// Returns `true` if the number is odd. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(3i.is_odd(), true); - /// assert_eq!(4i.is_odd(), false); - /// ``` - fn is_odd(&self) -> bool; - - /// Simultaneous truncated integer division and modulus. - /// Returns `(quotient, remainder)`. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(( 8i).div_rem( &3), ( 2, 2)); - /// assert_eq!(( 8i).div_rem(&-3), (-2, 2)); - /// assert_eq!((-8i).div_rem( &3), (-2, -2)); - /// assert_eq!((-8i).div_rem(&-3), ( 2, -2)); - /// - /// assert_eq!(( 1i).div_rem( &2), ( 0, 1)); - /// assert_eq!(( 1i).div_rem(&-2), ( 0, 1)); - /// assert_eq!((-1i).div_rem( &2), ( 0, -1)); - /// assert_eq!((-1i).div_rem(&-2), ( 0, -1)); - /// ``` - #[inline] - fn div_rem(&self, other: &Self) -> (Self, Self) { - (*self / *other, *self % *other) - } - - /// Simultaneous floored integer division and modulus. - /// Returns `(quotient, remainder)`. - /// - /// # Examples - /// - /// ``` - /// # #![allow(deprecated)] - /// # use num::Integer; - /// assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2)); - /// assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1)); - /// assert_eq!((-8i).div_mod_floor( &3), (-3, 1)); - /// assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2)); - /// - /// assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1)); - /// assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1)); - /// assert_eq!((-1i).div_mod_floor( &2), (-1, 1)); - /// assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1)); - /// ``` - fn div_mod_floor(&self, other: &Self) -> (Self, Self) { - (self.div_floor(other), self.mod_floor(other)) - } -} - -/// Simultaneous integer division and modulus -#[inline] pub fn div_rem(x: T, y: T) -> (T, T) { x.div_rem(&y) } -/// Floored integer division -#[inline] pub fn div_floor(x: T, y: T) -> T { x.div_floor(&y) } -/// Floored integer modulus -#[inline] pub fn mod_floor(x: T, y: T) -> T { x.mod_floor(&y) } -/// Simultaneous floored integer division and modulus -#[inline] pub fn div_mod_floor(x: T, y: T) -> (T, T) { x.div_mod_floor(&y) } - -/// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The -/// result is always positive. -#[inline(always)] pub fn gcd(x: T, y: T) -> T { x.gcd(&y) } -/// Calculates the Lowest Common Multiple (LCM) of the number and `other`. -#[inline(always)] pub fn lcm(x: T, y: T) -> T { x.lcm(&y) } - -macro_rules! impl_integer_for_int { - ($T:ty, $test_mod:ident) => ( - impl Integer for $T { - /// Floored integer division - #[inline] - fn div_floor(&self, other: &$T) -> $T { - // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, - // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) - match self.div_rem(other) { - (d, r) if (r > 0 && *other < 0) - || (r < 0 && *other > 0) => d - 1, - (d, _) => d, - } - } - - /// Floored integer modulo - #[inline] - fn mod_floor(&self, other: &$T) -> $T { - // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, - // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) - match *self % *other { - r if (r > 0 && *other < 0) - || (r < 0 && *other > 0) => r + *other, - r => r, - } - } - - /// Calculates `div_floor` and `mod_floor` simultaneously - #[inline] - fn div_mod_floor(&self, other: &$T) -> ($T,$T) { - // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, - // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) - match self.div_rem(other) { - (d, r) if (r > 0 && *other < 0) - || (r < 0 && *other > 0) => (d - 1, r + *other), - (d, r) => (d, r), - } - } - - /// Calculates the Greatest Common Divisor (GCD) of the number and - /// `other`. The result is always positive. - #[inline] - fn gcd(&self, other: &$T) -> $T { - // Use Euclid's algorithm - let mut m = *self; - let mut n = *other; - while m != 0 { - let temp = m; - m = n % temp; - n = temp; - } - n.abs() - } - - /// Calculates the Lowest Common Multiple (LCM) of the number and - /// `other`. - #[inline] - fn lcm(&self, other: &$T) -> $T { - // should not have to recalculate abs - ((*self * *other) / self.gcd(other)).abs() - } - - /// Deprecated, use `is_multiple_of` instead. - #[deprecated = "function renamed to `is_multiple_of`"] - #[inline] - fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } - - /// Returns `true` if the number is a multiple of `other`. - #[inline] - fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } - - /// Returns `true` if the number is divisible by `2` - #[inline] - fn is_even(&self) -> bool { self & 1 == 0 } - - /// Returns `true` if the number is not divisible by `2` - #[inline] - fn is_odd(&self) -> bool { !self.is_even() } - } - - #[cfg(test)] - mod $test_mod { - use Integer; - - /// Checks that the division rule holds for: - /// - /// - `n`: numerator (dividend) - /// - `d`: denominator (divisor) - /// - `qr`: quotient and remainder - #[cfg(test)] - fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) { - assert_eq!(d * q + r, n); - } - - #[test] - fn test_div_rem() { - fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) { - let (n,d) = nd; - let separate_div_rem = (n / d, n % d); - let combined_div_rem = n.div_rem(&d); - - assert_eq!(separate_div_rem, qr); - assert_eq!(combined_div_rem, qr); - - test_division_rule(nd, separate_div_rem); - test_division_rule(nd, combined_div_rem); - } - - test_nd_dr(( 8, 3), ( 2, 2)); - test_nd_dr(( 8, -3), (-2, 2)); - test_nd_dr((-8, 3), (-2, -2)); - test_nd_dr((-8, -3), ( 2, -2)); - - test_nd_dr(( 1, 2), ( 0, 1)); - test_nd_dr(( 1, -2), ( 0, 1)); - test_nd_dr((-1, 2), ( 0, -1)); - test_nd_dr((-1, -2), ( 0, -1)); - } - - #[test] - fn test_div_mod_floor() { - fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) { - let (n,d) = nd; - let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d)); - let combined_div_mod_floor = n.div_mod_floor(&d); - - assert_eq!(separate_div_mod_floor, dm); - assert_eq!(combined_div_mod_floor, dm); - - test_division_rule(nd, separate_div_mod_floor); - test_division_rule(nd, combined_div_mod_floor); - } - - test_nd_dm(( 8, 3), ( 2, 2)); - test_nd_dm(( 8, -3), (-3, -1)); - test_nd_dm((-8, 3), (-3, 1)); - test_nd_dm((-8, -3), ( 2, -2)); - - test_nd_dm(( 1, 2), ( 0, 1)); - test_nd_dm(( 1, -2), (-1, -1)); - test_nd_dm((-1, 2), (-1, 1)); - test_nd_dm((-1, -2), ( 0, -1)); - } - - #[test] - fn test_gcd() { - assert_eq!((10 as $T).gcd(&2), 2 as $T); - assert_eq!((10 as $T).gcd(&3), 1 as $T); - assert_eq!((0 as $T).gcd(&3), 3 as $T); - assert_eq!((3 as $T).gcd(&3), 3 as $T); - assert_eq!((56 as $T).gcd(&42), 14 as $T); - assert_eq!((3 as $T).gcd(&-3), 3 as $T); - assert_eq!((-6 as $T).gcd(&3), 3 as $T); - assert_eq!((-4 as $T).gcd(&-2), 2 as $T); - } - - #[test] - fn test_lcm() { - assert_eq!((1 as $T).lcm(&0), 0 as $T); - assert_eq!((0 as $T).lcm(&1), 0 as $T); - assert_eq!((1 as $T).lcm(&1), 1 as $T); - assert_eq!((-1 as $T).lcm(&1), 1 as $T); - assert_eq!((1 as $T).lcm(&-1), 1 as $T); - assert_eq!((-1 as $T).lcm(&-1), 1 as $T); - assert_eq!((8 as $T).lcm(&9), 72 as $T); - assert_eq!((11 as $T).lcm(&5), 55 as $T); - } - - #[test] - fn test_even() { - assert_eq!((-4 as $T).is_even(), true); - assert_eq!((-3 as $T).is_even(), false); - assert_eq!((-2 as $T).is_even(), true); - assert_eq!((-1 as $T).is_even(), false); - assert_eq!((0 as $T).is_even(), true); - assert_eq!((1 as $T).is_even(), false); - assert_eq!((2 as $T).is_even(), true); - assert_eq!((3 as $T).is_even(), false); - assert_eq!((4 as $T).is_even(), true); - } - - #[test] - fn test_odd() { - assert_eq!((-4 as $T).is_odd(), false); - assert_eq!((-3 as $T).is_odd(), true); - assert_eq!((-2 as $T).is_odd(), false); - assert_eq!((-1 as $T).is_odd(), true); - assert_eq!((0 as $T).is_odd(), false); - assert_eq!((1 as $T).is_odd(), true); - assert_eq!((2 as $T).is_odd(), false); - assert_eq!((3 as $T).is_odd(), true); - assert_eq!((4 as $T).is_odd(), false); - } - } - ) -} - -impl_integer_for_int!(i8, test_integer_i8) -impl_integer_for_int!(i16, test_integer_i16) -impl_integer_for_int!(i32, test_integer_i32) -impl_integer_for_int!(i64, test_integer_i64) -impl_integer_for_int!(int, test_integer_int) - -macro_rules! impl_integer_for_uint { - ($T:ty, $test_mod:ident) => ( - impl Integer for $T { - /// Unsigned integer division. Returns the same result as `div` (`/`). - #[inline] - fn div_floor(&self, other: &$T) -> $T { *self / *other } - - /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`). - #[inline] - fn mod_floor(&self, other: &$T) -> $T { *self % *other } - - /// Calculates the Greatest Common Divisor (GCD) of the number and `other` - #[inline] - fn gcd(&self, other: &$T) -> $T { - // Use Euclid's algorithm - let mut m = *self; - let mut n = *other; - while m != 0 { - let temp = m; - m = n % temp; - n = temp; - } - n - } - - /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. - #[inline] - fn lcm(&self, other: &$T) -> $T { - (*self * *other) / self.gcd(other) - } - - /// Deprecated, use `is_multiple_of` instead. - #[deprecated = "function renamed to `is_multiple_of`"] - #[inline] - fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } - - /// Returns `true` if the number is a multiple of `other`. - #[inline] - fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } - - /// Returns `true` if the number is divisible by `2`. - #[inline] - fn is_even(&self) -> bool { self & 1 == 0 } - - /// Returns `true` if the number is not divisible by `2`. - #[inline] - fn is_odd(&self) -> bool { !self.is_even() } - } - - #[cfg(test)] - mod $test_mod { - use Integer; - - #[test] - fn test_div_mod_floor() { - assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T); - assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T); - assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T)); - assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T); - assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T); - assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T)); - assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T); - assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T); - assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T)); - } - - #[test] - fn test_gcd() { - assert_eq!((10 as $T).gcd(&2), 2 as $T); - assert_eq!((10 as $T).gcd(&3), 1 as $T); - assert_eq!((0 as $T).gcd(&3), 3 as $T); - assert_eq!((3 as $T).gcd(&3), 3 as $T); - assert_eq!((56 as $T).gcd(&42), 14 as $T); - } - - #[test] - #[allow(type_overflow)] - fn test_lcm() { - assert_eq!((1 as $T).lcm(&0), 0 as $T); - assert_eq!((0 as $T).lcm(&1), 0 as $T); - assert_eq!((1 as $T).lcm(&1), 1 as $T); - assert_eq!((8 as $T).lcm(&9), 72 as $T); - assert_eq!((11 as $T).lcm(&5), 55 as $T); - assert_eq!((99 as $T).lcm(&17), 1683 as $T); - } - - #[test] - fn test_is_multiple_of() { - assert!((6 as $T).is_multiple_of(&(6 as $T))); - assert!((6 as $T).is_multiple_of(&(3 as $T))); - assert!((6 as $T).is_multiple_of(&(1 as $T))); - } - - #[test] - fn test_even() { - assert_eq!((0 as $T).is_even(), true); - assert_eq!((1 as $T).is_even(), false); - assert_eq!((2 as $T).is_even(), true); - assert_eq!((3 as $T).is_even(), false); - assert_eq!((4 as $T).is_even(), true); - } - - #[test] - fn test_odd() { - assert_eq!((0 as $T).is_odd(), false); - assert_eq!((1 as $T).is_odd(), true); - assert_eq!((2 as $T).is_odd(), false); - assert_eq!((3 as $T).is_odd(), true); - assert_eq!((4 as $T).is_odd(), false); - } - } - ) -} - -impl_integer_for_uint!(u8, test_integer_u8) -impl_integer_for_uint!(u16, test_integer_u16) -impl_integer_for_uint!(u32, test_integer_u32) -impl_integer_for_uint!(u64, test_integer_u64) -impl_integer_for_uint!(uint, test_integer_uint) diff --git a/src/libnum/lib.rs b/src/libnum/lib.rs deleted file mode 100644 index 063bb17e09a6d..0000000000000 --- a/src/libnum/lib.rs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Simple numerics. -//! -//! This crate contains arbitrary-sized integer, rational, and complex types. -//! -//! ## Example -//! -//! This example uses the BigRational type and [Newton's method][newt] to -//! approximate a square root to arbitrary precision: -//! -//! ``` -//! # #![allow(deprecated)] -//! extern crate num; -//! -//! use num::bigint::BigInt; -//! use num::rational::{Ratio, BigRational}; -//! -//! fn approx_sqrt(number: u64, iterations: uint) -> BigRational { -//! let start: Ratio = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); -//! let mut approx = start.clone(); -//! -//! for _ in range(0, iterations) { -//! approx = (approx + (start / approx)) / -//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap()); -//! } -//! -//! approx -//! } -//! -//! fn main() { -//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 -//! } -//! ``` -//! -//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method - -#![allow(unknown_features)] -#![feature(macro_rules, slicing_syntax)] -#![feature(default_type_params)] - -#![crate_name = "num"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/num"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/", - html_playground_url = "http://play.rust-lang.org/")] -#![allow(deprecated)] // from_str_radix - -extern crate rand; - -pub use bigint::{BigInt, BigUint}; -pub use rational::{Rational, BigRational}; -pub use complex::Complex; -pub use integer::Integer; - -pub mod bigint; -pub mod complex; -pub mod integer; -pub mod rational; diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs deleted file mode 100644 index ceaf685c19a51..0000000000000 --- a/src/libnum/rational.rs +++ /dev/null @@ -1,803 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Rational numbers - -use Integer; - -use std::cmp; -use std::fmt; -use std::from_str::FromStr; -use std::num; -use std::num::{Zero, One, ToStrRadix, FromStrRadix}; - -use bigint::{BigInt, BigUint, Sign, Plus, Minus}; - -/// Represents the ratio between 2 numbers. -#[deriving(Clone, Hash)] -#[allow(missing_doc)] -pub struct Ratio { - numer: T, - denom: T -} - -/// Alias for a `Ratio` of machine-sized integers. -pub type Rational = Ratio; -pub type Rational32 = Ratio; -pub type Rational64 = Ratio; - -/// Alias for arbitrary precision rationals. -pub type BigRational = Ratio; - -impl - Ratio { - /// Creates a ratio representing the integer `t`. - #[inline] - pub fn from_integer(t: T) -> Ratio { - Ratio::new_raw(t, One::one()) - } - - /// Creates a ratio without checking for `denom == 0` or reducing. - #[inline] - pub fn new_raw(numer: T, denom: T) -> Ratio { - Ratio { numer: numer, denom: denom } - } - - /// Create a new Ratio. Fails if `denom == 0`. - #[inline] - pub fn new(numer: T, denom: T) -> Ratio { - if denom == Zero::zero() { - fail!("denominator == 0"); - } - let mut ret = Ratio::new_raw(numer, denom); - ret.reduce(); - ret - } - - /// Converts to an integer. - #[inline] - pub fn to_integer(&self) -> T { - self.trunc().numer - } - - /// Gets an immutable reference to the numerator. - #[inline] - pub fn numer<'a>(&'a self) -> &'a T { - &self.numer - } - - /// Gets an immutable reference to the denominator. - #[inline] - pub fn denom<'a>(&'a self) -> &'a T { - &self.denom - } - - /// Returns true if the rational number is an integer (denominator is 1). - #[inline] - pub fn is_integer(&self) -> bool { - self.denom == One::one() - } - - /// Put self into lowest terms, with denom > 0. - fn reduce(&mut self) { - let g : T = self.numer.gcd(&self.denom); - - // FIXME(#5992): assignment operator overloads - // self.numer /= g; - self.numer = self.numer / g; - // FIXME(#5992): assignment operator overloads - // self.denom /= g; - self.denom = self.denom / g; - - // keep denom positive! - if self.denom < Zero::zero() { - self.numer = -self.numer; - self.denom = -self.denom; - } - } - - /// Returns a `reduce`d copy of self. - pub fn reduced(&self) -> Ratio { - let mut ret = self.clone(); - ret.reduce(); - ret - } - - /// Returns the reciprocal. - #[inline] - pub fn recip(&self) -> Ratio { - Ratio::new_raw(self.denom.clone(), self.numer.clone()) - } - - /// Rounds towards minus infinity. - #[inline] - pub fn floor(&self) -> Ratio { - if *self < Zero::zero() { - Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) - } else { - Ratio::from_integer(self.numer / self.denom) - } - } - - /// Rounds towards plus infinity. - #[inline] - pub fn ceil(&self) -> Ratio { - if *self < Zero::zero() { - Ratio::from_integer(self.numer / self.denom) - } else { - Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) - } - } - - /// Rounds to the nearest integer. Rounds half-way cases away from zero. - #[inline] - pub fn round(&self) -> Ratio { - let one: T = One::one(); - let two: T = one + one; - - // Find unsigned fractional part of rational number - let fractional = self.fract().abs(); - - // The algorithm compares the unsigned fractional part with 1/2, that - // is, a/b >= 1/2, or a >= b/2. For odd denominators, we use - // a >= (b/2)+1. This avoids overflow issues. - let half_or_larger = if fractional.denom().is_even() { - *fractional.numer() >= *fractional.denom() / two - } else { - *fractional.numer() >= (*fractional.denom() / two) + one - }; - - if half_or_larger { - if *self >= Zero::zero() { - self.trunc() + One::one() - } else { - self.trunc() - One::one() - } - } else { - self.trunc() - } - } - - /// Rounds towards zero. - #[inline] - pub fn trunc(&self) -> Ratio { - Ratio::from_integer(self.numer / self.denom) - } - - /// Returns the fractional part of a number. - #[inline] - pub fn fract(&self) -> Ratio { - Ratio::new_raw(self.numer % self.denom, self.denom.clone()) - } -} - -impl Ratio { - /// Converts a float into a rational number. - pub fn from_float(f: T) -> Option { - if !f.is_finite() { - return None; - } - let (mantissa, exponent, sign) = f.integer_decode(); - let bigint_sign: Sign = if sign == 1 { Plus } else { Minus }; - if exponent < 0 { - let one: BigInt = One::one(); - let denom: BigInt = one << ((-exponent) as uint); - let numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); - Some(Ratio::new(BigInt::from_biguint(bigint_sign, numer), denom)) - } else { - let mut numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); - numer = numer << (exponent as uint); - Some(Ratio::from_integer(BigInt::from_biguint(bigint_sign, numer))) - } - } -} - -/* Comparisons */ - -// comparing a/b and c/d is the same as comparing a*d and b*c, so we -// abstract that pattern. The following macro takes a trait and either -// a comma-separated list of "method name -> return value" or just -// "method name" (return value is bool in that case) -macro_rules! cmp_impl { - (impl $imp:ident, $($method:ident),+) => { - cmp_impl!(impl $imp, $($method -> bool),+) - }; - // return something other than a Ratio - (impl $imp:ident, $($method:ident -> $res:ty),*) => { - impl + $imp> $imp for Ratio { - $( - #[inline] - fn $method(&self, other: &Ratio) -> $res { - (self.numer * other.denom). $method (&(self.denom*other.numer)) - } - )* - } - }; -} -cmp_impl!(impl PartialEq, eq, ne) -cmp_impl!(impl PartialOrd, lt -> bool, gt -> bool, le -> bool, ge -> bool, - partial_cmp -> Option) -cmp_impl!(impl Eq, ) -cmp_impl!(impl Ord, cmp -> cmp::Ordering) - -/* Arithmetic */ -// a/b * c/d = (a*c)/(b*d) -impl - Mul,Ratio> for Ratio { - #[inline] - fn mul(&self, rhs: &Ratio) -> Ratio { - Ratio::new(self.numer * rhs.numer, self.denom * rhs.denom) - } -} - -// (a/b) / (c/d) = (a*d)/(b*c) -impl - Div,Ratio> for Ratio { - #[inline] - fn div(&self, rhs: &Ratio) -> Ratio { - Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer) - } -} - -// Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern -macro_rules! arith_impl { - (impl $imp:ident, $method:ident) => { - impl - $imp,Ratio> for Ratio { - #[inline] - fn $method(&self, rhs: &Ratio) -> Ratio { - Ratio::new((self.numer * rhs.denom).$method(&(self.denom * rhs.numer)), - self.denom * rhs.denom) - } - } - } -} - -// a/b + c/d = (a*d + b*c)/(b*d) -arith_impl!(impl Add, add) - -// a/b - c/d = (a*d - b*c)/(b*d) -arith_impl!(impl Sub, sub) - -// a/b % c/d = (a*d % b*c)/(b*d) -arith_impl!(impl Rem, rem) - -impl - Neg> for Ratio { - #[inline] - fn neg(&self) -> Ratio { - Ratio::new_raw(-self.numer, self.denom.clone()) - } -} - -/* Constants */ -impl - Zero for Ratio { - #[inline] - fn zero() -> Ratio { - Ratio::new_raw(Zero::zero(), One::one()) - } - - #[inline] - fn is_zero(&self) -> bool { - *self == Zero::zero() - } -} - -impl - One for Ratio { - #[inline] - fn one() -> Ratio { - Ratio::new_raw(One::one(), One::one()) - } -} - -impl - Num for Ratio {} - -impl - num::Signed for Ratio { - #[inline] - fn abs(&self) -> Ratio { - if self.is_negative() { -self.clone() } else { self.clone() } - } - - #[inline] - fn abs_sub(&self, other: &Ratio) -> Ratio { - if *self <= *other { Zero::zero() } else { *self - *other } - } - - #[inline] - fn signum(&self) -> Ratio { - if *self > Zero::zero() { - num::one() - } else if self.is_zero() { - num::zero() - } else { - - num::one::>() - } - } - - #[inline] - fn is_positive(&self) -> bool { *self > Zero::zero() } - - #[inline] - fn is_negative(&self) -> bool { *self < Zero::zero() } -} - -/* String conversions */ -impl fmt::Show for Ratio { - /// Renders as `numer/denom`. If denom=1, renders as numer. - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.denom == One::one() { - write!(f, "{}", self.numer) - } else { - write!(f, "{}/{}", self.numer, self.denom) - } - } -} - -impl ToStrRadix for Ratio { - /// Renders as `numer/denom` where the numbers are in base `radix`. - fn to_str_radix(&self, radix: uint) -> String { - format!("{}/{}", - self.numer.to_str_radix(radix), - self.denom.to_str_radix(radix)) - } -} - -impl - FromStr for Ratio { - /// Parses `numer/denom` or just `numer`. - fn from_str(s: &str) -> Option> { - let mut split = s.splitn(1, '/'); - - let num = split.next().and_then(|n| FromStr::from_str(n)); - let den = split.next().or(Some("1")).and_then(|d| FromStr::from_str(d)); - - match (num, den) { - (Some(n), Some(d)) => Some(Ratio::new(n, d)), - _ => None - } - } -} - -impl - FromStrRadix for Ratio { - /// Parses `numer/denom` where the numbers are in base `radix`. - fn from_str_radix(s: &str, radix: uint) -> Option> { - let split: Vec<&str> = s.splitn(1, '/').collect(); - if split.len() < 2 { - None - } else { - let a_option: Option = FromStrRadix::from_str_radix( - *split.get(0), - radix); - a_option.and_then(|a| { - let b_option: Option = - FromStrRadix::from_str_radix(*split.get(1), radix); - b_option.and_then(|b| { - Some(Ratio::new(a.clone(), b.clone())) - }) - }) - } - } -} - -#[cfg(test)] -mod test { - - use super::{Ratio, Rational, BigRational}; - use std::num::{Zero, One, FromStrRadix, FromPrimitive, ToStrRadix}; - use std::from_str::FromStr; - use std::hash::hash; - use std::num; - use std::i32; - - pub static _0 : Rational = Ratio { numer: 0, denom: 1}; - pub static _1 : Rational = Ratio { numer: 1, denom: 1}; - pub static _2: Rational = Ratio { numer: 2, denom: 1}; - pub static _1_2: Rational = Ratio { numer: 1, denom: 2}; - pub static _3_2: Rational = Ratio { numer: 3, denom: 2}; - #[allow(non_uppercase_statics)] - pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2}; - pub static _1_3: Rational = Ratio { numer: 1, denom: 3}; - #[allow(non_uppercase_statics)] - pub static _neg1_3: Rational = Ratio { numer: -1, denom: 3}; - pub static _2_3: Rational = Ratio { numer: 2, denom: 3}; - #[allow(non_uppercase_statics)] - pub static _neg2_3: Rational = Ratio { numer: -2, denom: 3}; - - pub fn to_big(n: Rational) -> BigRational { - Ratio::new( - FromPrimitive::from_int(n.numer).unwrap(), - FromPrimitive::from_int(n.denom).unwrap() - ) - } - - #[test] - fn test_test_constants() { - // check our constants are what Ratio::new etc. would make. - assert_eq!(_0, Zero::zero()); - assert_eq!(_1, One::one()); - assert_eq!(_2, Ratio::from_integer(2i)); - assert_eq!(_1_2, Ratio::new(1i,2i)); - assert_eq!(_3_2, Ratio::new(3i,2i)); - assert_eq!(_neg1_2, Ratio::new(-1i,2i)); - } - - #[test] - fn test_new_reduce() { - let one22 = Ratio::new(2i,2); - - assert_eq!(one22, One::one()); - } - #[test] - #[should_fail] - fn test_new_zero() { - let _a = Ratio::new(1i,0); - } - - - #[test] - fn test_cmp() { - assert!(_0 == _0 && _1 == _1); - assert!(_0 != _1 && _1 != _0); - assert!(_0 < _1 && !(_1 < _0)); - assert!(_1 > _0 && !(_0 > _1)); - - assert!(_0 <= _0 && _1 <= _1); - assert!(_0 <= _1 && !(_1 <= _0)); - - assert!(_0 >= _0 && _1 >= _1); - assert!(_1 >= _0 && !(_0 >= _1)); - } - - - #[test] - fn test_to_integer() { - assert_eq!(_0.to_integer(), 0); - assert_eq!(_1.to_integer(), 1); - assert_eq!(_2.to_integer(), 2); - assert_eq!(_1_2.to_integer(), 0); - assert_eq!(_3_2.to_integer(), 1); - assert_eq!(_neg1_2.to_integer(), 0); - } - - - #[test] - fn test_numer() { - assert_eq!(_0.numer(), &0); - assert_eq!(_1.numer(), &1); - assert_eq!(_2.numer(), &2); - assert_eq!(_1_2.numer(), &1); - assert_eq!(_3_2.numer(), &3); - assert_eq!(_neg1_2.numer(), &(-1)); - } - #[test] - fn test_denom() { - assert_eq!(_0.denom(), &1); - assert_eq!(_1.denom(), &1); - assert_eq!(_2.denom(), &1); - assert_eq!(_1_2.denom(), &2); - assert_eq!(_3_2.denom(), &2); - assert_eq!(_neg1_2.denom(), &2); - } - - - #[test] - fn test_is_integer() { - assert!(_0.is_integer()); - assert!(_1.is_integer()); - assert!(_2.is_integer()); - assert!(!_1_2.is_integer()); - assert!(!_3_2.is_integer()); - assert!(!_neg1_2.is_integer()); - } - - #[test] - fn test_show() { - assert_eq!(format!("{}", _2), "2".to_string()); - assert_eq!(format!("{}", _1_2), "1/2".to_string()); - assert_eq!(format!("{}", _0), "0".to_string()); - assert_eq!(format!("{}", Ratio::from_integer(-2i)), "-2".to_string()); - } - - mod arith { - use super::{_0, _1, _2, _1_2, _3_2, _neg1_2, to_big}; - use super::super::{Ratio, Rational}; - - #[test] - fn test_add() { - fn test(a: Rational, b: Rational, c: Rational) { - assert_eq!(a + b, c); - assert_eq!(to_big(a) + to_big(b), to_big(c)); - } - - test(_1, _1_2, _3_2); - test(_1, _1, _2); - test(_1_2, _3_2, _2); - test(_1_2, _neg1_2, _0); - } - - #[test] - fn test_sub() { - fn test(a: Rational, b: Rational, c: Rational) { - assert_eq!(a - b, c); - assert_eq!(to_big(a) - to_big(b), to_big(c)) - } - - test(_1, _1_2, _1_2); - test(_3_2, _1_2, _1); - test(_1, _neg1_2, _3_2); - } - - #[test] - fn test_mul() { - fn test(a: Rational, b: Rational, c: Rational) { - assert_eq!(a * b, c); - assert_eq!(to_big(a) * to_big(b), to_big(c)) - } - - test(_1, _1_2, _1_2); - test(_1_2, _3_2, Ratio::new(3i,4i)); - test(_1_2, _neg1_2, Ratio::new(-1i, 4i)); - } - - #[test] - fn test_div() { - fn test(a: Rational, b: Rational, c: Rational) { - assert_eq!(a / b, c); - assert_eq!(to_big(a) / to_big(b), to_big(c)) - } - - test(_1, _1_2, _2); - test(_3_2, _1_2, _1 + _2); - test(_1, _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2); - } - - #[test] - fn test_rem() { - fn test(a: Rational, b: Rational, c: Rational) { - assert_eq!(a % b, c); - assert_eq!(to_big(a) % to_big(b), to_big(c)) - } - - test(_3_2, _1, _1_2); - test(_2, _neg1_2, _0); - test(_1_2, _2, _1_2); - } - - #[test] - fn test_neg() { - fn test(a: Rational, b: Rational) { - assert_eq!(-a, b); - assert_eq!(-to_big(a), to_big(b)) - } - - test(_0, _0); - test(_1_2, _neg1_2); - test(-_1, _1); - } - #[test] - fn test_zero() { - assert_eq!(_0 + _0, _0); - assert_eq!(_0 * _0, _0); - assert_eq!(_0 * _1, _0); - assert_eq!(_0 / _neg1_2, _0); - assert_eq!(_0 - _0, _0); - } - #[test] - #[should_fail] - fn test_div_0() { - let _a = _1 / _0; - } - } - - #[test] - fn test_round() { - assert_eq!(_1_3.ceil(), _1); - assert_eq!(_1_3.floor(), _0); - assert_eq!(_1_3.round(), _0); - assert_eq!(_1_3.trunc(), _0); - - assert_eq!(_neg1_3.ceil(), _0); - assert_eq!(_neg1_3.floor(), -_1); - assert_eq!(_neg1_3.round(), _0); - assert_eq!(_neg1_3.trunc(), _0); - - assert_eq!(_2_3.ceil(), _1); - assert_eq!(_2_3.floor(), _0); - assert_eq!(_2_3.round(), _1); - assert_eq!(_2_3.trunc(), _0); - - assert_eq!(_neg2_3.ceil(), _0); - assert_eq!(_neg2_3.floor(), -_1); - assert_eq!(_neg2_3.round(), -_1); - assert_eq!(_neg2_3.trunc(), _0); - - assert_eq!(_1_2.ceil(), _1); - assert_eq!(_1_2.floor(), _0); - assert_eq!(_1_2.round(), _1); - assert_eq!(_1_2.trunc(), _0); - - assert_eq!(_neg1_2.ceil(), _0); - assert_eq!(_neg1_2.floor(), -_1); - assert_eq!(_neg1_2.round(), -_1); - assert_eq!(_neg1_2.trunc(), _0); - - assert_eq!(_1.ceil(), _1); - assert_eq!(_1.floor(), _1); - assert_eq!(_1.round(), _1); - assert_eq!(_1.trunc(), _1); - - // Overflow checks - - let _neg1 = Ratio::from_integer(-1); - let _large_rat1 = Ratio::new(i32::MAX, i32::MAX-1); - let _large_rat2 = Ratio::new(i32::MAX-1, i32::MAX); - let _large_rat3 = Ratio::new(i32::MIN+2, i32::MIN+1); - let _large_rat4 = Ratio::new(i32::MIN+1, i32::MIN+2); - let _large_rat5 = Ratio::new(i32::MIN+2, i32::MAX); - let _large_rat6 = Ratio::new(i32::MAX, i32::MIN+2); - let _large_rat7 = Ratio::new(1, i32::MIN+1); - let _large_rat8 = Ratio::new(1, i32::MAX); - - assert_eq!(_large_rat1.round(), One::one()); - assert_eq!(_large_rat2.round(), One::one()); - assert_eq!(_large_rat3.round(), One::one()); - assert_eq!(_large_rat4.round(), One::one()); - assert_eq!(_large_rat5.round(), _neg1); - assert_eq!(_large_rat6.round(), _neg1); - assert_eq!(_large_rat7.round(), Zero::zero()); - assert_eq!(_large_rat8.round(), Zero::zero()); - } - - #[test] - fn test_fract() { - assert_eq!(_1.fract(), _0); - assert_eq!(_neg1_2.fract(), _neg1_2); - assert_eq!(_1_2.fract(), _1_2); - assert_eq!(_3_2.fract(), _1_2); - } - - #[test] - fn test_recip() { - assert_eq!(_1 * _1.recip(), _1); - assert_eq!(_2 * _2.recip(), _1); - assert_eq!(_1_2 * _1_2.recip(), _1); - assert_eq!(_3_2 * _3_2.recip(), _1); - assert_eq!(_neg1_2 * _neg1_2.recip(), _1); - } - - #[test] - fn test_to_from_str() { - fn test(r: Rational, s: String) { - assert_eq!(FromStr::from_str(s.as_slice()), Some(r)); - assert_eq!(r.to_string(), s); - } - test(_1, "1".to_string()); - test(_0, "0".to_string()); - test(_1_2, "1/2".to_string()); - test(_3_2, "3/2".to_string()); - test(_2, "2".to_string()); - test(_neg1_2, "-1/2".to_string()); - } - #[test] - fn test_from_str_fail() { - fn test(s: &str) { - let rational: Option = FromStr::from_str(s); - assert_eq!(rational, None); - } - - let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1"]; - for &s in xs.iter() { - test(s); - } - } - - #[test] - fn test_to_from_str_radix() { - fn test(r: Rational, s: String, n: uint) { - assert_eq!(FromStrRadix::from_str_radix(s.as_slice(), n), - Some(r)); - assert_eq!(r.to_str_radix(n).to_string(), s); - } - fn test3(r: Rational, s: String) { test(r, s, 3) } - fn test16(r: Rational, s: String) { test(r, s, 16) } - - test3(_1, "1/1".to_string()); - test3(_0, "0/1".to_string()); - test3(_1_2, "1/2".to_string()); - test3(_3_2, "10/2".to_string()); - test3(_2, "2/1".to_string()); - test3(_neg1_2, "-1/2".to_string()); - test3(_neg1_2 / _2, "-1/11".to_string()); - - test16(_1, "1/1".to_string()); - test16(_0, "0/1".to_string()); - test16(_1_2, "1/2".to_string()); - test16(_3_2, "3/2".to_string()); - test16(_2, "2/1".to_string()); - test16(_neg1_2, "-1/2".to_string()); - test16(_neg1_2 / _2, "-1/4".to_string()); - test16(Ratio::new(13i,15i), "d/f".to_string()); - test16(_1_2*_1_2*_1_2*_1_2, "1/10".to_string()); - } - - #[test] - fn test_from_str_radix_fail() { - fn test(s: &str) { - let radix: Option = FromStrRadix::from_str_radix(s, 3); - assert_eq!(radix, None); - } - - let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1", "3/2"]; - for &s in xs.iter() { - test(s); - } - } - - #[test] - fn test_from_float() { - fn test(given: T, (numer, denom): (&str, &str)) { - let ratio: BigRational = Ratio::from_float(given).unwrap(); - assert_eq!(ratio, Ratio::new( - FromStr::from_str(numer).unwrap(), - FromStr::from_str(denom).unwrap())); - } - - // f32 - test(3.14159265359f32, ("13176795", "4194304")); - test(2f32.powf(100.), ("1267650600228229401496703205376", "1")); - test(-2f32.powf(100.), ("-1267650600228229401496703205376", "1")); - test(1.0 / 2f32.powf(100.), ("1", "1267650600228229401496703205376")); - test(684729.48391f32, ("1369459", "2")); - test(-8573.5918555f32, ("-4389679", "512")); - - // f64 - test(3.14159265359f64, ("3537118876014453", "1125899906842624")); - test(2f64.powf(100.), ("1267650600228229401496703205376", "1")); - test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1")); - test(684729.48391f64, ("367611342500051", "536870912")); - test(-8573.5918555f64, ("-4713381968463931", "549755813888")); - test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376")); - } - - #[test] - fn test_from_float_fail() { - use std::{f32, f64}; - - assert_eq!(Ratio::from_float(f32::NAN), None); - assert_eq!(Ratio::from_float(f32::INFINITY), None); - assert_eq!(Ratio::from_float(f32::NEG_INFINITY), None); - assert_eq!(Ratio::from_float(f64::NAN), None); - assert_eq!(Ratio::from_float(f64::INFINITY), None); - assert_eq!(Ratio::from_float(f64::NEG_INFINITY), None); - } - - #[test] - fn test_signed() { - assert_eq!(_neg1_2.abs(), _1_2); - assert_eq!(_3_2.abs_sub(&_1_2), _1); - assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero()); - assert_eq!(_1_2.signum(), One::one()); - assert_eq!(_neg1_2.signum(), - num::one::>()); - assert!(_neg1_2.is_negative()); - assert!(! _neg1_2.is_positive()); - assert!(! _1_2.is_negative()); - } - - #[test] - fn test_hash() { - assert!(hash(&_0) != hash(&_1)); - assert!(hash(&_0) != hash(&_3_2)); - } -} diff --git a/src/libsemver/lib.rs b/src/libsemver/lib.rs deleted file mode 100644 index cc0e7d9ba2d98..0000000000000 --- a/src/libsemver/lib.rs +++ /dev/null @@ -1,462 +0,0 @@ -// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Semantic version parsing and comparison. -//! -//! Semantic versioning (see http://semver.org/) is a set of rules for -//! assigning version numbers intended to convey meaning about what has -//! changed, and how much. A version number has five parts: -//! -//! * Major number, updated for incompatible API changes -//! * Minor number, updated for backwards-compatible API additions -//! * Patch number, updated for backwards-compatible bugfixes -//! * Pre-release information (optional), preceded by a hyphen (`-`) -//! * Build metadata (optional), preceded by a plus sign (`+`) -//! -//! The three mandatory components are required to be decimal numbers. The -//! pre-release information and build metadata are required to be a -//! period-separated list of identifiers containing only alphanumeric -//! characters and hyphens. -//! -//! An example version number with all five components is -//! `0.8.1-rc.3.0+20130922.linux`. - -#![crate_name = "semver"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/semver"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(default_type_params)] - -use std::char; -use std::cmp; -use std::fmt::Show; -use std::fmt; -use std::hash; - -/// An identifier in the pre-release or build metadata. If the identifier can -/// be parsed as a decimal value, it will be represented with `Numeric`. -#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[allow(missing_doc)] -pub enum Identifier { - Numeric(uint), - AlphaNumeric(String) -} - -impl fmt::Show for Identifier { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Numeric(ref n) => n.fmt(f), - AlphaNumeric(ref s) => s.fmt(f) - } - } -} - - -/// Represents a version number conforming to the semantic versioning scheme. -#[deriving(Clone, Eq)] -pub struct Version { - /// The major version, to be incremented on incompatible changes. - pub major: uint, - /// The minor version, to be incremented when functionality is added in a - /// backwards-compatible manner. - pub minor: uint, - /// The patch version, to be incremented when backwards-compatible bug - /// fixes are made. - pub patch: uint, - /// The pre-release version identifier, if one exists. - pub pre: Vec, - /// The build metadata, ignored when determining version precedence. - pub build: Vec, -} - -impl fmt::Show for Version { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{}.{}.{}", self.major, self.minor, self.patch)) - if !self.pre.is_empty() { - try!(write!(f, "-")); - for (i, x) in self.pre.iter().enumerate() { - if i != 0 { try!(write!(f, ".")) }; - try!(x.fmt(f)); - } - } - if !self.build.is_empty() { - try!(write!(f, "+")); - for (i, x) in self.build.iter().enumerate() { - if i != 0 { try!(write!(f, ".")) }; - try!(x.fmt(f)); - } - } - Ok(()) - } -} - -impl cmp::PartialEq for Version { - #[inline] - fn eq(&self, other: &Version) -> bool { - // We should ignore build metadata here, otherwise versions v1 and v2 - // can exist such that !(v1 < v2) && !(v1 > v2) && v1 != v2, which - // violate strict total ordering rules. - self.major == other.major && - self.minor == other.minor && - self.patch == other.patch && - self.pre == other.pre - } -} - -impl cmp::PartialOrd for Version { - fn partial_cmp(&self, other: &Version) -> Option { - Some(self.cmp(other)) - } -} - -impl cmp::Ord for Version { - fn cmp(&self, other: &Version) -> Ordering { - match self.major.cmp(&other.major) { - Equal => {} - r => return r, - } - - match self.minor.cmp(&other.minor) { - Equal => {} - r => return r, - } - - match self.patch.cmp(&other.patch) { - Equal => {} - r => return r, - } - - // NB: semver spec says 0.0.0-pre < 0.0.0 - // but the version of ord defined for vec - // says that [] < [pre] so we alter it here - match (self.pre.len(), other.pre.len()) { - (0, 0) => Equal, - (0, _) => Greater, - (_, 0) => Less, - (_, _) => self.pre.cmp(&other.pre) - } - } -} - -impl hash::Hash for Version { - fn hash(&self, into: &mut S) { - self.major.hash(into); - self.minor.hash(into); - self.patch.hash(into); - self.pre.hash(into); - } -} - -fn take_nonempty_prefix>(rdr: &mut T, pred: |char| -> bool) - -> (String, Option) { - let mut buf = String::new(); - let mut ch = rdr.next(); - loop { - match ch { - None => break, - Some(c) if !pred(c) => break, - Some(c) => { - buf.push_char(c); - ch = rdr.next(); - } - } - } - (buf, ch) -} - -fn take_num>(rdr: &mut T) -> Option<(uint, Option)> { - let (s, ch) = take_nonempty_prefix(rdr, char::is_digit); - match from_str::(s.as_slice()) { - None => None, - Some(i) => Some((i, ch)) - } -} - -fn take_ident>(rdr: &mut T) -> Option<(Identifier, Option)> { - let (s,ch) = take_nonempty_prefix(rdr, char::is_alphanumeric); - if s.as_slice().chars().all(char::is_digit) { - match from_str::(s.as_slice()) { - None => None, - Some(i) => Some((Numeric(i), ch)) - } - } else { - Some((AlphaNumeric(s), ch)) - } -} - -fn expect(ch: Option, c: char) -> Option<()> { - if ch != Some(c) { - None - } else { - Some(()) - } -} - -fn parse_iter>(rdr: &mut T) -> Option { - let maybe_vers = take_num(rdr).and_then(|(major, ch)| { - expect(ch, '.').and_then(|_| Some(major)) - }).and_then(|major| { - take_num(rdr).and_then(|(minor, ch)| { - expect(ch, '.').and_then(|_| Some((major, minor))) - }) - }).and_then(|(major, minor)| { - take_num(rdr).and_then(|(patch, ch)| { - Some((major, minor, patch, ch)) - }) - }); - - let (major, minor, patch, ch) = match maybe_vers { - Some((a, b, c, d)) => (a, b, c, d), - None => return None - }; - - let mut pre = vec!(); - let mut build = vec!(); - - let mut ch = ch; - if ch == Some('-') { - loop { - let (id, c) = match take_ident(rdr) { - Some((id, c)) => (id, c), - None => return None - }; - pre.push(id); - ch = c; - if ch != Some('.') { break; } - } - } - - if ch == Some('+') { - loop { - let (id, c) = match take_ident(rdr) { - Some((id, c)) => (id, c), - None => return None - }; - build.push(id); - ch = c; - if ch != Some('.') { break; } - } - } - - Some(Version { - major: major, - minor: minor, - patch: patch, - pre: pre, - build: build, - }) -} - - -/// Parse a string into a semver object. -pub fn parse(s: &str) -> Option { - if !s.is_ascii() { - return None; - } - let s = s.trim(); - let v = parse_iter(&mut s.chars()); - match v { - Some(v) => { - if v.to_string().equiv(&s) { - Some(v) - } else { - None - } - } - None => None - } -} - -#[test] -fn test_parse() { - assert_eq!(parse(""), None); - assert_eq!(parse(" "), None); - assert_eq!(parse("1"), None); - assert_eq!(parse("1.2"), None); - assert_eq!(parse("1.2"), None); - assert_eq!(parse("1"), None); - assert_eq!(parse("1.2"), None); - assert_eq!(parse("1.2.3-"), None); - assert_eq!(parse("a.b.c"), None); - assert_eq!(parse("1.2.3 abc"), None); - - assert!(parse("1.2.3") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(), - build: vec!(), - })); - assert!(parse(" 1.2.3 ") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(), - build: vec!(), - })); - assert!(parse("1.2.3-alpha1") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(AlphaNumeric("alpha1".to_string())), - build: vec!(), - })); - assert!(parse(" 1.2.3-alpha1 ") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(AlphaNumeric("alpha1".to_string())), - build: vec!() - })); - assert!(parse("1.2.3+build5") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(), - build: vec!(AlphaNumeric("build5".to_string())) - })); - assert!(parse(" 1.2.3+build5 ") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(), - build: vec!(AlphaNumeric("build5".to_string())) - })); - assert!(parse("1.2.3-alpha1+build5") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(AlphaNumeric("alpha1".to_string())), - build: vec!(AlphaNumeric("build5".to_string())) - })); - assert!(parse(" 1.2.3-alpha1+build5 ") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(AlphaNumeric("alpha1".to_string())), - build: vec!(AlphaNumeric("build5".to_string())) - })); - assert!(parse("1.2.3-1.alpha1.9+build5.7.3aedf ") == Some(Version { - major: 1u, - minor: 2u, - patch: 3u, - pre: vec!(Numeric(1),AlphaNumeric("alpha1".to_string()),Numeric(9)), - build: vec!(AlphaNumeric("build5".to_string()), - Numeric(7), - AlphaNumeric("3aedf".to_string())) - })); - -} - -#[test] -fn test_eq() { - assert_eq!(parse("1.2.3"), parse("1.2.3")); - assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1")); - assert_eq!(parse("1.2.3+build.42"), parse("1.2.3+build.42")); - assert_eq!(parse("1.2.3-alpha1+42"), parse("1.2.3-alpha1+42")); - assert_eq!(parse("1.2.3+23"), parse("1.2.3+42")); -} - -#[test] -fn test_ne() { - assert!(parse("0.0.0") != parse("0.0.1")); - assert!(parse("0.0.0") != parse("0.1.0")); - assert!(parse("0.0.0") != parse("1.0.0")); - assert!(parse("1.2.3-alpha") != parse("1.2.3-beta")); -} - -#[test] -fn test_show() { - assert_eq!(format!("{}", parse("1.2.3").unwrap()), - "1.2.3".to_string()); - assert_eq!(format!("{}", parse("1.2.3-alpha1").unwrap()), - "1.2.3-alpha1".to_string()); - assert_eq!(format!("{}", parse("1.2.3+build.42").unwrap()), - "1.2.3+build.42".to_string()); - assert_eq!(format!("{}", parse("1.2.3-alpha1+42").unwrap()), - "1.2.3-alpha1+42".to_string()); -} - -#[test] -fn test_to_string() { - assert_eq!(parse("1.2.3").unwrap().to_string(), "1.2.3".to_string()); - assert_eq!(parse("1.2.3-alpha1").unwrap().to_string(), "1.2.3-alpha1".to_string()); - assert_eq!(parse("1.2.3+build.42").unwrap().to_string(), "1.2.3+build.42".to_string()); - assert_eq!(parse("1.2.3-alpha1+42").unwrap().to_string(), "1.2.3-alpha1+42".to_string()); -} - -#[test] -fn test_lt() { - assert!(parse("0.0.0") < parse("1.2.3-alpha2")); - assert!(parse("1.0.0") < parse("1.2.3-alpha2")); - assert!(parse("1.2.0") < parse("1.2.3-alpha2")); - assert!(parse("1.2.3-alpha1") < parse("1.2.3")); - assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2")); - assert!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2"))); - assert!(!(parse("1.2.3+23") < parse("1.2.3+42"))); -} - -#[test] -fn test_le() { - assert!(parse("0.0.0") <= parse("1.2.3-alpha2")); - assert!(parse("1.0.0") <= parse("1.2.3-alpha2")); - assert!(parse("1.2.0") <= parse("1.2.3-alpha2")); - assert!(parse("1.2.3-alpha1") <= parse("1.2.3-alpha2")); - assert!(parse("1.2.3-alpha2") <= parse("1.2.3-alpha2")); - assert!(parse("1.2.3+23") <= parse("1.2.3+42")); -} - -#[test] -fn test_gt() { - assert!(parse("1.2.3-alpha2") > parse("0.0.0")); - assert!(parse("1.2.3-alpha2") > parse("1.0.0")); - assert!(parse("1.2.3-alpha2") > parse("1.2.0")); - assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1")); - assert!(parse("1.2.3") > parse("1.2.3-alpha2")); - assert!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2"))); - assert!(!(parse("1.2.3+23") > parse("1.2.3+42"))); -} - -#[test] -fn test_ge() { - assert!(parse("1.2.3-alpha2") >= parse("0.0.0")); - assert!(parse("1.2.3-alpha2") >= parse("1.0.0")); - assert!(parse("1.2.3-alpha2") >= parse("1.2.0")); - assert!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha1")); - assert!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha2")); - assert!(parse("1.2.3+23") >= parse("1.2.3+42")); -} - -#[test] -fn test_spec_order() { - let vs = ["1.0.0-alpha", - "1.0.0-alpha.1", - "1.0.0-alpha.beta", - "1.0.0-beta", - "1.0.0-beta.2", - "1.0.0-beta.11", - "1.0.0-rc.1", - "1.0.0"]; - let mut i = 1; - while i < vs.len() { - let a = parse(vs[i-1]).unwrap(); - let b = parse(vs[i]).unwrap(); - assert!(a < b); - i += 1; - } -} diff --git a/src/liburl/lib.rs b/src/liburl/lib.rs deleted file mode 100644 index bf9a959afffbd..0000000000000 --- a/src/liburl/lib.rs +++ /dev/null @@ -1,1243 +0,0 @@ -// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Types/fns concerning URLs (see RFC 3986) - -#![crate_name = "url"] -#![deprecated="This is being removed. Use rust-url instead. http://servo.github.io/rust-url/"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/", - html_playground_url = "http://play.rust-lang.org/")] -#![feature(default_type_params)] - -use std::collections::HashMap; -use std::collections::hashmap::{Occupied, Vacant}; -use std::fmt; -use std::from_str::FromStr; -use std::hash; -use std::uint; -use std::path::BytesContainer; - -/// A Uniform Resource Locator (URL). A URL is a form of URI (Uniform Resource -/// Identifier) that includes network location information, such as hostname or -/// port number. -/// -/// # Example -/// -/// ```rust -/// # #![allow(deprecated)] -/// use url::Url; -/// -/// let raw = "https://username@example.com:8080/foo/bar?baz=qux#quz"; -/// match Url::parse(raw) { -/// Ok(u) => println!("Parsed '{}'", u), -/// Err(e) => println!("Couldn't parse '{}': {}", raw, e), -/// } -/// ``` -#[deriving(Clone, PartialEq, Eq)] -pub struct Url { - /// The scheme part of a URL, such as `https` in the above example. - pub scheme: String, - /// A URL subcomponent for user authentication. `username` in the above example. - pub user: Option, - /// A domain name or IP address. For example, `example.com`. - pub host: String, - /// A TCP port number, for example `8080`. - pub port: Option, - /// The path component of a URL, for example `/foo/bar?baz=qux#quz`. - pub path: Path, -} - -#[deriving(Clone, PartialEq, Eq)] -pub struct Path { - /// The path component of a URL, for example `/foo/bar`. - pub path: String, - /// The query component of a URL. - /// `vec![("baz".to_string(), "qux".to_string())]` represents the fragment - /// `baz=qux` in the above example. - pub query: Query, - /// The fragment component, such as `quz`. Not including the leading `#` character. - pub fragment: Option -} - -/// An optional subcomponent of a URI authority component. -#[deriving(Clone, PartialEq, Eq)] -pub struct UserInfo { - /// The user name. - pub user: String, - /// Password or other scheme-specific authentication information. - pub pass: Option -} - -/// Represents the query component of a URI. -pub type Query = Vec<(String, String)>; - -impl Url { - pub fn new(scheme: String, - user: Option, - host: String, - port: Option, - path: String, - query: Query, - fragment: Option) - -> Url { - Url { - scheme: scheme, - user: user, - host: host, - port: port, - path: Path::new(path, query, fragment) - } - } - - /// Parses a URL, converting it from a string to a `Url` representation. - /// - /// # Arguments - /// * rawurl - a string representing the full URL, including scheme. - /// - /// # Return value - /// - /// `Err(e)` if the string did not represent a valid URL, where `e` is a - /// `String` error message. Otherwise, `Ok(u)` where `u` is a `Url` struct - /// representing the URL. - pub fn parse(rawurl: &str) -> DecodeResult { - // scheme - let (scheme, rest) = try!(get_scheme(rawurl)); - - // authority - let (userinfo, host, port, rest) = try!(get_authority(rest)); - - // path - let has_authority = host.len() > 0; - let (path, rest) = try!(get_path(rest, has_authority)); - - // query and fragment - let (query, fragment) = try!(get_query_fragment(rest)); - - let url = Url::new(scheme.to_string(), - userinfo, - host.to_string(), - port, - path, - query, - fragment); - Ok(url) - } -} - -#[deprecated="use `Url::parse`"] -pub fn from_str(s: &str) -> Result { - Url::parse(s) -} - -impl Path { - pub fn new(path: String, - query: Query, - fragment: Option) - -> Path { - Path { - path: path, - query: query, - fragment: fragment, - } - } - - /// Parses a URL path, converting it from a string to a `Path` representation. - /// - /// # Arguments - /// * rawpath - a string representing the path component of a URL. - /// - /// # Return value - /// - /// `Err(e)` if the string did not represent a valid URL path, where `e` is a - /// `String` error message. Otherwise, `Ok(p)` where `p` is a `Path` struct - /// representing the URL path. - pub fn parse(rawpath: &str) -> DecodeResult { - let (path, rest) = try!(get_path(rawpath, false)); - - // query and fragment - let (query, fragment) = try!(get_query_fragment(rest.as_slice())); - - Ok(Path{ path: path, query: query, fragment: fragment }) - } -} - -#[deprecated="use `Path::parse`"] -pub fn path_from_str(s: &str) -> Result { - Path::parse(s) -} - -impl UserInfo { - #[inline] - pub fn new(user: String, pass: Option) -> UserInfo { - UserInfo { user: user, pass: pass } - } -} - -fn encode_inner(c: T, full_url: bool) -> String { - c.container_as_bytes().iter().fold(String::new(), |mut out, &b| { - match b as char { - // unreserved: - 'A' ... 'Z' - | 'a' ... 'z' - | '0' ... '9' - | '-' | '.' | '_' | '~' => out.push_char(b as char), - - // gen-delims: - ':' | '/' | '?' | '#' | '[' | ']' | '@' | - // sub-delims: - '!' | '$' | '&' | '"' | '(' | ')' | '*' | - '+' | ',' | ';' | '=' - if full_url => out.push_char(b as char), - - ch => out.push_str(format!("%{:02X}", ch as uint).as_slice()), - }; - - out - }) -} - -/// Encodes a URI by replacing reserved characters with percent-encoded -/// character sequences. -/// -/// This function is compliant with RFC 3986. -/// -/// # Example -/// -/// ```rust -/// # #![allow(deprecated)] -/// use url::encode; -/// -/// let url = encode("https://example.com/Rust (programming language)"); -/// println!("{}", url); // https://example.com/Rust%20(programming%20language) -/// ``` -pub fn encode(container: T) -> String { - encode_inner(container, true) -} - - -/// Encodes a URI component by replacing reserved characters with percent- -/// encoded character sequences. -/// -/// This function is compliant with RFC 3986. -pub fn encode_component(container: T) -> String { - encode_inner(container, false) -} - -pub type DecodeResult = Result; - -/// Decodes a percent-encoded string representing a URI. -/// -/// This will only decode escape sequences generated by `encode`. -/// -/// # Example -/// -/// ```rust -/// # #![allow(deprecated)] -/// use url::decode; -/// -/// let url = decode("https://example.com/Rust%20(programming%20language)"); -/// println!("{}", url); // https://example.com/Rust (programming language) -/// ``` -pub fn decode(container: T) -> DecodeResult { - decode_inner(container, true) -} - -/// Decode a string encoded with percent encoding. -pub fn decode_component(container: T) -> DecodeResult { - decode_inner(container, false) -} - -fn decode_inner(c: T, full_url: bool) -> DecodeResult { - let mut out = String::new(); - let mut iter = c.container_as_bytes().iter().map(|&b| b); - - loop { - match iter.next() { - Some(b) => match b as char { - '%' => { - let bytes = match (iter.next(), iter.next()) { - (Some(one), Some(two)) => [one as u8, two as u8], - _ => return Err(format!("Malformed input: found '%' \ - without two trailing bytes")), - }; - - // Only decode some characters if full_url: - match uint::parse_bytes(bytes, 16u).unwrap() as u8 as char { - // gen-delims: - ':' | '/' | '?' | '#' | '[' | ']' | '@' | - - // sub-delims: - '!' | '$' | '&' | '"' | '(' | ')' | '*' | - '+' | ',' | ';' | '=' - if full_url => { - out.push_char('%'); - out.push_char(bytes[0u] as char); - out.push_char(bytes[1u] as char); - } - - ch => out.push_char(ch) - } - } - ch => out.push_char(ch) - }, - None => return Ok(out), - } - } -} - -/// Encode a hashmap to the 'application/x-www-form-urlencoded' media type. -pub fn encode_form_urlencoded(m: &HashMap>) -> String { - fn encode_plus(s: &T) -> String { - s.as_slice().bytes().fold(String::new(), |mut out, b| { - match b as char { - 'A' ... 'Z' - | 'a' ... 'z' - | '0' ... '9' - | '_' | '.' | '-' => out.push_char(b as char), - ' ' => out.push_char('+'), - ch => out.push_str(format!("%{:X}", ch as uint).as_slice()) - } - - out - }) - } - - let mut first = true; - m.iter().fold(String::new(), |mut out, (key, values)| { - let key = encode_plus(key); - - for value in values.iter() { - if first { - first = false; - } else { - out.push_char('&'); - } - - out.push_str(key.as_slice()); - out.push_char('='); - out.push_str(encode_plus(value).as_slice()); - } - - out - }) -} - -/// Decode a string encoded with the 'application/x-www-form-urlencoded' media -/// type into a hashmap. -pub fn decode_form_urlencoded(s: &[u8]) - -> DecodeResult>> { - fn maybe_push_value(map: &mut HashMap>, - key: String, - value: String) { - if key.len() > 0 && value.len() > 0 { - match map.entry(key) { - Vacant(entry) => { entry.set(vec![value]); }, - Occupied(mut entry) => { entry.get_mut().push(value); }, - } - } - } - - let mut out = HashMap::new(); - let mut iter = s.iter().map(|&x| x); - - let mut key = String::new(); - let mut value = String::new(); - let mut parsing_key = true; - - loop { - match iter.next() { - Some(b) => match b as char { - '&' | ';' => { - maybe_push_value(&mut out, key, value); - - parsing_key = true; - key = String::new(); - value = String::new(); - } - '=' => parsing_key = false, - ch => { - let ch = match ch { - '%' => { - let bytes = match (iter.next(), iter.next()) { - (Some(one), Some(two)) => [one as u8, two as u8], - _ => return Err(format!("Malformed input: found \ - '%' without two trailing bytes")) - }; - - uint::parse_bytes(bytes, 16u).unwrap() as u8 as char - } - '+' => ' ', - ch => ch - }; - - if parsing_key { - key.push_char(ch) - } else { - value.push_char(ch) - } - } - }, - None => { - maybe_push_value(&mut out, key, value); - return Ok(out) - } - } - } -} - -fn split_char_first(s: &str, c: char) -> (&str, &str) { - let mut iter = s.splitn(1, c); - - match (iter.next(), iter.next()) { - (Some(a), Some(b)) => (a, b), - (Some(a), None) => (a, ""), - (None, _) => unreachable!(), - } -} - -impl fmt::Show for UserInfo { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.pass { - Some(ref pass) => write!(f, "{}:{}@", self.user, *pass), - None => write!(f, "{}@", self.user), - } - } -} - -fn query_from_str(rawquery: &str) -> DecodeResult { - let mut query: Query = vec!(); - if !rawquery.is_empty() { - for p in rawquery.split('&') { - let (k, v) = split_char_first(p, '='); - query.push((try!(decode_component(k)), - try!(decode_component(v)))); - } - } - - Ok(query) -} - -/// Converts an instance of a URI `Query` type to a string. -/// -/// # Example -/// -/// ```rust -/// # #![allow(deprecated)] -/// let query = vec![("title".to_string(), "The Village".to_string()), -/// ("north".to_string(), "52.91".to_string()), -/// ("west".to_string(), "4.10".to_string())]; -/// println!("{}", url::query_to_str(&query)); // title=The%20Village&north=52.91&west=4.10 -/// ``` -pub fn query_to_str(query: &Query) -> String { - query.iter().enumerate().fold(String::new(), |mut out, (i, &(ref k, ref v))| { - if i != 0 { - out.push_char('&'); - } - - out.push_str(encode_component(k.as_slice()).as_slice()); - out.push_char('='); - out.push_str(encode_component(v.as_slice()).as_slice()); - out - }) -} - -/// Returns a tuple of the URI scheme and the rest of the URI, or a parsing error. -/// -/// Does not include the separating `:` character. -/// -/// # Example -/// -/// ```rust -/// # #![allow(deprecated)] -/// use url::get_scheme; -/// -/// let scheme = match get_scheme("https://example.com/") { -/// Ok((sch, _)) => sch, -/// Err(_) => "(None)", -/// }; -/// println!("Scheme in use: {}.", scheme); // Scheme in use: https. -/// ``` -pub fn get_scheme(rawurl: &str) -> DecodeResult<(&str, &str)> { - for (i,c) in rawurl.chars().enumerate() { - let result = match c { - 'A' ... 'Z' - | 'a' ... 'z' => continue, - '0' ... '9' | '+' | '-' | '.' => { - if i != 0 { continue } - - Err("url: Scheme must begin with a letter.".to_string()) - } - ':' => { - if i == 0 { - Err("url: Scheme cannot be empty.".to_string()) - } else { - Ok((rawurl.slice(0,i), rawurl.slice(i+1,rawurl.len()))) - } - } - _ => Err("url: Invalid character in scheme.".to_string()), - }; - - return result; - } - - Err("url: Scheme must be terminated with a colon.".to_string()) -} - -// returns userinfo, host, port, and unparsed part, or an error -fn get_authority(rawurl: &str) -> - DecodeResult<(Option, &str, Option, &str)> { - enum State { - Start, // starting state - PassHostPort, // could be in user or port - Ip6Port, // either in ipv6 host or port - Ip6Host, // are in an ipv6 host - InHost, // are in a host - may be ipv6, but don't know yet - InPort // are in port - } - - #[deriving(Clone, PartialEq)] - enum Input { - Digit, // all digits - Hex, // digits and letters a-f - Unreserved // all other legal characters - } - - if !rawurl.starts_with("//") { - // there is no authority. - return Ok((None, "", None, rawurl)); - } - - let len = rawurl.len(); - let mut st = Start; - let mut input = Digit; // most restricted, start here. - - let mut userinfo = None; - let mut host = ""; - let mut port = None; - - let mut colon_count = 0u; - let mut pos = 0; - let mut begin = 2; - let mut end = len; - - for (i,c) in rawurl.chars().enumerate() - // ignore the leading '//' handled by early return - .skip(2) { - // deal with input class first - match c { - '0' ... '9' => (), - 'A' ... 'F' - | 'a' ... 'f' => { - if input == Digit { - input = Hex; - } - } - 'G' ... 'Z' - | 'g' ... 'z' - | '-' | '.' | '_' | '~' | '%' - | '&' |'\'' | '(' | ')' | '+' - | '!' | '*' | ',' | ';' | '=' => input = Unreserved, - ':' | '@' | '?' | '#' | '/' => { - // separators, don't change anything - } - _ => return Err("Illegal character in authority".to_string()), - } - - // now process states - match c { - ':' => { - colon_count += 1; - match st { - Start => { - pos = i; - st = PassHostPort; - } - PassHostPort => { - // multiple colons means ipv6 address. - if input == Unreserved { - return Err( - "Illegal characters in IPv6 address.".to_string()); - } - st = Ip6Host; - } - InHost => { - pos = i; - if input == Unreserved { - // must be port - host = rawurl.slice(begin, i); - st = InPort; - } else { - // can't be sure whether this is an ipv6 address or a port - st = Ip6Port; - } - } - Ip6Port => { - if input == Unreserved { - return Err("Illegal characters in authority.".to_string()); - } - st = Ip6Host; - } - Ip6Host => { - if colon_count > 7 { - host = rawurl.slice(begin, i); - pos = i; - st = InPort; - } - } - _ => return Err("Invalid ':' in authority.".to_string()), - } - input = Digit; // reset input class - } - - '@' => { - input = Digit; // reset input class - colon_count = 0; // reset count - match st { - Start => { - let user = rawurl.slice(begin, i).to_string(); - userinfo = Some(UserInfo::new(user, None)); - st = InHost; - } - PassHostPort => { - let user = rawurl.slice(begin, pos).to_string(); - let pass = rawurl.slice(pos+1, i).to_string(); - userinfo = Some(UserInfo::new(user, Some(pass))); - st = InHost; - } - _ => return Err("Invalid '@' in authority.".to_string()), - } - begin = i+1; - } - - '?' | '#' | '/' => { - end = i; - break; - } - _ => () - } - } - - // finish up - match st { - Start => host = rawurl.slice(begin, end), - PassHostPort - | Ip6Port => { - if input != Digit { - return Err("Non-digit characters in port.".to_string()); - } - host = rawurl.slice(begin, pos); - port = Some(rawurl.slice(pos+1, end)); - } - Ip6Host - | InHost => host = rawurl.slice(begin, end), - InPort => { - if input != Digit { - return Err("Non-digit characters in port.".to_string()); - } - port = Some(rawurl.slice(pos+1, end)); - } - } - - let rest = rawurl.slice(end, len); - // If we have a port string, ensure it parses to u16. - let port = match port { - None => None, - opt => match opt.and_then(|p| FromStr::from_str(p)) { - None => return Err(format!("Failed to parse port: {}", port)), - opt => opt - } - }; - - Ok((userinfo, host, port, rest)) -} - - -// returns the path and unparsed part of url, or an error -fn get_path(rawurl: &str, is_authority: bool) -> DecodeResult<(String, &str)> { - let len = rawurl.len(); - let mut end = len; - for (i,c) in rawurl.chars().enumerate() { - match c { - 'A' ... 'Z' - | 'a' ... 'z' - | '0' ... '9' - | '&' |'\'' | '(' | ')' | '.' - | '@' | ':' | '%' | '/' | '+' - | '!' | '*' | ',' | ';' | '=' - | '_' | '-' | '~' => continue, - '?' | '#' => { - end = i; - break; - } - _ => return Err("Invalid character in path.".to_string()) - } - } - - if is_authority && end != 0 && !rawurl.starts_with("/") { - Err("Non-empty path must begin with \ - '/' in presence of authority.".to_string()) - } else { - Ok((try!(decode_component(rawurl.slice(0, end))), - rawurl.slice(end, len))) - } -} - -// returns the parsed query and the fragment, if present -fn get_query_fragment(rawurl: &str) -> DecodeResult<(Query, Option)> { - let (before_fragment, raw_fragment) = split_char_first(rawurl, '#'); - - // Parse the fragment if available - let fragment = match raw_fragment { - "" => None, - raw => Some(try!(decode_component(raw))) - }; - - match before_fragment.slice_shift_char() { - (Some('?'), rest) => Ok((try!(query_from_str(rest)), fragment)), - (None, "") => Ok((vec!(), fragment)), - _ => Err(format!("Query didn't start with '?': '{}..'", before_fragment)), - } -} - -impl FromStr for Url { - fn from_str(s: &str) -> Option { - Url::parse(s).ok() - } -} - -impl FromStr for Path { - fn from_str(s: &str) -> Option { - Path::parse(s).ok() - } -} - -impl fmt::Show for Url { - /// Converts a URL from `Url` to string representation. - /// - /// # Returns - /// - /// A string that contains the formatted URL. Note that this will usually - /// be an inverse of `from_str` but might strip out unneeded separators; - /// for example, "http://somehost.com?", when parsed and formatted, will - /// result in just "http://somehost.com". - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{}:", self.scheme)); - - if !self.host.is_empty() { - try!(write!(f, "//")); - match self.user { - Some(ref user) => try!(write!(f, "{}", *user)), - None => {} - } - match self.port { - Some(ref port) => try!(write!(f, "{}:{}", self.host, - *port)), - None => try!(write!(f, "{}", self.host)), - } - } - - write!(f, "{}", self.path) - } -} - -impl fmt::Show for Path { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{}", self.path)); - if !self.query.is_empty() { - try!(write!(f, "?{}", query_to_str(&self.query))) - } - - match self.fragment { - Some(ref fragment) => { - write!(f, "#{}", encode_component(fragment.as_slice())) - } - None => Ok(()) - } - } -} - -impl hash::Hash for Url { - fn hash(&self, state: &mut S) { - self.to_string().hash(state) - } -} - -impl hash::Hash for Path { - fn hash(&self, state: &mut S) { - self.to_string().hash(state) - } -} - -// Put a few tests outside of the 'test' module so they can test the internal -// functions and those functions don't need 'pub' - -#[test] -fn test_split_char_first() { - let (u,v) = split_char_first("hello, sweet world", ','); - assert_eq!(u, "hello"); - assert_eq!(v, " sweet world"); - - let (u,v) = split_char_first("hello sweet world", ','); - assert_eq!(u, "hello sweet world"); - assert_eq!(v, ""); -} - -#[test] -fn test_get_authority() { - let (u, h, p, r) = get_authority( - "//user:pass@rust-lang.org/something").unwrap(); - assert_eq!(u, Some(UserInfo::new("user".to_string(), Some("pass".to_string())))); - assert_eq!(h, "rust-lang.org"); - assert!(p.is_none()); - assert_eq!(r, "/something"); - - let (u, h, p, r) = get_authority( - "//rust-lang.org:8000?something").unwrap(); - assert!(u.is_none()); - assert_eq!(h, "rust-lang.org"); - assert_eq!(p, Some(8000)); - assert_eq!(r, "?something"); - - let (u, h, p, r) = get_authority("//rust-lang.org#blah").unwrap(); - assert!(u.is_none()); - assert_eq!(h, "rust-lang.org"); - assert!(p.is_none()); - assert_eq!(r, "#blah"); - - // ipv6 tests - let (_, h, _, _) = get_authority( - "//2001:0db8:85a3:0042:0000:8a2e:0370:7334#blah").unwrap(); - assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334"); - - let (_, h, p, _) = get_authority( - "//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah").unwrap(); - assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334"); - assert_eq!(p, Some(8000)); - - let (u, h, p, _) = get_authority( - "//us:p@2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah" - ).unwrap(); - assert_eq!(u, Some(UserInfo::new("us".to_string(), Some("p".to_string())))); - assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334"); - assert_eq!(p, Some(8000)); - - // invalid authorities; - assert!(get_authority("//user:pass@rust-lang:something").is_err()); - assert!(get_authority("//user@rust-lang:something:/path").is_err()); - assert!(get_authority( - "//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a").is_err()); - assert!(get_authority( - "//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00").is_err()); - // outside u16 range - assert!(get_authority("//user:pass@rust-lang:65536").is_err()); - - // these parse as empty, because they don't start with '//' - let (_, h, _, _) = get_authority("user:pass@rust-lang").unwrap(); - assert_eq!(h, ""); - let (_, h, _, _) = get_authority("rust-lang.org").unwrap(); - assert_eq!(h, ""); -} - -#[test] -fn test_get_path() { - let (p, r) = get_path("/something+%20orother", true).unwrap(); - assert_eq!(p, "/something+ orother".to_string()); - assert_eq!(r, ""); - let (p, r) = get_path("test@email.com#fragment", false).unwrap(); - assert_eq!(p, "test@email.com".to_string()); - assert_eq!(r, "#fragment"); - let (p, r) = get_path("/gen/:addr=?q=v", false).unwrap(); - assert_eq!(p, "/gen/:addr=".to_string()); - assert_eq!(r, "?q=v"); - - //failure cases - assert!(get_path("something?q", true).is_err()); -} - -#[cfg(test)] -mod tests { - use {encode_form_urlencoded, decode_form_urlencoded, decode, encode, - encode_component, decode_component, UserInfo, get_scheme, Url, Path}; - - use std::collections::HashMap; - use std::path::BytesContainer; - - #[test] - fn test_url_parse() { - let url = "http://user:pass@rust-lang.org:8080/doc/~u?s=v#something"; - let u = from_str::(url).unwrap(); - - assert_eq!(u.scheme, "http".to_string()); - assert_eq!(u.user, Some(UserInfo::new("user".to_string(), Some("pass".to_string())))); - assert_eq!(u.host, "rust-lang.org".to_string()); - assert_eq!(u.port, Some(8080)); - assert_eq!(u.path.path, "/doc/~u".to_string()); - assert_eq!(u.path.query, vec!(("s".to_string(), "v".to_string()))); - assert_eq!(u.path.fragment, Some("something".to_string())); - } - - #[test] - fn test_path_parse() { - let path = "/doc/~u?s=v#something"; - let u = from_str::(path).unwrap(); - - assert_eq!(u.path, "/doc/~u".to_string()); - assert_eq!(u.query, vec!(("s".to_string(), "v".to_string()))); - assert_eq!(u.fragment, Some("something".to_string())); - } - - #[test] - fn test_url_parse_host_slash() { - let urlstr = "http://0.42.42.42/"; - let url = from_str::(urlstr).unwrap(); - assert_eq!(url.host, "0.42.42.42".to_string()); - assert_eq!(url.path.path, "/".to_string()); - } - - #[test] - fn test_path_parse_host_slash() { - let pathstr = "/"; - let path = from_str::(pathstr).unwrap(); - assert_eq!(path.path, "/".to_string()); - } - - #[test] - fn test_url_host_with_port() { - let urlstr = "scheme://host:1234"; - let url = from_str::(urlstr).unwrap(); - assert_eq!(url.scheme, "scheme".to_string()); - assert_eq!(url.host, "host".to_string()); - assert_eq!(url.port, Some(1234)); - // is empty path really correct? Other tests think so - assert_eq!(url.path.path, "".to_string()); - - let urlstr = "scheme://host:1234/"; - let url = from_str::(urlstr).unwrap(); - assert_eq!(url.scheme, "scheme".to_string()); - assert_eq!(url.host, "host".to_string()); - assert_eq!(url.port, Some(1234)); - assert_eq!(url.path.path, "/".to_string()); - } - - #[test] - fn test_url_with_underscores() { - let urlstr = "http://dotcom.com/file_name.html"; - let url = from_str::(urlstr).unwrap(); - assert_eq!(url.path.path, "/file_name.html".to_string()); - } - - #[test] - fn test_path_with_underscores() { - let pathstr = "/file_name.html"; - let path = from_str::(pathstr).unwrap(); - assert_eq!(path.path, "/file_name.html".to_string()); - } - - #[test] - fn test_url_with_dashes() { - let urlstr = "http://dotcom.com/file-name.html"; - let url = from_str::(urlstr).unwrap(); - assert_eq!(url.path.path, "/file-name.html".to_string()); - } - - #[test] - fn test_path_with_dashes() { - let pathstr = "/file-name.html"; - let path = from_str::(pathstr).unwrap(); - assert_eq!(path.path, "/file-name.html".to_string()); - } - - #[test] - fn test_no_scheme() { - assert!(get_scheme("noschemehere.html").is_err()); - } - - #[test] - fn test_invalid_scheme_errors() { - assert!(Url::parse("99://something").is_err()); - assert!(Url::parse("://something").is_err()); - } - - #[test] - fn test_full_url_parse_and_format() { - let url = "http://user:pass@rust-lang.org/doc?s=v#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_userless_url_parse_and_format() { - let url = "http://rust-lang.org/doc?s=v#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_queryless_url_parse_and_format() { - let url = "http://user:pass@rust-lang.org/doc#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_empty_query_url_parse_and_format() { - let url = "http://user:pass@rust-lang.org/doc?#something"; - let should_be = "http://user:pass@rust-lang.org/doc#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), should_be); - } - - #[test] - fn test_fragmentless_url_parse_and_format() { - let url = "http://user:pass@rust-lang.org/doc?q=v"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_minimal_url_parse_and_format() { - let url = "http://rust-lang.org/doc"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_url_with_port_parse_and_format() { - let url = "http://rust-lang.org:80/doc"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_scheme_host_only_url_parse_and_format() { - let url = "http://rust-lang.org"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_pathless_url_parse_and_format() { - let url = "http://user:pass@rust-lang.org?q=v#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_scheme_host_fragment_only_url_parse_and_format() { - let url = "http://rust-lang.org#something"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_url_component_encoding() { - let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B"; - let u = from_str::(url).unwrap(); - assert!(u.path.path == "/doc uments".to_string()); - assert!(u.path.query == vec!(("ba%d ".to_string(), "#&+".to_string()))); - } - - #[test] - fn test_path_component_encoding() { - let path = "/doc%20uments?ba%25d%20=%23%26%2B"; - let p = from_str::(path).unwrap(); - assert!(p.path == "/doc uments".to_string()); - assert!(p.query == vec!(("ba%d ".to_string(), "#&+".to_string()))); - } - - #[test] - fn test_url_without_authority() { - let url = "mailto:test@email.com"; - let u = from_str::(url).unwrap(); - assert_eq!(format!("{}", u).as_slice(), url); - } - - #[test] - fn test_encode() { - fn t(input: T, expected: &str) { - assert_eq!(encode(input), expected.to_string()) - } - - t("", ""); - t("http://example.com", "http://example.com"); - t("foo bar% baz", "foo%20bar%25%20baz"); - t(" ", "%20"); - t("!", "!"); - t("\"", "\""); - t("#", "#"); - t("$", "$"); - t("%", "%25"); - t("&", "&"); - t("'", "%27"); - t("(", "("); - t(")", ")"); - t("*", "*"); - t("+", "+"); - t(",", ","); - t("/", "/"); - t(":", ":"); - t(";", ";"); - t("=", "="); - t("?", "?"); - t("@", "@"); - t("[", "["); - t("]", "]"); - t("\0", "%00"); - t("\n", "%0A"); - - let a: &[_] = &[0u8, 10, 37]; - t(a, "%00%0A%25"); - } - - #[test] - fn test_encode_component() { - fn t(input: T, expected: &str) { - assert_eq!(encode_component(input), expected.to_string()) - } - - t("", ""); - t("http://example.com", "http%3A%2F%2Fexample.com"); - t("foo bar% baz", "foo%20bar%25%20baz"); - t(" ", "%20"); - t("!", "%21"); - t("#", "%23"); - t("$", "%24"); - t("%", "%25"); - t("&", "%26"); - t("'", "%27"); - t("(", "%28"); - t(")", "%29"); - t("*", "%2A"); - t("+", "%2B"); - t(",", "%2C"); - t("/", "%2F"); - t(":", "%3A"); - t(";", "%3B"); - t("=", "%3D"); - t("?", "%3F"); - t("@", "%40"); - t("[", "%5B"); - t("]", "%5D"); - t("\0", "%00"); - t("\n", "%0A"); - - let a: &[_] = &[0u8, 10, 37]; - t(a, "%00%0A%25"); - } - - #[test] - fn test_decode() { - fn t(input: T, expected: &str) { - assert_eq!(decode(input), Ok(expected.to_string())) - } - - assert!(decode("sadsadsda%").is_err()); - assert!(decode("waeasd%4").is_err()); - t("", ""); - t("abc/def 123", "abc/def 123"); - t("abc%2Fdef%20123", "abc%2Fdef 123"); - t("%20", " "); - t("%21", "%21"); - t("%22", "%22"); - t("%23", "%23"); - t("%24", "%24"); - t("%25", "%"); - t("%26", "%26"); - t("%27", "'"); - t("%28", "%28"); - t("%29", "%29"); - t("%2A", "%2A"); - t("%2B", "%2B"); - t("%2C", "%2C"); - t("%2F", "%2F"); - t("%3A", "%3A"); - t("%3B", "%3B"); - t("%3D", "%3D"); - t("%3F", "%3F"); - t("%40", "%40"); - t("%5B", "%5B"); - t("%5D", "%5D"); - - t("%00%0A%25".as_bytes(), "\0\n%"); - } - - #[test] - fn test_decode_component() { - fn t(input: T, expected: &str) { - assert_eq!(decode_component(input), Ok(expected.to_string())) - } - - assert!(decode_component("asacsa%").is_err()); - assert!(decode_component("acsas%4").is_err()); - t("", ""); - t("abc/def 123", "abc/def 123"); - t("abc%2Fdef%20123", "abc/def 123"); - t("%20", " "); - t("%21", "!"); - t("%22", "\""); - t("%23", "#"); - t("%24", "$"); - t("%25", "%"); - t("%26", "&"); - t("%27", "'"); - t("%28", "("); - t("%29", ")"); - t("%2A", "*"); - t("%2B", "+"); - t("%2C", ","); - t("%2F", "/"); - t("%3A", ":"); - t("%3B", ";"); - t("%3D", "="); - t("%3F", "?"); - t("%40", "@"); - t("%5B", "["); - t("%5D", "]"); - - t("%00%0A%25".as_bytes(), "\0\n%"); - } - - #[test] - fn test_encode_form_urlencoded() { - let mut m = HashMap::new(); - assert_eq!(encode_form_urlencoded(&m), "".to_string()); - - m.insert("".to_string(), vec!()); - m.insert("foo".to_string(), vec!()); - assert_eq!(encode_form_urlencoded(&m), "".to_string()); - - let mut m = HashMap::new(); - m.insert("foo".to_string(), vec!("bar".to_string(), "123".to_string())); - assert_eq!(encode_form_urlencoded(&m), "foo=bar&foo=123".to_string()); - - let mut m = HashMap::new(); - m.insert("foo bar".to_string(), vec!("abc".to_string(), "12 = 34".to_string())); - assert_eq!(encode_form_urlencoded(&m), - "foo+bar=abc&foo+bar=12+%3D+34".to_string()); - } - - #[test] - fn test_decode_form_urlencoded() { - assert_eq!(decode_form_urlencoded([]).unwrap().len(), 0); - - let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes(); - let form = decode_form_urlencoded(s).unwrap(); - assert_eq!(form.len(), 2); - assert_eq!(form.get(&"a".to_string()), &vec!("1".to_string())); - assert_eq!(form.get(&"foo bar".to_string()), - &vec!("abc".to_string(), "12 = 34".to_string())); - } -} diff --git a/src/libuuid/lib.rs b/src/libuuid/lib.rs deleted file mode 100644 index c041ca3799dc4..0000000000000 --- a/src/libuuid/lib.rs +++ /dev/null @@ -1,865 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! -Generate and parse UUIDs - -Provides support for Universally Unique Identifiers (UUIDs). A UUID is a -unique 128-bit number, stored as 16 octets. UUIDs are used to assign unique -identifiers to entities without requiring a central allocating authority. - -They are particularly useful in distributed systems, though can be used in -disparate areas, such as databases and network protocols. Typically a UUID is -displayed in a readable string form as a sequence of hexadecimal digits, -separated into groups by hyphens. - -The uniqueness property is not strictly guaranteed, however for all practical -purposes, it can be assumed that an unintentional collision would be extremely -unlikely. - -# Examples - -To create a new random (V4) UUID and print it out in hexadecimal form: - -```rust -# #![allow(deprecated)] -# extern crate uuid; -use uuid::Uuid; - -fn main() { - let uuid1 = Uuid::new_v4(); - println!("{}", uuid1.to_string()); -} -``` - -# Strings - -Examples of string representations: - -* simple: `936DA01F9ABD4d9d80C702AF85C822A8` -* hyphenated: `550e8400-e29b-41d4-a716-446655440000` -* urn: `urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4` - -# References - -* [Wikipedia: Universally Unique Identifier]( - http://en.wikipedia.org/wiki/Universally_unique_identifier) -* [RFC4122: A Universally Unique IDentifier (UUID) URN Namespace]( - http://tools.ietf.org/html/rfc4122) - -*/ - -#![crate_name = "uuid"] -#![deprecated = "This is now a cargo package located at: \ - https://github.com/rust-lang/uuid"] -#![allow(deprecated)] -#![crate_type = "rlib"] -#![crate_type = "dylib"] -#![license = "MIT/ASL2"] -#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "http://www.rust-lang.org/favicon.ico", - html_root_url = "http://doc.rust-lang.org/nightly/", - html_playground_url = "http://play.rust-lang.org/")] - -#![feature(default_type_params)] - -// test harness access -#[cfg(test)] -extern crate test; -extern crate serialize; - -use std::char::Char; -use std::default::Default; -use std::fmt; -use std::from_str::FromStr; -use std::hash; -use std::mem::{transmute,transmute_copy}; -use std::num::FromStrRadix; -use std::rand; -use std::rand::Rng; -use std::slice; - -use serialize::{Encoder, Encodable, Decoder, Decodable}; - -/// A 128-bit (16 byte) buffer containing the ID -pub type UuidBytes = [u8, ..16]; - -/// The version of the UUID, denoting the generating algorithm -#[deriving(PartialEq)] -pub enum UuidVersion { - /// Version 1: MAC address - Version1Mac = 1, - /// Version 2: DCE Security - Version2Dce = 2, - /// Version 3: MD5 hash - Version3Md5 = 3, - /// Version 4: Random - Version4Random = 4, - /// Version 5: SHA-1 hash - Version5Sha1 = 5, -} - -/// The reserved variants of UUIDs -#[deriving(PartialEq)] -pub enum UuidVariant { - /// Reserved by the NCS for backward compatibility - VariantNCS, - /// As described in the RFC4122 Specification (default) - VariantRFC4122, - /// Reserved by Microsoft for backward compatibility - VariantMicrosoft, - /// Reserved for future expansion - VariantFuture, -} - -/// A Universally Unique Identifier (UUID) -pub struct Uuid { - /// The 128-bit number stored in 16 bytes - bytes: UuidBytes -} - -impl hash::Hash for Uuid { - fn hash(&self, state: &mut S) { - self.bytes.hash(state) - } -} - -/// A UUID stored as fields (identical to UUID, used only for conversions) -struct UuidFields { - /// First field, 32-bit word - data1: u32, - /// Second field, 16-bit short - data2: u16, - /// Third field, 16-bit short - data3: u16, - /// Fourth field, 8 bytes - data4: [u8, ..8] -} - -/// Error details for string parsing failures -#[allow(missing_doc)] -pub enum ParseError { - ErrorInvalidLength(uint), - ErrorInvalidCharacter(char, uint), - ErrorInvalidGroups(uint), - ErrorInvalidGroupLength(uint, uint, uint), -} - -/// Converts a ParseError to a string -impl fmt::Show for ParseError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - ErrorInvalidLength(found) => - write!(f, "Invalid length; expecting 32, 36 or 45 chars, \ - found {}", found), - ErrorInvalidCharacter(found, pos) => - write!(f, "Invalid character; found `{}` (0x{:02x}) at \ - offset {}", found, found as uint, pos), - ErrorInvalidGroups(found) => - write!(f, "Malformed; wrong number of groups: expected 1 \ - or 5, found {}", found), - ErrorInvalidGroupLength(group, found, expecting) => - write!(f, "Malformed; length of group {} was {}, \ - expecting {}", group, found, expecting), - } - } -} - -// Length of each hyphenated group in hex digits -#[allow(non_uppercase_statics)] -static UuidGroupLens: [uint, ..5] = [8u, 4u, 4u, 4u, 12u]; - -/// UUID support -impl Uuid { - /// Returns a nil or empty UUID (containing all zeroes) - pub fn nil() -> Uuid { - let uuid = Uuid{ bytes: [0, .. 16] }; - uuid - } - - /// Create a new UUID of the specified version - pub fn new(v: UuidVersion) -> Option { - match v { - Version4Random => Some(Uuid::new_v4()), - _ => None - } - } - - /// Creates a new random UUID - /// - /// Uses the `rand` module's default RNG task as the source - /// of random numbers. Use the rand::Rand trait to supply - /// a custom generator if required. - pub fn new_v4() -> Uuid { - let ub = rand::task_rng().gen_iter::().take(16).collect::>(); - let mut uuid = Uuid{ bytes: [0, .. 16] }; - slice::bytes::copy_memory(uuid.bytes, ub.as_slice()); - uuid.set_variant(VariantRFC4122); - uuid.set_version(Version4Random); - uuid - } - - /// Creates a UUID using the supplied field values - /// - /// # Arguments - /// * `d1` A 32-bit word - /// * `d2` A 16-bit word - /// * `d3` A 16-bit word - /// * `d4` Array of 8 octets - pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid { - // First construct a temporary field-based struct - let mut fields = UuidFields { - data1: 0, - data2: 0, - data3: 0, - data4: [0, ..8] - }; - - fields.data1 = d1.to_be(); - fields.data2 = d2.to_be(); - fields.data3 = d3.to_be(); - slice::bytes::copy_memory(fields.data4, d4); - - unsafe { - transmute(fields) - } - } - - /// Creates a UUID using the supplied bytes - /// - /// # Arguments - /// * `b` An array or slice of 16 bytes - pub fn from_bytes(b: &[u8]) -> Option { - if b.len() != 16 { - return None - } - - let mut uuid = Uuid{ bytes: [0, .. 16] }; - slice::bytes::copy_memory(uuid.bytes, b); - Some(uuid) - } - - /// Specifies the variant of the UUID structure - fn set_variant(&mut self, v: UuidVariant) { - // Octet 8 contains the variant in the most significant 3 bits - match v { - VariantNCS => // b0xx... - self.bytes[8] = self.bytes[8] & 0x7f, - VariantRFC4122 => // b10x... - self.bytes[8] = (self.bytes[8] & 0x3f) | 0x80, - VariantMicrosoft => // b110... - self.bytes[8] = (self.bytes[8] & 0x1f) | 0xc0, - VariantFuture => // b111... - self.bytes[8] = (self.bytes[8] & 0x1f) | 0xe0, - } - } - - /// Returns the variant of the UUID structure - /// - /// This determines the interpretation of the structure of the UUID. - /// Currently only the RFC4122 variant is generated by this module. - /// - /// * [Variant Reference](http://tools.ietf.org/html/rfc4122#section-4.1.1) - pub fn get_variant(&self) -> Option { - if self.bytes[8] & 0x80 == 0x00 { - Some(VariantNCS) - } else if self.bytes[8] & 0xc0 == 0x80 { - Some(VariantRFC4122) - } else if self.bytes[8] & 0xe0 == 0xc0 { - Some(VariantMicrosoft) - } else if self.bytes[8] & 0xe0 == 0xe0 { - Some(VariantFuture) - } else { - None - } - } - - /// Specifies the version number of the UUID - fn set_version(&mut self, v: UuidVersion) { - self.bytes[6] = (self.bytes[6] & 0xF) | ((v as u8) << 4); - } - - /// Returns the version number of the UUID - /// - /// This represents the algorithm used to generate the contents. - /// - /// Currently only the Random (V4) algorithm is supported by this - /// module. There are security and privacy implications for using - /// older versions - see [Wikipedia: Universally Unique Identifier]( - /// http://en.wikipedia.org/wiki/Universally_unique_identifier) for - /// details. - /// - /// * [Version Reference](http://tools.ietf.org/html/rfc4122#section-4.1.3) - pub fn get_version_num(&self) -> uint { - (self.bytes[6] >> 4) as uint - } - - /// Returns the version of the UUID - /// - /// This represents the algorithm used to generate the contents - pub fn get_version(&self) -> Option { - let v = self.bytes[6] >> 4; - match v { - 1 => Some(Version1Mac), - 2 => Some(Version2Dce), - 3 => Some(Version3Md5), - 4 => Some(Version4Random), - 5 => Some(Version5Sha1), - _ => None - } - } - - /// Return an array of 16 octets containing the UUID data - pub fn as_bytes(&self) -> &[u8] { - self.bytes.as_slice() - } - - /// Returns the UUID as a string of 16 hexadecimal digits - /// - /// Example: `936DA01F9ABD4d9d80C702AF85C822A8` - pub fn to_simple_str(&self) -> String { - let mut s: Vec = Vec::from_elem(32, 0u8); - for i in range(0u, 16u) { - let digit = format!("{:02x}", self.bytes[i] as uint); - *s.get_mut(i*2+0) = digit.as_bytes()[0]; - *s.get_mut(i*2+1) = digit.as_bytes()[1]; - } - String::from_utf8(s).unwrap() - } - - /// Returns a string of hexadecimal digits, separated into groups with a hyphen. - /// - /// Example: `550e8400-e29b-41d4-a716-446655440000` - pub fn to_hyphenated_str(&self) -> String { - // Convert to field-based struct as it matches groups in output. - // Ensure fields are in network byte order, as per RFC. - let mut uf: UuidFields; - unsafe { - uf = transmute_copy(&self.bytes); - } - uf.data1 = uf.data1.to_be(); - uf.data2 = uf.data2.to_be(); - uf.data3 = uf.data3.to_be(); - let s = format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-\ - {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", - uf.data1, - uf.data2, uf.data3, - uf.data4[0], uf.data4[1], - uf.data4[2], uf.data4[3], uf.data4[4], - uf.data4[5], uf.data4[6], uf.data4[7]); - s - } - - /// Returns the UUID formatted as a full URN string - /// - /// This is the same as the hyphenated format, but with the "urn:uuid:" prefix. - /// - /// Example: `urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4` - pub fn to_urn_str(&self) -> String { - format!("urn:uuid:{}", self.to_hyphenated_str()) - } - - /// Parses a UUID from a string of hexadecimal digits with optional hyphens - /// - /// Any of the formats generated by this module (simple, hyphenated, urn) are - /// supported by this parsing function. - pub fn parse_string(us: &str) -> Result { - - let mut us = us.clone(); - let orig_len = us.len(); - - // Ensure length is valid for any of the supported formats - if orig_len != 32 && orig_len != 36 && orig_len != 45 { - return Err(ErrorInvalidLength(orig_len)); - } - - // Strip off URN prefix if present - if us.starts_with("urn:uuid:") { - us = us.slice(9, orig_len); - } - - // Make sure all chars are either hex digits or hyphen - for (i, c) in us.chars().enumerate() { - match c { - '0'...'9' | 'A'...'F' | 'a'...'f' | '-' => {}, - _ => return Err(ErrorInvalidCharacter(c, i)), - } - } - - // Split string up by hyphens into groups - let hex_groups: Vec<&str> = us.split_str("-").collect(); - - // Get the length of each group - let group_lens: Vec = hex_groups.iter().map(|&v| v.len()).collect(); - - // Ensure the group lengths are valid - match group_lens.len() { - // Single group, no hyphens - 1 => { - if group_lens[0] != 32 { - return Err(ErrorInvalidLength(group_lens[0])); - } - }, - // Five groups, hyphens in between each - 5 => { - // Ensure each group length matches the expected - for (i, (&gl, &expected)) in - group_lens.iter().zip(UuidGroupLens.iter()).enumerate() { - if gl != expected { - return Err(ErrorInvalidGroupLength(i, gl, expected)) - } - } - }, - _ => { - return Err(ErrorInvalidGroups(group_lens.len())); - } - } - - // Normalise into one long hex string - let vs = hex_groups.concat(); - - // At this point, we know we have a valid hex string, without hyphens - assert!(vs.len() == 32); - assert!(vs.as_slice().chars().all(|c| c.is_digit_radix(16))); - - // Allocate output UUID buffer - let mut ub = [0u8, ..16]; - - // Extract each hex digit from the string - for i in range(0u, 16u) { - ub[i] = FromStrRadix::from_str_radix(vs.as_slice() - .slice(i*2, (i+1)*2), - 16).unwrap(); - } - - Ok(Uuid::from_bytes(ub).unwrap()) - } - - /// Tests if the UUID is nil - pub fn is_nil(&self) -> bool { - return self.bytes.iter().all(|&b| b == 0); - } -} - -impl Default for Uuid { - /// Returns the nil UUID, which is all zeroes - fn default() -> Uuid { - Uuid::nil() - } -} - -impl Clone for Uuid { - /// Returns a copy of the UUID - fn clone(&self) -> Uuid { *self } -} - -impl FromStr for Uuid { - /// Parse a hex string and interpret as a UUID - /// - /// Accepted formats are a sequence of 32 hexadecimal characters, - /// with or without hyphens (grouped as 8, 4, 4, 4, 12). - fn from_str(us: &str) -> Option { - let result = Uuid::parse_string(us); - match result { - Ok(u) => Some(u), - Err(_) => None - } - } -} - -/// Convert the UUID to a hexadecimal-based string representation -impl fmt::Show for Uuid { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_simple_str()) - } -} - -/// Test two UUIDs for equality -/// -/// UUIDs are equal only when they are byte-for-byte identical -impl PartialEq for Uuid { - fn eq(&self, other: &Uuid) -> bool { - self.bytes == other.bytes - } -} - -impl Eq for Uuid {} - -// FIXME #9845: Test these more thoroughly -impl, E> Encodable for Uuid { - /// Encode a UUID as a hyphenated string - fn encode(&self, e: &mut T) -> Result<(), E> { - e.emit_str(self.to_hyphenated_str().as_slice()) - } -} - -impl, E> Decodable for Uuid { - /// Decode a UUID from a string - fn decode(d: &mut T) -> Result { - match from_str(try!(d.read_str()).as_slice()) { - Some(decode) => Ok(decode), - None => Err(d.error("Unable to decode UUID")) - } - } -} - -/// Generates a random instance of UUID (V4 conformant) -impl rand::Rand for Uuid { - #[inline] - fn rand(rng: &mut R) -> Uuid { - let ub = rng.gen_iter::().take(16).collect::>(); - let mut uuid = Uuid{ bytes: [0, .. 16] }; - slice::bytes::copy_memory(uuid.bytes, ub.as_slice()); - uuid.set_variant(VariantRFC4122); - uuid.set_version(Version4Random); - uuid - } -} - -#[cfg(test)] -mod uuidtest { - use super::{Uuid, VariantMicrosoft, VariantNCS, VariantRFC4122, - Version1Mac, Version2Dce, Version3Md5, Version4Random, - Version5Sha1}; - use std::rand; - - #[test] - fn test_nil() { - let nil = Uuid::nil(); - let not_nil = Uuid::new_v4(); - - assert!(nil.is_nil()); - assert!(!not_nil.is_nil()); - } - - #[test] - fn test_new() { - // Supported - let uuid1 = Uuid::new(Version4Random).unwrap(); - let s = uuid1.to_simple_str(); - - assert!(s.len() == 32); - assert!(uuid1.get_version().unwrap() == Version4Random); - - // Test unsupported versions - assert!(Uuid::new(Version1Mac) == None); - assert!(Uuid::new(Version2Dce) == None); - assert!(Uuid::new(Version3Md5) == None); - assert!(Uuid::new(Version5Sha1) == None); - } - - #[test] - fn test_new_v4() { - let uuid1 = Uuid::new_v4(); - - assert!(uuid1.get_version().unwrap() == Version4Random); - assert!(uuid1.get_variant().unwrap() == VariantRFC4122); - } - - #[test] - fn test_get_version() { - let uuid1 = Uuid::new_v4(); - - assert!(uuid1.get_version().unwrap() == Version4Random); - assert!(uuid1.get_version_num() == 4); - } - - #[test] - fn test_get_variant() { - let uuid1 = Uuid::new_v4(); - let uuid2 = Uuid::parse_string("550e8400-e29b-41d4-a716-446655440000").unwrap(); - let uuid3 = Uuid::parse_string("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap(); - let uuid4 = Uuid::parse_string("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap(); - let uuid5 = Uuid::parse_string("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap(); - let uuid6 = Uuid::parse_string("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap(); - - assert!(uuid1.get_variant().unwrap() == VariantRFC4122); - assert!(uuid2.get_variant().unwrap() == VariantRFC4122); - assert!(uuid3.get_variant().unwrap() == VariantRFC4122); - assert!(uuid4.get_variant().unwrap() == VariantMicrosoft); - assert!(uuid5.get_variant().unwrap() == VariantMicrosoft); - assert!(uuid6.get_variant().unwrap() == VariantNCS); - } - - #[test] - fn test_parse_uuid_v4() { - use super::{ErrorInvalidCharacter, ErrorInvalidGroups, - ErrorInvalidGroupLength, ErrorInvalidLength}; - - // Invalid - assert!(Uuid::parse_string("").is_err()); - assert!(Uuid::parse_string("!").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E45").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa-BBF-329BF39FA1E4").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa-BGBF-329BF39FA1E4").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa-B6BFF329BF39FA1E4").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faaXB6BFF329BF39FA1E4").is_err()); - assert!(Uuid::parse_string("F9168C5E-CEB-24fa-eB6BFF32-BF39FA1E4").is_err()); - assert!(Uuid::parse_string("01020304-1112-2122-3132-41424344").is_err()); - assert!(Uuid::parse_string("67e5504410b1426f9247bb680e5fe0c").is_err()); - assert!(Uuid::parse_string("67e5504410b1426f9247bb680e5fe0c88").is_err()); - assert!(Uuid::parse_string("67e5504410b1426f9247bb680e5fe0cg8").is_err()); - assert!(Uuid::parse_string("67e5504410b1426%9247bb680e5fe0c8").is_err()); - - // Valid - assert!(Uuid::parse_string("00000000000000000000000000000000").is_ok()); - assert!(Uuid::parse_string("67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok()); - assert!(Uuid::parse_string("67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok()); - assert!(Uuid::parse_string("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").is_ok()); - assert!(Uuid::parse_string("67e5504410b1426f9247bb680e5fe0c8").is_ok()); - assert!(Uuid::parse_string("01020304-1112-2122-3132-414243444546").is_ok()); - assert!(Uuid::parse_string("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok()); - - // Nil - let nil = Uuid::nil(); - assert!(Uuid::parse_string("00000000000000000000000000000000").unwrap() == nil); - assert!(Uuid::parse_string("00000000-0000-0000-0000-000000000000").unwrap() == nil); - - // Round-trip - let uuid_orig = Uuid::new_v4(); - let orig_str = uuid_orig.to_string(); - let uuid_out = Uuid::parse_string(orig_str.as_slice()).unwrap(); - assert!(uuid_orig == uuid_out); - - // Test error reporting - let e = Uuid::parse_string("67e5504410b1426f9247bb680e5fe0c").unwrap_err(); - assert!(match e { ErrorInvalidLength(n) => n==31, _ => false }); - - let e = Uuid::parse_string("67e550X410b1426f9247bb680e5fe0cd").unwrap_err(); - assert!(match e { ErrorInvalidCharacter(c, n) => c=='X' && n==6, _ => false }); - - let e = Uuid::parse_string("67e550-4105b1426f9247bb680e5fe0c").unwrap_err(); - assert!(match e { ErrorInvalidGroups(n) => n==2, _ => false }); - - let e = Uuid::parse_string("F9168C5E-CEB2-4faa-B6BF1-02BF39FA1E4").unwrap_err(); - assert!(match e { ErrorInvalidGroupLength(g, n, e) => g==3 && n==5 && e==4, _ => false }); - } - - #[test] - fn test_to_simple_str() { - let uuid1 = Uuid::new_v4(); - let s = uuid1.to_simple_str(); - - assert!(s.len() == 32); - assert!(s.as_slice().chars().all(|c| c.is_digit_radix(16))); - } - - #[test] - fn test_to_string() { - let uuid1 = Uuid::new_v4(); - let s = uuid1.to_string(); - - assert!(s.len() == 32); - assert!(s.as_slice().chars().all(|c| c.is_digit_radix(16))); - } - - #[test] - fn test_to_hyphenated_str() { - let uuid1 = Uuid::new_v4(); - let s = uuid1.to_hyphenated_str(); - - assert!(s.len() == 36); - assert!(s.as_slice().chars().all(|c| c.is_digit_radix(16) || c == '-')); - } - - #[test] - fn test_to_urn_str() { - let uuid1 = Uuid::new_v4(); - let ss = uuid1.to_urn_str(); - let s = ss.as_slice().slice(9, ss.len()); - - assert!(ss.as_slice().starts_with("urn:uuid:")); - assert!(s.len() == 36); - assert!(s.as_slice() - .chars() - .all(|c| c.is_digit_radix(16) || c == '-')); - } - - #[test] - fn test_to_str_matching() { - let uuid1 = Uuid::new_v4(); - - let hs = uuid1.to_hyphenated_str(); - let ss = uuid1.to_string(); - - let hsn = String::from_chars(hs.as_slice() - .chars() - .filter(|&c| c != '-') - .collect::>() - .as_slice()); - - assert!(hsn == ss); - } - - #[test] - fn test_string_roundtrip() { - let uuid = Uuid::new_v4(); - - let hs = uuid.to_hyphenated_str(); - let uuid_hs = Uuid::parse_string(hs.as_slice()).unwrap(); - assert!(uuid_hs == uuid); - - let ss = uuid.to_string(); - let uuid_ss = Uuid::parse_string(ss.as_slice()).unwrap(); - assert!(uuid_ss == uuid); - } - - #[test] - fn test_compare() { - let uuid1 = Uuid::new_v4(); - let uuid2 = Uuid::new_v4(); - - assert!(uuid1 == uuid1); - assert!(uuid2 == uuid2); - assert!(uuid1 != uuid2); - assert!(uuid2 != uuid1); - } - - #[test] - fn test_from_fields() { - let d1: u32 = 0xa1a2a3a4; - let d2: u16 = 0xb1b2; - let d3: u16 = 0xc1c2; - let d4: Vec = vec!(0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8); - - let u = Uuid::from_fields(d1, d2, d3, d4.as_slice()); - - let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8".to_string(); - let result = u.to_simple_str(); - assert!(result == expected); - } - - #[test] - fn test_from_bytes() { - let b = vec!( 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, - 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8 ); - - let u = Uuid::from_bytes(b.as_slice()).unwrap(); - let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8".to_string(); - - assert!(u.to_simple_str() == expected); - } - - #[test] - fn test_as_bytes() { - let u = Uuid::new_v4(); - let ub = u.as_bytes(); - - assert!(ub.len() == 16); - assert!(! ub.iter().all(|&b| b == 0)); - } - - #[test] - fn test_bytes_roundtrip() { - let b_in: [u8, ..16] = [ 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, - 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8 ]; - - let u = Uuid::from_bytes(b_in.clone()).unwrap(); - - let b_out = u.as_bytes(); - - assert!(b_in == b_out); - } - - #[test] - fn test_operator_eq() { - let u1 = Uuid::new_v4(); - let u2 = u1.clone(); - let u3 = Uuid::new_v4(); - - assert!(u1 == u1); - assert!(u1 == u2); - assert!(u2 == u1); - - assert!(u1 != u3); - assert!(u3 != u1); - assert!(u2 != u3); - assert!(u3 != u2); - } - - #[test] - fn test_rand_rand() { - let mut rng = rand::task_rng(); - let u: Uuid = rand::Rand::rand(&mut rng); - let ub = u.as_bytes(); - - assert!(ub.len() == 16); - assert!(! ub.iter().all(|&b| b == 0)); - } - - #[test] - fn test_serialize_round_trip() { - use serialize::json; - - let u = Uuid::new_v4(); - let s = json::encode(&u); - let u2 = json::decode(s.as_slice()).unwrap(); - assert_eq!(u, u2); - } - - #[test] - fn test_bad_decode() { - use serialize::json; - use serialize::{Decodable}; - - let js_good = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a8".to_string()); - let js_bad1 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7ah".to_string()); - let js_bad2 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a".to_string()); - - let u_good: Result = Decodable::decode(&mut json::Decoder::new(js_good)); - let u_bad1: Result = Decodable::decode(&mut json::Decoder::new(js_bad1)); - let u_bad2: Result = Decodable::decode(&mut json::Decoder::new(js_bad2)); - assert!(u_good.is_ok()); - assert!(u_bad1.is_err()); - assert!(u_bad2.is_err()); - } - - #[test] - fn test_iterbytes_impl_for_uuid() { - use std::collections::HashSet; - let mut set = HashSet::new(); - let id1 = Uuid::new_v4(); - let id2 = Uuid::new_v4(); - set.insert(id1); - assert!(set.contains(&id1)); - assert!(!set.contains(&id2)); - } -} - -#[cfg(test)] -mod bench { - extern crate test; - use self::test::Bencher; - use super::Uuid; - - #[bench] - pub fn create_uuids(b: &mut Bencher) { - b.iter(|| { - Uuid::new_v4(); - }) - } - - #[bench] - pub fn uuid_to_string(b: &mut Bencher) { - let u = Uuid::new_v4(); - b.iter(|| { - u.to_string(); - }) - } - - #[bench] - pub fn parse_str(b: &mut Bencher) { - let s = "urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"; - b.iter(|| { - Uuid::parse_string(s).unwrap(); - }) - } -}