From 18f89ef0784fdebe24f163276c68713632995cdf Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Fri, 28 Feb 2020 14:35:16 -0500 Subject: [PATCH] Consistent initialization style --- include/boost/static_string/static_string.hpp | 92 ++++++++++--------- 1 file changed, 49 insertions(+), 43 deletions(-) diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index b7df809..4ad3e8e 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -1257,7 +1257,7 @@ public: at(size_type pos) { BOOST_STATIC_STRING_THROW_IF( - pos >= size(), std::out_of_range{"pos >= size()"}); + pos >= size(), std::out_of_range("pos >= size()")); return data()[pos]; } @@ -1270,7 +1270,7 @@ public: at(size_type pos) const { BOOST_STATIC_STRING_THROW_IF( - pos >= size(), std::out_of_range{"pos >= size()"}); + pos >= size(), std::out_of_range("pos >= size()")); return data()[pos]; } @@ -1526,7 +1526,7 @@ public: reserve(size_type n) { BOOST_STATIC_STRING_THROW_IF( - n > max_size(), std::length_error{"n > max_size()"}); + n > max_size(), std::length_error("n > max_size()")); } /** Returns the number of characters that can be held in currently allocated storage. @@ -1592,7 +1592,7 @@ public: value_type ch) { BOOST_STATIC_STRING_THROW_IF( - index > size(), std::out_of_range{"index > size()"}); + index > size(), std::out_of_range("index > size()")); insert(begin() + index, count, ch); return *this; } @@ -1658,7 +1658,7 @@ public: size_type count) { BOOST_STATIC_STRING_THROW_IF( - index > size(), std::out_of_range{"index > size()"}); + index > size(), std::out_of_range("index > size()")); insert(data() + index, s, s + count); return *this; } @@ -4235,7 +4235,7 @@ private: assign_char(value_type, std::false_type) { BOOST_STATIC_STRING_THROW( - std::length_error{"max_size() == 0"}); + std::length_error("max_size() == 0")); // This eliminates any potential warnings #ifdef BOOST_STATIC_STRING_NO_NORETURN return *this; @@ -4260,7 +4260,7 @@ private: size_type n2) { BOOST_STATIC_STRING_THROW_IF( - pos > size(), std::out_of_range{"pos > size()"}); + pos > size(), std::out_of_range("pos > size()")); return replace_unchecked(data() + pos, data() + pos + capped_length(pos, n1), s, n2); } @@ -4280,7 +4280,7 @@ private: size_type count) { BOOST_STATIC_STRING_THROW_IF( - index > size(), std::out_of_range{"index > size()"}); + index > size(), std::out_of_range("index > size()")); insert_unchecked(data() + index, s, count); return *this; } @@ -4310,7 +4310,7 @@ private: size_type length) const { BOOST_STATIC_STRING_THROW_IF( - index > size(), std::out_of_range{"index > size()"}); + index > size(), std::out_of_range("index > size()")); return (std::min)(length, size() - index); } }; @@ -4935,7 +4935,7 @@ basic_static_string(const_pointer s) { const auto count = traits_type::length(s); BOOST_STATIC_STRING_THROW_IF(count > max_size(), - std::length_error{"count > max_size()"}); + std::length_error("count > max_size()")); traits_type::copy(data(), s, count + 1); this->set_size(count); } @@ -4950,7 +4950,7 @@ assign( basic_static_string& { BOOST_STATIC_STRING_THROW_IF(count > max_size(), - std::length_error{"count > max_size()"}); + std::length_error("count > max_size()")); this->set_size(count); traits_type::assign(data(), size(), ch); return term(); @@ -4966,7 +4966,7 @@ assign( basic_static_string& { BOOST_STATIC_STRING_THROW_IF(count > max_size(), - std::length_error{"count > max_size()"}); + std::length_error("count > max_size()")); this->set_size(count); traits_type::move(data(), s, size()); return term(); @@ -4991,7 +4991,7 @@ assign( { this->set_size(i); term(); - BOOST_STATIC_STRING_THROW(std::length_error{"n > max_size()"}); + BOOST_STATIC_STRING_THROW(std::length_error("n > max_size()")); } traits_type::assign(*ptr, *first); } @@ -5012,7 +5012,7 @@ insert( const auto curr_size = size(); const auto curr_data = data(); BOOST_STATIC_STRING_THROW_IF( - count > max_size() - curr_size, std::length_error{"count() > max_size() - size()"}); + count > max_size() - curr_size, std::length_error("count() > max_size() - size()")); const auto index = pos - curr_data; traits_type::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); traits_type::assign(&curr_data[index], count, ch); @@ -5038,31 +5038,37 @@ insert( const std::size_t count = detail::distance(first, last); const std::size_t index = pos - curr_data; const auto first_addr = &*first; - BOOST_STATIC_STRING_THROW_IF( - count > max_size() - curr_size, std::length_error{"count > max_size() - size()"}); - const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, first_addr); - if (!inside || (inside && (first_addr + count <= pos))) - { - traits_type::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); - detail::copy_with_traits(first, last, &curr_data[index]); - } + // gcc's static analyzer does not recognize exit via + // exception as non-evaluation + if (count > max_size() - curr_size) + BOOST_STATIC_STRING_THROW( + std::length_error("count > max_size() - size()")); else { - const size_type offset = first_addr - curr_data; - traits_type::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); - if (offset < index) + const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, first_addr); + if (!inside || (inside && (first_addr + count <= pos))) { - const size_type diff = index - offset; - traits_type::copy(&curr_data[index], &curr_data[offset], diff); - traits_type::copy(&curr_data[index + diff], &curr_data[index + count], count - diff); + traits_type::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); + detail::copy_with_traits(first, last, &curr_data[index]); } else { - traits_type::copy(&curr_data[index], &curr_data[offset + count], count); + const size_type offset = first_addr - curr_data; + traits_type::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); + if (offset < index) + { + const size_type diff = index - offset; + traits_type::copy(&curr_data[index], &curr_data[offset], diff); + traits_type::copy(&curr_data[index + diff], &curr_data[index + count], count - diff); + } + else + { + traits_type::copy(&curr_data[index], &curr_data[offset + count], count); + } } + this->set_size(curr_size + count); + return curr_data + index; } - this->set_size(curr_size + count); - return curr_data + index; } template @@ -5114,7 +5120,7 @@ push_back( { const auto curr_size = size(); BOOST_STATIC_STRING_THROW_IF( - curr_size >= max_size(), std::length_error{"size() >= max_size()"}); + curr_size >= max_size(), std::length_error("size() >= max_size()")); traits_type::assign(data()[curr_size], ch); this->set_size(curr_size + 1); term(); @@ -5131,7 +5137,7 @@ append( { const auto curr_size = size(); BOOST_STATIC_STRING_THROW_IF( - count > max_size() - curr_size, std::length_error{"count > max_size() - size()"}); + count > max_size() - curr_size, std::length_error("count > max_size() - size()")); traits_type::assign(end(), count, ch); this->set_size(curr_size + count); return term(); @@ -5148,7 +5154,7 @@ append( { const auto curr_size = size(); BOOST_STATIC_STRING_THROW_IF( - count > max_size() - curr_size, std::length_error{"count > max_size() - size()"}); + count > max_size() - curr_size, std::length_error("count > max_size() - size()")); traits_type::copy(end(), s, count); this->set_size(curr_size + count); return term(); @@ -5161,7 +5167,7 @@ basic_static_string:: resize(size_type n, value_type c) { BOOST_STATIC_STRING_THROW_IF( - n > max_size(), std::length_error{"n > max_size()"}); + n > max_size(), std::length_error("n > max_size()")); const auto curr_size = size(); if(n > curr_size) traits_type::assign(data() + curr_size, n - curr_size, c); @@ -5192,9 +5198,9 @@ swap(basic_static_string& s) { const auto curr_size = size(); BOOST_STATIC_STRING_THROW_IF( - curr_size > s.max_size(), std::length_error{"size() > s.max_size()"}); + curr_size > s.max_size(), std::length_error("size() > s.max_size()")); BOOST_STATIC_STRING_THROW_IF( - s.size() > max_size(), std::length_error{"s.size() > max_size()"}); + s.size() > max_size(), std::length_error("s.size() > max_size()")); basic_static_string tmp(s); s.set_size(curr_size); traits_type::copy(&s.data()[0], data(), curr_size + 1); @@ -5219,7 +5225,7 @@ replace( BOOST_STATIC_STRING_THROW_IF( n > max_size() || curr_size - n1 >= max_size() - n, - std::length_error{"replaced string exceeds max_size()"}); + std::length_error("replaced string exceeds max_size()")); const auto pos = i1 - curr_data; traits_type::move(&curr_data[pos + n], i2, (end() - i2) + 1); traits_type::assign(&curr_data[pos], n, c); @@ -5250,7 +5256,7 @@ replace( BOOST_STATIC_STRING_THROW_IF( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, - std::length_error{"replaced string exceeds max_size()"}); + std::length_error("replaced string exceeds max_size()")); const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, first_addr); if (inside && first_addr == i1 && n1 == n2) return *this; @@ -5314,7 +5320,7 @@ replace( BOOST_STATIC_STRING_THROW_IF( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, - std::length_error{"replaced string exceeds max_size()"}); + std::length_error("replaced string exceeds max_size()")); // Rotate to the correct order. [i2, end] will now start with the replaced string, // continue to the existing string not being replaced, and end with a null terminator std::rotate(&curr_data[pos], &curr_data[curr_size + 1], &curr_data[curr_size + n2 + 1]); @@ -5464,7 +5470,7 @@ read_back( if (overwrite_null) term(); BOOST_STATIC_STRING_THROW( - std::length_error{"count > max_size() - size()"}); + std::length_error("count > max_size() - size()")); } traits_type::assign(curr_data[new_size++ + (!overwrite_null)], *first); } @@ -5489,7 +5495,7 @@ replace_unchecked( BOOST_STATIC_STRING_THROW_IF( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, - std::length_error{"replaced string exceeds max_size()"}); + std::length_error("replaced string exceeds max_size()")); traits_type::move(&curr_data[pos + n2], i2, (end() - i2) + 1); traits_type::copy(&curr_data[pos], s, n2); this->set_size((curr_size - n1) + n2); @@ -5510,7 +5516,7 @@ insert_unchecked( const auto curr_size = size(); BOOST_STATIC_STRING_THROW_IF( count > max_size() - curr_size, - std::length_error{"count > max_size() - size()"}); + std::length_error("count > max_size() - size()")); const std::size_t index = pos - curr_data; traits_type::move(&curr_data[index + count], pos, (end() - pos) + 1); traits_type::copy(&curr_data[index], s, count);