MCPcopy Create free account
hub / github.com/BaseXdb/basex / toDouble

Method toDouble

basex-core/src/main/java/org/basex/util/Token.java:544–601  ·  view source on GitHub ↗

Converts the specified token into a double value. @param token token to be converted @return resulting double value, or Double#NaN is returned if the input is invalid

(final byte[] token)

Source from the content-addressed store, hash-verified

542 * @return resulting double value, or {@link Double#NaN} is returned if the input is invalid
543 */
544 public static double toDouble(final byte[] token) {
545 final int tl = token.length;
546 int s = -1;
547 while(++s < tl && ws(token[s]));
548 if(s == tl) return Double.NaN;
549
550 // check for integer value
551 int e = s;
552 boolean f = false;
553 for(int p = s; p < tl; ++p) {
554 final byte b = token[p];
555 if(e == s) {
556 if(digit(b) || b == '+') continue;
557 if(ws(b)) {
558 e = p + 1;
559 } else {
560 f = b == 'e' || b == 'E' || b == '.' || b == '-';
561 if(!f) return Double.NaN;
562 }
563 } else if(!ws(b)) {
564 return Double.NaN;
565 }
566 }
567 if(e == s) e = tl;
568 if(!f && e - s <= 9) {
569 final int d = toInt(token, s, e);
570 return d == Integer.MIN_VALUE ? Double.NaN : d;
571 }
572
573 // check if the value is a double
574 final int l = e - s;
575 if(l == 1) return Double.NaN;
576 final char[] str = new char[l];
577 int sd = 0, se = 0;
578 for(int p = 0; p < l; ++p) {
579 char b = (char) token[s + p];
580 if(b == 'e' || b == 'E') {
581 // 'e1', '1e', '1e1e1'
582 b = p == 0 || p + 1 == l || ++se > 1 ? 0 : 'e';
583 } else if(b == '-') {
584 // '1-', '1.-1', '1-1'
585 if(p > 0 && (p + 1 == l || str[p - 1] != 'e')) b = 0;
586 } else if(b == '.') {
587 // '1e.', '1.1.1',
588 if(p > 0 && str[p - 1] == 'e' || ++sd > 1) b = 0;
589 }
590 if(b == 0) return Double.NaN;
591 str[p] = b;
592 }
593
594 // parse as double value
595 try {
596 return Double.parseDouble(new String(str));
597 } catch(final NumberFormatException ex) {
598 Util.debug(ex);
599 return Double.NaN;
600 }
601 }

Callers 15

textDblMethod · 0.95
readDoubleMethod · 0.95
optMethod · 0.95
optMethod · 0.95
parseMethod · 0.95
cmpNumMethod · 0.95
acceptsMethod · 0.45
parseAnnotationsMethod · 0.45
qfMethod · 0.45
textDblMethod · 0.45
textMethod · 0.45
addMethod · 0.45

Calls 4

wsMethod · 0.95
digitMethod · 0.95
toIntMethod · 0.95
debugMethod · 0.95

Tested by

no test coverage detected