NAME
std/string - String utilities for ZuzuScript modules.
SYNOPSIS
from std/string import *;
let s := "abcdef";
say( substr( s, 1, 3 ) ); # bcd
say( index( s, "cd" ) ); # 2
say( replace( s, "cd", "XY" ) );
let label := "Zia.+";
let safe := /^${quotemeta(label)}:/i;
let rx := pattern_to_regexp( "cd", true );
say( search( s, rx ) ); # CD (if present)
say( trim(" hello ") ); # hello
say( title("hello_world") ); # Hello World
IMPLEMENTATION SUPPORT
This module is supported by all implementations of ZuzuScript.
DESCRIPTION
This module provides helper functions that are useful when writing higher-level parsers and serializers in ZuzuScript.
EXPORTS
Functions
substr(String text, Number start, Number length?)Parameters:
textis source text,startis a zero-based character offset, andlengthis optional. Returns:String. Returns a substring.index(String text, String needle, Number start?)Parameters:
textis source text,needleis the text to find, andstartis optional. Returns:Number. Returns the first matching index or-1.rindex(String text, String needle, Number start?)Parameters:
textis source text,needleis the text to find, andstartis optional. Returns:Number. Returns the last matching index or-1.contains(String text, String needle)Parameters:
textis source text andneedleis the text to find. Returns:Boolean. Returns true whenneedleoccurs intext.starts_with(String text, String prefix)Parameters:
textis source text andprefixis the expected prefix. Returns:Boolean. Returns true whentextstarts withprefix.ends_with(String text, String suffix)Parameters:
textis source text andsuffixis the expected suffix. Returns:Boolean. Returns true whentextends withsuffix.chr(Number codepoint)Parameters:
codepointis a Unicode codepoint. Returns:String. Returns the character forcodepoint.ord(String text, Number index?)Parameters:
textis source text andindexis an optional character offset. Returns:Number. Returns the codepoint atindex.replace(String text, String pattern, String replacement, String flags?)Parameters:
textis source text,patternis a string or regexp,replacementis replacement text, andflagsare optional regexp flags. Returns:String. Replaces matching text.search(String text, String pattern, String flags?)Parameters:
textis source text,patternis a string or regexp, andflagsare optional regexp flags. Returns: match value ornull. Finds the first match.matches(String text, String pattern, String flags?)Parameters:
textis source text,patternis a string or regexp, andflagsare optional regexp flags. Returns:Boolean. Returns true whentextmatchespattern.pattern_to_regexp(String pattern, Boolean case_insensitive?)Parameters:
patternis a pattern string andcase_insensitiverequests case-insensitive matching. Returns:Regexp. Compiles a regexp from a string pattern.quotemeta(String text)Parameters:
textis source text. Returns:String. Prefixes regexp metacharacters, slash delimiters, and quote characters with\so the result can be safely interpolated into a regexp as literal text, for example/^${quotemeta(label)}:/i.sprint(String format, ...args)Parameters:
formatis a format string andargsare replacement values. Returns:String. Formats text.split(String text, String separator_or_regexp, Number limit?)Parameters:
textis source text,separator_or_regexpis the split separator, andlimitis optional. Returns:Array. Splits text into parts.join(String separator, Array values)Parameters:
separatorjoins items andvaluesis an array. Returns:String. Joins values into one string.trim(String text)Parameters:
textis source text. Returns:String. Removes leading and trailing whitespace.pad(String text, Number width, String pad_char?, String side?)Parameters:
textis source text,widthis target width,pad_charis optional, andsideselects padding side. Returns:String. Pads text to the requested width.chomp(String text)Parameters:
textis source text. Returns:String. Removes one trailing line ending.title(String text)Parameters:
textis source text. Returns:String. Converts text to title case.snake(String text)Parameters:
textis source text. Returns:String. Converts text to snake_case.kebab(String text)Parameters:
textis source text. Returns:String. Converts text to kebab-case.camel(String text)Parameters:
textis source text. Returns:String. Converts text to camelCase.
PORTABILITY
Regex-heavy and Unicode-sensitive primitives may be runtime-supported, while higher-level API composition is validated through shared fixtures so module behaviour stays portable across runtimes.
COPYRIGHT AND LICENCE
std/string is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.