Debian Codegraph @

net/sf/saxon/type/StringToDouble.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.type;
9
10 import net.sf.saxon.trans.Err;
11 import net.sf.saxon.value.DoubleValue;
12 import net.sf.saxon.value.Whitespace;
13
14 /**
15 * This class converts a string to an xs:double according to the rules in XML Schema 1.0
16 */
17 public class StringToDouble extends StringConverter {
18
19 private static StringToDouble THE_INSTANCE = new StringToDouble();
20
21 /**
22 * Get the singleton instance
23 *
24 * @return the singleton instance of this class
25 */
26
27 public static StringToDouble getInstance() {
28 return THE_INSTANCE;
29 }
30
31 protected StringToDouble() {
32 }
33
34 /**
35 * Convert a string to a double.
36 *
37 * @param s the String to be converted
38 * @return a double representing the value of the String
39 * @throws NumberFormatException if the value cannot be converted
40 */
41
42 public double stringToNumber(CharSequence s) throws NumberFormatException {
43 // first try to parse simple numbers by hand (it's cheaper)
44 int len = s.length();
45 boolean containsDisallowedChars = false;
46 boolean containsWhitespace = false;
47 if (len < 9) {
48 boolean useJava = false;
49 long num = 0;
50 int dot = -1;
51 int lastDigit = -1;
52 boolean onlySpaceAllowed = false;
53 loop:
54 for (int i = 0; i < len; i++) {
55 char c = s.charAt(i);
56 switch (c) {
57 case ' ':
58 case '\n':
59 case '\t':
60 case '\r':
61 containsWhitespace = true;
62 if (lastDigit != -1) {
63 onlySpaceAllowed = true;
64 }
65 break;
66 case '0':
67 case '1':
68 case '2':
69 case '3':
70 case '4':
71 case '5':
72 case '6':
73 case '7':
74 case '8':
75 case '9':
76 if (onlySpaceAllowed) {
77 throw new NumberFormatException("Numeric value contains embedded whitespace");
78 }
79 lastDigit = i;
80 num = num * 10 + (c - '0');
81 break;
82 case '.':
83 if (onlySpaceAllowed) {
84 throw new NumberFormatException("Numeric value contains embedded whitespace");
85 }
86 if (dot != -1) {
87 throw new NumberFormatException("Only one decimal point allowed");
88 }
89 dot = i;
90 break;
91 case 'x':
92 case 'X':
93 case 'f':
94 case 'F':
95 case 'd':
96 case 'D':
97 case 'n':
98 case 'N':
99 containsDisallowedChars = true;
100 useJava = true;
101 break loop;
102 default:
103 // there's something like a sign or an exponent: take the slow train instead
104 // But keep going to look for disallowed characters - bug 3495
105 useJava = true;
106 break;
107 }
108 }
109 if (!useJava) {
110 if (lastDigit == -1) {
111 throw new NumberFormatException("String to double conversion: no digits found");
112 } else if (dot == -1 || dot > lastDigit) {
113 return (double) num;
114 } else {
115 int afterPoint = lastDigit - dot;
116 return (double) num / powers[afterPoint];
117 }
118 }
119 } else {
120 loop2:
121 for (int i = 0; i < len; i++) {
122 char c = s.charAt(i);
123 switch (c) {
124 case ' ':
125 case '\n':
126 case '\t':
127 case '\r':
128 containsWhitespace = true;
129 break;
130 case '0':
131 case '1':
132 case '2':
133 case '3':
134 case '4':
135 case '5':
136 case '6':
137 case '7':
138 case '8':
139 case '9':
140 case '.':
141 case 'e':
142 case 'E':
143 case '+':
144 case '-':
145 break;
146 default:
147 containsDisallowedChars = true;
148 break loop2;
149 }
150 }
151 }
152 String n = containsWhitespace ? Whitespace.trimWhitespace(s).toString() : s.toString();
153 if ("INF".equals(n)) {
154 return Double.POSITIVE_INFINITY;
155 } else if ("+INF".equals(n)) {
156 // Allowed in XSD 1.1 but not in XSD 1.0
157 return signedPositiveInfinity();
158 } else if ("-INF".equals(n)) {
159 return Double.NEGATIVE_INFINITY;
160 } else if ("NaN".equals(n)) {
161 return Double.NaN;
162 } else {
163 // reject strings containing characters such as (x, f, d) allowed in Java but not in XPath,
164 // and other representations of NaN and Infinity such as 'Infinity'
165 if (containsDisallowedChars) {
166 throw new NumberFormatException("invalid floating point value: " + s);
167 }
168 return Double.parseDouble(n);
169 }
170 }
171
172 protected double signedPositiveInfinity() {
173 throw new NumberFormatException("the float/double value '+INF' is not allowed under XSD 1.0");
174 }
175
176 /*@NotNull*/ private static double[] powers = new double[]{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
177
178 /**
179 * Convert a string to the target type of this converter.
180 *
181 * @param input the string to be converted
182 * @return either an {@link net.sf.saxon.value.AtomicValue} of the appropriate type for this converter (if conversion
183 * succeeded), or a {@link net.sf.saxon.type.ValidationFailure} if conversion failed.
184 */
185 @Override
186 public ConversionResult convertString(CharSequence input) {
187 try {
188 double d = stringToNumber(input);
189 return new DoubleValue(d);
190 } catch (NumberFormatException e) {
191 return new ValidationFailure("Cannot convert string " + Err.wrap(input, Err.VALUE) + " to double");
192 }
193 }
194
195 }
196