<commit_before>// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn f() -> String {  //~ ERROR E0269
                    //~^ HELP detailed explanation
    0u8;
    "bla".to_string();  //~ HELP consider removing this semicolon
}

fn main() {}
<commit_msg>Beef up test<commit_after>// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn f() -> String {  //~ ERROR E0269
                    //~^ HELP detailed explanation
    0u8;
    "bla".to_string();  //~ HELP consider removing this semicolon
}

fn g() -> String {  //~ ERROR E0269
                    //~^ HELP detailed explanation
    "this won't work".to_string();
    "removeme".to_string(); //~ HELP consider removing this semicolon
}

fn main() {}
<|endoftext|>


<commit_before><commit_msg>Oh, I'm funny<commit_after><|endoftext|>


<commit_before><commit_msg>Check that `chdir` fails for non-utf8 paths<commit_after>// compile-flags: -Zmiri-disable-isolation

extern {
    pub fn chdir(dir: *const u8) -> i32;
}

fn main() {
    let path = vec![0xc3u8, 0x28, 0];
    // test that `chdir` errors with invalid utf-8 path
    unsafe { chdir(path.as_ptr()) };  //~ ERROR is not a valid utf-8 string
}
<|endoftext|>


<commit_before><commit_msg>Reorganized type_system. Added some doc-comments.<commit_after><|endoftext|>


<commit_before>use std::fs::File;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::default::Default;
use error::Result;
use chrono::{DateTime, FixedOffset, Datelike, Timelike};
use yaml_rust::{Yaml, YamlLoader};
use std::io::Read;
use regex::Regex;
use rss;

use liquid::{Renderable, LiquidOptions, Context, Value};

use pulldown_cmark as cmark;
use liquid;

#[derive(Debug)]
pub struct Document {
    pub path: String,
    pub attributes: HashMap<String, Value>,
    pub content: String,
    pub layout: Option<String>,
    pub is_post: bool,
    pub date: Option<DateTime<FixedOffset>>,
    file_path: String,
    markdown: bool,
}

fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
    let mut file = try!(File::open(path));
    let mut text = String::new();
    try!(file.read_to_string(&mut text));
    Ok(text)
}

fn yaml_to_liquid(yaml: &Yaml) -> Option<Value> {
    match *yaml {
        Yaml::Real(ref s) |
        Yaml::String(ref s) => Some(Value::Str(s.to_owned())),
        Yaml::Integer(i) => Some(Value::Num(i as f32)),
        Yaml::Boolean(b) => Some(Value::Bool(b)),
        Yaml::Array(ref a) => Some(Value::Array(a.iter().filter_map(yaml_to_liquid).collect())),
        Yaml::BadValue | Yaml::Null => None,
        _ => panic!("Not implemented yet"),
    }
}

/// Formats a user specified custom path, adding custom parameters
/// and "exploding" the URL.
fn format_path(p: &str,
               attributes: &HashMap<String, Value>,
               date: &Option<DateTime<FixedOffset>>)
               -> Result<String> {
    let mut p = p.to_owned();

    let time_vars = Regex::new(":(year|month|i_month|day|i_day|short_year|hour|minute|second)")
        .unwrap();
    if time_vars.is_match(&p) {
        let date =
            try!(date.ok_or(format!("Can not format file path without a valid date ({:?})", p)));

        p = p.replace(":year", &date.year().to_string());
        p = p.replace(":month", &format!("{:02}", &date.month()));
        p = p.replace(":i_month", &date.month().to_string());
        p = p.replace(":day", &format!("{:02}", &date.day()));
        p = p.replace(":i_day", &date.day().to_string());
        p = p.replace(":hour", &format!("{:02}", &date.hour()));
        p = p.replace(":minute", &format!("{:02}", &date.minute()));
        p = p.replace(":second", &format!("{:02}", &date.second()));
    }

    for (key, val) in attributes {
        p = match *val {
            Value::Str(ref v) => p.replace(&(String::from(":") + key), v),
            Value::Num(ref v) => p.replace(&(String::from(":") + key), &v.to_string()),
            _ => p,
        }
    }

    // TODO if title is present inject title slug

    let mut path = Path::new(&p);

    // remove the root prefix (leading slash on unix systems)
    if path.has_root() {
        let mut components = path.components();
        components.next();
        path = components.as_path();
    }

    let mut path_buf = path.to_path_buf();

    // explode the url if no extension was specified
    if path_buf.extension().is_none() {
        path_buf.push("index.html")
    }

    Ok(path_buf.to_string_lossy().into_owned())
}

impl Document {
    pub fn new(path: String,
               attributes: HashMap<String, Value>,
               content: String,
               layout: Option<String>,
               is_post: bool,
               date: Option<DateTime<FixedOffset>>,
               file_path: String,
               markdown: bool)
               -> Document {
        Document {
            path: path,
            attributes: attributes,
            content: content,
            layout: layout,
            is_post: is_post,
            date: date,
            file_path: file_path,
            markdown: markdown,
        }
    }

    pub fn parse(file_path: &Path,
                 source: &Path,
                 mut is_post: bool,
                 post_path: &Option<String>)
                 -> Result<Document> {
        let mut attributes = HashMap::new();
        let mut content = try!(read_file(file_path));

        // if there is front matter, split the file and parse it
        // TODO: make this a regex to support lines of any length
        if content.contains("---") {
            let content2 = content.clone();
            let mut content_splits = content2.splitn(2, "---");

            // above the split are the attributes
            let attribute_string = content_splits.next().unwrap_or("");

            // everything below the split becomes the new content
            content = content_splits.next().unwrap_or("").to_owned();

            let yaml_result = try!(YamlLoader::load_from_str(attribute_string));

            let yaml_attributes = try!(yaml_result[0]
                .as_hash()
                .ok_or(format!("Incorrect front matter format in {:?}", file_path)));

            for (key, value) in yaml_attributes {
                if let Some(v) = yaml_to_liquid(value) {
                    attributes.insert(try!(key.as_str().ok_or(format!("Invalid key {:?}", key)))
                                          .to_owned(),
                                      v);
                }
            }
        }

        if let &mut Value::Bool(val) = attributes.entry("is_post".to_owned())
            .or_insert(Value::Bool(is_post)) {
            is_post = val;
        }

        let date = attributes.get("date")
            .and_then(|d| d.as_str())
            .and_then(|d| DateTime::parse_from_str(d, "%d %B %Y %H:%M:%S %z").ok());

        // if the file has a .md extension we assume it's markdown
        // TODO add a "markdown" flag to yaml front matter
        let markdown = file_path.extension().unwrap_or(OsStr::new("")) == OsStr::new("md");

        let layout = attributes.get("extends").and_then(|l| l.as_str()).map(|x| x.to_owned());

        let new_path = try!(file_path.strip_prefix(source)
            .map_err(|_| "File path not in source".to_owned()));

        let mut path_buf = PathBuf::from(new_path);
        path_buf.set_extension("html");

        // if the user specified a custom path override
        // format it and push it over the original file name
        // TODO replace "date", "pretty", "ordinal" and "none"
        // for Jekyl compatibility
        if let Some(path) = attributes.get("path").and_then(|p| p.as_str()) {
            path_buf = PathBuf::from(try!(format_path(path, &attributes, &date)));
        } else if is_post {
            // check if there is a global setting for post paths
            if let &Some(ref path) = post_path {
                path_buf = PathBuf::from(try!(format_path(path, &attributes, &date)));
            }
        };

        let path = try!(path_buf.to_str()
            .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path_buf)));

        // Swap back slashes to forward slashes to ensure the URL's are valid on Windows
        attributes.insert("path".to_owned(), Value::Str(path.replace("\\", "/")));

        Ok(Document::new(path.to_owned(),
                         attributes,
                         content,
                         layout,
                         is_post,
                         date,
                         file_path.to_string_lossy().into_owned(),
                         markdown))
    }

    /// Metadata for generating RSS feeds
    pub fn to_rss(&self, root_url: &str) -> rss::Item {
        rss::Item {
            title: self.attributes.get("title").and_then(|s| s.as_str()).map(|s| s.to_owned()),
            link: Some(root_url.to_owned() + &self.path),
            pub_date: self.date.map(|date| date.to_rfc2822()),
            description: self.attributes
                .get("description")
                .and_then(|s| s.as_str())
                .map(|s| s.to_owned()),
            ..Default::default()
        }
    }

    pub fn as_html(&self,
                   source: &Path,
                   post_data: &[Value],
                   layouts_path: &Path)
                   -> Result<String> {
        let options = LiquidOptions { file_system: Some(source.to_owned()), ..Default::default() };
        let template = try!(liquid::parse(&self.content, options));

        let layout = if let Some(ref layout) = self.layout {
            Some(try!(read_file(layouts_path.join(layout)).map_err(|e| {
                format!("Layout {} can not be read (defined in {}): {}",
                        layout,
                        self.file_path,
                        e)
            })))
        } else {
            None
        };

        let mut data = Context::with_values(self.attributes.clone());
        data.set_val("posts", Value::Array(post_data.to_vec()));

        let mut html = try!(template.render(&mut data)).unwrap_or(String::new());

        if self.markdown {
            html = {
                let mut buf = String::new();
                let parser = cmark::Parser::new(&html);
                cmark::html::push_html(&mut buf, parser);
                buf
            };
        }

        let options = LiquidOptions { file_system: Some(source.to_owned()), ..Default::default() };

        let template = if let Some(layout) = layout {
            data.set_val("content", Value::Str(html));

            try!(liquid::parse(&layout, options))
        } else {
            try!(liquid::parse(&html, options))
        };

        Ok(try!(template.render(&mut data)).unwrap_or(String::new()))
    }
}
<commit_msg>Fix Windows RSS paths<commit_after>use std::fs::File;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::default::Default;
use error::Result;
use chrono::{DateTime, FixedOffset, Datelike, Timelike};
use yaml_rust::{Yaml, YamlLoader};
use std::io::Read;
use regex::Regex;
use rss;

use liquid::{Renderable, LiquidOptions, Context, Value};

use pulldown_cmark as cmark;
use liquid;

#[derive(Debug)]
pub struct Document {
    pub path: String,
    pub attributes: HashMap<String, Value>,
    pub content: String,
    pub layout: Option<String>,
    pub is_post: bool,
    pub date: Option<DateTime<FixedOffset>>,
    file_path: String,
    markdown: bool,
}

fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
    let mut file = try!(File::open(path));
    let mut text = String::new();
    try!(file.read_to_string(&mut text));
    Ok(text)
}

