Libraryless. Click here for Pure Java version (5052L/28K).
1 | // i must point at the (possibly imaginary) opening bracket ("{") |
2 | // index returned is index of closing bracket + 1 (or l(tok)) |
3 | |
4 | // TODO: could use iterator to be even faster |
5 | |
6 | static int tok_findEndOfBlock_IContentsIndexedList2(IContentsIndexedList2<S> tok, int i) { |
7 | int n = l(tok); |
8 | |
9 | // find opening & closing brackets after start index |
10 | |
11 | TreeSet<HasIndex> openers = tok.indicesOf_treeSetOfHasIndex("{"); |
12 | TreeSet<HasIndex> closers = tok.indicesOf_treeSetOfHasIndex("}"); |
13 | |
14 | // now step through both sets |
15 | |
16 | int level = 1; |
17 | HasIndex start = new(i); |
18 | HasIndex nextOpener = openers.higher(start); |
19 | HasIndex nextCloser = closers.higher(start); |
20 | |
21 | while true { |
22 | if (nextCloser == null) ret n; // block isn't closed |
23 | |
24 | if (nextOpener != null && nextOpener.idx < nextCloser.idx) { |
25 | // process opener |
26 | ++level; |
27 | nextOpener = openers.higher(nextOpener); |
28 | } else { |
29 | // process closer |
30 | if (--level == 0) |
31 | ret nextCloser.idx+1; |
32 | nextCloser = closers.higher(nextCloser); |
33 | } |
34 | } |
35 | } |
Began life as a copy of #1025221
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034666 |
Snippet name: | tok_findEndOfBlock_IContentsIndexedList2 |
Eternal ID of this version: | #1034666/3 |
Text MD5: | 5ff30397f2d98fb74710d6da5c60d34a |
Transpilation MD5: | 6b39a5c815a279a849f9ffe4f09caa7b |
Author: | stefan |
Category: | javax / transpiling |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-01 15:50:59 |
Source code size: | 1068 bytes / 35 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 166 / 226 |
Version history: | 2 change(s) |
Referenced in: | [show references] |