Skip to content

Commit ec3d65a

Browse files
authored
Merge pull request #19 from JamesMGreene/fix-alpha-ordering
Improve alphabetical ordering for non-version entries
2 parents 63a23f2 + 489a1a1 commit ec3d65a

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

ext/version_sorter/version_sorter.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ compare_version_number(const struct version_number *a,
7171
}
7272
}
7373

74+
if (max_n == 0) {
75+
return strcmp(a->original, b->original);
76+
}
77+
7478
if (a->size < b->size)
7579
return (b->num_flags & (1ull << n)) ? -1 : 1;
7680

@@ -149,10 +153,7 @@ parse_version_number(const char *string)
149153
if (string[offset] == '-' || isalpha(string[offset])) {
150154
uint16_t start = offset;
151155

152-
if (string[offset] == '-')
153-
offset++;
154-
155-
while (isalpha(string[offset]))
156+
while (string[offset] == '-' || isalpha(string[offset]))
156157
offset++;
157158

158159
version->comp[comp_n].string.offset = start;

test/version_sorter_test.rb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ def test_sorts_versions_correctly
1919
assert_equal sorted_versions, VersionSorter.sort(versions)
2020
end
2121

22+
def test_sorts_versions_correctly_with_lowercase_prefixes
23+
versions = %w( v1.0.9 v1.0.10 v2.0 v3.1.4.2 v1.0.9a )
24+
sorted_versions = %w( v1.0.9a v1.0.9 v1.0.10 v2.0 v3.1.4.2 )
25+
26+
assert_equal sorted_versions, VersionSorter.sort(versions)
27+
end
28+
29+
def test_sorts_versions_correctly_with_uppercase_prefixes
30+
versions = %w( V1.0.9 V1.0.10 V2.0 V3.1.4.2 V1.0.9a )
31+
sorted_versions = %w( V1.0.9a V1.0.9 V1.0.10 V2.0 V3.1.4.2 )
32+
33+
assert_equal sorted_versions, VersionSorter.sort(versions)
34+
end
35+
2236
def test_sorts_versions_like_rubygems
2337
versions = %w(1.0.9.b 1.0.9 1.0.10 2.0 3.1.4.2 1.0.9a 2.0rc2 2.0-rc1)
2438
if !Gem.respond_to?(:rubygems_version) || Gem.rubygems_version < Gem::Version.new('2.1.0')
@@ -38,7 +52,7 @@ def test_returns_same_object
3852
end
3953

4054
def test_reverse_sorts_versions_correctly
41-
versions = %w(1.0.9 1.0.10 2.0 3.1.4.2 1.0.9a)
55+
versions = %w( 1.0.9 1.0.10 2.0 3.1.4.2 1.0.9a )
4256
sorted_versions = %w( 3.1.4.2 2.0 1.0.10 1.0.9 1.0.9a )
4357

4458
assert_equal sorted_versions, VersionSorter.rsort(versions)
@@ -59,18 +73,51 @@ def test_does_not_raise_on_number_overflow
5973

6074
def test_handles_non_version_data
6175
non_versions = [
62-
"", " ", ".", "-", "ćevapčići", "The Quick Brown Fox", '!@#$%^&*()',
76+
" ", ".", "-", "", "ćevapčići", "The Quick Brown Fox", '!@#$%^&*()',
6377
"<--------->", "a12a8a4a22122d01541b62193e9bdad7f5eda552", "1." * 65
6478
]
6579
sorted = [
66-
"<--------->", "-", "The Quick Brown Fox",
67-
"a12a8a4a22122d01541b62193e9bdad7f5eda552", "ćevapčići",
68-
"", " ", ".", '!@#$%^&*()', "1." * 65
80+
"", " ", '!@#$%^&*()', "-", ".", "<--------->",
81+
"The Quick Brown Fox", "a12a8a4a22122d01541b62193e9bdad7f5eda552",
82+
"ćevapčići", "1." * 65
83+
]
84+
85+
assert_equal sorted, VersionSorter.sort(non_versions)
86+
end
87+
88+
def test_sorts_non_version_data_with_trailing_numbers
89+
non_versions = [
90+
"my-patch-2", "my-patch1", "my-patch2", "my-patch", "my-patch-1"
91+
]
92+
sorted = [
93+
"my-patch", "my-patch1", "my-patch2", "my-patch-1", "my-patch-2"
6994
]
7095

7196
assert_equal sorted, VersionSorter.sort(non_versions)
7297
end
7398

99+
# This verifies the sort order of a subset of `test/tags.txt`
100+
def test_yui_style_tags
101+
yui_tags = [
102+
"yui3-571", "yui3-309", "yui3-1405", "yui3-1537", "yui3-440",
103+
"yui3-572", "yui3-1406", "yui3-1538", "yui3-441", "yui3-573"
104+
]
105+
sorted = [
106+
"yui3-309", "yui3-440", "yui3-441", "yui3-571", "yui3-572",
107+
"yui3-573", "yui3-1405", "yui3-1406", "yui3-1537", "yui3-1538"
108+
]
109+
110+
assert_equal sorted, VersionSorter.sort(yui_tags)
111+
end
112+
113+
# This verifies the sort order of the example in `README.md`
114+
def test_readme_examples
115+
readme_versions = ["1.0.9", "2.0", "1.0.10", "1.0.3", "2.0.pre"]
116+
sorted = ["1.0.3", "1.0.9", "1.0.10", "2.0.pre", "2.0"]
117+
118+
assert_equal sorted, VersionSorter.sort(readme_versions)
119+
end
120+
74121
def test_sort_bang
75122
versions = ["10.0", "1.0", "2.0"]
76123
VersionSorter.sort! versions

0 commit comments

Comments
 (0)