Code View

mstring / mstring-1.3.1.2 / src / MFormat.cpp
// SPDX-License-Identifier: MIT
#include "MFormat.hh"

#include <ostream>

namespace
{
  bool IsMemberCall( const std::string &format )
  {
    static const char *const members[] =
    {
      "width", "precision", "fill", "setf", "unsetf", "flags", "imbue"
    };

    std::string::size_type const paren = format.find( '(' );

    if ( paren == std::string::npos )
    {
      return false;
    }

    std::string name = format.substr( 0, paren );
    name.erase( name.find_last_not_of( " \t" ) + 1 );

    for ( const char *member : members )
    {
      if ( name == member )
      {
        return true;
      }
    }

    return false;
  }
}

FormatWriter::FormatWriter( std::ostream &stream, std::string &indent )
  : Writer( stream, indent )
{
  // blank
}

void FormatWriter::operator( )( const std::string &format )
{
  _Stream << _Indent << "stream";

  if ( format[0] == '.' )
  {
    _Stream << format;
  }
  else if ( IsMemberCall( format ) )
  {
    _Stream << '.' << format;
  }
  else
  {
    _Stream << " << " << format;
  }

  _Stream << ';' << std::endl;
}