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
15 changes: 15 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ func BenchmarkToFormattedCase_Default(b *testing.B) {
_ = ToFormattedCase(words)
}
}

func BenchmarkSplitMixCase(b *testing.B) {
words := []Word{
ExactCaseWord("thisIsAMixedCaseString"),
ExactCaseWord("AnotherMixedCaseStringWithMoreParts"),
ExactCaseWord("ShortOne"),
ExactCaseWord("SuperLongMixedCaseStringWithManyManyCapitalLettersToTriggerReallocationIfBufferIsTooSmall"),
}

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = WordsToFormattedCase(words, OptionMixCaseSupport(), OptionDelimiter("-"))
}
}
7 changes: 6 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,13 @@ func separateOptionsAny(opts []any) ([]any, []any) {

// Helper function to split words in mixed case
func splitMixCase(input, delimiter string) string {
if delimiter == "" {
return input
}
var result strings.Builder
result.Grow(len(input))
// Pre-allocate to avoid resizing.
// We add a buffer for potential delimiters (assuming roughly 50% increase).
result.Grow(len(input) + len(input)/2)
for i, r := range input {
if i > 0 && unicode.IsUpper(r) {
result.WriteString(delimiter)
Expand Down
Loading