Skip to content

Add strfmt macro function instead of changing strformat#5349

Merged
kwvanderlinde merged 1 commit intoRPTools:developfrom
fishface60:strfmt
Apr 4, 2025
Merged

Add strfmt macro function instead of changing strformat#5349
kwvanderlinde merged 1 commit intoRPTools:developfrom
fishface60:strfmt

Conversation

@fishface60
Copy link
Contributor

@fishface60 fishface60 commented Apr 3, 2025

Identify the Bug or Feature request

fixes #5226

Description of the Change

Macros depend on the behaviour of strformat bypassing interpolations if there are no passed parameters, contrary to documentation, so strformat's behaviour can't be changed.

Instead we can introduce a new strfmt macro function with more well-defined behaviour of always handling String.format escapes and also not processing the interpolations if you use both interpolations and positional formatting.

Possible Drawbacks

Having both strformat and strfmt is confusing. strformat should be deprecated but it is slightly faster if you are only interpolating strings.

Documentation Notes

strfmt behaves like strformat is documented to.

strfmt and strformat should share a wiki page and behaviours where strformat diverges should be documented, i.e.

  1. Formatting Data Type

    NOTE: These format specifiers are ignored if strformat is used and no positional arguments are provided, so %% and %n format specifiers are ignored. Use strfmt if format specifiers are required without positional arguments.

  2. Formatting by Embedding Variables

    NOTE: The contents of the variables are interpreted as format specifiers if strformat is used and positional arguments are provided, so a string containing the value "100%" may be interpreted as a format string and cause errors. Use strfmt if embedded variables and positional arguments are both used and the variables may contain % characters.

Release Notes

  • Added strfmt macro function to replace strformat so %% and %n format specifiers may be used without positional parameters and allow mixing Variable Embedding with positional formatting. strformat is retained with the previous behaviour for compatibility with existing macros.

This change is Reviewable

@github-actions github-actions bot added the bug label Apr 3, 2025
@fishface60
Copy link
Contributor Author

This will conflict with #5343 when release-1.17 is merged back into develop.

If you would like to do that first I can rebase this change on top.

@kwvanderlinde
Copy link
Collaborator

Created #5351 to merge release-1.17 first.

@cwisniew
Copy link
Member

cwisniew commented Apr 4, 2025

Created #5351 to merge release-1.17 first.

And it has now been merged

Macros depend on the behaviour of strformat bypassing interpolations if
there are no passed parameters, contrary to documentation, so
strformat's behaviour can't be changed.

Instead we can introduce a new strfmt macro function with more
well-defined behaviour of always handling String.format escapes and also
not processing the interpolations if you use both interpolations and
positional formatting.
@fishface60
Copy link
Contributor Author

Thanks, rebased.

@kwvanderlinde kwvanderlinde moved this from Todo to To-Be-Merged in MapTool 1.18 Apr 4, 2025
@kwvanderlinde kwvanderlinde added this pull request to the merge queue Apr 4, 2025
Merged via the queue into RPTools:develop with commit d7e3e90 Apr 4, 2025
4 checks passed
@github-project-automation github-project-automation bot moved this from To-Be-Merged to Merged in MapTool 1.18 Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

[Bug]: strformat format literals don't work when not passing parameters

3 participants