MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / index_of

Function index_of

aiscript-vm/src/builtins/string.rs:156–182  ·  view source on GitHub ↗

Find and position functions

(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

154
155// Find and position functions
156fn index_of<'gc>(
157 _mc: &'gc Mutation<'gc>,
158 receiver: Value<'gc>,
159 args: Vec<Value<'gc>>,
160) -> Result<Value<'gc>, VmError> {
161 let substr = string_arg!(&args, 0, "index_of")?;
162
163 // Optional start position
164 let start = if args.len() > 1 {
165 float_arg!(&args, 1, "index_of")? as usize
166 } else {
167 0
168 };
169
170 let s = receiver.as_string_value()?;
171 let s = s.as_str();
172 let substr = substr.to_str().unwrap();
173
174 if start > s.len() {
175 return Ok(Value::Number(-1.0));
176 }
177
178 match s[start..].find(substr) {
179 Some(pos) => Ok(Value::Number((pos + start) as f64)),
180 None => Ok(Value::Number(-1.0)),
181 }
182}
183
184fn last_index_of<'gc>(
185 _mc: &'gc Mutation<'gc>,

Callers

nothing calls this directly

Calls 4

lenMethod · 0.80
as_string_valueMethod · 0.80
to_strMethod · 0.80
as_strMethod · 0.45

Tested by

no test coverage detected