diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-01-08 13:05:27 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-01-08 13:05:27 +0900 |
| commit | 3046cc7bd8bdf470dbe8eec2de4c7a1caedcd352 (patch) | |
| tree | ccf95a1fce4afea8d9d97684e79cca5a864672ae /mrbgems | |
| parent | c150f115a162c6f44d0866dba2cea91572a0ea98 (diff) | |
| parent | 2040347d26a979898e6d1454c0780df0eead705e (diff) | |
| download | mruby-3046cc7bd8bdf470dbe8eec2de4c7a1caedcd352.tar.gz mruby-3046cc7bd8bdf470dbe8eec2de4c7a1caedcd352.zip | |
Merge branch 'master' of github.com:mruby/mruby
Diffstat (limited to 'mrbgems')
| -rw-r--r-- | mrbgems/mruby-bin-mirb/tools/mirb/mirb.c | 14 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-mruby/tools/mruby/mruby.c | 8 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/src/hash-ext.c | 3 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/src/numeric_ext.c | 1 | ||||
| -rw-r--r-- | mrbgems/mruby-random/src/mt19937ar.c | 1 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-string-utf8/src/string.c | 34 | ||||
| -rw-r--r-- | mrbgems/mruby-string-utf8/test/string.rb | 16 | ||||
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 1 |
9 files changed, 60 insertions, 22 deletions
diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c index 81eef7f82..5505e8e5e 100644 --- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c +++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c @@ -8,21 +8,17 @@ #include <stdlib.h> #include <string.h> - -#include <mruby.h> +#include "mruby.h" #include "mruby/array.h" -#include <mruby/proc.h> -#include <mruby/data.h> -#include <mruby/compile.h> +#include "mruby/proc.h" +#include "mruby/compile.h" +#include "mruby/string.h" + #ifdef ENABLE_READLINE #include <limits.h> #include <readline/readline.h> #include <readline/history.h> -#endif -#include <mruby/string.h> - -#ifdef ENABLE_READLINE static const char *history_file_name = ".mirb_history"; char history_path[PATH_MAX]; #endif diff --git a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c index ef0ac4978..54b2f8b65 100644 --- a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +++ b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c @@ -1,13 +1,11 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include "mruby.h" -#include "mruby/proc.h" #include "mruby/array.h" -#include "mruby/string.h" #include "mruby/compile.h" #include "mruby/dump.h" #include "mruby/variable.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #ifndef ENABLE_STDIO static void diff --git a/mrbgems/mruby-hash-ext/src/hash-ext.c b/mrbgems/mruby-hash-ext/src/hash-ext.c index 94518924e..c6e72f451 100644 --- a/mrbgems/mruby-hash-ext/src/hash-ext.c +++ b/mrbgems/mruby-hash-ext/src/hash-ext.c @@ -6,11 +6,8 @@ #include "mruby.h" #include "mruby/array.h" -#include "mruby/class.h" #include "mruby/hash.h" #include "mruby/khash.h" -#include "mruby/string.h" -#include "mruby/variable.h" /* * call-seq: diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c index bb6955b45..22c1668fa 100644 --- a/mrbgems/mruby-numeric-ext/src/numeric_ext.c +++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c @@ -1,6 +1,5 @@ #include <limits.h> #include "mruby.h" -#include "mruby/numeric.h" static mrb_value mrb_int_chr(mrb_state *mrb, mrb_value x) diff --git a/mrbgems/mruby-random/src/mt19937ar.c b/mrbgems/mruby-random/src/mt19937ar.c index 3246909e8..41ed2c00d 100644 --- a/mrbgems/mruby-random/src/mt19937ar.c +++ b/mrbgems/mruby-random/src/mt19937ar.c @@ -4,7 +4,6 @@ ** See Copyright Notice in mruby.h */ -#include <stdio.h> #include "mt19937ar.h" /* Period parameters */ diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 6718e734a..f194bbc0e 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -1,7 +1,7 @@ -#include "mruby.h" -#include "mruby/string.h" #include <ctype.h> #include <string.h> +#include "mruby.h" +#include "mruby/string.h" static mrb_value mrb_str_getbyte(mrb_state *mrb, mrb_value str) diff --git a/mrbgems/mruby-string-utf8/src/string.c b/mrbgems/mruby-string-utf8/src/string.c index 5379dd3aa..c377726a2 100644 --- a/mrbgems/mruby-string-utf8/src/string.c +++ b/mrbgems/mruby-string-utf8/src/string.c @@ -247,6 +247,38 @@ mrb_str_aref_m(mrb_state *mrb, mrb_value str) return mrb_str_aref(mrb, str, a1); } +static mrb_value +mrb_str_reverse_bang(mrb_state *mrb, mrb_value str) +{ + int utf8_len = mrb_utf8_strlen(str); + if (utf8_len > 1) { + int len = RSTRING_LEN(str); + char *buf = (char *)mrb_malloc(mrb, len); + unsigned char* p = (unsigned char*)buf; + unsigned char* e = (unsigned char*)buf + len; + unsigned char* r = (unsigned char*)RSTRING_END(str); + + memcpy(buf, RSTRING_PTR(str), len); + mrb_str_modify(mrb, mrb_str_ptr(str)); + + while (p<e) { + int clen = utf8len(p); + r -= clen; + memcpy(r, p, clen); + p += clen; + } + mrb_free(mrb, buf); + } + + return str; +} + +static mrb_value +mrb_str_reverse(mrb_state *mrb, mrb_value str) +{ + return mrb_str_reverse_bang(mrb, mrb_str_dup(mrb, str)); +} + void mrb_mruby_string_utf8_gem_init(mrb_state* mrb) { @@ -255,6 +287,8 @@ mrb_mruby_string_utf8_gem_init(mrb_state* mrb) mrb_define_method(mrb, s, "size", mrb_str_size, MRB_ARGS_NONE()); mrb_define_method(mrb, s, "[]", mrb_str_aref_m, MRB_ARGS_ANY()); mrb_define_method(mrb, s, "slice", mrb_str_aref_m, MRB_ARGS_ANY()); + mrb_define_method(mrb, s, "reverse", mrb_str_reverse, MRB_ARGS_NONE()); + mrb_define_method(mrb, s, "reverse!", mrb_str_reverse_bang, MRB_ARGS_NONE()); } void diff --git a/mrbgems/mruby-string-utf8/test/string.rb b/mrbgems/mruby-string-utf8/test/string.rb index 4ee815a36..fc21c0a93 100644 --- a/mrbgems/mruby-string-utf8/test/string.rb +++ b/mrbgems/mruby-string-utf8/test/string.rb @@ -9,3 +9,19 @@ assert('String#[]') do assert_equal "んに", "こんにちわ世界"[1,2] assert_equal "世", "こんにちわ世界"["世"] end + +assert('String#reverse', '15.2.10.5.29') do + a = 'こんにちわ世界!' + a.reverse + + assert_equal 'こんにちわ世界!', a + assert_equal '!界世わちにんこ', 'こんにちわ世界!'.reverse +end + +assert('String#reverse!', '15.2.10.5.30') do + a = 'こんにちわ世界!' + a.reverse! + + assert_equal '!界世わちにんこ', a + assert_equal '!界世わちにんこ', 'こんにちわ世界!'.reverse! +end diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 309684422..e6d3b0796 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -10,7 +10,6 @@ #include "mruby/array.h" #include "mruby/string.h" #include "mruby/class.h" -#include "mruby/data.h" #include "mruby/variable.h" #define RSTRUCT_ARY(st) mrb_ary_ptr(st) |
