-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move template funcs from utils package to template package
- Loading branch information
1 parent
422092f
commit 9f17301
Showing
5 changed files
with
102 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package template | ||
|
||
import ( | ||
"encoding/hex" | ||
) | ||
|
||
func FormatSerial(bytes []byte) string { | ||
if len(bytes) == 0 { | ||
return "" | ||
} | ||
dst := make([]byte, hex.EncodedLen(len(bytes))) | ||
hex.Encode(dst, bytes) | ||
final := make([]byte, 3*len(bytes)) | ||
for i := 0; i < len(bytes); i++ { | ||
final[3*i] = dst[2*i] | ||
final[3*i+1] = dst[2*i+1] | ||
final[3*i+2] = ':' | ||
} | ||
return string(final[:len(final)-1]) | ||
} | ||
|
||
func Chunk(str string, length int) []string { | ||
chunks := make([]string, 0) | ||
for i := 0; i <= len(str)-length; i += length { | ||
chunks = append(chunks, str[i:i+length]) | ||
} | ||
if len(str)%length != 0 { | ||
chunks = append(chunks, str[len(str)-len(str)%length:]) | ||
} | ||
return chunks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package template | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFormatSerial(t *testing.T) { | ||
tests := []struct { | ||
input []byte | ||
expected string | ||
}{ | ||
{ | ||
input: []byte{}, | ||
expected: "", | ||
}, | ||
{ | ||
input: []byte{0x00}, | ||
expected: "00", | ||
}, | ||
{ | ||
input: []byte{0x12, 0x34}, | ||
expected: "12:34", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
got := FormatSerial(tc.input) | ||
if got != tc.expected { | ||
t.Fatalf("expected: %v, got: %v", tc.expected, got) | ||
} | ||
} | ||
} | ||
|
||
func TestChunk(t *testing.T) { | ||
tests := []struct { | ||
str string | ||
length int | ||
expected []string | ||
}{ | ||
{ | ||
str: "", | ||
length: 2, | ||
expected: []string{}, | ||
}, | ||
{ | ||
str: "aabbccdd", | ||
length: 2, | ||
expected: []string{"aa", "bb", "cc", "dd"}, | ||
}, | ||
{ | ||
str: "aabbccd", | ||
length: 2, | ||
expected: []string{"aa", "bb", "cc", "d"}, | ||
}, | ||
{ | ||
str: "aaa", | ||
length: 5, | ||
expected: []string{"aaa"}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
got := Chunk(tc.str, tc.length) | ||
if !reflect.DeepEqual(got, tc.expected) { | ||
t.Fatalf("expected: %v, got: %v", tc.expected, got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters