From e5a416034d8b4d47e3feaeed8262888aea4496d7 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 27 Aug 2026 15:41:07 +0200 Subject: [PATCH] Add String#unicode_normalize! The in-place form was missing (unicode_normalize / unicode_normalized? already existed). It normalizes the receiver's bytes and returns self, even when the string is already normalized. The form argument is validated (ArgumentError for an unknown form) before the frozen check (FrozenError), matching MRI's order. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15830 -> 15840 (core/string/unicode_normalize 6 -> 12). Co-Authored-By: Claude Opus 4.8 --- internal/vm/unicode_normalize.go | 10 ++++++++++ internal/vm/unicode_normalize_test.go | 26 ++++++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/vm/unicode_normalize.go b/internal/vm/unicode_normalize.go index abc820ce..6781a576 100644 --- a/internal/vm/unicode_normalize.go +++ b/internal/vm/unicode_normalize.go @@ -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 + }) } diff --git a/internal/vm/unicode_normalize_test.go b/internal/vm/unicode_normalize_test.go index 5ebdf627..0f1f8081 100644 --- a/internal/vm/unicode_normalize_test.go +++ b/internal/vm/unicode_normalize_test.go @@ -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) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 5fa61811..b99d6b80 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15830 +15840