Str

Source: lib/Str.cs

This page is generated from the source by scripts/gen-reference.py.

String support for zero mode.

The core library's string (zerocore/System/String.cs) has Length, an indexer, ==, !=, + (string operands), Equals, IsNullOrEmpty and string switch; $"..." with non-string holes, Substring and parsing are missing. This file fills the gap without a garbage collector:

  • System.MemoryExtensions: read-only operations on ReadOnlySpan (SequenceEqual, StartsWith, IndexOf, Trim, Split, ...), with the same names as .NET. Defining AsSpan(string) also switches on C# 14's implicit string -> ReadOnlySpan conversion, so strings can be passed to any span parameter. None of these allocate.

  • Str: parsing, ordinal comparison, and new strings built in an Arena (Str.Format(arena, $"..."), Str.Concat). Arena strings are ordinary strings, freed in bulk when their arena is reset.

  • \("..." works through custom interpolated string handlers: Out.Line(\)"...") writes straight to stdout, StrBuilder.Append($"...") into the arena.

a + b mallocs a string that is never freed; in a loop, prefer Str.Concat or Str.Format, which use the arena. Instead of s.Substring(i, n) use s.AsSpan(i, n). A null string acts as an empty one everywhere here.

Members

IsWhiteSpace

public static bool IsWhiteSpace(char c)

Same set as .NET's char.IsWhiteSpace.

Compare

public static int Compare(ReadOnlySpan<char> a, ReadOnlySpan<char> b)

Ordinal comparison: negative, zero or positive, like string.CompareOrdinal.

TryParse

public static bool TryParse(ReadOnlySpan<char> s, out long value)

An optional sign followed by decimal digits.

public static bool TryParse(ReadOnlySpan<char> s, out int value)
public static bool TryParse(ReadOnlySpan<char> s, out ulong value)

An optional '+' followed by decimal digits.

TryParseHex

public static bool TryParseHex(ReadOnlySpan<char> s, out ulong value)

Hex digits (either case) with an optional 0x prefix, up to 64 bits.

TryParse

public static bool TryParse(ReadOnlySpan<char> s, out double value)

[sign] digits [. digits] [e|E [sign] digits], or NaN / Infinity / -Infinity (what Fmt prints; case-insensitive, "inf" too). ".5" and "5." are accepted, like .NET. Too large gives +-Infinity and too small gives 0, also like .NET. The result is correctly rounded (denormals included), except for rare inputs within about 2^-100 of a halfway point between two doubles. Digits past the 19th significant one only affect such halfway cases.

Format

public static string Format(
    Arena arena,
    ref StrBuilder.InterpolatedHandler handler)

$"..." into a new string in arena. Returns null if the arena isn't created or is out of memory.

Safety: The string is freed with the rest of the arena (Restore, Reset, Destroy).

ToArenaString

public static string ToArenaString(Arena arena, ReadOnlySpan<char> text)

Copies text into a new string in arena, or returns null.

Concat

public static string Concat(
    Arena arena,
    ReadOnlySpan<char> a,
    ReadOnlySpan<char> b)
public static string Concat(
    Arena arena,
    ReadOnlySpan<char> a,
    ReadOnlySpan<char> b,
    ReadOnlySpan<char> c)

The replacement for a + b + c. Returns null if the arena is out of memory.

Span and string operations

Extension methods on ReadOnlySpan<char> and string, under the names .NET uses (System.MemoryExtensions).

AsSpan

public static ReadOnlySpan<char> AsSpan(this string text)

The string's characters, without copying. Null gives an empty span.

public static ReadOnlySpan<char> AsSpan(this string text, int start)
public static ReadOnlySpan<char> AsSpan(this string text, int start, int length)

SequenceEqual

public static bool SequenceEqual(
    this ReadOnlySpan<char> span,
    ReadOnlySpan<char> other)

Ordinal equality: the replacement for a == b.

StartsWith

public static bool StartsWith(
    this ReadOnlySpan<char> span,
    ReadOnlySpan<char> value)

EndsWith

public static bool EndsWith(
    this ReadOnlySpan<char> span,
    ReadOnlySpan<char> value)

IndexOf

public static int IndexOf(this ReadOnlySpan<char> span, char value)
public static int IndexOf(
    this ReadOnlySpan<char> span,
    ReadOnlySpan<char> value)

First index of value, or -1. An empty value is found at 0.

LastIndexOf

public static int LastIndexOf(this ReadOnlySpan<char> span, char value)

Contains

public static bool Contains(this ReadOnlySpan<char> span, char value)
public static bool Contains(
    this ReadOnlySpan<char> span,
    ReadOnlySpan<char> value)

Trim

public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span)

TrimStart

public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span)

TrimEnd

public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span)

Split

public static SpanSplitEnumerator Split(
    this ReadOnlySpan<char> span,
    char separator)

Splits on separator without allocating: use it in foreach. Like string.Split, "a,,b" gives "a", "", "b" and "" gives one empty item.

SequenceEqual

public static bool SequenceEqual(this string text, ReadOnlySpan<char> other)

StartsWith

public static bool StartsWith(this string text, ReadOnlySpan<char> value)

EndsWith

public static bool EndsWith(this string text, ReadOnlySpan<char> value)

IndexOf

public static int IndexOf(this string text, char value)
public static int IndexOf(this string text, ReadOnlySpan<char> value)

LastIndexOf

public static int LastIndexOf(this string text, char value)

Contains

public static bool Contains(this string text, char value)
public static bool Contains(this string text, ReadOnlySpan<char> value)

Trim

public static ReadOnlySpan<char> Trim(this string text)

TrimStart

public static ReadOnlySpan<char> TrimStart(this string text)

TrimEnd

public static ReadOnlySpan<char> TrimEnd(this string text)

Split

public static SpanSplitEnumerator Split(this string text, char separator)