SLFormatter.sleela
lib/text/SLFormatter.sleela · 241 line(s) · sleela
< lib/text/SLFormatter.sleela >
< SLeeLa Standard Library Definition. >
< Definition: SLFormatter -- a comprehensive text formatter for the text >
< library family. >
< >
< SLFormatter assembles formatted text. It splits into two honest layers: >
< * In-language assembly implemented for real over (+): positional >
< substitution helpers (with one/two/three args), key/value lines, list >
< rendering over a String[], padding by repetition, and separators. >
< * Bridged printf-style formatting (%d, %s, %5.2f, locale, width/precision) >
< that needs char-level spec parsing: format()/formatArgs() return an >
< inspectable "text.bridge:format:<pattern>:<args>" descriptor the native >
< runtime fulfils -- never a faked result. >
< >
< Comments use the angle-bracket style; #sleela 2.1. >
#sleela 2.5
material;
< Family: text. Native VM/OS work crosses an explicit bridge. >
class SLFormatter {
< Last pattern set, for stateful callers; the helpers are also stateless. >
String value = "";
void set(String v) { value = v; }
String get() { return value; }
void clear() { value = ""; }
< ---- in-language assembly (real) ---------------------------------- >
< render1..render3: substitute {} placeholders left to right with args, >
< built purely with +. The pattern is split on the literal "{}" marker. >
String render1(String a) { return fill(value, a); }
String render2(String a, String b) { return fill(fill(value, a), b); }
String render3(String a, String b, String c) { return fill(fill(fill(value, a), b), c); }
< fill(pattern, arg): appends arg to pattern. Finding a "{}" marker inside a >
< raw pattern is char-level work the language cannot do, so fill() is the >
< honest append primitive render1..3 build on; for true positional templating >
< use template() below (caller supplies the literal segments) or the bridged >
< format(). >
String fill(String pattern, String arg) { return pattern + arg; }
< template(segments, args): REAL positional substitution, fully in-language. >
< The caller supplies the literal pieces already split, so no char search is >
< needed. The result interleaves segments and args: >
< segments = ["Hello, ", "! You have ", " messages."] >
< args = ["Max", "3"] >
< -> "Hello, Max! You have 3 messages." >
< Works for any counts: leftover segments are appended, missing args are >
< treated as "". This is the dependable template engine for the text family. >
String template(String[] segments, String[] args) {
String out = "";
int s = arrayLength(segments);
int a = arrayLength(args);
int i = 0;
while (i < s) {
out = out + segments[i];
if (i < a) { out = out + args[i]; }
i = i + 1;
}
< any extra args beyond the segment slots are appended in order >
while (i < a) { out = out + args[i]; i = i + 1; }
return out;
}
< templateSep(args, sep): join args with a separator and no literal frame -- >
< a common special case of template(). Real. >
String templateSep(String[] args, String sep) {
return list(args, sep);
}
< prefixSuffix: the dependable in-language form -- caller supplies the parts. >
String wrap(String prefix, String body, String suffix) { return prefix + body + suffix; }
< key/value line rendering (real). >
String kv(String key, String val) { return key + ": " + val; }
String kvLine(String key, String val) { return key + ": " + val + "\n"; }
String field(String key, String val, String sep) { return key + sep + val; }
< list(parts, sep): render a String[] separated by sep (real). >
String list(String[] parts, String sep) {
String out = "";
int i = 0;
int n = arrayLength(parts);
while (i < n) {
if (i > 0) { out = out + sep; }
out = out + parts[i];
i = i + 1;
}
return out;
}
< bullets(parts): render a String[] as a dashed bullet block (real). >
String bullets(String[] parts) {
String out = "";
int i = 0;
int n = arrayLength(parts);
while (i < n) { out = out + "- " + parts[i] + "\n"; i = i + 1; }
return out;
}
< numbered(parts): render a String[] as a 1-based numbered list (real). >
String numbered(String[] parts) {
String out = "";
int i = 0;
int n = arrayLength(parts);
while (i < n) { out = out + (i + 1) + ". " + parts[i] + "\n"; i = i + 1; }
return out;
}
< table2(keys, vals): render two parallel String[] as aligned "key: val" >
< lines (real). Uses the shorter of the two lengths. >
String table2(String[] keys, String[] vals) {
String out = "";
int i = 0;
int nk = arrayLength(keys);
int nv = arrayLength(vals);
int n = nk;
if (nv < nk) { n = nv; }
while (i < n) { out = out + keys[i] + ": " + vals[i] + "\n"; i = i + 1; }
return out;
}
< repeatChar(padChar, count): padChar repeated count times (real). The >
< in-language padding primitive. count <= 0 yields "". >
String repeatChar(String padChar, int count) {
String out = "";
int i = 0;
while (i < count) { out = out + padChar; i = i + 1; }
return out;
}
< pad / padRight(s, count, padChar): append `count` pad units after s (real). >
< Honest naming: this adds `count` units of padding (it cannot measure s to >
< pad to an absolute column -- that is the bridged padTo()). >
String pad(String s, int count, String padChar) { return s + repeatChar(padChar, count); }
String padRight(String s, int count, String padChar) { return s + repeatChar(padChar, count); }
< padLeft(s, count, padChar): prepend `count` pad units before s (real). >
String padLeft(String s, int count, String padChar) { return repeatChar(padChar, count) + s; }
< indent(block, padChar, count): prefix with `count` pad units (real). >
String indent(String block, String padChar, int count) {
return repeatChar(padChar, count) + block;
}
< ---- real column alignment over a char-cell view ------------------- >
< When the caller supplies the content as a String[] of glyph cells, we know >
< its true character width (arrayLength), so we can align to an ABSOLUTE >
< column fully in-language -- the exact-column case that the raw-String >
< padLeft/padRight could not do and padTo() had to bridge. >
String columnLeft(String[] cells, int width, String padChar) {
String body = fromCells(cells);
int n = arrayLength(cells);
String pad = "";
int i = n;
while (i < width) { pad = pad + padChar; i = i + 1; }
return body + pad;
}
String columnRight(String[] cells, int width, String padChar) {
String body = fromCells(cells);
int n = arrayLength(cells);
String pad = "";
int i = n;
while (i < width) { pad = pad + padChar; i = i + 1; }
return pad + body;
}
String columnCenter(String[] cells, int width, String padChar) {
int n = arrayLength(cells);
int total = width - n;
if (total < 0) { total = 0; }
int left = total / 2;
int right = total - left;
return repeatChar(padChar, left) + fromCells(cells) + repeatChar(padChar, right);
}
< fromCells(cells): rejoin a char-cell array into a String (real). >
String fromCells(String[] cells) {
String out = "";
int i = 0;
int n = arrayLength(cells);
while (i < n) { out = out + cells[i]; i = i + 1; }
return out;
}
< ---- bridged printf-style formatting ------------------------------ >
String bridge(String op, String a) { return "text.bridge:" + op + ":" + a; }
String bridgeArg(String op, String a, String b){ return "text.bridge:" + op + ":" + a + ":" + b; }
< format(pattern, args): printf-style, needs spec parsing -> bridged. >
String format(String pattern, String args) { return bridgeArg("format", pattern, args); }
String formatOne(String pattern, String a) { return bridgeArg("format", pattern, a); }
String padTo(String s, String width) { return bridgeArg("padTo", s, width); }
String alignRight(String s, String width) { return bridgeArg("alignRight", s, width); }
< ---- introspection ------------------------------------------------- >
String typeName() { return "SLeeLa.text.SLFormatter"; }
boolean available() { return true; }
< SLCloneable / SLDisposable intent. >
SLFormatter copy() { SLFormatter c = new SLFormatter(); c.set(value); return c; }
void dispose() { value = ""; }
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
< Eleven catalogued attributes with getter/setter pairs, so a GUI, loader, >
< or catalog can read and (where appropriate) override each record. >
< Values are faithful to the subject's real origin where publicly known. >
String metaVersion = "1.0.0";
int metaId = 3;
String metaAuthor = "SLeeLa standard library";
String metaDate = "2026-10-23";
String metaCopyright = "MEARVK LLC";
String metaTrademark = "none";
String metaCorporation = "MEARVK LLC";
String metaOrigin = "International";
String metaBirthDate = "2026";
int metaScienceRating = 8;
String metaNationalSchedule = "The Java Language Specification (java.util.Formatter); IEEE Std 1003.1 printf";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
}