fn yaml_to_liquid(yaml: &Yaml) -> Option<Value> {
    match *yaml {
        Yaml::Real(ref s) |
        Yaml::String(ref s) => Some(Value::Str(s.to_owned())),
        Yaml::Integer(i) => Some(Value::Num(i as f32)),
        Yaml::Boolean(b) => Some(Value::Bool(b)),
        Yaml::Array(ref a) => Some(Value::Array(a.iter().filter_map(yaml_to_liquid).collect())),
        Yaml::BadValue | Yaml::Null => None,
        _ => panic!("Not implemented yet"),
    }
}

/// Formats a user specified custom path, adding custom parameters
/// and "exploding" the URL.
fn format_path(p: &str,
               attributes: &HashMap<String, Value>,
               date: &Option<DateTime<FixedOffset>>)
               -> Result<String> {
    let mut p = p.to_owned();

    let time_vars = Regex::new(":(year|month|i_month|day|i_day|short_year|hour|minute|second)")
        .unwrap();
    if time_vars.is_match(&p) {
        let date =
            try!(date.ok_or(format!("Can not format file path without a valid date ({:?})", p)));

        p = p.replace(":year", &date.year().to_string());
        p = p.replace(":month", &format!("{:02}", &date.month()));
        p = p.replace(":i_month", &date.month().to_string());
        p = p.replace(":day", &format!("{:02}", &date.day()));
        p = p.replace(":i_day", &date.day().to_string());
        p = p.replace(":hour", &format!("{:02}", &date.hour()));
        p = p.replace(":minute", &format!("{:02}", &date.minute()));
        p = p.replace(":second", &format!("{:02}", &date.second()));
    }

    for (key, val) in attributes {
        p = match *val {
            Value::Str(ref v) => p.replace(&(String::from(":") + key), v),
            Value::Num(ref v) => p.replace(&(String::from(":") + key), &v.to_string()),
            _ => p,
        }
    }

    // TODO if title is present inject title slug

    let mut path = Path::new(&p);

    // remove the root prefix (leading slash on unix systems)
    if path.has_root() {
        let mut components = path.components();
        components.next();
        path = components.as_path();
    }

    let mut path_buf = path.to_path_buf();

    // explode the url if no extension was specified
    if path_buf.extension().is_none() {
        path_buf.push("index.html")
    }

    Ok(path_buf.to_string_lossy().into_owned())
}

impl Document {
    pub fn new(path: String,
               attributes: HashMap<String, Value>,
               content: String,
               layout: Option<String>,
               is_post: bool,
               date: Option<DateTime<FixedOffset>>,
               file_path: String,
               markdown: bool)
               -> Document {
        Document {
            path: path,
            attributes: attributes,
            content: content,
            layout: layout,
            is_post: is_post,
            date: date,
            file_path: file_path,
            markdown: markdown,
        }
    }

    pub fn parse(file_path: &Path,
                 source: &Path,
                 mut is_post: bool,
                 post_path: &Option<String>)
                 -> Result<Document> {
        let mut attributes = HashMap::new();
        let mut content = try!(read_file(file_path));

        // if there is front matter, split the file and parse it
        // TODO: make this a regex to support lines of any length
        if content.contains("---") {
            let content2 = content.clone();
            let mut content_splits = content2.splitn(2, "---");

            // above the split are the attributes
            let attribute_string = content_splits.next().unwrap_or("");

            // everything below the split becomes the new content
            content = content_splits.next().unwrap_or("").to_owned();

            let yaml_result = try!(YamlLoader::load_from_str(attribute_string));

            let yaml_attributes = try!(yaml_result[0]
                .as_hash()
                .ok_or(format!("Incorrect front matter format in {:?}", file_path)));

            for (key, value) in yaml_attributes {
                if let Some(v) = yaml_to_liquid(value) {
                    attributes.insert(try!(key.as_str().ok_or(format!("Invalid key {:?}", key)))
                                          .to_owned(),
                                      v);
                }
            }
        }

        if let &mut Value::Bool(val) = attributes.entry("is_post".to_owned())
            .or_insert(Value::Bool(is_post)) {
            is_post = val;
        }

        let date = attributes.get("date")
            .and_then(|d| d.as_str())
            .and_then(|d| DateTime::parse_from_str(d, "%d %B %Y %H:%M:%S %z").ok());

        // if the file has a .md extension we assume it's markdown
        // TODO add a "markdown" flag to yaml front matter
        let markdown = file_path.extension().unwrap_or(OsStr::new("")) == OsStr::new("md");

        let layout = attributes.get("extends").and_then(|l| l.as_str()).map(|x| x.to_owned());

        let new_path = try!(file_path.strip_prefix(source)
            .map_err(|_| "File path not in source".to_owned()));

        let mut path_buf = PathBuf::from(new_path);
        path_buf.set_extension("html");

        // if the user specified a custom path override
        // format it and push it over the original file name
        // TODO replace "date", "pretty", "ordinal" and "none"
        // for Jekyl compatibility
        if let Some(path) = attributes.get("path").and_then(|p| p.as_str()) {
            path_buf = PathBuf::from(try!(format_path(path, &attributes, &date)));
        } else if is_post {
            // check if there is a global setting for post paths
            if let &Some(ref path) = post_path {
                path_buf = PathBuf::from(try!(format_path(path, &attributes, &date)));
            }
        };

        let path = try!(path_buf.to_str()
            .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path_buf)));

        // Swap back slashes to forward slashes to ensure the URL's are valid on Windows
        attributes.insert("path".to_owned(), Value::Str(path.replace("\\", "/")));

        Ok(Document::new(path.to_owned(),
                         attributes,
                         content,
                         layout,
                         is_post,
                         date,
                         file_path.to_string_lossy().into_owned(),
                         markdown))
    }

    /// Metadata for generating RSS feeds
    pub fn to_rss(&self, root_url: &str) -> rss::Item {
        rss::Item {
            title: self.attributes.get("title").and_then(|s| s.as_str()).map(|s| s.to_owned()),
            // Swap back slashes to forward slashes to ensure the URL's are valid on Windows
            link: Some(root_url.to_owned() + &self.path.replace("\\", "/")),
            pub_date: self.date.map(|date| date.to_rfc2822()),
            description: self.attributes
                .get("description")
                .and_then(|s| s.as_str())
                .map(|s| s.to_owned()),
            ..Default::default()
        }
    }

    pub fn as_html(&self,
                   source: &Path,
                   post_data: &[Value],
                   layouts_path: &Path)
                   -> Result<String> {
        let options = LiquidOptions { file_system: Some(source.to_owned()), ..Default::default() };
        let template = try!(liquid::parse(&self.content, options));

        let layout = if let Some(ref layout) = self.layout {
            Some(try!(read_file(layouts_path.join(layout)).map_err(|e| {
                format!("Layout {} can not be read (defined in {}): {}",
                        layout,
                        self.file_path,
                        e)
            })))
        } else {
            None
        };

        let mut data = Context::with_values(self.attributes.clone());
        data.set_val("posts", Value::Array(post_data.to_vec()));

        let mut html = try!(template.render(&mut data)).unwrap_or(String::new());

        if self.markdown {
            html = {
                let mut buf = String::new();
                let parser = cmark::Parser::new(&html);
                cmark::html::push_html(&mut buf, parser);
                buf
            };
        }

        let options = LiquidOptions { file_system: Some(source.to_owned()), ..Default::default() };

        let template = if let Some(layout) = layout {
            data.set_val("content", Value::Str(html));

            try!(liquid::parse(&layout, options))
        } else {
            try!(liquid::parse(&html, options))
        };

        Ok(try!(template.render(&mut data)).unwrap_or(String::new()))
    }
}
<|endoftext|>


<commit_before><commit_msg>fixed is_post attribute<commit_after><|endoftext|>


<commit_before><commit_msg>don't allocate megabyte-big hasher on the stack<commit_after>#![cfg(test)]

extern crate alloc_no_stdlib as alloc;
use super::super::alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator, bzero};
use super::encode::{BrotliEncoderCreateInstance};
#[cfg(not(feature="no-stdlib"))]
use std::vec::Vec;
#[cfg(not(feature="no-stdlib"))]
use std::io;

use core::ops;
use super::entropy_encode::{HuffmanTree};
use super::command::{Command};
pub use super::super::{BrotliDecompressStream, BrotliResult, BrotliState, HuffmanCode};

declare_stack_allocator_struct!(MemPool, 4096, stack);

fn oneshot_compress(input: &mut [u8], mut output: &mut [u8]) -> (i32, usize) {
  let mut available_out: usize = output.len();
  let mut stack_u8_buffer = define_allocator_memory_pool!(4096, u8, [0; 22 * 1024], stack);
  let mut stack_u16_buffer = define_allocator_memory_pool!(4096, u16, [0; 2 * 1024], stack);
  let mut stack_i32_buffer = define_allocator_memory_pool!(4096, i32, [0; 2 * 1024], stack);
  let mut stack_u32_buffer = define_allocator_memory_pool!(4096, u32, [0; 2 * 1024], stack);
  let mut stack_mc_buffer = define_allocator_memory_pool!(4096,
                                                          Command,
                                                          [Command::default(); 2 * 1024],
                                                          stack);
  let stack_u8_allocator = MemPool::<u8>::new_allocator(&mut stack_u8_buffer, bzero);
  let stack_u16_allocator = MemPool::<u16>::new_allocator(&mut stack_u16_buffer, bzero);
  let stack_i32_allocator = MemPool::<i32>::new_allocator(&mut stack_i32_buffer, bzero);
  let stack_u32_allocator = MemPool::<u32>::new_allocator(&mut stack_u32_buffer, bzero);
  let stack_mc_allocator = MemPool::<Command>::new_allocator(&mut stack_mc_buffer, bzero);
  let mut s_orig = BrotliEncoderCreateInstance(stack_u8_allocator,
                                               stack_u16_allocator,
                                               stack_i32_allocator,
                                               stack_u32_allocator,
                                               stack_mc_allocator);
  return (0,0);
}

fn oneshot_decompress(mut compressed: &mut [u8], mut output: &mut [u8]) -> (BrotliResult, usize, usize) {
  let mut available_in: usize = compressed.len();
  let mut available_out: usize = output.len();
  let mut stack_u8_buffer = define_allocator_memory_pool!(4096, u8, [0; 100 * 1024], stack);
  let mut stack_u32_buffer = define_allocator_memory_pool!(4096, u32, [0; 12 * 1024], stack);
  let mut stack_hc_buffer = define_allocator_memory_pool!(4096,
                                                          HuffmanCode,
                                                          [HuffmanCode::default(); 18 * 1024],
                                                          stack);

  let stack_u8_allocator = MemPool::<u8>::new_allocator(&mut stack_u8_buffer, bzero);
  let stack_u32_allocator = MemPool::<u32>::new_allocator(&mut stack_u32_buffer, bzero);
  let stack_hc_allocator = MemPool::<HuffmanCode>::new_allocator(&mut stack_hc_buffer, bzero);
  let mut input_offset: usize = 0;
  let mut output_offset: usize = 0;
  let mut written: usize = 0;
  let mut brotli_state =
    BrotliState::new(stack_u8_allocator, stack_u32_allocator, stack_hc_allocator);
  let result = BrotliDecompressStream(&mut available_in,
                                      &mut input_offset,
                                      &compressed[..],
                                      &mut available_out,
                                      &mut output_offset,
                                      &mut output,
                                      &mut written,
                                      &mut brotli_state);
  brotli_state.BrotliStateCleanup();
  return (result, input_offset, output_offset);

}

fn oneshot(input: &mut [u8], mut compressed: &mut [u8], mut output: &mut [u8]) -> (BrotliResult, usize, usize) {
  let (success, mut available_in) = oneshot_compress(input, compressed);
  if success == 0 {
      //return (BrotliResult::ResultFailure, 0, 0);
      available_in = compressed.len();
  }
  return oneshot_decompress(&mut compressed[..available_in], output);
}

#[test]
fn test_roundtrip_10x10y() {
  const BUFFER_SIZE: usize = 16384;
  let mut compressed: [u8; 12] = [0x1b, 0x13, 0x00, 0x00, 0xa4, 0xb0, 0xb2, 0xea, 0x81, 0x47, 0x02,
                             0x8a];
  let mut output = [0u8; BUFFER_SIZE];
  let mut input  = [0u8; BUFFER_SIZE];
  let (result, compressed_offset, output_offset) = oneshot(&mut input[..], &mut compressed, &mut output[..]);
  match result {
    BrotliResult::ResultSuccess => {}
    _ => assert!(false),
  }
  let mut i: usize = 0;
  while i < 10 {
    assert_eq!(output[i], 'X' as u8);
    assert_eq!(output[i + 10], 'Y' as u8);
    i += 1;
  }
  assert_eq!(output_offset, 20);
  assert_eq!(compressed_offset, compressed.len());
}

#[test]
fn test_roundtrip_x() {
  const BUFFER_SIZE: usize = 16384;
  let mut compressed: [u8; 5] = [0x0b, 0x00, 0x80, 0x58, 0x03];
  let mut output = [0u8; BUFFER_SIZE];
  let mut input = [0u8; BUFFER_SIZE];
  let (result, compressed_offset, output_offset) = oneshot(&mut input[..], &mut compressed[..], &mut output[..]);
  match result {
    BrotliResult::ResultSuccess => {}
    _ => assert!(false),
  }
  assert_eq!(output[0], 'X' as u8);
  assert_eq!(output_offset, 1);
  assert_eq!(compressed_offset, compressed.len());
}

#[test]
fn test_roundtrip_empty() {
  const BUFFER_SIZE: usize = 16384;
  let mut compressed: [u8; 1] = [0x06];
  let mut output = [0u8; 1];
  let (result, compressed_offset, output_offset) = oneshot(&mut [], &mut compressed[..], &mut output[..]);
  match result {
    BrotliResult::ResultSuccess => {}
    _ => assert!(false),
  }
  assert_eq!(output_offset, 0);
  assert_eq!(compressed_offset, compressed.len());
}
const QF_BUFFER_SIZE: usize = 180 * 1024;
static mut quick_fox_output: [u8; QF_BUFFER_SIZE] = [0u8; QF_BUFFER_SIZE];

#[cfg(not(feature="no-stdlib"))]
struct Buffer {
  data: Vec<u8>,
  read_offset: usize,
}
#[cfg(not(feature="no-stdlib"))]
impl Buffer {
  pub fn new(buf: &[u8]) -> Buffer {
    let mut ret = Buffer {
      data: Vec::<u8>::new(),
      read_offset: 0,
    };
    ret.data.extend(buf);
    return ret;
  }
}
#[cfg(not(feature="no-stdlib"))]
impl io::Read for Buffer {
  fn read(self: &mut Self, buf: &mut [u8]) -> io::Result<usize> {
    let bytes_to_read = ::core::cmp::min(buf.len(), self.data.len() - self.read_offset);
    if bytes_to_read > 0 {
      buf[0..bytes_to_read]
        .clone_from_slice(&self.data[self.read_offset..self.read_offset + bytes_to_read]);
    }
    self.read_offset += bytes_to_read;
    return Ok(bytes_to_read);
  }
}
#[cfg(not(feature="no-stdlib"))]
impl io::Write for Buffer {
  fn write(self: &mut Self, buf: &[u8]) -> io::Result<usize> {
    self.data.extend(buf);
    return Ok(buf.len());
  }
  fn flush(self: &mut Self) -> io::Result<()> {
    return Ok(());
  }
}

<|endoftext|>


<commit_before><commit_msg>It may not work but it has most of the necessary functions and it compiles<commit_after><|endoftext|>


<commit_before><commit_msg>Regression test for rust-lang/rust#53675.<commit_after>// rust-lang/rust#53675: At one point the compiler errored when a test
// named `panic` used the `assert!` macro in expression position.

// compile-pass
// compile-flags: --test

mod in_expression_position {
    #[test]
    fn panic() {
        assert!(true)
    }
}

mod in_statement_position {
    #[test]
    fn panic() {
        assert!(true);
    }
}

mod what_if_we_use_panic_directly_in_expr {
    #[test]
    #[should_panic]
    fn panic() {
        panic!("in expr")
    }
}

mod what_if_we_use_panic_directly_in_stmt {
    #[test]
    #[should_panic]
    fn panic() {
        panic!("in stmt");
    }
}
<|endoftext|>


<commit_before><commit_msg>UPDATE FILE LISTING FUNCTIONALITY DAMMIT<commit_after><|endoftext|>


<commit_before><commit_msg>Change shift operand literal to hex<commit_after><|endoftext|>


<commit_before><commit_msg>an empy message<commit_after><|endoftext|>


<commit_before><commit_msg> removed filter for testing<commit_after><|endoftext|>


<commit_before><commit_msg>empty message<commit_after><|endoftext|>


<commit_before><commit_msg>(refactor) Coerce req/res (does not compile).<commit_after><|endoftext|>


<commit_before><commit_msg>Remove calls to exit() and replace them with error propagation up to main()<commit_after><|endoftext|>


<commit_before><commit_msg>Fix: Make "list" default command of imag-tag<commit_after><|endoftext|>


<commit_before><commit_msg>Added a comment with a sketch of how building should work<commit_after><|endoftext|>


<commit_before><commit_msg>Complete problem 10<commit_after>use math::primes;
pub fn demo(n: u64) {
    println!("{:?}", primes::primes(n));
}
<|endoftext|>


<commit_before><commit_msg>Get Vulkan renderer displaying a triangle on Linux<commit_after><|endoftext|>


<commit_before><commit_msg>Initial repository config structures<commit_after>/// `repository` - configuration data and other persistent data w.r.t. a single repository.

use std::ffi::CString;
use std::path::PathBuf;

#[derive(Clone, Serialize, Deserialize)]
pub struct CephConfig {
    /// The directory in which `ceph.conf` is stored.
    pub conf_dir: PathBuf,

    /// The working RADOS object pool.
    pub pool: CString,

    /// The working RADOS user.
    pub user: CString,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Config {
    pub ceph: CephConfig,
}

#[derive(Clone)]
pub struct Repository {
    config: Config,
}
<|endoftext|>


<commit_before><commit_msg>Token definitions<commit_after>use std::vec::Vec;

pub fn get_token_types<'a>() -> Vec<(&'a str, &'a str)>
{
    // TODO: Read types from rules file
    let mut token_types: Vec<(&str, &str)> = Vec::new();

    token_types.push(("comment.line",       	r"//[^\n|$]*"));
    token_types.push(("comment.block",      	r"/\*([^\*]|(\*[^/]))*\*/"));
    token_types.push(("control",            	r"\.|\(|\)|\{|}|\[|]|;|,"));
    token_types.push(("literal.int",        	r"(0(x|b|o|d))?[:digit:]+"));
    token_types.push(("literal.long",       	r"(0(x|b|o|d))?[:digit:]+[lL]"));
    token_types.push(("literal.float",      	r"(\.(\d+)(f|F)?)|((\d+)\.(\d+)(f|F)?)|((\d+)(f|F))"));
    token_types.push(("literal.double",     	r"[:digit:]*\.[:digit:]+"));
    token_types.push(("literal.char",       	r"'[a-z A-Z]?'"));
    token_types.push(("literal.string",     	"\"([^\"\n\r]|\\.)*\""));
    token_types.push(("literal.boolean",    	r"true|false"));
    token_types.push(("literal.null",       	r"null"));
    token_types.push(("block.generics.args",	r"<( )*([a-zA-Z],?( )*)+( )*>"));
    token_types.push(("operator.unary",     	r"(\+\+)|(--)"));
    token_types.push(("operator.binary",    	r"((\+|<<|>>|-|/|\*|\||&)=?)|="));
    token_types.push(("operator.comparator",	r">|>=|<|<=|&&|\|\||==|instanceof"));
    token_types.push(("type",               	r"boolean|long|int|byte|short|char|double|float|void"));
    token_types.push(("mod.permission",     	r"public|private|protected"));
    token_types.push(("mod.access",    			r"static"));
    token_types.push(("mod.behaviour", 			r"final|volitile|transient|synchronized|native|abstract|throws"));
    token_types.push(("mod.inheritance",		r"extends|implements"));
    token_types.push(("action",             	r"return|continue|break|throw|new|assert|strictfp"));
    token_types.push(("construct.conditional", 	r"if|else"));
    token_types.push(("construct.handles",		r"try|catch|finally"));
    token_types.push(("construct.switch", 		r"switch|case:|default:"));
    token_types.push(("construct.loop",		 	r"do|while|for"));
    token_types.push(("construct.type", 		r"class|interface|enum"));
    token_types.push(("special.package",      	r"package"));
    token_types.push(("special.reserved",   	r"goto|const"));
    token_types.push(("special.import",     	r"import( +static)?( )+([a-zA-Z\._])+\*?"));
    token_types.push(("identifier",				r"([:alpha:]|[\$_])+([:alnum:]|[\$_])*"));

    token_types
}
<|endoftext|>


<commit_before><commit_msg>add basic std config<commit_after>use ritual::cli;
use ritual::config::{Config, CrateProperties, GlobalConfig};
use ritual_common::cpp_build_config::{CppBuildConfigData, CppLibraryType};
use ritual_common::errors::{FancyUnwrap, Result};
use ritual_common::{target, toml};

pub const CPP_STD_VERSION: &str = "0.0.0";

fn create_config() -> Result<Config> {
    let mut crate_properties = CrateProperties::new("cpp_std", CPP_STD_VERSION);
    let mut custom_fields = toml::value::Table::new();
    let mut package_data = toml::value::Table::new();
    package_data.insert(
        "authors".to_string(),
        toml::Value::Array(vec![toml::Value::String(
            "Pavel Strakhov <ri@idzaaus.org>".to_string(),
        )]),
    );
    package_data.insert(
        "description".to_string(),
        toml::Value::String("Bindings for C++ standard library".into()),
    );
    // TODO: doc url
    //package_data.insert("documentation".to_string(), toml::Value::String(doc_url));
    package_data.insert(
        "repository".to_string(),
        toml::Value::String("https://github.com/rust-qt/ritual".to_string()),
    );
    package_data.insert(
        "license".to_string(),
        toml::Value::String("MIT OR Apache-2.0".to_string()),
    );
    package_data.insert(
        "keywords".to_string(),
        toml::Value::Array(vec![
            toml::Value::String("ffi".to_string()),
            toml::Value::String("ritual".to_string()),
        ]),
    );
    package_data.insert(
        "categories".to_string(),
        toml::Value::Array(vec![toml::Value::String(
            "external-ffi-bindings".to_string(),
        )]),
    );

    custom_fields.insert("package".to_string(), toml::Value::Table(package_data));
    crate_properties.set_custom_fields(custom_fields);

    let mut config = Config::new(crate_properties);
    config.set_cpp_lib_version("11");

    //config.add_include_directive("...");

    config.add_cpp_parser_argument("-std=c++11");
    let mut data = CppBuildConfigData::new();
    data.set_library_type(CppLibraryType::Static);
    config
        .cpp_build_config_mut()
        .add(target::Condition::True, data);
    Ok(config)
}

fn main() {
    let mut config = GlobalConfig::new();
    config.set_all_crate_names(vec!["cpp_std".into()]);
    config.set_create_config_hook(|_crate_name| create_config());

    cli::run_from_args(config).fancy_unwrap();
}
<|endoftext|>


<commit_before><commit_msg>Add problem #104<commit_after>#[link(name = "prob0104", vers = "0.0")];
#[crate_type = "lib"];

extern mod common;
extern mod extra;

use std::u64;
use std::iterator::UnfoldrIterator;
use common::problem::Problem;

pub static problem: Problem<'static> = Problem {
    id: 104,
    answer: "329468",
    solver: solve
};

fn is_pandigit(n: u64) -> bool {
    let mut hist = [false, .. 10];
    let mut cnt = 0;
    let mut itr = n;
    while itr > 0 {
        let (d, r) = itr.div_rem(&10);
        if r == 0 || hist[r] { return false; }
        hist[r] = true;
        itr = d;
        cnt += 1;
    }
    return cnt == 9;
}

pub fn solve() -> ~str {
    let base = u64::from_str("1" + "0".repeat(9)).unwrap();

    let phi = (1.0 + (5.0f64).sqrt()) / 2.0;
    let next_fib_first10 = |st: &mut (u64, uint)| {
        let (n, cnt) = (st.n0(), st.n1());
        let next = match cnt {
            0 => 1,
            1 => 1,
            2 => 2,
            3 => 3,
            4 => 5,
            _ => {
                let mut f = ((n as f64) * phi + 0.5) as u64;
                while f > base * base { f /= 10; }
                f
            }
        };
        *st = (next, cnt + 1);
        let mut curr = n;
        while curr > base { curr /= 10; }
        Some(curr)
    };

    let next_fib_last10 = |st: &mut (u64, u64)| {
        let (n0, n1) = *st;
        let next = (n0 + n1) % base;
        *st = (n1, next);
        Some(n0)
    };

    let first = UnfoldrIterator::new(next_fib_first10, (0, 0));
    let last = UnfoldrIterator::new(next_fib_last10, (0, 1));
    let mut it = first.zip(last).enumerate().filter(|&(_, (f, l))| is_pandigit(f) && is_pandigit(l));
    let (k, _) = it.next().unwrap();
    return k.to_str();
}
<|endoftext|>


<commit_before><commit_msg>Add get_media_preview module declaration<commit_after><|endoftext|>


<commit_before><commit_msg>Make payload size be 50k to compare benchmarks<commit_after><|endoftext|>


<commit_before><commit_msg>Initial tests - Correction<commit_after><|endoftext|>


<commit_before><commit_msg>Remove unnecessary return keyword<commit_after><|endoftext|>


<commit_before><commit_msg>scouting<commit_after><|endoftext|>


<commit_before><commit_msg>Add argument parsing code<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

extern crate getopts;

use getopts::Options;
use std::io::Error;
use std::fs;
use std::env;
use std::process;

fn print_usage(program: &str, opts: Options) {
    let brief = format!("Usage: {} [options]", program);
    println!("fsgen: Generates an HDFS fsimage.\n");
    println!("By using the fsgen tool, you can quickly generate large HDFS");
    println!("fsimages for testing purposes.  fsgen has two main outputs,");
    println!("an fsimage.xml file and a block directory.");
    print!("{}", opts.usage(&brief));
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let mut opts = Options::new();
    let program = args[0].clone();

    opts.optopt("d", "num_datanodes", "set the number of datanodes to generate", "NUM_DATANODES");
    opts.optflag("h", "help", "print this help menu");
    opts.optopt("n", "num_inodes", "set the number of inodes to generate", "NUM_INODES");
    opts.optopt("o", "out", "set the output directory", "NAME");
    opts.optopt("r", "repl", "set the replication factor to use", "REPL_FACTOR");
    opts.optopt("s", "seed", "set the random seed to use", "RAND_SEED");
    let matches = match opts.parse(&args[1..]) {
        Ok(m) => { m }
        Err(f) => { panic!(f.to_string()) }
    };
    if matches.opt_present("h") {
        print_usage(&program, opts);
        return;
    }
    let num_datanodes = match matches.opt_str("d") {
        None => 4 as u64,
        Some(val) => val.parse::<u64>().unwrap(),
    };
    let num_inodes = match matches.opt_str("n") {
        None => 10000 as u64,
        Some(val) => val.parse::<u64>().unwrap(),
    };
    let out_dir = matches.opt_str("o").unwrap_or("".to_owned());
    if out_dir == "" {
        println!("You must specify an output directory with -o.  -h for help.");
        process::exit(1);
    };
    let repl = match matches.opt_str("r") {
        None => 3 as u16,
        Some(val) => val.parse::<u16>().unwrap(),
    };
    let seed = match matches.opt_str("s") {
        None => 0xdeadbeef as u64,
        Some(val) => val.parse::<u64>().unwrap(),
    };
    // The statements here will be executed when the compiled binary is called
    // Print text to the console
    println!("** fsgen: Generating fsimage with num_datanodes={}, num_inodes={}, \
        out_dir={}, repl={}, seed={}.", num_datanodes, num_inodes,
        out_dir, repl, seed);
    match run(&out_dir) {
        Ok(_) => println!("** Done."),
        Err(err) => {
            println!("** ERROR: {:?}", err);
            process::exit(1);
        }
    }
}

fn run(out_dir: &String) -> Result<(), std::io::Error> {
    if fs::metadata(out_dir).is_ok() {
        try!(fs::remove_dir_all(out_dir));
        println!("** deleted existing output directory {}", out_dir)
    }
    return Result::Ok(());
}

//fn writeImageXml() -> Result<(), io::Error> {
//    let mut f = try!(File::create("fsimage.xml"));
//}
<|endoftext|>


<commit_before>/*
Module: fs

File system manipulation
*/

import core::ctypes;
import core::vec;
import core::option;
import os;
import os::getcwd;
import os_fs;

#[abi = "cdecl"]
native mod rustrt {
    fn rust_path_is_dir(path: str::sbuf) -> int;
    fn rust_path_exists(path: str::sbuf) -> int;
}

/*
Function: path_sep

Get the default path separator for the host platform
*/
fn path_sep() -> str { ret str::from_char(os_fs::path_sep); }

// FIXME: This type should probably be constrained
/*
Type: path

A path or fragment of a filesystem path
*/
type path = str;

/*
Function: dirname

Get the directory portion of a path

Returns all of the path up to, but excluding, the final path separator.
The dirname of "/usr/share" will be "/usr", but the dirname of
"/usr/share/" is "/usr/share".

If the path is not prefixed with a directory, then "." is returned.
*/
fn dirname(p: path) -> path {
    let i: int = str::rindex(p, os_fs::path_sep as u8);
    if i == -1 {
        i = str::rindex(p, os_fs::alt_path_sep as u8);
        if i == -1 { ret "."; }
    }
    ret str::substr(p, 0u, i as uint);
}

/*
Function: basename

Get the file name portion of a path

Returns the portion of the path after the final path separator.
The basename of "/usr/share" will be "share". If there are no
path separators in the path then the returned path is identical to
the provided path. If an empty path is provided or the path ends
with a path separator then an empty path is returned.
*/
fn basename(p: path) -> path {
    let i: int = str::rindex(p, os_fs::path_sep as u8);
    if i == -1 {
        i = str::rindex(p, os_fs::alt_path_sep as u8);
        if i == -1 { ret p; }
    }
    let len = str::byte_len(p);
    if i + 1 as uint >= len { ret p; }
    ret str::slice(p, i + 1 as uint, len);
}

// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
/*
Function: connect

Connects to path segments

Given paths `pre` and `post` this function will return a path
that is equal to `post` appended to `pre`, inserting a path separator
between the two as needed.
*/
fn connect(pre: path, post: path) -> path {
    let len = str::byte_len(pre);
    ret if pre[len - 1u] == os_fs::path_sep as u8 {

            // Trailing '/'?
            pre + post
        } else { pre + path_sep() + post };
}

/*
Function: connect_many

Connects a vector of path segments into a single path.

Inserts path separators as needed.
*/
fn connect_many(paths: [path]) : vec::is_not_empty(paths) -> path {
    ret if vec::len(paths) == 1u {
        paths[0]
    } else {
        let rest = vec::slice(paths, 1u, vec::len(paths));
        check vec::is_not_empty(rest);
        connect(paths[0], connect_many(rest))
    }
}

/*
Function: path_is_dir

Indicates whether a path represents a directory.
*/
fn path_is_dir(p: path) -> bool {
    ret str::as_buf(p, {|buf| rustrt::rust_path_is_dir(buf) != 0 });
}

/*
Function: path_exists

Indicates whether a path exists.
*/
fn path_exists(p: path) -> bool {
    ret str::as_buf(p, {|buf| rustrt::rust_path_exists(buf) != 0 });
}

/*
Function: make_dir

Creates a directory at the specified path.
*/
fn make_dir(p: path, mode: ctypes::c_int) -> bool {
    ret mkdir(p, mode);

    #[cfg(target_os = "win32")]
    fn mkdir(_p: path, _mode: ctypes::c_int) -> bool unsafe {
        // FIXME: turn mode into something useful?
        ret str::as_buf(_p, {|buf|
            os::kernel32::CreateDirectoryA(
                buf, unsafe::reinterpret_cast(0))
        });
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn mkdir(_p: path, _mode: ctypes::c_int) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::mkdir(buf, _mode) == 0i32 });
    }
}

/*
Function: list_dir

Lists the contents of a directory.
*/
fn list_dir(p: path) -> [str] {
    let p = p;
    let pl = str::byte_len(p);
    if pl == 0u || p[pl - 1u] as char != os_fs::path_sep { p += path_sep(); }
    let full_paths: [str] = [];
    for filename: str in os_fs::list_dir(p) {
        if !str::eq(filename, ".") {
            if !str::eq(filename, "..") { full_paths += [p + filename]; }
        }
    }
    ret full_paths;
}

/*
Function: remove_dir

Removes a directory at the specified path.
*/
fn remove_dir(p: path) -> bool {
   ret rmdir(p);

    #[cfg(target_os = "win32")]
    fn rmdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::kernel32::RemoveDirectoryA(buf)});
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn rmdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::rmdir(buf) == 0i32 });
    }
}

fn change_dir(p: path) -> bool {
    ret chdir(p);

    #[cfg(target_os = "win32")]
    fn chdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::kernel32::SetCurrentDirectoryA(buf)});
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn chdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::chdir(buf) == 0i32 });
    }
}

/*
Function: path_is_absolute

Indicates whether a path is absolute.

A path is considered absolute if it begins at the filesystem root ("/") or,
on Windows, begins with a drive letter.
*/
fn path_is_absolute(p: path) -> bool { ret os_fs::path_is_absolute(p); }

// FIXME: under Windows, we should prepend the current drive letter to paths
// that start with a slash.
/*
Function: make_absolute

Convert a relative path to an absolute path

If the given path is relative, return it prepended with the current working
directory. If the given path is already an absolute path, return it
as is.
*/
fn make_absolute(p: path) -> path {
    if path_is_absolute(p) { ret p; } else { ret connect(getcwd(), p); }
}

/*
Function: split

Split a path into it's individual components

Splits a given path by path separators and returns a vector containing
each piece of the path. On Windows, if the path is absolute then
the first element of the returned vector will be the drive letter
followed by a colon.
*/
fn split(p: path) -> [path] {
    let split1 = str::split(p, os_fs::path_sep as u8);
    let split2 = [];
    for s in split1 {
        split2 += str::split(s, os_fs::alt_path_sep as u8);
    }
    ret split2;
}

/*
Function: splitext

Split a path into a pair of strings with the first element being the filename
without the extension and the second being either empty or the file extension
including the period. Leading periods in the basename are ignored.  If the
path includes directory components then they are included in the filename part
of the result pair.
*/
fn splitext(p: path) -> (str, str) {
    if str::is_empty(p) { ("", "") }
    else {
        let parts = str::split(p, '.' as u8);
        if vec::len(parts) > 1u {
            let base = str::connect(vec::init(parts), ".");
            let ext = "." + option::get(vec::last(parts));

            fn is_dotfile(base: str) -> bool {
                str::is_empty(base)
                    || str::ends_with(
                        base, str::from_char(os_fs::path_sep))
                    || str::ends_with(
                        base, str::from_char(os_fs::alt_path_sep))
            }

            fn ext_contains_sep(ext: str) -> bool {
                vec::len(split(ext)) > 1u
            }

            fn no_basename(ext: str) -> bool {
                str::ends_with(
                    ext, str::from_char(os_fs::path_sep))
                    || str::ends_with(
                        ext, str::from_char(os_fs::alt_path_sep))
            }

            if is_dotfile(base)
                || ext_contains_sep(ext)
                || no_basename(ext) {
                (p, "")
            } else {
                (base, ext)
            }
        } else {
            (p, "")
        }
    }
}

/*
Function: normalize

Removes extra "." and ".." entries from paths.

Does not follow symbolic links.
*/
fn normalize(p: path) -> path {
    let s = split(p);
    let s = strip_dots(s);
    let s = rollup_doubledots(s);

    let s = if check vec::is_not_empty(s) {
        connect_many(s)
    } else {
        ""
    };
    let s = reabsolute(p, s);
    let s = reterminate(p, s);

    let s = if str::byte_len(s) == 0u {
        "."
    } else {
        s
    };

    ret s;

    fn strip_dots(s: [path]) -> [path] {
        vec::filter_map(s, { |elem|
            if elem == "." {
                option::none
            } else {
                option::some(elem)
            }
        })
    }

    fn rollup_doubledots(s: [path]) -> [path] {
        if vec::is_empty(s) {
            ret [];
        }

        let t = [];
        let i = vec::len(s);
        let skip = 0;
        do {
            i -= 1u;
            if s[i] == ".." {
                skip += 1;
            } else {
                if skip == 0 {
                    t += [s[i]];
                } else {
                    skip -= 1;
                }
            }
        } while i != 0u;
        let t = vec::reversed(t);
        while skip > 0 {
            t += [".."];
            skip -= 1;
        }
        ret t;
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn reabsolute(orig: path, new: path) -> path {
        if path_is_absolute(orig) {
            path_sep() + new
        } else {
            new
        }
    }

    #[cfg(target_os = "win32")]
    fn reabsolute(orig: path, new: path) -> path {
       if path_is_absolute(orig) && orig[0] == os_fs::path_sep as u8 {
           str::from_char(os_fs::path_sep) + new
       } else {
           new
       }
    }

    fn reterminate(orig: path, new: path) -> path {
        let last = orig[str::byte_len(orig) - 1u];
        if last == os_fs::path_sep as u8
            || last == os_fs::path_sep as u8 {
            ret new + path_sep();
        } else {
            ret new;
        }
    }
}

/*
Function: homedir

Returns the path to the user's home directory, if known.

On Unix, returns the value of the "HOME" environment variable if it is set and
not equal to the empty string.

On Windows, returns the value of the "HOME" environment variable if it is set
and not equal to the empty string. Otherwise, returns the value of the
"USERPROFILE" environment variable if it is set and not equal to the empty
string.

Otherwise, homedir returns option::none.
*/
fn homedir() -> option<path> {
    ret alt generic_os::getenv("HOME") {
        some(p) {
    	    if !str::is_empty(p) {
                some(p)
            } else {
	        secondary()
	    }
	}
	none. {
	    secondary()
	}
    };

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn secondary() -> option<path> {
        none
    }

    #[cfg(target_os = "win32")]
    fn secondary() -> option<path> {
        option::maybe(none, generic_os::getenv("USERPROFILE")) {|p|
            if !str::is_empty(p) {
                some(p)
	    } else {
	        none
	    }
        }
    }
}

// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
<commit_msg>std: Untabify<commit_after>/*
Module: fs

File system manipulation
*/

import core::ctypes;
import core::vec;
import core::option;
import os;
import os::getcwd;
import os_fs;

#[abi = "cdecl"]
native mod rustrt {
    fn rust_path_is_dir(path: str::sbuf) -> int;
    fn rust_path_exists(path: str::sbuf) -> int;
}

/*
Function: path_sep

Get the default path separator for the host platform
*/
fn path_sep() -> str { ret str::from_char(os_fs::path_sep); }

// FIXME: This type should probably be constrained
/*
Type: path

A path or fragment of a filesystem path
*/
type path = str;

/*
Function: dirname

Get the directory portion of a path

Returns all of the path up to, but excluding, the final path separator.
The dirname of "/usr/share" will be "/usr", but the dirname of
"/usr/share/" is "/usr/share".

If the path is not prefixed with a directory, then "." is returned.
*/
fn dirname(p: path) -> path {
    let i: int = str::rindex(p, os_fs::path_sep as u8);
    if i == -1 {
        i = str::rindex(p, os_fs::alt_path_sep as u8);
        if i == -1 { ret "."; }
    }
    ret str::substr(p, 0u, i as uint);
}

/*
Function: basename

Get the file name portion of a path

Returns the portion of the path after the final path separator.
The basename of "/usr/share" will be "share". If there are no
path separators in the path then the returned path is identical to
the provided path. If an empty path is provided or the path ends
with a path separator then an empty path is returned.
*/
fn basename(p: path) -> path {
    let i: int = str::rindex(p, os_fs::path_sep as u8);
    if i == -1 {
        i = str::rindex(p, os_fs::alt_path_sep as u8);
        if i == -1 { ret p; }
    }
    let len = str::byte_len(p);
    if i + 1 as uint >= len { ret p; }
    ret str::slice(p, i + 1 as uint, len);
}

// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
/*
Function: connect

Connects to path segments

Given paths `pre` and `post` this function will return a path
that is equal to `post` appended to `pre`, inserting a path separator
between the two as needed.
*/
fn connect(pre: path, post: path) -> path {
    let len = str::byte_len(pre);
    ret if pre[len - 1u] == os_fs::path_sep as u8 {

            // Trailing '/'?
            pre + post
        } else { pre + path_sep() + post };
}

/*
Function: connect_many

Connects a vector of path segments into a single path.

Inserts path separators as needed.
*/
fn connect_many(paths: [path]) : vec::is_not_empty(paths) -> path {
    ret if vec::len(paths) == 1u {
        paths[0]
    } else {
        let rest = vec::slice(paths, 1u, vec::len(paths));
        check vec::is_not_empty(rest);
        connect(paths[0], connect_many(rest))
    }
}

/*
Function: path_is_dir

Indicates whether a path represents a directory.
*/
fn path_is_dir(p: path) -> bool {
    ret str::as_buf(p, {|buf| rustrt::rust_path_is_dir(buf) != 0 });
}

/*
Function: path_exists

Indicates whether a path exists.
*/
fn path_exists(p: path) -> bool {
    ret str::as_buf(p, {|buf| rustrt::rust_path_exists(buf) != 0 });
}

/*
Function: make_dir

Creates a directory at the specified path.
*/
fn make_dir(p: path, mode: ctypes::c_int) -> bool {
    ret mkdir(p, mode);

    #[cfg(target_os = "win32")]
    fn mkdir(_p: path, _mode: ctypes::c_int) -> bool unsafe {
        // FIXME: turn mode into something useful?
        ret str::as_buf(_p, {|buf|
            os::kernel32::CreateDirectoryA(
                buf, unsafe::reinterpret_cast(0))
        });
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn mkdir(_p: path, _mode: ctypes::c_int) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::mkdir(buf, _mode) == 0i32 });
    }
}

