kmp 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7d9ef58fc7947f5b3f2901cf4c52e7ad3e5ad99
4
- data.tar.gz: 68909c1f9efaeef13ef684828c13b7e4c01b29aa
3
+ metadata.gz: 6c55b1f5d55cf0a2bd424f55968e9e55904b9ec3
4
+ data.tar.gz: f5b3085e1c3bd98c034386e662390065184cb4fe
5
5
  SHA512:
6
- metadata.gz: 8cf863aaf38f13b29b0a8b1a5ddb2da28457e31d8064e2f0265614d3457ce653da7221984e58e0d424409c4a97018ffc3bbebaef4c8cd4a282a0752564c59deb
7
- data.tar.gz: 378da39201d4cc1f38dfc6d99b249731fcfaf5ca91c3c3fe665a33627d7716dd64e8af00f0ce9fd6d5d78be45b645b2f7acf67354affdb9b3c5961f6180537b5
6
+ metadata.gz: bcc427627cf11179534bb50f9d34d6f962b620988ff1d894dfa997c97ccbc3dccc54998028d70180fbe2de3727ca62dcfa6247d569bf99de3bb39aa99e3af813
7
+ data.tar.gz: 39021da676db49f1022f3e2834609f1cd64cdf2b0e1e032a5aa9e643893d0b4d1cbe79769d48274dca0c0ac7201e4eb2cc0b222aabd915febf4414afeeb798f8
@@ -6,3 +6,7 @@
6
6
  * memory allocation bug fix
7
7
  * make function pointer constant
8
8
  * improve code standard
9
+
10
+ ## v0.3.0
11
+ * Remove unnecessary test methods
12
+ * add replace method
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kmp (0.2.0)
4
+ kmp (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -31,14 +31,17 @@ Or install it yourself as:
31
31
  ``` ruby
32
32
  require 'kmp'
33
33
  k = Kmp::String.new 'abcdeab'
34
- k.match 'ab' #=> [0, 4]
34
+ k.match 'ab' #=> [0, 5]
35
35
 
36
36
  k.length #=> 7
37
+
38
+ k.replace 'ab', 'X' #=> 'XcdeX'
37
39
  ```
38
40
 
39
41
  ## Complexity
40
42
 
41
43
  - match function complexity O(N+M), N original text string length, M match string length
44
+ - replace function complexity O(N+M)
42
45
  - length complexity O(1)
43
46
 
44
47
  ## Benchmark
@@ -1,14 +1,8 @@
1
1
  #include<ruby.h>
2
2
  #include<kmp_string.h>
3
3
 
4
- static VALUE hello_world(VALUE klass)
5
- {
6
- return rb_str_new2("hello world");
7
- }
8
-
9
4
  void Init_kmp()
10
5
  {
11
6
  VALUE mKmp = rb_define_module("Kmp");
12
- rb_define_singleton_method(mKmp, "hello_world", hello_world, 0);
13
7
  Init_kmp_string(mKmp);
14
8
  }
@@ -63,6 +63,8 @@ static VALUE match(VALUE self, VALUE rb_str)
63
63
  int * prefix;
64
64
  int n,m,q;
65
65
 
66
+ Check_Type(rb_str, T_STRING);
67
+
66
68
  Data_Get_Struct(self, struct Str, obj);
67
69
  str = calloc(strlen(obj->ptr) + 1, sizeof(char));
68
70
  strcpy(str, obj->ptr);
@@ -93,6 +95,71 @@ static VALUE match(VALUE self, VALUE rb_str)
93
95
  return positions;
94
96
  }
95
97
 
98
+
99
+ static VALUE replace(VALUE self, VALUE rb_str, VALUE rb_string_sub)
100
+ {
101
+ struct Str * obj;
102
+ char * str;
103
+ char * new_str;
104
+ char * sub_str;
105
+
106
+ Check_Type(rb_str, T_STRING);
107
+ Check_Type(rb_string_sub, T_STRING);
108
+
109
+ Data_Get_Struct(self, struct Str, obj);
110
+ str = calloc(strlen(obj->ptr) + 1, sizeof(char));
111
+ strcpy(str, obj->ptr);
112
+ int current_str_len = strlen(str);
113
+
114
+ sub_str = (char *) calloc(RSTRING_LEN(rb_string_sub) + 1, sizeof(char));
115
+ memcpy(sub_str, StringValuePtr(rb_string_sub), RSTRING_LEN(rb_string_sub));
116
+
117
+
118
+ VALUE pos = match(self, rb_str);
119
+ VALUE * arr = rb_array_const_ptr(pos);
120
+
121
+ int replace_str_len, sub_string_len, occurance;
122
+
123
+ replace_str_len = RSTRING_LEN(rb_str);
124
+ sub_string_len = strlen(sub_str);
125
+ occurance = rb_array_len(pos);
126
+
127
+ int new_str_len = (current_str_len - (replace_str_len * occurance) + (sub_string_len * occurance));
128
+ new_str = (char *) calloc(new_str_len+1, sizeof(char));
129
+
130
+ int indx = 0;
131
+ int occurance_indx = occurance ? 0 : -1;
132
+ int str_indx = 0;
133
+
134
+ while(str_indx<=new_str_len)
135
+ {
136
+ if(occurance_indx != -1 && occurance_indx<occurance && RB_NUM2INT(arr[occurance_indx]) == indx)
137
+ {
138
+ int sub_indx = 0;
139
+ while(sub_indx<sub_string_len)
140
+ {
141
+ new_str[str_indx] = sub_str[sub_indx];
142
+ str_indx++;
143
+ sub_indx++;
144
+ }
145
+ indx += replace_str_len;
146
+ occurance_indx++;
147
+ }
148
+ else
149
+ {
150
+ new_str[str_indx++] = str[indx++];
151
+ }
152
+ }
153
+
154
+ VALUE rb_new_str = rb_str_new2(new_str);
155
+
156
+ free(new_str);
157
+ free(sub_str);
158
+ free(str);
159
+
160
+ return rb_new_str;
161
+ }
162
+
96
163
  void Init_kmp_string(VALUE mKmp)
97
164
  {
98
165
  VALUE cKmpString = rb_define_class_under(mKmp, "String", rb_cObject);
@@ -100,6 +167,7 @@ void Init_kmp_string(VALUE mKmp)
100
167
  rb_define_alloc_func(cKmpString, allocate);
101
168
  rb_define_method(cKmpString, "initialize", initialize, 1);
102
169
  rb_define_method(cKmpString, "match", match, 1);
170
+ rb_define_method(cKmpString, "replace", replace, 2);
103
171
  rb_define_attr(cKmpString, "str", 1, 0);
104
172
  rb_define_attr(cKmpString, "length", 1, 0);
105
173
  }
Binary file
@@ -1,3 +1,3 @@
1
1
  module Kmp
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tanvir hasan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-04 00:00:00.000000000 Z
11
+ date: 2018-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler