...

Source file src/github.com/syndtr/goleveldb/leveldb/util.go

Documentation: github.com/syndtr/goleveldb/leveldb

     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", "Ti"}
    24  
    25  func shortenb(bytes int64) 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 int64) 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 maxInt(a, b int) int {
    62  	if a > b {
    63  		return a
    64  	}
    65  	return b
    66  }
    67  
    68  type fdSorter []storage.FileDesc
    69  
    70  func (p fdSorter) Len() int {
    71  	return len(p)
    72  }
    73  
    74  func (p fdSorter) Less(i, j int) bool {
    75  	return p[i].Num < p[j].Num
    76  }
    77  
    78  func (p fdSorter) Swap(i, j int) {
    79  	p[i], p[j] = p[j], p[i]
    80  }
    81  
    82  func sortFds(fds []storage.FileDesc) {
    83  	sort.Sort(fdSorter(fds))
    84  }
    85  
    86  func ensureBuffer(b []byte, n int) []byte {
    87  	if cap(b) < n {
    88  		return make([]byte, n)
    89  	}
    90  	return b[:n]
    91  }
    92  

View as plain text