コード例 #1
0
ファイル: njs_regexp.c プロジェクト: nginx/njs
static njs_ret_t
njs_regexp_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
                               nxt_uint_t nargs, njs_index_t unused)
{
    u_char                *source;
    int32_t               length;
    uint32_t              size;
    njs_value_t           *value;
    njs_regexp_pattern_t  *pattern;

    value = &args[0];

    if (value->type == NJS_REGEXP) {
        pattern = value->data.u.regexp->pattern;
        source = pattern->source;

        size = strlen((char *) source);
        length = nxt_utf8_length(source, size);

        return njs_regexp_string_create(vm, &vm->retval, source, size, length);
    }

    vm->exception = &njs_exception_type_error;

    return NXT_ERROR;
}
コード例 #2
0
static njs_ret_t
njs_regexp_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
    nxt_uint_t nargs, njs_index_t unused)
{
    u_char                *source;
    size_t                length, size;
    njs_regexp_pattern_t  *pattern;

    pattern = args[0].data.u.regexp->pattern;
    source = pattern->source;

    size = strlen((char *) source);
    length = nxt_utf8_length(source, size);

    return njs_regexp_string_create(vm, &vm->retval, source, size, length);
}
コード例 #3
0
static njs_ret_t
njs_regexp_prototype_source(njs_vm_t *vm, njs_value_t *value)
{
    u_char                *source;
    size_t                length, size;
    njs_regexp_pattern_t  *pattern;

    pattern = value->data.u.regexp->pattern;
    /* Skip starting "/". */
    source = pattern->source + 1;

    size = strlen((char *) source) - pattern->flags;
    length = nxt_utf8_length(source, size);

    return njs_regexp_string_create(vm, &vm->retval, source, size, length);
}
コード例 #4
0
static njs_ret_t
njs_regexp_exec_result(njs_vm_t *vm, njs_regexp_t *regexp, u_char *string,
    nxt_regex_match_data_t *match_data, nxt_uint_t utf8)
{
    int                 *captures;
    u_char              *start;
    int32_t             size, length;
    njs_ret_t           ret;
    nxt_uint_t          i, n;
    njs_array_t         *array;
    njs_object_prop_t   *prop;
    nxt_lvlhsh_query_t  lhq;

    static const njs_value_t  njs_string_index = njs_string("index");
    static const njs_value_t  njs_string_input = njs_string("input");

    array = njs_array_alloc(vm, regexp->pattern->ncaptures, 0);
    if (nxt_slow_path(array == NULL)) {
        goto fail;
    }

    captures = nxt_regex_captures(match_data);

    for (i = 0; i < regexp->pattern->ncaptures; i++) {
        n = 2 * i;

        if (captures[n] != -1) {
            start = &string[captures[n]];
            size = captures[n + 1] - captures[n];

            switch (utf8) {
            case 0:
                length = 0;
                break;
            case 1:
                length = size;
                break;
            default:
                length = nxt_utf8_length(start, size);
                break;
            }

            ret = njs_regexp_string_create(vm, &array->start[i],
                                           start, size, length);
            if (nxt_slow_path(ret != NXT_OK)) {
                goto fail;
            }

        } else {
            array->start[i] = njs_value_void;
        }
    }

    prop = njs_object_prop_alloc(vm, &njs_string_index);
    if (nxt_slow_path(prop == NULL)) {
        goto fail;
    }

    /* TODO: Non UTF-8 position */

    njs_number_set(&prop->value, regexp->last_index + captures[0]);

    if (regexp->pattern->global) {
        regexp->last_index += captures[1];
    }

    lhq.key_hash = NJS_INDEX_HASH;
    lhq.key.len = sizeof("index") - 1;
    lhq.key.data = (u_char *) "index";
    lhq.replace = 0;
    lhq.value = prop;
    lhq.pool = vm->mem_cache_pool;
    lhq.proto = &njs_object_hash_proto;

    ret = nxt_lvlhsh_insert(&array->object.hash, &lhq);
    if (nxt_slow_path(ret != NXT_OK)) {
        goto fail;
    }

    prop = njs_object_prop_alloc(vm, &njs_string_input);
    if (nxt_slow_path(prop == NULL)) {
        goto fail;
    }

    njs_string_copy(&prop->value, &regexp->string);

    lhq.key_hash = NJS_INPUT_HASH;
    lhq.key.len = sizeof("input") - 1;
    lhq.key.data = (u_char *) "input";
    lhq.value = prop;

    ret = nxt_lvlhsh_insert(&array->object.hash, &lhq);

    if (nxt_fast_path(ret == NXT_OK)) {
        vm->retval.data.u.array = array;
        vm->retval.type = NJS_ARRAY;
        vm->retval.data.truth = 1;

        ret = NXT_OK;
        goto done;
    }

fail:

    ret = NXT_ERROR;

done:

    nxt_regex_match_data_free(match_data, vm->regex_context);

    return ret;
}