assemblyscript-regex
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -12,7 +12,7 @@ { | ||
"textFile": "build/optimized.wat", | ||
"sourceMap": true, | ||
"sourceMap": false, | ||
"optimizeLevel": 3, | ||
"shrinkLevel": 1, | ||
"converge": false, | ||
"noAssert": false | ||
"shrinkLevel": 0, | ||
"converge": true, | ||
"noAssert": true | ||
}, | ||
@@ -19,0 +19,0 @@ "test": { |
@@ -46,1 +46,10 @@ import { expectMatch, expectNotMatch } from "./utils"; | ||
}); | ||
it("supports case insensitive matching", () => { | ||
// simple ranges | ||
expectMatch("[a-c]", ["A", "C", "a", "c"], "i"); | ||
expectNotMatch("[a-c]", ["D", "d"], "i"); | ||
// complex | ||
expectMatch("[W-c]", ["W", "w", "C", "c"], "i"); | ||
expectNotMatch("[W-c]", ["V", "v", "D", "d"], "i"); | ||
}); |
import { RegExp, Match } from ".."; | ||
export function expectMatch(regex: string, arr: string[]): void { | ||
let regexp = new RegExp(regex); | ||
export function expectMatch( | ||
regex: string, | ||
arr: string[], | ||
flags: string = "" | ||
): void { | ||
let regexp = new RegExp(regex, flags); | ||
for (let i = 0; i < arr.length; i++) { | ||
@@ -12,4 +16,8 @@ const value = arr[i]; | ||
export function expectNotMatch(regex: string, arr: string[]): void { | ||
let regexp = new RegExp(regex); | ||
export function expectNotMatch( | ||
regex: string, | ||
arr: string[], | ||
flags: string = "" | ||
): void { | ||
let regexp = new RegExp(regex, flags); | ||
for (let i = 0; i < arr.length; i++) { | ||
@@ -16,0 +24,0 @@ const match = regexp.exec(arr[i]); |
@@ -11,2 +11,3 @@ import { isDigit, isAlpha, isWhitespace, Char } from "../char"; | ||
import { Flags } from "../regexp"; | ||
import { Range } from "../util"; | ||
@@ -40,3 +41,6 @@ const enum MatcherType { | ||
): CharacterRangeMatcher { | ||
return new CharacterRangeMatcher(node.from, node.to, flags.ignoreCase); | ||
return new CharacterRangeMatcher( | ||
new Range(node.from, node.to), | ||
flags.ignoreCase | ||
); | ||
} | ||
@@ -94,8 +98,22 @@ | ||
const LOWERCASE_LETTERS = new Range(Char.a, Char.z); | ||
const UPPERCASE_LETTERS = new Range(Char.A, Char.Z); | ||
const UPPER_LOWER_OFFSET = Char.a - Char.A; | ||
export class CharacterRangeMatcher extends Matcher { | ||
constructor(private from: u32, private to: u32, private ignoreCase: bool) { | ||
private ranges: Range[]; | ||
constructor(private range: Range, ignoreCase: bool) { | ||
super(MatcherType.CharacterRange); | ||
this.ranges = [range]; | ||
if (ignoreCase) { | ||
this.from |= 0x20; | ||
this.to |= 0x20; | ||
const lowerIntersect = range.intersection(LOWERCASE_LETTERS); | ||
if (lowerIntersect) { | ||
this.ranges.push(lowerIntersect.offset(-UPPER_LOWER_OFFSET)); | ||
} | ||
const upperIntersect = range.intersection(UPPERCASE_LETTERS); | ||
if (upperIntersect) { | ||
this.ranges.push(upperIntersect.offset(UPPER_LOWER_OFFSET)); | ||
} | ||
} | ||
@@ -105,6 +123,8 @@ } | ||
matches(code: u32): bool { | ||
if (this.ignoreCase) { | ||
code |= 0x20; | ||
for (let i = 0, len = this.ranges.length; i < len; i++) { | ||
if (code >= u32(this.ranges[i].from) && code <= u32(this.ranges[i].to)) { | ||
return true; | ||
} | ||
} | ||
return code >= this.from && code <= this.to; | ||
return false; | ||
} | ||
@@ -111,0 +131,0 @@ } |
@@ -14,1 +14,15 @@ export function last<T>(arr: T[]): T { | ||
} | ||
export class Range { | ||
constructor(public from: i32, public to: i32) {} | ||
intersection(other: Range): Range | null { | ||
const lower = i32(Math.max(this.from, other.from)); | ||
const upper = i32(Math.min(this.to, other.to)); | ||
return lower < upper ? new Range(lower, upper) : null; | ||
} | ||
offset(value: i32): Range { | ||
return new Range(this.from + value, this.to + value); | ||
} | ||
} |
@@ -8,13 +8,10 @@ global.TextDecoder = require("text-encoding").TextDecoder; | ||
wasmModule = loader.instantiateSync( | ||
fs.readFileSync("./build/debug/assemblyscript-regex.wasm"), | ||
{ | ||
env: { | ||
log: () => { | ||
const { __getString } = wasmModule.exports; | ||
console.log(__getString(strPtr)); | ||
}, | ||
wasmModule = loader.instantiateSync(fs.readFileSync("./build/optimized.wasm"), { | ||
env: { | ||
log: () => { | ||
const { __getString } = wasmModule.exports; | ||
console.log(__getString(strPtr)); | ||
}, | ||
} | ||
); | ||
}, | ||
}); | ||
@@ -21,0 +18,0 @@ // the executeRegExp exported function is ex |
{ | ||
"name": "assemblyscript-regex", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "A regex engine built with AssemblyScript", | ||
@@ -14,4 +14,4 @@ "ascMain": "assembly/index.ts", | ||
"eslint:write": "npm run eslint -- --fix ", | ||
"asbuild:untouched": "asb --target debug", | ||
"asbuild:optimized": "asb", | ||
"asbuild:untouched": "asc assembly/index.ts --target debug", | ||
"asbuild:optimized": "asc assembly/index.ts --target release", | ||
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized", | ||
@@ -30,3 +30,2 @@ "tsrun": "ts-node ts/index.ts", | ||
"@typescript-eslint/parser": "^4.14.1", | ||
"asbuild": "0.0.10", | ||
"assemblyscript": "^0.18.0", | ||
@@ -33,0 +32,0 @@ "benchmark": "^2.1.4", |
@@ -24,4 +24,7 @@ const fs = require("fs"); | ||
"test contains an octal escape sequence": [1102], | ||
// the test results measure captured groups using character length / locations | ||
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length | ||
// this is tricky to reproduce | ||
"test requires a substring function": [1087], | ||
"requires triage": [ | ||
1087, | ||
1363, | ||
@@ -36,11 +39,6 @@ 1369, | ||
], | ||
"as-pect test issue": [1145, 1146], | ||
"test indicates a malformed regex, whereas it appears OK in JS": [1189], | ||
"test regex contains syntax not supported in JS": [82, 1158, 281], | ||
"the test behaviour differs between PCRE and JS": [290], | ||
"aspect [Actual]: <Match>null vs [Expected]: Not <Match>null issue": [ | ||
153, | ||
203, | ||
204, | ||
], | ||
"test appears to be incorrect?": [203, 204], | ||
}; | ||
@@ -47,0 +45,0 @@ |
@@ -8,8 +8,4 @@ import "assemblyscript/std/portable/index"; | ||
const regexObj = new RegExp("^(a){1,3}"); | ||
const match = regexObj.exec("abc"); | ||
const regexObj = new RegExp("[a-c]", "i"); | ||
const match = regexObj.exec("A"); | ||
console.log(JSON.stringify(match, null, 2)); | ||
const regexObj2 = new RegExp("(a|b)c|a(b|c)"); | ||
const match2 = regexObj2.exec("ab"); | ||
console.log(JSON.stringify(match2, null, 2)); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
254397
14
4506