Esempio n. 1
0
func writeArrayOrSlice(w io.Writer, val reflect.ArrayOrSliceValue) os.Error {
  writeByte(w, LIST);
  binary.Write(w, binary.BigEndian, uint32(val.Len()));
  
  for i := 0; i < val.Len(); i++ {
    if err := writeValue(w, val.Elem(i)); err != nil {
      return err
    }
  }

  writeByte(w, NIL);
  return nil;
}
Esempio n. 2
0
func PackArray(writer io.Writer, value reflect.ArrayOrSliceValue) (n int, err os.Error) {
	{
		elemType, ok := value.Type().(reflect.ArrayOrSliceType).Elem().(*reflect.UintType)
		if ok && elemType.Kind() == reflect.Uint8 {
			return PackBytes(writer, value.Interface().([]byte))
		}
	}

	l := value.Len()
	if l < 16 {
		n, err := writer.Write([]byte{0x90 | byte(l)})
		if err != nil {
			return n, err
		}
		for i := 0; i < l; i++ {
			_n, err := PackValue(writer, value.Elem(i))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else if l < 65536 {
		n, err := writer.Write([]byte{0xdc, byte(l >> 8), byte(l)})
		if err != nil {
			return n, err
		}
		for i := 0; i < l; i++ {
			_n, err := PackValue(writer, value.Elem(i))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else {
		n, err := writer.Write([]byte{0xdd, byte(l >> 24), byte(l >> 16), byte(l >> 8), byte(l)})
		if err != nil {
			return n, err
		}
		for i := 0; i < l; i++ {
			_n, err := PackValue(writer, value.Elem(i))
			if err != nil {
				return n, err
			}
			n += _n
		}
	}
	return n, nil
}
Esempio n. 3
0
func writeArrayOrSlice(w io.Writer, val reflect.ArrayOrSliceValue) os.Error {
	fmt.Fprint(w, "[")

	for i := 0; i < val.Len(); i++ {
		if err := writeValue(w, val.Elem(i)); err != nil {
			return err
		}

		if i < val.Len()-1 {
			fmt.Fprint(w, ",")
		}
	}

	fmt.Fprint(w, "]")
	return nil
}
Esempio n. 4
0
func writeArrayOrSlice(w io.Writer, val reflect.ArrayOrSliceValue) (err os.Error) {
	_, err = fmt.Fprint(w, "l")
	if err != nil {
		return
	}
	for i := 0; i < val.Len(); i++ {
		if err := writeValue(w, val.Elem(i)); err != nil {
			return err
		}
	}

	_, err = fmt.Fprint(w, "e")
	if err != nil {
		return
	}
	return nil
}
Esempio n. 5
0
func writeArrayOrSlice(w io.Writer, val reflect.ArrayOrSliceValue) (err os.Error) {
	if _, err = fmt.Fprint(w, "["); err != nil {
		return
	}

	for i := 0; i < val.Len(); i++ {
		if err = writeValue(w, val.Elem(i)); err != nil {
			return
		}

		if i < val.Len()-1 {
			if _, err = fmt.Fprint(w, ","); err != nil {
				return
			}
		}
	}

	_, err = fmt.Fprint(w, "]")
	return
}
Esempio n. 6
0
func fuzzyEqualArrayOrSlice2(a reflect.ArrayOrSliceValue, b reflect.ArrayOrSliceValue) bool {
	if a.Len() != b.Len() {
		return false
	}

	for i := 0; i < a.Len(); i++ {
		ea := deInterface(a.Elem(i))
		eb := deInterface(b.Elem(i))
		if !fuzzyEqualValue(ea, eb) {
			return false
		}
	}
	return true
}