aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_string.rb
Commit message (Collapse)AuthorAgeFilesLines
* Make String methods return String instances when called on a subclass instanceJeremy Evans2020-11-201-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This modifies the following String methods to return String instances instead of subclass instances: * String#* * String#capitalize * String#center * String#chomp * String#chop * String#delete * String#delete_prefix * String#delete_suffix * String#downcase * String#dump * String#each/#each_line * String#gsub * String#ljust * String#lstrip * String#partition * String#reverse * String#rjust * String#rpartition * String#rstrip * String#scrub * String#slice! * String#slice/#[] * String#split * String#squeeze * String#strip * String#sub * String#succ/#next * String#swapcase * String#tr * String#tr_s * String#upcase This also fixes a bug in String#swapcase where it would return the receiver instead of a copy of the receiver if the receiver was the empty string. Some string methods were left to return subclass instances: * String#+@ * String#-@ Both of these methods will return the receiver (subclass instance) in some cases, so it is best to keep the returned class consistent. Fixes [#10845]
* Disable deprecation warning by the default [Feature #16345]Nobuyoshi Nakada2020-09-251-7/+0
| | | | And `-w` option turns it on.
* Let String#slice! return nil (#3533)Soutaro Matsumoto2020-09-111-0/+2
| | | Returns `nil` instead of an empty string when non-integer number is given (to make it 2.7 compatible).
* The deprecation of enumerators with block has been withdrawnNobuyoshi Nakada2020-08-311-66/+34
| | | | https://bugs.ruby-lang.org/issues/6670#change-75907
* register_fstring: avoid duping the passed string when possibleJean Boussier2020-08-191-0/+16
| | | | | | | | If the passed string is frozen, bare and not shared, then there is no need to duplicate it. Ref: 4ab69ebbd7cef8539f687e1f948845d076461dc6 Ref: https://bugs.ruby-lang.org/issues/11386
* rb_str_{index,rindex}_m: Handle /\K/ in patternKasumi Hanazuki2020-08-131-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | When the pattern Regexp given to String#index and String#rindex contain a /\K/ (lookbehind) operator, these methods return the position where the beginning of the lookbehind pattern matches, while they are expected to return the position where the \K matches. ``` # without patch "abcdbce".index(/b\Kc/) # => 1 "abcdbce".rindex(/b\Kc/) # => 4 ``` This patch fixes this problem by using BEG(0) instead of the return value of rb_reg_search. ``` # with patch "abcdbce".index(/b\Kc/) # => 2 "abcdbce".rindex(/b\Kc/) # => 5 ``` Fixes [Bug #17118]
* rb_str_{partition,rpartition}_m: Handle /\K/ in patternKasumi Hanazuki2020-08-131-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | When the pattern given to String#partition and String#rpartition contain a /\K/ (lookbehind) operator, the methods return strings sliced at incorrect positions. ``` # without patch "abcdbce".partition(/b\Kc/) # => ["a", "c", "cdbce"] "abcdbce".rpartition(/b\Kc/) # => ["abcd", "c", "ce"] ``` This patch fixes the problem by using BEG(0) instead of the return value of rb_reg_search. ``` # with patch "abcdbce".partition(/b\Kc/) # => ["ab", "c", "dbce"] "abcdbce".rpartition(/b\Kc/) # => ["abcdb", "c", "e"] ``` As a side-effect this patch makes String#partition 2x faster when the pattern is a costly Regexp by performing Regexp search only once, which was unexpectedly done twice in the original implementation. Fixes [Bug #17119]
* string.c(rb_str_split_m): Handle /\K/ correctlyKasumi Hanazuki2020-08-121-0/+5
| | | | | | | Use BEG(0) instead of the result of rb_reg_search to handle the cases when the separator Regexp contains /\K/ (lookbehind) operator. Fixes [Bug #17113]
* Added NUL-contained casesNobuyoshi Nakada2020-07-311-0/+17
|
* Warn non-nil `$/` [Feature #14240]Nobuyoshi Nakada2020-02-231-0/+14
|
* Fix `String#partition`Nobuyoshi Nakada2020-01-161-0/+2
| | | | | Split with the matched part when the separator matches the empty part at the beginning. [Bug #11014]
* Refined the warning message for $, and $;Nobuyoshi Nakada2019-12-201-1/+1
| | | | [Bug #16438]
* Revert "Regexp#match{?} with nil raises TypeError as String, Symbol (#1506)"NARUSE, Yui2019-12-041-5/+0
| | | | | This reverts commit 2a22a6b2d8465934e75520a7fdcf522d50890caf. Revert [Feature #13083]
* Deprecate taint/trust and related methods, and make the methods no-opsJeremy Evans2019-11-181-62/+17
| | | | | | 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.
* Regexp#match{?} with nil raises TypeError as String, Symbol (#1506)Kenichi Kamiya2019-10-171-0/+5
| | | | | | | | | | | | | | | | | | | | | | | * {String|Symbol}#match{?} with nil returns falsy To improve consistency with Regexp#match{?} * String#match(nil) returns `nil` instead of TypeError * String#match?(nil) returns `false` instead of TypeError * Symbol#match(nil) returns `nil` instead of TypeError * Symbol#match?(nil) returns `false` instead of TypeError * Prefer exception * Follow empty ENV * Drop outdated specs * Write ruby/spec for above https://github.com/ruby/ruby/pull/1506/files#r183242981 * Fix merge miss
* Make rb_scan_args handle keywords more similar to Ruby methods (#2460)Jeremy Evans2019-09-251-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Cfuncs that use rb_scan_args with the : entry suffer similar keyword argument separation issues that Ruby methods suffer if the cfuncs accept optional or variable arguments. This makes the following changes to : handling. * Treats as **kw, prompting keyword argument separation warnings if called with a positional hash. * Do not look for an option hash if empty keywords are provided. For backwards compatibility, treat an empty keyword splat as a empty mandatory positional hash argument, but emit a a warning, as this behavior will be removed in Ruby 3. The argument number check needs to be moved lower so it can correctly handle an empty positional argument being added. * If the last argument is nil and it is necessary to treat it as an option hash in order to make sure all arguments are processed, continue to treat the last argument as the option hash. Emit a warning in this case, as this behavior will be removed in Ruby 3. * If splitting the keyword hash into two hashes, issue a warning, as we will not be splitting hashes in Ruby 3. * If the keyword argument is required to fill a mandatory positional argument, continue to do so, but emit a warning as this behavior will be going away in Ruby 3. * If keyword arguments are provided and the last argument is not a hash, that indicates something wrong. This can happen if a cfunc is calling rb_scan_args multiple times, and providing arguments that were not passed to it from Ruby. Callers need to switch to the new rb_scan_args_kw function, which allows passing of whether keywords were provided. This commit fixes all warnings caused by the changes above. It switches some function calls to *_kw versions with appropriate kw_splat flags. If delegating arguments, RB_PASS_CALLED_KEYWORDS is used. If creating new arguments, RB_PASS_KEYWORDS is used if the last argument is a hash to be treated as keywords. In open_key_args in io.c, use rb_scan_args_kw. In this case, the arguments provided come from another C function, not Ruby. The last argument may or may not be a hash, so we can't set keyword argument mode. However, if it is a hash, we don't want to warn when treating it as keywords. In Ruby files, make sure to appropriately use keyword splats or literal keywords when calling Cfuncs that now issue keyword argument separation warnings through rb_scan_args. Also, make sure not to pass nil in place of an option hash. Work around Kernel#warn warnings due to problems in the Rubygems override of the method. There is an open pull request to fix these issues in Rubygems, but part of the Rubygems tests for their override fail on ruby-head due to rb_scan_args not recognizing empty keyword splats, which this commit fixes. Implementation wise, adding rb_scan_args_kw is kind of a pain, because rb_scan_args takes a variable number of arguments. In order to not duplicate all the code, the function internals need to be split into two functions taking a va_list, and to avoid passing in a ton of arguments, a single struct argument is used to handle the variables previously local to the function.
* Fixed heap-use-after-freeNobuyoshi Nakada2019-08-151-0/+6
| | | | | | * string.c (rb_str_sub_bang): retrieves a pointer to the replacement string buffer just before using it, for the case of replacement with the receiver string itself. [Bug #16105]
* Occupy match dataNobuyoshi Nakada2019-07-271-0/+3
| | | | | * string.c (rb_str_split_m): occupy match data not to be modified during yielding the block. [Bug #16024]
* Check the result of String#-@Nobuyoshi Nakada2019-07-141-3/+5
|
* Make String#-@ not freeze receiver if called on unfrozen subclass instanceJeremy Evans2019-07-021-0/+16
| | | | | | | | | rb_fstring behavior in this case is to freeze the receiver. I'm not sure if that should be changed, so this takes the conservative approach of duping the receiver in String#-@ before passing to rb_fstring. Fixes [Bug #15926]
* Fixed String#grapheme_clusters with wide encodingsNobuyoshi Nakada2019-06-291-4/+11
| | | | | | | | * string.c (get_reg_grapheme_cluster): make regexp from properly encoded sources fro wide-char encodings. [Bug #15965] * regparse.c (node_extended_grapheme_cluster): suppress false duplicated range warning for the time being.
* Hoisted out WIDE_ENCODINGSNobuyoshi Nakada2019-06-291-8/+8
|
* New buffer for shared stringNobuyoshi Nakada2019-06-191-0/+5
| | | | | * string.c (rb_str_init): allocate new buffer if the string is shared. [Bug #15937]
* Preserve the string content at self-copyingNobuyoshi Nakada2019-06-191-0/+9
| | | | | * string.c (rb_str_init): preserve the embedded content when self-copying with a capacity. [Bug #15937]
* String#b: Don't depend on dependent stringAlan Wu2019-06-181-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | Registering a string that depend on a dependent string as fstring can lead to use-after-free. See c06ddfe and 3f95620 for details. The following script triggers use-after-free on trunk, 2.4.6, 2.5.5 and 2.6.3. Credits to @wanabe for using eval as a cross-version way of registering a fstring. ```ruby a = ('j' * 24).b.b eval('', binding, a) p a 4.times { GC.start } p a ``` - string.c (str_replace_shared_without_enc): when given a dependent string, depend on the root of the dependent string. [Bug #15934]
* Update String#crypt tests to work on OpenBSDJeremy Evans2019-06-011-4/+17
| | | | | | | Skip the webrick httpauth tests that use crypt when testing on OpenBSD. Fixes [Bug #11363]
* Get rid of indirect sharingNobuyoshi Nakada2019-04-271-0/+9
| | | | | | | | | * string.c (str_duplicate): share the root shared string if the original string is already sharing, so that all shared strings refer the root shared string directly. indirect sharing can cause a dangling pointer. [Bug #15792]
* string.c: warn non-nil $;nobu2019-04-181-3/+10
| | | | | | | | * string.c (rb_str_split_m): warn use of non-nil $;. * string.c (rb_fs_setter): warn when set to non-nil value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: remove the deprecation warnings of `String#bytes` with blockmame2018-12-261-53/+35
| | | | | | | And its friends: lines, chars, grapheme_clusters, and codepoints. [Feature #6670] [ruby-core:90728] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66579 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Revert "string.c: remove the deprecation warnings of `String#bytes` with block"mame2018-12-261-35/+53
| | | | | | Forgot to write the ticket number in the commit log... git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66578 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: remove the deprecation warnings of `String#bytes` with blockmame2018-12-261-53/+35
| | | | | | And its friends: lines, chars, grapheme_clusters, and codepoints. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* change diaeresis from above to below for better visibilityduerst2018-12-041-4/+4
| | | | | | | | | | In test/ruby/test_regexp.rb and test/ruby/test_string.rb, change some instances of COMBINING DIAERESIS (U+0308, above) to COMBINING DIAERESIS BELOW (U+0324) to make it more easily visible in test output, particularly in the context of double quotes surrounding strings. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66192 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* assertions for r65956nobu2018-11-241-1/+8
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65959 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Don't use single byte optimization on grapheme clustersnaruse2018-11-241-0/+1
| | | | | | Unicode Text Segmentation considers CRLF as a character. [Bug #15337] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65954 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* test/lib/test/unit/assertions.rb: skip memory leak checkk0kubun2018-10-231-1/+0
| | | | | | | | | | for all test cases on MJIT. In addition to those 2 tests, TestAutoload#test_no_leak newly failed and most of assert_no_memory_leak usages are likely to randomly fail. Let me just skip all of them but let's revisit this to check it properly later. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65315 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* test/ruby/test_string.rb: skip test_crypt for MJIT againk0kubun2018-10-221-0/+1
| | | | | | | | | Partially reverting r65285. Actually this one is failing due to memory consumption on MJIT, so this seems not catching the bug of MJIT. test/ruby/test_io.rb: unify the skip message with it git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65309 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* try to remove some test skips for MJITk0kubun2018-10-211-4/+0
| | | | | | | | | Eric Wong made some effort to keep compatibility around fd with MJIT. Also I'm hoping r65279 (and r65280) eliminates major MJIT bugs, so I want to start solely testing MJIT. Other test skips branched by MJIT enablement seemed reasonable to me. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* test: skip 2 major unstable tests with MJITk0kubun2018-06-261-0/+4
| | | | | | | | | for CI with cppflags=-DMJIT_FORCE_ENABLE. Since I have no idea to fix this immediately, let me skip this for now and take a look later. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63752 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: fix scanned substring with `\K`nobu2018-04-241-0/+2
| | | | | | | | * string.c (scan_once): fix the matched substring with `\K`, the beginning of that string may differ from the matched position. [ruby-core:86663] [Bug #14707] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63252 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: fix checking ordernobu2018-04-161-0/+3
| | | | | | | | * string.c (str_undump): check for suffix before if Unicode escape conflicts with it. the message "but used force_encoding" sounds strange when it is not used. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63162 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* fix each_grapheme_cluster's size [Bug #14363]naruse2018-03-221-3/+10
| | | | | | From: Hugo Peixoto <hugo.peixoto@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62892 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Revert "each_grapheme_cluster shouldn't return size [Bug #14363]"naruse2018-03-221-3/+0
| | | | | | This reverts commit r62887. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62891 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* each_grapheme_cluster shouldn't return size [Bug #14363]naruse2018-03-221-0/+3
| | | | | | From: Stefan Schüßler <mail@stefanschuessler.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: split with blocknobu2018-03-151-0/+40
| | | | | | | * string.c (rb_str_split_m): yield each split substrings if the block is given, instead of returing the array. [Feature #4780] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62763 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* test_array.rb (test_slice!): moved misplaced testnobu2018-03-151-1/+0
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* trick ruby-mode.el by heredocsnobu2018-01-311-1/+3
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62119 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: clear substring code rangenobu2018-01-251-0/+6
| | | | | | | | * string.c (str_substr): substring of broken code range string may be valid or broken. patch by tommy (Masahiro Tomita) at [ruby-dev:50430] [Bug #14388]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* string.c: chomp rs at the endnobu2017-12-291-0/+4
| | | | | | | | | * string.c (rb_str_enumerate_lines): should chomp record separator only, but not a newline, at the end of the receiver as well as middle, if the separator is given. [ruby-core:84552] [Bug #14257] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Don't allow mixed escapenaruse2017-12-211-0/+2
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61381 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* move dump format validation into parsing epiloguenaruse2017-12-211-0/+10
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e