diff --git a/include/gsl/span b/include/gsl/span index 1c8b2e5..2f076e0 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -128,7 +128,7 @@ namespace details constexpr span_iterator(const Span* span, typename Span::index_type index) GSL_NOEXCEPT : span_(span), index_(index) { - Expects(span == nullptr || (0 <= index_ && index <= span_->length())); + Expects(span == nullptr || (0 <= index_ && index <= span_->size())); } friend span_iterator; @@ -140,19 +140,19 @@ namespace details constexpr reference operator*() const GSL_NOEXCEPT { - Expects(index_ != span_->length()); + Expects(index_ != span_->size()); return *(span_->data() + index_); } constexpr pointer operator->() const GSL_NOEXCEPT { - Expects(index_ != span_->length()); + Expects(index_ != span_->size()); return span_->data() + index_; } constexpr span_iterator& operator++() GSL_NOEXCEPT { - Expects(0 <= index_ && index_ != span_->length()); + Expects(0 <= index_ && index_ != span_->size()); ++index_; return *this; } @@ -166,7 +166,7 @@ namespace details constexpr span_iterator& operator--() GSL_NOEXCEPT { - Expects(index_ != 0 && index_ <= span_->length()); + Expects(index_ != 0 && index_ <= span_->size()); --index_; return *this; } @@ -186,7 +186,7 @@ namespace details constexpr span_iterator& operator+=(difference_type n) GSL_NOEXCEPT { - Expects((index_ + n) >= 0 && (index_ + n) <= span_->length()); + Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); index_ += n; return *this; } @@ -461,9 +461,7 @@ public: // [span.obs], span observers - constexpr index_type length() const GSL_NOEXCEPT { return size(); } constexpr index_type size() const GSL_NOEXCEPT { return storage_.size(); } - constexpr index_type length_bytes() const GSL_NOEXCEPT { return size_bytes(); } constexpr index_type size_bytes() const GSL_NOEXCEPT { return size() * narrow_cast(sizeof(element_type)); @@ -483,10 +481,10 @@ public: // [span.iter], span iterator support constexpr iterator begin() const GSL_NOEXCEPT { return {this, 0}; } - constexpr iterator end() const GSL_NOEXCEPT { return {this, length()}; } + constexpr iterator end() const GSL_NOEXCEPT { return {this, size()}; } constexpr const_iterator cbegin() const GSL_NOEXCEPT { return {this, 0}; } - constexpr const_iterator cend() const GSL_NOEXCEPT { return {this, length()}; } + constexpr const_iterator cend() const GSL_NOEXCEPT { return {this, size()}; } constexpr reverse_iterator rbegin() const GSL_NOEXCEPT { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const GSL_NOEXCEPT { return reverse_iterator{begin()}; } diff --git a/include/gsl/string_span b/include/gsl/string_span index fa8b515..c56d136 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -145,7 +145,7 @@ template span::type, dynamic_extent> ensure_z(Cont& cont) { - return ensure_z(cont.data(), static_cast(cont.length())); + return ensure_z(cont.data(), static_cast(cont.size())); } template diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 3e487cd..ebbf5ef 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -51,33 +51,33 @@ TEST_CASE("default_constructor") { { span s; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span s; - CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile + CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span s{}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } @@ -98,60 +98,60 @@ TEST_CASE("from_nullptr_constructor") { { span s = nullptr; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs = nullptr; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s = nullptr; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs = nullptr; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span s = nullptr; - CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile + CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span s{nullptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s{nullptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } -TEST_CASE("from_nullptr_length_constructor") +TEST_CASE("from_nullptr_size_constructor") { { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { @@ -179,10 +179,10 @@ TEST_CASE("from_nullptr_length_constructor") { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } @@ -195,7 +195,7 @@ TEST_CASE("from_pointer_length_constructor") { { span s = { &arr[0], i }; - CHECK(s.length() == i); + CHECK(s.size() == i); CHECK(s.data() == &arr[0]); CHECK(s.empty() == (i == 0)); for (int j = 0; j < i; ++j) @@ -207,7 +207,7 @@ TEST_CASE("from_pointer_length_constructor") } { span s = { &arr[i], 4-i }; - CHECK(s.length() == 4-i); + CHECK(s.size() == 4-i); CHECK(s.data() == &arr[i]); CHECK(s.empty() == (4-i == 0)); for (int j = 0; j < 4-i; ++j) @@ -222,14 +222,14 @@ TEST_CASE("from_pointer_length_constructor") { span s{&arr[0], 2}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; span s{p, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { @@ -240,14 +240,14 @@ TEST_CASE("from_pointer_length_constructor") { auto s = make_span(&arr[0], 2); - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; auto s = make_span(p, static_cast::index_type>(0)); - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { @@ -263,24 +263,24 @@ TEST_CASE("from_pointer_pointer_constructor") { span s{&arr[0], &arr[2]}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span s{&arr[0], &arr[2]}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span s{&arr[0], &arr[0]}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { span s{&arr[0], &arr[0]}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds @@ -299,13 +299,13 @@ TEST_CASE("from_pointer_pointer_constructor") { int* p = nullptr; span s{p, p}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { int* p = nullptr; span s{p, p}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds @@ -317,19 +317,19 @@ TEST_CASE("from_pointer_pointer_constructor") { auto s = make_span(&arr[0], &arr[2]); - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { auto s = make_span(&arr[0], &arr[0]); - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { int* p = nullptr; auto s = make_span(p, p); - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } } @@ -339,12 +339,12 @@ TEST_CASE("from_array_constructor") { span s{arr}; - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } { span s{arr}; - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; @@ -356,18 +356,18 @@ TEST_CASE("from_array_constructor") { span s{arr}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { span s{arr2d}; - CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); + CHECK((s.size() == 6 && s.data() == &arr2d[0][0])); CHECK((s[0] == 1 && s[5] == 6)); } { span s{arr2d}; - CHECK((s.length() == 0 && s.data() == &arr2d[0][0])); + CHECK((s.size() == 0 && s.data() == &arr2d[0][0])); } { @@ -376,7 +376,7 @@ TEST_CASE("from_array_constructor") #endif { span s{&(arr2d[0]), 1}; - CHECK((s.length() == 1 && s.data() == &arr2d[0])); + CHECK((s.size() == 1 && s.data() == &arr2d[0])); } int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; @@ -384,13 +384,13 @@ TEST_CASE("from_array_constructor") #ifdef CONFIRM_COMPILATION_ERRORS { span s{arr3d}; - CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[11] == 12)); } { span s{arr3d}; - CHECK((s.length() == 0 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 0 && s.data() == &arr3d[0][0][0])); } { @@ -399,28 +399,28 @@ TEST_CASE("from_array_constructor") { span s{arr3d}; - CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[5] == 6)); } #endif { span s{&arr3d[0], 1}; - CHECK((s.length() == 1 && s.data() == &arr3d[0])); + CHECK((s.size() == 1 && s.data() == &arr3d[0])); } { auto s = make_span(arr); - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } { auto s = make_span(&(arr2d[0]), 1); - CHECK((s.length() == 1 && s.data() == &arr2d[0])); + CHECK((s.size() == 1 && s.data() == &arr2d[0])); } { auto s = make_span(&arr3d[0], 1); - CHECK((s.length() == 1 && s.data() == &arr3d[0])); + CHECK((s.size() == 1 && s.data() == &arr3d[0])); } } @@ -430,12 +430,12 @@ TEST_CASE("from_dynamic_array_constructor") { span s(&arr[0][0][0], 10); - CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); + CHECK((s.size() == 10 && s.data() == &arr[0][0][0])); } { auto s = make_span(&arr[0][0][0], 10); - CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); + CHECK((s.size() == 10 && s.data() == &arr[0][0][0])); } delete[] arr; @@ -723,17 +723,17 @@ TEST_CASE("copy_move_and_assignment") int arr[] = {3, 4, 5}; span s2 = arr; - CHECK((s2.length() == 3 && s2.data() == &arr[0])); + CHECK((s2.size() == 3 && s2.data() == &arr[0])); s2 = s1; CHECK(s2.empty()); auto get_temp_span = [&]() -> span { return {&arr[1], 2}; }; - auto use_span = [&](span s) { CHECK((s.length() == 2 && s.data() == &arr[1])); }; + auto use_span = [&](span s) { CHECK((s.size() == 2 && s.data() == &arr[1])); }; use_span(get_temp_span()); s1 = get_temp_span(); - CHECK((s1.length() == 2 && s1.data() == &arr[1])); + CHECK((s1.size() == 2 && s1.data() == &arr[1])); } TEST_CASE("first") @@ -742,35 +742,35 @@ TEST_CASE("first") { span av = arr; - CHECK(av.first<2>().length() == 2); - CHECK(av.first(2).length() == 2); + CHECK(av.first<2>().size() == 2); + CHECK(av.first(2).size() == 2); } { span av = arr; - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); + CHECK(av.first<0>().size() == 0); + CHECK(av.first(0).size() == 0); } { span av = arr; - CHECK(av.first<5>().length() == 5); - CHECK(av.first(5).length() == 5); + CHECK(av.first<5>().size() == 5); + CHECK(av.first(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK(av.first<6>().length() == 6); - CHECK(av.first<-1>().length() == -1); + CHECK(av.first<6>().size() == 6); + CHECK(av.first<-1>().size() == -1); #endif - CHECK_THROWS_AS(av.first(6).length(), fail_fast); + CHECK_THROWS_AS(av.first(6).size(), fail_fast); } { span av; - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); + CHECK(av.first<0>().size() == 0); + CHECK(av.first(0).size() == 0); } } @@ -780,34 +780,34 @@ TEST_CASE("last") { span av = arr; - CHECK(av.last<2>().length() == 2); - CHECK(av.last(2).length() == 2); + CHECK(av.last<2>().size() == 2); + CHECK(av.last(2).size() == 2); } { span av = arr; - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); + CHECK(av.last<0>().size() == 0); + CHECK(av.last(0).size() == 0); } { span av = arr; - CHECK(av.last<5>().length() == 5); - CHECK(av.last(5).length() == 5); + CHECK(av.last<5>().size() == 5); + CHECK(av.last(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK(av.last<6>().length() == 6); + CHECK(av.last<6>().size() == 6); #endif - CHECK_THROWS_AS(av.last(6).length(), fail_fast); + CHECK_THROWS_AS(av.last(6).size(), fail_fast); } { span av; - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); + CHECK(av.last<0>().size() == 0); + CHECK(av.last(0).size() == 0); } } @@ -817,64 +817,64 @@ TEST_CASE("subspan") { span av = arr; - CHECK((av.subspan<2, 2>().length() == 2)); - CHECK(av.subspan(2, 2).length() == 2); - CHECK(av.subspan(2, 3).length() == 3); + CHECK((av.subspan<2, 2>().size() == 2)); + CHECK(av.subspan(2, 2).size() == 2); + CHECK(av.subspan(2, 3).size() == 3); } { span av = arr; - CHECK((av.subspan<0, 0>().length() == 0)); - CHECK(av.subspan(0, 0).length() == 0); + CHECK((av.subspan<0, 0>().size() == 0)); + CHECK(av.subspan(0, 0).size() == 0); } { span av = arr; - CHECK((av.subspan<0, 5>().length() == 5)); - CHECK(av.subspan(0, 5).length() == 5); - CHECK_THROWS_AS(av.subspan(0, 6).length(), fail_fast); - CHECK_THROWS_AS(av.subspan(1, 5).length(), fail_fast); + CHECK((av.subspan<0, 5>().size() == 5)); + CHECK(av.subspan(0, 5).size() == 5); + CHECK_THROWS_AS(av.subspan(0, 6).size(), fail_fast); + CHECK_THROWS_AS(av.subspan(1, 5).size(), fail_fast); } { span av = arr; - CHECK((av.subspan<4, 0>().length() == 0)); - CHECK(av.subspan(4, 0).length() == 0); - CHECK(av.subspan(5, 0).length() == 0); - CHECK_THROWS_AS(av.subspan(6, 0).length(), fail_fast); + CHECK((av.subspan<4, 0>().size() == 0)); + CHECK(av.subspan(4, 0).size() == 0); + CHECK(av.subspan(5, 0).size() == 0); + CHECK_THROWS_AS(av.subspan(6, 0).size(), fail_fast); } { span av; - CHECK((av.subspan<0, 0>().length() == 0)); - CHECK(av.subspan(0, 0).length() == 0); - CHECK_THROWS_AS((av.subspan<1, 0>().length()), fail_fast); + CHECK((av.subspan<0, 0>().size() == 0)); + CHECK(av.subspan(0, 0).size() == 0); + CHECK_THROWS_AS((av.subspan<1, 0>().size()), fail_fast); } { span av; - CHECK(av.subspan(0).length() == 0); - CHECK_THROWS_AS(av.subspan(1).length(), fail_fast); + CHECK(av.subspan(0).size() == 0); + CHECK_THROWS_AS(av.subspan(1).size(), fail_fast); } { span av = arr; - CHECK(av.subspan(0).length() == 5); - CHECK(av.subspan(1).length() == 4); - CHECK(av.subspan(4).length() == 1); - CHECK(av.subspan(5).length() == 0); - CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); + CHECK(av.subspan(0).size() == 5); + CHECK(av.subspan(1).size() == 4); + CHECK(av.subspan(4).size() == 1); + CHECK(av.subspan(5).size() == 0); + CHECK_THROWS_AS(av.subspan(6).size(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } { span av = arr; - CHECK(av.subspan(0).length() == 5); - CHECK(av.subspan(1).length() == 4); - CHECK(av.subspan(4).length() == 1); - CHECK(av.subspan(5).length() == 0); - CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); + CHECK(av.subspan(0).size() == 5); + CHECK(av.subspan(1).size() == 4); + CHECK(av.subspan(4).size() == 1); + CHECK(av.subspan(5).size() == 0); + CHECK_THROWS_AS(av.subspan(6).size(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } @@ -1315,17 +1315,17 @@ TEST_CASE("as_bytes") { const span s = a; - CHECK(s.length() == 4); + CHECK(s.size() == 4); const span bs = as_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } { span s; const auto bs = as_bytes(s); - CHECK(bs.length() == s.length()); - CHECK(bs.length() == 0); + CHECK(bs.size() == s.size()); + CHECK(bs.size() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast(bs.data()) == static_cast(s.data())); CHECK(bs.data() == nullptr); @@ -1335,7 +1335,7 @@ TEST_CASE("as_bytes") span s = a; const auto bs = as_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } } @@ -1347,18 +1347,18 @@ TEST_CASE("as_writeable_bytes") #ifdef CONFIRM_COMPILATION_ERRORS // you should not be able to get writeable bytes for const objects span s = a; - CHECK(s.length() == 4); + CHECK(s.size() == 4); span bs = as_writeable_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); #endif } { span s; const auto bs = as_writeable_bytes(s); - CHECK(bs.length() == s.length()); - CHECK(bs.length() == 0); + CHECK(bs.size() == s.size()); + CHECK(bs.size() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast(bs.data()) == static_cast(s.data())); CHECK(bs.data() == nullptr); @@ -1368,7 +1368,7 @@ TEST_CASE("as_writeable_bytes") span s = a; const auto bs = as_writeable_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } } @@ -1378,12 +1378,12 @@ TEST_CASE("fixed_size_conversions") // converting to an span from an equal size array is ok span s4 = arr; - CHECK(s4.length() == 4); + CHECK(s4.size() == 4); // converting to dynamic_range is always ok { span s = s4; - CHECK(s.length() == s4.length()); + CHECK(s.size() == s4.size()); static_cast(s); }