2020-08-15 06:57:47 +02:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use crate::plugin::*;
|
2021-06-16 17:49:18 +02:00
|
|
|
use crate::{def_package, FnPtr, INT};
|
|
|
|
use std::fmt::{Binary, LowerHex, Octal};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-11-16 09:28:04 +01:00
|
|
|
use crate::Array;
|
2020-05-06 13:45:17 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-11-16 09:28:04 +01:00
|
|
|
use crate::Map;
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2021-07-24 08:11:16 +02:00
|
|
|
pub const FUNC_TO_STRING: &str = "to_string";
|
|
|
|
pub const FUNC_TO_DEBUG: &str = "to_debug";
|
2020-11-30 04:20:51 +01:00
|
|
|
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
|
2021-11-05 16:22:05 +01:00
|
|
|
lib.standard = true;
|
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
|
2021-06-16 17:49:18 +02:00
|
|
|
combine_with_exported_module!(lib, "number_formatting", number_formatting);
|
2020-04-21 17:01:10 +02:00
|
|
|
});
|
2020-08-15 06:57:47 +02:00
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
// Register print and debug
|
|
|
|
|
2021-07-10 05:06:13 +02:00
|
|
|
#[inline]
|
2021-04-02 13:26:55 +02:00
|
|
|
pub fn print_with_func(
|
2021-03-31 04:16:38 +02:00
|
|
|
fn_name: &str,
|
|
|
|
ctx: &NativeCallContext,
|
|
|
|
value: &mut Dynamic,
|
|
|
|
) -> crate::ImmutableString {
|
2021-10-19 13:57:15 +02:00
|
|
|
match ctx.call_fn_raw(fn_name, true, false, &mut [value]) {
|
2021-11-13 05:23:35 +01:00
|
|
|
Ok(result) if result.is::<crate::ImmutableString>() => {
|
|
|
|
result.into_immutable_string().expect("`ImmutableString`")
|
|
|
|
}
|
2020-11-30 04:20:51 +01:00
|
|
|
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
|
|
|
|
Err(_) => ctx.engine().map_type_name(value.type_name()).into(),
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 06:57:47 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[export_module]
|
|
|
|
mod print_debug_functions {
|
2021-02-23 12:08:05 +01:00
|
|
|
use crate::ImmutableString;
|
|
|
|
|
2021-04-02 13:26:55 +02:00
|
|
|
#[rhai_fn(name = "print", pure)]
|
|
|
|
pub fn print_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
|
|
|
|
print_with_func(FUNC_TO_STRING, &ctx, item)
|
2021-02-23 12:08:05 +01:00
|
|
|
}
|
2021-04-02 13:26:55 +02:00
|
|
|
#[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()
|
2021-02-23 12:08:05 +01:00
|
|
|
}
|
2020-10-19 17:49:01 +02:00
|
|
|
#[rhai_fn(name = "print", name = "debug")]
|
2021-11-07 11:12:37 +01:00
|
|
|
pub fn print_empty_string(ctx: NativeCallContext) -> ImmutableString {
|
|
|
|
ctx.engine().const_empty_string()
|
2020-10-19 17:49:01 +02:00
|
|
|
}
|
|
|
|
#[rhai_fn(name = "print", name = "to_string")]
|
|
|
|
pub fn print_string(s: ImmutableString) -> ImmutableString {
|
|
|
|
s
|
|
|
|
}
|
2021-02-24 04:05:39 +01:00
|
|
|
#[rhai_fn(name = "debug", name = "to_debug", pure)]
|
2020-10-19 17:49:01 +02:00
|
|
|
pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
|
2021-02-23 12:08:05 +01:00
|
|
|
f.to_string().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
2021-10-20 10:22:12 +02:00
|
|
|
#[rhai_fn(name = "print", name = "to_string")]
|
|
|
|
pub fn print_f64(number: f64) -> ImmutableString {
|
|
|
|
crate::ast::FloatWrapper::new(number).to_string().into()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[rhai_fn(name = "print", name = "to_string")]
|
|
|
|
pub fn print_f32(number: f32) -> ImmutableString {
|
|
|
|
crate::ast::FloatWrapper::new(number).to_string().into()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[rhai_fn(name = "debug", name = "to_debug")]
|
|
|
|
pub fn debug_f64(number: f64) -> ImmutableString {
|
|
|
|
format!("{:?}", crate::ast::FloatWrapper::new(number)).into()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[rhai_fn(name = "debug", name = "to_debug")]
|
|
|
|
pub fn debug_f32(number: f32) -> ImmutableString {
|
|
|
|
format!("{:?}", crate::ast::FloatWrapper::new(number)).into()
|
2020-10-19 17:49:01 +02:00
|
|
|
}
|
2020-09-23 06:00:03 +02:00
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2021-10-20 10:22:12 +02:00
|
|
|
#[rhai_fn(
|
|
|
|
name = "print",
|
|
|
|
name = "to_string",
|
|
|
|
name = "debug",
|
|
|
|
name = "to_debug",
|
|
|
|
pure
|
|
|
|
)]
|
|
|
|
pub fn format_array(ctx: NativeCallContext, array: &mut Array) -> ImmutableString {
|
|
|
|
let len = array.len();
|
|
|
|
let mut result = String::with_capacity(len * 5 + 2);
|
|
|
|
result.push('[');
|
|
|
|
|
|
|
|
array.iter_mut().enumerate().for_each(|(i, x)| {
|
|
|
|
result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, x));
|
|
|
|
if i < len - 1 {
|
|
|
|
result.push_str(", ");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
result.push(']');
|
|
|
|
result.into()
|
2020-11-30 04:20:51 +01:00
|
|
|
}
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-10-20 10:22:12 +02:00
|
|
|
#[rhai_fn(
|
|
|
|
name = "print",
|
|
|
|
name = "to_string",
|
|
|
|
name = "debug",
|
|
|
|
name = "to_debug",
|
|
|
|
pure
|
|
|
|
)]
|
|
|
|
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString {
|
|
|
|
let len = map.len();
|
|
|
|
let mut result = String::with_capacity(len * 5 + 3);
|
|
|
|
result.push_str("#{");
|
|
|
|
|
|
|
|
map.iter_mut().enumerate().for_each(|(i, (k, v))| {
|
|
|
|
result.push_str(&format!(
|
|
|
|
"{:?}: {}{}",
|
|
|
|
k,
|
|
|
|
&print_with_func(FUNC_TO_DEBUG, &ctx, v),
|
|
|
|
if i < len - 1 { ", " } else { "" }
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
result.push('}');
|
|
|
|
result.into()
|
2020-08-22 05:29:04 +02:00
|
|
|
}
|
2020-08-15 06:57:47 +02:00
|
|
|
}
|
2021-06-16 17:49:18 +02:00
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
mod number_formatting {
|
|
|
|
#[rhai_fn(skip)]
|
|
|
|
pub fn to_hex<T: LowerHex>(value: T) -> ImmutableString {
|
|
|
|
format!("{:x}", value).into()
|
|
|
|
}
|
|
|
|
#[rhai_fn(skip)]
|
|
|
|
pub fn to_octal<T: Octal>(value: T) -> ImmutableString {
|
|
|
|
format!("{:o}", value).into()
|
|
|
|
}
|
|
|
|
#[rhai_fn(skip)]
|
|
|
|
pub fn to_binary<T: Binary>(value: T) -> ImmutableString {
|
|
|
|
format!("{:b}", value).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn int_to_hex(value: INT) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn int_to_octal(value: INT) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
2021-07-04 10:51:05 +02:00
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn int_to_binary(value: INT) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
2021-06-16 17:49:18 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
pub mod numbers {
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn u8_to_hex(value: u8) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn u16_to_hex(value: u16) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn u32_to_hex(value: u32) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn u64_to_hex(value: u64) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn i8_to_hex(value: i8) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn i16_to_hex(value: i16) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn i32_to_hex(value: i32) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn i64_to_hex(value: i64) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn u8_to_octal(value: u8) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn u16_to_octal(value: u16) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn u32_to_octal(value: u32) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn u64_to_octal(value: u64) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn i8_to_octal(value: i8) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn i16_to_octal(value: i16) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn i32_to_octal(value: i32) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn i64_to_octal(value: i64) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn u8_to_binary(value: u8) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn u16_to_binary(value: u16) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn u32_to_binary(value: u32) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn u64_to_binary(value: u64) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn i8_to_binary(value: i8) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn i16_to_binary(value: i16) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn i32_to_binary(value: i32) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn i64_to_binary(value: i64) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
|
|
|
|
pub mod num_128 {
|
|
|
|
#[rhai_fn(name = "to_hex")]
|
|
|
|
pub fn u128_to_hex(value: u128) -> ImmutableString {
|
|
|
|
to_hex(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_octal")]
|
|
|
|
pub fn i128_to_octal(value: i128) -> ImmutableString {
|
|
|
|
to_octal(value)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "to_binary")]
|
|
|
|
pub fn i128_to_binary(value: i128) -> ImmutableString {
|
|
|
|
to_binary(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|