Creating AbstractStrings

question

#1

Hello,

I face a problem when creating an AbstractString type. Maybe is not the best implementation. Nevertheless, I want to give it a try. Here is my toy-model which fails of course, because, it is not finished. I don’t know how to continue with it.

bitstype 32 MyChar

type MyString <: AbstractString
  len::Int
  MyString(arr::Array{MyChar,1}) = begin
    # What should be go here?
    x = new(length(arr))
  end
end

# I'm only guessing
Base.endof(mystr::MyString) = mystr.len
Base.next(mystr::MyString, i::Int) = unsafe_load(pointer_from_objref(mystr), i+1)

x = reinterpret(MyChar, 0x00000000)
y = reinterpret(MyChar, 0x00000001)

mystr = MyString([x,y])

Thanks


#2

You need a field to actually store the array in the type. And you don’t need a length field if you have an array field, since the length is stored in the array. And then you don’t need to declare a constructor, since the default will work.

type MyString <: AbstractString
  data::Vector{MyChar}
end
Base.endof(mystr::MyString) = length(mystr.data)
Base.next(mystr::MyString, i) = (mystr.data[i], i+1)
Base.start(mystr::MyString) = 1
Base.done(mystr::MyString, i) = i > length(mystr.data)

If you want a string type with 32-bit code units, then maybe you should just use the UTF32String type from https://github.com/JuliaArchive/LegacyStrings.jl


#3

Thanks @stevengj!!