MCPcopy Index your code
hub / github.com/Shopify/go-lua / formatHelper

Function formatHelper

string.go:71–142  ·  view source on GitHub ↗
(l *State, fs string, argCount int)

Source from the content-addressed store, hash-verified

69}
70
71func formatHelper(l *State, fs string, argCount int) string {
72 var b bytes.Buffer
73 for i, arg := 0, 1; i < len(fs); i++ {
74 if fs[i] != '%' {
75 b.WriteByte(fs[i])
76 } else if i++; fs[i] == '%' {
77 b.WriteByte(fs[i])
78 } else {
79 if arg++; arg > argCount {
80 ArgumentError(l, arg, "no value")
81 }
82 f := scanFormat(l, fs[i:])
83 switch i += len(f) - 2; fs[i] {
84 case 'c':
85 // Ensure each character is represented by a single byte, while preserving format modifiers.
86 c := CheckInteger(l, arg)
87 fmt.Fprintf(&b, f, 'x')
88 buf := b.Bytes()
89 buf[len(buf)-1] = byte(c)
90 case 'i': // The fmt package doesn't support %i.
91 f = f[:len(f)-1] + "d"
92 fallthrough
93 case 'd':
94 n := CheckNumber(l, arg)
95 ArgumentCheck(l, math.Floor(n) == n && -math.Pow(2, 63) <= n && n < math.Pow(2, 63), arg, "number has no integer representation")
96 ni := int(n)
97 fmt.Fprintf(&b, f, ni)
98 case 'u': // The fmt package doesn't support %u.
99 f = f[:len(f)-1] + "d"
100 n := CheckNumber(l, arg)
101 ArgumentCheck(l, math.Floor(n) == n && 0.0 <= n && n < math.Pow(2, 64), arg, "not a non-negative number in proper range")
102 ni := uint(n)
103 fmt.Fprintf(&b, f, ni)
104 case 'o', 'x', 'X':
105 n := CheckNumber(l, arg)
106 ArgumentCheck(l, 0.0 <= n && n < math.Pow(2, 64), arg, "not a non-negative number in proper range")
107 ni := uint(n)
108 fmt.Fprintf(&b, f, ni)
109 case 'e', 'E', 'f', 'g', 'G':
110 fmt.Fprintf(&b, f, CheckNumber(l, arg))
111 case 'q':
112 s := CheckString(l, arg)
113 b.WriteByte('"')
114 for i := 0; i < len(s); i++ {
115 switch s[i] {
116 case '"', '\\', '\n':
117 b.WriteByte('\\')
118 b.WriteByte(s[i])
119 default:
120 if 0x20 <= s[i] && s[i] != 0x7f { // ASCII control characters don't correspond to a Unicode range.
121 b.WriteByte(s[i])
122 } else if i+1 < len(s) && unicode.IsDigit(rune(s[i+1])) {
123 fmt.Fprintf(&b, "\\%03d", s[i])
124 } else {
125 fmt.Fprintf(&b, "\\%d", s[i])
126 }
127 }
128 }

Callers 1

string.goFile · 0.85

Calls 9

ArgumentErrorFunction · 0.85
scanFormatFunction · 0.85
CheckIntegerFunction · 0.85
CheckNumberFunction · 0.85
ArgumentCheckFunction · 0.85
CheckStringFunction · 0.85
ToStringMetaFunction · 0.85
ErrorfFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected