Move range contains into builtin.

This commit is contained in:
Stephen Chung 2021-12-18 14:50:37 +08:00
parent fc84600b22
commit 2b79a65454
2 changed files with 22 additions and 14 deletions

View File

@ -250,7 +250,7 @@ pub fn get_builtin_binary_op_fn(
OP_CONTAINS => Some(|_, args| { OP_CONTAINS => Some(|_, args| {
let s = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN); let s = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
let c = args[1].as_char().expect(BUILTIN); let c = args[1].as_char().expect(BUILTIN);
Ok((s.contains(c)).into()) Ok(s.contains(c).into())
}), }),
_ => None, _ => None,
}; };
@ -306,6 +306,16 @@ pub fn get_builtin_binary_op_fn(
// Handle ranges here because ranges are implemented as custom type // Handle ranges here because ranges are implemented as custom type
if type1 == TypeId::of::<ExclusiveRange>() { if type1 == TypeId::of::<ExclusiveRange>() {
if type2 == TypeId::of::<INT>() {
return match op {
OP_CONTAINS => Some(|_, args| {
let range = &*args[0].read_lock::<ExclusiveRange>().expect(BUILTIN);
let x = args[1].as_int().expect("`INT`");
Ok(range.contains(&x).into())
}),
_ => None,
};
}
if type1 == type2 { if type1 == type2 {
return match op { return match op {
"==" => Some(impl_op!(ExclusiveRange == ExclusiveRange)), "==" => Some(impl_op!(ExclusiveRange == ExclusiveRange)),
@ -316,6 +326,16 @@ pub fn get_builtin_binary_op_fn(
} }
if type1 == TypeId::of::<InclusiveRange>() { if type1 == TypeId::of::<InclusiveRange>() {
if type2 == TypeId::of::<INT>() {
return match op {
OP_CONTAINS => Some(|_, args| {
let range = &*args[0].read_lock::<InclusiveRange>().expect(BUILTIN);
let x = args[1].as_int().expect("`INT`");
Ok(range.contains(&x).into())
}),
_ => None,
};
}
if type1 == type2 { if type1 == type2 {
return match op { return match op {
"==" => Some(impl_op!(InclusiveRange == InclusiveRange)), "==" => Some(impl_op!(InclusiveRange == InclusiveRange)),
@ -433,11 +453,7 @@ pub fn get_builtin_binary_op_fn(
">=" => Some(impl_op!(ImmutableString >= ImmutableString)), ">=" => Some(impl_op!(ImmutableString >= ImmutableString)),
"<" => Some(impl_op!(ImmutableString < ImmutableString)), "<" => Some(impl_op!(ImmutableString < ImmutableString)),
"<=" => Some(impl_op!(ImmutableString <= ImmutableString)), "<=" => Some(impl_op!(ImmutableString <= ImmutableString)),
OP_CONTAINS => Some(|_, args| { OP_CONTAINS => Some(impl_op!(ImmutableString.contains(ImmutableString.as_str()))),
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
Ok((s1.contains(s2.as_str())).into())
}),
_ => None, _ => None,
}; };
} }

View File

@ -537,10 +537,6 @@ mod range_functions {
pub fn end(range: &mut ExclusiveRange) -> INT { pub fn end(range: &mut ExclusiveRange) -> INT {
range.end range.end
} }
#[rhai_fn(name = "contains", pure)]
pub fn contains(range: &mut ExclusiveRange, value: INT) -> bool {
range.contains(&value)
}
#[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)] #[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)]
pub fn is_inclusive(range: &mut ExclusiveRange) -> bool { pub fn is_inclusive(range: &mut ExclusiveRange) -> bool {
let _range = range; let _range = range;
@ -559,10 +555,6 @@ mod range_functions {
pub fn end_inclusive(range: &mut InclusiveRange) -> INT { pub fn end_inclusive(range: &mut InclusiveRange) -> INT {
*range.end() *range.end()
} }
#[rhai_fn(name = "contains", pure)]
pub fn contains_inclusive(range: &mut InclusiveRange, value: INT) -> bool {
range.contains(&value)
}
#[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)] #[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)]
pub fn is_inclusive_inclusive(range: &mut InclusiveRange) -> bool { pub fn is_inclusive_inclusive(range: &mut InclusiveRange) -> bool {
let _range = range; let _range = range;