Remove additional dead code

This commit is contained in:
Peter Ammon 2024-06-29 16:31:00 -07:00
parent 1ed256d328
commit 6b4dbf3b05
No known key found for this signature in database
3 changed files with 1 additions and 235 deletions

View file

@ -1084,10 +1084,6 @@ pub fn has_working_tty_timestamps() -> bool {
/// empty string. /// empty string.
pub static EMPTY_STRING: WString = WString::new(); pub static EMPTY_STRING: WString = WString::new();
/// A global, empty string list. This is useful for functions which wish to return a reference
/// to an empty string.
pub static EMPTY_STRING_LIST: Vec<WString> = vec![];
/// A function type to check for cancellation. /// A function type to check for cancellation.
/// Return true if execution should cancel. /// Return true if execution should cancel.
/// todo!("Maybe remove the box? It is only needed for get_bg_context.") /// todo!("Maybe remove the box? It is only needed for get_bg_context.")
@ -1279,154 +1275,6 @@ pub fn should_suppress_stderr_for_tests() -> bool {
.unwrap_or_default() .unwrap_or_default()
} }
/// Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer.
pub fn format_size(mut sz: i64) -> WString {
let mut result = WString::new();
const sz_names: [&str; 8] = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if sz < 0 {
result += "unknown";
} else if sz == 0 {
result += wgettext!("empty");
} else if sz < 1024 {
result += &sprintf!("%lldB", sz)[..];
} else {
for (i, sz_name) in sz_names.iter().enumerate() {
if sz < (1024 * 1024) || i == sz_names.len() - 1 {
let isz = sz / 1024;
if isz > 9 {
result += &sprintf!("%lld%ls", isz, *sz_name)[..];
} else {
result += &sprintf!("%.1f%ls", sz as f64 / 1024.0, *sz_name)[..];
}
break;
}
sz /= 1024;
}
}
result
}
/// Version of format_size that does not allocate memory.
pub fn format_size_safe(buff: &mut [u8; 128], mut sz: u64) {
let buff_size = 128;
let max_len = buff_size - 1; // need to leave room for a null terminator
buff.fill(0);
let mut idx = 0;
const sz_names: [&str; 8] = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if sz == 0 {
let empty = "empty".as_bytes();
buff[..empty.len()].copy_from_slice(empty);
} else if sz < 1024 {
append_ull(buff, &mut sz, &mut idx, max_len);
append_str(buff, "B", &mut idx, max_len);
} else {
for (i, sz_name) in sz_names.iter().enumerate() {
if sz < (1024 * 1024) || i == sz_names.len() - 1 {
let mut isz = sz / 1024;
append_ull(buff, &mut isz, &mut idx, max_len);
if isz <= 9 {
// Maybe append a single fraction digit.
let mut remainder = sz % 1024;
if remainder > 0 {
let tmp = [b'.', extract_most_significant_digit(&mut remainder)];
let tmp = std::str::from_utf8(&tmp).unwrap();
append_str(buff, tmp, &mut idx, max_len);
}
}
append_str(buff, sz_name, &mut idx, max_len);
break;
}
sz /= 1024;
}
}
}
/// Writes out a long safely.
pub fn format_llong_safe<CharT: From<u8>, I64>(buff: &mut [CharT; 64], val: I64)
where
i64: From<I64>,
{
let val = i64::from(val);
let uval = val.unsigned_abs();
if val >= 0 {
format_safe_impl(buff, 64, uval);
} else {
buff[0] = CharT::from(b'-');
format_safe_impl(&mut buff[1..], 63, uval);
}
}
pub fn format_ullong_safe<CharT: From<u8>>(buff: &mut [CharT; 64], val: u64) {
format_safe_impl(buff, 64, val);
}
fn format_safe_impl<CharT: From<u8>>(buff: &mut [CharT], size: usize, mut val: u64) {
let mut idx = 0;
if val == 0 {
buff[idx] = CharT::from(b'0');
idx += 1;
} else {
// Generate the string backwards, then reverse it.
while val != 0 {
buff[idx] = CharT::from((val % 10) as u8 + b'0');
idx += 1;
val /= 10;
}
buff[..idx].reverse();
}
buff[idx] = CharT::from(b'\0');
idx += 1;
assert!(idx <= size, "Buffer overflowed");
}
fn append_ull(buff: &mut [u8], val: &mut u64, inout_idx: &mut usize, max_len: usize) {
let mut idx = *inout_idx;
while *val > 0 && idx < max_len {
buff[idx] = extract_most_significant_digit(val);
idx += 1;
}
*inout_idx = idx;
}
fn append_str(buff: &mut [u8], s: &str, inout_idx: &mut usize, max_len: usize) {
let mut idx = *inout_idx;
let mut bytes = s.as_bytes();
while !bytes.is_empty() && idx < max_len {
buff[idx] = bytes[0];
idx += 1;
bytes = &bytes[1..];
}
*inout_idx = idx;
}
/// Crappy function to extract the most significant digit of an unsigned long long value.
fn extract_most_significant_digit(xp: &mut u64) -> u8 {
let mut place_value = 1;
let mut x = *xp;
while x >= 10 {
x /= 10;
place_value *= 10;
}
*xp -= place_value * x;
x as u8 + b'0'
}
/// "Narrows" a wide character string. This just grabs any ASCII characters and truncates.
pub fn narrow_string_safe(buff: &mut [u8; 64], s: &wstr) {
let mut idx = 0;
for c in s.chars() {
if c as u32 <= 127 {
buff[idx] = c as u8;
idx += 1;
if idx + 1 == 64 {
break;
}
}
}
buff[idx] = b'\0';
}
/// Stored in blocks to reference the file which created the block. /// Stored in blocks to reference the file which created the block.
pub type FilenameRef = Arc<WString>; pub type FilenameRef = Arc<WString>;
@ -2158,12 +2006,6 @@ macro_rules! fwprintf {
}; };
} }
#[allow(unused_macros)]
#[deprecated = "use printf!"]
pub fn fputws(_s: &wstr, _fd: RawFd) {
panic!()
}
// test-only // test-only
#[allow(unused_macros)] #[allow(unused_macros)]
#[deprecated = "use printf!"] #[deprecated = "use printf!"]

View file

@ -2261,23 +2261,6 @@ fn expand_command_token(ctx: &OperationContext<'_>, cmd_tok: &mut WString) -> bo
) )
} }
/// Create a new completion entry.
///
/// \param completions The array of completions to append to
/// \param comp The completion string
/// \param desc The description of the completion
/// \param flags completion flags
#[deprecated = "Use Vec::push()"]
pub fn append_completion(
completions: &mut Vec<Completion>,
comp: WString,
desc: WString,
flags: CompleteFlags,
r#match: StringFuzzyMatch,
) {
completions.push(Completion::new(comp, desc, r#match, flags))
}
/// Add an unexpanded completion "rule" to generate completions from for a command. /// Add an unexpanded completion "rule" to generate completions from for a command.
/// ///
/// # Examples /// # Examples

View file

@ -1,7 +1,4 @@
use crate::common::{ use crate::common::{scoped_push, truncate_at_nul, ScopeGuard, ScopeGuarding};
cstr2wcstring, format_llong_safe, format_size_safe, scoped_push, truncate_at_nul, ScopeGuard,
ScopeGuarding,
};
use crate::wchar::prelude::*; use crate::wchar::prelude::*;
#[test] #[test]
@ -71,62 +68,6 @@ fn test_scope_guard_consume() {
assert_eq!(obj.value, "nu"); assert_eq!(obj.value, "nu");
} }
#[test]
fn test_format() {
// Testing formatting functions
struct Test {
val: u64,
expected: &'static str,
}
let tests = [
Test {
val: 0,
expected: "empty",
},
Test {
val: 1,
expected: "1B",
},
Test {
val: 2,
expected: "2B",
},
Test {
val: 1024,
expected: "1kB",
},
Test {
val: 1870,
expected: "1.8kB",
},
Test {
val: 4322911,
expected: "4.1MB",
},
];
for test in tests {
let mut buff = [0_u8; 128];
format_size_safe(&mut buff, test.val);
assert_eq!(cstr2wcstring(&buff), WString::from_str(test.expected));
}
for j in -129..=129 {
let mut buff1 = [0_u8; 64];
let mut buff2 = [0_u8; 64];
format_llong_safe(&mut buff1, j);
unsafe { libc::snprintf(buff2.as_mut_ptr().cast(), 64, "%d\0".as_ptr().cast(), j) };
assert_eq!(cstr2wcstring(&buff1), cstr2wcstring(&buff2));
}
let q = i64::MIN;
let mut buff1 = [0_u8; 64];
let mut buff2 = [0_u8; 64];
format_llong_safe(&mut buff1, q);
unsafe { libc::snprintf(buff2.as_mut_ptr().cast(), 64, "%lld\0".as_ptr().cast(), q) };
assert_eq!(cstr2wcstring(&buff1), cstr2wcstring(&buff2));
}
#[test] #[test]
fn test_truncate_at_nul() { fn test_truncate_at_nul() {
assert_eq!(truncate_at_nul(L!("abc\0def")), L!("abc")); assert_eq!(truncate_at_nul(L!("abc\0def")), L!("abc"));