Skip to content

Commit

Permalink
Fix incorrect example in string formatting docs. (#873)
Browse files Browse the repository at this point in the history
%s is defined to support "all numerical types (int, uint, and double)."
but later the docs also say:
"Passing an incorrect type (an integer to `%s`) is considered an error"

The example in the parenthetical contradicts the definition of %s.

This change replaces the parenthetical example with one that is
accurate and verified by an existing test case. This change also adds
an explicit test case to verify that it is valid to pass an in to %s.
  • Loading branch information
nicksnyder authored Dec 16, 2023
1 parent a18f8e7 commit 9c277eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const (
// "a map inside a list: %s".format([[1, 2, 3, {"a": "x", "b": "y", "c": "z"}]]) // returns "a map inside a list: [1, 2, 3, {"a":"x", "b":"y", "c":"d"}]"
// "true bool: %s - false bool: %s\nbinary bool: %b".format([true, false, true]) // returns "true bool: true - false bool: false\nbinary bool: 1"
//
// Passing an incorrect type (an integer to `%s`) is considered an error, as well as attempting
// Passing an incorrect type (a string to `%b`) is considered an error, as well as attempting
// to use more formatting clauses than there are arguments (`%d %d %d` while passing two ints, for instance).
// If compile-time checking is enabled, and the formatting string is a constant, and the argument list is a literal,
// then letting any arguments go unused/unformatted is also considered an error.
Expand Down
8 changes: 8 additions & 0 deletions ext/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ func TestStringFormat(t *testing.T) {
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "int support for string",
format: "%s",
formatArgs: `999999999999`,
expectedOutput: "999999999999",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
},
{
name: "bytes support for string",
format: "some bytes: %s",
Expand Down

0 comments on commit 9c277eb

Please sign in to comment.