Skip to content

core/fmt: Second attempt at improving number formatting#6193

Open
BunterSchatten wants to merge 3 commits intoodin-lang:masterfrom
BunterSchatten:fmt-fixes
Open

core/fmt: Second attempt at improving number formatting#6193
BunterSchatten wants to merge 3 commits intoodin-lang:masterfrom
BunterSchatten:fmt-fixes

Conversation

@BunterSchatten
Copy link
Contributor

This pull request tries to improve the number formatting of the core/fmt package to do what the docs say it should.

Notable changes are:

  • default padding is now " " instead of "0" (fixes [fmt] left padding of numbers should default to space instead of 0 #3112)
  • right padding is now always done with spaces, to keep the formatted number correct
  • the space flag now adds a space, when there is no sign
  • the plus flag now also adds a space before NaN
  • the hash flag now correctly adds a base prefix (eg. "0x") to number and removes it from pointers
  • the base prefix is now counted towards the total width

I didn't use the suggestion from #3115 (comment), because I didn't want to change the formatting syntax.
This is my second attempt at improving number formatting, I'm closing the first one (#6148), because I found a few other problems afterwards that needed more extensive changes.

Here are a few formatting examples that are changed by this pull request.

// format                        old           new
// ------------------------------------------------

fmt.printf("%.0i",  0)         //  ""       ->   "0"

fmt.printf("% i",  1)          //  "1"      ->   " 1"
fmt.printf("% i", -1)          //  "-1"     ->   "-1"

fmt.printf("%4i",  1)          //  "0001"   ->   "   1"
fmt.printf("%4i", -1)          //  "-001"   ->   "  -1"

fmt.printf("% 4i",  1234)      //  "1234"   ->   " 1234"
fmt.printf("% 4i", -1234)      //  "-1234"  ->   "-1234"

fmt.printf("%-4i",  1)         //  "1000"   ->   "1   "
fmt.printf("%-4i", -1)         //  "-100"   ->   "-1  "

fmt.printf("%- 4i",  1)        //  "1   "   ->   " 1  "
fmt.printf("%- 4i", -1)        //  "-1  "   ->   "-1  "

fmt.printf("%+4i",  1)         //  "+001"   ->   "  +1"
fmt.printf("%+4i", -1)         //  "-001"   ->   "  -1"

fmt.printf("%+0 4i",  1)       //  "  +1"   ->   "+001"
fmt.printf("%+0 4i", -1)       //  "  -1"   ->   "-001"

fmt.printf("% 04i",  1)        //  "   1"   ->   " 001"
fmt.printf("% 04i", -1)        //  "  -1"   ->   "-001"

fmt.printf("%-04i",  1)        //  "1000"   ->   "1   "
fmt.printf("%-04i", -1)        //  "-100"   ->   "-1  "

fmt.printf("%- 04i",  1)       //  "1   "   ->   " 1  "
fmt.printf("%- 04i", -1)       //  "-1  "   ->   "-1  "

fmt.printf("%#04x",  10)       //  "000a"   ->   "0x0a"
fmt.printf("%#04x", -10)       //  "-00a"   ->   "-0xa"

fmt.printf("%#+04x",  10)      //  "+00a"   ->   "+0xa"
fmt.printf("%#+04x", -10)      //  "-00a"   ->   "-0xa"

fmt.printf("%#-04x",  10)      //  "0xa0"   ->   "0xa "
fmt.printf("%#-04x", -10)      //  "-0xa"   ->   "-0xa"

fmt.printf("%#-4x",  10)       //  "0xa0"   ->   "0xa "
fmt.printf("%#-4x", -10)       //  "-0xa"   ->   "-0xa"

fmt.printf("%# 04x",  10)      //  "   a"   ->   " 0xa"
fmt.printf("%# 04x", -10)      //  "  -a"   ->   "-0xa"

fmt.printf("%# +04x",  10)     //  "  +a"   ->   "+0xa"
fmt.printf("%# +04x", -10)     //  "  -a"   ->   "-0xa"

fmt.printf("%# -04x",  10)     //  "0xa "   ->   " 0xa"
fmt.printf("%# -04x", -10)     //  "-0xa"   ->   "-0xa"

fmt.printf("%+f",  NaN)        //  "NaN"    ->   " NaN"

fmt.printf("% f",  3.14)       //  "3.140"  ->   " 3.140"
fmt.printf("% f",  NaN)        //  "NaN"    ->   " NaN"

fmt.printf("%7.3f",  3.14)     //  "003.140"  ->   "  3.140"
fmt.printf("%7.3f", -3.14)     //  "-03.140"  ->   " -3.140"
fmt.printf("%7.3f", +Inf)      //  "+000Inf"  ->   "   +Inf"
fmt.printf("%7.3f", -Inf)      //  "-000Inf"  ->   "   -Inf"
fmt.printf("%7.3f",  NaN)      //  "0000NaN"  ->   "    NaN"

fmt.printf("%07.3f", +Inf)     //  "+000Inf"  ->   "   +Inf"
fmt.printf("%07.3f", -Inf)     //  "-000Inf"  ->   "   -Inf"
fmt.printf("%07.3f",  NaN)     //  "0000NaN"  ->   "    NaN"

fmt.printf("%-7.3f",  3.14)    //  "3.14000"  ->   "3.140  "
fmt.printf("%-7.3f", -3.14)    //  "-3.1400"  ->   "-3.140 "
fmt.printf("%-7.3f", +Inf)     //  "+Inf000"  ->   "+Inf   "
fmt.printf("%-7.3f", -Inf)     //  "-Inf000"  ->   "-Inf   "
fmt.printf("%-7.3f",  NaN)     //  "NaN0000"  ->   "NaN    "

fmt.printf("%- 7.3f",  3.14)   //  "3.140  "  ->   " 3.140 "
fmt.printf("%- 7.3f",  NaN)    //  "NaN    "  ->   " NaN   "

fmt.printf("%-07.3f",  3.14)   //  "3.14000"  ->   "3.140  "
fmt.printf("%-07.3f", -3.14)   //  "-3.1400"  ->   "-3.140 "
fmt.printf("%-07.3f", +Inf)    //  "+Inf000"  ->   "+Inf   "
fmt.printf("%-07.3f", -Inf)    //  "-Inf000"  ->   "-Inf   "
fmt.printf("%-07.3f",  NaN)    //  "NaN0000"  ->   "NaN    "

fmt.printf("%- 07.3f",  3.14)  //  "3.140  "  ->   " 3.140 "
fmt.printf("%- 07.3f",  NaN)   //  "NaN    "  ->   " NaN   "

- pad with spaces by default, not zeros
- correctly handle hash and space flag
- simplify formatting logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[fmt] left padding of numbers should default to space instead of 0

1 participant