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

update advanced formatting with named arguments #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions chapter-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,27 +913,32 @@ This list is non-exhaustive.

So far we have only covered formatting specifiers. Format strings actually follow this format, where between each pair of square brackets is a parameter you have to replace with something.

`{[position][specifier]:[fill][alignment][width].[precision]}`
`{[argument][specifier]:[fill][alignment][width].[precision]}`

| Name | Meaning |
|-----------|-----------------------------------------------------------------------------------------|
| Position | The index of the argument that should be inserted |
| argument | The name or index of the argument that should be inserted |
| Specifier | A type-dependent formatting option |
| Fill | A single character used for padding |
| Alignment | One of three characters '<', '^' or '>'; these are for left, middle and right alignment |
| Width | The total width of the field (characters) |
| Precision | How many decimals a formatted number should have |


Position usage.
argument usage.
```zig
test "position" {
test "arument" {
var b: [3]u8 = undefined;
try expect(eql(
u8,
try bufPrint(&b, "{0s}{0s}{1s}", .{"a", "b"}),
try bufPrint(&b, "{0s}{0s}{1s}", .{ "a", "b" }),
"aab",
));
try expect(eql(
u8,
try bufPrint(&b, "{[a_name]s}{[name_2]s}{[a_name]s}", .{ .a_name = "a" .name_2 = "b" }),
"aba",
));
}
```

Expand Down