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

Fix issue #83 (from Formatting.jl), handle precision argument for Pyt… #70

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
license = "MIT"
name = "Format"
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
version = "1.3.3"
version = "1.3.4"

[deps]

Expand Down
47 changes: 33 additions & 14 deletions src/fmtcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,43 @@ function _repprint(out::IO, c::AbstractChar, n::Int)
end
end


### print string or char

function _truncstr(s::AbstractString, slen, prec)
prec == 0 && return ("", 0)
i, n = 0, 1
siz = ncodeunits(s)
while n <= siz
(prec -= textwidth(s[n])) < 0 && break
i = n
n = nextind(s, i)
end
str = SubString(s, 1, i)
return (str, textwidth(str))
end

_truncstr(s::AbstractChar, slen, prec) = ("", 0)

function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,AbstractChar})
pad = fs.width - textwidth(s)
if pad <= 0
print(out, s)
elseif fs.align == '<'
print(out, s)
_repprint(out, fs.fill, pad)
elseif fs.align == '^'
_repprint(out, fs.fill, pad>>1)
print(out, s)
_repprint(out, fs.fill, (pad+1)>>1)
else
_repprint(out, fs.fill, pad)
print(out, s)
slen = textwidth(s)
str, slen = 0 <= fs.prec < slen ? _truncstr(s, slen, fs.prec) : (s, slen)
prepad = postpad = 0
pad = fs.width - slen
if pad > 0
if fs.align == '<'
postpad = pad
elseif fs.align == '^'
prepad, postpad = pad>>1, (pad+1)>>1
else
prepad = pad
end
end
# left padding
prepad == 0 || _repprint(out, fs.fill, prepad)
# print string
print(out, str)
# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end

_unsigned_abs(x::Signed) = unsigned(abs(x))
Expand Down
10 changes: 10 additions & 0 deletions test/fmtspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ end
@test pyfmt("#b", 6) == "0b110"
@test pyfmt("#o", 6) == "0o6"
@test pyfmt("#x", 6) == "0x6"
@test pyfmt("#X", 10) == "0XA"
end

@testset "Format string" begin
Expand All @@ -75,6 +76,15 @@ end
@test pyfmt("⋆^5s", "αβγ") == "⋆αβγ⋆"
@test pyfmt("*<5s", "abc") == "abc**"
@test pyfmt("⋆<5s", "αβγ") == "αβγ⋆⋆"

# Issue #83 (in Formatting.jl)
@test pyfmt(".3s", "\U1f595\U1f596") == "🖕"
@test pyfmt(".4s", "\U1f595\U1f596") == "🖕🖖"
@test pyfmt(".5s", "\U1f595\U1f596") == "🖕🖖"

@test pyfmt("6.3s", "\U1f595\U1f596") == "🖕 "
@test pyfmt("6.4s", "\U1f595\U1f596") == "🖕🖖 "
@test pyfmt("6.5s", "\U1f595\U1f596") == "🖕🖖 "
end

@testset "Format Symbol" begin
Expand Down
Loading