From d33a62ffa6df974a4d25e672e9d4deafb458f367 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 25 Jun 2016 17:22:27 -0400 Subject: [PATCH] libshared: Integrated the submodule - Integrated RX. --- src/CMakeLists.txt | 77 ++++++++--------- src/RX.cpp | 159 ------------------------------------ src/RX.h | 58 ------------- src/columns/CMakeLists.txt | 1 + src/commands/CMakeLists.txt | 1 + test/rx.t.cpp | 142 -------------------------------- 6 files changed, 41 insertions(+), 397 deletions(-) delete mode 100644 src/RX.cpp delete mode 100644 src/RX.h delete mode 100644 test/rx.t.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fde10cd3c..d3d7880c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,52 +3,53 @@ include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/commands ${CMAKE_SOURCE_DIR}/src/columns + ${CMAKE_SOURCE_DIR}/src/libshared/src ${TASK_INCLUDE_DIRS}) -set (task_SRCS CLI2.cpp CLI2.h - Color.cpp Color.h - Config.cpp Config.h - Context.cpp Context.h - DOM.cpp DOM.h - Dates.cpp Dates.h - Eval.cpp Eval.h - Filter.cpp Filter.h - FS.cpp FS.h - Hooks.cpp Hooks.h - ISO8601.cpp ISO8601.h - JSON.cpp JSON.h - Lexer.cpp Lexer.h - Msg.cpp Msg.h - Nibbler.cpp Nibbler.h - RX.cpp RX.h - TDB2.cpp TDB2.h - Task.cpp Task.h - Timer.cpp Timer.h - TLSClient.cpp TLSClient.h - Variant.cpp Variant.h - ViewTask.cpp ViewTask.h - ViewText.cpp ViewText.h - dependency.cpp - feedback.cpp - i18n.h - legacy.cpp - recur.cpp - rules.cpp - sort.cpp - text.cpp text.h - utf8.cpp utf8.h - util.cpp util.h - wcwidth6.cpp) +add_library (task CLI2.cpp CLI2.h + Color.cpp Color.h + Config.cpp Config.h + Context.cpp Context.h + DOM.cpp DOM.h + Dates.cpp Dates.h + Eval.cpp Eval.h + Filter.cpp Filter.h + FS.cpp FS.h + Hooks.cpp Hooks.h + ISO8601.cpp ISO8601.h + JSON.cpp JSON.h + Lexer.cpp Lexer.h + Msg.cpp Msg.h + Nibbler.cpp Nibbler.h + TDB2.cpp TDB2.h + Task.cpp Task.h + Timer.cpp Timer.h + TLSClient.cpp TLSClient.h + Variant.cpp Variant.h + ViewTask.cpp ViewTask.h + ViewText.cpp ViewText.h + dependency.cpp + feedback.cpp + i18n.h + legacy.cpp + recur.cpp + rules.cpp + sort.cpp + text.cpp text.h + utf8.cpp utf8.h + util.cpp util.h + wcwidth6.cpp) + +add_library (libshared libshared/src/RX.cpp libshared/src/RX.h) -add_library (task STATIC ${task_SRCS}) add_executable (task_executable main.cpp) add_executable (calc_executable calc.cpp) add_executable (lex_executable lex.cpp) # Yes, 'task' is included twice, otherwise linking fails on assorted OSes. -target_link_libraries (task_executable task commands columns task ${TASK_LIBRARIES}) -target_link_libraries (calc_executable task commands columns task ${TASK_LIBRARIES}) -target_link_libraries (lex_executable task commands columns task ${TASK_LIBRARIES}) +target_link_libraries (task_executable task commands columns libshared task ${TASK_LIBRARIES}) +target_link_libraries (calc_executable task commands columns libshared task ${TASK_LIBRARIES}) +target_link_libraries (lex_executable task commands columns libshared task ${TASK_LIBRARIES}) set_property (TARGET task_executable PROPERTY OUTPUT_NAME "task") diff --git a/src/RX.cpp b/src/RX.cpp deleted file mode 100644 index b68a063b7..000000000 --- a/src/RX.cpp +++ /dev/null @@ -1,159 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright 2006 - 2016, Paul Beckingham, Federico Hernandez. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -// http://www.opensource.org/licenses/mit-license.php -// -//////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include - -//////////////////////////////////////////////////////////////////////////////// -RX::RX () -{ -} - -//////////////////////////////////////////////////////////////////////////////// -RX::RX ( - const std::string& pattern, - bool case_sensitive /* = true */) -: _compiled (false) -, _pattern (pattern) -, _case_sensitive (case_sensitive) -{ - compile (); -} - -//////////////////////////////////////////////////////////////////////////////// -RX::RX (const RX& other) -{ - _compiled = false; - _pattern = other._pattern; - _case_sensitive = other._case_sensitive; -} - -//////////////////////////////////////////////////////////////////////////////// -RX::~RX () -{ - if (_compiled) - regfree (&_regex); -} - -//////////////////////////////////////////////////////////////////////////////// -RX& RX::operator= (const RX& other) -{ - _compiled = false; - _pattern = other._pattern; - _case_sensitive = other._case_sensitive; - - return *this; -} - -//////////////////////////////////////////////////////////////////////////////// -void RX::compile () -{ - if (! _compiled) - { - memset (&_regex, 0, sizeof (regex_t)); - - int result; - if ((result = regcomp (&_regex, _pattern.c_str (), -#if defined REG_ENHANCED - REG_ENHANCED | REG_EXTENDED | REG_NEWLINE | -#else - REG_EXTENDED | REG_NEWLINE | -#endif - (_case_sensitive ? 0 : REG_ICASE))) != 0) - { - char message[256]; - regerror (result, &_regex, message, 256); - throw std::string (message); - } - - _compiled = true; - } -} - -//////////////////////////////////////////////////////////////////////////////// -bool RX::match (const std::string& in) -{ - if (! _compiled) - compile (); - - return regexec (&_regex, in.c_str (), 0, nullptr, 0) == 0 ? true : false; -} - -//////////////////////////////////////////////////////////////////////////////// -bool RX::match ( - std::vector& matches, - const std::string& in) -{ - if (! _compiled) - compile (); - - regmatch_t rm[2]; - int offset = 0; - int length = in.length (); - while (regexec (&_regex, in.c_str () + offset, 2, &rm[0], 0) == 0 && - offset < length) - { - matches.push_back (in.substr (rm[0].rm_so + offset, rm[0].rm_eo - rm[0].rm_so)); - offset += rm[0].rm_eo; - - // Protection against zero-width patterns causing infinite loops. - if (rm[0].rm_so == rm[0].rm_eo) - ++offset; - } - - return matches.size () ? true : false; -} - -//////////////////////////////////////////////////////////////////////////////// -bool RX::match ( - std::vector & start, - std::vector & end, - const std::string& in) -{ - if (! _compiled) - compile (); - - regmatch_t rm[2]; - int offset = 0; - int length = in.length (); - while (regexec (&_regex, in.c_str () + offset, 2, &rm[0], 0) == 0 && - offset < length) - { - start.push_back (rm[0].rm_so + offset); - end.push_back (rm[0].rm_eo + offset); - offset += rm[0].rm_eo; - - // Protection against zero-width patterns causing infinite loops. - if (rm[0].rm_so == rm[0].rm_eo) - ++offset; - } - - return start.size () ? true : false; -} - -//////////////////////////////////////////////////////////////////////////////// diff --git a/src/RX.h b/src/RX.h deleted file mode 100644 index b98371113..000000000 --- a/src/RX.h +++ /dev/null @@ -1,58 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright 2006 - 2016, Paul Beckingham, Federico Hernandez. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -// http://www.opensource.org/licenses/mit-license.php -// -//////////////////////////////////////////////////////////////////////////////// - -#ifndef INCLUDED_RX -#define INCLUDED_RX - -#include -#include -#include - -class RX -{ -public: - RX (); - RX (const std::string&, bool caseSensitive = true); - RX (const RX&); - ~RX (); - RX& operator= (const RX&); - - bool match (const std::string&); - bool match (std::vector&, const std::string&); - bool match (std::vector &, std::vector &, const std::string&); - -private: - void compile (); - -private: - bool _compiled {false}; - std::string _pattern {}; - bool _case_sensitive {false}; - regex_t _regex; -}; - -#endif - diff --git a/src/columns/CMakeLists.txt b/src/columns/CMakeLists.txt index 76ccdc0fc..0bfffc941 100644 --- a/src/columns/CMakeLists.txt +++ b/src/columns/CMakeLists.txt @@ -3,6 +3,7 @@ include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/commands ${CMAKE_SOURCE_DIR}/src/columns + ${CMAKE_SOURCE_DIR}/src/libshared/src ${TASK_INCLUDE_DIRS}) set (columns_SRCS Column.cpp Column.h diff --git a/src/commands/CMakeLists.txt b/src/commands/CMakeLists.txt index 575e351e7..188a8ef81 100644 --- a/src/commands/CMakeLists.txt +++ b/src/commands/CMakeLists.txt @@ -3,6 +3,7 @@ include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/commands ${CMAKE_SOURCE_DIR}/src/columns + ${CMAKE_SOURCE_DIR}/src/libshared/src ${TASK_INCLUDE_DIRS}) set (commands_SRCS Command.cpp Command.h diff --git a/test/rx.t.cpp b/test/rx.t.cpp deleted file mode 100644 index 134bacf97..000000000 --- a/test/rx.t.cpp +++ /dev/null @@ -1,142 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright 2006 - 2016, Paul Beckingham, Federico Hernandez. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -// http://www.opensource.org/licenses/mit-license.php -// -//////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include -#include - -Context context; - -int main (int, char**) -{ - UnitTest ut (26); - - // Ensure environment has no influence. - unsetenv ("TASKDATA"); - unsetenv ("TASKRC"); - - std::string text = "This is a test."; - - RX r1 ("i. ", true); - ut.ok (r1.match (text), text + " =~ /i. /"); - - std::vector matches; - ut.ok (r1.match (matches, text), text + " =~ /i. /"); - ut.ok (matches.size () == 2, "2 match"); - ut.is (matches[0], "is ", "$1 == is\\s"); - ut.is (matches[1], "is ", "$1 == is\\s"); - - text = "abcdefghijklmnopqrstuvwxyz"; - - RX r3 ("t..", true); - ut.ok (r3.match (text), "t.."); - - RX r4 ("T..", false); - ut.ok (r4.match (text), "T.."); - - RX r5 ("T..", true); - ut.ok (!r5.match (text), "! T.."); - - text = "this is a test of the regex engine."; - // |...:....|....:....|....:....|....: - - RX r6 ("^this"); - ut.ok (r6.match (text), "^this matches"); - - RX r7 ("engine\\.$"); - ut.ok (r7.match (text), "engine\\.$ matches"); - - std::vector results; - std::vector start; - std::vector end; - RX r8 ("e..", true); - ut.ok (r8.match (results, text), "e.. there are matches"); - ut.ok (r8.match (start, end, text), "e.. there are matches"); - ut.is (results.size (), (size_t) 4, "e.. == 4 matches"); - ut.is (results[0], "est", "e..[0] == 'est'"); - ut.is (start[0], 11, "e..[0] == 11->"); - ut.is (end[0], 14, "e..[0] == ->14"); - - results.clear (); - RX r9 ("e", true); - ut.ok (r9.match (results, text), "e there are matches"); - ut.is (results.size (), (size_t) 6, "e == 6 matches"); - - start.clear (); - end.clear (); - ut.ok (r9.match (start, end, text), "e there are matches"); - ut.is (start.size (), (size_t) 6, "e == 6 matches"); - -#if defined(DARWIN) || defined(CYGWIN) || defined(FREEBSD) || defined(OPENBSD) - text = "this is the end."; - ut.pass (text + " =~ /\\bthe/"); - ut.pass (text + " =~ /the\\b/"); - ut.pass (text + " =~ /\\bthe\\b/"); -#elif defined(SOLARIS) - RX r10 ("\\"); - ut.ok (r11.match (text), text + " =~ /the\\>/"); - - RX r12 ("\\"); - ut.ok (r12.match (text), text + " =~ /\\/"); -#else - RX r10 ("\\bthe"); - text = "this is the end."; - ut.ok (r10.match (text), text + " =~ /\\bthe/"); - - RX r11 ("the\\b"); - ut.ok (r11.match (text), text + " =~ /the\\b/"); - - RX r12 ("\\bthe\\b"); - ut.ok (r12.match (text), text + " =~ /\\bthe\\b/"); -#endif - -#if defined(DARWIN) - text = "D0"; - RX r13 ("D\\d"); - ut.ok (r13.match (text), text + " =~ /D\\d/"); -#else - ut.skip (" =~ /D\\d/"); -#endif - - text = "D0"; - RX r14 ("D[[:digit:]]"); - ut.ok (r14.match (text), text + " =~ /D[[:digit:]]/"); - - text = "D0"; - RX r15 ("D[0-9]"); - ut.ok (r15.match (text), text + " =~ /D[0-9]/"); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -