lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In an ideal world we’d lose string escapes altogether and have compile-time concatenating with coercion of numeric constants to single-character strings, and compiler-defined constants for the common ASCII control codes:

 

Mystring = “something”..[23]..”something else”..[0x33]..[CR]

 

IMHO string escapes and their bastard cousins, format codes are amongst the worst of the legacy of “C” and how anyone can describe them as “principle of least surprise” defeats me. Certainly Windows programmers get a very nasty surprise every time they forget to double up the backslashes in file specifications! I’ve been programming in C for twenty years now, and Lua for 5, and have found these conventions completely un-learnable – I have to consult a reference book every time.

 

But it is probably too late for change now and if we must have escape code there may as well be a full set of the damn things!

 

From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Joseph Stewart
Sent: 11 March 2009 21:33
To: Lua list
Subject: Re: Feature proposal: \x## notation in strings

 

How about this instead:

 

function binary(t)

  local r = {}

  for i=1,#t do

    if type(t[i]) == "number" then t[i] = string.char(t[i]) end

  end

  return table.concat(t)

end

 

x = binary{"this", 0x81, "this", 0x33}

 

I know this is less efficient than having the lexer do it, but it's an option that works without any mods to the core.

 

On Wed, Mar 11, 2009 at 5:17 PM, Sam Roberts <vieuxtech@gmail.com> wrote:

On Wed, Mar 11, 2009 at 8:14 AM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> This is also my opinion. Hex escape sequences feel more natural than
>> decimal ones (at least to programmers) :) It would be interesting to
>> know why decimal ones were chosen in the first place ?
>
> This may sound weird, but I know many people that grew up comfortable
> with decimal numbers...

All the arguments that apply to the usefulness of hex literal
representation for numbers:

i = 0x54 -- this is lua code bloat? It could have been i = 82

apply to strings

i = "\x54"

Including the argument that it can be done perfectly well with an
add-on function:

i = X(54)

I've never used a programming language with 1-based array indexing. I
didn't "expect" that, but I can work with it no problem, as with
algol-like do/end, 0 being true (actually, I expected that, but
whatever :-), and all the other areas where lua is or is not similar
to language X.

The lack of hex encodings isn't about expectations when coming from
other languages, and I regret people keep bringing that up since its
obviously not convincing.

Its about the difficulty of working with binary data in lua strings,
which otherwise are will suited for this task. Its about the printout
I keep beside my monitor that I continuously refer to when coding in
lua to map decimal back and forth to binary-friendly hex format, and
my tediously verbose code:

 "this"..string.char(0x81).."that"..string.char(0x33)

instead of

 "this\x81that\x33"


> (Also, we wanted to make easy the encoding of IPs ;)

This is the only use I can think of where decimal is routinely used to
deal with binary, and the  "familiar with decimal numbers" argument
would suggest using 2130706433 instead of 127.0.0.1 for localhost
would be more comfortable for people. Browsers support this, but I
doubt people use this "feature" very often.

I work with IPs all the time in lua, and they are more usefully
represented as hex. If you want to find the broadcast address for ip
address 10.1.96.8/22 you rewrite it in hex, "\x0a\x01\x60\x08", now
its obvious.

Anyhow, binary escape sequences in lua are for representing binary
data, and that's what feels cumbersome, even for a number of us who
have worked hard to absorb the lua way.

Cheers,
Sam

 

Attachment: smime.p7s
Description: S/MIME cryptographic signature