Exemplo n.º 1
0
// any to string
func atos(i interface{}) (s string) {
	switch t := i.(type) {
	case int64:
		s = strconv.Itoa64(t)
	case uint64:
		s = strconv.Uitoa64(t)
	case float32:
		s = strconv.Ftoa32(t, 'f', -1)
	case float64:
		s = strconv.Ftoa64(t, 'f', -1)
	case []byte:
		s = string(t)
	case Date:
		return t.String()
	case Time:
		return t.String()
	case DateTime:
		return t.String()
	case string:
		return t
	default:
		panic("Not a string or compatible type")
	}
	return
}
Exemplo n.º 2
0
func AsString(v interface{}) (string, os.Error) {
	switch value := v.(type) {
	default:
		return "", os.NewError(fmt.Sprintf("unexpected type: %T", value))
	case int:
		return strconv.Itoa(value), nil
	case int8:
		return strconv.Itoa(int(value)), nil
	case int16:
		return strconv.Itoa(int(value)), nil
	case int32:
		return strconv.Itoa(int(value)), nil
	case int64:
		return strconv.Itoa64(value), nil
	case uint:
		return strconv.Uitoa(value), nil
	case uint8:
		return strconv.Uitoa(uint(value)), nil
	case uint16:
		return strconv.Uitoa(uint(value)), nil
	case uint32:
		return strconv.Uitoa(uint(value)), nil
	case uint64:
		return strconv.Uitoa64(value), nil
	case float32:
		return strconv.Ftoa32(value, 'g', -1), nil
	case float64:
		return strconv.Ftoa64(value, 'g', -1), nil
	case string:
		return value, nil
	}
	panic(fmt.Sprintf("unsupported type: %s", reflect.ValueOf(v).Type().Name()))
}
Exemplo n.º 3
0
func valueToString(v reflect.Value) (string, os.Error) {
	if v == nil {
		return "null", nil
	}

	switch v := v.(type) {
	case *reflect.PtrValue:
		return valueToString(reflect.Indirect(v))
	case *reflect.InterfaceValue:
		return valueToString(v.Elem())
	case *reflect.BoolValue:
		x := v.Get()
		if x {
			return "true", nil
		} else {
			return "false", nil
		}

	case *reflect.IntValue:
		return strconv.Itoa(v.Get()), nil
	case *reflect.Int8Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int16Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int32Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int64Value:
		return strconv.Itoa64(v.Get()), nil

	case *reflect.UintValue:
		return strconv.Uitoa(v.Get()), nil
	case *reflect.Uint8Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint16Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint32Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint64Value:
		return strconv.Uitoa64(v.Get()), nil
	case *reflect.UintptrValue:
		return strconv.Uitoa64(uint64(v.Get())), nil

	case *reflect.FloatValue:
		return strconv.Ftoa(v.Get(), 'g', -1), nil
	case *reflect.Float32Value:
		return strconv.Ftoa32(v.Get(), 'g', -1), nil
	case *reflect.Float64Value:
		return strconv.Ftoa64(v.Get(), 'g', -1), nil

	case *reflect.StringValue:
		return v.Get(), nil
	case *reflect.SliceValue:
		typ := v.Type().(*reflect.SliceType)
		if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
			return string(v.Interface().([]byte)), nil
		}
	}
	return "", os.NewError("Unsupported type")
}
Exemplo n.º 4
0
func (e *encoder) floatv(tag string, in reflect.Value) {
	// FIXME: Handle 64 bits here.
	s := strconv.Ftoa32(float32(in.Float()), 'g', -1)
	switch s {
	case "+Inf":
		s = ".inf"
	case "-Inf":
		s = "-.inf"
	case "NaN":
		s = ".nan"
	}
	e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE)
}
Exemplo n.º 5
0
func formatReflectValue(x reflect.Value) (string, os.Error) {
	/*
	   if !x.CanSet() {
	       return "", ErrorCantSet
	   }
	*/
	var (
		errc os.Error
		kind = x.Kind()
		//vintstr   string
	)
	switch kind {
	// Format pointers to standard types.
	case reflect.String:
		return x.Interface().(string), nil
	case reflect.Int:
		return strconv.Itoa(x.Interface().(int)), nil
	case reflect.Int8:
		return strconv.Itoa64(int64(x.Interface().(int8))), nil
	case reflect.Int16:
		return strconv.Itoa64(int64(x.Interface().(int16))), nil
	case reflect.Int32:
		return strconv.Itoa64(int64(x.Interface().(int32))), nil
	case reflect.Int64:
		return strconv.Itoa64(x.Interface().(int64)), nil
	case reflect.Uint:
		return strconv.Uitoa(x.Interface().(uint)), nil
	case reflect.Uint8:
		return strconv.Uitoa64(uint64(x.Interface().(uint8))), nil
	case reflect.Uint16:
		return strconv.Uitoa64(uint64(x.Interface().(uint16))), nil
	case reflect.Uint32:
		return strconv.Uitoa64(uint64(x.Interface().(uint32))), nil
	case reflect.Uint64:
		return strconv.Uitoa64(x.Interface().(uint64)), nil
	case reflect.Float32:
		return strconv.Ftoa32(x.Interface().(float32), FloatFmt, FloatPrec), nil
	case reflect.Float64:
		return strconv.Ftoa64(x.Interface().(float64), FloatFmt, FloatPrec), nil
	case reflect.Complex64:
		fallthrough
	case reflect.Complex128:
		errc = ErrorUnimplemented
	case reflect.Bool:
		return strconv.Btoa(x.Interface().(bool)), nil
	default:
		errc = ErrorFieldType
	}
	return "", errc
}
Exemplo n.º 6
0
// Encode values fro each field
func encodeParamValues(a []interface{}) ([]byte, int) {
	var b []byte
	for i := 0; i < len(a); i++ {
		f := a[i]
		switch t := f.(type) {
		case string:
			b = append(b, packString(string(t))...)
		case int:
			b = append(b, packString(strconv.Itoa(int(t)))...)
		case float32:
			b = append(b, packString(strconv.Ftoa32(float32(t), 'f', -1))...)
		case float64:
			b = append(b, packString(strconv.Ftoa64(float64(t), 'f', -1))...)
		}
	}
	return b, len(b)
}
Exemplo n.º 7
0
func (m *SelectManager) Project(any interface{}) *SelectManager {
	switch val := any.(type) {
	case ast.Node:
		m.project(val)
	case string:
		m.project(ast.NewSqlLiteral(any.(string)))
	case bool:
		m.project(ast.NewSqlLiteral(strconv.Btoa(any.(bool))))
	case int:
		m.project(ast.NewSqlLiteral(strconv.Itoa(any.(int))))
	case int64:
		m.project(ast.NewSqlLiteral(strconv.Itoa64(any.(int64))))
	case float32:
		m.project(ast.NewSqlLiteral(strconv.Ftoa32(any.(float32), 'f', 0)))
	case float64:
		m.project(ast.NewSqlLiteral(strconv.Ftoa64(any.(float64), 'f', 0)))
	}
	return m
}
Exemplo n.º 8
0
func (c *ToSql) Convert(unknown interface{}) (s string) {
	switch val := unknown.(type) {
	case string:
		s = strconv.Quote(val)
	case bool:
		s = strconv.Btoa(val)
	case int:
		s = strconv.Itoa(val)
	case int64:
		s = strconv.Itoa64(val)
	case uint:
		s = strconv.Uitoa(val)
	case uint64:
		s = strconv.Uitoa64(val)
	case float32:
		s = strconv.Ftoa32(val, 'f', -1)
	case float64:
		s = strconv.Ftoa64(val, 'f', -1)
	default:
		s = c.ConvertUnknown(unknown)
	}
	return
}
Exemplo n.º 9
0
func (conn *Conn) writeBind(stmt *Statement) {
	values := make([]string, len(stmt.params))

	var paramValuesLen int
	for i, param := range stmt.params {
		value := param.value
		if val, ok := value.(uint64); ok {
			value = int64(val)
		}

		switch val := value.(type) {
		case bool:
			if val {
				values[i] = "t"
			} else {
				values[i] = "f"
			}

		case byte:
			values[i] = string([]byte{val})

		case float32:
			values[i] = strconv.Ftoa32(val, 'f', -1)

		case float64:
			values[i] = strconv.Ftoa64(val, 'f', -1)

		case int:
			values[i] = strconv.Itoa(val)

		case int16:
			values[i] = strconv.Itoa(int(val))

		case int32:
			values[i] = strconv.Itoa(int(val))

		case int64:
			switch param.typ {
			case Date:
				values[i] = time.SecondsToUTC(val).Format("2006-01-02")

			case Time, TimeTZ:
				values[i] = time.SecondsToUTC(val).Format("15:04:05")

			case Timestamp, TimestampTZ:
				values[i] = time.SecondsToUTC(val).Format("2006-01-02 15:04:05")

			default:
				values[i] = strconv.Itoa64(val)
			}

		case nil:

		case *big.Rat:
			if val.IsInt() {
				values[i] = val.Num().String()
			} else {
				// FIXME: Find a better way to do this.
				prec999 := val.FloatString(999)
				trimmed := strings.TrimRight(prec999, "0")
				sepIndex := strings.Index(trimmed, ".")
				prec := len(trimmed) - sepIndex - 1
				values[i] = val.FloatString(prec)
			}

		case string:
			values[i] = val

		case *time.Time:
			switch param.typ {
			case Date:
				values[i] = val.Format("2006-01-02")

			case Time, TimeTZ:
				values[i] = val.Format("15:04:05")

			case Timestamp, TimestampTZ:
				values[i] = val.Format("2006-01-02 15:04:05")

			default:
				panic("invalid use of *time.Time")
			}

		default:
			panic("unsupported parameter type")
		}

		paramValuesLen += len(values[i])
	}

	msgLen := int32(4 +
		len(stmt.portalName) + 1 +
		len(stmt.name) + 1 +
		2 + 2 +
		2 + len(stmt.params)*4 + paramValuesLen +
		2 + 2)

	conn.writeFrontendMessageCode(_Bind)
	conn.writeInt32(msgLen)
	conn.writeString0(stmt.portalName)
	conn.writeString0(stmt.name)
	conn.writeInt16(1)
	conn.writeInt16(int16(textFormat))
	conn.writeInt16(int16(len(stmt.params)))

	for i, param := range stmt.params {
		if param.value == nil {
			conn.writeInt32(-1)
		} else {
			conn.writeInt32(int32(len(values[i])))
			conn.writeString(values[i])
		}
	}

	conn.writeInt16(1)
	conn.writeInt16(int16(textFormat))

	conn.writeFlush()
}
Exemplo n.º 10
0
// Fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
func (f *Fmt) Fmt_G32(v float32) *Fmt {
	return fmtString(f, strconv.Ftoa32(v, 'G', doPrec(f, -1)))
}
Exemplo n.º 11
0
// Fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
func (f *Fmt) Fmt_fb32(v float32) *Fmt { return fmtString(f, strconv.Ftoa32(v, 'b', 0)) }
Exemplo n.º 12
0
func (p TType) CoerceData(data interface{}) (interface{}, bool) {
	if data == nil {
		switch p {
		case STOP:
			return nil, true
		case VOID:
			return nil, true
		case BOOL:
			return false, true
		case BYTE:
			return byte(0), true
		case DOUBLE:
			return float64(0), true
		case I16:
			return int16(0), true
		case I32:
			return int32(0), true
		case I64:
			return int64(0), true
		case STRING, UTF8, UTF16:
			return "", true
		case BINARY:
			return nil, true
		case STRUCT:
			return NewTStructEmpty(""), true
		case MAP:
			return NewTMapDefault(), true
		case LIST:
			return NewTListDefault(), true
		case SET:
			return NewTSetDefault(), true
		default:
			panic("Invalid thrift type to coerce")
		}
	}
	switch p {
	case STOP:
		return nil, true
	case VOID:
		return nil, true
	case BOOL:
		switch b := data.(type) {
		default:
			return false, false
		case bool:
			return b, true
		case Numeric:
			return bool(b.Int() != 0), true
		case int:
			return b != 0, true
		case byte:
			return b != 0, true
		case int8:
			return b != 0, true
		case int16:
			return b != 0, true
		case int32:
			return b != 0, true
		case int64:
			return b != 0, true
		case uint:
			return b != 0, true
		case uint16:
			return b != 0, true
		case uint32:
			return b != 0, true
		case uint64:
			return b != 0, true
		case float32:
			return b != 0, true
		case float64:
			return b != 0, true
		case []byte:
			return len(b) > 1 || (len(b) == 1 && b[0] != 0), true
		case Stringer:
			v := b.String()
			if v == "false" || v == "0" || len(v) == 0 {
				return false, true
			}
			return true, true
		case string:
			if b == "false" || b == "0" || len(b) == 0 {
				return false, true
			}
			return true, true
		}
	case BYTE:
		if b, ok := data.(byte); ok {
			return b, true
		}
		if b, ok := data.(Numeric); ok {
			return b.Byte(), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return byte(1), true
			}
			return byte(0), true
		}
		if b, ok := data.(int); ok {
			return byte(b), true
		}
		if b, ok := data.(int8); ok {
			return byte(b), true
		}
		if b, ok := data.(int16); ok {
			return byte(b), true
		}
		if b, ok := data.(int32); ok {
			return byte(b), true
		}
		if b, ok := data.(int64); ok {
			return byte(b), true
		}
		if b, ok := data.(uint); ok {
			return byte(b), true
		}
		if b, ok := data.(uint8); ok {
			return byte(b), true
		}
		if b, ok := data.(uint16); ok {
			return byte(b), true
		}
		if b, ok := data.(uint32); ok {
			return byte(b), true
		}
		if b, ok := data.(uint64); ok {
			return byte(b), true
		}
		if b, ok := data.(float32); ok {
			return byte(int(b)), true
		}
		if b, ok := data.(float64); ok {
			return byte(int(b)), true
		}
		if b, ok := data.([]byte); ok {
			if len(b) > 0 {
				return b[0], true
			}
			return byte(0), true
		}
		if b, ok := data.(Stringer); ok {
			data = b.String()
		}
		if b, ok := data.(string); ok {
			i1, err := strconv.Atoi(b)
			if err != nil {
				return byte(int(i1)), true
			}
		}
		return byte(0), false
	case DOUBLE:
		if b, ok := data.(float32); ok {
			return float64(b), true
		}
		if b, ok := data.(float64); ok {
			return b, true
		}
		if b, ok := data.(Numeric); ok {
			return bool(b.Float64() != 0.0), true
		}
		if b, ok := data.(byte); ok {
			return float64(b), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return float64(1.0), true
			}
			return float64(0.0), true
		}
		if b, ok := data.(int); ok {
			return float64(b), true
		}
		if b, ok := data.(int8); ok {
			return float64(b), true
		}
		if b, ok := data.(int16); ok {
			return float64(b), true
		}
		if b, ok := data.(int32); ok {
			return float64(b), true
		}
		if b, ok := data.(int64); ok {
			return float64(b), true
		}
		if b, ok := data.(uint); ok {
			return float64(b), true
		}
		if b, ok := data.(uint8); ok {
			return float64(b), true
		}
		if b, ok := data.(uint16); ok {
			return float64(b), true
		}
		if b, ok := data.(uint32); ok {
			return float64(b), true
		}
		if b, ok := data.(uint64); ok {
			return float64(b), true
		}
		if b, ok := data.([]byte); ok {
			if len(b) > 0 {
				return float64(b[0]), true
			}
			return float64(0), true
		}
		if b, ok := data.(Stringer); ok {
			data = b.String()
		}
		if b, ok := data.(string); ok {
			d1, err := strconv.Atof64(b)
			if err != nil {
				return d1, true
			}
		}
		return float64(0), false
	case I16:
		if b, ok := data.(int16); ok {
			return b, true
		}
		if b, ok := data.(int); ok {
			return int16(b), true
		}
		if b, ok := data.(Numeric); ok {
			return bool(b.Int16() != 0), true
		}
		if b, ok := data.(byte); ok {
			return int16(b), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return int16(1.0), true
			}
			return int16(0.0), true
		}
		if b, ok := data.(int8); ok {
			return int16(b), true
		}
		if b, ok := data.(int32); ok {
			return int16(b), true
		}
		if b, ok := data.(int64); ok {
			return int16(b), true
		}
		if b, ok := data.(uint); ok {
			return int16(b), true
		}
		if b, ok := data.(uint8); ok {
			return int16(b), true
		}
		if b, ok := data.(uint16); ok {
			return int16(b), true
		}
		if b, ok := data.(uint32); ok {
			return int16(b), true
		}
		if b, ok := data.(uint64); ok {
			return int16(b), true
		}
		if b, ok := data.(float32); ok {
			return int16(int(b)), true
		}
		if b, ok := data.(float64); ok {
			return int16(int(b)), true
		}
		if b, ok := data.(Stringer); ok {
			data = b.String()
		}
		if b, ok := data.(string); ok {
			i1, err := strconv.Atoi(b)
			if err != nil {
				return int16(i1), true
			}
		}
		return int16(0), false
	case I32:
		if b, ok := data.(int32); ok {
			return b, true
		}
		if b, ok := data.(int); ok {
			return int32(b), true
		}
		if b, ok := data.(Numeric); ok {
			return bool(b.Int32() != 0), true
		}
		if b, ok := data.(byte); ok {
			return int32(b), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return int32(1.0), true
			}
			return int32(0.0), true
		}
		if b, ok := data.(int8); ok {
			return int32(b), true
		}
		if b, ok := data.(int16); ok {
			return int32(b), true
		}
		if b, ok := data.(int64); ok {
			return int32(b), true
		}
		if b, ok := data.(uint); ok {
			return int32(b), true
		}
		if b, ok := data.(uint8); ok {
			return int32(b), true
		}
		if b, ok := data.(uint16); ok {
			return int32(b), true
		}
		if b, ok := data.(uint32); ok {
			return int32(b), true
		}
		if b, ok := data.(uint64); ok {
			return int32(b), true
		}
		if b, ok := data.(float32); ok {
			return int32(int(b)), true
		}
		if b, ok := data.(float64); ok {
			return int32(int(b)), true
		}
		if b, ok := data.(Stringer); ok {
			data = b.String()
		}
		if b, ok := data.(string); ok {
			i1, err := strconv.Atoi(b)
			if err != nil {
				return int32(i1), true
			}
		}
		return int32(0), false
	case I64:
		if b, ok := data.(int64); ok {
			return b, true
		}
		if b, ok := data.(int32); ok {
			return int64(b), true
		}
		if b, ok := data.(int); ok {
			return int64(b), true
		}
		if b, ok := data.(Numeric); ok {
			return bool(b.Int64() != 0), true
		}
		if b, ok := data.(byte); ok {
			return int64(b), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return int64(1.0), true
			}
			return int64(0.0), true
		}
		if b, ok := data.(int8); ok {
			return int64(b), true
		}
		if b, ok := data.(int16); ok {
			return int64(b), true
		}
		if b, ok := data.(uint); ok {
			return int64(b), true
		}
		if b, ok := data.(uint8); ok {
			return int64(b), true
		}
		if b, ok := data.(uint16); ok {
			return int64(b), true
		}
		if b, ok := data.(uint32); ok {
			return int64(b), true
		}
		if b, ok := data.(uint64); ok {
			return int64(b), true
		}
		if b, ok := data.(float32); ok {
			return int64(b), true
		}
		if b, ok := data.(float64); ok {
			return int64(b), true
		}
		if b, ok := data.(Stringer); ok {
			data = b.String()
		}
		if b, ok := data.(string); ok {
			i1, err := strconv.Atoi64(b)
			if err != nil {
				return i1, true
			}
		}
		return int64(0), false
	case STRING, UTF8, UTF16:
		if b, ok := data.([]byte); ok {
			return string(b), true
		}
		if b, ok := data.(Enumer); ok {
			if i1, ok := data.(int); ok {
				return string(i1), true
			}
			return b.String(), true
		}
		if b, ok := data.(Stringer); ok {
			return b.String(), true
		}
		if b, ok := data.(string); ok {
			return b, true
		}
		if b, ok := data.(int); ok {
			return string(b), true
		}
		if b, ok := data.(byte); ok {
			return string(b), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return "true", true
			}
			return "false", true
		}
		if b, ok := data.(int8); ok {
			return string(b), true
		}
		if b, ok := data.(int16); ok {
			return string(b), true
		}
		if b, ok := data.(int32); ok {
			return string(b), true
		}
		if b, ok := data.(int64); ok {
			return string(b), true
		}
		if b, ok := data.(uint); ok {
			return string(b), true
		}
		if b, ok := data.(uint8); ok {
			return string(b), true
		}
		if b, ok := data.(uint16); ok {
			return string(b), true
		}
		if b, ok := data.(uint32); ok {
			return string(b), true
		}
		if b, ok := data.(uint64); ok {
			return string(b), true
		}
		if b, ok := data.(float32); ok {
			return strconv.Ftoa32(b, 'g', -1), true
		}
		if b, ok := data.(float64); ok {
			return strconv.Ftoa64(b, 'g', -1), true
		}
		return "", false
	case BINARY:
		if b, ok := data.([]byte); ok {
			return b, true
		}
		if b, ok := data.(Enumer); ok {
			if i1, ok := data.(int); ok {
				return []byte(string(i1)), true
			}
			return []byte(b.String()), true
		}
		if b, ok := data.(Stringer); ok {
			return []byte(b.String()), true
		}
		if b, ok := data.(string); ok {
			return []byte(b), true
		}
		if b, ok := data.(int); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(byte); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(bool); ok {
			if b {
				return []byte("true"), true
			}
			return []byte("false"), true
		}
		if b, ok := data.(int8); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(int16); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(int32); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(int64); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(uint); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(uint8); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(uint16); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(uint32); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(uint64); ok {
			return []byte(string(b)), true
		}
		if b, ok := data.(float32); ok {
			return []byte(strconv.Ftoa32(b, 'g', -1)), true
		}
		if b, ok := data.(float64); ok {
			return []byte(strconv.Ftoa64(b, 'g', -1)), true
		}
		return "", false
	case STRUCT:
		if b, ok := data.(TStruct); ok {
			return b, true
		}
		return NewTStructEmpty(""), true
	case MAP:
		if b, ok := data.(TMap); ok {
			return b, true
		}
		return NewTMapDefault(), false
	case LIST:
		if b, ok := data.(TList); ok {
			return b, true
		}
		return NewTListDefault(), false
	case SET:
		if b, ok := data.(TSet); ok {
			return b, true
		}
		return NewTSetDefault(), false
	default:
		panic("Invalid thrift type to coerce")
	}
	return nil, false
}
Exemplo n.º 13
0
// Fmt_f32 formats a float32 in the form -1.23.
func (f *Fmt) Fmt_f32(v float32) *Fmt {
	return fmtString(f, strconv.Ftoa32(v, 'f', doPrec(f, 6)))
}
Exemplo n.º 14
0
// output gives out the PDF representation of v.
func output(v interface{}) []byte {
	// Check for nil
	if v == nil {
		return []byte("null")
	}

	// Check simple types with simple type assertion
	switch t := v.(type) {
	case outputter:
		return t.output()
	case bool:
		if t {
			return []byte("true")
		} else {
			return []byte("false")
		}
	case int:
		return []byte(strconv.Itoa(t))
	case float32:
		return []byte(strconv.Ftoa32(t, 'f', -1))
	case float64:
		// TODO 2.3 prints 2.299999952316284. Is it OK with PDF?
		return []byte(strconv.Ftoa64(t, 'f', -1))
	case string:
		// TODO non-ASCII characters?
		// TODO escapes, \n, \t, etc. (p. 54)
		// TODO break long lines (p. 54)
		// TODO what about hexadecimal strings? (p. 56)
		return []byte("(" + t + ")")
	case name:
		// TODO escape non-regular characters using # (p. 57)
		// TODO check length limit (p. 57)
		return []byte("/" + string(t))
	case []byte:
		return outputStream(t)
	case *bytes.Buffer:
		return outputStream(t.Bytes())
	case reflect.Value:
		return output(t.Interface())
	}

	switch r := reflect.ValueOf(v); r.Kind() {
	case reflect.Invalid:
		panic("unsupported type passed to output")
	case reflect.Array, reflect.Slice:
		buf := bytes.NewBufferString("[ ")

		for i := 0; i < r.Len(); i++ {
			buf.Write(output(r.Index(i)))
			buf.WriteString(" ")
		}

		buf.WriteString("]")

		return buf.Bytes()
	case reflect.Map:
		buf := bytes.NewBufferString("<<\n")

		for _, k := range r.MapKeys() {
			if k.Kind() != reflect.String {
				panic("key of map passed to output is not string")
			}
			buf.Write(output(name(k.String())))
			buf.WriteString(" ")
			buf.Write(output(r.MapIndex(k)))
			buf.WriteString("\n")
		}

		buf.WriteString(">>")

		return buf.Bytes()
	}

	return []byte("null")
}
Exemplo n.º 15
0
func (e *encodeState) reflectValue(v reflect.Value) {
	if v == nil {
		e.WriteString("null")
		return
	}

	if j, ok := v.Interface().(Marshaler); ok {
		b, err := j.MarshalJSON()
		if err == nil {
			// copy JSON into buffer, checking validity.
			err = Compact(&e.Buffer, b)
		}
		if err != nil {
			e.error(&MarshalerError{v.Type(), err})
		}
		return
	}

	switch v := v.(type) {
	case *reflect.BoolValue:
		x := v.Get()
		if x {
			e.WriteString("true")
		} else {
			e.WriteString("false")
		}

	case *reflect.IntValue:
		e.WriteString(strconv.Itoa(v.Get()))
	case *reflect.Int8Value:
		e.WriteString(strconv.Itoa(int(v.Get())))
	case *reflect.Int16Value:
		e.WriteString(strconv.Itoa(int(v.Get())))
	case *reflect.Int32Value:
		e.WriteString(strconv.Itoa(int(v.Get())))
	case *reflect.Int64Value:
		e.WriteString(strconv.Itoa64(v.Get()))

	case *reflect.UintValue:
		e.WriteString(strconv.Uitoa(v.Get()))
	case *reflect.Uint8Value:
		e.WriteString(strconv.Uitoa(uint(v.Get())))
	case *reflect.Uint16Value:
		e.WriteString(strconv.Uitoa(uint(v.Get())))
	case *reflect.Uint32Value:
		e.WriteString(strconv.Uitoa(uint(v.Get())))
	case *reflect.Uint64Value:
		e.WriteString(strconv.Uitoa64(v.Get()))
	case *reflect.UintptrValue:
		e.WriteString(strconv.Uitoa64(uint64(v.Get())))

	case *reflect.FloatValue:
		e.WriteString(strconv.Ftoa(v.Get(), 'g', -1))
	case *reflect.Float32Value:
		e.WriteString(strconv.Ftoa32(v.Get(), 'g', -1))
	case *reflect.Float64Value:
		e.WriteString(strconv.Ftoa64(v.Get(), 'g', -1))

	case *reflect.StringValue:
		e.string(v.Get())

	case *reflect.StructValue:
		e.WriteByte('{')
		t := v.Type().(*reflect.StructType)
		n := v.NumField()
		for i := 0; i < n; i++ {
			if i > 0 {
				e.WriteByte(',')
			}
			f := t.Field(i)
			if f.Tag != "" {
				e.string(f.Tag)
			} else {
				e.string(f.Name)
			}
			e.WriteByte(':')
			e.reflectValue(v.Field(i))
		}
		e.WriteByte('}')

	case *reflect.MapValue:
		if _, ok := v.Type().(*reflect.MapType).Key().(*reflect.StringType); !ok {
			e.error(&UnsupportedTypeError{v.Type()})
		}
		if v.IsNil() {
			e.WriteString("null")
			break
		}
		e.WriteByte('{')
		var sv stringValues = v.Keys()
		sort.Sort(sv)
		for i, k := range sv {
			if i > 0 {
				e.WriteByte(',')
			}
			e.string(k.(*reflect.StringValue).Get())
			e.WriteByte(':')
			e.reflectValue(v.Elem(k))
		}
		e.WriteByte('}')

	case reflect.ArrayOrSliceValue:
		e.WriteByte('[')
		n := v.Len()
		for i := 0; i < n; i++ {
			if i > 0 {
				e.WriteByte(',')
			}
			e.reflectValue(v.Elem(i))
		}
		e.WriteByte(']')

	case interfaceOrPtrValue:
		if v.IsNil() {
			e.WriteString("null")
			return
		}
		e.reflectValue(v.Elem())

	default:
		e.error(&UnsupportedTypeError{v.Type()})
	}
	return
}
Exemplo n.º 16
0
// fmt_f32 formats a float32 in the form -1.23.
func (f *fmt) fmt_f32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'f', doPrec(f, 6))) }
Exemplo n.º 17
0
// fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1))) }
Exemplo n.º 18
0
// fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
Exemplo n.º 19
0
/*
 * Process image
 */
