kikito/inspect.lua

Control characters in strings are not escaped or incorrectly escaped

Kodiologist opened this issue · 6 comments

$ 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

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.

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.

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

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

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 :)

Closed by b718a2e