Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 The Kubernetes Authors. |
| 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 resource |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "math/big" |
| 24 | "strconv" |
| 25 | "strings" |
| 26 | |
| 27 | inf "gopkg.in/inf.v0" |
| 28 | ) |
| 29 | |
| 30 | // Quantity is a fixed-point representation of a number. |
| 31 | // It provides convenient marshaling/unmarshaling in JSON and YAML, |
| 32 | // in addition to String() and Int64() accessors. |
| 33 | // |
| 34 | // The serialization format is: |
| 35 | // |
| 36 | // <quantity> ::= <signedNumber><suffix> |
| 37 | // (Note that <suffix> may be empty, from the "" case in <decimalSI>.) |
| 38 | // <digit> ::= 0 | 1 | ... | 9 |
| 39 | // <digits> ::= <digit> | <digit><digits> |
| 40 | // <number> ::= <digits> | <digits>.<digits> | <digits>. | .<digits> |
| 41 | // <sign> ::= "+" | "-" |
| 42 | // <signedNumber> ::= <number> | <sign><number> |
| 43 | // <suffix> ::= <binarySI> | <decimalExponent> | <decimalSI> |
| 44 | // <binarySI> ::= Ki | Mi | Gi | Ti | Pi | Ei |
| 45 | // (International System of units; See: http://physics.nist.gov/cuu/Units/binary.html) |
| 46 | // <decimalSI> ::= m | "" | k | M | G | T | P | E |
| 47 | // (Note that 1024 = 1Ki but 1000 = 1k; I didn't choose the capitalization.) |
| 48 | // <decimalExponent> ::= "e" <signedNumber> | "E" <signedNumber> |
| 49 | // |
| 50 | // No matter which of the three exponent forms is used, no quantity may represent |
| 51 | // a number greater than 2^63-1 in magnitude, nor may it have more than 3 decimal |
| 52 | // places. Numbers larger or more precise will be capped or rounded up. |
| 53 | // (E.g.: 0.1m will rounded up to 1m.) |
| 54 | // This may be extended in the future if we require larger or smaller quantities. |
| 55 | // |
| 56 | // When a Quantity is parsed from a string, it will remember the type of suffix |
| 57 | // it had, and will use the same type again when it is serialized. |
| 58 | // |
| 59 | // Before serializing, Quantity will be put in "canonical form". |
| 60 | // This means that Exponent/suffix will be adjusted up or down (with a |
| 61 | // corresponding increase or decrease in Mantissa) such that: |
| 62 | // a. No precision is lost |
| 63 | // b. No fractional digits will be emitted |
| 64 | // c. The exponent (or suffix) is as large as possible. |
| 65 | // The sign will be omitted unless the number is negative. |
| 66 | // |
| 67 | // Examples: |
| 68 | // 1.5 will be serialized as "1500m" |
| 69 | // 1.5Gi will be serialized as "1536Mi" |
| 70 | // |
| 71 | // Note that the quantity will NEVER be internally represented by a |
| 72 | // floating point number. That is the whole point of this exercise. |
| 73 | // |
| 74 | // Non-canonical values will still parse as long as they are well formed, |
| 75 | // but will be re-emitted in their canonical form. (So always use canonical |
| 76 | // form, or don't diff.) |
| 77 | // |
| 78 | // This format is intended to make it difficult to use these numbers without |
| 79 | // writing some sort of special handling code in the hopes that that will |
| 80 | // cause implementors to also use a fixed point implementation. |
| 81 | // |
| 82 | // +protobuf=true |
| 83 | // +protobuf.embed=string |
| 84 | // +protobuf.options.marshal=false |
| 85 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 86 | // +k8s:deepcopy-gen=true |
| 87 | // +k8s:openapi-gen=true |
| 88 | type Quantity struct { |
| 89 | // i is the quantity in int64 scaled form, if d.Dec == nil |
| 90 | i int64Amount |
| 91 | // d is the quantity in inf.Dec form if d.Dec != nil |
| 92 | d infDecAmount |
| 93 | // s is the generated value of this quantity to avoid recalculation |
| 94 | s string |
| 95 | |
| 96 | // Change Format at will. See the comment for Canonicalize for |
| 97 | // more details. |
| 98 | Format |
| 99 | } |
| 100 | |
| 101 | // CanonicalValue allows a quantity amount to be converted to a string. |
| 102 | type CanonicalValue interface { |
| 103 | // AsCanonicalBytes returns a byte array representing the string representation |
| 104 | // of the value mantissa and an int32 representing its exponent in base-10. Callers may |
| 105 | // pass a byte slice to the method to avoid allocations. |
| 106 | AsCanonicalBytes(out []byte) ([]byte, int32) |
| 107 | // AsCanonicalBase1024Bytes returns a byte array representing the string representation |
| 108 | // of the value mantissa and an int32 representing its exponent in base-1024. Callers |
| 109 | // may pass a byte slice to the method to avoid allocations. |
| 110 | AsCanonicalBase1024Bytes(out []byte) ([]byte, int32) |
| 111 | } |
| 112 | |
| 113 | // Format lists the three possible formattings of a quantity. |
| 114 | type Format string |
| 115 | |
| 116 | const ( |
| 117 | DecimalExponent = Format("DecimalExponent") // e.g., 12e6 |
| 118 | BinarySI = Format("BinarySI") // e.g., 12Mi (12 * 2^20) |
| 119 | DecimalSI = Format("DecimalSI") // e.g., 12M (12 * 10^6) |
| 120 | ) |
| 121 | |
| 122 | // MustParse turns the given string into a quantity or panics; for tests |
| 123 | // or others cases where you know the string is valid. |
| 124 | func MustParse(str string) Quantity { |
| 125 | q, err := ParseQuantity(str) |
| 126 | if err != nil { |
| 127 | panic(fmt.Errorf("cannot parse '%v': %v", str, err)) |
| 128 | } |
| 129 | return q |
| 130 | } |
| 131 | |
| 132 | const ( |
| 133 | // splitREString is used to separate a number from its suffix; as such, |
| 134 | // this is overly permissive, but that's OK-- it will be checked later. |
| 135 | splitREString = "^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$" |
| 136 | ) |
| 137 | |
| 138 | var ( |
| 139 | // Errors that could happen while parsing a string. |
| 140 | ErrFormatWrong = errors.New("quantities must match the regular expression '" + splitREString + "'") |
| 141 | ErrNumeric = errors.New("unable to parse numeric part of quantity") |
| 142 | ErrSuffix = errors.New("unable to parse quantity's suffix") |
| 143 | ) |
| 144 | |
| 145 | // parseQuantityString is a fast scanner for quantity values. |
| 146 | func parseQuantityString(str string) (positive bool, value, num, denom, suffix string, err error) { |
| 147 | positive = true |
| 148 | pos := 0 |
| 149 | end := len(str) |
| 150 | |
| 151 | // handle leading sign |
| 152 | if pos < end { |
| 153 | switch str[0] { |
| 154 | case '-': |
| 155 | positive = false |
| 156 | pos++ |
| 157 | case '+': |
| 158 | pos++ |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // strip leading zeros |
| 163 | Zeroes: |
| 164 | for i := pos; ; i++ { |
| 165 | if i >= end { |
| 166 | num = "0" |
| 167 | value = num |
| 168 | return |
| 169 | } |
| 170 | switch str[i] { |
| 171 | case '0': |
| 172 | pos++ |
| 173 | default: |
| 174 | break Zeroes |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // extract the numerator |
| 179 | Num: |
| 180 | for i := pos; ; i++ { |
| 181 | if i >= end { |
| 182 | num = str[pos:end] |
| 183 | value = str[0:end] |
| 184 | return |
| 185 | } |
| 186 | switch str[i] { |
| 187 | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 188 | default: |
| 189 | num = str[pos:i] |
| 190 | pos = i |
| 191 | break Num |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // if we stripped all numerator positions, always return 0 |
| 196 | if len(num) == 0 { |
| 197 | num = "0" |
| 198 | } |
| 199 | |
| 200 | // handle a denominator |
| 201 | if pos < end && str[pos] == '.' { |
| 202 | pos++ |
| 203 | Denom: |
| 204 | for i := pos; ; i++ { |
| 205 | if i >= end { |
| 206 | denom = str[pos:end] |
| 207 | value = str[0:end] |
| 208 | return |
| 209 | } |
| 210 | switch str[i] { |
| 211 | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 212 | default: |
| 213 | denom = str[pos:i] |
| 214 | pos = i |
| 215 | break Denom |
| 216 | } |
| 217 | } |
| 218 | // TODO: we currently allow 1.G, but we may not want to in the future. |
| 219 | // if len(denom) == 0 { |
| 220 | // err = ErrFormatWrong |
| 221 | // return |
| 222 | // } |
| 223 | } |
| 224 | value = str[0:pos] |
| 225 | |
| 226 | // grab the elements of the suffix |
| 227 | suffixStart := pos |
| 228 | for i := pos; ; i++ { |
| 229 | if i >= end { |
| 230 | suffix = str[suffixStart:end] |
| 231 | return |
| 232 | } |
| 233 | if !strings.ContainsAny(str[i:i+1], "eEinumkKMGTP") { |
| 234 | pos = i |
| 235 | break |
| 236 | } |
| 237 | } |
| 238 | if pos < end { |
| 239 | switch str[pos] { |
| 240 | case '-', '+': |
| 241 | pos++ |
| 242 | } |
| 243 | } |
| 244 | Suffix: |
| 245 | for i := pos; ; i++ { |
| 246 | if i >= end { |
| 247 | suffix = str[suffixStart:end] |
| 248 | return |
| 249 | } |
| 250 | switch str[i] { |
| 251 | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 252 | default: |
| 253 | break Suffix |
| 254 | } |
| 255 | } |
| 256 | // we encountered a non decimal in the Suffix loop, but the last character |
| 257 | // was not a valid exponent |
| 258 | err = ErrFormatWrong |
| 259 | return |
| 260 | } |
| 261 | |
| 262 | // ParseQuantity turns str into a Quantity, or returns an error. |
| 263 | func ParseQuantity(str string) (Quantity, error) { |
| 264 | if len(str) == 0 { |
| 265 | return Quantity{}, ErrFormatWrong |
| 266 | } |
| 267 | if str == "0" { |
| 268 | return Quantity{Format: DecimalSI, s: str}, nil |
| 269 | } |
| 270 | |
| 271 | positive, value, num, denom, suf, err := parseQuantityString(str) |
| 272 | if err != nil { |
| 273 | return Quantity{}, err |
| 274 | } |
| 275 | |
| 276 | base, exponent, format, ok := quantitySuffixer.interpret(suffix(suf)) |
| 277 | if !ok { |
| 278 | return Quantity{}, ErrSuffix |
| 279 | } |
| 280 | |
| 281 | precision := int32(0) |
| 282 | scale := int32(0) |
| 283 | mantissa := int64(1) |
| 284 | switch format { |
| 285 | case DecimalExponent, DecimalSI: |
| 286 | scale = exponent |
| 287 | precision = maxInt64Factors - int32(len(num)+len(denom)) |
| 288 | case BinarySI: |
| 289 | scale = 0 |
| 290 | switch { |
| 291 | case exponent >= 0 && len(denom) == 0: |
| 292 | // only handle positive binary numbers with the fast path |
| 293 | mantissa = int64(int64(mantissa) << uint64(exponent)) |
| 294 | // 1Mi (2^20) has ~6 digits of decimal precision, so exponent*3/10 -1 is roughly the precision |
| 295 | precision = 15 - int32(len(num)) - int32(float32(exponent)*3/10) - 1 |
| 296 | default: |
| 297 | precision = -1 |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if precision >= 0 { |
| 302 | // if we have a denominator, shift the entire value to the left by the number of places in the |
| 303 | // denominator |
| 304 | scale -= int32(len(denom)) |
| 305 | if scale >= int32(Nano) { |
| 306 | shifted := num + denom |
| 307 | |
| 308 | var value int64 |
| 309 | value, err := strconv.ParseInt(shifted, 10, 64) |
| 310 | if err != nil { |
| 311 | return Quantity{}, ErrNumeric |
| 312 | } |
| 313 | if result, ok := int64Multiply(value, int64(mantissa)); ok { |
| 314 | if !positive { |
| 315 | result = -result |
| 316 | } |
| 317 | // if the number is in canonical form, reuse the string |
| 318 | switch format { |
| 319 | case BinarySI: |
| 320 | if exponent%10 == 0 && (value&0x07 != 0) { |
| 321 | return Quantity{i: int64Amount{value: result, scale: Scale(scale)}, Format: format, s: str}, nil |
| 322 | } |
| 323 | default: |
| 324 | if scale%3 == 0 && !strings.HasSuffix(shifted, "000") && shifted[0] != '0' { |
| 325 | return Quantity{i: int64Amount{value: result, scale: Scale(scale)}, Format: format, s: str}, nil |
| 326 | } |
| 327 | } |
| 328 | return Quantity{i: int64Amount{value: result, scale: Scale(scale)}, Format: format}, nil |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | amount := new(inf.Dec) |
| 334 | if _, ok := amount.SetString(value); !ok { |
| 335 | return Quantity{}, ErrNumeric |
| 336 | } |
| 337 | |
| 338 | // So that no one but us has to think about suffixes, remove it. |
| 339 | if base == 10 { |
| 340 | amount.SetScale(amount.Scale() + Scale(exponent).infScale()) |
| 341 | } else if base == 2 { |
| 342 | // numericSuffix = 2 ** exponent |
| 343 | numericSuffix := big.NewInt(1).Lsh(bigOne, uint(exponent)) |
| 344 | ub := amount.UnscaledBig() |
| 345 | amount.SetUnscaledBig(ub.Mul(ub, numericSuffix)) |
| 346 | } |
| 347 | |
| 348 | // Cap at min/max bounds. |
| 349 | sign := amount.Sign() |
| 350 | if sign == -1 { |
| 351 | amount.Neg(amount) |
| 352 | } |
| 353 | |
| 354 | // This rounds non-zero values up to the minimum representable value, under the theory that |
| 355 | // if you want some resources, you should get some resources, even if you asked for way too small |
| 356 | // of an amount. Arguably, this should be inf.RoundHalfUp (normal rounding), but that would have |
| 357 | // the side effect of rounding values < .5n to zero. |
| 358 | if v, ok := amount.Unscaled(); v != int64(0) || !ok { |
| 359 | amount.Round(amount, Nano.infScale(), inf.RoundUp) |
| 360 | } |
| 361 | |
| 362 | // The max is just a simple cap. |
| 363 | // TODO: this prevents accumulating quantities greater than int64, for instance quota across a cluster |
| 364 | if format == BinarySI && amount.Cmp(maxAllowed.Dec) > 0 { |
| 365 | amount.Set(maxAllowed.Dec) |
| 366 | } |
| 367 | |
| 368 | if format == BinarySI && amount.Cmp(decOne) < 0 && amount.Cmp(decZero) > 0 { |
| 369 | // This avoids rounding and hopefully confusion, too. |
| 370 | format = DecimalSI |
| 371 | } |
| 372 | if sign == -1 { |
| 373 | amount.Neg(amount) |
| 374 | } |
| 375 | |
| 376 | return Quantity{d: infDecAmount{amount}, Format: format}, nil |
| 377 | } |
| 378 | |
| 379 | // DeepCopy returns a deep-copy of the Quantity value. Note that the method |
| 380 | // receiver is a value, so we can mutate it in-place and return it. |
| 381 | func (q Quantity) DeepCopy() Quantity { |
| 382 | if q.d.Dec != nil { |
| 383 | tmp := &inf.Dec{} |
| 384 | q.d.Dec = tmp.Set(q.d.Dec) |
| 385 | } |
| 386 | return q |
| 387 | } |
| 388 | |
| 389 | // OpenAPISchemaType is used by the kube-openapi generator when constructing |
| 390 | // the OpenAPI spec of this type. |
| 391 | // |
| 392 | // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators |
| 393 | func (_ Quantity) OpenAPISchemaType() []string { return []string{"string"} } |
| 394 | |
| 395 | // OpenAPISchemaFormat is used by the kube-openapi generator when constructing |
| 396 | // the OpenAPI spec of this type. |
| 397 | func (_ Quantity) OpenAPISchemaFormat() string { return "" } |
| 398 | |
| 399 | // CanonicalizeBytes returns the canonical form of q and its suffix (see comment on Quantity). |
| 400 | // |
| 401 | // Note about BinarySI: |
| 402 | // * If q.Format is set to BinarySI and q.Amount represents a non-zero value between |
| 403 | // -1 and +1, it will be emitted as if q.Format were DecimalSI. |
| 404 | // * Otherwise, if q.Format is set to BinarySI, fractional parts of q.Amount will be |
| 405 | // rounded up. (1.1i becomes 2i.) |
| 406 | func (q *Quantity) CanonicalizeBytes(out []byte) (result, suffix []byte) { |
| 407 | if q.IsZero() { |
| 408 | return zeroBytes, nil |
| 409 | } |
| 410 | |
| 411 | var rounded CanonicalValue |
| 412 | format := q.Format |
| 413 | switch format { |
| 414 | case DecimalExponent, DecimalSI: |
| 415 | case BinarySI: |
| 416 | if q.CmpInt64(-1024) > 0 && q.CmpInt64(1024) < 0 { |
| 417 | // This avoids rounding and hopefully confusion, too. |
| 418 | format = DecimalSI |
| 419 | } else { |
| 420 | var exact bool |
| 421 | if rounded, exact = q.AsScale(0); !exact { |
| 422 | // Don't lose precision-- show as DecimalSI |
| 423 | format = DecimalSI |
| 424 | } |
| 425 | } |
| 426 | default: |
| 427 | format = DecimalExponent |
| 428 | } |
| 429 | |
| 430 | // TODO: If BinarySI formatting is requested but would cause rounding, upgrade to |
| 431 | // one of the other formats. |
| 432 | switch format { |
| 433 | case DecimalExponent, DecimalSI: |
| 434 | number, exponent := q.AsCanonicalBytes(out) |
| 435 | suffix, _ := quantitySuffixer.constructBytes(10, exponent, format) |
| 436 | return number, suffix |
| 437 | default: |
| 438 | // format must be BinarySI |
| 439 | number, exponent := rounded.AsCanonicalBase1024Bytes(out) |
| 440 | suffix, _ := quantitySuffixer.constructBytes(2, exponent*10, format) |
| 441 | return number, suffix |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // AsInt64 returns a representation of the current value as an int64 if a fast conversion |
| 446 | // is possible. If false is returned, callers must use the inf.Dec form of this quantity. |
| 447 | func (q *Quantity) AsInt64() (int64, bool) { |
| 448 | if q.d.Dec != nil { |
| 449 | return 0, false |
| 450 | } |
| 451 | return q.i.AsInt64() |
| 452 | } |
| 453 | |
| 454 | // ToDec promotes the quantity in place to use an inf.Dec representation and returns itself. |
| 455 | func (q *Quantity) ToDec() *Quantity { |
| 456 | if q.d.Dec == nil { |
| 457 | q.d.Dec = q.i.AsDec() |
| 458 | q.i = int64Amount{} |
| 459 | } |
| 460 | return q |
| 461 | } |
| 462 | |
| 463 | // AsDec returns the quantity as represented by a scaled inf.Dec. |
| 464 | func (q *Quantity) AsDec() *inf.Dec { |
| 465 | if q.d.Dec != nil { |
| 466 | return q.d.Dec |
| 467 | } |
| 468 | q.d.Dec = q.i.AsDec() |
| 469 | q.i = int64Amount{} |
| 470 | return q.d.Dec |
| 471 | } |
| 472 | |
| 473 | // AsCanonicalBytes returns the canonical byte representation of this quantity as a mantissa |
| 474 | // and base 10 exponent. The out byte slice may be passed to the method to avoid an extra |
| 475 | // allocation. |
| 476 | func (q *Quantity) AsCanonicalBytes(out []byte) (result []byte, exponent int32) { |
| 477 | if q.d.Dec != nil { |
| 478 | return q.d.AsCanonicalBytes(out) |
| 479 | } |
| 480 | return q.i.AsCanonicalBytes(out) |
| 481 | } |
| 482 | |
| 483 | // IsZero returns true if the quantity is equal to zero. |
| 484 | func (q *Quantity) IsZero() bool { |
| 485 | if q.d.Dec != nil { |
| 486 | return q.d.Dec.Sign() == 0 |
| 487 | } |
| 488 | return q.i.value == 0 |
| 489 | } |
| 490 | |
| 491 | // Sign returns 0 if the quantity is zero, -1 if the quantity is less than zero, or 1 if the |
| 492 | // quantity is greater than zero. |
| 493 | func (q *Quantity) Sign() int { |
| 494 | if q.d.Dec != nil { |
| 495 | return q.d.Dec.Sign() |
| 496 | } |
| 497 | return q.i.Sign() |
| 498 | } |
| 499 | |
| 500 | // AsScale returns the current value, rounded up to the provided scale, and returns |
| 501 | // false if the scale resulted in a loss of precision. |
| 502 | func (q *Quantity) AsScale(scale Scale) (CanonicalValue, bool) { |
| 503 | if q.d.Dec != nil { |
| 504 | return q.d.AsScale(scale) |
| 505 | } |
| 506 | return q.i.AsScale(scale) |
| 507 | } |
| 508 | |
| 509 | // RoundUp updates the quantity to the provided scale, ensuring that the value is at |
| 510 | // least 1. False is returned if the rounding operation resulted in a loss of precision. |
| 511 | // Negative numbers are rounded away from zero (-9 scale 1 rounds to -10). |
| 512 | func (q *Quantity) RoundUp(scale Scale) bool { |
| 513 | if q.d.Dec != nil { |
| 514 | q.s = "" |
| 515 | d, exact := q.d.AsScale(scale) |
| 516 | q.d = d |
| 517 | return exact |
| 518 | } |
| 519 | // avoid clearing the string value if we have already calculated it |
| 520 | if q.i.scale >= scale { |
| 521 | return true |
| 522 | } |
| 523 | q.s = "" |
| 524 | i, exact := q.i.AsScale(scale) |
| 525 | q.i = i |
| 526 | return exact |
| 527 | } |
| 528 | |
| 529 | // Add adds the provide y quantity to the current value. If the current value is zero, |
| 530 | // the format of the quantity will be updated to the format of y. |
| 531 | func (q *Quantity) Add(y Quantity) { |
| 532 | q.s = "" |
| 533 | if q.d.Dec == nil && y.d.Dec == nil { |
| 534 | if q.i.value == 0 { |
| 535 | q.Format = y.Format |
| 536 | } |
| 537 | if q.i.Add(y.i) { |
| 538 | return |
| 539 | } |
| 540 | } else if q.IsZero() { |
| 541 | q.Format = y.Format |
| 542 | } |
| 543 | q.ToDec().d.Dec.Add(q.d.Dec, y.AsDec()) |
| 544 | } |
| 545 | |
| 546 | // Sub subtracts the provided quantity from the current value in place. If the current |
| 547 | // value is zero, the format of the quantity will be updated to the format of y. |
| 548 | func (q *Quantity) Sub(y Quantity) { |
| 549 | q.s = "" |
| 550 | if q.IsZero() { |
| 551 | q.Format = y.Format |
| 552 | } |
| 553 | if q.d.Dec == nil && y.d.Dec == nil && q.i.Sub(y.i) { |
| 554 | return |
| 555 | } |
| 556 | q.ToDec().d.Dec.Sub(q.d.Dec, y.AsDec()) |
| 557 | } |
| 558 | |
| 559 | // Cmp returns 0 if the quantity is equal to y, -1 if the quantity is less than y, or 1 if the |
| 560 | // quantity is greater than y. |
| 561 | func (q *Quantity) Cmp(y Quantity) int { |
| 562 | if q.d.Dec == nil && y.d.Dec == nil { |
| 563 | return q.i.Cmp(y.i) |
| 564 | } |
| 565 | return q.AsDec().Cmp(y.AsDec()) |
| 566 | } |
| 567 | |
| 568 | // CmpInt64 returns 0 if the quantity is equal to y, -1 if the quantity is less than y, or 1 if the |
| 569 | // quantity is greater than y. |
| 570 | func (q *Quantity) CmpInt64(y int64) int { |
| 571 | if q.d.Dec != nil { |
| 572 | return q.d.Dec.Cmp(inf.NewDec(y, inf.Scale(0))) |
| 573 | } |
| 574 | return q.i.Cmp(int64Amount{value: y}) |
| 575 | } |
| 576 | |
| 577 | // Neg sets quantity to be the negative value of itself. |
| 578 | func (q *Quantity) Neg() { |
| 579 | q.s = "" |
| 580 | if q.d.Dec == nil { |
| 581 | q.i.value = -q.i.value |
| 582 | return |
| 583 | } |
| 584 | q.d.Dec.Neg(q.d.Dec) |
| 585 | } |
| 586 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 587 | // Equal checks equality of two Quantities. This is useful for testing with |
| 588 | // cmp.Equal. |
| 589 | func (q Quantity) Equal(v Quantity) bool { |
| 590 | return q.Cmp(v) == 0 |
| 591 | } |
| 592 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 593 | // int64QuantityExpectedBytes is the expected width in bytes of the canonical string representation |
| 594 | // of most Quantity values. |
| 595 | const int64QuantityExpectedBytes = 18 |
| 596 | |
| 597 | // String formats the Quantity as a string, caching the result if not calculated. |
| 598 | // String is an expensive operation and caching this result significantly reduces the cost of |
| 599 | // normal parse / marshal operations on Quantity. |
| 600 | func (q *Quantity) String() string { |
| 601 | if len(q.s) == 0 { |
| 602 | result := make([]byte, 0, int64QuantityExpectedBytes) |
| 603 | number, suffix := q.CanonicalizeBytes(result) |
| 604 | number = append(number, suffix...) |
| 605 | q.s = string(number) |
| 606 | } |
| 607 | return q.s |
| 608 | } |
| 609 | |
| 610 | // MarshalJSON implements the json.Marshaller interface. |
| 611 | func (q Quantity) MarshalJSON() ([]byte, error) { |
| 612 | if len(q.s) > 0 { |
| 613 | out := make([]byte, len(q.s)+2) |
| 614 | out[0], out[len(out)-1] = '"', '"' |
| 615 | copy(out[1:], q.s) |
| 616 | return out, nil |
| 617 | } |
| 618 | result := make([]byte, int64QuantityExpectedBytes, int64QuantityExpectedBytes) |
| 619 | result[0] = '"' |
| 620 | number, suffix := q.CanonicalizeBytes(result[1:1]) |
| 621 | // if the same slice was returned to us that we passed in, avoid another allocation by copying number into |
| 622 | // the source slice and returning that |
| 623 | if len(number) > 0 && &number[0] == &result[1] && (len(number)+len(suffix)+2) <= int64QuantityExpectedBytes { |
| 624 | number = append(number, suffix...) |
| 625 | number = append(number, '"') |
| 626 | return result[:1+len(number)], nil |
| 627 | } |
| 628 | // if CanonicalizeBytes needed more space than our slice provided, we may need to allocate again so use |
| 629 | // append |
| 630 | result = result[:1] |
| 631 | result = append(result, number...) |
| 632 | result = append(result, suffix...) |
| 633 | result = append(result, '"') |
| 634 | return result, nil |
| 635 | } |
| 636 | |
| 637 | // UnmarshalJSON implements the json.Unmarshaller interface. |
| 638 | // TODO: Remove support for leading/trailing whitespace |
| 639 | func (q *Quantity) UnmarshalJSON(value []byte) error { |
| 640 | l := len(value) |
| 641 | if l == 4 && bytes.Equal(value, []byte("null")) { |
| 642 | q.d.Dec = nil |
| 643 | q.i = int64Amount{} |
| 644 | return nil |
| 645 | } |
| 646 | if l >= 2 && value[0] == '"' && value[l-1] == '"' { |
| 647 | value = value[1 : l-1] |
| 648 | } |
| 649 | |
| 650 | parsed, err := ParseQuantity(strings.TrimSpace(string(value))) |
| 651 | if err != nil { |
| 652 | return err |
| 653 | } |
| 654 | |
| 655 | // This copy is safe because parsed will not be referred to again. |
| 656 | *q = parsed |
| 657 | return nil |
| 658 | } |
| 659 | |
| 660 | // NewQuantity returns a new Quantity representing the given |
| 661 | // value in the given format. |
| 662 | func NewQuantity(value int64, format Format) *Quantity { |
| 663 | return &Quantity{ |
| 664 | i: int64Amount{value: value}, |
| 665 | Format: format, |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // NewMilliQuantity returns a new Quantity representing the given |
| 670 | // value * 1/1000 in the given format. Note that BinarySI formatting |
| 671 | // will round fractional values, and will be changed to DecimalSI for |
| 672 | // values x where (-1 < x < 1) && (x != 0). |
| 673 | func NewMilliQuantity(value int64, format Format) *Quantity { |
| 674 | return &Quantity{ |
| 675 | i: int64Amount{value: value, scale: -3}, |
| 676 | Format: format, |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | // NewScaledQuantity returns a new Quantity representing the given |
| 681 | // value * 10^scale in DecimalSI format. |
| 682 | func NewScaledQuantity(value int64, scale Scale) *Quantity { |
| 683 | return &Quantity{ |
| 684 | i: int64Amount{value: value, scale: scale}, |
| 685 | Format: DecimalSI, |
| 686 | } |
| 687 | } |
| 688 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 689 | // Value returns the unscaled value of q rounded up to the nearest integer away from 0. |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 690 | func (q *Quantity) Value() int64 { |
| 691 | return q.ScaledValue(0) |
| 692 | } |
| 693 | |
| 694 | // MilliValue returns the value of ceil(q * 1000); this could overflow an int64; |
| 695 | // if that's a concern, call Value() first to verify the number is small enough. |
| 696 | func (q *Quantity) MilliValue() int64 { |
| 697 | return q.ScaledValue(Milli) |
| 698 | } |
| 699 | |
| 700 | // ScaledValue returns the value of ceil(q * 10^scale); this could overflow an int64. |
| 701 | // To detect overflow, call Value() first and verify the expected magnitude. |
| 702 | func (q *Quantity) ScaledValue(scale Scale) int64 { |
| 703 | if q.d.Dec == nil { |
| 704 | i, _ := q.i.AsScaledInt64(scale) |
| 705 | return i |
| 706 | } |
| 707 | dec := q.d.Dec |
| 708 | return scaledValue(dec.UnscaledBig(), int(dec.Scale()), int(scale.infScale())) |
| 709 | } |
| 710 | |
| 711 | // Set sets q's value to be value. |
| 712 | func (q *Quantity) Set(value int64) { |
| 713 | q.SetScaled(value, 0) |
| 714 | } |
| 715 | |
| 716 | // SetMilli sets q's value to be value * 1/1000. |
| 717 | func (q *Quantity) SetMilli(value int64) { |
| 718 | q.SetScaled(value, Milli) |
| 719 | } |
| 720 | |
| 721 | // SetScaled sets q's value to be value * 10^scale |
| 722 | func (q *Quantity) SetScaled(value int64, scale Scale) { |
| 723 | q.s = "" |
| 724 | q.d.Dec = nil |
| 725 | q.i = int64Amount{value: value, scale: scale} |
| 726 | } |
| 727 | |
| 728 | // Copy is a convenience function that makes a deep copy for you. Non-deep |
| 729 | // copies of quantities share pointers and you will regret that. |
| 730 | func (q *Quantity) Copy() *Quantity { |
| 731 | if q.d.Dec == nil { |
| 732 | return &Quantity{ |
| 733 | s: q.s, |
| 734 | i: q.i, |
| 735 | Format: q.Format, |
| 736 | } |
| 737 | } |
| 738 | tmp := &inf.Dec{} |
| 739 | return &Quantity{ |
| 740 | s: q.s, |
| 741 | d: infDecAmount{tmp.Set(q.d.Dec)}, |
| 742 | Format: q.Format, |
| 743 | } |
| 744 | } |