Class: Kmp::String

Inherits:
Object
  • Object
show all
Defined in:
lib/kmp/string.rb,
ext/kmp/kmp_string.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rb_string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'ext/kmp/kmp_string.c', line 23

static VALUE initialize(VALUE self, VALUE rb_string)
{
    struct Str * str;

    Check_Type(rb_string, T_STRING);
    Data_Get_Struct(self, struct Str, str);

    str->ptr = calloc(RSTRING_LEN(rb_string) + 1 , sizeof(char));
    memcpy(str->ptr, StringValuePtr(rb_string), RSTRING_LEN(rb_string));

    rb_iv_set(self, "@str", rb_string);
    rb_iv_set(self, "@length", INT2NUM(RSTRING_LEN(rb_string)));

    return self;
}

Instance Attribute Details

#lengthObject (readonly)

#strObject (readonly)

Instance Method Details

#inspectObject



3
4
5
# File 'lib/kmp/string.rb', line 3

def inspect
  str
end

#match(rb_str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/kmp/kmp_string.c', line 57

static VALUE match(VALUE self, VALUE rb_str)
{
    VALUE positions = rb_ary_new();
    struct Str * obj;
    char * str;
    char * ptrn;
    int * prefix;
    int n,m,q;

    Check_Type(rb_str, T_STRING);

    Data_Get_Struct(self, struct Str, obj);
    str = calloc(strlen(obj->ptr) + 1, sizeof(char));
    strcpy(str, obj->ptr);

    ptrn = (char *) calloc(RSTRING_LEN(rb_str) + 1, sizeof(char));
    memcpy(ptrn, StringValuePtr(rb_str), RSTRING_LEN(rb_str));

    prefix = compute_prefix(ptrn);

    n = strlen(str);
    m = strlen(ptrn);

    q = -1;
    for(int i=0; i<n; i++)
    {
        while( q>-1 && ptrn[q+1]!=str[i] ) q = prefix[q];
        if( ptrn[q+1]==str[i] ) q = q+1;
        if( q==m-1 )
        {
            rb_ary_push(positions, INT2NUM(i-m+1));
            q = prefix[q];
        }
    }
    free(prefix);
    free(ptrn);
    free(str);

    return positions;
}

#replace(rb_str, rb_string_sub) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/kmp/kmp_string.c', line 99

static VALUE replace(VALUE self, VALUE rb_str, VALUE rb_string_sub)
{
    struct Str * obj;
    char * str;
    char * new_str;
    char * sub_str;

    Check_Type(rb_str, T_STRING);
    Check_Type(rb_string_sub, T_STRING);

    Data_Get_Struct(self, struct Str, obj);
    str = calloc(strlen(obj->ptr) + 1, sizeof(char));
    strcpy(str, obj->ptr);
    int current_str_len = strlen(str);

    sub_str = (char *) calloc(RSTRING_LEN(rb_string_sub) + 1, sizeof(char));
    memcpy(sub_str, StringValuePtr(rb_string_sub), RSTRING_LEN(rb_string_sub));


    VALUE pos = match(self, rb_str);
    VALUE * arr = rb_array_const_ptr(pos);

    int replace_str_len, sub_string_len, occurance;

    replace_str_len = RSTRING_LEN(rb_str);
    sub_string_len = strlen(sub_str);
    occurance = rb_array_len(pos);

    int new_str_len = (current_str_len - (replace_str_len * occurance) + (sub_string_len * occurance));
    new_str = (char *) calloc(new_str_len+1, sizeof(char));

    int indx = 0;
    int occurance_indx = occurance ? 0 : -1;
    int str_indx = 0;

    while(str_indx<=new_str_len)
    {
        if(occurance_indx != -1 && occurance_indx<occurance && RB_NUM2INT(arr[occurance_indx]) == indx)
        {
            int sub_indx = 0;
            while(sub_indx<sub_string_len)
            {
                new_str[str_indx] = sub_str[sub_indx];
                str_indx++;
                sub_indx++;
            }
            indx += replace_str_len;
            occurance_indx++;
        }
        else
        {
            new_str[str_indx++] = str[indx++];
        }
    }

    VALUE rb_new_str = rb_str_new2(new_str);

    free(new_str);
    free(sub_str);
    free(str);

    return rb_new_str;
}