-
-
Save revsmoke/2525c1d9b1154c01bc49ca0b96cef0bc to your computer and use it in GitHub Desktop.
coldfusion component; allows you to encode an integer as a base62 string using [a-zA-Z0-9] - useful for url shortening. should handle from 0 to 9,223,372,036,854,775,807
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
variables.ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
variables.BASE = bigInt(len(ALPHABET).toString()); | |
private any function bigInt (required string input ){ | |
return createObject("java", "java.math.BigInteger").init(input); | |
} | |
private any function stringBuilder (string input = "") { | |
return createObject("java", "java.lang.StringBuilder").init(input); | |
} | |
public string function fromBase10(required numeric i) { | |
i = createObject("java", "java.lang.Long").valueOf(i); | |
var sb = stringBuilder(); | |
if (i == 0) { | |
fromBase10Builder(i, sb); | |
} else { | |
while (i > 0) { | |
i = fromBase10Builder(i, sb); | |
} | |
} | |
return sb.reverse().toString(); | |
} | |
private numeric function fromBase10Builder (required numeric i, required any sb) { | |
var bi = bigInt(i.longValue().toString()); | |
var rem = bi.mod(BASE).intValue(); | |
sb.append(ALPHABET.charAt(rem)); | |
return bi.divide(BASE); | |
} | |
public numeric function toBase10 (required string str) { | |
str = str.replaceAll("[^a-zA-Z0-9]", ""); | |
return toBase10Helper(stringBuilder(str).reverse().toString().toCharArray()).longValue(); | |
} | |
private numeric function toBase10Helper (required array chars) { | |
var n = bigInt("0"); | |
for (var i = arrayLen(chars)-1; i >= 0; i--) { | |
n = n.add(toBase10Power(ALPHABET.indexOf(chars[i+1]), i)); | |
} | |
return n; | |
} | |
private numeric function toBase10Power (required numeric n, required numeric pow) { | |
var bi = bigInt(n.longValue().toString()); | |
return bi.multiply(BASE.pow(pow.intValue())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfparam name="url.base10input" default="0" /> | |
<cfparam name="url.base62input" default="" /> | |
<cfset url.base10input = trim(url.base10input) /> | |
<cfset url.base62input = trim(url.base62input) /> | |
<cfset base62 = new base62() /> | |
<cfoutput> | |
<form action="index.cfm" method="get"> | |
<table> | |
<tr> | |
<th>Base 62 string:</th> | |
<td> | |
<input type="text" value="#url.base62input#" name="base62input" /> | |
</td> | |
<td> | |
<cfif len(trim(url.base62input))> | |
<cfset a = base62.toBase10(url.base62input) /> | |
<cfset b = base62.fromBase10(a) /> | |
#a# / #b# | |
</cfif> | |
</td> | |
</tr> | |
<tr> | |
<th>Base 10 integer:</th> | |
<td> | |
<input type="text" value="#url.base10input#" name="base10input" /> | |
</td> | |
<td> | |
<cfif isNumeric(url.base10input)> | |
<cfset a = base62.fromBase10(url.base10input) /> | |
<cfset b = base62.toBase10(a) /> | |
#a# / #b# | |
</cfif> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<input type="Submit" /> | |
</td> | |
</tr> | |
</table> | |
</form> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment