2012-12-12 13:21:37 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.util;
|
|
|
|
|
|
|
|
public class StringUtils {
|
|
|
|
/*
|
2013-02-07 15:17:06 -08:00
|
|
|
* This method tries to guess if the given string could be a search query or URL,
|
|
|
|
* and returns a previous result if there is ambiguity
|
|
|
|
*
|
2012-12-12 13:21:37 -08:00
|
|
|
* Search examples:
|
|
|
|
* foo
|
|
|
|
* foo bar.com
|
|
|
|
* foo http://bar.com
|
|
|
|
*
|
|
|
|
* URL examples
|
|
|
|
* foo.com
|
|
|
|
* foo.c
|
|
|
|
* :foo
|
|
|
|
* http://foo.com bar
|
2013-02-07 15:17:06 -08:00
|
|
|
*
|
|
|
|
* wasSearchQuery specifies whether text was a search query before the latest change
|
|
|
|
* in text. In ambiguous cases where the new text can be either a search or a URL,
|
|
|
|
* wasSearchQuery is returned
|
2012-12-12 13:21:37 -08:00
|
|
|
*/
|
2013-02-07 15:17:06 -08:00
|
|
|
public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
|
2013-07-18 07:20:34 -07:00
|
|
|
// We remove leading and trailing white spaces when decoding URLs
|
|
|
|
text = text.trim();
|
2012-12-12 13:21:37 -08:00
|
|
|
if (text.length() == 0)
|
2013-02-07 15:17:06 -08:00
|
|
|
return wasSearchQuery;
|
2012-12-12 13:21:37 -08:00
|
|
|
|
|
|
|
int colon = text.indexOf(':');
|
|
|
|
int dot = text.indexOf('.');
|
|
|
|
int space = text.indexOf(' ');
|
|
|
|
|
2013-02-07 15:17:06 -08:00
|
|
|
// If a space is found before any dot and colon, we assume this is a search query
|
|
|
|
if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// 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;
|
2012-12-12 13:21:37 -08:00
|
|
|
}
|
2013-04-17 09:13:49 -07:00
|
|
|
|
2013-09-17 20:54:48 -07:00
|
|
|
public static class UrlFlags {
|
|
|
|
public static final int NONE = 0;
|
|
|
|
public static final int STRIP_HTTPS = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-17 09:13:49 -07:00
|
|
|
public static String stripScheme(String url) {
|
2013-09-17 20:54:48 -07:00
|
|
|
return stripScheme(url, UrlFlags.NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String stripScheme(String url, int flags) {
|
2013-07-02 12:49:06 -07:00
|
|
|
if (url == null) {
|
2013-04-17 09:13:49 -07:00
|
|
|
return url;
|
2013-07-02 12:49:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
int end = url.length();
|
2013-04-17 09:13:49 -07:00
|
|
|
|
|
|
|
if (url.startsWith("http://")) {
|
2013-07-02 12:49:06 -07:00
|
|
|
start = 7;
|
2013-09-17 20:54:48 -07:00
|
|
|
} else if (url.startsWith("https://") && flags == UrlFlags.STRIP_HTTPS) {
|
|
|
|
start = 8;
|
2013-07-02 12:49:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url.endsWith("/")) {
|
|
|
|
end--;
|
2013-04-17 09:13:49 -07:00
|
|
|
}
|
2013-07-02 12:49:06 -07:00
|
|
|
|
|
|
|
return url.substring(start, end);
|
2013-04-17 09:13:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String stripCommonSubdomains(String host) {
|
2013-07-02 12:49:06 -07:00
|
|
|
if (host == null) {
|
2013-04-17 09:13:49 -07:00
|
|
|
return host;
|
2013-07-02 12:49:06 -07:00
|
|
|
}
|
|
|
|
|
2013-04-17 09:13:49 -07:00
|
|
|
// In contrast to desktop, we also strip mobile subdomains,
|
|
|
|
// since its unlikely users are intentionally typing them
|
2013-07-02 12:49:06 -07:00
|
|
|
int start = 0;
|
2013-04-17 09:13:49 -07:00
|
|
|
|
2013-07-02 12:49:06 -07:00
|
|
|
if (host.startsWith("www.")) {
|
|
|
|
start = 4;
|
|
|
|
} else if (host.startsWith("mobile.")) {
|
|
|
|
start = 7;
|
|
|
|
} else if (host.startsWith("m.")) {
|
|
|
|
start = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return host.substring(start);
|
|
|
|
}
|
2012-12-12 13:21:37 -08:00
|
|
|
}
|