-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from essentialkaos/develop
Version 7.3.0
- Loading branch information
Showing
7 changed files
with
214 additions
and
43 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package jsonutil | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2009-2017 ESSENTIAL KAOS // | ||
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func ExampleEncodeToFile() { | ||
var data = make(map[string]int) | ||
|
||
data["john"] = 100 | ||
data["bob"] = 300 | ||
|
||
err := EncodeToFile("/path/to/file.json", data, 0600) | ||
|
||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
} | ||
} | ||
|
||
func ExampleDecodeFile() { | ||
var data = make(map[string]int) | ||
|
||
err := DecodeFile("/path/to/file.json", data) | ||
|
||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
} | ||
} |
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
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,95 @@ | ||
package sortutil | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2009-2017 ESSENTIAL KAOS // | ||
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import "sort" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
type naturalSlice []string | ||
|
||
func (s naturalSlice) Len() int { return len(s) } | ||
func (s naturalSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s naturalSlice) Less(i, j int) bool { return NaturalLess(s[i], s[j]) } | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// StringsNatural sorts a slice of strings in natural order | ||
// Limitation: only ASCII digits (0-9) are considered. | ||
func StringsNatural(a []string) { | ||
sort.Sort(naturalSlice(a)) | ||
} | ||
|
||
// NaturalLess compares two strings using natural ordering. This means that e.g. | ||
// "abc2" < "abc12" | ||
// This code based on sortorder package created by @fvbommel | ||
func NaturalLess(s1, s2 string) bool { | ||
i1, i2 := 0, 0 | ||
l1, l2 := len(s1), len(s2) | ||
|
||
for i1 < l1 && i2 < l2 { | ||
c1, c2 := s1[i1], s2[i2] | ||
d1, d2 := isDigit(c1), isDigit(c2) | ||
|
||
if d1 != d2 { | ||
return d1 | ||
} else if !d1 { | ||
if c1 != c2 { | ||
return c1 < c2 | ||
} | ||
|
||
i1++ | ||
i2++ | ||
|
||
continue | ||
} | ||
|
||
for i1 < l1 && s1[i1] == '0' { | ||
i1++ | ||
} | ||
|
||
for i2 < l2 && s2[i2] == '0' { | ||
i2++ | ||
} | ||
|
||
n1, n2 := i1, i2 | ||
|
||
for i1 < l1 && isDigit(s1[i1]) { | ||
i1++ | ||
} | ||
|
||
for i2 < l2 && isDigit(s2[i2]) { | ||
i2++ | ||
} | ||
|
||
ln1, ln2 := i1-n1, i2-n2 | ||
|
||
if ln1 != ln2 { | ||
return ln1 < ln2 | ||
} | ||
|
||
nl1, nl2 := s1[n1:i1], s2[n2:i2] | ||
|
||
if nl1 != nl2 { | ||
return nl1 < nl2 | ||
} | ||
} | ||
|
||
return l1 < l2 | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func isDigit(b byte) bool { | ||
switch b { | ||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
Oops, something went wrong.