Skip to content

Add strict variants of UpperCaseFirst#13

Merged
arran4 merged 5 commits intomainfrom
add-strict-uppercase-first-14880112615616114181
Feb 5, 2026
Merged

Add strict variants of UpperCaseFirst#13
arran4 merged 5 commits intomainfrom
add-strict-uppercase-first-14880112615616114181

Conversation

@arran4
Copy link
Owner

@arran4 arran4 commented Jan 30, 2026

Added UpperCaseFirstWithErr and MustUpperCaseFirst to types.go along with ErrRune. Added corresponding tests in types_test.go.


PR created automatically by Jules for task 14880112615616114181 started by @arran4

This commit introduces two new variants of the UpperCaseFirst function:
- UpperCaseFirstWithErr: Returns the modified string and an error if the first rune is invalid.
- MustUpperCaseFirst: Panics if the first rune is invalid.

It also introduces ErrRune to explicitly identify invalid rune errors.
These additions provide more control over UTF-8 decoding errors compared to the silent failure of the original UpperCaseFirst function.
Tests are added to cover these new functions.

Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules there should be complete support for this and we should see greater code reuse please.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored the code to use a shared internal helper performCaseFirst to eliminate duplication between the UpperCaseFirst variants.

To provide complete support, I've also implemented the symmetric LowerCaseFirst, LowerCaseFirstWithErr, and MustLowerCaseFirst functions. Additionally, I've updated ToFormattedCase to use LowerCaseFirst when OptionFirstLower is used, which also fixes a bug where multibyte characters were handled incorrectly.

- Introduced `performCaseFirst` internal helper to reduce duplication in casing functions.
- Added `LowerCaseFirst`, `LowerCaseFirstWithErr`, and `MustLowerCaseFirst` for symmetry with `UpperCaseFirst` variants.
- Updated `ToFormattedCase` to use `LowerCaseFirst`, fixing incorrect handling of multibyte characters when `OptionFirstLower` is used.
- Added comprehensive tests for new functions and bug fix.

Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
"unicode/utf8"
)

var ErrRune = errors.New("invalid rune")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules is this the only error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ErrRune is the only error condition explicitly handled here, which corresponds to utf8.RuneError occurring with a non-zero size (indicating invalid UTF-8).

Empty strings are treated as valid no-ops (returning the empty string without error), which is consistent with the behavior of standard library functions like strings.ToUpper.

- Introduced `performCaseFirst` helper to unify casing logic.
- Added `LowerCaseFirst`, `LowerCaseFirstWithErr`, and `MustLowerCaseFirst`.
- Updated `ToFormattedCase` to use `LowerCaseFirst` for correctness with multibyte characters.
- Validated `ErrRune` scope; empty strings are valid no-ops.
- Added comprehensive tests.

Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
@arran4
Copy link
Owner Author

arran4 commented Feb 4, 2026

@jules go format

Run go fmt ./...
found packages strings2 (benchmark_test.go) and main (verify_utf8.go) in /home/runner/work/strings2/strings2
Error: Process completed with exit code 1.

@google-labs-jules
Copy link
Contributor

@jules go format

Run go fmt ./...
found packages strings2 (benchmark_test.go) and main (verify_utf8.go) in /home/runner/work/strings2/strings2
Error: Process completed with exit code 1.

I have run go fmt ./... and removed the temporary verification file. The code should now be properly formatted.

- Ran `go fmt ./...` to fix formatting issues in `benchmark_test.go` and `types_test.go`.
- Removed temporary file `verify_utf8.go`.

Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
@arran4
Copy link
Owner Author

arran4 commented Feb 4, 2026

@jules go format needs to be run as per:

diff --git a/metadata_test.go b/metadata_test.go
index 144db63..967f361 100644
--- a/metadata_test.go
+++ b/metadata_test.go
@@ -220,25 +220,25 @@ func TestCircularConsistency(t *testing.T) {
 // TestInternalFlagsConsistency ensures that options set flags that are internally consistent
 // or that conflicting flags are handled deterministically (Meta Data logic).
 func TestInternalFlagsConsistency(t *testing.T) {
-    // This tests the logic inside ToFormattedCase that derives internal flags from CaseMode
+	// This tests the logic inside ToFormattedCase that derives internal flags from CaseMode
 	// We can't access local variables inside ToFormattedCase, but we can verify the outcome
 	// matches the expected behavior of those flags.
 
-    // Case 1: CMScreaming sets cfg.screaming = true.
-    // SingleCaseWord should be Uppercased.
-    t.Run("CMScreaming implies screaming", func(t *testing.T) {
-        res := ToFormattedCase([]Word{SingleCaseWord("hello")}, OptionCaseMode(CMScreaming))
-        if res != "HELLO" {
-             t.Errorf("CMScreaming did not result in screaming output: %q", res)
-        }
-    })
+	// Case 1: CMScreaming sets cfg.screaming = true.
+	// SingleCaseWord should be Uppercased.
+	t.Run("CMScreaming implies screaming", func(t *testing.T) {
+		res := ToFormattedCase([]Word{SingleCaseWord("hello")}, OptionCaseMode(CMScreaming))
+		if res != "HELLO" {
+			t.Errorf("CMScreaming did not result in screaming output: %q", res)
+		}
+	})
 
-    // Case 2: CMWhispering implies whispering.
-    // SingleCaseWord should be Lowercased (even if input is upper).
-    t.Run("CMWhispering implies whispering", func(t *testing.T) {
-        res := ToFormattedCase([]Word{SingleCaseWord("HELLO")}, OptionCaseMode(CMWhispering))
-        if res != "hello" {
-             t.Errorf("CMWhispering did not result in whispering output: %q", res)
-        }
-    })
+	// Case 2: CMWhispering implies whispering.
+	// SingleCaseWord should be Lowercased (even if input is upper).
+	t.Run("CMWhispering implies whispering", func(t *testing.T) {
+		res := ToFormattedCase([]Word{SingleCaseWord("HELLO")}, OptionCaseMode(CMWhispering))
+		if res != "hello" {
+			t.Errorf("CMWhispering did not result in whispering output: %q", res)
+		}
+	})
 }

@google-labs-jules
Copy link
Contributor

@jules go format needs to be run as per:

diff --git a/metadata_test.go b/metadata_test.go
index 144db63..967f361 100644
--- a/metadata_test.go
+++ b/metadata_test.go
@@ -220,25 +220,25 @@ func TestCircularConsistency(t *testing.T) {
 // TestInternalFlagsConsistency ensures that options set flags that are internally consistent
 // or that conflicting flags are handled deterministically (Meta Data logic).
 func TestInternalFlagsConsistency(t *testing.T) {
-    // This tests the logic inside ToFormattedCase that derives internal flags from CaseMode
+	// This tests the logic inside ToFormattedCase that derives internal flags from CaseMode
 	// We can't access local variables inside ToFormattedCase, but we can verify the outcome
 	// matches the expected behavior of those flags.
 
-    // Case 1: CMScreaming sets cfg.screaming = true.
-    // SingleCaseWord should be Uppercased.
-    t.Run("CMScreaming implies screaming", func(t *testing.T) {
-        res := ToFormattedCase([]Word{SingleCaseWord("hello")}, OptionCaseMode(CMScreaming))
-        if res != "HELLO" {
-             t.Errorf("CMScreaming did not result in screaming output: %q", res)
-        }
-    })
+	// Case 1: CMScreaming sets cfg.screaming = true.
+	// SingleCaseWord should be Uppercased.
+	t.Run("CMScreaming implies screaming", func(t *testing.T) {
+		res := ToFormattedCase([]Word{SingleCaseWord("hello")}, OptionCaseMode(CMScreaming))
+		if res != "HELLO" {
+			t.Errorf("CMScreaming did not result in screaming output: %q", res)
+		}
+	})
 
-    // Case 2: CMWhispering implies whispering.
-    // SingleCaseWord should be Lowercased (even if input is upper).
-    t.Run("CMWhispering implies whispering", func(t *testing.T) {
-        res := ToFormattedCase([]Word{SingleCaseWord("HELLO")}, OptionCaseMode(CMWhispering))
-        if res != "hello" {
-             t.Errorf("CMWhispering did not result in whispering output: %q", res)
-        }
-    })
+	// Case 2: CMWhispering implies whispering.
+	// SingleCaseWord should be Lowercased (even if input is upper).
+	t.Run("CMWhispering implies whispering", func(t *testing.T) {
+		res := ToFormattedCase([]Word{SingleCaseWord("HELLO")}, OptionCaseMode(CMWhispering))
+		if res != "hello" {
+			t.Errorf("CMWhispering did not result in whispering output: %q", res)
+		}
+	})
 }

I have run go fmt ./.... It updated benchmark_test.go and types_test.go. However, I cannot locate metadata_test.go in the current repository state, so I was unable to format it. If this file exists in the branch, please ensure it is tracked or let me know if I should recreate it.

- Formatted `benchmark_test.go` and `types_test.go`.
- Note: `metadata_test.go` was not found and thus not formatted.

Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
Copy link
Owner Author

@arran4 arran4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arran4 arran4 merged commit 8a4c344 into main Feb 5, 2026
3 of 4 checks passed
@arran4 arran4 deleted the add-strict-uppercase-first-14880112615616114181 branch February 5, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant