src/pathname.rs in faster_path-0.3.9 vs src/pathname.rs in faster_path-0.3.10

- old
+ new

@@ -7,11 +7,10 @@ use extname; use plus; use relative_path_from; use debug; use helpers::{TryFrom, to_str}; -use pathname_sys::null_byte_check; use path_parsing::{SEP, find_last_non_sep_pos}; use ruru; use ruru::{ RString, @@ -21,14 +20,15 @@ NilClass, Object, Class, VerifiedObject, Exception as Exc, - AnyException as Exception + AnyException as Exception, }; use ruru::types::{Value, ValueType}; -use std::path::{MAIN_SEPARATOR,Path}; +use std::borrow::Cow; +use std::path::{MAIN_SEPARATOR, Path}; use std::fs; type MaybeArray = Result<ruru::Array, ruru::result::Error>; type MaybeString = Result<ruru::RString, ruru::result::Error>; type MaybeBoolean = Result<ruru::Boolean, ruru::result::Error>; @@ -43,43 +43,18 @@ instance.instance_variable_set("@path", RString::new(path).to_any_object()); Pathname { value: instance.value() } } - pub fn new_checked(path: AnyObject) -> Result<Pathname, Exception> { - let pth: Value = if Class::from_existing("String").case_equals(&path) { - path.value() - } else if path.respond_to("to_path") { - path.send("to_path", None).value() - } else { - return Err( - Exception::new( - "ArgumentError", - Some("The type for the argument provided to Pathname.new was invalid.") - ) - ) - }; - - if null_byte_check(path.value()) { - return Err( Exception::new("ArgumentError", Some("pathname contains null byte")) ) - } - - // if it crashes then dup the path string here before assigning to @path - let mut instance = Class::from_existing("Pathname").allocate(); - instance.instance_variable_set("@path", RString::from(pth).to_any_object()); - - Ok(Pathname { value: instance.value() }) - } - pub fn to_any_object(&self) -> AnyObject { AnyObject::from(self.value()) } } impl From<Value> for Pathname { fn from(value: Value) -> Self { - Pathname { value: value } + Pathname { value } } } impl TryFrom<AnyObject> for Pathname { type Error = debug::RubyDebugInfo; @@ -300,32 +275,18 @@ _ => Boolean::new(false) } } pub fn pn_join(args: MaybeArray) -> AnyObject { - let mut args = args.unwrap(); - let path_self = anyobject_to_string(args.shift()).unwrap(); - let mut qty = args.length(); - if qty <= 0 { - return Pathname::new(&path_self).to_any_object(); - } - - let mut result = String::new(); - - loop { - if qty == 0 { break; } - - let item = args.pop(); - result = plus::plus_paths(&anyobject_to_string(item).unwrap(), &result); - if result.as_bytes().get(0) == Some(&SEP) { - return Pathname::new(&result).to_any_object() + let paths = args.unwrap().into_iter().map(|arg| anyobject_to_string(arg).unwrap()).collect::<Vec<_>>(); + let mut paths_iter = paths.iter().rev(); + let mut result = Cow::Borrowed(paths_iter.next().unwrap().as_str()); + for part in paths_iter { + result = plus::plus_paths(&part, result.as_ref()); + if result.as_bytes().first() == Some(&SEP) { + break; } - - qty -= 1; } - - let result = plus::plus_paths(&path_self, &result); - Pathname::new(&result).to_any_object() } // pub fn pn_mkpath(pth: MaybeString) -> NilClass { // NilClass::new()