Skip to content

Commit

Permalink
[js] Refactor nqp::iscclass to always use regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Dec 2, 2017
1 parent f1b2692 commit de279cc
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions src/vm/js/nqp-runtime/cclass.js
Expand Up @@ -13,50 +13,38 @@ const ALPHABETIC = xregexp('^\\pL');
const NUMERIC = xregexp('^\\p{Nd}');
const HEXADECIMAL = xregexp('^[0-9A-Fa-f]');
const WHITESPACE = xregexp('^\\p{White_Space}');
const PRINTING = xregexp('^[^\u0000-\u001F\u007F-\u009F]');
const BLANK = xregexp('^[\t\\p{Zs}]');
const NEWLINE = xregexp('[\n\r\u0085\u2029\f\u000b\u2028]');
const CONTROL = xregexp('^[\u0000-\u001F\u007F-\u009F]');
const PUNCTUATION = xregexp('^\\pP');
const ALPHANUMERIC = xregexp('^[\\pL\\p{Nd}]');
const NEWLINE = xregexp('[\n\r\u0085\u2029\f\u000b\u2028]');
const WORD = xregexp('^[\\pL_\\p{Nd}]');
const ANY = xregexp('^\\p{Any}');

const cclassToRegex = [];
cclassToRegex[1] = UPPERCASE;
cclassToRegex[2] = LOWERCASE;
cclassToRegex[4] = ALPHABETIC;
cclassToRegex[8] = NUMERIC;
cclassToRegex[16] = HEXADECIMAL;
cclassToRegex[32] = WHITESPACE;
cclassToRegex[64] = PRINTING;
cclassToRegex[256] = BLANK;
cclassToRegex[512] = CONTROL;
cclassToRegex[1024] = PUNCTUATION;
cclassToRegex[2048] = ALPHANUMERIC;
cclassToRegex[4096] = NEWLINE;
cclassToRegex[8192] = WORD;
cclassToRegex[65535] = ANY;

function iscclass(cclass, target, offset) {
if (offset < 0 || offset >= target.length) return 0;
switch (cclass) {
// ANY
case 65535: return 1;
// UPPERCASE
case 1: return boolish(UPPERCASE.test(target[offset]));
// LOWERCASE
case 2: return boolish(LOWERCASE.test(target[offset]));
// ALPHABETIC
case 4: return boolish(ALPHABETIC.test(target[offset]));
// NUMERIC
case 8: return boolish(NUMERIC.test(target[offset]));
// HEXADECIMAL
case 16: return boolish(HEXADECIMAL.test(target[offset]));
// WHITESPACE
case 32: return boolish(WHITESPACE.test(target[offset]));
// BLANK
case 256: return boolish(BLANK.test(target[offset]));
// PRINTING
case 64: {
const cp = target.codePointAt(offset);
return boolish(!((cp >= 0 && cp < 32) || (cp >= 127 && cp < 160)));
}
// CONTROL
case 512: {
const cp = target.codePointAt(offset);
return boolish((cp >= 0 && cp < 32) || (cp >= 127 && cp < 160));
}
// PUNCTUATION
case 1024: return boolish(PUNCTUATION.test(target[offset])); // HACK
// ALPHANUMERIC
case 2048: return boolish(ALPHANUMERIC.test(target[offset]));
// NEWLINE
case 4096: return boolish(NEWLINE.test(target[offset]));
// WORD
case 8192: return boolish(WORD.test(target[offset]));
default: throw 'cclass ' + cclass + ' not yet implemented';
const regex = cclassToRegex[cclass];
if (regex === undefined) {
throw 'cclass ' + cclass + ' not yet implemented';
} else if (typeof regex !== 'string') {
return boolish(regex.test(target[offset]));
}
}

Expand Down

0 comments on commit de279cc

Please sign in to comment.