Implement string functions with to_string/to_debug.
This commit is contained in:
@@ -10,8 +10,8 @@ use crate::Array;
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::Map;
|
||||
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
const FUNC_TO_DEBUG: &'static str = "to_debug";
|
||||
pub const FUNC_TO_STRING: &'static str = "to_string";
|
||||
pub const FUNC_TO_DEBUG: &'static str = "to_debug";
|
||||
|
||||
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
|
||||
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
|
||||
@@ -19,9 +19,8 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
||||
|
||||
// Register print and debug
|
||||
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
#[inline(always)]
|
||||
fn print_with_func(
|
||||
pub fn print_with_func(
|
||||
fn_name: &str,
|
||||
ctx: &NativeCallContext,
|
||||
value: &mut Dynamic,
|
||||
@@ -39,17 +38,25 @@ fn print_with_func(
|
||||
mod print_debug_functions {
|
||||
use crate::ImmutableString;
|
||||
|
||||
#[rhai_fn(name = "print", name = "to_string", pure)]
|
||||
pub fn print_generic(item: &mut Dynamic) -> ImmutableString {
|
||||
item.to_string().into()
|
||||
#[rhai_fn(name = "print", pure)]
|
||||
pub fn print_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
|
||||
print_with_func(FUNC_TO_STRING, &ctx, item)
|
||||
}
|
||||
#[rhai_fn(name = "debug", name = "to_debug", pure)]
|
||||
pub fn debug_generic(item: &mut Dynamic) -> ImmutableString {
|
||||
format!("{:?}", item).into()
|
||||
#[rhai_fn(name = "to_string", pure)]
|
||||
pub fn to_string_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
|
||||
ctx.engine().map_type_name(&item.to_string()).into()
|
||||
}
|
||||
#[rhai_fn(name = "debug", pure)]
|
||||
pub fn debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
|
||||
print_with_func(FUNC_TO_DEBUG, &ctx, item)
|
||||
}
|
||||
#[rhai_fn(name = "to_debug", pure)]
|
||||
pub fn to_debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
|
||||
ctx.engine().map_type_name(&format!("{:?}", item)).into()
|
||||
}
|
||||
#[rhai_fn(name = "print", name = "debug")]
|
||||
pub fn print_empty_string() -> ImmutableString {
|
||||
"".to_string().into()
|
||||
Default::default()
|
||||
}
|
||||
#[rhai_fn(name = "print", name = "to_string")]
|
||||
pub fn print_string(s: ImmutableString) -> ImmutableString {
|
||||
|
@@ -6,6 +6,8 @@ use crate::stdlib::{
|
||||
};
|
||||
use crate::{def_package, Dynamic, ImmutableString, StaticVec, INT};
|
||||
|
||||
use super::string_basic::{print_with_func, FUNC_TO_STRING};
|
||||
|
||||
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
|
||||
combine_with_exported_module!(lib, "string", string_functions);
|
||||
|
||||
@@ -21,22 +23,30 @@ mod string_functions {
|
||||
use crate::ImmutableString;
|
||||
|
||||
#[rhai_fn(name = "+", name = "append")]
|
||||
pub fn add_append(string: &str, item: Dynamic) -> ImmutableString {
|
||||
format!("{}{}", string, item).into()
|
||||
pub fn add_append(ctx: NativeCallContext, string: &str, mut item: Dynamic) -> ImmutableString {
|
||||
let s = print_with_func(FUNC_TO_STRING, &ctx, &mut item);
|
||||
format!("{}{}", string, s).into()
|
||||
}
|
||||
#[rhai_fn(name = "+", pure)]
|
||||
pub fn add_prepend(item: &mut Dynamic, string: &str) -> ImmutableString {
|
||||
format!("{}{}", item, string).into()
|
||||
pub fn add_prepend(
|
||||
ctx: NativeCallContext,
|
||||
item: &mut Dynamic,
|
||||
string: &str,
|
||||
) -> ImmutableString {
|
||||
let s = print_with_func(FUNC_TO_STRING, &ctx, item);
|
||||
format!("{}{}", s, string).into()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn add_append_unit(string: ImmutableString, _x: ()) -> ImmutableString {
|
||||
pub fn add_append_unit(string: ImmutableString, _item: ()) -> ImmutableString {
|
||||
string
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn add_prepend_unit(_x: (), string: ImmutableString) -> ImmutableString {
|
||||
pub fn add_prepend_unit(_item: (), string: ImmutableString) -> ImmutableString {
|
||||
string
|
||||
}
|
||||
#[rhai_fn(name = "+=")]
|
||||
pub fn add_append_assign_unit(_string: &mut ImmutableString, _item: ()) {}
|
||||
|
||||
#[rhai_fn(name = "len", get = "len")]
|
||||
pub fn len(string: &str) -> INT {
|
||||
|
Reference in New Issue
Block a user