cmultibyte 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/ext/cmultibyte.c CHANGED
@@ -9,6 +9,7 @@ int *cp1252;
9
9
 
10
10
  static ID idUnpack;
11
11
  static ID idPack;
12
+ static ID idForceEncoding;
12
13
 
13
14
  static int inline is_cont(uint8_t byte) { return (byte > 127 && byte < 192); }
14
15
  static int inline is_lead(uint8_t byte) { return (byte > 191 && byte < 245); }
@@ -87,12 +88,20 @@ static void cp1252_hash_to_array(VALUE cp1252_hash) {
87
88
  }
88
89
  }
89
90
 
90
- static VALUE tidy_bytes(VALUE string) {
91
+ struct protected_tidy_bytes_arg {
92
+ VALUE string;
93
+ uint8_t *arr;
94
+ };
95
+
96
+ static VALUE protected_tidy_bytes(VALUE arg) {
97
+ struct protected_tidy_bytes_arg* args = (struct protected_tidy_bytes_arg *)arg;
98
+ VALUE string = args->string;
99
+ uint8_t *arr = args->arr;
100
+
91
101
  int conts_expected = 0;
92
102
  int i, j;
93
103
  uint8_t byte;
94
104
 
95
- uint8_t *arr = ALLOC_N(uint8_t, 4 * RSTRING_LEN(string));
96
105
  uint8_t *curr = arr;
97
106
  uint8_t *prev = curr;
98
107
  uint8_t *last_lead = curr;
@@ -131,7 +140,6 @@ static VALUE tidy_bytes(VALUE string) {
131
140
  curr = tidy_byte2(temp[j], curr);
132
141
  }
133
142
 
134
- //last_lead += (curr-prev);
135
143
  conts_expected = 0;
136
144
  }
137
145
  if (is_lead(byte)) {
@@ -155,10 +163,28 @@ static VALUE tidy_bytes(VALUE string) {
155
163
  }
156
164
  }
157
165
  VALUE str = rb_str_new((const char *)arr, curr-arr);
166
+ return rb_funcall(str, idForceEncoding, 1, rb_str_new2("UTF-8"));
167
+ }
168
+
169
+ static VALUE tidy_bytes(VALUE string) {
170
+ uint8_t *arr = ALLOC_N(uint8_t, 4 * RSTRING_LEN(string));
171
+ VALUE ret;
172
+ int exception = 0;
173
+ struct protected_tidy_bytes_arg args;
174
+
175
+ args.string = string;
176
+ args.arr = arr;
177
+
178
+ ret = rb_protect(protected_tidy_bytes, (VALUE)&args, &exception);
179
+
158
180
  xfree(arr);
159
- return str;
181
+ if (exception) {
182
+ rb_jump_tag(exception);
183
+ }
184
+ return ret;
160
185
  }
161
186
 
187
+
162
188
  static VALUE force_tidy_bytes(VALUE string) {
163
189
  uint8_t *arr = malloc(4 * sizeof(uint8_t) * RSTRING_LEN(string));
164
190
  uint8_t *curr = arr;
@@ -184,6 +210,7 @@ VALUE rb_tidy_bytes(int argc, VALUE *argv, VALUE self) {
184
210
  void Init_cmultibyte() {
185
211
  idUnpack = rb_intern("unpack");
186
212
  idPack = rb_intern("pack");
213
+ idForceEncoding = rb_intern("force_encoding");
187
214
 
188
215
  rb_funcall(rb_cObject, rb_intern("require"), 1, rb_str_new2("active_support/all"));
189
216
 
@@ -1,3 +1,3 @@
1
1
  module Cmultibyte
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: cmultibyte
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Burke Libbey
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-16 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement