LCOV - code coverage report
Current view: top level - utilities/include/mcrl2/utilities - text_utility.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 38 39 97.4 %
Date: 2024-04-21 03:44:01 Functions: 7 11 63.6 %
Legend: Lines: hit not hit

          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/text_utility.h
      10             : /// \brief String manipulation functions.
      11             : 
      12             : #ifndef MCRL2_UTILITIES_TEXT_UTILITY_H
      13             : #define MCRL2_UTILITIES_TEXT_UTILITY_H
      14             : 
      15             : #include <algorithm>
      16             : #include <iostream>
      17             : #include <iterator>
      18             : #include <limits>
      19             : #include <sstream>
      20             : #include <string>
      21             : #include <vector>
      22             : 
      23             : namespace mcrl2
      24             : {
      25             : 
      26             : namespace utilities
      27             : {
      28             : 
      29             : /// \brief Checks whether the input only contains proper ascii characters,
      30             : ///        including common control characters. 
      31             : /// \param input The string to be checked. 
      32             : /// \return true iff the input consists of ordinary ascii characters only. 
      33             : template <class CharContainer>
      34             : inline bool contains_only_ascii_symbols(const CharContainer& input)
      35             : {
      36             :   for(const typename CharContainer::value_type& c: input)
      37             :   {
      38             :     // Allow printable characters and a tab, line feed, and a carriage return
      39             :     if (!(std::isprint(c) || c=='\t' || c=='\n' || c=='\r' ))
      40             :     {
      41             :       return false;
      42             :     }
      43             :   }
      44             :   return true;
      45             : }
      46             : 
      47             : /// \brief Transform parameter into string.
      48             : /// \param x Some expression
      49             : /// \pre type T has operator <<
      50             : /// \return The string representation of x.
      51             : template <typename T>
      52             : inline
      53        2211 : std::string to_string(const T& x)
      54             : {
      55        2211 :   std::stringstream ss;
      56        2211 :   ss << x;
      57        4422 :   return ss.str();
      58        2211 : }
      59             : 
      60             : /// \brief Split a string into paragraphs.
      61             : /// \param text A string
      62             : /// \return The paragraphs of <tt>text</tt>
      63             : std::vector<std::string> split_paragraphs(const std::string& text);
      64             : 
      65             : /// \brief Split the text.
      66             : /// \param line A string
      67             : /// \param separators A string
      68             : /// \return The splitted text
      69             : std::vector<std::string> split(const std::string& line, const std::string& separators);
      70             : 
      71             : /// \brief Read text from a file.
      72             : /// \param filename A string
      73             : /// \param warn If true, a warning is printed to standard error if the file is not found
      74             : /// \return The contents of the file
      75             : std::string read_text(const std::string& filename, bool warn=false);
      76             : 
      77             : /// \brief Read text from a stream.
      78             : /// \param in An input stream
      79             : /// \return The text read from the stream
      80             : inline
      81        2592 : std::string read_text(std::istream& in)
      82             : {
      83        2592 :   in.unsetf(std::ios::skipws); //  Turn of white space skipping on the stream
      84        2592 :   std::string s;
      85        5184 :   std::copy(
      86        2592 :     std::istream_iterator<char>(in),
      87        2592 :     std::istream_iterator<char>(),
      88             :     std::back_inserter(s)
      89             :   );
      90        2592 :   return s;
      91           0 : }
      92             : 
      93             : /// \brief Remove comments from a text (everything from '%' until end of line).
      94             : /// \param text A string
      95             : /// \return The removal result
      96             : std::string remove_comments(const std::string& text);
      97             : 
      98             : /// \brief Removes whitespace from a string.
      99             : /// \param text A string
     100             : /// \return The removal result
     101             : std::string remove_whitespace(const std::string& text);
     102             : 
     103             : /// \brief Regular expression replacement in a string.
     104             : /// \param src A string
     105             : /// \param dest A string
     106             : /// \param text A string
     107             : /// \return The transformed string
     108             : std::string regex_replace(const std::string& src, const std::string& dest, const std::string& text);
     109             : 
     110             : /// \brief Split a string using a regular expression separator.
     111             : /// \param text A string
     112             : /// \param sep A string
     113             : /// \return The splitted string
     114             : std::vector<std::string> regex_split(const std::string& text, const std::string& sep);
     115             : 
     116             : /// \brief Remove all trailing and leading spaces from the input.
     117             : /// \param text A string
     118             : /// \return The trimmed string
     119             : std::string trim_copy(const std::string& text);
     120             : 
     121             : /// \brief Remove all trailing and leading spaces from the input.
     122             : /// \param text A string
     123             : void trim(std::string& text);
     124             : 
     125             : /// \brief Joins a sequence of strings. This is a replacement for
     126             : /// boost::algorithm::join, since it gives stack overflow errors with
     127             : /// Visual C++ express 9.0 under some circumstances.
     128             : template <typename Container>
     129        1091 : std::string string_join(const Container& c, const std::string& separator)
     130             : {
     131        1091 :   std::ostringstream out;
     132        3460 :   for (typename Container::const_iterator i = c.begin(); i != c.end(); ++i)
     133             :   {
     134        2369 :     if (i != c.begin())
     135             :     {
     136        1512 :       out << separator;
     137             :     }
     138        2369 :     out << *i;
     139             :   }
     140        2182 :   return out.str();
     141        1091 : }
     142             : 
     143             : /// \brief Apply word wrapping to a text.
     144             : /// \param text A string of text.
     145             : /// \param max_line_length The maximum line length.
     146             : /// \return The wrapped text.
     147             : std::string word_wrap_text(const std::string& text, unsigned int max_line_length = 78);
     148             : 
     149             : /// \brief Test if a string is a number.
     150             : /// \param s A string of text.
     151             : /// \return True if s is of the form "0 | -? [1-9][0-9]*", false otherwise
     152             : bool is_numeric_string(const std::string& s);
     153             : 
     154             : /// \brief Convert a number to a string in the buffer starting at position start_position.
     155             : /// \param number The number to be converted.
     156             : /// \param buffer A buffer in which the string will be stored that is sufficiently large.
     157             : /// \param start_position The first position where a number is written. 
     158       38066 : inline void number2string(std::size_t number, std::string& buffer, std::size_t start_position)
     159             : {
     160             :   // First calculate the number of digital digits of number;
     161       38066 :   std::size_t number_of_digits=0;
     162       38066 :   if (number==0)
     163             :   {
     164         444 :     number_of_digits=1;
     165             :   }
     166             :   else
     167             :   {
     168      154016 :     for(std::size_t copy=number ; copy!=0; ++number_of_digits, copy=copy/10)
     169             :     {}
     170             :   }
     171             : 
     172             :   // Put the number in the buffer at the right position.
     173       38066 :   std::size_t position=start_position+number_of_digits;
     174       38066 :   buffer.resize(position);
     175             : 
     176      154904 :   while (position>start_position)
     177             :   {
     178      116838 :     --position;
     179      116838 :     buffer[position] = '0' + number % 10;
     180      116838 :     number = number/10;
     181             :   }
     182       38066 : }
     183             : 
     184             : /// \brief Convert a number to string. 
     185             : /// \details This function is much faster than std::to_string and 
     186             : ///          its use is therefore preferred in those cases were performance counts. 
     187             : /// \param number A number to be transformed. 
     188       14422 : inline std::string number2string(std::size_t number) 
     189             : { 
     190       14422 :   std::string buffer;
     191       14422 :   buffer.reserve(std::numeric_limits<std::size_t>::digits10 + 1); 
     192       14422 :   number2string(number, buffer, 0); 
     193       28844 :   return std::string(buffer); 
     194       14422 : }  
     195             : 
     196             : } // namespace utilities
     197             : 
     198             : } // namespace mcrl2
     199             : 
     200             : #endif // MCRL2_UTILITIES_TEXT_UTILITY_H

Generated by: LCOV version 1.14