mirror of
https://github.com/apple/pkl.git
synced 2026-09-13 13:21:47 +02:00
Add hex/binary/octal support to String.toInt() (#1808)
This commit is contained in:
@@ -263,18 +263,80 @@ examples {
|
||||
str1.codePoints
|
||||
}
|
||||
|
||||
["toInt()"] {
|
||||
["toInt() decimal"] {
|
||||
"123".toInt()
|
||||
"-123".toInt()
|
||||
"- 123".toInt()
|
||||
"+123".toInt()
|
||||
"+ 123".toInt()
|
||||
"1_2__3___".toInt()
|
||||
"-1_2__3___".toInt()
|
||||
"+1_2__3___".toInt()
|
||||
"0".toInt()
|
||||
"-0".toInt()
|
||||
"+0".toInt()
|
||||
"-9223372036854775808".toInt() // math.minInt
|
||||
"9223372036854775807".toInt() // math.maxInt
|
||||
}
|
||||
|
||||
["toInt() hex"] {
|
||||
"0x123".toInt()
|
||||
"0X123".toInt()
|
||||
"-0x123".toInt()
|
||||
"- 0x123".toInt()
|
||||
"+0x123".toInt()
|
||||
"+ 0x123".toInt()
|
||||
"0x1_2__3___".toInt()
|
||||
"-0x1_2__3___".toInt()
|
||||
"+0x1_2__3___".toInt()
|
||||
"0x0".toInt()
|
||||
"-0x0".toInt()
|
||||
"+0x0".toInt()
|
||||
"0x1E_2".toInt() // test underscore removal after 'E' succeeds for toInt (fails for toFloat)
|
||||
}
|
||||
|
||||
["toInt() binary"] {
|
||||
"0b101".toInt()
|
||||
"0B101".toInt()
|
||||
"-0b101".toInt()
|
||||
"- 0b101".toInt()
|
||||
"+0b101".toInt()
|
||||
"+ 0b101".toInt()
|
||||
"0b1_0__1___".toInt()
|
||||
"-0b1_0__1___".toInt()
|
||||
"+0b1_0__1___".toInt()
|
||||
"0b0".toInt()
|
||||
"-0b0".toInt()
|
||||
"+0b0".toInt()
|
||||
}
|
||||
|
||||
["toInt() octal"] {
|
||||
"0o123".toInt()
|
||||
"0O123".toInt()
|
||||
"-0o123".toInt()
|
||||
"- 0o123".toInt()
|
||||
"+0o123".toInt()
|
||||
"+ 0o123".toInt()
|
||||
"0o1_2__3___".toInt()
|
||||
"-0o1_2__3___".toInt()
|
||||
"+0o1_2__3___".toInt()
|
||||
"0o0".toInt()
|
||||
"-0o0".toInt()
|
||||
"+0o0".toInt()
|
||||
}
|
||||
|
||||
["toInt() error cases"] {
|
||||
module.catch(() -> "1.2".toInt())
|
||||
module.catch(() -> "9223372036854775808".toInt())
|
||||
module.catch(() -> "-9223372036854775809".toInt())
|
||||
module.catch(() -> "abc".toInt())
|
||||
module.catch(() -> "_1_000".toInt())
|
||||
module.catch(() -> "0p0".toInt())
|
||||
module.catch(() -> "0x-0".toInt())
|
||||
module.catch(() -> "0x+0".toInt())
|
||||
module.catch(() -> "0x0 + 0x1".toInt())
|
||||
module.catch(() -> "".toInt())
|
||||
module.catch(() -> "0o123e4".toInt())
|
||||
}
|
||||
|
||||
["toIntOrNull()"] {
|
||||
@@ -289,6 +351,11 @@ examples {
|
||||
"-9223372036854775809".toIntOrNull()
|
||||
"abc".toIntOrNull()
|
||||
"_1_2__3___".toIntOrNull()
|
||||
"0p0".toIntOrNull()
|
||||
"0x-0".toIntOrNull()
|
||||
"0x+0".toIntOrNull()
|
||||
"0x0 + 0x1".toIntOrNull()
|
||||
"".toIntOrNull()
|
||||
}
|
||||
|
||||
["toFloat()"] {
|
||||
|
||||
+32
-8
@@ -2255,9 +2255,15 @@ alias {
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Parses this string as a signed decimal (base 10) integer.
|
||||
Parses this string as a signed integer.
|
||||
|
||||
Throws if this string cannot be parsed as a signed decimal integer,
|
||||
Supports integer formats supported by Pkl:
|
||||
* Decimal (base 10)
|
||||
* Hexadecimal (base 16) with prefix `0x`
|
||||
* Binary (base 2) with prefix `0b`
|
||||
* Octal (base 8) with prefix `0o`
|
||||
|
||||
Throws if this string cannot be parsed,
|
||||
or if the integer is too large to fit into [Int].
|
||||
"""
|
||||
annotations = List()
|
||||
@@ -2272,9 +2278,15 @@ alias {
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Parses this string as a signed decimal (base 10) integer.
|
||||
Parses this string as a signed integer.
|
||||
|
||||
Returns [null] if this string cannot be parsed as a signed decimal integer,
|
||||
Supports integer formats supported by Pkl:
|
||||
* Decimal (base 10)
|
||||
* Hexadecimal (base 16) with prefix `0x`
|
||||
* Binary (base 2) with prefix `0b`
|
||||
* Octal (base 8) with prefix `0o`
|
||||
|
||||
Returns [null] if this string cannot be parsed,
|
||||
or if the integer is too large to fit into [Int].
|
||||
"""
|
||||
annotations = List()
|
||||
@@ -3192,9 +3204,15 @@ alias {
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Parses this string as a signed decimal (base 10) integer.
|
||||
Parses this string as a signed integer.
|
||||
|
||||
Throws if this string cannot be parsed as a signed decimal integer,
|
||||
Supports integer formats supported by Pkl:
|
||||
* Decimal (base 10)
|
||||
* Hexadecimal (base 16) with prefix `0x`
|
||||
* Binary (base 2) with prefix `0b`
|
||||
* Octal (base 8) with prefix `0o`
|
||||
|
||||
Throws if this string cannot be parsed,
|
||||
or if the integer is too large to fit into [Int].
|
||||
"""
|
||||
annotations = List()
|
||||
@@ -3209,9 +3227,15 @@ alias {
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Parses this string as a signed decimal (base 10) integer.
|
||||
Parses this string as a signed integer.
|
||||
|
||||
Returns [null] if this string cannot be parsed as a signed decimal integer,
|
||||
Supports integer formats supported by Pkl:
|
||||
* Decimal (base 10)
|
||||
* Hexadecimal (base 16) with prefix `0x`
|
||||
* Binary (base 2) with prefix `0b`
|
||||
* Octal (base 8) with prefix `0o`
|
||||
|
||||
Returns [null] if this string cannot be parsed,
|
||||
or if the integer is too large to fit into [Int].
|
||||
"""
|
||||
annotations = List()
|
||||
|
||||
@@ -213,18 +213,76 @@ examples {
|
||||
List()
|
||||
List(97, 98, 99, 100, 101, 102, 103)
|
||||
}
|
||||
["toInt()"] {
|
||||
["toInt() decimal"] {
|
||||
123
|
||||
-123
|
||||
-123
|
||||
123
|
||||
123
|
||||
123
|
||||
-123
|
||||
123
|
||||
-123
|
||||
0
|
||||
0
|
||||
0
|
||||
-9223372036854775808
|
||||
9223372036854775807
|
||||
}
|
||||
["toInt() hex"] {
|
||||
291
|
||||
291
|
||||
-291
|
||||
-291
|
||||
291
|
||||
291
|
||||
291
|
||||
-291
|
||||
291
|
||||
0
|
||||
0
|
||||
0
|
||||
482
|
||||
}
|
||||
["toInt() binary"] {
|
||||
5
|
||||
5
|
||||
-5
|
||||
-5
|
||||
5
|
||||
5
|
||||
5
|
||||
-5
|
||||
5
|
||||
0
|
||||
0
|
||||
0
|
||||
}
|
||||
["toInt() octal"] {
|
||||
83
|
||||
83
|
||||
-83
|
||||
-83
|
||||
83
|
||||
83
|
||||
83
|
||||
-83
|
||||
83
|
||||
0
|
||||
0
|
||||
0
|
||||
}
|
||||
["toInt() error cases"] {
|
||||
"Cannot parse string as `Int`. String: \"1.2\""
|
||||
"Cannot parse string as `Int`. String: \"9223372036854775808\""
|
||||
"Cannot parse string as `Int`. String: \"-9223372036854775809\""
|
||||
"Cannot parse string as `Int`. String: \"abc\""
|
||||
"Cannot parse string as `Int`. String: \"_1_000\""
|
||||
"Cannot parse string as `Int`. String: \"0p0\""
|
||||
"Cannot parse string as `Int`. String: \"0x-0\""
|
||||
"Cannot parse string as `Int`. String: \"0x+0\""
|
||||
"Cannot parse string as `Int`. String: \"0x0 + 0x1\""
|
||||
"Cannot parse string as `Int`. String: \"\""
|
||||
"Cannot parse string as `Int`. String: \"0o123e4\""
|
||||
}
|
||||
["toIntOrNull()"] {
|
||||
123
|
||||
@@ -238,6 +296,11 @@ examples {
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
}
|
||||
["toFloat()"] {
|
||||
0.0
|
||||
|
||||
Reference in New Issue
Block a user