Skip to content

Commit

Permalink
Merge pull request #1313 from adamdecaf/reduce-more-allocations
Browse files Browse the repository at this point in the history
Reduce more allocations
  • Loading branch information
adamdecaf authored Oct 24, 2023
2 parents e3059e8 + be57c39 commit 03f0f2e
Show file tree
Hide file tree
Showing 31 changed files with 1,197 additions and 440 deletions.
84 changes: 59 additions & 25 deletions addenda02.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,68 @@ func NewAddenda02() *Addenda02 {
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate call to confirm successful parsing and data validity.
func (addenda02 *Addenda02) Parse(record string) {
if utf8.RuneCountInString(record) != 94 {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
runes := []rune(record)

// 1-1 Always 7
// 2-3 Always 02
addenda02.TypeCode = string(runes[1:3])
// 4-10 Based on the information entered (04-10) 7 alphanumeric
addenda02.ReferenceInformationOne = strings.TrimSpace(string(runes[3:10]))
// 11-13 Based on the information entered (11-13) 3 alphanumeric
addenda02.ReferenceInformationTwo = strings.TrimSpace(string(runes[10:13]))
// 14-19
addenda02.TerminalIdentificationCode = strings.TrimSpace(string(runes[13:19]))
// 20-25
addenda02.TransactionSerialNumber = strings.TrimSpace(string(runes[19:25]))
// 26-29
addenda02.TransactionDate = strings.TrimSpace(string(runes[25:29]))
// 30-35
addenda02.AuthorizationCodeOrExpireDate = strings.TrimSpace(string(runes[29:35]))
// 36-62
addenda02.TerminalLocation = strings.TrimSpace(string(runes[35:62]))
// 63-77
addenda02.TerminalCity = strings.TrimSpace(string(runes[62:77]))
// 78-79
addenda02.TerminalState = strings.TrimSpace(string(runes[77:79]))
// 80-94
addenda02.TraceNumber = strings.TrimSpace(string(runes[79:94]))
buf := getBuffer()
defer saveBuffer(buf)

reset := func() string {
out := buf.String()
buf.Reset()
return out
}

// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++

// Append rune to buffer
buf.WriteRune(r)

// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always 02
addenda02.TypeCode = reset()
case 10:
// 4-10 Based on the information entered (04-10) 7 alphanumeric
addenda02.ReferenceInformationOne = strings.TrimSpace(reset())
case 13:
// 11-13 Based on the information entered (11-13) 3 alphanumeric
addenda02.ReferenceInformationTwo = strings.TrimSpace(reset())
case 19:
// 14-19
addenda02.TerminalIdentificationCode = strings.TrimSpace(reset())
case 25:
// 20-25
addenda02.TransactionSerialNumber = strings.TrimSpace(reset())
case 29:
// 26-29
addenda02.TransactionDate = strings.TrimSpace(reset())
case 35:
// 30-35
addenda02.AuthorizationCodeOrExpireDate = strings.TrimSpace(reset())
case 62:
// 36-62
addenda02.TerminalLocation = strings.TrimSpace(reset())
case 77:
// 63-77
addenda02.TerminalCity = strings.TrimSpace(reset())
case 79:
// 78-79
addenda02.TerminalState = strings.TrimSpace(reset())
case 94:
// 80-94
addenda02.TraceNumber = strings.TrimSpace(reset())
}
}
}

// String writes the Addenda02 struct to a 94 character string.
Expand Down
53 changes: 40 additions & 13 deletions addenda05.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,48 @@ func NewAddenda05() *Addenda05 {
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate call to confirm successful parsing and data validity.
func (addenda05 *Addenda05) Parse(record string) {
if utf8.RuneCountInString(record) != 94 {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
runes := []rune(record)

// 1-1 Always 7
// 2-3 Always 05
addenda05.TypeCode = string(runes[1:3])
// 4-83 Based on the information entered (04-83) 80 alphanumeric
addenda05.PaymentRelatedInformation = strings.TrimSpace(string(runes[3:83]))
// 84-87 SequenceNumber is consecutively assigned to each Addenda05 Record following
// an Entry Detail Record
addenda05.SequenceNumber = addenda05.parseNumField(string(runes[83:87]))
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda05.EntryDetailSequenceNumber = addenda05.parseNumField(string(runes[87:94]))

buf := getBuffer()
defer saveBuffer(buf)

reset := func() string {
out := buf.String()
buf.Reset()
return out
}

// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++

// Append rune to buffer
buf.WriteRune(r)

// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always 05
addenda05.TypeCode = reset()
case 83:
// 4-83 Based on the information entered (04-83) 80 alphanumeric
addenda05.PaymentRelatedInformation = strings.TrimSpace(reset())
case 87:
// 84-87 SequenceNumber is consecutively assigned to each Addenda05 Record following
// an Entry Detail Record
addenda05.SequenceNumber = addenda05.parseNumField(reset())
case 94:
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda05.EntryDetailSequenceNumber = addenda05.parseNumField(reset())
}
}
}

