📄 doubleparser.java
字号:
/* libaegisvm - The Aegis Virtual Machine for executing Java bytecode Copyright (C) 2001-2002 Philip W. L. Fong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*///// Taken from Kore 0.0.7//package aegis;public final class DoubleParser { private char[] chars; private int length; private int index; private int parseUnsignedInt() throws NumberFormatException { if (index >= length) throw new NumberFormatException(); int start = index; int value = 0; for ( ; index < length; index++) { int d = Character.digit(chars[index], 10); if (d < 0) break; value *= 10; value += d; } if (index == start) throw new NumberFormatException(); return value; } private int parseSignedInt() throws NumberFormatException { if (index >= length) throw new NumberFormatException(); char sign = '+'; switch (chars[index]) { case '-': sign = '-'; index++; break; case '+': sign = '+'; index++; break; } int value = parseUnsignedInt(); return (sign == '-') ? -value : value; } private double parseIntegerPart() throws NumberFormatException { if (index >= length) throw new NumberFormatException(); double value = 0.0; for ( ; index < length; index++) { int d = Character.digit(chars[index], 10); if (d < 0) break; value *= 10; value += d; } return value; } private double parseFractionalPart(boolean nonEmpty) throws NumberFormatException { if (index >= length) throw new NumberFormatException(); int start = index; double value = 0.0; int c = 0; for ( ; index < length; index++) { int d = Character.digit(chars[index], 10); if (d < 0) break; c++; if (c < negexp.length) value += negexp[c] * d; } if (nonEmpty && (index == start)) throw new NumberFormatException(); return value; } private static final double negexp[] = { 1e-0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15, 1e-16, 1e-17, 1e-18, 1e-19, 1e-20, 1e-21, 1e-22, 1e-23, 1e-24, 1e-25, 1e-26, 1e-27, 1e-28, 1e-29 }; private double parseExponent(double value) throws NumberFormatException { if (index >= chars.length) return value; switch (chars[index]) { case 'e': case 'E': index++; break; default: throw new NumberFormatException(); } int exponent = parseSignedInt(); if (index < chars.length) throw new NumberFormatException(); if (exponent >= 0) { for (int i = 0; i < exponent; i++) value *= 10.0; return value; } else if (exponent > -negexp.length) { return value * negexp[-exponent]; } else { for (int i = 0; i > exponent; i--) value /= 10.0; return value; } } public double parse() throws NumberFormatException { if (index >= chars.length) throw new NumberFormatException(); char sign = '+'; switch (chars[index]) { case '-': sign = '-'; index++; break; case '+': sign = '+'; index++; break; } if (index >= chars.length) throw new NumberFormatException(); double value; if (chars[index] == '.') { index++; value = parseFractionalPart(true); value = parseExponent(value); } else { value = parseIntegerPart(); if (index >= chars.length) throw new NumberFormatException(); if (chars[index] != '.') { throw new NumberFormatException(); } index++; value += parseFractionalPart(false); value = parseExponent(value); } return (sign == '-') ? -value : value; } public DoubleParser(String s) { chars = s.toCharArray(); length = chars.length; index = 0; } public static double parseDouble(String s) throws NumberFormatException { DoubleParser p = new DoubleParser(s); return p.parse(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -