PushFString pushes onto the stack a formatted string and returns that string. It is similar to fmt.Sprintf, but has some differences: the conversion specifiers are quite restricted. There are no flags, widths, or precisions. The conversion specifiers can only be %% (inserts a % in the string), %s
(format string, args ...interface{})
| 897 | // |
| 898 | // http://www.lua.org/manual/5.2/manual.html#lua_pushfstring |
| 899 | func (l *State) PushFString(format string, args ...interface{}) string { |
| 900 | n, i := 0, 0 |
| 901 | for { |
| 902 | e := strings.IndexRune(format, '%') |
| 903 | if e < 0 { |
| 904 | break |
| 905 | } |
| 906 | l.checkStack(2) // format + item |
| 907 | l.push(format[:e]) |
| 908 | switch format[e+1] { |
| 909 | case 's': |
| 910 | if args[i] == nil { |
| 911 | l.push("(null)") |
| 912 | } else { |
| 913 | l.push(args[i].(string)) |
| 914 | } |
| 915 | i++ |
| 916 | case 'c': |
| 917 | l.push(string(args[i].(rune))) |
| 918 | i++ |
| 919 | case 'd': |
| 920 | l.push(float64(args[i].(int))) |
| 921 | i++ |
| 922 | case 'f': |
| 923 | l.push(args[i].(float64)) |
| 924 | i++ |
| 925 | case 'p': |
| 926 | l.push(fmt.Sprintf("%p", args[i])) |
| 927 | i++ |
| 928 | case '%': |
| 929 | l.push("%") |
| 930 | default: |
| 931 | l.runtimeError("invalid option " + format[e:e+2] + " to 'lua_pushfstring'") |
| 932 | } |
| 933 | n += 2 |
| 934 | format = format[e+2:] |
| 935 | } |
| 936 | l.checkStack(1) |
| 937 | l.push(format) |
| 938 | if n > 0 { |
| 939 | l.concat(n + 1) |
| 940 | } |
| 941 | return l.stack[l.top-1].(string) |
| 942 | } |
| 943 | |
| 944 | // PushGoClosure pushes a new Go closure onto the stack. |
| 945 | // |