From dc51fa2ed0cabaa1f8dd5f4e351cea4a0fc6f8be Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 26 Dec 2017 17:29:32 +1100 Subject: [PATCH] view: char constructor shouldn't include null characters --- test/view.cpp | 5 +++++ view.hpp | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/test/view.cpp b/test/view.cpp index 0e9d6266..e0f99ec8 100644 --- a/test/view.cpp +++ b/test/view.cpp @@ -10,6 +10,11 @@ main (int, char**) { util::TAP::logger tap; + tap.expect_eq ( + std::size (util::make_view ("foo")), 3u, + "character array view does not include trailing null" + ); + const std::string s = "this is a test string"; const std::string t = "not the same string"; diff --git a/view.hpp b/view.hpp index 263e6afd..db47cd56 100644 --- a/view.hpp +++ b/view.hpp @@ -46,6 +46,16 @@ namespace util { { ; } + //--------------------------------------------------------------------- + // explicitly cater for the char array case so that we don't + // accidentally include the trailing null in the data. + template + view (const char (&value)[N]): + view (std::begin (value), std::begin (value) + N - 1) + { + static_assert (N > 0); + } + //--------------------------------------------------------------------- template view (const ValueT(&value)[N]):