#![no_std]
use core::default::Default;
pub const DEFAULT_BUFFER_SIZE: usize = 4096;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct String<const N: usize> {
pub chars: [u8; N],
pub len: usize,
}
mod methods;
mod tostring;
mod vec;
pub use tostring::ToString;
pub use methods::*;
pub use vec::Vec;
macro_rules! vec {
($($x:expr),*) => {
{
let mut temp_vec = Vec::<u8, DEFAULT_BUFFER_SIZE>::new();
$(
temp_vec.push($x);
)*
temp_vec
}
};
}
macro_rules! string {
($x:expr) => {
{
let mut temp_string = String::<DEFAULT_BUFFER_SIZE>::new();
temp_string.push_str(stringify!($x));
temp_string
}
};
($($x:expr),*) => {
{
let mut temp_string = String::<DEFAULT_BUFFER_SIZE>::new();
$(
temp_string.push($x);
)*
temp_string
}
};
}
impl<const N: usize> Default for String<N> {
fn default() -> Self {
String::<N>::from("")
}
}
impl<const N: usize> core::fmt::Display for String<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<const N: usize> core::fmt::Debug for String<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:#?}", self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
extern crate std;
#[test]
fn test_new() {
let mut s = String::<64>::new();
s.push('a');
s.push('b');
s.push('รถ');
s.insert(1, 'x');
std::println!("s: {:#?}", s.as_str());
s.push_str("bchahahahaha");
std::println!("s: {:#?}", s.split("ah"));
std::println!("42: {:#?}", 42.to_string::<17>());
}
#[test]
fn test_vec() {
let mut v = vec![1, 2, 3, 4, 5];
std::println!("v: {}", v);
v.insert(2, 42);
std::println!("v: {}", v);
v.remove(2);
std::println!("v: {}", v);
}
#[test]
fn test_string() {
let mut s = string!["abcdef"];
std::println!("s: {}", s);
s.insert(2, 'x');
std::println!("s: {}", s);
s.remove(2);
std::println!("s: {}", s);
}
}