Popup YouTube Video
Sheerpower Logo

String handling


Why Sheerpower Strings Are Different

In many languages, strings are expensive — they allocate memory, trigger garbage collection, and often require copying even for simple operations.

Sheerpower takes a different approach. Strings are designed as reusable memory buffers, with support for zero-copy slicing (VIEW), high-speed overlay, and compile-time optimization.

This means you can process millions of strings with predictable performance — without special libraries, workarounds, or manual optimization.

Business Application Impact

Business applications process massive amounts of text: customer data, logs, transactions, and reports.

Sheerpower is designed for this workload. Strings are handled with high efficiency and predictable performance, even at scale.

Unlike many modern languages, Sheerpower provides memory safety without relying on garbage collection, eliminating pauses and reducing overhead.

The result is a system where string-heavy applications remain fast, stable, and easy to reason about.

Performance and Memory Architecture

Sheerpower String Handling Overview

Sheerpower's string system is designed around a single principle: avoid unnecessary allocation and copying.

  • Mutability & Buffer Reuse

    Strings in Sheerpower are mutable. If a variable holds "The rain in Spain" and is updated to "Enjoy life", Sheerpower repurposes the existing memory buffer rather than creating a new object.
  • No Garbage Collection

    Sheerpower manages memory through dynamic memory pools. You do not need to worry about memory leaks, allocations, or deallocations. And because there is no garbage collector, your application avoids the random slowdowns and pauses common in other languages.
  • Smart Copying (SIDs)

    Every string has a globally unique String ID (SID). When copying strings, Sheerpower checks the SID first. If the source and target SIDs already match, it skips the copy process entirely to increase performance.
  • Efficiency Example: SIDs used as a Hinting System

    Sheerpower uses an internal String ID (SID) based hinting system.
    • You read the Bible into bible$.
    • You call getword$(bible$, 1000). The system records this position.
    • A subsequent call to getword$(bible$, 1001) resumes directly from that SID-enabled hint, rather than re-scanning the first 1000 words.
    • Zero-Copy String Views

      For highly efficient string parsing, Sheerpower offers a VIEW feature that creates zero-copy views directly into a source string. Instead of allocating new substrings, a view stores only positions and references the current buffer of the source string.

      If the source string changes, the VIEW reflects that change on the next reference. Because views are recalculated only when used, and cached, repeated access is very fast — even when parsing large text records or scanning thousands of fields.

      For a detailed guide to string views, see String Views (Zero-Copy Slices).


Sheerpower provides memory safety without garbage collection. This eliminates the string handling slowdowns that other languages experience

Note: See the fileinfo$() function for an easy way to read the entire contents of a file, such as the Bible, into a variable.

Basic Operations: Concatenation

To concatenate (combine) two or more strings, use the plus sign (+). For example, string1 + string2 will join them. Additionally, you can use the JOIN() function when you need to concatenate a large number of strings efficiently.
fullname$ = 'Sally Sue' sentence$ = fullname$ + ' was here.' print sentence$ sentence$ = 'So I heard.' print sentence$ sentence$ = '' l = join(sentence$, fullname$, ' was here') print sentence$

Special Assignment Statements: LSET, CSET, RSET

In Sheerpower, three special assignment statements let you modify an existing string without creating a new string. These statements overlay new text directly into the target variable's current buffer:

  • LSET — left-justify and space-fill
  • CSET — center within the target
  • RSET — right-justify and space-fill

These operations never expand or shrink the target string. Instead:

  • If the new string is longer than the target, it is truncated.
  • If the new string is shorter, it is positioned (left, centered, or right) and the remainder is space-filled.

This makes LSET, CSET, and RSET useful for fixed-length fields, updating views without reallocation, and producing structured text layouts while avoiding unnecessary memory allocations.

Syntax

LSET target$ = source$ CSET target$ = source$ RSET target$ = source$

LSET / CSET / RSET Visual

Base
x$ = "12345"
1
2
3
4
5
Original 5-character field.
LSET
LSET x$ = "abc"
a
b
c
 
 
Left-justified, space-filled on the right: "abc··".
CSET
CSET x$ = "abc"
 
a
b
c
 
