StringUtils: justify: fix crash, supports HR lines
authorNiki Roo <niki@nikiroo.be>
Thu, 12 Apr 2018 11:31:59 +0000 (13:31 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 12 Apr 2018 11:31:59 +0000 (13:31 +0200)
src/be/nikiroo/utils/StringUtils.java

index b8468a132430f624775249180cc424f2c462bec1..2c90d29d0e1bc45c1cb4dfadfb64d4cd7988ee21 100644 (file)
@@ -267,7 +267,8 @@ public class StringUtils {
 
                                previousLineComplete = current.isEmpty()
                                                || previousItemBulletSpacing != null
-                                               || (previous != null && isFullLine(previous));
+                                               || (previous != null && isFullLine(previous))
+                                               || isHrLine(current) || isHrLine(previous);
                        }
 
                        if (previous == null) {
@@ -575,7 +576,8 @@ public class StringUtils {
 
        static private boolean isItemLine(String line) {
                String spacing = getItemSpacing(line);
-               return spacing != null && line.charAt(spacing.length()) == '-';
+               return spacing != null && !spacing.isEmpty()
+                               && line.charAt(spacing.length()) == '-';
        }
 
        static private String getItemSpacing(String line) {
@@ -588,4 +590,22 @@ public class StringUtils {
 
                return "";
        }
+
+       static private boolean isHrLine(CharSequence line) {
+               int count = 0;
+               if (line != null) {
+                       for (int i = 0; i < line.length(); i++) {
+                               char car = line.charAt(i);
+                               if (car == ' ' || car == '\t' || car == '*' || car == '-'
+                                               || car == '_' || car == '~' || car == '=' || car == '/'
+                                               || car == '\\') {
+                                       count++;
+                               } else {
+                                       return false;
+                               }
+                       }
+               }
+
+               return count > 2;
+       }
 }