Repository navigation
settings.toml: parse integers with our own routine instead of strtol() - #66
Merged
mikeysklar merged 1 commit intoOct 17, 2026
Conversation
Replace strtol() in get_int() and strtoul() in read_unicode_escape() with a small digit parser. This drops strtol, strtoul, _ctype_, errno and _impure_data from the link: -660 bytes flash and -80 bytes RAM on the ItsyBitsy nRF52840, -620 bytes flash on the Feather RP2040. Integers now also accept 0o and 0b prefixes and underscores (1_000). As a superset of TOML, a sign is allowed before 0x/0o/0b, the prefix may be uppercase, and underscores are not checked to be between digits. Out-of-range integers are no longer clamped to LONG_MAX. settings_get_int() returns an error for them; supervisor.get_setting() raises ValueError for hex, octal and binary, and returns a float for decimal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Thanks Dan, merged into the adafruit#11620 branch. Sizes measured on the same tree with and without your commit. The savings are substantial as you claimed on nRF / RP2. ESP32-S3 takes a smaller hit.
It also adds two bugs:
In The I'll push fixes for both to adafruit#11620, with tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR note written by @dhalbert. Claude wrote the code. @mikeysklar, for your consideration.
We were using
strtol()to parse integers andstrtoul()to parse\uescapes.strtol()doesn't handle underscores, and nothing else in the firmware uses it.strtoul()is only used elsewhere by the web workflow.If we replace both with our own routine, we can support more features, and in less space. The new routine is
parse_digits(). Sign, prefix, and range handling are done inget_int(). The new code handles0o,0b,0O, and0B, allows underscores, and still prevents leading zeros, the point of the original PR. It also doesn't clamp out-of-range values toLONG_MAX: a too-big hex, octal, or binary value raisesValueError, and a too-big decimal value comes back as a float. It's more relaxed than the TOML spec at some things.It passes
tests/circuitpython-manual/settingson a Metro M4 Express and the unix coveragemake test.Size