1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.utils; 18 19import com.android.annotations.NonNull; 20 21public class SdkUtils { 22 /** 23 * Returns true if the given string ends with the given suffix, using a 24 * case-insensitive comparison. 25 * 26 * @param string the full string to be checked 27 * @param suffix the suffix to be checked for 28 * @return true if the string case-insensitively ends with the given suffix 29 */ 30 public static boolean endsWithIgnoreCase(String string, String suffix) { 31 return string.regionMatches(true /* ignoreCase */, string.length() - suffix.length(), 32 suffix, 0, suffix.length()); 33 } 34 35 /** 36 * Returns true if the given sequence ends with the given suffix (case 37 * sensitive). 38 * 39 * @param sequence the character sequence to be checked 40 * @param suffix the suffix to look for 41 * @return true if the given sequence ends with the given suffix 42 */ 43 public static boolean endsWith(CharSequence sequence, CharSequence suffix) { 44 return endsWith(sequence, sequence.length(), suffix); 45 } 46 47 /** 48 * Returns true if the given sequence ends at the given offset with the given suffix (case 49 * sensitive) 50 * 51 * @param sequence the character sequence to be checked 52 * @param endOffset the offset at which the sequence is considered to end 53 * @param suffix the suffix to look for 54 * @return true if the given sequence ends with the given suffix 55 */ 56 public static boolean endsWith(CharSequence sequence, int endOffset, CharSequence suffix) { 57 if (endOffset < suffix.length()) { 58 return false; 59 } 60 61 for (int i = endOffset - 1, j = suffix.length() - 1; j >= 0; i--, j--) { 62 if (sequence.charAt(i) != suffix.charAt(j)) { 63 return false; 64 } 65 } 66 67 return true; 68 } 69 70 /** 71 * Returns true if the given string starts with the given prefix, using a 72 * case-insensitive comparison. 73 * 74 * @param string the full string to be checked 75 * @param prefix the prefix to be checked for 76 * @return true if the string case-insensitively starts with the given prefix 77 */ 78 public static boolean startsWithIgnoreCase(String string, String prefix) { 79 return string.regionMatches(true /* ignoreCase */, 0, prefix, 0, prefix.length()); 80 } 81 82 /** 83 * Returns true if the given string starts at the given offset with the 84 * given prefix, case insensitively. 85 * 86 * @param string the full string to be checked 87 * @param offset the offset in the string to start looking 88 * @param prefix the prefix to be checked for 89 * @return true if the string case-insensitively starts at the given offset 90 * with the given prefix 91 */ 92 public static boolean startsWith(String string, int offset, String prefix) { 93 return string.regionMatches(true /* ignoreCase */, offset, prefix, 0, prefix.length()); 94 } 95 96 /** 97 * Strips the whitespace from the given string 98 * 99 * @param string the string to be cleaned up 100 * @return the string, without whitespace 101 */ 102 public static String stripWhitespace(String string) { 103 StringBuilder sb = new StringBuilder(string.length()); 104 for (int i = 0, n = string.length(); i < n; i++) { 105 char c = string.charAt(i); 106 if (!Character.isWhitespace(c)) { 107 sb.append(c); 108 } 109 } 110 111 return sb.toString(); 112 } 113 114 /** For use by {@link #getLineSeparator()} */ 115 private static String sLineSeparator; 116 117 /** 118 * Returns the default line separator to use. 119 * <p> 120 * NOTE: If you have an associated {@link IDocument}, it is better to call 121 * {@link TextUtilities#getDefaultLineDelimiter(IDocument)} since that will 122 * allow (for example) editing a \r\n-delimited document on a \n-delimited 123 * platform and keep a consistent usage of delimiters in the file. 124 * 125 * @return the delimiter string to use 126 */ 127 @NonNull 128 public static String getLineSeparator() { 129 if (sLineSeparator == null) { 130 // This is guaranteed to exist: 131 sLineSeparator = System.getProperty("line.separator"); //$NON-NLS-1$ 132 } 133 134 return sLineSeparator; 135 } 136} 137