StringUtil Class Reference

#include <string_util.hpp>

List of all members.

Static Public Member Functions

static bool has_suffix (const std::string &data, const std::string &suffix)
static bool numeric_less (const std::string &lhs, const std::string &rhs)
 Compare two strings according to their numeric value, similar to what 'sort -n' does.


Detailed Description

Definition at line 22 of file string_util.hpp.


Member Function Documentation

bool StringUtil::has_suffix ( const std::string &  data,
const std::string &  suffix 
) [static]

Definition at line 22 of file string_util.cpp.

Referenced by World::load().

00023 {
00024   if (data.length() >= suffix.length())
00025   {
00026     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
00027   }
00028   else
00029   {
00030     return false;
00031   }
00032 }

bool StringUtil::numeric_less ( const std::string &  lhs,
const std::string &  rhs 
) [static]

Compare two strings according to their numeric value, similar to what 'sort -n' does.

Definition at line 35 of file string_util.cpp.

Referenced by World::load(), and OptionsMenu::OptionsMenu().

00036 {
00037   std::string::size_type i = 0;
00038   std::string::size_type min_len = std::min(lhs.size(), rhs.size());
00039 
00040   while(i < min_len)
00041   {
00042     if (isdigit(lhs[i]) && isdigit(rhs[i]))
00043     {
00044       // have two digits, so check which number is smaller
00045       std::string::size_type li = i+1;
00046       std::string::size_type ri = i+1;
00047 
00048       // find the end of the number in both strings
00049       while(li < lhs.size() && isdigit(lhs[li])) { li += 1; }
00050       while(ri < rhs.size() && isdigit(rhs[ri])) { ri += 1; }
00051 
00052       if (li == ri)
00053       {
00054         // end is at the same point in both strings, so do a detaile
00055         // comparism of the numbers
00056         for(std::string::size_type j = i; j < li; ++j)
00057         {
00058           if (lhs[j] != rhs[j])
00059           {
00060             return lhs[j] < rhs[j];
00061           }
00062         }
00063 
00064         // numbers are the same, so jump to the end of the number and compare
00065         i = li;
00066       }
00067       else
00068       {
00069         // numbers have different numbers of digits, so the number
00070         // with the least digits wins
00071         return li < ri;
00072       }
00073     }
00074     else
00075     {
00076       // do normal character comparism
00077       if (lhs[i] != rhs[i])
00078       {
00079         return lhs[i] < rhs[i];
00080       }
00081       else
00082       {
00083         // strings are the same so far, so continue
00084         i += 1;
00085       }
00086     }
00087   }
00088 
00089   return lhs.size() < rhs.size();
00090 }


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:36 2014 for SuperTux by  doxygen 1.5.1