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

86
LINES

< > BotCompany Repo | #1032592 // JSONSpecialIntegral - find special characters quickly in long string (e.g. brackets, quotes, backslash)

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

Libraryless. Click here for Pure Java version (4074L/23K).

// supports 8 different character classes
sclass JSONSpecialIntegral {
  int minLevel = 1, maxLevel = 8;

  byte[] input;
  
  int[] bracketLevel; // diff +1 for opening bracket, diff -1 for closing bracket
  int[] bracketCount; // diff +1 for any bracket
  byte[] quotesBitSet; // is character within string? (byte[] = faster replacement for BitSet)
  
  *() {}
  *(byte[] *input) {}

  void analyze(S input) {
    this.input = toUtf8(input); // XXX probably wrong
    analyze();
  }
  
  void analyze(byte[] input) {
    this.input = input;
    analyze();
  }
  
  void analyze {
    byte[] input = this.input;
    int n = input.length;
    bracketLevel = new int[n];
    bracketCount = new int[n];
    quotesBitSet = byteArrayBitSet_new(n);

    int bracketLvl = 0, bracketCnt = 0;
    bool inQuotes;

    for i to n: {
      byte b = input[i];
      if (isOpener(b)) {
        if (!inQuotes) { ++bracketLvl; ++bracketCnt; }
      } else if (isCloser(b)) {
        if (!inQuotes) { --bracketLvl; ++bracketCnt; }
      } else if (inQuotes && b == '\'') {
        bracketLevel[i] = bracketLvl;
        bracketCount[i] = bracketCnt;
        setBit(quotesBitSet, i, inQuotes);
        i++;
      } else if (b == '"') {
        inQuotes = !inQuotes;
      }
      ifdef JSONSpecialIntegral_debug
        printVars(+i, +b, c := (char) b, +bracketLvl, +bracketCnt, +inQuotes);
      endifdef
      bracketLevel[i] = bracketLvl;
      bracketCount[i] = bracketCnt;
      setBit(quotesBitSet, i, inQuotes);
    }
  }
  
  int bracketLevel(int idx) {
    if (idx < 0) ret 0;
    ret bracketLevel[min(bracketLevel.length-1, idx)];
  }
  
  int bracketCount(int idx) {
    if (idx < 0) ret 0;
    ret bracketCount[min(bracketLevel.length-1, idx)];
  }
  
  bool inQuoteBit(int idx) {
    ret getBit(quotesBitSet, idx);
  }
  
  bool isOpener(byte b) {
    ret b == (byte) '{' || b == (byte) '[';
  }
  
  bool isCloser(byte b) {
    ret b == (byte) '}' || b == (byte) ']';
  }
  
  S stats() {
    ret renderVars(
      inputLength := l(input),
      sizeFactor := formatDouble((elementSize(bracketLevel)+elementSize(bracketCount)+1/8.0), 2);
  }
  
  int n() { ret input.length; }
}

Author comment

Began life as a copy of #1032583

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032592
Snippet name: JSONSpecialIntegral - find special characters quickly in long string (e.g. brackets, quotes, backslash)
Eternal ID of this version: #1032592/16
Text MD5: d70c608ede79a4fa9815f51d034cf3a5
Transpilation MD5: 2b4c09dafc0fbe95a23bf89db1f186db
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-23 09:42:15
Source code size: 2259 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 104 / 240
Version history: 15 change(s)
Referenced in: [show references]