faster_path 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa3f9dba549d669279573d0241b6d2c682f79662
4
- data.tar.gz: aa054bab991c3002071a7ec48d73088294f2b01d
3
+ metadata.gz: b2f9feadf34476752439130f2871285f20bd67d5
4
+ data.tar.gz: 184c00898cdca31bc822bf9c0023ca24f729c839
5
5
  SHA512:
6
- metadata.gz: ea433ac9b113962f898e229af0a466f0b6c37136fcc709ed9ff0c07659c5515537c3747fffd518a88051c919c08757aada3b1b47f714af42aee15e85eccd9d6a
7
- data.tar.gz: e813af21a48a83b4b278663a2e2ef4c7ce9874e682651ce3ef529165c923a4a84830c53341d9725757573e75e63ff0e6f1c921d5382d8ed67bd957fbcaf4d7a7
6
+ metadata.gz: 963c1a8874494f241daeb36c791c0136107be377df03739276b1e1c3b354a352616972c72ac137192cade9d8168ec5f863d1aaeb2c7974a949887f018652ce53
7
+ data.tar.gz: f66e104da591aa1b690eedbc5dca48eb636c0cdb27d5d150b3425d67e6e6c568932cf0474fd80915fae6d477715af85337d54a93065561db9fb03d404d0910ee
data/Cargo.toml CHANGED
@@ -14,4 +14,3 @@ crate-type = ["dylib"]
14
14
 
15
15
  [dependencies]
16
16
  libc = "0.2.11"
