src/util/string_util.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include <algorithm>
00018 
00019 #include "string_util.hpp"
00020 
00021 bool
00022 StringUtil::has_suffix(const std::string& data, const std::string& suffix)
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 }
00033 
00034 bool
00035 StringUtil::numeric_less(const std::string& lhs, const std::string& rhs)
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 }
00091 
00092 /* EOF */

Generated on Mon Jun 9 03:38:23 2014 for SuperTux by  doxygen 1.5.1