From d6d30dd9590b41fbcdc88b43767b27a31bfb1c73 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 14 Apr 2023 13:41:52 -0700 Subject: [PATCH] [ADT] add StringViewExtras llvm::starts_with for std::string_view std::string_view::starts_with isn't available until C++20. Create llvm::starts_with for now; we can delete this when LLVM moves to C++20 one day. To run the newly added unit test: $ cd llvm/build; ninja ADTTests; cd - $ ./llvm/build/unittests/ADT/ADTTests --gtest_filter=StringViewExtrasTest.\* Reviewed By: MaskRay, erichkeane Differential Revision: https://reviews.llvm.org/D148367 --- llvm/include/llvm/ADT/StringViewExtras.h | 35 +++++++++++++++++++++++++++++ llvm/unittests/ADT/CMakeLists.txt | 1 + llvm/unittests/ADT/StringViewExtrasTest.cpp | 32 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 llvm/include/llvm/ADT/StringViewExtras.h create mode 100644 llvm/unittests/ADT/StringViewExtrasTest.cpp diff --git a/llvm/include/llvm/ADT/StringViewExtras.h b/llvm/include/llvm/ADT/StringViewExtras.h new file mode 100644 index 0000000..5508371 --- /dev/null +++ b/llvm/include/llvm/ADT/StringViewExtras.h @@ -0,0 +1,35 @@ +//===- llvm/ADT/StringViewExtras.h - Useful string_view functions C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file contains some functions that are useful when dealing with +/// string_views. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_STRINGVIEWEXTRAS_H +#define LLVM_ADT_STRINGVIEWEXTRAS_H + +#include + +namespace llvm { + +// FIXME: std::string_view::starts_with is not available until C++20. Once LLVM +// is upgraded to C++20, remove this header and users. + +inline constexpr bool starts_with(std::string_view self, char C) { + return !self.empty() && self.front() == C; +} +inline constexpr bool starts_with(std::string_view haystack, + std::string_view needle) { + return haystack.substr(0, needle.size()) == needle; +} + +} // end namespace llvm + +#endif // LLVM_ADT_STRINGVIEWEXTRAS_H diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt index c519025..f6e4164 100644 --- a/llvm/unittests/ADT/CMakeLists.txt +++ b/llvm/unittests/ADT/CMakeLists.txt @@ -79,6 +79,7 @@ add_llvm_unittest(ADTTests StringRefTest.cpp StringSetTest.cpp StringSwitchTest.cpp + StringViewExtrasTest.cpp TinyPtrVectorTest.cpp TwineTest.cpp TypeSwitchTest.cpp diff --git a/llvm/unittests/ADT/StringViewExtrasTest.cpp b/llvm/unittests/ADT/StringViewExtrasTest.cpp new file mode 100644 index 0000000..ffbbcb4 --- /dev/null +++ b/llvm/unittests/ADT/StringViewExtrasTest.cpp @@ -0,0 +1,32 @@ +//===- StringExtrasTest.cpp - Unit tests for String extras ----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringViewExtras.h" +#include "llvm/Support/raw_ostream.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include + +using namespace llvm; + +TEST(StringViewExtrasTest, starts_with) { + std::string haystack = "hello world"; + EXPECT_TRUE(llvm::starts_with(haystack, 'h')); + EXPECT_FALSE(llvm::starts_with(haystack, '\0')); + EXPECT_TRUE(llvm::starts_with(haystack, "hello")); + // TODO: should this differ from \0? + EXPECT_TRUE(llvm::starts_with(haystack, "")); + + std::string empty; + EXPECT_FALSE(llvm::starts_with(empty, 'h')); + EXPECT_FALSE(llvm::starts_with(empty, '\0')); + EXPECT_FALSE(llvm::starts_with(empty, "hello")); + // TODO: should this differ from \0? + EXPECT_TRUE(llvm::starts_with(empty, "")); +} -- 2.7.4