naturalsorter 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -1
- data/lib/#natcmp.rb# +76 -0
- data/lib/naturalsorter/version.rb +1 -1
- data/lib/versioncmp.rb +12 -0
- metadata +11 -5
data/README.markdown
CHANGED
data/lib/#natcmp.rb#
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# natcmp.rb
|
2
|
+
#
|
3
|
+
# Natural order comparison of two strings
|
4
|
+
# e.g. "my_prog_v1.1.0" < "my_prog_v1.2.0" < "my_prog_v1.10.0"
|
5
|
+
# which does not follow alphabetically
|
6
|
+
#
|
7
|
+
# Based on Martin Pool's "Natural Order String Comparison" originally written in C
|
8
|
+
# http://sourcefrog.net/projects/natsort/
|
9
|
+
#
|
10
|
+
# This implementation is Copyright (C) 2003 by Alan Davies
|
11
|
+
# <cs96and_AT_yahoo_DOT_co_DOT_uk>
|
12
|
+
#
|
13
|
+
# This software is provided 'as-is', without any express or implied
|
14
|
+
# warranty. In no event will the authors be held liable for any damages
|
15
|
+
# arising from the use of this software.
|
16
|
+
#
|
17
|
+
# Permission is granted to anyone to use this software for any purpose,
|
18
|
+
# including commercial applications, and to alter it and redistribute it
|
19
|
+
# freely, subject to the following restrictions:
|
20
|
+
#
|
21
|
+
# 1. The origin of this software must not be misrepresented; you must not
|
22
|
+
# claim that you wrote the original software. If you use this software
|
23
|
+
# in a product, an acknowledgment in the product documentation would be
|
24
|
+
# appreciated but is not required.
|
25
|
+
# 2. Altered source versions must be plainly marked as such, and must not be
|
26
|
+
# misrepresented as being the original software.
|
27
|
+
# 3. This notice may not be removed or altered from any source distribution.
|
28
|
+
|
29
|
+
class Natcmp
|
30
|
+
|
31
|
+
# 'Natural order' comparison of two strings
|
32
|
+
def self.natcmp(str1, str2, caseInsensitive=false)
|
33
|
+
str1, str2 = str1.dup, str2.dup
|
34
|
+
compareExpression = /^(\D*)(\d*)(.*)$/
|
35
|
+
|
36
|
+
if caseInsensitive
|
37
|
+
str1.downcase!
|
38
|
+
str2.downcase!
|
39
|
+
end
|
40
|
+
|
41
|
+
# Remove all whitespace
|
42
|
+
str1.gsub!(/\s*/, '')
|
43
|
+
str2.gsub!(/\s*/, '')
|
44
|
+
|
45
|
+
while (str1.length > 0) or (str2.length > 0) do
|
46
|
+
# Extract non-digits, digits and rest of string
|
47
|
+
str1 =~ compareExpression
|
48
|
+
chars1, num1, str1 = $1.dup, $2.dup, $3.dup
|
49
|
+
|
50
|
+
str2 =~ compareExpression
|
51
|
+
chars2, num2, str2 = $1.dup, $2.dup, $3.dup
|
52
|
+
|
53
|
+
# Compare the non-digits
|
54
|
+
case (chars1 <=> chars2)
|
55
|
+
when 0 # Non-digits are the same, compare the digits...
|
56
|
+
# If either number begins with a zero, then compare alphabetically,
|
57
|
+
# otherwise compare numerically
|
58
|
+
if (num1[0] != 48) and (num2[0] != 48)
|
59
|
+
num1, num2 = num1.to_i, num2.to_i
|
60
|
+
end
|
61
|
+
|
62
|
+
case (num1 <=> num2)
|
63
|
+
when -1 then return -1
|
64
|
+
when 1 then return 1
|
65
|
+
end
|
66
|
+
when -1 then return -1
|
67
|
+
when 1 then return 1
|
68
|
+
end # case
|
69
|
+
|
70
|
+
end # while
|
71
|
+
|
72
|
+
# Strings are naturally equal
|
73
|
+
return 0
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/versioncmp.rb
CHANGED
@@ -28,6 +28,18 @@ class Versioncmp
|
|
28
28
|
# 'Natural version order' comparison of two version strings
|
29
29
|
def self.compare(a, b)
|
30
30
|
|
31
|
+
if (!a.nil? || a.eql?("") ) && b.nil?
|
32
|
+
return 1
|
33
|
+
end
|
34
|
+
|
35
|
+
if (!b.nil? || b.eql?("")) && a.nil?
|
36
|
+
return -1
|
37
|
+
end
|
38
|
+
|
39
|
+
if a.nil? && b.nil?
|
40
|
+
return -1
|
41
|
+
end
|
42
|
+
|
31
43
|
offset1 = 0;
|
32
44
|
offset2 = 0;
|
33
45
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naturalsorter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.6'
|
25
30
|
description: This GEM is sorting Arrays in a natural order. a2 < a10
|
26
31
|
email:
|
27
32
|
- robert.reiz@gmx.com
|
@@ -33,6 +38,7 @@ files:
|
|
33
38
|
- Gemfile
|
34
39
|
- README.markdown
|
35
40
|
- Rakefile
|
41
|
+
- lib/#natcmp.rb#
|
36
42
|
- lib/natcmp.rb
|
37
43
|
- lib/naturalsorter.rb
|
38
44
|
- lib/naturalsorter/version.rb
|
@@ -60,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
66
|
version: '0'
|
61
67
|
requirements: []
|
62
68
|
rubyforge_project: naturalsorter
|
63
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.24
|
64
70
|
signing_key:
|
65
71
|
specification_version: 3
|
66
72
|
summary: Sorting arrays in natural order
|