You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
795 B

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import "io"
  3. type String string
  4. // String
  5. func (self String) Equals(other Binary) bool {
  6. return self == other
  7. }
  8. func (self String) Less(other Binary) bool {
  9. if o, ok := other.(String); ok {
  10. return self < o
  11. } else {
  12. panic("Cannot compare unequal types")
  13. }
  14. }
  15. func (self String) ByteSize() int {
  16. return len(self)+4
  17. }
  18. func (self String) WriteTo(w io.Writer) (n int64, err error) {
  19. var n_ int
  20. _, err = UInt32(len(self)).WriteTo(w)
  21. if err != nil { return n, err }
  22. n_, err = w.Write([]byte(self))
  23. return int64(n_+4), err
  24. }
  25. func ReadString(r io.Reader) String {
  26. length := int(ReadUInt32(r))
  27. bytes := make([]byte, length)
  28. _, err := io.ReadFull(r, bytes)
  29. if err != nil { panic(err) }
  30. return String(bytes)
  31. }