From fd2fd60f7c88ce8b50a5d491fd9b1b7c5bc960d8 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 21 Feb 2013 22:12:34 -0800 Subject: [PATCH] Identifier collection now smarter about strings Previously, a string like 'foo\'bar\'zoo' would make the collection process think that "bar" is not inside a string because it wouldn't recognize that the quotes are escaped. Now it does. Fixes #143. --- cpp/ycm/IdentifierUtils.cpp | 6 ++++-- cpp/ycm/tests/IdentifierUtils_test.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cpp/ycm/IdentifierUtils.cpp b/cpp/ycm/IdentifierUtils.cpp index fbfac6cb..b559b9be 100644 --- a/cpp/ycm/IdentifierUtils.cpp +++ b/cpp/ycm/IdentifierUtils.cpp @@ -30,9 +30,11 @@ const char *COMMENT_AND_STRING_REGEX = "|" "/\\*.*?\\*/" // C-style comments, '/* ... */' "|" - "'[^']*'" // Anything inside single quotes, '...' + "'(?:\\\\'|.)*?'" // Anything inside single quotes, '...', but mind the + // escaped quote "|" - "\"[^\"]*\""; // Anything inside double quotes, "..." + "\"(?:\\\\\"|.)*?\""; // Anything inside double quotes, "...", but mind + // the escaped double quote const char *IDENTIFIER_REGEX = "[_a-zA-Z]\\w*"; diff --git a/cpp/ycm/tests/IdentifierUtils_test.cpp b/cpp/ycm/tests/IdentifierUtils_test.cpp index 04f16a57..48092c1e 100644 --- a/cpp/ycm/tests/IdentifierUtils_test.cpp +++ b/cpp/ycm/tests/IdentifierUtils_test.cpp @@ -63,6 +63,15 @@ TEST( IdentifierUtilsTest, RemoveIdentifierFreeTextWorks ) { "bar \n" "qux" ); + EXPECT_STREQ( RemoveIdentifierFreeText( + "foo \n" + "bar 'fo\\'oz\\nfoo'\n" + "qux" + ).c_str(), + "foo \n" + "bar \n" + "qux" ); + EXPECT_STREQ( RemoveIdentifierFreeText( "foo \n" "bar \"foo\"\n" @@ -71,6 +80,15 @@ TEST( IdentifierUtilsTest, RemoveIdentifierFreeTextWorks ) { "foo \n" "bar \n" "qux" ); + + EXPECT_STREQ( RemoveIdentifierFreeText( + "foo \n" + "bar \"fo\\\"oz\\nfoo\"\n" + "qux" + ).c_str(), + "foo \n" + "bar \n" + "qux" ); }