Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
fb87b746
Commit
fb87b746
authored
10 years ago
by
Vincent Petry
Browse files
Options
Downloads
Plain Diff
Merge pull request #14213 from AW-UC/naturalsort_defaultcollator-patch-file-sorting
Update naturalsort_defaultcollator.php. Fixes #13982
parents
7e7e0c51
0066c6f0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/private/naturalsort.php
+13
-0
13 additions, 0 deletions
lib/private/naturalsort.php
lib/private/naturalsort_defaultcollator.php
+7
-3
7 additions, 3 deletions
lib/private/naturalsort_defaultcollator.php
tests/lib/naturalsort.php
+95
-9
95 additions, 9 deletions
tests/lib/naturalsort.php
with
115 additions
and
12 deletions
lib/private/naturalsort.php
+
13
−
0
View file @
fb87b746
...
...
@@ -27,6 +27,19 @@ class NaturalSort {
private
$collator
;
private
$cache
=
array
();
/**
* Instantiate a new \OC\NaturalSort instance.
* @param object $injectedCollator
*/
public
function
__construct
(
$injectedCollator
=
null
)
{
// inject an instance of \Collator('en_US') to force using the php5-intl Collator
// or inject an instance of \OC\NaturalSort_DefaultCollator to force using Owncloud's default collator
if
(
isset
(
$injectedCollator
))
{
$this
->
collator
=
$injectedCollator
;
\OC_Log
::
write
(
'core'
,
'forced use of '
.
get_class
(
$injectedCollator
),
\OC_Log
::
DEBUG
);
}
}
/**
* Split the given string in chunks of numbers and strings
* @param string $t string
...
...
This diff is collapsed.
Click to expand it.
lib/private/naturalsort_defaultcollator.php
+
7
−
3
View file @
fb87b746
...
...
@@ -22,9 +22,13 @@ namespace OC;
class
NaturalSort_DefaultCollator
{
public
function
compare
(
$a
,
$b
)
{
$result
=
strcasecmp
(
$a
,
$b
);
if
(
$result
===
0
)
{
if
(
$a
===
$b
)
{
return
0
;
}
return
(
$a
<
$b
)
?
-
1
:
1
;
return
(
$a
>
$b
)
?
-
1
:
1
;
}
return
(
$result
<
0
)
?
-
1
:
1
;
}
}
This diff is collapsed.
Click to expand it.
tests/lib/naturalsort.php
+
95
−
9
View file @
fb87b746
...
...
@@ -8,27 +8,32 @@
class
Test_NaturalSort
extends
\Test\TestCase
{
public
function
setUp
()
{
parent
::
setUp
();
/**
* @dataProvider naturalSortDataProvider
*/
public
function
testNaturalSortCompare
(
$array
,
$sorted
)
{
if
(
!
class_exists
(
'Collator'
))
{
$this
->
markTestSkipped
(
'The intl module is not available, natural sorting
will
not work as expected.'
);
$this
->
markTestSkipped
(
'The intl module is not available, natural sorting
might
not work as expected.'
);
return
;
}
$comparator
=
\OC\NaturalSort
::
getInstance
();
usort
(
$array
,
array
(
$comparator
,
'compare'
));
$this
->
assertEquals
(
$sorted
,
$array
);
}
/**
* @dataProvider
naturalSort
DataProvider
* @dataProvider
defaultCollator
DataProvider
*/
public
function
test
NaturalSort
Compare
(
$array
,
$sorted
)
public
function
test
DefaultCollator
Compare
(
$array
,
$sorted
)
{
$comparator
=
\OC\NaturalSort
::
getInstance
(
);
$comparator
=
new
\OC\NaturalSort
(
new
\OC\NaturalSort_DefaultCollator
()
);
usort
(
$array
,
array
(
$comparator
,
'compare'
));
$this
->
assertEquals
(
$sorted
,
$array
);
}
/**
* Data provider for natural sort.
* Data provider for natural sort
ing with php5-intl's Collator
.
* Must provide the same result as in core/js/tests/specs/coreSpec.js
* @return array test cases
*/
...
...
@@ -181,4 +186,85 @@ class Test_NaturalSort extends \Test\TestCase {
),
);
}
/**
* Data provider for natural sorting with \OC\NaturalSort_DefaultCollator.
* Must provide the same result as in core/js/tests/specs/coreSpec.js
* @return array test cases
*/
public
function
defaultCollatorDataProvider
()
{
return
array
(
// different casing
array
(
// unsorted
array
(
'aaa'
,
'bbb'
,
'BBB'
,
'AAA'
),
// sorted
array
(
'aaa'
,
'AAA'
,
'bbb'
,
'BBB'
)
),
// numbers
array
(
// unsorted
array
(
'124.txt'
,
'abc1'
,
'123.txt'
,
'abc'
,
'abc2'
,
'def (2).txt'
,
'ghi 10.txt'
,
'abc12'
,
'def.txt'
,
'def (1).txt'
,
'ghi 2.txt'
,
'def (10).txt'
,
'abc10'
,
'def (12).txt'
,
'z'
,
'ghi.txt'
,
'za'
,
'ghi 1.txt'
,
'ghi 12.txt'
,
'zz'
,
'15.txt'
,
'15b.txt'
,
),
// sorted
array
(
'15.txt'
,
'15b.txt'
,
'123.txt'
,
'124.txt'
,
'abc'
,
'abc1'
,
'abc2'
,
'abc10'
,
'abc12'
,
'def.txt'
,
'def (1).txt'
,
'def (2).txt'
,
'def (10).txt'
,
'def (12).txt'
,
'ghi.txt'
,
'ghi 1.txt'
,
'ghi 2.txt'
,
'ghi 10.txt'
,
'ghi 12.txt'
,
'z'
,
'za'
,
'zz'
,
)
),
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment