commit a590770d279e2357d5bedfe49a04016b5f6feb57 Author: zyppe <210hcl@gmail.com> Date: Thu Feb 29 14:29:47 2024 +0800 Initialize for ruby2.5 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5d2d1f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ruby-2.5.9.tar.xz diff --git a/.ruby2.5.metadata b/.ruby2.5.metadata new file mode 100644 index 0000000..8eb3fd9 --- /dev/null +++ b/.ruby2.5.metadata @@ -0,0 +1 @@ +ddaa9e107b816140e45e896b081f5b8a886f7fef415d0b52798c937814e08dff ruby-2.5.9.tar.xz diff --git a/did_you_mean-1.2.0.gem b/did_you_mean-1.2.0.gem new file mode 100644 index 0000000..d26553b Binary files /dev/null and b/did_you_mean-1.2.0.gem differ diff --git a/remove-unneeded-files.patch b/remove-unneeded-files.patch new file mode 100644 index 0000000..6c1f788 --- /dev/null +++ b/remove-unneeded-files.patch @@ -0,0 +1,13 @@ +Index: ruby-2.5.7/gems/did_you_mean-1.2.0/did_you_mean.gemspec +=================================================================== +--- ruby-2.5.7.orig/gems/did_you_mean-1.2.0/did_you_mean.gemspec ++++ ruby-2.5.7/gems/did_you_mean-1.2.0/did_you_mean.gemspec +@@ -11,7 +11,7 @@ Gem::Specification.new do |s| + s.date = "2017-12-13" + s.description = "The gem that has been saving people from typos since 2014.".freeze + s.email = ["mail@yukinishijima.net".freeze] +- s.files = [".gitignore".freeze, ".ruby-version".freeze, ".travis.yml".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE.txt".freeze, "README.md".freeze, "Rakefile".freeze, "benchmark/jaro_winkler/memory_usage.rb".freeze, "benchmark/jaro_winkler/speed.rb".freeze, "benchmark/levenshtein/memory_usage.rb".freeze, "benchmark/levenshtein/speed.rb".freeze, "benchmark/memory_usage.rb".freeze, "did_you_mean.gemspec".freeze, "doc/CHANGELOG.md.erb".freeze, "doc/changelog_generator.rb".freeze, "evaluation/calculator.rb".freeze, "evaluation/dictionary_generator.rb".freeze, "evaluation/incorrect_words.yaml".freeze, "lib/did_you_mean.rb".freeze, "lib/did_you_mean/core_ext/name_error.rb".freeze, "lib/did_you_mean/experimental.rb".freeze, "lib/did_you_mean/experimental/initializer_name_correction.rb".freeze, "lib/did_you_mean/experimental/ivar_name_correction.rb".freeze, "lib/did_you_mean/formatters/plain_formatter.rb".freeze, "lib/did_you_mean/formatters/verbose_formatter.rb".freeze, "lib/did_you_mean/jaro_winkler.rb".freeze, "lib/did_you_mean/levenshtein.rb".freeze, "lib/did_you_mean/spell_checker.rb".freeze, "lib/did_you_mean/spell_checkers/key_error_checker.rb".freeze, "lib/did_you_mean/spell_checkers/method_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/null_checker.rb".freeze, "lib/did_you_mean/verbose.rb".freeze, "lib/did_you_mean/verbose_formatter.rb".freeze, "lib/did_you_mean/version.rb".freeze, "test/core_ext/name_error_extension_test.rb".freeze, "test/edit_distance/jaro_winkler_test.rb".freeze, "test/experimental/initializer_name_correction_test.rb".freeze, "test/experimental/method_name_checker_test.rb".freeze, "test/spell_checker_test.rb".freeze, "test/spell_checking/class_name_check_test.rb".freeze, "test/spell_checking/key_name_check_test.rb".freeze, "test/spell_checking/method_name_check_test.rb".freeze, "test/spell_checking/uncorrectable_name_check_test.rb".freeze, "test/spell_checking/variable_name_check_test.rb".freeze, "test/test_helper.rb".freeze, "test/verbose_formatter_test.rb".freeze] ++ s.files = [".gitignore".freeze, ".ruby-version".freeze, ".travis.yml".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE.txt".freeze, "README.md".freeze, "Rakefile".freeze, "benchmark/jaro_winkler/memory_usage.rb".freeze, "benchmark/jaro_winkler/speed.rb".freeze, "benchmark/levenshtein/memory_usage.rb".freeze, "benchmark/levenshtein/speed.rb".freeze, "benchmark/memory_usage.rb".freeze, "did_you_mean.gemspec".freeze, "doc/CHANGELOG.md.erb".freeze, "doc/changelog_generator.rb".freeze, "evaluation/calculator.rb".freeze, "evaluation/dictionary_generator.rb".freeze, "evaluation/incorrect_words.yaml".freeze, "lib/did_you_mean.rb".freeze, "lib/did_you_mean/core_ext/name_error.rb".freeze, "lib/did_you_mean/experimental.rb".freeze, "lib/did_you_mean/experimental/initializer_name_correction.rb".freeze, "lib/did_you_mean/experimental/ivar_name_correction.rb".freeze, "lib/did_you_mean/formatters/plain_formatter.rb".freeze, "lib/did_you_mean/formatters/verbose_formatter.rb".freeze, "lib/did_you_mean/jaro_winkler.rb".freeze, "lib/did_you_mean/levenshtein.rb".freeze, "lib/did_you_mean/spell_checker.rb".freeze, "lib/did_you_mean/spell_checkers/key_error_checker.rb".freeze, "lib/did_you_mean/spell_checkers/method_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb".freeze, "lib/did_you_mean/spell_checkers/null_checker.rb".freeze, "lib/did_you_mean/verbose.rb".freeze, "lib/did_you_mean/verbose_formatter.rb".freeze, "lib/did_you_mean/version.rb".freeze, "test/core_ext/name_error_extension_test.rb".freeze, "test/edit_distance/jaro_winkler_test.rb".freeze, "test/experimental/initializer_name_correction_test.rb".freeze, "test/experimental/method_name_checker_test.rb".freeze, "test/spell_checker_test.rb".freeze, "test/spell_checking/class_name_check_test.rb".freeze, "test/spell_checking/key_name_check_test.rb".freeze, "test/spell_checking/method_name_check_test.rb".freeze, "test/spell_checking/uncorrectable_name_check_test.rb".freeze, "test/spell_checking/variable_name_check_test.rb".freeze, "test/test_helper.rb".freeze, "test/verbose_formatter_test.rb".freeze].reject{|path| path.start_with?('evaluation/') } + s.homepage = "https://github.com/yuki24/did_you_mean".freeze + s.licenses = ["MIT".freeze] + s.required_ruby_version = Gem::Requirement.new(">= 2.5.0dev".freeze) diff --git a/ruby2.5-default.macros b/ruby2.5-default.macros new file mode 100644 index 0000000..e729e49 --- /dev/null +++ b/ruby2.5-default.macros @@ -0,0 +1,7 @@ +%rb_default_ruby_suffix ruby2.5 + +%rb_default_ruby ruby25 +%rb_default_ruby_abi ruby:2.5.0 + +%rb_build_versions %rb_default_ruby +%rb_build_ruby_abis %rb_default_ruby_abi diff --git a/ruby2.5-rpmlintrc b/ruby2.5-rpmlintrc new file mode 100644 index 0000000..045f9b7 --- /dev/null +++ b/ruby2.5-rpmlintrc @@ -0,0 +1,2 @@ +addFilter("files-duplicate /usr/share/ri/.*") +addFilter("unexpanded-macro /usr/share/ri/.*") diff --git a/ruby2.5.changes b/ruby2.5.changes new file mode 100644 index 0000000..d4052e0 --- /dev/null +++ b/ruby2.5.changes @@ -0,0 +1,326 @@ +* Fri Oct 20 2023 mrueckert@suse.de +- update suse.patch to 531fb8b2cc + - fix quadratic behavior in the uri parser (boo#1209891 + CVE-2023-28755) + - fix expensive regexp in the RFC2822 time parser (boo#1209967 + CVE-2023-28756) + - backport date 2.0.3 (boo#1193035 CVE-2021-41817) + - merge CGI 0.1.0.2: (boo#1205726 CVE-2021-33621) + - When parsing cookies, only decode the values + - HTTP response splitting in CGI +* Mon Sep 5 2022 mrueckert@suse.de +- Update suse.patch to 41adc98ad1: + - Cookie Prefix Spoofing in CGI::Cookie.parse (boo#1193081 CVE-2021-41819) +- add back some lost chunks to the suse.patch +* Wed Apr 20 2022 mrueckert@suse.de +- Update suse.patch: + - backport fix for CVE-2022-28739: ruby: Buffer overrun in + String-to-Float conversion (boo#1198441) + - back port date 2.0.3 CVE-2021-41817 (boo#1193035) + - merge the previous bug fixes into suse.patch + - CVE-2021-32066.patch + - CVE-2021-31810.patch + - CVE-2021-31799.patch +* Wed Apr 20 2022 mrueckert@suse.de +- Add Requires to make and gcc to ruby-devel to make the default + extconf.rb work +* Thu Nov 11 2021 ali.abdallah@suse.com + Add patches to fix the following CVE's: + - CVE-2021-32066.patch (CVE-2021-32066): Fix StartTLS stripping + vulnerability in Net:IMAP (bsc#1188160) + - CVE-2021-31810.patch (CVE-2021-31810): Fix trusting FTP PASV + responses vulnerability in Net:FTP (bsc#1188161) + - CVE-2021-31799.patch (CVE-2021-31799): Fix Command injection + vulnerability in RDoc (bsc#1190375) +* Tue Apr 13 2021 mrueckert@suse.de +- Update to 2.5.9 (boo#1184644) + https://www.ruby-lang.org/en/news/2021/04/05/ruby-2-5-9-released/ + - CVE-2020-25613: Potential HTTP Request Smuggling Vulnerability + in WEBrick + - CVE-2021-28965: XML round-trip vulnerability in REXML + Complete list of changes at + https://github.com/ruby/ruby/compare/v2_5_8...v2_5_9 +- Update suse.patch: + Remove fix for CVE-2020-25613 as it is included in the update +* Mon Mar 15 2021 mrueckert@suse.de +- Update suse.patch: (boo#1177125) + Backport fix CVE-2020-25613: Potential HTTP Request Smuggling + Vulnerability in WEBrick +* Fri Oct 16 2020 mrueckert@suse.de +- replace all patches with suse.patch (v2_5_8..2.5-suse) + (we keep remove-unneeded-files.patch as it can not be done in our + backports branch) +- backport patch to enable optimizations also on ARM64 + (boo#1177222) +* Tue Apr 28 2020 mrueckert@suse.de +- make sure that update-alternative weight for the default + distribution is always greater than our normal weight +* Tue Apr 28 2020 mrueckert@suse.de +- make the update-alternative weight based on the ruby version +* Tue Apr 7 2020 mrueckert@suse.de +- Update to 2.5.8 (boo#1167244 boo#1168938) + - CVE-2020-10663: Unsafe Object Creation Vulnerability in JSON + (Additional fix) + - CVE-2020-10933: Heap exposure vulnerability in the socket + library + https://github.com/ruby/ruby/compare/v2_5_7...v2_5_8 +- drop CVE-2020-8130.patch and rake-12.3.0.gem: included upstream +* Fri Mar 6 2020 mrueckert@suse.de +- Fix CVE-2020-8130 (boo# 1164804) for the intree copy of rake: + - add CVE-2020-8130.patch and rake-12.3.0.gem +* Thu Feb 6 2020 mrueckert@suse.de +- remove test files which are not needed at runtime (boo#1162396) + - adds remove-unneeded-files.patch and did_you_mean-1.2.0.gem +* Tue Oct 8 2019 mrueckert@suse.de +- update to 2.5.7 + - https://www.ruby-lang.org/en/news/2019/10/01/ruby-2-5-7-released/ + - CVE-2019-16255: A code injection vulnerability of Shell#[] + and Shell#test (boo#1152990) + - CVE-2019-16254: HTTP response splitting in WEBrick + (Additional fix) (boo#1152992) + - CVE-2019-15845: A NUL injection vulnerability of File.fnmatch + and File.fnmatch? (boo#1152994) + - CVE-2019-16201: Regular Expression Denial of Service + vulnerability of WEBrick’s Digest access authentication + (boo#1152995) + - https://www.ruby-lang.org/en/news/2019/08/28/ruby-2-5-6-released/ + - Multiple jQuery vulnerabilities in RDoc (CVE-2012-6708 + CVE-2015-9251) +* Tue Jul 9 2019 mrueckert@suse.de +- fix running tests (boo#1140844) + just passing the DISABLED_TESTS variable is wrong. probably a + relict from calling the test scripts directly. use TESTOPTS now. +* Thu Jun 13 2019 mrueckert@suse.de +- refreshed patches with new patch series: + 0001-make-gem-build-reproducible.patch + 0002-gc.c-tick-for-POWER-arch.patch + 0003-Mark-Gemspec-reproducible-change-fixing-784225-too.patch + 0004-Make-gemspecs-reproducible.patch +- rename patch now that it is generated from git: + old: 450160263aed8c446ce5b142d71f921ab4118f3a.patch + new: 0005-Include-the-alternative-malloc-header-instead-of-mal.patch + old: use-pie.patch + new: 0006-Use-PIE-for-the-binaries.patch +- ruby: change over of the Japanese Era to the new emperor May + 1st 2019 (boo#1133790) + 0007-date-support-for-Reiwa-new-Japanese-era.patch +* Wed Mar 27 2019 mrueckert@suse.de +- for some reason the --enable-pie option does not work as + expected. Fix this for now with a patch that just injects the + - pie flag in the Makefile (adds use-pie.patch) (boo#1130028) +* Fri Mar 15 2019 mrueckert@suse.de +- update to 2.5.5 + https://www.ruby-lang.org/en/news/2019/03/13/ruby-2-5-4-released/ + - CVE-2019-8320: Delete directory using symlink when + decompressing tar (boo#1130627) + - CVE-2019-8321: Escape sequence injection vulnerability in + verbose (boo#1130623) + - CVE-2019-8322: Escape sequence injection vulnerability in gem + owner (boo#1130622) + - CVE-2019-8323: Escape sequence injection vulnerability in API + response handling (boo#1130620) + - CVE-2019-8324: Installing a malicious gem may lead to arbitrary + code execution (boo#1130617) + - CVE-2019-8325: Escape sequence injection vulnerability in + errors (boo#1130611) + https://www.ruby-lang.org/en/news/2019/03/15/ruby-2-5-5-released/ +* Thu Feb 7 2019 mrueckert@suse.de +- replace the awk based provides generation with the new file-attr + handler in ruby-bundled-gems-rpmhelper + This kills one provides rubygem-name = version + But this should not have be used since a while anymore. +- add option to build without docs for testing +- provide support to undo the split of the stdlib: + pass --without=separate_stdlib to "osc build" +* Fri Nov 23 2018 mliska@suse.cz +- Use parallel make. +* Thu Nov 22 2018 mliska@suse.cz +- Disable compressed sections as they are not supported by rpm + (https://bugs.ruby-lang.org/issues/12934). +* Wed Nov 7 2018 mrueckert@suse.de +- update to 2.5.3 + This release includes some bug fixes and some security fixes. + - CVE-2018-16396: Tainted flags are not propagated in Array#pack + and String#unpack with some directives (boo#1112532) + - CVE-2018-16395: OpenSSL::X509::Name equality check does not + work correctly (boo#1112530) + https://github.com/ruby/ruby/compare/v2_5_1...v2_5_3 +- drop frozen-pop3.patch +* Tue Oct 16 2018 mrueckert@suse.de +- backport 450160263aed8c446ce5b142d71f921ab4118f3a.patch: + Include the alternative malloc header instead of malloc.h +* Tue Aug 28 2018 mrueckert@suse.de +- update to 2.5.1 + This release includes some bug fixes and some security fixes. + - CVE-2017-17742: HTTP response splitting in WEBrick + (boo#1087434) + - CVE-2018-6914: Unintentional file and directory creation with + directory traversal in tempfile and tmpdir (boo#1087441) + - CVE-2018-8777: DoS by large request in WEBrick (boo#1087436) + - CVE-2018-8778: Buffer under-read in String#unpack (boo#1087433) + - CVE-2018-8779: Unintentional socket creation by poisoned NUL + byte in UNIXServer and UNIXSocket (boo#1087440) + - CVE-2018-8780: Unintentional directory traversal by poisoned + NUL byte in Dir (boo#1087437) + - Multiple vulnerabilities in RubyGems + CVE-2018-1000079 (boo#1082058) + CVE-2018-1000075 (boo#1082014) + CVE-2018-1000078 (boo#1082011) + CVE-2018-1000077 (boo#1082010) + CVE-2018-1000076 (boo#1082009) + CVE-2018-1000074 (boo#1082008) + CVE-2018-1000073 (boo#1082007) + https://github.com/ruby/ruby/compare/v2_5_0...v2_5_1 +* Tue Aug 28 2018 mrueckert@suse.de +- added frozen-pop3.patch: + Net::POPMail methods modify frozen literal when using default arg + https://redmine.ruby-lang.org/issues/14416 +* Thu Mar 22 2018 mrueckert@suse.de +- wrong files where installed from the macro files after adding + dump-version.rb +* Thu Jan 25 2018 mrueckert@suse.de +- fix dump-versions.rb: it was picking up system rdoc versions on + some source dirs +* Mon Jan 15 2018 mrueckert@suse.de +- add reproducible build patches from debian + 0003-Mark-Gemspec-reproducible-change-fixing-784225-too.patch + 0004-Make-gemspecs-reproducible.patch +* Mon Dec 25 2017 mrueckert@suse.de +- drop 316f58076d29dcff053256992d9ec19fed7e698f.patch +- no longer bundling bundler +* Mon Dec 25 2017 mrueckert@suse.de +- update to 2.5.0 final + Ruby 2.5.0 is the first stable release of the Ruby 2.5 series. It + introduces many new features and performance improvements. The + notable changes are as follows: + - New Features + - rescue/else/ensure are now allowed to be used directly with + do/end blocks. [Feature #12906] + - Add yield_self to yield given block in its context. Unlike + tap, it returns the result of the block. [Feature #6721] + - Support branch coverage and method coverage measurement. The + branch coverage indicates which branches are executed and + which are not. The method coverage indicates which methods + are invoked and which are not. By running the test suite with + these new features, you will know which branches and methods + are executed, and evaluate total coverage of the test suite + more strictly. [Feature #13901] + - Hash#slice [Feature #8499] and Hash#transform_keys [Feature + [#13583]] + - Struct.new can create classes that accept keyword arguments. + [Feature #11925] + - Enumerable#any?,all?,none? and one? accept a pattern argument + [Feature #11286] + - Top-level constant look-up is no longer available. [Feature + [#11547]] + - One of our most loved libraries, pp.rb, is now automatically + loaded. You no longer have to write require "pp". [Feature + [#14123]] + - Print backtrace and error message in reverse order (oldest + call first, most recent call last). When a long backtrace + appears on your terminal (TTY), you can easily find the cause + line at the bottom of the backtrace. Note that the order is + reversed only when backtrace is printed out to the terminal + directly. [Feature #8661] [experimental] + - Performance improvements + - About 5-10%% performance improvement by removing all trace + instructions from overall bytecode (instruction sequences). + The trace instruction was added to support the TracePoint. + However, in most cases, TracePoint is not used and trace + instructions are pure overhead. Instead, now we use a dynamic + instrumentation technique. See [Feature #14104] for more + details. + - Block passing by a block parameter (e.g. def foo(&b); + bar(&b); end) is about 3 times faster than Ruby 2.4 by “Lazy + Proc allocation” technique. [Feature #14045] + - Mutex is rewritten to be smaller and faster. [Feature #13517] + - ERB now generates code from a template which runs twice as + fast as Ruby 2.4. + - Improve performance of some built-in methods including + Array#concat, Enumerable#sort_by, String#concat, + String#index, Time#+ and more. + - IO.copy_stream uses copy_file_range(2) to copy offload + [Feature #13867] + - Other notable changes since 2.4 + - SecureRandom now prefers OS-provided sources over OpenSSL. + [Bug #9569] + - Promote cmath, csv, date, dbm, etc, fcntl, fiddle, fileutils, + gdbm, ipaddr, scanf, sdbm, stringio, strscan, webrick, zlib + from standard libraries to default gems. + - Update to Onigmo 6.1.3. + - It adds the absence operator. + - Note that Ruby 2.4.1 also includes this change. + - Update to Psych 3.0.2. + - Update to RubyGems 2.7.3. + - Update to RDoc 6.0.1. + - Switch the lexer from IRB based one to Ripper. This + dramatically improves the performance of document + generation. + - Fix a significant amount of bugs that existed over ten + years. + - Add support for new Ruby syntax from the latest versions. + - Update supported Unicode version to 10.0.0. + - Thread.report_on_exception is now set to true by default. + This change helps debugging of multi-threaded programs. + [Feature #14143] + - IO#write now receives multiple arguments [Feature #9323] For + details see: https://github.com/ruby/ruby/blob/v2_5_0/NEWS + https://github.com/ruby/ruby/compare/v2_4_0...v2_5_0 +* Tue Dec 19 2017 mrueckert@suse.de +- switch to https urls +* Mon Dec 18 2017 mrueckert@suse.de +- update to 2.5.0~rc1 + https://www.ruby-lang.org/en/news/2017/12/14/ruby-2-5-0-rc1-released/ +- added 316f58076d29dcff053256992d9ec19fed7e698f.patch + to fix building rbtrace and ruby-prof +* Mon Nov 20 2017 mrueckert@suse.de +- disable jemalloc again because of: (boo#1068883) + https://github.com/jemalloc/jemalloc/issues/937 +* Fri Nov 17 2017 mrueckert@suse.de +- update to 60813 + see installed /usr/share/doc/packages/ruby2.5/ChangeLog +* Wed Nov 15 2017 mrueckert@suse.de +- update to 60739 + see installed /usr/share/doc/packages/ruby2.5/ChangeLog +* Wed Nov 15 2017 mrueckert@suse.de +- make the whole u-a handling less error prone by having the list + in variable ua-binaries +* Thu Nov 9 2017 jdelvare@suse.de +- Add conflicts to libruby to make sure ruby and ruby-stdlib are + also updated when libruby is updated (bsc#1048072.) +* Thu Nov 9 2017 mrueckert@suse.de +- exclude all testsuites for the stdlib gems +* Wed Nov 8 2017 mrueckert@suse.de +- build jemalloc +* Tue Oct 31 2017 mrueckert@suse.de +- update to 60568 + see installed /usr/share/doc/packages/ruby2.5/ChangeLog + - this fixes the "ruby -rubygems" on 2.5 +* Wed Oct 4 2017 mrueckert@suse.de +- update intree gem list +- bundler is now part of core too! +* Wed Oct 4 2017 mrueckert@suse.de +- update to r60035: + see installed /usr/share/doc/packages/ruby2.5/ChangeLog +- revert some of the wrong Group changes +- drop autoreconf -fi and the buildrequires for the related + packages +* Fri Sep 22 2017 mrueckert@suse.de +- make it easier to sync the versions from the gemspec with the + spec file: + ruby dump-versions.rb $unpacked_tarball_dir +* Fri Sep 22 2017 mrueckert@suse.de +- add conflicts for all intree gems +* Mon Sep 11 2017 jengelh@inai.de +- Fix RPM groups. Replace old RPM macros by modern ones. +- Ensure neutrality of descriptions. +* Fri Sep 8 2017 mrueckert@suse.de +- update to 59623 +* Wed Aug 9 2017 mrueckert@suse.de +- fix gem provides +- install macro files with 2.5 version +* Wed Aug 9 2017 mrueckert@suse.de +- initial package (Fate#324013) +- port 2 patches we still need from the 2.4 package: + 0001-make-gem-build-reproducible.patch + 0002-gc.c-tick-for-POWER-arch.patch diff --git a/ruby2.5.macros b/ruby2.5.macros new file mode 100644 index 0000000..f9439c2 --- /dev/null +++ b/ruby2.5.macros @@ -0,0 +1,16 @@ +# +# IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT +# +# if you change any macros here you have to update the copy in the +# prjconf aswell. +# +# IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT +# +%rubygemsruby25() rubygem(ruby:2.5.0:%{expand:%%rubygemsx%*} %{expand:%%{rubygems%*}} +%rubygemsxruby25() %{expand:%%{rubygemsx%*}} + +%rubyruby25() ruby2.5 %{expand:%%rubyx%*} %{expand:%%{ruby%*}} +%rubyxruby25() %{expand:%%{rubyx%*}} + +%rubydevelruby25() ruby2.5-devel %{expand:%%rubydevelx%*} %{expand:%%{rubydevel%*}} +%rubydevelxruby25() %{expand:%%{rubydevelx%*}} diff --git a/ruby2.5.spec b/ruby2.5.spec new file mode 100644 index 0000000..8718cc3 --- /dev/null +++ b/ruby2.5.spec @@ -0,0 +1,456 @@ +# +# spec file for package ruby2.5 +# +# Copyright (c) 2022-2023 ZhuningOS +# + + +Name: ruby2.5 + +#### +#!!! +#!!! when updating, check versions of embedded rubygems in package stdlib below +#!!! +#### + +%define patch_level p0 +Version: 2.5.9 +Release: 150000.4.29.1 +%define pkg_version %{version} +# make the exported API version explicit +%define api_version 2.5.0 +%define rb_binary_suffix .ruby2.5 +%define rb_soname ruby2.5 +%define _rb_ua_weight 25 + +# ruby-macros and ruby-common version +%define rpm_macros_version 3 + +# +%define libname libruby2_5-2_5 +# keep in sync with macro file! +%define rb_ver %{api_version} +%define rb_arch %(echo %{_target_cpu}-linux-gnu | sed -e "s/ppc/powerpc/") +%define rb_arch_short %(echo %{_target_cpu}-linux | sed -e "s/ppc/powerpc/ ; s/i[0-9]86/x86/") +%define rb_libdir %{_libdir}/ruby/%{rb_ver}/ +%define rb_archdir %{_libdir}/ruby/%{rb_ver}/%{rb_arch} +%define rb_extdir %{_libdir}/ruby/gems/%{rb_ver}/extensions/ +%define rb_extarchdir %{_libdir}/ruby/gems/%{rb_ver}/extensions/%{rb_arch_short}/ +%define rb_extversionedarchdir %{_libdir}/ruby/gems/%{rb_ver}/extensions/%{rb_arch_short}/%{rb_ver} +%define rb_extdocdir %{_libdir}/ruby/gems/%{rb_ver}/doc/extensions +%define rb_extarchdocdir %{_libdir}/ruby/gems/%{rb_ver}/doc/extensions/%{rb_arch_short} + +%if "%{rb_default_ruby_suffix}" == "%{rb_soname}" +%define is_default_ruby 1 +%define rb_ua_weight 1%{_rb_ua_weight} +%else +%define rb_ua_weight %{_rb_ua_weight} +%endif + +%define ua_binaries rake rdoc ri +# keep in sync with macro file! +# +# from valgrind.spec +%ifarch %ix86 x86_64 ppc ppc64 +%define use_valgrind 1 +%endif +# turn on testsuite by default. we dont hard fail anyway. +%bcond_without run_tests +%bcond_without build_docs +%if 0%{?is_opensuse} || 0%{?sle_version} >= 150000 +%bcond_with jemalloc +%else +%bcond_with jemalloc +%endif +%bcond_without separate_stdlib + +# +BuildRequires: gdbm-devel +BuildRequires: ruby-bundled-gems-rpmhelper +%if %{with jemalloc} +BuildRequires: jemalloc-devel +%endif +BuildRequires: libffi-devel +BuildRequires: libyaml-devel +BuildRequires: ncurses-devel +BuildRequires: netcfg +BuildRequires: openssl-devel +BuildRequires: pkg-config +BuildRequires: procps +BuildRequires: readline-devel +BuildRequires: timezone +BuildRequires: zlib-devel +# this requires is needed as distros older than 11.3 have a buildignore on freetype2, without this the detection of the tk extension fails +BuildRequires: freetype2-devel +%if 0%{?suse_version} > 1010 +BuildRequires: xorg-x11-libX11-devel +%else +BuildRequires: xorg-x11-devel +%endif +%if 0%{?use_valgrind} +%if 0%{?suse_version} > 1021 +BuildRequires: valgrind-devel +%else +BuildRequires: valgrind +%endif +%endif +BuildRequires: xz +Provides: ruby(abi) = %{rb_ver} +%if 0%{?suse_version} > 1130 +Conflicts: ruby(abi) = %{rb_ver} +%endif +Provides: ruby25 = %{version}-%{release} +%if 0%{?is_default_ruby} +Provides: ruby-default = %{version}-%{release} +Conflicts: otherproviders(ruby-default) +%endif +Requires: %{libname} = %{version} +%if %{with separate_stdlib} +Requires: %{name}-stdlib = %{version} +%else +Provides: %{name}-stdlib = %{version}-%{release} +Obsoletes: %{name}-stdlib < %{version}-%{release} +%endif +PreReq: update-alternatives +BuildRequires: update-alternatives +Requires: ruby-common + +# +Url: https://www.ruby-lang.org/ +Source: https://cache.ruby-lang.org/pub/ruby/2.5/ruby-%{pkg_version}.tar.xz +# the file was created by applying all patches and then running gem build in the gems/did_you_mean-1.2.0 directory +Source1: did_you_mean-1.2.0.gem +# +Source3: %{name}.macros +Source4: %{name}-default.macros +Source98: series +Source99: %{name}-rpmlintrc + +Patch01: suse.patch +# this can not be in our backports git as the files are not there yet +Patch02: remove-unneeded-files.patch +# +Summary: An Interpreted Object-Oriented Scripting Language +License: BSD-2-Clause OR Ruby +Group: Development/Languages/Ruby + +%description +Ruby is an interpreted scripting language for object-oriented programming. It +has many features for processing text files and performing system management +tasks (as in Perl). It is extensible. + +* Ruby features: + +- Simple Syntax + +- *Normal* Object-Oriented features (class, method calls, for + example) + +- *Advanced* Object-Oriented features(Mix-in, Singleton-method, for + example) + +- Operator Overloading + +- Exception Handling + +- Iterators and Closures + +- Garbage Collection + +- Dynamic Loading of Object Files (on some architectures) + +%package -n %{libname} +Summary: Dynamic runtime library for Ruby +# Both ruby and ruby-stdlib now depend explicitly on libruby by version, +# however it was not always the case, so the conflicts statements below +# are needed to handle updates. +Group: System/Libraries +Conflicts: %{name} < %{version}-%{release} +Conflicts: %{name}-stdlib < %{version}-%{release} + +%description -n %{libname} +Dynamic runtime library for Ruby + +%package doc +Summary: Documentation and samples for Ruby +Group: Development/Languages/Ruby + +%description doc +Documentation and samples for Ruby + +%if %{with separate_stdlib} +%package stdlib +Summary: The Ruby standard library +Group: Development/Languages/Ruby +Provides: ruby-stdlib = %{version}-%{release} +Requires: %{libname} = %{version} + +%description stdlib +The Ruby standard library +%endif + +%package devel +Summary: Development files to link against Ruby +Group: Development/Languages/Ruby +Requires: %{name} = %{version} +Provides: rubygems = 2.5.2 +Provides: rubygems_with_buildroot_patch +%if %{with jemalloc} +Requires: jemalloc-devel +%endif +# extconf.rb defaults to make +Requires: make +Requires: gcc + +%description devel +Development files to link against Ruby. + +%package devel-extra +Summary: Special development files of ruby, normally not installed +Group: Development/Languages/Ruby +Requires: %{name}-devel = %{version} + +%description devel-extra +Development files to link against Ruby. + +%package doc-ri +Summary: Ruby Interactive Documentation +Group: Development/Languages/Ruby +Requires: %{name} = %{version} +%if 0%{?suse_version} >= 1121 +BuildArch: noarch +%endif + +%description doc-ri +This package contains the RI docs for ruby + +%package doc-html +Summary: This package contains the HTML docs for ruby +Group: Development/Languages/Ruby +Requires: %{name} = %{version} +%if 0%{?suse_version} >= 1121 +BuildArch: noarch +%endif + +%description doc-html +This package contains the HTML docs for ruby + +%package examples +Summary: Example scripts for ruby +Group: Development/Languages/Ruby +Requires: %{name} = %{version} +%if 0%{?suse_version} >= 1121 +BuildArch: noarch +%endif + +%description examples +Example scripts for ruby + +%package test-suite +Requires: %{name} = %{version} +Summary: An Interpreted Object-Oriented Scripting Language +Group: Development/Languages/Ruby +%if 0%{?suse_version} >= 1121 +BuildArch: noarch +%endif + +%description test-suite +Ruby is an interpreted scripting language for object-oriented programming. It +has many features for processing text files and performing system management +tasks (as in Perl). It is extensible. + +* Ruby features: + +- Simple Syntax + +- *Normal* Object-Oriented features (class, method calls, for + example) + +- *Advanced* Object-Oriented features(Mix-in, Singleton-method, for + example) + +- Operator Overloading + +- Exception Handling + +- Iterators and Closures + +- Garbage Collection + +- Dynamic Loading of Object Files (on some architectures) + +%prep +%autosetup -p1 -n ruby-%{pkg_version} +find sample -type f -print0 | xargs -r0 chmod a-x +grep -Erl '^#! */' benchmark bootstraptest ext lib sample test \ + | xargs -r perl -p -i -e 's|^#!\s*\S+(\s+.*)?$|#!/usr/bin/ruby%{rb_binary_suffix} $1|' + +%build +rm -rv gems/did_you_mean-1.2.0/evaluation +cp %{SOURCE1} gems/ +# iseq.c needs -fno-strict-aliasing +export LANG="en_US.UTF-8" +export LC_ALL="en_US.UTF-8" +export LC_CTYPE="en_US.UTF-8" +export CFLAGS="%{optflags} -fno-strict-aliasing" +%configure \ + %if %{with jemalloc} + --with-jemalloc \ + %endif + --program-suffix="%{rb_binary_suffix}" \ + --with-soname=%{rb_soname} \ + --target=%{_target_platform} \ + %if 0%{?use_valgrind} + --with-valgrind \ + %endif + %if ! %{with build_docs} + --disable-install-doc \ + --disable-install-rdoc \ + --disable-install-capi \ + %endif + --with-mantype=man \ + --enable-shared \ + --disable-static \ + --disable-rpath \ + --with-compress-debug-sections=no +make all V=1 %{?_smp_mflags} + +%install +%makeinstall V=1 +echo "%defattr(-,root,root,-)" > devel-extra-excludes +echo "%defattr(-,root,root,-)" > devel-extra-list +for i in iseq.h insns.inc insns_info.inc revision.h version.h thread_pthread.h \ + ruby_atomic.h method.h internal.h id.h vm_core.h vm_opts.h node.h eval_intern.h vm_debug.h; do + install -m 644 $i %{buildroot}%{_includedir}/ruby-%{rb_ver}/ + echo "%exclude %{_includedir}/ruby-%{rb_ver}/$i" >> devel-extra-excludes + echo "%{_includedir}/ruby-%{rb_ver}/$i" >> devel-extra-list +done + +# Create gem native extensions dir +# use shell scripting here as we're executing the just installed binary +%define rb_binary %{buildroot}/usr/bin/ruby -I %{buildroot}%{rb_libdir} -I %{buildroot}%{rb_archdir} +export LD_LIBRARY_PATH=%{buildroot}%{_libdir} + +install -D -m 0644 %{S:3} %{buildroot}/etc/rpm/macros.suse-ruby2.5 +mkdir -p %{buildroot}%{_sysconfdir}/alternatives +%if 0%{?is_default_ruby} + install -D -m 0644 %{S:4} %{buildroot}/etc/rpm/macros.suse-ruby2.5-default + for bin in %{buildroot}%{_bindir}/{erb,gem,irb,ruby}%{rb_binary_suffix} ; do + # yes really hard links + ln $bin ${bin%%%{rb_binary_suffix}} + done + for man in %{buildroot}%{_mandir}/man1/*%{rb_binary_suffix}.1* ; do + # yes really hard links + # TODO: this is dangerous as we cant anc + ln $man ${man%%%{rb_binary_suffix}.1}.1 + done + ln -s lib%{rb_soname}.so %{buildroot}%{_libdir}/libruby.so +%endif +for bin in %{ua_binaries}; do + # dummy + mv %{buildroot}%_bindir/${bin}%{rb_binary_suffix} %{buildroot}%_bindir/$bin.ruby%{rb_binary_suffix} + # + ln -s $bin %{buildroot}%{_sysconfdir}/alternatives/$bin + ln -s %{_sysconfdir}/alternatives/$bin %{buildroot}%_bindir/$bin + # + ln -s ${bin}%{rb_binary_suffix} %{buildroot}%{_sysconfdir}/alternatives//$bin%{rb_binary_suffix} + ln -s %{_sysconfdir}/alternatives/$bin%{rb_binary_suffix} %{buildroot}%_bindir/$bin%{rb_binary_suffix} +done +install -dD %{buildroot}%{rb_extdir} %{buildroot}%{rb_extarchdir} %{buildroot}%{rb_extversionedarchdir} %{buildroot}%{rb_extarchdocdir} +chmod -vR go-w,go+rX %{buildroot}%{_libdir}/ruby + +%post +for bin in %{ua_binaries}; do + /usr/sbin/update-alternatives --install \ + %{_bindir}/$bin $bin %{_bindir}/$bin.ruby%{rb_binary_suffix} %{rb_ua_weight} + /usr/sbin/update-alternatives --install \ + %{_bindir}/$bin%{rb_binary_suffix} $bin%{rb_binary_suffix} %{_bindir}/$bin.ruby%{rb_binary_suffix} %{rb_ua_weight} +done + +%preun +if [ "$1" = 0 ] ; then + for bin in %{ua_binaries}; do + /usr/sbin/update-alternatives --remove $bin %{_bindir}/$bin.ruby%{rb_binary_suffix} + /usr/sbin/update-alternatives --remove $bin%{rb_binary_suffix} %{_bindir}/$bin.ruby%{rb_binary_suffix} + done +fi + +%if %{with run_tests} +%check +DISABLE_TESTS="" +%ifarch armv7l armv7hl armv7hnl +# test_call_double(DL::TestDL) fails on ARM HardFP +# http://bugs.ruby-lang.org/issues/6592 +DISABLE_TESTS="-x test_dl2.rb $DISABLE_TESTS" + +# Workaround OpenSSL::TestPKeyRSA#test_sign_verify_memory_leak timeouts on ARM. +# https://bugs.ruby-lang.org/issues/9984 +sed -i -e 's|20_000|10_000|g' test/openssl/test_pkey_rsa.rb +%endif +# Allow MD5 in OpenSSL. +# https://bugs.ruby-lang.org/issues/9154 +export OPENSSL_ENABLE_MD5_VERIFY=1 +export LD_LIBRARY_PATH="$PWD" +# we know some tests will fail when they do not find a /usr/bin/ruby +make -j 4 check V=1 TESTOPTS="$DISABLE_TESTS" ||: +%endif + +%post -n %{libname} -p /sbin/ldconfig +%postun -n %{libname} -p /sbin/ldconfig + +%files +%defattr(-,root,root,-) +%ghost %{_sysconfdir}/alternatives/rake* +%ghost %{_sysconfdir}/alternatives/rdoc* +%ghost %{_sysconfdir}/alternatives/ri* +%{_bindir}/erb* +%{_bindir}/gem* +%{_bindir}/irb* +%{_bindir}/rake* +%{_bindir}/rdoc* +%{_bindir}/ri* +%{_bindir}/ruby* +%{_mandir}/man1/ri*.1* +%{_mandir}/man1/irb*.1* +%{_mandir}/man1/erb*.1* +#{_mandir}/man1/rake*.1* +%{_mandir}/man1/ruby*.1* +%doc ChangeLog COPYING COPYING.ja GPL KNOWNBUGS.rb LEGAL NEWS README* +%config /etc/rpm/macros.suse-ruby2.5* + +%if %{with separate_stdlib} +%files stdlib +%defattr(-,root,root,-) +%endif +%{_libdir}/ruby/ +%exclude %{_libdir}/ruby/gems/%{api_version}/gems/*/test/ +%dir %rb_extdir +%dir %rb_extarchdir +%dir %rb_extversionedarchdir +%dir %rb_extdocdir +%dir %rb_extarchdocdir + +%files -n %{libname} +%defattr(-,root,root,-) +%{_libdir}/libruby*.so.* + +%files devel -f devel-extra-excludes +%defattr(-,root,root,-) +%{_includedir}/ruby-%{rb_ver} +%{_libdir}/libruby*.so +%{_libdir}/pkgconfig/ruby-2.5.pc + +%files devel-extra -f devel-extra-list + +%if %{with build_docs} +%files doc +%defattr(-,root,root,-) +%doc doc/* sample/ + +%files doc-ri +%defattr(-,root,root,-) +%dir %{_datadir}/ri/ +%{_datadir}/ri/%{rb_ver}/ +%endif + +%changelog diff --git a/series b/series new file mode 100644 index 0000000..a2c7b87 --- /dev/null +++ b/series @@ -0,0 +1,11 @@ +# Patch series file for quilt, created by quilt setup +# Source: ruby-2.5.5.tar.xz +# Patchdir: ruby-2.5.5 +# +0001-make-gem-build-reproducible.patch +0002-gc.c-tick-for-POWER-arch.patch +0003-Mark-Gemspec-reproducible-change-fixing-784225-too.patch +0004-Make-gemspecs-reproducible.patch +0005-Include-the-alternative-malloc-header-instead-of-mal.patch +0006-Use-PIE-for-the-binaries.patch +0007-date-support-for-Reiwa-new-Japanese-era.patch diff --git a/suse.patch b/suse.patch new file mode 100644 index 0000000..15cb83f --- /dev/null +++ b/suse.patch @@ -0,0 +1,3844 @@ +diff --git a/Makefile.in b/Makefile.in +index 25075f5900..515cac832b 100644 +--- a/Makefile.in ++++ b/Makefile.in +@@ -233,7 +233,7 @@ miniruby$(EXEEXT): + $(PROGRAM): + @$(RM) $@ + $(ECHO) linking $@ +- $(Q) $(PURIFY) $(CC) $(LDFLAGS) $(XLDFLAGS) $(MAINOBJ) $(EXTOBJS) $(LIBRUBYARG) $(MAINLIBS) $(LIBS) $(EXTLIBS) $(OUTFLAG)$@ ++ $(Q) $(PURIFY) $(CC) -pie $(LDFLAGS) $(XLDFLAGS) $(MAINOBJ) $(EXTOBJS) $(LIBRUBYARG) $(MAINLIBS) $(LIBS) $(EXTLIBS) $(OUTFLAG)$@ + $(Q) $(POSTLINK) + + # We must `rm' the library each time this rule is invoked because "updating" a +diff --git a/ext/bigdecimal/bigdecimal.gemspec b/ext/bigdecimal/bigdecimal.gemspec +index c8c90870ea..9cf22f7e0a 100644 +--- a/ext/bigdecimal/bigdecimal.gemspec ++++ b/ext/bigdecimal/bigdecimal.gemspec +@@ -6,6 +6,7 @@ + s.name = "bigdecimal" + s.version = bigdecimal_version + s.authors = ["Kenta Murata", "Zachary Scott", "Shigeo Kobayashi"] ++ s.date = RUBY_RELEASE_DATE + s.email = ["mrkn@mrkn.jp"] + + s.summary = "Arbitrary-precision decimal floating-point number library." +diff --git a/ext/cgi/escape/escape.c b/ext/cgi/escape/escape.c +index ced1b182eb..578d651dc1 100644 +--- a/ext/cgi/escape/escape.c ++++ b/ext/cgi/escape/escape.c +@@ -11,6 +11,21 @@ RUBY_EXTERN const signed char ruby_digit36_to_number_table[]; + static VALUE rb_cCGI, rb_mUtil, rb_mEscape; + static ID id_accept_charset; + ++#define HTML_ESCAPE_MAX_LEN 6 ++ ++static const struct { ++ uint8_t len; ++ char str[HTML_ESCAPE_MAX_LEN+1]; ++} html_escape_table[UCHAR_MAX+1] = { ++#define HTML_ESCAPE(c, str) [c] = {rb_strlen_lit(str), str} ++ HTML_ESCAPE('\'', "'"), ++ HTML_ESCAPE('&', "&"), ++ HTML_ESCAPE('"', """), ++ HTML_ESCAPE('<', "<"), ++ HTML_ESCAPE('>', ">"), ++#undef HTML_ESCAPE ++}; ++ + static void + html_escaped_cat(VALUE str, char c) + { +@@ -44,40 +59,35 @@ preserve_original_state(VALUE orig, VALUE dest) + static VALUE + optimized_escape_html(VALUE str) + { +- long i, len, beg = 0; +- VALUE dest = 0; +- const char *cstr; +- +- len = RSTRING_LEN(str); +- cstr = RSTRING_PTR(str); +- +- for (i = 0; i < len; i++) { +- switch (cstr[i]) { +- case '\'': +- case '&': +- case '"': +- case '<': +- case '>': +- if (!dest) { +- dest = rb_str_buf_new(len); +- } +- +- rb_str_cat(dest, cstr + beg, i - beg); +- beg = i + 1; +- +- html_escaped_cat(dest, cstr[i]); +- break; +- } ++ VALUE vbuf; ++ typedef char escape_buf[HTML_ESCAPE_MAX_LEN]; ++ char *buf = *ALLOCV_N(escape_buf, vbuf, RSTRING_LEN(str)); ++ const char *cstr = RSTRING_PTR(str); ++ const char *end = cstr + RSTRING_LEN(str); ++ ++ char *dest = buf; ++ while (cstr < end) { ++ const unsigned char c = *cstr++; ++ uint8_t len = html_escape_table[c].len; ++ if (len) { ++ memcpy(dest, html_escape_table[c].str, len); ++ dest += len; ++ } ++ else { ++ *dest++ = c; ++ } + } + +- if (dest) { +- rb_str_cat(dest, cstr + beg, len - beg); +- preserve_original_state(str, dest); +- return dest; ++ VALUE escaped; ++ if (RSTRING_LEN(str) < (dest - buf)) { ++ escaped = rb_str_new(buf, dest - buf); ++ preserve_original_state(str, escaped); + } + else { +- return rb_str_dup(str); ++ escaped = rb_str_dup(str); + } ++ ALLOCV_END(vbuf); ++ return escaped; + } + + static VALUE +diff --git a/ext/date/date_core.c b/ext/date/date_core.c +index c250633426..1734ec0349 100644 +--- a/ext/date/date_core.c ++++ b/ext/date/date_core.c +@@ -51,6 +51,9 @@ static double positive_inf, negative_inf; + #define f_add3(x,y,z) f_add(f_add(x, y), z) + #define f_sub3(x,y,z) f_sub(f_sub(x, y), z) + ++static VALUE date_initialize(int argc, VALUE *argv, VALUE self); ++static VALUE datetime_initialize(int argc, VALUE *argv, VALUE self); ++ + inline static int + f_cmp(VALUE x, VALUE y) + { +@@ -94,7 +97,7 @@ f_ge_p(VALUE x, VALUE y) + { + if (FIXNUM_P(x) && FIXNUM_P(y)) + return f_boolcast(FIX2LONG(x) >= FIX2LONG(y)); +- return rb_funcall(x, rb_intern(">="), 1, y); ++ return rb_funcall(x, id_ge_p, 1, y); + } + + inline static VALUE +@@ -102,7 +105,7 @@ f_eqeq_p(VALUE x, VALUE y) + { + if (FIXNUM_P(x) && FIXNUM_P(y)) + return f_boolcast(FIX2LONG(x) == FIX2LONG(y)); +- return rb_funcall(x, rb_intern("=="), 1, y); ++ return rb_funcall(x, id_eqeq_p, 1, y); + } + + inline static VALUE +@@ -236,11 +239,8 @@ f_negative_p(VALUE x) + struct SimpleDateData + { + unsigned flags; +- VALUE nth; /* not always canonicalized */ + int jd; /* as utc */ +- /* df is zero */ +- /* sf is zero */ +- /* of is zero */ ++ VALUE nth; /* not always canonicalized */ + date_sg_t sg; /* 2298874..2426355 or -/+oo -- at most 22 bits */ + /* decoded as utc=local */ + int year; /* truncated */ +@@ -259,11 +259,8 @@ struct SimpleDateData + struct ComplexDateData + { + unsigned flags; +- VALUE nth; /* not always canonicalized */ + int jd; /* as utc */ +- int df; /* as utc, in secs */ +- VALUE sf; /* in nano secs */ +- int of; /* in secs */ ++ VALUE nth; /* not always canonicalized */ + date_sg_t sg; /* 2298874..2426355 or -/+oo -- at most 22 bits */ + /* decoded as local */ + int year; /* truncated */ +@@ -277,6 +274,9 @@ struct ComplexDateData + /* packed civil */ + unsigned pc; + #endif ++ int df; /* as utc, in secs */ ++ int of; /* in secs */ ++ VALUE sf; /* in nano secs */ + }; + + union DateData { +@@ -315,31 +315,31 @@ canon(VALUE x) + + #ifndef USE_PACK + #define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \ + (x)->jd = _jd;\ + (x)->sg = (date_sg_t)(_sg);\ + (x)->year = _year;\ + (x)->mon = _mon;\ + (x)->mday = _mday;\ +- (x)->flags = _flags;\ +-} ++ (x)->flags = (_flags) & ~COMPLEX_DAT;\ ++} while (0) + #else + #define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \ + (x)->jd = _jd;\ + (x)->sg = (date_sg_t)(_sg);\ + (x)->year = _year;\ + (x)->pc = PACK2(_mon, _mday);\ +- (x)->flags = _flags;\ +-} ++ (x)->flags = (_flags) & ~COMPLEX_DAT;\ ++} while (0) + #endif + + #ifndef USE_PACK + #define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\ + _year, _mon, _mday, _hour, _min, _sec, _flags) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\ + (x)->jd = _jd;\ + (x)->df = _df;\ +@@ -352,12 +352,12 @@ _year, _mon, _mday, _hour, _min, _sec, _flags) \ + (x)->hour = _hour;\ + (x)->min = _min;\ + (x)->sec = _sec;\ +- (x)->flags = _flags;\ +-} ++ (x)->flags = (_flags) | COMPLEX_DAT;\ ++} while (0) + #else + #define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\ + _year, _mon, _mday, _hour, _min, _sec, _flags) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\ + (x)->jd = _jd;\ + (x)->df = _df;\ +@@ -366,13 +366,13 @@ _year, _mon, _mday, _hour, _min, _sec, _flags) \ + (x)->sg = (date_sg_t)(_sg);\ + (x)->year = _year;\ + (x)->pc = PACK5(_mon, _mday, _hour, _min, _sec);\ +- (x)->flags = _flags;\ +-} ++ (x)->flags = (_flags) | COMPLEX_DAT;\ ++} while (0) + #endif + + #ifndef USE_PACK + #define copy_simple_to_complex(obj, x, y) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ + (x)->jd = (y)->jd;\ + (x)->df = 0;\ +@@ -386,10 +386,10 @@ _year, _mon, _mday, _hour, _min, _sec, _flags) \ + (x)->min = 0;\ + (x)->sec = 0;\ + (x)->flags = (y)->flags;\ +-} ++} while (0) + #else + #define copy_simple_to_complex(obj, x, y) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ + (x)->jd = (y)->jd;\ + (x)->df = 0;\ +@@ -399,12 +399,12 @@ _year, _mon, _mday, _hour, _min, _sec, _flags) \ + (x)->year = (y)->year;\ + (x)->pc = PACK5(EX_MON((y)->pc), EX_MDAY((y)->pc), 0, 0, 0);\ + (x)->flags = (y)->flags;\ +-} ++} while (0) + #endif + + #ifndef USE_PACK + #define copy_complex_to_simple(obj, x, y) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ + (x)->jd = (y)->jd;\ + (x)->sg = (date_sg_t)((y)->sg);\ +@@ -412,17 +412,17 @@ _year, _mon, _mday, _hour, _min, _sec, _flags) \ + (x)->mon = (y)->mon;\ + (x)->mday = (y)->mday;\ + (x)->flags = (y)->flags;\ +-} ++} while (0) + #else + #define copy_complex_to_simple(obj, x, y) \ +-{\ ++do {\ + RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ + (x)->jd = (y)->jd;\ + (x)->sg = (date_sg_t)((y)->sg);\ + (x)->year = (y)->year;\ + (x)->pc = PACK2(EX_MON((y)->pc), EX_MDAY((y)->pc));\ + (x)->flags = (y)->flags;\ +-} ++} while (0) + #endif + + /* base */ +@@ -1109,7 +1109,7 @@ m_virtual_sg(union DateData *x) + } + + #define canonicalize_jd(_nth, _jd) \ +-{\ ++do {\ + if (_jd < 0) {\ + _nth = f_sub(_nth, INT2FIX(1));\ + _jd += CM_PERIOD;\ +@@ -1118,7 +1118,7 @@ m_virtual_sg(union DateData *x) + _nth = f_add(_nth, INT2FIX(1));\ + _jd -= CM_PERIOD;\ + }\ +-} ++} while (0) + + inline static void + canonicalize_s_jd(VALUE obj, union DateData *x) +@@ -1928,13 +1928,13 @@ m_sec(union DateData *x) + } + + #define decode_offset(of,s,h,m)\ +-{\ ++do {\ + int a;\ + s = (of < 0) ? '-' : '+';\ + a = (of < 0) ? -of : of;\ + h = a / HOUR_IN_SECONDS;\ + m = a % HOUR_IN_SECONDS / MINUTE_IN_SECONDS;\ +-} ++} while (0) + + static VALUE + of2str(int of) +@@ -2333,6 +2333,9 @@ VALUE date_zone_to_diff(VALUE); + static int + offset_to_sec(VALUE vof, int *rof) + { ++ int try_rational = 1; ++ ++ again: + switch (TYPE(vof)) { + case T_FIXNUM: + { +@@ -2359,10 +2362,11 @@ offset_to_sec(VALUE vof, int *rof) + default: + expect_numeric(vof); + vof = f_to_r(vof); +-#ifdef CANONICALIZATION_FOR_MATHN +- if (!k_rational_p(vof)) +- return offset_to_sec(vof, rof); +-#endif ++ if (!k_rational_p(vof)) { ++ if (!try_rational) Check_Type(vof, T_RATIONAL); ++ try_rational = 0; ++ goto again; ++ } + /* fall through */ + case T_RATIONAL: + { +@@ -2371,17 +2375,10 @@ offset_to_sec(VALUE vof, int *rof) + + vs = day_to_sec(vof); + +-#ifdef CANONICALIZATION_FOR_MATHN + if (!k_rational_p(vs)) { +- if (!FIXNUM_P(vs)) +- return 0; +- n = FIX2LONG(vs); +- if (n < -DAY_IN_SECONDS || n > DAY_IN_SECONDS) +- return 0; +- *rof = (int)n; +- return 1; ++ vn = vs; ++ goto rounded; + } +-#endif + vn = rb_rational_num(vs); + vd = rb_rational_den(vs); + +@@ -2391,6 +2388,7 @@ offset_to_sec(VALUE vof, int *rof) + vn = f_round(vs); + if (!f_eqeq_p(vn, vs)) + rb_warning("fraction of offset is ignored"); ++ rounded: + if (!FIXNUM_P(vn)) + return 0; + n = FIX2LONG(vn); +@@ -2420,12 +2418,12 @@ offset_to_sec(VALUE vof, int *rof) + /* date */ + + #define valid_sg(sg) \ +-{\ ++do {\ + if (!c_valid_start_p(sg)) {\ + sg = 0;\ + rb_warning("invalid start is ignored");\ + }\ +-} ++} while (0) + + static VALUE + valid_jd_sub(int argc, VALUE *argv, VALUE klass, int need_jd) +@@ -2968,7 +2966,7 @@ d_simple_new_internal(VALUE klass, + + obj = TypedData_Make_Struct(klass, struct SimpleDateData, + &d_lite_type, dat); +- set_to_simple(obj, dat, nth, jd, sg, y, m, d, flags & ~COMPLEX_DAT); ++ set_to_simple(obj, dat, nth, jd, sg, y, m, d, flags); + + assert(have_jd_p(dat) || have_civil_p(dat)); + +@@ -2990,7 +2988,7 @@ d_complex_new_internal(VALUE klass, + obj = TypedData_Make_Struct(klass, struct ComplexDateData, + &d_lite_type, dat); + set_to_complex(obj, dat, nth, jd, df, sf, of, sg, +- y, m, d, h, min, s, flags | COMPLEX_DAT); ++ y, m, d, h, min, s, flags); + + assert(have_jd_p(dat) || have_civil_p(dat)); + assert(have_df_p(dat) || have_time_p(dat)); +@@ -3207,47 +3205,47 @@ s_trunc(VALUE s, VALUE *fr) + } + + #define num2num_with_frac(s,n) \ +-{\ ++do {\ + s = s##_trunc(v##s, &fr);\ + if (f_nonzero_p(fr)) {\ + if (argc > n)\ + rb_raise(rb_eArgError, "invalid fraction");\ + fr2 = fr;\ + }\ +-} ++} while (0) + + #define num2int_with_frac(s,n) \ +-{\ ++do {\ + s = NUM2INT(s##_trunc(v##s, &fr));\ + if (f_nonzero_p(fr)) {\ + if (argc > n)\ + rb_raise(rb_eArgError, "invalid fraction");\ + fr2 = fr;\ + }\ +-} ++} while (0) + + #define canon24oc() \ +-{\ ++do {\ + if (rh == 24) {\ + rh = 0;\ + fr2 = f_add(fr2, INT2FIX(1));\ + }\ +-} ++} while (0) + + #define add_frac() \ +-{\ ++do {\ + if (f_nonzero_p(fr2))\ + ret = d_lite_plus(ret, fr2);\ +-} ++} while (0) + + #define val2sg(vsg,dsg) \ +-{\ ++do {\ + dsg = NUM2DBL(vsg);\ + if (!c_valid_start_p(dsg)) {\ + dsg = DEFAULT_SG;\ + rb_warning("invalid start is ignored");\ + }\ +-} ++} while (0) + + static VALUE d_lite_plus(VALUE, VALUE); + +@@ -3384,10 +3382,21 @@ date_s_ordinal(int argc, VALUE *argv, VALUE klass) + */ + static VALUE + date_s_civil(int argc, VALUE *argv, VALUE klass) ++{ ++ return date_initialize(argc, argv, d_lite_s_alloc_simple(klass)); ++} ++ ++static VALUE ++date_initialize(int argc, VALUE *argv, VALUE self) + { + VALUE vy, vm, vd, vsg, y, fr, fr2, ret; + int m, d; + double sg; ++ struct SimpleDateData *dat = rb_check_typeddata(self, &d_lite_type); ++ ++ if (!simple_dat_p(dat)) { ++ rb_raise(rb_eTypeError, "Date expected"); ++ } + + rb_scan_args(argc, argv, "04", &vy, &vm, &vd, &vsg); + +@@ -3417,11 +3426,7 @@ date_s_civil(int argc, VALUE *argv, VALUE klass) + &rm, &rd)) + rb_raise(rb_eArgError, "invalid date"); + +- ret = d_simple_new_internal(klass, +- nth, 0, +- sg, +- ry, rm, rd, +- HAVE_CIVIL); ++ set_to_simple(self, dat, nth, 0, sg, ry, rm, rd, HAVE_CIVIL); + } + else { + VALUE nth; +@@ -3433,12 +3438,9 @@ date_s_civil(int argc, VALUE *argv, VALUE klass) + &ns)) + rb_raise(rb_eArgError, "invalid date"); + +- ret = d_simple_new_internal(klass, +- nth, rjd, +- sg, +- ry, rm, rd, +- HAVE_JD | HAVE_CIVIL); ++ set_to_simple(self, dat, nth, rjd, sg, ry, rm, rd, HAVE_JD | HAVE_CIVIL); + } ++ ret = self; + add_frac(); + return ret; + } +@@ -3679,9 +3681,11 @@ date_s_today(int argc, VALUE *argv, VALUE klass) + #define ref_hash0(k) rb_hash_aref(hash, k) + #define del_hash0(k) rb_hash_delete(hash, k) + +-#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k)), v) +-#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k))) +-#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k))) ++#define sym(x) ID2SYM(rb_intern(x"")) ++ ++#define set_hash(k,v) set_hash0(sym(k), v) ++#define ref_hash(k) ref_hash0(sym(k)) ++#define del_hash(k) del_hash0(sym(k)) + + static VALUE + rt_rewrite_frags(VALUE hash) +@@ -3718,8 +3722,6 @@ rt_rewrite_frags(VALUE hash) + return hash; + } + +-#define sym(x) ID2SYM(rb_intern(x)) +- + static VALUE d_lite_year(VALUE); + static VALUE d_lite_wday(VALUE); + static VALUE d_lite_jd(VALUE); +@@ -4290,12 +4292,40 @@ date_s_strptime(int argc, VALUE *argv, VALUE klass) + + VALUE date__parse(VALUE str, VALUE comp); + ++static size_t ++get_limit(VALUE opt) ++{ ++ if (!NIL_P(opt)) { ++ VALUE limit = rb_hash_aref(opt, ID2SYM(rb_intern("limit"))); ++ if (NIL_P(limit)) return SIZE_MAX; ++ return NUM2SIZET(limit); ++ } ++ return 128; ++} ++ ++static void ++check_limit(VALUE str, VALUE opt) ++{ ++ if (NIL_P(str)) return; ++ if (SYMBOL_P(str)) str = rb_sym2str(str); ++ ++ StringValue(str); ++ size_t slen = RSTRING_LEN(str); ++ size_t limit = get_limit(opt); ++ if (slen > limit) { ++ rb_raise(rb_eArgError, ++ "string length (%"PRI_SIZE_PREFIX"u) exceeds the limit %"PRI_SIZE_PREFIX"u", slen, limit); ++ } ++} ++ + static VALUE + date_s__parse_internal(int argc, VALUE *argv, VALUE klass) + { +- VALUE vstr, vcomp, hash; ++ VALUE vstr, vcomp, hash, opt; + +- rb_scan_args(argc, argv, "11", &vstr, &vcomp); ++ rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt); ++ if (!NIL_P(opt)) argc--; ++ check_limit(vstr, opt); + StringValue(vstr); + if (!rb_enc_str_asciicompat_p(vstr)) + rb_raise(rb_eArgError, +@@ -4320,7 +4350,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass) + + /* + * call-seq: +- * Date._parse(string[, comp=true]) -> hash ++ * Date._parse(string[, comp=true], limit: 128) -> hash + * + * Parses the given representation of date and time, and returns a + * hash of parsed elements. This method does not function as a +@@ -4331,6 +4361,10 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass) + * it full. + * + * Date._parse('2001-02-03') #=> {:year=>2001, :mon=>2, :mday=>3} ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s__parse(int argc, VALUE *argv, VALUE klass) +@@ -4340,7 +4374,7 @@ date_s__parse(int argc, VALUE *argv, VALUE klass) + + /* + * call-seq: +- * Date.parse(string='-4712-01-01'[, comp=true[, start=Date::ITALY]]) -> date ++ * Date.parse(string='-4712-01-01'[, comp=true[, start=Date::ITALY]], limit: 128) -> date + * + * Parses the given representation of date and time, and creates a + * date object. This method does not function as a validator. +@@ -4352,13 +4386,18 @@ date_s__parse(int argc, VALUE *argv, VALUE klass) + * Date.parse('2001-02-03') #=> # + * Date.parse('20010203') #=> # + * Date.parse('3rd Feb 2001') #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_parse(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, comp, sg; ++ VALUE str, comp, sg, opt; + +- rb_scan_args(argc, argv, "03", &str, &comp, &sg); ++ rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -4370,11 +4409,12 @@ date_s_parse(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE argv2[2], hash; +- +- argv2[0] = str; +- argv2[1] = comp; +- hash = date_s__parse(2, argv2, klass); ++ int argc2 = 2; ++ VALUE argv2[3]; ++ argv2[0] = str; ++ argv2[1] = comp; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__parse(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } +@@ -4388,19 +4428,28 @@ VALUE date__jisx0301(VALUE); + + /* + * call-seq: +- * Date._iso8601(string) -> hash ++ * Date._iso8601(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__iso8601(VALUE klass, VALUE str) ++date_s__iso8601(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__iso8601(str); + } + + /* + * call-seq: +- * Date.iso8601(string='-4712-01-01'[, start=Date::ITALY]) -> date ++ * Date.iso8601(string='-4712-01-01'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some typical ISO 8601 formats. +@@ -4408,13 +4457,18 @@ date_s__iso8601(VALUE klass, VALUE str) + * Date.iso8601('2001-02-03') #=> # + * Date.iso8601('20010203') #=> # + * Date.iso8601('2001-W05-6') #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_iso8601(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -4424,38 +4478,56 @@ date_s_iso8601(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__iso8601(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__iso8601(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * Date._rfc3339(string) -> hash ++ * Date._rfc3339(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__rfc3339(VALUE klass, VALUE str) ++date_s__rfc3339(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__rfc3339(str); + } + + /* + * call-seq: +- * Date.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY]) -> date ++ * Date.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some typical RFC 3339 formats. + * + * Date.rfc3339('2001-02-03T04:05:06+07:00') #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_rfc3339(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -4465,38 +4537,56 @@ date_s_rfc3339(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__rfc3339(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__rfc3339(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * Date._xmlschema(string) -> hash ++ * Date._xmlschema(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__xmlschema(VALUE klass, VALUE str) ++date_s__xmlschema(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__xmlschema(str); + } + + /* + * call-seq: +- * Date.xmlschema(string='-4712-01-01'[, start=Date::ITALY]) -> date ++ * Date.xmlschema(string='-4712-01-01'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some typical XML Schema formats. + * + * Date.xmlschema('2001-02-03') #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_xmlschema(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -4506,41 +4596,58 @@ date_s_xmlschema(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__xmlschema(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__xmlschema(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * Date._rfc2822(string) -> hash +- * Date._rfc822(string) -> hash ++ * Date._rfc2822(string, limit: 128) -> hash ++ * Date._rfc822(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__rfc2822(VALUE klass, VALUE str) ++date_s__rfc2822(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__rfc2822(str); + } + + /* + * call-seq: +- * Date.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY]) -> date +- * Date.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY]) -> date ++ * Date.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) -> date ++ * Date.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some typical RFC 2822 formats. + * + * Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_rfc2822(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); + + switch (argc) { + case 0: +@@ -4550,39 +4657,56 @@ date_s_rfc2822(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__rfc2822(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__rfc2822(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * Date._httpdate(string) -> hash ++ * Date._httpdate(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__httpdate(VALUE klass, VALUE str) ++date_s__httpdate(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__httpdate(str); + } + + /* + * call-seq: +- * Date.httpdate(string='Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY]) -> date ++ * Date.httpdate(string='Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some RFC 2616 format. + * + * Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_httpdate(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); + + switch (argc) { + case 0: +@@ -4592,38 +4716,60 @@ date_s_httpdate(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__httpdate(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__httpdate(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * Date._jisx0301(string) -> hash ++ * Date._jisx0301(string, limit: 128) -> hash + * + * Returns a hash of parsed elements. ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE +-date_s__jisx0301(VALUE klass, VALUE str) ++date_s__jisx0301(int argc, VALUE *argv, VALUE klass) + { ++ VALUE str, opt; ++ ++ rb_scan_args(argc, argv, "1:", &str, &opt); ++ check_limit(str, opt); ++ + return date__jisx0301(str); + } + + /* + * call-seq: +- * Date.jisx0301(string='-4712-01-01'[, start=Date::ITALY]) -> date ++ * Date.jisx0301(string='-4712-01-01'[, start=Date::ITALY], limit: 128) -> date + * + * Creates a new Date object by parsing from a string according to + * some typical JIS X 0301 formats. + * + * Date.jisx0301('H13.02.03') #=> # ++ * ++ * For no-era year, legacy format, Heisei is assumed. ++ * ++ * Date.jisx0301('13.02.03') #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + date_s_jisx0301(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -4633,7 +4779,11 @@ date_s_jisx0301(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__jisx0301(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ if (!NIL_P(opt)) argv2[argc2++] = opt; ++ VALUE hash = date_s__jisx0301(argc2, argv2, klass); + return d_new_by_frags(klass, hash, sg); + } + } +@@ -4691,14 +4841,14 @@ dup_obj_as_complex(VALUE self) + } + + #define val2off(vof,iof) \ +-{\ ++do {\ + if (!offset_to_sec(vof, &iof)) {\ + iof = 0;\ + rb_warning("invalid offset is ignored");\ + }\ +-} ++} while (0) + +-#ifndef NDEBUG ++#if 0 + static VALUE + d_lite_initialize(int argc, VALUE *argv, VALUE self) + { +@@ -4751,7 +4901,7 @@ d_lite_initialize(int argc, VALUE *argv, VALUE self) + "cannot load complex into simple"); + + set_to_complex(self, &dat->c, nth, rjd, df, sf, of, sg, +- 0, 0, 0, 0, 0, 0, HAVE_JD | HAVE_DF | COMPLEX_DAT); ++ 0, 0, 0, 0, 0, 0, HAVE_JD | HAVE_DF); + } + } + return self; +@@ -4770,8 +4920,28 @@ d_lite_initialize_copy(VALUE copy, VALUE date) + { + get_d2(copy, date); + if (simple_dat_p(bdat)) { +- adat->s = bdat->s; +- adat->s.flags &= ~COMPLEX_DAT; ++ if (simple_dat_p(adat)) { ++ adat->s = bdat->s; ++ } ++ else { ++ adat->c.flags = bdat->s.flags | COMPLEX_DAT; ++ adat->c.nth = bdat->s.nth; ++ adat->c.jd = bdat->s.jd; ++ adat->c.df = 0; ++ adat->c.sf = INT2FIX(0); ++ adat->c.of = 0; ++ adat->c.sg = bdat->s.sg; ++ adat->c.year = bdat->s.year; ++#ifndef USE_PACK ++ adat->c.mon = bdat->s.mon; ++ adat->c.mday = bdat->s.mday; ++ adat->c.hour = bdat->s.hour; ++ adat->c.min = bdat->s.min; ++ adat->c.sec = bdat->s.sec; ++#else ++ adat->c.pc = bdat->s.pc; ++#endif ++ } + } + else { + if (!complex_dat_p(adat)) +@@ -4779,7 +4949,6 @@ d_lite_initialize_copy(VALUE copy, VALUE date) + "cannot load complex into simple"); + + adat->c = bdat->c; +- adat->c.flags |= COMPLEX_DAT; + } + } + return copy; +@@ -5513,8 +5682,10 @@ d_lite_new_offset(int argc, VALUE *argv, VALUE self) + static VALUE + d_lite_plus(VALUE self, VALUE other) + { ++ int try_rational = 1; + get_d1(self); + ++ again: + switch (TYPE(other)) { + case T_FIXNUM: + { +@@ -5724,18 +5895,21 @@ d_lite_plus(VALUE self, VALUE other) + default: + expect_numeric(other); + other = f_to_r(other); +-#ifdef CANONICALIZATION_FOR_MATHN +- if (!k_rational_p(other)) +- return d_lite_plus(self, other); +-#endif ++ if (!k_rational_p(other)) { ++ if (!try_rational) Check_Type(other, T_RATIONAL); ++ try_rational = 0; ++ goto again; ++ } + /* fall through */ + case T_RATIONAL: + { + VALUE nth, sf, t; + int jd, df, s; + +- if (wholenum_p(other)) +- return d_lite_plus(self, rb_rational_num(other)); ++ if (wholenum_p(other)) { ++ other = rb_rational_num(other); ++ goto again; ++ } + + if (f_positive_p(other)) + s = +1; +@@ -6242,7 +6416,7 @@ cmp_gen(VALUE self, VALUE other) + return INT2FIX(f_cmp(m_ajd(dat), other)); + else if (k_date_p(other)) + return INT2FIX(f_cmp(m_ajd(dat), f_ajd(other))); +- return rb_num_coerce_cmp(self, other, rb_intern("<=>")); ++ return rb_num_coerce_cmp(self, other, id_cmp); + } + + static VALUE +@@ -6371,7 +6545,7 @@ equal_gen(VALUE self, VALUE other) + return f_eqeq_p(m_real_local_jd(dat), other); + else if (k_date_p(other)) + return f_eqeq_p(m_real_local_jd(dat), f_jd(other)); +- return rb_num_coerce_cmp(self, other, rb_intern("==")); ++ return rb_num_coerce_cmp(self, other, id_eqeq_p); + } + + /* +@@ -6469,7 +6643,7 @@ d_lite_to_s(VALUE self) + static VALUE + mk_inspect_raw(union DateData *x, VALUE klass) + { +- char flags[5]; ++ char flags[6]; + + flags[0] = (x->flags & COMPLEX_DAT) ? 'C' : 'S'; + flags[1] = (x->flags & HAVE_JD) ? 'j' : '-'; +@@ -6635,7 +6809,9 @@ tmx_m_of(union DateData *x) + static char * + tmx_m_zone(union DateData *x) + { +- return RSTRING_PTR(m_zone(x)); ++ VALUE zone = m_zone(x); ++ /* TODO: fix potential dangling pointer */ ++ return RSTRING_PTR(zone); + } + + static const struct tmx_funcs tmx_funcs = { +@@ -6785,7 +6961,7 @@ date_strftime_internal(int argc, VALUE *argv, VALUE self, + * + * %M - Minute of the hour (00..59) + * +- * %S - Second of the minute (00..59) ++ * %S - Second of the minute (00..60) + * + * %L - Millisecond of the second (000..999) + * %N - Fractional seconds digits, default is 9 digits (nanosecond) +@@ -7018,10 +7194,14 @@ jisx0301_date_format(char *fmt, size_t size, VALUE jd, VALUE y) + c = 'S'; + s = 1925; + } +- else { ++ else if (d < 2458605) { + c = 'H'; + s = 1988; + } ++ else { ++ c = 'R'; ++ s = 2018; ++ } + snprintf(fmt, size, "%c%02ld" ".%%m.%%d", c, FIX2INT(y) - s); + return fmt; + } +@@ -7099,6 +7279,10 @@ d_lite_marshal_dump(VALUE self) + static VALUE + d_lite_marshal_load(VALUE self, VALUE a) + { ++ VALUE nth, sf; ++ int jd, df, of; ++ double sg; ++ + get_d1(self); + + rb_check_frozen(self); +@@ -7111,63 +7295,33 @@ d_lite_marshal_load(VALUE self, VALUE a) + case 2: /* 1.6.x */ + case 3: /* 1.8.x, 1.9.2 */ + { +- VALUE ajd, of, sg, nth, sf; +- int jd, df, rof; +- double rsg; +- ++ VALUE ajd, vof, vsg; + + if (RARRAY_LEN(a) == 2) { + ajd = f_sub(RARRAY_AREF(a, 0), half_days_in_day); +- of = INT2FIX(0); +- sg = RARRAY_AREF(a, 1); +- if (!k_numeric_p(sg)) +- sg = DBL2NUM(RTEST(sg) ? GREGORIAN : JULIAN); ++ vof = INT2FIX(0); ++ vsg = RARRAY_AREF(a, 1); ++ if (!k_numeric_p(vsg)) ++ vsg = DBL2NUM(RTEST(vsg) ? GREGORIAN : JULIAN); + } + else { + ajd = RARRAY_AREF(a, 0); +- of = RARRAY_AREF(a, 1); +- sg = RARRAY_AREF(a, 2); ++ vof = RARRAY_AREF(a, 1); ++ vsg = RARRAY_AREF(a, 2); + } + +- old_to_new(ajd, of, sg, +- &nth, &jd, &df, &sf, &rof, &rsg); +- +- if (!df && f_zero_p(sf) && !rof) { +- set_to_simple(self, &dat->s, nth, jd, rsg, 0, 0, 0, HAVE_JD); +- } else { +- if (!complex_dat_p(dat)) +- rb_raise(rb_eArgError, +- "cannot load complex into simple"); +- +- set_to_complex(self, &dat->c, nth, jd, df, sf, rof, rsg, +- 0, 0, 0, 0, 0, 0, +- HAVE_JD | HAVE_DF | COMPLEX_DAT); +- } ++ old_to_new(ajd, vof, vsg, ++ &nth, &jd, &df, &sf, &of, &sg); + } + break; + case 6: + { +- VALUE nth, sf; +- int jd, df, of; +- double sg; +- + nth = RARRAY_AREF(a, 0); + jd = NUM2INT(RARRAY_AREF(a, 1)); + df = NUM2INT(RARRAY_AREF(a, 2)); + sf = RARRAY_AREF(a, 3); + of = NUM2INT(RARRAY_AREF(a, 4)); + sg = NUM2DBL(RARRAY_AREF(a, 5)); +- if (!df && f_zero_p(sf) && !of) { +- set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD); +- } else { +- if (!complex_dat_p(dat)) +- rb_raise(rb_eArgError, +- "cannot load complex into simple"); +- +- set_to_complex(self, &dat->c, nth, jd, df, sf, of, sg, +- 0, 0, 0, 0, 0, 0, +- HAVE_JD | HAVE_DF | COMPLEX_DAT); +- } + } + break; + default: +@@ -7175,6 +7329,18 @@ d_lite_marshal_load(VALUE self, VALUE a) + break; + } + ++ if (simple_dat_p(dat)) { ++ if (df || !f_zero_p(sf) || of) { ++ rb_raise(rb_eArgError, ++ "cannot load complex into simple"); ++ } ++ set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD); ++ } else { ++ set_to_complex(self, &dat->c, nth, jd, df, sf, of, sg, ++ 0, 0, 0, 0, 0, 0, ++ HAVE_JD | HAVE_DF); ++ } ++ + if (FL_TEST(a, FL_EXIVAR)) { + rb_copy_generic_ivar(self, a); + FL_SET(self, FL_EXIVAR); +@@ -7354,10 +7520,21 @@ datetime_s_ordinal(int argc, VALUE *argv, VALUE klass) + */ + static VALUE + datetime_s_civil(int argc, VALUE *argv, VALUE klass) ++{ ++ return datetime_initialize(argc, argv, d_lite_s_alloc_complex(klass)); ++} ++ ++static VALUE ++datetime_initialize(int argc, VALUE *argv, VALUE self) + { + VALUE vy, vm, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret; + int m, d, h, min, s, rof; + double sg; ++ struct ComplexDateData *dat = rb_check_typeddata(self, &d_lite_type); ++ ++ if (!complex_dat_p(dat)) { ++ rb_raise(rb_eTypeError, "DateTime expected"); ++ } + + rb_scan_args(argc, argv, "08", &vy, &vm, &vd, &vh, &vmin, &vs, &vof, &vsg); + +@@ -7401,13 +7578,13 @@ datetime_s_civil(int argc, VALUE *argv, VALUE klass) + rb_raise(rb_eArgError, "invalid date"); + canon24oc(); + +- ret = d_complex_new_internal(klass, +- nth, 0, +- 0, INT2FIX(0), +- rof, sg, +- ry, rm, rd, +- rh, rmin, rs, +- HAVE_CIVIL | HAVE_TIME); ++ set_to_complex(self, dat, ++ nth, 0, ++ 0, INT2FIX(0), ++ rof, sg, ++ ry, rm, rd, ++ rh, rmin, rs, ++ HAVE_CIVIL | HAVE_TIME); + } + else { + VALUE nth; +@@ -7426,14 +7603,15 @@ datetime_s_civil(int argc, VALUE *argv, VALUE klass) + time_to_df(rh, rmin, rs), + rof); + +- ret = d_complex_new_internal(klass, +- nth, rjd2, +- 0, INT2FIX(0), +- rof, sg, +- ry, rm, rd, +- rh, rmin, rs, +- HAVE_JD | HAVE_CIVIL | HAVE_TIME); ++ set_to_complex(self, dat, ++ nth, rjd2, ++ 0, INT2FIX(0), ++ rof, sg, ++ ry, rm, rd, ++ rh, rmin, rs, ++ HAVE_JD | HAVE_CIVIL | HAVE_TIME); + } ++ ret = self; + add_frac(); + return ret; + } +@@ -7925,7 +8103,7 @@ datetime_s_strptime(int argc, VALUE *argv, VALUE klass) + + /* + * call-seq: +- * DateTime.parse(string='-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]]) -> datetime ++ * DateTime.parse(string='-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]], limit: 128) -> datetime + * + * Parses the given representation of date and time, and creates a + * DateTime object. This method does not function as a validator. +@@ -7939,13 +8117,18 @@ datetime_s_strptime(int argc, VALUE *argv, VALUE klass) + * #=> # + * DateTime.parse('3rd Feb 2001 04:05:06 PM') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_parse(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, comp, sg; ++ VALUE str, comp, sg, opt; + +- rb_scan_args(argc, argv, "03", &str, &comp, &sg); ++ rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -7957,18 +8140,20 @@ datetime_s_parse(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE argv2[2], hash; +- +- argv2[0] = str; +- argv2[1] = comp; +- hash = date_s__parse(2, argv2, klass); ++ int argc2 = 2; ++ VALUE argv2[3]; ++ argv2[0] = str; ++ argv2[1] = comp; ++ argv2[2] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__parse(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * DateTime.iso8601(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY]) -> datetime ++ * DateTime.iso8601(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) -> datetime + * + * Creates a new DateTime object by parsing from a string according to + * some typical ISO 8601 formats. +@@ -7979,13 +8164,18 @@ datetime_s_parse(int argc, VALUE *argv, VALUE klass) + * #=> # + * DateTime.iso8601('2001-W05-6T04:05:06+07:00') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_iso8601(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -7995,27 +8185,37 @@ datetime_s_iso8601(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__iso8601(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2--; ++ VALUE hash = date_s__iso8601(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * DateTime.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY]) -> datetime ++ * DateTime.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) -> datetime + * + * Creates a new DateTime object by parsing from a string according to + * some typical RFC 3339 formats. + * + * DateTime.rfc3339('2001-02-03T04:05:06+07:00') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -8025,27 +8225,37 @@ datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__rfc3339(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__rfc3339(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * DateTime.xmlschema(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY]) -> datetime ++ * DateTime.xmlschema(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) -> datetime + * + * Creates a new DateTime object by parsing from a string according to + * some typical XML Schema formats. + * + * DateTime.xmlschema('2001-02-03T04:05:06+07:00') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -8055,28 +8265,38 @@ datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__xmlschema(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__xmlschema(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * DateTime.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY]) -> datetime +- * DateTime.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY]) -> datetime ++ * DateTime.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) -> datetime ++ * DateTime.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) -> datetime + * + * Creates a new DateTime object by parsing from a string according to + * some typical RFC 2822 formats. + * + * DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -8086,7 +8306,12 @@ datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__rfc2822(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__rfc2822(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } +@@ -8100,13 +8325,18 @@ datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass) + * + * DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT') + * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_httpdate(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -8116,27 +8346,42 @@ datetime_s_httpdate(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__httpdate(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__httpdate(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } + + /* + * call-seq: +- * DateTime.jisx0301(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY]) -> datetime ++ * DateTime.jisx0301(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) -> datetime + * + * Creates a new DateTime object by parsing from a string according to + * some typical JIS X 0301 formats. + * + * DateTime.jisx0301('H13.02.03T04:05:06+07:00') + * #=> # ++ * ++ * For no-era year, legacy format, Heisei is assumed. ++ * ++ * DateTime.jisx0301('13.02.03T04:05:06+07:00') ++ * #=> # ++ * ++ * Raise an ArgumentError when the string length is longer than _limit_. ++ * You can stop this check by passing `limit: nil`, but note that ++ * it may take a long time to parse. + */ + static VALUE + datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass) + { +- VALUE str, sg; ++ VALUE str, sg, opt; + +- rb_scan_args(argc, argv, "02", &str, &sg); ++ rb_scan_args(argc, argv, "02:", &str, &sg, &opt); ++ if (!NIL_P(opt)) argc--; + + switch (argc) { + case 0: +@@ -8146,7 +8391,12 @@ datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass) + } + + { +- VALUE hash = date_s__jisx0301(klass, str); ++ int argc2 = 1; ++ VALUE argv2[2]; ++ argv2[0] = str; ++ argv2[1] = opt; ++ if (!NIL_P(opt)) argc2++; ++ VALUE hash = date_s__jisx0301(argc2, argv2, klass); + return dt_new_by_frags(klass, hash, sg); + } + } +@@ -8230,7 +8480,7 @@ dt_lite_to_s(VALUE self) + * + * %M - Minute of the hour (00..59) + * +- * %S - Second of the minute (00..59) ++ * %S - Second of the minute (00..60) + * + * %L - Millisecond of the second (000..999) + * %N - Fractional seconds digits, default is 9 digits (nanosecond) +@@ -8643,7 +8893,7 @@ datetime_to_date(VALUE self) + VALUE new = d_lite_s_alloc_simple(cDate); + { + get_d1b(new); +- copy_complex_to_simple(new, &bdat->s, &adat->c) ++ copy_complex_to_simple(new, &bdat->s, &adat->c); + bdat->s.jd = m_local_jd(adat); + bdat->s.flags &= ~(HAVE_DF | HAVE_TIME | COMPLEX_DAT); + return new; +@@ -9008,14 +9258,18 @@ mk_ary_of_str(long len, const char *a[]) + return o; + } + ++static VALUE ++d_lite_zero(VALUE x) ++{ ++ return INT2FIX(0); ++} ++ + void + Init_date_core(void) + { + #undef rb_intern + #define rb_intern(str) rb_intern_const(str) + +- assert(fprintf(stderr, "assert() is now active\n")); +- + id_cmp = rb_intern("<=>"); + id_le_p = rb_intern("<="); + id_ge_p = rb_intern(">="); +@@ -9231,23 +9485,22 @@ Init_date_core(void) + */ + rb_define_const(cDate, "GREGORIAN", DBL2NUM(GREGORIAN)); + +- rb_define_alloc_func(cDate, d_lite_s_alloc); ++ rb_define_alloc_func(cDate, d_lite_s_alloc_simple); + + #ifndef NDEBUG +-#define de_define_private_method rb_define_private_method +- de_define_private_method(CLASS_OF(cDate), "_valid_jd?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_jd?", + date_s__valid_jd_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_ordinal?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_ordinal?", + date_s__valid_ordinal_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_civil?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_civil?", + date_s__valid_civil_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_date?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_date?", + date_s__valid_civil_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_commercial?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_commercial?", + date_s__valid_commercial_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_weeknum?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_weeknum?", + date_s__valid_weeknum_p, -1); +- de_define_private_method(CLASS_OF(cDate), "_valid_nth_kday?", ++ rb_define_private_method(CLASS_OF(cDate), "_valid_nth_kday?", + date_s__valid_nth_kday_p, -1); + #endif + +@@ -9260,11 +9513,11 @@ Init_date_core(void) + date_s_valid_commercial_p, -1); + + #ifndef NDEBUG +- de_define_private_method(CLASS_OF(cDate), "valid_weeknum?", ++ rb_define_private_method(CLASS_OF(cDate), "valid_weeknum?", + date_s_valid_weeknum_p, -1); +- de_define_private_method(CLASS_OF(cDate), "valid_nth_kday?", ++ rb_define_private_method(CLASS_OF(cDate), "valid_nth_kday?", + date_s_valid_nth_kday_p, -1); +- de_define_private_method(CLASS_OF(cDate), "zone_to_diff", ++ rb_define_private_method(CLASS_OF(cDate), "zone_to_diff", + date_s_zone_to_diff, 1); + #endif + +@@ -9275,21 +9528,18 @@ Init_date_core(void) + date_s_gregorian_leap_p, 1); + + #ifndef NDEBUG +-#define de_define_singleton_method rb_define_singleton_method +-#define de_define_alias rb_define_alias +- de_define_singleton_method(cDate, "new!", date_s_new_bang, -1); +- de_define_alias(rb_singleton_class(cDate), "new_l!", "new"); ++ rb_define_singleton_method(cDate, "new!", date_s_new_bang, -1); ++ rb_define_alias(rb_singleton_class(cDate), "new_l!", "new"); + #endif + + rb_define_singleton_method(cDate, "jd", date_s_jd, -1); + rb_define_singleton_method(cDate, "ordinal", date_s_ordinal, -1); + rb_define_singleton_method(cDate, "civil", date_s_civil, -1); +- rb_define_singleton_method(cDate, "new", date_s_civil, -1); + rb_define_singleton_method(cDate, "commercial", date_s_commercial, -1); + + #ifndef NDEBUG +- de_define_singleton_method(cDate, "weeknum", date_s_weeknum, -1); +- de_define_singleton_method(cDate, "nth_kday", date_s_nth_kday, -1); ++ rb_define_singleton_method(cDate, "weeknum", date_s_weeknum, -1); ++ rb_define_singleton_method(cDate, "nth_kday", date_s_nth_kday, -1); + #endif + + rb_define_singleton_method(cDate, "today", date_s_today, -1); +@@ -9297,29 +9547,26 @@ Init_date_core(void) + rb_define_singleton_method(cDate, "strptime", date_s_strptime, -1); + rb_define_singleton_method(cDate, "_parse", date_s__parse, -1); + rb_define_singleton_method(cDate, "parse", date_s_parse, -1); +- rb_define_singleton_method(cDate, "_iso8601", date_s__iso8601, 1); ++ rb_define_singleton_method(cDate, "_iso8601", date_s__iso8601, -1); + rb_define_singleton_method(cDate, "iso8601", date_s_iso8601, -1); +- rb_define_singleton_method(cDate, "_rfc3339", date_s__rfc3339, 1); ++ rb_define_singleton_method(cDate, "_rfc3339", date_s__rfc3339, -1); + rb_define_singleton_method(cDate, "rfc3339", date_s_rfc3339, -1); +- rb_define_singleton_method(cDate, "_xmlschema", date_s__xmlschema, 1); ++ rb_define_singleton_method(cDate, "_xmlschema", date_s__xmlschema, -1); + rb_define_singleton_method(cDate, "xmlschema", date_s_xmlschema, -1); +- rb_define_singleton_method(cDate, "_rfc2822", date_s__rfc2822, 1); +- rb_define_singleton_method(cDate, "_rfc822", date_s__rfc2822, 1); ++ rb_define_singleton_method(cDate, "_rfc2822", date_s__rfc2822, -1); ++ rb_define_singleton_method(cDate, "_rfc822", date_s__rfc2822, -1); + rb_define_singleton_method(cDate, "rfc2822", date_s_rfc2822, -1); + rb_define_singleton_method(cDate, "rfc822", date_s_rfc2822, -1); +- rb_define_singleton_method(cDate, "_httpdate", date_s__httpdate, 1); ++ rb_define_singleton_method(cDate, "_httpdate", date_s__httpdate, -1); + rb_define_singleton_method(cDate, "httpdate", date_s_httpdate, -1); +- rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, 1); ++ rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, -1); + rb_define_singleton_method(cDate, "jisx0301", date_s_jisx0301, -1); + +-#ifndef NDEBUG +-#define de_define_method rb_define_method +- de_define_method(cDate, "initialize", d_lite_initialize, -1); +-#endif ++ rb_define_method(cDate, "initialize", date_initialize, -1); + rb_define_method(cDate, "initialize_copy", d_lite_initialize_copy, 1); + + #ifndef NDEBUG +- de_define_method(cDate, "fill", d_lite_fill, 0); ++ rb_define_method(cDate, "fill", d_lite_fill, 0); + #endif + + rb_define_method(cDate, "ajd", d_lite_ajd, 0); +@@ -9341,8 +9588,8 @@ Init_date_core(void) + rb_define_method(cDate, "cwday", d_lite_cwday, 0); + + #ifndef NDEBUG +- de_define_private_method(cDate, "wnum0", d_lite_wnum0, 0); +- de_define_private_method(cDate, "wnum1", d_lite_wnum1, 0); ++ rb_define_private_method(cDate, "wnum0", d_lite_wnum0, 0); ++ rb_define_private_method(cDate, "wnum1", d_lite_wnum1, 0); + #endif + + rb_define_method(cDate, "wday", d_lite_wday, 0); +@@ -9356,18 +9603,14 @@ Init_date_core(void) + rb_define_method(cDate, "saturday?", d_lite_saturday_p, 0); + + #ifndef NDEBUG +- de_define_method(cDate, "nth_kday?", d_lite_nth_kday_p, 2); ++ rb_define_method(cDate, "nth_kday?", d_lite_nth_kday_p, 2); + #endif + +- rb_define_private_method(cDate, "hour", d_lite_hour, 0); +- rb_define_private_method(cDate, "min", d_lite_min, 0); +- rb_define_private_method(cDate, "minute", d_lite_min, 0); +- rb_define_private_method(cDate, "sec", d_lite_sec, 0); +- rb_define_private_method(cDate, "second", d_lite_sec, 0); +- rb_define_private_method(cDate, "sec_fraction", d_lite_sec_fraction, 0); +- rb_define_private_method(cDate, "second_fraction", d_lite_sec_fraction, 0); +- rb_define_private_method(cDate, "offset", d_lite_offset, 0); +- rb_define_private_method(cDate, "zone", d_lite_zone, 0); ++ rb_define_private_method(cDate, "hour", d_lite_zero, 0); ++ rb_define_private_method(cDate, "min", d_lite_zero, 0); ++ rb_define_private_method(cDate, "minute", d_lite_zero, 0); ++ rb_define_private_method(cDate, "sec", d_lite_zero, 0); ++ rb_define_private_method(cDate, "second", d_lite_zero, 0); + + rb_define_method(cDate, "julian?", d_lite_julian_p, 0); + rb_define_method(cDate, "gregorian?", d_lite_gregorian_p, 0); +@@ -9380,8 +9623,6 @@ Init_date_core(void) + rb_define_method(cDate, "julian", d_lite_julian, 0); + rb_define_method(cDate, "gregorian", d_lite_gregorian, 0); + +- rb_define_private_method(cDate, "new_offset", d_lite_new_offset, -1); +- + rb_define_method(cDate, "+", d_lite_plus, 1); + rb_define_method(cDate, "-", d_lite_minus, 1); + +@@ -9409,7 +9650,7 @@ Init_date_core(void) + + rb_define_method(cDate, "to_s", d_lite_to_s, 0); + #ifndef NDEBUG +- de_define_method(cDate, "inspect_raw", d_lite_inspect_raw, 0); ++ rb_define_method(cDate, "inspect_raw", d_lite_inspect_raw, 0); + #endif + rb_define_method(cDate, "inspect", d_lite_inspect, 0); + +@@ -9426,7 +9667,7 @@ Init_date_core(void) + rb_define_method(cDate, "jisx0301", d_lite_jisx0301, 0); + + #ifndef NDEBUG +- de_define_method(cDate, "marshal_dump_old", d_lite_marshal_dump_old, 0); ++ rb_define_method(cDate, "marshal_dump_old", d_lite_marshal_dump_old, 0); + #endif + rb_define_method(cDate, "marshal_dump", d_lite_marshal_dump, 0); + rb_define_method(cDate, "marshal_load", d_lite_marshal_load, 1); +@@ -9573,6 +9814,7 @@ Init_date_core(void) + */ + + cDateTime = rb_define_class("DateTime", cDate); ++ rb_define_alloc_func(cDateTime, d_lite_s_alloc_complex); + + rb_define_singleton_method(cDateTime, "jd", datetime_s_jd, -1); + rb_define_singleton_method(cDateTime, "ordinal", datetime_s_ordinal, -1); +@@ -9582,9 +9824,9 @@ Init_date_core(void) + datetime_s_commercial, -1); + + #ifndef NDEBUG +- de_define_singleton_method(cDateTime, "weeknum", ++ rb_define_singleton_method(cDateTime, "weeknum", + datetime_s_weeknum, -1); +- de_define_singleton_method(cDateTime, "nth_kday", ++ rb_define_singleton_method(cDateTime, "nth_kday", + datetime_s_nth_kday, -1); + #endif + +@@ -9612,19 +9854,16 @@ Init_date_core(void) + rb_define_singleton_method(cDateTime, "jisx0301", + datetime_s_jisx0301, -1); + +-#define f_public(m,s) rb_funcall(m, rb_intern("public"), 1,\ +- ID2SYM(rb_intern(s))) +- +- f_public(cDateTime, "hour"); +- f_public(cDateTime, "min"); +- f_public(cDateTime, "minute"); +- f_public(cDateTime, "sec"); +- f_public(cDateTime, "second"); +- f_public(cDateTime, "sec_fraction"); +- f_public(cDateTime, "second_fraction"); +- f_public(cDateTime, "offset"); +- f_public(cDateTime, "zone"); +- f_public(cDateTime, "new_offset"); ++ rb_define_method(cDateTime, "hour", d_lite_hour, 0); ++ rb_define_method(cDateTime, "min", d_lite_min, 0); ++ rb_define_method(cDateTime, "minute", d_lite_min, 0); ++ rb_define_method(cDateTime, "sec", d_lite_sec, 0); ++ rb_define_method(cDateTime, "second", d_lite_sec, 0); ++ rb_define_method(cDateTime, "sec_fraction", d_lite_sec_fraction, 0); ++ rb_define_method(cDateTime, "second_fraction", d_lite_sec_fraction, 0); ++ rb_define_method(cDateTime, "offset", d_lite_offset, 0); ++ rb_define_method(cDateTime, "zone", d_lite_zone, 0); ++ rb_define_method(cDateTime, "new_offset", d_lite_new_offset, -1); + + rb_define_method(cDateTime, "to_s", dt_lite_to_s, 0); + +@@ -9652,15 +9891,15 @@ Init_date_core(void) + #ifndef NDEBUG + /* tests */ + +- de_define_singleton_method(cDate, "test_civil", date_s_test_civil, 0); +- de_define_singleton_method(cDate, "test_ordinal", date_s_test_ordinal, 0); +- de_define_singleton_method(cDate, "test_commercial", ++ rb_define_singleton_method(cDate, "test_civil", date_s_test_civil, 0); ++ rb_define_singleton_method(cDate, "test_ordinal", date_s_test_ordinal, 0); ++ rb_define_singleton_method(cDate, "test_commercial", + date_s_test_commercial, 0); +- de_define_singleton_method(cDate, "test_weeknum", date_s_test_weeknum, 0); +- de_define_singleton_method(cDate, "test_nth_kday", date_s_test_nth_kday, 0); +- de_define_singleton_method(cDate, "test_unit_conv", ++ rb_define_singleton_method(cDate, "test_weeknum", date_s_test_weeknum, 0); ++ rb_define_singleton_method(cDate, "test_nth_kday", date_s_test_nth_kday, 0); ++ rb_define_singleton_method(cDate, "test_unit_conv", + date_s_test_unit_conv, 0); +- de_define_singleton_method(cDate, "test_all", date_s_test_all, 0); ++ rb_define_singleton_method(cDate, "test_all", date_s_test_all, 0); + #endif + } + +diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c +index b74230d291..f06c07bae4 100644 +--- a/ext/date/date_parse.c ++++ b/ext/date/date_parse.c +@@ -40,9 +40,9 @@ RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int bas + #define f_sub_bang(s,r,x) rb_funcall(s, rb_intern("sub!"), 2, r, x) + #define f_gsub_bang(s,r,x) rb_funcall(s, rb_intern("gsub!"), 2, r, x) + +-#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k)), v) +-#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k))) +-#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k))) ++#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k"")), v) ++#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k""))) ++#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k""))) + + #define cstr2num(s) rb_cstr_to_inum(s, 10, 0) + #define str2num(s) rb_str_to_inum(s, 10, 0) +@@ -66,7 +66,13 @@ static const char abbr_months[][4] = { + #define asubt_string() rb_str_new("\024", 1) + #endif + +-#define DECDIGIT "0123456789" ++static size_t ++digit_span(const char *s, const char *e) ++{ ++ size_t i = 0; ++ while (s + i < e && isdigit(s[i])) i++; ++ return i; ++} + + static void + s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) +@@ -92,7 +98,7 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + y = d; + d = Qnil; + } +- if (!NIL_P(d) && *RSTRING_PTR(d) == '\'') { ++ if (!NIL_P(d) && RSTRING_LEN(d) > 0 && *RSTRING_PTR(d) == '\'') { + y = d; + d = Qnil; + } +@@ -103,17 +109,20 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + size_t l; + + s = RSTRING_PTR(y); +- while (!issign((unsigned char)*s) && !isdigit((unsigned char)*s)) ++ ep = RSTRING_END(y); ++ while (s < ep && !issign(*s) && !isdigit(*s)) + s++; ++ if (s >= ep) goto no_date; + bp = s; + if (issign((unsigned char)*s)) + s++; +- l = strspn(s, DECDIGIT); ++ l = digit_span(s, ep); + ep = s + l; + if (*ep) { + y = d; + d = rb_str_new(bp, ep - bp); + } ++ no_date:; + } + + if (!NIL_P(m)) { +@@ -152,8 +161,10 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + VALUE iy; + + s = RSTRING_PTR(y); +- while (!issign((unsigned char)*s) && !isdigit((unsigned char)*s)) ++ ep = RSTRING_END(y); ++ while (s < ep && !issign(*s) && !isdigit(*s)) + s++; ++ if (s >= ep) goto no_year; + bp = s; + if (issign(*s)) { + s++; +@@ -161,7 +172,7 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + } + if (sign) + c = Qfalse; +- l = strspn(s, DECDIGIT); ++ l = digit_span(s, ep); + ep = s + l; + if (l > 2) + c = Qfalse; +@@ -175,6 +186,7 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + ALLOCV_END(vbuf); + } + set_hash("year", iy); ++ no_year:; + } + + if (bc) +@@ -186,10 +198,12 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + VALUE im; + + s = RSTRING_PTR(m); +- while (!isdigit((unsigned char)*s)) ++ ep = RSTRING_END(m); ++ while (s < ep && !isdigit(*s)) + s++; ++ if (s >= ep) goto no_month; + bp = s; +- l = strspn(s, DECDIGIT); ++ l = digit_span(s, ep); + ep = s + l; + { + char *buf; +@@ -201,6 +215,7 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + ALLOCV_END(vbuf); + } + set_hash("mon", im); ++ no_month:; + } + + if (!NIL_P(d)) { +@@ -209,10 +224,12 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + VALUE id; + + s = RSTRING_PTR(d); +- while (!isdigit((unsigned char)*s)) ++ ep = RSTRING_END(d); ++ while (s < ep && !isdigit(*s)) + s++; ++ if (s >= ep) goto no_mday; + bp = s; +- l = strspn(s, DECDIGIT); ++ l = digit_span(s, ep); + ep = s + l; + { + char *buf; +@@ -224,6 +241,7 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) + ALLOCV_END(vbuf); + } + set_hash("mday", id); ++ no_mday:; + } + + if (!NIL_P(c)) +@@ -263,18 +281,18 @@ regcomp(const char *source, long len, int opt) + } + + #define REGCOMP(pat,opt) \ +-{ \ ++do { \ + if (NIL_P(pat)) \ + pat = regcomp(pat##_source, sizeof pat##_source - 1, opt); \ +-} ++} while (0) + + #define REGCOMP_0(pat) REGCOMP(pat, 0) + #define REGCOMP_I(pat) REGCOMP(pat, ONIG_OPTION_IGNORECASE) + + #define MATCH(s,p,c) \ +-{ \ ++do { \ + return match(s, p, hash, c); \ +-} ++} while (0) + + static int + match(VALUE str, VALUE pat, VALUE hash, int (*cb)(VALUE, VALUE)) +@@ -314,30 +332,30 @@ subx(VALUE str, VALUE rep, VALUE pat, VALUE hash, int (*cb)(VALUE, VALUE)) + } + + #define SUBS(s,p,c) \ +-{ \ ++do { \ + return subx(s, asp_string(), p, hash, c); \ +-} ++} while (0) + + #ifdef TIGHT_PARSER + #define SUBA(s,p,c) \ +-{ \ ++do { \ + return subx(s, asuba_string(), p, hash, c); \ +-} ++} while (0) + + #define SUBB(s,p,c) \ +-{ \ ++do { \ + return subx(s, asubb_string(), p, hash, c); \ +-} ++} while (0) + + #define SUBW(s,p,c) \ +-{ \ ++do { \ + return subx(s, asubw_string(), p, hash, c); \ +-} ++} while (0) + + #define SUBT(s,p,c) \ +-{ \ ++do { \ + return subx(s, asubt_string(), p, hash, c); \ +-} ++} while (0) + #endif + + #include "zonetab.h" +@@ -706,16 +724,14 @@ parse_era(VALUE str, VALUE hash) + static int + check_year_width(VALUE y) + { +- char *s; +- size_t l; ++ const char *s; ++ long l; + ++ l = RSTRING_LEN(y); ++ if (l < 2) return 0; + s = RSTRING_PTR(y); +- l = strcspn(s, DECDIGIT); +- s += l; +- l = strspn(s, DECDIGIT); +- if (l != 2) +- return 0; +- return 1; ++ if (!isdigit(s[1])) return 0; ++ return (l == 2 || !isdigit(s[2])); + } + + static int +@@ -1196,6 +1212,9 @@ parse_iso2(VALUE str, VALUE hash) + return 1; + } + ++#define JISX0301_ERA_INITIALS "mtshr" ++#define JISX0301_DEFAULT_ERA 'H' /* obsolete */ ++ + static int + gengo(int c) + { +@@ -1206,6 +1225,7 @@ gengo(int c) + case 'T': case 't': e = 1911; break; + case 'S': case 's': e = 1925; break; + case 'H': case 'h': e = 1988; break; ++ case 'R': case 'r': e = 2018; break; + default: e = 0; break; + } + return e; +@@ -1236,11 +1256,11 @@ parse_jis(VALUE str, VALUE hash) + { + static const char pat_source[] = + #ifndef TIGHT_PARSER +- "\\b([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" ++ "\\b([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + #else + BOS + FPW_COM FPT_COM +- "([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" ++ "([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + TEE_FPT COM_FPW + EOS + #endif +@@ -2938,7 +2958,7 @@ jisx0301_cb(VALUE m, VALUE hash) + s[i] = rb_reg_nth_match(i, m); + } + +- ep = gengo(NIL_P(s[1]) ? 'h' : *RSTRING_PTR(s[1])); ++ ep = gengo(NIL_P(s[1]) ? JISX0301_DEFAULT_ERA : *RSTRING_PTR(s[1])); + set_hash("year", f_add(str2num(s[2]), INT2FIX(ep))); + set_hash("mon", str2num(s[3])); + set_hash("mday", str2num(s[4])); +@@ -2963,7 +2983,7 @@ static int + jisx0301(VALUE str, VALUE hash) + { + static const char pat_source[] = +- "\\A\\s*([mtsh])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" ++ "\\A\\s*([" JISX0301_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" + "(?:t" + "(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d*))?)?" + "(z|[-+]\\d{2}(?::?\\d{2})?)?)?)?\\s*\\z"; +diff --git a/ext/date/date_strptime.c b/ext/date/date_strptime.c +index 4f93219317..4383eb6fa1 100644 +--- a/ext/date/date_strptime.c ++++ b/ext/date/date_strptime.c +@@ -79,14 +79,17 @@ read_digits(const char *s, VALUE *n, size_t width) + { + size_t l; + +- l = strspn(s, "0123456789"); ++ if (!width) ++ return 0; ++ ++ l = 0; ++ while (ISDIGIT(s[l])) { ++ if (++l == width) break; ++ } + + if (l == 0) + return 0; + +- if (width < l) +- l = width; +- + if ((4 * l * sizeof(char)) <= (sizeof(long)*CHAR_BIT)) { + const char *os = s; + long v; +@@ -113,26 +116,26 @@ read_digits(const char *s, VALUE *n, size_t width) + } + } + +-#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k)), v) +-#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k))) +-#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k))) ++#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k"")), v) ++#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k""))) ++#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k""))) + + #define fail() \ +-{ \ ++do { \ + set_hash("_fail", Qtrue); \ + return 0; \ +-} ++} while (0) + + #define fail_p() (!NIL_P(ref_hash("_fail"))) + + #define READ_DIGITS(n,w) \ +-{ \ ++do { \ + size_t l; \ + l = read_digits(&str[si], &n, w); \ + if (l == 0) \ + fail(); \ + si += l; \ +-} ++} while (0) + + #define READ_DIGITS_MAX(n) READ_DIGITS(n, LONG_MAX) + +@@ -147,14 +150,14 @@ valid_range_p(VALUE v, int a, int b) + } + + #define recur(fmt) \ +-{ \ ++do { \ + size_t l; \ + l = date__strptime_internal(&str[si], slen - si, \ + fmt, sizeof fmt - 1, hash); \ + if (fail_p()) \ + return 0; \ + si += l; \ +-} ++} while (0) + + VALUE date_zone_to_diff(VALUE); + +@@ -237,9 +240,9 @@ date__strptime_internal(const char *str, size_t slen, + VALUE n; + + if (NUM_PATTERN_P()) +- READ_DIGITS(n, 2) ++ READ_DIGITS(n, 2); + else +- READ_DIGITS_MAX(n) ++ READ_DIGITS_MAX(n); + set_hash("_cent", n); + goto matched; + } +@@ -278,9 +281,9 @@ date__strptime_internal(const char *str, size_t slen, + VALUE n; + + if (NUM_PATTERN_P()) +- READ_DIGITS(n, 4) ++ READ_DIGITS(n, 4); + else +- READ_DIGITS_MAX(n) ++ READ_DIGITS_MAX(n); + set_hash("cwyear", n); + goto matched; + } +@@ -358,9 +361,9 @@ date__strptime_internal(const char *str, size_t slen, + } + osi = si; + if (NUM_PATTERN_P()) +- READ_DIGITS(n, c == 'L' ? 3 : 9) ++ READ_DIGITS(n, c == 'L' ? 3 : 9); + else +- READ_DIGITS_MAX(n) ++ READ_DIGITS_MAX(n); + if (sign == -1) + n = f_negate(n); + set_hash("sec_fraction", +@@ -426,9 +429,7 @@ date__strptime_internal(const char *str, size_t slen, + if (sign == -1) + n = f_negate(n); + set_hash("seconds", +- rb_rational_new2(n, +- f_expt(INT2FIX(10), +- INT2FIX(3)))); ++ rb_rational_new2(n, INT2FIX(1000))); + goto matched; + } + +@@ -529,24 +530,24 @@ date__strptime_internal(const char *str, size_t slen, + goto matched; + + case 'Y': +- { +- VALUE n; +- int sign = 1; +- +- if (issign(str[si])) { +- if (str[si] == '-') +- sign = -1; +- si++; +- } +- if (NUM_PATTERN_P()) +- READ_DIGITS(n, 4) +- else +- READ_DIGITS_MAX(n) ++ { ++ VALUE n; ++ int sign = 1; ++ ++ if (issign(str[si])) { ++ if (str[si] == '-') ++ sign = -1; ++ si++; ++ } ++ if (NUM_PATTERN_P()) ++ READ_DIGITS(n, 4); ++ else ++ READ_DIGITS_MAX(n); + if (sign == -1) + n = f_negate(n); +- set_hash("year", n); +- goto matched; +- } ++ set_hash("year", n); ++ goto matched; ++ } + + case 'y': + { +diff --git a/ext/io/console/io-console.gemspec b/ext/io/console/io-console.gemspec +index 1256162468..99e18bda9a 100644 +--- a/ext/io/console/io-console.gemspec ++++ b/ext/io/console/io-console.gemspec +@@ -5,7 +5,7 @@ + Gem::Specification.new do |s| + s.name = "io-console" + s.version = _VERSION +- s.date = date ++ s.date = RUBY_RELEASE_DATE + s.summary = "Console interface" + s.email = "nobu@ruby-lang.org" + s.description = "add console capabilities to IO instances." +diff --git a/gc.c b/gc.c +index c02ac627f0..fc303879eb 100644 +--- a/gc.c ++++ b/gc.c +@@ -47,7 +47,9 @@ + # endif + #endif + #ifdef HAVE_MALLOC_USABLE_SIZE +-# ifdef HAVE_MALLOC_H ++# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER ++# include RUBY_ALTERNATIVE_MALLOC_HEADER ++# elif HAVE_MALLOC_H + # include + # elif defined(HAVE_MALLOC_NP_H) + # include +@@ -975,6 +977,31 @@ tick(void) + return val; + } + ++#elif defined(__powerpc64__) && \ ++ ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) ++typedef unsigned long long tick_t; ++#define PRItick "llu" ++ ++static __inline__ tick_t ++tick(void) ++{ ++ unsigned long long val = __builtin_ppc_get_timebase(); ++ return val; ++} ++ ++#elif defined(__aarch64__) && defined(__GNUC__) ++typedef unsigned long tick_t; ++#define PRItick "lu" ++ ++static __inline__ tick_t ++tick(void) ++{ ++ unsigned long val; ++ __asm__ __volatile__ ("mrs %0, cntvct_el0", : "=r" (val)); ++ return val; ++} ++ ++ + #elif defined(_WIN32) && defined(_MSC_VER) + #include + typedef unsigned __int64 tick_t; +diff --git a/gc.h b/gc.h +index 2c91e06620..0f8a56f94d 100644 +--- a/gc.h ++++ b/gc.h +@@ -6,6 +6,8 @@ + #define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movq\t%%rsp, %0" : "=r" (*(p))) + #elif defined(__i386) && defined(__GNUC__) + #define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movl\t%%esp, %0" : "=r" (*(p))) ++#elif defined(__aarch64__) && defined(__GNUC__) ++#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mov\t%0, sp" : "=r" (*(p))) + #else + NOINLINE(void rb_gc_set_stack_end(VALUE **stack_end_p)); + #define SET_MACHINE_STACK_END(p) rb_gc_set_stack_end(p) +diff --git a/include/ruby/defines.h b/include/ruby/defines.h +index 2c72a7cb8a..85f3bda3c1 100644 +--- a/include/ruby/defines.h ++++ b/include/ruby/defines.h +@@ -358,7 +358,7 @@ void rb_ia64_flushrs(void); + #ifndef UNALIGNED_WORD_ACCESS + # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \ +- defined(__powerpc64__) || \ ++ defined(__powerpc64__) || defined(__aarch64__) || \ + defined(__mc68020__) + # define UNALIGNED_WORD_ACCESS 1 + # else +diff --git a/lib/cgi/cookie.rb b/lib/cgi/cookie.rb +index a2155edb77..4410f3174f 100644 +--- a/lib/cgi/cookie.rb ++++ b/lib/cgi/cookie.rb +@@ -40,6 +40,10 @@ class CGI + class Cookie < Array + @@accept_charset="UTF-8" unless defined?(@@accept_charset) + ++ TOKEN_RE = %r"\A[[!-~]&&[^()<>@,;:\\\"/?=\[\]{}]]+\z" ++ PATH_VALUE_RE = %r"\A[[ -~]&&[^;]]*\z" ++ DOMAIN_VALUE_RE = %r"\A\.?(?