Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes, improvements, optimization & refactoring before parser generation #288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Optimize string field accumulation to do fewer allocations
  • Loading branch information
Peter Dolak authored and petee-d committed Dec 5, 2022
commit d03a0d2c34bd3898863544e7b9b309c5e065e356
7 changes: 6 additions & 1 deletion nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,14 @@ func setField(tokens []lexer.Token, strct reflect.Value, field structLexerField,
if err != nil {
return err
}
if len(fieldValue) == 0 {
return nil
}
accumulated := f.String()
for _, v := range fieldValue {
f.Set(reflect.ValueOf(f.String() + v.String()).Convert(f.Type()))
accumulated += v.String()
}
f.SetString(accumulated)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ func TestRepetitionAcrossFields(t *testing.T) {
}

func TestAccumulateString(t *testing.T) {
type customString string
type testAccumulateString struct {
A string `@"."+`
A customString `@"."+`
}

parser := mustTestParser[testAccumulateString](t)
Expand Down