Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,16 @@ public:
}
#endif // _HAS_CXX17

_CONSTEXPR20 basic_string& append(
_In_reads_(_Count) const _Elem* const _Ptr, const size_type _Off, _CRT_GUARDOVERFLOW const size_type _Count) {
// append(string_view(_Ptr).substr(_Off, _Count))
const size_type _Length = _Convert_size<size_type>(_Traits::length(_Ptr));
if (_Off > _Length) {
_Scary_val::_Xran();
}
return _Append(_Ptr + _Off, (_STD min) (_Length - _Off, _Count));
}

_CONSTEXPR20 basic_string& append(
_In_reads_(_Count) const _Elem* const _Ptr, _CRT_GUARDOVERFLOW const size_type _Count) {
// append [_Ptr, _Ptr + _Count)
Expand Down Expand Up @@ -1645,6 +1655,16 @@ public:
}
#endif // _HAS_CXX17

_CONSTEXPR20 basic_string& assign(
_In_reads_(_Count) const _Elem* const _Ptr, const size_type _Off, _CRT_GUARDOVERFLOW const size_type _Count) {
// assign(string_view(_Ptr).substr(_Off, _Count))
const size_type _Length = _Convert_size<size_type>(_Traits::length(_Ptr));
if (_Off > _Length) {
_Scary_val::_Xran();
}
return _Assign(_Ptr + _Off, (_STD min) (_Length - _Off, _Count));
}

_CONSTEXPR20 basic_string& assign(
_In_reads_(_Count) const _Elem* const _Ptr, _CRT_GUARDOVERFLOW const size_type _Count) {
// assign [_Ptr, _Ptr + _Count)
Expand Down
45 changes: 45 additions & 0 deletions tests/std/tests/VSO_0174871_string_replace/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ void test_shrink_to_fit() {
assert(example == longerStr);
}

template <typename T = char>
struct CharAllocator {
using value_type = T;

CharAllocator() = delete;
explicit CharAllocator(int) noexcept {}
template <typename U>
CharAllocator(const CharAllocator<U>&) noexcept {}

T* allocate(std::size_t n) {
return new T[n];
}
void deallocate(T* p, std::size_t) noexcept {
delete[] p;
}
};

void test_LWG3662() {
// append/assign(NTBS, pos, n) should not construct a temporary string
basic_string<char, char_traits<char>, CharAllocator<char>> s(CharAllocator<char>(0));

s.append("hello", 1, 3);
assert(s == "ell");
s.assign("world", 1, 3);
assert(s == "orl");

s.clear();

try {
s.append("hello", 10, 1);
puts("append with out-of-range position should throw");
abort();
} catch (const out_of_range&) {
// purposely do nothing on out_of_range
}
try {
s.assign("world", 10, 1);
puts("assign with out-of-range position should throw");
abort();
} catch (const out_of_range&) {
// purposely do nothing on out_of_range
}
}

int main() {
// Plain replacements with shrinking / same size / growing
test_replace(3, 3, "ab", "012ab6789");
Expand Down Expand Up @@ -142,4 +186,5 @@ int main() {

test_index_boundary_cases();
test_shrink_to_fit();
test_LWG3662();
}