Find and position functions
(
_mc: &'gc Mutation<'gc>,
receiver: Value<'gc>,
args: Vec<Value<'gc>>,
)
| 154 | |
| 155 | // Find and position functions |
| 156 | fn 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 | |
| 184 | fn last_index_of<'gc>( |
| 185 | _mc: &'gc Mutation<'gc>, |
nothing calls this directly
no test coverage detected