17
- #regex = "0.1"
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  [![Build Status](https://travis-ci.org/danielpclark/faster_path.svg?branch=master)](https://travis-ci.org/danielpclark/faster_path)
4
4
  [![Tweet This](https://raw.githubusercontent.com/danielpclark/faster_path/master/assets/tweet.png)](https://twitter.com/share?url=https%3A%2F%2Fgithub.com%2Fdanielpclark%2Ffaster_path&via=6ftdan&hashtags=Ruby&text=You%20could%20save%2015%25%20or%20more%20on%20website%20load%20time%20by%20switching%20to%20the%20FasterPath%20gem.)
5
5
 
6
+ _Build is currently failing is for the monkey-patch tests only._
7
+
6
8
  #### As of gem version 0.0.9 this shaves off 66% of my Rails applications page load time.
7
9
 
8
10
  The primary **GOAL** of this project is to improve performance in the most heavily used areas of Ruby as
@@ -85,6 +87,7 @@ Current methods implemented:
85
87
  |FasterPath Rust Implementation|Ruby 2.3.1 Implementation|Performance Improvement|
86
88
  |---|---|:---:|
87
89
  | `FasterPath.absolute?` | `Pathname#absolute?` | 1234.6% |
90
+ | `FasterPath.basename` | `File.basename` | 31.3% |
88
91
  | `FasterPath.chop_basename` | `Pathname#chop_basename` | 46.7% |
89
92
  | `FasterPath.relative?` | `Pathname#relative?` | 1262.3% |
90
93
  | `FasterPath.blank?` | | |
@@ -3,8 +3,8 @@ module FasterPath
3
3
  ::File.class_eval do
4
4
  def basename(pth)
5
5
  FasterPath.basename(pth)
6
- end if FasterPath.respond_to? :basename
7
- end unless true # No need to open class when we're not using it yet
6
+ end
7
+ end
8
8
 
9
9
  ::Pathname.class_eval do
10
10
  def absolute?
@@ -3,9 +3,9 @@ module FasterPath
3
3
  refine File do
4
4
  def basename(pth)
5
5
  FasterPath.basename(pth)
6
- end if FasterPath.respond_to? :basename
6
+ end
7
7
  end
8
- end unless true # No need to open class when we're not using it yet
8
+ end
9
9
 
10
10
  module RefinePathname
11
11
  refine Pathname do
@@ -1,3 +1,3 @@
1
1
  module FasterPath
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/faster_path.rb CHANGED
@@ -28,7 +28,7 @@ module FasterPath
28
28
 
29
29
  def self.basename(pth, ext="")
30
30
  Rust.basename(pth, ext)
31
- end unless true # WAY_TOO_SLOW! 5600X slower
31
+ end
32
32
 
33
33
  # EXAMPLE
34
34
  #def self.one_and_two
@@ -56,7 +56,7 @@ module FasterPath
56
56
  attach_function :is_relative, [ :string ], :bool
57
57
  attach_function :is_blank, [ :string ], :bool
58
58
  attach_function :both_are_blank, [ :string, :string ], :bool
59
- #attach_function :basename, [ :string, :string ], :string
59
+ attach_function :basename, [ :string, :string ], :string
60
60
  attach_function :dirname, [ :string ], :string
61
61
  attach_function :basename_for_chop, [ :string ], :string # decoupling behavior
62
62
  attach_function :dirname_for_chop, [ :string ], :string # decoupling behavior
data/src/basename.rs CHANGED
@@ -1,22 +1,93 @@
1
- #[no_mangle]
2
- pub extern fn basename(string: *const c_char, comp_ext: *const c_char) -> *const c_char {
1
+ #[allow(dead_code)]
2
+ fn rubyish_basename(string: &str, globish_string: &str) -> String {
3
+ let result = if globish_string.chars().next().unwrap_or("muffins".chars().next().unwrap()).eq(&".".chars().next().unwrap()) {
4
+ if globish_string == ".*" {
5
+ let base = string.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("");
6
+ let index = base.rfind(".");
7
+ let (first, _) = base.split_at(index.unwrap());
8
+ first
9
+ } else {
10
+ if &string[string.len()-globish_string.len()..] == globish_string {
11
+ &string[0..string.len()-globish_string.len()]
12
+ } else {
13
+ string
14
+ }.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("")
15
+ }
16
+ } else {
17
+ if &string[string.len()-globish_string.len()..] == globish_string {
18
+ &string[0..string.len()-globish_string.len()]
19
+ } else {
20
+ string
21
+ }.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("")
22
+ };
23
+ result.to_string()
24
+ }
25
+
26
+ #[test]
27
+ fn it_chomps_strings_correctly(){
28
+ assert_eq!(rubyish_basename("","") , "");
29
+ assert_eq!(rubyish_basename("ruby","") , "ruby");
30
+ assert_eq!(rubyish_basename("ruby.rb",".rb") , "ruby");
31
+ assert_eq!(rubyish_basename("ruby.rb",".*") , "ruby");
32
+ assert_eq!(rubyish_basename(".ruby/ruby.rb",".rb") , "ruby");
33
+ assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".rb") , "ruby.rb.swp");
34
+ assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".swp") , "ruby.rb");
35
+ assert_eq!(rubyish_basename(".ruby/ruby.rb.swp",".*") , "ruby.rb");
36
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp","") , "asdf.rb.swp");
37
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb");
38
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", "*") , "asdf.rb.swp");
39
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".") , "asdf.rb.swp");
40
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb");
41
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb") , "asdf.rb.swp");
42
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".swp") , "asdf.rb");
43
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".sw") , "asdf.rb.swp");
44
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".sw*") , "asdf.rb.swp");
45
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.s*") , "asdf.rb.swp");
46
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".s*") , "asdf.rb.swp");
47
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".s**") , "asdf.rb.swp");
48
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".**") , "asdf.rb.swp");
49
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*") , "asdf.rb");
50
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".*.*") , "asdf.rb.swp");
51
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.swp") , "asdf");
52
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".rb.s*p") , "asdf.rb.swp");
53
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*.s*p") , "asdf.rb.swp");
54
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*.sw*p") , "asdf.rb.swp");
55
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", ".r*b.sw*p") , "asdf.rb.swp");
56
+ assert_eq!(rubyish_basename("asdf/asdf.rb.swp", "rb.swp") , "asdf.");
57
+ }
58
+
59
+ //#[no_mangle]
60
+ pub extern fn basename(str_pth: *const c_char, comp_ext: *const c_char) -> *const c_char {
3
61
  let c_str1 = unsafe {
4
- assert!(!string.is_null());
5
- CStr::from_ptr(string)
62
+ assert!(!str_pth.is_null());
63
+ CStr::from_ptr(str_pth)
6
64
  };
7
65
  let c_str2 = unsafe {
8
66
  assert!(!comp_ext.is_null());
9
67
  CStr::from_ptr(comp_ext)
10
68
  };
11
- let r_str = str::from_utf8(c_str1.to_bytes()).unwrap();
12
- let r_str_chomp = str::from_utf8(c_str2.to_bytes()).unwrap();
13
-
14
- let r_str = if !r_str_chomp.is_empty() {
15
- chomp_pathish_regex(r_str, r_str_chomp)
16
- } else { r_str.to_string() };
69
+ let string = str::from_utf8(c_str1.to_bytes()).unwrap();
70
+ let globish_string = str::from_utf8(c_str2.to_bytes()).unwrap();
17
71
 
18
- let part = Path::new(&r_str[..]).file_name().unwrap_or(OsStr::new("")).to_str();
19
-
20
- let output = CString::new(format!("{}", part.unwrap())).unwrap();
21
- output.into_raw()
72
+ let result = if globish_string.chars().next().unwrap_or("muffins".chars().next().unwrap()).eq(&".".chars().next().unwrap()) {
73
+ if globish_string == ".*" {
74
+ let base = string.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("");
75
+ let index = base.rfind(".");
76
+ let (first, _) = base.split_at(index.unwrap());
77
+ first
78
+ } else {
79
+ if &string[string.len()-globish_string.len()..] == globish_string {
80
+ &string[0..string.len()-globish_string.len()]
81
+ } else {
82
+ string
83
+ }.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("")
84
+ }
85
+ } else {
86
+ if &string[string.len()-globish_string.len()..] == globish_string {
87
+ &string[0..string.len()-globish_string.len()]
88
+ } else {
89
+ string
90
+ }.rsplit_terminator(MAIN_SEPARATOR).nth(0).unwrap_or("")
91
+ };
92
+ CString::new(result).unwrap().into_raw()
22
93
  }
