Add comparison operators to ImmutableString.

This commit is contained in:
Stephen Chung 2020-07-30 13:28:25 +08:00
parent 691e04292f
commit e505a06839
4 changed files with 51 additions and 9 deletions

View File

@ -1105,7 +1105,7 @@ impl Engine {
.downcast_ref::<ImmutableString>()
.ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
map.get_mut(index.as_str())
map.get_mut(index)
.map(Target::from)
.unwrap_or_else(|| Target::from(()))
})
@ -1208,10 +1208,8 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
Dynamic(Union::Map(rhs_value)) => match lhs_value {
// Only allows String or char
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_str()).into()),
Dynamic(Union::Char(c)) => {
Ok(rhs_value.contains_key(c.to_string().as_str()).into())
}
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(&s).into()),
Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()),
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
},
Dynamic(Union::Str(rhs_value)) => match lhs_value {

View File

@ -665,7 +665,8 @@ impl Engine {
))
})
.and_then(|s| FnPtr::try_from(s))
.map(Into::<Dynamic>::into);
.map(Into::<Dynamic>::into)
.map_err(|err| err.new_position(expr.position()));
}
}

View File

@ -23,7 +23,7 @@ fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
lib.set_fn_2_mut(
"has",
|map: &mut Map, prop: ImmutableString| Ok(map.contains_key(prop.as_str())),
|map: &mut Map, prop: ImmutableString| Ok(map.contains_key(&prop)),
);
lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT));
lib.set_fn_1_mut("clear", |map: &mut Map| {
@ -32,7 +32,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
});
lib.set_fn_2_mut(
"remove",
|x: &mut Map, name: ImmutableString| Ok(x.remove(name.as_str()).unwrap_or_else(|| ().into())),
|x: &mut Map, name: ImmutableString| Ok(x.remove(&name).unwrap_or_else(|| ().into())),
);
lib.set_fn_2_mut(
"mixin",
@ -47,7 +47,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
"fill_with",
|map1: &mut Map, map2: Map| {
map2.into_iter().for_each(|(key, value)| {
if !map1.contains_key(key.as_str()) {
if !map1.contains_key(&key) {
map1.insert(key, value);
}
});

View File

@ -6,6 +6,7 @@ use crate::stdlib::{
any::TypeId,
borrow::Borrow,
boxed::Box,
cmp::Ordering,
fmt,
hash::{BuildHasher, Hash, Hasher},
iter::FromIterator,
@ -141,6 +142,12 @@ impl AsRef<String> for ImmutableString {
}
}
impl Borrow<String> for ImmutableString {
fn borrow(&self) -> &String {
&self.0
}
}
impl Borrow<str> for ImmutableString {
fn borrow(&self) -> &str {
self.0.as_str()
@ -348,6 +355,42 @@ impl AddAssign<char> for ImmutableString {
}
}
impl<S: AsRef<str>> PartialEq<S> for ImmutableString {
fn eq(&self, other: &S) -> bool {
self.as_str().eq(other.as_ref())
}
}
impl PartialEq<ImmutableString> for str {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl PartialEq<ImmutableString> for String {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl<S: AsRef<str>> PartialOrd<S> for ImmutableString {
fn partial_cmp(&self, other: &S) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_ref())
}
}
impl PartialOrd<ImmutableString> for str {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.partial_cmp(other.as_str())
}
}
impl PartialOrd<ImmutableString> for String {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl ImmutableString {
/// Consume the `ImmutableString` and convert it into a `String`.
/// If there are other references to the same string, a cloned copy is returned.