/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Rhino code, released
 * May 6, 1999.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1997-1999
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Hannes Wallnoefer
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 *
 * ***** END LICENSE BLOCK ***** */

package org.mozilla.javascript;

/**
 * <p>This class represents a string composed of two components, each of which
 * may be a <code>java.lang.String</code> or another ConsString.</p>
 *
 * <p>This string representation is optimized for concatenation using the "+"
 * operator. Instead of immediately copying both components to a new character
 * array, ConsString keeps references to the original components and only
 * converts them to a String if either toString() is called or a certain depth
 * level is reached.</p>
 *
 * <p>Note that instances of this class are only immutable if both parts are
 * immutable, i.e. either Strings or ConsStrings that are ultimately composed
 * of Strings.</p>
 *
 * <p>Both the name and the concept are borrowed from V8.</p>
 */
public final class ConsString implements CharSequence {

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

    public ConsString(CharSequence str1, CharSequence str2) {
        s1 = str1;
        s2 = str2;
        length = str1.length() + str2.length();
        depth = 1;
        if (str1 instanceof ConsString) {
            depth += ((ConsString)str1).depth;
        }
        if (str2 instanceof ConsString) {
            depth += ((ConsString)str2).depth;
        }
        // Don't let it grow too deep, can cause stack overflows
        if (depth > 200) {
            flatten();
        }
    }

    @Override
    public String toString() {
        return depth == 0 ? (String)s1 : flatten();
    }

    private String flatten() {
        if (depth > 0) {
            StringBuilder b = new StringBuilder(length);
            appendTo(b);
            s1 = b.toString();
            s2 = "";
            depth = 0;
        }
        return (String)s1;
    }

    private 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);
        }
    }

    @Override
    public int length() {
        return length;
    }

    @Override
    public char charAt(int index) {
        return toString().charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return toString().substring(start, end);
    }

}

@@ -75,1573 +75,208 @@
 -*-%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 Source Code Form is subject to the terms of the Mozilla Public%0A* License, v. 2.0. If a copy of the MPL was not distributed with
  this
+%0A*
  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 *****
+, You can obtain one at http://mozilla.org/MPL/2.0/.
  */%0A

Update copyright notice to use MPL2