|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package bytequantity |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "math" |
| 19 | + "regexp" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + /// Examples: 1mb, 1 gb, 1.0tb, 1mib, 2g, 2.001 t |
| 26 | + byteQuantityRegex = `^([0-9]+\.?[0-9]{0,3})[ ]?(mi?b?|gi?b?|ti?b?)?$` |
| 27 | + mib = "MiB" |
| 28 | + gib = "GiB" |
| 29 | + tib = "TiB" |
| 30 | + gbConvert = 1 << 10 |
| 31 | + tbConvert = gbConvert << 10 |
| 32 | + maxGiB = math.MaxUint64 / gbConvert |
| 33 | + maxTiB = math.MaxUint64 / tbConvert |
| 34 | +) |
| 35 | + |
| 36 | +// ByteQuantity is a data type representing a byte quantity |
| 37 | +type ByteQuantity struct { |
| 38 | + Quantity uint64 |
| 39 | +} |
| 40 | + |
| 41 | +// ParseToByteQuantity parses a string representation of a byte quantity to a ByteQuantity type. |
| 42 | +// A unit can be appended such as 16 GiB. If no unit is appended, GiB is assumed. |
| 43 | +func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) { |
| 44 | + bqRegexp := regexp.MustCompile(byteQuantityRegex) |
| 45 | + matches := bqRegexp.FindStringSubmatch(strings.ToLower(byteQuantityStr)) |
| 46 | + if len(matches) < 2 { |
| 47 | + return ByteQuantity{}, fmt.Errorf("%s is not a valid byte quantity", byteQuantityStr) |
| 48 | + } |
| 49 | + |
| 50 | + quantityStr := matches[1] |
| 51 | + unit := gib |
| 52 | + if len(matches) > 2 && matches[2] != "" { |
| 53 | + unit = matches[2] |
| 54 | + } |
| 55 | + quantity := uint64(0) |
| 56 | + switch strings.ToLower(string(unit[0])) { |
| 57 | + //mib |
| 58 | + case "m": |
| 59 | + inputDecSplit := strings.Split(quantityStr, ".") |
| 60 | + if len(inputDecSplit) == 2 { |
| 61 | + d, err := strconv.Atoi(inputDecSplit[1]) |
| 62 | + if err != nil { |
| 63 | + return ByteQuantity{}, err |
| 64 | + } |
| 65 | + if d != 0 { |
| 66 | + return ByteQuantity{}, fmt.Errorf("cannot accept floating point MB value, only integers are accepted") |
| 67 | + } |
| 68 | + } |
| 69 | + // need error here so that this quantity doesn't bind in the local scope |
| 70 | + var err error |
| 71 | + quantity, err = strconv.ParseUint(inputDecSplit[0], 10, 64) |
| 72 | + if err != nil { |
| 73 | + return ByteQuantity{}, err |
| 74 | + } |
| 75 | + //gib |
| 76 | + case "g": |
| 77 | + quantityDec, err := strconv.ParseFloat(quantityStr, 10) |
| 78 | + if err != nil { |
| 79 | + return ByteQuantity{}, err |
| 80 | + } |
| 81 | + if quantityDec > maxGiB { |
| 82 | + return ByteQuantity{}, fmt.Errorf("error GiB value is too large") |
| 83 | + } |
| 84 | + quantity = uint64(quantityDec * gbConvert) |
| 85 | + //tib |
| 86 | + case "t": |
| 87 | + quantityDec, err := strconv.ParseFloat(quantityStr, 10) |
| 88 | + if err != nil { |
| 89 | + return ByteQuantity{}, err |
| 90 | + } |
| 91 | + if quantityDec > maxTiB { |
| 92 | + return ByteQuantity{}, fmt.Errorf("error TiB value is too large") |
| 93 | + } |
| 94 | + quantity = uint64(quantityDec * tbConvert) |
| 95 | + default: |
| 96 | + return ByteQuantity{}, fmt.Errorf("error unit %s is not supported", unit) |
| 97 | + } |
| 98 | + |
| 99 | + return ByteQuantity{ |
| 100 | + Quantity: quantity, |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +// FromTiB returns a byte quantity of the passed in tebibytes quantity |
| 105 | +func FromTiB(tib uint64) ByteQuantity { |
| 106 | + return ByteQuantity{ |
| 107 | + Quantity: tib * tbConvert, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// FromGiB returns a byte quantity of the passed in gibibytes quantity |
| 112 | +func FromGiB(gib uint64) ByteQuantity { |
| 113 | + return ByteQuantity{ |
| 114 | + Quantity: gib * gbConvert, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// FromMiB returns a byte quantity of the passed in mebibytes quantity |
| 119 | +func FromMiB(mib uint64) ByteQuantity { |
| 120 | + return ByteQuantity{ |
| 121 | + Quantity: mib, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// StringMiB returns a byte quantity in a mebibytes string representation |
| 126 | +func (bq ByteQuantity) StringMiB() string { |
| 127 | + return fmt.Sprintf("%.0f %s", bq.MiB(), mib) |
| 128 | +} |
| 129 | + |
| 130 | +// StringGiB returns a byte quantity in a gibibytes string representation |
| 131 | +func (bq ByteQuantity) StringGiB() string { |
| 132 | + return fmt.Sprintf("%.3f %s", bq.GiB(), gib) |
| 133 | +} |
| 134 | + |
| 135 | +// StringTiB returns a byte quantity in a tebibytes string representation |
| 136 | +func (bq ByteQuantity) StringTiB() string { |
| 137 | + return fmt.Sprintf("%.3f %s", bq.TiB(), tib) |
| 138 | +} |
| 139 | + |
| 140 | +// MiB returns a byte quantity in mebibytes |
| 141 | +func (bq ByteQuantity) MiB() float64 { |
| 142 | + return float64(bq.Quantity) |
| 143 | +} |
| 144 | + |
| 145 | +// GiB returns a byte quantity in gibibytes |
| 146 | +func (bq ByteQuantity) GiB() float64 { |
| 147 | + return float64(bq.Quantity) * 1 / gbConvert |
| 148 | +} |
| 149 | + |
| 150 | +// TiB returns a byte quantity in tebibytes |
| 151 | +func (bq ByteQuantity) TiB() float64 { |
| 152 | + return float64(bq.Quantity) * 1 / tbConvert |
| 153 | +} |
0 commit comments