Improved insert_values : 1) fixed bug 2) Allow for arbitrary number of parameters (well, as much as C++ allows without variadic templates)

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3976 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2009-09-01 20:40:55 +00:00
parent b615f71818
commit 2d234f6fbb
2 changed files with 297 additions and 126 deletions

View File

@ -24,6 +24,7 @@
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <exception>
namespace StringUtils
{
@ -115,30 +116,56 @@ namespace StringUtils
* \param s The string to split.
* \param c The character by which the string is split.
*/
std::vector<std::string> split(const std::string& s, char c)
std::vector<std::string> split(const std::string& s, char c, bool keepSplitChar)
{
std::vector<std::string> result;
try
{
std::string::size_type start=0;
while(start!=std::string::npos)
{
std::string::size_type i=s.find(c, start);
if(i!=std::string::npos)
if (i!=std::string::npos)
{
result.push_back(std::string(s,start, i-start));
if (keepSplitChar)
{
int from = start-1;
if (from < 0) from = 0;
result.push_back(std::string(s, from, i-from));
}
else result.push_back(std::string(s,start, i-start));
start=i+1;
}
else
{
result.push_back(std::string(s,start));
if (keepSplitChar) result.push_back(std::string(s,start-1));
else result.push_back(std::string(s,start));
start = i;
}
}
return result;
}
catch (std::exception& e)
{
fprintf(stderr, "Fatal error in split(std::string) : %s @ line %i\n", __FILE__, __LINE__);
printf("Splitting %s\n", s.c_str());
for (int n=0; n<(int)result.size(); n++)
{
printf("Split : %s\n", result[n].c_str());
}
exit(1);
}
} // split
/** Same as above, but for wide strings */
std::vector<irr::core::stringw> split(const irr::core::stringw& s, char c)
std::vector<irr::core::stringw> split(const irr::core::stringw& s, char c, bool keepSplitChar)
{
try
{
std::vector<irr::core::stringw> result;
@ -148,17 +175,31 @@ namespace StringUtils
irr::s32 i = s.findNext(c, start);
if (i != -1)
{
result.push_back( s.subString(start, i-start) );
if (keepSplitChar)
{
int from = start-1;
if (from < 0) from = 0;
result.push_back( s.subString(from, i-from) );
}
else result.push_back( s.subString(start, i-start) );
start = i+1;
}
else
{
result.push_back( s.subString(start, s.size()-start) );
if (keepSplitChar) result.push_back( s.subString(start - 1, s.size()-start + 1) );
else result.push_back( s.subString(start, s.size()-start) );
return result;
//start = i+1;
}
}
return result;
}
catch (std::exception& e)
{
fprintf(stderr, "Fatal error in split(stringw) : %s @ line %i\n", __FILE__, __LINE__);
exit(1);
}
} // split
@ -170,6 +211,8 @@ namespace StringUtils
* \param path The string to split.
*/
std::vector<std::string> splitPath(const std::string& path)
{
try
{
std::vector<std::string> dirs=StringUtils::split(path,':');
for(int i=(int)dirs.size()-1; i>=0; i--)
@ -204,8 +247,84 @@ namespace StringUtils
} // for i
#endif
return dirs;
}
catch (std::exception& e)
{
fprintf(stderr, "Fatal error in splitPath : %s @ line %i\n", __FILE__, __LINE__);
exit(1);
}
} // splitPath
// ------------------------------------------------------------------------
std::string insertValues(const std::string &s, std::vector<std::string>& all_vals)
{
try
{
std::vector<std::string> sv = StringUtils::split(s, '%', true);
std::string new_string="";
const unsigned int item_count = sv.size();
for (unsigned int i=0; i<item_count; i++)
{
if(sv[i][0] != '%')
{
new_string += sv[i];
}
else
{
if(sv[i][1]=='s' || sv[i][1]=='d' || sv[i][1]=='i')
{
assert(all_vals.size() > 0);
new_string += all_vals[0]+sv[i].substr(2);
all_vals.erase(all_vals.begin());
}
else
new_string+=sv[i];
}
}
return new_string;
}
catch (std::exception& e)
{
fprintf(stderr,"Fatal error in insertValues(std::string) : %s @ line %i\n", __FILE__, __LINE__);
exit(1);
}
}
// ------------------------------------------------------------------------
irr::core::stringw insertValues(const irr::core::stringw &s, std::vector<std::string>& all_vals)
{
try
{
std::vector<irr::core::stringw> sv = StringUtils::split(s, '%', true);
irr::core::stringw new_string="";
for (unsigned int i=0; i<sv.size(); i++)
{
if(sv[i][0] != '%')
{
new_string += sv[i];
}
else
{
if (sv[i][1]=='s' || sv[i][1]=='d' || sv[i][1]=='i')
{
new_string += all_vals[0].c_str();
new_string += sv[i].subString(2, sv[i].size()-2);
all_vals.erase(all_vals.begin());
}
else
new_string+=sv[i];
}
}
return new_string;
}
catch (std::exception& e)
{
fprintf(stderr,"Fatal error in insertValues(stringw) : %s @ line %i\n", __FILE__, __LINE__);
exit(1);
}
}
// ------------------------------------------------------------------------
/** Converts a time in seconds into a string of the form mm:ss:hh (minutes,
* seconds, 1/100 seconds.

View File

@ -76,13 +76,13 @@ namespace StringUtils
}
std::string toUpperCase(const std::string&);
std::vector<std::string> split(const std::string& s, char c);
std::vector<std::string> split(const std::string& s, char c, bool keepSplitChar=false);
std::vector<std::string> splitPath(const std::string& path);
std::vector<irr::core::stringw> split(const irr::core::stringw& s, char c);
std::vector<irr::core::stringw> split(const irr::core::stringw& s, char c, bool keepSplitChar=false);
// ------------------------------------------------------------------------
/** Replaces the first %s or %d in the string with the first value
/** Replaces the first %s or %i/%d in the string with the first value
* converted to a string), the 2nd %s or %d with the second value etc.
* So this is basically a simplified s(n)printf replacement, but doesn't
* do any fancy formatting (and no type checks either - so you can print
@ -96,7 +96,21 @@ namespace StringUtils
* to see two strings instead of one sentence, see xgettext manual
* for why this is a bad idea)
* \param s String in which all %s or %d are replaced.
* \param v1,v2,v3 Value(s) to replace all %s or %d with.
* \param all_vals Value(s) to replace all %s or %d with.
*/
std::string insertValues(const std::string &s, std::vector<std::string>& all_vals);
// ------------------------------------------------------------------------
/** Same as above but for wide-strings */
irr::core::stringw insertValues(const irr::core::stringw &s, std::vector<std::string>& all_vals);
// ------------------------------------------------------------------------
// Note: the order in which the templates are specified is important, since
// otherwise some compilers will not find the right template to use.
/** Shortcut insert_values taking three values, see above for
* full docs.
* \param s String in which all %s or %d are replaced.
* \param v1,v2, v3 Value(s) to replace all %s or %d with.
*/
template <class T1, class T2, class T3>
std::string insertValues(const std::string &s, const T1 &v1,
@ -104,29 +118,16 @@ namespace StringUtils
{
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy<<v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy<<v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy<<v3; all_vals.push_back(dummy.str());
std::vector<std::string> sv = StringUtils::split(s, '%');
std::string new_string="";
for(unsigned int i=0; i<sv.size(); i++)
{
if(sv[i][0]=='s' || sv[i][0]=='d' || sv[i][0]=='i')
{
new_string+=all_vals[0]+sv[i].substr(1);
all_vals.erase(all_vals.begin());
}
else
new_string+=sv[i];
}
return new_string;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v3; all_vals.push_back(dummy.str());
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
// Note: the order in which the templates are specified is important, since
// otherwise some compilers will not find the right template to use.
/** Overloaded insert_values taking two values, see below for
/** Shortcut insert_values taking three values, see above for
* full docs.
* \param s String in which all %s or %d are replaced.
* \param v1,v2 Value(s) to replace all %s or %d with.
@ -135,10 +136,15 @@ namespace StringUtils
std::string insertValues(const std::string &s, const T1 &v1,
const T2 &v2)
{
return insertValues(s, v1, v2, "");
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Overloaded insert_values taking one value, see below for
/** Shortcut insert_values taking three values, see above for
* full docs.
* \param s String in which all %s or %d are replaced.
* \param v1 Value to replace.
@ -146,49 +152,85 @@ namespace StringUtils
template <class T1>
std::string insertValues(const std::string &s, const T1 &v1)
{
return insertValues(s, v1, "", "");
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
return insertValues(s, all_vals);
}
/** Like the other one above but for wide strings */
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2, class T3, class T4, class T5>
irr::core::stringw insertValues(const irr::core::stringw &s, const T1 &v1,
const T2 &v2, const T3 &v3, const T4 &v4,
const T5 &v5)
{
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v3; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v4; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v5; all_vals.push_back(dummy.str());
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2, class T3, class T4>
irr::core::stringw insertValues(const irr::core::stringw &s, const T1 &v1,
const T2 &v2, const T3 &v3, const T4 &v4)
{
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v3; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v4; all_vals.push_back(dummy.str());
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2, class T3>
irr::core::stringw insertValues(const irr::core::stringw &s, const T1 &v1,
const T2 &v2, const T3 &v3)
{
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy<<v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy<<v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy<<v3; all_vals.push_back(dummy.str());
std::vector<irr::core::stringw> sv = StringUtils::split(s, '%');
irr::core::stringw new_string="";
for (unsigned int i=0; i<sv.size(); i++)
{
if (sv[i][0]=='s' || sv[i][0]=='d' || sv[i][0]=='i')
{
new_string += all_vals[0].c_str();
new_string += sv[i].subString(1, sv[i].size()-1);
all_vals.erase(all_vals.begin());
}
else
new_string+=sv[i];
}
return new_string;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v3; all_vals.push_back(dummy.str());
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2>
irr::core::stringw insertValues(const irr::core::stringw &s, const T1 &v1,
const T2 &v2)
{
return insertValues(s, v1, v2, "");
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
dummy << v2; all_vals.push_back(dummy.str()); dummy.str("");
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1>
irr::core::stringw insertValues(const irr::core::stringw &s, const T1 &v1)
{
return insertValues(s, v1, "", "");
std::vector<std::string> all_vals;
std::ostringstream dummy;
dummy << v1; all_vals.push_back(dummy.str()); dummy.str("");
return insertValues(s, all_vals);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2, class T3>
irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1,
const T2 &v2, const T3 &v3)
@ -197,21 +239,27 @@ namespace StringUtils
return insertValues(s, v1, v2, v3);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1, class T2>
irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1,
const T2 &v2)
{
irr::core::stringw s(chars);
return insertValues(s, v1, v2, "");
return insertValues(s, v1, v2);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for wide strings */
template <class T1>
irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1)
{
irr::core::stringw s(chars);
return insertValues(s, v1, "", "");
return insertValues(s, v1);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for C strings */
template <class T1, class T2, class T3>
std::string insertValues(const char* chars, const T1 &v1,
const T2 &v2, const T3 &v3)
@ -220,19 +268,23 @@ namespace StringUtils
return insertValues(s, v1, v2, v3);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for C strings */
template <class T1, class T2>
std::string insertValues(const char* chars, const T1 &v1,
const T2 &v2)
{
std::string s(chars);
return insertValues(s, v1, v2, "");
return insertValues(s, v1, v2);
}
// ------------------------------------------------------------------------
/** Like the other ones above but for C strings */
template <class T1>
std::string insertValues(const char* chars, const T1 &v1)
{
std::string s(chars);
return insertValues(s, v1, "", "");
return insertValues(s, v1);
}
} // namespace StringUtils