| 1 | // The libc crate on Windows doesn't have snprintf |
| 2 | #![cfg(not(windows))] |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::{TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#, TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#}; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::rawrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/::c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#; |
| 8 | |
| 9 | use libcrust-analyzer cargo libc 0.2.147 crate/::snprintfrust-analyzer cargo libc 0.2.147 snprintf().; |
| 10 | |
| 11 | use sprintfrust-analyzer cargo sprintf 0.4.2 crate/::*; |
| 12 | |
| 13 | fn check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().<T: Printfrust-analyzer cargo sprintf 0.4.2 format/Printf#>(fmt: &str, arg: T) { |
| 14 | let our_result = sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!(fmt, arg).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 15 | let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0_u8; our_result.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1]; |
| 16 | let cfmt = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(fmt).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 17 | let clen: usize = unsafe { |
| 18 | snprintfrust-analyzer cargo libc 0.2.147 snprintf().( |
| 19 | buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().() as *mut c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#, |
| 20 | buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 21 | cfmt.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), |
| 22 | arg, |
| 23 | ) |
| 24 | } |
| 25 | .try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() |
| 26 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 27 | buf.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(clen); // drop the final '\0', etc. |
| 28 | let c_result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 29 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(our_result, c_result); |
| 30 | } |
| 31 | |
| 32 | fn check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().(fmt: &str, arg: &str) { |
| 33 | let our_result = sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!(fmt, arg).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 34 | let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0_u8; our_result.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1]; |
| 35 | let cfmt = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(fmt).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 36 | let carg = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(arg).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 37 | let clen: usize = unsafe { |
| 38 | snprintfrust-analyzer cargo libc 0.2.147 snprintf().( |
| 39 | buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().() as *mut c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#, |
| 40 | buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 41 | cfmt.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), |
| 42 | carg.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), |
| 43 | ) |
| 44 | } |
| 45 | .try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() |
| 46 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 47 | buf.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(clen); // drop the final '\0', etc. |
| 48 | let c_result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 49 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(our_result, c_result); |
| 50 | } |
| 51 | |
| 52 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 53 | fn test_intrust-analyzer cargo sprintf 0.4.2 test_int().() { |
| 54 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%d", 12); |
| 55 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("~%d~", 148); |
| 56 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("00%dxx", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().91232); |
| 57 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%x", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().9232); |
| 58 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%X", 432); |
| 59 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%09X", 432); |
| 60 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%9X", 432); |
| 61 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%+9X", 492); |
| 62 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("% #9x", 4589); |
| 63 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%2o", 4); |
| 64 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("% 12d", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().4); |
| 65 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("% 12d", 48); |
| 66 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%ld", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().4_i64); |
| 67 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%lX", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().4_i64); |
| 68 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%ld", 48_i64); |
| 69 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%-8hd", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][Neg]neg().12_i16); |
| 70 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%llx", 0x0123456789abcdef_u64); |
| 71 | } |
| 72 | |
| 73 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 74 | fn test_floatrust-analyzer cargo sprintf 0.4.2 test_float().() { |
| 75 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%f", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().46.38); |
| 76 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%012.3f", 1.2); |
| 77 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%012.3e", 1.7); |
| 78 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%e", 1e300); |
| 79 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%012.3g%%!", 2.6); |
| 80 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%012.5G", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2.69); |
| 81 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%+7.4f", 42.785); |
| 82 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("{}% 7.4E", 493.12); |
| 83 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("% 7.4E", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().120.3); |
| 84 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%-10F", f64::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]INFINITY.); |
| 85 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%+010F", f64::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]INFINITY.); |
| 86 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1f", 999.99); |
| 87 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1f", 9.99); |
| 88 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1e", 9.99); |
| 89 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.2f", 9.99); |
| 90 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.2e", 9.99); |
| 91 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.3f", 9.99); |
| 92 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.3e", 9.99); |
| 93 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1g", 9.99); |
| 94 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1G", 9.99); |
| 95 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1f", 2.99); |
| 96 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1e", 2.99); |
| 97 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1g", 2.99); |
| 98 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1f", 2.599); |
| 99 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1e", 2.599); |
| 100 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%.1g", 2.599); |
| 101 | |
| 102 | // MacOS libc behaves differently from glibc for nan. glibc is the reference implementation. |
| 103 | if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_env = "gnu") { |
| 104 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("% f", f64::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]NAN.); |
| 105 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%+f", f64::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]NAN.); |
| 106 | } else { |
| 107 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("% f", f64::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]NAN.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), " nan"); |
| 108 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%+f", f64::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]NAN.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "+nan"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 113 | fn test_strrust-analyzer cargo sprintf 0.4.2 test_str().() { |
| 114 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("test %% with string: %s yay\n", "FOO"); |
| 115 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().( |
| 116 | "%s", |
| 117 | "testing with a slightly longer string to make sure it doesn't truncate", |
| 118 | ); |
| 119 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("test char %c", '~'); |
| 120 | let c_string = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().("test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 121 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%s", c_string.as_c_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]as_c_str().()); |
| 122 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%s", c_string); |
| 123 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%4s", "A"); |
| 124 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%4s", "ð’€€"); // multi-byte character test (4 bytes) |
| 125 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%-4sX", "A"); |
| 126 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%-4sX", "ð’€€"); // multi-byte character test (4 bytes) |
| 127 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%1.3s", "ABCDEFG"); |
| 128 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%1.4s", "𒀀𒀀"); // multi-byte character test (4 bytes per char) |
| 129 | check_fmt_srust-analyzer cargo sprintf 0.4.2 check_fmt_s().("%8.4s", "ABCDEFG"); |
| 130 | |
| 131 | // glibc does not handle UTF-8 strings correctly when truncating, but we cannot produce malformed UTF-8 |
| 132 | // strings in Rust. Instead, we round down to the nearest character boundary. |
| 133 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.1s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), " "); |
| 134 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.2s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), " "); |
| 135 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.3s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), " "); |
| 136 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.4s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "𒀀"); |
| 137 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.5s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "𒀀"); |
| 138 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.6s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "𒀀"); |
| 139 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.7s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "𒀀"); |
| 140 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%1.8s", "𒀀𒀀𒀀").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "𒀀𒀀"); |
| 141 | } |
| 142 | |
| 143 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 144 | fn test_charrust-analyzer cargo sprintf 0.4.2 test_char().() { |
| 145 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%c", 'x'); |
| 146 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%c", b'x'); |
| 147 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%c", b'x' as c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#); |
| 148 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%c", u16::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/convert/impl#[u16][`TryFrom<char>`]try_from().('x').unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 149 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%c", u32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryFrom<U>`]try_from().('x').unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 150 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%4c", 'A'); |
| 151 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%-4cX", 'A'); |
| 152 | } |
| 153 | |
| 154 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 155 | fn test_sanityrust-analyzer cargo sprintf 0.4.2 test_sanity().() { |
| 156 | // u8 must not misinterpret bytes from multi-byte UTF-8 characters |
| 157 | let bytes = "∆".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 158 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 1); |
| 159 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sprintfrust-analyzer cargo sprintf 0.4.2 sprintf!!("%c", bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PrintfErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#::WrongTyperust-analyzer cargo sprintf 0.4.2 PrintfError#WrongType#)); |
| 160 | } |
| 161 | |
| 162 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 163 | fn test_ptrrust-analyzer cargo sprintf 0.4.2 test_ptr().() { |
| 164 | let buf: [u8; 4] = [0; 4]; |
| 165 | let ptr_const: *const u8 = buf.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().(); |
| 166 | let ptr_mut: *mut u8 = ptr_const.cast_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast_mut().(); |
| 167 | |
| 168 | // pointer: expects usize and pointer to have the same size |
| 169 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<usize>(), size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<*const u8>(),); |
| 170 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%p", ptr_const); |
| 171 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%p", ptr_mut); |
| 172 | |
| 173 | // numeric: works the same as libc if you use the correct length specifier |
| 174 | if size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<usize>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<u64>() { |
| 175 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%llx", ptr_const); |
| 176 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%llx", ptr_mut); |
| 177 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%#llx", ptr_const); |
| 178 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%#llx", ptr_mut); |
| 179 | } else if size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<usize>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<u32>() { |
| 180 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%x", ptr_const); |
| 181 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%x", ptr_mut); |
| 182 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%#x", ptr_const); |
| 183 | check_fmtrust-analyzer cargo sprintf 0.4.2 check_fmt().("%#x", ptr_mut); |
| 184 | } |
| 185 | } |