Skip to content

Commit

Permalink
Merge pull request #13 from rodrimati1992/trim_nul_release
Browse files Browse the repository at this point in the history
0.2.10 version
  • Loading branch information
rodrimati1992 authored Sep 27, 2024
2 parents 58e8143 + d53db27 commit 23b8eaf
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "const_panic"
version = "0.2.9"
version = "0.2.10"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2021"
license = "Zlib"
Expand Down
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ This changelog is a summary of the changes made in each release.

# 0.2

### 0.2.10

[Fixed internal use of the panic macro to not append nul bytes to the output](<https://github.com/rodrimati1992/const_panic/pull/12>).

Added `const_panic::utils::bytes_up_to` function.


### 0.2.7

Added `concat_` macro, which requires `"non_basic"` feature.
Expand Down
2 changes: 1 addition & 1 deletion src/fmt/char_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const fn hex_as_ascii(n: u8) -> u8 {
}
}

#[cfg(any(test, feature = "fmt"))]
#[cfg(test)]
pub(crate) const fn char_debug_len(c: char) -> usize {
let inner = match c {
'\t' | '\r' | '\n' | '\\' | '\'' => 2,
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ mod panic_val;
#[cfg(feature = "non_basic")]
mod const_default;

#[cfg(not(feature = "non_basic"))]
mod utils;

#[cfg(feature = "non_basic")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
pub mod utils;

#[cfg(all(test, not(feature = "test")))]
Expand Down
66 changes: 45 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod utils_1_64_tests;
mod non_basic_utils;

#[cfg(feature = "non_basic")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
pub use self::non_basic_utils::*;

pub(crate) const fn min_usize(l: usize, r: usize) -> usize {
Expand Down Expand Up @@ -231,32 +232,55 @@ pub(crate) const fn tail_byte_array<const TO: usize>(

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

#[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;
/// Const equivalent of `&buffer[..upto]` with saturating indexing.
///
/// "saturating indexing" means that if `upto > buffer.len()`,
/// then this returns all of `buffer` instead of panicking.
///
/// # Example
///
/// ```rust
/// use const_panic::utils::bytes_up_to;
///
/// const BYTES: &[u8] = &[3, 5, 8, 13, 21, 34, 55, 89];
///
/// const SLICE: &[u8] = bytes_up_to(BYTES, 4);
/// assert_eq!(SLICE, &[3, 5, 8, 13][..]);
///
/// const WHOLE: &[u8] = bytes_up_to(BYTES, usize::MAX);
/// assert_eq!(WHOLE, &[3, 5, 8, 13, 21, 34, 55, 89][..]);
///
/// ```
pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
if upto > buffer.len() {
return buffer;
}

if out.len() != upto {
panic!("BUG!")
}
#[cfg(not(feature = "rust_1_64"))]
{
let mut to_truncate = buffer.len() - upto;
let mut out: &[u8] = buffer;

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

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

out
}

// SAFETY: the above conditional ensures that `upto` doesn't
// create a partially-dangling slice
unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
#[cfg(feature = "rust_1_64")]
{
// SAFETY: the above conditional ensures that `upto` doesn't
// create a partially-dangling slice
unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
}
}

2 changes: 2 additions & 0 deletions tests/main_test_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ mod main_tests {
mod pvcount_tests;

mod string_tests;

mod utils_tests;
}
34 changes: 34 additions & 0 deletions tests/main_tests/utils_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use const_panic::utils::bytes_up_to;


macro_rules! case {
($bytes:expr, $upto:expr) => ({
const SLICE: &[u8] = bytes_up_to($bytes, $upto);
assert_eq!(slice, );
})
}


#[test]
fn test_bytes_up_to_isconst() {
const SLICE: &[u8] = bytes_up_to(&[10, 20], 1);

assert_eq!(SLICE, &[10][..]);
}

#[test]
fn test_bytes_up_to() {
const BYTES: &[u8] = &[3, 5, 8, 13, 21, 34];

let iter = (0..=BYTES.len() + 2).chain([usize::MAX - 1, usize::MAX]);

for bytes_len in iter.clone() {
let bytes = BYTES.get(..bytes_len).unwrap_or(BYTES);
for upto in iter.clone() {
assert_eq!(
bytes_up_to(bytes, upto),
bytes.get(..upto).unwrap_or(bytes),
)
}
}
}

0 comments on commit 23b8eaf

Please sign in to comment.