diff --git a/README.md b/README.md index 72fb038..cc5b865 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ A Go library for parsing, transforming, and validating HL7 version 2.x messages Zero external dependencies. Requires Go 1.23+. +> [!WARNING] +> The code in this project is mostly generated by an AI tool (Claude Code). This project is intended as a personal experiment to observe the efficacy of generative AI tools in software development. All code has been reviewed by a human, and the implemented algorithms were developed using instruction from a human. + ```go msg, _ := hl7.ParseMessage(rawBytes) diff --git a/accessor.go b/accessor.go index cb8e8bf..58b8dbc 100644 --- a/accessor.go +++ b/accessor.go @@ -14,7 +14,10 @@ package hl7 -import "strconv" +import ( + "strconv" + "unsafe" +) // Location represents a specific position in an HL7 message hierarchy. // All indices are 0-based internally. The string representation uses @@ -38,21 +41,29 @@ type Location struct { // Location{Segment: "OBX", SegmentIndex: 1, Field: 5}.String() // "OBX(1)-5" // Location{Segment: "PID", Field: 3, Repetition: 1}.String() // "PID-3[1]" func (loc Location) String() string { - s := loc.Segment + b := make([]byte, len(loc.Segment), 110) + copy(b, []byte(loc.Segment)) if loc.SegmentIndex > 0 { - s += "(" + strconv.Itoa(loc.SegmentIndex) + ")" + b = append(b, '(') + b = strconv.AppendInt(b, int64(loc.SegmentIndex), 10) + b = append(b, ')') } - s += "-" + strconv.Itoa(loc.Field) + b = append(b, '-') + b = strconv.AppendInt(b, int64(loc.Field), 10) if loc.Repetition > 0 { - s += "[" + strconv.Itoa(loc.Repetition) + "]" + b = append(b, '[') + b = strconv.AppendInt(b, int64(loc.Repetition), 10) + b = append(b, ']') } if loc.Component > 0 { - s += "." + strconv.Itoa(loc.Component) + b = append(b, '.') + b = strconv.AppendInt(b, int64(loc.Component), 10) } if loc.SubComponent > 0 { - s += "." + strconv.Itoa(loc.SubComponent) + b = append(b, '.') + b = strconv.AppendInt(b, int64(loc.SubComponent), 10) } - return s + return unsafe.String(unsafe.SliceData(b), len(b)) } // ParseLocation parses a terser-style location string into a Location. diff --git a/accessor_test.go b/accessor_test.go index 8167346..8198f10 100644 --- a/accessor_test.go +++ b/accessor_test.go @@ -424,6 +424,21 @@ func TestMessageGetPresentButEmptyField(t *testing.T) { } } +func BenchmarkLocationString(b *testing.B) { + l := Location{ + Segment: "PID", + SegmentIndex: 123, + Field: 123, + Repetition: 123, + Component: 123, + SubComponent: 123, + } + + for b.Loop() { + _ = l.String() + } +} + func BenchmarkParseLocation(b *testing.B) { inputs := []struct { name string