Commit 720c017e by Abseil Team Committed by Titus Winters

Changes imported from Abseil "staging" branch:

  - a42e9b454ca8be7d021789cdb9bcada07d3e2d3e Merge pull request #57. by Derek Mauro <dmauro@google.com>
  - b1e03838f059c034a6489501804d516326246042 Move the long ostream tests into a separate source file u... by Alex Strelnikov <strel@google.com>
  - 7c56b7dbb05faa7e8653632e00be470331d79cb9 Return reference from absl::InlinedVector::emplace_back(). by Abseil Team <absl-team@google.com>
  - 85b070822b62688ff348d9ad9cc9e230a851f617 Treat \u or \U followed by Unicode surrogate character as... by Abseil Team <absl-team@google.com>

GitOrigin-RevId: a42e9b454ca8be7d021789cdb9bcada07d3e2d3e
Change-Id: I7d8fb68ffd7eb4e9e737f21fbed6d56b71985f94
parent 5fe41aff
...@@ -365,13 +365,14 @@ class InlinedVector { ...@@ -365,13 +365,14 @@ class InlinedVector {
// InlinedVector::emplace_back() // InlinedVector::emplace_back()
// //
// Constructs and appends an object to the inlined vector. // Constructs and appends an object to the inlined vector.
//
// Returns a reference to the inserted element.
template <typename... Args> template <typename... Args>
void emplace_back(Args&&... args) { value_type& emplace_back(Args&&... args) {
size_type s = size(); size_type s = size();
assert(s <= capacity()); assert(s <= capacity());
if (ABSL_PREDICT_FALSE(s == capacity())) { if (ABSL_PREDICT_FALSE(s == capacity())) {
GrowAndEmplaceBack(std::forward<Args>(args)...); return GrowAndEmplaceBack(std::forward<Args>(args)...);
return;
} }
assert(s < capacity()); assert(s < capacity());
...@@ -383,7 +384,7 @@ class InlinedVector { ...@@ -383,7 +384,7 @@ class InlinedVector {
tag().set_inline_size(s + 1); tag().set_inline_size(s + 1);
space = inlined_space(); space = inlined_space();
} }
Construct(space + s, std::forward<Args>(args)...); return Construct(space + s, std::forward<Args>(args)...);
} }
// InlinedVector::push_back() // InlinedVector::push_back()
...@@ -703,26 +704,30 @@ class InlinedVector { ...@@ -703,26 +704,30 @@ class InlinedVector {
} }
template <typename... Args> template <typename... Args>
void GrowAndEmplaceBack(Args&&... args) { value_type& GrowAndEmplaceBack(Args&&... args) {
assert(size() == capacity()); assert(size() == capacity());
const size_type s = size(); const size_type s = size();
Allocation new_allocation(allocator(), 2 * capacity()); Allocation new_allocation(allocator(), 2 * capacity());
value_type& new_element =
Construct(new_allocation.buffer() + s, std::forward<Args>(args)...); Construct(new_allocation.buffer() + s, std::forward<Args>(args)...);
UninitializedCopy(std::make_move_iterator(data()), UninitializedCopy(std::make_move_iterator(data()),
std::make_move_iterator(data() + s), std::make_move_iterator(data() + s),
new_allocation.buffer()); new_allocation.buffer());
ResetAllocation(new_allocation, s + 1); ResetAllocation(new_allocation, s + 1);
return new_element;
} }
void InitAssign(size_type n); void InitAssign(size_type n);
void InitAssign(size_type n, const value_type& t); void InitAssign(size_type n, const value_type& t);
template <typename... Args> template <typename... Args>
void Construct(pointer p, Args&&... args) { value_type& Construct(pointer p, Args&&... args) {
AllocatorTraits::construct(allocator(), p, std::forward<Args>(args)...); AllocatorTraits::construct(allocator(), p, std::forward<Args>(args)...);
return *p;
} }
template <typename Iter> template <typename Iter>
......
...@@ -393,6 +393,19 @@ TEST(InlinedVectorTest, Noexcept) { ...@@ -393,6 +393,19 @@ TEST(InlinedVectorTest, Noexcept) {
absl::InlinedVector<MoveCanThrow, 2>>::value)); absl::InlinedVector<MoveCanThrow, 2>>::value));
} }
TEST(InlinedVectorTest, EmplaceBack) {
absl::InlinedVector<std::pair<std::string, int>, 1> v;
auto& inlined_element = v.emplace_back("answer", 42);
EXPECT_EQ(&inlined_element, &v[0]);
EXPECT_EQ(inlined_element.first, "answer");
EXPECT_EQ(inlined_element.second, 42);
auto& allocated_element = v.emplace_back("taxicab", 1729);
EXPECT_EQ(&allocated_element, &v[1]);
EXPECT_EQ(allocated_element.first, "taxicab");
EXPECT_EQ(allocated_element.second, 1729);
}
TEST(IntVec, Insert) { TEST(IntVec, Insert) {
for (int len = 0; len < 20; len++) { for (int len = 0; len < 20; len++) {
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#error ABSL_STACKTRACE_INL_HEADER cannot be directly set #error ABSL_STACKTRACE_INL_HEADER cannot be directly set
#elif defined(__native_client__) || defined(__APPLE__) || \ #elif defined(__native_client__) || defined(__APPLE__) || \
defined(__FreeBSD__) || defined(__ANDROID__) || defined(__myriad2__) || \ defined(__FreeBSD__) || defined(__ANDROID__) || defined(__myriad2__) || \
defined(asmjs__) || defined(__Fuchsia__) defined(__asmjs__) || defined(__Fuchsia__)
#define ABSL_STACKTRACE_INL_HEADER \ #define ABSL_STACKTRACE_INL_HEADER \
"absl/debugging/internal/stacktrace_unimplemented-inl.inc" "absl/debugging/internal/stacktrace_unimplemented-inl.inc"
......
...@@ -27,8 +27,8 @@ cc_test( ...@@ -27,8 +27,8 @@ cc_test(
name = "int128_test", name = "int128_test",
size = "small", size = "small",
srcs = [ srcs = [
"int128_stream_test.cc",
"int128_test.cc", "int128_test.cc",
"int128_test_unsigned_ostream_cases.inc",
], ],
copts = ABSL_TEST_COPTS, copts = ABSL_TEST_COPTS,
deps = [ deps = [
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <random> #include <random>
#include <sstream>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -427,26 +426,4 @@ TEST(Uint128, ConstexprTest) { ...@@ -427,26 +426,4 @@ TEST(Uint128, ConstexprTest) {
EXPECT_EQ(minus_two, absl::MakeUint128(-1, -2)); EXPECT_EQ(minus_two, absl::MakeUint128(-1, -2));
} }
TEST(Uint128, OStream) {
struct StreamCase {
absl::uint128 val;
std::ios_base::fmtflags flags;
std::streamsize width;
char fill;
const char* rep;
};
std::vector<StreamCase> cases = {
#include "absl/numeric/int128_test_unsigned_ostream_cases.inc"
};
for (const StreamCase& test_case : cases) {
std::ostringstream os;
os.flags(test_case.flags);
os.width(test_case.width);
os.fill(test_case.fill);
os << test_case.val;
EXPECT_EQ(test_case.rep, os.str());
}
}
} // namespace } // namespace
...@@ -72,6 +72,17 @@ inline int hex_digit_to_int(char c) { ...@@ -72,6 +72,17 @@ inline int hex_digit_to_int(char c) {
return x & 0xf; return x & 0xf;
} }
inline bool IsSurrogate(char32_t c, absl::string_view src, std::string* error) {
if (c >= 0xD800 && c <= 0xDFFF) {
if (error) {
*error = absl::StrCat("invalid surrogate character (0xD800-DFFF): \\",
src);
}
return true;
}
return false;
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// CUnescapeInternal() // CUnescapeInternal()
// Implements both CUnescape() and CUnescapeForNullTerminatedString(). // Implements both CUnescape() and CUnescapeForNullTerminatedString().
...@@ -214,6 +225,9 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -214,6 +225,9 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
d += 5; d += 5;
break; break;
} }
if (IsSurrogate(rune, absl::string_view(hex_start, 5), error)) {
return false;
}
d += strings_internal::EncodeUTF8Char(d, rune); d += strings_internal::EncodeUTF8Char(d, rune);
break; break;
} }
...@@ -259,6 +273,9 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -259,6 +273,9 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
d += 9; d += 9;
break; break;
} }
if (IsSurrogate(rune, absl::string_view(hex_start, 9), error)) {
return false;
}
d += strings_internal::EncodeUTF8Char(d, rune); d += strings_internal::EncodeUTF8Char(d, rune);
break; break;
} }
......
...@@ -162,7 +162,10 @@ TEST(Unescape, BasicFunction) { ...@@ -162,7 +162,10 @@ TEST(Unescape, BasicFunction) {
std::string bad[] = std::string bad[] =
{"\\u1", // too short {"\\u1", // too short
"\\U1", // too short "\\U1", // too short
"\\Uffffff", "\\Uffffff", // exceeds 0x10ffff (largest Unicode)
"\\U00110000", // exceeds 0x10ffff (largest Unicode)
"\\uD835", // surrogate character (D800-DFFF)
"\\U0000DD04", // surrogate character (D800-DFFF)
"\\777", // exceeds 0xff "\\777", // exceeds 0xff
"\\xABCD"}; // exceeds 0xff "\\xABCD"}; // exceeds 0xff
for (const std::string& e : bad) { for (const std::string& e : bad) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment