Code Monkey home page Code Monkey logo

shorturl's Introduction

ShortURL

Bijective conversion between natural numbers (IDs) and short strings

Useful for URL shorteners and short IDs in your permalinks

Usage

  • ShortURL.encode() takes an ID and turns it into a short string
  • ShortURL.decode() takes a short string and turns it into an ID

Features

  • very short strings (much shorter than hex)
    • uses large alphabet of 51 characters
  • URL-safe (in contrast to Base64)
    • doesn't use any characters that need to be encoded for use in URLs
  • protection against offensive words (in contrast to Base64 and hex)
    • vowels are not used
  • unambiguous (in contrast to Base64 and hex)
    • look-alike characters are not used

Example output

  • 3141592 <=> vJST
  • 123456789 <=> pgK8p

Number range

All numbers from 1 to 2,147,483,647 inclusively can be safely encoded and decoded again in all implementations.

Contributing

All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.

License

This project is licensed under the terms of the MIT License.

shorturl's People

Contributors

11ujjawal avatar andraantariksa avatar bohdanszymanik avatar diatrevolo avatar guilhebl avatar hs0ucy avatar imgurpreetsk avatar it6c65 avatar marciuz avatar marzsv avatar msaad1200 avatar nforysinski avatar ocram avatar olefav avatar ray0x44 avatar skorobagatko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shorturl's Issues

in 64bit php, everything >= 0xE33BF7209CBFD54 is decoded (or encoded?) incorrectly

in 64bit php, the following code

<?php 
define ( "NUM", 1023372036854775122 );
for($i = NUM; $i < PHP_INT_MAX; ++ $i) {
    $encoded = ShortURL::encode ( $i );
    $decoded = ShortURL::decode ( $encoded );
    if ($i !== $decoded) {
        var_dump ( $i, $encoded, $decoded );
        die ( 'FAILED after ' . (($i - NUM) + 1) . ' attempt(s)' . PHP_EOL );
    }
}
die ( 'SUCCESS' );

should echo SUCCESS (but due to #8 , we know it would fail at 9223372036854775296, but it's much worse than that) , in 64bit php, it echoes

int(1023372036854775124)
string(11) "bDt2WgSwgz3"
int(1023372036854775073)
FAILED after 3 attempt(s)

somehow 1023372036854775124 turned into 1023372036854775073

Fixed lenght for output?

Hi!
I'm using the SQL Server class.
Is there any way to make the output encoded string to always be, let says, 12 characters?

Thanks

Concurrency issue

HI Ujjawal,

Thanks for these excellent examples. Its not an issue but a question about implementation.

How this implementation ensures the concurrency issue?. Is it an auto-incremented id from the database and then we should pass that to this code?

in 64bit php, everything between 0x7ffffffffffffE00 - PHP_INT_MAX is returned as float

the following code should echo FOUND after 1 attempt(s) ,
and it works as expected in 32bit php, but in 64bit php it echos FOUND after 513 attempt(s)

for($i = PHP_INT_MAX; $i > 0; -- $i) {
    if (! is_float ( ShortURL::decode ( ShortURL::encode ( $i ) ) )) {
        //var_dump ( $i );
        die ( 'FOUND after '.((PHP_INT_MAX-$i)+1).' attempt(s)' );
    }
}
die ( 'NOT FOUND' );

Supporting long/big-int numbers!

All numbers from 1 to 2,147,483,647 inclusively can be safely encoded and decoded again in all implementations.

I think this not supporting number bigger than 2,147,483,647 is a weakness, That must be Useful to support long/big-int numbers.

Swift 4 version

I have created a Swift 4 version, based on the Scala implementation, and would like to submit a pull request.

Is it safe to change the alphabet used?

We are hoping to use this for creating order IDs that can't be guessed, and are finding that using mixed case is not ideal for a couple reasons.

If we take out all the uppercase letters, would there be any issues that arise with regard to uniqueness?

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.