Code View

cparse / cparse-1.3.0.2 / src / cparse / Indentation.cpp
// SPDX-License-Identifier: MIT
#include "Indentation.hh"

#include <limits>
#include <ostream>

namespace fedem
{
  namespace parser
  {
    Indentation::Indentation( uint_fast16_t step_size ) noexcept
      : stepSize( step_size ),
        indent( 0 )
    {
      // blank
    }

    Indentation::~Indentation( ) noexcept = default;

    Indentation::Indentation( Indentation const& object ) noexcept
      : stepSize( object.stepSize ),
        indent( object.indent )
    {
      // blank
    }

    Indentation& Indentation::operator=( Indentation const& object ) noexcept
    {
      if( this != &object )
      {
        stepSize = object.stepSize;
        indent = object.indent;
      }
      return *this;
    }

    void Indentation::left( uint_fast16_t step ) noexcept
    {
      auto const jump = step * stepSize;

      if( indent < jump )
      {
        indent = 0;
      }
      else
      {
        indent -= jump;
      }
    }

    void Indentation::right( uint_fast16_t step ) noexcept
    {
      auto const jump = step * stepSize;

      if( ( std::numeric_limits< decltype( indent ) >::max( ) - indent ) < jump )
      {
        indent = std::numeric_limits< decltype( indent ) >::max( );
      }
      else
      {
        indent += jump;
      }
    }

    void Indentation::setStepSize( uint_fast16_t step_size ) noexcept
    {
      stepSize = step_size;
    }

    uint_fast16_t Indentation::getStepSize( ) const noexcept
    {
      return stepSize;
    }

    void Indentation::setIndentation( uint_fast16_t new_indent ) noexcept
    {
      indent = new_indent;
    }

    uint_fast16_t Indentation::getIndentation( ) const noexcept
    {
      return indent;
    }

    void Indentation::reset( uint_fast16_t step_size ) noexcept
    {
      stepSize = step_size;
      indent = 0;
    }

    std::ostream& operator<<( std::ostream& stream, Indentation const& object )
    {
      for( auto count = object.getIndentation( ); count > 0; --count )
      {
        stream << ' ';
      }

      return stream;
    }
  }  // end namespace parser
}  // end namespace fedem