Skip to content

Commit

Permalink
[#586] Replace Ansi.Text.clone() with copy constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jan 2, 2019
1 parent 94e937a commit e0465f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Support was added for the following environment variables to control enabling AN
- [#576] Bugfix: fixed StringIndexOutOfBoundsException in shell-jline2 completion when cursor was before `=` when option parameter was attached to option name.
- [#583] Bugfix: Default exception handler now exits on exception if exitCode was set, regardless of exception type.
- [#584] Add documentation for generating autocompletion script during a Maven build. Thanks to [Bob Tiernay](https://github.com/bobtiernay-okta).
- [#586] Replace Ansi.Text.clone() with copy constructor.

## <a name="3.9.0-deprecated"></a> Deprecations
No features were deprecated in this release.
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -10234,6 +10234,15 @@ public class Text implements Cloneable {
* @param maxLength max length of this text */
public Text(int maxLength) { this.maxLength = maxLength; }

/** Copy constructor.
* @since 3.9 */
public Text(Text other) {
this.maxLength = other.maxLength;
this.from = other.from;
this.length = other.length;
this.plain = new StringBuilder(other.plain);
this.sections = new ArrayList<StyledSection>(other.sections);
}
/**
* Constructs a Text with the specified String, which may contain markup like
* {@code @|bg(red),white,underline some text|@}.
Expand Down Expand Up @@ -10283,13 +10292,10 @@ public Text(String input) {
private void addStyledSection(int start, int length, String startStyle, String endStyle) {
sections.add(new StyledSection(start, length, startStyle, endStyle));
}
public Object clone() {
try { return super.clone(); } catch (CloneNotSupportedException e) { throw new IllegalStateException(e); }
}
public Object clone() { return new Text(this); }

public Text[] splitLines() {
List<Text> result = new ArrayList<Text>();
boolean trailingEmptyString = plain.length() == 0;
int start = 0, end = 0;
for (int i = 0; i < plain.length(); i++, end = i) {
char c = plain.charAt(i);
Expand All @@ -10298,13 +10304,11 @@ public Text[] splitLines() {
eol |= c == '\r';
if (eol) {
result.add(this.substring(start, end));
trailingEmptyString = i == plain.length() - 1;
start = i + 1;
}
}
if (start < plain.length() || trailingEmptyString) {
result.add(this.substring(start, plain.length()));
}
// add remainder (may be empty string)
result.add(this.substring(start, plain.length()));
return result.toArray(new Text[result.size()]);
}

Expand Down
9 changes: 6 additions & 3 deletions src/test/java/picocli/CommandLineHelpAnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,13 @@ ansi.new Text("@|bold 012\r\n34|@").concat("5\r\nAA\r\n6").concat("@|underline 7
@Test
public void testTextSplitLinesEmpty() {
Ansi ansi = Ansi.ON;
Ansi.Text text = ansi.new Text("");
Ansi.Text text = ansi.new Text("abc\n\n\n");
Ansi.Text[] lines = text.splitLines();
assertEquals(1, lines.length);
assertEquals(ansi.new Text(""), lines[0]);
assertEquals(4, lines.length);
assertEquals(ansi.new Text("abc"), lines[0]);
assertEquals(ansi.new Text(""), lines[1]);
assertEquals(ansi.new Text(""), lines[2]);
assertEquals(ansi.new Text(""), lines[3]);
}
@Test
public void testTextSplitLinesStartEnd() {
Expand Down

0 comments on commit e0465f7

Please sign in to comment.