MCPcopy Index your code
hub / github.com/yuin/gopher-lua / strFind

Function strFind

stringlib.go:90–134  ·  view source on GitHub ↗
(L *LState)

Source from the content-addressed store, hash-verified

88}
89
90func strFind(L *LState) int {
91 str := L.CheckString(1)
92 pattern := L.CheckString(2)
93 if len(pattern) == 0 {
94 L.Push(LNumber(1))
95 L.Push(LNumber(0))
96 return 2
97 }
98 init := luaIndex2StringIndex(str, L.OptInt(3, 1), true)
99 plain := false
100 if L.GetTop() == 4 {
101 plain = LVAsBool(L.Get(4))
102 }
103
104 if plain {
105 pos := strings.Index(str[init:], pattern)
106 if pos < 0 {
107 L.Push(LNil)
108 return 1
109 }
110 L.Push(LNumber(init+pos) + 1)
111 L.Push(LNumber(init + pos + len(pattern)))
112 return 2
113 }
114
115 mds, err := pm.Find(pattern, unsafeFastStringToReadOnlyBytes(str), init, 1)
116 if err != nil {
117 L.RaiseError(err.Error())
118 }
119 if len(mds) == 0 {
120 L.Push(LNil)
121 return 1
122 }
123 md := mds[0]
124 L.Push(LNumber(md.Capture(0) + 1))
125 L.Push(LNumber(md.Capture(1)))
126 for i := 2; i < md.CaptureLength(); i += 2 {
127 if md.IsPosCapture(i) {
128 L.Push(LNumber(md.Capture(i)))
129 } else {
130 L.Push(LString(str[md.Capture(i):md.Capture(i+1)]))
131 }
132 }
133 return md.CaptureLength()/2 + 1
134}
135
136func strFormat(L *LState) int {
137 str := L.CheckString(1)

Callers

nothing calls this directly

Calls 15

FindFunction · 0.92
LNumberTypeAlias · 0.85
luaIndex2StringIndexFunction · 0.85
LVAsBoolFunction · 0.85
LStringTypeAlias · 0.85
CheckStringMethod · 0.80
OptIntMethod · 0.80
CaptureMethod · 0.80
CaptureLengthMethod · 0.80
IsPosCaptureMethod · 0.80
PushMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…