本文整理汇总了Golang中code/google/com/p/go/tools/go/exact.StringVal函数的典型用法代码示例。如果您正苦于以下问题:Golang StringVal函数的具体用法?Golang StringVal怎么用?Golang StringVal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了StringVal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: value
func (p *exporter) value(x exact.Value) {
if trace {
p.tracef("value { ")
defer p.tracef("} ")
}
switch kind := x.Kind(); kind {
case exact.Bool:
tag := falseTag
if exact.BoolVal(x) {
tag = trueTag
}
p.int(tag)
case exact.Int:
if i, ok := exact.Int64Val(x); ok {
p.int(int64Tag)
p.int64(i)
return
}
p.int(floatTag)
p.float(x)
case exact.Float:
p.int(fractionTag)
p.fraction(x)
case exact.Complex:
p.int(complexTag)
p.fraction(exact.Real(x))
p.fraction(exact.Imag(x))
case exact.String:
p.int(stringTag)
p.string(exact.StringVal(x))
default:
panic(fmt.Sprintf("unexpected value kind %d", kind))
}
}
示例2: checkPrintf
// checkPrintf checks a call to a formatted print routine such as Printf.
// call.Args[formatIndex] is (well, should be) the format argument.
func (f *File) checkPrintf(call *ast.CallExpr, name string, formatIndex int) {
if formatIndex >= len(call.Args) {
f.Bad(call.Pos(), "too few arguments in call to", name)
return
}
lit := f.pkg.types[call.Args[formatIndex]].Value
if lit == nil {
if *verbose {
f.Warn(call.Pos(), "can't check non-constant format in call to", name)
}
return
}
if lit.Kind() != exact.String {
f.Badf(call.Pos(), "constant %v not a string in call to %s", lit, name)
return
}
format := exact.StringVal(lit)
firstArg := formatIndex + 1 // Arguments are immediately after format string.
if !strings.Contains(format, "%") {
if len(call.Args) > firstArg {
f.Badf(call.Pos(), "no formatting directive in %s call", name)
}
return
}
// Hard part: check formats against args.
argNum := firstArg
indexed := false
for i, w := 0, 0; i < len(format); i += w {
w = 1
if format[i] == '%' {
state := f.parsePrintfVerb(call, name, format[i:], firstArg, argNum)
if state == nil {
return
}
w = len(state.format)
if state.indexed {
indexed = true
}
if !f.okPrintfArg(call, state) { // One error per format is enough.
return
}
if len(state.argNums) > 0 {
// Continue with the next sequential argument.
argNum = state.argNums[len(state.argNums)-1] + 1
}
}
}
// Dotdotdot is hard.
if call.Ellipsis.IsValid() && argNum >= len(call.Args)-1 {
return
}
// If the arguments were direct indexed, we assume the programmer knows what's up.
// Otherwise, there should be no leftover arguments.
if !indexed && argNum != len(call.Args) {
expect := argNum - firstArg
numArgs := len(call.Args) - firstArg
f.Badf(call.Pos(), "wrong number of args for format in %s call: %d needed but %d args", name, expect, numArgs)
}
}
示例3: CheckAll
//ranges through config file and checks all expressions.
// prints result messages to stdout
func (c *checker) CheckAll() ([]CheckResult, error) {
result := []CheckResult{}
cnf, err := conf.ReadConfigFile(c.configFile)
if err != nil {
return nil, err
}
for _, section := range cnf.GetSections() {
if section == "default" {
continue
}
expr, _ := cnf.GetString(section, "expr")
_, r, err := types.Eval(expr, c.pkg, c.sc)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
cr := &CheckResult{
Name: section,
}
var m string
if exact.BoolVal(r) {
m, err = cnf.GetString(section, "true")
if err != nil {
continue
}
} else {
m, err = cnf.GetString(section, "false")
if err != nil {
continue
}
}
val, err := cnf.GetString(section, "val")
if err == nil {
t, v, err := types.Eval(val, c.pkg, c.sc)
if err == nil {
if types.Identical(t, types.Typ[types.UntypedFloat]) || types.Identical(t, types.Typ[types.Float64]) {
x, _ := exact.Float64Val(v)
cr.Value = x
}
}
}
owner, err := cnf.GetString(section, "owner")
if err == nil {
cr.Owner = owner
} else {
cr.Owner = "unknown"
}
_, msg, err := types.Eval(m, c.pkg, c.sc)
if err != nil {
cr.Message = m
} else {
cr.Message = exact.StringVal(msg)
}
result = append(result, *cr)
}
return result, nil
}
示例4: constValue
// constValue returns the value of the constant with the
// dynamic type tag appropriate for c.Type().
func constValue(c *ssa.Const) value {
if c.IsNil() {
return zero(c.Type()) // typed nil
}
if t, ok := c.Type().Underlying().(*types.Basic); ok {
// TODO(adonovan): eliminate untyped constants from SSA form.
switch t.Kind() {
case types.Bool, types.UntypedBool:
return exact.BoolVal(c.Value)
case types.Int, types.UntypedInt:
// Assume sizeof(int) is same on host and target.
return int(c.Int64())
case types.Int8:
return int8(c.Int64())
case types.Int16:
return int16(c.Int64())
case types.Int32, types.UntypedRune:
return int32(c.Int64())
case types.Int64:
return c.Int64()
case types.Uint:
// Assume sizeof(uint) is same on host and target.
return uint(c.Uint64())
case types.Uint8:
return uint8(c.Uint64())
case types.Uint16:
return uint16(c.Uint64())
case types.Uint32:
return uint32(c.Uint64())
case types.Uint64:
return c.Uint64()
case types.Uintptr:
// Assume sizeof(uintptr) is same on host and target.
return uintptr(c.Uint64())
case types.Float32:
return float32(c.Float64())
case types.Float64, types.UntypedFloat:
return c.Float64()
case types.Complex64:
return complex64(c.Complex128())
case types.Complex128, types.UntypedComplex:
return c.Complex128()
case types.String, types.UntypedString:
if c.Value.Kind() == exact.String {
return exact.StringVal(c.Value)
}
return string(rune(c.Int64()))
}
}
panic(fmt.Sprintf("constValue: %s", c))
}
示例5: Name
func (c *Const) Name() string {
var s string
if c.Value.Kind() == exact.String {
s = exact.StringVal(c.Value)
const max = 20
if len(s) > max {
s = s[:max-3] + "..." // abbreviate
}
s = strconv.Quote(s)
} else {
s = c.Value.String()
}
return s + ":" + c.typ.String()
}
示例6: Name
func (l *Literal) Name() string {
var s string
if l.Value.Kind() == exact.String {
s = exact.StringVal(l.Value)
const maxLit = 20
if len(s) > maxLit {
s = s[:maxLit-3] + "..." // abbreviate
}
s = strconv.Quote(s)
} else {
s = l.Value.String()
}
return s + ":" + l.Type_.String()
}
示例7: valstring
func (c *Const) valstring() string {
if c.Value == nil {
return "nil"
} else if c.Value.Kind() == exact.String {
s := exact.StringVal(c.Value)
const max = 20
if len(s) > max {
s = s[:max-3] + "..." // abbreviate
}
return strconv.Quote(s)
} else {
return c.Value.String()
}
}
示例8: identifierConstant
func (c *funcContext) identifierConstant(expr ast.Expr) (string, bool) {
val := c.p.info.Types[expr].Value
if val == nil {
return "", false
}
s := exact.StringVal(val)
if len(s) == 0 {
return "", false
}
for i, c := range s {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (i > 0 && c >= '0' && c <= '9') || c == '_' || c == '$') {
return "", false
}
}
return s, true
}
示例9: RelString
func (c *Const) RelString(from *types.Package) string {
var s string
if c.Value == nil {
s = "nil"
} else if c.Value.Kind() == exact.String {
s = exact.StringVal(c.Value)
const max = 20
// TODO(adonovan): don't cut a rune in half.
if len(s) > max {
s = s[:max-3] + "..." // abbreviate
}
s = strconv.Quote(s)
} else {
s = c.Value.String()
}
return s + ":" + relType(c.Type(), from)
}
示例10:
func ext۰reflect۰rtype۰MethodByName(a *analysis, cgn *cgnode) {
// If we have access to the callsite,
// and the argument is a string constant,
// return only that method.
var name string
if site := cgn.callersite; site != nil {
if c, ok := site.instr.Common().Args[0].(*ssa.Const); ok {
name = exact.StringVal(c.Value)
}
}
a.addConstraint(&rtypeMethodByNameConstraint{
cgn: cgn,
name: name,
t: a.funcParams(cgn.obj),
result: a.funcResults(cgn.obj),
})
}
示例11: analyzeNapParam
func analyzeNapParam(pkg *types.Package, info *types.Info, c *ast.CallExpr) (string, bool) {
sel, ok := c.Fun.(*ast.SelectorExpr)
if !ok {
return "", false
}
xType := info.Types[sel.X]
if xType.Type == nil {
return "", false
}
if xType.Type.String() != "github.com/Logiraptor/nap.Request" {
return "", false
}
// printNode(c)
nameTV := info.Types[c.Args[0]]
return exact.StringVal(nameTV.Value), true
}
示例12: Test
func Test(t *testing.T) {
switch runtime.GOOS {
case "windows":
t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS)
}
conf := loader.Config{
Fset: token.NewFileSet(),
ParserMode: parser.ParseComments,
SourceImports: true,
}
// Each entry is a single-file package.
// (Multi-file packages aren't interesting for this test.)
// Order matters: each non-template package is processed using
// the preceding template package.
for _, filename := range []string{
"testdata/A.template",
"testdata/A1.go",
"testdata/A2.go",
"testdata/B.template",
"testdata/B1.go",
"testdata/C.template",
"testdata/C1.go",
"testdata/D.template",
"testdata/D1.go",
"testdata/E.template",
"testdata/E1.go",
"testdata/bad_type.template",
"testdata/no_before.template",
"testdata/no_after_return.template",
"testdata/type_mismatch.template",
"testdata/expr_type_mismatch.template",
} {
pkgname := strings.TrimSuffix(filepath.Base(filename), ".go")
if err := conf.CreateFromFilenames(pkgname, filename); err != nil {
t.Fatal(err)
}
}
iprog, err := conf.Load()
if err != nil {
t.Fatal(err)
}
var xform *eg.Transformer
for _, info := range iprog.Created {
file := info.Files[0]
filename := iprog.Fset.File(file.Pos()).Name() // foo.go
if strings.HasSuffix(filename, "template") {
// a new template
shouldFail, _ := info.Pkg.Scope().Lookup("shouldFail").(*types.Const)
xform, err = eg.NewTransformer(iprog.Fset, info, *verboseFlag)
if err != nil {
if shouldFail == nil {
t.Errorf("NewTransformer(%s): %s", filename, err)
} else if want := exact.StringVal(shouldFail.Val()); !strings.Contains(err.Error(), want) {
t.Errorf("NewTransformer(%s): got error %q, want error %q", filename, err, want)
}
} else if shouldFail != nil {
t.Errorf("NewTransformer(%s) succeeded unexpectedly; want error %q",
filename, shouldFail.Val())
}
continue
}
if xform == nil {
t.Errorf("%s: no previous template", filename)
continue
}
// apply previous template to this package
n := xform.Transform(&info.Info, info.Pkg, file)
if n == 0 {
t.Errorf("%s: no matches", filename)
continue
}
got := filename + "t" // foo.got
golden := filename + "lden" // foo.golden
// Write actual output to foo.got.
if err := eg.WriteAST(iprog.Fset, got, file); err != nil {
t.Error(err)
}
defer os.Remove(got)
// Compare foo.got with foo.golden.
var cmd *exec.Cmd
switch runtime.GOOS {
case "plan9":
cmd = exec.Command("/bin/diff", "-c", golden, got)
default:
cmd = exec.Command("/usr/bin/diff", "-u", golden, got)
}
//.........这里部分代码省略.........
示例13: translateExpr
func (c *PkgContext) translateExpr(expr ast.Expr) string {
exprType := c.info.Types[expr]
if value, valueFound := c.info.Values[expr]; valueFound {
basic := types.Typ[types.String]
if value.Kind() != exact.String { // workaround for bug in go/types
basic = exprType.Underlying().(*types.Basic)
}
switch {
case basic.Info()&types.IsBoolean != 0:
return strconv.FormatBool(exact.BoolVal(value))
case basic.Info()&types.IsInteger != 0:
if is64Bit(basic) {
d, _ := exact.Uint64Val(value)
return fmt.Sprintf("new %s(%d, %d)", c.typeName(exprType), d>>32, d&(1<<32-1))
}
d, _ := exact.Int64Val(value)
return strconv.FormatInt(d, 10)
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(value)
return strconv.FormatFloat(f, 'g', -1, 64)
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return fmt.Sprintf("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case basic.Info()&types.IsString != 0:
buffer := bytes.NewBuffer(nil)
for _, r := range []byte(exact.StringVal(value)) {
switch r {
case '\b':
buffer.WriteString(`\b`)
case '\f':
buffer.WriteString(`\f`)
case '\n':
buffer.WriteString(`\n`)
case '\r':
buffer.WriteString(`\r`)
case '\t':
buffer.WriteString(`\t`)
case '\v':
buffer.WriteString(`\v`)
case '"':
buffer.WriteString(`\"`)
case '\\':
buffer.WriteString(`\\`)
default:
if r < 0x20 || r > 0x7E {
fmt.Fprintf(buffer, `\x%02X`, r)
continue
}
buffer.WriteByte(r)
}
}
return `"` + buffer.String() + `"`
default:
panic("Unhandled constant type: " + basic.String())
}
}
switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
}
collectIndexedElements := func(elementType types.Type) []string {
elements := make([]string, 0)
i := 0
zero := c.zeroValue(elementType)
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, _ := exact.Int64Val(c.info.Values[kve.Key])
i = int(key)
element = kve.Value
}
for len(elements) <= i {
elements = append(elements, zero)
}
elements[i] = c.translateExprToType(element, elementType)
i++
}
return elements
}
switch t := exprType.Underlying().(type) {
case *types.Array:
elements := collectIndexedElements(t.Elem())
if len(elements) != 0 {
zero := c.zeroValue(t.Elem())
for len(elements) < int(t.Len()) {
elements = append(elements, zero)
}
return createListComposite(t.Elem(), elements)
}
return fmt.Sprintf("Go$makeArray(%s, %d, function() { return %s; })", toArrayType(t.Elem()), t.Len(), c.zeroValue(t.Elem()))
case *types.Slice:
return fmt.Sprintf("new %s(%s)", c.typeName(exprType), createListComposite(t.Elem(), collectIndexedElements(t.Elem())))
case *types.Map:
//.........这里部分代码省略.........
示例14: Write
func Write(pkg *types.Package, out io.Writer, sizes types.Sizes) {
fmt.Fprintf(out, "package %s\n", pkg.Name())
e := &exporter{pkg: pkg, imports: make(map[*types.Package]bool), out: out}
for _, imp := range pkg.Imports() {
e.addImport(imp)
}
for _, name := range pkg.Scope().Names() {
obj := pkg.Scope().Lookup(name)
_, isTypeName := obj.(*types.TypeName)
if obj.Exported() || isTypeName {
e.toExport = append(e.toExport, obj)
}
}
for i := 0; i < len(e.toExport); i++ {
switch o := e.toExport[i].(type) {
case *types.TypeName:
fmt.Fprintf(out, "type %s %s\n", e.makeName(o), e.makeType(o.Type().Underlying()))
if _, isInterface := o.Type().Underlying().(*types.Interface); !isInterface {
writeMethods := func(t types.Type) {
methods := types.NewMethodSet(t)
for i := 0; i < methods.Len(); i++ {
m := methods.At(i)
if len(m.Index()) > 1 {
continue // method of embedded field
}
out.Write([]byte("func (? " + e.makeType(m.Recv()) + ") " + e.makeName(m.Obj()) + e.makeSignature(m.Type()) + "\n"))
}
}
writeMethods(o.Type())
writeMethods(types.NewPointer(o.Type()))
}
case *types.Func:
out.Write([]byte("func " + e.makeName(o) + e.makeSignature(o.Type()) + "\n"))
case *types.Const:
optType := ""
basic, isBasic := o.Type().(*types.Basic)
if !isBasic || basic.Info()&types.IsUntyped == 0 {
optType = " " + e.makeType(o.Type())
}
basic = o.Type().Underlying().(*types.Basic)
var val string
switch {
case basic.Info()&types.IsBoolean != 0:
val = strconv.FormatBool(exact.BoolVal(o.Val()))
case basic.Info()&types.IsInteger != 0:
if basic.Kind() == types.Uint64 {
d, _ := exact.Uint64Val(o.Val())
val = fmt.Sprintf("%#x", d)
break
}
d, _ := exact.Int64Val(o.Val())
if basic.Kind() == types.UntypedRune {
switch {
case d < 0 || d > unicode.MaxRune:
val = fmt.Sprintf("('\\x00' + %d)", d)
case d > 0xffff:
val = fmt.Sprintf("'\\U%08x'", d)
default:
val = fmt.Sprintf("'\\u%04x'", d)
}
break
}
val = fmt.Sprintf("%#x", d)
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(o.Val())
val = strconv.FormatFloat(f, 'b', -1, 64)
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(o.Val()))
i, _ := exact.Float64Val(exact.Imag(o.Val()))
val = fmt.Sprintf("(%s+%si)", strconv.FormatFloat(r, 'b', -1, 64), strconv.FormatFloat(i, 'b', -1, 64))
case basic.Info()&types.IsString != 0:
val = fmt.Sprintf("%#v", exact.StringVal(o.Val()))
default:
panic("Unhandled constant type: " + basic.String())
}
out.Write([]byte("const " + e.makeName(o) + optType + " = " + val + "\n"))
case *types.Var:
out.Write([]byte("var " + e.makeName(o) + " " + e.makeType(o.Type()) + "\n"))
default:
panic(fmt.Sprintf("Unhandled object: %T\n", o))
}
}
fmt.Fprintf(out, "$$\n")
}
示例15: translateExpr
func (c *funcContext) translateExpr(expr ast.Expr) *expression {
exprType := c.p.info.Types[expr].Type
if value := c.p.info.Types[expr].Value; value != nil {
basic := types.Typ[types.String]
if value.Kind() != exact.String { // workaround for bug in go/types
basic = exprType.Underlying().(*types.Basic)
}
switch {
case basic.Info()&types.IsBoolean != 0:
return c.formatExpr("%s", strconv.FormatBool(exact.BoolVal(value)))
case basic.Info()&types.IsInteger != 0:
if is64Bit(basic) {
d, _ := exact.Uint64Val(value)
if basic.Kind() == types.Int64 {
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatInt(int64(d)>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatUint(d>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
d, _ := exact.Int64Val(value)
return c.formatExpr("%s", strconv.FormatInt(d, 10))
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(value)
return c.formatExpr("%s", strconv.FormatFloat(f, 'g', -1, 64))
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case basic.Info()&types.IsString != 0:
return c.formatExpr("%s", encodeString(exact.StringVal(value)))
default:
panic("Unhandled constant type: " + basic.String())
}
}
switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
}
collectIndexedElements := func(elementType types.Type) []string {
elements := make([]string, 0)
i := 0
zero := c.zeroValue(elementType)
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, _ := exact.Int64Val(c.p.info.Types[kve.Key].Value)
i = int(key)
element = kve.Value
}
for len(elements) <= i {
elements = append(elements, zero)
}
elements[i] = c.translateImplicitConversion(element, elementType).String()
i++
}
return elements
}
switch t := exprType.Underlying().(type) {
case *types.Array:
elements := collectIndexedElements(t.Elem())
if len(elements) != 0 {
zero := c.zeroValue(t.Elem())
for len(elements) < int(t.Len()) {
elements = append(elements, zero)
}
return c.formatExpr(`go$toNativeArray("%s", [%s])`, typeKind(t.Elem()), strings.Join(elements, ", "))
}
return c.formatExpr(`go$makeNativeArray("%s", %d, function() { return %s; })`, typeKind(t.Elem()), int(t.Len()), c.zeroValue(t.Elem()))
case *types.Slice:
return c.formatExpr("new %s([%s])", c.typeName(exprType), strings.Join(collectIndexedElements(t.Elem()), ", "))
case *types.Map:
mapVar := c.newVariable("_map")
keyVar := c.newVariable("_key")
assignments := ""
for _, element := range e.Elts {
kve := element.(*ast.KeyValueExpr)
assignments += c.formatExpr(`%s = %s, %s[%s] = { k: %s, v: %s }, `, keyVar, c.translateImplicitConversion(kve.Key, t.Key()), mapVar, c.makeKey(c.newIdent(keyVar, t.Key()), t.Key()), keyVar, c.translateImplicitConversion(kve.Value, t.Elem())).String()
}
return c.formatExpr("(%s = new Go$Map(), %s%s)", mapVar, assignments, mapVar)
case *types.Struct:
elements := make([]string, t.NumFields())
isKeyValue := true
if len(e.Elts) != 0 {
_, isKeyValue = e.Elts[0].(*ast.KeyValueExpr)
}
if !isKeyValue {
for i, element := range e.Elts {
elements[i] = c.translateImplicitConversion(element, t.Field(i).Type()).String()
}
}
if isKeyValue {
for i := range elements {
elements[i] = c.zeroValue(t.Field(i).Type())
}
for _, element := range e.Elts {
//.........这里部分代码省略.........