From 423c865ed6092b9bae291a3fc2ef31ce8cd3eff7 Mon Sep 17 00:00:00 2001 From: Zhongli Wu Date: Sun, 4 Oct 2015 12:49:51 -0700 Subject: [PATCH 1/3] 1. Support String parse for very large integer. 2. Support a return val for illegal input string in decode functions. --- Java/ShortURL.java | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Java/ShortURL.java b/Java/ShortURL.java index 66d340a..efe0f54 100644 --- a/Java/ShortURL.java +++ b/Java/ShortURL.java @@ -14,10 +14,14 @@ * * Source: https://github.com/delight-im/ShortURL (Apache License 2.0) */ + +import java.math.BigInteger; + public class ShortURL { - public static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"; - public static final int BASE = ALPHABET.length(); + private static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"; + private static final int BASE = ALPHABET.length(); + private static final String FORBIDSET = "01aeilouAEIOU"; public static String encode(int num) { StringBuilder str = new StringBuilder(); @@ -31,9 +35,42 @@ public static String encode(int num) { public static int decode(String str) { int num = 0; for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + if (FORBIDSET.indexOf(c) >= 0) { + num = -1; + break; + } num = num * BASE + ALPHABET.indexOf(str.charAt(i)); } return num; } + public static String encode(String num) throws NumberFormatException { + BigInteger bnum = new BigInteger(num); + BigInteger bBase = new BigInteger(Integer.toString(BASE)); + StringBuilder str = new StringBuilder(); + while (bnum.compareTo(BigInteger.ZERO) > 0) { + int n = bnum.mod(bBase).intValue(); + str.insert(0, ALPHABET.charAt(n)); + bnum = bnum.divide(bBase); + } + + return str.toString(); + } + + public static String decodeWithBigNumber(String str) { + BigInteger bNum = new BigInteger("0"); + BigInteger bBase = new BigInteger(Integer.toString(BASE)); + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + if (FORBIDSET.indexOf(c) >= 0) { + bNum = new BigInteger("-1"); + break; + } + BigInteger br = new BigInteger(Integer.toString(ALPHABET.indexOf(c))); + bNum = bNum.multiply(bBase).add(br); + } + + return bNum.toString(); + } } From 75a9cc94d6a71fc7ef4eefec40a3850418d10e6e Mon Sep 17 00:00:00 2001 From: Zhongli Wu Date: Tue, 6 Oct 2015 17:40:15 -0700 Subject: [PATCH 2/3] Add support for Big Integer. --- JavaScript/ShortURL.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/JavaScript/ShortURL.js b/JavaScript/ShortURL.js index 5e05282..1c2ead1 100644 --- a/JavaScript/ShortURL.js +++ b/JavaScript/ShortURL.js @@ -14,6 +14,9 @@ * * Source: https://github.com/delight-im/ShortURL (Apache License 2.0) */ + +var bigInt = require("big-integer"); + var ShortURL = new function() { var _alphabet = '23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_', @@ -28,6 +31,20 @@ var ShortURL = new function() { return str; }; + /** + * @param {num} input number represented by string + * @return {str} encoded short url + */ + this.encodeBigInteger = function (num) { + var str = ''; + var bNum = bigInt(num); + while (bNum.compare(0) == 1) { + str = _alphabet.charAt(bNum.mod(_base)) + str; + bNum = bNum.divide(_base); + } + return str; + } + this.decode = function(str) { var num = 0; for (var i = 0; i < str.length; i++) { @@ -36,4 +53,17 @@ var ShortURL = new function() { return num; }; + + /** + * @param {str} ShortURL represented by string + * @return {str} decoded number represented by string + */ + this.decodeBigInteger = function(str) { + var bNum = bigInt(0); + for (var i = 0; i < str.length; i++) { + bNum = bNum.multiply(_base).add(_alphabet.indexOf(str.charAt(i))); + } + return bNum; + } + }; From 5811c97b2c4846be4e1cf78c16926b56859f8f82 Mon Sep 17 00:00:00 2001 From: Zhongli Wu Date: Tue, 27 Oct 2015 18:29:56 -0700 Subject: [PATCH 3/3] Adjust some minor things for merge --- Java/ShortURL.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Java/ShortURL.java b/Java/ShortURL.java index efe0f54..f83cea6 100644 --- a/Java/ShortURL.java +++ b/Java/ShortURL.java @@ -19,9 +19,8 @@ public class ShortURL { - private static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"; - private static final int BASE = ALPHABET.length(); - private static final String FORBIDSET = "01aeilouAEIOU"; + public static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"; + public static final int BASE = ALPHABET.length(); public static String encode(int num) { StringBuilder str = new StringBuilder(); @@ -36,7 +35,7 @@ public static int decode(String str) { int num = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); - if (FORBIDSET.indexOf(c) >= 0) { + if (ALPHABET.indexOf(c) < 0) { num = -1; break; } @@ -58,12 +57,12 @@ public static String encode(String num) throws NumberFormatException { return str.toString(); } - public static String decodeWithBigNumber(String str) { + public static BigInteger decodeWithBigNumber(String str) { BigInteger bNum = new BigInteger("0"); BigInteger bBase = new BigInteger(Integer.toString(BASE)); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); - if (FORBIDSET.indexOf(c) >= 0) { + if (ALPHABET.indexOf(c) < 0) { bNum = new BigInteger("-1"); break; } @@ -71,6 +70,6 @@ public static String decodeWithBigNumber(String str) { bNum = bNum.multiply(bBase).add(br); } - return bNum.toString(); + return bNum; } }