Skip to content

Commit

Permalink
Use char_traits::length in string_view ctor (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Oct 24, 2018
1 parent 895fb98 commit a9d6b63
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
16 changes: 3 additions & 13 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,6 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
return static_cast<typename std::make_unsigned<Int>::type>(value);
}

// A constexpr std::char_traits::length replacement for pre-C++17.
template <typename Char>
FMT_CONSTEXPR size_t length(const Char *s) {
const Char *start = s;
while (*s) ++s;
return s - start;
}
#if FMT_GCC_VERSION && !defined(__arm__)
FMT_CONSTEXPR size_t length(const char *s) { return std::strlen(s); }
#endif

#if FMT_GCC_VERSION && FMT_GCC_VERSION < 405
template <typename... T>
struct is_constructible: std::false_type {};
Expand Down Expand Up @@ -377,8 +366,8 @@ class basic_string_view {
the size with ``std::char_traits<Char>::length``.
\endrst
*/
FMT_CONSTEXPR basic_string_view(const Char *s)
: data_(s), size_(internal::length(s)) {}
basic_string_view(const Char *s)
: data_(s), size_(std::char_traits<Char>::length(s)) {}

/** Constructs a string reference from a ``std::basic_string`` object. */
template <typename Alloc>
Expand Down Expand Up @@ -422,6 +411,7 @@ class basic_string_view {
}
friend bool operator<(basic_string_view lhs, basic_string_view rhs) {
return lhs.compare(rhs) < 0;

}
friend bool operator<=(basic_string_view lhs, basic_string_view rhs) {
return lhs.compare(rhs) <= 0;
Expand Down
5 changes: 3 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2273,9 +2273,10 @@ struct test_format_string_handler {
bool error = false;
};

FMT_CONSTEXPR bool parse_string(fmt::string_view s) {
template <size_t N>
FMT_CONSTEXPR bool parse_string(const char (&s)[N]) {
test_format_string_handler h;
fmt::internal::parse_format_string<true>(s, h);
fmt::internal::parse_format_string<true>(fmt::string_view(s, N - 1), h);
return !h.error;
}

Expand Down

0 comments on commit a9d6b63

Please sign in to comment.