magus v0.4.3 is out. See what's new
¶ View generated markdown
7 min read

strings

String helpers Buzz's builtins lack: case conversion, comparison, affix trimming, padding, and splitting into lines or fields.

Naming convention: import the module under its bare name (import "strings"), reach members with a backslash, and call methods in camelCase: strings\someMethod.

Methods

camelCase

Convert s to camelCase.

Signature: strings\camelCase(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

Example:

import "std";
import "strings";

std\print(strings\camelCase("hello world"));
// -> "helloWorld"

snakeCase

Convert s to snake_case.

Signature: strings\snakeCase(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

Example:

import "std";
import "strings";

std\print(strings\snakeCase("HelloWorld"));
// -> "hello_world"

kebabCase

Convert s to kebab-case.

Signature: strings\kebabCase(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

Example:

import "std";
import "strings";

std\print(strings\kebabCase("MyComponentName"));
// -> "my-component-name"

pascalCase

Convert s to PascalCase.

Signature: strings\pascalCase(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

Example:

import "std";
import "strings";

std\print(strings\pascalCase("user_profile"));
// -> "UserProfile"

capitalize

Uppercase the first rune of s and lowercase the rest.

Signature: strings\capitalize(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

Example:

import "std";
import "strings";

std\print(strings\capitalize("hELLO"));
// -> "Hello"

words

Split s into its constituent words (splitting on case changes, digits, and separators).

Signature: strings\words(s) -> []string - source

Parameter Type Optional Description
s string

Returns: []string

Example:

import "std";
import "strings";

final parts = strings\words("parseHTTPResponse2");
foreach (w in parts) { std\print(w); }
// -> parse
// -> HTTP
// -> Response
// -> 2

ellipsis

Trim s to at most length runes, appending "..." when truncated.

Signature: strings\ellipsis(s, length) -> string - source

Parameter Type Optional Description
s string
length int

Returns: string

Example:

import "std";
import "strings";

std\print(strings\ellipsis("the quick brown fox", 12));
// -> "the quick..."

upperFirst

Uppercase the first rune of s, leaving the rest untouched. Unlike capitalize, which lowercases the remainder, this preserves interior casing - the form a label or breadcrumb built from an existing string needs.

Signature: strings\upperFirst(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

compare

Compare a and b lexicographically by byte, returning -1, 0, or 1. Buzz has no < operator on str, so this is what a list.sort comparator over strings is built from.

Signature: strings\compare(a, b) -> int - source

Parameter Type Optional Description
a string
b string

Returns: int

Example:

import "std";
import "strings";

// Buzz has no `<` on str, so sorting strings means handing list.sort a
// comparator built from compare.
final names = mut ["charlie", "alice", "bob"];
names.sort(fun (a: str, b: str) > bool => strings\compare(a, b) < 0);
std\print(names.join(", "));
// -> "alice, bob, charlie"

contains

Report whether s contains substr.

Signature: strings\contains(s, substr) -> bool - source

Parameter Type Optional Description
s string
substr string

Returns: bool

trimPrefix

Remove prefix from the start of s if present, otherwise return s unchanged. Buzz's str.trim only strips whitespace, so there is no built-in way to drop a known affix.

Signature: strings\trimPrefix(s, prefix) -> string - source

Parameter Type Optional Description
s string
prefix string

Returns: string

trimSuffix

Remove suffix from the end of s if present, otherwise return s unchanged.

Signature: strings\trimSuffix(s, suffix) -> string - source

Parameter Type Optional Description
s string
suffix string

Returns: string

padLeft

Left-pad s with pad until it is length runes wide; s is returned unchanged when already that wide or wider. pad defaults to a space. Zero-padding a number so it sorts lexically is the usual reason.

Signature: strings\padLeft(s, length, [pad]) -> string - source

Parameter Type Optional Description
s string
length int
pad string yes

Returns: string

padRight

Right-pad s with pad until it is length runes wide; s is returned unchanged when already that wide or wider. pad defaults to a space. Aligning a column of output is the usual reason.

Signature: strings\padRight(s, length, [pad]) -> string - source

Parameter Type Optional Description
s string
length int
pad string yes

Returns: string

lines

Split s into lines on \n, tolerating \r\n endings and dropping the trailing empty element a final newline would produce. Reading a command's stdout line by line is what this is for; fs.read_lines is the same shape over a file.

Signature: strings\lines(s) -> []string - source

Parameter Type Optional Description
s string

Returns: []string

fields

Split s around runs of whitespace, discarding empties. Picking a column out of a tool's version banner is what this is for; str.split(" ") cannot, because it yields an empty element per extra space.

Signature: strings\fields(s) -> []string - source

Parameter Type Optional Description
s string

Returns: []string

splitN

Split s on sep into at most n pieces, leaving any remaining separators in the final piece; n of -1 means no limit. Parsing a KEY=VALUE line whose value itself contains the separator needs this rather than str.split.

Signature: strings\splitN(s, sep, n) -> []string - source

Parameter Type Optional Description
s string
sep string
n int

Returns: []string

collapseWs

Fold every run of whitespace in s into a single space and trim the ends, so a multi-line value reads as one clean line.

Signature: strings\collapseWs(s) -> string - source

Parameter Type Optional Description
s string

Returns: string

generatedreference/buzz/stringsmodulestdlibmagusfile
Last updated (29d8b3de)
Earlier changes on this page (6)

Full history ↗ · Blame source ↗

Glossary

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. Magusfile in full.

Op

A single tool-native command a target composes (long form: operation); the middle of the work hierarchy (Spell to Op to Target). Op in full.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. Module in full.

Buzz

The language magusfiles are written in (the .buzz engine). Buzz in full.

Tool

An action a model can call through its host: a name, a description, a typed input and a result. magus serves its tools over MCP, each a read of the workspace model or a run of the CLI it already has. Tool in full.

Run

One target executing under one magus invocation, such as magus run test web or magus affected ci. A run keeps its captured output behind an output reference. Every magus run is a run whether or not any job asked for it; see Job for how the two relate. Run in full.

Conventions

This page uses none of the site's convention markers. The full set is on the conventions page.