Bug 1265708 - Pre: Implement StringUtils.stripRef r=sebastian
authorAndrzej Hunt <ahunt@mozilla.com>
Sun, 05 Jun 2016 17:37:12 -0700
changeset 339408 dc03fe0bad2a83df21395abee0a0574387642982
parent 339407 1a529039f1a746a54ce1e727ad80595723d302ba
child 339409 1888dfa820b765ff63f7a5c1c223857100fe9d98
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1265708
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1265708 - Pre: Implement StringUtils.stripRef r=sebastian MozReview-Commit-ID: AO25iiUMXLE
mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
@@ -57,16 +57,36 @@ public class StringUtils {
         // Otherwise, if a dot or a colon is found, we assume this is a URL
         if (dot > -1 || colon > -1) {
             return false;
         }
         // Otherwise, text is ambiguous, and we keep its status unchanged
         return wasSearchQuery;
     }
 
+    /**
+     * Strip the ref from a URL, if present
+     *
+     * @return The base URL, without the ref. The original String is returned if it has no ref,
+     *         of if the input is malformed.
+     */
+    public static String stripRef(final String inputURL) {
+        if (inputURL == null) {
+            return null;
+        }
+
+        final int refIndex = inputURL.indexOf('#');
+
+        if (refIndex >= 0) {
+            return inputURL.substring(0, refIndex);
+        }
+
+        return inputURL;
+    }
+
     public static class UrlFlags {
         public static final int NONE = 0;
         public static final int STRIP_HTTPS = 1;
     }
 
     public static String stripScheme(String url) {
         return stripScheme(url, UrlFlags.NONE);
     }
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
@@ -6,16 +6,18 @@
 package org.mozilla.gecko.util;
 
 import junit.framework.Assert;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 
+import static org.junit.Assert.assertEquals;
+
 @RunWith(TestRunner.class)
 public class TestStringUtils {
     @Test
     public void testIsHttpOrHttps() {
         // No value
         Assert.assertFalse(StringUtils.isHttpOrHttps(null));
         Assert.assertFalse(StringUtils.isHttpOrHttps(""));
 
@@ -35,9 +37,20 @@ public class TestStringUtils {
         Assert.assertFalse(StringUtils.isHttpOrHttps("ftp://people.mozilla.org"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("javascript:window.google.com"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("tel://1234567890"));
 
         // No scheme
         Assert.assertFalse(StringUtils.isHttpOrHttps("google.com"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("git@github.com:mozilla/gecko-dev.git"));
     }
+
+    @Test
+    public void testStripRef() {
+        assertEquals(StringUtils.stripRef(null), null);
+        assertEquals(StringUtils.stripRef(""), "");
+
+        assertEquals(StringUtils.stripRef("??AAABBBCCC"), "??AAABBBCCC");
+        assertEquals(StringUtils.stripRef("https://mozilla.org"), "https://mozilla.org");
+        assertEquals(StringUtils.stripRef("https://mozilla.org#BBBB"), "https://mozilla.org");
+        assertEquals(StringUtils.stripRef("https://mozilla.org/#BBBB"), "https://mozilla.org/");
+    }
 }