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
17 changes: 17 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package strings2

import (
"testing"
"unicode"
)

func BenchmarkUpperCaseFirst_ASCII_Short(b *testing.B) {
Expand Down Expand Up @@ -181,3 +182,19 @@ func BenchmarkSplitMixCase(b *testing.B) {
_, _ = WordsToFormattedCase(words, OptionMixCaseSupport(), OptionDelimiter("-"))
}
}

func BenchmarkPerformCaseFirst(b *testing.B) {
s := "test"
fn := unicode.ToUpper
for i := 0; i < b.N; i++ {
performCaseFirst(s, fn)
}
}

func BenchmarkPerformCaseFirst_Long(b *testing.B) {
s := "teststringwithmorecharacters"
fn := unicode.ToUpper
for i := 0; i < b.N; i++ {
performCaseFirst(s, fn)
}
}
51 changes: 0 additions & 51 deletions parts_num_test.go

This file was deleted.

22 changes: 0 additions & 22 deletions perform_case_first_bench_test.go

This file was deleted.

46 changes: 43 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ func upperCaseFirstLower(s string, mode UTF8Mode) (string, error) {

func (w ExactCaseWord) String() string { return string(w) }

// WordLength returns the string length of the given Word type without allocating.
func WordLength(word Word) (int, error) {
switch w := word.(type) {
case SingleCaseWord:
return len(w), nil
case FirstUpperCaseWord:
return len(w), nil
case ExactCaseWord:
return len(w), nil
case AcronymWord:
return len(w), nil
case UpperCaseWord:
return len(w), nil
case SeparatorWord:
return len(w), nil
default:
return 0, fmt.Errorf("unknown word type: %T", word)
}
}

// Options
type Option func(*caseConfig)

Expand Down Expand Up @@ -318,8 +338,28 @@ func WordsToFormattedCase(words []Word, opts ...any) (string, error) {
cfg.firstUpper = true
}

result := make([]string, 0, len(words))
size := 0
for _, word := range words {
l, err := WordLength(word)
if err != nil {
return "", err
}
// heuristic: add 5 to allow for transformations like splitMixCase
size += l + 5
}
delimiterLen := len(cfg.delimiter)
if len(words) > 1 {
size += delimiterLen * (len(words) - 1)
}

var b strings.Builder
b.Grow(size)
Comment thread
arran4 marked this conversation as resolved.

for i, word := range words {
if i > 0 {
b.WriteString(cfg.delimiter)
}

var w string
switch word := word.(type) {
case SingleCaseWord:
Expand Down Expand Up @@ -393,10 +433,10 @@ func WordsToFormattedCase(words []Word, opts ...any) (string, error) {
w = word.String()
}

result = append(result, w)
b.WriteString(w)
}

final := strings.Join(result, cfg.delimiter)
final := b.String()

if cfg.firstUpper {
final = UpperCaseFirst(final)
Expand Down
197 changes: 0 additions & 197 deletions types_internal_test.go

This file was deleted.

Loading
Loading