diff options
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrblib/hash.rb | 17 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/test/hash.rb | 13 | ||||
| -rw-r--r-- | src/string.c | 20 |
3 files changed, 40 insertions, 10 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 1ebc540e8..48bade330 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -120,4 +120,21 @@ class Hash def flatten(level=1) self.to_a.flatten(level) end + + ## + # call-seq: + # hsh.invert -> new_hash + # + # Returns a new hash created by using <i>hsh</i>'s values as keys, and + # the keys as values. + # + # h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } + # h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"} + # + + def invert + h = Hash.new + self.each {|k, v| h[v] = k } + h + end end diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index 7d8d66b4e..10880666e 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -82,3 +82,16 @@ assert("Hash#flatten") do assert_equal [1, "one", 2, 2, "two", 3, 3, ["three"]], a.flatten(2) assert_equal [1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(3) end + +assert("Hash#invert") do + h = { 1 => 'one', 2 => 'two', 3 => 'three', + true => 'true', nil => 'nil' }.invert + assert_equal 1, h['one'] + assert_equal true, h['true'] + assert_equal nil, h['nil'] + + h = { 'a' => 1, 'b' => 2, 'c' => 1 }.invert + assert_equal(2, h.length) + assert_include(%w[a c], h[1]) + assert_equal('b', h[2]) +end diff --git a/src/string.c b/src/string.c index b1ecf68e6..3876bc569 100644 --- a/src/string.c +++ b/src/string.c @@ -914,7 +914,7 @@ static mrb_value mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str) { char *p, *pend; - int modify = 0; + mrb_bool modify = FALSE; struct RString *s = mrb_str_ptr(str); mrb_str_modify(mrb, s); @@ -922,12 +922,12 @@ mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str) p = STR_PTR(s); pend = STR_PTR(s) + STR_LEN(s); if (ISLOWER(*p)) { *p = TOUPPER(*p); - modify = 1; + modify = TRUE; } while (++p < pend) { if (ISUPPER(*p)) { *p = TOLOWER(*p); - modify = 1; + modify = TRUE; } } if (modify) return str; @@ -1128,7 +1128,7 @@ static mrb_value mrb_str_downcase_bang(mrb_state *mrb, mrb_value str) { char *p, *pend; - int modify = 0; + mrb_bool modify = FALSE; struct RString *s = mrb_str_ptr(str); mrb_str_modify(mrb, s); @@ -1137,7 +1137,7 @@ mrb_str_downcase_bang(mrb_state *mrb, mrb_value str) while (p < pend) { if (ISUPPER(*p)) { *p = TOLOWER(*p); - modify = 1; + modify = TRUE; } p++; } @@ -1851,7 +1851,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) char *ptr = RSTRING_PTR(str); char *eptr = RSTRING_END(str); char *bptr = ptr; - int skip = 1; + mrb_bool skip = TRUE; unsigned int c; end = beg; @@ -1864,14 +1864,14 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) } else { end = ptr - bptr; - skip = 0; + skip = FALSE; if (lim_p && lim <= i) break; } } else if (ascii_isspace(c)) { mrb_ary_push(mrb, result, mrb_str_subseq(mrb, str, beg, end-beg)); mrb_gc_arena_restore(mrb, ai); - skip = 1; + skip = TRUE; beg = ptr - bptr; if (lim_p) ++i; } @@ -2285,7 +2285,7 @@ mrb_str_upcase_bang(mrb_state *mrb, mrb_value str) { struct RString *s = mrb_str_ptr(str); char *p, *pend; - int modify = 0; + mrb_bool modify = FALSE; mrb_str_modify(mrb, s); p = RSTRING_PTR(str); @@ -2293,7 +2293,7 @@ mrb_str_upcase_bang(mrb_state *mrb, mrb_value str) while (p < pend) { if (ISLOWER(*p)) { *p = TOUPPER(*p); - modify = 1; + modify = TRUE; } p++; } |
