Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
starts_with.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/starts_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 
12 
13 #include <algorithm>
14 
15 #include <tlx/string/to_lower.hpp>
16 
17 namespace tlx {
18 
19 bool starts_with(const std::string& str, const std::string& match) {
20  if (match.size() > str.size())
21  return false;
22 
23  return std::equal(match.begin(), match.end(), str.begin());
24 }
25 
26 bool starts_with(const std::string& str, const char* match) {
27  std::string::const_iterator s = str.begin();
28 
29  while (*match != 0) {
30  if (s == str.end() || *s != *match) return false;
31  ++s, ++match;
32  }
33 
34  return true;
35 }
36 
37 /******************************************************************************/
38 
39 bool starts_with_icase(const std::string& str, const std::string& match) {
40  if (match.size() > str.size())
41  return false;
42 
43  return std::equal(match.begin(), match.end(), str.begin(),
44  [](const char& c1, const char& c2) {
45  return to_lower(c1) == to_lower(c2);
46  });
47 }
48 
49 bool starts_with_icase(const std::string& str, const char* match) {
50  std::string::const_iterator s = str.begin();
51 
52  while (*match != 0) {
53  if (s == str.end() || to_lower(*s) != to_lower(*match))
54  return false;
55  ++s, ++match;
56  }
57 
58  return true;
59 }
60 
61 } // namespace tlx
62 
63 /******************************************************************************/
char to_lower(char ch)
Transform the given character to lower case without any localization.
Definition: to_lower.cpp:17
bool starts_with_icase(const std::string &str, const std::string &match)
Checks if the given match string is located at the start of this string.
Definition: starts_with.cpp:39
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
bool starts_with(const std::string &str, const std::string &match)
Checks if the given match string is located at the start of this string.
Definition: starts_with.cpp:19