diff --git a/libc/src/__support/CPP/StringView.h b/libc/src/__support/CPP/StringView.h --- a/libc/src/__support/CPP/StringView.h +++ b/libc/src/__support/CPP/StringView.h @@ -107,19 +107,14 @@ // Moves the start of the view forward by n characters. // The behavior is undefined if n > size(). - StringView remove_prefix(size_t N) const { - if (N >= Len) - return StringView(); - return StringView(Data + N, Len - N); + void remove_prefix(size_t N) { + Len -= N; + Data += N; } // Moves the end of the view back by n characters. // The behavior is undefined if n > size(). - StringView remove_suffix(size_t N) const { - if (N >= Len) - return StringView(); - return StringView(Data, Len - N); - } + void remove_suffix(size_t N) { Len -= N; } // An equivalent method is not available in std::string_view. StringView trim(const char C) const { diff --git a/libc/src/stdlib/getenv.cpp b/libc/src/stdlib/getenv.cpp --- a/libc/src/stdlib/getenv.cpp +++ b/libc/src/stdlib/getenv.cpp @@ -32,8 +32,10 @@ if (cur[env_var_name.size()] != '=') continue; - return const_cast( - cur.remove_prefix(env_var_name.size() + 1).data()); + // Remove the name and the equals sign. + cur.remove_prefix(env_var_name.size() + 1); + // We know that data is null terminated, so this is safe. + return const_cast(cur.data()); } return nullptr; diff --git a/libc/test/src/__support/CPP/stringview_test.cpp b/libc/test/src/__support/CPP/stringview_test.cpp --- a/libc/test/src/__support/CPP/stringview_test.cpp +++ b/libc/test/src/__support/CPP/stringview_test.cpp @@ -78,43 +78,35 @@ } TEST(LlvmLibcStringViewTest, RemovePrefix) { - StringView v("123456789"); - - auto p = v.remove_prefix(0); - ASSERT_EQ(p.size(), size_t(9)); - ASSERT_TRUE(p.equals(StringView("123456789"))); - - p = v.remove_prefix(4); - ASSERT_EQ(p.size(), size_t(5)); - ASSERT_TRUE(p.equals(StringView("56789"))); - - p = v.remove_prefix(9); - ASSERT_EQ(p.size(), size_t(0)); - ASSERT_TRUE(p.data() == nullptr); - - p = v.remove_prefix(10); - ASSERT_EQ(p.size(), size_t(0)); - ASSERT_TRUE(p.data() == nullptr); + StringView a("123456789"); + a.remove_prefix(0); + ASSERT_EQ(a.size(), size_t(9)); + ASSERT_TRUE(a.equals(StringView("123456789"))); + + StringView b("123456789"); + b.remove_prefix(4); + ASSERT_EQ(b.size(), size_t(5)); + ASSERT_TRUE(b.equals(StringView("56789"))); + + StringView c("123456789"); + c.remove_prefix(9); + ASSERT_EQ(c.size(), size_t(0)); } TEST(LlvmLibcStringViewTest, RemoveSuffix) { - StringView v("123456789"); - - auto p = v.remove_suffix(0); - ASSERT_EQ(p.size(), size_t(9)); - ASSERT_TRUE(p.equals(StringView("123456789"))); - - p = v.remove_suffix(4); - ASSERT_EQ(p.size(), size_t(5)); - ASSERT_TRUE(p.equals(StringView("12345"))); - - p = v.remove_suffix(9); - ASSERT_EQ(p.size(), size_t(0)); - ASSERT_TRUE(p.data() == nullptr); - - p = v.remove_suffix(10); - ASSERT_EQ(p.size(), size_t(0)); - ASSERT_TRUE(p.data() == nullptr); + StringView a("123456789"); + a.remove_suffix(0); + ASSERT_EQ(a.size(), size_t(9)); + ASSERT_TRUE(a.equals(StringView("123456789"))); + + StringView b("123456789"); + b.remove_suffix(4); + ASSERT_EQ(b.size(), size_t(5)); + ASSERT_TRUE(b.equals(StringView("12345"))); + + StringView c("123456789"); + c.remove_suffix(9); + ASSERT_EQ(c.size(), size_t(0)); } TEST(LlvmLibcStringViewTest, TrimSingleChar) {