Skip to content

Commit

Permalink
fin rewriting to ruru & dropped libc
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpclark committed Sep 12, 2017
1 parent b4fe3c0 commit 4f18c3b
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 410 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ readme = "README.md"

[lib]
name = "faster_path"
crate-type = ["cdylib"]
crate-type = ["dylib"]

[dependencies]
libc = "0.2.30"
ruru = "0.9.3"
array_tool = "0.4.1"
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,29 @@ task :libruby_release do
filename = RbConfig::CONFIG["LIBRUBY_ALIASES"].split(" ").first
libfile = File.join(RbConfig::CONFIG["libdir"], filename)
deps = "target/release/deps"

printf "Copying libruby.so ... "
unless File.exist? "#{deps}/#{filename}"
FileUtils.mkdir_p deps
FileUtils.cp libfile, deps
end
exit 1 unless File.exist? "#{deps}/#{filename}"
puts "libruby.so copied."
end

desc "Add libruby to debug deps"
task :libruby_debug do
filename = RbConfig::CONFIG["LIBRUBY_ALIASES"].split(" ").first
libfile = File.join(RbConfig::CONFIG["libdir"], filename)
deps = "target/debug/deps"

printf "Copying libruby.so ... "
unless File.exist? "#{deps}/#{filename}"
FileUtils.mkdir_p deps
FileUtils.cp libfile, deps
end
exit 1 unless File.exist? "#{deps}/#{filename}"
puts "libruby.so copied."
end

desc "Build Rust extension"
Expand Down
72 changes: 0 additions & 72 deletions benches/path_parsing.rs

This file was deleted.

53 changes: 24 additions & 29 deletions lib/faster_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ module FasterPath

FasterPathname.class_eval do
private :add_trailing_separator
private :basename
private :chop_basename
private :dirname
private :entries
private :extname
private :plus
end

require_relative 'ffi/basename'
require_relative 'ffi/dirname'
require_relative 'ffi/extname'

def self.rust_arch_bits
Rust.rust_arch_bits
end
Expand All @@ -33,64 +33,59 @@ def self.ruby_arch_bits
end

def self.absolute?(pth)
FasterPathname.allocate.send(:absolute?, pth.to_s)
FasterPathname.allocate.send(:absolute?, pth)
end

def self.directory?(pth)
Rust.is_directory(pth)
def self.add_trailing_separator(pth)
FasterPathname.allocate.send(:add_trailing_separator, pth)
end

def self.relative?(pth)
Rust.is_relative(pth)
def self.blank?(str)
FasterPathname.allocate.send(:blank?, str)
end

def self.dirname(pth)
Dirname::Binding.dirname(pth).to_s
def self.basename(pth, ext="")
FasterPathname.allocate.send(:basename, pth, ext)
end

def self.chop_basename(pth)
result = FasterPathname.allocate.send(:chop_basename, pth)
result unless result.empty?
end

def self.blank?(str)
Rust.is_blank(str)
def self.directory?(pth)
FasterPathname.allocate.send(:directory?, pth)
end

def self.basename(pth, ext="")
Basename::Binding.basename(pth, ext).to_s
def self.dirname(pth)
FasterPathname.allocate.send(:dirname, pth)
end

def self.plus(pth, pth2)
FasterPathname.allocate.send(:plus, pth.to_s, pth2.to_s)
def self.entries(pth)
FasterPathname.allocate.send(:entries, pth)
end

def self.add_trailing_separator(pth)
FasterPathname.allocate.send(:add_trailing_separator, pth.to_s)
def self.extname(pth)
FasterPathname.allocate.send(:extname, pth)
end

def self.has_trailing_separator?(pth)
Rust.has_trailing_separator(pth)
FasterPathname.allocate.send(:has_trailing_separator?, pth)
end

def self.extname(pth)
Extname::Binding.extname(pth).to_s
def self.plus(pth, pth2)
FasterPathname.allocate.send(:plus, pth.to_s, pth2.to_s)
end

def self.entries(pth)
FasterPathname.allocate.send(:entries, pth)
def self.relative?(pth)
FasterPathname.allocate.send(:relative?, pth)
end

module Rust
extend FFI::Library
ffi_lib ::FasterPath::FFI_LIBRARY

attach_function :rust_arch_bits, [], :int32
attach_function :is_directory, [ :string ], :bool
attach_function :is_relative, [ :string ], :bool
attach_function :is_blank, [ :string ], :bool
attach_function :both_are_blank, [ :string, :string ], :bool
attach_function :has_trailing_separator, [ :string ], :bool
end
private_constant :Rust
end
21 changes: 0 additions & 21 deletions lib/ffi/basename.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/ffi/dirname.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/ffi/extname.rb

This file was deleted.

50 changes: 16 additions & 34 deletions src/basename.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
use libc::c_char;
use std::ffi::{CStr, CString};
extern crate array_tool;
use path_parsing::extract_last_path_segment;
use self::array_tool::string::Squeeze;

#[no_mangle]
pub extern "C" fn basename(c_pth: *const c_char, c_ext: *const c_char) -> *const c_char {
if c_pth.is_null() || c_ext.is_null() {
return c_pth;
pub fn basename(pth: &str, ext: &str) -> String {
// Known edge case
match &pth.squeeze("/")[..] {
"/" => { return "/".to_string() }
_ => {}
}
let pth = unsafe { CStr::from_ptr(c_pth) }.to_str().unwrap();
let ext = unsafe { CStr::from_ptr(c_ext) }.to_str().unwrap();

let name = rust::basename(pth, ext);

CString::new(name).unwrap().into_raw()
}

pub mod rust {
extern crate array_tool;
use path_parsing::extract_last_path_segment;
use self::array_tool::string::Squeeze;
let mut name = extract_last_path_segment(pth);

pub fn basename(pth: &str, ext: &str) -> String {
// Known edge case
match &pth.squeeze("/")[..] {
"/" => { return "/".to_string() }
_ => {}
if ext == ".*" {
if let Some(dot_i) = name.rfind('.') {
name = &name[0..dot_i];
}

let mut name = extract_last_path_segment(pth);

if ext == ".*" {
if let Some(dot_i) = name.rfind('.') {
name = &name[0..dot_i];
}
} else if name.ends_with(ext) {
name = &name[..name.len() - ext.len()];
};
name.to_string()
}
} else if name.ends_with(ext) {
name = &name[..name.len() - ext.len()];
};
name.to_string()
}

22 changes: 0 additions & 22 deletions src/both_are_blank.rs

This file was deleted.

Loading

0 comments on commit 4f18c3b

Please sign in to comment.