Search code examples
regexlualua-patternscharacter-class

Exact Ascii codes matched by Lua patterns


I am translating a project from Lua to C++. In the Lua version, I use Lua's regular expressions, but for a purpose so simple that in C++ I could do it by simply comparing the characters against some Ascii codes.

To do that, however, I need the exact ascii code which each character class matches.

As an example, %s matches all space characters, but what exactly are those characters? I need to know it for every Lua character class.


Solution

  • Take a peek at the Lua source:

    case 'a' : res = isalpha(c); break;
    case 'c' : res = iscntrl(c); break;
    case 'd' : res = isdigit(c); break;
    case 'g' : res = isgraph(c); break;
    case 'l' : res = islower(c); break;
    case 'p' : res = ispunct(c); break;
    case 's' : res = isspace(c); break;
    case 'u' : res = isupper(c); break;
    case 'w' : res = isalnum(c); break;
    case 'x' : res = isxdigit(c); break;
    case 'z' : res = (c == 0); break;  /* deprecated option */
    

    You can see that there are analogous methods in C++ <cctype> (ctype.h):

    isalnum     Check if character is alphanumeric (function )
    isalpha     Check if character is alphabetic (function )
    isblank     Check if character is blank (function )
    iscntrl     Check if character is a control character (function )
    isdigit     Check if character is decimal digit (function )
    isgraph     Check if character has graphical representation (function )
    islower     Check if character is lowercase letter (function )
    isprint     Check if character is printable (function )
    ispunct     Check if character is a punctuation character (function )
    isspace     Check if character is a white-space (function )
    isupper     Check if character is uppercase letter (function )
    isxdigit    Check if character is hexadecimal digit (function )
    

    There are also corresponding ASCII value ranges on that page.