1// Copyright 2012 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Make sure the strings are long enough to trigger the one-char string replace. 29var prefix1024 = "0123456789ABCDEF"; 30for (var i = 0; i < 6; i++) prefix1024 += prefix1024; 31 32function test_replace(result, expected, search, replace) { 33 assertEquals(expected, result.replace(search, replace)); 34} 35 36// '$' in the replace string. 37test_replace(prefix1024 + "abcdefghijklmnopqrstuvwxyz", 38 prefix1024 + "abcdefghijk#l#mnopqrstuvwxyz", 39 "l", "#$&#"); 40 41test_replace(prefix1024 + "abcdefghijklmnopqrstuvwxyz\u1234", 42 prefix1024 + "abcdefghijk\u2012l\u2012mnopqrstuvwxyz\u1234", 43 "l", "\u2012$&\u2012"); 44 45test_replace(prefix1024 + "abcdefghijklmnopqrstuvwxyz", 46 prefix1024 + "abcdefghijk$mnopqrstuvwxyz", 47 "l", "$$"); 48 49test_replace(prefix1024 + "abcdefghijklmnopqrstuvwxyz\u1234", 50 prefix1024 + "abcdefghijk$mnopqrstuvwxyz\u1234", 51 "l", "$$"); 52 53// Zero length replace string. 54test_replace(prefix1024 + "abcdefghijklmnopqrstuvwxyz", 55 prefix1024 + "abcdefghijklmnopqstuvwxyz", 56 "r", ""); 57 58test_replace(prefix1024 + "abcdefghijklmnopq\u1234stuvwxyz", 59 prefix1024 + "abcdefghijklmnopqstuvwxyz", 60 "\u1234", ""); 61 62// Search char not found. 63var not_found_1 = prefix1024 + "abcdefghijklmnopqrstuvwxyz"; 64test_replace(not_found_1, not_found_1, "+", "-"); 65 66var not_found_2 = prefix1024 + "abcdefghijklm\u1234nopqrstuvwxyz"; 67test_replace(not_found_2, not_found_2, "+", "---"); 68 69var not_found_3 = prefix1024 + "abcdefghijklmnopqrstuvwxyz"; 70test_replace(not_found_3, not_found_3, "\u1234", "ZZZ"); 71 72// Deep cons tree. 73var nested_1 = ""; 74for (var i = 0; i < 100000; i++) nested_1 += "y"; 75var nested_1_result = prefix1024 + nested_1 + "aa"; 76nested_1 = prefix1024 + nested_1 + "z"; 77test_replace(nested_1, nested_1_result, "z", "aa"); 78 79var nested_2 = "\u2244"; 80for (var i = 0; i < 100000; i++) nested_2 += "y"; 81var nested_2_result = prefix1024 + nested_2 + "aa"; 82nested_2 = prefix1024 + nested_2 + "\u2012"; 83test_replace(nested_2, nested_2_result, "\u2012", "aa"); 84 85// Sliced string as input. A cons string is always flattened before sliced. 86var slice_1 = ("ab" + prefix1024 + "cdefghijklmnopqrstuvwxyz").slice(1, -1); 87var slice_1_result = "b" + prefix1024 + "cdefghijklmnopqrstuvwxQ"; 88test_replace(slice_1, slice_1_result, "y", "Q"); 89 90var slice_2 = (prefix1024 + "abcdefghijklmno\u1234\u1234p").slice(1, -1); 91var slice_2_result = prefix1024.substr(1) + "abcdefghijklmnoQ\u1234"; 92test_replace(slice_2, slice_2_result, "\u1234", "Q"); 93