// String writes the Addenda05 struct to a 94 character string.
Expand Down
65 changes: 48 additions & 17 deletions addenda10.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,56 @@ func NewAddenda10() *Addenda10 {
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate call to confirm successful parsing and data validity.
func (addenda10 *Addenda10) Parse(record string) {
if utf8.RuneCountInString(record) != 94 {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
runes := []rune(record)

// 1-1 Always 7
// 2-3 Always 10
addenda10.TypeCode = string(runes[1:3])
// 04-06 Describes the type of payment
addenda10.TransactionTypeCode = string(runes[3:6])
// 07-24 Payment Amount For inbound IAT payments this field should contain the USD amount or may be blank.
addenda10.ForeignPaymentAmount = addenda10.parseNumField(string(runes[06:24]))
// 25-46 Insert blanks or zeros
addenda10.ForeignTraceNumber = strings.TrimSpace(string(runes[24:46]))
// 47-81 Receiving Company Name/Individual Name
addenda10.Name = strings.TrimSpace(string(runes[46:81]))
// 82-87 reserved - Leave blank
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda10.EntryDetailSequenceNumber = addenda10.parseNumField(string(runes[87:94]))

buf := getBuffer()
defer saveBuffer(buf)

reset := func() string {
out := buf.String()
buf.Reset()
return out
}

// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++

// Append rune to buffer
buf.WriteRune(r)

// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always 10
addenda10.TypeCode = reset()
case 6:
// 04-06 Describes the type of payment
addenda10.TransactionTypeCode = reset()
case 24:
// 07-24 Payment Amount For inbound IAT payments this field should contain the USD amount or may be blank.
addenda10.ForeignPaymentAmount = addenda10.parseNumField(reset())
case 46:
// 25-46 Insert blanks or zeros
addenda10.ForeignTraceNumber = strings.TrimSpace(reset())
case 81:
// 47-81 Receiving Company Name/Individual Name
addenda10.Name = strings.TrimSpace(reset())
case 87:
// 82-87 reserved - Leave blank
reset()
case 94:
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda10.EntryDetailSequenceNumber = addenda10.parseNumField(reset())
}
}
}

// String writes the Addenda10 struct to a 94 character string.
Expand Down
55 changes: 42 additions & 13 deletions addenda11.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,50 @@ func NewAddenda11() *Addenda11 {
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate call to confirm successful parsing and data validity.
func (addenda11 *Addenda11) Parse(record string) {
if utf8.RuneCountInString(record) != 94 {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
runes := []rune(record)

// 1-1 Always 7
// 2-3 Always 11
addenda11.TypeCode = string(runes[1:3])
// 4-38
addenda11.OriginatorName = strings.TrimSpace(string(runes[3:38]))
// 39-73
addenda11.OriginatorStreetAddress = strings.TrimSpace(string(runes[38:73]))
// 74-87 reserved - Leave blank
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda11.EntryDetailSequenceNumber = addenda11.parseNumField(string(runes[87:94]))

buf := getBuffer()
defer saveBuffer(buf)

reset := func() string {
out := buf.String()
buf.Reset()
return out
}

// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++

// Append rune to buffer
buf.WriteRune(r)

// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always 11
addenda11.TypeCode = reset()
case 38:
// 4-38
addenda11.OriginatorName = strings.TrimSpace(reset())
case 73:
// 39-73
addenda11.OriginatorStreetAddress = strings.TrimSpace(reset())
case 87:
// 74-87 reserved - Leave blank
reset()
case 94:
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda11.EntryDetailSequenceNumber = addenda11.parseNumField(reset())
}
}
}

// String writes the Addenda11 struct to a 94 character string.
Expand Down
55 changes: 42 additions & 13 deletions addenda12.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,50 @@ func NewAddenda12() *Addenda12 {
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate call to confirm successful parsing and data validity.
func (addenda12 *Addenda12) Parse(record string) {
if utf8.RuneCountInString(record) != 94 {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
runes := []rune(record)

// 1-1 Always 7
// 2-3 Always 12
addenda12.TypeCode = string(runes[1:3])
// 4-38
addenda12.OriginatorCityStateProvince = strings.TrimSpace(string(runes[3:38]))
// 39-73
addenda12.OriginatorCountryPostalCode = strings.TrimSpace(string(runes[38:73]))
// 74-87 reserved - Leave blank
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda12.EntryDetailSequenceNumber = addenda12.parseNumField(string(runes[87:94]))

buf := getBuffer()
defer saveBuffer(buf)

reset := func() string {
out := buf.String()
buf.Reset()
return out
}

// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++

// Append rune to buffer
buf.WriteRune(r)

// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always 12
addenda12.TypeCode = reset()
case 38:
// 4-38
addenda12.OriginatorCityStateProvince = strings.TrimSpace(reset())
case 73:
// 39-73
addenda12.OriginatorCountryPostalCode = strings.TrimSpace(reset())
case 87:
// 74-87 reserved - Leave blank
reset()
case 94:
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda12.EntryDetailSequenceNumber = addenda12.parseNumField(reset())
}
}
}

// String writes the Addenda12 struct to a 94 character string.
Expand Down
Loading

0 comments on commit 03f0f2e

Please sign in to comment.