Avoid String#sub with empty pattern to make endless loop [#534 state:resolved]

Signed-off-by: Sam Stephenson <sam@37signals.com>
This commit is contained in:
Samuel Lebeau 2009-02-05 09:59:00 +08:00 committed by Sam Stephenson
parent 9bb3d7517d
commit ceb7b72621
2 changed files with 10 additions and 0 deletions

View File

@ -39,6 +39,11 @@ Object.extend(String.prototype, (function() {
var result = '', source = this, match;
replacement = prepareReplacement(replacement);
if (!(pattern.length || pattern.source)) {
replacement = replacement('');
return replacement + source.split('').join(replacement) + replacement;
}
while (source.length > 0) {
if (match = source.match(pattern)) {
result += source.slice(0, match.index);

View File

@ -44,6 +44,11 @@ new Test.Unit.Runner({
'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>'));
this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
source.gsub('', 'bar'));
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
source.gsub(new RegExp(''), 'bar'));
},
testGsubWithReplacementTemplateString: function() {