Discussion:
[PATCH, libstdc++, C++20] Implement P0457R2 String Prefix and Suffix Checking.
Ed Smith-Rowland
2018-11-30 02:35:03 UTC
Permalink
Greetings,

This patch implements starts_with and ends_with for basic_string and
basic_string_view for C++20.

This builds and tests cleanly on x86_64-linux.

Ed
Jonathan Wakely
2018-11-30 10:33:55 UTC
Permalink
Post by Ed Smith-Rowland
Greetings,
This patch implements starts_with and ends_with for basic_string and
basic_string_view for C++20.
This was on my TODO list, thanks for taking care of it.
Post by Ed Smith-Rowland
+#if __cplusplus > 201703L
+ bool
+ starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
+ { return __sv_type(this->data(), this->size()).starts_with(__x); }
+
+ bool
+ starts_with(_CharT __x) const noexcept
+ { return __sv_type(this->data(), this->size()).starts_with(__x); }
+
+ bool
+ starts_with(const _CharT* __x) const
This can be noexcept. It isn't in the standard, because it has a
narrow contract (there's a precondition that __x is null-terminated).
But since we can't reliably detect whether __x is null-terminated, and
even if we could we wouldn't want to throw an exception, we can just
make it noexcept. Same for the ends_with(const _CharT*) one, and the
equivalent members in the COW basic_string and basic_string_view.
Post by Ed Smith-Rowland
Index: include/std/string_view
===================================================================
--- include/std/string_view (revision 266645)
+++ include/std/string_view (working copy)
@@ -227,7 +227,6 @@
__sv = __tmp;
}
-
size_type
@@ -387,6 +386,36 @@
traits_type::length(__str));
}
+#if __cplusplus > 201703L
+ constexpr bool
+ starts_with(basic_string_view __x) const noexcept
+ { return this->size() >= __x.size()
+ && this->compare(0, __x.size(), __x) == 0; }
Please put the opening and closing braces on their own lines, as for
Post by Ed Smith-Rowland
+ constexpr bool
+ ends_with(basic_string_view __x) const noexcept
+ {
+ return this->size() >= __x.size()
+ && this->compare(this->size() - __x.size(), npos, __x) == 0;
+ }
OK with those changes, thanks.
Ed Smith-Rowland
2018-11-30 16:27:32 UTC
Permalink
Post by Jonathan Wakely
Post by Ed Smith-Rowland
Greetings,
This patch implements starts_with and ends_with for basic_string and
basic_string_view for C++20.
This was on my TODO list, thanks for taking care of it.
Post by Ed Smith-Rowland
+#if __cplusplus > 201703L
+      bool
+      starts_with(basic_string_view<_CharT, _Traits> __x) const
noexcept
+      { return __sv_type(this->data(),
this->size()).starts_with(__x); }
+
+      bool
+      starts_with(_CharT __x) const noexcept
+      { return __sv_type(this->data(),
this->size()).starts_with(__x); }
+
+      bool
+      starts_with(const _CharT* __x) const
This can be noexcept. It isn't in the standard, because it has a
narrow contract (there's a precondition that __x is null-terminated).
But since we can't reliably detect whether __x is null-terminated, and
even if we could we wouldn't want to throw an exception, we can just
make it noexcept. Same for the ends_with(const _CharT*) one, and the
equivalent members in the COW basic_string and basic_string_view.
Post by Ed Smith-Rowland
Index: include/std/string_view
===================================================================
--- include/std/string_view    (revision 266645)
+++ include/std/string_view    (working copy)
@@ -227,7 +227,6 @@
    __sv = __tmp;
      }
-
      size_type
@@ -387,6 +386,36 @@
                      traits_type::length(__str));
      }
+#if __cplusplus > 201703L
+      constexpr bool
+      starts_with(basic_string_view __x) const noexcept
+      { return this->size() >= __x.size()
+        && this->compare(0, __x.size(), __x) == 0; }
Please put the opening and closing braces on their own lines, as for
Post by Ed Smith-Rowland
+      constexpr bool
+      ends_with(basic_string_view __x) const noexcept
+      {
+    return this->size() >= __x.size()
+        && this->compare(this->size() - __x.size(), npos, __x) == 0;
+      }
OK with those changes, thanks
Committed 266674.

New patch attached.

Ed

Loading...