Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace: substring on match function argument error #5673

Closed
mdesantis opened this issue Feb 4, 2014 · 13 comments
Closed

replace: substring on match function argument error #5673

mdesantis opened this issue Feb 4, 2014 · 13 comments

Comments

@mdesantis
Copy link

Hello,

I'm implementing a simple template engine for Julia (version 0.3.0-767~ubuntu13.10.1). I get this error:

replace("test <% 1 %> test", r"<%.*?%>"s, s -> parse(s[3:end-2]))
ERROR: a SubString must coincide with the end of the original string to be convertible to pointer
 in convert at string.jl:662

It should be a bug, isn't it?

@pao
Copy link
Member

pao commented Feb 4, 2014

More SubString fallout. The parse method is ultimately expecting a null-terminated string. Try:

replace("test <% 1 %> test", r"<%(.*?)%>"s, s -> parse(utf8(s[3:end-2])))

We probably need to teach parse to do that for us.

@mdesantis
Copy link
Author

Confirmed that works using utf8():

replace("test <% 1 %> test", r"<%.*?%>"s, s -> parse(utf8(s[3:end-2])))
#=> "test 1 test"

@pao
Copy link
Member

pao commented Feb 4, 2014

See also #5675

@stevengj
Copy link
Member

stevengj commented Feb 4, 2014

The right thing is for parse to call bytestring on its argument before passing it to ccall.

@pao
Copy link
Member

pao commented Feb 4, 2014

Thanks, @stevengj, that sounds good.

@JeffBezanson
Copy link
Sponsor Member

Those null terminators really are hell. Either we make functions like replace and split slower up-front, or we face copying a string potentially many times. One slightly scary option is to update SubStrings in-place with null-terminated copies as necessary, so copying is never needed more than once for a given string. I'm not sure whether that's worthwhile or advisable though.

@stevengj
Copy link
Member

stevengj commented Feb 5, 2014

Even scarier option: Just temporarily write a null character into the string during the ccall.

@JeffBezanson
Copy link
Sponsor Member

Wow, that is true evil genius.

@stevengj
Copy link
Member

stevengj commented Feb 5, 2014

(But if the ccall might throw an exception, you need to have an exception handler to undo the null.)

@StefanKarpinski
Copy link
Sponsor Member

Oh, man. That's so crazy it might work. I'm not sure if exception trapping is going to be low enough overhead though.

@carlobaldassi
Copy link
Member

It would also need to check for partially overlapping sub-strings, and use copies in that case.

@stevengj
Copy link
Member

stevengj commented Feb 5, 2014

@carlobaldassi, that's true, the unusual case of passing multiple substring arguments to the same function would be tricky.

Also, this would be very difficult to do automatically because of perverse situations involving passing callback functions.

@kmsquire
Copy link
Member

kmsquire commented Feb 5, 2014

Even scarier option: Just temporarily write a null character into the string during the ccall.

The main concern here is that in C, strings are mutable.

stevengj added a commit that referenced this issue Feb 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants