consumeNum consumes the next decimal number. 1st return value is the integer part. 2nd return value is whether a decimal part was encountered. 3rd return value is the decimal part as a float. If the number is negative, both the 1st and 3rd return value are negative. The decimal value is returned sep
()
| 39 | // The decimal value is returned separately from the integer value so |
| 40 | // as to support large integer values which would not fit in a float. |
| 41 | func (l *intervalLexer) consumeNum() (int64, bool, float64) { |
| 42 | if l.err != nil { |
| 43 | return 0, false, 0 |
| 44 | } |
| 45 | |
| 46 | offset := l.offset |
| 47 | |
| 48 | neg := false |
| 49 | if l.offset < len(l.str) && l.str[l.offset] == '-' { |
| 50 | // Remember a leading negative sign. We can't use "intPart < 0" |
| 51 | // below, because when the input syntax is "-0.xxxx" intPart is 0. |
| 52 | neg = true |
| 53 | } |
| 54 | |
| 55 | // Integer part before the decimal separator. |
| 56 | intPart := l.consumeInt() |
| 57 | |
| 58 | var decPart float64 |
| 59 | hasDecimal := false |
| 60 | if l.offset < len(l.str) && l.str[l.offset] == '.' { |
| 61 | hasDecimal = true |
| 62 | start := l.offset |
| 63 | |
| 64 | // Advance offset to prepare a valid argument to ParseFloat(). |
| 65 | l.offset++ |
| 66 | for ; l.offset < len(l.str) && l.str[l.offset] >= '0' && l.str[l.offset] <= '9'; l.offset++ { |
| 67 | } |
| 68 | // Try to convert. |
| 69 | value, err := strconv.ParseFloat(l.str[start:l.offset], 64) |
| 70 | if err != nil { |
| 71 | l.err = pgerror.Newf( |
| 72 | pgcode.InvalidDatetimeFormat, "interval: %v", err) |
| 73 | return 0, false, 0 |
| 74 | } |
| 75 | decPart = value |
| 76 | } |
| 77 | |
| 78 | // Ensure we have something. |
| 79 | if offset == l.offset { |
| 80 | l.err = pgerror.Newf( |
| 81 | pgcode.InvalidDatetimeFormat, "interval: missing number at position %d: %q", offset, l.str) |
| 82 | return 0, false, 0 |
| 83 | } |
| 84 | |
| 85 | if neg { |
| 86 | decPart = -decPart |
| 87 | } |
| 88 | return intPart, hasDecimal, decPart |
| 89 | } |
| 90 | |
| 91 | // Consumes the next integer. |
| 92 | func (l *intervalLexer) consumeInt() int64 { |
no test coverage detected