Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General Improvements: C extension, Py2 Py3 compatibility, Performance #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python/include *.txt
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ USAGE

### C ###

#include <base92/base92.h>
To build as a shared library in $PREFIX.

mkdir -p $PREFIX/include/ $PREFIX/lib/
gcc -shared -Wl,-soname,libbase92 -o $PREFIX/lib/libbase92.so -fPIC -Ic/src c/src/base92.c
cp -a c/src/base92.h $PREFIX/include/

Use:

#include <base92.h>
...
strcmp(base92encode("hello world", 11), "Fc_$aOTdKnsM*k") == 0;
base92decode("Fc_$aOTdKnsM*k", &length);
Expand All @@ -29,6 +37,12 @@ USAGE

### Python ###

To build:

cd python
python setup.py build_ext --inplace
python setup.py install

Fire up your favorite python:

>>> import base92
Expand Down
12 changes: 4 additions & 8 deletions c/src/base92.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ unsigned char* base92encode(unsigned char* str, int len) {
int tmp;
unsigned char c;
unsigned char *res;

if (len == 0) {
return "~";
return (unsigned char*)"~";
}
// precalculate how much space we need to malloc
size = (len * 8) % 13;
Expand Down Expand Up @@ -159,17 +159,13 @@ unsigned char* base92decode(unsigned char* str, int* len) {
unsigned char* res;
unsigned long workspace;
unsigned short wssize;
size = strlen(str);
size = strlen((char*)str);
// handle small cases first
if (strcmp(str, "~") == 0 || size == 0) {
if (strcmp((char*)str, "~") == 0 || size == 0) {
res = (unsigned char*)malloc(sizeof(char) * 1);
res[0] = 0;
return res;
}
// this case does not fit the specs
if (size < 2) {
res = NULL;
}
// calculate size
*len = ((size/2 * 13) + (size%2 * 6)) / 8;
res = (unsigned char *)malloc(sizeof(char) * (*len));
Expand Down
1 change: 0 additions & 1 deletion python/MANIFEST.txt

This file was deleted.

36 changes: 32 additions & 4 deletions python/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ A little library for encoding byte-strings into strings easily
typeable on a standard US 101-key keyboard, with strictly better
information density than base64 or base85 encodings.

It is python3 compatible and has a C backend.

--------
BUILDING
--------

Compile the C extension and install.

python setup.py build_ext --inplace # creates base92/base92_extension.so
python setup.py install

-----
USAGE
-----
Expand All @@ -17,6 +28,26 @@ Fire up your favorite python::
'hello world'
>>> base92.encode('\x61\xf2\x05\x99\x42')
'DJ8gER!'

>>> import base92.test
>>> base92.test.run()
testing and cross validating encoders and decoders from modules [<module 'base92.cbase92' from 'base92/cbase92.pyc'>, <module 'base92.base92' from 'base92/base92.py'>]
selected regression tests passed
generating 10000 random byte strings
10000 randomized X == decode(encode(X)) tests passed
performance of module <module 'base92.cbase92' from 'base92/cbase92.pyc'> on the 10000 random byte strings
- encoding: 0.00835490226746s
- decoding: 0.00846481323242s
performance of module <module 'base92.base92' from 'base92/base92.py'> on the 10000 random byte strings
- encoding: 1.75639009476s
- decoding: 1.28861784935s

If the C backend is not available, the python backend will be used:

rm -f base92/base92_extension.so

>>> import base92
Falling back to base92 python backend due to: No module named base92_extension

We use doctests, so running the tests is as easy as executing the
base92.py library file with your python.
Expand All @@ -25,10 +56,7 @@ base92.py library file with your python.
MISC
----

This library is pure python: there may be a cbase92 forthcoming,
backed by a C library.

This library has not been tested with python3.
This library has a C extension as a backend and falls back to python if the backend isn't available.

There is more information available at
<https://github.com/thenoviceoof/base92>
20 changes: 16 additions & 4 deletions python/base92/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Import routines from base92.base92 for manipulating base92 encoded strings.
"""
Import routines from base92.cbase92 or base92.base92 for manipulating base92 encoded strings.

Example:

Expand All @@ -9,6 +9,18 @@
'Fc_$aOTdKnsM*k'
>>> decode(x)
'hello world'
'''
"""

from base92 import encode, decode, b92encode, b92decode, __version__
from . import base92

try:
from . import cbase92
preferred_base92 = cbase92
except (ImportError, OSError) as e:
print('Falling back to base92 python backend due to: {}'.format(e))
preferred_base92 = base92
cbase92 = None

encode = b92encode = preferred_base92.encode
decode = b92decode = preferred_base92.decode
__version__ = base92.__version__
Loading