Ejemplo n.º 1
0
/**
 * Saffire method: match a string against (compiled) regex
 */
SAFFIRE_METHOD(regex, match) {
    t_string_object *str;
    int ovector[OVECCOUNT];
    int rc;

    // Parse the arguments
    if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "s", &str)) {
        saffire_warning("Error while parsing argument list\n");
        RETURN_NUMERICAL(0);
    }

    // Convert to utf8 and execute regex
    rc = pcre_exec(self->regex, 0, str->value, strlen(str->value), 0, 0, ovector, OVECCOUNT);

    // Check result
    if (rc < 0) {
        switch (rc) {
            case PCRE_ERROR_NOMATCH:
                saffire_warning("String didn't match.\n");
                break;
            default :
                saffire_warning("Error while matching: %d\n", rc);
                break;
        }
    }

    // Display result
    for (int i=0; i<rc; i++) {
        DEBUG_PRINT("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str->value + ovector[2*i]);
    }

    // Return number of matches
    RETURN_NUMERICAL(rc);
}
Ejemplo n.º 2
0
/**
 * Saffire method:
 */
SAFFIRE_METHOD(hash, add) {
    t_string_object *key, *val;

    if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "ss", &key, &val)) {
        saffire_warning("Error while parsing argument list\n");
        RETURN_NUMERICAL(0);
    }

    ht_add(self->ht, key->value, val->value);
    RETURN_SELF;
}
Ejemplo n.º 3
0
/**
 * Saffire method:
 */
SAFFIRE_METHOD(hash, remove) {
    t_string_object *key;

    if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "s", &key)) {
        saffire_warning("Error while parsing argument list\n");
        RETURN_NUMERICAL(0);
    }

    ht_remove(self->ht, key->value);
    RETURN_SELF;
}
Ejemplo n.º 4
0
/**
  * Saffire method: Returns object stored at "key" inside the hash (or NULL when not found)
  */
SAFFIRE_METHOD(hash, find) {
    t_string_object *key;

    if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "s", &key)) {
        saffire_warning("Error while parsing argument list\n");
        RETURN_NUMERICAL(0);
    }

    t_object *obj = ht_find(self->ht, key->value);
    if (obj == NULL) RETURN_NULL;
    RETURN_OBJECT(obj);
}
Ejemplo n.º 5
0
static t_object *obj_new(t_object *obj, va_list arg_list) {
    const char *error;
    int erroffset;

    // Create new object and copy all info
    t_regex_object *new_obj = smm_malloc(sizeof(t_regex_object));
    memcpy(new_obj, Object_Regex, sizeof(t_regex_object));

    new_obj->regex_string = va_arg(arg_list, char *);
    int pcre_options = va_arg(arg_list, int);

    new_obj->regex = pcre_compile(new_obj->regex_string, PCRE_UTF8 | pcre_options, &error, &erroffset, 0);
    if (! new_obj->regex) {
        saffire_warning("pcre_compiled failed (offset: %d), %s\n", erroffset, error);
    }

    return (t_object *)new_obj;
}
Ejemplo n.º 6
0
SAFFIRE_OPERATOR_METHOD(numerical, sr) {
    t_numerical_object *self = (t_numerical_object *)_self;
    t_numerical_object *other;

    // Parse the arguments
    if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "n", &other)) {
        saffire_warning("Error while parsing argument list\n");
        RETURN_NUMERICAL(0);
    }

    if (in_place) {
        DEBUG_PRINT("Add to self\n");
        self->value >>= other->value;
        RETURN_SELF;
    }

    t_object *obj = object_new(Object_Numerical, self->value >> other->value);
    RETURN_OBJECT(obj);
}