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

123
LINES

< > BotCompany Repo | #1027363 // Fluid Text File Spike [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (4457L/24K).

1  
!7
2  
3  
cmodule FluidTextFileSpike > DynPrintLog {
4  
  srecord Entry(S header, S text, SS params) {
5  
    long targetIndex;
6  
    S actualText() { ret unquote(text); }
7  
  }
8  
  
9  
  switchable int maxRefLength = 8;
10  
11  
  long maxFileSize() {
12  
    ret rangeOfNHexNumberWithNDigits(maxRefLength);
13  
  }
14  
  
15  
  transient S demoContent = autoUnindent_mls([=[
16  
    refs="at *", name="index" [[
17  
      entry 1 at abc
18  
      entry 2 at def
19  
    ]]
20  
    i=abc [[hello]]
21  
    i=def [[world]]
22  
  ]=]);
23  
24  
  start-thread {
25  
    print(demoContent);
26  
    Producer<S> p = javaTok_onReader(stringReader(demoContent));
27  
    new StringBuilder buf;
28  
    new L<Entry> entries;
29  
    S t;
30  
    p.next(); // skip initial white space
31  
    while ((t = p.next()) != null) {
32  
      if (isMLS(t)) {
33  
        S header = getAndClearStringBuilder(buf);
34  
        entries.add(new Entry(header, t, parseEqualsCommaProperties(header)));
35  
        p.next(); // skip white space
36  
      } else
37  
        buf.append(t); // part of entry header
38  
    }
39  
    pnlStruct(entries);
40  
41  
    // make a map and pessimistically assign byte indices to all entries
42  
43  
    new Map<S, Entry> relocationMap;
44  
    for (Entry e : entries)
45  
      mapPut(relocationMap, e.params.get("i"), e);
46  
    makeIndices(entries);
47  
    print(+relocationMap);
48  
49  
    // rewrite references
50  
51  
    for (Entry e : entries)
52  
      if (hasReferences(e))
53  
        e.text = replaceRefs(e, r -> lookupInRelocationMap(relocationMap, r));
54  
55  
    // write out
56  
57  
    long idx = 0; // index into file
58  
    for (Entry e : entries) {
59  
      assertEquals(idx, e.targetIndex);
60  
      e.params.put("i", renderReference(e.targetIndex));
61  
      S line = entryToString(e, idx);
62  
      print_noNewLine(line);
63  
      idx += lUtf8(line);
64  
    }
65  
  }
66  
67  
  S lookupInRelocationMap(Map<S, Entry> relocationMap, S ref) {
68  
    Entry e = relocationMap.get(ref);
69  
    //print("lookupInRelocationMap: " + ref + " => " + e);
70  
    ret e == null ? null : renderReference(e.targetIndex);
71  
  }
72  
73  
  S renderReference(long idx) {
74  
    ret takeLast(maxRefLength, longToHex(idx));
75  
  }
76  
77  
  void makeIndices(L<Entry> entries) {
78  
    long idx = 0; // index into file
79  
    for (Entry e : entries) {
80  
      e.targetIndex = idx;
81  
      idx += pessimisticEntryLength(e);
82  
    }
83  
  }
84  
85  
  S entryToString(Entry e, long idx) {
86  
    ret entryToString(e, renderReference(idx));
87  
  }
88  
  
89  
  S entryToString(Entry e, S idx) {
90  
    SS params = litorderedmap(
91  
      i := idx,
92  
      l := intToHex_flexLength(l(e.text)));
93  
    mapPutAll_noOverwrite(params, e.params);
94  
    ret renderEqualsCommaProperties(params) + " " + e.text + "\n";
95  
  }  
96  
97  
  S dummyRef() {
98  
    ret rep('0', maxRefLength);
99  
  }
100  
101  
  bool hasReferences(Entry e) {
102  
    ret eq(e.params.get("refs"), quote("at *"));
103  
  }
104  
105  
  int pessimisticEntryLength(Entry e) {
106  
    Entry e2 = e;
107  
    if (hasReferences(e)) {
108  
      e2 = cloneObject(e);
109  
      e2.text = replaceRefs(e, r -> dummyRef());
110  
    }      
111  
    ret l(entryToString(e2, dummyRef()));
112  
  }
113  
114  
  S replaceRefs(Entry e, IF1<S> f) {
115  
    LS tok = javaTok(e.actualText());
116  
    for (int i : jfindAll(tok, "at *")) {
117  
      S x = f.get(tok.get(i+2));
118  
      if (nempty(x))
119  
        tok.set(i+2, x);
120  
    }
121  
    ret multiLineQuote(join(tok));
122  
  }
123  
}

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: #1027363
Snippet name: Fluid Text File Spike [dev.]
Eternal ID of this version: #1027363/37
Text MD5: 7393ceb1a88d71a5e2d158c004da4c8a
Transpilation MD5: fdbb6d30c7bb59484f6ed6a3846c2c98
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-15 15:59:38
Source code size: 3120 bytes / 123 lines
Pitched / IR pitched: No / No
Views / Downloads: 202 / 695
Version history: 36 change(s)
Referenced in: [show references]