/*
Function: list_dir

Lists the contents of a directory.
*/
fn list_dir(p: path) -> [str] {
    let p = p;
    let pl = str::byte_len(p);
    if pl == 0u || p[pl - 1u] as char != os_fs::path_sep { p += path_sep(); }
    let full_paths: [str] = [];
    for filename: str in os_fs::list_dir(p) {
        if !str::eq(filename, ".") {
            if !str::eq(filename, "..") { full_paths += [p + filename]; }
        }
    }
    ret full_paths;
}

/*
Function: remove_dir

Removes a directory at the specified path.
*/
fn remove_dir(p: path) -> bool {
   ret rmdir(p);

    #[cfg(target_os = "win32")]
    fn rmdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::kernel32::RemoveDirectoryA(buf)});
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn rmdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::rmdir(buf) == 0i32 });
    }
}

fn change_dir(p: path) -> bool {
    ret chdir(p);

    #[cfg(target_os = "win32")]
    fn chdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::kernel32::SetCurrentDirectoryA(buf)});
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn chdir(_p: path) -> bool {
        ret str::as_buf(_p, {|buf| os::libc::chdir(buf) == 0i32 });
    }
}

/*
Function: path_is_absolute

Indicates whether a path is absolute.

A path is considered absolute if it begins at the filesystem root ("/") or,
on Windows, begins with a drive letter.
*/
fn path_is_absolute(p: path) -> bool { ret os_fs::path_is_absolute(p); }

// FIXME: under Windows, we should prepend the current drive letter to paths
// that start with a slash.
/*
Function: make_absolute

Convert a relative path to an absolute path

If the given path is relative, return it prepended with the current working
directory. If the given path is already an absolute path, return it
as is.
*/
fn make_absolute(p: path) -> path {
    if path_is_absolute(p) { ret p; } else { ret connect(getcwd(), p); }
}

/*
Function: split

Split a path into it's individual components

Splits a given path by path separators and returns a vector containing
each piece of the path. On Windows, if the path is absolute then
the first element of the returned vector will be the drive letter
followed by a colon.
*/
fn split(p: path) -> [path] {
    let split1 = str::split(p, os_fs::path_sep as u8);
    let split2 = [];
    for s in split1 {
        split2 += str::split(s, os_fs::alt_path_sep as u8);
    }
    ret split2;
}

/*
Function: splitext

Split a path into a pair of strings with the first element being the filename
without the extension and the second being either empty or the file extension
including the period. Leading periods in the basename are ignored.  If the
path includes directory components then they are included in the filename part
of the result pair.
*/
fn splitext(p: path) -> (str, str) {
    if str::is_empty(p) { ("", "") }
    else {
        let parts = str::split(p, '.' as u8);
        if vec::len(parts) > 1u {
            let base = str::connect(vec::init(parts), ".");
            let ext = "." + option::get(vec::last(parts));

            fn is_dotfile(base: str) -> bool {
                str::is_empty(base)
                    || str::ends_with(
                        base, str::from_char(os_fs::path_sep))
                    || str::ends_with(
                        base, str::from_char(os_fs::alt_path_sep))
            }

            fn ext_contains_sep(ext: str) -> bool {
                vec::len(split(ext)) > 1u
            }

            fn no_basename(ext: str) -> bool {
                str::ends_with(
                    ext, str::from_char(os_fs::path_sep))
                    || str::ends_with(
                        ext, str::from_char(os_fs::alt_path_sep))
            }

            if is_dotfile(base)
                || ext_contains_sep(ext)
                || no_basename(ext) {
                (p, "")
            } else {
                (base, ext)
            }
        } else {
            (p, "")
        }
    }
}

/*
Function: normalize

Removes extra "." and ".." entries from paths.

Does not follow symbolic links.
*/
fn normalize(p: path) -> path {
    let s = split(p);
    let s = strip_dots(s);
    let s = rollup_doubledots(s);

    let s = if check vec::is_not_empty(s) {
        connect_many(s)
    } else {
        ""
    };
    let s = reabsolute(p, s);
    let s = reterminate(p, s);

    let s = if str::byte_len(s) == 0u {
        "."
    } else {
        s
    };

    ret s;

    fn strip_dots(s: [path]) -> [path] {
        vec::filter_map(s, { |elem|
            if elem == "." {
                option::none
            } else {
                option::some(elem)
            }
        })
    }

    fn rollup_doubledots(s: [path]) -> [path] {
        if vec::is_empty(s) {
            ret [];
        }

        let t = [];
        let i = vec::len(s);
        let skip = 0;
        do {
            i -= 1u;
            if s[i] == ".." {
                skip += 1;
            } else {
                if skip == 0 {
                    t += [s[i]];
                } else {
                    skip -= 1;
                }
            }
        } while i != 0u;
        let t = vec::reversed(t);
        while skip > 0 {
            t += [".."];
            skip -= 1;
        }
        ret t;
    }

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn reabsolute(orig: path, new: path) -> path {
        if path_is_absolute(orig) {
            path_sep() + new
        } else {
            new
        }
    }

    #[cfg(target_os = "win32")]
    fn reabsolute(orig: path, new: path) -> path {
       if path_is_absolute(orig) && orig[0] == os_fs::path_sep as u8 {
           str::from_char(os_fs::path_sep) + new
       } else {
           new
       }
    }

    fn reterminate(orig: path, new: path) -> path {
        let last = orig[str::byte_len(orig) - 1u];
        if last == os_fs::path_sep as u8
            || last == os_fs::path_sep as u8 {
            ret new + path_sep();
        } else {
            ret new;
        }
    }
}

/*
Function: homedir

Returns the path to the user's home directory, if known.

On Unix, returns the value of the "HOME" environment variable if it is set and
not equal to the empty string.

On Windows, returns the value of the "HOME" environment variable if it is set
and not equal to the empty string. Otherwise, returns the value of the
"USERPROFILE" environment variable if it is set and not equal to the empty
string.

Otherwise, homedir returns option::none.
*/
fn homedir() -> option<path> {
    ret alt generic_os::getenv("HOME") {
        some(p) {
            if !str::is_empty(p) {
                some(p)
            } else {
                secondary()
            }
        }
        none. {
            secondary()
        }
    };

    #[cfg(target_os = "linux")]
    #[cfg(target_os = "macos")]
    #[cfg(target_os = "freebsd")]
    fn secondary() -> option<path> {
        none
    }

    #[cfg(target_os = "win32")]
    fn secondary() -> option<path> {
        option::maybe(none, generic_os::getenv("USERPROFILE")) {|p|
            if !str::is_empty(p) {
                some(p)
            } else {
                none
            }
        }
    }
}

// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
<|endoftext|>


<commit_before><commit_msg>Initial commit with an implementation of ngrams<commit_after>
fn main() {
    // let min_n = 1;
    // let max_n: usize = 5;
    // let text = "Estimates of the number of languages in the world vary between 5,000 and 7,000. However, any precise estimate depends on a partly arbitrary distinction between languages and dialects. Natural languages are spoken or signed, but any language can be encoded into secondary media using auditory, visual, or tactile stimuli – for example, in graphic writing, braille, or whistling. This is because human language is modality-independent. Depending on philosophical perspectives regarding the definition of language and meaning, when used as a general concept, 'language' may refer to the cognitive ability to learn and use systems of complex communication, or to describe the set of rules that makes up these systems, or the set of utterances that can be produced from those rules. All languages rely on the process of semiosis to relate signs to particular meanings. Oral and sign languages contain a phonological system that governs how symbols are used to form sequences known as words or morphemes, and a syntactic system that governs how words and morphemes are combined to form phrases and utterances.";
    let text = "Estimates of the number of languages in the world";
    for n in 1..6 {
        for x in ngrams(text, n) {
            println!("{:?}", x);
        }
    }
}

fn ngrams(text: &str, n: usize) -> NGrams {
    NGrams {
        n: n,
        chars: text.chars(),
        last_ngram: String::with_capacity(n*2),
    }
}

struct NGrams<'a> {
    n: usize,
    chars: std::str::Chars<'a>,
    last_ngram: String,
}

impl<'a> Iterator for NGrams<'a> {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        if self.last_ngram.len() < self.n {
            while self.last_ngram.len() < self.n {
                let new_char_option = self.chars.next();
                if new_char_option == None {
                    return None;
                }

                self.last_ngram.push(new_char_option.unwrap())
            }
            return Some(self.last_ngram.clone());
        }

        let new_char_option = self.chars.next();
        if new_char_option == None {
            return None;
        }

        let ngram = self.build_new_ngram(new_char_option.unwrap());
        self.last_ngram = ngram.clone();
        Some(ngram)
    }
}

impl<'a> NGrams<'a> {
    fn build_new_ngram(&mut self, new_char: char) -> String {
        let mut ngram = String::with_capacity(self.n*2);
        let current_chars = self.last_ngram.chars().skip(1);

        for char in current_chars {
            ngram.push(char);
        }
        ngram.push(new_char);

        ngram
    }
}
<|endoftext|>


<commit_before><commit_msg>Make RectangleInt fields public<commit_after><|endoftext|>


<commit_before>// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by the Rust Project Developers

// Copyright (c) 2012-2014 The Rust Project Developers
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// - Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in
//   the documentation and/or other materials provided with the
//   distribution.
//
// - Neither the name of "The Computer Language Benchmarks Game" nor
//   the name of "The Computer Language Shootout Benchmarks" nor the
//   names of its contributors may be used to endorse or promote
//   products derived from this software without specific prior
//   written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

#![feature(macro_rules)]
#![feature(simd)]
#![allow(experimental)]

// ignore-pretty very bad with line comments

use std::io;
use std::os;
use std::simd::f64x2;
use std::sync::{Arc, Future};

const ITER: int = 50;
const LIMIT: f64 = 2.0;
const WORKERS: uint = 16;

