Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1029560 // CCI_Choice - CharacterClassIterator branching out depending on next char

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2692L/17K).

1  
sclass CCI_Choice extends CharacterClassIterator {
2  
  bool canEndHere;
3  
  
4  
  // can be a HashMap or TreeMap (e.g. with special comparator)
5  
  Map<Char, CharacterClassIterator> map;
6  
  
7  
  // computed on demand
8  
  CharRange acceptedCharRange;
9  
10  
  *() {}
11  
  *(bool canEndHere, Map<Char, CharacterClassIterator> *map) {}
12  
  
13  
  CharacterClassIterator acceptChar(char c) {
14  
    ret assertNotNull(map.get(c));
15  
  }
16  
17  
  bool canEndHere() {
18  
    ret canEndHere;
19  
  }
20  
  
21  
  bool canAcceptChar(char c) {
22  
    ret map.containsKey(c);
23  
  }
24  
25  
  Cl<Char> acceptedChars() { ret keys(map); }
26  
27  
  CharRange acceptedCharRange() {
28  
    if (empty(map)) null;
29  
    if (acceptedCharRange == null)
30  
      acceptedCharRange = calcAcceptedCharRange();
31  
    ret acceptedCharRange;
32  
  }
33  
  
34  
  CharRange calcAcceptedCharRange() {
35  
    if (map instanceof TreeMap) {
36  
      ret CharRange(firstKey(map), lastKey((TreeMap<Char, CharacterClassIterator>) map));
37  
    } else {
38  
      char min = 0xFFFF, max = 0;
39  
      for (char c : keys(map)) {
40  
        if (c < min) min = c;
41  
        if (c > max) max = c;
42  
      }
43  
      ret CharRange(min, max); 
44  
    }
45  
  }
46  
  
47  
  // object is immutable, so no cloning necessary
48  
  CharacterClassIterator cloneCCI() {
49  
    this;
50  
  }
51  
}  

Author comment

Began life as a copy of #1029558

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029560
Snippet name: CCI_Choice - CharacterClassIterator branching out depending on next char
Eternal ID of this version: #1029560/8
Text MD5: 615db8a1e3ff8d1ec0360a260b70bc26
Transpilation MD5: 9ebd3cb80bd73d149f11fba9599eb1ea
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-23 12:55:14
Source code size: 1251 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 237 / 516
Version history: 7 change(s)
Referenced in: [show references]