Vex: Fix Backslashes into Frontslashes

   818   6   1
User Avatar
Member
1035 posts
Joined: April 2017
Offline
Hi!

I've posted about this about 2 years ago and found the solution. Sadly, I'm getting into the same problem and the solution doesnt work this time.

I've got a path with backslashes that I want to convert with frontslashes. It's not working and I don't get why...

Here's the old post about it: https://www.sidefx.com/forum/topic/85105/ [www.sidefx.com]

Attachments:
Houdini_backslashes_01.JPG (62.2 KB)

User Avatar
Member
7984 posts
Joined: Sept. 2011
Online
backslash is the 'escape character' in several languages. if you want a literal backslash, it needs to itself be escaped with another backslash. depending on how many layers if indirection, it could take more than one extra backslash to get a literal backslash. I tend to start at two and use trial and error to find out how many it needs. wrangles need to put at least three or four in place of one to work, but it depends on how they get in the code.
User Avatar
Member
43 posts
Joined: March 2017
Offline
Backslash escapes in vex seem broken to me, at least in 20.0.653 and 20.5.278.
Testing with a simple:
s@filepath = replace(s@filepath, "\\", "/");

"\" and "\\" result in the syntax errors "Mismatched quotation marks" and "Unexpected end of file".


"\\\" results in syntax errors "Unexpected end of file" and "Unterminated string" while printing the following to the console:
);


}

cvex
)
{
__vex_snippet_snippet(filepath);
}

I might file a bug report.
Edited by pixelninja - July 22, 2024 02:28:24
User Avatar
Member
283 posts
Joined: Jan. 2013
Online
pixelninja
"\\\" results in syntax errors "Unexpected end of file" and "Unterminated string" while printing the following to the console:

That's how it's going to work.
s@filepath = replace(s@filepath, "\\\", '/');
User Avatar
Member
43 posts
Joined: March 2017
Offline
Interesting that does work! Thanks

Notes for anyone else running into this issue:

It seems that two unintuitive things are necessary for this to work:
  • The quotes on the source and replace string can't be the same (i.e. must be single quotes for one and double for the other, doesn't matter which)
  • You need to escape the escape in order for the snippet parameter to correctly send a single escaped backslash.

The second point there is covered in vex documentation but the first one feels like a bug, or a least an unfortunate result of how snippet parameter has to handle strings to function.

The reason for the requirement of the alternating quotes seems to be that even though "\\\" converts to "\\" (you can check by middle clicking on the snippet parameter) before it does that the syntax parsing seems to treat it as "\", so it sees an unterminated string. Meaning that it sees the next instance of a double quote as closing the string. Oddly this seems to only apply to the current line, using double quotes on subsequent lines works fine despite the syntax highlighting still being broken.

Some extra odd behaviour is that using single versus double quotes for both arguments produces different outputs in the console when they fail.
This snippet:
s@filepath = replace(s@filepath, "\\\", "/");
string TESTING = "Some more code";
Results in this output:
);
);

While this snipppet:
s@filepath = replace(s@filepath, '\\\', '/');
string TESTING = "Some more code";
Results in this output:
);
    string TESTING = "Some more code";

# 16 "<vexpression>"
}

cvex
__vex_snippet(export string filepath = "")
{
    __vex_snippet_snippet(filepath);
}

I assume that this has something to do with how c/c++ handles characters versus string literals but that's just a guess. Given that vex uses single and double quotes interchangeably it's still an unexpected outcome.


Side note: I also tried using raw strings and that doesn't work either.

tl;dr
Escaping backslashes in the vex snippet parameter is funky and produces weird results.
If you need to escape a backslash use 3 and don't reuse the same quotations (single or double) again until the next line.
Edited by pixelninja - July 22, 2024 21:26:10
User Avatar
Member
283 posts
Joined: Jan. 2013
Online
pixelninja
Oddly this seems to only apply to the current line, using double quotes on subsequent lines works fine despite the syntax highlighting still being broken.

I agree. There is some problem with how quotes are interpreted on the same line. I've found at least 3 ways around this besides just changing double quotes to single quotes.

We can just move the next argument to the next line. That looks terrible.
s@filepath = replace(s@filepath, "\\\", 
"/");
string TESTING = "Some more code";

It's a little better if we just create another variable.
string s = "\\\";
s@filepath = replace(s@filepath, s, "/");
string TESTING = "Some more code";

And since we know the ascii codes for these characters, we can use the chr() expression directly so that we don't have to remember how many characters must be in the string for it to work correctly.
#define BSLASH chr(92)
#define FSLASH chr(47)

s@filepath = replace(s@filepath, BSLASH, FSLASH);
string TESTING = "Some more code";
User Avatar
Member
43 posts
Joined: March 2017
Offline
Great solutions
  • Quick Links