Skip to content

Commit

Permalink
types: optimize floatStrToIntStr (pingcap#2396)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Jan 5, 2017
1 parent ef7eac6 commit 39a7b90
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions util/types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ func floatStrToIntStr(validFloat string) (string, error) {
eIdx = i
}
}
if dotIdx == -1 && eIdx == -1 {
return validFloat, nil
if eIdx == -1 {
if dotIdx == -1 {
return validFloat, nil
}
return validFloat[:dotIdx], nil
}
var intCnt int
digits := make([]byte, 0, len(validFloat))
Expand All @@ -193,23 +196,17 @@ func floatStrToIntStr(validFloat string) (string, error) {
} else {
digits = append(digits, validFloat[:dotIdx]...)
intCnt = len(digits)
if eIdx == -1 {
digits = append(digits, validFloat[dotIdx+1:]...)
} else {
digits = append(digits, validFloat[dotIdx+1:eIdx]...)
}
digits = append(digits, validFloat[dotIdx+1:eIdx]...)
}
if eIdx != -1 {
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
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
}
Expand Down

0 comments on commit 39a7b90

Please sign in to comment.