Skip to content

Commit

Permalink
Enable frozen string literals
Browse files Browse the repository at this point in the history
In preparation for frozen string literals being enabled by default in
Ruby (see
ruby/ruby@12be40a),
opt into frozen string literals now and fix any issues raised by running
a full compile and run of the test suite with Ruby's
`--enable-frozen-string-literal --debug-frozen-string-literal` options.

We also take this opportunity to remove the now-redundant encoding
comments as we no longer support Ruby versions that default to anything
except UTF-8.
  • Loading branch information
mudge committed Mar 30, 2024
1 parent cc4940a commit d452475
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
Expand Down
4 changes: 3 additions & 1 deletion ext/re2/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down Expand Up @@ -126,7 +128,7 @@ def build_extension(static_p = false)

# Pass -x c++ to force gcc to compile the test program
# as C++ (as it will end in .c by default).
compile_options = "-x c++"
compile_options = +"-x c++"

have_library("stdc++")
have_header("stdint.h")
Expand Down
2 changes: 2 additions & 0 deletions ext/re2/recipes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down
2 changes: 2 additions & 0 deletions lib/re2.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down
2 changes: 2 additions & 0 deletions lib/re2/regexp.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down
2 changes: 2 additions & 0 deletions lib/re2/scanner.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down
2 changes: 2 additions & 0 deletions lib/re2/string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# re2 (https://github.com/mudge/re2)
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
# backtracking regular expression engines like those used in PCRE, Perl, and
Expand Down
2 changes: 2 additions & 0 deletions re2.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'lib/re2/version'

Gem::Specification.new do |s|
Expand Down
1 change: 1 addition & 0 deletions scripts/update-dependencies
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "net/http"
require "digest/sha2"
Expand Down
2 changes: 2 additions & 0 deletions spec/kernel_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

RSpec.describe Kernel do
describe ".RE2" do
it "returns an RE2::Regexp instance given a pattern" do
Expand Down
7 changes: 4 additions & 3 deletions spec/re2/match_data_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true

require 'objspace'

RSpec.describe RE2::MatchData do
Expand Down Expand Up @@ -142,7 +143,7 @@
end

it "returns a copy, not the actual original" do
string = "bob"
string = +"bob"
re = RE2::Regexp.new('(\D+)').match(string)

expect(re.string).to_not equal(string)
Expand All @@ -155,7 +156,7 @@
end

it "does not copy the string if it was already frozen" do
string = "bob".freeze
string = "bob"
re = RE2::Regexp.new('(\D+)').match(string)

expect(re.string).to equal(string)
Expand Down
2 changes: 2 additions & 0 deletions spec/re2/regexp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

RSpec.describe RE2::Regexp do
describe "#initialize" do
it "returns an instance given only a pattern" do
Expand Down
2 changes: 1 addition & 1 deletion spec/re2/scanner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
# frozen_string_literal: true

RSpec.describe RE2::Scanner do
describe "#regexp" do
Expand Down
2 changes: 2 additions & 0 deletions spec/re2/set_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

RSpec.describe RE2::Set do
describe "#initialize" do
it "returns an instance given no args" do
Expand Down
2 changes: 2 additions & 0 deletions spec/re2/string_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "re2/string"

class String
Expand Down
2 changes: 2 additions & 0 deletions spec/re2_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

RSpec.describe RE2 do
describe ".Replace" do
it "only replaces the first occurrence of the pattern" do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "re2"

# To test passing objects that can be coerced to a String.
Expand Down

0 comments on commit d452475

Please sign in to comment.