From 134196d4015888582803d2bbd36016d25e7ec196 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 12 Mar 2014 15:43:26 +0400 Subject: [PATCH] Completes java.lang.StringBuilder|StringBuffer --- .../java/lang/TAbstractStringBuilder.java | 41 +++- .../classlib/java/lang/TStringBuffer.java | 218 ++++++++++++++++++ .../classlib/java/lang/TStringBuilder.java | 6 + .../parsing/ClasspathResourceMapper.java | 4 +- .../java/org/teavm/parsing/ProgramParser.java | 2 +- 5 files changed, 266 insertions(+), 5 deletions(-) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index 9a875ce26..c4e39724d 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -648,6 +648,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ return append(s, 0, s.length()); } + protected TAbstractStringBuilder append(TStringBuffer s) { + return append((TCharSequence)s); + } + protected TAbstractStringBuilder insert(int index, TCharSequence s) { return insert(index, s, 0, s.length()); } @@ -675,8 +679,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ @Override public TCharSequence subSequence(int start, int end) { - // TODO: implement - throw new TUnsupportedOperationException(); + return substring(start, end); } public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { @@ -783,4 +786,38 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } return this; } + + public TString substring(int from, int to) { + if (from > to || from < 0 || to >= length) { + throw new TIndexOutOfBoundsException(); + } + return new TString(buffer, from, to - from); + } + + public TString substring(int from) { + return substring(from, length); + } + + public void setCharAt(int index, char ch) { + if (index > length) { + throw new TIndexOutOfBoundsException(); + } + buffer[index] = ch; + } + + public int offsetByCodePoints(int index, int codePointOffset) { + return TCharacter.offsetByCodePoints(this, index, codePointOffset); + } + + public int codePointCount(int beginIndex, int endIndex) { + return TCharacter.codePointCount(this, beginIndex, endIndex); + } + + public int codePointAt(int index) { + return TCharacter.codePointAt(this, index); + } + + public int codePointBefore(int index) { + return TCharacter.codePointBefore(this, index); + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java new file mode 100644 index 000000000..ee9921dad --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java @@ -0,0 +1,218 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +/** + * + * @author Alexey Andreev + */ +public class TStringBuffer extends TAbstractStringBuilder implements TAppendable { + public TStringBuffer(int capacity) { + super(capacity); + } + + public TStringBuffer() { + super(); + } + + public TStringBuffer(TString value) { + super(value); + } + + public TStringBuffer(TCharSequence value) { + super(value); + } + + @Override + public TStringBuffer append(TString string) { + super.append(string); + return this; + } + + @Override + public TStringBuffer append(int value) { + super.append(value); + return this; + } + + @Override + public TStringBuffer append(long value) { + super.append(value); + return this; + } + + @Override + public TStringBuffer append(float value) { + super.append(value); + return this; + } + + @Override + public TStringBuffer append(double value) { + super.append(value); + return this; + } + + @Override + public TStringBuffer append(char c) { + super.append(c); + return this; + } + + @Override + public TStringBuffer append(char[] chars, int offset, int len) { + super.append(chars, offset, len); + return this; + } + + @Override + public TStringBuffer append(char[] chars) { + super.append(chars); + return this; + } + + @Override + public TStringBuffer appendCodePoint(int codePoint) { + super.appendCodePoint(codePoint); + return this; + } + + @Override + public TStringBuffer append(TCharSequence s, int start, int end) { + super.append(s, start, end); + return this; + } + + @Override + public TStringBuffer append(TCharSequence s) { + super.append(s); + return this; + } + + @Override + public TStringBuffer append(TStringBuffer s) { + super.append(s); + return this; + } + + @Override + public TStringBuffer append(TObject obj) { + super.append(obj); + return this; + } + + @Override + public TStringBuffer append(boolean b) { + super.append(b); + return this; + } + + @Override + public TStringBuffer insert(int target, long value) { + super.insert(target, value); + return this; + } + + @Override + public TStringBuffer insert(int target, float value) { + super.insert(target, value); + return this; + } + + @Override + public TStringBuffer insert(int target, double value) { + super.insert(target, value); + return this; + } + + @Override + public TStringBuffer insert(int index, TCharSequence s, int start, int end) { + super.insert(index, s, start, end); + return this; + } + + @Override + public TStringBuffer insert(int index, TCharSequence s) { + super.insert(index, s); + return this; + } + + @Override + public TStringBuffer insert(int index, int value) { + super.insert(index, value); + return this; + } + + @Override + public TStringBuffer insert(int index, char[] chars, int offset, int len) { + super.insert(index, chars, offset, len); + return this; + } + + @Override + public TStringBuffer insert(int index, TObject obj) { + super.insert(index, obj); + return this; + } + + @Override + public TStringBuffer insert(int index, char[] chars) { + super.insert(index, chars); + return this; + } + + @Override + public TStringBuffer insert(int index, boolean b) { + super.insert(index, b); + return this; + } + + @Override + public TStringBuffer insert(int index, char c) { + super.insert(index, c); + return this; + } + + @Override + public TStringBuffer delete(int start, int end) { + super.delete(start, end); + return this; + } + + @Override + public TStringBuffer replace(int start, int end, TString str) { + super.replace(start, end, str); + return this; + } + + @Override + public TStringBuffer deleteCharAt(int index) { + deleteCharAt(index); + return this; + } + + @Override + public TStringBuffer insert(int index, TString string) { + super.insert(index, string); + return this; + } + + @Override + public TStringBuffer reverse() { + super.reverse(); + return this; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java index b07259cc5..92fd8ec96 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java @@ -102,6 +102,12 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl return this; } + @Override + public TStringBuilder append(TStringBuffer s) { + super.append(s); + return this; + } + @Override public TStringBuilder append(TObject obj) { super.append(obj); diff --git a/teavm-core/src/main/java/org/teavm/parsing/ClasspathResourceMapper.java b/teavm-core/src/main/java/org/teavm/parsing/ClasspathResourceMapper.java index 19ac6e3dc..31722768a 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/ClasspathResourceMapper.java +++ b/teavm-core/src/main/java/org/teavm/parsing/ClasspathResourceMapper.java @@ -67,7 +67,7 @@ public class ClasspathResourceMapper implements Mapper { String packageName = propertyName.substring(PACKAGE_PREFIX.length()); Transformation transformation = getTransformation(cache, packageName); transformation.packagePrefix = properties.getProperty(propertyName) + "."; - transformation.fullPrefix = transformation.packagePrefix + transformation.packageName + "."; + transformation.fullPrefix = transformation.packagePrefix + transformation.packageName; } else if (propertyName.startsWith(CLASS_PREFIX)) { String packageName = propertyName.substring(CLASS_PREFIX.length()); Transformation transformation = getTransformation(cache, packageName); @@ -80,7 +80,7 @@ public class ClasspathResourceMapper implements Mapper { Transformation transformation = cache.get(packageName); if (transformation == null) { transformation = new Transformation(); - transformation.packageName = packageName; + transformation.packageName = packageName + "."; transformation.fullPrefix = packageName + "."; cache.put(packageName, transformation); } diff --git a/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java b/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java index 8c3db9edc..f334c983e 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java +++ b/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java @@ -1102,7 +1102,7 @@ public class ProgramParser { int b = popSingle(); int a = popSingle(); int tmp = pushSingle(); - popSingle(); + pushSingle(); emitAssignInsn(a, tmp); emitAssignInsn(b, a); emitAssignInsn(tmp, b);