diff options
author | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-18 15:52:21 -0500 |
---|---|---|
committer | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-18 15:52:21 -0500 |
commit | 2d1481ce953a869b129bf2c98d7c084d7180f8a3 (patch) | |
tree | 3b68e6eb3c9c36715db320dc9cd99bdc0e583865 | |
parent | d024510e8d2e75683f5499cac54271f1bdbfd498 (diff) |
tested search and file functions; fixed relevant bugs
-rw-r--r-- | space.js | 33 | ||||
-rw-r--r-- | tests/space_file.js | 14 | ||||
-rw-r--r-- | tests/space_search.js | 16 |
3 files changed, 50 insertions, 13 deletions
@@ -59,16 +59,24 @@ function Space(){ // CLASS return writes; } this.tofile = function(filename){ - fs.writeFile(filename, this.print(), (err)=>{console.log(err)}); + fs.writeFileSync(filename, this.print()); }; this.fromfile = function(filename){ //Reads an external file into internal data - this.adhoc(fs.readFileSync(filename)); + this.adhoc(fs.readFileSync(filename,'utf8')); } this.adhoc = function(text){ - this.data = text.split('\n').map(row => replace( row.replace(/\\(.)/g,'$1').split('') ,'&','' )); + text = text.split('\n') + text = text.map(row => row.split('')); + this.data = text.map(row => { + for (let i = 0; i<row.length; i++){ + if (row[i] == '\\') row.splice(i,1); + else if (row[i] == '&') row[i] = ''; + } + return row; + }); } this.print = function(){ - return this.data.map(row => replace(row,'','&').join('').replace(/\\|&/g, '\\$&')).join('\n'); + return this.data.map(row => replace(replace(replace(row,'&','\\&'),'\\','\\\\'),'','&').join('')).join('\n'); } this.comb = function(other, func, offset){ for (let row = offset[0]; row<Math.max(other.data.length+offset[0],this.data.length); row++){ @@ -79,16 +87,15 @@ function Space(){ // CLASS this.data[row].unshift(''); if (chr >= this.data[row].length) this.data[row].push(''); - let otherchar = other.data[row][chr]; otherchar = (typeof(otherchar) == 'undefined') ? '' : otherchar; - this.data[row][chr] = func(this.data[row][chr], otherchar); + this.data[row][chr] = func(this.data[row][chr], other.data[row][chr] || ''); console.log(row,chr,this.data[row][chr]) }} } - this.search = function(other){ - loc = []; - for (let line=0; line<=this.data.length-other.data.length; line++) for (let chr=0; chr<=this.data[y].length-other.data[0].length; chr++){ - match = true; - for (var y=0; y<other.data.length; y++) for (var x=0; x<other[y].data.length; x++){ + this.search = function(other){ //Returns first instance of a subspace (prioritized vertically then horizontally) + let loc = []; + for (let line=0; line<=this.data.length-other.data.length; line++){ for (let chr=0; chr<=this.data[line].length-other.data[0].length; chr++){ + var match = true; + for (let y=0; y<other.data.length; y++) for (let x=0; x<other.data[y].length; x++){ if (this.data[line+y][chr+x] != other.data[y][x]){ match = false; break; @@ -98,13 +105,13 @@ function Space(){ // CLASS loc = [line,chr]; break; } - } + } if (match) break;} return loc; } this.subsection = function(range){ // range is a standard quadruplet newspace = new Space(); for (let line=0; line<range[2]-range[0]; line++) for (let chr=0; chr<range[3]-range[1]; chr++){ - newspace.data[line][chr] = this.data[line+range[0]][line+range[1]]; + newspace.data[line][chr] = this.data[line+range[0]][line+range[1]] || ''; } } } diff --git a/tests/space_file.js b/tests/space_file.js new file mode 100644 index 0000000..84e42a3 --- /dev/null +++ b/tests/space_file.js @@ -0,0 +1,14 @@ +// Tests Space.{to,from}file +const space = require('../space') + +text = new space.Space(); + +text.adhoc('\ +line of text\n\ +other&&\\\\&&line of text\n\ +\n\ +final\n'); + +text.tofile('local') +text.fromfile('local') +console.log(text.print()); //Should be losslessly transmitted, and local contains a copy diff --git a/tests/space_search.js b/tests/space_search.js new file mode 100644 index 0000000..fffdcc7 --- /dev/null +++ b/tests/space_search.js @@ -0,0 +1,16 @@ +// Tests space.search + +const space = require('../space') + +let text = new space.Space(); +text.adhoc('\ +\n\ + jarvis\n\ + jarvis\n\ + jarvis \ +'); + +let line = new space.Space(); +line.adhoc('jarvis'); + +console.log(text.search(line)) |