brutal/samples/Hello/HelloStrings.cs
Rian Drake b0c9fd9cdc samples: add Hello/Demo samples for most BRUTAL packages
Add samples/, a collection of single-file .NET 10 apps run with
`dotnet run <file>.cs`. Hello/ has one minimal sample per package
that has a public API, many of them headless; Demo/ has fuller
programs that combine packages into a more interesting application.

samples/README.md indexes every sample and covers the layout, error
handling and per-OS requirements.
2026-10-18 18:06:00 +13:00

25 lines
806 B
C#

/*
* HelloStrings
*
* Works with UTF-8 text using String8 and StringBuilder<byte>, which are designed to avoid
* allocations.
*/
#:package Ahwoo.Brutal.Strings@2026.10.1
using Brutal.Strings;
var greeting = new String8("Hello, café!");
Console.WriteLine($"Text: {greeting.ToString16()}");
Console.WriteLine($"UTF-8 bytes: {greeting.Length}, code points: {Unicode.GetRuneCount(greeting.AsSpan())}");
Console.WriteLine($"Starts with \"Hello\": {greeting.StartsWith("Hello"u8)}");
// A StringBuilder that writes UTF-8 bytes directly.
var builder = new StringBuilder<byte>();
builder.Append($"Pi is roughly {Math.PI:F3}");
builder.AppendLine();
builder.Append("Built as UTF-8"u8);
Console.WriteLine(builder.ToString());
Console.WriteLine($"Builder holds {builder.WrittenSpan.Length} UTF-8 bytes");