@@ -10,7 +10,6 @@ import java.text.DecimalFormatSymbols
10
10
import java.text.NumberFormat
11
11
import java.text.ParsePosition
12
12
import java.util.Locale
13
- import kotlin.reflect.KFunction1
14
13
15
14
private val logger = KotlinLogging .logger {}
16
15
@@ -74,13 +73,14 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
74
73
* its fallback char from [fallbackDecimalFormatSymbols] if it's safe to do so.
75
74
* [additionals] will be added to the set as well when they're safe to add.
76
75
*/
77
- fun KFunction1 < DecimalFormatSymbols , Char > .fromLocalWithFallBack (vararg additionals : Char ): Set <Char > =
76
+ fun (( DecimalFormatSymbols ) -> Char ) .fromLocalWithFallBack(vararg additionals : Char ): Set <Char > =
78
77
buildSet {
79
- val char = this @fromLocalWithFallBack(localDecimalFormatSymbols).lowercaseChar()
78
+ val getChar = this @fromLocalWithFallBack
79
+ val char = getChar(localDecimalFormatSymbols).lowercaseChar()
80
80
add(char)
81
81
82
82
// add fallback char if it's safe to do so
83
- val fallbackChar = this @fromLocalWithFallBack (fallbackDecimalFormatSymbols).lowercaseChar()
83
+ val fallbackChar = getChar (fallbackDecimalFormatSymbols).lowercaseChar()
84
84
if (fallbackChar !in localChars && ! localStrings.any { fallbackChar in it }) {
85
85
add(fallbackChar)
86
86
}
@@ -102,13 +102,14 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
102
102
* its fallback string from [fallbackDecimalFormatSymbols] if it's safe to do so.
103
103
* [additionals] will be added to the set as well when they're safe to add.
104
104
*/
105
- fun KFunction1 < DecimalFormatSymbols , String > .fromLocalWithFallBack (vararg additionals : String ): Set <String > =
105
+ fun (( DecimalFormatSymbols ) -> String ) .fromLocalWithFallBack(vararg additionals : String ): Set <String > =
106
106
buildSet {
107
- val string = this @fromLocalWithFallBack(localDecimalFormatSymbols).lowercase()
107
+ val getString = this @fromLocalWithFallBack
108
+ val string = getString(localDecimalFormatSymbols).lowercase()
108
109
add(string)
109
110
110
111
// add fallback string if it's safe to do so
111
- val fallbackString = this @fromLocalWithFallBack (fallbackDecimalFormatSymbols).lowercase()
112
+ val fallbackString = getString (fallbackDecimalFormatSymbols).lowercase()
112
113
if (! fallbackString.any { it in localChars } && fallbackString !in localStrings) {
113
114
add(fallbackString)
114
115
}
@@ -135,7 +136,7 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
135
136
.withNaN(DecimalFormatSymbols ::getNaN.fromLocalWithFallBack(" nan" , " na" , " n/a" ))
136
137
}
137
138
138
- // fallback method for parsing doubles
139
+ /* * Fallback method for parsing doubles. */
139
140
protected open fun String.parseToDoubleOrNullFallback (): Double? =
140
141
when (lowercase()) {
141
142
" inf" , " +inf" , " infinity" , " +infinity" , " infty" , " +infty" , " ∞" , " +∞" -> Double .POSITIVE_INFINITY
@@ -147,10 +148,8 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
147
148
else -> {
148
149
// not thread safe; must be created here
149
150
val numberFormat = NumberFormat .getInstance(locale)
150
-
151
151
val parsePosition = ParsePosition (0 )
152
152
val result = numberFormat.parse(this , parsePosition)?.toDouble()
153
-
154
153
if (parsePosition.index != this .length || parsePosition.errorIndex != - 1 ) {
155
154
null
156
155
} else {
@@ -163,10 +162,21 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
163
162
}
164
163
}
165
164
166
- public open fun parseOrNull (ba : ByteArray , charset : Charset = Charsets .UTF_8 ): Double? {
165
+ /* *
166
+ * Parses a double value from a substring of the specified byte array.
167
+ *
168
+ * It uses the [fast double parser][ConfigurableDoubleParser] if [ParserOptions.useFastDoubleParser] is enabled,
169
+ * else, or if that fails, it uses [parseToDoubleOrNullFallback].
170
+ */
171
+ public open fun parseOrNull (
172
+ ba : ByteArray ,
173
+ offset : Int = 0,
174
+ length : Int = ba.size,
175
+ charset : Charset = Charsets .UTF_8 ,
176
+ ): Double? {
167
177
if (parserOptions.useFastDoubleParser && charset in supportedFastCharsets) {
168
178
try {
169
- return parser.parseDouble(ba)
179
+ return parser.parseDouble(ba, offset, length )
170
180
} catch (e: Exception ) {
171
181
logger.debug(e) {
172
182
" Failed to parse '${
@@ -175,9 +185,16 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
175
185
}
176
186
}
177
187
}
178
- return ba.toString(charset).parseToDoubleOrNullFallback()
188
+ return String (bytes = ba, offset = offset, length = length, charset = charset)
189
+ .parseToDoubleOrNullFallback()
179
190
}
180
191
192
+ /* *
193
+ * Parses a double value from the specified [CharSequence].
194
+ *
195
+ * It uses the [fast double parser][ConfigurableDoubleParser] if [ParserOptions.useFastDoubleParser] is enabled,
196
+ * else, or if that fails, it uses [parseToDoubleOrNullFallback].
197
+ */
181
198
public open fun parseOrNull (cs : CharSequence ): Double? {
182
199
if (parserOptions.useFastDoubleParser) {
183
200
try {
@@ -192,10 +209,16 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
192
209
return cs.toString().parseToDoubleOrNullFallback()
193
210
}
194
211
195
- public open fun parseOrNull (ca : CharArray ): Double? {
212
+ /* *
213
+ * Parses a double value from the specified [CharArray].
214
+ *
215
+ * It uses the [fast double parser][ConfigurableDoubleParser] if [ParserOptions.useFastDoubleParser] is enabled,
216
+ * else, or if that fails, it uses [parseToDoubleOrNullFallback].
217
+ */
218
+ public open fun parseOrNull (ca : CharArray , offset : Int = 0, length : Int = ca.size): Double? {
196
219
if (parserOptions.useFastDoubleParser) {
197
220
try {
198
- return parser.parseDouble(ca)
221
+ return parser.parseDouble(ca, offset, length )
199
222
} catch (e: Exception ) {
200
223
logger.debug(e) {
201
224
" Failed to parse '${
@@ -204,7 +227,6 @@ public open class DoubleParser(private val parserOptions: ParserOptions) {
204
227
}
205
228
}
206
229
}
207
-
208
- return ca.joinToString(" " ).parseToDoubleOrNullFallback()
230
+ return String (chars = ca, offset = offset, length = length).parseToDoubleOrNullFallback()
209
231
}
210
232
}
0 commit comments