lua-users home
lua-l archive

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


Many years ago I *thought* I posted a source patch to allow hexadecimal escape 
sequences in Lua character strings, in hopes that it would be included into 
future versions. I was unable to find that post in the archives, though I did 
see a few messages that I remembered reading then. I'm now getting back to 
using Lua again and wanted to share this patch with those who may find it 
useful.

The procedure given below is for Unix; use cygwin or apply it manually under 
Windows.

$ tar xzf lua-5.0.tar.gz
$ cd lua-5.0
$ cat > lua-5.0-hex-patch.diff
*** src/llex-orig.c     2003-03-24 07:39:34.000000000 -0500
--- src/llex.c  2004-01-06 10:49:02.000000000 -0500
***************
*** 255,260 ****
--- 255,269 ----
  }
  
  
+ 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) {
    size_t l = 0;
    checkbuffer(LS, l);
***************
*** 281,286 ****
--- 290,311 ----
            case 't': save(LS, '\t', l); next(LS); break;
            case 'v': save(LS, '\v', l); next(LS); break;
            case '\n': save(LS, '\n', l); inclinenumber(LS); break;
+           case 'x': case 'X': {
+             int c = 0;
+             next(LS);
+             if (!isxdigit(LS->current)) {
+               save(LS, '\0', l);
+               luaX_lexerror(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(LS, c, l);
+             break;
+           }
            case EOZ: break;  /* will raise an error next loop */
            default: {
              if (!isdigit(LS->current))
***************
*** 298,305 ****
                }
                save(LS, c, l);
              }
!           }
!         }
          break;
        default:
          save_and_next(LS, l);
--- 323,330 ----
                }
                save(LS, c, l);
              }
!           }   /* default '\\' case - check for decimal digits, etc. */
!         }   /* switch on character after '\\' */
          break;
        default:
          save_and_next(LS, l);
^D
$ patch -p0 < /pub/packages/lua-5.0-hex-patch.diff
$ ./configure
$ make

Hope that helps anyone who finds this feature useful.

Doug Rogers