From 244c81a647872f9595f0919b132b13f4577b8897 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 26 Jul 2015 23:43:40 -0400 Subject: [PATCH] Lexer: Implemented ::commonLength for word root comparison --- src/Lexer.cpp | 18 ++++++++++++++++++ src/Lexer.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index d841b6b97..0f53354ed 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -367,6 +367,24 @@ int Lexer::hexToInt (int c0, int c1, int c2, int c3) hexToInt (c3); } +//////////////////////////////////////////////////////////////////////////////// +// Compares two strings, and returns the number bytes in common. +// +// left: wonderful +// right: wonderbread +// returns: ^ 6 +int Lexer::commonLength (const std::string& left, const std::string& right) +{ + std::string::size_type l = 0; + std::string::size_type r = 0; + while (left[l] == right[r] && + utf8_next_char (left, l) && + utf8_next_char (right, r)) + ; + + return (int) l; +} + //////////////////////////////////////////////////////////////////////////////// // Lexer::Type::string // '|" diff --git a/src/Lexer.h b/src/Lexer.h index f92582131..f6db6e168 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -85,6 +85,7 @@ public: static int hexToInt (int); static int hexToInt (int, int); static int hexToInt (int, int, int, int); + static int commonLength (const std::string&, const std::string&); bool isEOS () const;