Line data Source code
1 : // Author(s): Wieger Wesselink 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : /// \file mcrl2/utilities/detail/split.h 10 : /// \brief add your file description here. 11 : 12 : #ifndef MCRL2_UTILITIES_DETAIL_SPLIT_H 13 : #define MCRL2_UTILITIES_DETAIL_SPLIT_H 14 : 15 : #include "mcrl2/utilities/detail/separate_keyword_section.h" 16 : #include <map> 17 : 18 : namespace mcrl2 { 19 : 20 : namespace utilities { 21 : 22 : namespace detail { 23 : 24 : // Splits a text into non-empty sections. The sections are trimmed. 25 : inline 26 1 : std::vector<std::string> split_text(const std::string& text, const std::string& keyword) 27 : { 28 1 : std::vector<std::string> result; 29 1 : std::vector<std::string> sections = utilities::regex_split(text, keyword); 30 27 : for (std::string& section: sections) 31 : { 32 26 : boost::trim(section); 33 26 : if (!section.empty()) 34 : { 35 26 : result.push_back(section); 36 : } 37 : } 38 2 : return result; 39 1 : } 40 : 41 : // Splits a text into sections based on keywords. The result maps keywords to the corresponding text. 42 : inline 43 26 : std::map<std::string, std::string> split_text_using_keywords(const std::string& text, const std::vector<std::string>& keywords) 44 : { 45 26 : std::map<std::string, std::string> result; 46 26 : std::string text1 = text; 47 78 : for (auto i = keywords.begin(); i != keywords.end(); ++i) 48 : { 49 52 : const std::string& keyword = *i; 50 52 : std::pair<std::string, std::string> p = separate_keyword_section(text1, keyword, keywords); 51 52 : result[keyword] = p.first; 52 52 : text1 = p.second; 53 52 : } 54 52 : return result; 55 26 : } 56 : 57 : // Splits a text into nonempty lines, and returns them. The lines are trimmed. 58 : inline 59 26 : std::vector<std::string> nonempty_lines(const std::string& text) 60 : { 61 52 : std::vector<std::string> lines = utilities::regex_split(text, "\\n"); 62 26 : std::vector<std::string> result; 63 52 : for (std::string& line: lines) 64 : { 65 26 : boost::trim(line); 66 26 : if (!line.empty()) 67 : { 68 26 : result.push_back(line); 69 : } 70 : } 71 52 : return result; 72 26 : } 73 : 74 : } // namespace detail 75 : 76 : } // namespace utilities 77 : 78 : } // namespace mcrl2 79 : 80 : #endif // MCRL2_UTILITIES_DETAIL_SPLIT_H