C API identify null symbol

Viewed 158

I have the following function to identify null symbols, where ns should represent the null symbol.

#include"k.h"

K test (K x) {
    K ns = ks("");
    if (abs(x->t) != KS) {
        R krr("type");
    }
    if (x->t > 0) {
        K res = ktn(KB,x->n);
        for (int i=0;i<x->n;++i){
            if (kK(x)[i] == ns) {
                kG(res)[i] = 1;
            }
            else {
                kG(res)[i] = 0;
            }
        }
        R res;
    }
    else {
        if (x == ns) {
            R kb(1);
        }
        else {
            R kb(0);
        }
    }
}

The check for equality of the argument x (or the element kK(x)[i] of arg x) to ns does not work as I expected.

q)`test set `:test 2: `test,1
`test
q)test ``a`b
000b          
// should be 100b

How do I perform this comparison properly?

1 Answers

You would be better to compare the underlying char array rather than the entire K object

Also remember to remove the reference to ns when you are finished with it to avoid memory leaks

K test(K x) {
    K ns = ks("");
    if (abs(x->t) != KS) {
        R krr("type");
    }
    if (x->t > 0) {
        K res = ktn(KB, x->n);
        for (int i = 0; i<x->n; ++i) {
            if (kS(x)[i] == ns->s) {
                kG(res)[i] = 1;
            }
            else {
                kG(res)[i] = 0;
            }
        }
        r0(ns);
        R res;
    }
    else {
        if (x->s == ns->s) {
            r0(ns);
            R kb(1);
        }
        else {
            r0(ns);
            R kb(0);
        }
    }
Related