diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/src/com/android/utils/SdkUtils.java | 136 | ||||
-rw-r--r-- | common/tests/src/com/android/utils/SdkUtilsTest.java | 84 |
2 files changed, 220 insertions, 0 deletions
diff --git a/common/src/com/android/utils/SdkUtils.java b/common/src/com/android/utils/SdkUtils.java new file mode 100644 index 0000000..b49c120 --- /dev/null +++ b/common/src/com/android/utils/SdkUtils.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.utils; + +import com.android.annotations.NonNull; + +public class SdkUtils { + /** + * Returns true if the given string ends with the given suffix, using a + * case-insensitive comparison. + * + * @param string the full string to be checked + * @param suffix the suffix to be checked for + * @return true if the string case-insensitively ends with the given suffix + */ + public static boolean endsWithIgnoreCase(String string, String suffix) { + return string.regionMatches(true /* ignoreCase */, string.length() - suffix.length(), + suffix, 0, suffix.length()); + } + + /** + * Returns true if the given sequence ends with the given suffix (case + * sensitive). + * + * @param sequence the character sequence to be checked + * @param suffix the suffix to look for + * @return true if the given sequence ends with the given suffix + */ + public static boolean endsWith(CharSequence sequence, CharSequence suffix) { + return endsWith(sequence, sequence.length(), suffix); + } + + /** + * Returns true if the given sequence ends at the given offset with the given suffix (case + * sensitive) + * + * @param sequence the character sequence to be checked + * @param endOffset the offset at which the sequence is considered to end + * @param suffix the suffix to look for + * @return true if the given sequence ends with the given suffix + */ + public static boolean endsWith(CharSequence sequence, int endOffset, CharSequence suffix) { + if (endOffset < suffix.length()) { + return false; + } + + for (int i = endOffset - 1, j = suffix.length() - 1; j >= 0; i--, j--) { + if (sequence.charAt(i) != suffix.charAt(j)) { + return false; + } + } + + return true; + } + + /** + * Returns true if the given string starts with the given prefix, using a + * case-insensitive comparison. + * + * @param string the full string to be checked + * @param prefix the prefix to be checked for + * @return true if the string case-insensitively starts with the given prefix + */ + public static boolean startsWithIgnoreCase(String string, String prefix) { + return string.regionMatches(true /* ignoreCase */, 0, prefix, 0, prefix.length()); + } + + /** + * Returns true if the given string starts at the given offset with the + * given prefix, case insensitively. + * + * @param string the full string to be checked + * @param offset the offset in the string to start looking + * @param prefix the prefix to be checked for + * @return true if the string case-insensitively starts at the given offset + * with the given prefix + */ + public static boolean startsWith(String string, int offset, String prefix) { + return string.regionMatches(true /* ignoreCase */, offset, prefix, 0, prefix.length()); + } + + /** + * Strips the whitespace from the given string + * + * @param string the string to be cleaned up + * @return the string, without whitespace + */ + public static String stripWhitespace(String string) { + StringBuilder sb = new StringBuilder(string.length()); + for (int i = 0, n = string.length(); i < n; i++) { + char c = string.charAt(i); + if (!Character.isWhitespace(c)) { + sb.append(c); + } + } + + return sb.toString(); + } + + /** For use by {@link #getLineSeparator()} */ + private static String sLineSeparator; + + /** + * Returns the default line separator to use. + * <p> + * NOTE: If you have an associated {@link IDocument}, it is better to call + * {@link TextUtilities#getDefaultLineDelimiter(IDocument)} since that will + * allow (for example) editing a \r\n-delimited document on a \n-delimited + * platform and keep a consistent usage of delimiters in the file. + * + * @return the delimiter string to use + */ + @NonNull + public static String getLineSeparator() { + if (sLineSeparator == null) { + // This is guaranteed to exist: + sLineSeparator = System.getProperty("line.separator"); //$NON-NLS-1$ + } + + return sLineSeparator; + } +} diff --git a/common/tests/src/com/android/utils/SdkUtilsTest.java b/common/tests/src/com/android/utils/SdkUtilsTest.java new file mode 100644 index 0000000..29a4d51 --- /dev/null +++ b/common/tests/src/com/android/utils/SdkUtilsTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.utils; + +import junit.framework.TestCase; + +public class SdkUtilsTest extends TestCase { + public void testEndsWithIgnoreCase() { + assertTrue(SdkUtils.endsWithIgnoreCase("foo", "foo")); + assertTrue(SdkUtils.endsWithIgnoreCase("foo", "Foo")); + assertTrue(SdkUtils.endsWithIgnoreCase("foo", "foo")); + assertTrue(SdkUtils.endsWithIgnoreCase("Barfoo", "foo")); + assertTrue(SdkUtils.endsWithIgnoreCase("BarFoo", "foo")); + assertTrue(SdkUtils.endsWithIgnoreCase("BarFoo", "foO")); + + assertFalse(SdkUtils.endsWithIgnoreCase("foob", "foo")); + assertFalse(SdkUtils.endsWithIgnoreCase("foo", "fo")); + } + + public void testStartsWithIgnoreCase() { + assertTrue(SdkUtils.startsWithIgnoreCase("foo", "foo")); + assertTrue(SdkUtils.startsWithIgnoreCase("foo", "Foo")); + assertTrue(SdkUtils.startsWithIgnoreCase("foo", "foo")); + assertTrue(SdkUtils.startsWithIgnoreCase("barfoo", "bar")); + assertTrue(SdkUtils.startsWithIgnoreCase("BarFoo", "bar")); + assertTrue(SdkUtils.startsWithIgnoreCase("BarFoo", "bAr")); + + assertFalse(SdkUtils.startsWithIgnoreCase("bfoo", "foo")); + assertFalse(SdkUtils.startsWithIgnoreCase("fo", "foo")); + } + + public void testStartsWith() { + assertTrue(SdkUtils.startsWith("foo", 0, "foo")); + assertTrue(SdkUtils.startsWith("foo", 0, "Foo")); + assertTrue(SdkUtils.startsWith("Foo", 0, "foo")); + assertTrue(SdkUtils.startsWith("aFoo", 1, "foo")); + + assertFalse(SdkUtils.startsWith("aFoo", 0, "foo")); + assertFalse(SdkUtils.startsWith("aFoo", 2, "foo")); + } + + public void testEndsWith() { + assertTrue(SdkUtils.endsWith("foo", "foo")); + assertTrue(SdkUtils.endsWith("foobar", "obar")); + assertTrue(SdkUtils.endsWith("foobar", "bar")); + assertTrue(SdkUtils.endsWith("foobar", "ar")); + assertTrue(SdkUtils.endsWith("foobar", "r")); + assertTrue(SdkUtils.endsWith("foobar", "")); + + assertTrue(SdkUtils.endsWith(new StringBuilder("foobar"), "bar")); + assertTrue(SdkUtils.endsWith(new StringBuilder("foobar"), new StringBuffer("obar"))); + assertTrue(SdkUtils.endsWith("foobar", new StringBuffer("obar"))); + + assertFalse(SdkUtils.endsWith("foo", "fo")); + assertFalse(SdkUtils.endsWith("foobar", "Bar")); + assertFalse(SdkUtils.endsWith("foobar", "longfoobar")); + } + + public void testEndsWith2() { + assertTrue(SdkUtils.endsWith("foo", "foo".length(), "foo")); + assertTrue(SdkUtils.endsWith("foo", "fo".length(), "fo")); + assertTrue(SdkUtils.endsWith("foo", "f".length(), "f")); + } + + public void testStripWhitespace() { + assertEquals("foo", SdkUtils.stripWhitespace("foo")); + assertEquals("foobar", SdkUtils.stripWhitespace("foo bar")); + assertEquals("foobar", SdkUtils.stripWhitespace(" foo bar \n\t")); + } +} |