Skip to content

Commit

Permalink
fix infinite loop in split_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Sep 27, 2017
1 parent dd1df7a commit 495beb7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 50 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -13,8 +13,8 @@
* fix newline in prompt found while [answering question on SO](https://stackoverflow.com/a/46399564/387194)
* fix insert of newline in the middle of the command line if there is "word space word" and you
press space after space
* fix `iterate_formatting` reset index that was causing `split_equal` with keep words when word is longer
then the limit
* fix infinite loop in `split_equal` with keep words when word is longer than the limit and
there is space before long word


## 1.8.0
Expand Down
42 changes: 27 additions & 15 deletions js/jquery.terminal-1.8.0.js
Expand Up @@ -32,7 +32,7 @@
* Copyright (c) 2007-2013 Alexandru Marasteanu <hello at alexei dot ro>
* licensed under 3 clause BSD license
*
* Date: Tue, 26 Sep 2017 21:09:42 +0000
* Date: Wed, 27 Sep 2017 09:24:51 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2394,6 +2394,9 @@
return wcwidth;
}
})();
function text(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text();
}
// -------------------------------------------------------------------------
var is_mobile = (function(a) {
var check = false;
Expand Down Expand Up @@ -2537,9 +2540,9 @@
// :: string and execute callback with text count and other data
// ---------------------------------------------------------------------
iterate_formatting: function iterate_formatting(string, callback) {
function is_space() {
function is_space(i) {
return string.substring(i - 6, i) === '&nbsp;' ||
string.substring(i - 1, i) === ' ';
string.substring(i - 1, i).match(/\s/);
}
function match_entity(index) {
return string.substring(index).match(/^(&[^;]+;)/);
Expand All @@ -2549,7 +2552,13 @@
var count = 0;
var match;
var space = -1;
//var limit = 10000;
for (var i = 0; i < string.length; i++) {
/*
if (--limit === 0) {
break;
}
*/
match = string.substring(i).match(format_start_re);
if (match) {
formatting = match[1];
Expand All @@ -2568,15 +2577,16 @@
}
var not_formatting = (formatting && in_text) || !formatting;
var opening = string[i] === '[' && string[i + 1] === '[';
if (is_space() && (not_formatting || opening)) {
if (is_space(i) && (not_formatting || opening)) {
space = i;
}
var braket = string[i].match(/[[\]]/);
if (not_formatting) {
if (string[i] === '&') { // treat entity as one character
// treat entity as one character
if (string[i] === '&') {
match = match_entity(i);
if (match) {
i += match[1].length - 2; // because continue adds 1 to i
i += match[1].length - 2; // 2 because continue adds 1 to i
continue;
}
++count;
Expand Down Expand Up @@ -2609,7 +2619,8 @@
space = ret.space;
}
if (ret.index !== undefined) {
i = ret.index + 1;
i = ret.index;

}
}
}
Expand Down Expand Up @@ -2702,21 +2713,22 @@
(data.count === length - 1 &&
strlen(line[data.index + 1]) === 2)) {
var can_break = false;
if (keep_words) {
var text = $.terminal.strip(line.substring(data.space));
if (keep_words && data.space >= first_index + 1) {
var stripped = $.terminal.strip(line.substring(data.space));
// replace html entities with characters
text = $('<span>' + text + '</span>').text();
stripped = $('<span>' + stripped + '</span>').text();
// real length, not counting formatting
var text_len = text.length;
var text_len = stripped.length;
var limit = data.index + length + 1;
text = text.substring(0, limit);
if (text.match(/\s|&nbsp;/) || limit > text_len) {
stripped = stripped.substring(0, limit);
if (stripped.match(/\s|&nbsp;/) || limit > text_len) {
can_break = true;
}
}
// if words is true we split at last space and make next loop
// continue where the space where located
if (keep_words && !last_bracket && data.space !== -1 &&
if (keep_words && !last_bracket &&
data.space >= first_index + 1 &&
data.index !== line_length - 1 && can_break) {
output = line.substring(first_index, data.space);
var new_index = data.space - 1;
Expand Down Expand Up @@ -2955,7 +2967,7 @@
// :: return number of characters without formatting
// ---------------------------------------------------------------------
length: function(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text().length;
return text(string).length;
},
// ---------------------------------------------------------------------
// :: return string where array items are in columns padded spaces
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-1.8.0.min.js

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions js/jquery.terminal-src.js
Expand Up @@ -2394,6 +2394,9 @@
return wcwidth;
}
})();
function text(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text();
}
// -------------------------------------------------------------------------
var is_mobile = (function(a) {
var check = false;
Expand Down Expand Up @@ -2537,9 +2540,9 @@
// :: string and execute callback with text count and other data
// ---------------------------------------------------------------------
iterate_formatting: function iterate_formatting(string, callback) {
function is_space() {
function is_space(i) {
return string.substring(i - 6, i) === '&nbsp;' ||
string.substring(i - 1, i) === ' ';
string.substring(i - 1, i).match(/\s/);
}
function match_entity(index) {
return string.substring(index).match(/^(&[^;]+;)/);
Expand All @@ -2549,7 +2552,13 @@
var count = 0;
var match;
var space = -1;
//var limit = 10000;
for (var i = 0; i < string.length; i++) {
/*
if (--limit === 0) {
break;
}
*/
match = string.substring(i).match(format_start_re);
if (match) {
formatting = match[1];
Expand All @@ -2568,15 +2577,16 @@
}
var not_formatting = (formatting && in_text) || !formatting;
var opening = string[i] === '[' && string[i + 1] === '[';
if (is_space() && (not_formatting || opening)) {
if (is_space(i) && (not_formatting || opening)) {
space = i;
}
var braket = string[i].match(/[[\]]/);
if (not_formatting) {
if (string[i] === '&') { // treat entity as one character
// treat entity as one character
if (string[i] === '&') {
match = match_entity(i);
if (match) {
i += match[1].length - 2; // because continue adds 1 to i
i += match[1].length - 2; // 2 because continue adds 1 to i
continue;
}
++count;
Expand Down Expand Up @@ -2609,7 +2619,8 @@
space = ret.space;
}
if (ret.index !== undefined) {
i = ret.index + 1;
i = ret.index;

}
}
}
Expand Down Expand Up @@ -2702,21 +2713,22 @@
(data.count === length - 1 &&
strlen(line[data.index + 1]) === 2)) {
var can_break = false;
if (keep_words) {
var text = $.terminal.strip(line.substring(data.space));
if (keep_words && data.space >= first_index + 1) {
var stripped = $.terminal.strip(line.substring(data.space));
// replace html entities with characters
text = $('<span>' + text + '</span>').text();
stripped = $('<span>' + stripped + '</span>').text();
// real length, not counting formatting
var text_len = text.length;
var text_len = stripped.length;
var limit = data.index + length + 1;
text = text.substring(0, limit);
if (text.match(/\s|&nbsp;/) || limit > text_len) {
stripped = stripped.substring(0, limit);
if (stripped.match(/\s|&nbsp;/) || limit > text_len) {
can_break = true;
}
}
// if words is true we split at last space and make next loop
// continue where the space where located
if (keep_words && !last_bracket && data.space !== -1 &&
if (keep_words && !last_bracket &&
data.space >= first_index + 1 &&
data.index !== line_length - 1 && can_break) {
output = line.substring(first_index, data.space);
var new_index = data.space - 1;
Expand Down Expand Up @@ -2955,7 +2967,7 @@
// :: return number of characters without formatting
// ---------------------------------------------------------------------
length: function(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text().length;
return text(string).length;
},
// ---------------------------------------------------------------------
// :: return string where array items are in columns padded spaces
Expand Down
42 changes: 27 additions & 15 deletions js/jquery.terminal.js
Expand Up @@ -32,7 +32,7 @@
* Copyright (c) 2007-2013 Alexandru Marasteanu <hello at alexei dot ro>
* licensed under 3 clause BSD license
*
* Date: Tue, 26 Sep 2017 21:09:42 +0000
* Date: Wed, 27 Sep 2017 09:24:51 +0000
*/

/* TODO:
Expand Down Expand Up @@ -2394,6 +2394,9 @@
return wcwidth;
}
})();
function text(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text();
}
// -------------------------------------------------------------------------
var is_mobile = (function(a) {
var check = false;
Expand Down Expand Up @@ -2537,9 +2540,9 @@
// :: string and execute callback with text count and other data
// ---------------------------------------------------------------------
iterate_formatting: function iterate_formatting(string, callback) {
function is_space() {
function is_space(i) {
return string.substring(i - 6, i) === '&nbsp;' ||
string.substring(i - 1, i) === ' ';
string.substring(i - 1, i).match(/\s/);
}
function match_entity(index) {
return string.substring(index).match(/^(&[^;]+;)/);
Expand All @@ -2549,7 +2552,13 @@
var count = 0;
var match;
var space = -1;
//var limit = 10000;
for (var i = 0; i < string.length; i++) {
/*
if (--limit === 0) {
break;
}
*/
match = string.substring(i).match(format_start_re);
if (match) {
formatting = match[1];
Expand All @@ -2568,15 +2577,16 @@
}
var not_formatting = (formatting && in_text) || !formatting;
var opening = string[i] === '[' && string[i + 1] === '[';
if (is_space() && (not_formatting || opening)) {
if (is_space(i) && (not_formatting || opening)) {
space = i;
}
var braket = string[i].match(/[[\]]/);
if (not_formatting) {
if (string[i] === '&') { // treat entity as one character
// treat entity as one character
if (string[i] === '&') {
match = match_entity(i);
if (match) {
i += match[1].length - 2; // because continue adds 1 to i
i += match[1].length - 2; // 2 because continue adds 1 to i
continue;
}
++count;
Expand Down Expand Up @@ -2609,7 +2619,8 @@
space = ret.space;
}
if (ret.index !== undefined) {
i = ret.index + 1;
i = ret.index;

}
}
}
Expand Down Expand Up @@ -2702,21 +2713,22 @@
(data.count === length - 1 &&
strlen(line[data.index + 1]) === 2)) {
var can_break = false;
if (keep_words) {
var text = $.terminal.strip(line.substring(data.space));
if (keep_words && data.space >= first_index + 1) {
var stripped = $.terminal.strip(line.substring(data.space));
// replace html entities with characters
text = $('<span>' + text + '</span>').text();
stripped = $('<span>' + stripped + '</span>').text();
// real length, not counting formatting
var text_len = text.length;
var text_len = stripped.length;
var limit = data.index + length + 1;
text = text.substring(0, limit);
if (text.match(/\s|&nbsp;/) || limit > text_len) {
stripped = stripped.substring(0, limit);
if (stripped.match(/\s|&nbsp;/) || limit > text_len) {
can_break = true;
}
}
// if words is true we split at last space and make next loop
// continue where the space where located
if (keep_words && !last_bracket && data.space !== -1 &&
if (keep_words && !last_bracket &&
data.space >= first_index + 1 &&
data.index !== line_length - 1 && can_break) {
output = line.substring(first_index, data.space);
var new_index = data.space - 1;
Expand Down Expand Up @@ -2955,7 +2967,7 @@
// :: return number of characters without formatting
// ---------------------------------------------------------------------
length: function(string) {
return $('<span>' + $.terminal.strip(string) + '</span>').text().length;
return text(string).length;
},
// ---------------------------------------------------------------------
// :: return string where array items are in columns padded spaces
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

0 comments on commit 495beb7

Please sign in to comment.