Commits

Victor Kotseruba committed 1b4716f

join encode/decode functions and wrappers

Comments (0)

Files changed (1)

 const char const* alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 const int base = 62;
 
-char* encode(long val) {
-    static char buf[8];
-    buf[7] = 0;
-    int i = 6;
-    for(; val && i ; --i, val /= base)
-        buf[i] = alphabet[val % base];
-    return &buf[i+1];
-}
-
-long decode(const char* val) {
-    int len = strlen(val), pos = 0;
-    long result = 0;
-    for(; pos < len; pos ++)
-        result += (strchr(alphabet, val[len - 1 - pos]) - alphabet) * powl(base, pos);
-    return result;
-}
-
 static PyObject* cbase62_encode(PyObject *self, PyObject *args) {
     long num;
     if (!PyArg_ParseTuple(args, "l", &num))
         return NULL;
-    return Py_BuildValue("s", encode(num));
+    int i = 6;
+    static char buf[8];
+    buf[7] = 0;
+    for(; num && i ; --i, num /= base)
+        buf[i] = alphabet[num % base];
+    return Py_BuildValue("s", &buf[i+1]);
 }
 
 static PyObject* cbase62_decode(PyObject *self, PyObject *args) {
     const char* encoded;
     if (!PyArg_ParseTuple(args, "s", &encoded))
         return NULL;
-    return Py_BuildValue("l", decode(encoded));
+    int len = strlen(encoded), pos = 0;
+    long result = 0;
+    for(; pos < len; pos ++)
+        result += (strchr(alphabet, encoded[len - 1 - pos]) - alphabet) * powl(base, pos);
+    return Py_BuildValue("l", result);
 }
 
 static PyMethodDef cbase62Methods[] = {