1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use log::trace;

use crate::convert::{FromMrb, TryFromMrb};
use crate::def::{ClassLike, Define};
use crate::eval::MrbEval;
use crate::extn::core::error::{ArgumentError, RubyException, RuntimeError, TypeError};
use crate::sys;
use crate::value::Value;
use crate::{Mrb, MrbError};

mod scan;

pub fn patch(interp: &Mrb) -> Result<(), MrbError> {
    if interp.borrow().class_spec::<RString>().is_some() {
        return Ok(());
    }
    let string = interp
        .borrow_mut()
        .def_class::<RString>("String", None, None);
    interp.eval(include_str!("string.rb"))?;
    string
        .borrow_mut()
        .add_method("ord", RString::ord, sys::mrb_args_none());
    string
        .borrow_mut()
        .add_method("scan", RString::scan, sys::mrb_args_req(1));
    string.borrow().define(interp).map_err(|_| MrbError::New)?;
    trace!("Patched String onto interpreter");
    Ok(())
}

#[allow(clippy::module_name_repetitions)]
pub struct RString;

impl RString {
    unsafe extern "C" fn ord(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        if let Ok(s) = String::try_from_mrb(&interp, Value::new(&interp, slf)) {
            if let Some(first) = s.chars().next() {
                // One UTF-8 character, which are at most 32 bits.
                Value::from_mrb(&interp, first as u32).inner()
            } else {
                drop(s);
                ArgumentError::raise(interp, "empty string")
            }
        } else {
            sys::mrb_sys_nil_value()
        }
    }

    unsafe extern "C" fn scan(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        let interp = unwrap_interpreter!(mrb);
        let value = Value::new(&interp, slf);
        let result =
            scan::Args::extract(&interp).and_then(|args| scan::method(&interp, args, value));

        match result {
            Ok(result) => result.inner(),
            Err(scan::Error::WrongType) => {
                TypeError::raise(interp, "wrong argument type (expected Regexp)")
            }
            Err(scan::Error::Fatal) => RuntimeError::raise(interp, "fatal String#scan error"),
        }
    }
}

// Tests from String core docs in Ruby 2.6.3
// https://ruby-doc.org/core-2.6.3/String.html
#[cfg(test)]
mod tests {
    use crate::convert::FromMrb;
    use crate::eval::MrbEval;
    use crate::extn::core::string;
    use crate::value::{Value, ValueLike};

    #[test]
    fn string_equal_squiggle() {
        let interp = crate::interpreter().expect("mrb init");
        string::patch(&interp).expect("string init");

        let value = interp.eval(r#""cat o' 9 tails" =~ /\d/"#).unwrap();
        assert_eq!(value.try_into::<Option<i64>>(), Ok(Some(7)));
        let value = interp.eval(r#""cat o' 9 tails" =~ 9"#).unwrap();
        assert_eq!(value.try_into::<Option<i64>>(), Ok(None));
    }

    #[test]
    fn string_idx() {
        let interp = crate::interpreter().expect("mrb init");
        string::patch(&interp).expect("string init");

        assert_eq!(
            &interp
                .eval(r"'hello there'[/[aeiou](.)\1/]")
                .unwrap()
                .try_into::<String>()
                .unwrap(),
            "ell"
        );
        assert_eq!(
            &interp
                .eval(r"'hello there'[/[aeiou](.)\1/, 0]")
                .unwrap()
                .try_into::<String>()
                .unwrap(),
            "ell"
        );
        assert_eq!(
            &interp
                .eval(r"'hello there'[/[aeiou](.)\1/, 1]")
                .unwrap()
                .try_into::<String>()
                .unwrap(),
            "l"
        );
        assert_eq!(
            interp
                .eval(r"'hello there'[/[aeiou](.)\1/, 2]")
                .unwrap()
                .try_into::<Option<String>>()
                .unwrap(),
            None
        );
        assert_eq!(
            &interp
                .eval(r"'hello there'[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, 'non_vowel']")
                .unwrap()
                .try_into::<String>()
                .unwrap(),
            "l"
        );
        assert_eq!(
            &interp
                .eval(r"'hello there'[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, 'vowel']")
                .unwrap()
                .try_into::<String>()
                .unwrap(),
            "e"
        );
    }

    #[test]
    fn string_scan() {
        let interp = crate::interpreter().expect("mrb init");
        string::patch(&interp).expect("string init");

        let s = Value::from_mrb(&interp, "abababa");
        let result = s
            .funcall::<Vec<String>, _, _>("scan", &[interp.eval("/./").expect("eval")])
            .expect("funcall");
        assert_eq!(
            result,
            vec!["a", "b", "a", "b", "a", "b", "a"]
                .into_iter()
                .map(str::to_owned)
                .collect::<Vec<_>>()
        );
        let result = s
            .funcall::<Vec<String>, _, _>("scan", &[interp.eval("/../").expect("eval")])
            .expect("funcall");
        assert_eq!(
            result,
            vec!["ab", "ab", "ab"]
                .into_iter()
                .map(str::to_owned)
                .collect::<Vec<_>>()
        );
        let result = s
            .funcall::<Vec<String>, _, _>("scan", &[interp.eval("'aba'").expect("eval")])
            .expect("funcall");
        assert_eq!(
            result,
            vec!["aba", "aba"]
                .into_iter()
                .map(str::to_owned)
                .collect::<Vec<_>>()
        );
        let result = s
            .funcall::<Vec<String>, _, _>("scan", &[interp.eval("'no no no'").expect("eval")])
            .expect("funcall");
        assert_eq!(result, <Vec<String>>::new());
    }

    #[test]
    fn string_unary_minus() {
        let interp = crate::interpreter().expect("mrb init");
        string::patch(&interp).expect("string init");

        let s = interp.eval("-'abababa'").expect("eval");
        let result = s.funcall::<bool, _, _>("frozen?", &[]).expect("funcall");
        assert!(result);
        let result = s.funcall::<String, _, _>("itself", &[]).expect("funcall");
        assert_eq!(result, "abababa");
    }
}