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

Fixes to StringFormatter #631

Merged
merged 3 commits into from May 5, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions spec/std/string_spec.cr
Expand Up @@ -596,6 +596,10 @@ describe "String" do
("%+d" % 123).should eq("+123")
("%+d" % -123).should eq("-123")
("% d" % 123).should eq(" 123")
("%i" % 123).should eq("123")
("%+i" % 123).should eq("+123")
("%+i" % -123).should eq("-123")
("% i" % 123).should eq(" 123")
("%20d" % 123).should eq(" 123")
("%+20d" % 123).should eq(" +123")
("%+20d" % -123).should eq(" -123")
Expand Down Expand Up @@ -630,14 +634,22 @@ describe "String" do
("%6o" % 123).should eq(" 173")
("%-6o" % 123).should eq("173 ")

("%x" % 123).should eq("7B")
("%+x" % 123).should eq("+7B")
("% x" % 123).should eq(" 7B")
("%-x" % 123).should eq("7B")
("%6x" % 123).should eq(" 7B")
("%-6x" % 123).should eq("7B ")

("こんに%xちは" % 123).should eq("こんに7Bちは")
("%x" % 123).should eq("7b")
("%+x" % 123).should eq("+7b")
("% x" % 123).should eq(" 7b")
("%-x" % 123).should eq("7b")
("%6x" % 123).should eq(" 7b")
("%-6x" % 123).should eq("7b ")

("%X" % 123).should eq("7B")
("%+X" % 123).should eq("+7B")
("% X" % 123).should eq(" 7B")
("%-X" % 123).should eq("7B")
("%6X" % 123).should eq(" 7B")
("%-6X" % 123).should eq("7B ")

("こんに%xちは" % 123).should eq("こんに7bちは")
("こんに%Xちは" % 123).should eq("こんに7Bちは")

("%f" % 123).should eq("123.000000")

Expand Down
20 changes: 13 additions & 7 deletions src/string/formatter.cr
Expand Up @@ -102,14 +102,15 @@ struct String::Formatter
when 'o'
flags.base = 8
int flags
when 'd'
when 'd', 'i'
flags.base = 10
int flags
when 'x'
when 'x', 'X'
flags.base = 16
flags.type = char
int flags
when 'a', 'A', 'e', 'E', 'f', 'g', 'G'
flags.float = char
flags.type = char
float flags
when '%'
char '%'
Expand Down Expand Up @@ -147,7 +148,12 @@ struct String::Formatter
end
end

int.to_s(flags.base, @io)
# if we are requesting lower-case "digits"
if flags.base > 10 && flags.type == 'x'
@io << int.to_s(flags.base).downcase
else
int.to_s(flags.base, @io)
end

if flags.right_padding?
pad_int int, flags
Expand Down Expand Up @@ -199,7 +205,7 @@ struct String::Formatter
io << '.'
io << precision if precision != 0
end
io << flags.float
io << flags.type

original_format_buf
end
Expand Down Expand Up @@ -268,14 +274,14 @@ struct String::Formatter
struct Flags
property space, sharp, plus, minus, zero, base
property width, width_length
property float, precision, precision_length
property type, precision, precision_length

def initialize
@space = @sharp = @plus = @minus = @zero = false
@width = 0
@width_length = 0
@base = 10
@float = 'f'
@type = ' '
@precision = nil
@precision_length = 0
end
Expand Down