-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce cons-string to make string concatenation more efficient
- Loading branch information
Showing
5 changed files
with
111 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 char charAt(int index) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public CharSequence subSequence(int start, int end) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters