u 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/README +38 -0
  2. data/Rakefile +64 -0
  3. data/ext/encoding/character/utf-8/break.c +25 -0
  4. data/ext/encoding/character/utf-8/data/break.h +22931 -0
  5. data/ext/encoding/character/utf-8/data/character-tables.h +14358 -0
  6. data/ext/encoding/character/utf-8/data/compose.h +1607 -0
  7. data/ext/encoding/character/utf-8/data/decompose.h +10926 -0
  8. data/ext/encoding/character/utf-8/data/generate-unicode-data.rb +1070 -0
  9. data/ext/encoding/character/utf-8/decompose.c +444 -0
  10. data/ext/encoding/character/utf-8/depend +65 -0
  11. data/ext/encoding/character/utf-8/extconf.rb +67 -0
  12. data/ext/encoding/character/utf-8/private.c +62 -0
  13. data/ext/encoding/character/utf-8/private.h +51 -0
  14. data/ext/encoding/character/utf-8/properties.c +1056 -0
  15. data/ext/encoding/character/utf-8/rb_includes.h +19 -0
  16. data/ext/encoding/character/utf-8/rb_methods.h +49 -0
  17. data/ext/encoding/character/utf-8/rb_private.h +52 -0
  18. data/ext/encoding/character/utf-8/rb_utf_aref.c +111 -0
  19. data/ext/encoding/character/utf-8/rb_utf_aset.c +105 -0
  20. data/ext/encoding/character/utf-8/rb_utf_casecmp.c +24 -0
  21. data/ext/encoding/character/utf-8/rb_utf_chomp.c +114 -0
  22. data/ext/encoding/character/utf-8/rb_utf_chop.c +44 -0
  23. data/ext/encoding/character/utf-8/rb_utf_collate.c +13 -0
  24. data/ext/encoding/character/utf-8/rb_utf_count.c +30 -0
  25. data/ext/encoding/character/utf-8/rb_utf_delete.c +60 -0
  26. data/ext/encoding/character/utf-8/rb_utf_downcase.c +13 -0
  27. data/ext/encoding/character/utf-8/rb_utf_each_char.c +27 -0
  28. data/ext/encoding/character/utf-8/rb_utf_foldcase.c +13 -0
  29. data/ext/encoding/character/utf-8/rb_utf_hex.c +14 -0
  30. data/ext/encoding/character/utf-8/rb_utf_index.c +50 -0
  31. data/ext/encoding/character/utf-8/rb_utf_insert.c +48 -0
  32. data/ext/encoding/character/utf-8/rb_utf_internal_bignum.c +332 -0
  33. data/ext/encoding/character/utf-8/rb_utf_internal_bignum.h +12 -0
  34. data/ext/encoding/character/utf-8/rb_utf_internal_tr.c +142 -0
  35. data/ext/encoding/character/utf-8/rb_utf_internal_tr.h +41 -0
  36. data/ext/encoding/character/utf-8/rb_utf_justify.c +96 -0
  37. data/ext/encoding/character/utf-8/rb_utf_length.c +14 -0
  38. data/ext/encoding/character/utf-8/rb_utf_lstrip.c +41 -0
  39. data/ext/encoding/character/utf-8/rb_utf_normalize.c +51 -0
  40. data/ext/encoding/character/utf-8/rb_utf_oct.c +14 -0
  41. data/ext/encoding/character/utf-8/rb_utf_reverse.c +13 -0
  42. data/ext/encoding/character/utf-8/rb_utf_rindex.c +88 -0
  43. data/ext/encoding/character/utf-8/rb_utf_rstrip.c +51 -0
  44. data/ext/encoding/character/utf-8/rb_utf_squeeze.c +70 -0
  45. data/ext/encoding/character/utf-8/rb_utf_strip.c +27 -0
  46. data/ext/encoding/character/utf-8/rb_utf_to_i.c +25 -0
  47. data/ext/encoding/character/utf-8/rb_utf_tr.c +250 -0
  48. data/ext/encoding/character/utf-8/rb_utf_upcase.c +13 -0
  49. data/ext/encoding/character/utf-8/tables.h +38 -0
  50. data/ext/encoding/character/utf-8/unicode.c +319 -0
  51. data/ext/encoding/character/utf-8/unicode.h +216 -0
  52. data/ext/encoding/character/utf-8/utf.c +1334 -0
  53. data/lib/encoding/character/utf-8.rb +201 -0
  54. data/lib/u.rb +16 -0
  55. data/lib/u/string.rb +185 -0
  56. data/lib/u/version.rb +5 -0
  57. data/test/unit/u.rb +5 -0
  58. data/test/unit/u/string.rb +91 -0
  59. metadata +174 -0
@@ -0,0 +1,51 @@
1
+ /*
2
+ * contents: UTF8.rstrip module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+
9
+ VALUE
10
+ rb_utf_rstrip_bang(UNUSED(VALUE self), VALUE str)
11
+ {
12
+ StringValue(str);
13
+ const char *begin = RSTRING(str)->ptr;
14
+ if (begin == NULL || RSTRING(str)->len == 0)
15
+ return Qnil;
16
+
17
+ const char *end = begin + RSTRING(str)->len;
18
+ const char *t = end;
19
+
20
+ /* Remove trailing '\0'’s. */
21
+ while (t > begin && t[-1] == '\0')
22
+ t--;
23
+
24
+ /* Remove trailing spaces. */
25
+ while (t > begin) {
26
+ /* FIXME: Should we be validating here? */
27
+ const char *prev = rb_utf_prev_validated(begin, t);
28
+
29
+ if (!unichar_isspace(utf_char(prev)))
30
+ break;
31
+
32
+ t = prev;
33
+ }
34
+
35
+ if (t == end)
36
+ return Qnil;
37
+
38
+ rb_str_modify(str);
39
+ RSTRING(str)->len = t - begin;
40
+ RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
41
+
42
+ return str;
43
+ }
44
+
45
+ VALUE
46
+ rb_utf_rstrip(VALUE self, VALUE str)
47
+ {
48
+ str = rb_utf_dup(str);
49
+ rb_utf_rstrip_bang(self, str);
50
+ return str;
51
+ }
@@ -0,0 +1,70 @@
1
+ /*
2
+ * contents: UTF8.squeeze module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+ #include "rb_utf_internal_tr.h"
9
+
10
+ VALUE
11
+ rb_utf_squeeze_bang(int argc, VALUE *argv, UNUSED(VALUE self))
12
+ {
13
+ need_at_least_n_arguments(argc, 1);
14
+
15
+ VALUE str = argv[0];
16
+ StringValue(str);
17
+ if (RSTRING(str)->len == 0)
18
+ return Qnil;
19
+
20
+ unsigned int table[TR_TABLE_SIZE];
21
+ if (argc == 1)
22
+ for (int i = 0; i < TR_TABLE_SIZE; i++)
23
+ table[i] = ~0U;
24
+ else
25
+ tr_setup_table_from_strings(table, argc - 1, &argv[1]);
26
+
27
+ rb_str_modify(str);
28
+
29
+ char *begin = RSTRING(str)->ptr;
30
+ char const *end = begin + RSTRING(str)->len;
31
+
32
+ /* We know that there is a character to eat (if the input isn’t
33
+ * invalid), as we’ve already verified that RSTRING(str)->len > 0, so
34
+ * ‘s_end’ must lay beyond ‘s’. Also, as we validate when we fetch the
35
+ * character, there’s no need to validate the call to utf_next(). */
36
+ unichar previous = _utf_char_validated(begin, end);
37
+ char *s = utf_next(begin);
38
+ char *t = s;
39
+ while (s < end) {
40
+ unichar c = _utf_char_validated(s, end);
41
+ char *next = utf_next(s);
42
+
43
+ if (c != previous || !tr_table_lookup(table, c)) {
44
+ memmove(t, s, next - s);
45
+ t += next - s;
46
+ previous = c;
47
+ }
48
+
49
+ s = next;
50
+ }
51
+ *t = '\0';
52
+
53
+ if (t - begin != RSTRING(str)->len) {
54
+ RSTRING(str)->len = t - begin;
55
+ return str;
56
+ }
57
+
58
+ return Qnil;
59
+ }
60
+
61
+ VALUE
62
+ rb_utf_squeeze(int argc, VALUE *argv, VALUE self)
63
+ {
64
+ need_at_least_n_arguments(argc, 1);
65
+
66
+ StringValue(argv[0]);
67
+ argv[0] = rb_utf_dup(argv[0]);
68
+ rb_utf_squeeze_bang(argc, argv, self);
69
+ return argv[0];
70
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * contents: UTF8.strip module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+
9
+ VALUE
10
+ rb_utf_strip_bang(VALUE self, VALUE str)
11
+ {
12
+ VALUE left = rb_utf_lstrip_bang(self, str);
13
+ VALUE right = rb_utf_rstrip_bang(self, str);
14
+
15
+ if (NIL_P(left) && NIL_P(right))
16
+ return Qnil;
17
+
18
+ return str;
19
+ }
20
+
21
+ VALUE
22
+ rb_utf_strip(VALUE self, VALUE str)
23
+ {
24
+ str = rb_utf_dup(str);
25
+ rb_utf_strip_bang(self, str);
26
+ return str;
27
+ }
@@ -0,0 +1,25 @@
1
+ /*
2
+ * contents: UTF8.to_i module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+ #include "rb_utf_internal_bignum.h"
9
+
10
+ VALUE
11
+ rb_utf_to_i(int argc, VALUE *argv, UNUSED(VALUE self))
12
+ {
13
+ VALUE str, rbbase;
14
+
15
+ int base = 10;
16
+ if (rb_scan_args(argc, argv, "11", &str, &rbbase) == 2)
17
+ base = NUM2INT(rbbase);
18
+
19
+ /* XXX: this test is actually unnecessary, as this will be checked in
20
+ * rb_utf_to_inum() as well. */
21
+ if (base < 0)
22
+ rb_raise(rb_eArgError, "illegal radix %d", base);
23
+
24
+ return rb_utf_to_inum(str, base, false);
25
+ }
@@ -0,0 +1,250 @@
1
+ /*
2
+ * contents: UTF8.tr module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+ #include "rb_utf_internal_tr.h"
9
+
10
+ struct tr_range
11
+ {
12
+ unichar begin;
13
+ unichar end;
14
+ };
15
+
16
+ static int
17
+ tr_ranges_setup(struct tr *tr, struct tr_range *ranges)
18
+ {
19
+ int n = 0;
20
+ bool was_inside_range = false;
21
+ while (tr_next(tr) != TR_FINISHED) {
22
+ if (tr->inside_range) {
23
+ if (!was_inside_range) {
24
+ ranges[n].begin = tr->now;
25
+ was_inside_range = true;
26
+ }
27
+ } else {
28
+ if (was_inside_range)
29
+ ranges[n].end = tr->now;
30
+ else
31
+ ranges[n].begin = ranges[n].end = tr->now;
32
+ n++;
33
+ was_inside_range = false;
34
+ }
35
+ }
36
+
37
+ return n;
38
+ }
39
+
40
+
41
+ struct tr_trans_closure
42
+ {
43
+ struct tr_range *from;
44
+ int n_from;
45
+ struct tr_range *to;
46
+ int n_to;
47
+ };
48
+
49
+ static unichar
50
+ tr_trans_replace_exclude(UNUSED(unichar c), void *closure)
51
+ {
52
+ return *((unichar *)closure);
53
+ }
54
+
55
+ static int
56
+ tr_trans_replace_include_offset_of(struct tr_range *ranges, int range)
57
+ {
58
+ int offset = 0;
59
+
60
+ for (int i = 0; i < range; i++)
61
+ offset += ranges[i].end - ranges[i].begin + 1;
62
+
63
+ return offset;
64
+ }
65
+
66
+ static unichar
67
+ tr_trans_replace_include(unichar c, void *v_closure)
68
+ {
69
+ struct tr_trans_closure *closure = (struct tr_trans_closure *)v_closure;
70
+
71
+ for (int i = closure->n_from - 1; i >= 0; i--) {
72
+ if (closure->from[i].begin >= c && closure->from[i].end <= c) {
73
+ int offset = tr_trans_replace_include_offset_of(closure->from, i);
74
+ int j;
75
+ for (j = 0; j < closure->n_to && offset > 0; j++)
76
+ offset -= closure->to[j].end - closure->to[j].begin + 1;
77
+
78
+ if (offset > 0)
79
+ return closure->to[closure->n_to - 1].end;
80
+
81
+ return closure->to[j].end - offset;
82
+ }
83
+ }
84
+
85
+ return closure->to[closure->n_to - 1].end;
86
+ }
87
+
88
+ static VALUE
89
+ tr_trans_do(VALUE src, unsigned int *translation,
90
+ unichar (*replace)(unichar, void *), void *closure, bool squeeze,
91
+ UNUSED(bool replace_content))
92
+ {
93
+ VALUE dst = Qnil;
94
+ long len;
95
+
96
+ again:
97
+ len = 0;
98
+
99
+ const char *s = RSTRING(src)->ptr;
100
+ const char *s_end = s + RSTRING(src)->len;
101
+
102
+ char *t = NULL;
103
+
104
+ if (dst != Qnil)
105
+ t = RSTRING(dst)->ptr;
106
+
107
+ bool modified = false;
108
+
109
+ /* TODO: this should really be refactored… */
110
+ if (squeeze) {
111
+ unichar prev_c = -1;
112
+
113
+ while (s < s_end) {
114
+ unichar c0 = utf_char(s);
115
+
116
+ const char *prev = s;
117
+ s = utf_next(s);
118
+
119
+ if (tr_table_lookup(translation, c0)) {
120
+ unichar c = replace(c0, closure);
121
+ if (prev_c == c)
122
+ continue;
123
+ prev_c = c;
124
+ len += unichar_to_utf(c, (t != NULL) ? t + len : NULL);
125
+ modified = true;
126
+ } else {
127
+ prev_c = -1;
128
+ if (t != NULL)
129
+ memcpy(t + len, prev, s - prev);
130
+ len += s - prev;
131
+ }
132
+
133
+ }
134
+
135
+ if (RSTRING(src)->len > (t + len - RSTRING(src)->ptr))
136
+ modified = true;
137
+ } else {
138
+ while (s < s_end) {
139
+ unichar c = utf_char(s);
140
+
141
+ const char *prev = s;
142
+ s = utf_next(s);
143
+
144
+ if (tr_table_lookup(translation, c)) {
145
+ len += unichar_to_utf(replace(c, closure),
146
+ (t != NULL) ? t + len : NULL);
147
+ modified = true;
148
+ } else {
149
+ if (t != NULL)
150
+ memcpy(t + len, prev, s - prev);
151
+ len += s - prev;
152
+ }
153
+ }
154
+ }
155
+
156
+ #ifdef RB_STR_REPLACE_IS_EXTERN
157
+ if (replace_content && !modified)
158
+ return Qnil;
159
+ #endif
160
+
161
+ if (dst == Qnil) {
162
+ #ifdef RB_STR_REPLACE_IS_EXTERN
163
+ if (replace_content && len <= RSTRING(src)->len)
164
+ dst = src;
165
+ else
166
+ #endif
167
+ dst = rb_str_buf_new(len);
168
+ goto again;
169
+ }
170
+
171
+ t[len] = '\0';
172
+ RSTRING(dst)->len = len;
173
+
174
+ #ifdef RB_STR_REPLACE_IS_EXTERN
175
+ if (dst != src && replace_content) {
176
+ rb_str_replace(src, dst);
177
+ return src;
178
+ }
179
+ #endif
180
+
181
+ return dst;
182
+ }
183
+
184
+ static VALUE
185
+ tr_trans(VALUE str, VALUE from, VALUE to, bool squeeze, bool replace_content)
186
+ {
187
+ StringValue(str);
188
+ StringValue(from);
189
+ StringValue(to);
190
+
191
+ if (RSTRING(str)->ptr == NULL || RSTRING(str)->len == 0)
192
+ return replace_content ? Qnil : str;
193
+
194
+ if (RSTRING(to)->len == 0)
195
+ return rb_utf_delete_bang(1, &from, str);
196
+
197
+ struct tr tr_from;
198
+ tr_init(&tr_from,
199
+ RSTRING(from)->ptr,
200
+ RSTRING(from)->ptr + RSTRING(from)->len);
201
+
202
+ struct tr tr_to;
203
+ tr_init(&tr_to,
204
+ RSTRING(to)->ptr,
205
+ RSTRING(to)->ptr + RSTRING(to)->len);
206
+
207
+ unsigned int translation[TR_TABLE_SIZE];
208
+ tr_setup_table(from, translation, true);
209
+
210
+ tr_init(&tr_from,
211
+ RSTRING(from)->ptr,
212
+ RSTRING(from)->ptr + RSTRING(from)->len);
213
+ if (tr_should_exclude(&tr_from)) {
214
+ /* This case is easy. Just include everything by default and
215
+ * exclude the rest as always. Replace characters found by the
216
+ * last character found in tr_to. */
217
+ while (tr_next(&tr_to) != TR_FINISHED)
218
+ ; /* We just need the last replacement character. */
219
+ return tr_trans_do(str, translation, tr_trans_replace_exclude,
220
+ &tr_to.now, squeeze, replace_content);
221
+ } else {
222
+ /* This case is hard. We need a full-fledged lookup of what
223
+ * character to translate to, not simply a check whether to
224
+ * include it or not. */
225
+ struct tr_trans_closure trans_closure;
226
+
227
+ struct tr_range from_ranges[utf_length_n(RSTRING(from)->ptr, RSTRING(from)->len)];
228
+ trans_closure.from = from_ranges;
229
+ trans_closure.n_from = tr_ranges_setup(&tr_from, from_ranges);
230
+
231
+ struct tr_range to_ranges[utf_length_n(RSTRING(to)->ptr, RSTRING(to)->len)];
232
+ trans_closure.to = to_ranges;
233
+ trans_closure.n_to = tr_ranges_setup(&tr_to, to_ranges);
234
+
235
+ return tr_trans_do(str, translation, tr_trans_replace_include,
236
+ &trans_closure, squeeze, replace_content);
237
+ }
238
+ }
239
+
240
+ VALUE
241
+ rb_utf_tr(UNUSED(VALUE self), VALUE str, VALUE from, VALUE to)
242
+ {
243
+ return tr_trans(str, from, to, false, false);
244
+ }
245
+
246
+ VALUE
247
+ rb_utf_tr_s(UNUSED(VALUE self), VALUE str, VALUE from, VALUE to)
248
+ {
249
+ return tr_trans(str, from, to, true, false);
250
+ }
@@ -0,0 +1,13 @@
1
+ /*
2
+ * contents: UTF8.upcase module function.
3
+ *
4
+ * Copyright © 2006 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #include "rb_includes.h"
8
+
9
+ VALUE
10
+ rb_utf_upcase(UNUSED(VALUE self), VALUE str)
11
+ {
12
+ return rb_utf_alloc_using(utf_upcase(StringValuePtr(str)));
13
+ }
@@ -0,0 +1,38 @@
1
+ /*
2
+ * contents: Functions for dealing with Unicode tables.
3
+ *
4
+ * Copyright © 2007 Nikolai Weibull <now@bitwi.se>
5
+ */
6
+
7
+ #ifndef TABLES_H
8
+ #define TABLES_H
9
+
10
+
11
+ /*
12
+ static inline int
13
+ split_unicode_table_lookup_page(const uint8_t data[][256], int16_t page, unichar c)
14
+ {
15
+ return (page >= UNICODE_MAX_TABLE_INDEX) ?
16
+ page - UNICODE_MAX_TABLE_INDEX :
17
+ data[page][c & 0xff];
18
+ }
19
+
20
+ static inline int
21
+ split_unicode_table_lookup(const uint8_t data[][256], const int16_t part1[], const int16_t part2[], unichar c, int fallback)
22
+ {
23
+ if (c <= UNICODE_LAST_CHAR_PART1)
24
+ return split_unicode_table_lookup_page(data,
25
+ part1[c >> 8],
26
+ c);
27
+
28
+ if (c >= UNICODE_FIRST_CHAR_PART2 && c <= UNICODE_LAST_CHAR)
29
+ return split_unicode_table_lookup_page(data,
30
+ part2[(c - UNICODE_FIRST_CHAR_PART2) >> 8],
31
+ c);
32
+
33
+ return fallback;
34
+ }
35
+ */
36
+
37
+
38
+ #endif /* TABLES_H */