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).

sclass CCI_Choice extends CharacterClassIterator {
  bool canEndHere;
  
  // can be a HashMap or TreeMap (e.g. with special comparator)
  Map<Char, CharacterClassIterator> map;
  
  // computed on demand
  CharRange acceptedCharRange;

  *() {}
  *(bool canEndHere, Map<Char, CharacterClassIterator> *map) {}
  
  CharacterClassIterator acceptChar(char c) {
    ret assertNotNull(map.get(c));
  }

  bool canEndHere() {
    ret canEndHere;
  }
  
  bool canAcceptChar(char c) {
    ret map.containsKey(c);
  }

  Cl<Char> acceptedChars() { ret keys(map); }

  CharRange acceptedCharRange() {
    if (empty(map)) null;
    if (acceptedCharRange == null)
      acceptedCharRange = calcAcceptedCharRange();
    ret acceptedCharRange;
  }
  
  CharRange calcAcceptedCharRange() {
    if (map instanceof TreeMap) {
      ret CharRange(firstKey(map), lastKey((TreeMap<Char, CharacterClassIterator>) map));
    } else {
      char min = 0xFFFF, max = 0;
      for (char c : keys(map)) {
        if (c < min) min = c;
        if (c > max) max = c;
      }
      ret CharRange(min, max); 
    }
  }
  
  // object is immutable, so no cloning necessary
  CharacterClassIterator cloneCCI() {
    this;
  }
}  

Author comment

Began life as a copy of #1029558

download  show line numbers  debug dex   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, 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: 46 / 131
Version history: 7 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1029560 & http://1029560.tinybrain.de