func process(input *ProcessInput, conn *websocket.Conn, stopCh chan bool) os.Error {
	log.Println(input)

	// open input file
	inputFile, err := os.OpenFile(UploadDir+input.Image, os.O_RDONLY, 0666)
	if err != nil {
		return err
	}
	defer inputFile.Close()

	// create output file
	outputFile, err := os.OpenFile(ResultDir+input.Image, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		return err
	}
	defer outputFile.Close()

	// decode png image
	inputImage, _, err := image.Decode(inputFile)
	if err != nil {
		return err
	}
	rgbaInput := rgba(inputImage)

	sivqParams := SIVQParameters{
		GammaAdjustment: float(input.GammaAdjust),
		AverageBias:     float(input.AverageBias),
		RotationStride:  float(input.RotationStride),
		MatchingStride:  input.MatchStride,
		MatchingOffset:  input.MatchingOffset,
		Threshold:       float(input.Threshold),
		ProgressCallback: func(p float) {
			conn.Write([]byte(strconv.Ftoa32(float32(p), 'f', 4)))
		},
		StopCh: stopCh}

	// get vector
	var ringVector *RingVector
	if len(input.VectorName) == 0 {
		vectorParams := RingVectorParameters{
			Radius:    input.VectorRadius,
			Count:     input.VectorRings,
			RadiusInc: input.RingSizeInc}

		ringVector = NewRingVector(vectorParams)
		ringVector.LoadData(rgbaInput, input.VecX, input.VecY)
	} else {
		// load vector from file
		vectorFile, err := os.OpenFile(VectorDir+input.VectorName, os.O_RDONLY, 0666)
		if err != nil {
			return err
		}
		defer vectorFile.Close()

		decoder := gob.NewDecoder(vectorFile)
		err = decoder.Decode(&ringVector)
		if err != nil {
			return err
		}
	}

	// do the magic
	outputImage := SIVQ(sivqParams, rgbaInput, ringVector)

	if err = png.Encode(outputFile, outputImage); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 20
0
// valueToString returns a textual representation of the reflection value val.
// For debugging only.
func valueToString(val Value) string {
	var str string
	if val == nil {
		return "<nil>"
	}
	typ := val.Type()
	switch val := val.(type) {
	case *IntValue:
		return strconv.Uitoa64(uint64(val.Get()))
	case *Int8Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Int16Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Int32Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Int64Value:
		return strconv.Itoa64(int64(val.Get()))
	case *UintValue:
		return strconv.Itoa64(int64(val.Get()))
	case *Uint8Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Uint16Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Uint32Value:
		return strconv.Itoa64(int64(val.Get()))
	case *Uint64Value:
		return strconv.Uitoa64(uint64(val.Get()))
	case *FloatValue:
		if strconv.FloatSize == 32 {
			return strconv.Ftoa32(float32(val.Get()), 'g', -1)
		} else {
			return strconv.Ftoa64(float64(val.Get()), 'g', -1)
		}
	case *Float32Value:
		return strconv.Ftoa32(val.Get(), 'g', -1)
	case *Float64Value:
		return strconv.Ftoa64(val.Get(), 'g', -1)
	case *StringValue:
		return val.Get()
	case *BoolValue:
		if val.Get() {
			return "true"
		} else {
			return "false"
		}
	case *PtrValue:
		v := val
		str = typ.String() + "("
		if v.IsNil() {
			str += "0"
		} else {
			str += "&" + valueToString(v.Elem())
		}
		str += ")"
		return str
	case ArrayOrSliceValue:
		v := val
		str += typ.String()
		str += "{"
		for i := 0; i < v.Len(); i++ {
			if i > 0 {
				str += ", "
			}
			str += valueToString(v.Elem(i))
		}
		str += "}"
		return str
	case *MapValue:
		t := typ.(*MapType)
		str = t.String()
		str += "{"
		str += "<can't iterate on maps>"
		str += "}"
		return str
	case *ChanValue:
		str = typ.String()
		return str
	case *StructValue:
		t := typ.(*StructType)
		v := val
		str += t.String()
		str += "{"
		for i, n := 0, v.NumField(); i < n; i++ {
			if i > 0 {
				str += ", "
			}
			str += valueToString(v.Field(i))
		}
		str += "}"
		return str
	case *InterfaceValue:
		return typ.String() + "(" + valueToString(val.Elem()) + ")"
	case *FuncValue:
		v := val
		return typ.String() + "(" + strconv.Itoa64(int64(v.Get())) + ")"
	default:
		panicln("valueToString: can't print type ", typ.String())
	}
	return "valueToString: can't happen"
}