#[inline(always)]
fn mandelbrot<W: io::Writer>(w: uint, mut out: W) -> io::IoResult<()> {
    assert!(WORKERS % 2 == 0);

    // Ensure w and h are multiples of 8.
    let w = (w + 7) / 8 * 8;
    let h = w;

    let chunk_size = h / WORKERS;

    // Account for remainders in workload division, e.g. 1000 / 16 = 62.5
    let last_chunk_size = if h % WORKERS != 0 {
        chunk_size + h % WORKERS
    } else {
        chunk_size
    };

    // precalc values
    let inverse_w_doubled = 2.0 / w as f64;
    let inverse_h_doubled = 2.0 / h as f64;
    let v_inverses = f64x2(inverse_w_doubled, inverse_h_doubled);
    let v_consts = f64x2(1.5, 1.0);

    // A lot of this code assumes this (so do other lang benchmarks)
    assert!(w == h);
    let mut precalc_r = Vec::with_capacity(w);
    let mut precalc_i = Vec::with_capacity(h);

    let precalc_futures = Vec::from_fn(WORKERS, |i| {
        Future::spawn(proc () {
            let mut rs = Vec::with_capacity(w / WORKERS);
            let mut is = Vec::with_capacity(w / WORKERS);

            let start = i * chunk_size;
            let end = if i == (WORKERS - 1) {
                start + last_chunk_size
            } else {
                (i + 1) * chunk_size
            };

            // This assumes w == h
            for x in range(start, end) {
                let xf = x as f64;
                let xy = f64x2(xf, xf);

                let f64x2(r, i) = xy * v_inverses - v_consts;
                rs.push(r);
                is.push(i);
            }

            (rs, is)
        })
    });

    for res in precalc_futures.into_iter() {
        let (rs, is) = res.unwrap();
        precalc_r.extend(rs.into_iter());
        precalc_i.extend(is.into_iter());
    }

    assert_eq!(precalc_r.len(), w);
    assert_eq!(precalc_i.len(), h);

    let arc_init_r = Arc::new(precalc_r);
    let arc_init_i = Arc::new(precalc_i);

    let data = Vec::from_fn(WORKERS, |i| {
        let vec_init_r = arc_init_r.clone();
        let vec_init_i = arc_init_i.clone();

        Future::spawn(proc () {
            let mut res: Vec<u8> = Vec::with_capacity((chunk_size * w) / 8);
            let init_r_slice = vec_init_r.as_slice();
            for &init_i in vec_init_i.slice(i * chunk_size, (i + 1) * chunk_size).iter() {
                write_line(init_i, init_r_slice, &mut res);
            }

            res
        })
    });

    try!(writeln!(&mut out as &mut Writer, "P4\n{} {}", w, h));
    for res in data.into_iter() {
        try!(out.write(res.unwrap().as_slice()));
    }
    out.flush()
}

fn write_line(init_i: f64, vec_init_r: &[f64], res: &mut Vec<u8>) {
    let v_init_i : f64x2 = f64x2(init_i, init_i);
    let v_2 : f64x2 = f64x2(2.0, 2.0);
    const LIMIT_SQUARED: f64 = LIMIT * LIMIT;

    for chunk_init_r in vec_init_r.chunks(8) {
        let mut cur_byte = 0xff;
        let mut i = 0;

        while i < 8 {
            let v_init_r = f64x2(chunk_init_r[i], chunk_init_r[i + 1]);
            let mut cur_r = v_init_r;
            let mut cur_i = v_init_i;
            let mut r_sq = v_init_r * v_init_r;
            let mut i_sq = v_init_i * v_init_i;

            let mut b = 0;
            for _ in range(0, ITER) {
                let r = cur_r;
                let i = cur_i;

                cur_i = v_2 * r * i + v_init_i;
                cur_r = r_sq - i_sq + v_init_r;

                let f64x2(bit1, bit2) = r_sq + i_sq;

                if bit1 > LIMIT_SQUARED {
                    b |= 2;
                    if b == 3 { break; }
                }

                if bit2 > LIMIT_SQUARED {
                    b |= 1;
                    if b == 3 { break; }
                }

                r_sq = cur_r * cur_r;
                i_sq = cur_i * cur_i;
            }

            cur_byte = (cur_byte << 2) + b;
            i += 2;
        }

        res.push(cur_byte^-1);
    }
}

fn main() {
    let args = os::args();
    let args = args.as_slice();
    let res = if args.len() < 2 {
        println!("Test mode: do not dump the image because it's not utf8, \
                  which interferes with the test runner.");
        mandelbrot(1000, io::util::NullWriter)
    } else {
        mandelbrot(from_str(args[1].as_slice()).unwrap(), io::stdout())
    };
    res.unwrap();
}
<commit_msg>fix shootout-mandelbrot to make it pass the shootout test<commit_after>// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by the Rust Project Developers

// Copyright (c) 2012-2014 The Rust Project Developers
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// - Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in
//   the documentation and/or other materials provided with the
//   distribution.
//
// - Neither the name of "The Computer Language Benchmarks Game" nor
//   the name of "The Computer Language Shootout Benchmarks" nor the
//   names of its contributors may be used to endorse or promote
//   products derived from this software without specific prior
//   written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

#![feature(macro_rules)]
#![feature(simd)]
#![allow(experimental)]

// ignore-pretty very bad with line comments

use std::io;
use std::os;
use std::simd::f64x2;
use std::sync::{Arc, Future};

const ITER: int = 50;
const LIMIT: f64 = 2.0;
const WORKERS: uint = 16;

#[inline(always)]
fn mandelbrot<W: io::Writer>(w: uint, mut out: W) -> io::IoResult<()> {
    assert!(WORKERS % 2 == 0);

    // Ensure w and h are multiples of 8.
    let w = (w + 7) / 8 * 8;
    let h = w;

    let chunk_size = h / WORKERS;

    // Account for remainders in workload division, e.g. 1000 / 16 = 62.5
    let last_chunk_size = if h % WORKERS != 0 {
        chunk_size + h % WORKERS
    } else {
        chunk_size
    };

    // precalc values
    let inverse_w_doubled = 2.0 / w as f64;
    let inverse_h_doubled = 2.0 / h as f64;
    let v_inverses = f64x2(inverse_w_doubled, inverse_h_doubled);
    let v_consts = f64x2(1.5, 1.0);

    // A lot of this code assumes this (so do other lang benchmarks)
    assert!(w == h);
    let mut precalc_r = Vec::with_capacity(w);
    let mut precalc_i = Vec::with_capacity(h);

    let precalc_futures = Vec::from_fn(WORKERS, |i| {
        Future::spawn(proc () {
            let mut rs = Vec::with_capacity(w / WORKERS);
            let mut is = Vec::with_capacity(w / WORKERS);

            let start = i * chunk_size;
            let end = if i == (WORKERS - 1) {
                start + last_chunk_size
            } else {
                (i + 1) * chunk_size
            };

            // This assumes w == h
            for x in range(start, end) {
                let xf = x as f64;
                let xy = f64x2(xf, xf);

                let f64x2(r, i) = xy * v_inverses - v_consts;
                rs.push(r);
                is.push(i);
            }

            (rs, is)
        })
    });

    for res in precalc_futures.into_iter() {
        let (rs, is) = res.unwrap();
        precalc_r.extend(rs.into_iter());
        precalc_i.extend(is.into_iter());
    }

    assert_eq!(precalc_r.len(), w);
    assert_eq!(precalc_i.len(), h);

    let arc_init_r = Arc::new(precalc_r);
    let arc_init_i = Arc::new(precalc_i);

    let data = Vec::from_fn(WORKERS, |i| {
        let vec_init_r = arc_init_r.clone();
        let vec_init_i = arc_init_i.clone();

        Future::spawn(proc () {
            let mut res: Vec<u8> = Vec::with_capacity((chunk_size * w) / 8);
            let init_r_slice = vec_init_r.as_slice();

            let start = i * chunk_size;
            let end = if i == (WORKERS - 1) {
                start + last_chunk_size
            } else {
                (i + 1) * chunk_size
            };

            for &init_i in vec_init_i.slice(start, end).iter() {
                write_line(init_i, init_r_slice, &mut res);
            }

            res
        })
    });

    try!(writeln!(&mut out as &mut Writer, "P4\n{} {}", w, h));
    for res in data.into_iter() {
        try!(out.write(res.unwrap().as_slice()));
    }
    out.flush()
}

fn write_line(init_i: f64, vec_init_r: &[f64], res: &mut Vec<u8>) {
    let v_init_i : f64x2 = f64x2(init_i, init_i);
    let v_2 : f64x2 = f64x2(2.0, 2.0);
    const LIMIT_SQUARED: f64 = LIMIT * LIMIT;

    for chunk_init_r in vec_init_r.chunks(8) {
        let mut cur_byte = 0xff;
        let mut i = 0;

        while i < 8 {
            let v_init_r = f64x2(chunk_init_r[i], chunk_init_r[i + 1]);
            let mut cur_r = v_init_r;
            let mut cur_i = v_init_i;
            let mut r_sq = v_init_r * v_init_r;
            let mut i_sq = v_init_i * v_init_i;

            let mut b = 0;
            for _ in range(0, ITER) {
                let r = cur_r;
                let i = cur_i;

                cur_i = v_2 * r * i + v_init_i;
                cur_r = r_sq - i_sq + v_init_r;

                let f64x2(bit1, bit2) = r_sq + i_sq;

                if bit1 > LIMIT_SQUARED {
                    b |= 2;
                    if b == 3 { break; }
                }

                if bit2 > LIMIT_SQUARED {
                    b |= 1;
                    if b == 3 { break; }
                }

                r_sq = cur_r * cur_r;
                i_sq = cur_i * cur_i;
            }

            cur_byte = (cur_byte << 2) + b;
            i += 2;
        }

        res.push(cur_byte^-1);
    }
}

fn main() {
    let args = os::args();
    let args = args.as_slice();
    let res = if args.len() < 2 {
        println!("Test mode: do not dump the image because it's not utf8, \
                  which interferes with the test runner.");
        mandelbrot(1000, io::util::NullWriter)
    } else {
        mandelbrot(from_str(args[1].as_slice()).unwrap(), io::stdout())
    };
    res.unwrap();
}
<|endoftext|>


<commit_before><commit_msg>bubble sort<commit_after><|endoftext|>


<commit_before><commit_msg>Account for min/max-width in outer intrinsic sizing<commit_after><|endoftext|>


<commit_before><commit_msg>Fix new::git_default_branch with different default<commit_after><|endoftext|>


<commit_before>use std::env;
use std::fmt;
use std::fs::{self, PathExt};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process;
use std::str::FromStr;
use std::sync::atomic;

use csv;

use Csv;

static XSV_INTEGRATION_TEST_DIR: &'static str = "xit";

static NEXT_ID: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;

pub struct Workdir {
    root: PathBuf,
    dir: PathBuf,
    flexible: bool,
}

