]> source.dussan.org Git - sonarqube.git/commitdiff
improve Slug implementation effectiveness
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 24 May 2016 08:39:09 +0000 (10:39 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 24 May 2016 14:36:19 +0000 (16:36 +0200)
sonar-core/src/main/java/org/sonar/core/util/Slug.java

index eb96a0b8da8eaf7228377a9839c69c00cf440c69..a25ecdfd452e03f61e3ec5db577b9bcc03a86499 100644 (file)
@@ -21,19 +21,86 @@ package org.sonar.core.util;
 
 import java.text.Normalizer;
 import java.util.Locale;
+import java.util.regex.Pattern;
 
 public class Slug {
+  private static final String DASH = "-";
+  private static final Pattern NON_ASCII_CHARS = Pattern.compile("[^\\p{ASCII}]");
+  private static final Pattern NON_WORD_CHARS = Pattern.compile("[^\\w+]");
+  private static final Pattern WHITESPACES_CHARS = Pattern.compile("\\s+");
+  private static final Pattern DASHES_IN_ROWS = Pattern.compile("[-]+");
 
-  private Slug() {
+  private String in;
+
+  private Slug(String in) {
+    this.in = in;
   }
 
   public static String slugify(String s) {
-    return Normalizer.normalize(s, Normalizer.Form.NFD)
-      .replaceAll("[^\\p{ASCII}]", "")
-      .replaceAll("[^\\w+]", "-")
-      .replaceAll("\\s+", "-")
-      .replaceAll("[-]+", "-")
-      .replaceAll("^-", "")
-      .replaceAll("-$", "").toLowerCase(Locale.ENGLISH);
+    Slug slug = new Slug(s);
+    return slug
+      .normalize()
+      .removeNonAsciiChars()
+      .dashifyNonWordChars()
+      .dashifyWhitespaceChars()
+      .collapseDashes()
+      .removeHeadingDash()
+      .removeTrailingDash()
+      .toLowerCase();
+  }
+
+  private Slug normalize() {
+    this.in = Normalizer.normalize(in, Normalizer.Form.NFD);
+    return this;
+  }
+
+  private Slug removeNonAsciiChars() {
+    this.in = removeAll(NON_ASCII_CHARS, in);
+    return this;
+  }
+
+  private Slug dashifyNonWordChars() {
+    this.in = dashify(NON_WORD_CHARS, in);
+    return this;
+  }
+
+  private Slug dashifyWhitespaceChars() {
+    this.in = dashify(WHITESPACES_CHARS, in);
+    return this;
+  }
+
+  private Slug collapseDashes() {
+    this.in = dashify(DASHES_IN_ROWS, in);
+    return this;
+  }
+
+  private Slug removeHeadingDash() {
+    if (this.in.startsWith(DASH)) {
+      this.in = this.in.substring(1);
+    }
+    return this;
+  }
+
+  private Slug removeTrailingDash() {
+    if (this.in.endsWith(DASH)) {
+      this.in = this.in.substring(0, this.in.length() - 1);
+    }
+    return this;
+  }
+
+  private String toLowerCase() {
+    return in.toLowerCase(Locale.ENGLISH);
+  }
+
+  private static String removeAll(Pattern pattern, String in) {
+    return replaceAll(pattern, in, "");
+  }
+
+  private static String dashify(Pattern pattern, String in) {
+    return replaceAll(pattern, in, DASH);
+  }
+
+  private static String replaceAll(Pattern pattern, String str, String replacement) {
+    return pattern.matcher(str).replaceAll(replacement);
   }
 }