101101Base. getindex (v:: MemoryView , :: Colon ) = v
102102Base. @propagate_inbounds Base. view (v:: MemoryView , idx:: AbstractUnitRange ) = v[idx]
103103
104+ # Efficient way to get `mem[1:include_last]`.
105+ # include_last must be in 0:length(mem)
104106function truncate (mem:: MemoryView , include_last:: Integer )
105107 lst = Int (include_last):: Int
106108 @boundscheck if (lst % UInt) > length (mem) % UInt
@@ -109,6 +111,8 @@ function truncate(mem::MemoryView, include_last::Integer)
109111 typeof (mem)(unsafe, mem. ref, lst)
110112end
111113
114+ # Efficient way to get `mem[from:end]`.
115+ # From must be in 1:length(mem).
112116function truncate_start_nonempty (mem:: MemoryView , from:: Integer )
113117 frm = Int (from):: Int
114118 @boundscheck if ((frm - 1 ) % UInt) ≥ length (mem) % UInt
@@ -118,11 +122,14 @@ function truncate_start_nonempty(mem::MemoryView, from::Integer)
118122 typeof (mem)(unsafe, newref, length (mem) - frm + 1 )
119123end
120124
125+ # Efficient way to get `mem[from:end]`.
126+ # From must be in 1:length(mem)+1.
121127function truncate_start (mem:: MemoryView , from:: Integer )
122128 frm = Int (from):: Int
123129 @boundscheck if ((frm - 1 ) % UInt) > length (mem) % UInt
124130 throw (BoundsError (mem, frm))
125131 end
132+ frm == 1 && return mem
126133 newref = @inbounds memoryref (mem. ref, frm - (from == length (mem) + 1 ))
127134 typeof (mem)(unsafe, newref, length (mem) - frm + 1 )
128135end
0 commit comments