ENIGMA Forums

Contributing to ENIGMA => Function Peer Review => Topic started by: RetroX on September 09, 2010, 03:35:56 PM

Title: Trimming
Post by: RetroX on September 09, 2010, 03:35:56 PM
I was bored, and I had these lying around.  Dunno if anyone would use them for a game, but hey, whatever.

Code: [Select]
string string_trim_leading(string str)
  {
  for (size_t i=0;i<str.length();i++)
    {
    if ((str[i]=='\n' || i==0) && isspace(str[i+1]) && str[i+1]!='\n')
      {
      str.erase(str.begin()+(--i)+2);
      }
    }

  return str;
  }


string string_trim_trailing(string str)
  {
  for (size_t i=0;i<str.length();i++)
    {
    if ((str[i]=='\n' || i==str.length()-1) && isspace(str[i-1]) && str[i-1]!='\n')
      {
      str.erase(str.begin()+(--i));
      }
    }

  return str;
  }


inline string string_trim_lines(string str) { return string_trim_leading(string_trim_trailing(str)); }


string string_trim_begin(string str)
  {
  if (str.length()>0)
    {
    while (isspace(str[0]))
      {
      str.erase(str.begin());
      if (str.length()==0)
        {
        break;
        }
      }
    }

  return str;
  }


string string_trim_end(string str)
  {
  if (str.length()>0)
    {
    while (isspace(str[str.length()-1]))
      {
      str.erase(str.length()-1,1);
      if (str.length()==0)
        {
        break;
        }
      }
    }

  return str;
  }


inline string string_trim_both(string str) { return string_trim_begin(string_trim_end(str)); }


string string_trim_chars(string str, string chars)
  {
  for (size_t i=0;i<str.length();i++)
    {
    for (size_t j=0;j<chars.length();j++)
      {
      if (str[i]==chars[j])
        {
        if (i>0)
          {
          while (isspace(str[i-1]))
            {
            str.erase(str.begin()+(--i));
            }
          }

        if (i<str.length()-1)
          {
          while (isspace(str[i+1]))
            {
            str.erase(str.begin()+i+1);
            }
          }
        }
      }
    }

  return str;
  }

inline bool string_empty(string str) { return string_trim_both(str)==""; }


string string_trim_leading(string str) - Trims whitespace at the beginning of lines
string string_trim_trailing(string str) - Trims whitespace at the end of lines
string string_trim_lines(string str) - Trims whitespace around lines
string string_trim_begin(string str) - Trims whitespace at the beginning of a string
string string_trim_end(string str) - Trims whitespace at the end of a string
string string_trim_both(string str) - Trims whitespace on both sides of a string
string string_trim_chars(string str, string chars) - Trims whitespace around certain characters
bool string_empty(string str) - Returns true if a string consists only of whitespace
Title: Re: Trimming
Post by: IsmAvatar on September 09, 2010, 04:36:49 PM
That's preceding. Or leading.
Title: Re: Trimming
Post by: RetroX on September 10, 2010, 05:47:26 PM
Leading sounds better.  Fixed.
Title: Re: Trimming
Post by: notachair on September 11, 2010, 02:34:50 AM
What about \t and \r\n?
Title: Re: Trimming
Post by: RetroX on September 11, 2010, 09:31:46 AM
isspace() returns true if a character is a space, \t, \r, or \n.

And it can work with other linebreaks if you convert it first.