Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make String#delete spec-compliant #2090

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions spec/core/string/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: false
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

Expand Down Expand Up @@ -54,19 +55,15 @@
end

it "respects backslash for escaping a -" do
NATFIXME 'respects backslash for escaping a -', exception: ArgumentError do
'Non-Authoritative Information'.delete(' \-\'').should ==
'NonAuthoritativeInformation'
end
'Non-Authoritative Information'.delete(' \-\'').should ==
'NonAuthoritativeInformation'
end

it "raises if the given ranges are invalid" do
not_supported_on :opal do
NATFIXME 'Encodings', exception: SpecFailedException do
xFF = [0xFF].pack('C')
range = "\x00 - #{xFF}".force_encoding('utf-8')
-> { "hello".delete(range).should == "" }.should raise_error(ArgumentError)
end
xFF = [0xFF].pack('C')
range = "\x00 - #{xFF}".force_encoding('utf-8')
-> { "hello".delete(range).should == "" }.should raise_error(ArgumentError)
end
-> { "hello".delete("h-e") }.should raise_error(ArgumentError)
-> { "hello".delete("^h-e") }.should raise_error(ArgumentError)
Expand Down
33 changes: 20 additions & 13 deletions src/string_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,33 @@ static auto character_class_handler(Env *env, Args args) {
auto new_selectors = Hashmap<String>(HashType::TMString);
StringView last_character = {};
bool negated = false;
// Split into selectors
auto it = selectors->begin();
if (*it == "^") {
++it;

if (it != selectors->end()) {
// Split into selectors
size_t index = 0;
auto result = selectors->next_char_result(&index);
if (result.second == "^") {
if (index != selectors->bytesize())
negated = true;
} else {
else
new_selectors.set("^");
}
result = selectors->next_char_result(&index);
}
for (; it != selectors->end(); ++it) {
auto value = *it;
if (value == "-" && last_character != StringView()) {
++it;
if (it == selectors->end()) {
while (!result.second.is_empty()) {
if (!result.first)
env->raise("ArgumentError", "invalid byte sequence in {}", selectors->encoding()->name()->as_string()->string());

auto value = result.second;
if (value == "-" && last_character != StringView() && last_character != "\\") {
result = selectors->next_char_result(&index);
if (result.second.is_empty()) {
new_selectors.set(value.to_string());
break;
}

auto next_value = *it;
auto next_value = result.second;
if (!result.first)
env->raise("ArgumentError", "invalid range \"{}-{}\" in string transliteration", last_character.to_string(), next_value.to_string());

if (last_character.to_string().cmp(next_value.to_string()) == 1)
env->raise("ArgumentError", "invalid range \"{}-{}\" in string transliteration", last_character.to_string(), next_value.to_string());

Expand All @@ -68,6 +74,7 @@ static auto character_class_handler(Env *env, Args args) {
last_character = value;
new_selectors.set(value.to_string());
}
result = selectors->next_char_result(&index);
}

// intersect with current selectors
Expand Down
Loading