Character counter, import C demo
A short C program:#include <stdio.h>
#include <memory.h>
int cnts[128];
int * count_characters(char * str, int length)
{
int i;
memset((void *)cnts, 0, 128 * sizeof(int));
for(i = 0; i < length; i++)
cnts[str[i]] += 1;
return(cnts);
}
compile it to a shared library
on macOS:
gcc count-characters.c -bundle -o count-characters.dylib
on other UNIX
gcc count-characters.c -shared -o count-characters.so
now use it:
newLISP v.9.2.0 on OSX UTF-8, execute 'newlisp -h' for more info.
> (define count-chars (import "count-characters.dylib" "count_characters"))
count_characters <8CEF0>
> (unpack (dup "lu" 128) (count-chars (read-file "war_and_peace.txt") 3217389))
(0 0 0 0 0 0 0 0 0 0 67418 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 511976 3938 17974 2 1 3 0
7526 640 640 339 1 39921 5850 30686 26 230 348
163 55 25 49 51 38 169 54 1003 1148 1 2 1 3138
2 6204 3638 1783 2021 1867 1908 1247 4016 7404
320 1191 687 3288 3627 1638 6117 35 2704 2974
6464 278 939 2896 348 1265 108 47 0 47 0 1 0
199012 30984 59225 116122 312451 52818 49877
162871 166004 2214 19194 95740 58261 180228
190868 38854 2300 144967 159746 219083 65006
25940 56197 3719 44945 2282 0 0 0 1 0)
>