diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/layoutparsing/document/graph/Boundary.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/layoutparsing/document/graph/Boundary.java index 845ce384..835c08b3 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/layoutparsing/document/graph/Boundary.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/layoutparsing/document/graph/Boundary.java @@ -140,23 +140,23 @@ public class Boundary implements Comparable { /** - * shrinks the boundary, such that textBlock.subSequence(boundary) returns a string without whitespaces. + * shrinks the boundary, such that textBlock.subSequence(boundary) returns a string without trailing or preceding whitespaces. * * @param textBlock TextBlock to check whitespaces against - * @return boundary + * @return trimmed boundary */ public Boundary trim(TextBlock textBlock) { - if (textBlock.isEmpty()) { - return textBlock.getBoundary(); + if (this.length() == 0) { + return this; } int trimmedStart = this.start; - while (Character.isWhitespace(textBlock.charAt(trimmedStart))) { + while (textBlock.containsIndex(trimmedStart) && trimmedStart < end && Character.isWhitespace(textBlock.charAt(trimmedStart))) { trimmedStart++; } int trimmedEnd = this.end; - while (Character.isWhitespace(textBlock.charAt(trimmedEnd - 1))) { + while (textBlock.containsIndex(trimmedEnd - 1) && trimmedStart < trimmedEnd && Character.isWhitespace(textBlock.charAt(trimmedEnd - 1))) { trimmedEnd--; }