[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    Patch to support "\x" escape sequences in string literals
From:       Doug Rogers <rogers () innocon ! com>
Date:       2001-02-23 16:06:17
Message-ID: 3A968A79.2B82EB96 () innocon ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I've modified src/llex.c to provide support for hexadecimal escape
sequences in string literals, something that I need for my own
environment. Since I know that there are others who would like this
feature, I'm attaching the diff output. After extracting lua.tar.gz
(v4.0), cd into the lua subdirectory and run "patch -p 0 <
[path-to-patch]llex-hex-escape.diff".

For example, I do the following under Linux on my machine:

    % cd /tmp
    % tar xzf /packages/Lua/lua.tar.gz
    % cd lua
    % patch -p 0 < /packages/Lua/llex-hex-escape.diff
    patching file `src/llex.c'
    % make
    ...

Note that adding this feature to the language *could* break programs
that already include "\x" in them for other purposes. Such code is
extremely unlikely. Still, you should run your unit tests again after
installing this. You do have unit tests, don't you? :)

In scanning the archives I ran across this very good suggestion from
Luiz Henrique de Figueiredo for printing strings that contain binary
characters (I switched his decimal to hex):

   gsub(s,"([0-31,127-255])",
        function (x) return format("\\x%02x",strbyte(x)) end)

Hope that helps someone!

Doug

--
--_._-__-____------_--_-_-_-___-___-____-_--_-___--____-___--_-__--___-_
Doug Rogers, ICI   | I think that people want peace so much that one
V:703.893.2007x220 | of these days governments had better get out of
www.innocon.com    | the way and let them have it. [Dwight D.
___________________| Eisenhower]



[Attachment #5 (text/html)]

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<tt></tt>&nbsp;
<br><tt>I've modified src/llex.c to provide support for hexadecimal escape</tt>
<br><tt>sequences in string literals, something that I need for my own</tt>
<br><tt>environment. Since I know that there are others who would like
this</tt>
<br><tt>feature, I'm attaching the diff output. After extracting lua.tar.gz</tt>
<br><tt>(v4.0), cd into the lua subdirectory and run "patch -p 0 &lt;</tt>
<br><tt>[path-to-patch]llex-hex-escape.diff".</tt><tt></tt>
<p><tt>For example, I do the following under Linux on my machine:</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp; % cd /tmp</tt>
<br><tt>&nbsp;&nbsp;&nbsp; % tar xzf /packages/Lua/lua.tar.gz</tt>
<br><tt>&nbsp;&nbsp;&nbsp; % cd lua</tt>
<br><tt>&nbsp;&nbsp;&nbsp; % patch -p 0 &lt; /packages/Lua/llex-hex-escape.diff</tt>
<br><tt>&nbsp;&nbsp;&nbsp; patching file `src/llex.c'</tt>
<br><tt>&nbsp;&nbsp;&nbsp; % make</tt>
<br><tt>&nbsp;&nbsp;&nbsp; ...</tt><tt></tt>
<p><tt>Note that adding this feature to the language *could* break programs</tt>
<br><tt>that already include "\x" in them for other purposes. Such code
is</tt>
<br><tt>extremely unlikely. Still, you should run your unit tests again
after</tt>
<br><tt>installing this. You do have unit tests, don't you? :)</tt><tt></tt>
<p><tt>In scanning the archives I ran across this very good suggestion
from</tt>
<br><tt>Luiz Henrique de Figueiredo for printing strings that contain binary</tt>
<br><tt>characters (I switched his decimal to hex):</tt><tt></tt>
<p><tt>&nbsp;&nbsp; gsub(s,"([0-31,127-255])",</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function (x) return
format("\\x%02x",strbyte(x)) end)</tt><tt></tt>
<p><tt>Hope that helps someone!</tt><tt></tt>
<p><tt>Doug</tt>
<pre>--&nbsp;
--_._-__-____------_--_-_-_-___-___-____-_--_-___--____-___--_-__--___-_
Doug Rogers, ICI&nbsp;&nbsp; | I think that people want peace so much that one
V:703.893.2007x220 | of these days governments had better get out of
www.innocon.com&nbsp;&nbsp;&nbsp; | the way and let them have it. [Dwight D.
___________________| Eisenhower]</pre>
&nbsp;</html>

["llex-hex-escape.diff" (text/plain)]

*** src.orig/llex.c	Thu Feb 22 14:57:23 2001
--- src/llex.c	Thu Feb 22 15:19:18 2001
***************
*** 225,230 ****
--- 225,237 ----
    seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
  }
  
+ static int hexval (char c)
+ {
+   if ((c >= '0') && (c <= '9')) return c - '0';
+   if ((c >= 'a') && (c <= 'f')) return 10 + c - 'a';
+   if ((c >= 'A') && (c <= 'F')) return 10 + c - 'A';
+   return 0;
+ }
  
  static void read_string (LexState *LS, int del, SemInfo *seminfo) {
    lua_State *L = LS->L;
***************
*** 260,265 ****
--- 267,288 ----
              if (c != (unsigned char)c) {
                save(L, '\0', l);
                luaX_error(LS, "escape sequence too large", TK_STRING);
+             }
+             save(L, c, l);
+             break;
+           }
+           case 'x': case 'X': {
+             int c = 0;
+             next(LS);
+             if (!isxdigit(LS->current)) {
+               save(L, '\0', l);
+               luaX_error(LS, "no hex digits given in hex escape sequence", TK_STRING);
+             }
+             c = hexval(LS->current);
+             next(LS);
+             if (isxdigit(LS->current)) {
+               c = 16*c + hexval(LS->current);
+               next(LS);
              }
              save(L, c, l);
              break;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic