mobile/android/base/util/StringUtils.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.util;
     8 import android.net.Uri;
     9 import android.text.TextUtils;
    11 public class StringUtils {
    13     private static final String FILTER_URL_PREFIX = "filter://";
    15     /*
    16      * This method tries to guess if the given string could be a search query or URL,
    17      * and returns a previous result if there is ambiguity
    18      *
    19      * Search examples:
    20      *  foo
    21      *  foo bar.com
    22      *  foo http://bar.com
    23      *
    24      * URL examples
    25      *  foo.com
    26      *  foo.c
    27      *  :foo
    28      *  http://foo.com bar
    29      *
    30      * wasSearchQuery specifies whether text was a search query before the latest change
    31      * in text. In ambiguous cases where the new text can be either a search or a URL,
    32      * wasSearchQuery is returned
    33     */
    34     public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
    35         // We remove leading and trailing white spaces when decoding URLs
    36         text = text.trim();
    37         if (text.length() == 0)
    38             return wasSearchQuery;
    40         int colon = text.indexOf(':');
    41         int dot = text.indexOf('.');
    42         int space = text.indexOf(' ');
    44         // If a space is found before any dot and colon, we assume this is a search query
    45         if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
    46             return true;
    47         }
    48         // Otherwise, if a dot or a colon is found, we assume this is a URL
    49         if (dot > -1 || colon > -1) {
    50             return false;
    51         }
    52         // Otherwise, text is ambiguous, and we keep its status unchanged
    53         return wasSearchQuery;
    54     }
    56     public static class UrlFlags {
    57         public static final int NONE = 0;
    58         public static final int STRIP_HTTPS = 1;
    59     }
    61     public static String stripScheme(String url) {
    62         return stripScheme(url, UrlFlags.NONE);
    63     }
    65     public static String stripScheme(String url, int flags) {
    66         if (url == null) {
    67             return url;
    68         }
    70         int start = 0;
    71         int end = url.length();
    73         if (url.startsWith("http://")) {
    74             start = 7;
    75         } else if (url.startsWith("https://") && flags == UrlFlags.STRIP_HTTPS) {
    76             start = 8;
    77         }
    79         if (url.endsWith("/")) {
    80             end--;
    81         }
    83         return url.substring(start, end);
    84     }
    86     public static String stripCommonSubdomains(String host) {
    87         if (host == null) {
    88             return host;
    89         }
    91         // In contrast to desktop, we also strip mobile subdomains,
    92         // since its unlikely users are intentionally typing them
    93         int start = 0;
    95         if (host.startsWith("www.")) {
    96             start = 4;
    97         } else if (host.startsWith("mobile.")) {
    98             start = 7;
    99         } else if (host.startsWith("m.")) {
   100             start = 2;
   101         }
   103         return host.substring(start);
   104     }
   106     /**
   107      * Searches the url query string for the first value with the given key.
   108      */
   109     public static String getQueryParameter(String url, String desiredKey) {
   110         if (TextUtils.isEmpty(url) || TextUtils.isEmpty(desiredKey)) {
   111             return null;
   112         }
   114         final String[] urlParts = url.split("\\?");
   115         if (urlParts.length < 2) {
   116             return null;
   117         }
   119         final String query = urlParts[1];
   120         for (final String param : query.split("&")) {
   121             final String pair[] = param.split("=");
   122             final String key = Uri.decode(pair[0]);
   124             // Key is empty or does not match the key we're looking for, discard
   125             if (TextUtils.isEmpty(key) || !key.equals(desiredKey)) {
   126                 continue;
   127             }
   128             // No value associated with key, discard
   129             if (pair.length < 2) {
   130                 continue;
   131             }
   132             final String value = Uri.decode(pair[1]);
   133             if (TextUtils.isEmpty(value)) {
   134                 return null;
   135             }
   136             return value;
   137         }
   139         return null;
   140     }
   142     public static boolean isFilterUrl(String url) {
   143         if (TextUtils.isEmpty(url)) {
   144             return false;
   145         }
   147         return url.startsWith(FILTER_URL_PREFIX);
   148     }
   150     public static String getFilterFromUrl(String url) {
   151         if (TextUtils.isEmpty(url)) {
   152             return null;
   153         }
   155         return url.substring(FILTER_URL_PREFIX.length());
   156     }
   157 }

mercurial