|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * TwinColGrid add-on |
| 4 | + * %% |
| 5 | + * Copyright (C) 2017 - 2025 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.twincolgrid; |
| 21 | +/* |
| 22 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 23 | + * contributor license agreements. See the NOTICE file distributed with |
| 24 | + * this work for additional information regarding copyright ownership. |
| 25 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 26 | + * (the "License"); you may not use this file except in compliance with |
| 27 | + * the License. You may obtain a copy of the License at |
| 28 | + * |
| 29 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 30 | + * |
| 31 | + * Unless required by applicable law or agreed to in writing, software |
| 32 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 33 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | + * See the License for the specific language governing permissions and |
| 35 | + * limitations under the License. |
| 36 | + */ |
| 37 | + |
| 38 | +import lombok.experimental.UtilityClass; |
| 39 | + |
| 40 | +@UtilityClass |
| 41 | +class CharSequenceUtils { |
| 42 | + |
| 43 | + /** |
| 44 | + * Green implementation of regionMatches. |
| 45 | + * |
| 46 | + * @param cs the {@link CharSequence} to be processed |
| 47 | + * @param ignoreCase whether or not to be case-insensitive |
| 48 | + * @param thisStart the index to start on the {@code cs} CharSequence |
| 49 | + * @param substring the {@link CharSequence} to be looked for |
| 50 | + * @param start the index to start on the {@code substring} CharSequence |
| 51 | + * @param length character length of the region |
| 52 | + * @return whether the region matched |
| 53 | + * @see String#regionMatches(boolean, int, String, int, int) |
| 54 | + */ |
| 55 | + static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart, |
| 56 | + final CharSequence substring, final int start, final int length) { |
| 57 | + if (cs instanceof String && substring instanceof String) { |
| 58 | + return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length); |
| 59 | + } |
| 60 | + int index1 = thisStart; |
| 61 | + int index2 = start; |
| 62 | + int tmpLen = length; |
| 63 | + |
| 64 | + // Extract these first so we detect NPEs the same as the java.lang.String version |
| 65 | + final int srcLen = cs.length() - thisStart; |
| 66 | + final int otherLen = substring.length() - start; |
| 67 | + |
| 68 | + // Check for invalid parameters |
| 69 | + if (thisStart < 0 || start < 0 || length < 0) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + // Check that the regions are long enough |
| 74 | + if (srcLen < length || otherLen < length) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + while (tmpLen-- > 0) { |
| 79 | + final char c1 = cs.charAt(index1++); |
| 80 | + final char c2 = substring.charAt(index2++); |
| 81 | + |
| 82 | + if (c1 == c2) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + if (!ignoreCase) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // The real same check as in String#regionMatches(boolean, int, String, int, int): |
| 91 | + final char u1 = Character.toUpperCase(c1); |
| 92 | + final char u2 = Character.toUpperCase(c2); |
| 93 | + if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments