[fb7fe6]: util / string_manip.cpp  Maximize  Restore  History

Download this file

125 lines (105 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* COPYRIGHT (C) 2001 by various authors
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Part written by John Levon and P. Elie
*/
#include <sstream>
#include "string_manip.h"
using std::string;
using std::vector;
/**
* erase_from_last_of - erase a sequence from the last occurence
* of a given char to the end of string
* @str: string
* @ch: the char from where we erase character
*
* erase char from the last occurence of @ch to the
* end of @str and return the string
*/
string erase_from_last_of(string const & str, char ch)
{
string result = str;
string::size_type pos = result.find_last_of(ch);
if (pos != string::npos)
result.erase(pos, result.length() - pos);
return result;
}
/**
* erase to_last_of - erase a sequence of character from
* the begining of string to the last occurence of a given char
* @str: string
* @ch: the characterto search
*
* erase char from the begin of @str to the last
* occurence of @ch from and return the string
*/
string erase_to_last_of(string const & str, char ch)
{
string result = str;
string::size_type pos = result.find_last_of(ch);
if (pos != string::npos)
result.erase(0, pos + 1);
return result;
}
/**
* rtrim - remove trailing character
* @str: the string
* @ch: the character to remove
*
* Returns the @str removed of its trailing @ch
*/
string rtrim(string const & str, char ch)
{
string result = str;
// a more efficient implementation is possible if we need it.
string::size_type slash = result.find_last_of(ch);
if (slash != string::npos)
result.erase(0, slash + 1);
return result;
}
/**
* tostr - convert integer to str
* i: the integer
*
* Returns the converted string
*/
string tostr(unsigned int i)
{
string str;
std::ostringstream ss(str);
ss << i;
return ss.str();
}
/**
* separate_token - separate a list of tokens
* @result: where to put results
* @str: the string to tokenize
* @ch: the separator_char
*
*/
void separate_token(vector<string> & result, const string & str, char sep)
{
string const temp = str;
size_t last_pos = 0;
for (size_t pos = 0 ; pos != temp.length() ; ) {
pos = temp.find_first_of(sep, last_pos);
if (pos == string::npos)
pos = temp.length();
string token = temp.substr(last_pos, pos - last_pos);
result.push_back(token);
if (pos != temp.length())
last_pos = pos + 1;
}
}