Debian Codegraph @

include/ruby/internal/core/rmatch.h

raw
1 #ifndef RBIMPL_RMATCH_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_RMATCH_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Defines struct ::RMatch.
22 */
23 #include "ruby/internal/attr/artificial.h"
24 #include "ruby/internal/attr/pure.h"
25 #include "ruby/internal/cast.h"
26 #include "ruby/internal/core/rbasic.h"
27 #include "ruby/internal/value.h"
28 #include "ruby/internal/value_type.h"
29 #include "ruby/assert.h"
30
31 /**
32 * Convenient casting macro.
33 *
34 * @param obj An object, which is in fact an ::RMatch.
35 * @return The passed object casted to ::RMatch.
36 */
37 #define RMATCH(obj) RBIMPL_CAST((struct RMatch *)(obj))
38 /** @cond INTERNAL_MACRO */
39 #define RMATCH_REGS RMATCH_REGS
40 /** @endcond */
41
42 struct re_patter_buffer; /* a.k.a. OnigRegexType, defined in onigmo.h */
43 struct re_registers; /* Also in onigmo.h */
44
45 /**
46 * @old{re_pattern_buffer}
47 *
48 * @internal
49 *
50 * @shyouhei wonders: is anyone actively using this typedef ...?
51 */
52 typedef struct re_pattern_buffer Regexp;
53
54 /**
55 * Represents the region of a capture group. This is basically for caching
56 * purpose. re_registers have similar concepts (`beg` and `end`) but they are
57 * in `ptrdiff_t*`. In order for us to implement `MatchData#offset` that info
58 * has to be converted to offset integers. This is the struct to hold such
59 * things.
60 *
61 * @internal
62 *
63 * But why on earth it has to be visible from extension libraries?
64 */
65 struct rmatch_offset {
66 long beg; /**< Beginning of a group. */
67 long end; /**< End of a group. */
68 };
69
70 /** Represents a match. */
71 struct rb_matchext_struct {
72 /**
73 * "Registers" of a match. This is a quasi-opaque struct that holds
74 * execution result of a match. Roughly resembles `&~`.
75 */
76 struct re_registers regs;
77
78 /** Capture group offsets, in C array. */
79 struct rmatch_offset *char_offset;
80
81 /** Number of ::rmatch_offset that ::rmatch::char_offset holds. */
82 int char_offset_num_allocated;
83 };
84
85 typedef struct rb_matchext_struct rb_matchext_t;
86
87 /**
88 * Regular expression execution context. When a regular expression "matches"
89 * to a string, it generates capture groups etc. This struct holds that info.
90 * Visible from Ruby as an instance of `MatchData`.
91 *
92 * @note There is no way for extension libraries to manually generate this
93 * struct except by actually exercising the match operation of a regular
94 * expression.
95 */
96 struct RMatch {
97
98 /** Basic part, including flags and class. */
99 struct RBasic basic;
100
101 /**
102 * The target string that the match was made against.
103 */
104 VALUE str;
105
106 /**
107 * The expression of this match.
108 */
109 VALUE regexp; /* RRegexp */
110 };
111
112 #define RMATCH_EXT(m) ((rb_matchext_t *)((char *)(m) + sizeof(struct RMatch)))
113
114 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
115 RBIMPL_ATTR_ARTIFICIAL()
116 /**
117 * Queries the raw ::re_registers.
118 *
119 * @param[in] match A match object
120 * @pre `match` must be of ::RMatch.
121 * @return Its execution result.
122 * @note Good. So you are aware of the fact that it could return NULL.
123 * Yes. It actually does. This is a really bizarre thing. The
124 * situation is about `String#gsub` and its family. They take
125 * strings as arguments, like `"foo".sub("bar", "baz")`. On such
126 * situations, in order to optimise memory allocations, these
127 * methods do not involve regular expressions at all. They just
128 * sequentially scan the receiver. Okay. The story begins here.
129 * Even when they do not kick our regexp engine, there must be
130 * backref objects e.g. `$&`. But how? You know what? Ruby fakes
131 * them. It allocates an empty ::RMatch and behaves as if there
132 * were execution contexts. In reality there weren't. No
133 * ::re_registers are allocated then. There is no way for this
134 * function but to return NULL for those fake ::RMatch. This is
135 * the reason for the nullability of this function.
136 */
137 static inline struct re_registers *
138 RMATCH_REGS(VALUE match)
139 {
140 RBIMPL_ASSERT_TYPE(match, RUBY_T_MATCH);
141 return &RMATCH_EXT(match)->regs;
142 }
143
144 #endif /* RBIMPL_RMATCH_H */