data/src/lib.rs CHANGED
@@ -5,7 +5,6 @@
5
5
  // http://opensource.org/licenses/MIT>, at your option. This file may not be
6
6
  // copied, modified, or distributed except according to those terms.
7
7
  extern crate libc;
8
- //extern crate regex;
9
8
 
10
9
  use std::path::{Path,MAIN_SEPARATOR};
11
10
  use libc::c_char;
@@ -13,18 +12,13 @@ use std::ffi::{CStr,CString,OsStr};
13
12
  use std::str;
14
13
  use std::mem;
15
14
 
16
- // Not including regex in current build so chomp_pathish_regex
17
- // and basename will be excluded in build
18
- //include!("tools/chomp_pathish_regex.rs");
19
- //include!("basename.rs");
20
-
21
-
22
15
  include!("ruby_string.rs");
23
16
  include!("ruby_array.rs");
24
17
  include!("is_absolute.rs");
25
18
  include!("is_relative.rs");
26
19
  include!("is_blank.rs");
27
20
  include!("both_are_blank.rs");
21
+ include!("basename.rs");
28
22
  include!("dirname.rs");
29
23
  include!("basename_for_chop.rs");
30
24
  include!("dirname_for_chop.rs");
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faster_path
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
@@ -143,7 +143,6 @@ files:
143
143
  - src/lib.rs
144
144
  - src/ruby_array.rs
145
145
  - src/ruby_string.rs
146
- - src/tools/chomp_pathish_regex.rs
147
146
  homepage: https://github.com/danielpclark/faster_path
148
147
  licenses:
149
148
  - MIT OR Apache-2.0
@@ -1,38 +0,0 @@
1
- fn chomp_pathish_regex(string: &str, globish_string: &str) -> String {
2
- use regex::Regex;
3
- // Keep first match of Regex pattern
4
- // \A(?:(.*)(?:\.{1}\w+)|(.*))\z
5
- let mut reg_builder: String = String::with_capacity(globish_string.len()+30); // need 29 chars + gs
6
- reg_builder.push_str(r"\A(?:(.*)");
7
- if globish_string.len() != 0 {
8
- let mut gs = globish_string.chars();
9
- loop {
10
- match gs.next() {
11
- Some('.') => { reg_builder.push_str(r"(?:\.{1}") },
12
- Some('*') => { reg_builder.push_str(r"\w+") },
13
- Some(x) => { reg_builder.push(x) },
14
- None => {
15
- reg_builder.push_str(r")|(.*)");
16
- break
17
- },
18
- }
19
- }
20
- }
21
- reg_builder.push_str(r")\z");
22
-
23
- let re = Regex::new(&reg_builder[..]).unwrap();
24
- let caps = re.captures(string).unwrap();
25
- caps.at(1).unwrap_or(caps.at(0).unwrap_or("")).to_string()
26
- }
27
-
28
- #[test]
29
- fn it_chomps_strings_correctly(){
30
- assert_eq!(chomp_pathish_regex("",""), "");
31
- assert_eq!(chomp_pathish_regex("ruby",""), "ruby");
32
- assert_eq!(chomp_pathish_regex("ruby.rb",".rb"), "ruby");
33
- assert_eq!(chomp_pathish_regex("ruby.rb",".*"), "ruby");
34
- assert_eq!(chomp_pathish_regex(".ruby/ruby.rb",".rb"), ".ruby/ruby");
35
- assert_eq!(chomp_pathish_regex(".ruby/ruby.rb.swp",".rb"), ".ruby/ruby.rb.swp");
36
- assert_eq!(chomp_pathish_regex(".ruby/ruby.rb.swp",".swp"), ".ruby/ruby.rb");
37
- assert_eq!(chomp_pathish_regex(".ruby/ruby.rb.swp",".*"), ".ruby/ruby.rb");
38
- }