Crystal: Incorrect String#gsub behavior

Created on 9 Nov 2019  路  8Comments  路  Source: crystal-lang/crystal

I need to escape all whitespace chars with ''.

"two words".gsub(/\s+/, "\ ")

Expected result: "two\ words".
Actual result: "two words".

Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx

question

Most helpful comment

I think syntax error is good.

All 8 comments

gsub behaves totally correct.

Did you look at the string replacement? It is actually two words.
In string literals the backslash character denotes an escape sequence and by default escapes the next character as itself (unless it is one of the special escape sequences). See https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html for details.

In order to write a literal backslash, you can use the backslash escape \\: "two\\ words".
Alternatively, there is also a non-interpolation string literal: %q[two\ words].

Got it!

Should we just make invalid escapes be syntax errors, so they're reserved for additional escapes possibly in the future. Otherwise it's always a breaking change to add an escape, and these kinds of errors can pop up.

There's no reason why "\ " should compile, when it's equivalent to " ".

I suppose that behaviour is simply a Ruby legacy. It's a valid option, though. I don't think there are many escape sequences that could possibly be added, so the breaking changes should not be a huge issue.

The possible alternatives for unrecognized escape sequences are:

  • Ignore the backslash (current behaviour)
  • Treat the backslash as literal backslash (Python)
  • Syntax error (C)

I think syntax error is good.

Should we reopen it?

No, better make it a new issue.

I think it should be a different issue, unrelated to gsub.

Was this page helpful?
0 / 5 - 0 ratings