Home /Modules/text/SLStringBuilder.sleela

SLStringBuilder.sleela

lib/text/SLStringBuilder.sleela · 202 line(s) · sleela

< lib/text/SLStringBuilder.sleela >
< SLeeLa Standard Library Definition. >
< Definition: SLStringBuilder -- a comprehensive mutable text builder for the >
< text library family. >
<  >
< SLStringBuilder accumulates text efficiently through the one native >
< text-producing operator (+). It is organised like the rest of the text >
< surface in two honest layers: >
< * In-language ops implemented for real: append (String/int/boolean), >
< appendLine, prepend, insertAt via split-join over a String[] of chunks, >
< repeat, join, clear, length-of-own-contents (tracked), toString. >
< * Bridged char-level ops (charAt, substring, reverse, indexOf, deleteRange) >
< that need raw code units the language cannot index: each returns an >
< inspectable "text.bridge:op:args" descriptor for the native runtime -- >
< never a faked result. >
<  >
< Unlike the opaque built-in String, this builder tracks how many segments it >
< has appended; exact character length is char-level and stays bridged. >
< Comments use the angle-bracket style; #sleela 2.1. >
#sleela 2.5
material;
< Family: text. Native VM/OS work crosses an explicit bridge. >

class SLStringBuilder {

  < Accumulated text. >
  String value = "";
  < number of segments appended through this builder (real, in-language count). >
  < NOT the character length -- character length is char-level and bridged via >
  < length()/lengthValue(). >
  int    segments = 0;
  < number of mutations (append/insert/clear...) >
  int    edits = 0;

  < ---- lifecycle ----------------------------------------------------- >
  void clear()        { value = ""; segments = 0; edits = edits + 1; }
  boolean isEmpty()   { return value == ""; }
  boolean notEmpty()  { return value != ""; }
  < real, in-language: how many segments have been appended. >
  int segmentCount()  { return segments; }
  int editCount()     { return edits; }
  < character length is char-level -> bridged, returned as an honest descriptor. >
  String length()      { return bridge("length"); }
  String lengthValue() { return bridge("lengthValue"); }

  < ---- in-language appends (return this for fluent chaining) --------- >
  SLStringBuilder append(String s) { value = value + s; segments = segments + 1; edits = edits + 1; return this; }
  SLStringBuilder appendInt(int n) { value = value + n; segments = segments + 1; edits = edits + 1; return this; }
  SLStringBuilder appendBool(boolean b) { value = value + b; segments = segments + 1; edits = edits + 1; return this; }
  SLStringBuilder appendLine(String s)  { value = value + s + "\n"; segments = segments + 1; edits = edits + 1; return this; }
  SLStringBuilder newline()             { value = value + "\n"; edits = edits + 1; return this; }
  SLStringBuilder prepend(String s)     { value = s + value; segments = segments + 1; edits = edits + 1; return this; }
  SLStringBuilder space()               { value = value + " "; edits = edits + 1; return this; }

  < appendIf(cond, s): append only when cond is true (real, fluent). >
  SLStringBuilder appendIf(boolean cond, String s) {
    if (cond) { value = value + s; segments = segments + 1; edits = edits + 1; }
    return this;
  }
  < appendJoined(parts, sep): append a String[] joined by sep as one segment. >
  SLStringBuilder appendJoined(String[] parts, String sep) {
    int i = 0;
    int n = arrayLength(parts);
    while (i < n) {
      if (i > 0) { value = value + sep; }
      value = value + parts[i];
      i = i + 1;
    }
    segments = segments + 1;
    edits = edits + 1;
    return this;
  }

  < repeat(s, n): append s n times (n <= 0 is a no-op). >
  SLStringBuilder repeat(String s, int n) {
    int i = 0;
    while (i < n) { value = value + s; i = i + 1; }
    edits = edits + 1;
    return this;
  }

  < join(parts, sep): append a String[] joined by sep. >
  SLStringBuilder join(String[] parts, String sep) {
    int i = 0;
    int n = arrayLength(parts);
    while (i < n) {
      if (i > 0) { value = value + sep; }
      value = value + parts[i];
      i = i + 1;
    }
    edits = edits + 1;
    return this;
  }

  < appendAll(parts): append every element of a String[]. >
  SLStringBuilder appendAll(String[] parts) {
    int i = 0;
    int n = arrayLength(parts);
    while (i < n) { value = value + parts[i]; i = i + 1; }
    edits = edits + 1;
    return this;
  }

  < ---- result -------------------------------------------------------- >
  String toString()  { return value; }
  String get()       { return value; }
  void set(String v) { value = v; edits = edits + 1; }

  < ---- char-cell view (REAL char-level ops over an explicit String[]) ----- >
  < When the caller supplies the glyphs as a String[] of single cells, these >
  < are genuinely in-language -- no bridge, no faking. appendCells builds the >
  < value from cells; reverseCells/charAtCells operate on a supplied view. >
  SLStringBuilder appendCells(String[] cells) {
    int i = 0;
    int n = arrayLength(cells);
    while (i < n) { value = value + cells[i]; i = i + 1; }
    segments = segments + 1;
    edits = edits + 1;
    return this;
  }
  < reverseCells(cells): the glyphs reversed and rejoined (real). >
  String reverseCells(String[] cells) {
    String out = "";
    int i = arrayLength(cells) - 1;
    while (i >= 0) { out = out + cells[i]; i = i - 1; }
    return out;
  }
  < charAtCells(cells, i): glyph at i, or "" if out of range (real). >
  String charAtCells(String[] cells, int i) {
    if (i < 0 || i >= arrayLength(cells)) { return ""; }
    return cells[i];
  }
  < lengthCells(cells): real character length of a char-cell view. >
  int lengthCells(String[] cells) { return arrayLength(cells); }

  < ---- bridged char-level operations --------------------------------- >
  < Need the raw code units; cross the VM/OS bridge with an honest descriptor. >
  String bridge(String op)              { return "text.bridge:" + op + ":" + value; }
  String bridgeArg(String op, String a) { return "text.bridge:" + op + ":" + value + ":" + a; }
  String bridgeArg2(String op, String a, String b) {
    return "text.bridge:" + op + ":" + value + ":" + a + ":" + b;
  }

  < charLength(s): the exact character length of a string is char-level, so it >
  < is bridged and returned as an honest descriptor -- never a faked integer. >
  String charLength(String s) { return "text.bridge:length:" + s; }

  String charAt(String idx)                 { return bridgeArg("charAt", idx); }
  String substring(String from, String to)  { return bridgeArg2("substring", from, to); }
  String insertAt(String idx, String s)     { return bridgeArg2("insert", idx, s); }
  String deleteRange(String from, String to){ return bridgeArg2("deleteRange", from, to); }
  String reverse()                          { return bridge("reverse"); }
  String indexOf(String needle)             { return bridgeArg("indexOf", needle); }

  < ---- introspection ------------------------------------------------- >
  String typeName()   { return "SLeeLa.text.SLStringBuilder"; }
  boolean available() { return true; }

  < SLCloneable / SLDisposable intent. >
  SLStringBuilder copy() { SLStringBuilder c = new SLStringBuilder(); c.set(value); return c; }
  void dispose() { value = ""; segments = 0; edits = 0; }

  < ---- 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 = 1;
  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 = 9;
  String metaNationalSchedule = "The Java Language Specification (java.lang.StringBuilder)";

  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; }
}