classlib: improve System.arraycopy for primitive arrays

This commit is contained in:
Alexey Andreev 2023-07-20 20:21:52 +02:00
parent 61eb666503
commit 9438380716
2 changed files with 13 additions and 10 deletions

View File

@ -53,6 +53,16 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin {
String dest = context.getParameterName(3); String dest = context.getParameterName(3);
String destPos = context.getParameterName(4); String destPos = context.getParameterName(4);
String length = context.getParameterName(5); String length = context.getParameterName(5);
writer.append("if").ws().append("(").append(length).ws().append("===").ws().append("0)").ws().append("{")
.indent().softNewLine();
writer.append("return;").ws().softNewLine();
writer.outdent().append("}").ws().append("else ");
writer.append("if").ws().append("(typeof " + src + ".data.buffer").ws().append("!==").ws()
.append("'undefined')").ws().append("{").indent().softNewLine();
writer.append(dest + ".data.set(" + src + ".data.subarray(" + srcPos + ",").ws()
.append(srcPos).ws().append("+").ws().append(length).append("),").ws()
.append(destPos).append(");").softNewLine();
writer.outdent().append("}").ws().append("else ");
writer.append("if (" + src + " !== " + dest + " || " + destPos + " < " + srcPos + ") {").indent().newLine(); writer.append("if (" + src + " !== " + dest + " || " + destPos + " < " + srcPos + ") {").indent().newLine();
writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine(); writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine();
writer.append(dest + ".data[" + destPos + "++] = " + src + ".data[" + srcPos + "++];").softNewLine(); writer.append(dest + ".data[" + destPos + "++] = " + src + ".data[" + srcPos + "++];").softNewLine();

View File

@ -47,17 +47,12 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
} }
public TString(char[] characters) { public TString(char[] characters) {
this.characters = new char[characters.length]; this(characters, 0, characters.length);
for (int i = 0; i < characters.length; ++i) {
this.characters[i] = characters[i];
}
} }
public TString(char[] value, int offset, int count) { public TString(char[] value, int offset, int count) {
this.characters = new char[count]; this.characters = new char[count];
for (int i = 0; i < count; ++i) { System.arraycopy(value, offset, this.characters, 0, count);
this.characters[i] = value[i + offset];
}
} }
static TString fromArray(char[] characters) { static TString fromArray(char[] characters) {
@ -177,9 +172,7 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
|| dstBegin + (srcEnd - srcBegin) > dst.length) { || dstBegin + (srcEnd - srcBegin) > dst.length) {
throw new TIndexOutOfBoundsException(); throw new TIndexOutOfBoundsException();
} }
while (srcBegin < srcEnd) { System.arraycopy(characters, srcBegin, dst, dstBegin, srcEnd - srcBegin);
dst[dstBegin++] = charAt(srcBegin++);
}
} }
public boolean contentEquals(TStringBuffer buffer) { public boolean contentEquals(TStringBuffer buffer) {