You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Init/Data/String/Slice.lean
+79Lines changed: 79 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1339,6 +1339,85 @@ Examples:
1339
1339
deffront (s : Slice) : Char :=
1340
1340
s.front?.getD default
1341
1341
1342
+
/--
1343
+
Checks whether the slice can be interpreted as the decimal representation of an integer.
1344
+
1345
+
A slice can be interpreted as a decimal integer if it only consists of at least one decimal digit
1346
+
and optionally {lit}`-` in front. Leading {lit}`+` characters are not allowed.
1347
+
1348
+
Use {name (scope := "Init.Data.String.Slice")}`String.Slice.toInt?` or {name (scope := "Init.Data.String.Slice")}`String.toInt!` to convert such a string to an integer.
1349
+
1350
+
Examples:
1351
+
* {lean}`"".toSlice.isInt = false`
1352
+
* {lean}`"-".toSlice.isInt = false`
1353
+
* {lean}`"0".toSlice.isInt = true`
1354
+
* {lean}`"-0".toSlice.isInt = true`
1355
+
* {lean}`"5".toSlice.isInt = true`
1356
+
* {lean}`"587".toSlice.isInt = true`
1357
+
* {lean}`"-587".toSlice.isInt = true`
1358
+
* {lean}`"+587".toSlice.isInt = false`
1359
+
* {lean}`" 5".toSlice.isInt = false`
1360
+
* {lean}`"2-3".toSlice.isInt = false`
1361
+
* {lean}`"0xff".toSlice.isInt = false`
1362
+
-/
1363
+
defisInt (s : Slice) : Bool :=
1364
+
if s.front = '-'then
1365
+
(s.drop 1).isNat
1366
+
else
1367
+
s.isNat
1368
+
1369
+
/--
1370
+
Interprets a slice as the decimal representation of an integer, returning it. Returns {lean}`none` if
1371
+
the string does not contain a decimal integer.
1372
+
1373
+
A string can be interpreted as a decimal integer if it only consists of at least one decimal digit
1374
+
and optionally {lit}`-` in front. Leading {lit}`+` characters are not allowed.
1375
+
1376
+
Use {name}`Slice.isInt` to check whether {name}`Slice.toInt?` would return {lean}`some`.
1377
+
{name (scope := "Init.Data.String.Slice")}`Slice.toInt!` is an alternative that panics instead of
1378
+
returning {lean}`none` when the string is not an integer.
1379
+
1380
+
Examples:
1381
+
* {lean}`"".toSlice.toInt? = none`
1382
+
* {lean}`"-".toSlice.toInt? = none`
1383
+
* {lean}`"0".toSlice.toInt? = some 0`
1384
+
* {lean}`"5".toSlice.toInt? = some 5`
1385
+
* {lean}`"-5".toSlice.toInt? = some (-5)`
1386
+
* {lean}`"587".toSlice.toInt? = some 587`
1387
+
* {lean}`"-587".toSlice.toInt? = some (-587)`
1388
+
* {lean}`" 5".toSlice.toInt? = none`
1389
+
* {lean}`"2-3".toSlice.toInt? = none`
1390
+
* {lean}`"0xff".toSlice.toInt? = none`
1391
+
-/
1392
+
deftoInt? (s : Slice) : Option Int :=
1393
+
if s.front = '-'then
1394
+
Int.negOfNat <$> (s.drop 1).toNat?
1395
+
else
1396
+
Int.ofNat <$> s.toNat?
1397
+
1398
+
/--
1399
+
Interprets a string as the decimal representation of an integer, returning it. Panics if the string
1400
+
does not contain a decimal integer.
1401
+
1402
+
A string can be interpreted as a decimal integer if it only consists of at least one decimal digit
1403
+
and optionally {lit}`-` in front. Leading `+` characters are not allowed.
1404
+
1405
+
Use {name}`Slice.isInt` to check whether {name}`Slice.toInt!` would return a value.
1406
+
{name}`Slice.toInt?` is a safer alternative that returns {lean}`none` instead of panicking when the
1407
+
string is not an integer.
1408
+
1409
+
Examples:
1410
+
* {lean}`"0".toSlice.toInt! = 0`
1411
+
* {lean}`"5".toSlice.toInt! = 5`
1412
+
* {lean}`"587".toSlice.toInt! = 587`
1413
+
* {lean}`"-587".toSlice.toInt! = -587`
1414
+
-/
1415
+
@[inline]
1416
+
deftoInt! (s : Slice) : Int :=
1417
+
match s.toInt? with
1418
+
| some v => v
1419
+
| none => panic "Int expected"
1420
+
1342
1421
/--
1343
1422
Returns the last character in {name}`s`. If {name}`s` is empty, returns {name}`none`.
0 commit comments