Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ends_with.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/ends_with.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #include <tlx/string/ends_with.hpp>
12 
13 #include <algorithm>
14 #include <cstring>
15 
16 #include <tlx/string/to_lower.hpp>
17 
18 namespace tlx {
19 
20 bool ends_with(const std::string& str, const std::string& match) {
21  if (match.size() > str.size())
22  return false;
23 
24  return std::equal(match.begin(), match.end(), str.end() - match.size());
25 }
26 
27 bool ends_with(const std::string& str, const char* match) {
28  size_t match_size = strlen(match);
29  if (match_size > str.size())
30  return false;
31 
32  std::string::const_iterator s = str.end() - match_size;
33 
34  while (*match != 0) {
35  if (s == str.end() || *s != *match) return false;
36  ++s, ++match;
37  }
38 
39  return true;
40 }
41 
42 /******************************************************************************/
43 
44 bool ends_with_icase(const std::string& str, const std::string& match) {
45  if (match.size() > str.size())
46  return false;
47 
48  return std::equal(match.begin(), match.end(), str.end() - match.size(),
49  [](const char& c1, const char& c2) {
50  return to_lower(c1) == to_lower(c2);
51  });
52 }
53 
54 bool ends_with_icase(const std::string& str, const char* match) {
55  size_t match_size = strlen(match);
56  if (match_size > str.size())
57  return false;
58 
59  std::string::const_iterator s = str.end() - match_size;
60 
61  while (*match != 0) {
62  if (s == str.end() || to_lower(*s) != to_lower(*match))
63  return false;
64  ++s, ++match;
65  }
66 
67  return true;
68 }
69 
70 } // namespace tlx
71 
72 /******************************************************************************/
char to_lower(char ch)
Transform the given character to lower case without any localization.
Definition: to_lower.cpp:17
bool ends_with_icase(const std::string &str, const std::string &match)
Checks if the given match string is located at the end of this string.
Definition: ends_with.cpp:44
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
bool ends_with(const std::string &str, const std::string &match)
Checks if the given match string is located at the end of this string.
Definition: ends_with.cpp:20