unnatural 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +40 -0
- data/.rubocop.yml +13 -0
- data/lib/unnatural/fast.rb +4 -0
- data/lib/unnatural/scan.rb +13 -1
- data/lib/unnatural/split.rb +3 -1
- data/lib/unnatural/substitution.rb +4 -1
- data/lib/unnatural/version.rb +1 -1
- data/lib/unnatural.rb +2 -1
- data/unnatural.gemspec +5 -6
- metadata +13 -41
- data/.travis.yml +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d30faac6de21215a8cb100e0621352ee6322e9559929b50c497aaa0c3a949765
|
4
|
+
data.tar.gz: 2faa48bf01a8117b96d150f62b00c8a9213cfb0210607dcefd8ded4bb5bb9ff9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be87f01ba42c62c779f5d77a2e86d252ef92d0e42a84d579f3b5462d051db55f502b84d01fce2ff3948f529e8e078a0a1a667e2d45b1304c55773bab1513c62d
|
7
|
+
data.tar.gz: d10fe9672a0402a334aeb9211c5963657388bd085657ef39f26d3aef5e444e6dcc3ff6f3a054959f874828970bf1f97da3e17f4484f8cc58cada814345dfdd79
|
@@ -0,0 +1,40 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ "master" ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ "master" ]
|
8
|
+
|
9
|
+
permissions:
|
10
|
+
contents: read
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
ruby-version:
|
18
|
+
- '2.1'
|
19
|
+
- '2.2'
|
20
|
+
- '2.3'
|
21
|
+
- '2.4'
|
22
|
+
- '2.5'
|
23
|
+
- '2.6'
|
24
|
+
- '2.7'
|
25
|
+
- '3.0'
|
26
|
+
- '3.1'
|
27
|
+
- jruby-9.1.17.0
|
28
|
+
- jruby-9.2.21.0
|
29
|
+
- jruby-9.3.6.0
|
30
|
+
|
31
|
+
steps:
|
32
|
+
- uses: actions/checkout@v3
|
33
|
+
- name: Set up Ruby
|
34
|
+
# https://github.com/ruby/setup-ruby#versioning
|
35
|
+
uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ${{ matrix.ruby-version }}
|
38
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
39
|
+
- name: Run tests
|
40
|
+
run: bundle exec rake
|
data/.rubocop.yml
ADDED
data/lib/unnatural/fast.rb
CHANGED
@@ -2,7 +2,11 @@ require 'ffi'
|
|
2
2
|
require 'ffi-compiler/loader'
|
3
3
|
|
4
4
|
module Unnatural
|
5
|
+
# Compares strings byte-by-byte. Comparison function implemented in C. Does
|
6
|
+
# not appear to sort unicode strings correctly. Much faster than any of the
|
7
|
+
# pure Ruby options. The default.
|
5
8
|
module Fast
|
9
|
+
# module to hold the C function
|
6
10
|
module Ext
|
7
11
|
extend FFI::Library
|
8
12
|
ffi_lib FFI::Compiler::Loader.find('unnatural_ext')
|
data/lib/unnatural/scan.rb
CHANGED
@@ -1,19 +1,27 @@
|
|
1
1
|
require 'strscan'
|
2
2
|
|
3
3
|
module Unnatural
|
4
|
+
# Compares strings using a `StringScanner`. Pure ruby. Tends to be
|
5
|
+
# outperformed by `Unnatural::Substitution` and `Unnatural::Split` when
|
6
|
+
# sorting short strings via the global sort function, but its comparison
|
7
|
+
# function is the fastest of the pure-ruby algorithms.
|
4
8
|
module Scan
|
5
9
|
def self.sort(enumerable)
|
6
10
|
enumerable.sort { |a, b| compare(a, b) }
|
7
11
|
end
|
8
12
|
|
9
13
|
def self.sort_by(enumerable)
|
10
|
-
raise ArgumentError,
|
14
|
+
raise ArgumentError, 'Block expected but none given' unless block_given?
|
11
15
|
enumerable
|
12
16
|
.map { |e| [(yield e), e] }
|
13
17
|
.sort { |a, b| compare(a, b) }
|
14
18
|
.map { |ary| ary[1] }
|
15
19
|
end
|
16
20
|
|
21
|
+
# rubocop: disable Metrics/AbcSize
|
22
|
+
# rubocop: disable Metrics/CyclomaticComplexity
|
23
|
+
# rubocop: disable Metrics/MethodLength
|
24
|
+
# rubocop: disable Metrics/PerceivedComplexity
|
17
25
|
def self.compare(a_string, b_string)
|
18
26
|
if a_string.is_a?(Array) && b_string.is_a?(Array)
|
19
27
|
a_string = a_string.first
|
@@ -57,5 +65,9 @@ module Unnatural
|
|
57
65
|
return +1 if b.eos?
|
58
66
|
end
|
59
67
|
end
|
68
|
+
# rubocop: enable Metrics/AbcSize
|
69
|
+
# rubocop: enable Metrics/CyclomaticComplexity
|
70
|
+
# rubocop: enable Metrics/MethodLength
|
71
|
+
# rubocop: enable Metrics/PerceivedComplexity
|
60
72
|
end
|
61
73
|
end
|
data/lib/unnatural/split.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module Unnatural
|
2
|
+
# Compares strings by spliting them into arrays of alternating string and
|
3
|
+
# integer values. Pure Ruby. Tends to be outperformed by the others.
|
2
4
|
module Split
|
3
5
|
SPLITTER = /(?<=\d)(?=\D)|(?<=\D)(?=\d)/
|
4
6
|
PRED = ['0'.ord.pred.chr].freeze
|
@@ -8,7 +10,7 @@ module Unnatural
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def self.sort_by(enumerable)
|
11
|
-
raise ArgumentError,
|
13
|
+
raise ArgumentError, 'Block expected but none given' unless block_given?
|
12
14
|
enumerable.sort_by { |s| split(yield s) }
|
13
15
|
end
|
14
16
|
|
@@ -1,4 +1,7 @@
|
|
1
1
|
module Unnatural
|
2
|
+
# Compares strings by zero-padding integer sequences such that all are the
|
3
|
+
# same length. Pure Ruby. Tends to be outperformed by `Unnatural::Scan` on
|
4
|
+
# longer strings. Recommended for sorting short unicode strings.
|
2
5
|
module Substitution
|
3
6
|
def self.sort(enumerable)
|
4
7
|
largest = enumerable.map(&:size).max
|
@@ -6,7 +9,7 @@ module Unnatural
|
|
6
9
|
end
|
7
10
|
|
8
11
|
def self.sort_by(enumerable)
|
9
|
-
raise ArgumentError,
|
12
|
+
raise ArgumentError, 'Block expected but none given' unless block_given?
|
10
13
|
largest = enumerable.map { |e| yield e }.map(&:size).max
|
11
14
|
enumerable.sort_by { |s| substitute((yield s), largest) }
|
12
15
|
end
|
data/lib/unnatural/version.rb
CHANGED
data/lib/unnatural.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'unnatural/version'
|
2
2
|
|
3
|
+
# A natural sort.
|
3
4
|
module Unnatural
|
4
5
|
def self.algorithms
|
5
6
|
[:Substitution, :Split, :Scan, :Fast]
|
@@ -20,7 +21,7 @@ module Unnatural
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def self.sort_by(enumerable)
|
23
|
-
raise ArgumentError,
|
24
|
+
raise ArgumentError, 'Block expected but none given' unless block_given?
|
24
25
|
@algorithm.sort_by(enumerable) { |*a| yield(*a) }
|
25
26
|
end
|
26
27
|
|
data/unnatural.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'unnatural/version'
|
@@ -10,11 +10,12 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['bjmllr@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = 'A natural sort.'
|
13
|
-
spec.description
|
13
|
+
spec.description = 'A natural sort implementation with an emphasis on speed.'
|
14
14
|
spec.homepage = 'https://github.com/bjmllr/unnatural'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
17
|
-
spec.files = `git ls-files -z
|
17
|
+
spec.files = `git ls-files -z`
|
18
|
+
.split("\x0")
|
18
19
|
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
20
|
spec.files += Dir.glob('ext/unnatural/*')
|
20
21
|
spec.require_paths = ['lib']
|
@@ -22,9 +23,7 @@ Gem::Specification.new do |spec|
|
|
22
23
|
spec.extensions = ['ext/unnatural/Rakefile']
|
23
24
|
|
24
25
|
spec.add_dependency 'ffi-compiler', '~> 1.0'
|
25
|
-
spec.add_dependency 'rake', '~> 10.0'
|
26
26
|
|
27
|
-
spec.add_development_dependency 'bundler', '~> 1.12'
|
28
|
-
spec.add_development_dependency 'minitest', '~> 5.0'
|
29
27
|
spec.add_development_dependency 'benchmark-ips'
|
28
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
30
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unnatural
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Miller
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi-compiler
|
@@ -25,33 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bundler
|
28
|
+
name: benchmark-ips
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
|
-
- - "
|
31
|
+
- - ">="
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
33
|
+
version: '0'
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- - "
|
38
|
+
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
40
|
+
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: minitest
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,20 +52,6 @@ dependencies:
|
|
66
52
|
- - "~>"
|
67
53
|
- !ruby/object:Gem::Version
|
68
54
|
version: '5.0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: benchmark-ips
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
55
|
description: A natural sort implementation with an emphasis on speed.
|
84
56
|
email:
|
85
57
|
- bjmllr@gmail.com
|
@@ -88,8 +60,9 @@ extensions:
|
|
88
60
|
- ext/unnatural/Rakefile
|
89
61
|
extra_rdoc_files: []
|
90
62
|
files:
|
63
|
+
- ".github/workflows/ruby.yml"
|
91
64
|
- ".gitignore"
|
92
|
-
- ".
|
65
|
+
- ".rubocop.yml"
|
93
66
|
- CODE_OF_CONDUCT.md
|
94
67
|
- Gemfile
|
95
68
|
- LICENSE.txt
|
@@ -110,7 +83,7 @@ homepage: https://github.com/bjmllr/unnatural
|
|
110
83
|
licenses:
|
111
84
|
- MIT
|
112
85
|
metadata: {}
|
113
|
-
post_install_message:
|
86
|
+
post_install_message:
|
114
87
|
rdoc_options: []
|
115
88
|
require_paths:
|
116
89
|
- lib
|
@@ -125,9 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
98
|
- !ruby/object:Gem::Version
|
126
99
|
version: '0'
|
127
100
|
requirements: []
|
128
|
-
|
129
|
-
|
130
|
-
signing_key:
|
101
|
+
rubygems_version: 3.3.7
|
102
|
+
signing_key:
|
131
103
|
specification_version: 4
|
132
104
|
summary: A natural sort.
|
133
105
|
test_files: []
|