提交 50419b61 编写于 作者: A aarzilli 提交者: Derek Parker

proc: implement conversion of integers to string

Go allows converting a single integer value to string, resulting in a
string containing a single unicode rune with the same code as the value
of the integer.
Allow the same conversion to happen.

Fixes #1322
上级 a2eb983e
......@@ -113,9 +113,15 @@ func (scope *EvalScope) evalToplevelTypeCast(t ast.Expr, cfg LoadConfig) (*Varia
return v, nil
case "string":
if argv.Kind != reflect.Slice {
return nil, nil
}
switch argv.Kind {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
b, _ := constant.Int64Val(argv.Value)
s := string(b)
v.Value = constant.MakeString(s)
v.Len = int64(len(s))
return v, nil
case reflect.Slice:
switch elemType := argv.RealType.(*godwarf.SliceType).ElemType.(type) {
case *godwarf.UintType:
if elemType.Name != "uint8" && elemType.Name != "byte" {
......@@ -144,6 +150,10 @@ func (scope *EvalScope) evalToplevelTypeCast(t ast.Expr, cfg LoadConfig) (*Varia
}
v.Len = int64(len(constant.StringVal(v.Value)))
return v, nil
default:
return nil, nil
}
}
return nil, nil
......
......@@ -748,6 +748,8 @@ func TestEvalExpression(t *testing.T) {
{"uint8(i5)", false, "253", "253", "uint8", nil},
{"int8(i5)", false, "-3", "-3", "int8", nil},
{"int8(i6)", false, "12", "12", "int8", nil},
{"string(byteslice[0])", false, `"t"`, `"t"`, "string", nil},
{"string(runeslice[0])", false, `"t"`, `"t"`, "string", nil},
// misc
{"i1", true, "1", "1", "int", nil},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册