OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / syndtr / goleveldb / leveldb / util.go
1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 package leveldb
8
9 import (
10         "fmt"
11         "sort"
12
13         "github.com/syndtr/goleveldb/leveldb/storage"
14 )
15
16 func shorten(str string) string {
17         if len(str) <= 8 {
18                 return str
19         }
20         return str[:3] + ".." + str[len(str)-3:]
21 }
22
23 var bunits = [...]string{"", "Ki", "Mi", "Gi"}
24
25 func shortenb(bytes int) string {
26         i := 0
27         for ; bytes > 1024 && i < 4; i++ {
28                 bytes /= 1024
29         }
30         return fmt.Sprintf("%d%sB", bytes, bunits[i])
31 }
32
33 func sshortenb(bytes int) string {
34         if bytes == 0 {
35                 return "~"
36         }
37         sign := "+"
38         if bytes < 0 {
39                 sign = "-"
40                 bytes *= -1
41         }
42         i := 0
43         for ; bytes > 1024 && i < 4; i++ {
44                 bytes /= 1024
45         }
46         return fmt.Sprintf("%s%d%sB", sign, bytes, bunits[i])
47 }
48
49 func sint(x int) string {
50         if x == 0 {
51                 return "~"
52         }
53         sign := "+"
54         if x < 0 {
55                 sign = "-"
56                 x *= -1
57         }
58         return fmt.Sprintf("%s%d", sign, x)
59 }
60
61 func minInt(a, b int) int {
62         if a < b {
63                 return a
64         }
65         return b
66 }
67
68 func maxInt(a, b int) int {
69         if a > b {
70                 return a
71         }
72         return b
73 }
74
75 type fdSorter []storage.FileDesc
76
77 func (p fdSorter) Len() int {
78         return len(p)
79 }
80
81 func (p fdSorter) Less(i, j int) bool {
82         return p[i].Num < p[j].Num
83 }
84
85 func (p fdSorter) Swap(i, j int) {
86         p[i], p[j] = p[j], p[i]
87 }
88
89 func sortFds(fds []storage.FileDesc) {
90         sort.Sort(fdSorter(fds))
91 }
92
93 func ensureBuffer(b []byte, n int) []byte {
94         if cap(b) < n {
95                 return make([]byte, n)
96         }
97         return b[:n]
98 }