string-cells-demo.sleela
lib/text/string-cells-demo.sleela · 210 line(s) · sleela
#sleela 2.5
material;
< string-cells-demo.sleela -- a self-contained, runnable reference for the >
< REAL char-cell toolkit added to the SLeeLa text/String surface: the new >
< subsequence search/replace, split, explicit-table case mapping, trim, and >
< padding/truncation helpers that are the stateless companion to SLString's >
< value-bound char-cell section (SLStringCells). >
< >
< The compiler's `run` compiles one file as a single unit, so this aggregate >
< inlines a compact SLStringCells plus a `main` that exercises it the way an >
< application (lexer, column formatter, template engine) would. Everything >
< printed is a REAL result -- no bridge, no faking: the semantics are exactly >
< the cells and tables supplied here. The authoritative class lives alongside >
< this file as SLStringCells.sleela. >
class SLStringCells {
< ---- 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 this record.
Values are faithful to the subject's real origin where publicly known. >>
String metaVersion = "1.0.0";
int metaId = 11558;
String metaAuthor = "Kiro Agent";
String metaDate = "2026-10-25";
String metaCopyright = "SLeeLa project";
String metaTrademark = "none";
String metaCorporation = "none";
String metaOrigin = "International";
String metaBirthDate = "2026-10-25";
int metaScienceRating = 9;
String metaNationalSchedule = "Unicode / ISO/IEC 10646; ANSI X3.4 (US-ASCII)";
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; }
String joinCells(String[] cells) {
String out = ""; int i = 0; int n = arrayLength(cells);
while (i < n) { out = out + cells[i]; i = i + 1; }
return out;
}
String[] slice(String[] cells, int from, int to) {
int n = arrayLength(cells); int lo = from; if (lo < 0) { lo = 0; }
int hi = to; if (hi > n) { hi = n; }
if (lo >= hi) { return new String[0]; }
int len = hi - lo; String[] out = new String[len]; int i = 0;
while (i < len) { out[i] = cells[lo + i]; i = i + 1; }
return out;
}
boolean matchesAt(String[] cells, int at, String[] needle) {
int nn = arrayLength(needle); int cn = arrayLength(cells);
if (at < 0 || at + nn > cn) { return false; }
int i = 0;
while (i < nn) { if (cells[at + i] != needle[i]) { return false; } i = i + 1; }
return true;
}
int indexOfSeq(String[] cells, String[] needle) {
int nn = arrayLength(needle); int cn = arrayLength(cells);
if (nn == 0) { return 0; }
int start = 0;
while (start + nn <= cn) { if (matchesAt(cells, start, needle)) { return start; } start = start + 1; }
return -1;
}
boolean containsSeq(String[] cells, String[] needle) { return indexOfSeq(cells, needle) >= 0; }
int countSeq(String[] cells, String[] needle) {
int nn = arrayLength(needle); int cn = arrayLength(cells);
if (nn == 0) { return 0; }
int c = 0; int start = 0;
while (start + nn <= cn) {
if (matchesAt(cells, start, needle)) { c = c + 1; start = start + nn; }
else { start = start + 1; }
}
return c;
}
String replaceSeq(String[] cells, String[] from, String[] to) {
int fn = arrayLength(from); int cn = arrayLength(cells);
if (fn == 0) { return joinCells(cells); }
String out = ""; int i = 0;
while (i < cn) {
if (i + fn <= cn && matchesAt(cells, i, from)) {
int j = 0; int tn = arrayLength(to);
while (j < tn) { out = out + to[j]; j = j + 1; }
i = i + fn;
} else { out = out + cells[i]; i = i + 1; }
}
return out;
}
String[] splitOn(String[] cells, String sepCell) {
int n = arrayLength(cells); int segs = 1; int i = 0;
while (i < n) { if (cells[i] == sepCell) { segs = segs + 1; } i = i + 1; }
String[] out = new String[segs]; int s = 0; String cur = ""; i = 0;
while (i < n) {
if (cells[i] == sepCell) { out[s] = cur; s = s + 1; cur = ""; }
else { cur = cur + cells[i]; }
i = i + 1;
}
out[s] = cur;
return out;
}
String mapCell(String cell, String[] fromSet, String[] toSet) {
int n = arrayLength(fromSet); if (arrayLength(toSet) < n) { n = arrayLength(toSet); }
int k = 0;
while (k < n) { if (cell == fromSet[k]) { return toSet[k]; } k = k + 1; }
return cell;
}
String[] map(String[] cells, String[] fromSet, String[] toSet) {
int n = arrayLength(cells); String[] out = new String[n]; int i = 0;
while (i < n) { out[i] = mapCell(cells[i], fromSet, toSet); i = i + 1; }
return out;
}
String mapToString(String[] cells, String[] fromSet, String[] toSet) { return joinCells(map(cells, fromSet, toSet)); }
boolean isBlankCell(String cell, String[] blanks) {
int i = 0; int n = arrayLength(blanks);
while (i < n) { if (blanks[i] == cell) { return true; } i = i + 1; }
return false;
}
String trim(String[] cells, String[] blanks) {
int n = arrayLength(cells); int lo = 0;
while (lo < n && isBlankCell(cells[lo], blanks)) { lo = lo + 1; }
int hi = n - 1;
while (hi >= lo && isBlankCell(cells[hi], blanks)) { hi = hi - 1; }
String out = ""; int i = lo;
while (i <= hi) { out = out + cells[i]; i = i + 1; }
return out;
}
String padCenter(String[] cells, int width, String padCell) {
int n = arrayLength(cells); if (n >= width) { return joinCells(cells); }
int total = width - n; int left = total / 2; int right = total - left;
String lpad = ""; int i = 0;
while (i < left) { lpad = lpad + padCell; i = i + 1; }
String rpad = ""; i = 0;
while (i < right) { rpad = rpad + padCell; i = i + 1; }
return lpad + joinCells(cells) + rpad;
}
String truncate(String[] cells, int width, String ellipsisCell) {
int n = arrayLength(cells); if (width <= 0) { return ""; }
if (n <= width) { return joinCells(cells); }
String out = ""; int keep = width - 1; int i = 0;
while (i < keep) { out = out + cells[i]; i = i + 1; }
return out + ellipsisCell;
}
void main() {
print("=== SLeeLa char-cell toolkit demo (all REAL, no bridge) ===");
< A "mississippi" view as explicit glyph cells. >
String[] miss = new String[11];
miss[0]="m"; miss[1]="i"; miss[2]="s"; miss[3]="s"; miss[4]="i"; miss[5]="s";
miss[6]="s"; miss[7]="i"; miss[8]="p"; miss[9]="p"; miss[10]="i";
print("text : " + joinCells(miss));
< multi-cell subsequence search ("ss"). >
String[] ss = new String[2]; ss[0]="s"; ss[1]="s";
print("indexOf ss : " + indexOfSeq(miss, ss));
print("contains ss : " + containsSeq(miss, ss));
print("count ss : " + countSeq(miss, ss));
< subsequence replace: "iss" -> "---". >
String[] iss = new String[3]; iss[0]="i"; iss[1]="s"; iss[2]="s";
String[] dash = new String[3]; dash[0]="-"; dash[1]="-"; dash[2]="-";
print("replace iss : " + replaceSeq(miss, iss, dash));
< split a CSV-ish cell view on ",". >
String[] csv = new String[7];
csv[0]="a"; csv[1]=","; csv[2]="b"; csv[3]="b"; csv[4]=","; csv[5]=""; csv[6]="c";
String[] fields = splitOn(csv, ",");
print("split fields: " + arrayLength(fields));
print("field[0] : [" + fields[0] + "]");
print("field[1] : [" + fields[1] + "]");
< explicit-table case mapping (honest: the table IS the semantics). >
String[] abc = new String[3]; abc[0]="a"; abc[1]="b"; abc[2]="c";
String[] lo = new String[3]; lo[0]="a"; lo[1]="b"; lo[2]="c";
String[] up = new String[3]; up[0]="A"; up[1]="B"; up[2]="C";
print("map upper : " + mapToString(abc, lo, up));
< trim over an explicit blank set. >
String[] padded = new String[6];
padded[0]=" "; padded[1]=" "; padded[2]="h"; padded[3]="i"; padded[4]=" "; padded[5]=" ";
String[] blanks = new String[1]; blanks[0]=" ";
print("trim : [" + trim(padded, blanks) + "]");
< centre + truncate to real character widths. >
String[] hi = new String[2]; hi[0]="h"; hi[1]="i";
print("padCenter 6 : [" + padCenter(hi, 6, ".") + "]");
print("truncate 4 : [" + truncate(miss, 4, "~") + "]");
print("done");
}
}