2021-12-27 15:28:11 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-12-27 14:56:50 +01:00
|
|
|
use crate::engine::{make_getter, make_setter, FN_GET, FN_SET};
|
|
|
|
use crate::{Identifier, ImmutableString};
|
2021-12-27 15:28:11 +01:00
|
|
|
use std::ops::AddAssign;
|
2021-12-27 14:56:50 +01:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
|
|
|
|
/// _(internals)_ A factory of identifiers from text strings.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
///
|
2022-01-04 08:22:48 +01:00
|
|
|
/// Normal identifiers, property getters and setters are interned separately.
|
2021-12-27 14:56:50 +01:00
|
|
|
#[derive(Debug, Clone, Default, Hash)]
|
|
|
|
pub struct StringsInterner {
|
2022-01-04 08:22:48 +01:00
|
|
|
/// Normal strings.
|
|
|
|
strings: std::collections::BTreeMap<Identifier, ImmutableString>,
|
2021-12-27 14:56:50 +01:00
|
|
|
/// Property getters.
|
2021-12-27 15:28:11 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
getters: std::collections::BTreeMap<Identifier, ImmutableString>,
|
2021-12-27 14:56:50 +01:00
|
|
|
/// Property setters.
|
2021-12-27 15:28:11 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
setters: std::collections::BTreeMap<Identifier, ImmutableString>,
|
2021-12-27 14:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StringsInterner {
|
2021-12-31 16:01:34 +01:00
|
|
|
/// Create a new [`StringsInterner`].
|
2021-12-27 14:56:50 +01:00
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2022-01-04 08:22:48 +01:00
|
|
|
strings: std::collections::BTreeMap::new(),
|
2021-12-27 15:28:11 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
getters: std::collections::BTreeMap::new(),
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
setters: std::collections::BTreeMap::new(),
|
2021-12-27 14:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Get an identifier from a text string and prefix, adding it to the interner if necessary.
|
|
|
|
///
|
2022-01-04 08:22:48 +01:00
|
|
|
/// # Prefix
|
|
|
|
///
|
|
|
|
/// Currently recognized prefixes are:
|
|
|
|
///
|
|
|
|
/// * `""` - None (normal string)
|
|
|
|
/// * `"get$"` - Property getter, not available under `no_object`
|
|
|
|
/// * `"set$"` - Property setter, not available under `no_object`
|
|
|
|
///
|
2021-12-27 14:56:50 +01:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the prefix is not recognized.
|
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
2022-01-04 08:22:48 +01:00
|
|
|
pub fn get(&mut self, prefix: impl AsRef<str>, text: impl AsRef<str>) -> ImmutableString {
|
|
|
|
let (dict, mapper): (_, fn(&str) -> Identifier) = match prefix.as_ref() {
|
|
|
|
"" => (&mut self.strings, |s| s.into()),
|
2021-12-27 15:28:11 +01:00
|
|
|
|
2022-01-04 08:22:48 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
FN_GET => (&mut self.getters, |s| make_getter(s)),
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
FN_SET => (&mut self.setters, |s| make_setter(s)),
|
2021-12-27 15:28:11 +01:00
|
|
|
|
2022-01-04 08:22:48 +01:00
|
|
|
_ => unreachable!("unsupported prefix {}", prefix.as_ref()),
|
|
|
|
};
|
|
|
|
|
|
|
|
if dict.contains_key(text.as_ref()) {
|
|
|
|
dict.get(text.as_ref()).expect("exists").clone()
|
|
|
|
} else {
|
|
|
|
let value: ImmutableString = mapper(text.as_ref()).into();
|
|
|
|
dict.insert(text.as_ref().into(), value.clone());
|
|
|
|
value
|
2021-12-27 14:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:26:54 +01:00
|
|
|
impl AddAssign<Self> for StringsInterner {
|
2021-12-27 14:56:50 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
2022-01-04 08:22:48 +01:00
|
|
|
self.strings.extend(rhs.strings.into_iter());
|
2021-12-27 15:28:11 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-01-04 08:22:48 +01:00
|
|
|
self.getters.extend(rhs.getters.into_iter());
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
self.setters.extend(rhs.setters.into_iter());
|
2021-12-27 14:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 07:26:54 +01:00
|
|
|
|
|
|
|
impl AddAssign<&Self> for StringsInterner {
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_assign(&mut self, rhs: &Self) {
|
2022-01-04 08:22:48 +01:00
|
|
|
self.strings
|
|
|
|
.extend(rhs.strings.iter().map(|(k, v)| (k.clone(), v.clone())));
|
2021-12-29 07:26:54 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-01-04 08:22:48 +01:00
|
|
|
self.getters
|
|
|
|
.extend(rhs.getters.iter().map(|(k, v)| (k.clone(), v.clone())));
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
self.setters
|
|
|
|
.extend(rhs.setters.iter().map(|(k, v)| (k.clone(), v.clone())));
|
2021-12-29 07:26:54 +01:00
|
|
|
}
|
|
|
|
}
|