| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | |
| 9 | package net.sf.saxon.regex.charclass; |
| 10 | |
| 11 | import net.sf.saxon.z.IntSet; |
| 12 | |
| 13 | /** |
| 14 | * A character class represents a set of characters for regex matching purposes. An |
| 15 | * IntSetCharacterClass is a character class represented by a set of integer |
| 16 | * codepoints |
| 17 | */ |
| 18 | |
| 19 | public class IntSetCharacterClass implements CharacterClass { |
| 20 | |
| 21 | private IntSet intSet; |
| 22 | |
| 23 | public IntSetCharacterClass(IntSet intSet) { |
| 24 | this.intSet = intSet; |
| 25 | } |
| 26 | |
| 27 | public IntSet getIntSet() { |
| 28 | return intSet; |
| 29 | } |
| 30 | |
| 31 | public boolean test(int value) { |
| 32 | return intSet.contains(value); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Ask whether this character class is known to be disjoint with another character class |
| 37 | * (that is, the two classes have no characters in common). If in doubt, return false. |
| 38 | * @param other the other character class |
| 39 | * @return true if the character classes are known to be disjoint; false if there may |
| 40 | * be characters in common between the two classes |
| 41 | */ |
| 42 | |
| 43 | public boolean isDisjoint(CharacterClass other) { |
| 44 | if (other instanceof IntSetCharacterClass) { |
| 45 | return intSet.intersect(((IntSetCharacterClass) other).intSet).isEmpty(); |
| 46 | } else if (other instanceof InverseCharacterClass) { |
| 47 | return other.isDisjoint(this); |
| 48 | } else { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | } |