Please forgive me if I'm giving too much obvious info; I'm not aware of
your background and I want to answer this as completely as possible.
This has to do with how Julia stirngs are converted to C strings. C strings
are just pointers to an array of bytes where the null byte '\0' denotes the
termination of a string. Julia strings are a wrapper around this structure.
For a regular String, converting a Julia string to a C string simply
returns the underlying pointer.
The following is my educated guess:
SubStrings, however, refer to the same area in memory as the String they
are a section of--they just contain indicators that denote where the start
and end of the substring should be.
Returning a pointer to the location of the SubString is problematic, as as
far as C routines are concerned, it ends when the original string ends: at
the null byte. Not a problem if the SubString is supposed to end at the
same place, but will not work if it is supposed to end earlier than that.
So, any call to a C function must create a copy of any SubString that ends
earlier than the end of the original String (bytestring, ascii, utf8, copy,
etc. will do this for you).
On Saturday, 1 February 2014 07:01:50 UTC-6, David van Leeuwen wrote:
>
> Hello,
>
> I have problems opening a file of which the name is specified as a
> SubString. The substring is a result of a readdlm, which is fine by me,
> but I don't know how to get rid of its substringness. Well, perhaps
> ascii() will do the job---but that is not the point of this message.
>
> The problem is isolated in the following code:
>
> julia> open(match(r"ab", "abc").match, "r") ## should try to open a file
> called "ab"
> ERROR: a SubString must coincide with the end of the original string to
> be convertible to pointer
> in convert at string.jl:659
> in open at io.jl:351
> in open at io.jl:363
>
>
> I believe the offending line is in a ccall
> systemerror("opening file $fname",
> ccall(:ios_file, Ptr{Void},
> (Ptr{Uint8}, Ptr{Uint8}, Int32, Int32, Int32, Int32
> ),
> s.ios, fname, rd, wr, cr, tr) == C_NULL)
>
>
> Cheers,
>
> ---david
>
>
>