Граф коммитов

590 Коммитов

Автор SHA1 Сообщение Дата
git 2dd1a037de * expand tabs. [ci skip]
Tabs were expanded because the file did not have any tab indentation in unedited lines.
Please update your editor config, and use misc/expand_tabs.rb in the pre-commit hook.
2022-10-10 13:22:15 +09:00
Nobuyoshi Nakada 0a98dd1cff
Should use dedecated function `Check_Type` 2022-10-10 13:21:57 +09:00
Vladimir Dementyev 4954c9fc0f Add MatchData#deconstruct/deconstruct_keys 2022-10-10 12:41:13 +09:00
Nobuyoshi Nakada c53667691a
[DOC] `offset` argument of Regexp#match 2022-08-18 23:25:05 +09:00
Aaron Patterson e4e054e3ce Speed up setting the backref match object
This patch speeds up setting the backref match object by avoiding some
memcopies.  Take the following code for example:

```ruby
"hello world" =~ /hello/
p $~
```

When the RE matches the string, we have to set the Match object in the
backref global.  So we would allocate a match object[^1] and use
`rb_reg_region_copy`[^2] to make a deep copy of the stack allocated
`re_registers` struct[^3] in to the newly created Ruby object.  This
could possibly trigger GC[^4], and would allocate new memory.

This patch makes a shallow copy of the `re_registers` struct on to the
Match object allowing the match object to manage the `re_registers`
pointer and also avoiding some calls to `xmalloc` and some manual
memcopy.

Benchmark looks like this:

```ruby

require "benchmark/ips"

def test_re thing
  thing =~ /hello/
end

Benchmark.ips do |x|
  x.report("re hit") do
    test_re "hello world"
  end

  x.report("re miss") do
    test_re "world"
  end
end
```

Before this patch:

```
$ ruby -v test.rb
ruby 3.2.0dev (2022-07-27T22:29:00Z master 4ad69899b7) [arm64-darwin21]
Ignoring bcrypt-3.1.16 because its extensions are not built. Try: gem pristine bcrypt --version 3.1.16
Warming up --------------------------------------
              re hit   345.401k i/100ms
             re miss   673.584k i/100ms
Calculating -------------------------------------
              re hit      3.452M (± 0.5%) i/s -     17.270M in   5.002535s
             re miss      6.736M (± 0.4%) i/s -     34.353M in   5.099593s
```

After this patch:

```
$ ./ruby -v test.rb
ruby 3.2.0dev (2022-08-01T21:24:12Z less-memcpy 0ff2a56606) [arm64-darwin21]
Warming up --------------------------------------
              re hit   419.578k i/100ms
             re miss   673.251k i/100ms
Calculating -------------------------------------
              re hit      4.201M (± 0.7%) i/s -     21.398M in   5.093593s
             re miss      6.716M (± 0.4%) i/s -     33.663M in   5.012756s
```

Matches get faster and misses maintain the same speed

