mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-24 18:37:38 +00:00
Refactor and add tests for TextBuffer (#738)
This commit is contained in:
@@ -12,46 +12,12 @@ public class TextBuffer
|
||||
public TextBuffer( char c, int length )
|
||||
{
|
||||
text = new char[length];
|
||||
for( int i = 0; i < length; i++ )
|
||||
{
|
||||
text[i] = c;
|
||||
}
|
||||
this.fill( c );
|
||||
}
|
||||
|
||||
public TextBuffer( String text )
|
||||
{
|
||||
this( text, 1 );
|
||||
}
|
||||
|
||||
public TextBuffer( String text, int repetitions )
|
||||
{
|
||||
int textLength = text.length();
|
||||
this.text = new char[textLength * repetitions];
|
||||
for( int i = 0; i < repetitions; i++ )
|
||||
{
|
||||
for( int j = 0; j < textLength; j++ )
|
||||
{
|
||||
this.text[j + i * textLength] = text.charAt( j );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TextBuffer( TextBuffer text )
|
||||
{
|
||||
this( text, 1 );
|
||||
}
|
||||
|
||||
public TextBuffer( TextBuffer text, int repetitions )
|
||||
{
|
||||
int textLength = text.length();
|
||||
this.text = new char[textLength * repetitions];
|
||||
for( int i = 0; i < repetitions; i++ )
|
||||
{
|
||||
for( int j = 0; j < textLength; j++ )
|
||||
{
|
||||
this.text[j + i * textLength] = text.charAt( j );
|
||||
}
|
||||
}
|
||||
this.text = text.toCharArray();
|
||||
}
|
||||
|
||||
public int length()
|
||||
@@ -59,39 +25,16 @@ public class TextBuffer
|
||||
return text.length;
|
||||
}
|
||||
|
||||
public String read()
|
||||
{
|
||||
return read( 0, text.length );
|
||||
}
|
||||
|
||||
public String read( int start )
|
||||
{
|
||||
return read( start, text.length );
|
||||
}
|
||||
|
||||
public String read( int start, int end )
|
||||
{
|
||||
start = Math.max( start, 0 );
|
||||
end = Math.min( end, text.length );
|
||||
int textLength = Math.max( end - start, 0 );
|
||||
return new String( text, start, textLength );
|
||||
}
|
||||
|
||||
public void write( String text )
|
||||
{
|
||||
write( text, 0, text.length() );
|
||||
write( text, 0 );
|
||||
}
|
||||
|
||||
public void write( String text, int start )
|
||||
{
|
||||
write( text, start, start + text.length() );
|
||||
}
|
||||
|
||||
public void write( String text, int start, int end )
|
||||
{
|
||||
int pos = start;
|
||||
start = Math.max( start, 0 );
|
||||
end = Math.min( end, pos + text.length() );
|
||||
int end = Math.min( start + text.length(), pos + text.length() );
|
||||
end = Math.min( end, this.text.length );
|
||||
for( int i = start; i < end; i++ )
|
||||
{
|
||||
@@ -101,23 +44,10 @@ public class TextBuffer
|
||||
|
||||
public void write( TextBuffer text )
|
||||
{
|
||||
write( text, 0, text.length() );
|
||||
}
|
||||
|
||||
public void write( TextBuffer text, int start )
|
||||
{
|
||||
write( text, start, start + text.length() );
|
||||
}
|
||||
|
||||
public void write( TextBuffer text, int start, int end )
|
||||
{
|
||||
int pos = start;
|
||||
start = Math.max( start, 0 );
|
||||
end = Math.min( end, pos + text.length() );
|
||||
end = Math.min( end, this.text.length );
|
||||
for( int i = start; i < end; i++ )
|
||||
int end = Math.min( text.length(), this.text.length );
|
||||
for( int i = 0; i < end; i++ )
|
||||
{
|
||||
this.text[i] = text.charAt( i - pos );
|
||||
this.text[i] = text.charAt( i );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,11 +56,6 @@ public class TextBuffer
|
||||
fill( c, 0, text.length );
|
||||
}
|
||||
|
||||
public void fill( char c, int start )
|
||||
{
|
||||
fill( c, start, text.length );
|
||||
}
|
||||
|
||||
public void fill( char c, int start, int end )
|
||||
{
|
||||
start = Math.max( start, 0 );
|
||||
@@ -141,52 +66,6 @@ public class TextBuffer
|
||||
}
|
||||
}
|
||||
|
||||
public void fill( String text )
|
||||
{
|
||||
fill( text, 0, this.text.length );
|
||||
}
|
||||
|
||||
public void fill( String text, int start )
|
||||
{
|
||||
fill( text, start, this.text.length );
|
||||
}
|
||||
|
||||
public void fill( String text, int start, int end )
|
||||
{
|
||||
int pos = start;
|
||||
start = Math.max( start, 0 );
|
||||
end = Math.min( end, this.text.length );
|
||||
|
||||
int textLength = text.length();
|
||||
for( int i = start; i < end; i++ )
|
||||
{
|
||||
this.text[i] = text.charAt( (i - pos) % textLength );
|
||||
}
|
||||
}
|
||||
|
||||
public void fill( TextBuffer text )
|
||||
{
|
||||
fill( text, 0, this.text.length );
|
||||
}
|
||||
|
||||
public void fill( TextBuffer text, int start )
|
||||
{
|
||||
fill( text, start, this.text.length );
|
||||
}
|
||||
|
||||
public void fill( TextBuffer text, int start, int end )
|
||||
{
|
||||
int pos = start;
|
||||
start = Math.max( start, 0 );
|
||||
end = Math.min( end, this.text.length );
|
||||
|
||||
int textLength = text.length();
|
||||
for( int i = start; i < end; i++ )
|
||||
{
|
||||
this.text[i] = text.charAt( (i - pos) % textLength );
|
||||
}
|
||||
}
|
||||
|
||||
public char charAt( int i )
|
||||
{
|
||||
return text[i];
|
||||
|
||||
Reference in New Issue
Block a user