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

Strings with '%' symbols which aren't token placeholders don't format correctly #2

Open
lawmaestro opened this issue Jul 21, 2018 · 3 comments

Comments

@lawmaestro
Copy link

For instance the following:
"%s 0% fat Greek style yogurt"
when formatted with:
"60ml"
should ideally result in:
"60ml 0% fat Greek style yogurt"
Instead the outcome is nil

I've taken a look over the code and this could be fixed by tweaking line 587 in Sprinter.swift:
if first == "%" { break }
to become:

if first == "%" {
  if string.last == nil || string.last == " " {
    break
  }
}

I've tested this locally and all unit tests pass with the change (as well as this new case). I'm happy to create a PR? Let me know, cheers.

@nicklockwood
Copy link
Owner

Yes, please do.

I need to check the spec for what the expected behavior is here. I would think you are supposed to write %% in this case and that a % on its own is a syntax error, but in a case like this it seems reasonable to allow it.

@lawmaestro
Copy link
Author

The problem with that approach is that it would break formatting for tokens inlined, for example:
Hello_%s_world

The approach I took to work around the problem initially was to swap out the '%' I wanted ignoring for a 'widepercent'. An alternative as you say could also be to double it up - '%%'. Would be nice to be able to defend against it in the formatting though :/

@nkalvi
Copy link

nkalvi commented Mar 23, 2021

According to the standard

Each conversion specification is introduced by the character ( '%' ). After the character '%', the following shall appear in sequence:
...
%: Write a '%' character; no argument is converted.

So, using %% here is expected.

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

No branches or pull requests

3 participants