Skip to content

Commit

Permalink
Merge pull request #631 from yyyc514/string_formatter
Browse files Browse the repository at this point in the history
Fixes to StringFormatter
  • Loading branch information
asterite committed May 5, 2015
2 parents 79989b7 + 0a452ef commit 69a19c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
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

0 comments on commit 69a19c0

Please sign in to comment.