Skip to content

Commit

Permalink
Merge pull request #12 from dgarrett/dylan/fix-nulls
Browse files Browse the repository at this point in the history
Create a slice of appropriate size so that nuls aren't logged
  • Loading branch information
rodrimati1992 authored Sep 27, 2024
2 parents defe77f + 70ea236 commit 58e8143
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
37 changes: 4 additions & 33 deletions src/array_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{utils::RangedBytes, FmtArg, PanicFmt, PanicVal};
use crate::{
utils::{bytes_up_to, RangedBytes},
FmtArg, PanicFmt, PanicVal,
};

use core::{
cmp::PartialEq,
Expand Down Expand Up @@ -288,35 +291,3 @@ impl<const CAP: usize> TinyString<CAP> {
}
}
}

////////////////////////////////////////////////////////////////////////////////

#[cfg(not(feature = "rust_1_64"))]
const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
let mut to_truncate = buffer.len() - upto;
let mut out: &[u8] = buffer;

while to_truncate != 0 {
if let [rem @ .., _] = out {
out = rem;
}
to_truncate -= 1;
}

if out.len() != upto {
panic!("BUG!")
}

out
}

#[cfg(feature = "rust_1_64")]
const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
if upto > buffer.len() {
return buffer;
}

// SAFETY: the above conditional ensures that `upto` doesn't
// create a partially-dangling slice
unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
}
5 changes: 3 additions & 2 deletions src/concat_panic_.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
fmt::FmtKind,
panic_val::{PanicClass, PanicVal, StrFmt},
utils::{string_cap, WasTruncated},
utils::{bytes_up_to, string_cap, WasTruncated},
};

/// Panics by concatenating the argument slice.
Expand Down Expand Up @@ -258,7 +258,8 @@ const fn panic_inner<const LEN: usize>(args: &[&[PanicVal<'_>]]) -> Result<Never
}

unsafe {
let str = core::str::from_utf8_unchecked(&buffer);
let buffer = bytes_up_to(&buffer, len);
let str = core::str::from_utf8_unchecked(buffer);
panic!("{}", str)
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,35 @@ pub(crate) const fn tail_byte_array<const TO: usize>(
bytes,
}
}

////////////////////////////////////////////////////////////////////////////////

#[cfg(not(feature = "rust_1_64"))]
pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
let mut to_truncate = buffer.len() - upto;
let mut out: &[u8] = buffer;

while to_truncate != 0 {
if let [rem @ .., _] = out {
out = rem;
}
to_truncate -= 1;
}

if out.len() != upto {
panic!("BUG!")
}

out
}

#[cfg(feature = "rust_1_64")]
pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
if upto > buffer.len() {
return buffer;
}

// SAFETY: the above conditional ensures that `upto` doesn't
// create a partially-dangling slice
unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
}

0 comments on commit 58e8143

Please sign in to comment.