authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-10-31 06:11:08-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-10-31 21:37:35-04:00
log149200aac5f7f78fbb3427e3a9445c80efd2116b
tree7da99fec70dc828341d22d4f4d3e5a4720e8892a
parentd890e817610dd75feef55c1f7983190852c622a5

mem: delete `readIntSlice` and `writeIntSlice`

After deleting all potentially incorrect usages, there were no more remaining.

1 files changed, 0 insertions(+), 171 deletions(-)

lib/std/mem.zig-171
...@@ -1612,17 +1612,6 @@ test readInt {...@@ -1612,17 +1612,6 @@ test readInt {
1612 try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .Little) == -4);1612 try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .Little) == -4);
1613}1613}
16141614
1615/// Asserts that buffer.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0
1616/// and ignores extra bytes.
1617/// The bit count of T must be evenly divisible by 8.
1618pub inline fn readIntSlice(comptime T: type, buffer: []const u8, endian: Endian) T {
1619 const byte_len = @divExact(@typeInfo(T).Int.bits, 8);
1620 return switch (endian) {
1621 .Little => readInt(T, buffer[0..byte_len], endian),
1622 .Big => readInt(T, buffer[buffer.len - byte_len ..], endian),
1623 };
1624}
1625
1626fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {1615fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {
1627 const uN = std.meta.Int(.unsigned, @bitSizeOf(T));1616 const uN = std.meta.Int(.unsigned, @bitSizeOf(T));
1628 const Log2N = std.math.Log2Int(T);1617 const Log2N = std.math.Log2Int(T);
...@@ -1760,166 +1749,6 @@ test writeInt {...@@ -1760,166 +1749,6 @@ test writeInt {
1760 try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff }));1749 try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff }));
1761}1750}
17621751
1763/// Writes a twos-complement integer to memory, with the specified endianness.
1764/// Asserts that buf.len >= @typeInfo(T).Int.bits / 8.
1765/// The bit count of T must be evenly divisible by 8.
1766/// Any extra bytes in buffer not part of the integer are set to zero, with
1767/// respect to endianness. To avoid the branch to check for extra buffer bytes,
1768/// use writeInt instead.
1769pub inline fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: Endian) void {
1770 const byte_len = @divExact(@typeInfo(T).Int.bits, 8);
1771 switch (endian) {
1772 .Little => {
1773 writeInt(T, buffer[0..byte_len], value, endian);
1774 @memset(buffer[byte_len..], 0);
1775 },
1776 .Big => {
1777 @memset(buffer[0 .. buffer.len - byte_len], 0);
1778 writeInt(T, buffer[buffer.len - byte_len ..][0..byte_len], value, endian);
1779 },
1780 }
1781}
1782
1783test writeIntSlice {
1784 try testWriteIntImpl();
1785 try comptime testWriteIntImpl();
1786}
1787fn testWriteIntImpl() !void {
1788 var bytes: [8]u8 = undefined;
1789
1790 writeIntSlice(u0, bytes[0..], 0, .Big);
1791 try testing.expect(eql(u8, &bytes, &[_]u8{
1792 0x00, 0x00, 0x00, 0x00,
1793 0x00, 0x00, 0x00, 0x00,
1794 }));
1795
1796 writeIntSlice(u0, bytes[0..], 0, .Little);
1797 try testing.expect(eql(u8, &bytes, &[_]u8{
1798 0x00, 0x00, 0x00, 0x00,
1799 0x00, 0x00, 0x00, 0x00,
1800 }));
1801
1802 writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, .Big);
1803 try testing.expect(eql(u8, &bytes, &[_]u8{
1804 0x12,
1805 0x34,
1806 0x56,
1807 0x78,
1808 0xCA,
1809 0xFE,
1810 0xBA,
1811 0xBE,
1812 }));
1813
1814 writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, .Little);
1815 try testing.expect(eql(u8, &bytes, &[_]u8{
1816 0x12,
1817 0x34,
1818 0x56,
1819 0x78,
1820 0xCA,
1821 0xFE,
1822 0xBA,
1823 0xBE,
1824 }));
1825
1826 writeIntSlice(u32, bytes[0..], 0x12345678, .Big);
1827 try testing.expect(eql(u8, &bytes, &[_]u8{
1828 0x00,
1829 0x00,
1830 0x00,
1831 0x00,
1832 0x12,
1833 0x34,
1834 0x56,
1835 0x78,
1836 }));
1837
1838 writeIntSlice(u32, bytes[0..], 0x78563412, .Little);
1839 try testing.expect(eql(u8, &bytes, &[_]u8{
1840 0x12,
1841 0x34,
1842 0x56,
1843 0x78,
1844 0x00,
1845 0x00,
1846 0x00,
1847 0x00,
1848 }));
1849
1850 writeIntSlice(u16, bytes[0..], 0x1234, .Big);
1851 try testing.expect(eql(u8, &bytes, &[_]u8{
1852 0x00,
1853 0x00,
1854 0x00,
1855 0x00,
1856 0x00,
1857 0x00,
1858 0x12,
1859 0x34,
1860 }));
1861
1862 writeIntSlice(u16, bytes[0..], 0x1234, .Little);
1863 try testing.expect(eql(u8, &bytes, &[_]u8{
1864 0x34,
1865 0x12,
1866 0x00,
1867 0x00,
1868 0x00,
1869 0x00,
1870 0x00,
1871 0x00,
1872 }));
1873
1874 writeIntSlice(i16, bytes[0..], @as(i16, -21555), .Little);
1875 try testing.expect(eql(u8, &bytes, &[_]u8{
1876 0xCD,
1877 0xAB,
1878 0x00,
1879 0x00,
1880 0x00,
1881 0x00,
1882 0x00,
1883 0x00,
1884 }));
1885
1886 writeIntSlice(i16, bytes[0..], @as(i16, -21555), .Big);
1887 try testing.expect(eql(u8, &bytes, &[_]u8{
1888 0x00,
1889 0x00,
1890 0x00,
1891 0x00,
1892 0x00,
1893 0x00,
1894 0xAB,
1895 0xCD,
1896 }));
1897
1898 writeIntSlice(u8, bytes[0..], 0x12, .Big);
1899 try testing.expect(eql(u8, &bytes, &[_]u8{
1900 0x00, 0x00, 0x00, 0x00,
1901 0x00, 0x00, 0x00, 0x12,
1902 }));
1903
1904 writeIntSlice(u8, bytes[0..], 0x12, .Little);
1905 try testing.expect(eql(u8, &bytes, &[_]u8{
1906 0x12, 0x00, 0x00, 0x00,
1907 0x00, 0x00, 0x00, 0x00,
1908 }));
1909
1910 writeIntSlice(i8, bytes[0..], -1, .Big);
1911 try testing.expect(eql(u8, &bytes, &[_]u8{
1912 0x00, 0x00, 0x00, 0x00,
1913 0x00, 0x00, 0x00, 0xff,
1914 }));
1915
1916 writeIntSlice(i8, bytes[0..], -1, .Little);
1917 try testing.expect(eql(u8, &bytes, &[_]u8{
1918 0xff, 0x00, 0x00, 0x00,
1919 0x00, 0x00, 0x00, 0x00,
1920 }));
1921}
1922
1923fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void {1752fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void {
1924 const uN = std.meta.Int(.unsigned, @bitSizeOf(T));1753 const uN = std.meta.Int(.unsigned, @bitSizeOf(T));
1925 const Log2N = std.math.Log2Int(T);1754 const Log2N = std.math.Log2Int(T);