Skip to content
Snippets Groups Projects
Commit 8aef7fea authored by deuce's avatar deuce
Browse files

Re-write make_lines() to not use regexps mildly faster now.

parent 51e213d8
No related branches found
No related tags found
No related merge requests found
...@@ -798,55 +798,70 @@ function make_lines(str, attr, nl_is_hardcr) ...@@ -798,55 +798,70 @@ function make_lines(str, attr, nl_is_hardcr)
} }
} }
while(str.length>0) { while(str.length>0) {
var m=str.match(/^(.{0,79} *)(\n?)/); var done=false;
if(m!=null) {
/* We have zero or more characters followed by zero or more spaces followed by zero or one \n */
nl[nl.length]=new Line; nl[nl.length]=new Line;
nl[nl.length-1].text=m[1]; while(str.length>0 && !done) {
nl[nl.length-1].attr=attr.substr(0,nl[nl.length-1].text.length); switch(str.substr(0,1)) {
str=str.substr(m[0].length); case '\n':
attr=attr.substr(m[0].length); str=str.substr(1);
if(nl_is_hardcr) { attr=attr.substr(1);
if(m[2].length>0) done=true;
if(nl_is_hardcr)
nl[nl.length-1].hardcr=true; nl[nl.length-1].hardcr=true;
}
else { else {
/* Deduce if this is a hard or soft CR as follows */ /* Deduce if this is a hard or soft CR as follows */
/* If the next char is a nl, or whitespace, it is hard. */ /* If the next char is a nl, or whitespace, it is hard. */
/* If there is room for the first word of the next line on this /* If there is room for the first word of the next line on this
* line, it is hard */ * line, it is hard */
/* Otherwise, it is soft. */ /* Otherwise, it is soft. */
if(str.substr(0,1)==' ' || str.substr(0,1)=='\t')
/* Next line starts with whitespace - is hard */
if(str.search(/^\s/)!=-1)
nl[nl.length-1].hardcr=true; nl[nl.length-1].hardcr=true;
else { else {
/* Get first word of next line */ var len;
var m2=str.match(/^(.*\s)/); for(len=0; str.length>=len && str.substr(len,1)!=' ' && str.substr(len,1)!='\t'; len++);
if(m2!=null) { if(nl[nl.length-1].text.length+len < 80)
if(m[1].length+m2[1].length<80)
nl[nl.length-1].hardcr=true; nl[nl.length-1].hardcr=true;
} }
} }
/* Add trailing space if needed */ break;
if((!nl[nl.length-1].hardcr) && nl[nl.length-1].text.search(/\s^/)==-1) { case ' ': /* Whitespace... never wrap here. */
nl[nl.length-1].text+=' '; case '\t':
nl[nl.length-1].attr+=nl[nl.length-1].attr.substr(-1); nl[nl.length-1].text+=str.substr(0,1);
nl[nl.length-1].attr+=attr.substr(0,1);
str=str.substr(1);
attr=attr.substr(1);
break;
default: /* Printable char... may need to wrap */
if(nl[nl.length-1].text.length>79) { /* Need to have wrapped */
var offset;
for(offset=nl[nl.length-1].text.length-1; offset>=0; offset--) {
if(nl[nl.length-1].text.substr(offset,1)!=' ' && nl[nl.length-1].text.substr(offset,1)!='\t') {
str=nl[nl.length-1].text.substr(offset)+str;
attr=nl[nl.length-1].attr.substr(offset)+attr;
break;
} }
} }
/* If we have 79 chars in a row with no spaces and not a whitspace if(offset==-1) {
* on the end, and the forst char of the next line isn't whitespace, /* There were no spaces this line. */
* we have a kludged line */ nl[nl.length-1].text=str.substr(0,79);
if(nl[nl.length-1].text.length==79 && nl[nl.length-1].text.search(/\s/)==-1 && str.search(/^\s/)==-1) nl[nl.length-1].attr=attr.substr(0,79);
nl[nl.length-1].kludged=true; nl[nl.length-1].kludged=true;
str=str.substr(79);
attr=attr.substr(79);
}
done=true;
} }
else { else {
/* Not sure how THIS could happen */ nl[nl.length-1].text+=str.substr(0,1);
alert("The impossible happened."); nl[nl.length-1].attr+=attr.substr(0,1);
console.pause(); str=str.substr(1);
exit(); attr=attr.substr(1);
}
} }
} }
}
return(nl); return(nl);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment