aboutsummaryrefslogtreecommitdiffstats
path: root/range.c
Commit message (Collapse)AuthorAgeFilesLines
* merge revision(s) ↵nagachika2020-07-191-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | bf1a6771f305ea286a3ae575676924551c03e857,c1463625555b061a2b94c3b6c5581730b482a285: [Backport #17012] [Backport #17014] Fix non-numeric exclusive Range#minmax bug The implementation of Range#minmax added in d5c60214c45 causes the following incorrect behaviour: ('a'...'c').minmax => ["a", ["a", "b"]] instead of ('a'...'c').minmax => ["a", "b"] This is because the C implementation of Range#minmax (range_minmax) directly delegates to the C implementation of Range#min (range_min) and Range#max (range_max), without changing the execution context. Range#max's C implementation (range_max), when given a non-numeric exclusive range, delegates to super, which is meant to call Enumerable#max. However, because range_max is called directly by range_minmax, super calls Enumerable#minmax instead, causing the incorrect nesting. Perhaps it is possible to change the execution context in an optimized manner, but the simplest solution seems to be to just explicitly delegate from Range#minmax to Range#min and Range#max. Use static variables in Range#minmax
* range.c: Range#min with a beginless one now raise an explicit exceptionYusuke Endoh2019-12-251-0/+7
| | | | [Bug #16450]
* Enhance Range docszverok2019-12-221-10/+24
| | | | | | | | * Change === docs to mention it uses cover? * Add different example to === docs to showcase behavior better * Change include? docs to mention cover? and clarify the difference
* Fixed misspellingsNobuyoshi Nakada2019-12-201-2/+2
| | | | Fixed misspellings reported at [Bug #16437], only in ruby and rubyspec.
* implement Range#count卜部昌平2019-12-041-0/+37
| | | | As matz requested in [Bug #16366].
* Deprecate taint/trust and related methods, and make the methods no-opsJeremy Evans2019-11-181-2/+0
| | | | | | This removes the related tests, and puts the related specs behind version guards. This affects all code in lib, including some libraries that may want to support older versions of Ruby.
* Make Range#=== operate like cover? instead of include? for string rangesJeremy Evans2019-08-141-9/+14
| | | | | | | | | | | | | | | | | | Previously, Range#=== treated string ranges that were not endless or beginless the same as include?, instead of the same as cover?. I think this was an oversight in 989e07c0f2fa664a54e52a475c2fcc145f06539d, as the commit message did not indicate this behavior was desired. This also makes some previously dead code no longer dead. Previously, the conditionals were doing this: if (RB_TYPE_P(beg, T_STRING) if (NIL_P(beg)) # can never be true This restructures it so at the NIL_P(beg) check, beg could possibly be nil (beginless ranges). Fixes [Bug #15449]
* Implement Range#minmaxJeremy Evans2019-08-141-0/+22
| | | | | | | | | Range#minmax was previous not implemented, so calling #minmax on range was actually calling Enumerable#minmax. This is a simple implementation of #minmax by just calling range_min and range_max. Fixes [Bug #15867] Fixes [Bug #15807]
* Document that Range#cover? returns false if <=> returns nilJeremy Evans2019-07-241-1/+4
| | | | Fixes [Bug #12090]
* range.c (inspect_range): omit beginless "nil"Yusuke Endoh2019-05-231-3/+9
| | | | | | | | | | | | except the special case `(nil..nil)`. ``` (1..).inspect #=> "1.." (..5).inspect #=> "..5" (nil..nil).inspect #=> "nil..nil" ``` [Bug #15745]
* Add exception support in `Range#first`.manga_osyo2019-05-011-0/+3
| | | | Closes: https://github.com/ruby/ruby/pull/2163
* range.c: force hash values fixablenobu2019-04-081-1/+1
| | | | | | | * range.c (range_hash): force hash values fixable on LLP64 environment. [ruby-core:92194] [Bug #15757] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: support to make beginless arithmetic sequencesmrkn2019-04-041-1/+3
| | | | | | | | | | * range.c (range_step): fix the guard condition so that a beginless range can be turned into a beginless arithmetic sequence. * test/ruby/test_range.rb (test_step): add assertions for the above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67433 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c (r_cover_range_p): support beginless rangemame2019-04-031-2/+3
| | | | | | `(..2).cover?(..1)` should return true. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67423 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Introduce beginless range [Feature#14799]mame2019-04-031-10/+39
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* [DOC] fix markups [ci skip]nobu2019-03-281-6/+4
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Fix styles [ci skip]nobu2019-01-091-2/+4
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c (range_last): disable optimization when each is redefinedmrkn2019-01-081-1/+2
| | | | | | | Do not use the optimized version of Range#last when Range#each is redefined. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66749 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: optimize rb_range_last for int renagemrkn2019-01-061-0/+61
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This change improves the performance of Range#last method for a range of integer. This optimization directly computes only required values only in a range instead of converting all values in the range to an array. The optimized implementation is 129-16.7k times faster than the previous one in the benchmark result given below. === Benchmark Result === ``` $ make benchmark ITEM=range_last COMPARE_RUBY=/Users/mrkn/.rbenv/versions/2.6.0/bin/ruby generating known_errors.inc known_errors.inc unchanged /Users/mrkn/src/github.com/ruby/ruby/revision.h unchanged /Users/mrkn/.rbenv/shims/ruby --disable=gems -rrubygems -I/Users/mrkn/src/github.com/ruby/ruby/benchmark/lib /Users/mrkn/src/github.com/ruby/ruby/benchmark/benchmark-driver/exe/benchmark-driver \ --executables="compare-ruby::/Users/mrkn/.rbenv/versions/2.6.0/bin/ruby -I.ext/common --disable-gem" \ --executables="built-ruby::./miniruby -I/Users/mrkn/src/github.com/ruby/ruby/lib -I. -I.ext/common -r/Users/mrkn/src/github.com/ruby/ruby/prelude --disable-gem" \ $(find /Users/mrkn/src/github.com/ruby/ruby/benchmark -maxdepth 1 -name '*range_last*.yml' -o -name '*range_last*.rb' | sort) Warming up -------------------------------------- (1..1_000_000).last(100) 35.600 i/s - 36.000 times in 1.011239s (28.09ms/i) (1..1_000_000).last(1000) 36.204 i/s - 39.000 times in 1.077240s (27.62ms/i) (1..1_000_000).last(10000) 36.470 i/s - 39.000 times in 1.069386s (27.42ms/i) Calculating ------------------------------------- compare-ruby built-ruby (1..1_000_000).last(100) 36.477 609.196k i/s - 106.000 times in 2.905950s 0.000174s (1..1_000_000).last(1000) 35.936 50.350k i/s - 108.000 times in 3.005321s 0.002145s (1..1_000_000).last(10000) 35.641 4.602k i/s - 109.000 times in 3.058233s 0.023685s Comparison: (1..1_000_000).last(100) built-ruby: 609195.7 i/s compare-ruby: 36.5 i/s - 16700.87x slower (1..1_000_000).last(1000) built-ruby: 50349.7 i/s compare-ruby: 35.9 i/s - 1401.08x slower (1..1_000_000).last(10000) built-ruby: 4602.1 i/s compare-ruby: 35.6 i/s - 129.12x slower ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66734 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: reject ArithmeticSequence in rb_range_valuesmrkn2018-12-211-0/+3
| | | | | | | Reject ArithmeticSequence in rb_range_values so that methods like Array#[] raises TypeError for ArithmeticSequence as an index. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66478 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: [DOC] fix typostomar2018-12-121-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66374 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Typo fix [DOC] [ci skip] [#15405]marcandre2018-12-121-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Documentation on endless ranges.marcandre2018-12-121-0/+28
| | | | | | Based on patch by Victor Shepelev [DOC] [#7552] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66369 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* enumerator.c: rb_arithmetic_sequence_extractmrkn2018-12-121-3/+0
| | | | | | | New public C-API for extracting components of Enumerator::ArithmeticSequence or Range. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66351 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Prefer rb_check_arity when 0 or 1 argumentsnobu2018-12-061-6/+1
| | | | | | | Especially over checking argc then calling rb_scan_args just to raise an ArgumentError. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: [DOC] improve docs for Range#cover?stomar2018-10-121-5/+6
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Adjust indent [ci skip]nobu2018-09-281-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Add Range#% to call-seq [ci skip]kazu2018-09-281-0/+4
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Add Range#%mrkn2018-09-281-0/+7
| | | | | | [Feature #14697] [ruby-core:86588] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* [DOC] Modify descriptions for ArithmeticSequencemrkn2018-09-121-0/+3
| | | | | | [ci-skip] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64697 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Range#cover? accepts Range object. [Feature #14473]tarui2018-09-051-4/+57
| | | | | | | | | | | | | | * range.c (range_cover): add code for range argument. If the argument is a Range, check it is or is not covered by the reciver. If it can be treated as a sequence, this method treats it that way. * test/ruby/test_range.rb (class TestRange): add tests for this feature. This patch is written by Owen Stephens. thank you! git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Remove extra semicolonkazu2018-08-201-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64480 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* enumerator.c: Introduce Enumerator::ArithmeticSequencemrkn2018-08-061-5/+13
| | | | | | | | | | | | | | | | | | | This commit introduces new core class Enumerator::ArithmeticSequence. Enumerator::ArithmeticSequence is a subclass of Enumerator, and represents a number generator of an arithmetic sequence. After this commit, Numeric#step and Range#step without blocks returned an ArithmeticSequence object instead of an Enumerator. This class introduces the following incompatibilities: - You can create a zero-step ArithmeticSequence, and its size is not ArgumentError, but Infinity. - You can create a negative-step ArithmeticSequence from a range. [ruby-core:82816] [Feature #13904] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: [DOC] small improvementstomar2018-07-081-2/+2
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63907 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Range#last and #max raises a RangeError if it is endlessmame2018-06-221-1/+9
| | | | | | | Also, Range#min raises an error if it is endless and a comparison method is specified. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Range#size now returns Float::INFINITY if it is endlessmame2018-06-221-2/+8
| | | | | | Fixes [Bug #14699] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Range#to_a now raises RangeError if it is endlessmame2018-06-221-0/+22
| | | | | | Fixes [Bug #14845] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Revert "range.c: prohibit `(1..nil)`"mame2018-06-131-8/+6
| | | | | | | This reverts commit a44c010764a16ae09aaed49d76eec055ca0057c8. Refs #14845. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: prohibit `(1..nil)`mame2018-06-131-6/+8
| | | | | | | | | | Now endless range can be created by either a literal `(1..)` or explicit range creation `Range.new(1, nil)`. [Bug #14845] This change is intended for "early failure"; for example, `(1..var).to_a` causes out of memory if `var` is inadvertently nil. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63646 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: === by cover?nobu2018-05-171-3/+14
| | | | | | | * range.c (range_eqq): switch `Range#===` to use `cover?` instead of `include?`. [Feature #14575] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63453 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: optimize range_each for Bignumnobu2018-04-291-8/+51
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63299 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: endless symbol rangenobu2018-04-281-2/+7
| | | | | | | * range.c (range_each): shortcirtuit endless symbol range too, as well as `#step`. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63297 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: adjust to rb_str_upto_eachnobu2018-04-281-36/+26
| | | | | | | | | | * range.c (range_each_func): adjust the signature of the callback function to rb_str_upto_each, and exit the loop if the callback returned non-zero. * string.c (rb_str_upto_endless_each): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63290 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: each on endless rangenobu2018-04-281-3/+2
| | | | | | | | | * range.c (range_each): endless range begins with string-like object should iterate from the converted result string, as well as `#each` on a string-end range or `#step` method on an endless range, i.e., `begin.succ` should not be called. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63283 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: step in bignumnobu2018-04-201-1/+1
| | | | | | | * range.c (range_step): honor step in bignum addition. [Feature #12912] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63208 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: fix fixnum loop conditionnobu2018-04-201-4/+3
| | | | | | | * range.c (range_step): FIXABLE + FIXABLE never overflow, but may not be FIXABLE. [Feature #12912] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* internal.h: use the same declaration as definitionk0kubun2018-04-191-3/+3
| | | | | | | | | | | | range.c: cast the function type to meet the declaration This change is for fixing build error on AppVeyor: https://ci.appveyor.com/project/ruby/ruby/build/1.0.8177 string.c ../string.c(4330) : error C4028: formal parameter 2 different from declaration git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63198 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Prefer CONST_ID to static global IDsmame2018-04-191-8/+10
| | | | | | Just refactoring. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63196 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* range.c: Make Range#bsearch support endless rangesmame2018-04-191-46/+69
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Make Range#min, max, include?, cover?, and === to support endless rangemame2018-04-191-5/+15
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63194 b2dd03c8-39d4-4d8f-98ff-823fe69b080e