Extended Text Library for Small Basic — Functions, Usage, and Tips
What it is
The Extended Text Library for Small Basic is an add-on collection of text-manipulation procedures that supplements Small Basic’s built-in string functions. It provides higher-level routines for common tasks (parsing, formatting, searching, and transformation) to make text-heavy programs simpler and more robust.
Core functions (typical)
- TrimAll(text): remove leading, trailing, and duplicate internal whitespace.
- SplitWords(text): split into a list/array of words using common delimiters (space, tab, punctuation).
- JoinWords(array, sep): join an array of words with a separator.
- ReplaceAll(text, old, new): replace all occurrences (case-sensitive or insensitive option).
- IndexOfN(text, sub, n): find the nth occurrence index of a substring.
- RegexMatch(text, pattern): perform pattern matching (if regex engine included) and return captures.
- WordCount(text): return number of words.
- SubstringSafe(text, start, length): safe substring handling with bounds checks.
- ToTitleCase(text): convert to title case with small-word rules.
- Pad(text, length, dir): pad left/right/center to a given width.
Typical usage patterns
- Preprocess user input: use TrimAll and NormalizeSpaces before parsing.
- Tokenization: SplitWords for token lists, then filter/transform tokens.
- Search & replace: ReplaceAll for bulk edits or IndexOfN + SubstringSafe for targeted changes.
- Formatting output: Pad and ToTitleCase to make console/UI text look consistent.
- Validation: RegexMatch or simple pattern checks to validate formats (emails, IDs).
Implementation notes for Small Basic
- Small Basic lacks native arrays/dictionaries like other languages; many libraries represent arrays as indexed properties of a GraphicsWindow or use lists via TextWindow. Expect helper routines to simulate arrays.
- Error handling is usually defensive: functions return empty string or -1 on failure, so check return values.
- Performance: string-heavy loops can be slow in Small Basic; prefer single-pass functions in the library that minimize repeated concatenation.
- Case sensitivity: explicitly provide case-insensitive options where needed since Small Basic string comparisons are straightforward but limited.
Tips and best practices
- Normalize early: trim and normalize spaces as soon as you receive input.
- Use library routines, not repeated code: centralize parsing logic to one helper to avoid bugs.
- Limit regex use if absent: if the library lacks regex, implement simple parsers for common patterns to keep performance predictable.
- Test edge cases: empty strings, very long strings, and missing delimiters.
- Document behavior: note how functions handle indexes (0-based vs 1-based) and empty inputs.
- Combine with file I/O helpers: if processing files, pair text routines with robust read/write helpers that handle encoding.
Example (pseudo-usage)
- Normalize input: cleaned = TrimAll(raw)
- Tokenize: words = SplitWords(cleaned)
- Capitalize each: for each w in words -> w = ToTitleCase(w)
- Reconstruct: output = JoinWords(words, “ “)
If you want, I can provide concrete Small Basic sample code for specific functions (e.g., TrimAll, SplitWords, ReplaceAll).
Leave a Reply