Merge pull request #161 from mattnewport/master

Fix gsl::ensure_z() to work for const char* strings
This commit is contained in:
Neil MacIntosh 2015-11-02 11:51:13 -08:00
commit 281e36ef3c
3 changed files with 25 additions and 4 deletions

13
.gitignore vendored
View File

@ -1 +1,14 @@
tests/unittest-cpp tests/unittest-cpp
CMakeFiles
tests/CMakeFiles
tests/Debug
*.opensdf
*.sdf
tests/*tests.dir
*.vcxproj
*.vcxproj.filters
*.sln
*.tlog
Testing/Temporary/*.*
CMakeCache.txt
*.suo

View File

@ -82,9 +82,9 @@ template<class T, class SizeType, const T Sentinel>
array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max()) array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max())
{ {
auto cur = seq; auto cur = seq;
while ((cur - seq) < max && *cur != Sentinel) ++cur; while (SizeType(cur - seq) < max && *cur != Sentinel) ++cur;
fail_fast_assert(*cur == Sentinel); fail_fast_assert(*cur == Sentinel);
return{ seq, cur - seq }; return{ seq, SizeType(cur - seq) };
} }
@ -96,7 +96,7 @@ array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::n
template<class T> template<class T>
inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, size_t max = std::numeric_limits<size_t>::max()) inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, size_t max = std::numeric_limits<size_t>::max())
{ {
return ensure_sentinel<0>(sz, max); return ensure_sentinel<T, size_t, 0>(sz, max);
} }
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation // TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation

View File

@ -79,6 +79,14 @@ SUITE(string_view_tests)
} }
} }
TEST(TestConstructFromConstCharPointer)
{
const char* s = "Hello";
cstring_view<> v = ensure_z(s);
CHECK(v.length() == 5);
CHECK(v.used_length() == v.length());
}
TEST(TestConversionToConst) TEST(TestConversionToConst)
{ {
char stack_string[] = "Hello"; char stack_string[] = "Hello";