<commit_before>#include "zcm/zcm_private.h"
#include "zcm/transport.h"
#include "zcm/nonblocking.h"

#include <string.h>

/* TODO remove malloc for preallocated mem and linked-lists */
/* RRR: DEFINITELY make this noted somewhere */
#ifndef ZCM_NONBLOCK_SUBS_MAX
#define ZCM_NONBLOCK_SUBS_MAX 512
#endif

struct zcm_nonblocking
{
    zcm_t *z;
    zcm_trans_t *zt;

    bool allChannelsEnabled;

    /* TODO speed this up */
    zcm_sub_t subs[ZCM_NONBLOCK_SUBS_MAX];
    bool      subInUse[ZCM_NONBLOCK_SUBS_MAX];
    size_t    subInUseEnd;
};

static bool isRegexChannel(const char* c, size_t clen)
{
    /* These chars are considered regex */
    size_t i;
    for (i = 0; i < clen; ++i)
        if (c[i] == '(' || c[i] == ')' || c[i] == '|' ||
            c[i] == '.' || c[i] == '*' || c[i] == '+') return true;

    return false;
}

static bool isSupportedRegex(const char* c, size_t clen)
{
    /* Currently only support strings formed as such: */
    /* "[any non-regex character any number of times].*" */
    if (!isRegexChannel(c, clen)) return true;

    if (clen < 2) return false;
    if (c[clen - 1] != '*') return false;
    if (c[clen - 2] != '.') return false;

    size_t i;
    for (i = 0; i < clen - 2; ++i)
        if (!((c[i] >= 'a' && c[i] <= 'z') ||
              (c[i] >= 'A' && c[i] <= 'Z') ||
              (c[i] >= '0' && c[i] <= '9') ||
              (c[i] == '_'))) return false;

    return true;
}

zcm_nonblocking_t *zcm_nonblocking_create(zcm_t *z, zcm_trans_t *zt)
{
    zcm_nonblocking_t *zcm;

    zcm = malloc(sizeof(zcm_nonblocking_t));
    if (!zcm) return NULL;
    zcm->z = z;
    zcm->zt = zt;
    zcm->allChannelsEnabled = false;
    zcm->subInUseEnd = 0;
    return zcm;
}

void zcm_nonblocking_destroy(zcm_nonblocking_t *zcm)
{
    if (zcm) {
        if (zcm->zt) zcm_trans_destroy(zcm->zt);
        free(zcm);
        zcm = NULL;
    }
}

int zcm_nonblocking_publish(zcm_nonblocking_t *z, const char *channel, const char *data,
                            uint32_t len)
{
    zcm_msg_t msg;

    msg.channel = channel;
    msg.len = len;
    msg.buf = (char*)data;
    return zcm_trans_sendmsg(z->zt, msg);
}<commit_msg>now initializing the array in zcm nonblocking<commit_after>#include "zcm/zcm_private.h"
#include "zcm/transport.h"
#include "zcm/nonblocking.h"

#include <string.h>

/* TODO remove malloc for preallocated mem and linked-lists */
/* RRR: DEFINITELY make this noted somewhere */
#ifndef ZCM_NONBLOCK_SUBS_MAX
#define ZCM_NONBLOCK_SUBS_MAX 512
#endif

struct zcm_nonblocking
{
    zcm_t *z;
    zcm_trans_t *zt;

    bool allChannelsEnabled;

    /* TODO speed this up */
    zcm_sub_t subs[ZCM_NONBLOCK_SUBS_MAX];
    bool      subInUse[ZCM_NONBLOCK_SUBS_MAX];
    size_t    subInUseEnd;
};

static bool isRegexChannel(const char* c, size_t clen)
{
    /* These chars are considered regex */
    size_t i;
    for (i = 0; i < clen; ++i)
        if (c[i] == '(' || c[i] == ')' || c[i] == '|' ||
            c[i] == '.' || c[i] == '*' || c[i] == '+') return true;

    return false;
}

static bool isSupportedRegex(const char* c, size_t clen)
{
    /* Currently only support strings formed as such: */
    /* "[any non-regex character any number of times].*" */
    if (!isRegexChannel(c, clen)) return true;

    if (clen < 2) return false;
    if (c[clen - 1] != '*') return false;
    if (c[clen - 2] != '.') return false;

    size_t i;
    for (i = 0; i < clen - 2; ++i)
        if (!((c[i] >= 'a' && c[i] <= 'z') ||
              (c[i] >= 'A' && c[i] <= 'Z') ||
              (c[i] >= '0' && c[i] <= '9') ||
              (c[i] == '_'))) return false;

    return true;
}

zcm_nonblocking_t *zcm_nonblocking_create(zcm_t *z, zcm_trans_t *zt)
{
    zcm_nonblocking_t *zcm;

    zcm = malloc(sizeof(zcm_nonblocking_t));
    if (!zcm) return NULL;
    zcm->z = z;
    zcm->zt = zt;
    zcm->allChannelsEnabled = false;

    size_t i;
    for (i = 0; i < ZCM_NONBLOCK_SUBS_MAX; ++i)
        zcm->subInUse[i] = false;

    zcm->subInUseEnd = 0;
    return zcm;
}

void zcm_nonblocking_destroy(zcm_nonblocking_t *zcm)
{
    if (zcm) {
        if (zcm->zt) zcm_trans_destroy(zcm->zt);
        free(zcm);
        zcm = NULL;
    }
}

int zcm_nonblocking_publish(zcm_nonblocking_t *z, const char *channel, const char *data,
                            uint32_t len)
{
    zcm_msg_t msg;

    msg.channel = channel;
    msg.len = len;
    msg.buf = (char*)data;
    return zcm_trans_sendmsg(z->zt, msg);
}
{'fraction_non_alphanumeric': 0.16106630824372759, 'fraction_numerical': 0.006496415770609319, 'mean_word_length': 2.597904915390814, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2711247', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1749, 'n_words': 418}