#[cfg(target_os = "windows")]
#[allow(unsafe_code)]
#[path = "windows.rs"]
mod os;
#[cfg(target_os = "macos")]
#[allow(unsafe_code)]
#[path = "macos.rs"]
mod os;
#[cfg(target_os = "ios")]
#[allow(unsafe_code)]
#[path = "ios.rs"]
mod os;
#[cfg(target_arch = "wasm32")]
mod os {
pub fn text() -> Option<String> {
crate::platform::backend::web::clipboard_read()
}
pub fn set_text(text: &str) -> bool {
crate::platform::backend::web::clipboard_write(text);
true
}
}
#[cfg(target_os = "android")]
mod os {
pub fn text() -> Option<String> {
crate::platform::backend::android::clipboard_text()
}
pub fn set_text(text: &str) -> bool {
crate::platform::backend::android::set_clipboard_text(text)
}
}
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32",
target_os = "android"
)))]
mod os {
pub fn text() -> Option<String> {
None
}
pub fn set_text(_text: &str) -> bool {
false
}
}
pub fn text() -> Option<String> {
os::text()
}
pub fn set_text(text: &str) -> bool {
os::set_text(text)
}
#[cfg_attr(not(any(target_os = "windows", test)), allow(dead_code))]
fn to_crlf(text: &str) -> String {
let mut out = String::with_capacity(text.len() + text.len() / 16);
let mut prev = '\0';
for c in text.chars() {
if c == '\n' && prev != '\r' {
out.push('\r');
}
out.push(c);
prev = c;
}
out
}
#[cfg_attr(not(any(target_os = "windows", test)), allow(dead_code))]
fn from_crlf(text: &str) -> String {
text.replace("\r\n", "\n")
}
#[cfg(test)]
mod tests {
use super::{from_crlf, to_crlf};
#[test]
fn line_endings_round_trip() {
assert_eq!(to_crlf("a\nb\r\nc"), "a\r\nb\r\nc");
assert_eq!(from_crlf("a\r\nb\rc"), "a\nb\rc");
for s in ["", "one", "a\nb", "\n\n", "x\r\ny"] {
assert_eq!(from_crlf(&to_crlf(s)), s.replace("\r\n", "\n"));
}
}
}