2 * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
5 * @file starts_with.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief C++20-like starts_with/ends_with for strings.
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.
22 * Returns true if null-terminated byte string @c s starts with @c prefix.
25 bool starts_with (const char* s, const char* prefix)
27 return strncmp(s, prefix, strlen(prefix)) == 0;
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.
36 * Returns true if string @c s starts with @c prefix.
39 bool starts_with (const std::string& s, const char* prefix){
40 return starts_with(s.c_str(), prefix);
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.
48 * Returns true if string @c s starts with @c prefix.
51 bool starts_with (const std::string& s, const std::string& prefix)
53 return strncmp (s.c_str(), prefix.c_str(), prefix.size()) == 0;
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.
61 * Returns true if null-terminated byte string @c s starts with @c suffix.
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;
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.
77 * Returns true if string @c s starts with @c suffix.
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;
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.
92 * Returns true if string @c s ends with @c suffix.
95 bool ends_with (const std::string& s, const std::string& suffix)
97 return s.size() >= suffix.size() &&
98 strcmp (s.c_str() + s.size() - suffix.size(), suffix.c_str()) == 0;
102 } // namespace CxxUtils