github.com/vmware/govmomi@v0.37.2/units/size.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package units
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"regexp"
    23  	"strconv"
    24  )
    25  
    26  type ByteSize int64
    27  
    28  const (
    29  	_  = iota
    30  	KB = 1 << (10 * iota)
    31  	MB
    32  	GB
    33  	TB
    34  	PB
    35  	EB
    36  )
    37  
    38  func (b ByteSize) String() string {
    39  	switch {
    40  	case b >= EB:
    41  		return fmt.Sprintf("%.1fEB", float32(b)/EB)
    42  	case b >= PB:
    43  		return fmt.Sprintf("%.1fPB", float32(b)/PB)
    44  	case b >= TB:
    45  		return fmt.Sprintf("%.1fTB", float32(b)/TB)
    46  	case b >= GB:
    47  		return fmt.Sprintf("%.1fGB", float32(b)/GB)
    48  	case b >= MB:
    49  		return fmt.Sprintf("%.1fMB", float32(b)/MB)
    50  	case b >= KB:
    51  		return fmt.Sprintf("%.1fKB", float32(b)/KB)
    52  	}
    53  	return fmt.Sprintf("%dB", b)
    54  }
    55  
    56  type FileSize int64
    57  
    58  func (b FileSize) String() string {
    59  	switch {
    60  	case b >= EB:
    61  		return fmt.Sprintf("%.1fE", float32(b)/EB)
    62  	case b >= PB:
    63  		return fmt.Sprintf("%.1fP", float32(b)/PB)
    64  	case b >= TB:
    65  		return fmt.Sprintf("%.1fT", float32(b)/TB)
    66  	case b >= GB:
    67  		return fmt.Sprintf("%.1fG", float32(b)/GB)
    68  	case b >= MB:
    69  		return fmt.Sprintf("%.1fM", float32(b)/MB)
    70  	case b >= KB:
    71  		return fmt.Sprintf("%.1fK", float32(b)/KB)
    72  	}
    73  	return fmt.Sprintf("%d", b)
    74  }
    75  
    76  var bytesRegexp = regexp.MustCompile(`^(?i)(\d+)([BKMGTPE]?)(ib|b)?$`)
    77  
    78  func (b *ByteSize) Set(s string) error {
    79  	m := bytesRegexp.FindStringSubmatch(s)
    80  	if len(m) == 0 {
    81  		return errors.New("invalid byte value")
    82  	}
    83  
    84  	i, err := strconv.ParseInt(m[1], 10, 64)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	*b = ByteSize(i)
    89  
    90  	switch m[2] {
    91  	case "B", "b", "":
    92  	case "K", "k":
    93  		*b *= ByteSize(KB)
    94  	case "M", "m":
    95  		*b *= ByteSize(MB)
    96  	case "G", "g":
    97  		*b *= ByteSize(GB)
    98  	case "T", "t":
    99  		*b *= ByteSize(TB)
   100  	case "P", "p":
   101  		*b *= ByteSize(PB)
   102  	case "E", "e":
   103  		*b *= ByteSize(EB)
   104  	default:
   105  		return errors.New("invalid byte suffix")
   106  	}
   107  
   108  	return nil
   109  }