LCOV - code coverage report
Current view: top level - src/plugins/yambi - input.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 19 20 95.0 %
Date: 2019-09-12 12:28:41 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief This file contains a class that represents textual input.
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : // -- Imports ------------------------------------------------------------------
      10             : 
      11             : #include <codecvt>
      12             : #include <locale>
      13             : #include <sstream>
      14             : #include <stdexcept>
      15             : 
      16             : #include "input.hpp"
      17             : 
      18             : using std::codecvt_utf8;
      19             : using std::ifstream;
      20             : using std::out_of_range;
      21             : using std::string;
      22             : using std::stringstream;
      23             : using std::wstring_convert;
      24             : 
      25             : // -- Class --------------------------------------------------------------------
      26             : 
      27             : /**
      28             :  * @brief This constructor creates an input from the given stream.
      29             :  *
      30             :  * @param stream This parameter stores the text this object operates on.
      31             :  */
      32         126 : Input::Input (ifstream const & stream)
      33             : {
      34         126 :         stringstream stringStream;
      35          63 :         stringStream << stream.rdbuf ();
      36         315 :         input = wstring_convert<codecvt_utf8<char32_t>, char32_t>{}.from_bytes (stringStream.str ());
      37          63 : }
      38             : 
      39             : /**
      40             :  * @brief This function returns a character that was not consumed yet.
      41             :  *
      42             :  * @param offset This variable specifies the index of the character
      43             :  *               this method should retrieve as offset to the last consumed
      44             :  *               character.
      45             :  *
      46             :  * @return A character which is `offset` positions away from the last
      47             :  *         consumed character
      48             :  */
      49       30696 : size_t Input::LA (size_t const offset) const
      50             : {
      51       30696 :         if (offset == 0 || position + offset > input.size ())
      52             :         {
      53             :                 return 0;
      54             :         }
      55       60974 :         return input[position + offset - 1];
      56             : }
      57             : 
      58             : /**
      59             :  * @brief This method consumes the next character of `input`.
      60             :  */
      61        5489 : void Input::consume ()
      62             : {
      63        5489 :         if (position + 1 > input.size ())
      64             :         {
      65           0 :                 throw out_of_range ("Unable to consume EOF");
      66             :                 return;
      67             :         }
      68        5489 :         position++;
      69        5489 : }
      70             : 
      71             : /**
      72             :  * @brief Retrieve the current position inside the input.
      73             :  *
      74             :  * @return The current position in number of characters
      75             :  */
      76         508 : size_t Input::index () const
      77             : {
      78         508 :         return position;
      79             : }
      80             : 
      81             : /**
      82             :  * @brief This method retrieves the text between `start` (inclusive) and the
      83             :  *        current position (exclusive).
      84             :  *
      85             :  * @param start This parameter specifies the start index of the string this
      86             :  *              functions returns.
      87             :  *
      88             :  * @return A UTF-8 encoded substring of input starting at `start` and ending
      89             :  *         one character before the current position in the input
      90             :  */
      91         508 : string Input::getText (size_t const start) const
      92             : {
      93        2032 :         string text = wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.to_bytes (input.substr (start, position - start));
      94         508 :         return text;
      95             : }
      96             : 
      97             : /**
      98             :  * @brief This method retrieves the whole text represented by this input object.
      99             :  *
     100             :  * @return A UTF-8 encoded string that stores the data of the input
     101             :  */
     102          11 : string Input::toString () const
     103             : {
     104          33 :         return wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.to_bytes (input);
     105             : }

Generated by: LCOV version 1.13