]> Cypherpunks.ru repositories - netstring.git/blobdiff - ns.go
Refactoring, io.Reader/Writer friendliness, performance optimization
[netstring.git] / ns.go
similarity index 53%
rename from netstring.go
rename to ns.go
index 8345556198e2836a3b7205221e36b84f529bd369..af1159f39262f4b6a23f0aac8a325284345e56af 100644 (file)
+++ b/ns.go
@@ -21,30 +21,15 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // (http://cr.yp.to/proto/netstrings.txt) format for binary string
 // serialization.
 //
-//   buf := bytes.NewBuffer(nil)
-//   w := netstring.NewWriter(buf)
-//   w.Write([]byte("hello")) // buf contains "5:hello,"
-//   w.Write([]byte("world!")) // buf contains "5:hello,6:world!,"
-//   r := netstring.NewReader(buf)
-//   size, err := r.Iter() // size is 5
-//   r.Discard() // discard (skip) current netstring ("hello")
-//   size, err = r.Iter() // size is 6
-//   out := make([]byte, size)
-//   r.Read(out) // out contains "world!" bytes
-//
-// Pay attention that netstring uses bufio for Reader and Writer.
+//   var b bytes.Buffer
+//   w := netstring.NewWriter(&b)
+//   n, _ = w.WriteChunk([]byte("hello")) // n is 8, "5:hello,"
+//   n, _ = w.WriteSize(6)                // n is 2
+//   n, _ = w.Write([]byte("wor"))        // n is 3
+//   n, _ = w.Write([]byte("ld!"))        // n is 3, "5:hello,6:world!,"
+//   r := netstring.NewReader(&b)
+//   size, err := r.Next()         // size is 5
+//   r.Discard()                   // skip that chunk
+//   size, err = r.Next()          // size is 6
+//   data, _ := ioutil.ReadAll(r)  // data contains "world!"
 package netstring
-
-import (
-       "errors"
-)
-
-const (
-       MaxPrefixSize = 21
-)
-
-var (
-       ErrBufSize    error = errors.New("Invalid destination buffer size")
-       ErrState      error = errors.New("Invalid state")
-       ErrTerminator error = errors.New("Invalid terminator")
-)