Skip to content

"\"-character differences between String vs Regex #3078

Open
@drosehn

Description

Here's me tripping over another minor difference between ruby and crystal.

Given the code:

#   Octal \007 = \a in some contexts
tab_str = "MiscLabel"
new_str = "\e]1;%s\a" % tab_str
new_str =~ /\a/

Crystal playground will show:
"MiscLabel" : String
"\e]1;MiscLabela" : String
nil : (Int32 | Nil)

In strings, ruby treats '\a' as the '\007' character, aka BEL. Crystal does not, which isn't necessarily a problem. But crystal makes use of some other library for doing regex's, and in that library '\a' is treated the same as '\007'. You can see that the string ends with an 'a', and the regex fails because it's trying to find /\a/. So let's say we change the string to use the octal value '\007':

tab_str = "MiscLabel"
new_str = "\e]1;%s\007" % tab_str
new_str =~ /\a/

Crystal playground now shows:
"MiscLabel" : String
"\e]1;MiscLabel\u{7}" : String
13 : (Int32 | Nil)

So, that works, if the user knows to use '\007' in strings, and '\a' in regex's. But the playground shows that character value as \u{7} when it displays the string, so maybe the cleaner and more constant thing for the programmer to do is to use that form in both contexts. So try:

tab_str = "MiscLabel"
new_str = "\e]1;%s\u{7}" % tab_str
new_str =~ /\u{7}/

but that gives you:
"MiscLabel" : String
"\e]1;MiscLabel\u{7}" : String
Syntax error in :12: invalid regex: PCRE does not support \L, \l, \N{name}, \U, or \u at 1
CLOSE

(I'm cheating a little there, because you won't actually see those first two lines unless you comment-out the regex, and then uncomment it and re-run to get the error message).

I won't show it here, but "\007" works fine because it results in the same value in both contexts.

I happened to be copying some code which had '\a' so that's the one I know about. I didn't check any of the other less-common ""-character codes. And for what it's worth, this was in code that I was copying from a bash shell, which also supports "\a" for "\007".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions