| 84 | } |
| 85 | |
| 86 | public static double parseDouble(CharSequence s, int start, int end) |
| 87 | { |
| 88 | //hack check for NaN at the start |
| 89 | if((end-start) == 3 && s.length() >= end && s.charAt(start) == 'N') |
| 90 | if(s.subSequence(start, end).toString().equals("NaN")) |
| 91 | return Double.NaN; |
| 92 | States state = States.SIGN; |
| 93 | int pos = start; |
| 94 | |
| 95 | int sign = 1; |
| 96 | long mantissa = 0; |
| 97 | /** |
| 98 | * Mantissa can only be incremented 18 times, then any more will |
| 99 | * overflow (2^63-1 ~= 9.2* 10^18 |
| 100 | */ |
| 101 | byte mantisaIncrements = 0; |
| 102 | int implicitExponent = 0; |
| 103 | //used for (val)e(val) case |
| 104 | int expoSign = 1; |
| 105 | int explicitExponent = 0; |
| 106 | |
| 107 | while(pos < end)//run the state machine |
| 108 | { |
| 109 | char c = s.charAt(pos); |
| 110 | switch(state) |
| 111 | { |
| 112 | case SIGN: |
| 113 | if (c == '-') |
| 114 | { |
| 115 | sign = -1; |
| 116 | pos++; |
| 117 | } |
| 118 | else if(c == '+') |
| 119 | pos++; |
| 120 | else if (!Character.isDigit(c))//not a '-', '+', or digit, so error |
| 121 | throw new NumberFormatException(); |
| 122 | state = States.LEADING_ZEROS_MANTISSA; |
| 123 | continue; |
| 124 | case LEADING_ZEROS_MANTISSA: |
| 125 | if(c == '0') |
| 126 | pos++; |
| 127 | else if(c == '.') |
| 128 | { |
| 129 | pos++; |
| 130 | state = States.LEADING_ZEROS_FRAC; |
| 131 | } |
| 132 | else if (Character.isDigit(c)) |
| 133 | state = States.MANTISSA_INT_PART; |
| 134 | else if(c == 'e' || c == 'E')//could be something like +0e0 |
| 135 | state = States.MANTISSA_FRAC_PART;//this is where that case is handeled |
| 136 | else |
| 137 | throw new NumberFormatException(); |
| 138 | continue; |
| 139 | case LEADING_ZEROS_FRAC: |
| 140 | if(c == '0') |
| 141 | { |
| 142 | pos++; |
| 143 | implicitExponent--; |