mirror of
https://github.com/revanced/smali.git
synced 2025-05-29 20:20:12 +02:00
Improve the performance of the IndentingWriter
This commit is contained in:
parent
8c2d92d954
commit
7e58d497ef
@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012, Google Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are
|
|
||||||
* met:
|
|
||||||
*
|
|
||||||
* * Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* * Redistributions in binary form must reproduce the above
|
|
||||||
* copyright notice, this list of conditions and the following disclaimer
|
|
||||||
* in the documentation and/or other materials provided with the
|
|
||||||
* distribution.
|
|
||||||
* * Neither the name of Google Inc. nor the names of its
|
|
||||||
* contributors may be used to endorse or promote products derived from
|
|
||||||
* this software without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jf.util;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Writer;
|
|
||||||
|
|
||||||
public class CommentingIndentingWriter extends IndentingWriter {
|
|
||||||
private final String commentStr;
|
|
||||||
|
|
||||||
public CommentingIndentingWriter(Writer writer, String commentStr) {
|
|
||||||
super(writer);
|
|
||||||
this.commentStr = commentStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void writeLineStart() throws IOException {
|
|
||||||
writer.write(commentStr);
|
|
||||||
}
|
|
||||||
}
|
|
@ -43,9 +43,6 @@ public class IndentingWriter extends Writer {
|
|||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeLineStart() throws IOException {
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void writeIndent() throws IOException {
|
protected void writeIndent() throws IOException {
|
||||||
for (int i=0; i<indentLevel; i++) {
|
for (int i=0; i<indentLevel; i++) {
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
@ -54,9 +51,6 @@ public class IndentingWriter extends Writer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int chr) throws IOException {
|
public void write(int chr) throws IOException {
|
||||||
if (beginningOfLine) {
|
|
||||||
writeLineStart();
|
|
||||||
}
|
|
||||||
if (chr == '\n') {
|
if (chr == '\n') {
|
||||||
writer.write(newLine);
|
writer.write(newLine);
|
||||||
beginningOfLine = true;
|
beginningOfLine = true;
|
||||||
@ -69,34 +63,73 @@ public class IndentingWriter extends Writer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes out a block of text that contains no newlines
|
||||||
|
*/
|
||||||
|
private void writeLine(char[] chars, int start, int len) throws IOException {
|
||||||
|
if (beginningOfLine && len > 0) {
|
||||||
|
writeIndent();
|
||||||
|
beginningOfLine = false;
|
||||||
|
}
|
||||||
|
writer.write(chars, start, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes out a block of text that contains no newlines
|
||||||
|
*/
|
||||||
|
private void writeLine(String str, int start, int len) throws IOException {
|
||||||
|
if (beginningOfLine && len > 0) {
|
||||||
|
writeIndent();
|
||||||
|
beginningOfLine = false;
|
||||||
|
}
|
||||||
|
writer.write(str, start, len);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(char[] chars) throws IOException {
|
public void write(char[] chars) throws IOException {
|
||||||
for (char chr: chars) {
|
write(chars, 0, chars.length);
|
||||||
write(chr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(char[] chars, int start, int len) throws IOException {
|
public void write(char[] chars, int start, int len) throws IOException {
|
||||||
// TODO: it might improve performance to scan until we reach a newline, and then submit a full chunk of chars at once
|
final int end = start+len;
|
||||||
len = start+len;
|
int pos = start;
|
||||||
while (start < len) {
|
while (pos < end) {
|
||||||
write(chars[start++]);
|
if (chars[pos] == '\n') {
|
||||||
|
writeLine(chars, start, pos-start);
|
||||||
|
|
||||||
|
writer.write(newLine);
|
||||||
|
beginningOfLine = true;
|
||||||
|
pos++;
|
||||||
|
start = pos;
|
||||||
|
} else {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
writeLine(chars, start, pos-start);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(String s) throws IOException {
|
public void write(String s) throws IOException {
|
||||||
for (int i=0; i<s.length(); i++) {
|
write(s, 0, s.length());
|
||||||
write(s.charAt(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(String str, int start, int len) throws IOException {
|
public void write(String str, int start, int len) throws IOException {
|
||||||
len = start+len;
|
final int end = start+len;
|
||||||
while (start < len) {
|
int pos = start;
|
||||||
write(str.charAt(start++));
|
while (pos < end) {
|
||||||
|
pos = str.indexOf('\n', start);
|
||||||
|
if (pos == -1) {
|
||||||
|
writeLine(str, start, end-start);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
writeLine(str, start, pos-start);
|
||||||
|
writer.write(newLine);
|
||||||
|
beginningOfLine = true;
|
||||||
|
start = pos+1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,9 +188,7 @@ public class IndentingWriter extends Writer {
|
|||||||
value >>>= 4;
|
value >>>= 4;
|
||||||
} while (value != 0);
|
} while (value != 0);
|
||||||
|
|
||||||
while (bufferIndex>0) {
|
writeLine(buffer, 0, bufferIndex);
|
||||||
write(buffer[--bufferIndex]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printSignedIntAsDec(int value) throws IOException {
|
public void printSignedIntAsDec(int value) throws IOException {
|
||||||
@ -175,8 +206,6 @@ public class IndentingWriter extends Writer {
|
|||||||
value = value / 10;
|
value = value / 10;
|
||||||
} while (value != 0);
|
} while (value != 0);
|
||||||
|
|
||||||
while (bufferIndex>0) {
|
writeLine(buffer, 0, bufferIndex);
|
||||||
write(buffer[--bufferIndex]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user