Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-numeric-ext / src / numeric_ext.c
1 #include <limits.h>
2 #include <mruby.h>
3
4 static inline mrb_int
5 to_int(mrb_state *mrb, mrb_value x)
6 {
7   x = mrb_to_int(mrb, x);
8   return mrb_fixnum(x);
9 }
10
11 /*
12  *  Document-method: Integer#chr
13  *  call-seq:
14  *     int.chr  ->  string
15  *
16  *  Returns a string containing the character represented by the +int+'s value
17  *  according to +encoding+.
18  *
19  *     65.chr    #=> "A"
20  *     230.chr   #=> "\xE6"
21  */
22 static mrb_value
23 mrb_int_chr(mrb_state *mrb, mrb_value x)
24 {
25   mrb_int chr;
26   char c;
27
28   chr = to_int(mrb, x);
29   if (chr >= (1 << CHAR_BIT)) {
30     mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", x);
31   }
32   c = (char)chr;
33
34   return mrb_str_new(mrb, &c, 1);
35 }
36
37 /*
38  *  call-seq:
39  *     int.allbits?(mask)  ->  true or false
40  *
41  *  Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
42  */
43 static mrb_value
44 mrb_int_allbits(mrb_state *mrb, mrb_value self)
45 {
46   mrb_int n, m;
47
48   mrb_get_args(mrb, "i", &m);
49   n = to_int(mrb, self);
50   return mrb_bool_value((n & m) == m);
51 }
52
53 /*
54  *  call-seq:
55  *     int.anybits?(mask)  ->  true or false
56  *
57  *  Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
58  */
59 static mrb_value
60 mrb_int_anybits(mrb_state *mrb, mrb_value self)
61 {
62   mrb_int n, m;
63
64   mrb_get_args(mrb, "i", &m);
65   n = to_int(mrb, self);
66   return mrb_bool_value((n & m) != 0);
67 }
68
69 /*
70  *  call-seq:
71  *     int.nobits?(mask)  ->  true or false
72  *
73  *  Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
74  */
75 static mrb_value
76 mrb_int_nobits(mrb_state *mrb, mrb_value self)
77 {
78   mrb_int n, m;
79
80   mrb_get_args(mrb, "i", &m);
81   n = to_int(mrb, self);
82   return mrb_bool_value((n & m) == 0);
83 }
84
85 void
86 mrb_mruby_numeric_ext_gem_init(mrb_state* mrb)
87 {
88   struct RClass *i = mrb_module_get(mrb, "Integral");
89
90   mrb_define_method(mrb, i, "chr", mrb_int_chr, MRB_ARGS_NONE());
91   mrb_define_method(mrb, i, "allbits?", mrb_int_allbits, MRB_ARGS_REQ(1));
92   mrb_define_method(mrb, i, "anybits?", mrb_int_anybits, MRB_ARGS_REQ(1));
93   mrb_define_method(mrb, i, "nobits?", mrb_int_nobits, MRB_ARGS_REQ(1));
94 }
95
96 void
97 mrb_mruby_numeric_ext_gem_final(mrb_state* mrb)
98 {
99 }