Class: RE2::Set
Class Method Summary collapse
-
.match_raises_errors? ⇒ Boolean
Returns whether the underlying RE2 version outputs error information from
RE2::Set::Match
. -
.size? ⇒ Boolean
Returns whether the underlying RE2 version has a Set::Size method.
Instance Method Summary collapse
-
#add(pattern) ⇒ Integer
Adds a pattern to the set.
-
#compile ⇒ Boolean
Compiles a Set so it can be used to match against.
-
#initialize(*args) ⇒ RE2::Set
constructor
Returns a new Set object, a collection of patterns that can be searched for simultaneously.
-
#length ⇒ Integer
Returns the size of the Set.
-
#match(*args) ⇒ Array<Integer>
Matches the given text against patterns in the set, returning an array of integer indices of the matching patterns if matched or an empty array if there are no matches.
-
#size ⇒ Integer
Returns the size of the Set.
Constructor Details
#initialize ⇒ RE2::Set #initialize(anchor) ⇒ RE2::Set #initialize(anchor, options) ⇒ RE2::Set
Returns a new RE2::Set object, a collection of patterns that can be searched for simultaneously.
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 |
# File 'ext/re2/re2.cc', line 1872
static VALUE re2_set_initialize(int argc, VALUE *argv, VALUE self) {
VALUE anchor, options;
re2_set *s;
rb_scan_args(argc, argv, "02", &anchor, &options);
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
RE2::Anchor re2_anchor = RE2::UNANCHORED;
if (!NIL_P(anchor)) {
Check_Type(anchor, T_SYMBOL);
ID id_anchor_arg = SYM2ID(anchor);
if (id_anchor_arg == id_unanchored) {
re2_anchor = RE2::UNANCHORED;
} else if (id_anchor_arg == id_anchor_start) {
re2_anchor = RE2::ANCHOR_START;
} else if (id_anchor_arg == id_anchor_both) {
re2_anchor = RE2::ANCHOR_BOTH;
} else {
rb_raise(rb_eArgError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both");
}
}
RE2::Options re2_options;
if (RTEST(options)) {
parse_re2_options(&re2_options, options);
}
s->set = new(std::nothrow) RE2::Set(re2_options, re2_anchor);
if (s->set == 0) {
rb_raise(rb_eNoMemError, "not enough memory to allocate RE2::Set object");
}
return self;
}
|
Class Method Details
.match_raises_errors? ⇒ Boolean
Returns whether the underlying RE2 version outputs error information from
RE2::Set::Match
. If not, #match will raise an error if attempting to set
its :exception
option to true
.
1993 1994 1995 1996 1997 1998 1999 |
# File 'ext/re2/re2.cc', line 1993 static VALUE re2_set_match_raises_errors_p(VALUE) { #ifdef HAVE_ERROR_INFO_ARGUMENT return Qtrue; #else return Qfalse; #endif } |
.size? ⇒ Boolean
Returns whether the underlying RE2 version has a Set::Size method.
2006 2007 2008 2009 2010 2011 2012 |
# File 'ext/re2/re2.cc', line 2006 static VALUE re2_set_size_p(VALUE) { #ifdef HAVE_SET_SIZE return Qtrue; #else return Qfalse; #endif } |
Instance Method Details
#add(pattern) ⇒ Integer
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 |
# File 'ext/re2/re2.cc', line 1922
static VALUE re2_set_add(VALUE self, VALUE pattern) {
StringValue(pattern);
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
/* To prevent the memory of the err string leaking when we call rb_raise,
* take a copy of it and let it go out of scope.
*/
char msg[100];
int index;
{
std::string err;
index = s->set->Add(
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)), &err);
strlcpy(msg, err.c_str(), sizeof(msg));
}
if (index < 0) {
rb_raise(rb_eArgError, "str rejected by RE2::Set->Add(): %s", msg);
}
return INT2FIX(index);
}
|
#compile ⇒ Boolean
1958 1959 1960 1961 1962 1963 |
# File 'ext/re2/re2.cc', line 1958
static VALUE re2_set_compile(VALUE self) {
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
return BOOL2RUBY(s->set->Compile());
}
|
#length ⇒ Integer
Returns the size of the RE2::Set.
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 |
# File 'ext/re2/re2.cc', line 1974
static VALUE re2_set_size(VALUE self) {
#ifdef HAVE_SET_SIZE
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
return INT2FIX(s->set->Size());
#else
rb_raise(re2_eSetUnsupportedError, "current version of RE2::Set does not have Size method");
#endif
}
|
#match(str) ⇒ Array<Integer> #match(str, options) ⇒ Array<Integer>
Matches the given text against patterns in the set, returning an array of integer indices of the matching patterns if matched or an empty array if there are no matches.
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 |
# File 'ext/re2/re2.cc', line 2054
static VALUE re2_set_match(int argc, VALUE *argv, const VALUE self) {
VALUE str, options;
bool raise_exception = true;
rb_scan_args(argc, argv, "11", &str, &options);
StringValue(str);
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
if (RTEST(options)) {
Check_Type(options, T_HASH);
VALUE exception_option = rb_hash_aref(options, ID2SYM(id_exception));
if (!NIL_P(exception_option)) {
raise_exception = RTEST(exception_option);
}
}
std::vector<int> v;
if (raise_exception) {
#ifdef HAVE_ERROR_INFO_ARGUMENT
RE2::Set::ErrorInfo e;
bool match_failed = !s->set->Match(
re2::StringPiece(RSTRING_PTR(str), RSTRING_LEN(str)), &v, &e);
VALUE result = rb_ary_new2(v.size());
if (match_failed) {
switch (e.kind) {
case RE2::Set::kNoError:
break;
case RE2::Set::kNotCompiled:
rb_raise(re2_eSetMatchError, "#match must not be called before #compile");
case RE2::Set::kOutOfMemory:
rb_raise(re2_eSetMatchError, "The DFA ran out of memory");
case RE2::Set::kInconsistent:
rb_raise(re2_eSetMatchError, "RE2::Prog internal error");
default: // Just in case a future version of libre2 adds new ErrorKinds
rb_raise(re2_eSetMatchError, "Unknown RE2::Set::ErrorKind: %d", e.kind);
}
} else {
for (std::vector<int>::size_type i = 0; i < v.size(); ++i) {
rb_ary_push(result, INT2FIX(v[i]));
}
}
return result;
#else
rb_raise(re2_eSetUnsupportedError, "current version of RE2::Set::Match() does not output error information, :exception option can only be set to false");
#endif
} else {
bool matched = s->set->Match(
re2::StringPiece(RSTRING_PTR(str), RSTRING_LEN(str)), &v);
VALUE result = rb_ary_new2(v.size());
if (matched) {
for (std::vector<int>::size_type i = 0; i < v.size(); ++i) {
rb_ary_push(result, INT2FIX(v[i]));
}
}
return result;
}
}
|
#size ⇒ Integer
Returns the size of the RE2::Set.
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 |
# File 'ext/re2/re2.cc', line 1974
static VALUE re2_set_size(VALUE self) {
#ifdef HAVE_SET_SIZE
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
return INT2FIX(s->set->Size());
#else
rb_raise(re2_eSetUnsupportedError, "current version of RE2::Set does not have Size method");
#endif
}
|