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 #28067

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,14 +553,66 @@ floor(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x)

## integer construction ##

"""
@int128_str str

Parse `str` as an [`Int128`](@ref).

# Examples
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> @int128_str "123456789123"
123456789123

julia> @int128_str "123456789123.4"
ERROR: LoadError: ArgumentError: invalid base 10 digit '.' in "123456789123.4"
Stacktrace:
[1] tryparse_internal(::Type{Int128}, ::String, ::Int64, ::Int64, ::Int64, ::Bool) at ./parse.jl:118
[...]
```
"""
macro int128_str(s)
return parse(Int128, s)
end

"""
@uint128_str str

Parse `str` as an [`UInt128`](@ref).

# Examples
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> @uint128_str "123456789123"
0x00000000000000000000001cbe991a83

julia> @uint128_str "-123456789123"
ERROR: LoadError: ArgumentError: invalid base 10 digit '-' in "-123456789123"
Stacktrace:
[1] tryparse_internal(::Type{UInt128}, ::String, ::Int64, ::Int64, ::Int64, ::Bool) at ./parse.jl:118
[...]
```
"""
macro uint128_str(s)
return parse(UInt128, s)
end

"""
@big_str str

Attempt to parse `str` as a [`BigInt`](@ref) or [`BigFloat`](@ref).
Throw an [`ArgumentError`] if `str` is in the wrong format for representation
as either of these types.

# Examples
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> @big_str "1_000_000_000_000"
1000000000000

julia> @big_str "_"
ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat
Stacktrace:
[1] top-level scope at none:0
```
"""
macro big_str(s)
if '_' in s
# remove _ in s[2:end-1]
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
```

## General Number Functions and Constants
Expand Down