After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 749576 - '\R' is invalid escape sequence
'\R' is invalid escape sequence
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Parser
0.26.x
Other Linux
: Normal normal
: 0.42
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2015-05-19 11:50 UTC by Dmitry Golovin
Modified: 2018-05-14 08:33 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Test case for regex literal with \R unicode newline match (153 bytes, text/x-vala)
2015-05-19 15:22 UTC, Al Thomas
  Details
scanner: Accept \R and \N escape sequences in regex literals (2.41 KB, patch)
2018-05-14 07:44 UTC, Rico Tzschichholz
committed Details | Review

Description Dmitry Golovin 2015-05-19 11:50:16 UTC
The PCRE (used in GLib for Regex) is encouraging to use '\R' escape sequence, but valac doesn't allow it. Valadoc also has it: http://references.valadoc.org/#!api=glib-2.0/GLib.Regex

This code will not compile:
    void main() {
        var myregex = new Regex("\R");
    }

The compiler says:
    error: invalid escape sequence
var myregex = new Regex("\R");
                           ^

Well, '\R' is not on the list of supported escape sequences in valascanner.vala, maybe it is a good idea to add it?
Comment 1 Al Thomas 2015-05-19 15:06:06 UTC
There is a difference between a string escape sequence and a PCRE pattern. For example if you want to print a newline:

print( "your text followed by a new line\n" );

In PCRE \R matches the pattern for any Unicode newline sequence. So a print statement such as:

print( "your text followed by a Unicode newline sequence\R" );

doesn't make too much sense because it could be translated to one of a number of newline sequences.

If you wish to pass \R to the PCRE engine escape the \, e.g.

var myregex = new Regex("\\R");

or use a verbatim string (see https://wiki.gnome.org/Projects/Vala/Tutorial#Strings), e.g.:

var myregex = new Regex("""\R""");

If you are looking for a faster implementation use regular expression literals, e.g.:

if ( /\\R/.match( test_string ) ) { print ( "matched" ); }

These can be 3x-4x faster, See https://bugzilla.gnome.org/attachment.cgi?id=157022&action=edit
Comment 2 Al Thomas 2015-05-19 15:22:50 UTC
Created attachment 303604 [details]
Test case for regex literal with \R unicode newline match
Comment 3 Al Thomas 2015-05-19 15:27:57 UTC
Seems like regex literals have a problem with \R. The attached test case will not compile. The test produces the error:

regex_literal_uncode_newline_match_test.vala:4.13-4.13: error: invalid escape sequence

Changing the code to use:

assert ( /\\Rnext/.match( a ) == true );
 
compiles, but fails the assertion with:

ERROR:/home/al/software_projects/regex_literal_uncode_newline_match_test.vala.c:42:_vala_main: assertion failed: (/\\Rnext/.match( a ) == true)
Comment 4 Rico Tzschichholz 2018-05-14 07:44:50 UTC
Created attachment 371988 [details] [review]
scanner: Accept \R and \N escape sequences in regex literals
Comment 5 Rico Tzschichholz 2018-05-14 08:33:27 UTC
Attachment 371988 [details] pushed as b22d43f - scanner: Accept \R and \N escape sequences in regex literals