Hello, 

On Saturday, February 1, 2014 8:42:07 PM UTC+1, Eric Davies wrote:
>
> 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.
>
> Thanks, it is fine, if only for future reference for others who happen to 
stumble upon something similar...
 

> 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.
>
> This is my guess, too.  So I understand where the problems comes from, but 
I think that the general interface to open(s::String) is either too 
general, or open() is not fully implemented. 

since SubString <: String, open(s::String) claims it can handle all Strings 
but the ccall chokes on the SubString.  It took a while before I figured 
out methods like ascii() and utf8() convert a SubString to a concrete 
String type.  However, I could not find a way to copy a substring into a 
string for, e.g., the purpose of an open() call.  For instance, both 
string(), convert(String,) and copy() did not work, the last two for 
obvious reasons.  Now that I'm busy with it again, I think the function

unsubstring{S}(s::SubString{S}) = convert(S,s)


(if such functionality doesn't already exist) could be useful in methods 
like open(s::String). 

> 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). 
>
> copy() results in a copy of a substring, which still beaks open()
 

> 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
>>
>>
>>

Reply via email to