Skip to content

Commit

Permalink
mstr: parameterize Trunc on string or []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Dec 22, 2024
1 parent 9e3c456 commit e913239
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mstr/mstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// exceeds this length, it is truncated at a point ≤ n so that the result does
// not end in a partial UTF-8 encoding. Trunc does not verify that s is valid
// UTF-8, but if it is the result will remain valid after truncation.
func Trunc(s string, n int) string {
func Trunc[String ~string | ~[]byte](s String, n int) String {
if n >= len(s) {
return s
}
Expand Down
5 changes: 4 additions & 1 deletion mstr/mstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func TestTrunc(t *testing.T) {
for _, tc := range tests {
got := mstr.Trunc(tc.input, tc.size)
if got != tc.want {
t.Errorf("Trunc(%q, %d): got %q, want %q", tc.input, tc.size, got, tc.want)
t.Errorf("Trunc(%q, %d) [string]: got %q, want %q", tc.input, tc.size, got, tc.want)
}
if got := mstr.Trunc([]byte(tc.input), tc.size); string(got) != tc.want {
t.Errorf("Trunc(%q, %d) [bytes]: got %q, want %q", tc.input, tc.size, got, tc.want)
}
}
}
Expand Down

0 comments on commit e913239

Please sign in to comment.