ATLAS Offline Software
starts_with.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
3  */
4 /**
5  * @file starts_with.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Mar, 2023
8  * @brief C++20-like starts_with/ends_with for strings.
9  */
10 
11 
12 #include <cstring>
13 
14 
15 namespace CxxUtils {
16 
17 /**
18  * @brief Test whether one null-terminated byte string starts with another.
19  * @param s null-terminated byte string in which to search.
20  * @param prefix Prefix for which to search.
21  *
22  * Returns true if null-terminated byte string @c s starts with @c prefix.
23  */
24 inline
25 bool starts_with (const char* s, const char* prefix)
26 {
27  return strncmp(s, prefix, strlen(prefix)) == 0;
28 }
29 
30 /**
31  * @brief Test whether one string starts with
32  * a null-terminated byte string.
33  * @param s String in which to search.
34  * @param prefix Prefix for which to search.
35  *
36  * Returns true if string @c s starts with @c prefix.
37  */
38 inline
39 bool starts_with (const std::string& s, const char* prefix){
40  return starts_with(s.c_str(), prefix);
41 }
42 
43 /**
44  * @brief Test whether one string starts with another.
45  * @param s String in which to search.
46  * @param prefix Prefix for which to search.
47  *
48  * Returns true if string @c s starts with @c prefix.
49  */
50 inline
51 bool starts_with (const std::string& s, const std::string& prefix)
52 {
53  return strncmp (s.c_str(), prefix.c_str(), prefix.size()) == 0;
54 }
55 
56 /**
57  * @brief Test whether one null-terminated byte string ends with another.
58  * @param s null-terminated byte string in which to search.
59  * @param suffix Suffix for which to search.
60  *
61  * Returns true if null-terminated byte string @c s starts with @c suffix.
62  */
63 inline
64 bool ends_with (const char* s, const char* suffix){
65  const size_t size_s = strlen(s);
66  const size_t size_suffix = strlen(suffix);
67  return size_s >= size_suffix &&
68  strcmp (s + size_s - size_suffix, suffix) == 0;
69 }
70 
71 /**
72  * @brief Test whether one string ends with
73  * a null-terminated byte string.
74  * @param s String in which to search.
75  * @param suffix Suffix for which to search.
76  *
77  * Returns true if string @c s starts with @c suffix.
78  */
79 inline
80 bool ends_with (const std::string& s, const char* suffix){
81  const size_t size_s = s.size();
82  const size_t size_suffix = strlen(suffix);
83  return size_s >= size_suffix &&
84  strcmp (s.c_str() + size_s - size_suffix, suffix) == 0;
85 }
86 
87 /**
88  * @brief Test whether one string ends with another.
89  * @param s String in which to search.
90  * @param suffix Suffix for which to search.
91  *
92  * Returns true if string @c s ends with @c suffix.
93  */
94 inline
95 bool ends_with (const std::string& s, const std::string& suffix)
96 {
97  return s.size() >= suffix.size() &&
98  strcmp (s.c_str() + s.size() - suffix.size(), suffix.c_str()) == 0;
99 }
100 
101 
102 } // namespace CxxUtils