// Turns a floating point string in seconds into a ns integer
//
// Guaranteed to be perfectly accurate
779036
func floatStringToNs(s string) (int64, error) {
	const zeros = "000000000"
	if point := strings.IndexRune(s, '.'); point >= 0 {
		tail := s[point+1:]
		if fill := 9 - len(tail); fill < 0 {
			tail = tail[:9]
		} else {
			tail += zeros[:fill]
		}
		s = s[:point] + tail
	} else if len(s) > 0 { // Make sure empty string produces an error
		s += zeros
	}
	return strconv.ParseInt(s, 10, 64)
}
634460
func TimeString(t time.Time) string {
	if t.IsZero() {
		return "0000-00-00 00:00:00"
	}
	if t.Nanosecond() == 0 {
		return t.Format(TimeFormat[:19])
	}
	return t.Format(TimeFormat)
}
925099
func IsUTFDigit(str string) bool {
	if IsNull(str) {
		return true
	}
	if strings.IndexAny(str, "+-") > 0 {
		return false
	}
	if len(str) > 1 {
		str = strings.TrimPrefix(str, "-")
		str = strings.TrimPrefix(str, "+")
	}
	for _, c := range str {
		if !unicode.IsDigit(c) { //digits && minus sign are ok
			return false
		}
	}
	return true

}
296846
func parseNum(in interface{}) (integral int64, fractional int64, err error) {
	if s, ok := in.(string); ok {
		ss := strings.Split(s, ".")
		if len(ss) > 2 {
			return 0, 0, fmt.Errorf("can not parse '%s' as a number - too many decimal points", s)
		}
		if len(ss) == 1 {
			integral, err := strconv.ParseInt(s, 0, 64)
			return integral, 0, err
		}
		integral, err := strconv.ParseInt(ss[0], 0, 64)
		if err != nil {
			return integral, 0, err
		}
		fractional, err = strconv.ParseInt(padRight(ss[1], "0", 9), 0, 64)
		return integral, fractional, err
	}
	if s, ok := in.(fmt.Stringer); ok {
		return parseNum(s.String())
	}
	if i, ok := in.(int); ok {
		return int64(i), 0, nil
	}
	if u, ok := in.(uint64); ok {
		return int64(u), 0, nil
	}
	if f, ok := in.(float64); ok {
		return 0, 0, fmt.Errorf("can not parse floating point number (%f) - use a string instead", f)
	}
	if in == nil {
		return 0, 0, nil
	}
	return 0, 0, nil
}
1002915
func (a *ArgType) ParsePrecision(dt string) (string, int, int) {
	var err error

	precision := -1
	scale := -1

	m := PrecScaleRE.FindStringSubmatchIndex(dt)
	if m != nil {
		// extract precision
		precision, err = strconv.Atoi(dt[m[2]:m[3]])
		if err != nil {
			panic("could not convert precision")
		}

		// extract scale
		if m[4] != -1 {
			scale, err = strconv.Atoi(dt[m[4]+1 : m[5]])
			if err != nil {
				panic("could not convert scale")
			}
		}

		// change dt
		dt = dt[:m[0]] + dt[m[1]:]
	}

	return dt, precision, scale
}
1203223
func ConvertStringToTimeDuration(input string) (time.Duration, error) {
	output, err := strconv.Atoi(input)
	return time.Duration(output), err
}
848896
func F(n float64, v uint64) (f int64) {

	s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)

	// with either be '0' or '0.xxxx', so if 1 then f will be zero
	// otherwise need to parse
	if len(s) != 1 {

		// ignoring error, because it can't fail as we generated
		// the string internally from a real number
		f, _ = strconv.ParseInt(s[2:], 10, 64)
	}

	return
}
172326
func (p *point) PrecisionString(precision string) string {
	if p.Time().IsZero() {
		return fmt.Sprintf("%s %s", p.Key(), string(p.fields))
	}
	return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields),
		p.UnixNano()/GetPrecisionMultiplier(precision))
}
568681
func ConvertInt8(str string) (int8, error) {
	i, err := strconv.ParseInt(str, 10, 8)
	if err != nil {
		return 0, err
	}
	return int8(i), nil
}
1190959
func nDigits(buf *bytes.Buffer, n, d int, pad byte) {
	tmp := make([]byte, n)
	j := n - 1
	for ; j >= 0 && d > 0; j-- {
		tmp[j] = digits[d%10]
		d /= 10
	}
	for ; j >= 0; j-- {
		tmp[j] = pad
	}
	buf.Write(tmp)
}
908065
func stringValToUint(val string) (uint, error) {
	parsedField, err := strconv.ParseUint(val, 10, 64)
	if err != nil {
		splitErr := strings.Split(err.Error(), ": ")
		errStr := splitErr[len(splitErr)-1]
		// return a value of zero and the error encountered during string parsing
		return 0, fmt.Errorf("Value '%v' cannot be parsed as an unsigned integer (%v)", val, errStr)
	}
	// return the unsigned integer equivalent of the input string and a nil error (indicating success)
	return uint(parsedField), nil
}
483835
func itos(i int64) string {
	s := strconv.FormatUint(uint64(i)+int64Adjust, 32)
	const z = "0000000000000"
	return z[len(s):] + s
}
477320
func StringToFloat64(from string) (float64, error) {
	val, err := strconv.ParseFloat(from, 64)
	return val, err
}
965090
func StringToInt32Slice(s, sep string) []int32 {
	if s == "" {
		return nil
	}

	fields := strings.Split(s, sep)
	slice := make([]int32, len(fields))
	for i, f := range fields {
		slice[i] = ToInt32(f)
	}
	return slice
}
208303
func (d *MyDecimal) ToString() (str []byte) {
	str = make([]byte, d.stringSize())
	digitsFrac := int(d.digitsFrac)
	wordStartIdx, digitsInt := d.removeLeadingZeros()
	if digitsInt+digitsFrac == 0 {
		digitsInt = 1
		wordStartIdx = 0
	}

	digitsIntLen := digitsInt
	if digitsIntLen == 0 {
		digitsIntLen = 1
	}
	digitsFracLen := digitsFrac
	length := digitsIntLen + digitsFracLen
	if d.negative {
		length++
	}
	if digitsFrac > 0 {
		length++
	}
	str = str[:length]
	strIdx := 0
	if d.negative {
		str[strIdx] = '-'
		strIdx++
	}
	var fill int
	if digitsFrac > 0 {
		fracIdx := strIdx + digitsIntLen
		fill = digitsFracLen - digitsFrac
		wordIdx := wordStartIdx + digitsToWords(digitsInt)
		str[fracIdx] = '.'
		fracIdx++
		for ; digitsFrac > 0; digitsFrac -= digitsPerWord {
			x := d.wordBuf[wordIdx]
			wordIdx++
			for i := myMin(digitsFrac, digitsPerWord); i > 0; i-- {
				y := x / digMask
				str[fracIdx] = byte(y) + '0'
				fracIdx++
				x -= y * digMask
				x *= 10
			}
		}
		for ; fill > 0; fill-- {
			str[fracIdx] = '0'
			fracIdx++
		}
	}
	fill = digitsIntLen - digitsInt
	if digitsInt == 0 {
		fill-- /* symbol 0 before digital point */
	}
	for ; fill > 0; fill-- {
		str[strIdx] = '0'
		strIdx++
	}
	if digitsInt > 0 {
		strIdx += digitsInt
		wordIdx := wordStartIdx + digitsToWords(digitsInt)
		for ; digitsInt > 0; digitsInt -= digitsPerWord {
			wordIdx--
			x := d.wordBuf[wordIdx]
			for i := myMin(digitsInt, digitsPerWord); i > 0; i-- {
				y := x / 10
				strIdx--
				str[strIdx] = '0' + byte(x-y*10)
				x = y
			}
		}
	} else {
		str[strIdx] = '0'
	}
	return
}
1219867
func floatStrToIntStr(validFloat string) (string, error) {
	var dotIdx = -1
	var eIdx = -1
	for i := 0; i < len(validFloat); i++ {
		switch validFloat[i] {
		case '.':
			dotIdx = i
		case 'e', 'E':
			eIdx = i
		}
	}
	if eIdx == -1 {
		if dotIdx == -1 {
			return validFloat, nil
		}
		return validFloat[:dotIdx], nil
	}
	var intCnt int
	digits := make([]byte, 0, len(validFloat))
	if dotIdx == -1 {
		digits = append(digits, validFloat[:eIdx]...)
		intCnt = len(digits)
	} else {
		digits = append(digits, validFloat[:dotIdx]...)
		intCnt = len(digits)
		digits = append(digits, validFloat[dotIdx+1:eIdx]...)
	}
	exp, err := strconv.Atoi(validFloat[eIdx+1:])
	if err != nil {
		return validFloat, errors.Trace(err)
	}
	if exp > 0 && intCnt > (math.MaxInt64-exp) {
		// (exp + incCnt) overflows MaxInt64.
		return validFloat, errors.Trace(ErrOverflow)
	}
	intCnt += exp
	if intCnt <= 0 {
		return "0", nil
	}
	if intCnt == 1 && (digits[0] == '-' || digits[0] == '+') {
		return "0", nil
	}
	var validInt string
	if intCnt <= len(digits) {
		validInt = string(digits[:intCnt])
	} else {
		extraZeroCount := intCnt - len(digits)
		if extraZeroCount > 20 {
			// Return overflow to avoid allocating too much memory.
			return validFloat, errors.Trace(ErrOverflow)
		}
		validInt = string(digits) + strings.Repeat("0", extraZeroCount)
	}
	return validInt, nil
}
772508
func float64ToInt(input float64) (output int) {
	r, _ := Round(input, 0)
	return int(r)
}