2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "string_util.hpp"
22 StringUtil::has_suffix(const std::string& data, const std::string& suffix)
24 if (data.length() >= suffix.length())
26 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
35 StringUtil::numeric_less(const std::string& lhs, const std::string& rhs)
37 std::string::size_type i = 0;
38 std::string::size_type min_len = std::min(lhs.size(), rhs.size());
42 if (isdigit(lhs[i]) && isdigit(rhs[i]))
44 // have two digits, so check which number is smaller
45 std::string::size_type li = i+1;
46 std::string::size_type ri = i+1;
48 // find the end of the number in both strings
49 while(li < lhs.size() && isdigit(lhs[li])) { li += 1; }
50 while(ri < rhs.size() && isdigit(rhs[ri])) { ri += 1; }
54 // end is at the same point in both strings, so do a detaile
55 // comparism of the numbers
56 for(std::string::size_type j = i; j < li; ++j)
60 return lhs[j] < rhs[j];
64 // numbers are the same, so jump to the end of the number and compare
69 // numbers have different numbers of digits, so the number
70 // with the least digits wins
76 // do normal character comparism
79 return lhs[i] < rhs[i];
83 // strings are the same so far, so continue
89 return lhs.size() < rhs.size();