aboutsummaryrefslogtreecommitdiff
path: root/space.js
diff options
context:
space:
mode:
Diffstat (limited to 'space.js')
-rw-r--r--space.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/space.js b/space.js
index 65b5355..c5a0927 100644
--- a/space.js
+++ b/space.js
@@ -138,6 +138,44 @@ function Space(){
return loc;
}
+ this.regex = function(strlines){
+ if (typeof strlines[0] !== 'string') throw ".regex needs arr of strings";
+ let lines = [];
+ for (let i = 0; i < strlines.length; i++) {
+ lines.push(new RegExp(strlines[i], 'g'));
+ }
+ let data = [];
+ for (let ln of this.data){
+ let newln = "";
+ for (c of ln){
+ if (c === '') newln += '\0';
+ else newln += c;
+ }
+ data.push(newln);
+ }
+ let loc;
+ data.forEach( (ln, ind) => {
+ if (loc || ((data.length - ind) < lines.length)) return;
+ let match;
+ while ( (match = lines[0].exec(ln)) !== null){
+ let xdisp = match.index;
+ let lnmatch = true;
+ for (let i=1; lines[i] !== undefined; i+= 1){
+ let imatch = lines[i].exec(data[ind+i].slice(xdisp));
+ if (!imatch || imatch.index !== 0){
+ lnmatch = false;
+ break;
+ }
+ }
+ if (lnmatch){
+ loc = [ind, xdisp];
+ break;
+ }
+ }
+ });
+ return (loc === undefined ? [] : loc);
+ }
+
this.subsection = function(range){ // range is a coordinate pair
if (range.length !== 2 || range[0].length !== 2) throw "not subsecting a coord pair";
// Similarly excludes this.loc