Uses 911K of libraries. Click here for Pure Java version (4457L/24K).
!7 cmodule FluidTextFileSpike > DynPrintLog { srecord Entry(S header, S text, SS params) { long targetIndex; S actualText() { ret unquote(text); } } switchable int maxRefLength = 8; long maxFileSize() { ret rangeOfNHexNumberWithNDigits(maxRefLength); } transient S demoContent = autoUnindent_mls([=[ refs="at *", name="index" [[ entry 1 at abc entry 2 at def ]] i=abc [[hello]] i=def [[world]] ]=]); start-thread { print(demoContent); Producer<S> p = javaTok_onReader(stringReader(demoContent)); new StringBuilder buf; new L<Entry> entries; S t; p.next(); // skip initial white space while ((t = p.next()) != null) { if (isMLS(t)) { S header = getAndClearStringBuilder(buf); entries.add(new Entry(header, t, parseEqualsCommaProperties(header))); p.next(); // skip white space } else buf.append(t); // part of entry header } pnlStruct(entries); // make a map and pessimistically assign byte indices to all entries new Map<S, Entry> relocationMap; for (Entry e : entries) mapPut(relocationMap, e.params.get("i"), e); makeIndices(entries); print(+relocationMap); // rewrite references for (Entry e : entries) if (hasReferences(e)) e.text = replaceRefs(e, r -> lookupInRelocationMap(relocationMap, r)); // write out long idx = 0; // index into file for (Entry e : entries) { assertEquals(idx, e.targetIndex); e.params.put("i", renderReference(e.targetIndex)); S line = entryToString(e, idx); print_noNewLine(line); idx += lUtf8(line); } } S lookupInRelocationMap(Map<S, Entry> relocationMap, S ref) { Entry e = relocationMap.get(ref); //print("lookupInRelocationMap: " + ref + " => " + e); ret e == null ? null : renderReference(e.targetIndex); } S renderReference(long idx) { ret takeLast(maxRefLength, longToHex(idx)); } void makeIndices(L<Entry> entries) { long idx = 0; // index into file for (Entry e : entries) { e.targetIndex = idx; idx += pessimisticEntryLength(e); } } S entryToString(Entry e, long idx) { ret entryToString(e, renderReference(idx)); } S entryToString(Entry e, S idx) { SS params = litorderedmap( i := idx, l := intToHex_flexLength(l(e.text))); mapPutAll_noOverwrite(params, e.params); ret renderEqualsCommaProperties(params) + " " + e.text + "\n"; } S dummyRef() { ret rep('0', maxRefLength); } bool hasReferences(Entry e) { ret eq(e.params.get("refs"), quote("at *")); } int pessimisticEntryLength(Entry e) { Entry e2 = e; if (hasReferences(e)) { e2 = cloneObject(e); e2.text = replaceRefs(e, r -> dummyRef()); } ret l(entryToString(e2, dummyRef())); } S replaceRefs(Entry e, IF1<S> f) { LS tok = javaTok(e.actualText()); for (int i : jfindAll(tok, "at *")) { S x = f.get(tok.get(i+2)); if (nempty(x)) tok.set(i+2, x); } ret multiLineQuote(join(tok)); } }
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: | 201 / 694 |
Version history: | 36 change(s) |
Referenced in: | -