()
| 106 | } |
| 107 | |
| 108 | func (dec *Decoder) getFloat() (float64, error) { |
| 109 | var end = dec.cursor |
| 110 | var start = dec.cursor |
| 111 | // look for following numbers |
| 112 | for j := dec.cursor + 1; j < dec.length || dec.read(); j++ { |
| 113 | switch dec.data[j] { |
| 114 | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 115 | end = j |
| 116 | continue |
| 117 | case '.': |
| 118 | // we get part before decimal as integer |
| 119 | beforeDecimal := dec.atoi64(start, end) |
| 120 | // then we get part after decimal as integer |
| 121 | start = j + 1 |
| 122 | // get number after the decimal point |
| 123 | for i := j + 1; i < dec.length || dec.read(); i++ { |
| 124 | c := dec.data[i] |
| 125 | if isDigit(c) { |
| 126 | end = i |
| 127 | // multiply the before decimal point portion by 10 using bitwise |
| 128 | // make sure it doesn't overflow |
| 129 | if end-start < 18 { |
| 130 | beforeDecimal = (beforeDecimal << 3) + (beforeDecimal << 1) |
| 131 | } |
| 132 | continue |
| 133 | } else if (c == 'e' || c == 'E') && j < i-1 { |
| 134 | // we have an exponent, convert first the value we got before the exponent |
| 135 | var afterDecimal int64 |
| 136 | expI := end - start + 2 |
| 137 | // if exp is too long, it means number is too long, just truncate the number |
| 138 | if expI >= len(pow10uint64) || expI < 0 { |
| 139 | expI = len(pow10uint64) - 2 |
| 140 | afterDecimal = dec.atoi64(start, start+expI-2) |
| 141 | } else { |
| 142 | // then we add both integers |
| 143 | // then we divide the number by the power found |
| 144 | afterDecimal = dec.atoi64(start, end) |
| 145 | } |
| 146 | dec.cursor = i + 1 |
| 147 | pow := pow10uint64[expI] |
| 148 | floatVal := float64(beforeDecimal+afterDecimal) / float64(pow) |
| 149 | exp, err := dec.getExponent() |
| 150 | if err != nil { |
| 151 | return 0, err |
| 152 | } |
| 153 | pExp := (exp + (exp >> 31)) ^ (exp >> 31) + 1 // absolute exponent |
| 154 | if pExp >= int64(len(pow10uint64)) || pExp < 0 { |
| 155 | return 0, dec.raiseInvalidJSONErr(dec.cursor) |
| 156 | } |
| 157 | // if exponent is negative |
| 158 | if exp < 0 { |
| 159 | return float64(floatVal) * (1 / float64(pow10uint64[pExp])), nil |
| 160 | } |
| 161 | return float64(floatVal) * float64(pow10uint64[pExp]), nil |
| 162 | } |
| 163 | dec.cursor = i |
| 164 | break |
| 165 | } |
no test coverage detected