Centered with one space on each side: "·abc·".
RSET
RSET x$ = "abc"
 
 
a
b
c
Right-justified, space-filled on the left: "··abc".
Original field New text Space padding

Examples

x$ = "12345" // Left-justify and pad with spaces LSET x$ = "abc" // x$ is now: "abc " // Center within the field CSET x$ = "abc" // x$ is now: " abc " // Right-justify and pad with spaces RSET x$ = "abc" // x$ is now: " abc"

When the source string is longer than the destination, the extra characters are simply discarded:

x$ = "12345" LSET x$ = "TOO-LONG" // x$ becomes: "TOO-L"

Behavior Summary

Key Points:
  • LSET, CSET, and RSET never allocate new memory for the target string.
  • They overlay directly into the existing string buffer, preserving the original length.
  • Longer source strings are truncated to fit the target length.
  • Shorter source strings are space-padded and positioned left, centered, or right.
  • These operations work naturally and efficiently on VIEW variables.

Comparison to Normal Assignment

Normal assignment creates a new string and changes the length of the variable:

x$ = "12345" x$ = "abc" // x$ becomes "abc"

Special assignment reuses the existing buffer and keeps the original length:

x$ = "12345" LSET x$ = "abc" // x$ becomes "abc "
Multi-line literal strings with %text

A %text block turns several source lines into ONE string literal, exactly as typed — no quoting, no concatenation, no escape characters:

x$ = %text some text more text // even lines that look like comments %end text print x$

Everything between %text and %end text is kept verbatim: leading spaces, blank lines, and comment indicators such as // and ! are all part of the string. Lines are joined with CR+LF, with no line ending after the last line. The markers are case-regardless, %end text may be indented, and %text must be the last thing on its line.

The block is an ordinary string literal, so it works anywhere a quoted string does — assignments, print, function arguments. It is ideal for blocks of HTML, SQL, test data, or help text. A single literal can hold up to 16K of text; build larger strings by concatenating blocks.

Indenting the block: %text trim

Inside an if or a routine, a flush-left block breaks the visual shape of your code. Add the trim option and the indentation the lines SHARE is removed from the string, while the lines' indentation relative to each other is kept:

if show_help then help$ = %text trim Commands: add - add an item done - finish up %end text print help$ end if // prints: // Commands: // add - add an item // done - finish up

Note: Blank lines come out empty, and they are ignored when the margin is worked out. The margin comes from the least-indented non-blank line: trim removes only the leading whitespace that all of those lines share. So if you add one line at a shallower indent, that line sets a new and smaller margin, and every other line comes out with the extra spaces still attached. If a block's output suddenly grows leading spaces, look for the one line whose indentation does not match the others.

Strings in Arrays

Everything above works on a whole array of strings at once. Every string function maps over a string array element by element (ucase$(names$), len(names$), trim$(padded$)), + concatenates element by element, and the six comparisons (=, <>, <, <=, >, >=) give a mask of ones and zeros with the same rules as the scalar operators — same(names$, "bob") is the case-blind mask. The array functions take strings too: split() and join$() go between text and array, sort(), unique(), isin() and filter() pick and order them. One line each:

dim names$(*), first$(*), last$(*) fill first$ with "Ann", "Ben", "Cal" fill last$ with "Lee", "Moe", "Dee" names$ = first$ + " " + last$ ! + pairs the two arrays element by element print names$ ! Ann Lee Ben Moe Cal Dee print ucase$(names$) ! ANN LEE BEN MOE CAL DEE -- every string function maps print len(names$) ! 7 7 7 print names$ = "Ben Moe" ! 0 1 0 -- a comparison is a mask print filter(names$, same(last$, "dee")) ! Cal Dee -- case-blind, through same() print join$(sort(names$), " | ") ! Ann Lee | Ben Moe | Cal Dee print size(unique(last$)) ! 3 -- how many different surnames

The full story — shapes, broadcasting, the mask idioms, the set operations — is on the array page: Array Math Functions, Index Lists & Slices, Solve(), Sort(), and More, sections 5, 10, 20, 22 and 24.

(Show/Hide Sheerpower String Handling Takeaways)
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.