String Utilities
String Utilities
Generic string helpers: StartsWith, EndsWith, Contains, PadLeft, Split, and an AsMessage wrapper for UI text.
Original, compile-verified Verse by Bizanator (Biloxi Studios). Clean-room sample you can drop into a UEFN project and adapt.
# String Utilities — Biloxi Studios Inc.
#
# Generic, dependency-free string helpers: case-insensitive checks, prefix/suffix
# tests, left-padding, simple splitting, and a `message` wrapper for UI text.
# Written as a module of parametric-free `string`/`[]char` functions.
#
# Teaches: iterating chars, building strings, <decides> for boolean-style checks,
# the <localizes> effect for producing `message` values, and char comparison.
using { /Verse.org/Simulation }
string_utils<public> := module:
# Succeeds when Source starts with Prefix.
StartsWith<public>(Source:string, Prefix:string)<decides><transacts>:void =
Prefix.Length <= Source.Length
Head := Source.Slice[0, Prefix.Length]
Head = Prefix
# Succeeds when Source ends with Suffix.
EndsWith<public>(Source:string, Suffix:string)<decides><transacts>:void =
Suffix.Length <= Source.Length
Tail := Source.Slice[Source.Length - Suffix.Length, Source.Length]
Tail = Suffix
# Succeeds when Source contains Needle (substring search).
Contains<public>(Source:string, Needle:string)<decides><transacts>:void =
Needle.Length <= Source.Length
var Found:logic = false
# Last start index where Needle could still fit.
LastStart := Source.Length - Needle.Length
for (Start := 0..LastStart):
if (Window := Source.Slice[Start, Start + Needle.Length], Window = Needle):
set Found = true
Found?
# Left-pad a non-negative integer with PadChar up to MinWidth characters,
# e.g. PadLeft(7, 3, '0') = "007".
PadLeft<public>(Number:int, MinWidth:int, PadChar:char)<transacts>:string =
var Result:[]char = "{Number}"
if (Result.Length < MinWidth):
for (Count := 1..MinWidth - Result.Length):
set Result = array{PadChar} + Result
"{Result}"
# Split Source on a single-character Separator into a list of segments.
Split<public>(Source:string, Separator:char)<transacts>:[]string =
var Segments:[]string = array{}
var Current:string = ""
for (Char : Source):
if (Char = Separator):
set Segments += array{Current}
set Current = ""
else:
set Current += "{Char}"
set Segments += array{Current}
Segments
# Wrap a plain string as a localizable `message` for HUD / UI widgets.
AsMessage<public><localizes>(Text:string):message = "{Text}"