From 4e6191899b9bdd44499d99229bfcf6b3fbd0b5be Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 12 Jul 2018 13:46:18 -0400 Subject: [PATCH 1/4] Doc some string parsing macros --- base/int.jl | 52 +++++++++++++++++++++++++++++++++++++++++ doc/src/base/numbers.md | 3 +++ 2 files changed, 55 insertions(+) diff --git a/base/int.jl b/base/int.jl index 299eada7b2d0c..a6a97dbcbbd31 100644 --- a/base/int.jl +++ b/base/int.jl @@ -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] diff --git a/doc/src/base/numbers.md b/doc/src/base/numbers.md index deea1c2746ea1..0670f22b776fb 100644 --- a/doc/src/base/numbers.md +++ b/doc/src/base/numbers.md @@ -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 From 471ef1ccb7e512183119ee4acb66d4dd24c87921 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 12 Jul 2018 17:21:49 -0400 Subject: [PATCH 2/4] Also show the prettier versions --- base/int.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/base/int.jl b/base/int.jl index a6a97dbcbbd31..abf533f9daeba 100644 --- a/base/int.jl +++ b/base/int.jl @@ -563,6 +563,9 @@ Parse `str` as an [`Int128`](@ref). julia> @int128_str "123456789123" 123456789123 +julia> int128"123456789123" +123456789123 + julia> @int128_str "123456789123.4" ERROR: LoadError: ArgumentError: invalid base 10 digit '.' in "123456789123.4" Stacktrace: @@ -584,6 +587,9 @@ Parse `str` as an [`UInt128`](@ref). julia> @uint128_str "123456789123" 0x00000000000000000000001cbe991a83 +julia> uint128"123456789123" +0x00000000000000000000001cbe991a83 + julia> @uint128_str "-123456789123" ERROR: LoadError: ArgumentError: invalid base 10 digit '-' in "-123456789123" Stacktrace: @@ -607,6 +613,9 @@ as either of these types. julia> @big_str "1_000_000_000_000" 1000000000000 +julia> big"1_000_000_000_000" +1000000000000 + julia> @big_str "_" ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat Stacktrace: From e969baab3bc488a780257a43fcff15e48306022f Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 20 Apr 2021 09:49:10 -0400 Subject: [PATCH 3/4] Update doc/src/base/numbers.md --- doc/src/base/numbers.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/src/base/numbers.md b/doc/src/base/numbers.md index 93e42826ad852..47a33b4447264 100644 --- a/doc/src/base/numbers.md +++ b/doc/src/base/numbers.md @@ -57,9 +57,6 @@ Base.bswap Base.hex2bytes Base.hex2bytes! Base.bytes2hex -Base.@big_str -Base.@int128_str -Base.@uint128_str ``` ## General Number Functions and Constants From 4a463e17f83603c6b900b128383d50d82fbc8ee5 Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Tue, 16 Nov 2021 19:37:38 -0500 Subject: [PATCH 4/4] Update base/int.jl Co-authored-by: Jameson Nash --- base/int.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/int.jl b/base/int.jl index 52279b1ae9eca..f28b231e6e0ac 100644 --- a/base/int.jl +++ b/base/int.jl @@ -680,7 +680,7 @@ julia> big"123_456" julia> big"7891.5" 7891.5 -julia> @big"_" +julia> big"_" ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat [...] ```