@@ -16,7 +16,7 @@ import java.math.BigInteger
16
16
// get() = this >= '0' && this <= '9'
17
17
18
18
fun String.isInt (isNegative : Boolean = false): Boolean {
19
- if (! isNegative && this [ 0 ] == ' - ' ) return substring(1 ).isInt(true )
19
+ if (! isNegative && ' - ' == this [ 0 ] ) return substring(1 ).isInt(true )
20
20
return isNotEmpty() && fold(true , { res, char ->
21
21
res && char.isDigit()
22
22
})
@@ -32,43 +32,55 @@ fun Char.safeLower() =
32
32
}
33
33
34
34
fun String.isHexInt (isNegative : Boolean = false): Boolean {
35
- if (! isNegative && this [ 0 ] == ' - ' ) return substring(1 ).isHexInt(true )
35
+ if (! isNegative && ' - ' == this [ 0 ] ) return substring(1 ).isHexInt(true )
36
36
return when {
37
37
length <= 2 -> false
38
- this [ 0 ] != ' 0 ' || this [1 ].safeLower() != ' x ' -> false
38
+ ' 0 ' != this [ 0 ] || ' x ' != this [1 ].safeLower() -> false
39
39
else -> (2 .. length - 1 )
40
40
.map { this [it].toLowerCase() }
41
- .none { ! it.isDigit() && ( it < ' a' || it > ' f' ) }
41
+ .all { it.isDigit() || it in ' a' .. ' f' }
42
42
}
43
43
}
44
44
45
45
fun String.isBigInt (isNegative : Boolean = false): Boolean {
46
- if (! isNegative && this [ 0 ] == ' - ' ) return substring(1 ).isBigInt(true )
46
+ if (! isNegative && ' - ' == this [ 0 ] ) return substring(1 ).isBigInt(true )
47
47
return when {
48
48
length <= 1 -> false
49
- this [length - 1 ].safeLower() != ' n ' -> false
49
+ ' n ' != this [length - 1 ].safeLower() -> false
50
50
else -> {
51
51
val a = substring(0 .. length - 2 )
52
52
a.isInt() || a.isHexInt() || a.isBinInt() || a.isOctInt()
53
53
}
54
54
}
55
55
}
56
56
57
+ fun String.isBigDec (isNegative : Boolean = false): Boolean {
58
+ if (! isNegative && ' -' == this [0 ]) return substring(1 ).isBigDec(true )
59
+ return when {
60
+ length <= 2 -> false
61
+ ' m' != this [length - 1 ].safeLower() -> false
62
+ else -> {
63
+ val a = substring(0 .. length - 2 )
64
+ 1 >= a.count { ' .' == it } && a.all { ' .' == it || it.isDigit() }
65
+ }
66
+ }
67
+ }
68
+
57
69
fun String.isBinInt (isNegative : Boolean = false): Boolean {
58
70
if (! isNegative && this [0 ] == ' -' ) return substring(1 ).isBinInt(true )
59
71
return when {
60
72
length <= 2 -> false
61
- this [ 0 ] != ' 0 ' || this [1 ].safeLower() != ' b ' -> false
62
- else -> (2 .. length - 1 ).none { this [it] != ' 0' && this [it] != ' 1' }
73
+ ' 0 ' != this [ 0 ] || ' b ' != this [1 ].safeLower() -> false
74
+ else -> (2 .. length - 1 ).none { ' 0' != this [it] && ' 1' != this [it] }
63
75
}
64
76
}
65
77
66
78
fun String.isOctInt (isNegative : Boolean = false): Boolean {
67
- if (! isNegative && this [ 0 ] == ' - ' ) return substring(1 ).isOctInt(true )
79
+ if (! isNegative && ' - ' == this [ 0 ] ) return substring(1 ).isOctInt(true )
68
80
return when {
69
81
length <= 1 -> false
70
- this [ 0 ] != ' 0 ' -> false
71
- else -> (1 .. length - 1 ).none { ! this [it].isOctalInt() }
82
+ ' 0 ' != this [ 0 ] -> false
83
+ else -> (1 .. length - 1 ).all { this [it].isOctalInt() }
72
84
}
73
85
}
74
86
@@ -79,19 +91,17 @@ fun String.toHexInt(): Int {
79
91
ret = ret shl 4
80
92
val char = this [it].safeLower()
81
93
if (char.isDigit()) ret + = (char - ' 0' )
82
- else /* if (char >= 'a' && char <= 'f') */ ret + = (char - ' a' + 10 )
83
- // ret *= 16
94
+ else ret + = (char - ' a' + 10 )
84
95
}
85
96
return ret
86
97
}
87
98
88
99
fun String.toBinInt (): Int {
89
- if (this [ 0 ] == ' - ' ) return - substring(1 ).toBinInt()
100
+ if (' - ' == this [ 0 ] ) return - substring(1 ).toBinInt()
90
101
var ret = 0
91
102
(2 .. length - 1 ).forEach {
92
103
ret = ret shl 1
93
- if (this [it] == ' 1' ) ++ ret
94
- // ret *= 2
104
+ if (' 1' == this [it]) ++ ret
95
105
}
96
106
return ret
97
107
}
@@ -113,7 +123,6 @@ fun String.toOctInt(): Int {
113
123
(1 .. length - 1 ).forEach {
114
124
ret = ret shl 3
115
125
ret + = this [it] - ' 0'
116
- // ret *= 8
117
126
}
118
127
return ret
119
128
}
0 commit comments