1 /** 2 ** @file mruby/range.h - Range class 3 ** 4 ** See Copyright Notice in mruby.h 5 */ 6 7 #ifndef MRUBY_RANGE_H 8 #define MRUBY_RANGE_H 9 10 #include "common.h" 11 12 /** 13 * Range class 14 */ 15 MRB_BEGIN_DECL 16 17 #if defined(MRB_NAN_BOXING) && defined(MRB_64BIT) || defined(MRB_WORD_BOXING) 18 # define MRB_RANGE_EMBED 19 #endif 20 21 #ifdef MRB_RANGE_EMBED 22 struct RRange { 23 MRB_OBJECT_HEADER; 24 mrb_value beg; 25 mrb_value end; 26 mrb_bool excl; 27 }; 28 # define mrb_gc_free_range(mrb, p) ((void)0) 29 # define RANGE_BEG(p) ((p)->beg) 30 # define RANGE_END(p) ((p)->end) 31 #else 32 typedef struct mrb_range_edges { 33 mrb_value beg; 34 mrb_value end; 35 } mrb_range_edges; 36 struct RRange { 37 MRB_OBJECT_HEADER; 38 mrb_range_edges *edges; 39 mrb_bool excl; 40 }; 41 # define mrb_gc_free_range(mrb, p) mrb_free(mrb, (p)->edges) 42 # define RANGE_BEG(p) ((p)->edges->beg) 43 # define RANGE_END(p) ((p)->edges->end) 44 #endif 45 46 #define mrb_range_beg(mrb, r) RANGE_BEG(mrb_range_ptr(mrb, r)) 47 #define mrb_range_end(mrb, r) RANGE_END(mrb_range_ptr(mrb, r)) 48 #define mrb_range_excl_p(mrb, r) RANGE_EXCL(mrb_range_ptr(mrb, r)) 49 #define mrb_range_raw_ptr(r) ((struct RRange*)mrb_ptr(r)) 50 #define mrb_range_value(p) mrb_obj_value((void*)(p)) 51 #define RANGE_EXCL(p) ((p)->excl) 52 53 MRB_API struct RRange* mrb_range_ptr(mrb_state *mrb, mrb_value range); 54 55 /* 56 * Initializes a Range. 57 * 58 * If the third parameter is FALSE then it includes the last value in the range. 59 * If the third parameter is TRUE then it excludes the last value in the range. 60 * 61 * @param start the beginning value. 62 * @param end the ending value. 63 * @param exclude represents the inclusion or exclusion of the last value. 64 */ 65 MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude); 66 67 enum mrb_range_beg_len { 68 MRB_RANGE_TYPE_MISMATCH = 0, /* (failure) not range */ 69 MRB_RANGE_OK = 1, /* (success) range */ 70 MRB_RANGE_OUT = 2 /* (failure) out of range */ 71 }; 72 73 MRB_API enum mrb_range_beg_len mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc); 74 mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int)); 75 void mrb_gc_mark_range(mrb_state *mrb, struct RRange *r); 76 77 MRB_END_DECL 78 79 #endif /* MRUBY_RANGE_H */ 80