Code Monkey home page Code Monkey logo

murka's Introduction

Tests GitHub release MIT license codecov lint workflow help wanted

murka

A high performance string validator and string sanitizer. The package is intended for checking incorrect characters' existence in a string. Validate() is 20 times faster than regexp package and 10 times faster than unicode package.

1. Installation

Use go get.

go get github.com/kaatinga/murka

Then import the validator package into your own code.

import "github.com/kaatinga/murka"

2. How to use

Validation

By default, Validate() checks only a-z, A-z and 0-9 characters. Additional checks are done by functions that comply with func (value rune) bool.

Prepare an additional function like this:

// CheckUnderscore checks underscore character.
func CheckUnderscore(value rune) bool {
    return value == 0x5f
}

After that you can call Validate().

err := murka.Validate(text, CheckUnderscore)
if err != nil {
    return err
}

Benchmarks:

BenchmarkValidate
BenchmarkValidate-8    	        45240508	       26.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateByRegexp
BenchmarkValidateByRegexp-8    	 2072374	       578.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateByUnicode
BenchmarkValidateByUnicode-8   	 4360131	       272.8 ns/op	       0 B/op	       0 allocs/op

String sanitization

Replace() checks only a-z, A-z and 0-9 characters by default. Like with Validate(), additional checks are done by functions that comply with func (value rune) bool. Replace() does not return any error, but substitute incorrect characters with the input character of rune type. There is no sense to use this function without an additional checker.

ReplaceNotaZ09() replaces all characters except a-z, A-z and 0-9. The most efficient way to sanitize a string using a-zA-Z0-9 pattern.

Benchmarks:

BenchmarkReplace
BenchmarkReplace-8          	 5310733	       229.0 ns/op	      24 B/op	       2 allocs/op
BenchmarkReplaceNotAz09
BenchmarkReplaceNotAz09-8   	 9882952	       116.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkStringsReplace
BenchmarkStringsReplace-8   	 9212074	       133.3 ns/op	      16 B/op	       2 allocs/op
BenchmarkStringsMap
BenchmarkStringsMap-8       	 8836887	       130.8 ns/op	      32 B/op	       2 allocs/op

murka's People

Contributors

kaatinga avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.