forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spreadsheet: fix for sheet ordering bug
Need to test this more, may be working by chance... Updates unidoc#154
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package algo | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
func isdigit(c byte) bool { | ||
return c >= '0' && c <= '9' | ||
} | ||
|
||
// NaturalLess compares two strings in a human manner so rId2 sorts less than rId10 | ||
func NaturalLess(lhs, rhs string) bool { | ||
lidx, ridx := 0, 0 | ||
for lidx < len(lhs) && ridx < len(rhs) { | ||
lc := lhs[lidx] | ||
rc := rhs[ridx] | ||
ldigit := isdigit(lc) | ||
rdigit := isdigit(rc) | ||
switch { | ||
// digits sort before characters | ||
case ldigit && !rdigit: | ||
return true | ||
// characters after digits | ||
case !ldigit && rdigit: | ||
return false | ||
// no digits, so compare the characters | ||
case !ldigit && !rdigit: | ||
if lc != rc { | ||
return lc < rc | ||
} | ||
lidx++ | ||
ridx++ | ||
// both digits, so parse and compare | ||
default: | ||
lend := lidx + 1 | ||
rend := ridx + 1 | ||
|
||
for lend < len(lhs) && isdigit(lhs[lend]) { | ||
lend++ | ||
} | ||
for rend < len(rhs) && isdigit(rhs[rend]) { | ||
rend++ | ||
} | ||
lv, _ := strconv.ParseUint(lhs[lidx:lend], 10, 64) | ||
rv, _ := strconv.ParseUint(rhs[lidx:rend], 10, 64) | ||
if lv != rv { | ||
return lv < rv | ||
} | ||
// digits are equal, so keep looking | ||
lidx = lend | ||
ridx = rend | ||
} | ||
} | ||
// fall back to comparing length | ||
return len(lhs) < len(rhs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package algo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"baliance.com/gooxml/algo" | ||
) | ||
|
||
func TestSort(t *testing.T) { | ||
|
||
tests := []struct { | ||
a, b string | ||
}{ | ||
{"rId1", "rId2"}, | ||
{"rId1", "rId10"}, | ||
{"rId2", "rId10"}, | ||
{"rId5", "rId10"}, | ||
{"rId5", "rId15"}, | ||
{"rId5", "rId51"}, | ||
{"rId1a", "rId1b"}, | ||
} | ||
|
||
for _, tc := range tests { | ||
if !algo.NaturalLess(tc.a, tc.b) { | ||
t.Errorf("bad sort, expected %s < %s", tc.a, tc.b) | ||
} else { | ||
// no need to check if it failed the first time | ||
if algo.NaturalLess(tc.b, tc.a) { | ||
t.Errorf("bad sort, expected %s > %s", tc.b, tc.a) | ||
} | ||
} | ||
} | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters