lua-users home
lua-l archive

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


 
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]
 
*** 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;