Pages: [1]
  Print  
Author Topic: Trimming  (Read 1194 times)
Offline (Male) RetroX
Posted on: September 09, 2010, 03:35:56 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
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
« Last Edit: September 10, 2010, 05:48:40 PM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Female) IsmAvatar
Reply #1 Posted on: September 09, 2010, 04:36:49 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 891

View Profile Email
That's preceding. Or leading.
Logged
Offline (Male) RetroX
Reply #2 Posted on: September 10, 2010, 05:47:26 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Leading sounds better.  Fixed.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) notachair
Reply #3 Posted on: September 11, 2010, 02:34:50 AM

Definitely not a chair
Contributor
Joined: Feb 2008
Posts: 300

View Profile
What about \t and \r\n?
Logged
Offline (Male) RetroX
Reply #4 Posted on: September 11, 2010, 09:31:46 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
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.
« Last Edit: September 11, 2010, 09:36:52 AM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: [1]
  Print