forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make base::StringPiece more constexpr
The following methods have been made constexpr: - the implicit conversion constructor base::StringPiece(const value_type*) - the comparison operators and base::StringPiece::compare() - base::StringPiece::starts_with() and base::StringPiece::ends_with() Since std::char_traits<> isn't constexpr-ified in C++14, this CL also implements base::CharTraits<> to provide constexpr versions for the base::StringPiece implementation. Finally, this also updates a few callers in Chrome to take advantage of the new constexpr functionality. Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_mojo Change-Id: Id3cf2be14bfb5adb53a5ca1bd74ec2a48e411f60 Reviewed-on: https://chromium-review.googlesource.com/1038746 Reviewed-by: Łukasz Anforowicz <lukasza@chromium.org> Reviewed-by: Dmitry Gozman <dgozman@chromium.org> Reviewed-by: kylechar <kylechar@chromium.org> Reviewed-by: danakj <danakj@chromium.org> Commit-Queue: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/master@{#556258}
- Loading branch information
1 parent
ce15d2f
commit 154c940
Showing
9 changed files
with
260 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_STRINGS_CHAR_TRAITS_H_ | ||
#define BASE_STRINGS_CHAR_TRAITS_H_ | ||
|
||
#include <stddef.h> | ||
|
||
namespace base { | ||
|
||
// constexpr version of http://en.cppreference.com/w/cpp/string/char_traits. | ||
// This currently just implements the bits needed to support a (mostly) | ||
// constexpr StringPiece. | ||
// | ||
// TODO(dcheng): Once we switch to C++17, most methods will become constexpr and | ||
// we can switch over to using the one in the standard library. | ||
template <typename T> | ||
struct CharTraits { | ||
// Performs a lexographical comparison of the first N characters of |s1| and | ||
// |s2|. Returns 0 if equal, -1 if |s1| is less than |s2|, and 1 if |s1| is | ||
// greater than |s2|. | ||
static constexpr int compare(const T* s1, const T* s2, size_t n) noexcept; | ||
|
||
// Returns the length of |s|, assuming null termination (and not including the | ||
// terminating null). | ||
static constexpr size_t length(const T* s) noexcept; | ||
}; | ||
|
||
template <typename T> | ||
constexpr int CharTraits<T>::compare(const T* s1, | ||
const T* s2, | ||
size_t n) noexcept { | ||
for (; n; --n, ++s1, ++s2) { | ||
if (*s1 < *s2) | ||
return -1; | ||
if (*s1 > *s2) | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
template <typename T> | ||
constexpr size_t CharTraits<T>::length(const T* s) noexcept { | ||
size_t i = 0; | ||
for (; *s; ++s) | ||
++i; | ||
return i; | ||
} | ||
|
||
// char specialization of CharTraits that can use clang's constexpr instrinsics, | ||
// where available. | ||
template <> | ||
struct CharTraits<char> { | ||
static constexpr int compare(const char* s1, | ||
const char* s2, | ||
size_t n) noexcept; | ||
static constexpr size_t length(const char* s) noexcept; | ||
}; | ||
|
||
constexpr int CharTraits<char>::compare(const char* s1, | ||
const char* s2, | ||
size_t n) noexcept { | ||
#if __has_feature(cxx_constexpr_string_builtins) | ||
return __builtin_memcmp(s1, s2, n); | ||
#else | ||
for (; n; --n, ++s1, ++s2) { | ||
if (*s1 < *s2) | ||
return -1; | ||
if (*s1 > *s2) | ||
return 1; | ||
} | ||
return 0; | ||
#endif | ||
} | ||
|
||
constexpr size_t CharTraits<char>::length(const char* s) noexcept { | ||
#if defined(__clang__) | ||
return __builtin_strlen(s); | ||
#else | ||
size_t i = 0; | ||
for (; *s; ++s) | ||
++i; | ||
return i; | ||
#endif | ||
} | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_STRINGS_CHAR_TRAITS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/strings/char_traits.h" | ||
#include "base/strings/string16.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace base { | ||
|
||
TEST(CharTraitsTest, CharCompare) { | ||
static_assert(CharTraits<char>::compare("abc", "def", 3) == -1, ""); | ||
static_assert(CharTraits<char>::compare("def", "def", 3) == 0, ""); | ||
static_assert(CharTraits<char>::compare("ghi", "def", 3) == 1, ""); | ||
} | ||
|
||
TEST(CharTraitsTest, CharLength) { | ||
static_assert(CharTraits<char>::length("") == 0, ""); | ||
static_assert(CharTraits<char>::length("abc") == 3, ""); | ||
} | ||
|
||
TEST(CharTraitsTest, Char16TCompare) { | ||
static_assert(CharTraits<char16_t>::compare(u"abc", u"def", 3) == -1, ""); | ||
static_assert(CharTraits<char16_t>::compare(u"def", u"def", 3) == 0, ""); | ||
static_assert(CharTraits<char16_t>::compare(u"ghi", u"def", 3) == 1, ""); | ||
} | ||
|
||
TEST(CharTraitsTest, Char16TLength) { | ||
static_assert(CharTraits<char16_t>::length(u"abc") == 3, ""); | ||
} | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.