1 /******************************************************************************
2 * Arachnoid Graphics Plugin for Mupen64Plus
3 * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
5 * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *****************************************************************************/
22 #include "StringFunctions.h"
27 namespace StringFunctions {
29 //-----------------------------------------------------------------------------
31 // @param str The string you want to split into many strings
32 // @param delims The character(s) that split strings
33 // @return Vector with the new strings
34 //-----------------------------------------------------------------------------
35 vector<string> split(const string& str, const string& delims, size_t maxSplits)
39 vector<string> strings;
44 //Find next interesting data
45 start = str.find_first_not_of(delims, start);
47 //Try to find delimiter
48 pos = str.find_first_of(delims, start);
55 else if (pos == string::npos || (maxSplits!=string::npos && numSplits == maxSplits) )
57 //No more spliting, copy the rest of the string
58 strings.push_back( str.substr(start) );
63 //Split string and add to return
64 strings.push_back( str.substr(start, pos - start) );
69 } while (pos != string::npos);
71 //Return vector with strings
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 void trim(string& str, bool left, bool right, const string delims)
80 //Erase characters from the left
83 str.erase(0, str.find_first_not_of(delims));
86 //Erase characters from the right
89 str.erase(str.find_last_not_of(delims)+1);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 char* trim(char* str, bool left, bool right)
101 //Set pointer to string
104 char* end = &str[strlen(str)-1];
107 while ( isspace( *p1 ) && p1 != end )
114 //Copy characters to begining of string
131 //Trim from the right
134 //Point to end of string
135 char* end = str + strlen(str) - 1;
137 //Remove white spaces in the end
138 while( end >= str && *end == ' ' )
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
149 std::vector<string> split(const char* str, const std::string& delims)
151 return split(string(str), delims);
155 } //namespace StringFunctions