Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/clipper2/core/Path64.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public Path64(Point64... path) {

@Override
public String toString() {
String s = "";
StringBuilder s = new StringBuilder();
for (Point64 p : this) {
s = s + p.toString() + " ";
s.append(p.toString()).append(" ");
}
return s;
return s.toString();
}

}
6 changes: 3 additions & 3 deletions src/main/java/clipper2/core/PathD.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public PathD(List<PointD> path) {

@Override
public String toString() {
String s = "";
StringBuilder s = new StringBuilder();
for (PointD p : this) {
s = s + p.toString() + " ";
s.append(p.toString()).append(" ");
}
return s;
return s.toString();
}

}
6 changes: 3 additions & 3 deletions src/main/java/clipper2/core/Paths64.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public Paths64(Path64... paths) {

@Override
public String toString() {
String s = "";
StringBuilder s = new StringBuilder();
for (Path64 p : this) {
s = s + p.toString() + "\n";
s.append(p.toString()).append("\n");
}
return s;
return s.toString();
}

}
6 changes: 3 additions & 3 deletions src/main/java/clipper2/core/PathsD.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public PathsD(List<PathD> paths) {

@Override
public String toString() {
String s = "";
StringBuilder s = new StringBuilder();
for (PathD p : this) {
s = s + p.toString() + "\n";
s.append(p.toString()).append("\n");
}
return s;
return s.toString();
}

}
22 changes: 12 additions & 10 deletions src/main/java/clipper2/engine/PolyPathBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,28 @@ public final void Clear() {

private String toStringInternal(int idx, int level) {
int count = children.size();
String result = "", padding = "", plural = "s";
StringBuilder result = new StringBuilder();
StringBuilder padding = new StringBuilder();
String plural = "s";
if (children.size() == 1) {
plural = "";
}
// Create padding by concatenating spaces
for (int i = 0; i < level * 2; i++) {
padding += " ";
padding.append(" ");
}

if ((level & 1) == 0) {
result += String.format("%s+- hole (%d) contains %d nested polygon%s.\n", padding, idx, children.size(), plural);
result.append(String.format("%s+- hole (%d) contains %d nested polygon%s.\n", padding, idx, children.size(), plural));
} else {
result += String.format("%s+- polygon (%d) contains %d hole%s.\n", padding, idx, children.size(), plural);
result.append(String.format("%s+- polygon (%d) contains %d hole%s.\n", padding, idx, children.size(), plural));
}
for (int i = 0; i < count; i++) {
if (children.get(i).getCount() > 0) {
result += children.get(i).toStringInternal(i, level + 1);
result.append(children.get(i).toStringInternal(i, level + 1));
}
}
return result;
return result.toString();
}

@Override
Expand All @@ -95,13 +97,13 @@ public String toString() {
if (children.size() == 1) {
plural = "";
}
String result = String.format("Polytree with %d polygon%s.\n", children.size(), plural);
StringBuilder result = new StringBuilder(String.format("Polytree with %d polygon%s.\n", children.size(), plural));
for (int i = 0; i < count; i++) {
if (children.get(i).getCount() > 0) {
result += children.get(i).toStringInternal(i, 1);
result.append(children.get(i).toStringInternal(i, 1));
}
}
return result + '\n';
return result.append('\n').toString();
}

}
}
54 changes: 54 additions & 0 deletions src/test/java/clipper2/TestToStringOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package clipper2;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import clipper2.core.Path64;
import clipper2.core.PathD;
import clipper2.core.Paths64;
import clipper2.core.PathsD;
import clipper2.core.Point64;
import clipper2.core.PointD;
import clipper2.engine.PolyPathBase;
import clipper2.engine.PolyTree64;

class TestToStringOutput {

@Test
void path64ToStringMatchesExistingFormat() {
Path64 path = new Path64(new Point64(1, 2), new Point64(3, 4));
assertEquals("(1,2) (3,4) ", path.toString());
}

@Test
void pathDToStringMatchesExistingFormat() {
PathD path = new PathD(2);
path.add(new PointD(1.5, 2.5));
path.add(new PointD(3.5, 4.5));
assertEquals("(1.500000,2.500000) (3.500000,4.500000) ", path.toString());
}

@Test
void paths64ToStringMatchesExistingFormat() {
Paths64 paths = new Paths64(new Path64(new Point64(1, 2)));
assertEquals("(1,2) \n", paths.toString());
}

@Test
void pathsDToStringMatchesExistingFormat() {
PathsD paths = new PathsD(1);
PathD path = new PathD(1);
path.add(new PointD(1.5, 2.5));
paths.add(path);
assertEquals("(1.500000,2.500000) \n", paths.toString());
}

@Test
void polyPathBaseToStringMatchesExistingFormat() {
PolyTree64 tree = new PolyTree64();
PolyPathBase polygon = tree.AddChild(new Path64());
polygon.AddChild(new Path64());
assertEquals("Polytree with 1 polygon.\n +- polygon (0) contains 1 hole.\n\n", tree.toString());
}
}