Skip to content

Instantly share code, notes, and snippets.

@RavuAlHemio
Created June 16, 2013 21:40
Show Gist options
  • Save RavuAlHemio/5793539 to your computer and use it in GitHub Desktop.
Save RavuAlHemio/5793539 to your computer and use it in GitHub Desktop.
/* 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;
}
}
#!/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