From ceb7b72621e1dea71948571da642281c5decf30c Mon Sep 17 00:00:00 2001 From: Samuel Lebeau Date: Thu, 5 Feb 2009 09:59:00 +0800 Subject: [PATCH] Avoid String#sub with empty pattern to make endless loop [#534 state:resolved] Signed-off-by: Sam Stephenson --- src/lang/string.js | 5 +++++ test/unit/string_test.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/lang/string.js b/src/lang/string.js index 844d6d9..5530ebb 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -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); diff --git a/test/unit/string_test.js b/test/unit/string_test.js index ab20366..3590579 100644 --- a/test/unit/string_test.js +++ b/test/unit/string_test.js @@ -44,6 +44,11 @@ new Test.Unit.Runner({ 'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'
')); this.assertEqual('ウィメンズ2007
クルーズコレクション', 'ウィメンズ2007\nクルーズコレクション'.gsub('\n','
')); + + this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar', + source.gsub('', 'bar')); + this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar', + source.gsub(new RegExp(''), 'bar')); }, testGsubWithReplacementTemplateString: function() {