How to create an OffsetArray from an existing array?

Sorry if the answer is obvious. I just couldn’t find a solution in the source code or via Googling.

Basically my use case is like this:

v = Vector{Char}("asdf")

# Doesn't work
offset_v = OffsetVector{Char}("asdf", 0:3)

# Doesn't work
offset_v = OffsetVector{Char}(v, 0:3)

# Doesn't work
offset_v = OffsetVector{Char}(undef, 0:3)
copyto!(offset_v, 0:3, v, 1:4)

# Copies the content but they are now off by one.
circcopy!(offset_v, v)
OffsetArray(::Array{Char,1}, 0:3) with eltype Char with indices 0:3:
 'f'
 'a'
 's'
 'd'

Is there an obvious way to do so without having to manually iterate the characters one by one and assign them?

The general syntax is OffsetArray(v, axes...):

julia> OffsetArray(v, 0:3)
OffsetArray(::Array{Char,1}, 0:3) with eltype Char with indices 0:3:
 'a'
 's'
 'd'
 'f'

Given that it works for Vector, the OffsetVector{Char}("asdf", 0:3) syntax seems to make sense, and could be added in a PR to OffsetArrays.

2 Likes