pub(crate) fn replace_repeat(
        &self,
        vm: &mut VM,
        given: &str,
        replace: &str,
    ) -> Result<(String, bool), RubyError> {
        let mut range = vec![];
        let mut i = 0;
        let mut last_captures = None;
        loop {
            if i >= given.len() {
                break;
            }
            match self.captures_from_pos(given, i) {
                Ok(None) => break,
                Ok(Some(captures)) => {
                    let m = captures.get(0).unwrap();
                    // the length of matched string can be 0.
                    // this is neccesary to avoid infinite loop.
                    i = if m.end() == m.start() {
                        m.end() + 1
                    } else {
                        m.end()
                    };
                    range.push((m.start(), m.end()));
                    last_captures = Some(captures);
                }
                Err(err) => return Err(RubyError::internal(format!("Capture failed. {:?}", err))),
            };
        }
        let mut res = given.to_string();
        for (start, end) in range.iter().rev() {
            res.replace_range(start..end, replace);
        }

        match last_captures {
            Some(c) => vm.get_captures(&c, given),
            None => {}
        }

        Ok((res, range.len() != 0))
    }
Replace all matches for `self` in `given` string with `replace`.

### return
(replaced:String, is_replaced?:bool)
ruruby/src/builtin/regexp.rs