#![allow(unused_variables)]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(unused_parens)]
#![allow(unused_assignments)]
#![allow(unused_mut)]
#![allow(dead_code)]
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
use crate::fstr;
use crate::tstr;
use core::cmp::{min, Ordering};
use core::ops::Add;
#[cfg(not(feature = "no-alloc"))]
extern crate alloc;
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
extern crate std;
#[derive(Copy, Clone, Eq)]
pub struct zstr<const N: usize> {
chrs: [u8; N],
} impl<const N: usize> zstr<N> {
pub fn make(s: &str) -> zstr<N> {
let mut chars = [0u8; N];
let bytes = s.as_bytes(); let mut i = 0;
let limit = if N == 0 { 0 } else { min(N - 1, bytes.len()) };
chars[..limit].clone_from_slice(&bytes[..limit]);
zstr { chrs: chars }
}
#[inline]
pub fn create(s: &str) -> zstr<N> {
Self::make(s)
}
pub fn try_make(s: &str) -> Result<zstr<N>, &str> {
if s.len() + 1 > N {
Err(s)
} else {
Ok(zstr::make(s))
}
}
pub const fn new() -> zstr<N> {
zstr {
chrs: [0;N]
}
}
pub const fn const_make(s:&str) -> zstr<N> {
let mut t = zstr::<N>::new();
let mut len = s.len();
if len+1>N { len = N-1; } let bytes = s.as_bytes();
let mut i = 0;
while i<len {
t.chrs[i] = bytes[i];
i += 1;
}
t
}
pub const fn const_try_make(s:&str) -> Option<zstr<N>> {
if s.len()+1>N {None}
else { Some(zstr::const_make(s)) }
}
pub const fn from_raw(s: &[u8]) -> zstr<N> {
let mut z = zstr { chrs: [0; N] };
let mut i = 0;
while i + 1 < N && i < s.len() && s[i] != 0 {
z.chrs[i] = s[i];
i += 1;
}
z
}
#[inline(always)]
pub const fn len(&self) -> usize {
self.blen()
}
pub const fn linear_len(&self) -> usize {
let mut i = 0;
while self.chrs[i] != 0 {
i += 1;
}
return i;
}
pub const fn check_integrity(&self) -> bool {
let mut n = self.linear_len();
if n == N {
return false;
}
while n < N {
if self.chrs[n] != 0 {
return false;
}
n += 1;
} true
}
pub fn clean(&mut self) {
let mut n = self.linear_len();
if n == N {
self.chrs[n - 1] = 0;
}
while n < N {
self.chrs[n] = 0;
n += 1;
} }
#[inline(always)]
pub const fn capacity(&self) -> usize {
if N == 0 {
return 0;
}
N - 1
}
const fn blen(&self) -> usize {
let (mut min, mut max) = (0, N);
let mut mid = 0;
while min < max {
mid = min + (max-min)/2; if self.chrs[mid] == 0 {
max = mid;
} else {
min = mid + 1;
}
} min
}
#[cfg(not(feature = "no-alloc"))]
pub fn to_string(&self) -> alloc::string::String {
alloc::string::String::from(self.to_str())
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.chrs[..self.blen() + 1]
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
let n = self.blen()+1;
&mut self.chrs[0..n]
}
#[inline]
pub fn as_bytes_non_terminated(&self) -> &[u8] {
&self.chrs[..self.blen()]
}
pub fn to_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.chrs[0..self.blen()]) }
}
pub fn as_str(&self) -> &str {
core::str::from_utf8(&self.chrs[0..self.blen()]).unwrap()
}
pub fn as_str_safe(&self) -> Result<&str,core::str::Utf8Error> {
core::str::from_utf8(&self.chrs[0..self.blen()])
}
pub fn set(&mut self, i: usize, c: char) -> bool {
let ref mut cbuf = [0u8; 4];
c.encode_utf8(cbuf);
let clen = c.len_utf8();
if let Some((bi, rc)) = self.to_str().char_indices().nth(i) {
if clen == rc.len_utf8() {
self.chrs[bi..bi + clen].clone_from_slice(&cbuf[..clen]);
return true;
}
}
return false;
} #[inline]
pub fn push<'t>(&mut self, s: &'t str) -> &'t str {
self.push_str(s)
}
pub fn push_str<'t>(&mut self, src: &'t str) -> &'t str {
let srclen = src.len();
let slen = self.blen();
let bytes = &src.as_bytes();
let length = core::cmp::min(slen + srclen, N - 1);
let remain = if N - 1 >= (slen + srclen) {
0
} else {
(srclen + slen) - N + 1
};
let mut i = 0;
while i < srclen && i + slen + 1 < N {
self.chrs[slen + i] = bytes[i];
i += 1;
} &src[srclen - remain..]
}
pub fn push_char(&mut self, c: char) -> bool {
let clen = c.len_utf8();
let slen = self.len();
if slen + clen >= N {
return false;
}
let mut buf = [0u8; 4]; c.encode_utf8(&mut buf);
for i in 0..clen {
self.chrs[slen + i] = buf[i];
}
self.chrs[slen + clen] = 0;
true
}
pub fn pop_char(&mut self) -> Option<char> {
if self.chrs[0] == 0 {
return None;
} let (ci, lastchar) = self.char_indices().last().unwrap();
let mut cm = ci;
while cm < N && self.chrs[cm] != 0 {
self.chrs[cm] = 0;
cm += 1;
}
Some(lastchar)
}
pub fn charlen(&self) -> usize {
self.to_str().chars().count()
}
pub fn nth(&self, n: usize) -> Option<char> {
self.to_str().chars().nth(n)
}
pub const fn nth_bytechar(&self, n: usize) -> char {
self.chrs[n] as char
}
pub const fn nth_ascii(&self, n: usize) -> char {
self.chrs[n] as char
}
pub fn is_ascii(&self) -> bool {
self.to_str().is_ascii()
}
pub fn truncate(&mut self, n: usize) {
if let Some((bi, c)) = self.to_str().char_indices().nth(n) {
let mut bm = bi;
while bm < N && self.chrs[bm] != 0 {
self.chrs[bm] = 0;
bm += 1;
}
}
}
pub fn truncate_bytes(&mut self, n: usize) {
if n < N {
assert!(self.is_char_boundary(n));
let mut m = n;
while m < N && self.chrs[m] != 0 {
self.chrs[m] = 0;
m += 1;
}
}
}
pub fn right_ascii_trim(&mut self) {
let mut n = self.blen();
while n > 0 && (self.chrs[n - 1] as char).is_ascii_whitespace() {
self.chrs[n - 1] = 0;
n -= 1;
}
assert!(self.is_char_boundary(n));
}
pub fn reverse_bytes(&mut self) {
let n = self.blen();
let m = n / 2;
let mut i = 0;
while i < m {
self.chrs.swap(i, n - i - 1);
i += 1;
}
}
pub fn swap_bytes(&mut self, i: usize, k: usize) -> bool {
if i != k && i < N && k < N && self.chrs[i] != 0 && self.chrs[k] != 0 {
self.chrs.swap(i, k);
true
} else {
false
}
}
pub fn clear(&mut self) {
self.chrs = [0; N];
}
pub fn make_ascii_lowercase(&mut self) {
assert!(self.is_ascii());
for b in &mut self.chrs {
if *b == 0 {
break;
} else if *b >= 65 && *b <= 90 {
*b += 32;
}
}
}
pub fn make_ascii_uppercase(&mut self) {
assert!(self.is_ascii());
for b in &mut self.chrs {
if *b == 0 {
break;
} else if *b >= 97 && *b <= 122 {
*b -= 32;
}
}
}
pub fn to_ascii_upper(&self) -> Self {
let mut cp = self.clone();
cp.make_ascii_uppercase();
cp
}
pub fn to_ascii_lower(&self) -> Self {
let mut cp = *self;
cp.make_ascii_lowercase();
cp
}
pub fn case_insensitive_eq<TA>(&self, other: TA) -> bool
where
TA: AsRef<str>,
{
if self.len() != other.as_ref().len() {
return false;
}
let obytes = other.as_ref().as_bytes();
for i in 0..self.len() {
let mut c = self.chrs[i];
if (c > 64 && c < 91) {
c = c | 32;
} let mut d = obytes[i];
if (d > 64 && d < 91) {
d = d | 32;
} if c != d {
return false;
}
} true
}
pub const fn to_ptr(&self) -> *const u8 {
let ptr = &self.chrs[0] as *const u8;
ptr
}
pub fn to_ptr_mut(&mut self) -> *mut u8 {
&mut self.chrs[0] as *mut u8
}
pub unsafe fn from_ptr(mut ptr: *const u8) -> Self {
let mut z = zstr::new();
let mut i = 0;
while *ptr != 0 && i + 1 < N {
z.chrs[i] = *ptr;
ptr = (ptr as usize + 1) as *const u8;
i += 1;
} z.chrs[i] = 0;
z
}
pub fn from_utf16(v: &[u16]) -> Result<Self, Self> {
let mut s = Self::new();
let mut len = 0; let mut buf = [0u8; 4];
for c in char::decode_utf16(v.iter().cloned()) {
if let Ok(c1) = c {
let cbytes = c1.encode_utf8(&mut buf);
let clen = c1.len_utf8();
len += clen;
if len + 1 > N {
s.chrs[len - clen] = 0;
return Err(s);
} else {
s.chrs[len - clen..len].copy_from_slice(&buf[..clen]);
}
} else {
s.chrs[len] = 0;
return Err(s);
}
}
s.chrs[len] = 0;
Ok(s)
} }
impl<const N: usize> core::ops::Deref for zstr<N> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.to_str()
}
}
impl<const N: usize> core::convert::AsRef<str> for zstr<N> {
fn as_ref(&self) -> &str {
self.to_str()
}
}
impl<const N: usize> core::convert::AsMut<str> for zstr<N> {
fn as_mut(&mut self) -> &mut str {
let blen = self.blen();
unsafe { core::str::from_utf8_unchecked_mut(&mut self.chrs[0..blen]) }
}
}
impl<T: AsRef<str> + ?Sized, const N: usize> core::convert::From<&T> for zstr<N> {
fn from(s: &T) -> zstr<N> {
zstr::make(s.as_ref())
}
}
impl<T: AsMut<str> + ?Sized, const N: usize> core::convert::From<&mut T> for zstr<N> {
fn from(s: &mut T) -> zstr<N> {
zstr::make(s.as_mut())
}
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
impl<const N: usize> std::convert::From<std::string::String> for zstr<N> {
fn from(s: std::string::String) -> zstr<N> {
zstr::<N>::make(&s[..])
}
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
impl<const N: usize, const M: usize> std::convert::From<fstr<M>> for zstr<N> {
fn from(s: fstr<M>) -> zstr<N> {
zstr::<N>::make(s.to_str())
}
}
impl<const N: usize, const M: usize> core::convert::From<tstr<M>> for zstr<N> {
fn from(s: tstr<M>) -> zstr<N> {
zstr::<N>::make(s.to_str())
}
}
impl<const N: usize> core::cmp::PartialOrd for zstr<N> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<const N: usize> core::cmp::Ord for zstr<N> {
fn cmp(&self, other: &Self) -> Ordering {
self.chrs[0..self.blen()].cmp(&other.chrs[0..other.blen()])
}
}
impl<const M: usize> zstr<M> {
pub fn resize<const N: usize>(&self) -> zstr<N> {
let slen = self.blen();
let length = if slen + 1 < N {
slen
} else if N == 0 {
0
} else {
N - 1
};
let mut chars = [0u8; N];
chars[..length].clone_from_slice(&self.chrs[..length]);
zstr { chrs: chars }
}
pub fn reallocate<const N: usize>(&self) -> Option<zstr<N>> {
if self.len() < N {
Some(self.resize())
} else {
None
}
}
}
impl<const N: usize> core::fmt::Display for zstr<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.pad(self.to_str())
}
}
impl<const N: usize> PartialEq<&str> for zstr<N> {
fn eq(&self, other: &&str) -> bool {
self.to_str() == *other } }
impl<const N: usize> PartialEq<&str> for &zstr<N> {
fn eq(&self, other: &&str) -> bool {
&self.to_str() == other
} }
impl<'t, const N: usize> PartialEq<zstr<N>> for &'t str {
fn eq(&self, other: &zstr<N>) -> bool {
&other.to_str() == self
}
}
impl<'t, const N: usize> PartialEq<&zstr<N>> for &'t str {
fn eq(&self, other: &&zstr<N>) -> bool {
&other.to_str() == self
}
}
impl<const N: usize> Default for zstr<N> {
fn default() -> Self {
zstr::<N>::make("")
}
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
impl<const N: usize, const M: usize> PartialEq<zstr<N>> for fstr<M> {
fn eq(&self, other: &zstr<N>) -> bool {
other.to_str() == self.to_str()
}
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
impl<const N: usize, const M: usize> PartialEq<fstr<N>> for zstr<M> {
fn eq(&self, other: &fstr<N>) -> bool {
other.to_str() == self.to_str()
}
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no-alloc"))]
impl<const N: usize, const M: usize> PartialEq<&fstr<N>> for zstr<M> {
fn eq(&self, other: &&fstr<N>) -> bool {
other.to_str() == self.to_str()
}
}
impl<const N: usize> core::fmt::Debug for zstr<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.pad(&self.to_str())
}
}
impl<const N: usize> zstr<N> {
pub fn substr(&self, start: usize, end: usize) -> zstr<N> {
let mut chars = [0u8; N];
let mut inds = self.char_indices();
let len = self.len();
let blen = self.blen();
if start >= len || end <= start {
return zstr { chrs: chars };
}
let (si, _) = inds.nth(start).unwrap();
let last = if (end >= len) {
blen
} else {
match inds.nth(end - start - 1) {
Some((ei, _)) => ei,
None => blen,
} }; chars[..last - si].clone_from_slice(&self.chrs[si..last]);
zstr { chrs: chars }
} }
pub type ztr8 = zstr<8>;
pub type ztr16 = zstr<16>;
pub type ztr32 = zstr<32>;
pub type ztr64 = zstr<64>;
pub type ztr128 = zstr<128>;
impl<const N: usize> core::fmt::Write for zstr<N> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
if s.len() + self.len() + 1 > N {
return Err(core::fmt::Error::default());
}
self.push(s);
Ok(())
} }
#[cfg(feature = "experimental")]
mod special_index {
use super::*;
use core::ops::{Range, RangeFrom, RangeFull, RangeTo};
use core::ops::{RangeInclusive, RangeToInclusive};
impl<const N: usize> core::ops::Index<Range<usize>> for zstr<N> {
type Output = str;
fn index(&self, index: Range<usize>) -> &Self::Output {
&self.to_str()[index]
}
} impl<const N: usize> core::ops::Index<RangeTo<usize>> for zstr<N> {
type Output = str;
fn index(&self, index: RangeTo<usize>) -> &Self::Output {
&self.to_str()[index]
}
} impl<const N: usize> core::ops::Index<RangeFrom<usize>> for zstr<N> {
type Output = str;
fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
&self.to_str()[index]
}
} impl<const N: usize> core::ops::Index<RangeInclusive<usize>> for zstr<N> {
type Output = str;
fn index(&self, index: RangeInclusive<usize>) -> &Self::Output {
&self.to_str()[index]
}
} impl<const N: usize> core::ops::Index<RangeToInclusive<usize>> for zstr<N> {
type Output = str;
fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
&self.to_str()[index]
}
} impl<const N: usize> core::ops::Index<RangeFull> for zstr<N> {
type Output = str;
fn index(&self, index: RangeFull) -> &Self::Output {
&self.to_str()[index]
}
}
impl<const N: usize> core::ops::Index<usize> for zstr<N> {
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
&self.chrs[index]
}
}
impl<const N: usize> core::ops::IndexMut<usize> for zstr<N> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let ln = self.blen();
if index >= ln {
panic!("index {} out of range ({})", index, ln);
}
&mut self.chrs[index]
}
} }
impl<const N: usize, TA: AsRef<str>> Add<TA> for zstr<N> {
type Output = zstr<N>;
fn add(self, other: TA) -> zstr<N> {
let mut a2 = self;
a2.push(other.as_ref());
a2
}
}
impl<const N: usize> Add<&zstr<N>> for &str {
type Output = zstr<N>;
fn add(self, other: &zstr<N>) -> zstr<N> {
let mut a2 = zstr::from(self);
a2.push(other);
a2
}
}
impl<const N: usize> Add<zstr<N>> for &str {
type Output = zstr<N>;
fn add(self, other: zstr<N>) -> zstr<N> {
let mut a2 = zstr::from(self);
a2.push(&other);
a2
}
}
impl<const N: usize> core::hash::Hash for zstr<N> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_ref().hash(state);
}
}
impl<const N: usize> core::cmp::PartialEq for zstr<N> {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl<const N: usize> core::str::FromStr for zstr<N> {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() < N {
Ok(zstr::from(s))
} else {
Err("capacity exceeded")
}
}
}
#[cfg(feature = "experimental")]
pub struct ChunkyIter<'t, const N:usize, const CS:usize> {
bur : &'t [u8;N],
index : usize,
}
#[cfg(feature = "experimental")]
impl<'t, const N:usize, const CS:usize> Iterator for ChunkyIter<'t,N,CS> {
type Item = &'t [u8];
fn next(&mut self) -> Option<Self::Item> {
if CS==0 || self.index + 1 > N || self.bur[self.index]==0 { None }
else {
self.index += CS;
Some(&self.bur[self.index-CS .. min(N,self.index)])
}
}}
#[cfg(feature = "experimental")]
impl<const N:usize> zstr<N> {
pub fn chunky_iter<'t,const CS:usize>(&'t self) -> ChunkyIter<'t,N,CS> {
ChunkyIter {
bur : &self.chrs,
index : 0,
}
}}