FastChatFilter 1.0.0

dotnet add package FastChatFilter --version 1.0.0
                    
NuGet\Install-Package FastChatFilter -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastChatFilter" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastChatFilter" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FastChatFilter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FastChatFilter --version 1.0.0
                    
#r "nuget: FastChatFilter, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FastChatFilter@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FastChatFilter&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FastChatFilter&version=1.0.0
                    
Install as a Cake Tool

FastChatFilter

High-performance, zero-allocation profanity filter for .NET. Uses binary trie data structure with Span-based API for maximum performance in game servers, chat systems, and real-time applications.

Features

  • Zero-allocation hot path: Contains() method allocates zero bytes on the heap
  • Binary Trie: Precompiled dictionary for O(n) text scanning
  • Span-based API: Works with ReadOnlySpan<char> for maximum flexibility
  • Case-insensitive matching: Built-in lowercase normalization
  • Multi-target: Supports .NET 8.0 and .NET Standard 2.1

Installation

# Install the runtime library
dotnet add package FastChatFilter

# Install the compiler tool (optional)
dotnet tool install -g FastChatFilter.Compiler

Quick Start

1. Create a word list (CSV)

# badwords.csv
badword
offensive
spam
inappropriate

2. Compile to binary format

fcfc -i badwords.csv -o badwords.bin --normalize lower

3. Use in your application

using FastChatFilter;

// Load the compiled dictionary
using var filter = ProfanityFilter.Load("badwords.bin");

// Check for profanity (zero allocation)
bool hasProfanity = filter.Contains(message.AsSpan());

// Mask profanity
string censored = filter.Mask(message, '*');
// "this is badword" -> "this is *******"

API Reference

ProfanityFilter

// Load from file
var filter = ProfanityFilter.Load("words.bin");
var filter = ProfanityFilter.Load("words.bin", new LoadOptions { EnableNormalization = false });

// Load from stream (embedded resources)
var filter = ProfanityFilter.Load(stream);

// Load from byte array
var filter = ProfanityFilter.LoadFromBytes(data);

// Check for profanity
bool contains = filter.Contains("text to check");
bool contains = filter.Contains(text.AsSpan()); // Zero allocation

// Mask profanity
string masked = filter.Mask("bad text", '*');
string masked = filter.Mask("bad text", '#', new MaskOptions { FixedMask = "***" });

// Find all matches
Span<MatchResult> results = stackalloc MatchResult[64];
int count = filter.FindMatches(text.AsSpan(), results);

LoadOptions

new LoadOptions
{
    EnableNormalization = true  // Enable case-insensitive matching (default: true)
}

MaskOptions

new MaskOptions
{
    PreserveLength = true,  // Replace each character (default: true)
    FixedMask = "***"       // Use fixed replacement string (optional)
}

Compiler CLI

The fcfc (FastChatFilter Compiler) tool converts CSV word lists to optimized binary format.

# Basic usage
fcfc -i words.csv -o words.bin

# With options
fcfc --input words.csv --output words.bin --normalize lower

# Help
fcfc --help

CSV Format

  • One word per line
  • Comma-separated values supported
  • Lines starting with # are comments
  • Quotes around words are automatically removed
# Sample word list
badword
offensive, inappropriate, spam
"quoted word"

Performance

Metric Target
Contains allocation 0 bytes
Contains throughput (50 chars) >1M ops/sec
Load time (10K words) <50ms

Integration Examples

ASP.NET Core Middleware

public class ProfanityMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ProfanityFilter _filter;

    public ProfanityMiddleware(RequestDelegate next, ProfanityFilter filter)
    {
        _next = next;
        _filter = filter;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        // Check request body for profanity...
        await _next(context);
    }
}

// In Program.cs
builder.Services.AddSingleton(_ => ProfanityFilter.Load("words.bin"));

Game Server

public class ChatHandler
{
    private readonly ProfanityFilter _filter;

    public ChatMessage ProcessMessage(string text)
    {
        if (_filter.Contains(text.AsSpan()))
        {
            return new ChatMessage { Text = _filter.Mask(text), IsCensored = true };
        }
        return new ChatMessage { Text = text };
    }
}

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 236 12/15/2025