murka

package module
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2025 License: MIT Imports: 5 Imported by: 0

README

murka

Tests GitHub release MIT license codecov lint workflow help wanted

A high-performance Go package for string validation and sanitization. Murka provides lightning-fast character validation and replacement functions, significantly outperforming standard Go libraries.

Key Features

  • Blazing Fast: Up to 20x faster than the standard regexp package and 10x faster than the unicode package
  • Zero Allocations: Core validation functions allocate no memory
  • Customizable: Easily extend with your own character validation functions
  • Dual Functionality: Both validation and sanitization capabilities

Installation

bash go get github.com/kaatinga/murka

Then import the package into your code:

import "github.com/kaatinga/murka"

Usage Guide

String Validation

Murka's validation functions check if a string contains only specified characters. By default, Validate() allows only alphanumeric characters (a-z, A-Z, 0-9).

Basic Validation
    text := "Hello123"
	err := murka.Validate(text)
	if err != nil {
		// Handle invalid characters
	}
Custom Validation

Extend validation by providing custom character check functions:

// CheckUnderscore allows underscore character func CheckUnderscore(value rune) bool { return value == '_' // or 0x5f in hex }
// CheckHyphen allows hyphen character func CheckHyphen(value rune) bool { return value == '-' }
// Now validate with multiple character sets
text := "user_name-123"
err := murka.Validate(text, CheckUnderscore, CheckHyphen)
if err != nil {
	// Handle invalid characters
}
String Sanitization

Murka provides functions to clean strings by replacing unwanted characters.

Replace with Custom Rules
// Allow alphanumeric characters and underscores, replace others with '-'
sanitized := murka.Replace(text, '-', CheckUnderscore)
Efficient Replacement

For maximum performance when you only want to keep alphanumeric characters:

// Replace everything except a-z, A-Z, 0-9 with '-'
sanitized := murka.ReplaceNonAlphanumeric(text, '-')

Performance

Murka is optimized for high-performance validation and sanitization with minimal memory usage.

Benchmarks
cpu: Apple M1
BenchmarkValidate
BenchmarkValidate-8            	30313746	        39.66 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateByRegexp
BenchmarkValidateByRegexp-8    	 2548946	       479.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateByUnicode
BenchmarkValidateByUnicode-8   	 4465455	       268.1 ns/op	       0 B/op	       0 allocs/op
cpu: Apple M1
BenchmarkReplace
BenchmarkReplace-8                  	 6680472	       235.4 ns/op	      24 B/op	       2 allocs/op
BenchmarkReplaceNonAlphanumeric
BenchmarkReplaceNonAlphanumeric-8   	10234512	       117.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkStringsReplace
BenchmarkStringsReplace-8           	 9784203	       122.7 ns/op	      16 B/op	       2 allocs/op
BenchmarkStringsMap
BenchmarkStringsMap-8               	10400167	       114.4 ns/op	      32 B/op	       2 allocs/op
BenchmarkHighlight
BenchmarkHighlight-8                	 2561349	       518.8 ns/op	     296 B/op	       6 allocs/op
BenchmarkGoString
BenchmarkGoString-8                 	 1342380	       891.6 ns/op	     232 B/op	      14 allocs/op

Use Cases

  • Input validation for usernames, file names, or other string fields
  • Sanitizing user input before database storage
  • Normalizing strings for consistent format
  • High-performance text processing pipelines

Contributing

Contributions are welcome! Check out the help wanted issues to get started.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIncorrectCharacter = errors.New("input string contains an incorrect character")
	ErrTooLongString      = errors.New("input string is too long, the maximum is 65535 characters")
	ErrIncorrectLength    = errors.New("the sample exceeds the maximum")
)

Functions

func CheckUnderscore

func CheckUnderscore(value rune) bool

CheckUnderscore checks underscore character.

func Highlight added in v1.2.1

func Highlight(text, left, right, sample string) (string, error)

Highlight takes in some content and locations and then inserts left/right, strings which can be used for highlighting around matching terms. For example, if you pass in "test" with sample "te", you will have "<strong>te</strong>st" as return.

func Replace added in v1.1.0

func Replace[I ~string | ~[]rune](input I, replacementChar rune, additionalCheckers ...func(value rune) bool) string

Replace substitutes characters that don't match the a-zA-Z0-9 pattern or additional validators with the specified replacement character. Returns the modified string.

func ReplaceNonAlphanumeric added in v1.4.0

func ReplaceNonAlphanumeric[I ~string | ~[]rune](input I, replacementChar rune) string

ReplaceNonAlphanumeric replaces all non-alphanumeric characters (not a-zA-Z0-9) in the input string with the specified replacement character. Provides an efficient way to sanitize a string to contain only alphanumeric characters.

func Validate

func Validate[I ~string](input I, additionalCheckers ...func(value rune) bool) error

Validate checks if all characters in the input string match the a-zA-Z0-9 pattern. Additional character validation can be provided through custom checker functions. Returns ErrIncorrectCharacter if any character doesn't match the pattern.

func ValidateOnly added in v1.2.5

func ValidateOnly[I ~string](input I, validators ...func(value rune) bool) error

ValidateOnly checks if all characters in the input string match only the provided character validation functions. Returns ErrIncorrectCharacter if any character doesn't pass the provided validators.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL