package humanize

import (
	
	
	
	
	
	
)

// IEC Sizes.
// kibis of bits
const (
	Byte = 1 << (iota * 10)
	KiByte
	MiByte
	GiByte
	TiByte
	PiByte
	EiByte
)

// SI Sizes.
const (
	IByte = 1
	KByte = IByte * 1000
	MByte = KByte * 1000
	GByte = MByte * 1000
	TByte = GByte * 1000
	PByte = TByte * 1000
	EByte = PByte * 1000
)

var bytesSizeTable = map[string]uint64{
	"b":   Byte,
	"kib": KiByte,
	"kb":  KByte,
	"mib": MiByte,
	"mb":  MByte,
	"gib": GiByte,
	"gb":  GByte,
	"tib": TiByte,
	"tb":  TByte,
	"pib": PiByte,
	"pb":  PByte,
	"eib": EiByte,
	"eb":  EByte,
	// Without suffix
	"":   Byte,
	"ki": KiByte,
	"k":  KByte,
	"mi": MiByte,
	"m":  MByte,
	"gi": GiByte,
	"g":  GByte,
	"ti": TiByte,
	"t":  TByte,
	"pi": PiByte,
	"p":  PByte,
	"ei": EiByte,
	"e":  EByte,
}

func logn(,  float64) float64 {
	return math.Log() / math.Log()
}

func countDigits( int64) int {
	 := 0
	for  != 0 {
		 /= 10
		 += 1
	}
	return 
}

func humanateBytes( uint64,  float64,  int,  []string) string {
	if  < 10 {
		return fmt.Sprintf("%d B", )
	}
	 := math.Floor(logn(float64(), ))
	 := [int()]
	 := float64() / math.Pow(, )

	// Determine how many decimal places to keep based on the unrounded
	// value's integer-digit count, then round exactly once to that
	// precision. Rounding first to a fixed precision and then again when
	// formatting (double rounding) can flip the result at a boundary due
	// to floating point error (see #103).
	 := countDigits(int64())
	 := max(-, 0)
	 := math.Pow10()
	 = math.Floor(*+0.5) / 

	// Rounding may have carried into an additional integer digit (e.g.
	// 9.996 -> 10.0), which means fewer decimal places should be shown
	// than originally computed.
	if  := countDigits(int64());  >  {
		 = max(-(-), 0)
	}

	 := "%%.%df %%s"
	 := fmt.Sprintf(, )
	return fmt.Sprintf(, , )
}

// Bytes produces a human-readable representation of an SI size.
//
// See also: ParseBytes.
//
// Bytes(82854982) -> 83 MB
func ( uint64) string {
	 := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
	return humanateBytes(, 1000, 2, )
}

// BytesN produces a human-readable representation of an SI size.
// n specifies the total number of digits to output, including the decimal part.
// If n is less than or equal to the number of digits in the integer part, the decimal part will be omitted.
//
// See also: ParseBytes.
//
// BytesN(82854982, 3) -> 82.9 MB
// BytesN(82854982, 4) -> 82.85 MB
func ( uint64,  int) string {
	 := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
	return humanateBytes(, 1000, , )
}

// IBytes produces a human-readable representation of an IEC size.
//
// See also: ParseBytes.
//
// IBytes(82854982) -> 79 MiB
func ( uint64) string {
	 := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
	return humanateBytes(, 1024, 2, )
}

// IBytesN produces a human-readable representation of an IEC size.
// n specifies the total number of digits to output, including the decimal part.
// If n is less than or equal to the number of digits in the integer part, the decimal part will be omitted.
//
// See also: ParseBytes.
//
// IBytesN(82854982, 4)  -> 79.02 MiB
// IBytesN(123456789, 3) -> 118 MiB
// IBytesN(123456789, 6) -> 117.738 MiB
func ( uint64,  int) string {
	 := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
	return humanateBytes(, 1024, , )
}

// ParseBytes parses a string representation of bytes into the number
// of bytes it represents.
//
// See Also: Bytes, IBytes.
//
// ParseBytes("42 MB") -> 42000000, nil
// ParseBytes("42 mib") -> 44040192, nil
func ( string) (uint64, error) {
	 := 0
	 := false
	for ,  := range  {
		if !(unicode.IsDigit() ||  == '.' ||  == ',') {
			break
		}
		if  == ',' {
			 = true
		}
		++
	}

	 := [:]
	if  {
		 = strings.Replace(, ",", "", -1)
	}

	,  := strconv.ParseFloat(, 64)
	if  != nil {
		return 0, 
	}

	 := strings.ToLower(strings.TrimSpace([:]))
	if ,  := bytesSizeTable[];  {
		// Parse whole-number inputs exactly. A float64 cannot represent
		// integers above 2^53, and the multiplication below would also reject
		// otherwise-valid values near math.MaxUint64, so route integers through
		// uint64 arithmetic with an overflow check.
		if !strings.ContainsRune(, '.') {
			if ,  := strconv.ParseUint(, 10, 64);  == nil {
				if ,  := bits.Mul64(, );  == 0 {
					return , nil
				}
				return 0, fmt.Errorf("too large: %v", )
			}
		}
		 *= float64()
		if  >= math.MaxUint64 {
			return 0, fmt.Errorf("too large: %v", )
		}
		return uint64(), nil
	}

	return 0, fmt.Errorf("unhandled size name: %v", )
}