-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the performance of creating null terminated byte strings
- Loading branch information
Showing
4 changed files
with
36 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package convert | ||
|
||
import "github.com/godbus/dbus/v5" | ||
|
||
// ToNullTerminatedString connverts a regular string into a null terminated dbus variant string. | ||
func ToNullTerminatedString(input string) dbus.Variant { | ||
return dbus.MakeVariant([]byte(input + "\000")) | ||
// ToNullTerminated connverts a regular string into a null terminated byte string. | ||
func ToNullTerminated(input string) []byte { | ||
terminated := make([]byte, len(input)+1) | ||
copy(terminated, input) | ||
return terminated | ||
} |
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,28 @@ | ||
package convert | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestToNullTerminated(t *testing.T) { | ||
input := "test" | ||
|
||
got := ToNullTerminated(input) | ||
expect := []byte{'t', 'e', 's', 't', '\000'} | ||
if !bytes.Equal(got, expect) { | ||
t.Fatalf("Got %v, expected %v", got, expect) | ||
} | ||
} | ||
|
||
var benchResult []byte | ||
|
||
func BenchmarkToNullTerminated(b *testing.B) { | ||
var result []byte | ||
|
||
for i := 0; i < b.N; i++ { | ||
result = ToNullTerminated("long_input_string") | ||
} | ||
|
||
benchResult = result | ||
} |