From e13b13e548af252cf709da7ac8adf0f2720b3b11 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 30 Nov 2023 10:04:06 -0800 Subject: [PATCH] initial commits --- string.go | 43 ++++++++++++++++++++ string_test.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 string.go create mode 100644 string_test.go diff --git a/string.go b/string.go new file mode 100644 index 0000000..79d5af7 --- /dev/null +++ b/string.go @@ -0,0 +1,43 @@ +package bytereader + +import ( + "io" +) + +var _ io.ByteReader = &internalByteReaderFromString{} + +type internalByteReaderFromString struct { + value string + index int +} + +// NewByteReaderFromString returns a new io.ByteReader from a string. +// +// Example usage: +// +// var s string = "ONCE 🙂 TWICE 🙂 THRICE 🙂 FOURCE" +// +// var byteReader io.ByteReader = bytereader.NewByteReaderFromString(s) +func NewByteReaderFromString(value string) io.ByteReader { + return &internalByteReaderFromString{ + value:value, + } +} + +func (receiver *internalByteReaderFromString) ReadByte() (byte, error) { + if nil == receiver { + return 0, errNilReceiver + } + + var value string = receiver.value + var index int = receiver.index + + if len(value) <= index { + return 0, io.EOF + } + + var b byte = receiver.value[index] + receiver.index++ + + return b, nil +} diff --git a/string_test.go b/string_test.go new file mode 100644 index 0000000..8dbaff5 --- /dev/null +++ b/string_test.go @@ -0,0 +1,107 @@ +package bytereader_test + +import ( + "testing" + + "io" + + "sourcecode.social/reiver/go-bytereader" +) + +func TestNewByteReaderFromString(t *testing.T) { + + tests := []struct{ + Value string + }{ + { + Value: "", + }, + + + + { + Value: "apple", + }, + { + Value: "banana", + }, + { + Value: "cherry", + }, + + + + { + Value: "ONCE TWICE THRICE FOURCE", + }, + + + + { + Value: "۰۱۲۳۴۵۶۷۸۹", + }, + + + + { + Value: "😈 👹 😡", + }, + } + + testloop: for testNumber, test := range tests { + + var byteReader io.ByteReader = bytereader.NewByteReaderFromString(test.Value) + + var buffer [64]byte + var p []byte = buffer[0:0] + { + length := len(test.Value) + + for i:=0; i