1 /* 2 * Copyright (C) 2016 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 17 package com.android.calculator2; 18 19 /** 20 * Some helpful methods operating on strings. 21 */ 22 23 public class StringUtils { 24 25 /** 26 * Return a string with n copies of c. 27 */ repeat(char c, int n)28 public static String repeat(char c, int n) { 29 final StringBuilder result = new StringBuilder(); 30 for (int i = 0; i < n; ++i) { 31 result.append(c); 32 } 33 return result.toString(); 34 } 35 36 /** 37 * Return a copy of the supplied string with commas added every three digits. 38 * The substring indicated by the supplied range is assumed to contain only 39 * a whole number, with no decimal point. 40 * Inserting a digit separator every 3 digits appears to be 41 * at least somewhat acceptable, though not necessarily preferred, everywhere. 42 * The grouping separator in the result is NOT localized. 43 */ addCommas(String s, int begin, int end)44 public static String addCommas(String s, int begin, int end) { 45 // Resist the temptation to use Java's NumberFormat, which converts to long or double 46 // and hence doesn't handle very large numbers. 47 StringBuilder result = new StringBuilder(); 48 int current = begin; 49 while (current < end && (s.charAt(current) == '-' || s.charAt(current) == ' ')) { 50 ++current; 51 } 52 result.append(s, begin, current); 53 while (current < end) { 54 result.append(s.charAt(current)); 55 ++current; 56 if ((end - current) % 3 == 0 && end != current) { 57 result.append(','); 58 } 59 } 60 return result.toString(); 61 } 62 63 /** 64 * Ignoring all occurrences of c in both strings, check whether old is a prefix of new. 65 * If so, return the remaining subsequence of whole. If not, return null. 66 */ getExtensionIgnoring(CharSequence whole, CharSequence prefix, char c)67 public static CharSequence getExtensionIgnoring(CharSequence whole, CharSequence prefix, 68 char c) { 69 int wIndex = 0; 70 int pIndex = 0; 71 final int wLen = whole.length(); 72 final int pLen = prefix.length(); 73 while (true) { 74 while (pIndex < pLen && prefix.charAt(pIndex) == c) { 75 ++pIndex; 76 } 77 while (wIndex < wLen && whole.charAt(wIndex) == c) { 78 ++wIndex; 79 } 80 if (pIndex == pLen) { 81 break; 82 } 83 if (wIndex == wLen || whole.charAt(wIndex) != prefix.charAt(pIndex) ) { 84 return null; 85 } 86 ++pIndex; 87 ++wIndex; 88 } 89 while (wIndex < wLen && whole.charAt(wIndex) == c) { 90 ++wIndex; 91 } 92 return whole.subSequence(wIndex, wLen); 93 } 94 } 95