package org.mozilla.javascript;

public class ConsString implements CharSequence {

    private CharSequence s1, s2;
    private final int length, depth;

    public ConsString(CharSequence str1, CharSequence str2) {
        s1 = str1;
        s2 = str2;
        length = str1.length() + str2.length();
        int d = 1;
        if (str1 instanceof ConsString) d += ((ConsString)str1).depth;
        if (str2 instanceof ConsString) d += ((ConsString)str2).depth;
        if (d > 100) {
            flatten();
            depth = 1;
        } else {
            depth = d;
        }
    }

    public String toString() {
        if (!(s1 instanceof String) || s2 != "") {
            flatten();
        }
        return (String) s1;
    }

    private synchronized void flatten() {
        StringBuilder b = new StringBuilder(length);
        appendTo(b);
        s1 = b.toString();
        s2 = "";
    }

    private synchronized void appendTo(StringBuilder b) {
        appendFragment(s1, b);
        appendFragment(s2, b);
    }

    private static void appendFragment(CharSequence s, StringBuilder b) {
        if (s instanceof ConsString) {
            ((ConsString)s).appendTo(b);
        } else {
            b.append(s);
        }
    }

    public int length() {
        return length;
    }

    public synchronized char charAt(int index) {
        if ((index < 0) || (index >= length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        int l1 = s1.length();
        return index >= l1 ? s2.charAt(index - l1) : s1.charAt(index);
    }

    public CharSequence subSequence(int start, int end) {
        throw new UnsupportedOperationException();
    }
}

@@ -1,36 +1,2262 @@
-package org.mozilla.javascript;%0A
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-%0A *%0A * ***** BEGIN LICENSE BLOCK *****%0A * Version: MPL 1.1/GPL 2.0%0A *%0A * The contents of this file are subject to the Mozilla Public License Version%0A * 1.1 (the %22License%22); you may not use this file except in compliance with%0A * the License. You may obtain a copy of the License at%0A * http://www.mozilla.org/MPL/%0A *%0A * Software distributed under the License is distributed on an %22AS IS%22 basis,%0A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License%0A * for the specific language governing rights and limitations under the%0A * License.%0A *%0A * The Original Code is Rhino code, released%0A * May 6, 1999.%0A *%0A * The Initial Developer of the Original Code is%0A * Netscape Communications Corporation.%0A * Portions created by the Initial Developer are Copyright (C) 1997-1999%0A * the Initial Developer. All Rights Reserved.%0A *%0A * Contributor(s):%0A *   Hannes Wallnoefer%0A *%0A * Alternatively, the contents of this file may be used under the terms of%0A * the GNU General Public License Version 2 or later (the %22GPL%22), in which%0A * case the provisions of the GPL are applicable instead of those above. If%0A * you wish to allow use of your version of this file only under the terms of%0A * the GPL and not to allow others to use your version of this file under the%0A * MPL, indicate your decision by deleting the provisions above and replacing%0A * them with the notice and other provisions required by the GPL. If you do%0A * not delete the provisions above, a recipient may use your version of this%0A * file under either the MPL or the GPL.%0A *%0A * ***** END LICENSE BLOCK ***** */%0A%0Apackage org.mozilla.javascript;%0A%0A/**%0A * %3Cp%3EA string implementation optimized for concatenation using the %22+%22 operator.%0A * Instead of copying characters to a new character array, this class keeps%0A * references to the two character sequences. Characters are only converted%0A * to a String if either toString() is called or a certain depth level is%0A * reached.%3C/p%3E%0A *%0A * %3Cp%3ENote that instances of this class are only immutable if both parts are%0A * immutable, i.e. either Strings or ConsStrings that are ultimately composed%0A * of Strings.%3C/p%3E%0A *%0A * %3Cp%3E%3C/p%3EBoth the name and the concept are borrowed from V8.%3C/p%3E%0A */
 %0Apub
@@ -2514,32 +2514,110 @@
  str2.length();%0A
+        // Don't let it grow too deep, can cause stack overflows in flatten()%0A
         int d = 

Add License header and comment to ConsString class