kikito / inspect.lua

Human-readable representation of Lua tables
https://github.com/kikito/inspect.lua
MIT License
1.38k stars 196 forks source link

Control characters in strings are not escaped or incorrectly escaped #9

Closed Kodiologist closed 10 years ago

Kodiologist commented 10 years ago
$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> inspect = require 'inspect'
> return inspect("\0")
"\000"  -- Okay
> return inspect("\\")
"\\"    -- Okay
> return inspect("\"")
'"'     -- Okay
> return inspect("\"'")
"\"'"   -- Okay
> return string.len(inspect("\006"))
3      -- Arguably wrong: shouldn't non-printing characters be escaped?
> return inspect("\t")
"\\t"   -- Wrong: doubly escaped
> return inspect("\n")
"\\n"   -- Wrong: doubly escaped
kikito commented 10 years ago

Hi,

\t, \n etc are escaped on purpose. The rationale is that I want to be able to distinguish between " " (a bunch of spaces) and the \t character.

I can't escape all non-printing characters, since they could be part of a more complex unicode string. Handling unicode goes beyond the scope of the lib though. So I am closing this.

Kodiologist commented 10 years ago

I see your point with respect to Unicode.

The rationale is that I want to be able to distinguish between " " (a bunch of spaces) and the \t character.

I agree, but shouldn't the result of inspect("\n") display as "\n" rather than "\\n"? In other words, shouldn't inspect("\n") return the Lua value "\\n" rather than the Lua value "\\\\n"? One is the correct escape sequence and the other isn't.

kikito commented 10 years ago

Hey, you are right about that. Reopening this, will investigate. Thanks!

Kodiologist commented 10 years ago

I'd be happy to take a shot at writing a patch if you'd like.

kikito commented 10 years ago

It would help a lot if you could do that. I have spent Christmas with my family and was mostly offline. I am a bit swamped right now :)

kikito commented 10 years ago

Closed by b718a2e55c7dd4b8e8e80718041c49a768c8271d