Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty set for strings #23

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub fn open() -> SysResult<()> {
open_for(ptr::null_mut())
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
//clippy is retard
#[inline]
///Opens clipboard associating it with specified window handle.
///
Expand Down Expand Up @@ -275,6 +277,7 @@ pub fn set(format: u32, data: &[u8]) -> SysResult<()> {
pub fn set_without_clear(format: u32, data: &[u8]) -> SysResult<()> {
let size = data.len();
if size == 0 {
#[allow(clippy::unit_arg)]
return Ok(unlikely_empty_size_result());
}

Expand Down Expand Up @@ -330,15 +333,12 @@ pub fn get_string(out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
///Copies unicode string onto clipboard, performing necessary conversions, returning true on
///success.
pub fn set_string(data: &str) -> SysResult<()> {
if data.is_empty() {
return Ok(unlikely_empty_size_result());
}

let size = unsafe {
MultiByteToWideChar(CP_UTF8, 0, data.as_ptr() as *const _, data.len() as _, ptr::null_mut(), 0)
};

if size != 0 {
//MultiByteToWideChar fails on empty input, but we can ignore it and just set buffer with null char
if size != 0 || data.is_empty() {
let mem = RawMem::new_global_mem((mem::size_of::<u16>() * (size as usize + 1)) as _)?;
{
let (ptr, _lock) = mem.lock()?;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ fn should_work_with_bytes() {
assert_eq!(format!("{0}{0}", text), output);
}

fn should_work_with_set_empty_string() {
let text = "";

let _clip = Clipboard::new_attempts(10).expect("Open clipboard");

Unicode.write_clipboard(&text).expect("Write text");

let mut output = String::new();

assert_eq!(Unicode.read_clipboard(&mut output).expect("Read text"), text.len());
assert_eq!(text, output);
}

extern "system" {
fn GetConsoleWindow() -> winapi::shared::windef::HWND;
}
Expand Down Expand Up @@ -106,5 +119,6 @@ fn clipboard_should_work() {
assert!(is_format_avail(CF_UNICODETEXT));
run!(should_work_with_wide_string);
run!(should_work_with_bytes);
run!(should_work_with_set_empty_string);
run!(should_set_owner);
}