rangecheck.go raw

   1  // Copyright 2013-2022 Frank Schroeder. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package properties
   6  
   7  import (
   8  	"fmt"
   9  	"math"
  10  )
  11  
  12  // make this a var to overwrite it in a test
  13  var is32Bit = ^uint(0) == math.MaxUint32
  14  
  15  // intRangeCheck checks if the value fits into the int type and
  16  // panics if it does not.
  17  func intRangeCheck(key string, v int64) int {
  18  	if is32Bit && (v < math.MinInt32 || v > math.MaxInt32) {
  19  		panic(fmt.Sprintf("Value %d for key %s out of range", v, key))
  20  	}
  21  	return int(v)
  22  }
  23  
  24  // uintRangeCheck checks if the value fits into the uint type and
  25  // panics if it does not.
  26  func uintRangeCheck(key string, v uint64) uint {
  27  	if is32Bit && v > math.MaxUint32 {
  28  		panic(fmt.Sprintf("Value %d for key %s out of range", v, key))
  29  	}
  30  	return uint(v)
  31  }
  32