Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc some string parsing macros #28085

Merged
merged 6 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -627,29 +627,46 @@ floor(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x)

"""
@int128_str str
@int128_str(str)

`@int128_str` parses a string into a Int128
Throws an `ArgumentError` if the string is not a valid integer
Parse `str` as an [`Int128`](@ref).
Throw an `ArgumentError` if the string is not a valid integer.

# Examples
```jldoctest
julia> int128"123456789123"
123456789123

julia> int128"123456789123.4"
ERROR: LoadError: ArgumentError: invalid base 10 digit '.' in "123456789123.4"
[...]
```
"""
macro int128_str(s)
return parse(Int128, s)
end

"""
@uint128_str str
@uint128_str(str)

`@uint128_str` parses a string into a UInt128
Throws an `ArgumentError` if the string is not a valid integer
Parse `str` as an [`UInt128`](@ref).
Throw an `ArgumentError` if the string is not a valid integer.

# Examples
```
julia> uint128"123456789123"
0x00000000000000000000001cbe991a83

julia> uint128"-123456789123"
ERROR: LoadError: ArgumentError: invalid base 10 digit '-' in "-123456789123"
[...]
```
"""
macro uint128_str(s)
return parse(UInt128, s)
end

"""
@big_str str
@big_str(str)

Parse a string into a [`BigInt`](@ref) or [`BigFloat`](@ref),
and throw an `ArgumentError` if the string is not a valid number.
Expand All @@ -662,6 +679,10 @@ julia> big"123_456"

julia> big"7891.5"
7891.5

julia> @big"_"
ViralBShah marked this conversation as resolved.
Show resolved Hide resolved
ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat
[...]
```
"""
macro big_str(s)
Expand Down
3 changes: 3 additions & 0 deletions doc/src/base/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Base.bswap
Base.hex2bytes
Base.hex2bytes!
Base.bytes2hex
Base.@big_str
Base.@int128_str
Base.@uint128_str
vtjnash marked this conversation as resolved.
Show resolved Hide resolved
```

## General Number Functions and Constants
Expand Down