From 11fe181e16501103d7c0f70344661ea2ef5d3df9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 10 Sep 2010 16:07:52 -0700 Subject: Add faster TextUtil function for searching delimited lists. The previous version in Settings allocated memory. Change-Id: I0f821112dc8f830689489f201ce268195f9e6cbd --- core/java/android/provider/Settings.java | 8 +------- core/java/android/text/TextUtils.java | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'core/java') diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index f37ef99..fd60115 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -3481,13 +3481,7 @@ public final class Settings { */ public static final boolean isLocationProviderEnabled(ContentResolver cr, String provider) { String allowedProviders = Settings.Secure.getString(cr, LOCATION_PROVIDERS_ALLOWED); - if (allowedProviders != null) { - return (allowedProviders.equals(provider) || - allowedProviders.contains("," + provider + ",") || - allowedProviders.startsWith(provider + ",") || - allowedProviders.endsWith("," + provider)); - } - return false; + return TextUtils.delimitedStringContains(allowedProviders, ',', provider); } /** diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index 9589bf3..8675d05 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -1651,7 +1651,36 @@ public class TextUtils { return mode; } - + + /** + * Does a comma-delimited list 'delimitedString' contain a certain item? + * (without allocating memory) + * + * @hide + */ + public static boolean delimitedStringContains( + String delimitedString, char delimiter, String item) { + if (isEmpty(delimitedString) || isEmpty(item)) { + return false; + } + int pos = -1; + int length = delimitedString.length(); + while ((pos = delimitedString.indexOf(item, pos + 1)) != -1) { + if (pos > 0 && delimitedString.charAt(pos - 1) != delimiter) { + continue; + } + int expectedDelimiterPos = pos + item.length(); + if (expectedDelimiterPos == length) { + // Match at end of string. + return true; + } + if (delimitedString.charAt(expectedDelimiterPos) == delimiter) { + return true; + } + } + return false; + } + private static Object sLock = new Object(); private static char[] sTemp = null; } -- cgit v1.1