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

More Robust Quoted Strings #110

Merged
merged 2 commits into from
Mar 3, 2015
Merged

More Robust Quoted Strings #110

merged 2 commits into from
Mar 3, 2015

Conversation

andrewstarks
Copy link
Contributor

Hey Steve,

I wanted to share this work with you, in case you found it useful. Please ignore/reject it if you don't find that it fits with Penlight's goals.

This pull request includes stringx.quote_string and changes to pretty.write. quote_string wraps a string such that reloading that string from a chunk of Lua code produces identical results:

mystr = "\n[[\n My tricky ]======] string.\n]]"
mystr2 = load("return " .. stringx.quote_string(mystr))()
assert(mystr == mystr2)

It provides the same facilities as ("%q"):format(str), plus:

  • Uses long quotes when it finds a pattern that matches the long-quote pattern (begin or end, balance doesn't matter) or when it finds a \n.
  • Adds an extra \n at the beginning, if found.
  • When Long quotes are used, it always uses a number of = signs that is one greater than any long-quotes that are embedded into the string.

The second, and highly related, changes were to pretty.write:

  • Uses stringx.quote_string instead of internal, literal methods.
  • Adds a space in front and in back of string, when long strings are used for key values:
my_table = {
    [ [[
My crazy key
]] ] = "foo"
}
  • Because of the first change, now embedded long-strings can round-trip through the pretty printer. Nested long-strings would not compile in Lua.

Finally, a small patch was added for pretty.number:

  • Added check for Lua 5.3. Locally patched tostring so that it returns 123 instead of 123.0. Without this patch, pretty.number was broken.

I also added tests and local function documentation, although this should be re-read and verified before accepting.

Andrew Starks added 2 commits March 4, 2014 10:46
… matches and escapes them by creating a long-string quote. It also uses long quotes for strings with return values embedded, just as pretty requires.
…reater number of equal signs than found in the scan. Needed to make pretty work for embedded lua code.
stevedonovan added a commit that referenced this pull request Mar 3, 2015
More Robust Quoted Strings
@stevedonovan stevedonovan merged commit 2945226 into lunarmodules:master Mar 3, 2015
@stevedonovan
Copy link
Contributor

Thanks, Andrew - I like robust at the moment!

@mpeterv
Copy link
Contributor

mpeterv commented Jul 18, 2016

Locally patched tostring so that it returns 123 instead of 123.0

It even returns 123 when formatting 123.456, neat.

@stevedonovan
Copy link
Contributor

On Mon, Jul 18, 2016 at 12:24 PM, Peter Melnichenko <
notifications@github.com> wrote:

It even returns 123 when formatting 123.456, neat.

This is definitely more aggressive rounding than we need ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants