Skip to content

Repository files navigation

Texnomic.Types.Extensions

NuGetDownloadsLicense: MIT.NET 10

A focused collection of high-performance extension methods for common .NET 10 types, with first-class support for Ethereum-style 256-bit word encoding, hexadecimal parsing, Brotli compression, and JSON converters for BigInteger and byte[].

Targets net10.0 and uses C# 14 extension blocks for ergonomic call sites such as Bytes.ToUInt256() and "0xabc".HexToInt64().

Installation

dotnet add package Texnomic.Types.Extensions

Usage

Hex parsing

usingTexnomic.Types.Extensions;byte[]Bytes="0xdeadbeef".HexToBytes();intI32="0x1a".HexToInt32();// 26BigIntegerU256="0xff...ff".HexToUInt256();// 2^256 - 1

Ethereum ABI 256-bit word decoding

ReadOnlySpan<byte>Payload= ...;// ABI-encoded return dataBigInteger[]Words=Payload.ToUInt256Array();// one BigInteger per 32-byte wordBigIntegerFirst=Payload.TakeWord().ToUInt256();BigIntegerNext=Payload.SkipWord().TakeWord().ToUInt256();

Fixed-width hex (preserves leading zeros)

BigIntegerAddress= ...;stringHex=Address.AsPaddedHex(Width:20);// 40-char hex, "0x000...000abc"byte[]Hash= ...;stringBytes32=Hash.AsPaddedHex(Width:32);// 64-char hex

JSON converters for BigInteger and byte[]

usingTexnomic.Types.Extensions.Json;usingSystem.Text.Json;varOptions=newJsonSerializerOptions{Converters={newHexBigIntegerJsonConverter(),newHexByteArrayJsonConverter(),},};// Round-trips "0x..." strings as BigInteger / byte[] transparently.varBlock=JsonSerializer.Deserialize<EvmBlock>(Json,Options);

Both converters read and write 0x-prefixed lowercase hexadecimal — the canonical Ethereum JSON-RPC data / quantity format.

Zero-allocation writes via TryWriteBytes

Span<byte>Buffer=stackallocbyte[32];if(Value.TryWriteBytes(Buffer,outvarWritten,IsBigEndian:true)){// 'Written' bytes of the encoded payload are now in Buffer.}

Available on int, uint, long, ulong, BigInteger, and bool.

Brotli compression

byte[]Compressed=Payload.Compress();byte[]Decompressed=Compressed.Decompress();// Async equivalentsbyte[]Zipped=awaitPayload.CompressAsync();byte[]Unzipped=awaitZipped.DecompressAsync();

Boyer-Moore byte search

ReadOnlySpan<byte>Data= ...;ReadOnlySpan<byte>Pattern= ...;intOffset=Data.BoyerMooreIndexOf(Pattern);// -1 if no match (IndexOf-style)List<int>All=Data.BoyerMooreSearch(Pattern);// every occurrence

Sync-over-async, deadlock-safe

Func<Task<int>>Work=async()=>awaitComputeAsync();intResult=Work.RunSync();// runs on TaskScheduler.Default, no captured context

JSON over WebSocket

awaitSocket.SendJsonAsync(Request,JsonOptions,CancellationToken);Memory<byte>Buffer=newbyte[64*1024];varResponse=awaitSocket.ReceiveJsonAsync<MyResponse>(Buffer,JsonOptions,CancellationToken);

Pooled JSON string reads

publicstaticMyDtoRead(refUtf8JsonReaderReader){varSymbol=Reader.GetStringOptimized();// interned via StringPool.SharedvarValue=Reader.GetHexUInt256();// "0x..." → BigIntegerreturnnewMyDto(Symbol,Value);}

Module overview

Source fileTarget typeHighlights
AsyncExtensions.csFunc<Task>, Func<Task<T>>, Task<T>RunSync()
BigIntegerExtensions.csBigIntegerToBigDecimal, ToBytes, TryWriteBytes, AsHex, AsPaddedHex
BoolExtensions.csboolToBytes, TryWriteBytes, AsHex
ByteArrayExtensions.csbyte[]Span/memory projections, 256-bit decoding, hex / AsPaddedHex / Base64, Brotli compression (sync + async)
ByteReadOnlySpanExtensions.csReadOnlySpan<byte>+ TakeWord / SkipWord + BoyerMooreSearch / BoyerMooreIndexOf
ByteSpanExtensions.csSpan<byte>Writable-span variant
ClientWebSocketExtensions.csClientWebSocketSendJsonAsync / ReceiveJsonAsync
DecimalExtensions.csdecimalAsHex
IntegerExtensions.csint, uintToBytes, TryWriteBytes, AsHex
LongExtensions.cslong, ulongToBytes, TryWriteBytes, AsHex
ReadOnlySpanExtensions.csReadOnlySpan<T>Take / Skip + bounds-safe SafeTake / SafeSkip
SpanExtensions.csSpan<T>Take / Skip
StringExtensions.csstringRemoveHexPrefix, hex parsing to bytes / BigDecimal / 32/64/256-bit ints
StringReadOnlySpanExtensions.csReadOnlySpan<char>Zero-allocation hex parsing
Utf8JsonReaderExtensions.csUtf8JsonReaderHex-token decoders, GetStringOptimized (StringPool.Shared)
Json/HexBigIntegerJsonConverter.csBigIntegerJsonConverter<BigInteger>0x-prefixed lowercase hex
Json/HexByteArrayJsonConverter.csbyte[]JsonConverter<byte[]>0x-prefixed lowercase hex

Dependencies

PackagePurpose
CommunityToolkit.HighPerformanceStringPool for allocation-free repeated string reads.
ExtendedNumerics.BigDecimalArbitrary-precision decimal arithmetic used by ToBigDecimal / HexToBigDecimal.
ThrowFluent argument validation.

Building from source

git clone https://github.com/Texnomic/Types.Extensions
cd Types.Extensions
dotnet build
dotnet test
dotnet pack -c Release

Run benchmarks:

dotnet run --project Benchmarks/Texnomic.Types.Extensions.Benchmarks -c Release -- --filter '*'

License

MIT © Texnomic.

About

High-performance extension methods for common .NET 10 types. BigInteger, byte arrays/spans, hex parsing, Brotli compression (pooled), Utf8JsonReader helpers, ClientWebSocket JSON, and System.Text.Json converters.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages