summaryrefslogtreecommitdiff
path: root/ext/strscan/strscan.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/strscan/strscan.c')
-rw-r--r--ext/strscan/strscan.c215
1 files changed, 104 insertions, 111 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index bc543f62b1..7e21a21f7e 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -22,7 +22,15 @@ extern size_t onig_region_memsize(const struct re_registers *regs);
#include <stdbool.h>
-#define STRSCAN_VERSION "3.1.6.dev"
+#define STRSCAN_VERSION "3.1.9.dev"
+
+
+#ifdef HAVE_RB_DEPRECATE_CONSTANT
+/* In ruby 3.0, defined but exposed in external headers */
+extern void rb_deprecate_constant(VALUE mod, const char *name);
+#else
+# define rb_deprecate_constant(mod, name) ((void)0)
+#endif
/* =======================================================================
Data Type Definitions
@@ -30,7 +38,6 @@ extern size_t onig_region_memsize(const struct re_registers *regs);
static VALUE StringScanner;
static VALUE ScanError;
-static ID id_byteslice;
static int usascii_encindex, utf8_encindex, binary_encindex;
@@ -97,7 +104,6 @@ static VALUE strscan_init_copy _((VALUE vself, VALUE vorig));
static VALUE strscan_s_mustc _((VALUE self));
static VALUE strscan_terminate _((VALUE self));
-static VALUE strscan_clear _((VALUE self));
static VALUE strscan_get_string _((VALUE self));
static VALUE strscan_set_string _((VALUE self, VALUE str));
static VALUE strscan_concat _((VALUE self, VALUE str));
@@ -119,14 +125,11 @@ static VALUE strscan_search_full _((VALUE self, VALUE re,
static void adjust_registers_to_matched _((struct strscanner *p));
static VALUE strscan_getch _((VALUE self));
static VALUE strscan_get_byte _((VALUE self));
-static VALUE strscan_getbyte _((VALUE self));
static VALUE strscan_peek _((VALUE self, VALUE len));
-static VALUE strscan_peep _((VALUE self, VALUE len));
static VALUE strscan_scan_base10_integer _((VALUE self));
static VALUE strscan_unscan _((VALUE self));
static VALUE strscan_bol_p _((VALUE self));
static VALUE strscan_eos_p _((VALUE self));
-static VALUE strscan_empty_p _((VALUE self));
static VALUE strscan_rest_p _((VALUE self));
static VALUE strscan_matched_p _((VALUE self));
static VALUE strscan_matched _((VALUE self));
@@ -179,12 +182,35 @@ extract_beg_len(struct strscanner *p, long beg_i, long len)
Constructor
======================================================================= */
+#ifdef RUBY_TYPED_EMBEDDABLE
+# define HAVE_RUBY_TYPED_EMBEDDABLE 1
+#else
+# ifdef HAVE_CONST_RUBY_TYPED_EMBEDDABLE
+# define RUBY_TYPED_EMBEDDABLE RUBY_TYPED_EMBEDDABLE
+# define HAVE_RUBY_TYPED_EMBEDDABLE 1
+# else
+# define RUBY_TYPED_EMBEDDABLE 0
+# endif
+#endif
+
+#ifdef HAVE_RB_GC_LOCATION
+static void
+strscan_compact(void *ptr)
+{
+ struct strscanner *p = ptr;
+ p->str = rb_gc_location(p->str);
+ p->regex = rb_gc_location(p->regex);
+}
+#else
+#define rb_gc_mark_movable rb_gc_mark
+#endif
+
static void
strscan_mark(void *ptr)
{
struct strscanner *p = ptr;
- rb_gc_mark(p->str);
- rb_gc_mark(p->regex);
+ rb_gc_mark_movable(p->str);
+ rb_gc_mark_movable(p->regex);
}
static void
@@ -192,24 +218,37 @@ strscan_free(void *ptr)
{
struct strscanner *p = ptr;
onig_region_free(&(p->regs), 0);
+#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
ruby_xfree(p);
+#endif
}
static size_t
strscan_memsize(const void *ptr)
{
- const struct strscanner *p = ptr;
- size_t size = sizeof(*p) - sizeof(p->regs);
+ size_t size = 0;
+#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
+ size += sizeof(struct strscanner);
+#endif
+
#ifdef HAVE_ONIG_REGION_MEMSIZE
- size += onig_region_memsize(&p->regs);
+ const struct strscanner *p = ptr;
+ size += onig_region_memsize(&p->regs) - sizeof(p->regs);
#endif
return size;
}
static const rb_data_type_t strscanner_type = {
- "StringScanner",
- {strscan_mark, strscan_free, strscan_memsize},
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
+ .wrap_struct_name = "StringScanner",
+ .function = {
+ .dmark = strscan_mark,
+ .dfree = strscan_free,
+ .dsize = strscan_memsize,
+#ifdef HAVE_RB_GC_LOCATION
+ .dcompact = strscan_compact,
+#endif
+ },
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
};
static VALUE
@@ -371,6 +410,9 @@ strscan_reset(VALUE self)
/*
* :markup: markdown
+ * :call-seq:
+ * terminate -> self
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/terminate.md
*/
@@ -386,21 +428,6 @@ strscan_terminate(VALUE self)
}
/*
- * call-seq:
- * clear -> self
- *
- * This method is obsolete; use the equivalent method StringScanner#terminate.
- */
-
- /* :nodoc: */
-static VALUE
-strscan_clear(VALUE self)
-{
- rb_warning("StringScanner#clear is obsolete; use #terminate instead");
- return strscan_terminate(self);
-}
-
-/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
@@ -515,6 +542,9 @@ strscan_concat(VALUE self, VALUE str)
/*
* :markup: markdown
+ * :call-seq:
+ * pos -> byte_position
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/get_pos.md
*/
@@ -529,6 +559,9 @@ strscan_get_pos(VALUE self)
/*
* :markup: markdown
+ * :call-seq:
+ * charpos -> character_position
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/get_charpos.md
*/
@@ -544,6 +577,10 @@ strscan_get_charpos(VALUE self)
/*
* :markup: markdown
+ * :call-seq:
+ * pos = n -> n
+ * pointer = n -> n
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/set_pos.md
*/
@@ -768,6 +805,9 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
/*
* :markup: markdown
+ * :call-seq:
+ * scan(pattern) -> substring or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/scan.md
*/
@@ -841,6 +881,9 @@ strscan_match_p(VALUE self, VALUE re)
/*
* :markup: markdown
+ * call-seq:
+ * skip(pattern) match_size or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/skip.md
*/
@@ -938,6 +981,9 @@ strscan_scan_full(VALUE self, VALUE re, VALUE s, VALUE f)
/*
* :markup: markdown
+ * :call-seq:
+ * scan_until(pattern) -> substring or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/scan_until.md
*/
@@ -1012,6 +1058,9 @@ strscan_exist_p(VALUE self, VALUE re)
/*
* :markup: markdown
+ * :call-seq:
+ * skip_until(pattern) -> matched_substring_size or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/skip_until.md
*/
@@ -1123,6 +1172,9 @@ adjust_registers_to_matched(struct strscanner *p)
/*
* :markup: markdown
+ * :call-seq:
+ * getch -> character or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/getch.md
*/
@@ -1196,6 +1248,9 @@ strscan_peek_byte(VALUE self)
/*
* :markup: markdown
+ * :call-seq:
+ * get_byte -> byte_as_character or nil
+ *
* :include: strscan/link_refs.txt
* :include: strscan/methods/get_byte.md
*/
@@ -1219,22 +1274,6 @@ strscan_get_byte(VALUE self)
}
/*
- * call-seq:
- * getbyte
- *
- * Equivalent to #get_byte.
- * This method is obsolete; use #get_byte instead.
- */
-
- /* :nodoc: */
-static VALUE
-strscan_getbyte(VALUE self)
-{
- rb_warning("StringScanner#getbyte is obsolete; use #get_byte instead");
- return strscan_get_byte(self);
-}
-
-/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
@@ -1269,22 +1308,6 @@ strscan_peek(VALUE self, VALUE vlen)
return extract_beg_len(p, p->curr, len);
}
-/*
- * call-seq:
- * peep
- *
- * Equivalent to #peek.
- * This method is obsolete; use #peek instead.
- */
-
- /* :nodoc: */
-static VALUE
-strscan_peep(VALUE self, VALUE vlen)
-{
- rb_warning("StringScanner#peep is obsolete; use #peek instead");
- return strscan_peek(self, vlen);
-}
-
static VALUE
strscan_parse_integer(struct strscanner *p, int base, long len)
{
@@ -1305,16 +1328,17 @@ strscan_parse_integer(struct strscanner *p, int base, long len)
}
static inline bool
-strscan_ascii_compat_fastpath(VALUE str) {
+strscan_ascii_compat_fastpath(VALUE str)
+{
int encindex = ENCODING_GET_INLINED(str);
- // The overwhelming majority of strings are in one of these 3 encodings.
+ /* The overwhelming majority of strings are in one of these 3 encodings. */
return encindex == utf8_encindex || encindex == binary_encindex || encindex == usascii_encindex;
}
static inline void
strscan_must_ascii_compat(VALUE str)
{
- // The overwhelming majority of strings are in one of these 3 encodings.
+ /* The overwhelming majority of strings are in one of these 3 encodings. */
if (RB_LIKELY(strscan_ascii_compat_fastpath(str))) {
return;
}
@@ -1322,11 +1346,12 @@ strscan_must_ascii_compat(VALUE str)
rb_must_asciicompat(str);
}
+/* :nodoc: */
static VALUE
strscan_scan_base10_integer(VALUE self)
{
char *ptr;
- long len = 0;
+ long len = 0, remaining_len;
struct strscanner *p;
GET_SCANNER(self, p);
@@ -1336,7 +1361,7 @@ strscan_scan_base10_integer(VALUE self)
ptr = CURPTR(p);
- long remaining_len = S_RESTLEN(p);
+ remaining_len = S_RESTLEN(p);
if (remaining_len <= 0) {
return Qnil;
@@ -1359,11 +1384,12 @@ strscan_scan_base10_integer(VALUE self)
return strscan_parse_integer(p, 10, len);
}
+/* :nodoc: */
static VALUE
strscan_scan_base16_integer(VALUE self)
{
char *ptr;
- long len = 0;
+ long len = 0, remaining_len;
struct strscanner *p;
GET_SCANNER(self, p);
@@ -1373,7 +1399,7 @@ strscan_scan_base16_integer(VALUE self)
ptr = CURPTR(p);
- long remaining_len = S_RESTLEN(p);
+ remaining_len = S_RESTLEN(p);
if (remaining_len <= 0) {
return Qnil;
@@ -1526,26 +1552,9 @@ strscan_eos_p(VALUE self)
/*
* call-seq:
- * empty?
- *
- * Equivalent to #eos?.
- * This method is obsolete, use #eos? instead.
- */
-
- /* :nodoc: */
-static VALUE
-strscan_empty_p(VALUE self)
-{
- rb_warning("StringScanner#empty? is obsolete; use #eos? instead");
- return strscan_eos_p(self);
-}
-
-/*
- * call-seq:
* rest?
*
* Returns true if and only if there is more data in the string. See #eos?.
- * This method is obsolete; use #eos? instead.
*
* s = StringScanner.new('test string')
* # These two are opposites
@@ -1673,7 +1682,7 @@ name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name
(const unsigned char* )name_end,
regs);
if (num >= 1) {
- return num;
+ return num;
}
}
rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s",
@@ -2053,22 +2062,6 @@ strscan_rest_size(VALUE self)
return INT2FIX(i);
}
-/*
- * call-seq:
- * restsize
- *
- * <tt>s.restsize</tt> is equivalent to <tt>s.rest_size</tt>.
- * This method is obsolete; use #rest_size instead.
- */
-
- /* :nodoc: */
-static VALUE
-strscan_restsize(VALUE self)
-{
- rb_warning("StringScanner#restsize is obsolete; use #rest_size instead");
- return strscan_rest_size(self);
-}
-
#define INSPECT_LENGTH 5
/*
@@ -2268,6 +2261,13 @@ strscan_named_captures(VALUE self)
======================================================================= */
/*
+ * Document-class: StringScanner::Error
+ *
+ * The error class for StringScanner.
+ * See StringScanner#unscan.
+ */
+
+/*
* Document-class: StringScanner
*
* :markup: markdown
@@ -2287,8 +2287,6 @@ Init_strscan(void)
ID id_scanerr = rb_intern("ScanError");
VALUE tmp;
- id_byteslice = rb_intern("byteslice");
-
usascii_encindex = rb_usascii_encindex();
utf8_encindex = rb_utf8_encindex();
binary_encindex = rb_ascii8bit_encindex();
@@ -2297,6 +2295,7 @@ Init_strscan(void)
ScanError = rb_define_class_under(StringScanner, "Error", rb_eStandardError);
if (!rb_const_defined(rb_cObject, id_scanerr)) {
rb_const_set(rb_cObject, id_scanerr, ScanError);
+ rb_deprecate_constant(rb_cObject, "ScanError");
}
tmp = rb_str_new2(STRSCAN_VERSION);
rb_obj_freeze(tmp);
@@ -2304,6 +2303,7 @@ Init_strscan(void)
tmp = rb_str_new2("$Id$");
rb_obj_freeze(tmp);
rb_const_set(StringScanner, rb_intern("Id"), tmp);
+ rb_deprecate_constant(StringScanner, "Id");
rb_define_alloc_func(StringScanner, strscan_s_allocate);
rb_define_private_method(StringScanner, "initialize", strscan_initialize, -1);
@@ -2311,7 +2311,6 @@ Init_strscan(void)
rb_define_singleton_method(StringScanner, "must_C_version", strscan_s_mustc, 0);
rb_define_method(StringScanner, "reset", strscan_reset, 0);
rb_define_method(StringScanner, "terminate", strscan_terminate, 0);
- rb_define_method(StringScanner, "clear", strscan_clear, 0);
rb_define_method(StringScanner, "string", strscan_get_string, 0);
rb_define_method(StringScanner, "string=", strscan_set_string, 1);
rb_define_method(StringScanner, "concat", strscan_concat, 1);
@@ -2336,11 +2335,9 @@ Init_strscan(void)
rb_define_method(StringScanner, "getch", strscan_getch, 0);
rb_define_method(StringScanner, "get_byte", strscan_get_byte, 0);
- rb_define_method(StringScanner, "getbyte", strscan_getbyte, 0);
rb_define_method(StringScanner, "scan_byte", strscan_scan_byte, 0);
rb_define_method(StringScanner, "peek", strscan_peek, 1);
rb_define_method(StringScanner, "peek_byte", strscan_peek_byte, 0);
- rb_define_method(StringScanner, "peep", strscan_peep, 1);
rb_define_private_method(StringScanner, "scan_base10_integer", strscan_scan_base10_integer, 0);
rb_define_private_method(StringScanner, "scan_base16_integer", strscan_scan_base16_integer, 0);
@@ -2350,7 +2347,6 @@ Init_strscan(void)
rb_define_method(StringScanner, "beginning_of_line?", strscan_bol_p, 0);
rb_alias(StringScanner, rb_intern("bol?"), rb_intern("beginning_of_line?"));
rb_define_method(StringScanner, "eos?", strscan_eos_p, 0);
- rb_define_method(StringScanner, "empty?", strscan_empty_p, 0);
rb_define_method(StringScanner, "rest?", strscan_rest_p, 0);
rb_define_method(StringScanner, "matched?", strscan_matched_p, 0);
@@ -2365,13 +2361,10 @@ Init_strscan(void)
rb_define_method(StringScanner, "rest", strscan_rest, 0);
rb_define_method(StringScanner, "rest_size", strscan_rest_size, 0);
- rb_define_method(StringScanner, "restsize", strscan_restsize, 0);
rb_define_method(StringScanner, "inspect", strscan_inspect, 0);
rb_define_method(StringScanner, "fixed_anchor?", strscan_fixed_anchor_p, 0);
rb_define_method(StringScanner, "named_captures", strscan_named_captures, 0);
-
- rb_require("strscan/strscan");
}