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

1  
// supports 8 different character classes
2  
sclass JSONSpecialIntegral {
3  
  int minLevel = 1, maxLevel = 8;
4  
5  
  byte[] input;
6  
  
7  
  int[] bracketLevel; // diff +1 for opening bracket, diff -1 for closing bracket
8  
  int[] bracketCount; // diff +1 for any bracket
9  
  byte[] quotesBitSet; // is character within string? (byte[] = faster replacement for BitSet)
10  
  
11  
  *() {}
12  
  *(byte[] *input) {}
13  
14  
  void analyze(S input) {
15  
    this.input = toUtf8(input); // XXX probably wrong
16  
    analyze();
17  
  }
18  
  
19  
  void analyze(byte[] input) {
20  
    this.input = input;
21  
    analyze();
22  
  }
23  
  
24  
  void analyze {
25  
    byte[] input = this.input;
26  
    int n = input.length;
27  
    bracketLevel = new int[n];
28  
    bracketCount = new int[n];
29  
    quotesBitSet = byteArrayBitSet_new(n);
30  
31  
    int bracketLvl = 0, bracketCnt = 0;
32  
    bool inQuotes;
33  
34  
    for i to n: {
35  
      byte b = input[i];
36  
      if (isOpener(b)) {
37  
        if (!inQuotes) { ++bracketLvl; ++bracketCnt; }
38  
      } else if (isCloser(b)) {
39  
        if (!inQuotes) { --bracketLvl; ++bracketCnt; }
40  
      } else if (inQuotes && b == '\'') {
41  
        bracketLevel[i] = bracketLvl;
42  
        bracketCount[i] = bracketCnt;
43  
        setBit(quotesBitSet, i, inQuotes);
44  
        i++;
45  
      } else if (b == '"') {
46  
        inQuotes = !inQuotes;
47  
      }
48  
      ifdef JSONSpecialIntegral_debug
49  
        printVars(+i, +b, c := (char) b, +bracketLvl, +bracketCnt, +inQuotes);
50  
      endifdef
51  
      bracketLevel[i] = bracketLvl;
52  
      bracketCount[i] = bracketCnt;
53  
      setBit(quotesBitSet, i, inQuotes);
54  
    }
55  
  }
56  
  
57  
  int bracketLevel(int idx) {
58  
    if (idx < 0) ret 0;
59  
    ret bracketLevel[min(bracketLevel.length-1, idx)];
60  
  }
61  
  
62  
  int bracketCount(int idx) {
63  
    if (idx < 0) ret 0;
64  
    ret bracketCount[min(bracketLevel.length-1, idx)];
65  
  }
66  
  
67  
  bool inQuoteBit(int idx) {
68  
    ret getBit(quotesBitSet, idx);
69  
  }
70  
  
71  
  bool isOpener(byte b) {
72  
    ret b == (byte) '{' || b == (byte) '[';
73  
  }
74  
  
75  
  bool isCloser(byte b) {
76  
    ret b == (byte) '}' || b == (byte) ']';
77  
  }
78  
  
79  
  S stats() {
80  
    ret renderVars(
81  
      inputLength := l(input),
82  
      sizeFactor := formatDouble((elementSize(bracketLevel)+elementSize(bracketCount)+1/8.0), 2);
83  
  }
84  
  
85  
  int n() { ret input.length; }
86  
}

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: 199 / 374
Version history: 15 change(s)
Referenced in: [show references]