|
| 1 | +/* |
| 2 | + * Copyright 2014 the original author or 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 | +package org.codehaus.groovy.grails.support.encoding; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.Writer; |
| 20 | + |
| 21 | +/** |
| 22 | + * Utility functions for handling java.lang.CharSequence instances |
| 23 | + * |
| 24 | + * |
| 25 | + * @author Lari Hotari |
| 26 | + * @since 2.3.10 |
| 27 | + * |
| 28 | + */ |
| 29 | +public class CharSequences { |
| 30 | + public static CharSequence createCharSequence(char[] chars) { |
| 31 | + return new CharArrayCharSequence(chars, 0, chars.length); |
| 32 | + } |
| 33 | + |
| 34 | + public static CharSequence createCharSequence(char[] chars, int start, int count) { |
| 35 | + return new CharArrayCharSequence(chars, start, count); |
| 36 | + } |
| 37 | + |
| 38 | + public static CharSequence createCharSequence(CharSequence str, int start, int count) { |
| 39 | + if(canUseOriginalForSubSequence(str, start, count)) { |
| 40 | + return str; |
| 41 | + } else { |
| 42 | + return new SubCharSequence(str, start, count); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Checks if start == 0 and count == length of CharSequence |
| 48 | + * It does this check only for String, StringBuilder and StringBuffer classes which have a fast way to check length |
| 49 | + * |
| 50 | + * Calculating length on GStringImpl requires building the result which is costly. |
| 51 | + * This helper method is to avoid calling length on other that String, StringBuilder and StringBuffer classes |
| 52 | + * when checking if the input CharSequence instance is already the same as the requested sub sequence |
| 53 | + * |
| 54 | + * @param str CharSequence input |
| 55 | + * @param start start index |
| 56 | + * @param count length on sub sequence |
| 57 | + * @return true if input is String, StringBuilder or StringBuffer class, start is 0 and count is length of input sequence |
| 58 | + */ |
| 59 | + public static boolean canUseOriginalForSubSequence(CharSequence str, int start, int count) { |
| 60 | + if (start != 0) return false; |
| 61 | + final Class<?> csqClass = str.getClass(); |
| 62 | + return (csqClass == String.class || csqClass == StringBuilder.class || csqClass == StringBuffer.class) && count == str.length(); |
| 63 | + } |
| 64 | + |
| 65 | + public static CharSequence createSingleCharSequence(int c) { |
| 66 | + return new SingleCharCharSequence(c); |
| 67 | + } |
| 68 | + |
| 69 | + public static CharSequence createSingleCharSequence(char ch) { |
| 70 | + return new SingleCharCharSequence(ch); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Writes a CharSequence instance in the most optimal way to the target writer |
| 75 | + * |
| 76 | + * |
| 77 | + * @param target writer |
| 78 | + * @param csq source CharSequence instance |
| 79 | + * @param start start/offset index |
| 80 | + * @param end end index + 1 |
| 81 | + * @throws IOException |
| 82 | + */ |
| 83 | + public static void writeCharSequence(Writer target, CharSequence csq, int start, int end) throws IOException { |
| 84 | + final Class<?> csqClass = csq.getClass(); |
| 85 | + if (csqClass == String.class) { |
| 86 | + target.write((String)csq, start, end - start); |
| 87 | + } |
| 88 | + else if (csqClass == StringBuffer.class) { |
| 89 | + char[] buf = new char[end - start]; |
| 90 | + ((StringBuffer)csq).getChars(start, end, buf, 0); |
| 91 | + target.write(buf); |
| 92 | + } |
| 93 | + else if (csqClass == StringBuilder.class) { |
| 94 | + char[] buf = new char[end - start]; |
| 95 | + ((StringBuilder)csq).getChars(start, end, buf, 0); |
| 96 | + target.write(buf); |
| 97 | + } |
| 98 | + else if (csq instanceof CharArrayAccessible) { |
| 99 | + char[] buf = new char[end - start]; |
| 100 | + ((CharArrayAccessible)csq).getChars(start, end, buf, 0); |
| 101 | + target.write(buf); |
| 102 | + } |
| 103 | + else { |
| 104 | + String str = csq.subSequence(start, end).toString(); |
| 105 | + target.write(str, 0, str.length()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static void writeCharSequence(Writer target, CharSequence csq) throws IOException { |
| 110 | + writeCharSequence(target, csq, 0, csq.length()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Provides an optimized way to copy CharSequence content to target array. |
| 115 | + * Uses getChars method available on String, StringBuilder and StringBuffer classes. |
| 116 | + * |
| 117 | + * Characters are copied from the source sequence <code>csq</code> into the |
| 118 | + * destination character array <code>dst</code>. The first character to |
| 119 | + * be copied is at index <code>srcBegin</code>; the last character to |
| 120 | + * be copied is at index <code>srcEnd-1</code>. The total number of |
| 121 | + * characters to be copied is <code>srcEnd-srcBegin</code>. The |
| 122 | + * characters are copied into the subarray of <code>dst</code> starting |
| 123 | + * at index <code>dstBegin</code> and ending at index: |
| 124 | + * <p><blockquote><pre> |
| 125 | + * dstbegin + (srcEnd-srcBegin) - 1 |
| 126 | + * </pre></blockquote> |
| 127 | + * |
| 128 | + * @param csq the source CharSequence instance. |
| 129 | + * @param srcBegin start copying at this offset. |
| 130 | + * @param srcEnd stop copying at this offset. |
| 131 | + * @param dst the array to copy the data into. |
| 132 | + * @param dstBegin offset into <code>dst</code>. |
| 133 | + * @throws NullPointerException if <code>dst</code> is |
| 134 | + * <code>null</code>. |
| 135 | + * @throws IndexOutOfBoundsException if any of the following is true: |
| 136 | + * <ul> |
| 137 | + * <li><code>srcBegin</code> is negative |
| 138 | + * <li><code>dstBegin</code> is negative |
| 139 | + * <li>the <code>srcBegin</code> argument is greater than |
| 140 | + * the <code>srcEnd</code> argument. |
| 141 | + * <li><code>srcEnd</code> is greater than |
| 142 | + * <code>this.length()</code>. |
| 143 | + * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than |
| 144 | + * <code>dst.length</code> |
| 145 | + * </ul> |
| 146 | + */ |
| 147 | + public static void getChars(CharSequence csq, int srcBegin, int srcEnd, char dst[], int dstBegin) { |
| 148 | + final Class<?> csqClass = csq.getClass(); |
| 149 | + if (csqClass == String.class) { |
| 150 | + ((String)csq).getChars(srcBegin, srcEnd, dst, dstBegin); |
| 151 | + } |
| 152 | + else if (csqClass == StringBuffer.class) { |
| 153 | + ((StringBuffer)csq).getChars(srcBegin, srcEnd, dst, dstBegin); |
| 154 | + } |
| 155 | + else if (csqClass == StringBuilder.class) { |
| 156 | + ((StringBuilder)csq).getChars(srcBegin, srcEnd, dst, dstBegin); |
| 157 | + } |
| 158 | + else if (csq instanceof CharArrayAccessible) { |
| 159 | + ((CharArrayAccessible)csq).getChars(srcBegin, srcEnd, dst, dstBegin); |
| 160 | + } |
| 161 | + else { |
| 162 | + String str = csq.subSequence(srcBegin, srcEnd).toString(); |
| 163 | + str.getChars(0, str.length(), dst, dstBegin); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments