diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-15 09:30:41 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-15 09:30:41 -0300 |
| commit | 067f761739f4cfa29384dca8b35c532978d0f372 (patch) | |
| tree | 1d8635291159400a0f2a4653f2da792b702f10ff | |
| parent | 7978a8d8b22b9f9d0e3d5689910be7fb3a4a02fc (diff) | |
| download | lua-067f761739f4cfa29384dca8b35c532978d0f372.tar.gz lua-067f761739f4cfa29384dca8b35c532978d0f372.tar.bz2 lua-067f761739f4cfa29384dca8b35c532978d0f372.zip | |
better code for numerical escapes
| -rw-r--r-- | llex.c | 47 |
1 files changed, 19 insertions, 28 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 2.52 2011/07/08 19:17:30 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 2.53 2011/07/08 20:01:38 roberto Exp roberto $ |
| 3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -297,39 +297,30 @@ static void escerror (LexState *ls, int *c, int n, const char *msg) { | |||
| 297 | 297 | ||
| 298 | 298 | ||
| 299 | static int readhexaesc (LexState *ls) { | 299 | static int readhexaesc (LexState *ls) { |
| 300 | int c[3]; /* keep input for error message */ | 300 | int c[3], i; /* keep input for error message */ |
| 301 | int i = 2; /* at least 'x?' will go to error message */ | 301 | int r = 0; /* result accumulator */ |
| 302 | c[0] = 'x'; | 302 | c[0] = 'x'; /* for error message */ |
| 303 | c[1] = next(ls); /* first hexa digit */ | 303 | for (i = 1; i < 3; i++) { /* read two hexa digits */ |
| 304 | if (lisxdigit(c[1])) { | 304 | c[i] = next(ls); |
| 305 | c[i++] = next(ls); /* second hexa digit */ | 305 | if (!lisxdigit(c[i])) |
| 306 | if (lisxdigit(c[2])) | 306 | escerror(ls, c, i + 1, "hexadecimal digit expected"); |
| 307 | return (luaO_hexavalue(c[1]) << 4) + luaO_hexavalue(c[2]); | 307 | r = (r << 4) + luaO_hexavalue(c[i]); |
| 308 | /* else go through to error */ | ||
| 309 | } | 308 | } |
| 310 | escerror(ls, c, i, "hexadecimal digit expected"); | 309 | return r; |
| 311 | return 0; /* to avoid warnings */ | ||
| 312 | } | 310 | } |
| 313 | 311 | ||
| 314 | 312 | ||
| 315 | static int readdecesc (LexState *ls) { | 313 | static int readdecesc (LexState *ls) { |
| 316 | int c[3], r; | 314 | int c[3], i; |
| 317 | int i = 2; /* at least two chars will be read */ | 315 | int r = 0; /* result accumulator */ |
| 318 | c[0] = ls->current; /* first char must be a digit */ | 316 | for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ |
| 319 | c[1] = next(ls); /* read second char */ | 317 | c[i] = ls->current; |
| 320 | r = c[0] - '0'; /* partial result */ | 318 | r = 10*r + c[i] - '0'; |
| 321 | if (lisdigit(c[1])) { | 319 | next(ls); |
| 322 | c[i++] = next(ls); /* read third char */ | ||
| 323 | r = 10*r + c[1] - '0'; /* update result */ | ||
| 324 | if (lisdigit(c[2])) { | ||
| 325 | r = 10*r + c[2] - '0'; /* update result */ | ||
| 326 | if (r > UCHAR_MAX) | ||
| 327 | escerror(ls, c, i, "decimal escape too large"); | ||
| 328 | return r; | ||
| 329 | } | ||
| 330 | } | 320 | } |
| 331 | /* else, has read one character that was not a digit */ | 321 | if (r > UCHAR_MAX) |
| 332 | zungetc(ls->z); /* return it to input stream */ | 322 | escerror(ls, c, i, "decimal escape too large"); |
| 323 | zungetc(ls->z); | ||
| 333 | return r; | 324 | return r; |
| 334 | } | 325 | } |
| 335 | 326 | ||
