diff --git a/src/Lexer.cpp b/src/Lexer.cpp index 4928739ee..60c46f6f2 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -1237,26 +1237,27 @@ bool Lexer::isOneWord (const std::string& text) // "'" // "\"" // 'one two' +// Result includes the quotes. bool Lexer::readWord ( const std::string& text, const std::string& quotes, std::string::size_type& cursor, std::string& word) { + if (quotes.find (text[cursor]) == std::string::npos) + return false; + std::string::size_type eos = text.length (); + int quote = text[cursor++]; + word = quote; - int quote = 0; - if (quotes.find (text[cursor]) != std::string::npos) - quote = text[cursor++]; - - word = ""; int c; while ((c = text[cursor])) { // Quoted word ends on a quote. if (quote && quote == c) { - ++cursor; + word += utf8_character (utf8_next_char (text, cursor)); break; } diff --git a/test/lexer.t.cpp b/test/lexer.t.cpp index c645aacb7..47e77db64 100644 --- a/test/lexer.t.cpp +++ b/test/lexer.t.cpp @@ -37,7 +37,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (1061); + UnitTest t (1067); std::vector > tokens; std::string token; @@ -215,7 +215,7 @@ int main (int argc, char** argv) std::string::size_type cursor = 0; std::string word; t.ok (Lexer::readWord ("'one two'", "'\"", cursor, word), "readWord ''one two'' --> true"); - t.is (word, "one two", " word '" + word + "'"); + t.is (word, "'one two'", " word '" + word + "'"); t.is ((int)cursor, 9, " cursor"); // static bool readWord (const std::string&, std::string::size_type&, std::string&); @@ -241,12 +241,12 @@ int main (int argc, char** argv) std::string text = "one 'two' three\\ four"; cursor = 0; - while (Lexer::readWord (text, cursor, word)) - { - t.diag ("'" + word + "'"); - while (Lexer::isWhitespace(text[cursor])) - ++cursor; - } + t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true"); + t.is (word, "one", " word '" + word + "'"); + t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true"); + t.is (word, "'two'", " word '" + word + "'"); + t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true"); + t.is (word, "three four", " word '" + word + "'"); // Test all Lexer types. #define NO {"",Lexer::Type::word}