-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathStringNumberConversions.kt
203 lines (163 loc) · 5.6 KB
/
StringNumberConversions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
package kotlin.text
/**
* Parses the string as a signed [Byte] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toByteOrNull(): Byte? = toByteOrNull(radix = 10)
/**
* Parses the string as a signed [Byte] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toByteOrNull(radix: Int): Byte? {
val int = this.toIntOrNull(radix) ?: return null
if (int < Byte.MIN_VALUE || int > Byte.MAX_VALUE) return null
return int.toByte()
}
/**
* Parses the string as a [Short] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toShortOrNull(): Short? = toShortOrNull(radix = 10)
/**
* Parses the string as a [Short] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toShortOrNull(radix: Int): Short? {
val int = this.toIntOrNull(radix) ?: return null
if (int < Short.MIN_VALUE || int > Short.MAX_VALUE) return null
return int.toShort()
}
/**
* Parses the string as an [Int] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toIntOrNull(): Int? = toIntOrNull(radix = 10)
/**
* Parses the string as an [Int] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toIntOrNull(radix: Int): Int? {
checkRadix(radix)
val length = this.length
if (length == 0) return null
val start: Int
val isNegative: Boolean
val limit: Int
val firstChar = this[0]
if (firstChar < '0') { // Possible leading sign
if (length == 1) return null // non-digit (possible sign) only, no digits after
start = 1
if (firstChar == '-') {
isNegative = true
limit = Int.MIN_VALUE
} else if (firstChar == '+') {
isNegative = false
limit = -Int.MAX_VALUE
} else
return null
} else {
start = 0
isNegative = false
limit = -Int.MAX_VALUE
}
val limitForMaxRadix = (-Int.MAX_VALUE) / 36
var limitBeforeMul = limitForMaxRadix
var result = 0
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null
if (result < limitBeforeMul) {
if (limitBeforeMul == limitForMaxRadix) {
limitBeforeMul = limit / radix
if (result < limitBeforeMul) {
return null
}
} else {
return null
}
}
result *= radix
if (result < limit + digit) return null
result -= digit
}
return if (isNegative) result else -result
}
/**
* Parses the string as a [Long] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toLongOrNull(): Long? = toLongOrNull(radix = 10)
/**
* Parses the string as a [Long] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toLongOrNull(radix: Int): Long? {
checkRadix(radix)
val length = this.length
if (length == 0) return null
val start: Int
val isNegative: Boolean
val limit: Long
val firstChar = this[0]
if (firstChar < '0') { // Possible leading sign
if (length == 1) return null // non-digit (possible sign) only, no digits after
start = 1
if (firstChar == '-') {
isNegative = true
limit = Long.MIN_VALUE
} else if (firstChar == '+') {
isNegative = false
limit = -Long.MAX_VALUE
} else
return null
} else {
start = 0
isNegative = false
limit = -Long.MAX_VALUE
}
val limitForMaxRadix = (-Long.MAX_VALUE) / 36
var limitBeforeMul = limitForMaxRadix
var result = 0L
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null
if (result < limitBeforeMul) {
if (limitBeforeMul == limitForMaxRadix) {
limitBeforeMul = limit / radix
if (result < limitBeforeMul) {
return null
}
} else {
return null
}
}
result *= radix
if (result < limit + digit) return null
result -= digit
}
return if (isNegative) result else -result
}
internal fun numberFormatError(input: String): Nothing = throw NumberFormatException("Invalid number format: '$input'")