summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb40
-rw-r--r--mrbgems/mruby-hash-ext/test/hash.rb20
-rw-r--r--src/load.c4
-rw-r--r--src/string.c117
-rw-r--r--test/t/exception.rb42
5 files changed, 142 insertions, 81 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 1ebc540e8..a5f04e5e1 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -120,4 +120,44 @@ 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
+
+ ##
+ # call-seq:
+ # hsh.keep_if {| key, value | block } -> hsh
+ # hsh.keep_if -> an_enumerator
+ #
+ # Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
+ # evaluates to false.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+
+ def keep_if(&block)
+ return to_enum :keep_if unless block_given?
+
+ keys = []
+ self.each do |k, v|
+ unless block.call([k, v])
+ self.delete(k)
+ end
+ end
+ self
+ end
end
diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb
index 7d8d66b4e..1334522ff 100644
--- a/mrbgems/mruby-hash-ext/test/hash.rb
+++ b/mrbgems/mruby-hash-ext/test/hash.rb
@@ -82,3 +82,23 @@ 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
+
+assert("Hash#keep_if") do
+ h = { 1 => 2, 3 => 4, 5 => 6 }
+ assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 })
+ h = { 1 => 2, 3 => 4, 5 => 6 }
+ assert_equal({ 1 => 2, 3=> 4, 5 =>6} , h.keep_if { true })
+end
diff --git a/src/load.c b/src/load.c
index c6aff4f9c..1142a6eaf 100644
--- a/src/load.c
+++ b/src/load.c
@@ -191,12 +191,10 @@ read_section_irep(mrb_state *mrb, const uint8_t *bin, mrb_bool alloc)
static int
read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t *len)
{
- int ret;
size_t i, fname_len, niseq;
char *fname;
uint16_t *lines;
- ret = MRB_DUMP_OK;
*len = 0;
bin += sizeof(uint32_t); /* record size */
*len += sizeof(uint32_t);
@@ -228,7 +226,7 @@ read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t
irep->filename = fname;
irep->lines = lines;
- return ret;
+ return MRB_DUMP_OK;
}
static int
diff --git a/src/string.c b/src/string.c
index aa4f6bed5..d5a849cec 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++;
}
@@ -1230,8 +1230,6 @@ mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
mrb_value
mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
{
- mrb_value str2;
-
if (len < 0) return mrb_nil_value();
if (!RSTRING_LEN(str)) {
len = 0;
@@ -1246,9 +1244,7 @@ mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
if (len <= 0) {
len = 0;
}
- str2 = mrb_str_subseq(mrb, str, beg, len);
-
- return str2;
+ return mrb_str_subseq(mrb, str, beg, len);
}
mrb_int
@@ -1264,8 +1260,7 @@ mrb_str_hash(mrb_state *mrb, mrb_value str)
key = key*65599 + *p;
p++;
}
- key = key + (key>>5);
- return key;
+ return key + (key>>5);
}
/* 15.2.10.5.20 */
@@ -1851,7 +1846,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 +1859,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 +2280,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 +2288,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++;
}
@@ -2484,54 +2479,54 @@ mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2)
mrb_value
mrb_str_inspect(mrb_state *mrb, mrb_value str)
{
- const char *p, *pend;
- char buf[CHAR_ESC_LEN + 1];
- mrb_value result = mrb_str_new_lit(mrb, "\"");
+ const char *p, *pend;
+ char buf[CHAR_ESC_LEN + 1];
+ mrb_value result = mrb_str_new_lit(mrb, "\"");
- p = RSTRING_PTR(str); pend = RSTRING_END(str);
- for (;p < pend; p++) {
- unsigned char c, cc;
+ p = RSTRING_PTR(str); pend = RSTRING_END(str);
+ for (;p < pend; p++) {
+ unsigned char c, cc;
- c = *p;
- if (c == '"'|| c == '\\' || (c == '#' && IS_EVSTR(p, pend))) {
- buf[0] = '\\'; buf[1] = c;
- mrb_str_cat(mrb, result, buf, 2);
- continue;
- }
- if (ISPRINT(c)) {
- buf[0] = c;
- mrb_str_cat(mrb, result, buf, 1);
- continue;
- }
- switch (c) {
- case '\n': cc = 'n'; break;
- case '\r': cc = 'r'; break;
- case '\t': cc = 't'; break;
- case '\f': cc = 'f'; break;
- case '\013': cc = 'v'; break;
- case '\010': cc = 'b'; break;
- case '\007': cc = 'a'; break;
- case 033: cc = 'e'; break;
- default: cc = 0; break;
- }
- if (cc) {
- buf[0] = '\\';
- buf[1] = (char)cc;
- mrb_str_cat(mrb, result, buf, 2);
- continue;
- }
- else {
- buf[0] = '\\';
- buf[3] = '0' + c % 8; c /= 8;
- buf[2] = '0' + c % 8; c /= 8;
- buf[1] = '0' + c % 8;
- mrb_str_cat(mrb, result, buf, 4);
- continue;
- }
+ c = *p;
+ if (c == '"'|| c == '\\' || (c == '#' && IS_EVSTR(p, pend))) {
+ buf[0] = '\\'; buf[1] = c;
+ mrb_str_cat(mrb, result, buf, 2);
+ continue;
+ }
+ if (ISPRINT(c)) {
+ buf[0] = c;
+ mrb_str_cat(mrb, result, buf, 1);
+ continue;
}
- mrb_str_cat_lit(mrb, result, "\"");
+ switch (c) {
+ case '\n': cc = 'n'; break;
+ case '\r': cc = 'r'; break;
+ case '\t': cc = 't'; break;
+ case '\f': cc = 'f'; break;
+ case '\013': cc = 'v'; break;
+ case '\010': cc = 'b'; break;
+ case '\007': cc = 'a'; break;
+ case 033: cc = 'e'; break;
+ default: cc = 0; break;
+ }
+ if (cc) {
+ buf[0] = '\\';
+ buf[1] = (char)cc;
+ mrb_str_cat(mrb, result, buf, 2);
+ continue;
+ }
+ else {
+ buf[0] = '\\';
+ buf[3] = '0' + c % 8; c /= 8;
+ buf[2] = '0' + c % 8; c /= 8;
+ buf[1] = '0' + c % 8;
+ mrb_str_cat(mrb, result, buf, 4);
+ continue;
+ }
+ }
+ mrb_str_cat_lit(mrb, result, "\"");
- return result;
+ return result;
}
/*
diff --git a/test/t/exception.rb b/test/t/exception.rb
index 8099e911f..0ce742527 100644
--- a/test/t/exception.rb
+++ b/test/t/exception.rb
@@ -57,15 +57,16 @@ end
# Not ISO specified
assert('Exception 1') do
- begin
+r=begin
1+1
ensure
2+2
- end == 2
+ end
+ assert_equal 2, r
end
assert('Exception 2') do
- begin
+r=begin
1+1
begin
2+2
@@ -74,11 +75,12 @@ assert('Exception 2') do
end
ensure
4+4
- end == 4
+ end
+ assert_equal 4, r
end
assert('Exception 3') do
- begin
+r=begin
1+1
begin
2+2
@@ -92,7 +94,8 @@ assert('Exception 3') do
ensure
6+6
end
- end == 4
+ end
+ assert_equal 4, r
end
assert('Exception 4') do
@@ -171,17 +174,18 @@ assert('Exception 7') do
end
assert('Exception 8') do
- begin
+r=begin
1
rescue
2
else
3
- end == 3
+ end
+ assert_equal 3, r
end
assert('Exception 9') do
- begin
+r=begin
1+1
rescue
2+2
@@ -189,11 +193,12 @@ assert('Exception 9') do
3+3
ensure
4+4
- end == 6
+ end
+ assert_equal 6, r
end
assert('Exception 10') do
- begin
+r=begin
1+1
begin
2+2
@@ -208,7 +213,8 @@ assert('Exception 10') do
6+6
ensure
7+7
- end == 12
+ end
+ assert_equal 12, r
end
assert('Exception 11') do
@@ -273,12 +279,12 @@ assert('Exception 16') do
raise "foo"
false
rescue => e
- e.message == "foo"
+ assert_equal "foo", e.message
end
end
assert('Exception 17') do
- begin
+r=begin
raise "a" # StandardError
rescue ArgumentError
1
@@ -288,11 +294,12 @@ assert('Exception 17') do
3
ensure
4
- end == 2
+ end
+ assert_equal 2, r
end
assert('Exception 18') do
- begin
+r=begin
0
rescue ArgumentError
1
@@ -302,7 +309,8 @@ assert('Exception 18') do
3
ensure
4
- end == 3
+ end
+ assert_equal 3, r
end
assert('Exception 19') do