Commit 6cbcbce
feat: support underscores in String.toNat? and String.toInt? (#11541)
This PR adds support for underscores as digit separators in
String.toNat?, String.toInt?, and related parsing functions. This makes
the string parsing functions consistent with Lean's numeric literal
syntax, which already supports underscores for readability (e.g.,
100_000_000).
The implementation validates that underscores:
- Cannot appear at the start or end of the number
- Cannot appear consecutively
- Are ignored when calculating the numeric value
This resolves a common source of friction when parsing user input from
command-line arguments, environment variables, or configuration files,
where users naturally expect to use the same numeric syntax they use in
source code.
## Examples
Before:
```lean
#eval "100_000_000".toNat? -- none
```
After:
```lean
#eval "100_000_000".toNat? -- some 100000000
#eval "1_000".toInt? -- some 1000
#eval "-1_000_000".toInt? -- some (-1000000)
```
## Testing
Added comprehensive tests in
`tests/lean/run/string_toNat_underscores.lean` covering:
- Basic underscore support
- Edge cases (leading/trailing/consecutive underscores)
- Both `toNat?` and `toInt?` functions
- String, Slice, and Substring types
All existing tests continue to pass.
Closes #11538
🤖 Prepared with Claude Code
---------
Co-authored-by: Claude Sonnet 4.5 <[email protected]>1 parent cdb994b commit 6cbcbce
File tree
3 files changed
+134
-10
lines changed- src/Init/Data/String
- tests/lean/run
3 files changed
+134
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1249 | 1249 | | |
1250 | 1250 | | |
1251 | 1251 | | |
1252 | | - | |
| 1252 | + | |
| 1253 | + | |
1253 | 1254 | | |
1254 | 1255 | | |
1255 | 1256 | | |
| |||
1260 | 1261 | | |
1261 | 1262 | | |
1262 | 1263 | | |
| 1264 | + | |
| 1265 | + | |
1263 | 1266 | | |
1264 | 1267 | | |
1265 | 1268 | | |
1266 | 1269 | | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
1267 | 1273 | | |
1268 | 1274 | | |
1269 | 1275 | | |
1270 | | - | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
1271 | 1290 | | |
1272 | 1291 | | |
1273 | 1292 | | |
1274 | 1293 | | |
1275 | 1294 | | |
1276 | 1295 | | |
1277 | | - | |
| 1296 | + | |
1278 | 1297 | | |
1279 | 1298 | | |
1280 | 1299 | | |
| |||
1285 | 1304 | | |
1286 | 1305 | | |
1287 | 1306 | | |
| 1307 | + | |
| 1308 | + | |
1288 | 1309 | | |
1289 | 1310 | | |
1290 | 1311 | | |
1291 | 1312 | | |
1292 | 1313 | | |
1293 | 1314 | | |
1294 | 1315 | | |
1295 | | - | |
| 1316 | + | |
1296 | 1317 | | |
1297 | 1318 | | |
1298 | 1319 | | |
| |||
1301 | 1322 | | |
1302 | 1323 | | |
1303 | 1324 | | |
1304 | | - | |
| 1325 | + | |
1305 | 1326 | | |
1306 | 1327 | | |
1307 | 1328 | | |
| |||
1310 | 1331 | | |
1311 | 1332 | | |
1312 | 1333 | | |
| 1334 | + | |
1313 | 1335 | | |
1314 | 1336 | | |
1315 | 1337 | | |
1316 | | - | |
| 1338 | + | |
1317 | 1339 | | |
1318 | 1340 | | |
1319 | 1341 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
403 | 403 | | |
404 | 404 | | |
405 | 405 | | |
406 | | - | |
| 406 | + | |
| 407 | + | |
407 | 408 | | |
408 | 409 | | |
409 | 410 | | |
410 | 411 | | |
411 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
412 | 426 | | |
413 | 427 | | |
414 | 428 | | |
415 | 429 | | |
416 | 430 | | |
417 | 431 | | |
418 | | - | |
| 432 | + | |
419 | 433 | | |
420 | 434 | | |
421 | 435 | | |
422 | 436 | | |
423 | 437 | | |
424 | | - | |
| 438 | + | |
425 | 439 | | |
426 | 440 | | |
427 | 441 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments