Created
June 16, 2013 21:40
-
-
Save RavuAlHemio/5793539 to your computer and use it in GitHub Desktop.
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
/* on Linux, compile me with: gcc -shared -o foreach.so foreach.c */ | |
#include <stddef.h> | |
typedef wchar_t (*transform_wct)(wchar_t); | |
void foreachwchar(wchar_t *cs, transform_wct func) | |
{ | |
while (cs[0] != L'\0') | |
{ | |
cs[0] = func(cs[0]); | |
++cs; | |
} | |
} |
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
#!/usr/bin/env python3 | |
import ctypes as ct | |
def mytransform(c): | |
return chr(ord(c) + 1) | |
# this is the wchar_t * we'll pass to foreachwchar | |
unibuf = ct.create_unicode_buffer("Sgd\x1fpthbj\x1faqnvm\x1fenw\x1fitlor\x1fnudq\x1fsgd\x1fk`yx\x1fcnf-") | |
# fetch foreachwchar | |
foreachlib = ct.CDLL("./foreach.so") | |
foreachwchar = foreachlib.foreachwchar | |
# this is effectively the Python version of that typedef | |
transform_wct = ct.CFUNCTYPE(ct.c_wchar, ct.c_wchar) | |
# now "convert" mytransform according to the above typedef | |
transformcallback = transform_wct(mytransform) | |
# calltime! | |
foreachwchar(unibuf, transformcallback) | |
# output! | |
print(unibuf.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment