#12446
#24687
In addition to std.fmt.parse* functions there are in no particular order:
One problem with such code duplication is that more code means more bugs. While writing this, I found #30880.
Some of the implementations above (like std.math.big.int.Mutable.setString) are likely too specific to benefit from a common implementation, but many of these use cases could use a common implementation.
Some properties that, in my opinion, should be configurable in a common implementation (feel free to argue against these or in favour of other properties):
- Should symbols like
_ be allowed between digits? Which symbols (', _)?
- Are
+ and - allowed?
- What base is the number? Should prefixes like
0x, 0o and 0b change the base to 16, 8 and 2.
- Is a leading
0 allowed? Should it change the base to 8?
- Is the complete input string a number or only the beginning of the input string?
Like the author of #24687, I'm not sure what the ideal API would look like. #12446 proposed a config struct.
There are both use cases, for the base to be comptime known and for the base to be known only at runtime (and for the base to be specified by a prefix).
For some use cases the exact length of the input string is comptime known, for other use cases it is not.
For some use cases like std.process.posixGetUserInfoPasswdStream, it might make sense to use a std.Io.Reader instead of a string as the input, but in other use cases, parsing from a fixed reader would probably generate worse machine code than parsing from a string. Also, since string representations of integers are not very long in most use cases, it might make sense in general to first create a []const u8 of the input.
[#12446](https://github.com/ziglang/zig/issues/12446)
[#24687](https://github.com/ziglang/zig/issues/24687)
In addition to `std.fmt.parse*` functions there are in no particular order:
- [`std.crypto.Certificate.parseTimeDigits`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/crypto/Certificate.zig#L653-L667)
- [`std.crypto.Certificate.parseYear4`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/crypto/Certificate.zig#L682-L694)
- [`std.http.Client.Response.Head.parseInt3`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/http/Client.zig#L694-L699)
- [`std.Io.net.Ip4Address.parse`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/Io/net.zig#L354-L384)
- [`std.Io.net.Ip6Address.parse`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/Io/net.zig#L465-L572)
- [`std.fmt.Parser.number`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt.zig#L167-L181)
- [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/parse.zig#L43-L46), [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/parse.zig#L58-L65), [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/parse.zig#L80-L85), [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/decimal.zig#L245-L247), [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/decimal.zig#L268-L270), and [here](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/fmt/parse_float/decimal.zig#L305-L309) in `std.fmt.parse_float`
- [`std.http.ChunkParser.feed`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/http/ChunkParser.zig#L52-L78)
- [`std.zig.number_literal.parseNumberLiteral`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/zig/number_literal.zig#L65-L179)
- Parsing of escape sequences in strings in [zig](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/zig/string_literal.zig#L151-L234) and [json](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/json/Scanner.zig#L722-L904)
- [`std.zig.AstGen.parseBitCount`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/zig/AstGen.zig#L8210-L8229)
- `std.process`: parsing [uid](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/process.zig#L199-L221) and [gid](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/process.zig#L222-L244) from `/etc/passwd`
- [one implementation](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/c/stdlib.zig#L200-L233) used by libzigc for `ato*` and `strto*` functions (#30792).
- [`std.math.big.int.Mutable.setString`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/math/big/int.zig#L347-L410)
- #30791
- more in aro, translate-c and resinator
- maybe more that I haven't found
One problem with such code duplication is that more code means more bugs. While writing this, I found #30880.
Some of the implementations above (like [`std.math.big.int.Mutable.setString`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/math/big/int.zig#L347-L410)) are likely too specific to benefit from a common implementation, but many of these use cases could use a common implementation.
Some properties that, in my opinion, should be configurable in a common implementation (feel free to argue against these or in favour of other properties):
- Should symbols like `_` be allowed between digits? Which symbols (`'`, `_`)?
- Are `+` and `-` allowed?
- What base is the number? Should prefixes like `0x`, `0o` and `0b` change the base to `16`, `8` and `2`.
- Is a leading `0` allowed? Should it change the base to `8`?
- Is the complete input string a number or only the beginning of the input string?
Like the author of [#24687](https://github.com/ziglang/zig/issues/24687), I'm not sure what the ideal API would look like. [#12446](https://github.com/ziglang/zig/issues/12446) proposed a config struct.
There are both use cases, for the base to be comptime known and for the base to be known only at runtime (and for the base to be specified by a prefix).
For some use cases the exact length of the input string is comptime known, for other use cases it is not.
For some use cases like [`std.process.posixGetUserInfoPasswdStream`](https://codeberg.org/ziglang/zig/src/commit/e5dc5a6eb5608c100e78d6116942a4cd17f56d00/lib/std/process.zig#L154-L247), it might make sense to use a `std.Io.Reader` instead of a string as the input, but in other use cases, parsing from a fixed reader would probably generate worse machine code than parsing from a string. Also, since string representations of integers are not very long in most use cases, it might make sense in general to first create a `[]const u8` of the input.