Skip to content

Commit de20ad9

Browse files
committed
Add exceptions
1 parent c826f6d commit de20ad9

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/xtd.core/include/xtd/span.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ namespace xtd {
119119
/// @name Public Properties
120120

121121
/// @{
122-
const_reference back() const noexcept {return *(data_ + size_ - 1);}
122+
const_reference back() const noexcept {
123+
if (empty()) throw argument_out_of_range_exception {};
124+
return *(data_ + size_ - 1);
125+
}
123126

124127
const_iterator begin() const {return cbegin();}
125128
iterator begin() {return iterator {data_};}
@@ -137,7 +140,10 @@ namespace xtd {
137140
const_iterator end() const {return cend();}
138141
iterator end() {return iterator {data_ + size_};}
139142

140-
const_reference front() const noexcept {return *data_;}
143+
const_reference front() const noexcept {
144+
if (empty()) throw argument_out_of_range_exception {};
145+
return *data_;
146+
}
141147

142148
const_reverse_iterator rbegin() const {return crbegin();}
143149
reverse_iterator rbegin() {return reverse_iterator {data_ + size_};}
@@ -163,12 +169,21 @@ namespace xtd {
163169
}
164170

165171
template<xtd::size count__>
166-
span<element_type, count__> first() const {return span<element_type, count__> {data_};}
172+
span<element_type, count__> first() const {
173+
if (count__ >= size_) throw argument_out_of_range_exception {};
174+
return span<element_type, count__> {data_};
175+
}
167176

168177
template<xtd::size count__>
169-
span<type_t, count__> last() const {return span<type_t, count__> {data_ + size_ - count__};}
178+
span<type_t, count__> last() const {
179+
if (count__ >= size_) throw argument_out_of_range_exception {};
180+
return span<type_t, count__> {data_ + size_ - count__};
181+
}
170182

171-
span<type_t> subspan(size_type offset, size_type count) const {return span<type_t> {data_ + offset, count};}
183+
span<type_t> subspan(size_type offset, size_type count) const {
184+
if (offset >= size_ || offset + count >= size_) throw argument_out_of_range_exception {};
185+
return span<type_t> {data_ + offset, count};
186+
}
172187

173188
string to_string() const noexcept override {return xtd::string::format("[{}]", xtd::string::join(", ", *this));}
174189
/// @}

0 commit comments

Comments
 (0)