From a8280bb31a1e63505eafa09565d4275557aebddc Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Thu, 06 Apr 2017 07:48:47 +0000
Subject: [PATCH] ShortURL aufgenommen

---
 src/java/de/uhilger/filecms/web/ShortURL.java |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/src/java/de/uhilger/filecms/web/ShortURL.java b/src/java/de/uhilger/filecms/web/ShortURL.java
new file mode 100644
index 0000000..7ced1e6
--- /dev/null
+++ b/src/java/de/uhilger/filecms/web/ShortURL.java
@@ -0,0 +1,46 @@
+
+package de.uhilger.filecms.web;
+
+/*
+ * ShortURL (https://github.com/delight-im/ShortURL)
+ * Copyright (c) delight.im (https://www.delight.im/)
+ * Licensed under the MIT License (https://opensource.org/licenses/MIT)
+ */
+
+/**
+ * ShortURL: Bijective conversion between natural numbers (IDs) and short strings
+ *
+ * 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:
+ * + large alphabet (51 chars) and thus very short resulting strings
+ * + proof against offensive words (removed 'a', 'e', 'i', 'o' and 'u')
+ * + unambiguous (removed 'I', 'l', '1', 'O' and '0')
+ *
+ * Example output:
+ * 123456789 <=> pgK8p
+ */
+public class ShortURL {
+
+	public static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_";
+	public static final int BASE = ALPHABET.length();
+
+	public static String encode(int num) {
+		StringBuilder str = new StringBuilder();
+		while (num > 0) {
+			str.insert(0, ALPHABET.charAt(num % BASE));
+			num = num / BASE;
+		}
+		return str.toString();
+	}
+
+	public static int decode(String str) {
+		int num = 0;
+		for (int i = 0; i < str.length(); i++) {
+			num = num * BASE + ALPHABET.indexOf(str.charAt(i));
+		}
+		return num;
+	}
+
+}
\ No newline at end of file

--
Gitblit v1.9.3