Giter Site home page Giter Site logo

strconv's Introduction

Tests GitHub release MIT license codecov lint workflow help wanted

The faststrconv package

The package contain several useful functions intended for conversion between string, []byte and different integer types.

For the sake of code shortness, generics are used, it means the package requires go1.18+.

GetUint32(), GetUint16(), GetByte()

The functions are faster alternative to strconv.Atoi().

goos: darwin
goarch: arm64
pkg: github.com/kaatinga/strconv
BenchmarkGetByte
BenchmarkGetByte-8                             	523370584	         2.042 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetUint16
BenchmarkGetUint16-8                           	582942614	         2.046 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetUint32
BenchmarkGetUint32-8                           	587540109	         2.045 ns/op	       0 B/op	       0 allocs/op
BenchmarkStrvconv_Atoi
BenchmarkStrvconv_Atoi-8                       	174534008	         7.387 ns/op	       0 B/op	       0 allocs/op
PASS

Uint162String, Byte2String, etc

The functions are faster alternative to strconv.Itoa().

goos: darwin
goarch: arm64
pkg: github.com/kaatinga/strconv
BenchmarkByte2String
BenchmarkByte2String-8                    	100000000	        10.54 ns/op	       0 B/op	       0 allocs/op
BenchmarkUint162String
BenchmarkUint162String-8                  	71960958	        16.57 ns/op	       0 B/op	       0 allocs/op
BenchmarkUint322String
BenchmarkUint322String-8                  	71680485	        16.82 ns/op	       0 B/op	       0 allocs/op
BenchmarkUint642String
BenchmarkUint642String-8                  	65918067	        17.98 ns/op	       0 B/op	       0 allocs/op
BenchmarkUint32And642StringWithUint64
BenchmarkUint32And642StringWithUint64-8   	58261146	        19.82 ns/op	       0 B/op	       0 allocs/op
BenchmarkUint32And642StringWithUint32
BenchmarkUint32And642StringWithUint32-8   	65289982	        18.19 ns/op	       0 B/op	       0 allocs/op
BenchmarkItoa
BenchmarkItoa-8                           	34061032	        35.20 ns/op	       8 B/op	       2 allocs/op
PASS

Warning. For the sake of maximum processing speed, all the converters have limited support of the leading zeros. The length of the string cannot exceed the maximum length of the string of every certain type. For example, 012 is supported for the byte type, whereas 0255 is not as it consists of 4 characters what is impossible for byte type. In case you process a date number, like month that is formatted with zero, like 09, you can use the fast converters of the strconv package directly, but if you still need to process formatted numbers like 0000000000000255 as byte, use DeleteLeadingZeros helper to remove leading zeros in the processed string first:

numberINeed, err = strconv.String2Byte(DeleteLeadingZeros("0000000000000255"))
if err != nil {
	...
}

strconv's People

Stargazers

 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.