bisgardo / go-natural Goto Github PK
View Code? Open in Web Editor NEWNatural string comparison in Go
Home Page: https://godoc.org/github.com/halleknast/go-natural
License: The Unlicense
Natural string comparison in Go
Home Page: https://godoc.org/github.com/halleknast/go-natural
License: The Unlicense
There are two small optimization opportunities for Natural
:
There's no need to check if the byte on right hand side is a number if the one on the left hand side isn't.
Iterate over both numbers simultaneously: If one number is shorter, we only need to keep reading the other number until it's been determined that it's larger.
Add benchmarks to measure if it makes a difference before implementing this.
Computing the numbers contained in the compared strings actually isn't necessary:
Longer numbers will always be larger. Therefore, after skipping leading zeros and resolving the lenghts of the numbers, we can actually just compare the individual digits as regular bytes.
The rule that numbers are always greater than non-numbers should take care of the rest.
Strings are currently indexed on the byte level. As Natural
should be correct given general UTF-8 encoded strings, this means that characters may be accessed "within" their multi-byte encoding.
In a UTF-8 encoded string, all bytes with a leading 0 are single-byte (ASCII) characters. This means that all bytes of multi-byte characters start with 1. This means that there cannot be any interference with numbers and multi-byte characters.
The only potential "issue" is that multi-byte characters may not compare to each other the "standard" way: As the characters are compared byte for byte the order will depend on the precise encoding (of which there may be more than one for some characters) rather than comparing the code point.
This is deemed an acceptable shortcoming: As long as the string is normalized (a reasonable requirement), the result is consistent ("longer" characters are "greater").
As a final note, the library is not intended to support character sets that UTF-8 are not compatible with.
Based on the above, the task is the following two items:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.