tlx
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-2019 Timo Bingmann <tb@panthema.net>
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 /******************************************************************************/
20 
21 bool starts_with(const char* str, const char* match) {
22  while (*match != 0) {
23  if (*str == 0 || *str != *match) return false;
24  ++str, ++match;
25  }
26  return true;
27 }
28 
29 bool starts_with(const char* str, const std::string& match) {
30  std::string::const_iterator m = match.begin();
31  while (m != match.end()) {
32  if (*str == 0 || *str != *m) return false;
33  ++str, ++m;
34  }
35  return true;
36 }
37 
38 bool starts_with(const std::string& str, const char* match) {
39  std::string::const_iterator s = str.begin();
40  while (*match != 0) {
41  if (s == str.end() || *s != *match) return false;
42  ++s, ++match;
43  }
44  return true;
45 }
46 
47 bool starts_with(const std::string& str, const std::string& match) {
48  if (match.size() > str.size())
49  return false;
50  return std::equal(match.begin(), match.end(), str.begin());
51 }
52 
53 /******************************************************************************/
54 
55 bool starts_with_icase(const char* str, const char* match) {
56  while (*match != 0) {
57  if (*str == 0 || to_lower(*str) != to_lower(*match))
58  return false;
59  ++str, ++match;
60  }
61  return true;
62 }
63 
64 bool starts_with_icase(const char* str, const std::string& match) {
65  std::string::const_iterator m = match.begin();
66  while (m != match.end()) {
67  if (*str == 0 || to_lower(*str) != to_lower(*m)) return false;
68  ++str, ++m;
69  }
70  return true;
71 }
72 
73 bool starts_with_icase(const std::string& str, const char* match) {
74  std::string::const_iterator s = str.begin();
75  while (*match != 0) {
76  if (s == str.end() || to_lower(*s) != to_lower(*match))
77  return false;
78  ++s, ++match;
79  }
80  return true;
81 }
82 
83 bool starts_with_icase(const std::string& str, const std::string& match) {
84  if (match.size() > str.size())
85  return false;
86  return std::equal(match.begin(), match.end(), str.begin(),
87  [](const char& c1, const char& c2) {
88  return to_lower(c1) == to_lower(c2);
89  });
90 }
91 
92 /******************************************************************************/
93 
94 } // namespace tlx
95 
96 /******************************************************************************/
char to_lower(char ch)
Transform the given character to lower case without any localization.
Definition: to_lower.cpp:17
bool starts_with(const char *str, const char *match)
Checks if the given match string is located at the start of this string.
Definition: starts_with.cpp:21
bool starts_with_icase(const char *str, const char *match)
Checks if the given match string is located at the start of this string.
Definition: starts_with.cpp:55