Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
array.c
Go to the documentation of this file.
1 #include "mruby.h"
2 #include "mruby/value.h"
3 #include "mruby/array.h"
4 
5 /*
6  * call-seq:
7  * Array.try_convert(obj) -> array or nil
8  *
9  * Try to convert <i>obj</i> into an array, using +to_ary+ method.
10  * Returns converted array or +nil+ if <i>obj</i> cannot be converted
11  * for any reason. This method can be used to check if an argument is an
12  * array.
13  *
14  * Array.try_convert([1]) #=> [1]
15  * Array.try_convert("1") #=> nil
16  *
17  * if tmp = Array.try_convert(arg)
18  * # the argument is an array
19  * elsif tmp = String.try_convert(arg)
20  * # the argument is a string
21  * end
22  *
23  */
24 
25 static mrb_value
26 mrb_ary_s_try_convert(mrb_state *mrb, mrb_value self)
27 {
28  mrb_value ary;
29 
30  mrb_get_args(mrb, "o", &ary);
31  return mrb_check_array_type(mrb, ary);
32 }
33 
34 /*
35  * call-seq:
36  * ary.assoc(obj) -> new_ary or nil
37  *
38  * Searches through an array whose elements are also arrays
39  * comparing _obj_ with the first element of each contained array
40  * using obj.==.
41  * Returns the first contained array that matches (that
42  * is, the first associated array),
43  * or +nil+ if no match is found.
44  * See also <code>Array#rassoc</code>.
45  *
46  * s1 = [ "colors", "red", "blue", "green" ]
47  * s2 = [ "letters", "a", "b", "c" ]
48  * s3 = "foo"
49  * a = [ s1, s2, s3 ]
50  * a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
51  * a.assoc("foo") #=> nil
52  */
53 
54 static mrb_value
55 mrb_ary_assoc(mrb_state *mrb, mrb_value ary)
56 {
57  mrb_int i;
58  mrb_value v, k;
59 
60  mrb_get_args(mrb, "o", &k);
61 
62  for (i = 0; i < RARRAY_LEN(ary); ++i) {
63  v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]);
64  if (!mrb_nil_p(v) && RARRAY_LEN(v) > 0 &&
65  mrb_equal(mrb, RARRAY_PTR(v)[0], k))
66  return v;
67  }
68  return mrb_nil_value();
69 }
70 
71 /*
72  * call-seq:
73  * ary.rassoc(obj) -> new_ary or nil
74  *
75  * Searches through the array whose elements are also arrays. Compares
76  * _obj_ with the second element of each contained array using
77  * <code>==</code>. Returns the first contained array that matches. See
78  * also <code>Array#assoc</code>.
79  *
80  * a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
81  * a.rassoc("two") #=> [2, "two"]
82  * a.rassoc("four") #=> nil
83  */
84 
85 static mrb_value
86 mrb_ary_rassoc(mrb_state *mrb, mrb_value ary)
87 {
88  mrb_int i;
89  mrb_value v, value;
90 
91  mrb_get_args(mrb, "o", &value);
92 
93  for (i = 0; i < RARRAY_LEN(ary); ++i) {
94  v = RARRAY_PTR(ary)[i];
95  if (mrb_type(v) == MRB_TT_ARRAY &&
96  RARRAY_LEN(v) > 1 &&
97  mrb_equal(mrb, RARRAY_PTR(v)[1], value))
98  return v;
99  }
100  return mrb_nil_value();
101 }
102 
103 /*
104  * call-seq:
105  * ary.at(index) -> obj or nil
106  *
107  * Returns the element at _index_. A
108  * negative index counts from the end of +self+. Returns +nil+
109  * if the index is out of range. See also <code>Array#[]</code>.
110  *
111  * a = [ "a", "b", "c", "d", "e" ]
112  * a.at(0) #=> "a"
113  * a.at(-1) #=> "e"
114  */
115 
116 static mrb_value
117 mrb_ary_at(mrb_state *mrb, mrb_value ary)
118 {
119  mrb_int pos;
120  mrb_get_args(mrb, "i", &pos);
121 
122  return mrb_ary_entry(ary, pos);
123 }
124 
125 void
127 {
128  struct RClass * a = mrb->array_class;
129 
130  mrb_define_class_method(mrb, a, "try_convert", mrb_ary_s_try_convert, MRB_ARGS_REQ(1));
131 
132  mrb_define_method(mrb, a, "assoc", mrb_ary_assoc, MRB_ARGS_REQ(1));
133  mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1));
134  mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
135 }
136 
137 void
139 {
140 }