impl Workdir {
    pub fn new(name: &str) -> Workdir {
        let id = NEXT_ID.fetch_add(1, atomic::Ordering::SeqCst);
        let root = env::current_exe().unwrap()
                       .parent()
                       .expect("executable's directory")
                       .to_path_buf();
        let dir = root.join(XSV_INTEGRATION_TEST_DIR)
                      .join(name)
                      .join(&format!("test-{}", id));

        // I don't get why this is necessary, but Travis seems to need it?
        // let md = fs::metadata(&dir);
        // if fs::metadata(&dir).map(|md| md.is_dir()).unwrap_or(false) {
            // if let Err(err) = fs::remove_dir_all(&dir) {
                // panic!("Could not remove directory '{:?}': {}", dir, err);
            // }
        // }
        // if fs::metadata(&dir).map(|md| md.is_file()).unwrap_or(false) {
            // if let Err(err) = fs::remove_file(&dir) {
                // panic!("Could not remove file '{:?}': {}", dir, err);
            // }
        // }
        if let Err(err) = fs::create_dir_all(&dir) {
            panic!("Could not create '{:?}': {}", dir, err);
        }
        Workdir { root: root, dir: dir, flexible: false }
    }

    pub fn flexible(mut self, yes: bool) -> Workdir {
        self.flexible = yes;
        self
    }

    pub fn create<T: Csv>(&self, name: &str, rows: T) {
        let mut wtr = match csv::Writer::from_file(&self.path(name)) {
            Ok(wtr) => wtr.flexible(self.flexible),
            Err(err) => panic!("Could not open '{:?}': {}",
                                self.path(name), err),
        };
        for row in rows.to_vecs().into_iter() {
            wtr.write(row.iter()).unwrap();
        }
        wtr.flush().unwrap();
    }

    pub fn create_indexed<T: Csv>(&self, name: &str, rows: T) {
        self.create(name, rows);

        let mut cmd = self.command("index");
        cmd.arg(name);
        self.run(&mut cmd);
    }

    pub fn read_stdout<T: Csv>(&self, cmd: &mut process::Command) -> T {
        let mut rdr = csv::Reader::from_string(self.stdout::<String>(cmd))
                                  .has_headers(false);
        Csv::from_vecs(rdr.records().collect::<Result<_, _>>().unwrap())
    }

    pub fn command(&self, sub_command: &str) -> process::Command {
        let mut cmd = process::Command::new(&self.xsv_bin());
        cmd.current_dir(&self.dir).arg(sub_command);
        cmd
    }

    pub fn output(&self, cmd: &mut process::Command) -> process::Output {
        debug!("[{}]: {:?}", self.dir.display(), cmd);
        let o = cmd.output().unwrap();
        if !o.status.success() {
            panic!("\n\n===== {:?} =====\n\
                    command failed but expected success!\
                    \n\ncwd: {}\
                    \n\nstatus: {}\
                    \n\nstdout: {}\n\nstderr: {}\
                    \n\n=====\n",
                   cmd, self.dir.display(), o.status,
                   String::from_utf8_lossy(&o.stdout),
                   String::from_utf8_lossy(&o.stderr))
        }
        o
    }

    pub fn run(&self, cmd: &mut process::Command) {
        self.output(cmd);
    }

    pub fn stdout<T: FromStr>(&self, cmd: &mut process::Command) -> T {
        let o = self.output(cmd);
        let stdout = String::from_utf8_lossy(&o.stdout);
        stdout.trim().parse().ok().expect(
            &format!("Could not convert from string: '{}'", stdout))
    }

    pub fn assert_err(&self, cmd: &mut process::Command) {
        let o = cmd.output().unwrap();
        if o.status.success() {
            panic!("\n\n===== {:?} =====\n\
                    command succeeded but expected failure!\
                    \n\ncwd: {}\
                    \n\nstatus: {}\
                    \n\nstdout: {}\n\nstderr: {}\
                    \n\n=====\n",
                   cmd, self.dir.display(), o.status,
                   String::from_utf8_lossy(&o.stdout),
                   String::from_utf8_lossy(&o.stderr));
        }
    }

    pub fn from_str<T: FromStr>(&self, name: &Path) -> T {
        let mut o = String::new();
        fs::File::open(name).unwrap().read_to_string(&mut o).unwrap();
        o.parse().ok().expect("fromstr")
    }

    pub fn path(&self, name: &str) -> PathBuf {
        self.dir.join(name)
    }

    pub fn xsv_bin(&self) -> PathBuf {
        self.root.join("xsv")
    }
}

impl fmt::Debug for Workdir {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "path={}", self.dir.display())
    }
}
<commit_msg>yet another try<commit_after>use std::env;
use std::fmt;
use std::fs::{self, PathExt};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process;
use std::str::FromStr;
use std::sync::atomic;

use csv;

use Csv;

static XSV_INTEGRATION_TEST_DIR: &'static str = "xit";

static NEXT_ID: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;

pub struct Workdir {
    root: PathBuf,
    dir: PathBuf,
    flexible: bool,
}

impl Workdir {
    pub fn new(name: &str) -> Workdir {
        let id = NEXT_ID.fetch_add(1, atomic::Ordering::SeqCst);
        let root = env::current_exe().unwrap()
                       .parent()
                       .expect("executable's directory")
                       .to_path_buf();
        let dir = root.join(XSV_INTEGRATION_TEST_DIR)
                      .join(name)
                      .join(&format!("test-{}", id));

        // I don't get why this is necessary, but Travis seems to need it?
        let md = fs::metadata(&dir);
        if fs::metadata(&dir).map(|md| md.is_dir()).unwrap_or(false) {
            if let Err(err) = fs::remove_dir_all(&dir) {
                panic!("Could not remove directory '{:?}': {}", dir, err);
            }
        }
        if fs::metadata(&dir).map(|md| md.is_file()).unwrap_or(false) {
            if let Err(err) = fs::remove_file(&dir) {
                panic!("Could not remove file '{:?}': {}", dir, err);
            }
        }
        if let Err(err) = fs::create_dir_all(&dir) {
            panic!("Could not create '{:?}': {}", dir, err);
        }
        Workdir { root: root, dir: dir, flexible: false }
    }

    pub fn flexible(mut self, yes: bool) -> Workdir {
        self.flexible = yes;
        self
    }

    pub fn create<T: Csv>(&self, name: &str, rows: T) {
        let mut wtr = match csv::Writer::from_file(&self.path(name)) {
            Ok(wtr) => wtr.flexible(self.flexible),
            Err(err) => panic!("Could not open '{:?}': {}",
                                self.path(name), err),
        };
        for row in rows.to_vecs().into_iter() {
            wtr.write(row.iter()).unwrap();
        }
        wtr.flush().unwrap();
    }

    pub fn create_indexed<T: Csv>(&self, name: &str, rows: T) {
        self.create(name, rows);

        let mut cmd = self.command("index");
        cmd.arg(name);
        self.run(&mut cmd);
    }

    pub fn read_stdout<T: Csv>(&self, cmd: &mut process::Command) -> T {
        let mut rdr = csv::Reader::from_string(self.stdout::<String>(cmd))
                                  .has_headers(false);
        Csv::from_vecs(rdr.records().collect::<Result<_, _>>().unwrap())
    }

    pub fn command(&self, sub_command: &str) -> process::Command {
        let mut cmd = process::Command::new(&self.xsv_bin());
        cmd.current_dir(&self.dir).arg(sub_command);
        cmd
    }

    pub fn output(&self, cmd: &mut process::Command) -> process::Output {
        debug!("[{}]: {:?}", self.dir.display(), cmd);
        let o = cmd.output().unwrap();
        if !o.status.success() {
            panic!("\n\n===== {:?} =====\n\
                    command failed but expected success!\
                    \n\ncwd: {}\
                    \n\nstatus: {}\
                    \n\nstdout: {}\n\nstderr: {}\
                    \n\n=====\n",
                   cmd, self.dir.display(), o.status,
                   String::from_utf8_lossy(&o.stdout),
                   String::from_utf8_lossy(&o.stderr))
        }
        o
    }

    pub fn run(&self, cmd: &mut process::Command) {
        self.output(cmd);
    }

    pub fn stdout<T: FromStr>(&self, cmd: &mut process::Command) -> T {
        let o = self.output(cmd);
        let stdout = String::from_utf8_lossy(&o.stdout);
        stdout.trim().parse().ok().expect(
            &format!("Could not convert from string: '{}'", stdout))
    }

    pub fn assert_err(&self, cmd: &mut process::Command) {
        let o = cmd.output().unwrap();
        if o.status.success() {
            panic!("\n\n===== {:?} =====\n\
                    command succeeded but expected failure!\
                    \n\ncwd: {}\
                    \n\nstatus: {}\
                    \n\nstdout: {}\n\nstderr: {}\
                    \n\n=====\n",
                   cmd, self.dir.display(), o.status,
                   String::from_utf8_lossy(&o.stdout),
                   String::from_utf8_lossy(&o.stderr));
        }
    }

    pub fn from_str<T: FromStr>(&self, name: &Path) -> T {
        let mut o = String::new();
        fs::File::open(name).unwrap().read_to_string(&mut o).unwrap();
        o.parse().ok().expect("fromstr")
    }

    pub fn path(&self, name: &str) -> PathBuf {
        self.dir.join(name)
    }

    pub fn xsv_bin(&self) -> PathBuf {
        self.root.join("xsv")
    }
}

impl fmt::Debug for Workdir {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "path={}", self.dir.display())
    }
}
<|endoftext|>


<commit_before><commit_msg>Add rayon benchmarks<commit_after>
#![feature(test)]

extern crate test;
use test::Bencher;

#[macro_use(s)]
extern crate ndarray;
extern crate ndarray_parallel;
use ndarray::prelude::*;
use ndarray_parallel::prelude::*;

const EXP_N: usize = 128;

#[bench]
fn map_exp_regular(bench: &mut Bencher)
{
    let mut a = Array2::<f64>::zeros((EXP_N, EXP_N));
    a.swap_axes(0, 1);
    bench.iter(|| {
        a.mapv_inplace(|x| x.exp());
    });
}

#[bench]
fn rayon_exp_regular(bench: &mut Bencher)
{
    let mut a = Array2::<f64>::zeros((EXP_N, EXP_N));
    a.swap_axes(0, 1);
    bench.iter(|| {
        a.view_mut().into_par_iter().for_each(|x| *x = x.exp());
    });
}

const FASTEXP: usize = 900;

#[inline]
fn fastexp(x: f64) -> f64 {
    let x = 1. + x/1024.;
    x.powi(1024)
}

#[bench]
fn map_fastexp_regular(bench: &mut Bencher)
{
    let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
    let mut a = a.slice_mut(s![.., ..-1]);
    bench.iter(|| {
        a.mapv_inplace(|x| fastexp(x))
    });
}

#[bench]
fn rayon_fastexp_regular(bench: &mut Bencher)
{
    let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
    let mut a = a.slice_mut(s![.., ..-1]);
    bench.iter(|| {
        a.view_mut().into_par_iter().for_each(|x| *x = fastexp(*x));
    });
}
<|endoftext|>