| 1 | package ezvcard.util; |
| 2 | |
| 3 | import java.util.BitSet; |
| 4 | |
| 5 | /* |
| 6 | Copyright (c) 2012-2023, Michael Angstadt |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions are met: |
| 11 | |
| 12 | 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | list of conditions and the following disclaimer. |
| 14 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | this list of conditions and the following disclaimer in the documentation |
| 16 | and/or other materials provided with the distribution. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | The views and conclusions contained in the software and documentation are those |
| 30 | of the authors and should not be interpreted as representing official policies, |
| 31 | either expressed or implied, of the FreeBSD Project. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Represents a collection of unique characters. This class is used in place of |
| 36 | * regular expressions to improve performance. |
| 37 | * @author Michael Angstadt |
| 38 | */ |
| 39 | public class CharacterBitSet { |
| 40 | private final BitSet bitSet = new BitSet(128); |
| 41 | private final String characters; |
| 42 | |
| 43 | /** |
| 44 | * @param characters the list of characters to check for. Ranges of |
| 45 | * characters are represented using a hyphen. Therefore, to include a raw |
| 46 | * hyphen in the character list, it must come at the very beginning or very |
| 47 | * end of the given string. In this example, "a-f_:-", the following |
| 48 | * characters are included in the final character list: lowercase letters a |
| 49 | * through f, underscores, colons, and hyphens |
| 50 | */ |
| 51 | public CharacterBitSet(String characters) { |
| 52 | this.characters = characters; |
| 53 | for (int i = 0; i < characters.length(); i++) { |
| 54 | char c = characters.charAt(i); |
| 55 | char next = (i < characters.length() - 2) ? characters.charAt(i + 1) : 0; |
| 56 | |
| 57 | if (next == '-') { |
| 58 | char start = c; |
| 59 | char end = characters.charAt(i + 2); |
| 60 | if (start > end) { |
| 61 | //swap them |
| 62 | char temp = start; |
| 63 | start = end; |
| 64 | end = temp; |
| 65 | } |
| 66 | |
| 67 | bitSet.set(start, end + 1); |
| 68 | i += 2; |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | bitSet.set(c); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the character list that was originally passed into this object. |
| 78 | * @return the character list |
| 79 | */ |
| 80 | public String characters() { |
| 81 | return characters; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the underlying {@link BitSet} object. |
| 86 | * @return the {@link BitSet} object |
| 87 | */ |
| 88 | public BitSet bitSet() { |
| 89 | return bitSet; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Determines if the given string contains *only* the characters in this bit |
| 94 | * set. |
| 95 | * @param string the string |
| 96 | * @return true if the string contains only the specified characters, false |
| 97 | * if not |
| 98 | */ |
| 99 | public boolean containsOnly(String string) { |
| 100 | return containsOnly(string, 0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Determines if the given string contains *only* the characters in this bit |
| 105 | * set. |
| 106 | * @param string the string |
| 107 | * @param startIndex the index to start at in the string |
| 108 | * @return true if the string contains only the specified characters, false |
| 109 | * if not |
| 110 | */ |
| 111 | public boolean containsOnly(String string, int startIndex) { |
| 112 | for (int i = startIndex; i < string.length(); i++) { |
| 113 | char c = string.charAt(i); |
| 114 | if (!bitSet.get(c)) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Determines if the given string contains at least one of the characters in |
| 123 | * this bit set. |
| 124 | * @param string the string |
| 125 | * @return true if the string contains at least one of the characters, false |
| 126 | * if not |
| 127 | */ |
| 128 | public boolean containsAny(String string) { |
| 129 | return containsAny(string, 0); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Determines if the given string contains at least one of the characters in |
| 134 | * this bit set. |
| 135 | * @param string the string |
| 136 | * @param startIndex the index to start at in the string |
| 137 | * @return true if the string contains at least one of the characters, false |
| 138 | * if not |
| 139 | */ |
| 140 | public boolean containsAny(String string, int startIndex) { |
| 141 | for (int i = startIndex; i < string.length(); i++) { |
| 142 | char c = string.charAt(i); |
| 143 | if (bitSet.get(c)) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return bitSet.hashCode(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object obj) { |
| 157 | if (this == obj) return true; |
| 158 | if (obj == null) return false; |
| 159 | if (getClass() != obj.getClass()) return false; |
| 160 | CharacterBitSet other = (CharacterBitSet) obj; |
| 161 | return bitSet.equals(other.bitSet); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return characters; |
| 167 | } |
| 168 | } |