Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: floatStrToIntStr will failed in some case such as the param is +999.9999e2 #11376

Merged
merged 8 commits into from Jul 26, 2019
12 changes: 10 additions & 2 deletions types/convert.go
Expand Up @@ -369,13 +369,20 @@ func getValidIntPrefix(sc *stmtctx.StatementContext, str string) (string, error)
return floatStrToIntStr(sc, floatPrefix, str)
}

// roundIntStr is to round int string base on the number following dot.
// roundIntStr is to round a **valid int string** base on the number following dot.
func roundIntStr(numNextDot byte, intStr string) string {
if numNextDot < '5' {
return intStr
}
retStr := []byte(intStr)
for i := len(intStr) - 1; i >= 0; i-- {
if !isDigit(intStr[i]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here i must be 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but this a valid int string, so we needn't to check whether it is 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i must be 0, can we move if !isDigit(intStr[i]) logic to outside the for loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// because this is a valid int str,
// and intStr[i] is not digit, so intStr[i+1] must be a digit
retStr[i+1] = '1'
retStr = append(retStr, '0')
break
}
if retStr[i] != '9' {
retStr[i]++
break
Expand Down Expand Up @@ -432,6 +439,7 @@ func floatStrToIntStr(sc *stmtctx.StatementContext, validFloat string, oriStr st
}
return intStr, nil
}
// intCnt and digits contain the prefix `+/-` if validFloat[0] is `+/-`
var intCnt int
digits := make([]byte, 0, len(validFloat))
if dotIdx == -1 {
Expand All @@ -457,7 +465,7 @@ func floatStrToIntStr(sc *stmtctx.StatementContext, validFloat string, oriStr st
}
if intCnt <= 0 {
intStr = "0"
if intCnt == 0 && len(digits) > 0 {
if intCnt == 0 && len(digits) > 0 && isDigit(digits[0]) {
intStr = roundIntStr(digits[0], intStr)
}
return intStr, nil
Expand Down
1 change: 1 addition & 0 deletions types/convert_test.go
Expand Up @@ -734,6 +734,7 @@ func (s *testTypeConvertSuite) TestGetValidFloat(c *C) {
{".5", "1"},
{"123.456789e5", "12345679"},
{"123.456784e5", "12345678"},
{"+999.9999e2", "+100000"},
}
for _, t := range tests2 {
str, err := floatStrToIntStr(sc, t.origin, t.origin)
Expand Down