src/jdk/nashorn/internal/runtime/ConsString.java

Mon, 11 Feb 2013 21:26:06 +0530

author
sundar
date
Mon, 11 Feb 2013 21:26:06 +0530
changeset 82
abea4ba28901
parent 7
5a1b0714df0e
child 663
98bab0cdd7bf
permissions
-rw-r--r--

8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
Reviewed-by: lagergren, jlaskey, attila

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.runtime;
jlaskey@3 27
jlaskey@3 28 import java.util.ArrayDeque;
jlaskey@3 29 import java.util.Deque;
jlaskey@3 30
jlaskey@3 31 /**
jlaskey@3 32 * This class represents a string composed of two parts which may themselves be
jlaskey@3 33 * instances of <tt>ConsString</tt> or {@link String}. Copying of characters to
jlaskey@3 34 * a proper string is delayed until it becomes necessary.
jlaskey@3 35 */
sundar@82 36 public final class ConsString implements CharSequence {
jlaskey@3 37
jlaskey@3 38 private CharSequence left, right;
jlaskey@3 39 final private int length;
jlaskey@3 40 private boolean flat = false;
jlaskey@3 41
jlaskey@3 42 /**
jlaskey@3 43 * Constructor
jlaskey@3 44 *
jlaskey@3 45 * Takes two {@link CharSequence} instances that, concatenated, forms this {@code ConsString}
jlaskey@3 46 *
jlaskey@3 47 * @param left left char sequence
jlaskey@3 48 * @param right right char sequence
jlaskey@3 49 */
jlaskey@3 50 public ConsString(final CharSequence left, final CharSequence right) {
jlaskey@3 51 assert left instanceof String || left instanceof ConsString;
jlaskey@3 52 assert right instanceof String || right instanceof ConsString;
jlaskey@3 53 this.left = left;
jlaskey@3 54 this.right = right;
jlaskey@3 55 length = left.length() + right.length();
jlaskey@3 56 }
jlaskey@3 57
jlaskey@3 58 @Override
jlaskey@3 59 public String toString() {
jlaskey@3 60 if (!flat) {
jlaskey@3 61 flatten();
jlaskey@3 62 }
jlaskey@3 63 return (String) left;
jlaskey@3 64 }
jlaskey@3 65
jlaskey@3 66 @Override
jlaskey@3 67 public int length() {
jlaskey@3 68 return length;
jlaskey@3 69 }
jlaskey@3 70
jlaskey@3 71 @Override
jlaskey@3 72 public char charAt(final int index) {
jlaskey@3 73 if (!flat) {
jlaskey@3 74 flatten();
jlaskey@3 75 }
jlaskey@3 76 return left.charAt(index);
jlaskey@3 77 }
jlaskey@3 78
jlaskey@3 79 @Override
jlaskey@3 80 public CharSequence subSequence(final int start, final int end) {
jlaskey@3 81 if (!flat) {
jlaskey@3 82 flatten();
jlaskey@3 83 }
jlaskey@3 84 return left.subSequence(start, end);
jlaskey@3 85 }
jlaskey@3 86
jlaskey@3 87 private void flatten() {
jlaskey@3 88 // We use iterative traversal as recursion may exceed the stack size limit.
jlaskey@3 89 final char[] chars = new char[length];
jlaskey@3 90 int pos = length;
jlaskey@3 91 // Strings are most often composed by appending to the end, which causes ConsStrings
jlaskey@3 92 // to be very unbalanced, with mostly single string elements on the right and a long
jlaskey@3 93 // linear list on the left. Traversing from right to left helps to keep the stack small
jlaskey@3 94 // in this scenario.
jlaskey@3 95 final Deque<CharSequence> stack = new ArrayDeque<>();
jlaskey@3 96 stack.addFirst(left);
jlaskey@3 97 CharSequence cs = right;
jlaskey@3 98
jlaskey@3 99 do {
jlaskey@3 100 if (cs instanceof ConsString) {
jlaskey@3 101 final ConsString cons = (ConsString) cs;
jlaskey@3 102 stack.addFirst(cons.left);
jlaskey@3 103 cs = cons.right;
jlaskey@3 104 } else {
jlaskey@3 105 final String str = (String) cs;
jlaskey@3 106 pos -= str.length();
jlaskey@3 107 str.getChars(0, str.length(), chars, pos);
jlaskey@3 108 cs = stack.isEmpty() ? null : stack.pollFirst();
jlaskey@3 109 }
jlaskey@3 110 } while (cs != null);
jlaskey@3 111
jlaskey@3 112 left = new String(chars);
jlaskey@3 113 right = "";
jlaskey@3 114 flat = true;
jlaskey@3 115 }
jlaskey@3 116
jlaskey@3 117 }

mercurial