#ifndef STRINGSPACE
#define STRINGSPACE

#include <cassert>
#include <string>
using namespace std;

#include <typedefs.h>

/*!
\brief My interpretation of space characters.
*/
class myisspace
{
public:

  /** Default space. */
  boolc operator ()(charc ch) const;
};

/*!
\brief Trim a string by removing leading and trailing space.
*/
template< typename SPC=myisspace >
class spacertrim
{
public:

  /** Functional object to test if a character is a space.*/
  SPC spacer;

  /** Trim the string of leading and trailing space. */
  void operator () ( string & s ) const;
};

/** */ 
stringc stringtrim(stringc& str);

/*!
\brief The functional object identifies an empty string.
\verbatim
  Identifying empty strings
    spacerdelete<> space;
    ... if (space(some_string)) ...
  For deleting empty strings
    spacedelete<>()[some_string];
  Linux needed cast for
    assertreturnOS( (bool)spacerdelete<>()[s] );
\endverbatim
*/
template< typename SPC=myisspace >
class spacerdelete
{
public:

  /** Functional object to test if a character is a space.*/
  SPC spacer;

  /** All characters must be space or an empty string for  
     the string to be interpreted as full of spaces. */
  boolc operator () ( stringc & s ) const;

  /** If the string is empty delete it. */
  boolc operator [] (string& str);

};

//---------------------------------------------------------
//  Implementation.

template< typename SPC >
boolc spacerdelete<SPC>::operator [] (string& str)
{
  if (spacerdelete<SPC>()(str))
  {
    str="";
    return true;
  }
  return false;
}

template< typename SPC >
boolc spacerdelete<SPC>::operator () ( stringc & s ) const
{
  if (s.empty())
    return true;
  uintc sz = s.size();
  uint i=0;
  for ( ; i<sz; ++i)
  {
    if (spacer(s[i])==false)
      return false;
  }

  return true;
}

template< typename SPC >
void spacertrim<SPC>::operator () ( string & s ) const
{
  uintc imax=s.size();

  if (imax==0)
    return;
   
  uint i(0);

  for ( ; i<imax; ++i )
    if (spacer(s[i])==false)
      break;
  uintc i0(i);

  for ( i=imax-1; i>0; --i)
    if (spacer(s[i])==false)
      break;
  uintc i1(i);

  // One character in string case.
  if (i0==i1)
  {
    if (spacer(s[i0]))
    {
      s.clear();
      return;
    }

    s = s[i0];
    return;
  }

  if (i0==0)
    if (i1==(imax-1))
      return;
   
  s = s.substr(i0,i1-i0+1);
}

;

#endif



