-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential bugs by switching from Formatting.jl to Format.jl
- Loading branch information
1 parent
6b869c3
commit c77f093
Showing
5 changed files
with
24 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[deps] | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" | ||
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8" | ||
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" | ||
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,48 @@ | ||
abstract type AbstractNumberFormatter end | ||
|
||
(::AbstractNumberFormatter)(x) = string(x) | ||
|
||
const float_regex = r"(?'mantissa'(?'before_dp'(?'sign'-?)(?'before_dp_nosign'\d+))(\.(?'after_dp'\d+))?)(?'e_or_E'e)(?'raw_exp'(?'sign_exp'-?)\+?0*(?'mag_exp'\d+))"i | ||
|
||
struct PlainNumberFormatter <: AbstractNumberFormatter end | ||
|
||
(::PlainNumberFormatter)(x) = string(x) | ||
|
||
|
||
struct PrintfNumberFormatter <: AbstractNumberFormatter | ||
fmt::String | ||
f::Function | ||
|
||
PrintfNumberFormatter(fmt::String) = new(fmt, Format.generate_formatter(fmt)) | ||
end | ||
PrintfNumberFormatter(fmt::String) = PrintfNumberFormatter(fmt, Formatting.generate_formatter(fmt)) | ||
(f::PrintfNumberFormatter)(x) = f.f(x) | ||
|
||
(f::PrintfNumberFormatter)(x) = f.f(x) | ||
|
||
struct StyledNumberFormatter <: AbstractNumberFormatter | ||
fmt::String | ||
f::Function | ||
|
||
StyledNumberFormatter(fmt::String="%.4g") = new(fmt) | ||
|
||
end | ||
|
||
function StyledNumberFormatter(significant_digits::Int) | ||
return StyledNumberFormatter("%.$(significant_digits)g") | ||
StyledNumberFormatter(fmt::String="%.4g") = new(fmt, Format.generate_formatter(fmt)) | ||
end | ||
|
||
(f::StyledNumberFormatter)(x) = string(x) | ||
function (f::StyledNumberFormatter)(x::AbstractFloat) | ||
s = @eval @sprintf $(f.fmt) $x | ||
return replace(s, float_regex => s"\g<mantissa> \\mathrm{\g<e_or_E>}{\g<sign_exp>\g<mag_exp>}") | ||
end | ||
StyledNumberFormatter(sig_digits::Int) = StyledNumberFormatter("%.$(significant_digits)g") | ||
|
||
(f::StyledNumberFormatter)(x::AbstractFloat) = | ||
replace(f.f(x), float_regex => s"\g<mantissa> \\mathrm{\g<e_or_E>}{\g<sign_exp>\g<mag_exp>}") | ||
(f::StyledNumberFormatter)(x::Unsigned) = "\\mathtt{0x$(string(x; base=16, pad=2sizeof(x)))}" | ||
|
||
|
||
struct FancyNumberFormatter <: AbstractNumberFormatter | ||
fmt::String | ||
f::Function | ||
exponent_format::SubstitutionString | ||
|
||
function FancyNumberFormatter(fmt::String="%.4g", | ||
exponent_format::SubstitutionString=s"\g<mantissa> \\cdot 10^{\g<sign_exp>\g<mag_exp>}") | ||
return new(fmt, exponent_format) | ||
end | ||
FancyNumberFormatter(fmt::String="%.4g", | ||
exponent_format::SubstitutionString=s"\g<mantissa> \\cdot 10^{\g<sign_exp>\g<mag_exp>}") = new(fmt, Format.generate_formatter(fmt), exponent_format) | ||
end | ||
|
||
function FancyNumberFormatter(fmt::String, mult_symbol) | ||
return FancyNumberFormatter(fmt, SubstitutionString("\\g<mantissa> $(escape_string(mult_symbol)) 10^{\\g<sign_exp>\\g<mag_exp>}")) | ||
end | ||
function FancyNumberFormatter(significant_digits, mult_symbol="\\cdot") | ||
return FancyNumberFormatter("%.$(significant_digits)g", mult_symbol) | ||
end | ||
FancyNumberFormatter(fmt::String, mult_symbol) = | ||
FancyNumberFormatter(fmt, SubstitutionString("\\g<mantissa> $(escape_string(mult_symbol)) 10^{\\g<sign_exp>\\g<mag_exp>}")) | ||
FancyNumberFormatter(significant_digits, mult_symbol="\\cdot") = | ||
FancyNumberFormatter("%.$(significant_digits)g", mult_symbol) | ||
|
||
(f::FancyNumberFormatter)(x) = string(x) | ||
|
||
function (f::FancyNumberFormatter)(x::AbstractFloat) | ||
s = @eval @sprintf $(f.fmt) $x | ||
return replace(s, float_regex => f.exponent_format) | ||
end | ||
|
||
(f::FancyNumberFormatter)(x::AbstractFloat) = replace(f.f(x), float_regex => f.exponent_format) | ||
(f::FancyNumberFormatter)(x::Unsigned) = "\\mathtt{0x$(string(x; base=16, pad=2sizeof(x)))}" |