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

#include <stringconvert.h>


// Integer greater than or equal to zero.
boolc isstringdigits(stringc & str)
{
  if (str.empty())
    return false;

  for (size_t i=0; i<str.length(); ++i)
  {
//cout << i << ":" << str[i] << "  " << isdigit(str[i]) << endl;
    if (isdigit(str[i])==false)
      return false;
  }

  return true;
}

boolc isstringinteger(stringc & str)
{
  if (str.empty())
    return false;

  if (str[0]=='-')
    return isstringinteger(str.substr(1));

  return isstringdigits(str);
}

boolc isstringzero_or_positive_real(stringc & str)
{
  if (str.empty())
    return false;

  if (str[0]=='.')
    return isstringdigits(str.substr(1));

  size_t k;
  uint kcount=0;
  for (size_t i=0; i<str.length(); ++i)
  {
    if (str[i]=='.')
    {
      k=i;
      ++kcount;
      continue;
    }

    if (isdigit(str[i])==false)
      return false;
  }

  if (kcount>1)
    return false;

  return true;
}

boolc isstringreal(stringc & str)
{
  if (str.empty())
    return false;

  if (str[0]=='-')
    return isstringzero_or_positive_real(str.substr(1));

  return isstringzero_or_positive_real(str);
}

boolc isstringnegative(stringc & str)
{
  if (str.empty())
    return false;

  if (str[0] != '-')
    return false;

  return isstringreal(str);
}

void stringconvert::findandreplace
( 
  string& source, 
  stringc & find, 
  stringc & replace 
) 
{ 
  size_t pos = 0; 
  size_t findlen = find.length(); 
  size_t replacelen = replace.length();

  if( findlen == 0 )
    return;

  for( ; (pos = source.find( find, pos )) != string::npos; )
  {
    source.replace( pos, findlen, replace );
    pos += replacelen;
  }
}

stringc stringconvert::findandreplacefn
( 
  stringc & source, 
  stringc & find, 
  stringc & replace 
) 
{ 
  string str(source); 
  findandreplace(str,find,replace); 
  return str; 
}


