Skip to content
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
10 changes: 10 additions & 0 deletions internal/vm/unicode_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ func (vm *VM) registerStringUnicodeNormalize() {
form := normForm(args)
return object.Bool(norm.IsNormalized(normSource(self), form))
})
// unicode_normalize! normalizes the receiver in place and returns self (even
// when already normalized). The form argument is validated before the frozen
// check, matching MRI.
vm.cString.define("unicode_normalize!", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
form := normForm(args)
s := self.(*object.String)
vm.checkFrozen(s)
s.SetBytes([]byte(norm.Normalize(normSource(self), form)))
return self
})
}
26 changes: 26 additions & 0 deletions internal/vm/unicode_normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,29 @@ func TestUnicodeNormalizeProvidedFeature(t *testing.T) {
t.Errorf(`require "unicode_normalize" got=%q want "true\nfalse\n"`, got)
}
}

// TestUnicodeNormalizeBang covers String#unicode_normalize! — in-place
// normalization returning self, the always-self result even when unchanged, and
// the argument-before-frozen error ordering. Asserted against MRI Ruby 4.0.6.
func TestUnicodeNormalizeBang(t *testing.T) {
cases := []struct{ src, want string }{
{`s = "à"; r = s.unicode_normalize!(:nfc); p [r.equal?(s), s.length]`, "[true, 1]\n"},
{`p "abc".unicode_normalize!`, "\"abc\"\n"},
{`s = "fi"; s.unicode_normalize!(:nfkc); p s`, "\"fi\"\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
errs := []struct{ src, want string }{
{`"x".freeze.unicode_normalize!`, "can't modify frozen String"},
{`"abc".unicode_normalize!(:bad)`, "Invalid normalization form bad."},
{`"x".freeze.unicode_normalize!(:bad)`, "Invalid normalization form bad."}, // form checked first
}
for _, c := range errs {
if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("src=%q err=%v, want substring %q", c.src, err, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15830
15840