| 1 | /* |
| 2 | * Copyright 2010 The Closure Compiler Authors. |
| 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.google.javascript.jscomp; |
| 18 | |
| 19 | /** |
| 20 | * Guava code that will eventually be open-sourced properly. Package-private |
| 21 | * until they're able to do that. A lot of these methods are discouraged |
| 22 | * anyways. |
| 23 | * |
| 24 | * @author [email protected] (Nick Santos) |
| 25 | */ |
| 26 | class Strings { |
| 27 | private Strings() {} // All static |
| 28 | |
| 29 | /** |
| 30 | * If this given string is of length {@code maxLength} or less, it will |
| 31 | * be returned as-is. |
| 32 | * Otherwise, it will be truncated to {@code maxLength}, regardless of whether |
| 33 | * there are any space characters in the String. If an ellipsis is requested |
| 34 | * to be appended to the truncated String, the String will be truncated so |
| 35 | * that the ellipsis will also fit within maxLength. |
| 36 | * If no truncation was necessary, no ellipsis will be added. |
| 37 | * @param source the String to truncate if necessary |
| 38 | * @param maxLength the maximum number of characters to keep |
| 39 | * @param addEllipsis if true, and if the String had to be truncated, |
| 40 | * add "..." to the end of the String before returning. Additionally, |
| 41 | * the ellipsis will only be added if maxLength is greater than 3. |
| 42 | * @return the original string if it's length is less than or equal to |
| 43 | * maxLength, otherwise a truncated string as mentioned above |
| 44 | */ |
| 45 | static String truncateAtMaxLength(String source, int maxLength, |
| 46 | boolean addEllipsis) { |
| 47 | |
| 48 | if (source.length() <= maxLength) { |
| 49 | return source; |
| 50 | } |
| 51 | if (addEllipsis && maxLength > 3) { |
| 52 | return unicodePreservingSubstring(source, 0, maxLength - 3) + "..."; |
| 53 | } |
| 54 | return unicodePreservingSubstring(source, 0, maxLength); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Normalizes {@code index} such that it respects Unicode character |
| 59 | * boundaries in {@code str}. |
| 60 | * |
| 61 | * <p>If {@code index} is the low surrogate of a Unicode character, |
| 62 | * the method returns {@code index - 1}. Otherwise, {@code index} is |
| 63 | * returned. |
| 64 | * |
| 65 | * <p>In the case in which {@code index} falls in an invalid surrogate pair |
| 66 | * (e.g. consecutive low surrogates, consecutive high surrogates), or if |
| 67 | * if it is not a valid index into {@code str}, the original value of |
| 68 | * {@code index} is returned. |
| 69 | * |
| 70 | * @param str the String |
| 71 | * @param index the index to be normalized |
| 72 | * @return a normalized index that does not split a Unicode character |
| 73 | */ |
| 74 | private static int unicodePreservingIndex(String str, int index) { |
| 75 | if (index > 0 && index < str.length()) { |
| 76 | if (Character.isHighSurrogate(str.charAt(index - 1)) && |
| 77 | Character.isLowSurrogate(str.charAt(index))) { |
| 78 | return index - 1; |
| 79 | } |
| 80 | } |
| 81 | return index; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns a substring of {@code str} that respects Unicode character |
| 86 | * boundaries. |
| 87 | * |
| 88 | * <p>The string will never be split between a [high, low] surrogate pair, |
| 89 | * as defined by {@link Character#isHighSurrogate} and |
| 90 | * {@link Character#isLowSurrogate}. |
| 91 | * |
| 92 | * <p>If {@code begin} or {@code end} are the low surrogate of a Unicode |
| 93 | * character, it will be offset by -1. |
| 94 | * |
| 95 | * <p>This behavior guarantees that |
| 96 | * {@code str.equals(StringUtil.unicodePreservingSubstring(str, 0, n) + |
| 97 | * StringUtil.unicodePreservingSubstring(str, n, str.length())) } is |
| 98 | * true for all {@code n}. |
| 99 | * </pre> |
| 100 | * |
| 101 | * <p>This means that unlike {@link String#substring(int, int)}, the length of |
| 102 | * the returned substring may not necessarily be equivalent to |
| 103 | * {@code end - begin}. |
| 104 | * |
| 105 | * @param str the original String |
| 106 | * @param begin the beginning index, inclusive |
| 107 | * @param end the ending index, exclusive |
| 108 | * @return the specified substring, possibly adjusted in order to not |
| 109 | * split Unicode surrogate pairs |
| 110 | * @throws IndexOutOfBoundsException if the {@code begin} is negative, |
| 111 | * or {@code end} is larger than the length of {@code str}, or |
| 112 | * {@code begin} is larger than {@code end} |
| 113 | */ |
| 114 | private static String unicodePreservingSubstring( |
| 115 | String str, int begin, int end) { |
| 116 | return str.substring(unicodePreservingIndex(str, begin), |
| 117 | unicodePreservingIndex(str, end)); |
| 118 | } |
| 119 | } |