File: String2ByteSlice.go

package info (click to toggle)
golang-github-shenwei356-util 0.0~git20201231.861956c-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 192 kB
  • sloc: makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package stringutil

import (
	"bytes"

	"github.com/shenwei356/natsort"
)

// String2ByteSlice is for sortint of string-[]byte pairs
type String2ByteSlice struct {
	Key   string
	Value []byte
}

// String2ByteSliceList is list of string2ByteSlice
type String2ByteSliceList []String2ByteSlice

// NaturalOrder is the global variable for sorting String2ByteSlice
var NaturalOrder = false

// IgnoreCase for ignoring case when sorting in natural order
var IgnoreCase = false

func (list String2ByteSliceList) Len() int { return len(list) }
func (list String2ByteSliceList) Less(i, j int) bool {
	if NaturalOrder {
		return natsort.Compare(list[i].Key, list[j].Key, IgnoreCase)
	}
	return list[i].Key < list[j].Key
}
func (list String2ByteSliceList) Swap(i, j int) {
	list[i], list[j] = list[j], list[i]
}

// ReversedString2ByteSliceList is reversed String2ByteSliceList
type ReversedString2ByteSliceList struct {
	String2ByteSliceList
}

// Less ...
func (list ReversedString2ByteSliceList) Less(i, j int) bool {
	if NaturalOrder {
		return !natsort.Compare(list.String2ByteSliceList[i].Key, list.String2ByteSliceList[j].Key, IgnoreCase)
	}
	return list.String2ByteSliceList[i].Key > list.String2ByteSliceList[j].Key
}

// ByValue ...
type ByValue struct {
	String2ByteSliceList
}

// Less ...
func (list ByValue) Less(i, j int) bool {
	c := bytes.Compare(list.String2ByteSliceList[i].Value, list.String2ByteSliceList[j].Value)
	if c == -1 {
		return true
	}
	return false
}

// ReversedByValue ...
type ReversedByValue struct {
	String2ByteSliceList
}

// Less ...
func (list ReversedByValue) Less(i, j int) bool {
	c := bytes.Compare(list.String2ByteSliceList[j].Value, list.String2ByteSliceList[i].Value)
	if c == -1 {
		return true
	}
	return false
}