[^1]: 24204d54ab/re.c (L1737)
[^2]: 24204d54ab/re.c (L1738)
[^3]: 24204d54ab/re.c (L1686)
[^4]: 24204d54ab/re.c (L981)
2022-08-02 09:04:04 -07:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Kazuhiro NISHIYAMA 846a6bb60f
[DOC] Fix a typo [ci skip] 2022-06-26 14:17:14 +09:00
Jeremy Evans 596f4b0d3a Document that Regexp#source does not retain lexer escapes
Related to [Feature #18838]
2022-06-20 15:56:28 -07:00
Nobuyoshi Nakada 4a6facc2d6 [Feature #18788] [DOC] String options to `Regexp.new`
Co-Authored-By: Janosch Müller <janosch.mueller@betterplace.org>
2022-06-20 19:35:12 +09:00
Nobuyoshi Nakada 1e9939dae2 [Feature #18788] Support options as `String` to `Regexp.new`
`Regexp.new` now supports passing the regexp flags not only as an
`Integer`, but also as a `String.  Unknown flags raise errors.
2022-06-20 19:35:12 +09:00
Nobuyoshi Nakada ab2a43265c Warn suspicious flag to `Regexp.new`
Now second argument should be `true`, `false`, `nil` or Integer.
This flag is confused with third argument some times.
2022-06-20 19:35:12 +09:00
Nobuyoshi Nakada 7f8a915715
[DOC] Refine Regexp.new argument descriptions 2022-06-20 18:39:50 +09:00
Nobuyoshi Nakada 914c26eab3
[DOC] Regexp timeout is float or nil 2022-06-20 17:47:44 +09:00
Nobuyoshi Nakada cd3a5cd0e3
[DOC] Fixed omissions in Regexp.new arguments 2022-06-20 09:26:11 +09:00
Jeremy Evans ec3542229b
Ignore invalid escapes in regexp comments
Invalid escapes are handled at multiple levels.  The first level
is in parse.y, so skip invalid unicode escape checks for regexps
in parse.y.

Make rb_reg_preprocess and unescape_nonascii accept the regexp
options.  In unescape_nonascii, if the regexp is an extended
regexp, when "#" is encountered, ignore all characters until the
end of line or end of regexp.

Unfortunately, in extended regexps, you can use "#" as a non-comment
character inside a character class, so also parse "[" and "]"
specially for extended regexps, and only skip comments if "#" is
not inside a character class. Handle nested character classes as well.

This issue doesn't just affect extended regexps, it also affects
"(#?" comments inside all regexps.  So for those comments, scan
until trailing ")" and ignore content inside.

I'm not sure if there are other corner cases not handled.  A
better fix would be to redesign the regexp parser so that it
unescaped during parsing instead of before parsing, so you already
know the current parsing state.

Fixes [Bug #18294]

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2022-06-06 13:50:03 -07:00
Burdette Lamar b41de3a1e8
[DOC] Enhanced RDoc for MatchData (#5822)
Treats:
    #to_s
    #named_captures
    #string
    #inspect
    #hash
    #==
2022-04-18 18:19:10 -05:00
Burdette Lamar 6db3f7c405
Enhanced RDoc for MatchData (#5821)
Treats:
    #[]
    #values_at
2022-04-18 15:52:07 -05:00
Burdette Lamar 86e23529ad
Enhanced RDoc for MatchData (#5820)
Treats:
    #pre_match
    #post_match
    #to_a
    #captures
2022-04-18 14:34:40 -05:00
Burdette Lamar b074bc3d61
[DOC] Enhanced RDoc for MatchData (#5819)
Treats:
    #begin
    #end
    #match
    #match_length
2022-04-18 13:02:35 -05:00
Burdette Lamar 9d1dd7a9ed
[DOC] Enhanced RDoc for MatchData (#5818)
Treats:
    #regexp
    #names
    #size
    #offset
2022-04-18 11:31:30 -05:00
Burdette Lamar 51ea67698e
[DOC] Enhanced RDoc for Regexp (#5815)
Treats:
    ::new
    ::escape
    ::try_convert
    ::union
    ::last_match
2022-04-18 10:45:29 -05:00
Burdette Lamar 2b4b513ef0
[DOC] Enhanced RDoc for Regexp (#5812)
Treats:

    #fixed_encoding?
    #hash
    #==
    #=~
    #match
    #match?

Also, in regexp.rdoc:

    Changes heading from 'Special Global Variables' to 'Regexp Global Variables'.
    Add tiny section 'Regexp Interpolation'.
2022-04-16 15:20:03 -05:00
Burdette Lamar e021754db0
[DOC] Enhanced RDoc for Regexp (#5807)
Treats:

    #source
    #inspect
    #to_s
    #casefold?
    #options
    #names
    #named_captures
2022-04-15 13:31:15 -05:00
Nobuyoshi Nakada d8189ed23f
Return only captured range in `MatchData` [Bug #18670] 2022-03-31 18:01:15 +09:00
Yusuke Endoh c499a4c28a re.c: stop a wrong warning of "flags ignored" on Regexp.new(//)
[Bug #18669]
2022-03-31 10:07:09 +09:00
Yusuke Endoh 5df2589b64 internal/ractor.h: Added
Currently it has only one function prototype.
2022-03-30 16:50:46 +09:00
Yusuke Endoh 2ade40276b re.c: raise Regexp::TimeoutError instead of RuntimeError 2022-03-30 16:50:46 +09:00
Yusuke Endoh ce87bb8bd6 re.c: Add `timeout` keyword for Regexp.new and Regexp#timeout 2022-03-30 16:50:46 +09:00
Yusuke Endoh ffc3b37f96 re.c: Add Regexp.timeout= and Regexp.timeout
[Feature #17837]
2022-03-30 16:50:46 +09:00
Shugo Maeda c8817d6a3e
Add String#byteindex, String#byterindex, and MatchData#byteoffset (#5518)
* Add String#byteindex, String#byterindex, and MatchData#byteoffset [Feature #13110]

Co-authored-by: NARUSE, Yui <naruse@airemix.jp>
2022-02-19 19:10:00 +09:00
Shugo Maeda cda5aee74e
LONG2NUM() should be used for rmatch_offset::{beg,end}
https://github.com/ruby/ruby/pull/5518#discussion_r809645406
2022-02-18 22:13:45 +09:00
Nobuyoshi Nakada 16fdc1ff46
[DOC] Fix broken links to literals.rdoc 2022-02-08 01:27:52 +09:00
S-H-GAMELINKS 804a714971 Replace to RBOOL macro 2022-01-17 13:49:37 +09:00
Burdette Lamar 28fb6d6b9e
Adding links to literals and Kernel (#5192)
* Adding links to literals and Kernel
2021-12-03 07:12:28 -06:00
S.H dc9112cf10
Using NIL_P macro instead of `== Qnil` 2021-10-03 22:34:45 +09:00
Jeremy Evans abc0304cb2 Avoid race condition in Regexp#match
In certain conditions, Regexp#match could return a MatchData with
missing captures.  This seems to require at the least, multiple
threads calling a method that calls the same block/proc/lambda
which calls Regexp#match.

The race condition happens because the MatchData is passed from
indirectly via the backref, and other threads can modify the
backref.

Fix the issue by:

1. Not reusing the existing MatchData from the backref, and always
   allocating a new MatchData.
2. Passing the MatchData directly to the caller using a VALUE*,
   instead of indirectly through the backref.

It's likely that variants of this issue exist for other Regexp
methods.  Anywhere that MatchData is passed implicitly through
the backref is probably vulnerable to this issue.

Fixes [Bug #17507]
2021-10-01 19:50:19 -09:00
Nobuyoshi Nakada f2cb6288bc
[Feature #18172] Add MatchData#match_length
The method to return the length of the matched substring
corresponding to the given argument.
2021-09-16 19:55:06 +09:00
Nobuyoshi Nakada 09d724e6f8
[Feature #18172] Add MatchData#match
The method to return the single matched substring corresponding to
the given argument.
2021-09-16 19:55:06 +09:00
S.H b8c3a84bdd
Refactor and Using RBOOL macro 2021-09-15 08:11:05 +09:00
Nobuyoshi Nakada c5570a7c11 Extract backref_number_check 2021-09-12 11:16:51 +09:00
Nobuyoshi Nakada 99d8c4832a Preserve the encoding of the argument in IndexError [Bug #18160] 2021-09-12 11:16:51 +09:00
Martin Dürst f2ffa88964 Show default argument explicitly for Rexexp#match? [ci skip] 2021-09-01 09:37:13 +09:00
Martin Dürst 45b8846bec Fix minor grammar issue in documentation of Regexp#match? [ci skip] 2021-09-01 09:24:34 +09:00
S.H 378e8cdad6
Using RBOOL macro 2021-08-02 12:06:44 +09:00
Nobuyoshi Nakada 9f3888d6a3 Warn more duplicate literal hash keys
Following non-special_const literals:
* T_REGEXP
2021-06-03 15:11:18 +09:00
S.H d627b75e01
Add static modifier to C function in re.c (#3153)
* add static modifier for rb_reg_eqq func

* add static modifier for rb_check_regexp_type func
2021-06-01 00:59:33 -07:00
Nobuyoshi Nakada 947d93b715
[DOC] {Array,MatchData}#values_at understand ranges [ci skip] 2021-02-07 10:30:43 +09:00
Marcus Stollsteimer 3108ad7bf3 [DOC] Fix grammar: "is same as" -> "is the same as" 2021-01-05 15:13:53 +01:00
Jeremy Evans 05313c914b Use category: :deprecated in warnings that are related to deprecation
Also document that both :deprecated and :experimental are supported
:category option values.

The locations where warnings were marked as deprecation warnings
was previously reviewed by shyouhei.

Comment a couple locations where deprecation warnings should probably
be used but are not currently used because deprecation warning
enablement has not occurred at the time they are called
(RUBY_FREE_MIN, RUBY_HEAP_MIN_SLOTS, -K).

Add assert_deprecated_warn to test assertions.  Use this to simplify
some tests, and fix failing tests after marking some warnings with
deprecated category.
2020-12-18 09:54:11 -08:00
Nobuyoshi Nakada 85aabef023 [Feature #17136] Remove special behavior from $KCODE 2020-11-28 18:51:36 +09:00