| 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.IntComplementSet; |
| 12 | import net.sf.saxon.z.IntSet; |
| 13 | |
| 14 | /** |
| 15 | * A character class represents a set of characters for regex matching purposes. An inverse |
| 16 | * character class is the complement of another character class |
| 17 | */ |
| 18 | |
| 19 | public class InverseCharacterClass implements CharacterClass { |
| 20 | |
| 21 | private CharacterClass complement; |
| 22 | |
| 23 | /** |
| 24 | * Create the complement of a character class, that is, a character class containing exactly |
| 25 | * those characters that are not included in the supplied character class |
| 26 | * @param complement the class of which this one is the complement |
| 27 | */ |
| 28 | |
| 29 | public InverseCharacterClass(CharacterClass complement) { |
| 30 | this.complement = complement; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the character class of which this class is the complement |
| 35 | * @return the complement of this character class |
| 36 | */ |
| 37 | |
| 38 | public CharacterClass getComplement() { |
| 39 | return complement; |
| 40 | } |
| 41 | |
| 42 | public boolean test(int value) { |
| 43 | return !complement.test(value); |
| 44 | } |
| 45 | |
| 46 | public boolean isDisjoint(CharacterClass other) { |
| 47 | return other == complement; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public IntSet getIntSet() { |
| 52 | IntSet comp = complement.getIntSet(); |
| 53 | return comp == null ? null : new IntComplementSet(complement.getIntSet()); |
| 54 | } |
| 55 | } |