From 4491d606df34c44efda47b6d17b605262f17e182 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 21 Jun 2018 12:55:12 -0700 Subject: Export of internal Abseil changes. -- 70f43a482d7d4ae4a255f17ca02b0106653dd600 by Shaindel Schwartz : Internal change PiperOrigin-RevId: 201571193 -- 93e6e9c2e683158be49d9dd1f5cb1a91d0c0f556 by Abseil Team : Internal change. PiperOrigin-RevId: 201567108 -- fbd8ee94fbe9f2448e5adf5e88706f9c8216048f by Juemin Yang : str_format release PiperOrigin-RevId: 201565129 -- 387faa301555a8a888c4429df52734aa806dca46 by Abseil Team : Adds a defaulted allocator parameter to the size_type constructor of InlinedVector PiperOrigin-RevId: 201558711 -- 39b15ea2c68d7129d70cbde7e71af900032595ec by Matt Calabrese : Update the variant implementation to eliminate unnecessary checking on alternative access when the index is known or required to be correct. PiperOrigin-RevId: 201529535 -- adab77f1f7bb363aa534297f22aae2b0f08889ea by Abseil Team : Import of CCTZ from GitHub. PiperOrigin-RevId: 201458388 -- a701dc0ba62e3cadf0de14203415b91df4ee8151 by Greg Falcon : Internal cleanup PiperOrigin-RevId: 201394836 -- 8a7191410b8f440fdfa27f722ff05e451502ab61 by Abseil Team : Import of CCTZ from GitHub. PiperOrigin-RevId: 201369269 GitOrigin-RevId: 70f43a482d7d4ae4a255f17ca02b0106653dd600 Change-Id: I8ab073b30b4e27405a3b6da2c826bb4f3f0b9af6 --- absl/strings/str_format_test.cc | 603 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 603 insertions(+) create mode 100644 absl/strings/str_format_test.cc (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc new file mode 100644 index 00000000..fe742bf9 --- /dev/null +++ b/absl/strings/str_format_test.cc @@ -0,0 +1,603 @@ + +#include +#include +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/strings/str_format.h" +#include "absl/strings/string_view.h" + +namespace absl { +namespace { +using str_format_internal::FormatArgImpl; + +class FormatEntryPointTest : public ::testing::Test { }; + +TEST_F(FormatEntryPointTest, Format) { + std::string sink; + EXPECT_TRUE(Format(&sink, "A format %d", 123)); + EXPECT_EQ("A format 123", sink); + sink.clear(); + + ParsedFormat<'d'> pc("A format %d"); + EXPECT_TRUE(Format(&sink, pc, 123)); + EXPECT_EQ("A format 123", sink); +} +TEST_F(FormatEntryPointTest, UntypedFormat) { + constexpr const char* formats[] = { + "", + "a", + "%80d", +#if !defined(_MSC_VER) && !defined(__ANDROID__) + // MSVC and Android don't support positional syntax. + "complicated multipart %% %1$d format %1$0999d", +#endif // _MSC_VER + }; + for (const char* fmt : formats) { + std::string actual; + int i = 123; + FormatArgImpl arg_123(i); + absl::Span args(&arg_123, 1); + UntypedFormatSpec format(fmt); + + EXPECT_TRUE(FormatUntyped(&actual, format, args)); + char buf[4096]{}; + snprintf(buf, sizeof(buf), fmt, 123); + EXPECT_EQ( + str_format_internal::FormatPack( + str_format_internal::UntypedFormatSpecImpl::Extract(format), args), + buf); + EXPECT_EQ(actual, buf); + } + // The internal version works with a preparsed format. + ParsedFormat<'d'> pc("A format %d"); + int i = 345; + FormatArg arg(i); + std::string out; + EXPECT_TRUE(str_format_internal::FormatUntyped( + &out, str_format_internal::UntypedFormatSpecImpl(&pc), {&arg, 1})); + EXPECT_EQ("A format 345", out); +} + +TEST_F(FormatEntryPointTest, StringFormat) { + EXPECT_EQ("123", StrFormat("%d", 123)); + constexpr absl::string_view view("=%d=", 4); + EXPECT_EQ("=123=", StrFormat(view, 123)); +} + +TEST_F(FormatEntryPointTest, AppendFormat) { + std::string s; + std::string& r = StrAppendFormat(&s, "%d", 123); + EXPECT_EQ(&s, &r); // should be same object + EXPECT_EQ("123", r); +} + +TEST_F(FormatEntryPointTest, AppendFormatFail) { + std::string s = "orig"; + + UntypedFormatSpec format(" more %d"); + FormatArgImpl arg("not an int"); + + EXPECT_EQ("orig", + str_format_internal::AppendPack( + &s, str_format_internal::UntypedFormatSpecImpl::Extract(format), + {&arg, 1})); +} + + +TEST_F(FormatEntryPointTest, ManyArgs) { + EXPECT_EQ("24", StrFormat("%24$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)); + EXPECT_EQ("60", StrFormat("%60$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + 53, 54, 55, 56, 57, 58, 59, 60)); +} + +TEST_F(FormatEntryPointTest, Preparsed) { + ParsedFormat<'d'> pc("%d"); + EXPECT_EQ("123", StrFormat(pc, 123)); + // rvalue ok? + EXPECT_EQ("123", StrFormat(ParsedFormat<'d'>("%d"), 123)); + constexpr absl::string_view view("=%d=", 4); + EXPECT_EQ("=123=", StrFormat(ParsedFormat<'d'>(view), 123)); +} + +TEST_F(FormatEntryPointTest, FormatCountCapture) { + int n = 0; + EXPECT_EQ("", StrFormat("%n", FormatCountCapture(&n))); + EXPECT_EQ(0, n); + EXPECT_EQ("123", StrFormat("%d%n", 123, FormatCountCapture(&n))); + EXPECT_EQ(3, n); +} + +TEST_F(FormatEntryPointTest, FormatCountCaptureWrongType) { + // Should reject int*. + int n = 0; + UntypedFormatSpec format("%d%n"); + int i = 123, *ip = &n; + FormatArgImpl args[2] = {FormatArgImpl(i), FormatArgImpl(ip)}; + + EXPECT_EQ("", str_format_internal::FormatPack( + str_format_internal::UntypedFormatSpecImpl::Extract(format), + absl::MakeSpan(args))); +} + +TEST_F(FormatEntryPointTest, FormatCountCaptureMultiple) { + int n1 = 0; + int n2 = 0; + EXPECT_EQ(" 1 2", + StrFormat("%5d%n%10d%n", 1, FormatCountCapture(&n1), 2, + FormatCountCapture(&n2))); + EXPECT_EQ(5, n1); + EXPECT_EQ(15, n2); +} + +TEST_F(FormatEntryPointTest, FormatCountCaptureExample) { + int n; + std::string s; + StrAppendFormat(&s, "%s: %n%s\n", "(1,1)", FormatCountCapture(&n), "(1,2)"); + StrAppendFormat(&s, "%*s%s\n", n, "", "(2,2)"); + EXPECT_EQ(7, n); + EXPECT_EQ( + "(1,1): (1,2)\n" + " (2,2)\n", + s); +} + +TEST_F(FormatEntryPointTest, Stream) { + const std::string formats[] = { + "", + "a", + "%80d", +#if !defined(_MSC_VER) && !defined(__ANDROID__) + // MSVC doesn't support positional syntax. + "complicated multipart %% %1$d format %1$080d", +#endif // _MSC_VER + }; + std::string buf(4096, '\0'); + for (const auto& fmt : formats) { + const auto parsed = ParsedFormat<'d'>::NewAllowIgnored(fmt); + std::ostringstream oss; + oss << StreamFormat(*parsed, 123); + int fmt_result = snprintf(&*buf.begin(), buf.size(), fmt.c_str(), 123); + ASSERT_TRUE(oss) << fmt; + ASSERT_TRUE(fmt_result >= 0 && static_cast(fmt_result) < buf.size()) + << fmt_result; + EXPECT_EQ(buf.c_str(), oss.str()); + } +} + +TEST_F(FormatEntryPointTest, StreamOk) { + std::ostringstream oss; + oss << StreamFormat("hello %d", 123); + EXPECT_EQ("hello 123", oss.str()); + EXPECT_TRUE(oss.good()); +} + +TEST_F(FormatEntryPointTest, StreamFail) { + std::ostringstream oss; + UntypedFormatSpec format("hello %d"); + FormatArgImpl arg("non-numeric"); + oss << str_format_internal::Streamable( + str_format_internal::UntypedFormatSpecImpl::Extract(format), {&arg, 1}); + EXPECT_EQ("hello ", oss.str()); // partial write + EXPECT_TRUE(oss.fail()); +} + +std::string WithSnprintf(const char* fmt, ...) { + std::string buf; + buf.resize(128); + va_list va; + va_start(va, fmt); + int r = vsnprintf(&*buf.begin(), buf.size(), fmt, va); + va_end(va); + EXPECT_GE(r, 0); + EXPECT_LT(r, buf.size()); + buf.resize(r); + return buf; +} + +TEST_F(FormatEntryPointTest, FloatPrecisionArg) { + // Test that positional parameters for width and precision + // are indexed to precede the value. + // Also sanity check the same formats against snprintf. + EXPECT_EQ("0.1", StrFormat("%.1f", 0.1)); + EXPECT_EQ("0.1", WithSnprintf("%.1f", 0.1)); + EXPECT_EQ(" 0.1", StrFormat("%*.1f", 5, 0.1)); + EXPECT_EQ(" 0.1", WithSnprintf("%*.1f", 5, 0.1)); + EXPECT_EQ("0.1", StrFormat("%.*f", 1, 0.1)); + EXPECT_EQ("0.1", WithSnprintf("%.*f", 1, 0.1)); + EXPECT_EQ(" 0.1", StrFormat("%*.*f", 5, 1, 0.1)); + EXPECT_EQ(" 0.1", WithSnprintf("%*.*f", 5, 1, 0.1)); +} +namespace streamed_test { +struct X {}; +std::ostream& operator<<(std::ostream& os, const X&) { + return os << "X"; +} +} // streamed_test + +TEST_F(FormatEntryPointTest, FormatStreamed) { + EXPECT_EQ("123", StrFormat("%s", FormatStreamed(123))); + EXPECT_EQ(" 123", StrFormat("%5s", FormatStreamed(123))); + EXPECT_EQ("123 ", StrFormat("%-5s", FormatStreamed(123))); + EXPECT_EQ("X", StrFormat("%s", FormatStreamed(streamed_test::X()))); + EXPECT_EQ("123", StrFormat("%s", FormatStreamed(StreamFormat("%d", 123)))); +} + +// Helper class that creates a temporary file and exposes a FILE* to it. +// It will close the file on destruction. +class TempFile { + public: + TempFile() : file_(std::tmpfile()) {} + ~TempFile() { std::fclose(file_); } + + std::FILE* file() const { return file_; } + + // Read the file into a std::string. + std::string ReadFile() { + std::fseek(file_, 0, SEEK_END); + int size = std::ftell(file_); + std::rewind(file_); + std::string str(2 * size, ' '); + int read_bytes = std::fread(&str[0], 1, str.size(), file_); + EXPECT_EQ(read_bytes, size); + str.resize(read_bytes); + EXPECT_TRUE(std::feof(file_)); + return str; + } + + private: + std::FILE* file_; +}; + +TEST_F(FormatEntryPointTest, FPrintF) { + TempFile tmp; + int result = + FPrintF(tmp.file(), "STRING: %s NUMBER: %010d", std::string("ABC"), -19); + EXPECT_EQ(result, 30); + EXPECT_EQ(tmp.ReadFile(), "STRING: ABC NUMBER: -000000019"); +} + +TEST_F(FormatEntryPointTest, FPrintFError) { + errno = 0; + int result = FPrintF(stdin, "ABC"); + EXPECT_LT(result, 0); + EXPECT_EQ(errno, EBADF); +} + +#if __GNUC__ +TEST_F(FormatEntryPointTest, FprintfTooLarge) { + std::FILE* f = std::fopen("/dev/null", "w"); + int width = 2000000000; + errno = 0; + int result = FPrintF(f, "%*d %*d", width, 0, width, 0); + EXPECT_LT(result, 0); + EXPECT_EQ(errno, EFBIG); + std::fclose(f); +} + +TEST_F(FormatEntryPointTest, PrintF) { + int stdout_tmp = dup(STDOUT_FILENO); + + TempFile tmp; + std::fflush(stdout); + dup2(fileno(tmp.file()), STDOUT_FILENO); + + int result = PrintF("STRING: %s NUMBER: %010d", std::string("ABC"), -19); + + std::fflush(stdout); + dup2(stdout_tmp, STDOUT_FILENO); + close(stdout_tmp); + + EXPECT_EQ(result, 30); + EXPECT_EQ(tmp.ReadFile(), "STRING: ABC NUMBER: -000000019"); +} +#endif // __GNUC__ + +TEST_F(FormatEntryPointTest, SNPrintF) { + char buffer[16]; + int result = + SNPrintF(buffer, sizeof(buffer), "STRING: %s", std::string("ABC")); + EXPECT_EQ(result, 11); + EXPECT_EQ(std::string(buffer), "STRING: ABC"); + + result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 123456); + EXPECT_EQ(result, 14); + EXPECT_EQ(std::string(buffer), "NUMBER: 123456"); + + result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 1234567); + EXPECT_EQ(result, 15); + EXPECT_EQ(std::string(buffer), "NUMBER: 1234567"); + + result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 12345678); + EXPECT_EQ(result, 16); + EXPECT_EQ(std::string(buffer), "NUMBER: 1234567"); + + result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 123456789); + EXPECT_EQ(result, 17); + EXPECT_EQ(std::string(buffer), "NUMBER: 1234567"); + + result = SNPrintF(nullptr, 0, "Just checking the %s of the output.", "size"); + EXPECT_EQ(result, 37); +} + +TEST(StrFormat, BehavesAsDocumented) { + std::string s = absl::StrFormat("%s, %d!", "Hello", 123); + EXPECT_EQ("Hello, 123!", s); + // The format of a replacement is + // '%'[position][flags][width['.'precision]][length_modifier][format] + EXPECT_EQ(absl::StrFormat("%1$+3.2Lf", 1.1), "+1.10"); + // Text conversion: + // "c" - Character. Eg: 'a' -> "A", 20 -> " " + EXPECT_EQ(StrFormat("%c", 'a'), "a"); + EXPECT_EQ(StrFormat("%c", 0x20), " "); + // Formats char and integral types: int, long, uint64_t, etc. + EXPECT_EQ(StrFormat("%c", int{'a'}), "a"); + EXPECT_EQ(StrFormat("%c", long{'a'}), "a"); // NOLINT + EXPECT_EQ(StrFormat("%c", uint64_t{'a'}), "a"); + // "s" - std::string Eg: "C" -> "C", std::string("C++") -> "C++" + // Formats std::string, char*, string_view, and Cord. + EXPECT_EQ(StrFormat("%s", "C"), "C"); + EXPECT_EQ(StrFormat("%s", std::string("C++")), "C++"); + EXPECT_EQ(StrFormat("%s", string_view("view")), "view"); + // Integral Conversion + // These format integral types: char, int, long, uint64_t, etc. + EXPECT_EQ(StrFormat("%d", char{10}), "10"); + EXPECT_EQ(StrFormat("%d", int{10}), "10"); + EXPECT_EQ(StrFormat("%d", long{10}), "10"); // NOLINT + EXPECT_EQ(StrFormat("%d", uint64_t{10}), "10"); + // d,i - signed decimal Eg: -10 -> "-10" + EXPECT_EQ(StrFormat("%d", -10), "-10"); + EXPECT_EQ(StrFormat("%i", -10), "-10"); + // o - octal Eg: 10 -> "12" + EXPECT_EQ(StrFormat("%o", 10), "12"); + // u - unsigned decimal Eg: 10 -> "10" + EXPECT_EQ(StrFormat("%u", 10), "10"); + // x/X - lower,upper case hex Eg: 10 -> "a"/"A" + EXPECT_EQ(StrFormat("%x", 10), "a"); + EXPECT_EQ(StrFormat("%X", 10), "A"); + // Floating-point, with upper/lower-case output. + // These format floating points types: float, double, long double, etc. + EXPECT_EQ(StrFormat("%.1f", float{1}), "1.0"); + EXPECT_EQ(StrFormat("%.1f", double{1}), "1.0"); + const long double long_double = 1.0; + EXPECT_EQ(StrFormat("%.1f", long_double), "1.0"); + // These also format integral types: char, int, long, uint64_t, etc.: + EXPECT_EQ(StrFormat("%.1f", char{1}), "1.0"); + EXPECT_EQ(StrFormat("%.1f", int{1}), "1.0"); + EXPECT_EQ(StrFormat("%.1f", long{1}), "1.0"); // NOLINT + EXPECT_EQ(StrFormat("%.1f", uint64_t{1}), "1.0"); + // f/F - decimal. Eg: 123456789 -> "123456789.000000" + EXPECT_EQ(StrFormat("%f", 123456789), "123456789.000000"); + EXPECT_EQ(StrFormat("%F", 123456789), "123456789.000000"); + // e/E - exponentiated Eg: .01 -> "1.00000e-2"/"1.00000E-2" + EXPECT_EQ(StrFormat("%e", .01), "1.000000e-02"); + EXPECT_EQ(StrFormat("%E", .01), "1.000000E-02"); + // g/G - exponentiate to fit Eg: .01 -> "0.01", 1e10 ->"1e+10"/"1E+10" + EXPECT_EQ(StrFormat("%g", .01), "0.01"); + EXPECT_EQ(StrFormat("%g", 1e10), "1e+10"); + EXPECT_EQ(StrFormat("%G", 1e10), "1E+10"); + // a/A - lower,upper case hex Eg: -3.0 -> "-0x1.8p+1"/"-0X1.8P+1" + +// On NDK r16, there is a regression in hexfloat formatting. +#if !defined(__NDK_MAJOR__) || __NDK_MAJOR__ != 16 + EXPECT_EQ(StrFormat("%.1a", -3.0), "-0x1.8p+1"); // .1 to fix MSVC output + EXPECT_EQ(StrFormat("%.1A", -3.0), "-0X1.8P+1"); // .1 to fix MSVC output +#endif + + // Other conversion + int64_t value = 0x7ffdeb6; + auto ptr_value = static_cast(value); + const int& something = *reinterpret_cast(ptr_value); + EXPECT_EQ(StrFormat("%p", &something), StrFormat("0x%x", ptr_value)); + + // Output widths are supported, with optional flags. + EXPECT_EQ(StrFormat("%3d", 1), " 1"); + EXPECT_EQ(StrFormat("%3d", 123456), "123456"); + EXPECT_EQ(StrFormat("%06.2f", 1.234), "001.23"); + EXPECT_EQ(StrFormat("%+d", 1), "+1"); + EXPECT_EQ(StrFormat("% d", 1), " 1"); + EXPECT_EQ(StrFormat("%-4d", -1), "-1 "); + EXPECT_EQ(StrFormat("%#o", 10), "012"); + EXPECT_EQ(StrFormat("%#x", 15), "0xf"); + EXPECT_EQ(StrFormat("%04d", 8), "0008"); + // Posix positional substitution. + EXPECT_EQ(absl::StrFormat("%2$s, %3$s, %1$s!", "vici", "veni", "vidi"), + "veni, vidi, vici!"); + // Length modifiers are ignored. + EXPECT_EQ(StrFormat("%hhd", int{1}), "1"); + EXPECT_EQ(StrFormat("%hd", int{1}), "1"); + EXPECT_EQ(StrFormat("%ld", int{1}), "1"); + EXPECT_EQ(StrFormat("%lld", int{1}), "1"); + EXPECT_EQ(StrFormat("%Ld", int{1}), "1"); + EXPECT_EQ(StrFormat("%jd", int{1}), "1"); + EXPECT_EQ(StrFormat("%zd", int{1}), "1"); + EXPECT_EQ(StrFormat("%td", int{1}), "1"); + EXPECT_EQ(StrFormat("%qd", int{1}), "1"); +} + +using str_format_internal::ExtendedParsedFormat; +using str_format_internal::ParsedFormatBase; + +struct SummarizeConsumer { + std::string* out; + explicit SummarizeConsumer(std::string* out) : out(out) {} + + bool Append(string_view s) { + *out += "[" + std::string(s) + "]"; + return true; + } + + bool ConvertOne(const str_format_internal::UnboundConversion& conv, + string_view s) { + *out += "{"; + *out += std::string(s); + *out += ":"; + *out += std::to_string(conv.arg_position) + "$"; + if (conv.width.is_from_arg()) { + *out += std::to_string(conv.width.get_from_arg()) + "$*"; + } + if (conv.precision.is_from_arg()) { + *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*"; + } + *out += conv.conv.Char(); + *out += "}"; + return true; + } +}; + +std::string SummarizeParsedFormat(const ParsedFormatBase& pc) { + std::string out; + if (!pc.ProcessFormat(SummarizeConsumer(&out))) out += "!"; + return out; +} + +class ParsedFormatTest : public testing::Test {}; + +TEST_F(ParsedFormatTest, SimpleChecked) { + EXPECT_EQ("[ABC]{d:1$d}[DEF]", + SummarizeParsedFormat(ParsedFormat<'d'>("ABC%dDEF"))); + EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", + SummarizeParsedFormat(ParsedFormat<'s', 'd', 'f'>("%sFFF%dZZZ%f"))); + EXPECT_EQ("{s:1$s}[ ]{.*d:3$.2$*d}", + SummarizeParsedFormat(ParsedFormat<'s', '*', 'd'>("%s %.*d"))); +} + +TEST_F(ParsedFormatTest, SimpleUncheckedCorrect) { + auto f = ParsedFormat<'d'>::New("ABC%dDEF"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); + + std::string format = "%sFFF%dZZZ%f"; + auto f2 = ParsedFormat<'s', 'd', 'f'>::New(format); + + ASSERT_TRUE(f2); + EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); + + f2 = ParsedFormat<'s', 'd', 'f'>::New("%s %d %f"); + + ASSERT_TRUE(f2); + EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); + + auto star = ParsedFormat<'*', 'd'>::New("%*d"); + ASSERT_TRUE(star); + EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); + + auto dollar = ParsedFormat<'d', 's'>::New("%2$s %1$d"); + ASSERT_TRUE(dollar); + EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); + // with reuse + dollar = ParsedFormat<'d', 's'>::New("%2$s %1$d %1$d"); + ASSERT_TRUE(dollar); + EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}", + SummarizeParsedFormat(*dollar)); +} + +TEST_F(ParsedFormatTest, SimpleUncheckedIgnoredArgs) { + EXPECT_FALSE((ParsedFormat<'d', 's'>::New("ABC"))); + EXPECT_FALSE((ParsedFormat<'d', 's'>::New("%dABC"))); + EXPECT_FALSE((ParsedFormat<'d', 's'>::New("ABC%2$s"))); + auto f = ParsedFormat<'d', 's'>::NewAllowIgnored("ABC"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f)); + f = ParsedFormat<'d', 's'>::NewAllowIgnored("%dABC"); + ASSERT_TRUE(f); + EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f)); + f = ParsedFormat<'d', 's'>::NewAllowIgnored("ABC%2$s"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f)); +} + +TEST_F(ParsedFormatTest, SimpleUncheckedUnsupported) { + EXPECT_FALSE(ParsedFormat<'d'>::New("%1$d %1$x")); + EXPECT_FALSE(ParsedFormat<'x'>::New("%1$d %1$x")); +} + +TEST_F(ParsedFormatTest, SimpleUncheckedIncorrect) { + EXPECT_FALSE(ParsedFormat<'d'>::New("")); + + EXPECT_FALSE(ParsedFormat<'d'>::New("ABC%dDEF%d")); + + std::string format = "%sFFF%dZZZ%f"; + EXPECT_FALSE((ParsedFormat<'s', 'd', 'g'>::New(format))); +} + +using str_format_internal::Conv; + +TEST_F(ParsedFormatTest, UncheckedCorrect) { + auto f = ExtendedParsedFormat::New("ABC%dDEF"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); + + std::string format = "%sFFF%dZZZ%f"; + auto f2 = + ExtendedParsedFormat::New(format); + + ASSERT_TRUE(f2); + EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); + + f2 = ExtendedParsedFormat::New( + "%s %d %f"); + + ASSERT_TRUE(f2); + EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); + + auto star = ExtendedParsedFormat::New("%*d"); + ASSERT_TRUE(star); + EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); + + auto dollar = ExtendedParsedFormat::New("%2$s %1$d"); + ASSERT_TRUE(dollar); + EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); + // with reuse + dollar = ExtendedParsedFormat::New("%2$s %1$d %1$d"); + ASSERT_TRUE(dollar); + EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}", + SummarizeParsedFormat(*dollar)); +} + +TEST_F(ParsedFormatTest, UncheckedIgnoredArgs) { + EXPECT_FALSE((ExtendedParsedFormat::New("ABC"))); + EXPECT_FALSE((ExtendedParsedFormat::New("%dABC"))); + EXPECT_FALSE((ExtendedParsedFormat::New("ABC%2$s"))); + auto f = ExtendedParsedFormat::NewAllowIgnored("ABC"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f)); + f = ExtendedParsedFormat::NewAllowIgnored("%dABC"); + ASSERT_TRUE(f); + EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f)); + f = ExtendedParsedFormat::NewAllowIgnored("ABC%2$s"); + ASSERT_TRUE(f); + EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f)); +} + +TEST_F(ParsedFormatTest, UncheckedMultipleTypes) { + auto dx = ExtendedParsedFormat::New("%1$d %1$x"); + EXPECT_TRUE(dx); + EXPECT_EQ("{1$d:1$d}[ ]{1$x:1$x}", SummarizeParsedFormat(*dx)); + + dx = ExtendedParsedFormat::New("%1$d"); + EXPECT_TRUE(dx); + EXPECT_EQ("{1$d:1$d}", SummarizeParsedFormat(*dx)); +} + +TEST_F(ParsedFormatTest, UncheckedIncorrect) { + EXPECT_FALSE(ExtendedParsedFormat::New("")); + + EXPECT_FALSE(ExtendedParsedFormat::New("ABC%dDEF%d")); + + std::string format = "%sFFF%dZZZ%f"; + EXPECT_FALSE((ExtendedParsedFormat::New(format))); +} + +TEST_F(ParsedFormatTest, RegressionMixPositional) { + EXPECT_FALSE((ExtendedParsedFormat::New("%1$d %o"))); +} + +} // namespace +} // namespace absl -- cgit v1.2.3 From 02687955b7ca8fc02ada9b14bc247deeb108d341 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 12 Jul 2018 10:34:29 -0700 Subject: Export of internal Abseil changes. -- 898e99cae89ca4cc27f86f719148f020d521dd58 by Abseil Team : Update comment. PiperOrigin-RevId: 204323401 -- b9d14db8b8a9dfb0e1cfb5967aaa0de1c4e94c42 by Abseil Team : Internal change PiperOrigin-RevId: 204178059 -- f3b5a667611a588aa06fea9168e997ef5cffa7ac by Abseil Team : Fix a potential reinterpret_cast compile error in absl::InlinedVector The current code will trigger a reinterpret_cast error enhanced by llvm r336738. PiperOrigin-RevId: 204131536 -- cc87d7a8302ad4471c1a25781d6ec19c2ce1524e by Abseil Team : Internal change. PiperOrigin-RevId: 203979040 -- bc5cae3cfc72af1d3e786d5a3b59a47e205afec9 by Gennadiy Rozental : Internal: add internal logging hooks PiperOrigin-RevId: 203850605 -- 503655c248f557f677c920078613522b010e73c8 by Derek Mauro : Cleanup stacktrace_config.h Instead of listing the platforms that aren't supported, list the ones we do support, and fallback to stacktrace_unimplemented-inl.inc at the end. Previously any platform that wasn't listed gets "#error Not supported yet". GitHub issue #135 PiperOrigin-RevId: 203823079 -- cb69925c4cacc14558cf103a09f218c37f466a16 by Abseil Team : Fix a minor typo in absl::variant documentation. PiperOrigin-RevId: 203679877 -- 23a0e4db10039011fa5fd879fb73d2f2bbd17301 by Abseil Team : Format .bzl files with buildifier PiperOrigin-RevId: 203461813 -- 1ad02616bdb715dfdc7f1a73313e383f4ededa03 by Abseil Team : Make the absl::SleepFor() tests treat any successful retry within a 48x deadline as a total success, thereby reducing flakiness. PiperOrigin-RevId: 203401603 -- b3dccbbc6563ea0173527076a3fff21433d48f47 by Abseil Team : Replace config_setting.values{"compiler"} with config_setting.flag_values{"@bazel_tools//tools/cpp:compiler"} Due to changes in Bazel we need to change the way "compiler" is specified in config_setting. This will not change the behavior of the config_setting itself. PiperOrigin-RevId: 203345693 -- 170f1692537460a4ba1756d34852275899c2339b by Matt Armstrong : Address test flakiness in the TimeoutTest. The basic idea is to loop forever until the expected scheduling delays are observed (within reasonable bounds), and only then assert the other invariants. PiperOrigin-RevId: 203188162 GitOrigin-RevId: 898e99cae89ca4cc27f86f719148f020d521dd58 Change-Id: Ie853ec050afa3a04c519393afe666bc03e11dc87 --- absl/BUILD.bazel | 7 +- absl/base/BUILD.bazel | 1 + absl/base/CMakeLists.txt | 2 +- absl/base/internal/raw_logging.cc | 16 + absl/base/internal/raw_logging.h | 43 +++ absl/base/raw_logging_test.cc | 29 ++ absl/compiler_config_setting.bzl | 39 ++ absl/container/inlined_vector.h | 7 +- absl/copts.bzl | 1 - absl/debugging/internal/stacktrace_config.h | 37 +- absl/strings/str_format_test.cc | 4 +- absl/synchronization/mutex.h | 4 +- absl/synchronization/mutex_test.cc | 575 +++++++++++++++++----------- absl/time/clock_test.cc | 94 +++-- absl/types/variant.h | 2 +- 15 files changed, 577 insertions(+), 284 deletions(-) create mode 100644 absl/compiler_config_setting.bzl (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/BUILD.bazel b/absl/BUILD.bazel index 439addbf..edd0274c 100644 --- a/absl/BUILD.bazel +++ b/absl/BUILD.bazel @@ -18,11 +18,10 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache 2.0 -config_setting( +load(":compiler_config_setting.bzl", "create_llvm_config") + +create_llvm_config( name = "llvm_compiler", - values = { - "compiler": "llvm", - }, visibility = [":__subpackages__"], ) diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 35414a25..06d092eb 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -362,6 +362,7 @@ cc_test( copts = ABSL_TEST_COPTS, deps = [ ":base", + "//absl/strings", "@com_google_googletest//:gtest_main", ], ) diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index 303533e2..01d2af08 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt @@ -310,7 +310,7 @@ absl_test( # test raw_logging_test set(RAW_LOGGING_TEST_SRC "raw_logging_test.cc") -set(RAW_LOGGING_TEST_PUBLIC_LIBRARIES absl::base) +set(RAW_LOGGING_TEST_PUBLIC_LIBRARIES absl::base absl::strings) absl_test( TARGET diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc index 1ce13888..41101bd7 100644 --- a/absl/base/internal/raw_logging.cc +++ b/absl/base/internal/raw_logging.cc @@ -206,6 +206,15 @@ void RawLog(absl::LogSeverity severity, const char* file, int line, va_end(ap); } +// Non-formatting version of RawLog(). +// +// TODO(gfalcon): When string_view no longer depends on base, change this +// interface to take its message as a string_view instead. +static void DefaultInternalLog(absl::LogSeverity severity, const char* file, + int line, const std::string& message) { + RawLog(severity, file, line, "%s", message.c_str()); +} + bool RawLoggingFullySupported() { #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED return true; @@ -214,5 +223,12 @@ bool RawLoggingFullySupported() { #endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED } +ABSL_CONST_INIT absl::base_internal::AtomicHook + internal_log_function(DefaultInternalLog); + +void RegisterInternalLogFunction(InternalLogFunction func) { + internal_log_function.Store(func); +} + } // namespace raw_logging_internal } // namespace absl diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h index a2b7207a..67abfd30 100644 --- a/absl/base/internal/raw_logging.h +++ b/absl/base/internal/raw_logging.h @@ -19,7 +19,10 @@ #ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_ #define ABSL_BASE_INTERNAL_RAW_LOGGING_H_ +#include + #include "absl/base/attributes.h" +#include "absl/base/internal/atomic_hook.h" #include "absl/base/log_severity.h" #include "absl/base/macros.h" #include "absl/base/port.h" @@ -57,6 +60,34 @@ } \ } while (0) +// ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above, +// except that if the richer log library is linked into the binary, we dispatch +// to that instead. This is potentially useful for internal logging and +// assertions, where we are using RAW_LOG neither for its async-signal-safety +// nor for its non-allocating nature, but rather because raw logging has very +// few other dependencies. +// +// The API is a subset of the above: each macro only takes two arguments. Use +// StrCat if you need to build a richer message. +#define ABSL_INTERNAL_LOG(severity, message) \ + do { \ + constexpr const char* absl_raw_logging_internal_basename = \ + ::absl::raw_logging_internal::Basename(__FILE__, \ + sizeof(__FILE__) - 1); \ + ::absl::raw_logging_internal::internal_log_function( \ + ABSL_RAW_LOGGING_INTERNAL_##severity, \ + absl_raw_logging_internal_basename, __LINE__, message); \ + } while (0) + +#define ABSL_INTERNAL_CHECK(condition, message) \ + do { \ + if (ABSL_PREDICT_FALSE(!(condition))) { \ + std::string death_message = "Check " #condition " failed: "; \ + death_message += std::string(message); \ + ABSL_INTERNAL_LOG(FATAL, death_message); \ + } \ + } while (0) + #define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo #define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning #define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError @@ -131,6 +162,18 @@ using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file, using AbortHook = void (*)(const char* file, int line, const char* buf_start, const char* prefix_end, const char* buf_end); +// Internal logging function for ABSL_INTERNAL_LOG to dispatch to. +// +// TODO(gfalcon): When string_view no longer depends on base, change this +// interface to take its message as a string_view instead. +using InternalLogFunction = void (*)(absl::LogSeverity severity, + const char* file, int line, + const std::string& message); + +extern base_internal::AtomicHook internal_log_function; + +void RegisterInternalLogFunction(InternalLogFunction func); + } // namespace raw_logging_internal } // namespace absl diff --git a/absl/base/raw_logging_test.cc b/absl/base/raw_logging_test.cc index dae4b351..ebbc5db9 100644 --- a/absl/base/raw_logging_test.cc +++ b/absl/base/raw_logging_test.cc @@ -18,12 +18,20 @@ #include "absl/base/internal/raw_logging.h" +#include + #include "gtest/gtest.h" +#include "absl/strings/str_cat.h" namespace { TEST(RawLoggingCompilationTest, Log) { ABSL_RAW_LOG(INFO, "RAW INFO: %d", 1); + ABSL_RAW_LOG(INFO, "RAW INFO: %d %d", 1, 2); + ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d", 1, 2, 3); + ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d", 1, 2, 3, 4); + ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d %d", 1, 2, 3, 4, 5); + ABSL_RAW_LOG(WARNING, "RAW WARNING: %d", 1); ABSL_RAW_LOG(ERROR, "RAW ERROR: %d", 1); } @@ -47,4 +55,25 @@ TEST(RawLoggingDeathTest, LogFatal) { kExpectedDeathOutput); } +TEST(InternalLog, CompilationTest) { + ABSL_INTERNAL_LOG(INFO, "Internal Log"); + std::string log_msg = "Internal Log"; + ABSL_INTERNAL_LOG(INFO, log_msg); + + ABSL_INTERNAL_LOG(INFO, log_msg + " 2"); + + float d = 1.1f; + ABSL_INTERNAL_LOG(INFO, absl::StrCat("Internal log ", 3, " + ", d)); +} + +TEST(InternalLogDeathTest, FailingCheck) { + EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_CHECK(1 == 0, "explanation"), + kExpectedDeathOutput); +} + +TEST(InternalLogDeathTest, LogFatal) { + EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_LOG(FATAL, "my dog has fleas"), + kExpectedDeathOutput); +} + } // namespace diff --git a/absl/compiler_config_setting.bzl b/absl/compiler_config_setting.bzl new file mode 100644 index 00000000..b77c4f56 --- /dev/null +++ b/absl/compiler_config_setting.bzl @@ -0,0 +1,39 @@ +# +# Copyright 2018 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Creates config_setting that allows selecting based on 'compiler' value.""" + +def create_llvm_config(name, visibility): + # The "do_not_use_tools_cpp_compiler_present" attribute exists to + # distinguish between older versions of Bazel that do not support + # "@bazel_tools//tools/cpp:compiler" flag_value, and newer ones that do. + # In the future, the only way to select on the compiler will be through + # flag_values{"@bazel_tools//tools/cpp:compiler"} and the else branch can + # be removed. + if hasattr(cc_common, "do_not_use_tools_cpp_compiler_present"): + native.config_setting( + name = name, + flag_values = { + "@bazel_tools//tools/cpp:compiler": "llvm", + }, + visibility = visibility, + ) + else: + native.config_setting( + name = name, + values = {"compiler": "llvm"}, + visibility = visibility, + ) diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 03660f16..75d98027 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -689,11 +689,14 @@ class InlinedVector { new (&rep_.allocation_storage.allocation) Allocation(allocation); } + // TODO(absl-team): investigate whether the reinterpret_cast is appropriate. value_type* inlined_space() { - return reinterpret_cast(&rep_.inlined_storage.inlined); + return reinterpret_cast( + std::addressof(rep_.inlined_storage.inlined[0])); } const value_type* inlined_space() const { - return reinterpret_cast(&rep_.inlined_storage.inlined); + return reinterpret_cast( + std::addressof(rep_.inlined_storage.inlined[0])); } value_type* allocated_space() { return allocation().buffer(); } diff --git a/absl/copts.bzl b/absl/copts.bzl index 20c9b619..0168ac5a 100644 --- a/absl/copts.bzl +++ b/absl/copts.bzl @@ -31,7 +31,6 @@ GCC_TEST_FLAGS = [ "-Wno-unused-private-field", ] - # Docs on single flags is preceded by a comment. # Docs on groups of flags is preceded by ###. diff --git a/absl/debugging/internal/stacktrace_config.h b/absl/debugging/internal/stacktrace_config.h index 48adfccc..dd713da8 100644 --- a/absl/debugging/internal/stacktrace_config.h +++ b/absl/debugging/internal/stacktrace_config.h @@ -21,26 +21,16 @@ #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_ #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_ -// First, test platforms which only support a stub. -#if ABSL_STACKTRACE_INL_HEADER +#if defined(ABSL_STACKTRACE_INL_HEADER) #error ABSL_STACKTRACE_INL_HEADER cannot be directly set -#elif defined(__native_client__) || defined(__APPLE__) || \ - defined(__FreeBSD__) || defined(__ANDROID__) || defined(__myriad2__) || \ - defined(__asmjs__) || defined(__wasm__) || defined(__Fuchsia__) -#define ABSL_STACKTRACE_INL_HEADER \ - "absl/debugging/internal/stacktrace_unimplemented-inl.inc" -// Next, test for Mips and Windows. -// TODO(marmstrong): Mips case, remove the check for ABSL_STACKTRACE_INL_HEADER -#elif defined(__mips__) && !defined(ABSL_STACKTRACE_INL_HEADER) -#define ABSL_STACKTRACE_INL_HEADER \ - "absl/debugging/internal/stacktrace_unimplemented-inl.inc" -#elif defined(_WIN32) // windows +#elif defined(_WIN32) #define ABSL_STACKTRACE_INL_HEADER \ "absl/debugging/internal/stacktrace_win32-inl.inc" -// Finally, test NO_FRAME_POINTER. -#elif !defined(NO_FRAME_POINTER) +#elif defined(__linux__) && !defined(__ANDROID__) + +#if !defined(NO_FRAME_POINTER) # if defined(__i386__) || defined(__x86_64__) #define ABSL_STACKTRACE_INL_HEADER \ "absl/debugging/internal/stacktrace_x86-inl.inc" @@ -53,22 +43,27 @@ # elif defined(__arm__) #define ABSL_STACKTRACE_INL_HEADER \ "absl/debugging/internal/stacktrace_arm-inl.inc" +# else +#define ABSL_STACKTRACE_INL_HEADER \ + "absl/debugging/internal/stacktrace_unimplemented-inl.inc" # endif #else // defined(NO_FRAME_POINTER) # if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) #define ABSL_STACKTRACE_INL_HEADER \ - "absl/debugging/internal/stacktrace_unimplemented-inl.inc" + "absl/debugging/internal/stacktrace_generic-inl.inc" # elif defined(__ppc__) || defined(__PPC__) -// Use glibc's backtrace. #define ABSL_STACKTRACE_INL_HEADER \ "absl/debugging/internal/stacktrace_generic-inl.inc" -# elif defined(__arm__) -# error stacktrace without frame pointer is not supported on ARM +# else +#define ABSL_STACKTRACE_INL_HEADER \ + "absl/debugging/internal/stacktrace_unimplemented-inl.inc" # endif #endif // NO_FRAME_POINTER -#if !defined(ABSL_STACKTRACE_INL_HEADER) -#error Not supported yet +#else +#define ABSL_STACKTRACE_INL_HEADER \ + "absl/debugging/internal/stacktrace_unimplemented-inl.inc" + #endif #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_ diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index fe742bf9..fed75faf 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -384,8 +384,8 @@ TEST(StrFormat, BehavesAsDocumented) { EXPECT_EQ(StrFormat("%G", 1e10), "1E+10"); // a/A - lower,upper case hex Eg: -3.0 -> "-0x1.8p+1"/"-0X1.8P+1" -// On NDK r16, there is a regression in hexfloat formatting. -#if !defined(__NDK_MAJOR__) || __NDK_MAJOR__ != 16 +// On Android platform <=21, there is a regression in hexfloat formatting. +#if !defined(__ANDROID_API__) || __ANDROID_API__ > 21 EXPECT_EQ(StrFormat("%.1a", -3.0), "-0x1.8p+1"); // .1 to fix MSVC output EXPECT_EQ(StrFormat("%.1A", -3.0), "-0X1.8P+1"); // .1 to fix MSVC output #endif diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h index 840b9d6b..4d9e0312 100644 --- a/absl/synchronization/mutex.h +++ b/absl/synchronization/mutex.h @@ -24,7 +24,7 @@ // Unlike a `std::mutex`, the Abseil `Mutex` provides the following additional // features: // * Conditional predicates intrinsic to the `Mutex` object -// * Reader/writer locks, in addition to standard exclusive/writer locks +// * Shared/reader locks, in addition to standard exclusive/writer locks // * Deadlock detection and debug support. // // The following helper classes are also defined within this file: @@ -290,7 +290,7 @@ class LOCKABLE Mutex { // Mutex::ReaderLockWhen() // Mutex::WriterLockWhen() // - // Blocks until simultaneously both `cond` is `true` and this` Mutex` can + // Blocks until simultaneously both `cond` is `true` and this `Mutex` can // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is // logically equivalent to `*Lock(); Await();` though they may have different // performance characteristics. diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc index 53b93784..b2820e20 100644 --- a/absl/synchronization/mutex_test.cc +++ b/absl/synchronization/mutex_test.cc @@ -29,6 +29,7 @@ #include #include "gtest/gtest.h" +#include "absl/base/attributes.h" #include "absl/base/internal/raw_logging.h" #include "absl/base/internal/sysinfo.h" #include "absl/memory/memory.h" @@ -54,8 +55,8 @@ CreateDefaultPool() { // Hack to schedule a function to run on a thread pool thread after a // duration has elapsed. static void ScheduleAfter(absl::synchronization_internal::ThreadPool *tp, - const std::function &func, - absl::Duration after) { + absl::Duration after, + const std::function &func) { tp->Schedule([func, after] { absl::SleepFor(after); func(); @@ -1150,249 +1151,369 @@ TEST(Mutex, DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS { // and so never expires/passes, and one that will expire/pass in the near // future. -// Encapsulate a Mutex-protected bool with its associated Condition/CondVar. -class Cond { - public: - explicit Cond(bool use_deadline) : use_deadline_(use_deadline), c_(&b_) {} - - void Set(bool v) { - absl::MutexLock lock(&mu_); - b_ = v; +static absl::Duration TimeoutTestAllowedSchedulingDelay() { + // Note: we use a function here because Microsoft Visual Studio fails to + // properly initialize constexpr static absl::Duration variables. + return absl::Milliseconds(150); +} + +// Returns true if `actual_delay` is close enough to `expected_delay` to pass +// the timeouts/deadlines test. Otherwise, logs warnings and returns false. +ABSL_MUST_USE_RESULT +static bool DelayIsWithinBounds(absl::Duration expected_delay, + absl::Duration actual_delay) { + bool pass = true; + // Do not allow the observed delay to be less than expected. This may occur + // in practice due to clock skew or when the synchronization primitives use a + // different clock than absl::Now(), but these cases should be handled by the + // the retry mechanism in each TimeoutTest. + if (actual_delay < expected_delay) { + ABSL_RAW_LOG(WARNING, + "Actual delay %s was too short, expected %s (difference %s)", + absl::FormatDuration(actual_delay).c_str(), + absl::FormatDuration(expected_delay).c_str(), + absl::FormatDuration(actual_delay - expected_delay).c_str()); + pass = false; } - - bool AwaitWithTimeout(absl::Duration timeout) { - absl::MutexLock lock(&mu_); - return use_deadline_ ? mu_.AwaitWithDeadline(c_, absl::Now() + timeout) - : mu_.AwaitWithTimeout(c_, timeout); + // If the expected delay is <= zero then allow a small error tolerance, since + // we do not expect context switches to occur during test execution. + // Otherwise, thread scheduling delays may be substantial in rare cases, so + // tolerate up to kTimeoutTestAllowedSchedulingDelay of error. + absl::Duration tolerance = expected_delay <= absl::ZeroDuration() + ? absl::Milliseconds(10) + : TimeoutTestAllowedSchedulingDelay(); + if (actual_delay > expected_delay + tolerance) { + ABSL_RAW_LOG(WARNING, + "Actual delay %s was too long, expected %s (difference %s)", + absl::FormatDuration(actual_delay).c_str(), + absl::FormatDuration(expected_delay).c_str(), + absl::FormatDuration(actual_delay - expected_delay).c_str()); + pass = false; } + return pass; +} + +// Parameters for TimeoutTest, below. +struct TimeoutTestParam { + // The file and line number (used for logging purposes only). + const char *from_file; + int from_line; + + // Should the absolute deadline API based on absl::Time be tested? If false, + // the relative deadline API based on absl::Duration is tested. + bool use_absolute_deadline; + + // The deadline/timeout used when calling the API being tested + // (e.g. Mutex::LockWhenWithDeadline). + absl::Duration wait_timeout; + + // The delay before the condition will be set true by the test code. If zero + // or negative, the condition is set true immediately (before calling the API + // being tested). Otherwise, if infinite, the condition is never set true. + // Otherwise a closure is scheduled for the future that sets the condition + // true. + absl::Duration satisfy_condition_delay; + + // The expected result of the condition after the call to the API being + // tested. Generally `true` means the condition was true when the API returns, + // `false` indicates an expected timeout. + bool expected_result; + + // The expected delay before the API under test returns. This is inherently + // flaky, so some slop is allowed (see `DelayIsWithinBounds` above), and the + // test keeps trying indefinitely until this constraint passes. + absl::Duration expected_delay; +}; - bool LockWhenWithTimeout(absl::Duration timeout) { - bool b = use_deadline_ ? mu_.LockWhenWithDeadline(c_, absl::Now() + timeout) - : mu_.LockWhenWithTimeout(c_, timeout); - mu_.Unlock(); - return b; +// Print a `TimeoutTestParam` to a debug log. +std::ostream &operator<<(std::ostream &os, const TimeoutTestParam ¶m) { + return os << "from: " << param.from_file << ":" << param.from_line + << " use_absolute_deadline: " + << (param.use_absolute_deadline ? "true" : "false") + << " wait_timeout: " << param.wait_timeout + << " satisfy_condition_delay: " << param.satisfy_condition_delay + << " expected_result: " + << (param.expected_result ? "true" : "false") + << " expected_delay: " << param.expected_delay; +} + +std::string FormatString(const TimeoutTestParam ¶m) { + std::ostringstream os; + os << param; + return os.str(); +} + +// Like `thread::Executor::ScheduleAt` except: +// a) Delays zero or negative are executed immediately in the current thread. +// b) Infinite delays are never scheduled. +// c) Calls this test's `ScheduleAt` helper instead of using `pool` directly. +static void RunAfterDelay(absl::Duration delay, + absl::synchronization_internal::ThreadPool *pool, + const std::function &callback) { + if (delay <= absl::ZeroDuration()) { + callback(); // immediate + } else if (delay != absl::InfiniteDuration()) { + ScheduleAfter(pool, delay, callback); } +} - bool ReaderLockWhenWithTimeout(absl::Duration timeout) { - bool b = use_deadline_ - ? mu_.ReaderLockWhenWithDeadline(c_, absl::Now() + timeout) - : mu_.ReaderLockWhenWithTimeout(c_, timeout); - mu_.ReaderUnlock(); - return b; - } +class TimeoutTest : public ::testing::Test, + public ::testing::WithParamInterface {}; - void Await() { - absl::MutexLock lock(&mu_); - mu_.Await(c_); - } +std::vector MakeTimeoutTestParamValues() { + // The `finite` delay is a finite, relatively short, delay. We make it larger + // than our allowed scheduling delay (slop factor) to avoid confusion when + // diagnosing test failures. The other constants here have clear meanings. + const absl::Duration finite = 3 * TimeoutTestAllowedSchedulingDelay(); + const absl::Duration never = absl::InfiniteDuration(); + const absl::Duration negative = -absl::InfiniteDuration(); + const absl::Duration immediate = absl::ZeroDuration(); - void Signal(bool v) { - absl::MutexLock lock(&mu_); - b_ = v; - cv_.Signal(); + // Every test case is run twice; once using the absolute deadline API and once + // using the relative timeout API. + std::vector values; + for (bool use_absolute_deadline : {false, true}) { + // Tests with a negative timeout (deadline in the past), which should + // immediately return current state of the condition. + + // The condition is already true: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + negative, // wait_timeout + immediate, // satisfy_condition_delay + true, // expected_result + immediate, // expected_delay + }); + + // The condition becomes true, but the timeout has already expired: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + negative, // wait_timeout + finite, // satisfy_condition_delay + false, // expected_result + immediate // expected_delay + }); + + // The condition never becomes true: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + negative, // wait_timeout + never, // satisfy_condition_delay + false, // expected_result + immediate // expected_delay + }); + + // Tests with an infinite timeout (deadline in the infinite future), which + // should only return when the condition becomes true. + + // The condition is already true: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + never, // wait_timeout + immediate, // satisfy_condition_delay + true, // expected_result + immediate // expected_delay + }); + + // The condition becomes true before the (infinite) expiry: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + never, // wait_timeout + finite, // satisfy_condition_delay + true, // expected_result + finite, // expected_delay + }); + + // Tests with a (small) finite timeout (deadline soon), with the condition + // becoming true both before and after its expiry. + + // The condition is already true: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + never, // wait_timeout + immediate, // satisfy_condition_delay + true, // expected_result + immediate // expected_delay + }); + + // The condition becomes true before the expiry: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + finite * 2, // wait_timeout + finite, // satisfy_condition_delay + true, // expected_result + finite // expected_delay + }); + + // The condition becomes true, but the timeout has already expired: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + finite, // wait_timeout + finite * 2, // satisfy_condition_delay + false, // expected_result + finite // expected_delay + }); + + // The condition never becomes true: + values.push_back(TimeoutTestParam{ + __FILE__, __LINE__, use_absolute_deadline, + finite, // wait_timeout + never, // satisfy_condition_delay + false, // expected_result + finite // expected_delay + }); } - - bool WaitWithTimeout(absl::Duration timeout) { - absl::MutexLock lock(&mu_); - absl::Time deadline = absl::Now() + timeout; - if (use_deadline_) { - while (!b_ && !cv_.WaitWithDeadline(&mu_, deadline)) { - } - } else { - while (!b_ && !cv_.WaitWithTimeout(&mu_, timeout)) { - timeout = deadline - absl::Now(); // recompute timeout - } + return values; +} + +// Instantiate `TimeoutTest` with `MakeTimeoutTestParamValues()`. +INSTANTIATE_TEST_CASE_P(All, TimeoutTest, + testing::ValuesIn(MakeTimeoutTestParamValues())); + +TEST_P(TimeoutTest, Await) { + const TimeoutTestParam params = GetParam(); + ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str()); + + // Because this test asserts bounds on scheduling delays it is flaky. To + // compensate it loops forever until it passes. Failures express as test + // timeouts, in which case the test log can be used to diagnose the issue. + for (int attempt = 1;; ++attempt) { + ABSL_RAW_LOG(INFO, "Attempt %d", attempt); + + absl::Mutex mu; + bool value = false; // condition value (under mu) + + std::unique_ptr pool = + CreateDefaultPool(); + RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] { + absl::MutexLock l(&mu); + value = true; + }); + + absl::MutexLock lock(&mu); + absl::Time start_time = absl::Now(); + absl::Condition cond(&value); + bool result = + params.use_absolute_deadline + ? mu.AwaitWithDeadline(cond, start_time + params.wait_timeout) + : mu.AwaitWithTimeout(cond, params.wait_timeout); + if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) { + EXPECT_EQ(params.expected_result, result); + break; } - return b_; - } - - void Wait() { - absl::MutexLock lock(&mu_); - while (!b_) cv_.Wait(&mu_); - } - - private: - const bool use_deadline_; - - bool b_; - absl::Condition c_; - absl::CondVar cv_; - absl::Mutex mu_; -}; - -class OperationTimer { - public: - OperationTimer() : start_(absl::Now()) {} - absl::Duration Get() const { return absl::Now() - start_; } - - private: - const absl::Time start_; -}; - -static void CheckResults(bool exp_result, bool act_result, - absl::Duration exp_duration, - absl::Duration act_duration) { - ABSL_RAW_CHECK(exp_result == act_result, "CheckResults failed"); - // Allow for some worse-case scheduling delay and clock skew. - if ((exp_duration - absl::Milliseconds(40) > act_duration) || - (exp_duration + absl::Milliseconds(150) < act_duration)) { - ABSL_RAW_LOG(FATAL, "CheckResults failed: operation took %s, expected %s", - absl::FormatDuration(act_duration).c_str(), - absl::FormatDuration(exp_duration).c_str()); } } -static void TestAwaitTimeout(Cond *cp, absl::Duration timeout, bool exp_result, - absl::Duration exp_duration) { - OperationTimer t; - bool act_result = cp->AwaitWithTimeout(timeout); - CheckResults(exp_result, act_result, exp_duration, t.Get()); -} - -static void TestLockWhenTimeout(Cond *cp, absl::Duration timeout, - bool exp_result, absl::Duration exp_duration) { - OperationTimer t; - bool act_result = cp->LockWhenWithTimeout(timeout); - CheckResults(exp_result, act_result, exp_duration, t.Get()); -} +TEST_P(TimeoutTest, LockWhen) { + const TimeoutTestParam params = GetParam(); + ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str()); + + // Because this test asserts bounds on scheduling delays it is flaky. To + // compensate it loops forever until it passes. Failures express as test + // timeouts, in which case the test log can be used to diagnose the issue. + for (int attempt = 1;; ++attempt) { + ABSL_RAW_LOG(INFO, "Attempt %d", attempt); + + absl::Mutex mu; + bool value = false; // condition value (under mu) + + std::unique_ptr pool = + CreateDefaultPool(); + RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] { + absl::MutexLock l(&mu); + value = true; + }); + + absl::Time start_time = absl::Now(); + absl::Condition cond(&value); + bool result = + params.use_absolute_deadline + ? mu.LockWhenWithDeadline(cond, start_time + params.wait_timeout) + : mu.LockWhenWithTimeout(cond, params.wait_timeout); + mu.Unlock(); -static void TestReaderLockWhenTimeout(Cond *cp, absl::Duration timeout, - bool exp_result, - absl::Duration exp_duration) { - OperationTimer t; - bool act_result = cp->ReaderLockWhenWithTimeout(timeout); - CheckResults(exp_result, act_result, exp_duration, t.Get()); + if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) { + EXPECT_EQ(params.expected_result, result); + break; + } + } } -static void TestWaitTimeout(Cond *cp, absl::Duration timeout, bool exp_result, - absl::Duration exp_duration) { - OperationTimer t; - bool act_result = cp->WaitWithTimeout(timeout); - CheckResults(exp_result, act_result, exp_duration, t.Get()); +TEST_P(TimeoutTest, ReaderLockWhen) { + const TimeoutTestParam params = GetParam(); + ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str()); + + // Because this test asserts bounds on scheduling delays it is flaky. To + // compensate it loops forever until it passes. Failures express as test + // timeouts, in which case the test log can be used to diagnose the issue. + for (int attempt = 0;; ++attempt) { + ABSL_RAW_LOG(INFO, "Attempt %d", attempt); + + absl::Mutex mu; + bool value = false; // condition value (under mu) + + std::unique_ptr pool = + CreateDefaultPool(); + RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] { + absl::MutexLock l(&mu); + value = true; + }); + + absl::Time start_time = absl::Now(); + bool result = + params.use_absolute_deadline + ? mu.ReaderLockWhenWithDeadline(absl::Condition(&value), + start_time + params.wait_timeout) + : mu.ReaderLockWhenWithTimeout(absl::Condition(&value), + params.wait_timeout); + mu.ReaderUnlock(); + + if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) { + EXPECT_EQ(params.expected_result, result); + break; + } + } } -// Tests with a negative timeout (deadline in the past), which should -// immediately return the current state of the condition. -static void TestNegativeTimeouts(absl::synchronization_internal::ThreadPool *tp, - Cond *cp) { - const absl::Duration negative = -absl::InfiniteDuration(); - const absl::Duration immediate = absl::ZeroDuration(); - - // The condition is already true: - cp->Set(true); - TestAwaitTimeout(cp, negative, true, immediate); - TestLockWhenTimeout(cp, negative, true, immediate); - TestReaderLockWhenTimeout(cp, negative, true, immediate); - TestWaitTimeout(cp, negative, true, immediate); - - // The condition becomes true, but the timeout has already expired: - const absl::Duration delay = absl::Milliseconds(200); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), 3 * delay); - TestAwaitTimeout(cp, negative, false, immediate); - TestLockWhenTimeout(cp, negative, false, immediate); - TestReaderLockWhenTimeout(cp, negative, false, immediate); - cp->Await(); // wait for the scheduled Set() to complete - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay); - TestWaitTimeout(cp, negative, false, immediate); - cp->Wait(); // wait for the scheduled Signal() to complete - - // The condition never becomes true: - cp->Set(false); - TestAwaitTimeout(cp, negative, false, immediate); - TestLockWhenTimeout(cp, negative, false, immediate); - TestReaderLockWhenTimeout(cp, negative, false, immediate); - TestWaitTimeout(cp, negative, false, immediate); -} - -// Tests with an infinite timeout (deadline in the infinite future), which -// should only return when the condition becomes true. -static void TestInfiniteTimeouts(absl::synchronization_internal::ThreadPool *tp, - Cond *cp) { - const absl::Duration infinite = absl::InfiniteDuration(); - const absl::Duration immediate = absl::ZeroDuration(); - - // The condition is already true: - cp->Set(true); - TestAwaitTimeout(cp, infinite, true, immediate); - TestLockWhenTimeout(cp, infinite, true, immediate); - TestReaderLockWhenTimeout(cp, infinite, true, immediate); - TestWaitTimeout(cp, infinite, true, immediate); - - // The condition becomes true before the (infinite) expiry: - const absl::Duration delay = absl::Milliseconds(200); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay); - TestAwaitTimeout(cp, infinite, true, delay); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay); - TestLockWhenTimeout(cp, infinite, true, delay); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay); - TestReaderLockWhenTimeout(cp, infinite, true, delay); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay); - TestWaitTimeout(cp, infinite, true, delay); -} - -// Tests with a (small) finite timeout (deadline soon), with the condition -// becoming true both before and after its expiry. -static void TestFiniteTimeouts(absl::synchronization_internal::ThreadPool *tp, - Cond *cp) { - const absl::Duration finite = absl::Milliseconds(400); - const absl::Duration immediate = absl::ZeroDuration(); +TEST_P(TimeoutTest, Wait) { + const TimeoutTestParam params = GetParam(); + ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str()); + + // Because this test asserts bounds on scheduling delays it is flaky. To + // compensate it loops forever until it passes. Failures express as test + // timeouts, in which case the test log can be used to diagnose the issue. + for (int attempt = 0;; ++attempt) { + ABSL_RAW_LOG(INFO, "Attempt %d", attempt); + + absl::Mutex mu; + bool value = false; // condition value (under mu) + absl::CondVar cv; // signals a change of `value` + + std::unique_ptr pool = + CreateDefaultPool(); + RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] { + absl::MutexLock l(&mu); + value = true; + cv.Signal(); + }); + + absl::MutexLock lock(&mu); + absl::Time start_time = absl::Now(); + absl::Duration timeout = params.wait_timeout; + absl::Time deadline = start_time + timeout; + while (!value) { + if (params.use_absolute_deadline ? cv.WaitWithDeadline(&mu, deadline) + : cv.WaitWithTimeout(&mu, timeout)) { + break; // deadline/timeout exceeded + } + timeout = deadline - absl::Now(); // recompute + } + bool result = value; // note: `mu` is still held - // The condition is already true: - cp->Set(true); - TestAwaitTimeout(cp, finite, true, immediate); - TestLockWhenTimeout(cp, finite, true, immediate); - TestReaderLockWhenTimeout(cp, finite, true, immediate); - TestWaitTimeout(cp, finite, true, immediate); - - // The condition becomes true before the expiry: - const absl::Duration delay1 = finite / 2; - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1); - TestAwaitTimeout(cp, finite, true, delay1); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1); - TestLockWhenTimeout(cp, finite, true, delay1); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1); - TestReaderLockWhenTimeout(cp, finite, true, delay1); - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay1); - TestWaitTimeout(cp, finite, true, delay1); - - // The condition becomes true, but the timeout has already expired: - const absl::Duration delay2 = finite * 2; - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), 3 * delay2); - TestAwaitTimeout(cp, finite, false, finite); - TestLockWhenTimeout(cp, finite, false, finite); - TestReaderLockWhenTimeout(cp, finite, false, finite); - cp->Await(); // wait for the scheduled Set() to complete - cp->Set(false); - ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay2); - TestWaitTimeout(cp, finite, false, finite); - cp->Wait(); // wait for the scheduled Signal() to complete - - // The condition never becomes true: - cp->Set(false); - TestAwaitTimeout(cp, finite, false, finite); - TestLockWhenTimeout(cp, finite, false, finite); - TestReaderLockWhenTimeout(cp, finite, false, finite); - TestWaitTimeout(cp, finite, false, finite); -} - -TEST(Mutex, Timeouts) { - auto tp = CreateDefaultPool(); - for (bool use_deadline : {false, true}) { - Cond cond(use_deadline); - TestNegativeTimeouts(tp.get(), &cond); - TestInfiniteTimeouts(tp.get(), &cond); - TestFiniteTimeouts(tp.get(), &cond); + if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) { + EXPECT_EQ(params.expected_result, result); + break; + } } } diff --git a/absl/time/clock_test.cc b/absl/time/clock_test.cc index f143c036..707166d0 100644 --- a/absl/time/clock_test.cc +++ b/absl/time/clock_test.cc @@ -35,36 +35,84 @@ TEST(Time, Now) { EXPECT_GE(after, now); } -TEST(SleepForTest, BasicSanity) { - absl::Duration sleep_time = absl::Milliseconds(2500); - absl::Time start = absl::Now(); - absl::SleepFor(sleep_time); - absl::Time end = absl::Now(); - EXPECT_LE(sleep_time - absl::Milliseconds(100), end - start); - EXPECT_GE(sleep_time + absl::Milliseconds(200), end - start); -} +enum class AlarmPolicy { kWithoutAlarm, kWithAlarm }; -#ifdef ABSL_HAVE_ALARM -// Helper for test SleepFor. +#if defined(ABSL_HAVE_ALARM) bool alarm_handler_invoked = false; + void AlarmHandler(int signo) { ASSERT_EQ(signo, SIGALRM); alarm_handler_invoked = true; } +#endif + +// Does SleepFor(d) take between lower_bound and upper_bound at least +// once between now and (now + timeout)? If requested (and supported), +// add an alarm for the middle of the sleep period and expect it to fire. +bool SleepForBounded(absl::Duration d, absl::Duration lower_bound, + absl::Duration upper_bound, absl::Duration timeout, + AlarmPolicy alarm_policy, int* attempts) { + const absl::Time deadline = absl::Now() + timeout; + while (absl::Now() < deadline) { +#if defined(ABSL_HAVE_ALARM) + sig_t old_alarm = SIG_DFL; + if (alarm_policy == AlarmPolicy::kWithAlarm) { + alarm_handler_invoked = false; + old_alarm = signal(SIGALRM, AlarmHandler); + alarm(absl::ToInt64Seconds(d / 2)); + } +#else + EXPECT_EQ(alarm_policy, AlarmPolicy::kWithoutAlarm); +#endif + ++*attempts; + absl::Time start = absl::Now(); + absl::SleepFor(d); + absl::Duration actual = absl::Now() - start; +#if defined(ABSL_HAVE_ALARM) + if (alarm_policy == AlarmPolicy::kWithAlarm) { + signal(SIGALRM, old_alarm); + if (!alarm_handler_invoked) continue; + } +#endif + if (lower_bound <= actual && actual <= upper_bound) { + return true; // yes, the SleepFor() was correctly bounded + } + } + return false; +} -TEST(SleepForTest, AlarmSupport) { - alarm_handler_invoked = false; - sig_t old_alarm = signal(SIGALRM, AlarmHandler); - alarm(2); - absl::Duration sleep_time = absl::Milliseconds(3500); - absl::Time start = absl::Now(); - absl::SleepFor(sleep_time); - absl::Time end = absl::Now(); - EXPECT_TRUE(alarm_handler_invoked); - EXPECT_LE(sleep_time - absl::Milliseconds(100), end - start); - EXPECT_GE(sleep_time + absl::Milliseconds(200), end - start); - signal(SIGALRM, old_alarm); +testing::AssertionResult AssertSleepForBounded(absl::Duration d, + absl::Duration early, + absl::Duration late, + absl::Duration timeout, + AlarmPolicy alarm_policy) { + const absl::Duration lower_bound = d - early; + const absl::Duration upper_bound = d + late; + int attempts = 0; + if (SleepForBounded(d, lower_bound, upper_bound, timeout, alarm_policy, + &attempts)) { + return testing::AssertionSuccess(); + } + return testing::AssertionFailure() + << "SleepFor(" << d << ") did not return within [" << lower_bound + << ":" << upper_bound << "] in " << attempts << " attempt" + << (attempts == 1 ? "" : "s") << " over " << timeout + << (alarm_policy == AlarmPolicy::kWithAlarm ? " with" : " without") + << " an alarm"; +} + +// Tests that SleepFor() returns neither too early nor too late. +TEST(SleepFor, Bounded) { + const absl::Duration d = absl::Milliseconds(2500); + const absl::Duration early = absl::Milliseconds(100); + const absl::Duration late = absl::Milliseconds(300); + const absl::Duration timeout = 48 * d; + EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout, + AlarmPolicy::kWithoutAlarm)); +#if defined(ABSL_HAVE_ALARM) + EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout, + AlarmPolicy::kWithAlarm)); +#endif } -#endif // ABSL_HAVE_ALARM } // namespace diff --git a/absl/types/variant.h b/absl/types/variant.h index fd1d49ac..9d98a9ed 100644 --- a/absl/types/variant.h +++ b/absl/types/variant.h @@ -248,7 +248,7 @@ using variant_alternative_t = typename variant_alternative::type; // // Example: // -// absl::variant bar = 42; +// absl::variant foo = 42; // if (absl::holds_alternative(foo)) { // std::cout << "The variant holds an integer"; // } -- cgit v1.2.3 From fb462224c058487763f263b7995d70efd0242c17 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 5 Sep 2018 10:20:22 -0700 Subject: Export of internal Abseil changes. -- 86b1c997fac1f77b0eacfab788659b5a89a6096e by Abseil Team : Import of CCTZ from GitHub. PiperOrigin-RevId: 211654320 -- 299b70e1247df768454a76eb957a184de9706f61 by Chris Kennelly : Avoid creating a misaligned reference to int. PiperOrigin-RevId: 211505883 -- c8fad4357ad0bfb3c5ad197c505509bc087072c9 by Abseil Team : Import of CCTZ from GitHub. PiperOrigin-RevId: 211458539 -- 0613feffcd36466c3e53a50758d7e8f17c001dce by Greg Falcon : Refactor a string unit test into a template function for internal purposes. PiperOrigin-RevId: 211100748 GitOrigin-RevId: 86b1c997fac1f77b0eacfab788659b5a89a6096e Change-Id: Ic6a932b6c27c6762dcdb3b0127f1e2be782418c1 --- absl/strings/escaping.cc | 6 ++- absl/strings/escaping_test.cc | 17 +++++--- absl/strings/str_format_test.cc | 2 +- .../internal/cctz/include/cctz/civil_time_detail.h | 8 +--- absl/time/internal/cctz/src/cctz_benchmark.cc | 50 +++++++++++----------- absl/time/internal/cctz/src/time_zone_format.cc | 2 +- .../internal/cctz/src/time_zone_format_test.cc | 8 ++-- 7 files changed, 48 insertions(+), 45 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 6742f446..8d8b00b2 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -939,7 +939,8 @@ constexpr char kBase64Chars[] = constexpr char kWebSafeBase64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; -void Base64EscapeInternal(const unsigned char* src, size_t szsrc, std::string* dest, +template +void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest, bool do_padding, const char* base64_chars) { const size_t calc_escaped_size = CalculateBase64EscapedLenInternal(szsrc, do_padding); @@ -951,7 +952,8 @@ void Base64EscapeInternal(const unsigned char* src, size_t szsrc, std::string* d dest->erase(escaped_len); } -bool Base64UnescapeInternal(const char* src, size_t slen, std::string* dest, +template +bool Base64UnescapeInternal(const char* src, size_t slen, String* dest, const signed char* unbase64) { // Determine the size of the output std::string. Base64 encodes every 3 bytes into // 4 characters. any leftover chars are added directly for good measure. diff --git a/absl/strings/escaping_test.cc b/absl/strings/escaping_test.cc index 3f65ec10..9dc27f3f 100644 --- a/absl/strings/escaping_test.cc +++ b/absl/strings/escaping_test.cc @@ -543,18 +543,19 @@ static struct { {"abcdefghijklmnopqrstuvwxyz", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="}, }; -TEST(Base64, EscapeAndUnescape) { +template +void TestEscapeAndUnescape() { // Check the short strings; this tests the math (and boundaries) for (const auto& tc : base64_tests) { - std::string encoded("this junk should be ignored"); + StringType encoded("this junk should be ignored"); absl::Base64Escape(tc.plaintext, &encoded); EXPECT_EQ(encoded, tc.cyphertext); - std::string decoded("this junk should be ignored"); + StringType decoded("this junk should be ignored"); EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded)); EXPECT_EQ(decoded, tc.plaintext); - std::string websafe(tc.cyphertext); + StringType websafe(tc.cyphertext); for (int c = 0; c < websafe.size(); ++c) { if ('+' == websafe[c]) websafe[c] = '-'; if ('/' == websafe[c]) websafe[c] = '_'; @@ -576,7 +577,7 @@ TEST(Base64, EscapeAndUnescape) { // Now try the long strings, this tests the streaming for (const auto& tc : absl::strings_internal::base64_strings()) { - std::string buffer; + StringType buffer; absl::WebSafeBase64Escape(tc.plaintext, &buffer); EXPECT_EQ(tc.cyphertext, buffer); } @@ -586,7 +587,7 @@ TEST(Base64, EscapeAndUnescape) { absl::string_view data_set[] = {"ab-/", absl::string_view("\0bcd", 4), absl::string_view("abc.\0", 5)}; for (absl::string_view bad_data : data_set) { - std::string buf; + StringType buf; EXPECT_FALSE(absl::Base64Unescape(bad_data, &buf)); EXPECT_FALSE(absl::WebSafeBase64Unescape(bad_data, &buf)); EXPECT_TRUE(buf.empty()); @@ -594,6 +595,10 @@ TEST(Base64, EscapeAndUnescape) { } } +TEST(Base64, EscapeAndUnescape) { + TestEscapeAndUnescape(); +} + TEST(Base64, DISABLED_HugeData) { const size_t kSize = size_t(3) * 1000 * 1000 * 1000; static_assert(kSize % 3 == 0, "kSize must be divisible by 3"); diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index fed75faf..9a6576dc 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -391,7 +391,7 @@ TEST(StrFormat, BehavesAsDocumented) { #endif // Other conversion - int64_t value = 0x7ffdeb6; + int64_t value = 0x7ffdeb4; auto ptr_value = static_cast(value); const int& something = *reinterpret_cast(ptr_value); EXPECT_EQ(StrFormat("%p", &something), StrFormat("0x%x", ptr_value)); diff --git a/absl/time/internal/cctz/include/cctz/civil_time_detail.h b/absl/time/internal/cctz/include/cctz/civil_time_detail.h index f831258f..5fe0967f 100644 --- a/absl/time/internal/cctz/include/cctz/civil_time_detail.h +++ b/absl/time/internal/cctz/include/cctz/civil_time_detail.h @@ -508,12 +508,8 @@ CONSTEXPR_F weekday get_weekday(const civil_day& cd) noexcept { CONSTEXPR_D int k_weekday_offsets[1 + 12] = { -1, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4, }; - year_t wd = cd.year() - (cd.month() < 3); - if (wd >= 0) { - wd += wd / 4 - wd / 100 + wd / 400; - } else { - wd += (wd - 3) / 4 - (wd - 99) / 100 + (wd - 399) / 400; - } + year_t wd = 2400 + (cd.year() % 400) - (cd.month() < 3); + wd += wd / 4 - wd / 100 + wd / 400; wd += k_weekday_offsets[cd.month()] + cd.day(); return k_weekday_by_sun_off[(wd % 7 + 7) % 7]; } diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc index c97df78c..4498d7d0 100644 --- a/absl/time/internal/cctz/src/cctz_benchmark.cc +++ b/absl/time/internal/cctz/src/cctz_benchmark.cc @@ -778,13 +778,13 @@ void BM_Zone_UTCTimeZone(benchmark::State& state) { } BENCHMARK(BM_Zone_UTCTimeZone); -// In each "ToDateTime" benchmark we switch between two instants -// separated by at least one transition in order to defeat any -// internal caching of previous results (e.g., see local_time_hint_). +// In each "ToCivil" benchmark we switch between two instants separated +// by at least one transition in order to defeat any internal caching of +// previous results (e.g., see local_time_hint_). // // The "UTC" variants use UTC instead of the Google/local time zone. -void BM_Time_ToDateTime_CCTZ(benchmark::State& state) { +void BM_Time_ToCivil_CCTZ(benchmark::State& state) { const cctz::time_zone tz = TestTimeZone(); std::chrono::system_clock::time_point tp = std::chrono::system_clock::from_time_t(1384569027); @@ -796,9 +796,9 @@ void BM_Time_ToDateTime_CCTZ(benchmark::State& state) { benchmark::DoNotOptimize(cctz::convert(tp, tz)); } } -BENCHMARK(BM_Time_ToDateTime_CCTZ); +BENCHMARK(BM_Time_ToCivil_CCTZ); -void BM_Time_ToDateTime_Libc(benchmark::State& state) { +void BM_Time_ToCivil_Libc(benchmark::State& state) { // No timezone support, so just use localtime. time_t t = 1384569027; time_t t2 = 1418962578; @@ -813,9 +813,9 @@ void BM_Time_ToDateTime_Libc(benchmark::State& state) { #endif } } -BENCHMARK(BM_Time_ToDateTime_Libc); +BENCHMARK(BM_Time_ToCivil_Libc); -void BM_Time_ToDateTimeUTC_CCTZ(benchmark::State& state) { +void BM_Time_ToCivilUTC_CCTZ(benchmark::State& state) { const cctz::time_zone tz = cctz::utc_time_zone(); std::chrono::system_clock::time_point tp = std::chrono::system_clock::from_time_t(1384569027); @@ -824,9 +824,9 @@ void BM_Time_ToDateTimeUTC_CCTZ(benchmark::State& state) { benchmark::DoNotOptimize(cctz::convert(tp, tz)); } } -BENCHMARK(BM_Time_ToDateTimeUTC_CCTZ); +BENCHMARK(BM_Time_ToCivilUTC_CCTZ); -void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) { +void BM_Time_ToCivilUTC_Libc(benchmark::State& state) { time_t t = 1384569027; struct tm tm; while (state.KeepRunning()) { @@ -838,16 +838,16 @@ void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) { #endif } } -BENCHMARK(BM_Time_ToDateTimeUTC_Libc); +BENCHMARK(BM_Time_ToCivilUTC_Libc); -// In each "FromDateTime" benchmark we switch between two YMDhms -// values separated by at least one transition in order to defeat any -// internal caching of previous results (e.g., see time_local_hint_). +// In each "FromCivil" benchmark we switch between two YMDhms values +// separated by at least one transition in order to defeat any internal +// caching of previous results (e.g., see time_local_hint_). // // The "UTC" variants use UTC instead of the Google/local time zone. // The "Day0" variants require normalization of the day of month. -void BM_Time_FromDateTime_CCTZ(benchmark::State& state) { +void BM_Time_FromCivil_CCTZ(benchmark::State& state) { const cctz::time_zone tz = TestTimeZone(); int i = 0; while (state.KeepRunning()) { @@ -860,9 +860,9 @@ void BM_Time_FromDateTime_CCTZ(benchmark::State& state) { } } } -BENCHMARK(BM_Time_FromDateTime_CCTZ); +BENCHMARK(BM_Time_FromCivil_CCTZ); -void BM_Time_FromDateTime_Libc(benchmark::State& state) { +void BM_Time_FromCivil_Libc(benchmark::State& state) { // No timezone support, so just use localtime. int i = 0; while (state.KeepRunning()) { @@ -886,20 +886,20 @@ void BM_Time_FromDateTime_Libc(benchmark::State& state) { benchmark::DoNotOptimize(mktime(&tm)); } } -BENCHMARK(BM_Time_FromDateTime_Libc); +BENCHMARK(BM_Time_FromCivil_Libc); -void BM_Time_FromDateTimeUTC_CCTZ(benchmark::State& state) { +void BM_Time_FromCivilUTC_CCTZ(benchmark::State& state) { const cctz::time_zone tz = cctz::utc_time_zone(); while (state.KeepRunning()) { benchmark::DoNotOptimize( cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz)); } } -BENCHMARK(BM_Time_FromDateTimeUTC_CCTZ); +BENCHMARK(BM_Time_FromCivilUTC_CCTZ); -// There is no BM_Time_FromDateTimeUTC_Libc. +// There is no BM_Time_FromCivilUTC_Libc. -void BM_Time_FromDateTimeDay0_CCTZ(benchmark::State& state) { +void BM_Time_FromCivilDay0_CCTZ(benchmark::State& state) { const cctz::time_zone tz = TestTimeZone(); int i = 0; while (state.KeepRunning()) { @@ -912,9 +912,9 @@ void BM_Time_FromDateTimeDay0_CCTZ(benchmark::State& state) { } } } -BENCHMARK(BM_Time_FromDateTimeDay0_CCTZ); +BENCHMARK(BM_Time_FromCivilDay0_CCTZ); -void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { +void BM_Time_FromCivilDay0_Libc(benchmark::State& state) { // No timezone support, so just use localtime. int i = 0; while (state.KeepRunning()) { @@ -938,7 +938,7 @@ void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { benchmark::DoNotOptimize(mktime(&tm)); } } -BENCHMARK(BM_Time_FromDateTimeDay0_Libc); +BENCHMARK(BM_Time_FromCivilDay0_Libc); const char* const kFormats[] = { RFC1123_full, // 0 diff --git a/absl/time/internal/cctz/src/time_zone_format.cc b/absl/time/internal/cctz/src/time_zone_format.cc index d9714092..a02b1e34 100644 --- a/absl/time/internal/cctz/src/time_zone_format.cc +++ b/absl/time/internal/cctz/src/time_zone_format.cc @@ -743,7 +743,7 @@ bool parse(const std::string& format, const std::string& input, data = ParseTM(data, spec.c_str(), &tm); // If we successfully parsed %p we need to remember whether the result - // was AM or PM so that we can adjust tm_hour before ConvertDateTime(). + // was AM or PM so that we can adjust tm_hour before time_zone::lookup(). // So reparse the input with a known AM hour, and check if it is shifted // to a PM hour. if (spec == "%p" && data != nullptr) { diff --git a/absl/time/internal/cctz/src/time_zone_format_test.cc b/absl/time/internal/cctz/src/time_zone_format_test.cc index a90dda76..6b9928ed 100644 --- a/absl/time/internal/cctz/src/time_zone_format_test.cc +++ b/absl/time/internal/cctz/src/time_zone_format_test.cc @@ -669,13 +669,13 @@ TEST(Parse, WithTimeZone) { utc_time_zone(), &tp)); ExpectTime(tp, tz, 2013, 6, 28, 19 - 8 - 7, 8, 9, -7 * 60 * 60, true, "PDT"); - // Check a skipped time (a Spring DST transition). parse() returns - // the preferred-offset result, as defined for ConvertDateTime(). + // Check a skipped time (a Spring DST transition). parse() uses the + // pre-transition offset. EXPECT_TRUE(parse("%Y-%m-%d %H:%M:%S", "2011-03-13 02:15:00", tz, &tp)); ExpectTime(tp, tz, 2011, 3, 13, 3, 15, 0, -7 * 60 * 60, true, "PDT"); - // Check a repeated time (a Fall DST transition). parse() returns - // the preferred-offset result, as defined for ConvertDateTime(). + // Check a repeated time (a Fall DST transition). parse() uses the + // pre-transition offset. EXPECT_TRUE(parse("%Y-%m-%d %H:%M:%S", "2011-11-06 01:15:00", tz, &tp)); ExpectTime(tp, tz, 2011, 11, 6, 1, 15, 0, -7 * 60 * 60, true, "PDT"); } -- cgit v1.2.3 From 8ff1374008259719b54a8cb128ef951c02da164c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 12 Sep 2018 11:03:25 -0700 Subject: Export of internal Abseil changes. -- 821196cfb2a3b943ffdc4c9e75daec92d7ffb28b by Abseil Team : Performance improvements PiperOrigin-RevId: 212668992 -- 704858e2e767016bad27d53eec01d9d48e546b23 by Abseil Team : Low-level Portability enchancements for Abseil Mutex on WebAssembly. Emscripten Pthreads do not use signals, so remove use of pthread_sigmask or other async-signal-safe related handling code. PiperOrigin-RevId: 212527958 -- be3e38cb4d493b755132d20c8c2d1a51e45d5449 by Jon Cohen : Internal change. PiperOrigin-RevId: 212523797 GitOrigin-RevId: 821196cfb2a3b943ffdc4c9e75daec92d7ffb28b Change-Id: I5694e23e4e09364a15dd6fc4e2e3f15e38835687 --- absl/base/internal/low_level_alloc.cc | 14 +- absl/base/internal/low_level_alloc.h | 7 +- absl/base/internal/thread_identity.cc | 10 ++ absl/memory/memory_test.cc | 1 - absl/strings/internal/str_format/arg.cc | 80 ++++------ absl/strings/internal/str_format/arg.h | 210 +++++++++++++-------------- absl/strings/internal/str_format/bind.h | 2 +- absl/strings/internal/str_format/extension.h | 10 +- absl/strings/str_format_test.cc | 36 +++++ 9 files changed, 199 insertions(+), 171 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/internal/low_level_alloc.cc b/absl/base/internal/low_level_alloc.cc index 0a6f3070..6e636a02 100644 --- a/absl/base/internal/low_level_alloc.cc +++ b/absl/base/internal/low_level_alloc.cc @@ -401,16 +401,20 @@ bool LowLevelAlloc::DeleteArena(Arena *arena) { ABSL_RAW_CHECK(munmap_result != 0, "LowLevelAlloc::DeleteArena: VitualFree failed"); #else +#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) == 0) { munmap_result = munmap(region, size); } else { munmap_result = base_internal::DirectMunmap(region, size); } +#else + munmap_result = munmap(region, size); +#endif // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING if (munmap_result != 0) { ABSL_RAW_LOG(FATAL, "LowLevelAlloc::DeleteArena: munmap failed: %d", errno); } -#endif +#endif // _WIN32 } section.Leave(); arena->~Arena(); @@ -545,6 +549,7 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) { MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); ABSL_RAW_CHECK(new_pages != nullptr, "VirtualAlloc failed"); #else +#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) != 0) { new_pages = base_internal::DirectMmap(nullptr, new_pages_size, PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); @@ -552,10 +557,15 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) { new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); } +#else + new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); +#endif // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING if (new_pages == MAP_FAILED) { ABSL_RAW_LOG(FATAL, "mmap error: %d", errno); } -#endif + +#endif // _WIN32 arena->mu.Lock(); s = reinterpret_cast(new_pages); s->header.size = new_pages_size; diff --git a/absl/base/internal/low_level_alloc.h b/absl/base/internal/low_level_alloc.h index 3c15605b..fba9466a 100644 --- a/absl/base/internal/low_level_alloc.h +++ b/absl/base/internal/low_level_alloc.h @@ -39,10 +39,13 @@ #define ABSL_LOW_LEVEL_ALLOC_MISSING 1 #endif -// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows. +// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or +// asm.js / WebAssembly. +// See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html +// for more information. #ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING #error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set -#elif defined(_WIN32) +#elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__) #define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1 #endif diff --git a/absl/base/internal/thread_identity.cc b/absl/base/internal/thread_identity.cc index 678e8568..cff9c1b4 100644 --- a/absl/base/internal/thread_identity.cc +++ b/absl/base/internal/thread_identity.cc @@ -68,6 +68,14 @@ void SetCurrentThreadIdentity( // NOTE: Not async-safe. But can be open-coded. absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey, reclaimer); + +#ifdef __EMSCRIPTEN__ + // Emscripten PThread implementation does not support signals. + // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html + // for more information. + pthread_setspecific(thread_identity_pthread_key, + reinterpret_cast(identity)); +#else // We must mask signals around the call to setspecific as with current glibc, // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent()) // may zero our value. @@ -81,6 +89,8 @@ void SetCurrentThreadIdentity( pthread_setspecific(thread_identity_pthread_key, reinterpret_cast(identity)); pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr); +#endif // !__EMSCRIPTEN__ + #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS // NOTE: Not async-safe. But can be open-coded. absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey, diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc index dee9b486..2e453e22 100644 --- a/absl/memory/memory_test.cc +++ b/absl/memory/memory_test.cc @@ -149,7 +149,6 @@ TEST(Make_UniqueTest, NotAmbiguousWithStdMakeUnique) { } #if 0 -// TODO(billydonahue): Make a proper NC test. // These tests shouldn't compile. TEST(MakeUniqueTestNC, AcceptMoveOnlyLvalue) { auto m = MoveOnly(); diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index eafb068f..b40be8ff 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -112,7 +112,7 @@ class ConvertedIntInfo { // Note: 'o' conversions do not have a base indicator, it's just that // the '#' flag is specified to modify the precision for 'o' conversions. string_view BaseIndicator(const ConvertedIntInfo &info, - const ConversionSpec &conv) { + const ConversionSpec conv) { bool alt = conv.flags().alt; int radix = conv.conv().radix(); if (conv.conv().id() == ConversionChar::p) @@ -127,7 +127,7 @@ string_view BaseIndicator(const ConvertedIntInfo &info, return {}; } -string_view SignColumn(bool neg, const ConversionSpec &conv) { +string_view SignColumn(bool neg, const ConversionSpec conv) { if (conv.conv().is_signed()) { if (neg) return "-"; if (conv.flags().show_pos) return "+"; @@ -136,7 +136,7 @@ string_view SignColumn(bool neg, const ConversionSpec &conv) { return {}; } -bool ConvertCharImpl(unsigned char v, const ConversionSpec &conv, +bool ConvertCharImpl(unsigned char v, const ConversionSpec conv, FormatSinkImpl *sink) { size_t fill = 0; if (conv.width() >= 0) fill = conv.width(); @@ -148,7 +148,7 @@ bool ConvertCharImpl(unsigned char v, const ConversionSpec &conv, } bool ConvertIntImplInner(const ConvertedIntInfo &info, - const ConversionSpec &conv, FormatSinkImpl *sink) { + const ConversionSpec conv, FormatSinkImpl *sink) { // Print as a sequence of Substrings: // [left_spaces][sign][base_indicator][zeroes][formatted][right_spaces] size_t fill = 0; @@ -202,8 +202,7 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, } template -bool ConvertIntImplInner(T v, const ConversionSpec &conv, - FormatSinkImpl *sink) { +bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) { ConvertedIntInfo info(v, conv.conv()); if (conv.flags().basic && conv.conv().id() != ConversionChar::p) { if (info.is_neg()) sink->Append(1, '-'); @@ -218,7 +217,7 @@ bool ConvertIntImplInner(T v, const ConversionSpec &conv, } template -bool ConvertIntArg(T v, const ConversionSpec &conv, FormatSinkImpl *sink) { +bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { if (conv.conv().is_float()) { return FormatConvertImpl(static_cast(v), conv, sink).value; } @@ -234,11 +233,11 @@ bool ConvertIntArg(T v, const ConversionSpec &conv, FormatSinkImpl *sink) { } template -bool ConvertFloatArg(T v, const ConversionSpec &conv, FormatSinkImpl *sink) { +bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { return conv.conv().is_float() && ConvertFloatImpl(v, conv, sink); } -inline bool ConvertStringArg(string_view v, const ConversionSpec &conv, +inline bool ConvertStringArg(string_view v, const ConversionSpec conv, FormatSinkImpl *sink) { if (conv.conv().id() != ConversionChar::s) return false; @@ -254,19 +253,19 @@ inline bool ConvertStringArg(string_view v, const ConversionSpec &conv, // ==================== Strings ==================== ConvertResult FormatConvertImpl(const std::string &v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } ConvertResult FormatConvertImpl(string_view v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } ConvertResult FormatConvertImpl(const char *v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { if (conv.conv().id() == ConversionChar::p) return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; @@ -283,7 +282,7 @@ ConvertResult FormatConvertImpl(const char *v, } // ==================== Raw pointers ==================== -ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec &conv, +ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) { if (conv.conv().id() != ConversionChar::p) return {false}; @@ -295,104 +294,83 @@ ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec &conv, } // ==================== Floats ==================== -FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec &conv, +FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } -FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec &conv, +FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } FloatingConvertResult FormatConvertImpl(long double v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } // ==================== Chars ==================== -IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec &conv, +IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(signed char v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned char v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } // ==================== Ints ==================== IntegralConvertResult FormatConvertImpl(short v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } -IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec &conv, +IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } -IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec &conv, +IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(long v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(long long v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(absl::uint128 v, - const ConversionSpec &conv, + const ConversionSpec conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } -template struct FormatArgImpl::TypedVTable; - -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; // NOLINT -template struct FormatArgImpl::TypedVTable; - -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; - -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; -template struct FormatArgImpl::TypedVTable; +ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(); + } // namespace str_format_internal diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index a9562188..3376d48a 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -33,7 +33,7 @@ struct HasUserDefinedConvert : std::false_type {}; template struct HasUserDefinedConvert< T, void_t(), std::declval(), + std::declval(), std::declval(), std::declval()))>> : std::true_type {}; template class StreamedWrapper; @@ -50,25 +50,23 @@ struct VoidPtr { : value(ptr ? reinterpret_cast(ptr) : 0) {} uintptr_t value; }; -ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec& conv, +ConvertResult FormatConvertImpl(VoidPtr v, ConversionSpec conv, FormatSinkImpl* sink); // Strings. -ConvertResult FormatConvertImpl(const std::string& v, - const ConversionSpec& conv, +ConvertResult FormatConvertImpl(const std::string& v, ConversionSpec conv, FormatSinkImpl* sink); -ConvertResult FormatConvertImpl(string_view v, - const ConversionSpec& conv, +ConvertResult FormatConvertImpl(string_view v, ConversionSpec conv, FormatSinkImpl* sink); ConvertResult FormatConvertImpl(const char* v, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); template ::value>::type* = nullptr, class AbslCordReader = ::CordReader> ConvertResult FormatConvertImpl(const AbslCord& value, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink) { if (conv.conv().id() != ConversionChar::s) return {false}; @@ -104,51 +102,48 @@ using IntegralConvertResult = using FloatingConvertResult = ConvertResult; // Floats. -FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec& conv, +FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv, FormatSinkImpl* sink); -FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec& conv, +FloatingConvertResult FormatConvertImpl(double v, ConversionSpec conv, FormatSinkImpl* sink); -FloatingConvertResult FormatConvertImpl(long double v, - const ConversionSpec& conv, +FloatingConvertResult FormatConvertImpl(long double v, ConversionSpec conv, FormatSinkImpl* sink); // Chars. -IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(char v, ConversionSpec conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(signed char v, - const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(signed char v, ConversionSpec conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(unsigned char v, - const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(unsigned char v, ConversionSpec conv, FormatSinkImpl* sink); // Ints. IntegralConvertResult FormatConvertImpl(short v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(int v, ConversionSpec conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(unsigned v, ConversionSpec conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(long v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(long long v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(uint128 v, const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(uint128 v, ConversionSpec conv, FormatSinkImpl* sink); template ::value, int> = 0> -IntegralConvertResult FormatConvertImpl(T v, const ConversionSpec& conv, +IntegralConvertResult FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink) { return FormatConvertImpl(static_cast(v), conv, sink); } @@ -159,11 +154,11 @@ template typename std::enable_if::value && !HasUserDefinedConvert::value, IntegralConvertResult>::type -FormatConvertImpl(T v, const ConversionSpec& conv, FormatSinkImpl* sink); +FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink); template ConvertResult FormatConvertImpl(const StreamedWrapper& v, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* out) { std::ostringstream oss; oss << v.v_; @@ -176,7 +171,7 @@ ConvertResult FormatConvertImpl(const StreamedWrapper& v, struct FormatCountCaptureHelper { template static ConvertResult ConvertHelper(const FormatCountCapture& v, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink) { const absl::enable_if_t& v2 = v; @@ -189,7 +184,7 @@ struct FormatCountCaptureHelper { template ConvertResult FormatConvertImpl(const FormatCountCapture& v, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* sink) { return FormatCountCaptureHelper::ConvertHelper(v, conv, sink); } @@ -199,20 +194,20 @@ ConvertResult FormatConvertImpl(const FormatCountCapture& v, struct FormatArgImplFriend { template static bool ToInt(Arg arg, int* out) { - if (!arg.vtbl_->to_int) return false; - *out = arg.vtbl_->to_int(arg.data_); - return true; + // A value initialized ConversionSpec has a `none` conv, which tells the + // dispatcher to run the `int` conversion. + return arg.dispatcher_(arg.data_, {}, out); } template - static bool Convert(Arg arg, const str_format_internal::ConversionSpec& conv, + static bool Convert(Arg arg, str_format_internal::ConversionSpec conv, FormatSinkImpl* out) { - return arg.vtbl_->convert(arg.data_, conv, out); + return arg.dispatcher_(arg.data_, conv, out); } template - static const void* GetVTablePtrForTest(Arg arg) { - return arg.vtbl_; + static typename Arg::Dispatcher GetVTablePtrForTest(Arg arg) { + return arg.dispatcher_; } }; @@ -229,11 +224,7 @@ class FormatArgImpl { char buf[kInlinedSpace]; }; - struct VTable { - bool (*convert)(Data, const str_format_internal::ConversionSpec& conv, - FormatSinkImpl* out); - int (*to_int)(Data); - }; + using Dispatcher = bool (*)(Data, ConversionSpec, void* out); template struct store_by_value @@ -253,10 +244,6 @@ class FormatArgImpl { : ByPointer))> { }; - // An instance of an FormatArgImpl::VTable suitable for 'T'. - template - struct TypedVTable; - // To reduce the number of vtables we will decay values before hand. // Anything with a user-defined Convert will get its own vtable. // For everything else: @@ -338,7 +325,10 @@ class FormatArgImpl { }; template - void Init(const T& value); + void Init(const T& value) { + data_ = Manager::SetValue(value); + dispatcher_ = &Dispatch; + } template static int ToIntVal(const T& val) { @@ -355,79 +345,75 @@ class FormatArgImpl { return static_cast(val); } - Data data_; - const VTable* vtbl_; -}; - -template -struct FormatArgImpl::TypedVTable { - private: - static bool ConvertImpl(Data arg, - const str_format_internal::ConversionSpec& conv, - FormatSinkImpl* out) { - return str_format_internal::FormatConvertImpl(Manager::Value(arg), conv, - out) - .value; + template + static bool ToInt(Data arg, int* out, std::true_type /* is_integral */, + std::false_type) { + *out = ToIntVal(Manager::Value(arg)); + return true; } - template - struct ToIntImpl { - static constexpr int (*value)(Data) = nullptr; - }; + template + static bool ToInt(Data arg, int* out, std::false_type, + std::true_type /* is_enum */) { + *out = ToIntVal(static_cast::type>( + Manager::Value(arg))); + return true; + } - template - struct ToIntImpl::value>::type> { - static int Invoke(Data arg) { return ToIntVal(Manager::Value(arg)); } - static constexpr int (*value)(Data) = &Invoke; - }; + template + static bool ToInt(Data, int*, std::false_type, std::false_type) { + return false; + } - template - struct ToIntImpl::value>::type> { - static int Invoke(Data arg) { - return ToIntVal(static_cast::type>( - Manager::Value(arg))); + template + static bool Dispatch(Data arg, ConversionSpec spec, void* out) { + // A `none` conv indicates that we want the `int` conversion. + if (ABSL_PREDICT_FALSE(spec.conv().id() == ConversionChar::none)) { + return ToInt(arg, static_cast(out), std::is_integral(), + std::is_enum()); } - static constexpr int (*value)(Data) = &Invoke; - }; - public: - static constexpr VTable value{&ConvertImpl, ToIntImpl<>::value}; -}; + return str_format_internal::FormatConvertImpl( + Manager::Value(arg), spec, static_cast(out)) + .value; + } -template -constexpr FormatArgImpl::VTable FormatArgImpl::TypedVTable::value; + Data data_; + Dispatcher dispatcher_; +}; -template -void FormatArgImpl::Init(const T& value) { - data_ = Manager::SetValue(value); - vtbl_ = &TypedVTable::value; -} +#define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \ + E template bool FormatArgImpl::Dispatch(Data, ConversionSpec, void*) + +#define ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(...) \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(str_format_internal::VoidPtr, \ + __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(bool, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(char, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(signed char, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned char, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(short, __VA_ARGS__); /* NOLINT */ \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned short, /* NOLINT */ \ + __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned int, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long, __VA_ARGS__); /* NOLINT */ \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long, /* NOLINT */ \ + __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long long, /* NOLINT */ \ + __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, /* NOLINT */ \ + __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long double, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(const char*, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(string_view, __VA_ARGS__) + +ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern); -extern template struct FormatArgImpl::TypedVTable; - -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; // NOLINT -extern template struct FormatArgImpl::TypedVTable; // NOLINT -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; // NOLINT -extern template struct FormatArgImpl::TypedVTable; // NOLINT -extern template struct FormatArgImpl::TypedVTable; // NOLINT -extern template struct FormatArgImpl::TypedVTable< - unsigned long long>; // NOLINT -extern template struct FormatArgImpl::TypedVTable; - -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; - -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; -extern template struct FormatArgImpl::TypedVTable; } // namespace str_format_internal } // namespace absl diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index a503b19b..1b52df9c 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -186,7 +186,7 @@ class StreamedWrapper { private: template friend ConvertResult FormatConvertImpl(const StreamedWrapper& v, - const ConversionSpec& conv, + ConversionSpec conv, FormatSinkImpl* out); const T& v_; }; diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index f43195c1..11b996ae 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -18,6 +18,7 @@ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_ #include +#include #include #include @@ -307,7 +308,12 @@ class ConversionSpec { public: Flags flags() const { return flags_; } LengthMod length_mod() const { return length_mod_; } - ConversionChar conv() const { return conv_; } + ConversionChar conv() const { + // Keep this field first in the struct . It generates better code when + // accessing it when ConversionSpec is passed by value in registers. + static_assert(offsetof(ConversionSpec, conv_) == 0, ""); + return conv_; + } // Returns the specified width. If width is unspecfied, it returns a negative // value. @@ -324,9 +330,9 @@ class ConversionSpec { void set_left(bool b) { flags_.left = b; } private: + ConversionChar conv_; Flags flags_; LengthMod length_mod_; - ConversionChar conv_; int width_; int precision_; }; diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 9a6576dc..aa14e211 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -601,3 +601,39 @@ TEST_F(ParsedFormatTest, RegressionMixPositional) { } // namespace } // namespace absl + +// Some codegen thunks that we can use to easily dump the generated assembly for +// different StrFormat calls. + +inline std::string CodegenAbslStrFormatInt(int i) { + return absl::StrFormat("%d", i); +} + +inline std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, + int64_t i64) { + return absl::StrFormat("%d %s %d", i, s, i64); +} + +inline void CodegenAbslStrAppendFormatInt(std::string* out, int i) { + absl::StrAppendFormat(out, "%d", i); +} + +inline void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, + const std::string& s, + int64_t i64) { + absl::StrAppendFormat(out, "%d %s %d", i, s, i64); +} + +auto absl_internal_str_format_force_codegen_funcs = std::make_tuple( + CodegenAbslStrFormatInt, CodegenAbslStrFormatIntStringInt64, + CodegenAbslStrAppendFormatInt, CodegenAbslStrAppendFormatIntStringInt64); + +bool absl_internal_str_format_force_codegen_always_false; +// Force the compiler to generate the functions by making it look like we +// escape the function pointers. +// It can't statically know that +// absl_internal_str_format_force_codegen_always_false is not changed by someone +// else. +bool absl_internal_str_format_force_codegen = + absl_internal_str_format_force_codegen_always_false && + printf("%p", &absl_internal_str_format_force_codegen_funcs) == 0; -- cgit v1.2.3 From f340f773edab951656b19b6f1a77c964a78ec4c2 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 10 Oct 2018 12:31:37 -0700 Subject: Export of internal Abseil changes. -- 906c47420646d510edd2479d5542c56f5fa31b65 by CJ Johnson : Import of CCTZ from GitHub. PiperOrigin-RevId: 216573923 -- 74560d4afd2b605909e677c6fc3076049fb3010a by Eric Fiselier : Avoid -Wformat-pedantic in benchmark. PiperOrigin-RevId: 216523769 -- 9bcc9da8b03e6d1ea43ee78931256c5541cb9686 by Eric Fiselier : Delete unused CityHash functions. PiperOrigin-RevId: 216464492 -- a42563b394c89fbb4c55cb5a6a5edbf96d271eea by Abseil Team : Introduce new Abseil interfaces for converting between civil times and absolute times.s Deprecates absl::ConvertDateTime() and absl::FromDateTime(). PiperOrigin-RevId: 216424948 -- 088e11235124267517d7f137854fa5554679c24f by Eric Fiselier : Remove unneeded break statements in test. PiperOrigin-RevId: 216403321 GitOrigin-RevId: 906c47420646d510edd2479d5542c56f5fa31b65 Change-Id: Idb44420be623e369c66f5a9c92bdc9ab46d3ec92 --- absl/container/internal/raw_hash_set_test.cc | 24 +- absl/hash/BUILD.bazel | 1 - absl/hash/CMakeLists.txt | 1 - absl/hash/internal/city.cc | 246 ----- absl/hash/internal/city.h | 16 - absl/hash/internal/city_crc.h | 41 - absl/hash/internal/city_test.cc | 1461 ++----------------------- absl/strings/str_format_test.cc | 26 +- absl/time/BUILD.bazel | 5 +- absl/time/CMakeLists.txt | 4 +- absl/time/civil_time.cc | 88 ++ absl/time/civil_time.h | 487 +++++++++ absl/time/civil_time_benchmark.cc | 57 + absl/time/civil_time_test.cc | 1073 ++++++++++++++++++ absl/time/format_benchmark.cc | 7 +- absl/time/format_test.cc | 37 +- absl/time/internal/cctz/src/time_zone_posix.h | 40 +- absl/time/internal/test_util.cc | 6 - absl/time/internal/test_util.h | 24 - absl/time/time.cc | 299 +++-- absl/time/time.h | 614 ++++++----- absl/time/time_benchmark.cc | 38 +- absl/time/time_norm_test.cc | 306 ------ absl/time/time_test.cc | 330 +++--- 24 files changed, 2657 insertions(+), 2574 deletions(-) delete mode 100644 absl/hash/internal/city_crc.h create mode 100644 absl/time/civil_time.cc create mode 100644 absl/time/civil_time.h create mode 100644 absl/time/civil_time_benchmark.cc create mode 100644 absl/time/civil_time_test.cc delete mode 100644 absl/time/time_norm_test.cc (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index cd33a3ac..90917b70 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -1029,7 +1029,6 @@ ExpectedStats XorSeedExpectedStats() { {{0.95, 0.1}}, {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}}; } - break; case 16: if (kRandomizesInserts) { return {0.1, @@ -1042,10 +1041,8 @@ ExpectedStats XorSeedExpectedStats() { {{0.95, 0.05}}, {{0.95, 0}, {0.99, 1}, {0.999, 4}, {0.9999, 10}}}; } - break; - default: - ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); } + ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); return {}; } TEST(Table, DISABLED_EnsureNonQuadraticTopNXorSeedByProbeSeqLength) { @@ -1125,7 +1122,6 @@ ExpectedStats LinearTransformExpectedStats() { {{0.95, 0.3}}, {{0.95, 0}, {0.99, 3}, {0.999, 15}, {0.9999, 25}}}; } - break; case 16: if (kRandomizesInserts) { return {0.1, @@ -1138,10 +1134,8 @@ ExpectedStats LinearTransformExpectedStats() { {{0.95, 0.1}}, {{0.95, 0}, {0.99, 1}, {0.999, 6}, {0.9999, 10}}}; } - break; - default: - ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); } + ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); return {}; } TEST(Table, DISABLED_EnsureNonQuadraticTopNLinearTransformByProbeSeqLength) { @@ -1834,18 +1828,15 @@ std::vector> StringTablePefectRatios() { } else { return {{0.995, 0.01}, {0.97, 0.01}, {0.89, 0.02}}; } - break; case 16: if (kRandomizesInserts) { return {{0.973, 0.01}, {0.965, 0.01}, {0.92, 0.02}}; } else { return {{0.995, 0.005}, {0.99, 0.005}, {0.94, 0.01}}; } - break; - default: - // Ignore anything else. - return {}; } + ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); + return {}; } // This is almost a change detector, but it allows us to know how we are @@ -1884,18 +1875,15 @@ std::vector> IntTablePefectRatios() { } else { return {{0.99, 0.01}, {0.99, 0.01}, {0.95, 0.02}}; } - break; case 16: if (kRandomizesInserts) { return {{0.98, 0.02}, {0.978, 0.02}, {0.96, 0.02}}; } else { return {{0.998, 0.003}, {0.995, 0.01}, {0.975, 0.02}}; } - break; - default: - // Ignore anything else. - return {}; } + ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); + return {}; } // This is almost a change detector, but it allows us to know how we are diff --git a/absl/hash/BUILD.bazel b/absl/hash/BUILD.bazel index 50aa5506..4f7c94ce 100644 --- a/absl/hash/BUILD.bazel +++ b/absl/hash/BUILD.bazel @@ -93,7 +93,6 @@ cc_library( srcs = ["internal/city.cc"], hdrs = [ "internal/city.h", - "internal/city_crc.h", ], copts = ABSL_DEFAULT_COPTS, deps = [ diff --git a/absl/hash/CMakeLists.txt b/absl/hash/CMakeLists.txt index 35081e37..474092f0 100644 --- a/absl/hash/CMakeLists.txt +++ b/absl/hash/CMakeLists.txt @@ -20,7 +20,6 @@ list(APPEND HASH_PUBLIC_HEADERS list(APPEND HASH_INTERNAL_HEADERS "internal/city.h" - "internal/city_crc.h" "internal/hash.h" ) diff --git a/absl/hash/internal/city.cc b/absl/hash/internal/city.cc index 8f72dd1b..122906fa 100644 --- a/absl/hash/internal/city.cc +++ b/absl/hash/internal/city.cc @@ -340,251 +340,5 @@ uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, return HashLen16(CityHash64(s, len) - seed0, seed1); } -// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings -// of any length representable in signed long. Based on City and Murmur. -static uint128 CityMurmur(const char *s, size_t len, uint128 seed) { - uint64_t a = Uint128Low64(seed); - uint64_t b = Uint128High64(seed); - uint64_t c = 0; - uint64_t d = 0; - int64_t l = len - 16; - if (l <= 0) { // len <= 16 - a = ShiftMix(a * k1) * k1; - c = b * k1 + HashLen0to16(s, len); - d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c)); - } else { // len > 16 - c = HashLen16(Fetch64(s + len - 8) + k1, a); - d = HashLen16(b + len, c + Fetch64(s + len - 16)); - a += d; - do { - a ^= ShiftMix(Fetch64(s) * k1) * k1; - a *= k1; - b ^= a; - c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; - c *= k1; - d ^= c; - s += 16; - l -= 16; - } while (l > 0); - } - a = HashLen16(a, c); - b = HashLen16(d, b); - return uint128(a ^ b, HashLen16(b, a)); -} - -uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { - if (len < 128) { - return CityMurmur(s, len, seed); - } - - // We expect len >= 128 to be the common case. Keep 56 bytes of state: - // v, w, x, y, and z. - std::pair v, w; - uint64_t x = Uint128Low64(seed); - uint64_t y = Uint128High64(seed); - uint64_t z = len * k1; - v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); - v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); - w.first = Rotate(y + z, 35) * k1 + x; - w.second = Rotate(x + Fetch64(s + 88), 53) * k1; - - // This is the same inner loop as CityHash64(), manually unrolled. - do { - x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; - y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; - x ^= w.second; - y += v.first + Fetch64(s + 40); - z = Rotate(z + w.first, 33) * k1; - v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); - w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16)); - std::swap(z, x); - s += 64; - x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; - y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; - x ^= w.second; - y += v.first + Fetch64(s + 40); - z = Rotate(z + w.first, 33) * k1; - v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); - w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16)); - std::swap(z, x); - s += 64; - len -= 128; - } while (ABSL_PREDICT_TRUE(len >= 128)); - x += Rotate(v.first + z, 49) * k0; - y = y * k0 + Rotate(w.second, 37); - z = z * k0 + Rotate(w.first, 27); - w.first *= 9; - v.first *= k0; - // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. - for (size_t tail_done = 0; tail_done < len;) { - tail_done += 32; - y = Rotate(x + y, 42) * k0 + v.second; - w.first += Fetch64(s + len - tail_done + 16); - x = x * k0 + w.first; - z += w.second + Fetch64(s + len - tail_done); - w.second += v.first; - v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second); - v.first *= k0; - } - // At this point our 56 bytes of state should contain more than - // enough information for a strong 128-bit hash. We use two - // different 56-byte-to-8-byte hashes to get a 16-byte final result. - x = HashLen16(x, v.first); - y = HashLen16(y + z, w.first); - return uint128(HashLen16(x + v.second, w.second) + y, - HashLen16(x + w.second, y + v.second)); -} - -uint128 CityHash128(const char *s, size_t len) { - return len >= 16 - ? CityHash128WithSeed(s + 16, len - 16, - uint128(Fetch64(s), Fetch64(s + 8) + k0)) - : CityHash128WithSeed(s, len, uint128(k0, k1)); -} } // namespace hash_internal } // namespace absl - -#ifdef __SSE4_2__ -#include -#include "absl/hash/internal/city_crc.h" - -namespace absl { -namespace hash_internal { - -// Requires len >= 240. -static void CityHashCrc256Long(const char *s, size_t len, uint32_t seed, - uint64_t *result) { - uint64_t a = Fetch64(s + 56) + k0; - uint64_t b = Fetch64(s + 96) + k0; - uint64_t c = result[0] = HashLen16(b, len); - uint64_t d = result[1] = Fetch64(s + 120) * k0 + len; - uint64_t e = Fetch64(s + 184) + seed; - uint64_t f = 0; - uint64_t g = 0; - uint64_t h = c + d; - uint64_t x = seed; - uint64_t y = 0; - uint64_t z = 0; - - // 240 bytes of input per iter. - size_t iters = len / 240; - len -= iters * 240; - do { -#undef CHUNK -#define CHUNK(r) \ - PERMUTE3(x, z, y); \ - b += Fetch64(s); \ - c += Fetch64(s + 8); \ - d += Fetch64(s + 16); \ - e += Fetch64(s + 24); \ - f += Fetch64(s + 32); \ - a += b; \ - h += f; \ - b += c; \ - f += d; \ - g += e; \ - e += z; \ - g += x; \ - z = _mm_crc32_u64(z, b + g); \ - y = _mm_crc32_u64(y, e + h); \ - x = _mm_crc32_u64(x, f + a); \ - e = Rotate(e, r); \ - c += e; \ - s += 40 - - CHUNK(0); - PERMUTE3(a, h, c); - CHUNK(33); - PERMUTE3(a, h, f); - CHUNK(0); - PERMUTE3(b, h, f); - CHUNK(42); - PERMUTE3(b, h, d); - CHUNK(0); - PERMUTE3(b, h, e); - CHUNK(33); - PERMUTE3(a, h, e); - } while (--iters > 0); - - while (len >= 40) { - CHUNK(29); - e ^= Rotate(a, 20); - h += Rotate(b, 30); - g ^= Rotate(c, 40); - f += Rotate(d, 34); - PERMUTE3(c, h, g); - len -= 40; - } - if (len > 0) { - s = s + len - 40; - CHUNK(33); - e ^= Rotate(a, 43); - h += Rotate(b, 42); - g ^= Rotate(c, 41); - f += Rotate(d, 40); - } - result[0] ^= h; - result[1] ^= g; - g += h; - a = HashLen16(a, g + z); - x += y << 32; - b += x; - c = HashLen16(c, z) + h; - d = HashLen16(d, e + result[0]); - g += e; - h += HashLen16(x, f); - e = HashLen16(a, d) + g; - z = HashLen16(b, c) + a; - y = HashLen16(g, h) + c; - result[0] = e + z + y + x; - a = ShiftMix((a + y) * k0) * k0 + b; - result[1] += a + result[0]; - a = ShiftMix(a * k0) * k0 + c; - result[2] = a + result[1]; - a = ShiftMix((a + e) * k0) * k0; - result[3] = a + result[2]; -} - -// Requires len < 240. -static void CityHashCrc256Short(const char *s, size_t len, uint64_t *result) { - char buf[240]; - memcpy(buf, s, len); - memset(buf + len, 0, 240 - len); - CityHashCrc256Long(buf, 240, ~static_cast(len), result); -} - -void CityHashCrc256(const char *s, size_t len, uint64_t *result) { - if (ABSL_PREDICT_TRUE(len >= 240)) { - CityHashCrc256Long(s, len, 0, result); - } else { - CityHashCrc256Short(s, len, result); - } -} - -uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { - if (len <= 900) { - return CityHash128WithSeed(s, len, seed); - } else { - uint64_t result[4]; - CityHashCrc256(s, len, result); - uint64_t u = Uint128High64(seed) + result[0]; - uint64_t v = Uint128Low64(seed) + result[1]; - return uint128(HashLen16(u, v + result[2]), - HashLen16(Rotate(v, 32), u * k0 + result[3])); - } -} - -uint128 CityHashCrc128(const char *s, size_t len) { - if (len <= 900) { - return CityHash128(s, len); - } else { - uint64_t result[4]; - CityHashCrc256(s, len, result); - return uint128(result[2], result[3]); - } -} - -} // namespace hash_internal -} // namespace absl - -#endif diff --git a/absl/hash/internal/city.h b/absl/hash/internal/city.h index 55b37b87..16df5563 100644 --- a/absl/hash/internal/city.h +++ b/absl/hash/internal/city.h @@ -23,15 +23,6 @@ // is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash // tables and most other hashing (excluding cryptography). // -// For 64-bit x86 code, on long strings, the picture is more complicated. -// On many recent Intel CPUs, such as Nehalem, Westmere, Sandy Bridge, etc., -// CityHashCrc128 appears to be faster than all competitors of comparable -// quality. CityHash128 is also good but not quite as fast. We believe our -// nearest competitor is Bob Jenkins' Spooky. We don't have great data for -// other 64-bit CPUs, but for long strings we know that Spooky is slightly -// faster than CityHash on some relatively recent AMD x86-64 CPUs, for example. -// Note that CityHashCrc128 is declared in citycrc.h. -// // For 32-bit x86 code, we don't know of anything faster than CityHash32 that // is of comparable quality. We believe our nearest competitor is Murmur3A. // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.) @@ -79,13 +70,6 @@ uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed); uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1); -// Hash function for a byte array. -uint128 CityHash128(const char *s, size_t len); - -// Hash function for a byte array. For convenience, a 128-bit seed is also -// hashed into the result. -uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); - // Hash function for a byte array. Most useful in 32-bit binaries. uint32_t CityHash32(const char *s, size_t len); diff --git a/absl/hash/internal/city_crc.h b/absl/hash/internal/city_crc.h deleted file mode 100644 index 6be6557d..00000000 --- a/absl/hash/internal/city_crc.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2018 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// This file declares the subset of the CityHash functions that require -// _mm_crc32_u64(). See the CityHash README for details. -// -// Functions in the CityHash family are not suitable for cryptography. - -#ifndef ABSL_HASH_INTERNAL_CITY_CRC_H_ -#define ABSL_HASH_INTERNAL_CITY_CRC_H_ - -#include "absl/hash/internal/city.h" - -namespace absl { -namespace hash_internal { - -// Hash function for a byte array. -uint128 CityHashCrc128(const char *s, size_t len); - -// Hash function for a byte array. For convenience, a 128-bit seed is also -// hashed into the result. -uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed); - -// Hash function for a byte array. Sets result[0] ... result[3]. -void CityHashCrc256(const char *s, size_t len, uint64_t *result); - -} // namespace hash_internal -} // namespace absl - -#endif // ABSL_HASH_INTERNAL_CITY_CRC_H_ diff --git a/absl/hash/internal/city_test.cc b/absl/hash/internal/city_test.cc index 678da53d..0427cd1a 100644 --- a/absl/hash/internal/city_test.cc +++ b/absl/hash/internal/city_test.cc @@ -18,9 +18,6 @@ #include #include #include "gtest/gtest.h" -#ifdef __SSE4_2__ -#include "absl/hash/internal/city_crc.h" -#endif namespace absl { namespace hash_internal { @@ -28,7 +25,6 @@ namespace hash_internal { static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; static const uint64_t kSeed0 = 1234567; static const uint64_t kSeed1 = k0; -static const uint128 kSeed128(kSeed0, kSeed1); static const int kDataSize = 1 << 20; static const int kTestSize = 300; @@ -49,1754 +45,539 @@ void setup() { } #define C(x) 0x##x##ULL -static const uint64_t testdata[kTestSize][16] = { +static const uint64_t testdata[kTestSize][4] = { {C(9ae16a3b2f90404f), C(75106db890237a4a), C(3feac5f636039766), - C(3df09dfc64c09a2b), C(3cb540c392e51e29), C(6b56343feac0663), - C(5b7bc50fd8e8ad92), C(3df09dfc64c09a2b), C(3cb540c392e51e29), - C(6b56343feac0663), C(5b7bc50fd8e8ad92), C(95162f24e6a5f930), - C(6808bdf4f1eb06e0), C(b3b1f3a67b624d82), C(c9a62f12bd4cd80b), C(dc56d17a)}, {C(541150e87f415e96), C(1aef0d24b3148a1a), C(bacc300e1e82345a), - C(c3cdc41e1df33513), C(2c138ff2596d42f6), C(f58e9082aed3055f), - C(162e192b2957163d), C(c3cdc41e1df33513), C(2c138ff2596d42f6), - C(f58e9082aed3055f), C(162e192b2957163d), C(fb99e85e0d16f90c), - C(608462c15bdf27e8), C(e7d2c5c943572b62), C(1baaa9327642798c), C(99929334)}, - {C(f3786a4b25827c1), C(34ee1a2bf767bd1c), C(2f15ca2ebfb631f2), - C(3149ba1dac77270d), C(70e2e076e30703c), C(59bcc9659bc5296), - C(9ecbc8132ae2f1d7), C(3149ba1dac77270d), C(70e2e076e30703c), - C(59bcc9659bc5296), C(9ecbc8132ae2f1d7), C(a01d30789bad7cf2), - C(ae03fe371981a0e0), C(127e3883b8788934), C(d0ac3d4c0a6fca32), - C(4252edb7)}, + {C(f3786a4b25827c1), C(34ee1a2bf767bd1c), C(2f15ca2ebfb631f2), C(4252edb7)}, {C(ef923a7a1af78eab), C(79163b1e1e9a9b18), C(df3b2aca6e1e4a30), - C(2193fb7620cbf23b), C(8b6a8ff06cda8302), C(1a44469afd3e091f), - C(8b0449376612506), C(2193fb7620cbf23b), C(8b6a8ff06cda8302), - C(1a44469afd3e091f), C(8b0449376612506), C(e9d9d41c32ad91d1), - C(b44ab09f58e3c608), C(19e9175f9fcf784), C(839b3c9581b4a480), C(ebc34f3c)}, + C(ebc34f3c)}, {C(11df592596f41d88), C(843ec0bce9042f9c), C(cce2ea1e08b1eb30), - C(4d09e42f09cc3495), C(666236631b9f253b), C(d28b3763cd02b6a3), - C(43b249e57c4d0c1b), C(4d09e42f09cc3495), C(666236631b9f253b), - C(d28b3763cd02b6a3), C(43b249e57c4d0c1b), C(3887101c8adea101), - C(8a9355d4efc91df0), C(3e610944cc9fecfd), C(5bf9eb60b08ac0ce), C(26f2b463)}, {C(831f448bdc5600b3), C(62a24be3120a6919), C(1b44098a41e010da), - C(dc07df53b949c6b), C(d2b11b2081aeb002), C(d212b02c1b13f772), - C(c0bed297b4be1912), C(dc07df53b949c6b), C(d2b11b2081aeb002), - C(d212b02c1b13f772), C(c0bed297b4be1912), C(682d3d2ad304e4af), - C(40e9112a655437a1), C(268b09f7ee09843f), C(6b9698d43859ca47), C(b042c047)}, - {C(3eca803e70304894), C(d80de767e4a920a), C(a51cfbb292efd53d), - C(d183dcda5f73edfa), C(3a93cbf40f30128c), C(1a92544d0b41dbda), - C(aec2c4bee81975e1), C(d183dcda5f73edfa), C(3a93cbf40f30128c), - C(1a92544d0b41dbda), C(aec2c4bee81975e1), C(5f91814d1126ba4b), - C(f8ac57eee87fcf1f), C(c55c644a5d0023cd), C(adb761e827825ff2), - C(e73bb0a8)}, + {C(3eca803e70304894), C(d80de767e4a920a), C(a51cfbb292efd53d), C(e73bb0a8)}, {C(1b5a063fb4c7f9f1), C(318dbc24af66dee9), C(10ef7b32d5c719af), - C(b140a02ef5c97712), C(b7d00ef065b51b33), C(635121d532897d98), - C(532daf21b312a6d6), C(b140a02ef5c97712), C(b7d00ef065b51b33), - C(635121d532897d98), C(532daf21b312a6d6), C(c0b09b75d943910), - C(8c84dfb5ef2a8e96), C(e5c06034b0353433), C(3170faf1c33a45dd), C(91dfdd75)}, {C(a0f10149a0e538d6), C(69d008c20f87419f), C(41b36376185b3e9e), - C(26b6689960ccf81d), C(55f23b27bb9efd94), C(3a17f6166dd765db), - C(c891a8a62931e782), C(26b6689960ccf81d), C(55f23b27bb9efd94), - C(3a17f6166dd765db), C(c891a8a62931e782), C(23852dc37ddd2607), - C(8b7f1b1ec897829e), C(d1d69452a54eed8a), C(56431f2bd766ec24), C(c87f95de)}, {C(fb8d9c70660b910b), C(a45b0cc3476bff1b), C(b28d1996144f0207), - C(98ec31113e5e35d2), C(5e4aeb853f1b9aa7), C(bcf5c8fe4465b7c8), - C(b1ea3a8243996f15), C(98ec31113e5e35d2), C(5e4aeb853f1b9aa7), - C(bcf5c8fe4465b7c8), C(b1ea3a8243996f15), C(cabbccedb6407571), - C(d1e40a84c445ec3a), C(33302aa908cf4039), C(9f15f79211b5cdf8), C(3f5538ef)}, {C(236827beae282a46), C(e43970221139c946), C(4f3ac6faa837a3aa), - C(71fec0f972248915), C(2170ec2061f24574), C(9eb346b6caa36e82), - C(2908f0fdbca48e73), C(71fec0f972248915), C(2170ec2061f24574), - C(9eb346b6caa36e82), C(2908f0fdbca48e73), C(8101c99f07c64abb), - C(b9f4b02b1b6a96a7), C(583a2b10cd222f88), C(199dae4cf9db24c), C(70eb1a1f)}, + C(70eb1a1f)}, {C(c385e435136ecf7c), C(d9d17368ff6c4a08), C(1b31eed4e5251a67), - C(df01a322c43a6200), C(298b65a1714b5a7e), C(933b83f0aedf23c), - C(157bcb44d63f765a), C(df01a322c43a6200), C(298b65a1714b5a7e), - C(933b83f0aedf23c), C(157bcb44d63f765a), C(d6e9fc7a272d8b51), - C(3ee5073ef1a9b777), C(63149e31fac02c59), C(2f7979ff636ba1d8), C(cfd63b83)}, {C(e3f6828b6017086d), C(21b4d1900554b3b0), C(bef38be1809e24f1), - C(d93251758985ee6c), C(32a9e9f82ba2a932), C(3822aacaa95f3329), - C(db349b2f90a490d8), C(d93251758985ee6c), C(32a9e9f82ba2a932), - C(3822aacaa95f3329), C(db349b2f90a490d8), C(8d49194a894a19ca), - C(79a78b06e42738e6), C(7e0f1eda3d390c66), C(1c291d7e641100a5), C(894a52ef)}, {C(851fff285561dca0), C(4d1277d73cdf416f), C(28ccffa61010ebe2), - C(77a4ccacd131d9ee), C(e1d08eeb2f0e29aa), C(70b9e3051383fa45), - C(582d0120425caba), C(77a4ccacd131d9ee), C(e1d08eeb2f0e29aa), - C(70b9e3051383fa45), C(582d0120425caba), C(a740eef1846e4564), - C(572dddb74ac3ae00), C(fdb5ca9579163bbd), C(a649b9b799c615d2), C(9cde6a54)}, {C(61152a63595a96d9), C(d1a3a91ef3a7ba45), C(443b6bb4a493ad0c), - C(a154296d11362d06), C(d0f0bf1f1cb02fc1), C(ccb87e09309f90d1), - C(b24a8e4881911101), C(a154296d11362d06), C(d0f0bf1f1cb02fc1), - C(ccb87e09309f90d1), C(b24a8e4881911101), C(1a481b4528559f58), - C(bf837a3150896995), C(4989ef6b941a3757), C(2e725ab72d0b2948), C(6c4898d5)}, {C(44473e03be306c88), C(30097761f872472a), C(9fd1b669bfad82d7), - C(3bab18b164396783), C(47e385ff9d4c06f), C(18062081bf558df), - C(63416eb68f104a36), C(3bab18b164396783), C(47e385ff9d4c06f), - C(18062081bf558df), C(63416eb68f104a36), C(4abda1560c47ac80), - C(1ea0e63dc6587aee), C(33ec79d92ebc1de), C(94f9dccef771e048), C(13e1978e)}, - {C(3ead5f21d344056), C(fb6420393cfb05c3), C(407932394cbbd303), - C(ac059617f5906673), C(94d50d3dcd3069a7), C(2b26c3b92dea0f0), - C(99b7374cc78fc3fb), C(ac059617f5906673), C(94d50d3dcd3069a7), - C(2b26c3b92dea0f0), C(99b7374cc78fc3fb), C(1a8e3c73cdd40ee8), - C(cbb5fca06747f45b), C(ceec44238b291841), C(28bf35cce9c90a25), C(51b4ba8)}, + C(13e1978e)}, + {C(3ead5f21d344056), C(fb6420393cfb05c3), C(407932394cbbd303), C(51b4ba8)}, {C(6abbfde37ee03b5b), C(83febf188d2cc113), C(cda7b62d94d5b8ee), - C(a4375590b8ae7c82), C(168fd42f9ecae4ff), C(23bbde43de2cb214), - C(a8c333112a243c8c), C(a4375590b8ae7c82), C(168fd42f9ecae4ff), - C(23bbde43de2cb214), C(a8c333112a243c8c), C(10ac012e8c518b49), - C(64a44605d8b29458), C(a67e701d2a679075), C(3a3a20f43ec92303), C(b6b06e40)}, - {C(943e7ed63b3c080), C(1ef207e9444ef7f8), C(ef4a9f9f8c6f9b4a), - C(6b54fc38d6a84108), C(32f4212a47a4665), C(6b5a9a8f64ee1da6), - C(9f74e86c6da69421), C(6b54fc38d6a84108), C(32f4212a47a4665), - C(6b5a9a8f64ee1da6), C(9f74e86c6da69421), C(946dd0cb30c1a08e), - C(fdf376956907eaaa), C(a59074c6eec03028), C(b1a3abcf283f34ac), C(240a2f2)}, + {C(943e7ed63b3c080), C(1ef207e9444ef7f8), C(ef4a9f9f8c6f9b4a), C(240a2f2)}, {C(d72ce05171ef8a1a), C(c6bd6bd869203894), C(c760e6396455d23a), - C(f86af0b40dcce7b), C(8d3c15d613394d3c), C(491e400491cd4ece), - C(7c19d3530ea3547f), C(f86af0b40dcce7b), C(8d3c15d613394d3c), - C(491e400491cd4ece), C(7c19d3530ea3547f), C(1362963a1dc32af9), - C(fb9bc11762e1385c), C(9e164ef1f5376083), C(6c15819b5e828a7e), C(5dcefc30)}, {C(4182832b52d63735), C(337097e123eea414), C(b5a72ca0456df910), - C(7ebc034235bc122f), C(d9a7783d4edd8049), C(5f8b04a15ae42361), - C(fc193363336453dd), C(7ebc034235bc122f), C(d9a7783d4edd8049), - C(5f8b04a15ae42361), C(fc193363336453dd), C(9b6c50224ef8c4f8), - C(ba225c7942d16c3f), C(6f6d55226a73c412), C(abca061fe072152a), C(7a48b105)}, {C(d6cdae892584a2cb), C(58de0fa4eca17dcd), C(43df30b8f5f1cb00), - C(9e4ea5a4941e097d), C(547e048d5a9daaba), C(eb6ecbb0b831d185), - C(e0168df5fad0c670), C(9e4ea5a4941e097d), C(547e048d5a9daaba), - C(eb6ecbb0b831d185), C(e0168df5fad0c670), C(afa9705f98c2c96a), - C(749436f48137a96b), C(759c041fc21df486), C(b23bf400107aa2ec), C(fd55007b)}, {C(5c8e90bc267c5ee4), C(e9ae044075d992d9), C(f234cbfd1f0a1e59), - C(ce2744521944f14c), C(104f8032f99dc152), C(4e7f425bfac67ca7), - C(9461b911a1c6d589), C(ce2744521944f14c), C(104f8032f99dc152), - C(4e7f425bfac67ca7), C(9461b911a1c6d589), C(5e5ecc726db8b60d), - C(cce68b0586083b51), C(8a7f8e54a9cba0fc), C(42f010181d16f049), C(6b95894c)}, {C(bbd7f30ac310a6f3), C(b23b570d2666685f), C(fb13fb08c9814fe7), - C(4ee107042e512374), C(1e2c8c0d16097e13), C(210c7500995aa0e6), - C(6c13190557106457), C(4ee107042e512374), C(1e2c8c0d16097e13), - C(210c7500995aa0e6), C(6c13190557106457), C(a99b31c96777f381), - C(8312ae8301d386c0), C(ed5042b2a4fa96a3), C(d71d1bb23907fe97), C(3360e827)}, - {C(36a097aa49519d97), C(8204380a73c4065), C(77c2004bdd9e276a), - C(6ee1f817ce0b7aee), C(e9dcb3507f0596ca), C(6bc63c666b5100e2), - C(e0b056f1821752af), C(6ee1f817ce0b7aee), C(e9dcb3507f0596ca), - C(6bc63c666b5100e2), C(e0b056f1821752af), C(8ea1114e60292678), - C(904b80b46becc77), C(46cd9bb6e9dff52f), C(4c91e3b698355540), C(45177e0b)}, - {C(dc78cb032c49217), C(112464083f83e03a), C(96ae53e28170c0f5), - C(d367ff54952a958), C(cdad930657371147), C(aa24dc2a9573d5fe), - C(eb136daa89da5110), C(d367ff54952a958), C(cdad930657371147), - C(aa24dc2a9573d5fe), C(eb136daa89da5110), C(de623005f6d46057), - C(b50c0c92b95e9b7f), C(a8aa54050b81c978), C(573fb5c7895af9b5), - C(7c6fffe4)}, + {C(36a097aa49519d97), C(8204380a73c4065), C(77c2004bdd9e276a), C(45177e0b)}, + {C(dc78cb032c49217), C(112464083f83e03a), C(96ae53e28170c0f5), C(7c6fffe4)}, {C(441593e0da922dfe), C(936ef46061469b32), C(204a1921197ddd87), - C(50d8a70e7a8d8f56), C(256d150ae75dab76), C(e81f4c4a1989036a), - C(d0f8db365f9d7e00), C(50d8a70e7a8d8f56), C(256d150ae75dab76), - C(e81f4c4a1989036a), C(d0f8db365f9d7e00), C(753d686677b14522), - C(9f76e0cb6f2d0a66), C(ab14f95988ec0d39), C(97621d9da9c9812f), C(bbc78da4)}, {C(2ba3883d71cc2133), C(72f2bbb32bed1a3c), C(27e1bd96d4843251), - C(a90f761e8db1543a), C(c339e23c09703cd8), C(f0c6624c4b098fd3), - C(1bae2053e41fa4d9), C(a90f761e8db1543a), C(c339e23c09703cd8), - C(f0c6624c4b098fd3), C(1bae2053e41fa4d9), C(3589e273c22ba059), - C(63798246e5911a0b), C(18e710ec268fc5dc), C(714a122de1d074f3), C(c5c25d39)}, {C(f2b6d2adf8423600), C(7514e2f016a48722), C(43045743a50396ba), - C(23dacb811652ad4f), C(c982da480e0d4c7d), C(3a9c8ed5a399d0a9), - C(951b8d084691d4e4), C(23dacb811652ad4f), C(c982da480e0d4c7d), - C(3a9c8ed5a399d0a9), C(951b8d084691d4e4), C(d9f87b4988cff2f7), - C(217a191d986aa3bc), C(6ad23c56b480350), C(dd78673938ceb2e7), C(b6e5d06e)}, + C(b6e5d06e)}, {C(38fffe7f3680d63c), C(d513325255a7a6d1), C(31ed47790f6ca62f), - C(c801faaa0a2e331f), C(491dbc58279c7f88), C(9c0178848321c97a), - C(9d934f814f4d6a3c), C(c801faaa0a2e331f), C(491dbc58279c7f88), - C(9c0178848321c97a), C(9d934f814f4d6a3c), C(606a3e4fc8763192), - C(bc15cb36a677ee84), C(52d5904157e1fe71), C(1588dd8b1145b79b), C(6178504e)}, - {C(b7477bf0b9ce37c6), C(63b1c580a7fd02a4), C(f6433b9f10a5dac), - C(68dd76db9d64eca7), C(36297682b64b67), C(42b192d71f414b7a), - C(79692cef44fa0206), C(68dd76db9d64eca7), C(36297682b64b67), - C(42b192d71f414b7a), C(79692cef44fa0206), C(f0979252f4776d07), - C(4b87cd4f1c9bbf52), C(51b84bbc6312c710), C(150720fbf85428a7), - C(bd4c3637)}, + {C(b7477bf0b9ce37c6), C(63b1c580a7fd02a4), C(f6433b9f10a5dac), C(bd4c3637)}, {C(55bdb0e71e3edebd), C(c7ab562bcf0568bc), C(43166332f9ee684f), - C(b2e25964cd409117), C(a010599d6287c412), C(fa5d6461e768dda2), - C(cb3ce74e8ec4f906), C(b2e25964cd409117), C(a010599d6287c412), - C(fa5d6461e768dda2), C(cb3ce74e8ec4f906), C(6120abfd541a2610), - C(aa88b148cc95794d), C(2686ca35df6590e3), C(c6b02d18616ce94d), C(6e7ac474)}, - {C(782fa1b08b475e7), C(fb7138951c61b23b), C(9829105e234fb11e), - C(9a8c431f500ef06e), C(d848581a580b6c12), C(fecfe11e13a2bdb4), - C(6c4fa0273d7db08c), C(9a8c431f500ef06e), C(d848581a580b6c12), - C(fecfe11e13a2bdb4), C(6c4fa0273d7db08c), C(482f43bf5ae59fcb), - C(f651fbca105d79e6), C(f09f78695d865817), C(7a99d0092085cf47), - C(1fb4b518)}, + {C(782fa1b08b475e7), C(fb7138951c61b23b), C(9829105e234fb11e), C(1fb4b518)}, {C(c5dc19b876d37a80), C(15ffcff666cfd710), C(e8c30c72003103e2), - C(7870765b470b2c5d), C(78a9103ff960d82), C(7bb50ffc9fac74b3), - C(477e70ab2b347db2), C(7870765b470b2c5d), C(78a9103ff960d82), - C(7bb50ffc9fac74b3), C(477e70ab2b347db2), C(a625238bdf7c07cf), - C(1128d515174809f5), C(b0f1647e82f45873), C(17792d1c4f222c39), C(31d13d6d)}, {C(5e1141711d2d6706), C(b537f6dee8de6933), C(3af0a1fbbe027c54), - C(ea349dbc16c2e441), C(38a7455b6a877547), C(5f97b9750e365411), - C(e8cde7f93af49a3), C(ea349dbc16c2e441), C(38a7455b6a877547), - C(5f97b9750e365411), C(e8cde7f93af49a3), C(ba101925ec1f7e26), - C(d5e84cab8192c71e), C(e256427726fdd633), C(a4f38e2c6116890d), C(26fa72e3)}, - {C(782edf6da001234f), C(f48cbd5c66c48f3), C(808754d1e64e2a32), - C(5d9dde77353b1a6d), C(11f58c54581fa8b1), C(da90fa7c28c37478), - C(5e9a2eafc670a88a), C(5d9dde77353b1a6d), C(11f58c54581fa8b1), - C(da90fa7c28c37478), C(5e9a2eafc670a88a), C(e35e1bc172e011ef), - C(bf9255a4450ae7fe), C(55f85194e26bc55f), C(4f327873e14d0e54), - C(6a7433bf)}, + {C(782edf6da001234f), C(f48cbd5c66c48f3), C(808754d1e64e2a32), C(6a7433bf)}, {C(d26285842ff04d44), C(8f38d71341eacca9), C(5ca436f4db7a883c), - C(bf41e5376b9f0eec), C(2252d21eb7e1c0e9), C(f4b70a971855e732), - C(40c7695aa3662afd), C(bf41e5376b9f0eec), C(2252d21eb7e1c0e9), - C(f4b70a971855e732), C(40c7695aa3662afd), C(770fe19e16ab73bb), - C(d603ebda6393d749), C(e58c62439aa50dbd), C(96d51e5a02d2d7cf), C(4e6df758)}, {C(c6ab830865a6bae6), C(6aa8e8dd4b98815c), C(efe3846713c371e5), - C(a1924cbf0b5f9222), C(7f4872369c2b4258), C(cd6da30530f3ea89), - C(b7f8b9a704e6cea1), C(a1924cbf0b5f9222), C(7f4872369c2b4258), - C(cd6da30530f3ea89), C(b7f8b9a704e6cea1), C(fa06ff40433fd535), - C(fb1c36fe8f0737f1), C(bb7050561171f80), C(b1bc23235935d897), C(d57f63ea)}, - {C(44b3a1929232892), C(61dca0e914fc217), C(a607cc142096b964), - C(f7dbc8433c89b274), C(2f5f70581c9b7d32), C(39bf5e5fec82dcca), - C(8ade56388901a619), C(f7dbc8433c89b274), C(2f5f70581c9b7d32), - C(39bf5e5fec82dcca), C(8ade56388901a619), C(c1c6a725caab3ea9), - C(c1c7906c2f80b898), C(9c3871a04cc884e6), C(df01813cbbdf217f), - C(52ef73b3)}, - {C(4b603d7932a8de4f), C(fae64c464b8a8f45), C(8fafab75661d602a), - C(8ffe870ef4adc087), C(65bea2be41f55b54), C(82f3503f636aef1), - C(5f78a282378b6bb0), C(8ffe870ef4adc087), C(65bea2be41f55b54), - C(82f3503f636aef1), C(5f78a282378b6bb0), C(7bf2422c0beceddb), - C(9d238d4780114bd), C(7ad198311906597f), C(ec8f892c0422aca3), C(3cb36c3)}, + C(d57f63ea)}, + {C(44b3a1929232892), C(61dca0e914fc217), C(a607cc142096b964), C(52ef73b3)}, + {C(4b603d7932a8de4f), C(fae64c464b8a8f45), C(8fafab75661d602a), C(3cb36c3)}, {C(4ec0b54cf1566aff), C(30d2c7269b206bf4), C(77c22e82295e1061), - C(3df9b04434771542), C(feddce785ccb661f), C(a644aff716928297), - C(dd46aee73824b4ed), C(3df9b04434771542), C(feddce785ccb661f), - C(a644aff716928297), C(dd46aee73824b4ed), C(bf8d71879da29b02), - C(fc82dccbfc8022a0), C(31bfcd0d9f48d1d3), C(c64ee24d0e7b5f8b), C(72c39bea)}, {C(ed8b7a4b34954ff7), C(56432de31f4ee757), C(85bd3abaa572b155), - C(7d2c38a926dc1b88), C(5245b9eb4cd6791d), C(fb53ab03b9ad0855), - C(3664026c8fc669d7), C(7d2c38a926dc1b88), C(5245b9eb4cd6791d), - C(fb53ab03b9ad0855), C(3664026c8fc669d7), C(45024d5080bc196), - C(b236ebec2cc2740), C(27231ad0e3443be4), C(145780b63f809250), C(a65aa25c)}, + C(a65aa25c)}, {C(5d28b43694176c26), C(714cc8bc12d060ae), C(3437726273a83fe6), - C(864b1b28ec16ea86), C(6a78a5a4039ec2b9), C(8e959533e35a766), - C(347b7c22b75ae65f), C(864b1b28ec16ea86), C(6a78a5a4039ec2b9), - C(8e959533e35a766), C(347b7c22b75ae65f), C(5005892bb61e647c), - C(fe646519b4a1894d), C(cd801026f74a8a53), C(8713463e9a1ab9ce), C(74740539)}, {C(6a1ef3639e1d202e), C(919bc1bd145ad928), C(30f3f7e48c28a773), - C(2e8c49d7c7aaa527), C(5e2328fc8701db7c), C(89ef1afca81f7de8), - C(b1857db11985d296), C(2e8c49d7c7aaa527), C(5e2328fc8701db7c), - C(89ef1afca81f7de8), C(b1857db11985d296), C(17763d695f616115), - C(b8f7bf1fcdc8322c), C(cf0c61938ab07a27), C(1122d3e6edb4e866), C(c3ae3c26)}, - {C(159f4d9e0307b111), C(3e17914a5675a0c), C(af849bd425047b51), - C(3b69edadf357432b), C(3a2e311c121e6bf2), C(380fad1e288d57e5), - C(bf7c7e8ef0e3b83a), C(3b69edadf357432b), C(3a2e311c121e6bf2), - C(380fad1e288d57e5), C(bf7c7e8ef0e3b83a), C(92966d5f4356ae9b), - C(2a03fc66c4d6c036), C(2516d8bddb0d5259), C(b3ffe9737ff5090), C(f29db8a2)}, + {C(159f4d9e0307b111), C(3e17914a5675a0c), C(af849bd425047b51), C(f29db8a2)}, {C(cc0a840725a7e25b), C(57c69454396e193a), C(976eaf7eee0b4540), - C(cd7a46850b95e901), C(c57f7d060dda246f), C(6b9406ead64079bf), - C(11b28e20a573b7bd), C(cd7a46850b95e901), C(c57f7d060dda246f), - C(6b9406ead64079bf), C(11b28e20a573b7bd), C(2d6db356e9369ace), - C(dc0afe10fba193), C(5cdb10885dbbfce), C(5c700e205782e35a), C(1ef4cbf4)}, + C(1ef4cbf4)}, {C(a2b27ee22f63c3f1), C(9ebde0ce1b3976b2), C(2fe6a92a257af308), - C(8c1df927a930af59), C(a462f4423c9e384e), C(236542255b2ad8d9), - C(595d201a2c19d5bc), C(8c1df927a930af59), C(a462f4423c9e384e), - C(236542255b2ad8d9), C(595d201a2c19d5bc), C(22c87d4604a67f3), - C(585a06eb4bc44c4f), C(b4175a7ac7eabcd8), C(a457d3eeba14ab8c), C(a9be6c41)}, - {C(d8f2f234899bcab3), C(b10b037297c3a168), C(debea2c510ceda7f), - C(9498fefb890287ce), C(ae68c2be5b1a69a6), C(6189dfba34ed656c), - C(91658f95836e5206), C(9498fefb890287ce), C(ae68c2be5b1a69a6), - C(6189dfba34ed656c), C(91658f95836e5206), C(c0bb4fff32aecd4d), - C(94125f505a50eef9), C(6ac406e7cfbce5bb), C(344a4b1dcdb7f5d8), C(fa31801)}, + {C(d8f2f234899bcab3), C(b10b037297c3a168), C(debea2c510ceda7f), C(fa31801)}, {C(584f28543864844f), C(d7cee9fc2d46f20d), C(a38dca5657387205), - C(7a0b6dbab9a14e69), C(c6d0a9d6b0e31ac4), C(a674d85812c7cf6), - C(63538c0351049940), C(7a0b6dbab9a14e69), C(c6d0a9d6b0e31ac4), - C(a674d85812c7cf6), C(63538c0351049940), C(9710e5f0bc93d1d), - C(c2bea5bd7c54ddd4), C(48739af2bed0d32d), C(ba2c4e09e21fba85), C(8331c5d8)}, - {C(a94be46dd9aa41af), C(a57e5b7723d3f9bd), C(34bf845a52fd2f), - C(843b58463c8df0ae), C(74b258324e916045), C(bdd7353230eb2b38), - C(fad31fced7abade5), C(843b58463c8df0ae), C(74b258324e916045), - C(bdd7353230eb2b38), C(fad31fced7abade5), C(2436aeafb0046f85), - C(65bc9af9e5e33161), C(92733b1b3ae90628), C(f48143eaf78a7a89), - C(e9876db8)}, + {C(a94be46dd9aa41af), C(a57e5b7723d3f9bd), C(34bf845a52fd2f), C(e9876db8)}, {C(9a87bea227491d20), C(a468657e2b9c43e7), C(af9ba60db8d89ef7), - C(cc76f429ea7a12bb), C(5f30eaf2bb14870a), C(434e824cb3e0cd11), - C(431a4d382e39d16e), C(cc76f429ea7a12bb), C(5f30eaf2bb14870a), - C(434e824cb3e0cd11), C(431a4d382e39d16e), C(9e51f913c4773a8), - C(32ab1925823d0add), C(99c61b54c1d8f69d), C(38cfb80f02b43b1f), C(27b0604e)}, {C(27688c24958d1a5c), C(e3b4a1c9429cf253), C(48a95811f70d64bc), - C(328063229db22884), C(67e9c95f8ba96028), C(7c6bf01c60436075), - C(fa55161e7d9030b2), C(328063229db22884), C(67e9c95f8ba96028), - C(7c6bf01c60436075), C(fa55161e7d9030b2), C(dadbc2f0dab91681), - C(da39d7a4934ca11), C(162e845d24c1b45c), C(eb5b9dcd8c6ed31b), C(dcec07f2)}, + C(dcec07f2)}, {C(5d1d37790a1873ad), C(ed9cd4bcc5fa1090), C(ce51cde05d8cd96a), - C(f72c26e624407e66), C(a0eb541bdbc6d409), C(c3f40a2f40b3b213), - C(6a784de68794492d), C(f72c26e624407e66), C(a0eb541bdbc6d409), - C(c3f40a2f40b3b213), C(6a784de68794492d), C(10a38a23dbef7937), - C(6a5560f853252278), C(c3387bbf3c7b82ba), C(fbee7c12eb072805), C(cff0a82a)}, {C(1f03fd18b711eea9), C(566d89b1946d381a), C(6e96e83fc92563ab), - C(405f66cf8cae1a32), C(d7261740d8f18ce6), C(fea3af64a413d0b2), - C(d64d1810e83520fe), C(405f66cf8cae1a32), C(d7261740d8f18ce6), - C(fea3af64a413d0b2), C(d64d1810e83520fe), C(e1334a00a580c6e8), - C(454049e1b52c15f), C(8895d823d9778247), C(efa7f2e88b826618), C(fec83621)}, - {C(f0316f286cf527b6), C(f84c29538de1aa5a), C(7612ed3c923d4a71), - C(d4eccebe9393ee8a), C(2eb7867c2318cc59), C(1ce621fd700fe396), - C(686450d7a346878a), C(d4eccebe9393ee8a), C(2eb7867c2318cc59), - C(1ce621fd700fe396), C(686450d7a346878a), C(75a5f37579f8b4cb), - C(500cc16eb6541dc7), C(b7b02317b539d9a6), C(3519ddff5bc20a29), C(743d8dc)}, + C(fec83621)}, + {C(f0316f286cf527b6), C(f84c29538de1aa5a), C(7612ed3c923d4a71), C(743d8dc)}, {C(297008bcb3e3401d), C(61a8e407f82b0c69), C(a4a35bff0524fa0e), - C(7a61d8f552a53442), C(821d1d8d8cfacf35), C(7cc06361b86d0559), - C(119b617a8c2be199), C(7a61d8f552a53442), C(821d1d8d8cfacf35), - C(7cc06361b86d0559), C(119b617a8c2be199), C(2996487da6721759), - C(61a901376070b91d), C(d88dee12ae9c9b3c), C(5665491be1fa53a7), C(64d41d26)}, - {C(43c6252411ee3be), C(b4ca1b8077777168), C(2746dc3f7da1737f), - C(2247a4b2058d1c50), C(1b3fa184b1d7bcc0), C(deb85613995c06ed), - C(cbe1d957485a3ccd), C(2247a4b2058d1c50), C(1b3fa184b1d7bcc0), - C(deb85613995c06ed), C(cbe1d957485a3ccd), C(dfe241f8f33c96b6), - C(6597eb05019c2109), C(da344b2a63a219cf), C(79b8e3887612378a), - C(acd90c81)}, + {C(43c6252411ee3be), C(b4ca1b8077777168), C(2746dc3f7da1737f), C(acd90c81)}, {C(ce38a9a54fad6599), C(6d6f4a90b9e8755e), C(c3ecc79ff105de3f), - C(e8b9ee96efa2d0e), C(90122905c4ab5358), C(84f80c832d71979c), - C(229310f3ffbbf4c6), C(e8b9ee96efa2d0e), C(90122905c4ab5358), - C(84f80c832d71979c), C(229310f3ffbbf4c6), C(cc9eb42100cd63a7), - C(7a283f2f3da7b9f), C(359b061d314e7a72), C(d0d959720028862), C(7c746a4b)}, - {C(270a9305fef70cf), C(600193999d884f3a), C(f4d49eae09ed8a1), - C(2e091b85660f1298), C(bfe37fae1cdd64c9), C(8dddfbab930f6494), - C(2ccf4b08f5d417a), C(2e091b85660f1298), C(bfe37fae1cdd64c9), - C(8dddfbab930f6494), C(2ccf4b08f5d417a), C(365c2ee85582fe6), - C(dee027bcd36db62a), C(b150994d3c7e5838), C(fdfd1a0e692e436d), - C(b1047e99)}, + C(7c746a4b)}, + {C(270a9305fef70cf), C(600193999d884f3a), C(f4d49eae09ed8a1), C(b1047e99)}, {C(e71be7c28e84d119), C(eb6ace59932736e6), C(70c4397807ba12c5), - C(7a9d77781ac53509), C(4489c3ccfda3b39c), C(fa722d4f243b4964), - C(25f15800bffdd122), C(7a9d77781ac53509), C(4489c3ccfda3b39c), - C(fa722d4f243b4964), C(25f15800bffdd122), C(ed85e4157fbd3297), - C(aab1967227d59efd), C(2199631212eb3839), C(3e4c19359aae1cc2), C(d1fd1068)}, {C(b5b58c24b53aaa19), C(d2a6ab0773dd897f), C(ef762fe01ecb5b97), - C(9deefbcfa4cab1f1), C(b58f5943cd2492ba), C(a96dcc4d1f4782a7), - C(102b62a82309dde5), C(9deefbcfa4cab1f1), C(b58f5943cd2492ba), - C(a96dcc4d1f4782a7), C(102b62a82309dde5), C(35fe52684763b338), - C(afe2616651eaad1f), C(43e38715bdfa05e7), C(83c9ba83b5ec4a40), C(56486077)}, {C(44dd59bd301995cf), C(3ccabd76493ada1a), C(540db4c87d55ef23), - C(cfc6d7adda35797), C(14c7d1f32332cf03), C(2d553ffbff3be99d), - C(c91c4ee0cb563182), C(cfc6d7adda35797), C(14c7d1f32332cf03), - C(2d553ffbff3be99d), C(c91c4ee0cb563182), C(9aa5e507f49136f0), - C(760c5dd1a82c4888), C(beea7e974a1cfb5c), C(640b247774fe4bf7), C(6069be80)}, - {C(b4d4789eb6f2630b), C(bf6973263ce8ef0e), C(d1c75c50844b9d3), - C(bce905900c1ec6ea), C(c30f304f4045487d), C(a5c550166b3a142b), - C(2f482b4e35327287), C(bce905900c1ec6ea), C(c30f304f4045487d), - C(a5c550166b3a142b), C(2f482b4e35327287), C(15b21ddddf355438), - C(496471fa3006bab), C(2a8fd458d06c1a32), C(db91e8ae812f0b8d), C(2078359b)}, + {C(b4d4789eb6f2630b), C(bf6973263ce8ef0e), C(d1c75c50844b9d3), C(2078359b)}, {C(12807833c463737c), C(58e927ea3b3776b4), C(72dd20ef1c2f8ad0), - C(910b610de7a967bf), C(801bc862120f6bf5), C(9653efeed5897681), - C(f5367ff83e9ebbb3), C(910b610de7a967bf), C(801bc862120f6bf5), - C(9653efeed5897681), C(f5367ff83e9ebbb3), C(cf56d489afd1b0bf), - C(c7c793715cae3de8), C(631f91d64abae47c), C(5f1f42fb14a444a2), C(9ea21004)}, {C(e88419922b87176f), C(bcf32f41a7ddbf6f), C(d6ebefd8085c1a0f), - C(d1d44fe99451ef72), C(ec951ba8e51e3545), C(c0ca86b360746e96), - C(aa679cc066a8040b), C(d1d44fe99451ef72), C(ec951ba8e51e3545), - C(c0ca86b360746e96), C(aa679cc066a8040b), C(51065861ece6ffc1), - C(76777368a2997e11), C(87f278f46731100c), C(bbaa4140bdba4527), C(9c9cfe88)}, {C(105191e0ec8f7f60), C(5918dbfcca971e79), C(6b285c8a944767b9), - C(d3e86ac4f5eccfa4), C(e5399df2b106ca1), C(814aadfacd217f1d), - C(2754e3def1c405a9), C(d3e86ac4f5eccfa4), C(e5399df2b106ca1), - C(814aadfacd217f1d), C(2754e3def1c405a9), C(99290323b9f06e74), - C(a9782e043f271461), C(13c8b3b8c275a860), C(6038d620e581e9e7), C(b70a6ddd)}, {C(a5b88bf7399a9f07), C(fca3ddfd96461cc4), C(ebe738fdc0282fc6), - C(69afbc800606d0fb), C(6104b97a9db12df7), C(fcc09198bb90bf9f), - C(c5e077e41a65ba91), C(69afbc800606d0fb), C(6104b97a9db12df7), - C(fcc09198bb90bf9f), C(c5e077e41a65ba91), C(db261835ee8aa08e), - C(db0ee662e5796dc9), C(fc1880ecec499e5f), C(648866fbe1502034), C(dea37298)}, {C(d08c3f5747d84f50), C(4e708b27d1b6f8ac), C(70f70fd734888606), - C(909ae019d761d019), C(368bf4aab1b86ef9), C(308bd616d5460239), - C(4fd33269f76783ea), C(909ae019d761d019), C(368bf4aab1b86ef9), - C(308bd616d5460239), C(4fd33269f76783ea), C(7d53b37c19713eab), - C(6bba6eabda58a897), C(91abb50efc116047), C(4e902f347e0e0e35), C(8f480819)}, - {C(2f72d12a40044b4b), C(889689352fec53de), C(f03e6ad87eb2f36), - C(ef79f28d874b9e2d), C(b512089e8e63b76c), C(24dc06833bf193a9), - C(3c23308ba8e99d7e), C(ef79f28d874b9e2d), C(b512089e8e63b76c), - C(24dc06833bf193a9), C(3c23308ba8e99d7e), C(5ceff7b85cacefb7), - C(ef390338898cd73), C(b12967d7d2254f54), C(de874cbd8aef7b75), C(30b3b16)}, + {C(2f72d12a40044b4b), C(889689352fec53de), C(f03e6ad87eb2f36), C(30b3b16)}, {C(aa1f61fdc5c2e11e), C(c2c56cd11277ab27), C(a1e73069fdf1f94f), - C(8184bab36bb79df0), C(c81929ce8655b940), C(301b11bf8a4d8ce8), - C(73126fd45ab75de9), C(8184bab36bb79df0), C(c81929ce8655b940), - C(301b11bf8a4d8ce8), C(73126fd45ab75de9), C(4bd6f76e4888229a), - C(9aae355b54a756d5), C(ca3de9726f6e99d5), C(83f80cac5bc36852), C(f31bc4e8)}, {C(9489b36fe2246244), C(3355367033be74b8), C(5f57c2277cbce516), - C(bc61414f9802ecaf), C(8edd1e7a50562924), C(48f4ab74a35e95f2), - C(cc1afcfd99a180e7), C(bc61414f9802ecaf), C(8edd1e7a50562924), - C(48f4ab74a35e95f2), C(cc1afcfd99a180e7), C(517dd5e3acf66110), - C(7dd3ad9e8978b30d), C(1f6d5dfc70de812b), C(947daaba6441aaf3), C(419f953b)}, {C(358d7c0476a044cd), C(e0b7b47bcbd8854f), C(ffb42ec696705519), - C(d45e44c263e95c38), C(df61db53923ae3b1), C(f2bc948cc4fc027c), - C(8a8000c6066772a3), C(d45e44c263e95c38), C(df61db53923ae3b1), - C(f2bc948cc4fc027c), C(8a8000c6066772a3), C(9fd93c942d31fa17), - C(d7651ecebe09cbd3), C(68682cefb6a6f165), C(541eb99a2dcee40e), C(20e9e76d)}, {C(b0c48df14275265a), C(9da4448975905efa), C(d716618e414ceb6d), - C(30e888af70df1e56), C(4bee54bd47274f69), C(178b4059e1a0afe5), - C(6e2c96b7f58e5178), C(30e888af70df1e56), C(4bee54bd47274f69), - C(178b4059e1a0afe5), C(6e2c96b7f58e5178), C(bb429d3b9275e9bc), - C(c198013f09cafdc6), C(ec0a6ee4fb5de348), C(744e1e8ed2eb1eb0), C(646f0ff8)}, {C(daa70bb300956588), C(410ea6883a240c6d), C(f5c8239fb5673eb3), - C(8b1d7bb4903c105f), C(cfb1c322b73891d4), C(5f3b792b22f07297), - C(fd64061f8be86811), C(8b1d7bb4903c105f), C(cfb1c322b73891d4), - C(5f3b792b22f07297), C(fd64061f8be86811), C(1d2db712921cfc2b), - C(cd1b2b2f2cee18ae), C(6b6f8790dc7feb09), C(46c179efa3f0f518), C(eeb7eca8)}, - {C(4ec97a20b6c4c7c2), C(5913b1cd454f29fd), C(a9629f9daf06d685), - C(852c9499156a8f3), C(3a180a6abfb79016), C(9fc3c4764037c3c9), - C(2890c42fc0d972cf), C(852c9499156a8f3), C(3a180a6abfb79016), - C(9fc3c4764037c3c9), C(2890c42fc0d972cf), C(1f92231d4e537651), - C(fab8bb07aa54b7b9), C(e05d2d771c485ed4), C(d50b34bf808ca731), C(8112bb9)}, + {C(4ec97a20b6c4c7c2), C(5913b1cd454f29fd), C(a9629f9daf06d685), C(8112bb9)}, {C(5c3323628435a2e8), C(1bea45ce9e72a6e3), C(904f0a7027ddb52e), - C(939f31de14dcdc7b), C(a68fdf4379df068), C(f169e1f0b835279d), - C(7498e432f9619b27), C(939f31de14dcdc7b), C(a68fdf4379df068), - C(f169e1f0b835279d), C(7498e432f9619b27), C(1aa2a1f11088e785), - C(d6ad72f45729de78), C(9a63814157c80267), C(55538e35c648e435), C(85a6d477)}, {C(c1ef26bea260abdb), C(6ee423f2137f9280), C(df2118b946ed0b43), - C(11b87fb1b900cc39), C(e33e59b90dd815b1), C(aa6cb5c4bafae741), - C(739699951ca8c713), C(11b87fb1b900cc39), C(e33e59b90dd815b1), - C(aa6cb5c4bafae741), C(739699951ca8c713), C(2b4389a967310077), - C(1d5382568a31c2c9), C(55d1e787fbe68991), C(277c254bc31301e7), C(56f76c84)}, {C(6be7381b115d653a), C(ed046190758ea511), C(de6a45ffc3ed1159), - C(a64760e4041447d0), C(e3eac49f3e0c5109), C(dd86c4d4cb6258e2), - C(efa9857afd046c7f), C(a64760e4041447d0), C(e3eac49f3e0c5109), - C(dd86c4d4cb6258e2), C(efa9857afd046c7f), C(fab793dae8246f16), - C(c9e3b121b31d094c), C(a2a0f55858465226), C(dba6f0ff39436344), C(9af45d55)}, {C(ae3eece1711b2105), C(14fd3f4027f81a4a), C(abb7e45177d151db), - C(501f3e9b18861e44), C(465201170074e7d8), C(96d5c91970f2cb12), - C(40fd28c43506c95d), C(501f3e9b18861e44), C(465201170074e7d8), - C(96d5c91970f2cb12), C(40fd28c43506c95d), C(e86c4b07802aaff3), - C(f317d14112372a70), C(641b13e587711650), C(4915421ab1090eaa), C(d1c33760)}, {C(376c28588b8fb389), C(6b045e84d8491ed2), C(4e857effb7d4e7dc), - C(154dd79fd2f984b4), C(f11171775622c1c3), C(1fbe30982e78e6f0), - C(a460a15dcf327e44), C(154dd79fd2f984b4), C(f11171775622c1c3), - C(1fbe30982e78e6f0), C(a460a15dcf327e44), C(f359e0900cc3d582), - C(7e11070447976d00), C(324e6daf276ea4b5), C(7aa6e2df0cc94fa2), C(c56bbf69)}, {C(58d943503bb6748f), C(419c6c8e88ac70f6), C(586760cbf3d3d368), - C(b7e164979d5ccfc1), C(12cb4230d26bf286), C(f1bf910d44bd84cb), - C(b32c24c6a40272), C(b7e164979d5ccfc1), C(12cb4230d26bf286), - C(f1bf910d44bd84cb), C(b32c24c6a40272), C(11ed12e34c48c039), - C(b0c2538e51d0a6ac), C(4269bb773e1d553a), C(e35a9dbabd34867), C(abecfb9b)}, + C(abecfb9b)}, {C(dfff5989f5cfd9a1), C(bcee2e7ea3a96f83), C(681c7874adb29017), - C(3ff6c8ac7c36b63a), C(48bc8831d849e326), C(30b078e76b0214e2), - C(42954e6ad721b920), C(3ff6c8ac7c36b63a), C(48bc8831d849e326), - C(30b078e76b0214e2), C(42954e6ad721b920), C(f9aeb33d164b4472), - C(7b353b110831dbdc), C(16f64c82f44ae17b), C(b71244cc164b3b2b), C(8de13255)}, {C(7fb19eb1a496e8f5), C(d49e5dfdb5c0833f), C(c0d5d7b2f7c48dc7), - C(1a57313a32f22dde), C(30af46e49850bf8b), C(aa0fe8d12f808f83), - C(443e31d70873bb6b), C(1a57313a32f22dde), C(30af46e49850bf8b), - C(aa0fe8d12f808f83), C(443e31d70873bb6b), C(bbeb67c49c9fdc13), - C(18f1e2a88f59f9d5), C(fb1b05038e5def11), C(d0450b5ce4c39c52), C(a98ee299)}, {C(5dba5b0dadccdbaa), C(4ba8da8ded87fcdc), C(f693fdd25badf2f0), - C(e9029e6364286587), C(ae69f49ecb46726c), C(18e002679217c405), - C(bd6d66e85332ae9f), C(e9029e6364286587), C(ae69f49ecb46726c), - C(18e002679217c405), C(bd6d66e85332ae9f), C(6bf330b1c353dd2a), - C(74e9f2e71e3a4152), C(3f85560b50f6c413), C(d33a52a47eaed2b4), C(3015f556)}, {C(688bef4b135a6829), C(8d31d82abcd54e8e), C(f95f8a30d55036d7), - C(3d8c90e27aa2e147), C(2ec937ce0aa236b4), C(89b563996d3a0b78), - C(39b02413b23c3f08), C(3d8c90e27aa2e147), C(2ec937ce0aa236b4), - C(89b563996d3a0b78), C(39b02413b23c3f08), C(8d475a2e64faf2d2), - C(48567f7dca46ecaf), C(254cda08d5f87a6d), C(ec6ae9f729c47039), C(5a430e29)}, {C(d8323be05433a412), C(8d48fa2b2b76141d), C(3d346f23978336a5), - C(4d50c7537562033f), C(57dc7625b61dfe89), C(9723a9f4c08ad93a), - C(5309596f48ab456b), C(4d50c7537562033f), C(57dc7625b61dfe89), - C(9723a9f4c08ad93a), C(5309596f48ab456b), C(7e453088019d220f), - C(8776067ba6ab9714), C(67e1d06bd195de39), C(74a1a32f8994b918), C(2797add0)}, {C(3b5404278a55a7fc), C(23ca0b327c2d0a81), C(a6d65329571c892c), - C(45504801e0e6066b), C(86e6c6d6152a3d04), C(4f3db1c53eca2952), - C(d24d69b3e9ef10f3), C(45504801e0e6066b), C(86e6c6d6152a3d04), - C(4f3db1c53eca2952), C(d24d69b3e9ef10f3), C(93a0de2219e66a70), - C(8932c7115ccb1f8a), C(5ef503fdf2841a8c), C(38064dd9efa80a41), C(27d55016)}, {C(2a96a3f96c5e9bbc), C(8caf8566e212dda8), C(904de559ca16e45e), - C(f13bc2d9c2fe222e), C(be4ccec9a6cdccfd), C(37b2cbdd973a3ac9), - C(7b3223cd9c9497be), C(f13bc2d9c2fe222e), C(be4ccec9a6cdccfd), - C(37b2cbdd973a3ac9), C(7b3223cd9c9497be), C(d5904440f376f889), - C(62b13187699c473c), C(4751b89251f26726), C(9500d84fa3a61ba8), C(84945a82)}, {C(22bebfdcc26d18ff), C(4b4d8dcb10807ba1), C(40265eee30c6b896), - C(3752b423073b119a), C(377dc5eb7c662bdb), C(2b9f07f93a6c25b9), - C(96f24ede2bdc0718), C(3752b423073b119a), C(377dc5eb7c662bdb), - C(2b9f07f93a6c25b9), C(96f24ede2bdc0718), C(f7699b12c31417bd), - C(17b366f401c58b2), C(bf60188d5f437b37), C(484436e56df17f04), C(3ef7e224)}, + C(3ef7e224)}, {C(627a2249ec6bbcc2), C(c0578b462a46735a), C(4974b8ee1c2d4f1f), - C(ebdbb918eb6d837f), C(8fb5f218dd84147c), C(c77dd1f881df2c54), - C(62eac298ec226dc3), C(ebdbb918eb6d837f), C(8fb5f218dd84147c), - C(c77dd1f881df2c54), C(62eac298ec226dc3), C(43eded83c4b60bd0), - C(9a0a403b5487503b), C(25f305d9147f0bda), C(3ad417f511bc1e64), C(35ed8dc8)}, - {C(3abaf1667ba2f3e0), C(ee78476b5eeadc1), C(7e56ac0a6ca4f3f4), - C(f1b9b413df9d79ed), C(a7621b6fd02db503), C(d92f7ba9928a4ffe), - C(53f56babdcae96a6), C(f1b9b413df9d79ed), C(a7621b6fd02db503), - C(d92f7ba9928a4ffe), C(53f56babdcae96a6), C(5302b89fc48713ab), - C(d03e3b04dbe7a2f2), C(fa74ef8af6d376a7), C(103c8cdea1050ef2), - C(6a75e43d)}, + {C(3abaf1667ba2f3e0), C(ee78476b5eeadc1), C(7e56ac0a6ca4f3f4), C(6a75e43d)}, {C(3931ac68c5f1b2c9), C(efe3892363ab0fb0), C(40b707268337cd36), - C(a53a6b64b1ac85c9), C(d50e7f86ee1b832b), C(7bab08fdd26ba0a4), - C(7587743c18fe2475), C(a53a6b64b1ac85c9), C(d50e7f86ee1b832b), - C(7bab08fdd26ba0a4), C(7587743c18fe2475), C(e3b5d5d490cf5761), - C(dfc053f7d065edd5), C(42ffd8d5fb70129f), C(599ca38677cccdc3), C(235d9805)}, - {C(b98fb0606f416754), C(46a6e5547ba99c1e), C(c909d82112a8ed2), - C(dbfaae9642b3205a), C(f676a1339402bcb9), C(f4f12a5b1ac11f29), - C(7db8bad81249dee4), C(dbfaae9642b3205a), C(f676a1339402bcb9), - C(f4f12a5b1ac11f29), C(7db8bad81249dee4), C(b26e46f2da95922e), - C(2aaedd5e12e3c611), C(a0e2d9082966074), C(c64da8a167add63d), C(f7d69572)}, + {C(b98fb0606f416754), C(46a6e5547ba99c1e), C(c909d82112a8ed2), C(f7d69572)}, {C(7f7729a33e58fcc4), C(2e4bc1e7a023ead4), C(e707008ea7ca6222), - C(47418a71800334a0), C(d10395d8fc64d8a4), C(8257a30062cb66f), - C(6786f9b2dc1ff18a), C(47418a71800334a0), C(d10395d8fc64d8a4), - C(8257a30062cb66f), C(6786f9b2dc1ff18a), C(5633f437bb2f180f), - C(e5a3a405737d22d6), C(ca0ff1ef6f7f0b74), C(d0ae600684b16df8), C(bacd0199)}, {C(42a0aa9ce82848b3), C(57232730e6bee175), C(f89bb3f370782031), - C(caa33cf9b4f6619c), C(b2c8648ad49c209f), C(9e89ece0712db1c0), - C(101d8274a711a54b), C(caa33cf9b4f6619c), C(b2c8648ad49c209f), - C(9e89ece0712db1c0), C(101d8274a711a54b), C(538e79f1e70135cd), - C(e1f5a76f983c844e), C(653c082fd66088fc), C(1b9c9b464b654958), C(e428f50e)}, {C(6b2c6d38408a4889), C(de3ef6f68fb25885), C(20754f456c203361), - C(941f5023c0c943f9), C(dfdeb9564fd66f24), C(2140cec706b9d406), - C(7b22429b131e9c72), C(941f5023c0c943f9), C(dfdeb9564fd66f24), - C(2140cec706b9d406), C(7b22429b131e9c72), C(94215c22eb940f45), - C(d28b9ed474f7249a), C(6f25e88f2fbf9f56), C(b6718f9e605b38ac), C(81eaaad3)}, {C(930380a3741e862a), C(348d28638dc71658), C(89dedcfd1654ea0d), - C(7e7f61684080106), C(837ace9794582976), C(5ac8ca76a357eb1b), - C(32b58308625661fb), C(7e7f61684080106), C(837ace9794582976), - C(5ac8ca76a357eb1b), C(32b58308625661fb), C(c09705c4572025d9), - C(f9187f6af0291303), C(1c0edd8ee4b02538), C(e6cb105daa0578a), C(addbd3e3)}, + C(addbd3e3)}, {C(94808b5d2aa25f9a), C(cec72968128195e0), C(d9f4da2bdc1e130f), - C(272d8dd74f3006cc), C(ec6c2ad1ec03f554), C(4ad276b249a5d5dd), - C(549a22a17c0cde12), C(272d8dd74f3006cc), C(ec6c2ad1ec03f554), - C(4ad276b249a5d5dd), C(549a22a17c0cde12), C(602119cb824d7cde), - C(f4d3cef240ef35fa), C(e889895e01911bc7), C(785a7e5ac20e852b), C(e66dbca0)}, {C(b31abb08ae6e3d38), C(9eb9a95cbd9e8223), C(8019e79b7ee94ea9), - C(7b2271a7a3248e22), C(3b4f700e5a0ba523), C(8ebc520c227206fe), - C(da3f861490f5d291), C(7b2271a7a3248e22), C(3b4f700e5a0ba523), - C(8ebc520c227206fe), C(da3f861490f5d291), C(d08a689f9f3aa60e), - C(547c1b97a068661f), C(4b15a67fa29172f0), C(eaf40c085191d80f), C(afe11fd5)}, {C(dccb5534a893ea1a), C(ce71c398708c6131), C(fe2396315457c164), - C(3f1229f4d0fd96fb), C(33130aa5fa9d43f2), C(e42693d5b34e63ab), - C(2f4ef2be67f62104), C(3f1229f4d0fd96fb), C(33130aa5fa9d43f2), - C(e42693d5b34e63ab), C(2f4ef2be67f62104), C(372e5153516e37b9), - C(af9ec142ab12cc86), C(777920c09345e359), C(e7c4a383bef8adc6), C(a71a406f)}, {C(6369163565814de6), C(8feb86fb38d08c2f), C(4976933485cc9a20), - C(7d3e82d5ba29a90d), C(d5983cc93a9d126a), C(37e9dfd950e7b692), - C(80673be6a7888b87), C(7d3e82d5ba29a90d), C(d5983cc93a9d126a), - C(37e9dfd950e7b692), C(80673be6a7888b87), C(57f732dc600808bc), - C(59477199802cc78b), C(f824810eb8f2c2de), C(c4a3437f05b3b61c), C(9d90eaf5)}, {C(edee4ff253d9f9b3), C(96ef76fb279ef0ad), C(a4d204d179db2460), - C(1f3dcdfa513512d6), C(4dc7ec07283117e4), C(4438bae88ae28bf9), - C(aa7eae72c9244a0d), C(1f3dcdfa513512d6), C(4dc7ec07283117e4), - C(4438bae88ae28bf9), C(aa7eae72c9244a0d), C(b9aedc8d3ecc72df), - C(b75a8eb090a77d62), C(6b15677f9cd91507), C(51d8282cb3a9ddbf), C(6665db10)}, {C(941993df6e633214), C(929bc1beca5b72c6), C(141fc52b8d55572d), - C(b3b782ad308f21ed), C(4f2676485041dee0), C(bfe279aed5cb4bc8), - C(2a62508a467a22ff), C(b3b782ad308f21ed), C(4f2676485041dee0), - C(bfe279aed5cb4bc8), C(2a62508a467a22ff), C(e74d29eab742385d), - C(56b05cd90ecfc293), C(c603728ea73f8844), C(8638fcd21bc692c4), C(9c977cbf)}, {C(859838293f64cd4c), C(484403b39d44ad79), C(bf674e64d64b9339), - C(44d68afda9568f08), C(478568ed51ca1d65), C(679c204ad3d9e766), - C(b28e788878488dc1), C(44d68afda9568f08), C(478568ed51ca1d65), - C(679c204ad3d9e766), C(b28e788878488dc1), C(d001a84d3a84fae6), - C(d376958fe4cb913e), C(17435277e36c86f0), C(23657b263c347aa6), C(ee83ddd4)}, - {C(c19b5648e0d9f555), C(328e47b2b7562993), C(e756b92ba4bd6a51), - C(c3314e362764ddb8), C(6481c084ee9ec6b5), C(ede23fb9a251771), - C(bd617f2643324590), C(c3314e362764ddb8), C(6481c084ee9ec6b5), - C(ede23fb9a251771), C(bd617f2643324590), C(d2d30c9b95e030f5), - C(8a517312ffc5795e), C(8b1f325033bd535e), C(3ee6e867e03f2892), C(26519cc)}, + {C(c19b5648e0d9f555), C(328e47b2b7562993), C(e756b92ba4bd6a51), C(26519cc)}, {C(f963b63b9006c248), C(9e9bf727ffaa00bc), C(c73bacc75b917e3a), - C(2c6aa706129cc54c), C(17a706f59a49f086), C(c7c1eec455217145), - C(6adfdc6e07602d42), C(2c6aa706129cc54c), C(17a706f59a49f086), - C(c7c1eec455217145), C(6adfdc6e07602d42), C(fb75fca30d848dd2), - C(5228c9ed14653ed4), C(953958910153b1a2), C(a430103a24f42a5d), C(a485a53f)}, {C(6a8aa0852a8c1f3b), C(c8f1e5e206a21016), C(2aa554aed1ebb524), - C(fc3e3c322cd5d89b), C(b7e3911dc2bd4ebb), C(fcd6da5e5fae833a), - C(51ed3c41f87f9118), C(fc3e3c322cd5d89b), C(b7e3911dc2bd4ebb), - C(fcd6da5e5fae833a), C(51ed3c41f87f9118), C(f31750cbc19c420a), - C(186dab1abada1d86), C(ca7f88cb894b3cd7), C(2859eeb1c373790c), C(f62bc412)}, {C(740428b4d45e5fb8), C(4c95a4ce922cb0a5), C(e99c3ba78feae796), - C(914f1ea2fdcebf5c), C(9566453c07cd0601), C(9841bf66d0462cd), - C(79140c1c18536aeb), C(914f1ea2fdcebf5c), C(9566453c07cd0601), - C(9841bf66d0462cd), C(79140c1c18536aeb), C(a963b930b05820c2), - C(6a7d9fa0c8c45153), C(64214c40d07cf39b), C(7057daf1d806c014), C(8975a436)}, - {C(658b883b3a872b86), C(2f0e303f0f64827a), C(975337e23dc45e1), - C(99468a917986162b), C(7b31434aac6e0af0), C(f6915c1562c7d82f), - C(e4071d82a6dd71db), C(99468a917986162b), C(7b31434aac6e0af0), - C(f6915c1562c7d82f), C(e4071d82a6dd71db), C(5f5331f077b5d996), - C(7b314ba21b747a4f), C(5a73cb9521da17f5), C(12ed435fae286d86), - C(94ff7f41)}, - {C(6df0a977da5d27d4), C(891dd0e7cb19508), C(fd65434a0b71e680), - C(8799e4740e573c50), C(9e739b52d0f341e8), C(cdfd34ba7d7b03eb), - C(5061812ce6c88499), C(8799e4740e573c50), C(9e739b52d0f341e8), - C(cdfd34ba7d7b03eb), C(5061812ce6c88499), C(612b8d8f2411dc5c), - C(878bd883d29c7787), C(47a846727182bb), C(ec4949508c8b3b9a), C(760aa031)}, - {C(a900275464ae07ef), C(11f2cfda34beb4a3), C(9abf91e5a1c38e4), - C(8063d80ab26f3d6d), C(4177b4b9b4f0393f), C(6de42ba8672b9640), - C(d0bccdb72c51c18), C(8063d80ab26f3d6d), C(4177b4b9b4f0393f), - C(6de42ba8672b9640), C(d0bccdb72c51c18), C(af3f611b7f22cf12), - C(3863c41492645755), C(928c7a616a8f14f9), C(a82c78eb2eadc58b), - C(3bda76df)}, + {C(658b883b3a872b86), C(2f0e303f0f64827a), C(975337e23dc45e1), C(94ff7f41)}, + {C(6df0a977da5d27d4), C(891dd0e7cb19508), C(fd65434a0b71e680), C(760aa031)}, + {C(a900275464ae07ef), C(11f2cfda34beb4a3), C(9abf91e5a1c38e4), C(3bda76df)}, {C(810bc8aa0c40bcb0), C(448a019568d01441), C(f60ec52f60d3aeae), - C(52c44837aa6dfc77), C(15d8d8fccdd6dc5b), C(345b793ccfa93055), - C(932160fe802ca975), C(52c44837aa6dfc77), C(15d8d8fccdd6dc5b), - C(345b793ccfa93055), C(932160fe802ca975), C(a624b0dd93fc18cd), - C(d955b254c2037f1e), C(e540533d370a664c), C(2ba4ec12514e9d7), C(498e2e65)}, + C(498e2e65)}, {C(22036327deb59ed7), C(adc05ceb97026a02), C(48bff0654262672b), - C(c791b313aba3f258), C(443c7757a4727bee), C(e30e4b2372171bdf), - C(f3db986c4156f3cb), C(c791b313aba3f258), C(443c7757a4727bee), - C(e30e4b2372171bdf), C(f3db986c4156f3cb), C(a939aefab97c6e15), - C(dbeb8acf1d5b0e6c), C(1e0eab667a795bba), C(80dd539902df4d50), C(d38deb48)}, {C(7d14dfa9772b00c8), C(595735efc7eeaed7), C(29872854f94c3507), - C(bc241579d8348401), C(16dc832804d728f0), C(e9cc71ae64e3f09e), - C(bef634bc978bac31), C(bc241579d8348401), C(16dc832804d728f0), - C(e9cc71ae64e3f09e), C(bef634bc978bac31), C(7f64b1fa2a9129e), - C(71d831bd530ac7f3), C(c7ad0a8a6d5be6f1), C(82a7d3a815c7aaab), C(82b3fb6b)}, {C(2d777cddb912675d), C(278d7b10722a13f9), C(f5c02bfb7cc078af), - C(4283001239888836), C(f44ca39a6f79db89), C(ed186122d71bcc9f), - C(8620017ab5f3ba3b), C(4283001239888836), C(f44ca39a6f79db89), - C(ed186122d71bcc9f), C(8620017ab5f3ba3b), C(e787472187f176c), - C(267e64c4728cf181), C(f1ba4b3007c15e30), C(8e3a75d5b02ecfc0), C(e500e25f)}, {C(f2ec98824e8aa613), C(5eb7e3fb53fe3bed), C(12c22860466e1dd4), - C(374dd4288e0b72e5), C(ff8916db706c0df4), C(cb1a9e85de5e4b8d), - C(d4d12afb67a27659), C(374dd4288e0b72e5), C(ff8916db706c0df4), - C(cb1a9e85de5e4b8d), C(d4d12afb67a27659), C(feb69095d1ba175a), - C(e2003aab23a47fad), C(8163a3ecab894b49), C(46d356674ce041f6), C(bd2bb07c)}, {C(5e763988e21f487f), C(24189de8065d8dc5), C(d1519d2403b62aa0), - C(9136456740119815), C(4d8ff7733b27eb83), C(ea3040bc0c717ef8), - C(7617ab400dfadbc), C(9136456740119815), C(4d8ff7733b27eb83), - C(ea3040bc0c717ef8), C(7617ab400dfadbc), C(fb336770c10b17a1), - C(6123b68b5b31f151), C(1e147d5f295eccf2), C(9ecbb1333556f977), C(3a2b431d)}, {C(48949dc327bb96ad), C(e1fd21636c5c50b4), C(3f6eb7f13a8712b4), - C(14cf7f02dab0eee8), C(6d01750605e89445), C(4f1cf4006e613b78), - C(57c40c4db32bec3b), C(14cf7f02dab0eee8), C(6d01750605e89445), - C(4f1cf4006e613b78), C(57c40c4db32bec3b), C(1fde5a347f4a326e), - C(cb5a54308adb0e3f), C(14994b2ba447a23c), C(7067d0abb4257b68), C(7322a83d)}, {C(b7c4209fb24a85c5), C(b35feb319c79ce10), C(f0d3de191833b922), - C(570d62758ddf6397), C(5e0204fb68a7b800), C(4383a9236f8b5a2b), - C(7bc1a64641d803a4), C(570d62758ddf6397), C(5e0204fb68a7b800), - C(4383a9236f8b5a2b), C(7bc1a64641d803a4), C(5434d61285099f7a), - C(d49449aacdd5dd67), C(97855ba0e9a7d75d), C(da67328062f3a62f), C(a645ca1c)}, {C(9c9e5be0943d4b05), C(b73dc69e45201cbb), C(aab17180bfe5083d), - C(c738a77a9a55f0e2), C(705221addedd81df), C(fd9bd8d397abcfa3), - C(8ccf0004aa86b795), C(c738a77a9a55f0e2), C(705221addedd81df), - C(fd9bd8d397abcfa3), C(8ccf0004aa86b795), C(2bb5db2280068206), - C(8c22d29f307a01d), C(274a22de02f473c8), C(b8791870f4268182), C(8909a45a)}, + C(8909a45a)}, {C(3898bca4dfd6638d), C(f911ff35efef0167), C(24bdf69e5091fc88), - C(9b82567ab6560796), C(891b69462b41c224), C(8eccc7e4f3af3b51), - C(381e54c3c8f1c7d0), C(9b82567ab6560796), C(891b69462b41c224), - C(8eccc7e4f3af3b51), C(381e54c3c8f1c7d0), C(c80fbc489a558a55), - C(1ba88e062a663af7), C(af7b1ef1c0116303), C(bd20e1a5a6b1a0cd), C(bd30074c)}, - {C(5b5d2557400e68e7), C(98d610033574cee), C(dfd08772ce385deb), - C(3c13e894365dc6c2), C(26fc7bbcda3f0ef), C(dbb71106cdbfea36), - C(785239a742c6d26d), C(3c13e894365dc6c2), C(26fc7bbcda3f0ef), - C(dbb71106cdbfea36), C(785239a742c6d26d), C(f810c415ae05b2f4), - C(bb9b9e7398526088), C(70128f1bf830a32b), C(bcc73f82b6410899), - C(c17cf001)}, + {C(5b5d2557400e68e7), C(98d610033574cee), C(dfd08772ce385deb), C(c17cf001)}, {C(a927ed8b2bf09bb6), C(606e52f10ae94eca), C(71c2203feb35a9ee), - C(6e65ec14a8fb565), C(34bff6f2ee5a7f79), C(2e329a5be2c011b), - C(73161c93331b14f9), C(6e65ec14a8fb565), C(34bff6f2ee5a7f79), - C(2e329a5be2c011b), C(73161c93331b14f9), C(15d13f2408aecf88), - C(9f5b61b8a4b55b31), C(8fe25a43b296dba6), C(bdad03b7300f284e), C(26ffd25a)}, {C(8d25746414aedf28), C(34b1629d28b33d3a), C(4d5394aea5f82d7b), - C(379f76458a3c8957), C(79dd080f9843af77), C(c46f0a7847f60c1d), - C(af1579c5797703cc), C(379f76458a3c8957), C(79dd080f9843af77), - C(c46f0a7847f60c1d), C(af1579c5797703cc), C(8b7d31f338755c14), - C(2eff97679512aaa8), C(df07d68e075179ed), C(c8fa6c7a729e7f1f), C(f1d8ce3c)}, {C(b5bbdb73458712f2), C(1ff887b3c2a35137), C(7f7231f702d0ace9), - C(1e6f0910c3d25bd8), C(ad9e250862102467), C(1c842a07abab30cd), - C(cd8124176bac01ac), C(1e6f0910c3d25bd8), C(ad9e250862102467), - C(1c842a07abab30cd), C(cd8124176bac01ac), C(ea6ebe7a79b67edc), - C(73f598ac9db26713), C(4f4e72d7460b8fc), C(365dc4b9fdf13f21), C(3ee8fb17)}, + C(3ee8fb17)}, {C(3d32a26e3ab9d254), C(fc4070574dc30d3a), C(f02629579c2b27c9), - C(b1cf09b0184a4834), C(5c03db48eb6cc159), C(f18c7fcf34d1df47), - C(dfb043419ecf1fa9), C(b1cf09b0184a4834), C(5c03db48eb6cc159), - C(f18c7fcf34d1df47), C(dfb043419ecf1fa9), C(dcd78d13f9ca658f), - C(4355d408ffe8e49f), C(81eefee908b593b4), C(590c213c20e981a3), C(a77acc2a)}, - {C(9371d3c35fa5e9a5), C(42967cf4d01f30), C(652d1eeae704145c), - C(ceaf1a0d15234f15), C(1450a54e45ba9b9), C(65e9c1fd885aa932), - C(354d4bc034ba8cbe), C(ceaf1a0d15234f15), C(1450a54e45ba9b9), - C(65e9c1fd885aa932), C(354d4bc034ba8cbe), C(8fd4ff484c08fb4b), - C(bf46749866f69ba0), C(cf1c21ede82c9477), C(4217548c43da109), C(f4556dee)}, - {C(cbaa3cb8f64f54e0), C(76c3b48ee5c08417), C(9f7d24e87e61ce9), - C(85b8e53f22e19507), C(bb57137739ca486b), C(c77f131cca38f761), - C(c56ac3cf275be121), C(85b8e53f22e19507), C(bb57137739ca486b), - C(c77f131cca38f761), C(c56ac3cf275be121), C(9ec1a6c9109d2685), - C(3dad0922e76afdb0), C(fd58cbf952958103), C(7b04c908e78639a1), - C(de287a64)}, + {C(9371d3c35fa5e9a5), C(42967cf4d01f30), C(652d1eeae704145c), C(f4556dee)}, + {C(cbaa3cb8f64f54e0), C(76c3b48ee5c08417), C(9f7d24e87e61ce9), C(de287a64)}, {C(b2e23e8116c2ba9f), C(7e4d9c0060101151), C(3310da5e5028f367), - C(adc52dddb76f6e5e), C(4aad4e925a962b68), C(204b79b7f7168e64), - C(df29ed6671c36952), C(adc52dddb76f6e5e), C(4aad4e925a962b68), - C(204b79b7f7168e64), C(df29ed6671c36952), C(e02927cac396d210), - C(5d500e71742b638a), C(5c9998af7f27b124), C(3fba9a2573dc2f7), C(878e55b9)}, - {C(8aa77f52d7868eb9), C(4d55bd587584e6e2), C(d2db37041f495f5), - C(ce030d15b5fe2f4), C(86b4a7a0780c2431), C(ee070a9ae5b51db7), - C(edc293d9595be5d8), C(ce030d15b5fe2f4), C(86b4a7a0780c2431), - C(ee070a9ae5b51db7), C(edc293d9595be5d8), C(3dfc5ec108260a2b), - C(8afe28c7123bf4e2), C(da82ef38023a7a5f), C(3e1f77b0174b77c3), C(7648486)}, + C(878e55b9)}, + {C(8aa77f52d7868eb9), C(4d55bd587584e6e2), C(d2db37041f495f5), C(7648486)}, {C(858fea922c7fe0c3), C(cfe8326bf733bc6f), C(4e5e2018cf8f7dfc), - C(64fd1bc011e5bab7), C(5c9e858728015568), C(97ac42c2b00b29b1), - C(7f89caf08c109aee), C(64fd1bc011e5bab7), C(5c9e858728015568), - C(97ac42c2b00b29b1), C(7f89caf08c109aee), C(9a8af34fd0e9dacf), - C(bbc54161aa1507e0), C(7cda723ccbbfe5ee), C(2c289d839fb93f58), C(57ac0fb1)}, {C(46ef25fdec8392b1), C(e48d7b6d42a5cd35), C(56a6fe1c175299ca), - C(fdfa836b41dcef62), C(2f8db8030e847e1b), C(5ba0a49ac4f9b0f8), - C(dae897ed3e3fce44), C(fdfa836b41dcef62), C(2f8db8030e847e1b), - C(5ba0a49ac4f9b0f8), C(dae897ed3e3fce44), C(9c432e31aef626e7), - C(9a36e1c6cd6e3dd), C(5095a167c34d19d), C(a70005cfa6babbea), C(d01967ca)}, + C(d01967ca)}, {C(8d078f726b2df464), C(b50ee71cdcabb299), C(f4af300106f9c7ba), - C(7d222caae025158a), C(cc028d5fd40241b9), C(dd42515b639e6f97), - C(e08e86531a58f87f), C(7d222caae025158a), C(cc028d5fd40241b9), - C(dd42515b639e6f97), C(e08e86531a58f87f), C(d93612c835b37d7b), - C(91dd61729b2fa7f4), C(ba765a1bdda09db7), C(55258b451b2b1297), C(96ecdf74)}, {C(35ea86e6960ca950), C(34fe1fe234fc5c76), C(a00207a3dc2a72b7), - C(80395e48739e1a67), C(74a67d8f7f43c3d7), C(dd2bdd1d62246c6e), - C(a1f44298ba80acf6), C(80395e48739e1a67), C(74a67d8f7f43c3d7), - C(dd2bdd1d62246c6e), C(a1f44298ba80acf6), C(ad86d86c187bf38), - C(26feea1f2eee240d), C(ed7f1fd066b23897), C(a768cf1e0fbb502), C(779f5506)}, + C(779f5506)}, {C(8aee9edbc15dd011), C(51f5839dc8462695), C(b2213e17c37dca2d), - C(133b299a939745c5), C(796e2aac053f52b3), C(e8d9fe1521a4a222), - C(819a8863e5d1c290), C(133b299a939745c5), C(796e2aac053f52b3), - C(e8d9fe1521a4a222), C(819a8863e5d1c290), C(c0737f0fe34d36ad), - C(e6d6d4a267a5cc31), C(98300a7911674c23), C(bef189661c257098), C(3c94c2de)}, {C(c3e142ba98432dda), C(911d060cab126188), C(b753fbfa8365b844), - C(fd1a9ba5e71b08a2), C(7ac0dc2ed7778533), C(b543161ff177188a), - C(492fc08a6186f3f4), C(fd1a9ba5e71b08a2), C(7ac0dc2ed7778533), - C(b543161ff177188a), C(492fc08a6186f3f4), C(fc4745f516afd3b6), - C(88c30370a53080e), C(65a1bb34abc465e2), C(abbd14662911c8b3), C(39f98faf)}, + C(39f98faf)}, {C(123ba6b99c8cd8db), C(448e582672ee07c4), C(cebe379292db9e65), - C(938f5bbab544d3d6), C(d2a95f9f2d376d73), C(68b2f16149e81aa3), - C(ad7e32f82d86c79d), C(938f5bbab544d3d6), C(d2a95f9f2d376d73), - C(68b2f16149e81aa3), C(ad7e32f82d86c79d), C(4574015ae8626ce2), - C(455aa6137386a582), C(658ad2542e8ec20), C(e31d7be2ca35d00), C(7af31199)}, + C(7af31199)}, {C(ba87acef79d14f53), C(b3e0fcae63a11558), C(d5ac313a593a9f45), - C(eea5f5a9f74af591), C(578710bcc36fbea2), C(7a8393432188931d), - C(705cfc5ec7cc172), C(eea5f5a9f74af591), C(578710bcc36fbea2), - C(7a8393432188931d), C(705cfc5ec7cc172), C(da85ebe5fc427976), - C(bfa5c7a454df54c8), C(4632b72a81bf66d2), C(5dd72877db539ee2), C(e341a9d6)}, - {C(bcd3957d5717dc3), C(2da746741b03a007), C(873816f4b1ece472), - C(2b826f1a2c08c289), C(da50f56863b55e74), C(b18712f6b3eed83b), - C(bdc7cc05ab4c685f), C(2b826f1a2c08c289), C(da50f56863b55e74), - C(b18712f6b3eed83b), C(bdc7cc05ab4c685f), C(9e45fb833d1b0af), - C(d7213081db29d82e), C(d2a6b6c6a09ed55e), C(98a7686cba323ca9), - C(ca24aeeb)}, + {C(bcd3957d5717dc3), C(2da746741b03a007), C(873816f4b1ece472), C(ca24aeeb)}, {C(61442ff55609168e), C(6447c5fc76e8c9cf), C(6a846de83ae15728), - C(effc2663cffc777f), C(93214f8f463afbed), C(a156ef06066f4e4e), - C(a407b6ed8769d51e), C(effc2663cffc777f), C(93214f8f463afbed), - C(a156ef06066f4e4e), C(a407b6ed8769d51e), C(bb2f9ed29745c02a), - C(981eecd435b36ad9), C(461a5a05fb9cdff4), C(bd6cb2a87b9f910c), C(b2252b57)}, - {C(dbe4b1b2d174757f), C(506512da18712656), C(6857f3e0b8dd95f), - C(5a4fc2728a9bb671), C(ebb971522ec38759), C(1a5a093e6cf1f72b), - C(729b057fe784f504), C(5a4fc2728a9bb671), C(ebb971522ec38759), - C(1a5a093e6cf1f72b), C(729b057fe784f504), C(71fcbf42a767f9cf), - C(114cfe772da6cdd), C(60cdf9cb629d9d7a), C(e270d10ad088b24e), C(72c81da1)}, + {C(dbe4b1b2d174757f), C(506512da18712656), C(6857f3e0b8dd95f), C(72c81da1)}, {C(531e8e77b363161c), C(eece0b43e2dae030), C(8294b82c78f34ed1), - C(e777b1fd580582f2), C(7b880f58da112699), C(562c6b189a6333f4), - C(139d64f88a611d4), C(e777b1fd580582f2), C(7b880f58da112699), - C(562c6b189a6333f4), C(139d64f88a611d4), C(53d8ef17eda64fa4), - C(bf3eded14dc60a04), C(2b5c559cf5ec07c5), C(8895f7339d03a48a), C(6b9fce95)}, {C(f71e9c926d711e2b), C(d77af2853a4ceaa1), C(9aa0d6d76a36fae7), - C(dd16cd0fbc08393), C(29a414a5d8c58962), C(72793d8d1022b5b2), - C(2e8e69cf7cbffdf0), C(dd16cd0fbc08393), C(29a414a5d8c58962), - C(72793d8d1022b5b2), C(2e8e69cf7cbffdf0), C(3721c0473aa99c9a), - C(1cff4ed9c31cd91c), C(4990735033cc482b), C(7fdf8c701c72f577), C(19399857)}, {C(cb20ac28f52df368), C(e6705ee7880996de), C(9b665cc3ec6972f2), - C(4260e8c254e9924b), C(f197a6eb4591572d), C(8e867ff0fb7ab27c), - C(f95502fb503efaf3), C(4260e8c254e9924b), C(f197a6eb4591572d), - C(8e867ff0fb7ab27c), C(f95502fb503efaf3), C(30c41876b08e3e22), - C(958e2419e3cd22f4), C(f0f3aa1fe119a107), C(481662310a379100), C(3c57a994)}, {C(e4a794b4acb94b55), C(89795358057b661b), C(9c4cdcec176d7a70), - C(4890a83ee435bc8b), C(d8c1c00fceb00914), C(9e7111ba234f900f), - C(eb8dbab364d8b604), C(4890a83ee435bc8b), C(d8c1c00fceb00914), - C(9e7111ba234f900f), C(eb8dbab364d8b604), C(b3261452963eebb), - C(6cf94b02792c4f95), C(d88fa815ef1e8fc), C(2d687af66604c73), C(c053e729)}, + C(c053e729)}, {C(cb942e91443e7208), C(e335de8125567c2a), C(d4d74d268b86df1f), - C(8ba0fdd2ffc8b239), C(f413b366c1ffe02f), C(c05b2717c59a8a28), - C(981188eab4fcc8fb), C(8ba0fdd2ffc8b239), C(f413b366c1ffe02f), - C(c05b2717c59a8a28), C(981188eab4fcc8fb), C(e563f49a1d9072ba), - C(3c6a3aa4a26367dc), C(ba0db13448653f34), C(31065d756074d7d6), C(51cbbba7)}, {C(ecca7563c203f7ba), C(177ae2423ef34bb2), C(f60b7243400c5731), - C(cf1edbfe7330e94e), C(881945906bcb3cc6), C(4acf0293244855da), - C(65ae042c1c2a28c2), C(cf1edbfe7330e94e), C(881945906bcb3cc6), - C(4acf0293244855da), C(65ae042c1c2a28c2), C(b25fa0a1cab33559), - C(d98e8daa28124131), C(fce17f50b9c351b3), C(3f995ccf7386864b), C(1acde79a)}, {C(1652cb940177c8b5), C(8c4fe7d85d2a6d6d), C(f6216ad097e54e72), - C(f6521b912b368ae6), C(a9fe4eff81d03e73), C(d6f623629f80d1a3), - C(2b9604f32cb7dc34), C(f6521b912b368ae6), C(a9fe4eff81d03e73), - C(d6f623629f80d1a3), C(2b9604f32cb7dc34), C(2a43d84dcf59c7e2), - C(d0a197c70c5dae0b), C(6e84d4bbc71d76a0), C(c7e94620378c6cb2), C(2d160d13)}, {C(31fed0fc04c13ce8), C(3d5d03dbf7ff240a), C(727c5c9b51581203), - C(6b5ffc1f54fecb29), C(a8e8e7ad5b9a21d9), C(c4d5a32cd6aac22d), - C(d7e274ad22d4a79a), C(6b5ffc1f54fecb29), C(a8e8e7ad5b9a21d9), - C(c4d5a32cd6aac22d), C(d7e274ad22d4a79a), C(368841ea5731a112), - C(feaf7bc2e73ca48f), C(636fb272e9ea1f6), C(5d9cb7580c3f6207), C(787f5801)}, + C(787f5801)}, {C(e7b668947590b9b3), C(baa41ad32938d3fa), C(abcbc8d4ca4b39e4), - C(381ee1b7ea534f4e), C(da3759828e3de429), C(3e015d76729f9955), - C(cbbec51a6485fbde), C(381ee1b7ea534f4e), C(da3759828e3de429), - C(3e015d76729f9955), C(cbbec51a6485fbde), C(9b86605281f20727), - C(fc6fcf508676982a), C(3b135f7a813a1040), C(d3a4706bea1db9c9), C(c9629828)}, {C(1de2119923e8ef3c), C(6ab27c096cf2fe14), C(8c3658edca958891), - C(4cc8ed3ada5f0f2), C(4a496b77c1f1c04e), C(9085b0a862084201), - C(a1894bde9e3dee21), C(4cc8ed3ada5f0f2), C(4a496b77c1f1c04e), - C(9085b0a862084201), C(a1894bde9e3dee21), C(367fb472dc5b277d), - C(7d39ccca16fc6745), C(763f988d70db9106), C(a8b66f7fecb70f02), C(be139231)}, {C(1269df1e69e14fa7), C(992f9d58ac5041b7), C(e97fcf695a7cbbb4), - C(e5d0549802d15008), C(424c134ecd0db834), C(6fc44fd91be15c6c), - C(a1a5ef95d50e537d), C(e5d0549802d15008), C(424c134ecd0db834), - C(6fc44fd91be15c6c), C(a1a5ef95d50e537d), C(d1e3daf5d05f5308), - C(4c7f81600eaa1327), C(109d1b8d1f9d0d2b), C(871e8699e0aeb862), C(7df699ef)}, {C(820826d7aba567ff), C(1f73d28e036a52f3), C(41c4c5a73f3b0893), - C(aa0d74d4a98db89b), C(36fd486d07c56e1d), C(d0ad23cbb6660d8a), - C(1264a84665b35e19), C(aa0d74d4a98db89b), C(36fd486d07c56e1d), - C(d0ad23cbb6660d8a), C(1264a84665b35e19), C(789682bf7d781b33), - C(6bfa6abd2fb5722d), C(6779cb3623d33900), C(435ca5214e1ee5f0), C(8ce6b96d)}, {C(ffe0547e4923cef9), C(3534ed49b9da5b02), C(548a273700fba03d), - C(28ac84ca70958f7e), C(d8ae575a68faa731), C(2aaaee9b9dcffd4c), - C(6c7faab5c285c6da), C(28ac84ca70958f7e), C(d8ae575a68faa731), - C(2aaaee9b9dcffd4c), C(6c7faab5c285c6da), C(45d94235f99ba78f), - C(ab5ea16f39497f5b), C(fb4d6c86fccbdca3), C(8104e6310a5fd2c7), C(6f9ed99c)}, {C(72da8d1b11d8bc8b), C(ba94b56b91b681c6), C(4e8cc51bd9b0fc8c), - C(43505ed133be672a), C(e8f2f9d973c2774e), C(677b9b9c7cad6d97), - C(4e1f5d56ef17b906), C(43505ed133be672a), C(e8f2f9d973c2774e), - C(677b9b9c7cad6d97), C(4e1f5d56ef17b906), C(eea3a6038f983767), - C(87109f077f86db01), C(ecc1ca41f74d61cc), C(34a87e86e83bed17), C(e0244796)}, - {C(d62ab4e3f88fc797), C(ea86c7aeb6283ae4), C(b5b93e09a7fe465), - C(4344a1a0134afe2), C(ff5c17f02b62341d), C(3214c6a587ce4644), - C(a905e7ed0629d05c), C(4344a1a0134afe2), C(ff5c17f02b62341d), - C(3214c6a587ce4644), C(a905e7ed0629d05c), C(b5c72690cd716e82), - C(7c6097649e6ebe7b), C(7ceee8c6e56a4dcd), C(80ca849dc53eb9e4), - C(4ccf7e75)}, + {C(d62ab4e3f88fc797), C(ea86c7aeb6283ae4), C(b5b93e09a7fe465), C(4ccf7e75)}, {C(d0f06c28c7b36823), C(1008cb0874de4bb8), C(d6c7ff816c7a737b), - C(489b697fe30aa65f), C(4da0fb621fdc7817), C(dc43583b82c58107), - C(4b0261debdec3cd6), C(489b697fe30aa65f), C(4da0fb621fdc7817), - C(dc43583b82c58107), C(4b0261debdec3cd6), C(a9748d7b6c0e016c), - C(7e8828f7ba4b034b), C(da0fa54348a2512a), C(ebf9745c0962f9ad), C(915cef86)}, {C(99b7042460d72ec6), C(2a53e5e2b8e795c2), C(53a78132d9e1b3e3), - C(c043e67e6fc64118), C(ff0abfe926d844d3), C(f2a9fe5db2e910fe), - C(ce352cdc84a964dd), C(c043e67e6fc64118), C(ff0abfe926d844d3), - C(f2a9fe5db2e910fe), C(ce352cdc84a964dd), C(b89bc028aa5e6063), - C(a354e7fdac04459c), C(68d6547e6e980189), C(c968dddfd573773e), C(5cb59482)}, - {C(4f4dfcfc0ec2bae5), C(841233148268a1b8), C(9248a76ab8be0d3), - C(334c5a25b5903a8c), C(4c94fef443122128), C(743e7d8454655c40), - C(1ab1e6d1452ae2cd), C(334c5a25b5903a8c), C(4c94fef443122128), - C(743e7d8454655c40), C(1ab1e6d1452ae2cd), C(fec766de4a8e476c), - C(cc0929da9567e71b), C(5f9ef5b5f150c35a), C(87659cabd649768f), - C(6ca3f532)}, + {C(4f4dfcfc0ec2bae5), C(841233148268a1b8), C(9248a76ab8be0d3), C(6ca3f532)}, {C(fe86bf9d4422b9ae), C(ebce89c90641ef9c), C(1c84e2292c0b5659), - C(8bde625a10a8c50d), C(eb8271ded1f79a0b), C(14dc6844f0de7a3c), - C(f85b2f9541e7e6da), C(8bde625a10a8c50d), C(eb8271ded1f79a0b), - C(14dc6844f0de7a3c), C(f85b2f9541e7e6da), C(2fe22cfd1683b961), - C(ea1d75c5b7aa01ca), C(9eef60a44876bb95), C(950c818e505c6f7f), C(e24f3859)}, {C(a90d81060932dbb0), C(8acfaa88c5fbe92b), C(7c6f3447e90f7f3f), - C(dd52fc14c8dd3143), C(1bc7508516e40628), C(3059730266ade626), - C(ffa526822f391c2), C(dd52fc14c8dd3143), C(1bc7508516e40628), - C(3059730266ade626), C(ffa526822f391c2), C(e25232d7afc8a406), - C(d2b8a5a3f3b5f670), C(6630f33edb7dfe32), C(c71250ba68c4ea86), C(adf5a9c7)}, {C(17938a1b0e7f5952), C(22cadd2f56f8a4be), C(84b0d1183d5ed7c1), - C(c1336b92fef91bf6), C(80332a3945f33fa9), C(a0f68b86f726ff92), - C(a3db5282cf5f4c0b), C(c1336b92fef91bf6), C(80332a3945f33fa9), - C(a0f68b86f726ff92), C(a3db5282cf5f4c0b), C(82640b6fc4916607), - C(2dc2a3aa1a894175), C(8b4c852bdee7cc9), C(10b9d0a08b55ff83), C(32264b75)}, + C(32264b75)}, {C(de9e0cb0e16f6e6d), C(238e6283aa4f6594), C(4fb9c914c2f0a13b), - C(497cb912b670f3b), C(d963a3f02ff4a5b6), C(4fccefae11b50391), - C(42ba47db3f7672f), C(497cb912b670f3b), C(d963a3f02ff4a5b6), - C(4fccefae11b50391), C(42ba47db3f7672f), C(1d6b655a1889feef), - C(5f319abf8fafa19f), C(715c2e49deb14620), C(8d9153082ecdcea4), C(a64b3376)}, - {C(6d4b876d9b146d1a), C(aab2d64ce8f26739), C(d315f93600e83fe5), - C(2fe9fabdbe7fdd4), C(755db249a2d81a69), C(f27929f360446d71), - C(79a1bf957c0c1b92), C(2fe9fabdbe7fdd4), C(755db249a2d81a69), - C(f27929f360446d71), C(79a1bf957c0c1b92), C(3c8a28d4c936c9cd), - C(df0d3d13b2c6a902), C(c76702dd97cd2edd), C(1aa220f7be16517), C(d33890e)}, + {C(6d4b876d9b146d1a), C(aab2d64ce8f26739), C(d315f93600e83fe5), C(d33890e)}, {C(e698fa3f54e6ea22), C(bd28e20e7455358c), C(9ace161f6ea76e66), - C(d53fb7e3c93a9e4), C(737ae71b051bf108), C(7ac71feb84c2df42), - C(3d8075cd293a15b4), C(d53fb7e3c93a9e4), C(737ae71b051bf108), - C(7ac71feb84c2df42), C(3d8075cd293a15b4), C(bf8cee5e095d8a7c), - C(e7086b3c7608143a), C(e55b0c2fa938d70c), C(fffb5f58e643649c), C(926d4b63)}, {C(7bc0deed4fb349f7), C(1771aff25dc722fa), C(19ff0644d9681917), - C(cf7d7f25bd70cd2c), C(9464ed9baeb41b4f), C(b9064f5c3cb11b71), - C(237e39229b012b20), C(cf7d7f25bd70cd2c), C(9464ed9baeb41b4f), - C(b9064f5c3cb11b71), C(237e39229b012b20), C(dd54d3f5d982dffe), - C(7fc7562dbfc81dbf), C(5b0dd1924f70945), C(f1760537d8261135), C(d51ba539)}, + C(d51ba539)}, {C(db4b15e88533f622), C(256d6d2419b41ce9), C(9d7c5378396765d5), - C(9040e5b936b8661b), C(276e08fa53ac27fd), C(8c944d39c2bdd2cc), - C(e2514c9802a5743c), C(9040e5b936b8661b), C(276e08fa53ac27fd), - C(8c944d39c2bdd2cc), C(e2514c9802a5743c), C(e82107b11ac90386), - C(7d6a22bc35055e6), C(fd6ea9d1c438d8ae), C(be6015149e981553), C(7f37636d)}, + C(7f37636d)}, {C(922834735e86ecb2), C(363382685b88328e), C(e9c92960d7144630), - C(8431b1bfd0a2379c), C(90383913aea283f9), C(a6163831eb4924d2), - C(5f3921b4f9084aee), C(8431b1bfd0a2379c), C(90383913aea283f9), - C(a6163831eb4924d2), C(5f3921b4f9084aee), C(7a70061a1473e579), - C(5b19d80dcd2c6331), C(6196b97931faad27), C(869bf6828e237c3f), C(b98026c0)}, {C(30f1d72c812f1eb8), C(b567cd4a69cd8989), C(820b6c992a51f0bc), - C(c54677a80367125e), C(3204fbdba462e606), C(8563278afc9eae69), - C(262147dd4bf7e566), C(c54677a80367125e), C(3204fbdba462e606), - C(8563278afc9eae69), C(262147dd4bf7e566), C(2178b63e7ee2d230), - C(e9c61ad81f5bff26), C(9af7a81b3c501eca), C(44104a3859f0238f), C(b877767e)}, - {C(168884267f3817e9), C(5b376e050f637645), C(1c18314abd34497a), - C(9598f6ab0683fcc2), C(1c805abf7b80e1ee), C(dec9ac42ee0d0f32), - C(8cd72e3912d24663), C(9598f6ab0683fcc2), C(1c805abf7b80e1ee), - C(dec9ac42ee0d0f32), C(8cd72e3912d24663), C(1f025d405f1c1d87), - C(bf7b6221e1668f8f), C(52316f64e692dbb0), C(7bf43df61ec51b39), C(aefae77)}, - {C(82e78596ee3e56a7), C(25697d9c87f30d98), C(7600a8342834924d), - C(6ba372f4b7ab268b), C(8c3237cf1fe243df), C(3833fc51012903df), - C(8e31310108c5683f), C(6ba372f4b7ab268b), C(8c3237cf1fe243df), - C(3833fc51012903df), C(8e31310108c5683f), C(126593715c2de429), - C(48ca8f35a3f54b90), C(b9322b632f4f8b0), C(926bb169b7337693), C(f686911)}, + {C(168884267f3817e9), C(5b376e050f637645), C(1c18314abd34497a), C(aefae77)}, + {C(82e78596ee3e56a7), C(25697d9c87f30d98), C(7600a8342834924d), C(f686911)}, {C(aa2d6cf22e3cc252), C(9b4dec4f5e179f16), C(76fb0fba1d99a99a), - C(9a62af3dbba140da), C(27857ea044e9dfc1), C(33abce9da2272647), - C(b22a7993aaf32556), C(9a62af3dbba140da), C(27857ea044e9dfc1), - C(33abce9da2272647), C(b22a7993aaf32556), C(bf8f88f8019bedf0), - C(ed2d7f01fb273905), C(6b45f15901b481cd), C(f88ebb413ba6a8d5), C(3deadf12)}, {C(7bf5ffd7f69385c7), C(fc077b1d8bc82879), C(9c04e36f9ed83a24), - C(82065c62e6582188), C(8ef787fd356f5e43), C(2922e53e36e17dfa), - C(9805f223d385010b), C(82065c62e6582188), C(8ef787fd356f5e43), - C(2922e53e36e17dfa), C(9805f223d385010b), C(692154f3491b787d), - C(e7e64700e414fbf), C(757d4d4ab65069a0), C(cd029446a8e348e2), C(ccf02a4e)}, + C(ccf02a4e)}, {C(e89c8ff9f9c6e34b), C(f54c0f669a49f6c4), C(fc3e46f5d846adef), - C(22f2aa3df2221cc), C(f66fea90f5d62174), C(b75defaeaa1dd2a7), - C(9b994cd9a7214fd5), C(22f2aa3df2221cc), C(f66fea90f5d62174), - C(b75defaeaa1dd2a7), C(9b994cd9a7214fd5), C(fac675a31804b773), - C(98bcb3b820c50fc6), C(e14af64d28cf0885), C(27466fbd2b360eb5), C(176c1722)}, - {C(a18fbcdccd11e1f4), C(8248216751dfd65e), C(40c089f208d89d7c), - C(229b79ab69ae97d), C(a87aabc2ec26e582), C(be2b053721eb26d2), - C(10febd7f0c3d6fcb), C(229b79ab69ae97d), C(a87aabc2ec26e582), - C(be2b053721eb26d2), C(10febd7f0c3d6fcb), C(9cc5b9b2f6e3bf7b), - C(655d8495fe624a86), C(6381a9f3d1f2bd7e), C(79ebabbfc25c83e2), C(26f82ad)}, + {C(a18fbcdccd11e1f4), C(8248216751dfd65e), C(40c089f208d89d7c), C(26f82ad)}, {C(2d54f40cc4088b17), C(59d15633b0cd1399), C(a8cc04bb1bffd15b), - C(d332cdb073d8dc46), C(272c56466868cb46), C(7e7fcbe35ca6c3f3), - C(ee8f51e5a70399d4), C(d332cdb073d8dc46), C(272c56466868cb46), - C(7e7fcbe35ca6c3f3), C(ee8f51e5a70399d4), C(16737a9c7581fe7b), - C(ed04bf52f4b75dcb), C(9707ffb36bd30c1a), C(1390f236fdc0de3e), C(b5244f42)}, {C(69276946cb4e87c7), C(62bdbe6183be6fa9), C(3ba9773dac442a1a), - C(702e2afc7f5a1825), C(8c49b11ea8151fdc), C(caf3fef61f5a86fa), - C(ef0b2ee8649d7272), C(702e2afc7f5a1825), C(8c49b11ea8151fdc), - C(caf3fef61f5a86fa), C(ef0b2ee8649d7272), C(9e34a4e08d9441e1), - C(7bdc0cd64d5af533), C(a926b14d99e3d868), C(fca923a17788cce4), C(49a689e5)}, - {C(668174a3f443df1d), C(407299392da1ce86), C(c2a3f7d7f2c5be28), - C(a590b202a7a5807b), C(968d2593f7ccb54e), C(9dd8d669e3e95dec), - C(ee0cc5dd58b6e93a), C(a590b202a7a5807b), C(968d2593f7ccb54e), - C(9dd8d669e3e95dec), C(ee0cc5dd58b6e93a), C(ac65d5a9466fb483), - C(221be538b2c9d806), C(5cbe9441784f9fd9), C(d4c7d5d6e3c122b8), C(59fcdd3)}, - {C(5e29be847bd5046), C(b561c7f19c8f80c3), C(5e5abd5021ccaeaf), - C(7432d63888e0c306), C(74bbceeed479cb71), C(6471586599575fdf), - C(6a859ad23365cba2), C(7432d63888e0c306), C(74bbceeed479cb71), - C(6471586599575fdf), C(6a859ad23365cba2), C(f9ceec84acd18dcc), - C(74a242ff1907437c), C(f70890194e1ee913), C(777dfcb4bb01f0ba), - C(4f4b04e9)}, + {C(668174a3f443df1d), C(407299392da1ce86), C(c2a3f7d7f2c5be28), C(59fcdd3)}, + {C(5e29be847bd5046), C(b561c7f19c8f80c3), C(5e5abd5021ccaeaf), C(4f4b04e9)}, {C(cd0d79f2164da014), C(4c386bb5c5d6ca0c), C(8e771b03647c3b63), - C(69db23875cb0b715), C(ada8dd91504ae37f), C(46bf18dbf045ed6a), - C(e1b5f67b0645ab63), C(69db23875cb0b715), C(ada8dd91504ae37f), - C(46bf18dbf045ed6a), C(e1b5f67b0645ab63), C(877be8f5dcddff4), - C(6d471b5f9ca2e2d1), C(802c86d6f495b9bb), C(a1f9b9b22b3be704), C(8b00f891)}, {C(e0e6fc0b1628af1d), C(29be5fb4c27a2949), C(1c3f781a604d3630), - C(c4af7faf883033aa), C(9bd296c4e9453cac), C(ca45426c1f7e33f9), - C(a6bbdcf7074d40c5), C(c4af7faf883033aa), C(9bd296c4e9453cac), - C(ca45426c1f7e33f9), C(a6bbdcf7074d40c5), C(e13a005d7142733b), - C(c02b7925c5eeefaf), C(d39119a60441e2d5), C(3c24c710df8f4d43), C(16e114f3)}, {C(2058927664adfd93), C(6e8f968c7963baa5), C(af3dced6fff7c394), - C(42e34cf3d53c7876), C(9cddbb26424dc5e), C(64f6340a6d8eddad), - C(2196e488eb2a3a4b), C(42e34cf3d53c7876), C(9cddbb26424dc5e), - C(64f6340a6d8eddad), C(2196e488eb2a3a4b), C(c9e9da25911a16fd), - C(e21b4683f3e196a8), C(cb80bf1a4c6fdbb4), C(53792e9b3c3e67f8), C(d6b6dadc)}, {C(dc107285fd8e1af7), C(a8641a0609321f3f), C(db06e89ffdc54466), - C(bcc7a81ed5432429), C(b6d7bdc6ad2e81f1), C(93605ec471aa37db), - C(a2a73f8a85a8e397), C(bcc7a81ed5432429), C(b6d7bdc6ad2e81f1), - C(93605ec471aa37db), C(a2a73f8a85a8e397), C(10a012b8ca7ac24b), - C(aac5fd63351595cf), C(5bb4c648a226dea0), C(9d11ecb2b5c05c5f), C(897e20ac)}, - {C(fbba1afe2e3280f1), C(755a5f392f07fce), C(9e44a9a15402809a), - C(6226a32e25099848), C(ea895661ecf53004), C(4d7e0158db2228b9), - C(e5a7d82922f69842), C(6226a32e25099848), C(ea895661ecf53004), - C(4d7e0158db2228b9), C(e5a7d82922f69842), C(2cea7713b69840ca), - C(18de7b9ae938375b), C(f127cca08f3cc665), C(b1c22d727665ad2), C(f996e05d)}, + {C(fbba1afe2e3280f1), C(755a5f392f07fce), C(9e44a9a15402809a), C(f996e05d)}, {C(bfa10785ddc1011b), C(b6e1c4d2f670f7de), C(517d95604e4fcc1f), - C(ca6552a0dfb82c73), C(b024cdf09e34ba07), C(66cd8c5a95d7393b), - C(e3939acf790d4a74), C(ca6552a0dfb82c73), C(b024cdf09e34ba07), - C(66cd8c5a95d7393b), C(e3939acf790d4a74), C(97827541a1ef051e), - C(ac2fce47ebe6500c), C(b3f06d3bddf3bd6a), C(1d74afb25e1ce5fe), C(c4306af6)}, - {C(534cc35f0ee1eb4e), C(b703820f1f3b3dce), C(884aa164cf22363), - C(f14ef7f47d8a57a3), C(80d1f86f2e061d7c), C(401d6c2f151b5a62), - C(e988460224108944), C(f14ef7f47d8a57a3), C(80d1f86f2e061d7c), - C(401d6c2f151b5a62), C(e988460224108944), C(7804d4135f68cd19), - C(5487b4b39e69fe8e), C(8cc5999015358a27), C(8f3729b61c2d5601), - C(6dcad433)}, - {C(7ca6e3933995dac), C(fd118c77daa8188), C(3aceb7b5e7da6545), - C(c8389799445480db), C(5389f5df8aacd50d), C(d136581f22fab5f), - C(c2f31f85991da417), C(c8389799445480db), C(5389f5df8aacd50d), - C(d136581f22fab5f), C(c2f31f85991da417), C(aefbf9ff84035a43), - C(8accbaf44adadd7c), C(e57f3657344b67f5), C(21490e5e8abdec51), - C(3c07374d)}, + {C(534cc35f0ee1eb4e), C(b703820f1f3b3dce), C(884aa164cf22363), C(6dcad433)}, + {C(7ca6e3933995dac), C(fd118c77daa8188), C(3aceb7b5e7da6545), C(3c07374d)}, {C(f0d6044f6efd7598), C(e044d6ba4369856e), C(91968e4f8c8a1a4c), - C(70bd1968996bffc2), C(4c613de5d8ab32ac), C(fe1f4f97206f79d8), - C(ac0434f2c4e213a9), C(70bd1968996bffc2), C(4c613de5d8ab32ac), - C(fe1f4f97206f79d8), C(ac0434f2c4e213a9), C(7490e9d82cfe22ca), - C(5fbbf7f987454238), C(c39e0dc8368ce949), C(22201d3894676c71), C(f0f4602c)}, {C(3d69e52049879d61), C(76610636ea9f74fe), C(e9bf5602f89310c0), - C(8eeb177a86053c11), C(e390122c345f34a2), C(1e30e47afbaaf8d6), - C(7b892f68e5f91732), C(8eeb177a86053c11), C(e390122c345f34a2), - C(1e30e47afbaaf8d6), C(7b892f68e5f91732), C(b87922525fa44158), - C(f440a1ee1a1a766b), C(ee8efad279d08c5c), C(421f910c5b60216e), C(3e1ea071)}, - {C(79da242a16acae31), C(183c5f438e29d40), C(6d351710ae92f3de), - C(27233b28b5b11e9b), C(c7dfe8988a942700), C(570ed11c4abad984), - C(4b4c04632f48311a), C(27233b28b5b11e9b), C(c7dfe8988a942700), - C(570ed11c4abad984), C(4b4c04632f48311a), C(12f33235442cbf9), - C(a35315ca0b5b8cdb), C(d8abde62ead5506b), C(fc0fcf8478ad5266), - C(67580f0c)}, + {C(79da242a16acae31), C(183c5f438e29d40), C(6d351710ae92f3de), C(67580f0c)}, {C(461c82656a74fb57), C(d84b491b275aa0f7), C(8f262cb29a6eb8b2), - C(49fa3070bc7b06d0), C(f12ed446bd0c0539), C(6d43ac5d1dd4b240), - C(7609524fe90bec93), C(49fa3070bc7b06d0), C(f12ed446bd0c0539), - C(6d43ac5d1dd4b240), C(7609524fe90bec93), C(391c2b2e076ec241), - C(f5e62deda7839f7b), C(3c7b3186a10d870f), C(77ef4f2cba4f1005), C(4e109454)}, - {C(53c1a66d0b13003), C(731f060e6fe797fc), C(daa56811791371e3), - C(57466046cf6896ed), C(8ac37e0e8b25b0c6), C(3e6074b52ad3cf18), - C(aa491ce7b45db297), C(57466046cf6896ed), C(8ac37e0e8b25b0c6), - C(3e6074b52ad3cf18), C(aa491ce7b45db297), C(f7a9227c5e5e22c3), - C(3d92e0841e29ce28), C(2d30da5b2859e59d), C(ff37fa1c9cbfafc2), - C(88a474a7)}, - {C(d3a2efec0f047e9), C(1cabce58853e58ea), C(7a17b2eae3256be4), - C(c2dcc9758c910171), C(cb5cddaeff4ddb40), C(5d7cc5869baefef1), - C(9644c5853af9cfeb), C(c2dcc9758c910171), C(cb5cddaeff4ddb40), - C(5d7cc5869baefef1), C(9644c5853af9cfeb), C(255c968184694ee1), - C(4e4d726eda360927), C(7d27dd5b6d100377), C(9a300e2020ddea2c), C(5b5bedd)}, + {C(53c1a66d0b13003), C(731f060e6fe797fc), C(daa56811791371e3), C(88a474a7)}, + {C(d3a2efec0f047e9), C(1cabce58853e58ea), C(7a17b2eae3256be4), C(5b5bedd)}, {C(43c64d7484f7f9b2), C(5da002b64aafaeb7), C(b576c1e45800a716), - C(3ee84d3d5b4ca00b), C(5cbc6d701894c3f9), C(d9e946f5ae1ca95), - C(24ca06e67f0b1833), C(3ee84d3d5b4ca00b), C(5cbc6d701894c3f9), - C(d9e946f5ae1ca95), C(24ca06e67f0b1833), C(3413d46b4152650e), - C(cbdfdbc2ab516f9c), C(2aad8acb739e0c6c), C(2bfc950d9f9fa977), C(1aaddfa7)}, {C(a7dec6ad81cf7fa1), C(180c1ab708683063), C(95e0fd7008d67cff), - C(6b11c5073687208), C(7e0a57de0d453f3), C(e48c267d4f646867), - C(2168e9136375f9cb), C(6b11c5073687208), C(7e0a57de0d453f3), - C(e48c267d4f646867), C(2168e9136375f9cb), C(64da194aeeea7fdf), - C(a3b9f01fa5885678), C(c316f8ee2eb2bd17), C(a7e4d80f83e4427f), C(5be07fd8)}, - {C(5408a1df99d4aff), C(b9565e588740f6bd), C(abf241813b08006e), - C(7da9e81d89fda7ad), C(274157cabe71440d), C(2c22d9a480b331f7), - C(e835c8ac746472d5), C(7da9e81d89fda7ad), C(274157cabe71440d), - C(2c22d9a480b331f7), C(e835c8ac746472d5), C(2038ce817a201ae4), - C(46f3289dfe1c5e40), C(435578a42d4b7c56), C(f96d9f409fcf561), C(cbca8606)}, + {C(5408a1df99d4aff), C(b9565e588740f6bd), C(abf241813b08006e), C(cbca8606)}, {C(a8b27a6bcaeeed4b), C(aec1eeded6a87e39), C(9daf246d6fed8326), - C(d45a938b79f54e8f), C(366b219d6d133e48), C(5b14be3c25c49405), - C(fdd791d48811a572), C(d45a938b79f54e8f), C(366b219d6d133e48), - C(5b14be3c25c49405), C(fdd791d48811a572), C(3de67b8d9e95d335), - C(903c01307cfbeed5), C(af7d65f32274f1d1), C(4dba141b5fc03c42), C(bde64d01)}, {C(9a952a8246fdc269), C(d0dcfcac74ef278c), C(250f7139836f0f1f), - C(c83d3c5f4e5f0320), C(694e7adeb2bf32e5), C(7ad09538a3da27f5), - C(2b5c18f934aa5303), C(c83d3c5f4e5f0320), C(694e7adeb2bf32e5), - C(7ad09538a3da27f5), C(2b5c18f934aa5303), C(c4dad7703d34326e), - C(825569e2bcdc6a25), C(b83d267709ca900d), C(44ed05151f5d74e6), C(ee90cf33)}, {C(c930841d1d88684f), C(5eb66eb18b7f9672), C(e455d413008a2546), - C(bc271bc0df14d647), C(b071100a9ff2edbb), C(2b1a4c1cc31a119a), - C(b5d7caa1bd946cef), C(bc271bc0df14d647), C(b071100a9ff2edbb), - C(2b1a4c1cc31a119a), C(b5d7caa1bd946cef), C(e02623ae10f4aadd), - C(d79f600389cd06fd), C(1e8da7965303e62b), C(86f50e10eeab0925), C(4305c3ce)}, - {C(94dc6971e3cf071a), C(994c7003b73b2b34), C(ea16e85978694e5), - C(336c1b59a1fc19f6), C(c173acaecc471305), C(db1267d24f3f3f36), - C(e9a5ee98627a6e78), C(336c1b59a1fc19f6), C(c173acaecc471305), - C(db1267d24f3f3f36), C(e9a5ee98627a6e78), C(718f334204305ae5), - C(e3b53c148f98d22c), C(a184012df848926), C(6e96386127d51183), C(4b3a1d76)}, - {C(7fc98006e25cac9), C(77fee0484cda86a7), C(376ec3d447060456), - C(84064a6dcf916340), C(fbf55a26790e0ebb), C(2e7f84151c31a5c2), - C(9f7f6d76b950f9bf), C(84064a6dcf916340), C(fbf55a26790e0ebb), - C(2e7f84151c31a5c2), C(9f7f6d76b950f9bf), C(125e094fbee2b146), - C(5706aa72b2eef7c2), C(1c4a2daa905ee66e), C(83d48029b5451694), - C(a8bb6d80)}, - {C(bd781c4454103f6), C(612197322f49c931), C(b9cf17fd7e5462d5), - C(e38e526cd3324364), C(85f2b63a5b5e840a), C(485d7cef5aaadd87), - C(d2b837a462f6db6d), C(e38e526cd3324364), C(85f2b63a5b5e840a), - C(485d7cef5aaadd87), C(d2b837a462f6db6d), C(3e41cef031520d9a), - C(82df73902d7f67e), C(3ba6fd54c15257cb), C(22f91f079be42d40), C(1f9fa607)}, + {C(94dc6971e3cf071a), C(994c7003b73b2b34), C(ea16e85978694e5), C(4b3a1d76)}, + {C(7fc98006e25cac9), C(77fee0484cda86a7), C(376ec3d447060456), C(a8bb6d80)}, + {C(bd781c4454103f6), C(612197322f49c931), C(b9cf17fd7e5462d5), C(1f9fa607)}, {C(da60e6b14479f9df), C(3bdccf69ece16792), C(18ebf45c4fecfdc9), - C(16818ee9d38c6664), C(5519fa9a1e35a329), C(cbd0001e4b08ed8), - C(41a965e37a0c731b), C(16818ee9d38c6664), C(5519fa9a1e35a329), - C(cbd0001e4b08ed8), C(41a965e37a0c731b), C(66e7b5dcca1ca28f), - C(963b2d993614347d), C(9b6fc6f41d411106), C(aaaecaccf7848c0c), C(8d0e4ed2)}, - {C(4ca56a348b6c4d3), C(60618537c3872514), C(2fbb9f0e65871b09), - C(30278016830ddd43), C(f046646d9012e074), C(c62a5804f6e7c9da), - C(98d51f5830e2bc1e), C(30278016830ddd43), C(f046646d9012e074), - C(c62a5804f6e7c9da), C(98d51f5830e2bc1e), C(7b2cbe5d37e3f29e), - C(7b8c3ed50bda4aa0), C(3ea60cc24639e038), C(f7706de9fb0b5801), - C(1bf31347)}, + {C(4ca56a348b6c4d3), C(60618537c3872514), C(2fbb9f0e65871b09), C(1bf31347)}, {C(ebd22d4b70946401), C(6863602bf7139017), C(c0b1ac4e11b00666), - C(7d2782b82bd494b6), C(97159ba1c26b304b), C(42b3b0fd431b2ac2), - C(faa81f82691c830c), C(7d2782b82bd494b6), C(97159ba1c26b304b), - C(42b3b0fd431b2ac2), C(faa81f82691c830c), C(7cc6449234c7e185), - C(aeaa6fa643ca86a5), C(1412db1c0f2e0133), C(4df2fe3e4072934f), C(1ae3fc5b)}, - {C(3cc4693d6cbcb0c), C(501689ea1c70ffa), C(10a4353e9c89e364), - C(58c8aba7475e2d95), C(3e2f291698c9427a), C(e8710d19c9de9e41), - C(65dda22eb04cf953), C(58c8aba7475e2d95), C(3e2f291698c9427a), - C(e8710d19c9de9e41), C(65dda22eb04cf953), C(d7729c48c250cffa), - C(ef76162b2ddfba4b), C(52371e17f4d51f6d), C(ddd002112ff0c833), - C(459c3930)}, + {C(3cc4693d6cbcb0c), C(501689ea1c70ffa), C(10a4353e9c89e364), C(459c3930)}, {C(38908e43f7ba5ef0), C(1ab035d4e7781e76), C(41d133e8c0a68ff7), - C(d1090893afaab8bc), C(96c4fe6922772807), C(4522426c2b4205eb), - C(efad99a1262e7e0d), C(d1090893afaab8bc), C(96c4fe6922772807), - C(4522426c2b4205eb), C(efad99a1262e7e0d), C(c7696029abdb465e), - C(4e18eaf03d517651), C(d006bced54c86ac8), C(4330326d1021860c), C(e00c4184)}, - {C(34983ccc6aa40205), C(21802cad34e72bc4), C(1943e8fb3c17bb8), - C(fc947167f69c0da5), C(ae79cfdb91b6f6c1), C(7b251d04c26cbda3), - C(128a33a79060d25e), C(fc947167f69c0da5), C(ae79cfdb91b6f6c1), - C(7b251d04c26cbda3), C(128a33a79060d25e), C(1eca842dbfe018dd), - C(50a4cd2ee0ba9c63), C(c2f5c97d8399682f), C(3f929fc7cbe8ecbb), - C(ffc7a781)}, + {C(34983ccc6aa40205), C(21802cad34e72bc4), C(1943e8fb3c17bb8), C(ffc7a781)}, {C(86215c45dcac9905), C(ea546afe851cae4b), C(d85b6457e489e374), - C(b7609c8e70386d66), C(36e6ccc278d1636d), C(2f873307c08e6a1c), - C(10f252a758505289), C(b7609c8e70386d66), C(36e6ccc278d1636d), - C(2f873307c08e6a1c), C(10f252a758505289), C(c8977646e81ab4b6), - C(8017b745cd80213b), C(960687db359bea0), C(ef4a470660799488), C(6a125480)}, + C(6a125480)}, {C(420fc255c38db175), C(d503cd0f3c1208d1), C(d4684e74c825a0bc), - C(4c10537443152f3d), C(720451d3c895e25d), C(aff60c4d11f513fd), - C(881e8d6d2d5fb953), C(4c10537443152f3d), C(720451d3c895e25d), - C(aff60c4d11f513fd), C(881e8d6d2d5fb953), C(9dec034a043f1f55), - C(e27a0c22e7bfb39d), C(2220b959128324), C(53240272152dbd8b), C(88a1512b)}, + C(88a1512b)}, {C(1d7a31f5bc8fe2f9), C(4763991092dcf836), C(ed695f55b97416f4), - C(f265edb0c1c411d7), C(30e1e9ec5262b7e6), C(c2c3ba061ce7957a), - C(d975f93b89a16409), C(f265edb0c1c411d7), C(30e1e9ec5262b7e6), - C(c2c3ba061ce7957a), C(d975f93b89a16409), C(e9d703123f43450a), - C(41383fedfed67c82), C(6e9f43ecbbbd6004), C(c7ccd23a24e77b8), C(549bbbe5)}, + C(549bbbe5)}, {C(94129a84c376a26e), C(c245e859dc231933), C(1b8f74fecf917453), - C(e9369d2e9007e74b), C(b1375915d1136052), C(926c2021fe1d2351), - C(1d943addaaa2e7e6), C(e9369d2e9007e74b), C(b1375915d1136052), - C(926c2021fe1d2351), C(1d943addaaa2e7e6), C(f5f515869c246738), - C(7e309cd0e1c0f2a0), C(153c3c36cf523e3b), C(4931c66872ea6758), C(c133d38c)}, - {C(1d3a9809dab05c8d), C(adddeb4f71c93e8), C(ef342eb36631edb), - C(301d7a61c4b3dbca), C(861336c3f0552d61), C(12c6db947471300f), - C(a679ef0ed761deb9), C(301d7a61c4b3dbca), C(861336c3f0552d61), - C(12c6db947471300f), C(a679ef0ed761deb9), C(5f713b720efcd147), - C(37ac330a333aa6b), C(3309dc9ec1616eef), C(52301d7a908026b5), C(fcace348)}, + {C(1d3a9809dab05c8d), C(adddeb4f71c93e8), C(ef342eb36631edb), C(fcace348)}, {C(90fa3ccbd60848da), C(dfa6e0595b569e11), C(e585d067a1f5135d), - C(6cef866ec295abea), C(c486c0d9214beb2d), C(d6e490944d5fe100), - C(59df3175d72c9f38), C(6cef866ec295abea), C(c486c0d9214beb2d), - C(d6e490944d5fe100), C(59df3175d72c9f38), C(3f23aeb4c04d1443), - C(9bf0515cd8d24770), C(958554f60ccaade2), C(5182863c90132fe8), C(ed7b6f9a)}, {C(2dbb4fc71b554514), C(9650e04b86be0f82), C(60f2304fba9274d3), - C(fcfb9443e997cab), C(f13310d96dec2772), C(709cad2045251af2), - C(afd0d30cc6376dad), C(fcfb9443e997cab), C(f13310d96dec2772), - C(709cad2045251af2), C(afd0d30cc6376dad), C(59d4bed30d550d0d), - C(58006d4e22d8aad1), C(eee12d2362d1f13b), C(35cf1d7faaf1d228), C(6d907dda)}, {C(b98bf4274d18374a), C(1b669fd4c7f9a19a), C(b1f5972b88ba2b7a), - C(73119c99e6d508be), C(5d4036a187735385), C(8fa66e192fd83831), - C(2abf64b6b592ed57), C(73119c99e6d508be), C(5d4036a187735385), - C(8fa66e192fd83831), C(2abf64b6b592ed57), C(d4501f95dd84b08c), - C(bf1552439c8bea02), C(4f56fe753ba7e0ba), C(4ca8d35cc058cfcd), C(7a4d48d5)}, {C(d6781d0b5e18eb68), C(b992913cae09b533), C(58f6021caaee3a40), - C(aafcb77497b5a20b), C(411819e5e79b77a3), C(bd779579c51c77ce), - C(58d11f5dcf5d075d), C(aafcb77497b5a20b), C(411819e5e79b77a3), - C(bd779579c51c77ce), C(58d11f5dcf5d075d), C(9eae76cde1cb4233), - C(32fe25a9bf657970), C(1c0c807948edb06a), C(b8f29a3dfaee254d), C(e686f3db)}, - {C(226651cf18f4884c), C(595052a874f0f51c), C(c9b75162b23bab42), - C(3f44f873be4812ec), C(427662c1dbfaa7b2), C(a207ff9638fb6558), - C(a738d919e45f550f), C(3f44f873be4812ec), C(427662c1dbfaa7b2), - C(a207ff9638fb6558), C(a738d919e45f550f), C(cb186ea05717e7d6), - C(1ca7d68a5871fdc1), C(5d4c119ea8ef3750), C(72b6a10fa2ff9406), C(cce7c55)}, - {C(a734fb047d3162d6), C(e523170d240ba3a5), C(125a6972809730e8), - C(d396a297799c24a1), C(8fee992e3069bad5), C(2e3a01b0697ccf57), - C(ee9c7390bd901cfa), C(d396a297799c24a1), C(8fee992e3069bad5), - C(2e3a01b0697ccf57), C(ee9c7390bd901cfa), C(56f2d9da0af28af2), - C(3fdd37b2fe8437cb), C(3d13eeeb60d6aec0), C(2432ae62e800a5ce), C(f58b96b)}, + {C(226651cf18f4884c), C(595052a874f0f51c), C(c9b75162b23bab42), C(cce7c55)}, + {C(a734fb047d3162d6), C(e523170d240ba3a5), C(125a6972809730e8), C(f58b96b)}, {C(c6df6364a24f75a3), C(c294e2c84c4f5df8), C(a88df65c6a89313b), - C(895fe8443183da74), C(c7f2f6f895a67334), C(a0d6b6a506691d31), - C(24f51712b459a9f0), C(895fe8443183da74), C(c7f2f6f895a67334), - C(a0d6b6a506691d31), C(24f51712b459a9f0), C(173a699481b9e088), - C(1dee9b77bcbf45d3), C(32b98a646a8667d0), C(3adcd4ee28f42a0e), C(1bbf6f60)}, - {C(d8d1364c1fbcd10), C(2d7cc7f54832deaa), C(4e22c876a7c57625), - C(a3d5d1137d30c4bd), C(1e7d706a49bdfb9e), C(c63282b20ad86db2), - C(aec97fa07916bfd6), C(a3d5d1137d30c4bd), C(1e7d706a49bdfb9e), - C(c63282b20ad86db2), C(aec97fa07916bfd6), C(7c9ba3e52d44f73e), - C(af62fd245811185d), C(8a9d2dacd8737652), C(bd2cce277d5fbec0), - C(ce5e0cc2)}, + {C(d8d1364c1fbcd10), C(2d7cc7f54832deaa), C(4e22c876a7c57625), C(ce5e0cc2)}, {C(aae06f9146db885f), C(3598736441e280d9), C(fba339b117083e55), - C(b22bf08d9f8aecf7), C(c182730de337b922), C(2b9adc87a0450a46), - C(192c29a9cfc00aad), C(b22bf08d9f8aecf7), C(c182730de337b922), - C(2b9adc87a0450a46), C(192c29a9cfc00aad), C(9fd733f1d84a59d9), - C(d86bd5c9839ace15), C(af20b57303172876), C(9f63cb7161b5364c), C(584cfd6f)}, {C(8955ef07631e3bcc), C(7d70965ea3926f83), C(39aed4134f8b2db6), - C(882efc2561715a9c), C(ef8132a18a540221), C(b20a3c87a8c257c1), - C(f541b8628fad6c23), C(882efc2561715a9c), C(ef8132a18a540221), - C(b20a3c87a8c257c1), C(f541b8628fad6c23), C(9552aed57a6e0467), - C(4d9fdd56867611a7), C(c330279bf23b9eab), C(44dbbaea2fcb8eba), C(8f9bbc33)}, {C(ad611c609cfbe412), C(d3c00b18bf253877), C(90b2172e1f3d0bfd), - C(371a98b2cb084883), C(33a2886ee9f00663), C(be9568818ed6e6bd), - C(f244a0fa2673469a), C(371a98b2cb084883), C(33a2886ee9f00663), - C(be9568818ed6e6bd), C(f244a0fa2673469a), C(b447050bd3e559e9), - C(d3b695dae7a13383), C(ded0bb65be471188), C(ca3c7a2b78922cae), C(d7640d95)}, - {C(d5339adc295d5d69), C(b633cc1dcb8b586a), C(ee84184cf5b1aeaf), - C(89f3aab99afbd636), C(f420e004f8148b9a), C(6818073faa797c7c), - C(dd3b4e21cbbf42ca), C(89f3aab99afbd636), C(f420e004f8148b9a), - C(6818073faa797c7c), C(dd3b4e21cbbf42ca), C(6a2b7db261164844), - C(cbead63d1895852a), C(93d37e1eae05e2f9), C(5d06db2703fbc3ae), C(3d12a2b)}, + {C(d5339adc295d5d69), C(b633cc1dcb8b586a), C(ee84184cf5b1aeaf), C(3d12a2b)}, {C(40d0aeff521375a8), C(77ba1ad7ecebd506), C(547c6f1a7d9df427), - C(21c2be098327f49b), C(7e035065ac7bbef5), C(6d7348e63023fb35), - C(9d427dc1b67c3830), C(21c2be098327f49b), C(7e035065ac7bbef5), - C(6d7348e63023fb35), C(9d427dc1b67c3830), C(4e3d018a43858341), - C(cf924bb44d6b43c5), C(4618b6a26e3446ae), C(54d3013fac3ed469), C(aaeafed0)}, {C(8b2d54ae1a3df769), C(11e7adaee3216679), C(3483781efc563e03), - C(9d097dd3152ab107), C(51e21d24126e8563), C(cba56cac884a1354), - C(39abb1b595f0a977), C(9d097dd3152ab107), C(51e21d24126e8563), - C(cba56cac884a1354), C(39abb1b595f0a977), C(81e6dd1c1109848f), - C(1644b209826d7b15), C(6ac67e4e4b4812f0), C(b3a9f5622c935bf7), C(95b9b814)}, {C(99c175819b4eae28), C(932e8ff9f7a40043), C(ec78dcab07ca9f7c), - C(c1a78b82ba815b74), C(458cbdfc82eb322a), C(17f4a192376ed8d7), - C(6f9e92968bc8ccef), C(c1a78b82ba815b74), C(458cbdfc82eb322a), - C(17f4a192376ed8d7), C(6f9e92968bc8ccef), C(93e098c333b39905), - C(d59b1cace44b7fdc), C(f7a64ed78c64c7c5), C(7c6eca5dd87ec1ce), C(45fbe66e)}, {C(2a418335779b82fc), C(af0295987849a76b), C(c12bc5ff0213f46e), - C(5aeead8d6cb25bb9), C(739315f7743ec3ff), C(9ab48d27111d2dcc), - C(5b87bd35a975929b), C(5aeead8d6cb25bb9), C(739315f7743ec3ff), - C(9ab48d27111d2dcc), C(5b87bd35a975929b), C(c3dd8d6d95a46bb3), - C(7bf9093215a4f483), C(cb557d6ed84285bd), C(daf58422f261fdb5), C(b4baa7a8)}, - {C(3b1fc6a3d279e67d), C(70ea1e49c226396), C(25505adcf104697c), - C(ba1ffba29f0367aa), C(a20bec1dd15a8b6c), C(e9bf61d2dab0f774), - C(f4f35bf5870a049c), C(ba1ffba29f0367aa), C(a20bec1dd15a8b6c), - C(e9bf61d2dab0f774), C(f4f35bf5870a049c), C(26787efa5b92385), - C(3d9533590ce30b59), C(a4da3e40530a01d4), C(6395deaefb70067c), - C(83e962fe)}, - {C(d97eacdf10f1c3c9), C(b54f4654043a36e0), C(b128f6eb09d1234), - C(d8ad7ec84a9c9aa2), C(e256cffed11f69e6), C(2cf65e4958ad5bda), - C(cfbf9b03245989a7), C(d8ad7ec84a9c9aa2), C(e256cffed11f69e6), - C(2cf65e4958ad5bda), C(cfbf9b03245989a7), C(9fa51e6686cf4444), - C(9425c117a34609d5), C(b25f7e2c6f30e96), C(ea5477c3f2b5afd1), C(aac3531c)}, + {C(3b1fc6a3d279e67d), C(70ea1e49c226396), C(25505adcf104697c), C(83e962fe)}, + {C(d97eacdf10f1c3c9), C(b54f4654043a36e0), C(b128f6eb09d1234), C(aac3531c)}, {C(293a5c1c4e203cd4), C(6b3329f1c130cefe), C(f2e32f8ec76aac91), - C(361e0a62c8187bff), C(6089971bb84d7133), C(93df7741588dd50b), - C(c2a9b6abcd1d80b1), C(361e0a62c8187bff), C(6089971bb84d7133), - C(93df7741588dd50b), C(c2a9b6abcd1d80b1), C(4d2f86869d79bc59), - C(85cd24d8aa570ff), C(b0dcf6ef0e94bbb5), C(2037c69aa7a78421), C(2b1db7cc)}, + C(2b1db7cc)}, {C(4290e018ffaedde7), C(a14948545418eb5e), C(72d851b202284636), - C(4ec02f3d2f2b23f2), C(ab3580708aa7c339), C(cdce066fbab3f65), - C(d8ed3ecf3c7647b9), C(4ec02f3d2f2b23f2), C(ab3580708aa7c339), - C(cdce066fbab3f65), C(d8ed3ecf3c7647b9), C(6d2204b3e31f344a), - C(61a4d87f80ee61d7), C(446c43dbed4b728f), C(73130ac94f58747e), C(cf00cd31)}, {C(f919a59cbde8bf2f), C(a56d04203b2dc5a5), C(38b06753ac871e48), - C(c2c9fc637dbdfcfa), C(292ab8306d149d75), C(7f436b874b9ffc07), - C(a5b56b0129218b80), C(c2c9fc637dbdfcfa), C(292ab8306d149d75), - C(7f436b874b9ffc07), C(a5b56b0129218b80), C(9188f7bdc47ec050), - C(cfe9345d03a15ade), C(40b520fb2750c49e), C(c2e83d343968af2e), C(7d3c43b8)}, {C(1d70a3f5521d7fa4), C(fb97b3fdc5891965), C(299d49bbbe3535af), - C(e1a8286a7d67946e), C(52bd956f047b298), C(cbd74332dd4204ac), - C(12b5be7752721976), C(e1a8286a7d67946e), C(52bd956f047b298), - C(cbd74332dd4204ac), C(12b5be7752721976), C(278426e27f6204b6), - C(932ca7a7cd610181), C(41647321f0a5914d), C(48f4aa61a0ae80db), C(cbd5fac6)}, {C(6af98d7b656d0d7c), C(d2e99ae96d6b5c0c), C(f63bd1603ef80627), - C(bde51033ac0413f8), C(bc0272f691aec629), C(6204332651bebc44), - C(1cbf00de026ea9bd), C(bde51033ac0413f8), C(bc0272f691aec629), - C(6204332651bebc44), C(1cbf00de026ea9bd), C(b9c7ed6a75f3ff1e), - C(7e310b76a5808e4f), C(acbbd1aad5531885), C(fc245f2473adeb9c), C(76d0fec4)}, - {C(395b7a8adb96ab75), C(582df7165b20f4a), C(e52bd30e9ff657f9), - C(6c71064996cbec8b), C(352c535edeefcb89), C(ac7f0aba15cd5ecd), - C(3aba1ca8353e5c60), C(6c71064996cbec8b), C(352c535edeefcb89), - C(ac7f0aba15cd5ecd), C(3aba1ca8353e5c60), C(5c30a288a80ce646), - C(c2940488b6617674), C(925f8cc66b370575), C(aa65d1283b9bb0ef), - C(405e3402)}, + {C(395b7a8adb96ab75), C(582df7165b20f4a), C(e52bd30e9ff657f9), C(405e3402)}, {C(3822dd82c7df012f), C(b9029b40bd9f122b), C(fd25b988468266c4), - C(43e47bd5bab1e0ef), C(4a71f363421f282f), C(880b2f32a2b4e289), - C(1299d4eda9d3eadf), C(43e47bd5bab1e0ef), C(4a71f363421f282f), - C(880b2f32a2b4e289), C(1299d4eda9d3eadf), C(d713a40226f5564), - C(4d8d34fedc769406), C(a85001b29cd9cac3), C(cae92352a41fd2b0), C(c732c481)}, {C(79f7efe4a80b951a), C(dd3a3fddfc6c9c41), C(ab4c812f9e27aa40), - C(832954ec9d0de333), C(94c390aa9bcb6b8a), C(f3b32afdc1f04f82), - C(d229c3b72e4b9a74), C(832954ec9d0de333), C(94c390aa9bcb6b8a), - C(f3b32afdc1f04f82), C(d229c3b72e4b9a74), C(1d11860d7ed624a6), - C(cadee20b3441b984), C(75307079bf306f7b), C(87902aa3b9753ba4), C(a8d123c9)}, - {C(ae6e59f5f055921a), C(e9d9b7bf68e82), C(5ce4e4a5b269cc59), - C(4960111789727567), C(149b8a37c7125ab6), C(78c7a13ab9749382), - C(1c61131260ca151a), C(4960111789727567), C(149b8a37c7125ab6), - C(78c7a13ab9749382), C(1c61131260ca151a), C(1e93276b35c309a0), - C(2618f56230acde58), C(af61130a18e4febf), C(7145deb18e89befe), - C(1e80ad7d)}, + {C(ae6e59f5f055921a), C(e9d9b7bf68e82), C(5ce4e4a5b269cc59), C(1e80ad7d)}, {C(8959dbbf07387d36), C(b4658afce48ea35d), C(8f3f82437d8cb8d6), - C(6566d74954986ba5), C(99d5235cc82519a7), C(257a23805c2d825), - C(ad75ccb968e93403), C(6566d74954986ba5), C(99d5235cc82519a7), - C(257a23805c2d825), C(ad75ccb968e93403), C(b45bd4cf78e11f7f), - C(80c5536bdc487983), C(a4fd76ecbf018c8a), C(3b9dac78a7a70d43), C(52aeb863)}, {C(4739613234278a49), C(99ea5bcd340bf663), C(258640912e712b12), - C(c8a2827404991402), C(7ee5e78550f02675), C(2ec53952db5ac662), - C(1526405a9df6794b), C(c8a2827404991402), C(7ee5e78550f02675), - C(2ec53952db5ac662), C(1526405a9df6794b), C(eddc6271170c5e1f), - C(f5a85f986001d9d6), C(95427c677bf58d58), C(53ed666dfa85cb29), C(ef7c0c18)}, {C(420e6c926bc54841), C(96dbbf6f4e7c75cd), C(d8d40fa70c3c67bb), - C(3edbc10e4bfee91b), C(f0d681304c28ef68), C(77ea602029aaaf9c), - C(90f070bd24c8483c), C(3edbc10e4bfee91b), C(f0d681304c28ef68), - C(77ea602029aaaf9c), C(90f070bd24c8483c), C(28bc8e41e08ceb86), - C(1eb56e48a65691ef), C(9fea5301c9202f0e), C(3fcb65091aa9f135), C(b6ad4b68)}, {C(c8601bab561bc1b7), C(72b26272a0ff869a), C(56fdfc986d6bc3c4), - C(83707730cad725d4), C(c9ca88c3a779674a), C(e1c696fbbd9aa933), - C(723f3baab1c17a45), C(83707730cad725d4), C(c9ca88c3a779674a), - C(e1c696fbbd9aa933), C(723f3baab1c17a45), C(f82abc7a1d851682), - C(30683836818e857d), C(78bfa3e89a5ab23f), C(6928234482b31817), C(c1e46b17)}, - {C(b2d294931a0e20eb), C(284ffd9a0815bc38), C(1f8a103aac9bbe6), - C(1ef8e98e1ea57269), C(5971116272f45a8b), C(187ad68ce95d8eac), - C(e94e93ee4e8ecaa6), C(1ef8e98e1ea57269), C(5971116272f45a8b), - C(187ad68ce95d8eac), C(e94e93ee4e8ecaa6), C(a0ff2a58611838b5), - C(b01e03849bfbae6f), C(d081e202e28ea3ab), C(51836bcee762bf13), - C(57b8df25)}, + {C(b2d294931a0e20eb), C(284ffd9a0815bc38), C(1f8a103aac9bbe6), C(57b8df25)}, {C(7966f53c37b6c6d7), C(8e6abcfb3aa2b88f), C(7f2e5e0724e5f345), - C(3eeb60c3f5f8143d), C(a25aec05c422a24f), C(b026b03ad3cca4db), - C(e6e030028cc02a02), C(3eeb60c3f5f8143d), C(a25aec05c422a24f), - C(b026b03ad3cca4db), C(e6e030028cc02a02), C(16fe679338b34bfc), - C(c1be385b5c8a9de4), C(65af5df6567530eb), C(ed3b303df4dc6335), C(e9fa36d6)}, {C(be9bb0abd03b7368), C(13bca93a3031be55), C(e864f4f52b55b472), - C(36a8d13a2cbb0939), C(254ac73907413230), C(73520d1522315a70), - C(8c9fdb5cf1e1a507), C(36a8d13a2cbb0939), C(254ac73907413230), - C(73520d1522315a70), C(8c9fdb5cf1e1a507), C(b3640570b926886), - C(fba2344ee87f7bab), C(de57341ab448df05), C(385612ee094fa977), C(8f8daefc)}, - {C(a08d128c5f1649be), C(a8166c3dbbe19aad), C(cb9f914f829ec62c), - C(5b2b7ca856fad1c3), C(8093022d682e375d), C(ea5d163ba7ea231f), - C(d6181d012c0de641), C(5b2b7ca856fad1c3), C(8093022d682e375d), - C(ea5d163ba7ea231f), C(d6181d012c0de641), C(e7d40d0ab8b08159), - C(2e82320f51b3a67e), C(27c2e356ea0b63a3), C(58842d01a2b1d077), C(6e1bb7e)}, + {C(a08d128c5f1649be), C(a8166c3dbbe19aad), C(cb9f914f829ec62c), C(6e1bb7e)}, {C(7c386f0ffe0465ac), C(530419c9d843dbf3), C(7450e3a4f72b8d8c), - C(48b218e3b721810d), C(d3757ac8609bc7fc), C(111ba02a88aefc8), - C(e86343137d3bfc2a), C(48b218e3b721810d), C(d3757ac8609bc7fc), - C(111ba02a88aefc8), C(e86343137d3bfc2a), C(44ad26b51661b507), - C(db1268670274f51e), C(62a5e75beae875f3), C(e266e7a44c5f28c6), C(fd0076f0)}, - {C(bb362094e7ef4f8), C(ff3c2a48966f9725), C(55152803acd4a7fe), - C(15747d8c505ffd00), C(438a15f391312cd6), C(e46ca62c26d821f5), - C(be78d74c9f79cb44), C(15747d8c505ffd00), C(438a15f391312cd6), - C(e46ca62c26d821f5), C(be78d74c9f79cb44), C(a8aa19f3aa59f09a), - C(effb3cddab2c9267), C(d78e41ad97cb16a5), C(ace6821513527d32), - C(899b17b6)}, + {C(bb362094e7ef4f8), C(ff3c2a48966f9725), C(55152803acd4a7fe), C(899b17b6)}, {C(cd80dea24321eea4), C(52b4fdc8130c2b15), C(f3ea100b154bfb82), - C(d9ccef1d4be46988), C(5ede0c4e383a5e66), C(da69683716a54d1e), - C(bfc3fdf02d242d24), C(d9ccef1d4be46988), C(5ede0c4e383a5e66), - C(da69683716a54d1e), C(bfc3fdf02d242d24), C(20ed30274651b3f5), - C(4c659824169e86c6), C(637226dae5b52a0e), C(7e050dbd1c71dc7f), C(e3e84e31)}, {C(d599a04125372c3a), C(313136c56a56f363), C(1e993c3677625832), - C(2870a99c76a587a4), C(99f74cc0b182dda4), C(8a5e895b2f0ca7b6), - C(3d78882d5e0bb1dc), C(2870a99c76a587a4), C(99f74cc0b182dda4), - C(8a5e895b2f0ca7b6), C(3d78882d5e0bb1dc), C(f466123732a3e25e), - C(aca5e59716a40e50), C(261d2e7383d0e686), C(ce9362d6a42c15a7), C(eef79b6b)}, - {C(dbbf541e9dfda0a), C(1479fceb6db4f844), C(31ab576b59062534), - C(a3335c417687cf3a), C(92ff114ac45cda75), C(c3b8a627384f13b5), - C(c4f25de33de8b3f7), C(a3335c417687cf3a), C(92ff114ac45cda75), - C(c3b8a627384f13b5), C(c4f25de33de8b3f7), C(eacbf520578c5964), - C(4cb19c5ab24f3215), C(e7d8a6f67f0c6e7), C(325c2413eb770ada), C(868e3315)}, - {C(c2ee3288be4fe2bf), C(c65d2f5ddf32b92), C(af6ecdf121ba5485), - C(c7cd48f7abf1fe59), C(ce600656ace6f53a), C(8a94a4381b108b34), - C(f9d1276c64bf59fb), C(c7cd48f7abf1fe59), C(ce600656ace6f53a), - C(8a94a4381b108b34), C(f9d1276c64bf59fb), C(219ce70ff5a112a5), - C(e6026c576e2d28d7), C(b8e467f25015e3a6), C(950cb904f37af710), - C(4639a426)}, + {C(dbbf541e9dfda0a), C(1479fceb6db4f844), C(31ab576b59062534), C(868e3315)}, + {C(c2ee3288be4fe2bf), C(c65d2f5ddf32b92), C(af6ecdf121ba5485), C(4639a426)}, {C(d86603ced1ed4730), C(f9de718aaada7709), C(db8b9755194c6535), - C(d803e1eead47604c), C(ad00f7611970a71b), C(bc50036b16ce71f5), - C(afba96210a2ca7d6), C(d803e1eead47604c), C(ad00f7611970a71b), - C(bc50036b16ce71f5), C(afba96210a2ca7d6), C(28f7a7be1d6765f0), - C(97bd888b93938c68), C(6ad41d1b407ded49), C(b9bfec098dc543e4), C(f3213646)}, {C(915263c671b28809), C(a815378e7ad762fd), C(abec6dc9b669f559), - C(d17c928c5342477f), C(745130b795254ad5), C(8c5db926fe88f8ba), - C(742a95c953e6d974), C(d17c928c5342477f), C(745130b795254ad5), - C(8c5db926fe88f8ba), C(742a95c953e6d974), C(279db8057b5d3e96), - C(98168411565b4ec4), C(50a72c54fa1125fa), C(27766a635db73638), C(17f148e9)}, - {C(2b67cdd38c307a5e), C(cb1d45bb5c9fe1c), C(800baf2a02ec18ad), - C(6531c1fe32bcb417), C(8c970d8df8cdbeb4), C(917ba5fc67e72b40), - C(4b65e4e263e0a426), C(6531c1fe32bcb417), C(8c970d8df8cdbeb4), - C(917ba5fc67e72b40), C(4b65e4e263e0a426), C(e0de33ce88a8b3a9), - C(f8ef98a437e16b08), C(a5162c0c7c5f7b62), C(dbdac43361b2b881), - C(bfd94880)}, + {C(2b67cdd38c307a5e), C(cb1d45bb5c9fe1c), C(800baf2a02ec18ad), C(bfd94880)}, {C(2d107419073b9cd0), C(a96db0740cef8f54), C(ec41ee91b3ecdc1b), - C(ffe319654c8e7ebc), C(6a67b8f13ead5a72), C(6dd10a34f80d532f), - C(6e9cfaece9fbca4), C(ffe319654c8e7ebc), C(6a67b8f13ead5a72), - C(6dd10a34f80d532f), C(6e9cfaece9fbca4), C(b4468eb6a30aa7e9), - C(e87995bee483222a), C(d036c2c90c609391), C(853306e82fa32247), C(bb1fa7f3)}, - {C(f3e9487ec0e26dfc), C(1ab1f63224e837fa), C(119983bb5a8125d8), - C(8950cfcf4bdf622c), C(8847dca82efeef2f), C(646b75b026708169), - C(21cab4b1687bd8b), C(8950cfcf4bdf622c), C(8847dca82efeef2f), - C(646b75b026708169), C(21cab4b1687bd8b), C(243b489a9eae6231), - C(5f3e634c4b779876), C(ff8abd1548eaf646), C(c7962f5f0151914b), C(88816b1)}, + {C(f3e9487ec0e26dfc), C(1ab1f63224e837fa), C(119983bb5a8125d8), C(88816b1)}, {C(1160987c8fe86f7d), C(879e6db1481eb91b), C(d7dcb802bfe6885d), - C(14453b5cc3d82396), C(4ef700c33ed278bc), C(1639c72ffc00d12e), - C(fb140ee6155f700d), C(14453b5cc3d82396), C(4ef700c33ed278bc), - C(1639c72ffc00d12e), C(fb140ee6155f700d), C(2e6b5c96a6620862), - C(a1f136998cbe19c), C(74e058a3b6c5a712), C(93dcf6bd33928b17), C(5c2faeb3)}, + C(5c2faeb3)}, {C(eab8112c560b967b), C(97f550b58e89dbae), C(846ed506d304051f), - C(276aa37744b5a028), C(8c10800ee90ea573), C(e6e57d2b33a1e0b7), - C(91f83563cd3b9dda), C(276aa37744b5a028), C(8c10800ee90ea573), - C(e6e57d2b33a1e0b7), C(91f83563cd3b9dda), C(afbb4739570738a1), - C(440ba98da5d8f69), C(fde4e9b0eda20350), C(e67dfa5a2138fa1), C(51b5fc6f)}, + C(51b5fc6f)}, {C(1addcf0386d35351), C(b5f436561f8f1484), C(85d38e22181c9bb1), - C(ff5c03f003c1fefe), C(e1098670afe7ff6), C(ea445030cf86de19), - C(f155c68b5c2967f8), C(ff5c03f003c1fefe), C(e1098670afe7ff6), - C(ea445030cf86de19), C(f155c68b5c2967f8), C(95d31b145dbb2e9e), - C(914fe1ca3deb3265), C(6066020b1358ccc1), C(c74bb7e2dee15036), C(33d94752)}, {C(d445ba84bf803e09), C(1216c2497038f804), C(2293216ea2237207), - C(e2164451c651adfb), C(b2534e65477f9823), C(4d70691a69671e34), - C(15be4963dbde8143), C(e2164451c651adfb), C(b2534e65477f9823), - C(4d70691a69671e34), C(15be4963dbde8143), C(762e75c406c5e9a3), - C(7b7579f7e0356841), C(480533eb066dfce5), C(90ae14ea6bfeb4ae), C(b0c92948)}, {C(37235a096a8be435), C(d9b73130493589c2), C(3b1024f59378d3be), - C(ad159f542d81f04e), C(49626a97a946096), C(d8d3998bf09fd304), - C(d127a411eae69459), C(ad159f542d81f04e), C(49626a97a946096), - C(d8d3998bf09fd304), C(d127a411eae69459), C(8f3253c4eb785a7b), - C(4049062f37e62397), C(b9fa04d3b670e5c1), C(1211a7967ac9350f), C(c7171590)}, {C(763ad6ea2fe1c99d), C(cf7af5368ac1e26b), C(4d5e451b3bb8d3d4), - C(3712eb913d04e2f2), C(2f9500d319c84d89), C(4ac6eb21a8cf06f9), - C(7d1917afcde42744), C(3712eb913d04e2f2), C(2f9500d319c84d89), - C(4ac6eb21a8cf06f9), C(7d1917afcde42744), C(6b58604b5dd10903), - C(c4288dfbc1e319fc), C(230f75ca96817c6e), C(8894cba3b763756c), C(240a67fb)}, {C(ea627fc84cd1b857), C(85e372494520071f), C(69ec61800845780b), - C(a3c1c5ca1b0367), C(eb6933997272bb3d), C(76a72cb62692a655), - C(140bb5531edf756e), C(a3c1c5ca1b0367), C(eb6933997272bb3d), - C(76a72cb62692a655), C(140bb5531edf756e), C(8d0d8067d1c925f4), - C(7b3fa56d8d77a10c), C(2bd00287b0946d88), C(f08c8e4bd65b8970), C(e1843cd5)}, {C(1f2ffd79f2cdc0c8), C(726a1bc31b337aaa), C(678b7f275ef96434), - C(5aa82bfaa99d3978), C(c18f96cade5ce18d), C(38404491f9e34c03), - C(891fb8926ba0418c), C(5aa82bfaa99d3978), C(c18f96cade5ce18d), - C(38404491f9e34c03), C(891fb8926ba0418c), C(e5f69a6398114c15), - C(7b8ded3623bc6b1d), C(2f3e5c5da5ff70e8), C(1ab142addea6a9ec), C(fda1452b)}, {C(39a9e146ec4b3210), C(f63f75802a78b1ac), C(e2e22539c94741c3), - C(8b305d532e61226e), C(caeae80da2ea2e), C(88a6289a76ac684e), - C(8ce5b5f9df1cbd85), C(8b305d532e61226e), C(caeae80da2ea2e), - C(88a6289a76ac684e), C(8ce5b5f9df1cbd85), C(8ae1fc4798e00d57), - C(e7164b8fb364fc46), C(6a978c9bd3a66943), C(ef10d5ae4dd08dc), C(a2cad330)}, + C(a2cad330)}, {C(74cba303e2dd9d6d), C(692699b83289fad1), C(dfb9aa7874678480), - C(751390a8a5c41bdc), C(6ee5fbf87605d34), C(6ca73f610f3a8f7c), - C(e898b3c996570ad), C(751390a8a5c41bdc), C(6ee5fbf87605d34), - C(6ca73f610f3a8f7c), C(e898b3c996570ad), C(98168a5858fc7110), - C(6f987fa27aa0daa2), C(f25e3e180d4b36a3), C(d0b03495aeb1be8a), C(53467e16)}, {C(4cbc2b73a43071e0), C(56c5db4c4ca4e0b7), C(1b275a162f46bd3d), - C(b87a326e413604bf), C(d8f9a5fa214b03ab), C(8a8bb8265771cf88), - C(a655319054f6e70f), C(b87a326e413604bf), C(d8f9a5fa214b03ab), - C(8a8bb8265771cf88), C(a655319054f6e70f), C(b499cb8e65a9af44), - C(bee7fafcc8307491), C(5d2e55fa9b27cda2), C(63b120f5fb2d6ee5), C(da14a8d0)}, {C(875638b9715d2221), C(d9ba0615c0c58740), C(616d4be2dfe825aa), - C(5df25f13ea7bc284), C(165edfaafd2598fb), C(af7215c5c718c696), - C(e9f2f9ca655e769), C(5df25f13ea7bc284), C(165edfaafd2598fb), - C(af7215c5c718c696), C(e9f2f9ca655e769), C(e459cfcb565d3d2d), - C(41d032631be2418a), C(c505db05fd946f60), C(54990394a714f5de), C(67333551)}, {C(fb686b2782994a8d), C(edee60693756bb48), C(e6bc3cae0ded2ef5), - C(58eb4d03b2c3ddf5), C(6d2542995f9189f1), C(c0beec58a5f5fea2), - C(ed67436f42e2a78b), C(58eb4d03b2c3ddf5), C(6d2542995f9189f1), - C(c0beec58a5f5fea2), C(ed67436f42e2a78b), C(dfec763cdb2b5193), - C(724a8d5345bd2d6), C(94d4fd1b81457c23), C(28e87c50cdede453), C(a0ebd66e)}, + C(a0ebd66e)}, {C(ab21d81a911e6723), C(4c31b07354852f59), C(835da384c9384744), - C(7f759dddc6e8549a), C(616dd0ca022c8735), C(94717ad4bc15ceb3), - C(f66c7be808ab36e), C(7f759dddc6e8549a), C(616dd0ca022c8735), - C(94717ad4bc15ceb3), C(f66c7be808ab36e), C(af8286b550b2f4b7), - C(745bd217d20a9f40), C(c73bfb9c5430f015), C(55e65922666e3fc2), C(4b769593)}, {C(33d013cc0cd46ecf), C(3de726423aea122c), C(116af51117fe21a9), - C(f271ba474edc562d), C(e6596e67f9dd3ebd), C(c0a288edf808f383), - C(b3def70681c6babc), C(f271ba474edc562d), C(e6596e67f9dd3ebd), - C(c0a288edf808f383), C(b3def70681c6babc), C(7da7864e9989b095), - C(bf2f8718693cd8a1), C(264a9144166da776), C(61ad90676870beb6), C(6aa75624)}, - {C(8ca92c7cd39fae5d), C(317e620e1bf20f1), C(4f0b33bf2194b97f), - C(45744afcf131dbee), C(97222392c2559350), C(498a19b280c6d6ed), - C(83ac2c36acdb8d49), C(45744afcf131dbee), C(97222392c2559350), - C(498a19b280c6d6ed), C(83ac2c36acdb8d49), C(7a69645c294daa62), - C(abe9d2be8275b3d2), C(39542019de371085), C(7f4efac8488cd6ad), - C(602a3f96)}, - {C(fdde3b03f018f43e), C(38f932946c78660), C(c84084ce946851ee), - C(b6dd09ba7851c7af), C(570de4e1bb13b133), C(c4e784eb97211642), - C(8285a7fcdcc7c58d), C(b6dd09ba7851c7af), C(570de4e1bb13b133), - C(c4e784eb97211642), C(8285a7fcdcc7c58d), C(d421f47990da899b), - C(8aed409c997eaa13), C(7a045929c2e29ccf), C(b373682a6202c86b), - C(cd183c4d)}, + {C(8ca92c7cd39fae5d), C(317e620e1bf20f1), C(4f0b33bf2194b97f), C(602a3f96)}, + {C(fdde3b03f018f43e), C(38f932946c78660), C(c84084ce946851ee), C(cd183c4d)}, {C(9c8502050e9c9458), C(d6d2a1a69964beb9), C(1675766f480229b5), - C(216e1d6c86cb524c), C(d01cf6fd4f4065c0), C(fffa4ec5b482ea0f), - C(a0e20ee6a5404ac1), C(216e1d6c86cb524c), C(d01cf6fd4f4065c0), - C(fffa4ec5b482ea0f), C(a0e20ee6a5404ac1), C(c1b037e4eebaf85e), - C(634e3d7c3ebf89eb), C(bcda972358c67d1), C(fd1352181e5b8578), C(960a4d07)}, + C(960a4d07)}, {C(348176ca2fa2fdd2), C(3a89c514cc360c2d), C(9f90b8afb318d6d0), - C(bceee07c11a9ac30), C(2e2d47dff8e77eb7), C(11a394cd7b6d614a), - C(1d7c41d54e15cb4a), C(bceee07c11a9ac30), C(2e2d47dff8e77eb7), - C(11a394cd7b6d614a), C(1d7c41d54e15cb4a), C(15baa5ae7312b0fc), - C(f398f596cc984635), C(8ab8fdf87a6788e8), C(b2b5c1234ab47e2), C(9ae998c4)}, + C(9ae998c4)}, {C(4a3d3dfbbaea130b), C(4e221c920f61ed01), C(553fd6cd1304531f), - C(bd2b31b5608143fe), C(ab717a10f2554853), C(293857f04d194d22), - C(d51be8fa86f254f0), C(bd2b31b5608143fe), C(ab717a10f2554853), - C(293857f04d194d22), C(d51be8fa86f254f0), C(1eee39e07686907e), - C(639039fe0e8d3052), C(d6ec1470cef97ff), C(370c82b860034f0f), C(74e2179d)}, + C(74e2179d)}, {C(b371f768cdf4edb9), C(bdef2ace6d2de0f0), C(e05b4100f7f1baec), - C(b9e0d415b4ebd534), C(c97c2a27efaa33d7), C(591cdb35f84ef9da), - C(a57d02d0e8e3756c), C(b9e0d415b4ebd534), C(c97c2a27efaa33d7), - C(591cdb35f84ef9da), C(a57d02d0e8e3756c), C(23f55f12d7c5c87b), - C(4c7ca0fe23221101), C(dbc3020480334564), C(d985992f32c236b1), C(ee9bae25)}, - {C(7a1d2e96934f61f), C(eb1760ae6af7d961), C(887eb0da063005df), - C(2228d6725e31b8ab), C(9b98f7e4d0142e70), C(b6a8c2115b8e0fe7), - C(b591e2f5ab9b94b1), C(2228d6725e31b8ab), C(9b98f7e4d0142e70), - C(b6a8c2115b8e0fe7), C(b591e2f5ab9b94b1), C(6c1feaa8065318e0), - C(4e7e2ca21c2e81fb), C(e9fe5d8ce7993c45), C(ee411fa2f12cf8df), - C(b66edf10)}, + {C(7a1d2e96934f61f), C(eb1760ae6af7d961), C(887eb0da063005df), C(b66edf10)}, {C(8be53d466d4728f2), C(86a5ac8e0d416640), C(984aa464cdb5c8bb), - C(87049e68f5d38e59), C(7d8ce44ec6bd7751), C(cc28d08ab414839c), - C(6c8f0bd34fe843e3), C(87049e68f5d38e59), C(7d8ce44ec6bd7751), - C(cc28d08ab414839c), C(6c8f0bd34fe843e3), C(b8496dcdc01f3e47), - C(2f03125c282ac26), C(82a8797ba3f5ef07), C(7c977a4d10bf52b8), C(d6209737)}, - {C(829677eb03abf042), C(43cad004b6bc2c0), C(f2f224756803971a), - C(98d0dbf796480187), C(fbcb5f3e1bef5742), C(5af2a0463bf6e921), - C(ad9555bf0120b3a3), C(98d0dbf796480187), C(fbcb5f3e1bef5742), - C(5af2a0463bf6e921), C(ad9555bf0120b3a3), C(283e39b3dc99f447), - C(bedaa1a4a0250c28), C(9d50546624ff9a57), C(4abaf523d1c090f6), C(b994a88)}, - {C(754435bae3496fc), C(5707fc006f094dcf), C(8951c86ab19d8e40), - C(57c5208e8f021a77), C(f7653fbb69cd9276), C(a484410af21d75cb), - C(f19b6844b3d627e8), C(57c5208e8f021a77), C(f7653fbb69cd9276), - C(a484410af21d75cb), C(f19b6844b3d627e8), C(f37400fc3ffd9514), - C(36ae0d821734edfd), C(5f37820af1f1f306), C(be637d40e6a5ad0), C(a05d43c0)}, + C(d6209737)}, + {C(829677eb03abf042), C(43cad004b6bc2c0), C(f2f224756803971a), C(b994a88)}, + {C(754435bae3496fc), C(5707fc006f094dcf), C(8951c86ab19d8e40), C(a05d43c0)}, {C(fda9877ea8e3805f), C(31e868b6ffd521b7), C(b08c90681fb6a0fd), - C(68110a7f83f5d3ff), C(6d77e045901b85a8), C(84ef681113036d8b), - C(3b9f8e3928f56160), C(68110a7f83f5d3ff), C(6d77e045901b85a8), - C(84ef681113036d8b), C(3b9f8e3928f56160), C(fc8b7f56c130835), - C(a11f3e800638e841), C(d9572267f5cf28c1), C(7897c8149803f2aa), C(c79f73a8)}, {C(2e36f523ca8f5eb5), C(8b22932f89b27513), C(331cd6ecbfadc1bb), - C(d1bfe4df12b04cbf), C(f58c17243fd63842), C(3a453cdba80a60af), - C(5737b2ca7470ea95), C(d1bfe4df12b04cbf), C(f58c17243fd63842), - C(3a453cdba80a60af), C(5737b2ca7470ea95), C(54d44a3f4477030c), - C(8168e02d4869aa7f), C(77f383a17778559d), C(95e1737d77a268fc), C(a490aff5)}, {C(21a378ef76828208), C(a5c13037fa841da2), C(506d22a53fbe9812), - C(61c9c95d91017da5), C(16f7c83ba68f5279), C(9c0619b0808d05f7), - C(83c117ce4e6b70a3), C(61c9c95d91017da5), C(16f7c83ba68f5279), - C(9c0619b0808d05f7), C(83c117ce4e6b70a3), C(cfb4c8af7fd01413), - C(fdef04e602e72296), C(ed6124d337889b1), C(4919c86707b830da), C(dfad65b4)}, - {C(ccdd5600054b16ca), C(f78846e84204cb7b), C(1f9faec82c24eac9), - C(58634004c7b2d19a), C(24bb5f51ed3b9073), C(46409de018033d00), - C(4a9805eed5ac802e), C(58634004c7b2d19a), C(24bb5f51ed3b9073), - C(46409de018033d00), C(4a9805eed5ac802e), C(e18de8db306baf82), - C(46bbf75f1fa025ff), C(5faf2fb09be09487), C(3fbc62bd4e558fb3), C(1d07dfb)}, + C(dfad65b4)}, + {C(ccdd5600054b16ca), C(f78846e84204cb7b), C(1f9faec82c24eac9), C(1d07dfb)}, {C(7854468f4e0cabd0), C(3a3f6b4f098d0692), C(ae2423ec7799d30d), - C(29c3529eb165eeba), C(443de3703b657c35), C(66acbce31ae1bc8d), - C(1acc99effe1d547e), C(29c3529eb165eeba), C(443de3703b657c35), - C(66acbce31ae1bc8d), C(1acc99effe1d547e), C(cf07f8a57906573d), - C(31bafb0bbb9a86e7), C(40c69492702a9346), C(7df61fdaa0b858af), C(416df9a0)}, {C(7f88db5346d8f997), C(88eac9aacc653798), C(68a4d0295f8eefa1), - C(ae59ca86f4c3323d), C(25906c09906d5c4c), C(8dd2aa0c0a6584ae), - C(232a7d96b38f40e9), C(ae59ca86f4c3323d), C(25906c09906d5c4c), - C(8dd2aa0c0a6584ae), C(232a7d96b38f40e9), C(8986ee00a2ed0042), - C(c49ae7e428c8a7d1), C(b7dd8280713ac9c2), C(e018720aed1ebc28), C(1f8fb9cc)}, {C(bb3fb5fb01d60fcf), C(1b7cc0847a215eb6), C(1246c994437990a1), - C(d4edc954c07cd8f3), C(224f47e7c00a30ab), C(d5ad7ad7f41ef0c6), - C(59e089281d869fd7), C(d4edc954c07cd8f3), C(224f47e7c00a30ab), - C(d5ad7ad7f41ef0c6), C(59e089281d869fd7), C(f29340d07a14b6f1), - C(c87c5ef76d9c4ef3), C(463118794193a9a), C(2922dcb0540f0dbc), C(7abf48e3)}, + C(7abf48e3)}, {C(2e783e1761acd84d), C(39158042bac975a0), C(1cd21c5a8071188d), - C(b1b7ec44f9302176), C(5cb476450dc0c297), C(dc5ef652521ef6a2), - C(3cc79a9e334e1f84), C(b1b7ec44f9302176), C(5cb476450dc0c297), - C(dc5ef652521ef6a2), C(3cc79a9e334e1f84), C(769e2a283dbcc651), - C(9f24b105c8511d3f), C(c31c15575de2f27e), C(ecfecf32c3ae2d66), C(dea4e3dd)}, {C(392058251cf22acc), C(944ec4475ead4620), C(b330a10b5cb94166), - C(54bc9bee7cbe1767), C(485820bdbe442431), C(54d6120ea2972e90), - C(f437a0341f29b72a), C(54bc9bee7cbe1767), C(485820bdbe442431), - C(54d6120ea2972e90), C(f437a0341f29b72a), C(8f30885c784d5704), - C(aa95376b16c7906a), C(e826928cfaf93dc3), C(20e8f54d1c16d7d8), C(c6064f22)}, - {C(adf5c1e5d6419947), C(2a9747bc659d28aa), C(95c5b8cb1f5d62c), - C(80973ea532b0f310), C(a471829aa9c17dd9), C(c2ff3479394804ab), - C(6bf44f8606753636), C(80973ea532b0f310), C(a471829aa9c17dd9), - C(c2ff3479394804ab), C(6bf44f8606753636), C(5184d2973e6dd827), - C(121b96369a332d9a), C(5c25d3475ab69e50), C(26d2961d62884168), - C(743bed9c)}, + {C(adf5c1e5d6419947), C(2a9747bc659d28aa), C(95c5b8cb1f5d62c), C(743bed9c)}, {C(6bc1db2c2bee5aba), C(e63b0ed635307398), C(7b2eca111f30dbbc), - C(230d2b3e47f09830), C(ec8624a821c1caf4), C(ea6ec411cdbf1cb1), - C(5f38ae82af364e27), C(230d2b3e47f09830), C(ec8624a821c1caf4), - C(ea6ec411cdbf1cb1), C(5f38ae82af364e27), C(a519ef515ea7187c), - C(6bad5efa7ebae05f), C(748abacb11a74a63), C(a28eef963d1396eb), C(fce254d5)}, {C(b00f898229efa508), C(83b7590ad7f6985c), C(2780e70a0592e41d), - C(7122413bdbc94035), C(e7f90fae33bf7763), C(4b6bd0fb30b12387), - C(557359c0c44f48ca), C(7122413bdbc94035), C(e7f90fae33bf7763), - C(4b6bd0fb30b12387), C(557359c0c44f48ca), C(d5656c3d6bc5f0d), - C(983ff8e5e784da99), C(628479671b445bf), C(e179a1e27ce68f5d), C(e47ec9d1)}, + C(e47ec9d1)}, {C(b56eb769ce0d9a8c), C(ce196117bfbcaf04), C(b26c3c3797d66165), - C(5ed12338f630ab76), C(fab19fcb319116d), C(167f5f42b521724b), - C(c4aa56c409568d74), C(5ed12338f630ab76), C(fab19fcb319116d), - C(167f5f42b521724b), C(c4aa56c409568d74), C(75fff4b42f8e9778), - C(94218f94710c1ea3), C(b7b05efb738b06a6), C(83fff2deabf9cd3), C(334a145c)}, + C(334a145c)}, {C(70c0637675b94150), C(259e1669305b0a15), C(46e1dd9fd387a58d), - C(fca4e5bc9292788e), C(cd509dc1facce41c), C(bbba575a59d82fe), - C(4e2e71c15b45d4d3), C(fca4e5bc9292788e), C(cd509dc1facce41c), - C(bbba575a59d82fe), C(4e2e71c15b45d4d3), C(5dc54582ead999c), - C(72612d1571963c6f), C(30318a9d2d3d1829), C(785dd00f4cc9c9a0), C(adec1e3c)}, {C(74c0b8a6821faafe), C(abac39d7491370e7), C(faf0b2a48a4e6aed), - C(967e970df9673d2a), C(d465247cffa415c0), C(33a1df0ca1107722), - C(49fc2a10adce4a32), C(967e970df9673d2a), C(d465247cffa415c0), - C(33a1df0ca1107722), C(49fc2a10adce4a32), C(c5707e079a284308), - C(573028266635dda6), C(f786f5eee6127fa0), C(b30d79cebfb51266), C(f6a9fbf8)}, {C(5fb5e48ac7b7fa4f), C(a96170f08f5acbc7), C(bbf5c63d4f52a1e5), - C(6cc09e60700563e9), C(d18f23221e964791), C(ffc23eeef7af26eb), - C(693a954a3622a315), C(815308a32a9b0daf), C(efb2ab27bf6fd0bd), - C(9f1ffc0986111118), C(f9a3aa1778ea3985), C(698fe54b2b93933b), - C(dacc2b28404d0f10), C(815308a32a9b0daf), C(efb2ab27bf6fd0bd), C(5398210c)}, }; void TestUnchanging(const uint64_t* expected, int offset, int len) { - const uint128 u = CityHash128(data + offset, len); - const uint128 v = CityHash128WithSeed(data + offset, len, kSeed128); EXPECT_EQ(expected[0], CityHash64(data + offset, len)); - EXPECT_EQ(expected[15], CityHash32(data + offset, len)); + EXPECT_EQ(expected[3], CityHash32(data + offset, len)); EXPECT_EQ(expected[1], CityHash64WithSeed(data + offset, len, kSeed0)); EXPECT_EQ(expected[2], CityHash64WithSeeds(data + offset, len, kSeed0, kSeed1)); - EXPECT_EQ(expected[3], Uint128Low64(u)); - EXPECT_EQ(expected[4], Uint128High64(u)); - EXPECT_EQ(expected[5], Uint128Low64(v)); - EXPECT_EQ(expected[6], Uint128High64(v)); -#ifdef __SSE4_2__ - const uint128 y = CityHashCrc128(data + offset, len); - const uint128 z = CityHashCrc128WithSeed(data + offset, len, kSeed128); - uint64_t crc256_results[4]; - CityHashCrc256(data + offset, len, crc256_results); - EXPECT_EQ(expected[7], Uint128Low64(y)); - EXPECT_EQ(expected[8], Uint128High64(y)); - EXPECT_EQ(expected[9], Uint128Low64(z)); - EXPECT_EQ(expected[10], Uint128High64(z)); - for (int i = 0; i < 4; i++) { - EXPECT_EQ(expected[11 + i], crc256_results[i]); - } -#endif } TEST(CityHashTest, Unchanging) { diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index aa14e211..ea9a3a17 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -605,35 +605,21 @@ TEST_F(ParsedFormatTest, RegressionMixPositional) { // Some codegen thunks that we can use to easily dump the generated assembly for // different StrFormat calls. -inline std::string CodegenAbslStrFormatInt(int i) { +std::string CodegenAbslStrFormatInt(int i) { // NOLINT return absl::StrFormat("%d", i); } -inline std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, - int64_t i64) { +std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, + int64_t i64) { // NOLINT return absl::StrFormat("%d %s %d", i, s, i64); } -inline void CodegenAbslStrAppendFormatInt(std::string* out, int i) { +void CodegenAbslStrAppendFormatInt(std::string* out, int i) { // NOLINT absl::StrAppendFormat(out, "%d", i); } -inline void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, +void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, const std::string& s, - int64_t i64) { + int64_t i64) { // NOLINT absl::StrAppendFormat(out, "%d %s %d", i, s, i64); } - -auto absl_internal_str_format_force_codegen_funcs = std::make_tuple( - CodegenAbslStrFormatInt, CodegenAbslStrFormatIntStringInt64, - CodegenAbslStrAppendFormatInt, CodegenAbslStrAppendFormatIntStringInt64); - -bool absl_internal_str_format_force_codegen_always_false; -// Force the compiler to generate the functions by making it look like we -// escape the function pointers. -// It can't statically know that -// absl_internal_str_format_force_codegen_always_false is not changed by someone -// else. -bool absl_internal_str_format_force_codegen = - absl_internal_str_format_force_codegen_always_false && - printf("%p", &absl_internal_str_format_force_codegen_funcs) == 0; diff --git a/absl/time/BUILD.bazel b/absl/time/BUILD.bazel index c7c16d43..969ddd2e 100644 --- a/absl/time/BUILD.bazel +++ b/absl/time/BUILD.bazel @@ -27,6 +27,7 @@ licenses(["notice"]) # Apache 2.0 cc_library( name = "time", srcs = [ + "civil_time.cc", "clock.cc", "duration.cc", "format.cc", @@ -35,6 +36,7 @@ cc_library( "time.cc", ], hdrs = [ + "civil_time.h", "clock.h", "time.h", ], @@ -72,10 +74,10 @@ cc_library( cc_test( name = "time_test", srcs = [ + "civil_time_test.cc", "clock_test.cc", "duration_test.cc", "format_test.cc", - "time_norm_test.cc", "time_test.cc", "time_zone_test.cc", ], @@ -94,6 +96,7 @@ cc_test( cc_test( name = "time_benchmark", srcs = [ + "civil_time_benchmark.cc", "clock_benchmark.cc", "duration_benchmark.cc", "format_benchmark.cc", diff --git a/absl/time/CMakeLists.txt b/absl/time/CMakeLists.txt index 06272364..53216cda 100644 --- a/absl/time/CMakeLists.txt +++ b/absl/time/CMakeLists.txt @@ -15,6 +15,7 @@ # list(APPEND TIME_PUBLIC_HEADERS + "civil_time.h" "clock.h" "time.h" ) @@ -29,6 +30,7 @@ list(APPEND TIME_INTERNAL_HEADERS ) list(APPEND TIME_SRC + "civil_time.cc" "time.cc" "clock.cc" "duration.cc" @@ -74,11 +76,11 @@ absl_library( # test time_test list(APPEND TIME_TEST_SRC + "civil_time_test.cc" "time_test.cc" "clock_test.cc" "duration_test.cc" "format_test.cc" - "time_norm_test.cc" "time_test.cc" "time_zone_test.cc" "internal/test_util.cc" diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc new file mode 100644 index 00000000..56541799 --- /dev/null +++ b/absl/time/civil_time.cc @@ -0,0 +1,88 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/time/civil_time.h" + +#include +#include + +#include "absl/strings/str_cat.h" +#include "absl/time/time.h" + +namespace absl { + +namespace { + +// Since a civil time has a larger year range than absl::Time (64-bit years vs +// 64-bit seconds, respectively) we normalize years to roughly +/- 400 years +// around the year 2400, which will produce an equivalent year in a range that +// absl::Time can handle. +inline civil_year_t NormalizeYear(civil_year_t year) { + return 2400 + year % 400; +} + +// Formats the given CivilSecond according to the given format. +std::string FormatYearAnd(string_view fmt, CivilSecond cs) { + const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(), + cs.hour(), cs.minute(), cs.second()); + const TimeZone utc = UTCTimeZone(); + // TODO(absl-team): Avoid conversion of fmt std::string. + return StrCat(cs.year(), FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); +} + +} // namespace + +std::string FormatCivilTime(CivilSecond c) { + return FormatYearAnd("-%m-%dT%H:%M:%S", c); +} +std::string FormatCivilTime(CivilMinute c) { + return FormatYearAnd("-%m-%dT%H:%M", c); +} +std::string FormatCivilTime(CivilHour c) { + return FormatYearAnd("-%m-%dT%H", c); +} +std::string FormatCivilTime(CivilDay c) { + return FormatYearAnd("-%m-%d", c); +} +std::string FormatCivilTime(CivilMonth c) { + return FormatYearAnd("-%m", c); +} +std::string FormatCivilTime(CivilYear c) { + return FormatYearAnd("", c); +} + +namespace time_internal { + +std::ostream& operator<<(std::ostream& os, CivilYear y) { + return os << FormatCivilTime(y); +} +std::ostream& operator<<(std::ostream& os, CivilMonth m) { + return os << FormatCivilTime(m); +} +std::ostream& operator<<(std::ostream& os, CivilDay d) { + return os << FormatCivilTime(d); +} +std::ostream& operator<<(std::ostream& os, CivilHour h) { + return os << FormatCivilTime(h); +} +std::ostream& operator<<(std::ostream& os, CivilMinute m) { + return os << FormatCivilTime(m); +} +std::ostream& operator<<(std::ostream& os, CivilSecond s) { + return os << FormatCivilTime(s); +} + +} // namespace time_internal + +} // namespace absl diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h new file mode 100644 index 00000000..a1f55305 --- /dev/null +++ b/absl/time/civil_time.h @@ -0,0 +1,487 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// File: civil_time.h +// ----------------------------------------------------------------------------- +// +// This header file defines abstractions for computing with "civil time". +// The term "civil time" refers to the legally recognized human-scale time +// that is represented by the six fields `YYYY-MM-DD hh:mm:ss`. A "date" +// is perhaps the most common example of a civil time (represented here as +// an `absl::CivilDay`). +// +// Modern-day civil time follows the Gregorian Calendar and is a +// time-zone-independent concept: a civil time of "2015-06-01 12:00:00", for +// example, is not tied to a time zone. Put another way, a civil time does not +// map to a unique point in time; a civil time must be mapped to an absolute +// time *through* a time zone. +// +// Because a civil time is what most people think of as "time," it is common to +// map absolute times to civil times to present to users. +// +// Time zones define the relationship between absolute and civil times. Given an +// absolute or civil time and a time zone, you can compute the other time: +// +// Civil Time = F(Absolute Time, Time Zone) +// Absolute Time = G(Civil Time, Time Zone) +// +// The Abseil time library allows you to construct such civil times from +// absolute times; consult time.h for such functionality. +// +// This library provides six classes for constructing civil-time objects, and +// provides several helper functions for rounding, iterating, and performing +// arithmetic on civil-time objects, while avoiding complications like +// daylight-saving time (DST): +// +// * `absl::CivilSecond` +// * `absl::CivilMinute` +// * `absl::CivilHour` +// * `absl::CivilDay` +// * `absl::CivilMonth` +// * `absl::CivilYear` +// +// Example: +// +// // Construct a civil-time object for a specific day +// const absl::CivilDay cd(1969, 07, 20); +// +// // Construct a civil-time object for a specific second +// const absl::CivilSecond cd(2018, 8, 1, 12, 0, 1); +// +// Note: In C++14 and later, this library is usable in a constexpr context. +// +// Example: +// +// // Valid in C++14 +// constexpr absl::CivilDay cd(1969, 07, 20); +// + +#ifndef ABSL_TIME_CIVIL_TIME_H_ +#define ABSL_TIME_CIVIL_TIME_H_ + +#include + +#include "absl/base/port.h" // Needed for string vs std::string +#include "absl/strings/string_view.h" +#include "absl/time/internal/cctz/include/cctz/civil_time.h" + +namespace absl { + +namespace time_internal { +struct second_tag : cctz::detail::second_tag {}; +struct minute_tag : second_tag, cctz::detail::minute_tag {}; +struct hour_tag : minute_tag, cctz::detail::hour_tag {}; +struct day_tag : hour_tag, cctz::detail::day_tag {}; +struct month_tag : day_tag, cctz::detail::month_tag {}; +struct year_tag : month_tag, cctz::detail::year_tag {}; +} // namespace time_internal + +// ----------------------------------------------------------------------------- +// CivilSecond, CivilMinute, CivilHour, CivilDay, CivilMonth, CivilYear +// ----------------------------------------------------------------------------- +// +// Each of these civil-time types is a simple value type with the same +// interface for construction and the same six accessors for each of the civil +// time fields (year, month, day, hour, minute, and second, aka YMDHMS). These +// classes differ only in their alignment, which is indicated by the type name +// and specifies the field on which arithmetic operates. +// +// CONSTRUCTION +// +// Each of the civil-time types can be constructed in two ways: by directly +// passing to the constructor up to six integers representing the YMDHMS fields, +// or by copying the YMDHMS fields from a differently aligned civil-time type. +// Omitted fields are assigned their minimum valid value. Hours, minutes, and +// seconds will be set to 0, month and day will be set to 1. Since there is no +// minimum year, the default is 1970. +// +// Examples: +// +// absl::CivilDay default_value; // 1970-01-01 00:00:00 +// +// absl::CivilDay a(2015, 2, 3); // 2015-02-03 00:00:00 +// absl::CivilDay b(2015, 2, 3, 4, 5, 6); // 2015-02-03 00:00:00 +// absl::CivilDay c(2015); // 2015-01-01 00:00:00 +// +// absl::CivilSecond ss(2015, 2, 3, 4, 5, 6); // 2015-02-03 04:05:06 +// absl::CivilMinute mm(ss); // 2015-02-03 04:05:00 +// absl::CivilHour hh(mm); // 2015-02-03 04:00:00 +// absl::CivilDay d(hh); // 2015-02-03 00:00:00 +// absl::CivilMonth m(d); // 2015-02-01 00:00:00 +// absl::CivilYear y(m); // 2015-01-01 00:00:00 +// +// m = absl::CivilMonth(y); // 2015-01-01 00:00:00 +// d = absl::CivilDay(m); // 2015-01-01 00:00:00 +// hh = absl::CivilHour(d); // 2015-01-01 00:00:00 +// mm = absl::CivilMinute(hh); // 2015-01-01 00:00:00 +// ss = absl::CivilSecond(mm); // 2015-01-01 00:00:00 +// +// Each civil-time class is aligned to the civil-time field indicated in the +// class's name after normalization. Alignment is performed by setting all the +// inferior fields to their minimum valid value (as described above). The +// following are examples of how each of the six types would align the fields +// representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the +// string format used here is not important; it's just a shorthand way of +// showing the six YMDHMS fields.) +// +// absl::CivilSecond : 2015-11-22 12:34:56 +// absl::CivilMinute : 2015-11-22 12:34:00 +// absl::CivilHour : 2015-11-22 12:00:00 +// absl::CivilDay : 2015-11-22 00:00:00 +// absl::CivilMonth : 2015-11-01 00:00:00 +// absl::CivilYear : 2015-01-01 00:00:00 +// +// Each civil-time type performs arithmetic on the field to which it is +// aligned. This means that adding 1 to an absl::CivilDay increments the day +// field (normalizing as necessary), and subtracting 7 from an absl::CivilMonth +// operates on the month field (normalizing as necessary). All arithmetic +// produces a valid civil time. Difference requires two similarly aligned +// civil-time objects and returns the scalar answer in units of the objects' +// alignment. For example, the difference between two absl::CivilHour objects +// will give an answer in units of civil hours. +// +// ALIGNMENT CONVERSION +// +// The alignment of a civil-time object cannot change, but the object may be +// used to construct a new object with a different alignment. This is referred +// to as "realigning". When realigning to a type with the same or more +// precision (e.g., absl::CivilDay -> absl::CivilSecond), the conversion may be +// performed implicitly since no information is lost. However, if information +// could be discarded (e.g., CivilSecond -> CivilDay), the conversion must +// be explicit at the call site. +// +// Examples: +// +// void UseDay(absl::CivilDay day); +// +// absl::CivilSecond cs; +// UseDay(cs); // Won't compile because data may be discarded +// UseDay(absl::CivilDay(cs)); // OK: explicit conversion +// +// absl::CivilDay cd; +// UseDay(cd); // OK: no conversion needed +// +// absl::CivilMonth cm; +// UseDay(cm); // OK: implicit conversion to absl::CivilDay +// +// NORMALIZATION +// +// Normalization takes invalid values and adjusts them to produce valid values. +// Within the civil-time library, integer arguments passed to the Civil* +// constructors may be out-of-range, in which case they are normalized by +// carrying overflow into a field of courser granularity to produce valid +// civil-time objects. This normalization enables natural arithmetic on +// constructor arguments without worrying about the field's range. +// +// Examples: +// +// // Out-of-range; normalized to 2016-11-01 +// absl::CivilDay d(2016, 10, 32); +// // Out-of-range, negative: normalized to 2016-10-30T23 +// absl::CivilHour h1(2016, 10, 31, -1); +// // Normalization is cumulative: normalized to 2016-10-30T23 +// absl::CivilHour h2(2016, 10, 32, -25); +// +// Note: If normalization is undesired, you can signal an error by comparing +// the constructor arguments to the normalized values returned by the YMDHMS +// properties. +// +// COMPARISON +// +// Comparison between civil-time objects considers all six YMDHMS fields, +// regardless of the type's alignment. Comparison between differently aligned +// civil-time types is allowed. +// +// Examples: +// +// absl::CivilDay feb_3(2015, 2, 3); // 2015-02-03 00:00:00 +// absl::CivilDay mar_4(2015, 3, 4); // 2015-03-04 00:00:00 +// // feb_3 < mar_4 +// // absl::CivilYear(feb_3) == absl::CivilYear(mar_4) +// +// absl::CivilSecond feb_3_noon(2015, 2, 3, 12, 0, 0); // 2015-02-03 12:00:00 +// // feb_3 < feb_3_noon +// // feb_3 == absl::CivilDay(feb_3_noon) +// +// // Iterates all the days of February 2015. +// for (absl::CivilDay d(2015, 2, 1); d < absl::CivilMonth(2015, 3); ++d) { +// // ... +// } +// +// ARITHMETIC +// +// Civil-time types support natural arithmetic operators such as addition, +// subtraction, and difference. Arithmetic operates on the civil-time field +// indicated in the type's name. Difference operators require arguments with +// the same alignment and return the answer in units of the alignment. +// +// Example: +// +// absl::CivilDay a(2015, 2, 3); +// ++a; // 2015-02-04 00:00:00 +// --a; // 2015-02-03 00:00:00 +// absl::CivilDay b = a + 1; // 2015-02-04 00:00:00 +// absl::CivilDay c = 1 + b; // 2015-02-05 00:00:00 +// int n = c - a; // n = 2 (civil days) +// int m = c - absl::CivilMonth(c); // Won't compile: different types. +// +// ACCESSORS +// +// Each civil-time type has accessors for all six of the civil-time fields: +// year, month, day, hour, minute, and second. +// +// civil_year_t year() +// int month() +// int day() +// int hour() +// int minute() +// int second() +// +// Recall that fields inferior to the type's aligment will be set to their +// minimum valid value. +// +// Example: +// +// absl::CivilDay d(2015, 6, 28); +// // d.year() == 2015 +// // d.month() == 6 +// // d.day() == 28 +// // d.hour() == 0 +// // d.minute() == 0 +// // d.second() == 0 +// +// CASE STUDY: Adding a month to January 31. +// +// One of the classic questions that arises when considering a civil time +// library (or a date library or a date/time library) is this: +// "What is the result of adding a month to January 31?" +// This is an interesting question because it is unclear what is meant by a +// "month", and several different answers are possible, depending on context: +// +// 1. March 3 (or 2 if a leap year), if "add a month" means to add a month to +// the current month, and adjust the date to overflow the extra days into +// March. In this case the result of "February 31" would be normalized as +// within the civil-time library. +// 2. February 28 (or 29 if a leap year), if "add a month" means to add a +// month, and adjust the date while holding the resulting month constant. +// In this case, the result of "February 31" would be truncated to the last +// day in February. +// 3. An error. The caller may get some error, an exception, an invalid date +// object, or perhaps return `false`. This may make sense because there is +// no single unambiguously correct answer to the question. +// +// Practically speaking, any answer that is not what the programmer intended +// is the wrong answer. +// +// The Abseil time library avoids this problem by making it impossible to +// ask ambiguous questions. All civil-time objects are aligned to a particular +// civil-field boundary (such as aligned to a year, month, day, hour, minute, +// or second), and arithmetic operates on the field to which the object is +// aligned. This means that in order to "add a month" the object must first be +// aligned to a month boundary, which is equivalent to the first day of that +// month. +// +// Of course, there are ways to compute an answer the question at hand using +// this Abseil time library, but they require the programmer to be explicit +// about the answer they expect. To illustrate, let's see how to compute all +// three of the above possible answers to the question of "Jan 31 plus 1 +// month": +// +// Example: +// +// const absl::CivilDay d(2015, 1, 31); +// +// // Answer 1: +// // Add 1 to the month field in the constructor, and rely on normalization. +// const auto normalized = absl::CivilDay(d.year(), d.month() + 1, d.day()); +// // normalized == 2015-03-03 (aka Feb 31) +// +// // Answer 2: +// // Add 1 to month field, capping to the end of next month. +// const auto next_month = absl::CivilMonth(d) + 1; +// const auto last_day_of_next_month = absl::CivilDay(next_month + 1) - 1; +// const auto capped = std::min(normalized, last_day_of_next_month); +// // capped == 2015-02-28 +// +// // Answer 3: +// // Signal an error if the normalized answer is not in next month. +// if (absl::CivilMonth(normalized) != next_month) { +// // error, month overflow +// } +// +using CivilSecond = + time_internal::cctz::detail::civil_time; +using CivilMinute = + time_internal::cctz::detail::civil_time; +using CivilHour = + time_internal::cctz::detail::civil_time; +using CivilDay = + time_internal::cctz::detail::civil_time; +using CivilMonth = + time_internal::cctz::detail::civil_time; +using CivilYear = + time_internal::cctz::detail::civil_time; + +// civil_year_t +// +// Type alias of a civil-time year value. This type is guaranteed to (at least) +// support any year value supported by `time_t`. +// +// Example: +// +// absl::CivilSecond cs = ...; +// absl::civil_year_t y = cs.year(); +// cs = absl::CivilSecond(y, 1, 1, 0, 0 0); // CivilSecond(CivilYear(cs)) +// +using civil_year_t = time_internal::cctz::year_t; + +// civil_diff_t +// +// Type alias of the difference between two civil-time values. +// This type is used to indicate arguments that are not +// normalized (such as parameters to the civil-time constructors), the results +// of civil-time subtraction, or the operand to civil-time addition. +// +// Example: +// +// absl::civil_diff_t n_sec = cs1 - cs2; // cs1 == cs2 + n_sec; +// +using civil_diff_t = time_internal::cctz::diff_t; + +// Weekday::monday, Weekday::tuesday, Weekday::wednesday, Weekday::thursday, +// Weekday::friday, Weekday::saturday, Weekday::sunday +// +// The Weekday enum class represents the civil-time concept of a "weekday" with +// members for all days of the week. +// +// absl::Weekday wd = absl::Weekday::thursday; +// +using Weekday = time_internal::cctz::weekday; + +// GetWeekday() +// +// Returns the absl::Weekday for the given absl::CivilDay. +// +// Example: +// +// absl::CivilDay a(2015, 8, 13); +// absl::Weekday wd = absl::GetWeekday(a); // wd == absl::Weekday::thursday +// +inline Weekday GetWeekday(CivilDay cd) { + return time_internal::cctz::get_weekday(cd); +} + +// NextWeekday() +// PrevWeekday() +// +// Returns the absl::CivilDay that strictly follows or precedes a given +// absl::CivilDay, and that falls on the given absl::Weekday. +// +// Example, given the following month: +// +// August 2015 +// Su Mo Tu We Th Fr Sa +// 1 +// 2 3 4 5 6 7 8 +// 9 10 11 12 13 14 15 +// 16 17 18 19 20 21 22 +// 23 24 25 26 27 28 29 +// 30 31 +// +// absl::CivilDay a(2015, 8, 13); +// // absl::GetWeekday(a) == absl::Weekday::thursday +// absl::CivilDay b = absl::NextWeekday(a, absl::Weekday::thursday); +// // b = 2015-08-20 +// absl::CivilDay c = absl::PrevWeekday(a, absl::Weekday::thursday); +// // c = 2015-08-06 +// +// absl::CivilDay d = ... +// // Gets the following Thursday if d is not already Thursday +// absl::CivilDay thurs1 = absl::PrevWeekday(d, absl::Weekday::thursday) + 7; +// // Gets the previous Thursday if d is not already Thursday +// absl::CivilDay thurs2 = absl::NextWeekday(d, absl::Weekday::thursday) - 7; +// +inline CivilDay NextWeekday(CivilDay cd, Weekday wd) { + return CivilDay(time_internal::cctz::next_weekday(cd, wd)); +} +inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) { + return CivilDay(time_internal::cctz::prev_weekday(cd, wd)); +} + +// GetYearDay() +// +// Returns the day-of-year for the given absl::CivilDay. +// +// Example: +// +// absl::CivilDay a(2015, 1, 1); +// int yd_jan_1 = absl::GetYearDay(a); // yd_jan_1 = 1 +// absl::CivilDay b(2015, 12, 31); +// int yd_dec_31 = absl::GetYearDay(b); // yd_dec_31 = 365 +// +inline int GetYearDay(CivilDay cd) { + return time_internal::cctz::get_yearday(cd); +} + +// FormatCivilTime() +// +// Formats the given civil-time value into a string value of the following +// format: +// +// Type | Format +// --------------------------------- +// CivilSecond | YYYY-MM-DDTHH:MM:SS +// CivilMinute | YYYY-MM-DDTHH:MM +// CivilHour | YYYY-MM-DDTHH +// CivilDay | YYYY-MM-DD +// CivilMonth | YYYY-MM +// CivilYear | YYYY +// +// Example: +// +// absl::CivilDay d = absl::CivilDay(1969, 7, 20); +// string day_string = absl::FormatCivilTime(d); // "1969-07-20" +// +std::string FormatCivilTime(CivilSecond c); +std::string FormatCivilTime(CivilMinute c); +std::string FormatCivilTime(CivilHour c); +std::string FormatCivilTime(CivilDay c); +std::string FormatCivilTime(CivilMonth c); +std::string FormatCivilTime(CivilYear c); + +namespace time_internal { // For functions found via ADL on civil-time tags. + +// Streaming Operators +// +// Each civil-time type may be sent to an output stream using operator<<(). +// The result matches the string produced by `FormatCivilTime()`. +// +// Example: +// +// absl::CivilDay d = absl::CivilDay("1969-07-20"); +// std::cout << "Date is: " << d << "\n"; +// +std::ostream& operator<<(std::ostream& os, CivilYear y); +std::ostream& operator<<(std::ostream& os, CivilMonth m); +std::ostream& operator<<(std::ostream& os, CivilDay d); +std::ostream& operator<<(std::ostream& os, CivilHour h); +std::ostream& operator<<(std::ostream& os, CivilMinute m); +std::ostream& operator<<(std::ostream& os, CivilSecond s); + +} // namespace time_internal + +} // namespace absl + +#endif // ABSL_TIME_CIVIL_TIME_H_ diff --git a/absl/time/civil_time_benchmark.cc b/absl/time/civil_time_benchmark.cc new file mode 100644 index 00000000..567c2a33 --- /dev/null +++ b/absl/time/civil_time_benchmark.cc @@ -0,0 +1,57 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/time/civil_time.h" + +#include "benchmark/benchmark.h" + +namespace { + +// Benchmark Time(ns) CPU(ns) Iterations +// ------------------------------------------------------------------------- +// BM_Difference_Days 20 20 34542508 +// BM_Step_Days 15 15 48098146 +// BM_Format 688 687 1019803 +// BM_Parse 921 920 762788 +// BM_RoundTripFormatParse 1766 1764 396092 + +void BM_Difference_Days(benchmark::State& state) { + const absl::CivilDay c(2014, 8, 22); + const absl::CivilDay epoch(1970, 1, 1); + while (state.KeepRunning()) { + const absl::civil_diff_t n = c - epoch; + benchmark::DoNotOptimize(n); + } +} +BENCHMARK(BM_Difference_Days); + +void BM_Step_Days(benchmark::State& state) { + const absl::CivilDay kStart(2014, 8, 22); + absl::CivilDay c = kStart; + while (state.KeepRunning()) { + benchmark::DoNotOptimize(++c); + } +} +BENCHMARK(BM_Step_Days); + +void BM_Format(benchmark::State& state) { + const absl::CivilSecond c(2014, 1, 2, 3, 4, 5); + while (state.KeepRunning()) { + const std::string s = absl::FormatCivilTime(c); + benchmark::DoNotOptimize(s); + } +} +BENCHMARK(BM_Format); + +} // namespace diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc new file mode 100644 index 00000000..dc83d7a9 --- /dev/null +++ b/absl/time/civil_time_test.cc @@ -0,0 +1,1073 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/time/civil_time.h" + +#include +#include +#include + +#include "absl/base/macros.h" +#include "gtest/gtest.h" + +namespace { + +TEST(CivilTime, DefaultConstruction) { + absl::CivilSecond ss; + EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss)); + + absl::CivilMinute mm; + EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm)); + + absl::CivilHour hh; + EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh)); + + absl::CivilDay d; + EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d)); + + absl::CivilMonth m; + EXPECT_EQ("1970-01", absl::FormatCivilTime(m)); + + absl::CivilYear y; + EXPECT_EQ("1970", absl::FormatCivilTime(y)); +} + +TEST(CivilTime, StructMember) { + struct S { + absl::CivilDay day; + }; + S s = {}; + EXPECT_EQ(absl::CivilDay{}, s.day); +} + +TEST(CivilTime, FieldsConstruction) { + EXPECT_EQ("2015-01-02T03:04:05", + absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015-01-02T03:04:00", + absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015-01-02T03:00:00", + absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3))); + EXPECT_EQ("2015-01-02T00:00:00", + absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2))); + EXPECT_EQ("2015-01-01T00:00:00", + absl::FormatCivilTime(absl::CivilSecond(2015, 1))); + EXPECT_EQ("2015-01-01T00:00:00", + absl::FormatCivilTime(absl::CivilSecond(2015))); + + EXPECT_EQ("2015-01-02T03:04", + absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015-01-02T03:04", + absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015-01-02T03:00", + absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3))); + EXPECT_EQ("2015-01-02T00:00", + absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2))); + EXPECT_EQ("2015-01-01T00:00", + absl::FormatCivilTime(absl::CivilMinute(2015, 1))); + EXPECT_EQ("2015-01-01T00:00", + absl::FormatCivilTime(absl::CivilMinute(2015))); + + EXPECT_EQ("2015-01-02T03", + absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015-01-02T03", + absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015-01-02T03", + absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3))); + EXPECT_EQ("2015-01-02T00", + absl::FormatCivilTime(absl::CivilHour(2015, 1, 2))); + EXPECT_EQ("2015-01-01T00", + absl::FormatCivilTime(absl::CivilHour(2015, 1))); + EXPECT_EQ("2015-01-01T00", + absl::FormatCivilTime(absl::CivilHour(2015))); + + EXPECT_EQ("2015-01-02", + absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015-01-02", + absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015-01-02", + absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3))); + EXPECT_EQ("2015-01-02", + absl::FormatCivilTime(absl::CivilDay(2015, 1, 2))); + EXPECT_EQ("2015-01-01", + absl::FormatCivilTime(absl::CivilDay(2015, 1))); + EXPECT_EQ("2015-01-01", + absl::FormatCivilTime(absl::CivilDay(2015))); + + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3))); + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2))); + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015, 1))); + EXPECT_EQ("2015-01", + absl::FormatCivilTime(absl::CivilMonth(2015))); + + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4, 5))); + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4))); + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3))); + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015, 1, 2))); + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015, 1))); + EXPECT_EQ("2015", + absl::FormatCivilTime(absl::CivilYear(2015))); +} + +TEST(CivilTime, FieldsConstructionLimits) { + const int kIntMax = std::numeric_limits::max(); + EXPECT_EQ("2038-01-19T03:14:07", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, 0, 0, kIntMax))); + EXPECT_EQ("6121-02-11T05:21:07", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, 0, kIntMax, kIntMax))); + EXPECT_EQ("251104-11-20T12:21:07", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, kIntMax, kIntMax, kIntMax))); + EXPECT_EQ("6130715-05-30T12:21:07", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, kIntMax, kIntMax, kIntMax, kIntMax))); + EXPECT_EQ("185087685-11-26T12:21:07", + absl::FormatCivilTime(absl::CivilSecond( + 1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax))); + + const int kIntMin = std::numeric_limits::min(); + EXPECT_EQ("1901-12-13T20:45:52", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, 0, 0, kIntMin))); + EXPECT_EQ("-2182-11-20T18:37:52", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, 0, kIntMin, kIntMin))); + EXPECT_EQ("-247165-02-11T10:37:52", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, 1, kIntMin, kIntMin, kIntMin))); + EXPECT_EQ("-6126776-08-01T10:37:52", + absl::FormatCivilTime(absl::CivilSecond( + 1970, 1, kIntMin, kIntMin, kIntMin, kIntMin))); + EXPECT_EQ("-185083747-10-31T10:37:52", + absl::FormatCivilTime(absl::CivilSecond( + 1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin))); +} + +TEST(CivilTime, RangeLimits) { + const absl::civil_year_t kYearMax = + std::numeric_limits::max(); + EXPECT_EQ(absl::CivilYear(kYearMax), + absl::CivilYear::max()); + EXPECT_EQ(absl::CivilMonth(kYearMax, 12), + absl::CivilMonth::max()); + EXPECT_EQ(absl::CivilDay(kYearMax, 12, 31), + absl::CivilDay::max()); + EXPECT_EQ(absl::CivilHour(kYearMax, 12, 31, 23), + absl::CivilHour::max()); + EXPECT_EQ(absl::CivilMinute(kYearMax, 12, 31, 23, 59), + absl::CivilMinute::max()); + EXPECT_EQ(absl::CivilSecond(kYearMax, 12, 31, 23, 59, 59), + absl::CivilSecond::max()); + + const absl::civil_year_t kYearMin = + std::numeric_limits::min(); + EXPECT_EQ(absl::CivilYear(kYearMin), + absl::CivilYear::min()); + EXPECT_EQ(absl::CivilMonth(kYearMin, 1), + absl::CivilMonth::min()); + EXPECT_EQ(absl::CivilDay(kYearMin, 1, 1), + absl::CivilDay::min()); + EXPECT_EQ(absl::CivilHour(kYearMin, 1, 1, 0), + absl::CivilHour::min()); + EXPECT_EQ(absl::CivilMinute(kYearMin, 1, 1, 0, 0), + absl::CivilMinute::min()); + EXPECT_EQ(absl::CivilSecond(kYearMin, 1, 1, 0, 0, 0), + absl::CivilSecond::min()); +} + +TEST(CivilTime, ImplicitCrossAlignment) { + absl::CivilYear year(2015); + absl::CivilMonth month = year; + absl::CivilDay day = month; + absl::CivilHour hour = day; + absl::CivilMinute minute = hour; + absl::CivilSecond second = minute; + + second = year; + EXPECT_EQ(second, year); + second = month; + EXPECT_EQ(second, month); + second = day; + EXPECT_EQ(second, day); + second = hour; + EXPECT_EQ(second, hour); + second = minute; + EXPECT_EQ(second, minute); + + minute = year; + EXPECT_EQ(minute, year); + minute = month; + EXPECT_EQ(minute, month); + minute = day; + EXPECT_EQ(minute, day); + minute = hour; + EXPECT_EQ(minute, hour); + + hour = year; + EXPECT_EQ(hour, year); + hour = month; + EXPECT_EQ(hour, month); + hour = day; + EXPECT_EQ(hour, day); + + day = year; + EXPECT_EQ(day, year); + day = month; + EXPECT_EQ(day, month); + + month = year; + EXPECT_EQ(month, year); + + // Ensures unsafe conversions are not allowed. + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + + EXPECT_FALSE( + (std::is_convertible::value)); + EXPECT_FALSE( + (std::is_convertible::value)); + + EXPECT_FALSE( + (std::is_convertible::value)); +} + +TEST(CivilTime, ExplicitCrossAlignment) { + // + // Assign from smaller units -> larger units + // + + absl::CivilSecond second(2015, 1, 2, 3, 4, 5); + EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second)); + + absl::CivilMinute minute(second); + EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute)); + + absl::CivilHour hour(minute); + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour)); + + absl::CivilDay day(hour); + EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day)); + + absl::CivilMonth month(day); + EXPECT_EQ("2015-01", absl::FormatCivilTime(month)); + + absl::CivilYear year(month); + EXPECT_EQ("2015", absl::FormatCivilTime(year)); + + // + // Now assign from larger units -> smaller units + // + + month = absl::CivilMonth(year); + EXPECT_EQ("2015-01", absl::FormatCivilTime(month)); + + day = absl::CivilDay(month); + EXPECT_EQ("2015-01-01", absl::FormatCivilTime(day)); + + hour = absl::CivilHour(day); + EXPECT_EQ("2015-01-01T00", absl::FormatCivilTime(hour)); + + minute = absl::CivilMinute(hour); + EXPECT_EQ("2015-01-01T00:00", absl::FormatCivilTime(minute)); + + second = absl::CivilSecond(minute); + EXPECT_EQ("2015-01-01T00:00:00", absl::FormatCivilTime(second)); +} + +// Metafunction to test whether difference is allowed between two types. +template +struct HasDiff { + template + static std::false_type test(...); + template + static std::true_type test(decltype(std::declval() - std::declval())); + static constexpr bool value = decltype(test(0))::value; +}; + +TEST(CivilTime, DisallowCrossAlignedDifference) { + // Difference is allowed between types with the same alignment. + static_assert(HasDiff::value, ""); + static_assert(HasDiff::value, ""); + static_assert(HasDiff::value, ""); + static_assert(HasDiff::value, ""); + static_assert(HasDiff::value, ""); + static_assert(HasDiff::value, ""); + + // Difference is disallowed between types with different alignments. + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + + static_assert(!HasDiff::value, ""); + static_assert(!HasDiff::value, ""); + + static_assert(!HasDiff::value, ""); +} + +TEST(CivilTime, ValueSemantics) { + const absl::CivilHour a(2015, 1, 2, 3); + const absl::CivilHour b = a; + const absl::CivilHour c(b); + absl::CivilHour d; + d = c; + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(d)); +} + +TEST(CivilTime, Relational) { + // Tests that the alignment unit is ignored in comparison. + const absl::CivilYear year(2014); + const absl::CivilMonth month(year); + EXPECT_EQ(year, month); + +#define TEST_RELATIONAL(OLDER, YOUNGER) \ + do { \ + EXPECT_FALSE(OLDER < OLDER); \ + EXPECT_FALSE(OLDER > OLDER); \ + EXPECT_TRUE(OLDER >= OLDER); \ + EXPECT_TRUE(OLDER <= OLDER); \ + EXPECT_FALSE(YOUNGER < YOUNGER); \ + EXPECT_FALSE(YOUNGER > YOUNGER); \ + EXPECT_TRUE(YOUNGER >= YOUNGER); \ + EXPECT_TRUE(YOUNGER <= YOUNGER); \ + EXPECT_EQ(OLDER, OLDER); \ + EXPECT_NE(OLDER, YOUNGER); \ + EXPECT_LT(OLDER, YOUNGER); \ + EXPECT_LE(OLDER, YOUNGER); \ + EXPECT_GT(YOUNGER, OLDER); \ + EXPECT_GE(YOUNGER, OLDER); \ + } while (0) + + // Alignment is ignored in comparison (verified above), so CivilSecond is + // used to test comparison in all field positions. + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0), + absl::CivilSecond(2015, 1, 1, 0, 0, 0)); + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0), + absl::CivilSecond(2014, 2, 1, 0, 0, 0)); + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0), + absl::CivilSecond(2014, 1, 2, 0, 0, 0)); + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0), + absl::CivilSecond(2014, 1, 1, 1, 0, 0)); + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 0, 0), + absl::CivilSecond(2014, 1, 1, 1, 1, 0)); + TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 1, 0), + absl::CivilSecond(2014, 1, 1, 1, 1, 1)); + + // Tests the relational operators of two different civil-time types. + TEST_RELATIONAL(absl::CivilDay(2014, 1, 1), + absl::CivilMinute(2014, 1, 1, 1, 1)); + TEST_RELATIONAL(absl::CivilDay(2014, 1, 1), + absl::CivilMonth(2014, 2)); + +#undef TEST_RELATIONAL +} + +TEST(CivilTime, Arithmetic) { + absl::CivilSecond second(2015, 1, 2, 3, 4, 5); + EXPECT_EQ("2015-01-02T03:04:06", absl::FormatCivilTime(second += 1)); + EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second + 1)); + EXPECT_EQ("2015-01-02T03:04:08", absl::FormatCivilTime(2 + second)); + EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second - 1)); + EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second -= 1)); + EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second++)); + EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(++second)); + EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second--)); + EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(--second)); + + absl::CivilMinute minute(2015, 1, 2, 3, 4); + EXPECT_EQ("2015-01-02T03:05", absl::FormatCivilTime(minute += 1)); + EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute + 1)); + EXPECT_EQ("2015-01-02T03:07", absl::FormatCivilTime(2 + minute)); + EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute - 1)); + EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute -= 1)); + EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute++)); + EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(++minute)); + EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute--)); + EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(--minute)); + + absl::CivilHour hour(2015, 1, 2, 3); + EXPECT_EQ("2015-01-02T04", absl::FormatCivilTime(hour += 1)); + EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour + 1)); + EXPECT_EQ("2015-01-02T06", absl::FormatCivilTime(2 + hour)); + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour - 1)); + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour -= 1)); + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour++)); + EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(++hour)); + EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour--)); + EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(--hour)); + + absl::CivilDay day(2015, 1, 2); + EXPECT_EQ("2015-01-03", absl::FormatCivilTime(day += 1)); + EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day + 1)); + EXPECT_EQ("2015-01-05", absl::FormatCivilTime(2 + day)); + EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day - 1)); + EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day -= 1)); + EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day++)); + EXPECT_EQ("2015-01-04", absl::FormatCivilTime(++day)); + EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day--)); + EXPECT_EQ("2015-01-02", absl::FormatCivilTime(--day)); + + absl::CivilMonth month(2015, 1); + EXPECT_EQ("2015-02", absl::FormatCivilTime(month += 1)); + EXPECT_EQ("2015-03", absl::FormatCivilTime(month + 1)); + EXPECT_EQ("2015-04", absl::FormatCivilTime(2 + month)); + EXPECT_EQ("2015-01", absl::FormatCivilTime(month - 1)); + EXPECT_EQ("2015-01", absl::FormatCivilTime(month -= 1)); + EXPECT_EQ("2015-01", absl::FormatCivilTime(month++)); + EXPECT_EQ("2015-03", absl::FormatCivilTime(++month)); + EXPECT_EQ("2015-03", absl::FormatCivilTime(month--)); + EXPECT_EQ("2015-01", absl::FormatCivilTime(--month)); + + absl::CivilYear year(2015); + EXPECT_EQ("2016", absl::FormatCivilTime(year += 1)); + EXPECT_EQ("2017", absl::FormatCivilTime(year + 1)); + EXPECT_EQ("2018", absl::FormatCivilTime(2 + year)); + EXPECT_EQ("2015", absl::FormatCivilTime(year - 1)); + EXPECT_EQ("2015", absl::FormatCivilTime(year -= 1)); + EXPECT_EQ("2015", absl::FormatCivilTime(year++)); + EXPECT_EQ("2017", absl::FormatCivilTime(++year)); + EXPECT_EQ("2017", absl::FormatCivilTime(year--)); + EXPECT_EQ("2015", absl::FormatCivilTime(--year)); +} + +TEST(CivilTime, ArithmeticLimits) { + const int kIntMax = std::numeric_limits::max(); + const int kIntMin = std::numeric_limits::min(); + + absl::CivilSecond second(1970, 1, 1, 0, 0, 0); + second += kIntMax; + EXPECT_EQ("2038-01-19T03:14:07", absl::FormatCivilTime(second)); + second -= kIntMax; + EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second)); + second += kIntMin; + EXPECT_EQ("1901-12-13T20:45:52", absl::FormatCivilTime(second)); + second -= kIntMin; + EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second)); + + absl::CivilMinute minute(1970, 1, 1, 0, 0); + minute += kIntMax; + EXPECT_EQ("6053-01-23T02:07", absl::FormatCivilTime(minute)); + minute -= kIntMax; + EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute)); + minute += kIntMin; + EXPECT_EQ("-2114-12-08T21:52", absl::FormatCivilTime(minute)); + minute -= kIntMin; + EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute)); + + absl::CivilHour hour(1970, 1, 1, 0); + hour += kIntMax; + EXPECT_EQ("246953-10-09T07", absl::FormatCivilTime(hour)); + hour -= kIntMax; + EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour)); + hour += kIntMin; + EXPECT_EQ("-243014-03-24T16", absl::FormatCivilTime(hour)); + hour -= kIntMin; + EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour)); + + absl::CivilDay day(1970, 1, 1); + day += kIntMax; + EXPECT_EQ("5881580-07-11", absl::FormatCivilTime(day)); + day -= kIntMax; + EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day)); + day += kIntMin; + EXPECT_EQ("-5877641-06-23", absl::FormatCivilTime(day)); + day -= kIntMin; + EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day)); + + absl::CivilMonth month(1970, 1); + month += kIntMax; + EXPECT_EQ("178958940-08", absl::FormatCivilTime(month)); + month -= kIntMax; + EXPECT_EQ("1970-01", absl::FormatCivilTime(month)); + month += kIntMin; + EXPECT_EQ("-178955001-05", absl::FormatCivilTime(month)); + month -= kIntMin; + EXPECT_EQ("1970-01", absl::FormatCivilTime(month)); + + absl::CivilYear year(0); + year += kIntMax; + EXPECT_EQ("2147483647", absl::FormatCivilTime(year)); + year -= kIntMax; + EXPECT_EQ("0", absl::FormatCivilTime(year)); + year += kIntMin; + EXPECT_EQ("-2147483648", absl::FormatCivilTime(year)); + year -= kIntMin; + EXPECT_EQ("0", absl::FormatCivilTime(year)); +} + +TEST(CivilTime, Difference) { + absl::CivilSecond second(2015, 1, 2, 3, 4, 5); + EXPECT_EQ(0, second - second); + EXPECT_EQ(10, (second + 10) - second); + EXPECT_EQ(-10, (second - 10) - second); + + absl::CivilMinute minute(2015, 1, 2, 3, 4); + EXPECT_EQ(0, minute - minute); + EXPECT_EQ(10, (minute + 10) - minute); + EXPECT_EQ(-10, (minute - 10) - minute); + + absl::CivilHour hour(2015, 1, 2, 3); + EXPECT_EQ(0, hour - hour); + EXPECT_EQ(10, (hour + 10) - hour); + EXPECT_EQ(-10, (hour - 10) - hour); + + absl::CivilDay day(2015, 1, 2); + EXPECT_EQ(0, day - day); + EXPECT_EQ(10, (day + 10) - day); + EXPECT_EQ(-10, (day - 10) - day); + + absl::CivilMonth month(2015, 1); + EXPECT_EQ(0, month - month); + EXPECT_EQ(10, (month + 10) - month); + EXPECT_EQ(-10, (month - 10) - month); + + absl::CivilYear year(2015); + EXPECT_EQ(0, year - year); + EXPECT_EQ(10, (year + 10) - year); + EXPECT_EQ(-10, (year - 10) - year); +} + +TEST(CivilTime, DifferenceLimits) { + const absl::civil_diff_t kDiffMax = + std::numeric_limits::max(); + const absl::civil_diff_t kDiffMin = + std::numeric_limits::min(); + + // Check day arithmetic at the end of the year range. + const absl::CivilDay max_day(kDiffMax, 12, 31); + EXPECT_EQ(1, max_day - (max_day - 1)); + EXPECT_EQ(-1, (max_day - 1) - max_day); + + // Check day arithmetic at the start of the year range. + const absl::CivilDay min_day(kDiffMin, 1, 1); + EXPECT_EQ(1, (min_day + 1) - min_day); + EXPECT_EQ(-1, min_day - (min_day + 1)); + + // Check the limits of the return value. + const absl::CivilDay d1(1970, 1, 1); + const absl::CivilDay d2(25252734927768524, 7, 27); + EXPECT_EQ(kDiffMax, d2 - d1); + EXPECT_EQ(kDiffMin, d1 - (d2 + 1)); +} + +TEST(CivilTime, Properties) { + absl::CivilSecond ss(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, ss.year()); + EXPECT_EQ(2, ss.month()); + EXPECT_EQ(3, ss.day()); + EXPECT_EQ(4, ss.hour()); + EXPECT_EQ(5, ss.minute()); + EXPECT_EQ(6, ss.second()); + + absl::CivilMinute mm(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, mm.year()); + EXPECT_EQ(2, mm.month()); + EXPECT_EQ(3, mm.day()); + EXPECT_EQ(4, mm.hour()); + EXPECT_EQ(5, mm.minute()); + EXPECT_EQ(0, mm.second()); + + absl::CivilHour hh(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, hh.year()); + EXPECT_EQ(2, hh.month()); + EXPECT_EQ(3, hh.day()); + EXPECT_EQ(4, hh.hour()); + EXPECT_EQ(0, hh.minute()); + EXPECT_EQ(0, hh.second()); + + absl::CivilDay d(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, d.year()); + EXPECT_EQ(2, d.month()); + EXPECT_EQ(3, d.day()); + EXPECT_EQ(0, d.hour()); + EXPECT_EQ(0, d.minute()); + EXPECT_EQ(0, d.second()); + + absl::CivilMonth m(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, m.year()); + EXPECT_EQ(2, m.month()); + EXPECT_EQ(1, m.day()); + EXPECT_EQ(0, m.hour()); + EXPECT_EQ(0, m.minute()); + EXPECT_EQ(0, m.second()); + + absl::CivilYear y(2015, 2, 3, 4, 5, 6); + EXPECT_EQ(2015, y.year()); + EXPECT_EQ(1, y.month()); + EXPECT_EQ(1, y.day()); + EXPECT_EQ(0, y.hour()); + EXPECT_EQ(0, y.minute()); + EXPECT_EQ(0, y.second()); +} + +TEST(CivilTime, Format) { + absl::CivilSecond ss; + EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss)); + + absl::CivilMinute mm; + EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm)); + + absl::CivilHour hh; + EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh)); + + absl::CivilDay d; + EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d)); + + absl::CivilMonth m; + EXPECT_EQ("1970-01", absl::FormatCivilTime(m)); + + absl::CivilYear y; + EXPECT_EQ("1970", absl::FormatCivilTime(y)); +} + +TEST(CivilTime, FormatAndParseLenient) { + absl::CivilSecond ss; + EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss)); + + absl::CivilMinute mm; + EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm)); + + absl::CivilHour hh; + EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh)); + + absl::CivilDay d; + EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d)); + + absl::CivilMonth m; + EXPECT_EQ("1970-01", absl::FormatCivilTime(m)); + + absl::CivilYear y; + EXPECT_EQ("1970", absl::FormatCivilTime(y)); +} + +TEST(CivilTime, OutputStream) { + absl::CivilSecond cs(2016, 2, 3, 4, 5, 6); + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilYear(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016.................X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilMonth(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016-02..............X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilDay(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016-02-03...........X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilHour(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016-02-03T04........X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilMinute(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::CivilSecond(cs); + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str()); + } + { + std::stringstream ss; + ss << std::left << std::setfill('.'); + ss << std::setw(3) << 'X'; + ss << std::setw(21) << absl::Weekday::wednesday; + ss << std::setw(3) << 'X'; + EXPECT_EQ("X..Wednesday............X..", ss.str()); + } +} + +TEST(CivilTime, Weekday) { + absl::CivilDay d(1970, 1, 1); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(d)) << d; + + // We used to get this wrong for years < -30. + d = absl::CivilDay(-31, 12, 24); + EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(d)) << d; +} + +TEST(CivilTime, NextPrevWeekday) { + // Jan 1, 1970 was a Thursday. + const absl::CivilDay thursday(1970, 1, 1); + + // Thursday -> Thursday + absl::CivilDay d = absl::NextWeekday(thursday, absl::Weekday::thursday); + EXPECT_EQ(7, d - thursday) << d; + EXPECT_EQ(d - 14, absl::PrevWeekday(thursday, absl::Weekday::thursday)); + + // Thursday -> Friday + d = absl::NextWeekday(thursday, absl::Weekday::friday); + EXPECT_EQ(1, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::friday)); + + // Thursday -> Saturday + d = absl::NextWeekday(thursday, absl::Weekday::saturday); + EXPECT_EQ(2, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::saturday)); + + // Thursday -> Sunday + d = absl::NextWeekday(thursday, absl::Weekday::sunday); + EXPECT_EQ(3, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::sunday)); + + // Thursday -> Monday + d = absl::NextWeekday(thursday, absl::Weekday::monday); + EXPECT_EQ(4, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::monday)); + + // Thursday -> Tuesday + d = absl::NextWeekday(thursday, absl::Weekday::tuesday); + EXPECT_EQ(5, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::tuesday)); + + // Thursday -> Wednesday + d = absl::NextWeekday(thursday, absl::Weekday::wednesday); + EXPECT_EQ(6, d - thursday) << d; + EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::wednesday)); +} + +// NOTE: Run this with --copt=-ftrapv to detect overflow problems. +TEST(CivilTime, DifferenceWithHugeYear) { + absl::CivilDay d1(9223372036854775807, 1, 1); + absl::CivilDay d2(9223372036854775807, 12, 31); + EXPECT_EQ(364, d2 - d1); + + d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1); + d2 = absl::CivilDay(-9223372036854775807 - 1, 12, 31); + EXPECT_EQ(365, d2 - d1); + + // Check the limits of the return value at the end of the year range. + d1 = absl::CivilDay(9223372036854775807, 1, 1); + d2 = absl::CivilDay(9198119301927009252, 6, 6); + EXPECT_EQ(9223372036854775807, d1 - d2); + d2 = d2 - 1; + EXPECT_EQ(-9223372036854775807 - 1, d2 - d1); + + // Check the limits of the return value at the start of the year range. + d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1); + d2 = absl::CivilDay(-9198119301927009254, 7, 28); + EXPECT_EQ(9223372036854775807, d2 - d1); + d2 = d2 + 1; + EXPECT_EQ(-9223372036854775807 - 1, d1 - d2); + + // Check the limits of the return value from either side of year 0. + d1 = absl::CivilDay(-12626367463883278, 9, 3); + d2 = absl::CivilDay(12626367463883277, 3, 28); + EXPECT_EQ(9223372036854775807, d2 - d1); + d2 = d2 + 1; + EXPECT_EQ(-9223372036854775807 - 1, d1 - d2); +} + +// NOTE: Run this with --copt=-ftrapv to detect overflow problems. +TEST(CivilTime, DifferenceNoIntermediateOverflow) { + // The difference up to the minute field would be below the minimum + // int64_t, but the 52 extra seconds brings us back to the minimum. + absl::CivilSecond s1(-292277022657, 1, 27, 8, 29 - 1, 52); + absl::CivilSecond s2(1970, 1, 1, 0, 0 - 1, 0); + EXPECT_EQ(-9223372036854775807 - 1, s1 - s2); + + // The difference up to the minute field would be above the maximum + // int64_t, but the -53 extra seconds brings us back to the maximum. + s1 = absl::CivilSecond(292277026596, 12, 4, 15, 30, 7 - 7); + s2 = absl::CivilSecond(1970, 1, 1, 0, 0, 0 - 7); + EXPECT_EQ(9223372036854775807, s1 - s2); +} + +TEST(CivilTime, NormalizeSimpleOverflow) { + absl::CivilSecond cs; + cs = absl::CivilSecond(2013, 11, 15, 16, 32, 59 + 1); + EXPECT_EQ("2013-11-15T16:33:00", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16, 59 + 1, 14); + EXPECT_EQ("2013-11-15T17:00:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 23 + 1, 32, 14); + EXPECT_EQ("2013-11-16T00:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 30 + 1, 16, 32, 14); + EXPECT_EQ("2013-12-01T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 12 + 1, 15, 16, 32, 14); + EXPECT_EQ("2014-01-15T16:32:14", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeSimpleUnderflow) { + absl::CivilSecond cs; + cs = absl::CivilSecond(2013, 11, 15, 16, 32, 0 - 1); + EXPECT_EQ("2013-11-15T16:31:59", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16, 0 - 1, 14); + EXPECT_EQ("2013-11-15T15:59:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 0 - 1, 32, 14); + EXPECT_EQ("2013-11-14T23:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 1 - 1, 16, 32, 14); + EXPECT_EQ("2013-10-31T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 1 - 1, 15, 16, 32, 14); + EXPECT_EQ("2012-12-15T16:32:14", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeMultipleOverflow) { + absl::CivilSecond cs(2013, 12, 31, 23, 59, 59 + 1); + EXPECT_EQ("2014-01-01T00:00:00", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeMultipleUnderflow) { + absl::CivilSecond cs(2014, 1, 1, 0, 0, 0 - 1); + EXPECT_EQ("2013-12-31T23:59:59", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeOverflowLimits) { + absl::CivilSecond cs; + + const int kintmax = std::numeric_limits::max(); + cs = absl::CivilSecond(0, kintmax, kintmax, kintmax, kintmax, kintmax); + EXPECT_EQ("185085715-11-27T12:21:07", absl::FormatCivilTime(cs)); + + const int kintmin = std::numeric_limits::min(); + cs = absl::CivilSecond(0, kintmin, kintmin, kintmin, kintmin, kintmin); + EXPECT_EQ("-185085717-10-31T10:37:52", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeComplexOverflow) { + absl::CivilSecond cs; + cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 + 123456789); + EXPECT_EQ("2017-10-14T14:05:23", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16, 32 + 1234567, 14); + EXPECT_EQ("2016-03-22T00:39:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16 + 123456, 32, 14); + EXPECT_EQ("2027-12-16T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15 + 1234, 16, 32, 14); + EXPECT_EQ("2017-04-02T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11 + 123, 15, 16, 32, 14); + EXPECT_EQ("2024-02-15T16:32:14", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeComplexUnderflow) { + absl::CivilSecond cs; + cs = absl::CivilSecond(1999, 3, 0, 0, 0, 0); // year 400 + EXPECT_EQ("1999-02-28T00:00:00", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 - 123456789); + EXPECT_EQ("2009-12-17T18:59:05", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16, 32 - 1234567, 14); + EXPECT_EQ("2011-07-12T08:25:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15, 16 - 123456, 32, 14); + EXPECT_EQ("1999-10-16T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11, 15 - 1234, 16, 32, 14); + EXPECT_EQ("2010-06-30T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11 - 123, 15, 16, 32, 14); + EXPECT_EQ("2003-08-15T16:32:14", absl::FormatCivilTime(cs)); +} + +TEST(CivilTime, NormalizeMishmash) { + absl::CivilSecond cs; + cs = absl::CivilSecond(2013, 11 - 123, 15 + 1234, 16 - 123456, 32 + 1234567, + 14 - 123456789); + EXPECT_EQ("1991-05-09T03:06:05", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11 + 123, 15 - 1234, 16 + 123456, 32 - 1234567, + 14 + 123456789); + EXPECT_EQ("2036-05-24T05:58:23", absl::FormatCivilTime(cs)); + + cs = absl::CivilSecond(2013, 11, -146097 + 1, 16, 32, 14); + EXPECT_EQ("1613-11-01T16:32:14", absl::FormatCivilTime(cs)); + cs = absl::CivilSecond(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14); + EXPECT_EQ("2013-11-01T16:32:14", absl::FormatCivilTime(cs)); +} + +// Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31) +// and check that they normalize to the expected time. 146097 days span +// the 400-year Gregorian cycle used during normalization. +TEST(CivilTime, NormalizeAllTheDays) { + absl::CivilDay expected(1970, 1, 1); + for (int day = 1; day <= 146097; ++day) { + absl::CivilSecond cs(1970, 1, day, 0, 0, 0); + EXPECT_EQ(expected, cs); + ++expected; + } +} + +TEST(CivilTime, NormalizeWithHugeYear) { + absl::CivilMonth c(9223372036854775807, 1); + EXPECT_EQ("9223372036854775807-01", absl::FormatCivilTime(c)); + c = c - 1; // Causes normalization + EXPECT_EQ("9223372036854775806-12", absl::FormatCivilTime(c)); + + c = absl::CivilMonth(-9223372036854775807 - 1, 1); + EXPECT_EQ("-9223372036854775808-01", absl::FormatCivilTime(c)); + c = c + 12; // Causes normalization + EXPECT_EQ("-9223372036854775807-01", absl::FormatCivilTime(c)); +} + +TEST(CivilTime, LeapYears) { + const absl::CivilSecond s1(2013, 2, 28 + 1, 0, 0, 0); + EXPECT_EQ("2013-03-01T00:00:00", absl::FormatCivilTime(s1)); + + const absl::CivilSecond s2(2012, 2, 28 + 1, 0, 0, 0); + EXPECT_EQ("2012-02-29T00:00:00", absl::FormatCivilTime(s2)); + + const absl::CivilSecond s3(1900, 2, 28 + 1, 0, 0, 0); + EXPECT_EQ("1900-03-01T00:00:00", absl::FormatCivilTime(s3)); + + const struct { + int year; + int days; + struct { + int month; + int day; + } leap_day; // The date of the day after Feb 28. + } kLeapYearTable[]{ + {1900, 365, {3, 1}}, + {1999, 365, {3, 1}}, + {2000, 366, {2, 29}}, // leap year + {2001, 365, {3, 1}}, + {2002, 365, {3, 1}}, + {2003, 365, {3, 1}}, + {2004, 366, {2, 29}}, // leap year + {2005, 365, {3, 1}}, + {2006, 365, {3, 1}}, + {2007, 365, {3, 1}}, + {2008, 366, {2, 29}}, // leap year + {2009, 365, {3, 1}}, + {2100, 365, {3, 1}}, + }; + + for (int i = 0; i < ABSL_ARRAYSIZE(kLeapYearTable); ++i) { + const int y = kLeapYearTable[i].year; + const int m = kLeapYearTable[i].leap_day.month; + const int d = kLeapYearTable[i].leap_day.day; + const int n = kLeapYearTable[i].days; + + // Tests incrementing through the leap day. + const absl::CivilDay feb28(y, 2, 28); + const absl::CivilDay next_day = feb28 + 1; + EXPECT_EQ(m, next_day.month()); + EXPECT_EQ(d, next_day.day()); + + // Tests difference in days of leap years. + const absl::CivilYear year(feb28); + const absl::CivilYear next_year = year + 1; + EXPECT_EQ(n, absl::CivilDay(next_year) - absl::CivilDay(year)); + } +} + +TEST(CivilTime, FirstThursdayInMonth) { + const absl::CivilDay nov1(2014, 11, 1); + const absl::CivilDay thursday = + absl::PrevWeekday(nov1, absl::Weekday::thursday) + 7; + EXPECT_EQ("2014-11-06", absl::FormatCivilTime(thursday)); + + // Bonus: Date of Thanksgiving in the United States + // Rule: Fourth Thursday of November + const absl::CivilDay thanksgiving = thursday + 7 * 3; + EXPECT_EQ("2014-11-27", absl::FormatCivilTime(thanksgiving)); +} + +TEST(CivilTime, DocumentationExample) { + absl::CivilSecond second(2015, 6, 28, 1, 2, 3); // 2015-06-28 01:02:03 + absl::CivilMinute minute(second); // 2015-06-28 01:02:00 + absl::CivilDay day(minute); // 2015-06-28 00:00:00 + + second -= 1; // 2015-06-28 01:02:02 + --second; // 2015-06-28 01:02:01 + EXPECT_EQ(minute, second - 1); // Comparison between types + EXPECT_LT(minute, second); + + // int diff = second - minute; // ERROR: Mixed types, won't compile + + absl::CivilDay june_1(2015, 6, 1); // Pass fields to c'tor. + int diff = day - june_1; // Num days between 'day' and June 1 + EXPECT_EQ(27, diff); + + // Fields smaller than alignment are floored to their minimum value. + absl::CivilDay day_floor(2015, 1, 2, 9, 9, 9); + EXPECT_EQ(0, day_floor.hour()); // 09:09:09 is floored + EXPECT_EQ(absl::CivilDay(2015, 1, 2), day_floor); + + // Unspecified fields default to their minium value + absl::CivilDay day_default(2015); // Defaults to Jan 1 + EXPECT_EQ(absl::CivilDay(2015, 1, 1), day_default); + + // Iterates all the days of June. + absl::CivilMonth june(day); // CivilDay -> CivilMonth + absl::CivilMonth july = june + 1; + for (absl::CivilDay day = june_1; day < july; ++day) { + // ... + } +} + +} // namespace diff --git a/absl/time/format_benchmark.cc b/absl/time/format_benchmark.cc index ee53d71c..766f1b39 100644 --- a/absl/time/format_benchmark.cc +++ b/absl/time/format_benchmark.cc @@ -38,7 +38,8 @@ void BM_Format_FormatTime(benchmark::State& state) { const absl::TimeZone lax = absl::time_internal::LoadTimeZone("America/Los_Angeles"); const absl::Time t = - absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1); + absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) + + absl::Nanoseconds(1); while (state.KeepRunning()) { benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length()); } @@ -50,8 +51,8 @@ void BM_Format_ParseTime(benchmark::State& state) { state.SetLabel(fmt); const absl::TimeZone lax = absl::time_internal::LoadTimeZone("America/Los_Angeles"); - absl::Time t = - absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1); + absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) + + absl::Nanoseconds(1); const std::string when = absl::FormatTime(fmt, t, lax); std::string err; while (state.KeepRunning()) { diff --git a/absl/time/format_test.cc b/absl/time/format_test.cc index 7c84c33f..40f4c246 100644 --- a/absl/time/format_test.cc +++ b/absl/time/format_test.cc @@ -118,7 +118,7 @@ TEST(FormatTime, RFC1123FormatPadsYear) { // locale specific absl::TimeZone tz = absl::UTCTimeZone(); // A year of 77 should be padded to 0077. - absl::Time t = absl::FromDateTime(77, 6, 28, 9, 8, 7, tz); + absl::Time t = absl::FromCivil(absl::CivilSecond(77, 6, 28, 9, 8, 7), tz); EXPECT_EQ("Mon, 28 Jun 0077 09:08:07 +0000", absl::FormatTime(absl::RFC1123_full, t, tz)); EXPECT_EQ("28 Jun 0077 09:08:07 +0000", @@ -154,9 +154,9 @@ TEST(ParseTime, Basics) { EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z", "2013-06-28 19:08:09 -0800", &t, &err)) << err; - absl::Time::Breakdown bd = t.In(absl::FixedTimeZone(-8 * 60 * 60)); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -8 * 60 * 60, false); - EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); + const auto ci = absl::FixedTimeZone(-8 * 60 * 60).At(t); + EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); + EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); } TEST(ParseTime, NullErrorString) { @@ -177,17 +177,17 @@ TEST(ParseTime, WithTimeZone) { EXPECT_TRUE( absl::ParseTime("%Y-%m-%d %H:%M:%S", "2013-06-28 19:08:09", tz, &t, &e)) << e; - absl::Time::Breakdown bd = t.In(tz); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -7 * 60 * 60, true); - EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); + auto ci = tz.At(t); + EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); + EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); // But the timezone is ignored when a UTC offset is present. EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z", "2013-06-28 19:08:09 +0800", tz, &t, &e)) << e; - bd = t.In(absl::FixedTimeZone(8 * 60 * 60)); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, 8 * 60 * 60, false); - EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); + ci = absl::FixedTimeZone(8 * 60 * 60).At(t); + EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); + EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); } TEST(ParseTime, ErrorCases) { @@ -332,15 +332,15 @@ TEST(ParseTime, InfiniteTime) { EXPECT_TRUE(absl::ParseTime("infinite-future %H:%M", "infinite-future 03:04", &t, &err)); EXPECT_NE(absl::InfiniteFuture(), t); - EXPECT_EQ(3, t.In(tz).hour); - EXPECT_EQ(4, t.In(tz).minute); + EXPECT_EQ(3, tz.At(t).cs.hour()); + EXPECT_EQ(4, tz.At(t).cs.minute()); // "infinite-past" as literal std::string EXPECT_TRUE( absl::ParseTime("infinite-past %H:%M", "infinite-past 03:04", &t, &err)); EXPECT_NE(absl::InfinitePast(), t); - EXPECT_EQ(3, t.In(tz).hour); - EXPECT_EQ(4, t.In(tz).minute); + EXPECT_EQ(3, tz.At(t).cs.hour()); + EXPECT_EQ(4, tz.At(t).cs.minute()); // The input doesn't match the format. EXPECT_FALSE(absl::ParseTime("infinite-future %H:%M", "03:04", &t, &err)); @@ -365,16 +365,17 @@ TEST(ParseTime, FailsOnUnrepresentableTime) { // TEST(FormatParse, RoundTrip) { - const absl::TimeZone gst = + const absl::TimeZone lax = absl::time_internal::LoadTimeZone("America/Los_Angeles"); - const absl::Time in = absl::FromDateTime(1977, 6, 28, 9, 8, 7, gst); + const absl::Time in = + absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax); const absl::Duration subseconds = absl::Nanoseconds(654321); std::string err; // RFC3339, which renders subseconds. { absl::Time out; - const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, gst); + const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, lax); EXPECT_TRUE(absl::ParseTime(absl::RFC3339_full, s, &out, &err)) << s << ": " << err; EXPECT_EQ(in + subseconds, out); // RFC3339_full includes %Ez @@ -383,7 +384,7 @@ TEST(FormatParse, RoundTrip) { // RFC1123, which only does whole seconds. { absl::Time out; - const std::string s = absl::FormatTime(absl::RFC1123_full, in, gst); + const std::string s = absl::FormatTime(absl::RFC1123_full, in, lax); EXPECT_TRUE(absl::ParseTime(absl::RFC1123_full, s, &out, &err)) << s << ": " << err; EXPECT_EQ(in, out); // RFC1123_full includes %z diff --git a/absl/time/internal/cctz/src/time_zone_posix.h b/absl/time/internal/cctz/src/time_zone_posix.h index 9ccd4a8b..ef2a8c16 100644 --- a/absl/time/internal/cctz/src/time_zone_posix.h +++ b/absl/time/internal/cctz/src/time_zone_posix.h @@ -68,25 +68,35 @@ namespace cctz { // it would take us to another day, and perhaps week, or even month. struct PosixTransition { enum DateFormat { J, N, M }; - struct { + + struct Date { + struct NonLeapDay { + std::int_fast16_t day; // day of non-leap year [1:365] + }; + struct Day { + std::int_fast16_t day; // day of year [0:365] + }; + struct MonthWeekWeekday { + std::int_fast8_t month; // month of year [1:12] + std::int_fast8_t week; // week of month [1:5] (5==last) + std::int_fast8_t weekday; // 0==Sun, ..., 6=Sat + }; + DateFormat fmt; + union { - struct { - std::int_fast16_t day; // day of non-leap year [1:365] - } j; - struct { - std::int_fast16_t day; // day of year [0:365] - } n; - struct { - std::int_fast8_t month; // month of year [1:12] - std::int_fast8_t week; // week of month [1:5] (5==last) - std::int_fast8_t weekday; // 0==Sun, ..., 6=Sat - } m; + NonLeapDay j; + Day n; + MonthWeekWeekday m; }; - } date; - struct { + }; + + struct Time { std::int_fast32_t offset; // seconds before/after 00:00:00 - } time; + }; + + Date date; + Time time; }; // The entirety of a POSIX-string specified time-zone rule. The standard diff --git a/absl/time/internal/test_util.cc b/absl/time/internal/test_util.cc index bbbef7da..4483f2a9 100644 --- a/absl/time/internal/test_util.cc +++ b/absl/time/internal/test_util.cc @@ -26,12 +26,6 @@ namespace cctz = absl::time_internal::cctz; namespace absl { namespace time_internal { -#if GTEST_USES_SIMPLE_RE -extern const char kZoneAbbrRE[] = ".*"; // just punt -#else -extern const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?"; -#endif - TimeZone LoadTimeZone(const std::string& name) { TimeZone tz; ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str()); diff --git a/absl/time/internal/test_util.h b/absl/time/internal/test_util.h index 8fd5fb9f..d9940293 100644 --- a/absl/time/internal/test_util.h +++ b/absl/time/internal/test_util.h @@ -17,35 +17,11 @@ #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "absl/time/time.h" -// This helper is a macro so that failed expectations show up with the -// correct line numbers. -// -// This is for internal testing of the Base Time library itself. This is not -// part of a public API. -#define ABSL_INTERNAL_EXPECT_TIME(bd, y, m, d, h, min, s, off, isdst) \ - do { \ - EXPECT_EQ(y, bd.year); \ - EXPECT_EQ(m, bd.month); \ - EXPECT_EQ(d, bd.day); \ - EXPECT_EQ(h, bd.hour); \ - EXPECT_EQ(min, bd.minute); \ - EXPECT_EQ(s, bd.second); \ - EXPECT_EQ(off, bd.offset); \ - EXPECT_EQ(isdst, bd.is_dst); \ - EXPECT_THAT(bd.zone_abbr, \ - testing::MatchesRegex(absl::time_internal::kZoneAbbrRE)); \ - } while (0) - namespace absl { namespace time_internal { -// A regular expression that matches all zone abbreviations (%Z). -extern const char kZoneAbbrRE[]; - // Loads the named timezone, but dies on any failure. absl::TimeZone LoadTimeZone(const std::string& name); diff --git a/absl/time/time.cc b/absl/time/time.cc index 71fd8ee6..0703856f 100644 --- a/absl/time/time.cc +++ b/absl/time/time.cc @@ -22,13 +22,14 @@ // NOTE: To keep type verbosity to a minimum, the following variable naming // conventions are used throughout this file. // -// cz: A cctz::time_zone // tz: An absl::TimeZone +// ci: An absl::TimeZone::CivilInfo +// ti: An absl::TimeZone::TimeInfo +// cd: An absl::CivilDay or a cctz::civil_day +// cs: An absl::CivilSecond or a cctz::civil_second +// bd: An absl::Time::Breakdown // cl: A cctz::time_zone::civil_lookup // al: A cctz::time_zone::absolute_lookup -// cd: A cctz::civil_day -// cs: A cctz::civil_second -// bd: An absl::Time::Breakdown #include "absl/time/time.h" @@ -75,7 +76,7 @@ inline absl::Time::Breakdown InfiniteFutureBreakdown() { return bd; } -inline Time::Breakdown InfinitePastBreakdown() { +inline absl::Time::Breakdown InfinitePastBreakdown() { Time::Breakdown bd; bd.year = std::numeric_limits::min(); bd.month = 1; @@ -92,6 +93,26 @@ inline Time::Breakdown InfinitePastBreakdown() { return bd; } +inline absl::TimeZone::CivilInfo InfiniteFutureCivilInfo() { + TimeZone::CivilInfo ci; + ci.cs = CivilSecond::max(); + ci.subsecond = InfiniteDuration(); + ci.offset = 0; + ci.is_dst = false; + ci.zone_abbr = "-00"; + return ci; +} + +inline absl::TimeZone::CivilInfo InfinitePastCivilInfo() { + TimeZone::CivilInfo ci; + ci.cs = CivilSecond::min(); + ci.subsecond = -InfiniteDuration(); + ci.offset = 0; + ci.is_dst = false; + ci.zone_abbr = "-00"; + return ci; +} + inline absl::TimeConversion InfiniteFutureTimeConversion() { absl::TimeConversion tc; tc.pre = tc.trans = tc.post = absl::InfiniteFuture(); @@ -134,19 +155,6 @@ Time MakeTimeWithOverflow(const cctz::time_point& sec, return time_internal::FromUnixDuration(time_internal::MakeDuration(hi)); } -inline absl::TimeConversion::Kind MapKind( - const cctz::time_zone::civil_lookup::civil_kind& kind) { - switch (kind) { - case cctz::time_zone::civil_lookup::UNIQUE: - return absl::TimeConversion::UNIQUE; - case cctz::time_zone::civil_lookup::SKIPPED: - return absl::TimeConversion::SKIPPED; - case cctz::time_zone::civil_lookup::REPEATED: - return absl::TimeConversion::REPEATED; - } - return absl::TimeConversion::UNIQUE; -} - // Returns Mon=1..Sun=7. inline int MapWeekday(const cctz::weekday& wd) { switch (wd) { @@ -170,9 +178,13 @@ inline int MapWeekday(const cctz::weekday& wd) { } // namespace +// +// Time +// + absl::Time::Breakdown Time::In(absl::TimeZone tz) const { - if (*this == absl::InfiniteFuture()) return absl::InfiniteFutureBreakdown(); - if (*this == absl::InfinitePast()) return absl::InfinitePastBreakdown(); + if (*this == absl::InfiniteFuture()) return InfiniteFutureBreakdown(); + if (*this == absl::InfinitePast()) return InfinitePastBreakdown(); const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(rep_)); const auto al = cctz::time_zone(tz).lookup(tp); @@ -187,92 +199,18 @@ absl::Time::Breakdown Time::In(absl::TimeZone tz) const { bd.minute = cs.minute(); bd.second = cs.second(); bd.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(rep_)); - bd.weekday = MapWeekday(get_weekday(cd)); - bd.yearday = get_yearday(cd); + bd.weekday = MapWeekday(cctz::get_weekday(cd)); + bd.yearday = cctz::get_yearday(cd); bd.offset = al.offset; bd.is_dst = al.is_dst; bd.zone_abbr = al.abbr; return bd; } -absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) { - const auto cz = cctz::time_zone(tz); - const auto cs = - cctz::civil_second(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); - const auto cl = cz.lookup(cs); - const auto tp = tm.tm_isdst == 0 ? cl.post : cl.pre; - return MakeTimeWithOverflow(tp, cs, cz); -} - -struct tm ToTM(absl::Time t, absl::TimeZone tz) { - const absl::Time::Breakdown bd = t.In(tz); - struct tm tm; - std::memset(&tm, 0, sizeof(tm)); - tm.tm_sec = bd.second; - tm.tm_min = bd.minute; - tm.tm_hour = bd.hour; - tm.tm_mday = bd.day; - tm.tm_mon = bd.month - 1; - - // Saturates tm.tm_year in cases of over/underflow, accounting for the fact - // that tm.tm_year is years since 1900. - if (bd.year < std::numeric_limits::min() + 1900) { - tm.tm_year = std::numeric_limits::min(); - } else if (bd.year > std::numeric_limits::max()) { - tm.tm_year = std::numeric_limits::max() - 1900; - } else { - tm.tm_year = static_cast(bd.year - 1900); - } - - tm.tm_wday = bd.weekday % 7; - tm.tm_yday = bd.yearday - 1; - tm.tm_isdst = bd.is_dst ? 1 : 0; - - return tm; -} - // -// Factory functions. +// Conversions from/to other time types. // -absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, - int min, int sec, TimeZone tz) { - // Avoids years that are too extreme for civil_second to normalize. - if (year > 300000000000) return InfiniteFutureTimeConversion(); - if (year < -300000000000) return InfinitePastTimeConversion(); - const auto cz = cctz::time_zone(tz); - const auto cs = cctz::civil_second(year, mon, day, hour, min, sec); - absl::TimeConversion tc; - tc.normalized = year != cs.year() || mon != cs.month() || day != cs.day() || - hour != cs.hour() || min != cs.minute() || sec != cs.second(); - const auto cl = cz.lookup(cs); - // Converts the civil_lookup struct to a TimeConversion. - tc.pre = MakeTimeWithOverflow(cl.pre, cs, cz, &tc.normalized); - tc.trans = MakeTimeWithOverflow(cl.trans, cs, cz, &tc.normalized); - tc.post = MakeTimeWithOverflow(cl.post, cs, cz, &tc.normalized); - tc.kind = MapKind(cl.kind); - return tc; -} - -absl::Time FromDateTime(int64_t year, int mon, int day, int hour, int min, - int sec, TimeZone tz) { - if (year > 300000000000) return InfiniteFuture(); - if (year < -300000000000) return InfinitePast(); - const auto cz = cctz::time_zone(tz); - const auto cs = cctz::civil_second(year, mon, day, hour, min, sec); - const auto cl = cz.lookup(cs); - return MakeTimeWithOverflow(cl.pre, cs, cz); -} - -absl::Time TimeFromTimespec(timespec ts) { - return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts)); -} - -absl::Time TimeFromTimeval(timeval tv) { - return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv)); -} - absl::Time FromUDate(double udate) { return time_internal::FromUnixDuration(absl::Milliseconds(udate)); } @@ -281,10 +219,6 @@ absl::Time FromUniversal(int64_t universal) { return absl::UniversalEpoch() + 100 * absl::Nanoseconds(universal); } -// -// Conversion to other time types. -// - int64_t ToUnixNanos(Time t) { if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 && time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 33 == 0) { @@ -321,6 +255,23 @@ int64_t ToUnixSeconds(Time t) { time_t ToTimeT(Time t) { return absl::ToTimespec(t).tv_sec; } +double ToUDate(Time t) { + return absl::FDivDuration(time_internal::ToUnixDuration(t), + absl::Milliseconds(1)); +} + +int64_t ToUniversal(absl::Time t) { + return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100)); +} + +absl::Time TimeFromTimespec(timespec ts) { + return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts)); +} + +absl::Time TimeFromTimeval(timeval tv) { + return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv)); +} + timespec ToTimespec(Time t) { timespec ts; absl::Duration d = time_internal::ToUnixDuration(t); @@ -359,15 +310,6 @@ timeval ToTimeval(Time t) { return tv; } -double ToUDate(Time t) { - return absl::FDivDuration(time_internal::ToUnixDuration(t), - absl::Milliseconds(1)); -} - -int64_t ToUniversal(absl::Time t) { - return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100)); -} - Time FromChrono(const std::chrono::system_clock::time_point& tp) { return time_internal::FromUnixDuration(time_internal::FromChrono( tp - std::chrono::system_clock::from_time_t(0))); @@ -381,4 +323,141 @@ std::chrono::system_clock::time_point ToChronoTime(absl::Time t) { time_internal::ToChronoDuration(d); } +// +// TimeZone +// + +absl::TimeZone::CivilInfo TimeZone::At(Time t) const { + if (t == absl::InfiniteFuture()) return InfiniteFutureCivilInfo(); + if (t == absl::InfinitePast()) return InfinitePastCivilInfo(); + + const auto ud = time_internal::ToUnixDuration(t); + const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(ud)); + const auto al = cz_.lookup(tp); + + TimeZone::CivilInfo ci; + ci.cs = CivilSecond(al.cs); + ci.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(ud)); + ci.offset = al.offset; + ci.is_dst = al.is_dst; + ci.zone_abbr = al.abbr; + return ci; +} + +absl::TimeZone::TimeInfo TimeZone::At(CivilSecond ct) const { + const cctz::civil_second cs(ct); + const auto cl = cz_.lookup(cs); + + TimeZone::TimeInfo ti; + switch (cl.kind) { + case cctz::time_zone::civil_lookup::UNIQUE: + ti.kind = TimeZone::TimeInfo::UNIQUE; + break; + case cctz::time_zone::civil_lookup::SKIPPED: + ti.kind = TimeZone::TimeInfo::SKIPPED; + break; + case cctz::time_zone::civil_lookup::REPEATED: + ti.kind = TimeZone::TimeInfo::REPEATED; + break; + } + ti.pre = MakeTimeWithOverflow(cl.pre, cs, cz_); + ti.trans = MakeTimeWithOverflow(cl.trans, cs, cz_); + ti.post = MakeTimeWithOverflow(cl.post, cs, cz_); + return ti; +} + +// +// Conversions involving time zones. +// + +absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, + int min, int sec, TimeZone tz) { + // Avoids years that are too extreme for CivilSecond to normalize. + if (year > 300000000000) return InfiniteFutureTimeConversion(); + if (year < -300000000000) return InfinitePastTimeConversion(); + + const CivilSecond cs(year, mon, day, hour, min, sec); + const auto ti = tz.At(cs); + + TimeConversion tc; + tc.pre = ti.pre; + tc.trans = ti.trans; + tc.post = ti.post; + switch (ti.kind) { + case TimeZone::TimeInfo::UNIQUE: + tc.kind = TimeConversion::UNIQUE; + break; + case TimeZone::TimeInfo::SKIPPED: + tc.kind = TimeConversion::SKIPPED; + break; + case TimeZone::TimeInfo::REPEATED: + tc.kind = TimeConversion::REPEATED; + break; + } + tc.normalized = false; + if (year != cs.year() || mon != cs.month() || day != cs.day() || + hour != cs.hour() || min != cs.minute() || sec != cs.second()) { + tc.normalized = true; + } + return tc; +} + +absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) { + const CivilSecond cs(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec); + const auto ti = tz.At(cs); + return tm.tm_isdst == 0 ? ti.post : ti.pre; +} + +struct tm ToTM(absl::Time t, absl::TimeZone tz) { + struct tm tm = {}; + + const auto ci = tz.At(t); + const auto& cs = ci.cs; + tm.tm_sec = cs.second(); + tm.tm_min = cs.minute(); + tm.tm_hour = cs.hour(); + tm.tm_mday = cs.day(); + tm.tm_mon = cs.month() - 1; + + // Saturates tm.tm_year in cases of over/underflow, accounting for the fact + // that tm.tm_year is years since 1900. + if (cs.year() < std::numeric_limits::min() + 1900) { + tm.tm_year = std::numeric_limits::min(); + } else if (cs.year() > std::numeric_limits::max()) { + tm.tm_year = std::numeric_limits::max() - 1900; + } else { + tm.tm_year = static_cast(cs.year() - 1900); + } + + const CivilDay cd(cs); + switch (GetWeekday(cd)) { + case Weekday::sunday: + tm.tm_wday = 0; + break; + case Weekday::monday: + tm.tm_wday = 1; + break; + case Weekday::tuesday: + tm.tm_wday = 2; + break; + case Weekday::wednesday: + tm.tm_wday = 3; + break; + case Weekday::thursday: + tm.tm_wday = 4; + break; + case Weekday::friday: + tm.tm_wday = 5; + break; + case Weekday::saturday: + tm.tm_wday = 6; + break; + } + tm.tm_yday = GetYearDay(cd) - 1; + tm.tm_isdst = ci.is_dst ? 1 : 0; + + return tm; +} + } // namespace absl diff --git a/absl/time/time.h b/absl/time/time.h index 50bf971d..2858da29 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -25,18 +25,29 @@ // * `absl::TimeZone` defines geopolitical time zone regions (as collected // within the IANA Time Zone database (https://www.iana.org/time-zones)). // +// Note: Absolute times are distinct from civil times, which refer to the +// human-scale time commonly represented by `YYYY-MM-DD hh:mm:ss`. The mapping +// between absolute and civil times can be specified by use of time zones +// (`absl::TimeZone` within this API). That is: +// +// Civil Time = F(Absolute Time, Time Zone) +// Absolute Time = G(Civil Time, Time Zone) +// +// See civil_time.h for abstractions related to constructing and manipulating +// civil time. // // Example: // // absl::TimeZone nyc; -// // // LoadTimeZone() may fail so it's always better to check for success. // if (!absl::LoadTimeZone("America/New_York", &nyc)) { // // handle error case // } // // // My flight leaves NYC on Jan 2, 2017 at 03:04:05 -// absl::Time takeoff = absl::FromDateTime(2017, 1, 2, 3, 4, 5, nyc); +// absl::CivilSecond cs(2017, 1, 2, 3, 4, 5); +// absl::Time takeoff = absl::FromCivil(cs, nyc); +// // absl::Duration flight_duration = absl::Hours(21) + absl::Minutes(35); // absl::Time landing = takeoff + flight_duration; // @@ -48,6 +59,7 @@ // "My flight will land in Sydney on %Y-%m-%d at %H:%M:%S", // landing, syd); // + #ifndef ABSL_TIME_TIME_H_ #define ABSL_TIME_TIME_H_ @@ -66,6 +78,7 @@ #include "absl/base/port.h" // Needed for string vs std::string #include "absl/strings/string_view.h" +#include "absl/time/civil_time.h" #include "absl/time/internal/cctz/include/cctz/time_zone.h" namespace absl { @@ -348,11 +361,11 @@ constexpr Duration InfiniteDuration(); // Factory functions for constructing `Duration` values from an integral number // of the unit indicated by the factory function's name. // -// Note: no "Days()" factory function exists because "a day" is ambiguous. Civil -// days are not always 24 hours long, and a 24-hour duration often does not -// correspond with a civil day. If a 24-hour duration is needed, use -// `absl::Hours(24)`. -// +// Note: no "Days()" factory function exists because "a day" is ambiguous. +// Civil days are not always 24 hours long, and a 24-hour duration often does +// not correspond with a civil day. If a 24-hour duration is needed, use +// `absl::Hours(24)`. (If you actually want a civil day, use absl::CivilDay +// from civil_time.h.) // // Example: // @@ -371,6 +384,7 @@ constexpr Duration Hours(int64_t n); // factories, which should be preferred. // // Example: +// // auto a = absl::Seconds(1.5); // OK // auto b = absl::Milliseconds(1500); // BETTER template = 0> @@ -546,7 +560,7 @@ std::string UnparseFlag(Duration d); // // `absl::Time` uses a resolution that is high enough to avoid loss in // precision, and a range that is wide enough to avoid overflow, when -// converting between tick counts in most Google time scales (i.e., precision +// converting between tick counts in most Google time scales (i.e., resolution // of at least one nanosecond, and range +/-100 billion years). Conversions // between the time scales are performed by truncating (towards negative // infinity) to the nearest representable point. @@ -556,7 +570,6 @@ std::string UnparseFlag(Duration d); // absl::Time t1 = ...; // absl::Time t2 = t1 + absl::Minutes(2); // absl::Duration d = t2 - t1; // == absl::Minutes(2) -// absl::Time::Breakdown bd = t1.In(absl::LocalTimeZone()); // class Time { public: @@ -590,7 +603,10 @@ class Time { // intended to represent an instant in time. So, rather than passing // a `Time::Breakdown` to a function, pass an `absl::Time` and an // `absl::TimeZone`. - struct Breakdown { + // + // Deprecated. Use `absl::TimeZone::CivilInfo`. + struct + Breakdown { int64_t year; // year (e.g., 2013) int month; // month of year [1:12] int day; // day of month [1:31] @@ -614,6 +630,8 @@ class Time { // Time::In() // // Returns the breakdown of this instant in the given TimeZone. + // + // Deprecated. Use `absl::TimeZone::At(Time)`. Breakdown In(TimeZone tz) const; template @@ -679,126 +697,6 @@ constexpr Time InfinitePast() { time_internal::MakeDuration(std::numeric_limits::min(), ~0U)); } -// TimeConversion -// -// An `absl::TimeConversion` represents the conversion of year, month, day, -// hour, minute, and second values (i.e., a civil time), in a particular -// `absl::TimeZone`, to a time instant (an absolute time), as returned by -// `absl::ConvertDateTime()`. (Subseconds must be handled separately.) -// -// It is possible, though, for a caller to try to convert values that -// do not represent an actual or unique instant in time (due to a shift -// in UTC offset in the `absl::TimeZone`, which results in a discontinuity in -// the civil-time components). For example, a daylight-saving-time -// transition skips or repeats civil times---in the United States, March -// 13, 2011 02:15 never occurred, while November 6, 2011 01:15 occurred -// twice---so requests for such times are not well-defined. -// -// To account for these possibilities, `absl::TimeConversion` is richer -// than just a single `absl::Time`. When the civil time is skipped or -// repeated, `absl::ConvertDateTime()` returns times calculated using the -// pre-transition and post-transition UTC offsets, plus the transition -// time itself. -// -// Examples: -// -// absl::TimeZone lax; -// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { -// // handle error case -// } -// -// // A unique civil time -// absl::TimeConversion jan01 = -// absl::ConvertDateTime(2011, 1, 1, 0, 0, 0, lax); -// // jan01.kind == TimeConversion::UNIQUE -// // jan01.pre is 2011/01/01 00:00:00 -0800 -// // jan01.trans is 2011/01/01 00:00:00 -0800 -// // jan01.post is 2011/01/01 00:00:00 -0800 -// -// // A Spring DST transition, when there is a gap in civil time -// absl::TimeConversion mar13 = -// absl::ConvertDateTime(2011, 3, 13, 2, 15, 0, lax); -// // mar13.kind == TimeConversion::SKIPPED -// // mar13.pre is 2011/03/13 03:15:00 -0700 -// // mar13.trans is 2011/03/13 03:00:00 -0700 -// // mar13.post is 2011/03/13 01:15:00 -0800 -// -// // A Fall DST transition, when civil times are repeated -// absl::TimeConversion nov06 = -// absl::ConvertDateTime(2011, 11, 6, 1, 15, 0, lax); -// // nov06.kind == TimeConversion::REPEATED -// // nov06.pre is 2011/11/06 01:15:00 -0700 -// // nov06.trans is 2011/11/06 01:00:00 -0800 -// // nov06.post is 2011/11/06 01:15:00 -0800 -// -// The input month, day, hour, minute, and second values can also be -// outside of their valid ranges, in which case they will be "normalized" -// during the conversion. -// -// Example: -// -// // "October 32" normalizes to "November 1". -// absl::TimeZone tz = absl::LocalTimeZone(); -// absl::TimeConversion tc = -// absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, tz); -// // tc.kind == TimeConversion::UNIQUE && tc.normalized == true -// // tc.pre.In(tz).month == 11 && tc.pre.In(tz).day == 1 -struct TimeConversion { - Time pre; // time calculated using the pre-transition offset - Time trans; // when the civil-time discontinuity occurred - Time post; // time calculated using the post-transition offset - - enum Kind { - UNIQUE, // the civil time was singular (pre == trans == post) - SKIPPED, // the civil time did not exist - REPEATED, // the civil time was ambiguous - }; - Kind kind; - - bool normalized; // input values were outside their valid ranges -}; - -// ConvertDateTime() -// -// The full generality of a civil time to absl::Time conversion. -TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, - int min, int sec, TimeZone tz); - -// FromDateTime() -// -// A convenience wrapper for `absl::ConvertDateTime()` that simply returns the -// "pre" `absl::Time`. That is, the unique result, or the instant that -// is correct using the pre-transition offset (as if the transition -// never happened). This is typically the answer that humans expected when -// faced with non-unique times, such as near daylight-saving time transitions. -// -// Example: -// -// absl::TimeZone seattle; -// if (!absl::LoadTimeZone("America/Los_Angeles", &seattle)) { -// // handle error case -// } -// absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, seattle); -Time FromDateTime(int64_t year, int mon, int day, int hour, int min, int sec, - TimeZone tz); - -// FromTM() -// -// Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and -// `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3) -// for a description of the expected values of the tm fields. IFF the indicated -// time instant is not unique (see `absl::ConvertDateTime()` above), the -// `tm_isdst` field is consulted to select the desired instant (`tm_isdst` > 0 -// means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0 means use the default -// like `absl::FromDateTime()`). -Time FromTM(const struct tm& tm, TimeZone tz); - -// ToTM() -// -// Converts the given `absl::Time` to a struct tm using the given time zone. -// See ctime(3) for a description of the values of the tm fields. -struct tm ToTM(Time t, TimeZone tz); - // FromUnixNanos() // FromUnixMicros() // FromUnixMillis() @@ -883,6 +781,340 @@ Time FromChrono(const std::chrono::system_clock::time_point& tp); // // tp == std::chrono::system_clock::from_time_t(123); std::chrono::system_clock::time_point ToChronoTime(Time); +// Support for flag values of type Time. Time flags must be specified in a +// format that matches absl::RFC3339_full. For example: +// +// --start_time=2016-01-02T03:04:05.678+08:00 +// +// Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required. +// +// Additionally, if you'd like to specify a time as a count of +// seconds/milliseconds/etc from the Unix epoch, use an absl::Duration flag +// and add that duration to absl::UnixEpoch() to get an absl::Time. +bool ParseFlag(const std::string& text, Time* t, std::string* error); +std::string UnparseFlag(Time t); + +// TimeZone +// +// The `absl::TimeZone` is an opaque, small, value-type class representing a +// geo-political region within which particular rules are used for converting +// between absolute and civil times (see https://git.io/v59Ly). `absl::TimeZone` +// values are named using the TZ identifiers from the IANA Time Zone Database, +// such as "America/Los_Angeles" or "Australia/Sydney". `absl::TimeZone` values +// are created from factory functions such as `absl::LoadTimeZone()`. Note: +// strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by +// value rather than const reference. +// +// For more on the fundamental concepts of time zones, absolute times, and civil +// times, see https://github.com/google/cctz#fundamental-concepts +// +// Examples: +// +// absl::TimeZone utc = absl::UTCTimeZone(); +// absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60); +// absl::TimeZone loc = absl::LocalTimeZone(); +// absl::TimeZone lax; +// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { +// // handle error case +// } +// +// See also: +// - https://github.com/google/cctz +// - http://www.iana.org/time-zones +// - http://en.wikipedia.org/wiki/Zoneinfo +class TimeZone { + public: + explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {} + TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit. + TimeZone(const TimeZone&) = default; + TimeZone& operator=(const TimeZone&) = default; + + explicit operator time_internal::cctz::time_zone() const { return cz_; } + + std::string name() const { return cz_.name(); } + + // TimeZone::CivilInfo + // + // Information about the civil time corresponding to an absolute time. + // This struct is not intended to represent an instant in time. So, rather + // than passing a `TimeZone::CivilInfo` to a function, pass an `absl::Time` + // and an `absl::TimeZone`. + struct CivilInfo { + CivilSecond cs; + Duration subsecond; + + // Note: The following fields exist for backward compatibility + // with older APIs. Accessing these fields directly is a sign of + // imprudent logic in the calling code. Modern time-related code + // should only access this data indirectly by way of FormatTime(). + // These fields are undefined for InfiniteFuture() and InfinitePast(). + int offset; // seconds east of UTC + bool is_dst; // is offset non-standard? + const char* zone_abbr; // time-zone abbreviation (e.g., "PST") + }; + + // TimeZone::At(Time) + // + // Returns the civil time for this TimeZone at a certain `absl::Time`. + // If the input time is infinite, the output civil second will be set to + // CivilSecond::max() or min(), and the subsecond will be infinite. + // + // Example: + // + // const auto epoch = lax.At(absl::UnixEpoch()); + // // epoch.cs == 1969-12-31 16:00:00 + // // epoch.subsecond == absl::ZeroDuration() + // // epoch.offset == -28800 + // // epoch.is_dst == false + // // epoch.abbr == "PST" + CivilInfo At(Time t) const; + + // TimeZone::TimeInfo + // + // Information about the absolute times corresponding to a civil time. + // (Subseconds must be handled separately.) + // + // It is possible for a caller to pass a civil-time value that does + // not represent an actual or unique instant in time (due to a shift + // in UTC offset in the TimeZone, which results in a discontinuity in + // the civil-time components). For example, a daylight-saving-time + // transition skips or repeats civil times---in the United States, + // March 13, 2011 02:15 never occurred, while November 6, 2011 01:15 + // occurred twice---so requests for such times are not well-defined. + // To account for these possibilities, `absl::TimeZone::TimeInfo` is + // richer than just a single `absl::Time`. + struct TimeInfo { + enum CivilKind { + UNIQUE, // the civil time was singular (pre == trans == post) + SKIPPED, // the civil time did not exist (pre => trans > post) + REPEATED, // the civil time was ambiguous (pre < trans <= post) + } kind; + Time pre; // time calculated using the pre-transition offset + Time trans; // when the civil-time discontinuity occurred + Time post; // time calculated using the post-transition offset + }; + + // TimeZone::At(CivilSecond) + // + // Returns an `absl::TimeInfo` containing the absolute time(s) for this + // TimeZone at an `absl::CivilSecond`. When the civil time is skipped or + // repeated, returns times calculated using the pre-transition and post- + // transition UTC offsets, plus the transition time itself. + // + // Examples: + // + // // A unique civil time + // const auto jan01 = lax.At(absl::CivilSecond(2011, 1, 1, 0, 0, 0)); + // // jan01.kind == TimeZone::TimeInfo::UNIQUE + // // jan01.pre is 2011-01-01 00:00:00 -0800 + // // jan01.trans is 2011-01-01 00:00:00 -0800 + // // jan01.post is 2011-01-01 00:00:00 -0800 + // + // // A Spring DST transition, when there is a gap in civil time + // const auto mar13 = lax.At(absl::CivilSecond(2011, 3, 13, 2, 15, 0)); + // // mar13.kind == TimeZone::TimeInfo::SKIPPED + // // mar13.pre is 2011-03-13 03:15:00 -0700 + // // mar13.trans is 2011-03-13 03:00:00 -0700 + // // mar13.post is 2011-03-13 01:15:00 -0800 + // + // // A Fall DST transition, when civil times are repeated + // const auto nov06 = lax.At(absl::CivilSecond(2011, 11, 6, 1, 15, 0)); + // // nov06.kind == TimeZone::TimeInfo::REPEATED + // // nov06.pre is 2011-11-06 01:15:00 -0700 + // // nov06.trans is 2011-11-06 01:00:00 -0800 + // // nov06.post is 2011-11-06 01:15:00 -0800 + TimeInfo At(CivilSecond ct) const; + + template + friend H AbslHashValue(H h, TimeZone tz) { + return H::combine(std::move(h), tz.cz_); + } + + private: + friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; } + friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; } + friend std::ostream& operator<<(std::ostream& os, TimeZone tz) { + return os << tz.name(); + } + + time_internal::cctz::time_zone cz_; +}; + +// LoadTimeZone() +// +// Loads the named zone. May perform I/O on the initial load of the named +// zone. If the name is invalid, or some other kind of error occurs, returns +// `false` and `*tz` is set to the UTC time zone. +inline bool LoadTimeZone(const std::string& name, TimeZone* tz) { + if (name == "localtime") { + *tz = TimeZone(time_internal::cctz::local_time_zone()); + return true; + } + time_internal::cctz::time_zone cz; + const bool b = time_internal::cctz::load_time_zone(name, &cz); + *tz = TimeZone(cz); + return b; +} + +// FixedTimeZone() +// +// Returns a TimeZone that is a fixed offset (seconds east) from UTC. +// Note: If the absolute value of the offset is greater than 24 hours +// you'll get UTC (i.e., no offset) instead. +inline TimeZone FixedTimeZone(int seconds) { + return TimeZone( + time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds))); +} + +// UTCTimeZone() +// +// Convenience method returning the UTC time zone. +inline TimeZone UTCTimeZone() { + return TimeZone(time_internal::cctz::utc_time_zone()); +} + +// LocalTimeZone() +// +// Convenience method returning the local time zone, or UTC if there is +// no configured local zone. Warning: Be wary of using LocalTimeZone(), +// and particularly so in a server process, as the zone configured for the +// local machine should be irrelevant. Prefer an explicit zone name. +inline TimeZone LocalTimeZone() { + return TimeZone(time_internal::cctz::local_time_zone()); +} + +// ToCivilSecond() +// ToCivilMinute() +// ToCivilHour() +// ToCivilDay() +// ToCivilMonth() +// ToCivilYear() +// +// Helpers for TimeZone::At(Time) to return particularly aligned civil times. +// +// Example: +// +// absl::Time t = ...; +// absl::TimeZone tz = ...; +// const auto cd = absl::ToCivilDay(t, tz); +inline CivilSecond ToCivilSecond(Time t, TimeZone tz) { + return tz.At(t).cs; // already a CivilSecond +} +inline CivilMinute ToCivilMinute(Time t, TimeZone tz) { + return CivilMinute(tz.At(t).cs); +} +inline CivilHour ToCivilHour(Time t, TimeZone tz) { + return CivilHour(tz.At(t).cs); +} +inline CivilDay ToCivilDay(Time t, TimeZone tz) { + return CivilDay(tz.At(t).cs); +} +inline CivilMonth ToCivilMonth(Time t, TimeZone tz) { + return CivilMonth(tz.At(t).cs); +} +inline CivilYear ToCivilYear(Time t, TimeZone tz) { + return CivilYear(tz.At(t).cs); +} + +// FromCivil() +// +// Helper for TimeZone::At(CivilSecond) that provides "order-preserving +// semantics." If the civil time maps to a unique time, that time is +// returned. If the civil time is repeated in the given time zone, the +// time using the pre-transition offset is returned. Otherwise, the +// civil time is skipped in the given time zone, and the transition time +// is returned. This means that for any two civil times, ct1 and ct2, +// (ct1 < ct2) => (FromCivil(ct1) <= FromCivil(ct2)), the equal case +// being when two non-existent civil times map to the same transition time. +// +// Note: Accepts civil times of any alignment. +inline Time FromCivil(CivilSecond ct, TimeZone tz) { + const auto ti = tz.At(ct); + if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans; + return ti.pre; +} + +// TimeConversion +// +// An `absl::TimeConversion` represents the conversion of year, month, day, +// hour, minute, and second values (i.e., a civil time), in a particular +// `absl::TimeZone`, to a time instant (an absolute time), as returned by +// `absl::ConvertDateTime()`. Lecacy version of `absl::TimeZone::TimeInfo`. +// +// Deprecated. Use `absl::TimeZone::TimeInfo`. +struct + TimeConversion { + Time pre; // time calculated using the pre-transition offset + Time trans; // when the civil-time discontinuity occurred + Time post; // time calculated using the post-transition offset + + enum Kind { + UNIQUE, // the civil time was singular (pre == trans == post) + SKIPPED, // the civil time did not exist + REPEATED, // the civil time was ambiguous + }; + Kind kind; + + bool normalized; // input values were outside their valid ranges +}; + +// ConvertDateTime() +// +// Legacy version of `absl::TimeZone::At(absl::CivilSecond)` that takes +// the civil time as six, separate values (YMDHMS). +// +// The input month, day, hour, minute, and second values can be outside +// of their valid ranges, in which case they will be "normalized" during +// the conversion. +// +// Example: +// +// // "October 32" normalizes to "November 1". +// absl::TimeConversion tc = +// absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, lax); +// // tc.kind == TimeConversion::UNIQUE && tc.normalized == true +// // absl::ToCivilDay(tc.pre, tz).month() == 11 +// // absl::ToCivilDay(tc.pre, tz).day() == 1 +// +// Deprecated. Use `absl::TimeZone::At(CivilSecond)`. +TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, + int min, int sec, TimeZone tz); + +// FromDateTime() +// +// A convenience wrapper for `absl::ConvertDateTime()` that simply returns +// the "pre" `absl::Time`. That is, the unique result, or the instant that +// is correct using the pre-transition offset (as if the transition never +// happened). +// +// Example: +// +// absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, lax); +// // t = 2017-09-26 09:30:00 -0700 +// +// Deprecated. Use `absl::TimeZone::At(CivilSecond).pre`. +inline Time FromDateTime(int64_t year, int mon, int day, int hour, + int min, int sec, TimeZone tz) { + return ConvertDateTime(year, mon, day, hour, min, sec, tz).pre; +} + +// FromTM() +// +// Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and +// `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3) +// for a description of the expected values of the tm fields. If the indicated +// time instant is not unique (see `absl::TimeZone::At(absl::CivilSecond)` +// above), the `tm_isdst` field is consulted to select the desired instant +// (`tm_isdst` > 0 means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0 +// means use the post-transition offset). +Time FromTM(const struct tm& tm, TimeZone tz); + +// ToTM() +// +// Converts the given `absl::Time` to a struct tm using the given time zone. +// See ctime(3) for a description of the values of the tm fields. +struct tm ToTM(Time t, TimeZone tz); + // RFC3339_full // RFC3339_sec // @@ -929,12 +1161,8 @@ extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z // // Example: // -// absl::TimeZone lax; -// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { -// // handle error case -// } -// absl::Time t = absl::FromDateTime(2013, 1, 2, 3, 4, 5, lax); -// +// absl::CivilSecond cs(2013, 1, 2, 3, 4, 5); +// absl::Time t = absl::FromCivil(cs, lax); // string f = absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05" // f = absl::FormatTime("%H:%M:%E3S", t, lax); // "03:04:05.000" // @@ -985,7 +1213,7 @@ inline std::ostream& operator<<(std::ostream& os, Time t) { // in the conversion. // // Date and time fields that are out-of-range will be treated as errors -// rather than normalizing them like `absl::FromDateTime()` does. For example, +// rather than normalizing them like `absl::CivilSecond` does. For example, // it is an error to parse the date "Oct 32, 2013" because 32 is out of range. // // A leap second of ":60" is normalized to ":00" of the following minute @@ -1012,121 +1240,11 @@ bool ParseTime(const std::string& format, const std::string& input, Time* time, // given TimeZone. This means that the input, by itself, does not identify a // unique instant. Being time-zone dependent, it also admits the possibility // of ambiguity or non-existence, in which case the "pre" time (as defined -// for ConvertDateTime()) is returned. For these reasons we recommend that +// by TimeZone::TimeInfo) is returned. For these reasons we recommend that // all date/time strings include a UTC offset so they're context independent. bool ParseTime(const std::string& format, const std::string& input, TimeZone tz, Time* time, std::string* err); -// Support for flag values of type Time. Time flags must be specified in a -// format that matches absl::RFC3339_full. For example: -// -// --start_time=2016-01-02T03:04:05.678+08:00 -// -// Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required. -// -// Additionally, if you'd like to specify a time as a count of -// seconds/milliseconds/etc from the Unix epoch, use an absl::Duration flag -// and add that duration to absl::UnixEpoch() to get an absl::Time. -bool ParseFlag(const std::string& text, Time* t, std::string* error); -std::string UnparseFlag(Time t); - -// TimeZone -// -// The `absl::TimeZone` is an opaque, small, value-type class representing a -// geo-political region within which particular rules are used for converting -// between absolute and civil times (see https://git.io/v59Ly). `absl::TimeZone` -// values are named using the TZ identifiers from the IANA Time Zone Database, -// such as "America/Los_Angeles" or "Australia/Sydney". `absl::TimeZone` values -// are created from factory functions such as `absl::LoadTimeZone()`. Note: -// strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by -// value rather than const reference. -// -// For more on the fundamental concepts of time zones, absolute times, and civil -// times, see https://github.com/google/cctz#fundamental-concepts -// -// Examples: -// -// absl::TimeZone utc = absl::UTCTimeZone(); -// absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60); -// absl::TimeZone loc = absl::LocalTimeZone(); -// absl::TimeZone lax; -// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { -// // handle error case -// } -// -// See also: -// - https://github.com/google/cctz -// - http://www.iana.org/time-zones -// - http://en.wikipedia.org/wiki/Zoneinfo -class TimeZone { - public: - explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {} - TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit. - TimeZone(const TimeZone&) = default; - TimeZone& operator=(const TimeZone&) = default; - - explicit operator time_internal::cctz::time_zone() const { return cz_; } - - std::string name() const { return cz_.name(); } - - template - friend H AbslHashValue(H h, TimeZone tz) { - return H::combine(std::move(h), tz.cz_); - } - - private: - friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; } - friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; } - friend std::ostream& operator<<(std::ostream& os, TimeZone tz) { - return os << tz.name(); - } - - time_internal::cctz::time_zone cz_; -}; - -// LoadTimeZone() -// -// Loads the named zone. May perform I/O on the initial load of the named -// zone. If the name is invalid, or some other kind of error occurs, returns -// `false` and `*tz` is set to the UTC time zone. -inline bool LoadTimeZone(const std::string& name, TimeZone* tz) { - if (name == "localtime") { - *tz = TimeZone(time_internal::cctz::local_time_zone()); - return true; - } - time_internal::cctz::time_zone cz; - const bool b = time_internal::cctz::load_time_zone(name, &cz); - *tz = TimeZone(cz); - return b; -} - -// FixedTimeZone() -// -// Returns a TimeZone that is a fixed offset (seconds east) from UTC. -// Note: If the absolute value of the offset is greater than 24 hours -// you'll get UTC (i.e., no offset) instead. -inline TimeZone FixedTimeZone(int seconds) { - return TimeZone( - time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds))); -} - -// UTCTimeZone() -// -// Convenience method returning the UTC time zone. -inline TimeZone UTCTimeZone() { - return TimeZone(time_internal::cctz::utc_time_zone()); -} - -// LocalTimeZone() -// -// Convenience method returning the local time zone, or UTC if there is -// no configured local zone. Warning: Be wary of using LocalTimeZone(), -// and particularly so in a server process, as the zone configured for the -// local machine should be irrelevant. Prefer an explicit zone name. -inline TimeZone LocalTimeZone() { - return TimeZone(time_internal::cctz::local_time_zone()); -} - // ============================================================================ // Implementation Details Follow // ============================================================================ diff --git a/absl/time/time_benchmark.cc b/absl/time/time_benchmark.cc index e1009946..9bbed6f8 100644 --- a/absl/time/time_benchmark.cc +++ b/absl/time/time_benchmark.cc @@ -169,32 +169,32 @@ void BM_Time_ToUnixSeconds(benchmark::State& state) { BENCHMARK(BM_Time_ToUnixSeconds); // -// FromDateTime +// FromCivil // -// In each "FromDateTime" benchmark we switch between two YMDhms -// values separated by at least one transition in order to defeat any -// internal caching of previous results (e.g., see time_local_hint_). +// In each "FromCivil" benchmark we switch between two YMDhms values +// separated by at least one transition in order to defeat any internal +// caching of previous results (e.g., see time_local_hint_). // // The "UTC" variants use UTC instead of the Google/local time zone. // The "Day0" variants require normalization of the day of month. // -void BM_Time_FromDateTime_Absl(benchmark::State& state) { +void BM_Time_FromCivil_Absl(benchmark::State& state) { const absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/Los_Angeles"); int i = 0; while (state.KeepRunning()) { if ((i & 1) == 0) { - absl::FromDateTime(2014, 12, 18, 20, 16, 18, tz); + absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); } else { - absl::FromDateTime(2013, 11, 15, 18, 30, 27, tz); + absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz); } ++i; } } -BENCHMARK(BM_Time_FromDateTime_Absl); +BENCHMARK(BM_Time_FromCivil_Absl); -void BM_Time_FromDateTime_Libc(benchmark::State& state) { +void BM_Time_FromCivil_Libc(benchmark::State& state) { // No timezone support, so just use localtime. int i = 0; while (state.KeepRunning()) { @@ -219,32 +219,32 @@ void BM_Time_FromDateTime_Libc(benchmark::State& state) { ++i; } } -BENCHMARK(BM_Time_FromDateTime_Libc); +BENCHMARK(BM_Time_FromCivil_Libc); -void BM_Time_FromDateTimeUTC_Absl(benchmark::State& state) { +void BM_Time_FromCivilUTC_Absl(benchmark::State& state) { const absl::TimeZone tz = absl::UTCTimeZone(); while (state.KeepRunning()) { - FromDateTime(2014, 12, 18, 20, 16, 18, tz); + absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); } } -BENCHMARK(BM_Time_FromDateTimeUTC_Absl); +BENCHMARK(BM_Time_FromCivilUTC_Absl); -void BM_Time_FromDateTimeDay0_Absl(benchmark::State& state) { +void BM_Time_FromCivilDay0_Absl(benchmark::State& state) { const absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/Los_Angeles"); int i = 0; while (state.KeepRunning()) { if ((i & 1) == 0) { - absl::FromDateTime(2014, 12, 0, 20, 16, 18, tz); + absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz); } else { - absl::FromDateTime(2013, 11, 0, 18, 30, 27, tz); + absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz); } ++i; } } -BENCHMARK(BM_Time_FromDateTimeDay0_Absl); +BENCHMARK(BM_Time_FromCivilDay0_Absl); -void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { +void BM_Time_FromCivilDay0_Libc(benchmark::State& state) { // No timezone support, so just use localtime. int i = 0; while (state.KeepRunning()) { @@ -269,7 +269,7 @@ void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { ++i; } } -BENCHMARK(BM_Time_FromDateTimeDay0_Libc); +BENCHMARK(BM_Time_FromCivilDay0_Libc); // // To/FromTimespec diff --git a/absl/time/time_norm_test.cc b/absl/time/time_norm_test.cc deleted file mode 100644 index 4436242e..00000000 --- a/absl/time/time_norm_test.cc +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright 2017 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// This file contains tests for FromDateTime() normalization, which is -// time-zone independent so we just use UTC throughout. - -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/time/internal/test_util.h" -#include "absl/time/time.h" - -namespace { - -TEST(TimeNormCase, SimpleOverflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = - absl::ConvertDateTime(2013, 11, 15, 16, 32, 59 + 1, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 33, 0, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16, 59 + 1, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 17, 0, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 23 + 1, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 16, 0, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 30 + 1, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 1, 16, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 12 + 1, 15, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 15, 16, 32, 14, 0, false); -} - -TEST(TimeNormCase, SimpleUnderflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = ConvertDateTime(2013, 11, 15, 16, 32, 0 - 1, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 31, 59, 0, false); - - tc = ConvertDateTime(2013, 11, 15, 16, 0 - 1, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 15, 59, 14, 0, false); - - tc = ConvertDateTime(2013, 11, 15, 0 - 1, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 14, 23, 32, 14, 0, false); - - tc = ConvertDateTime(2013, 11, 1 - 1, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 10, 31, 16, 32, 14, 0, false); - - tc = ConvertDateTime(2013, 1 - 1, 15, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 12, 15, 16, 32, 14, 0, false); -} - -TEST(TimeNormCase, MultipleOverflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - absl::TimeConversion tc = ConvertDateTime(2013, 12, 31, 23, 59, 59 + 1, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 1, 0, 0, 0, 0, false); -} - -TEST(TimeNormCase, MultipleUnderflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - absl::TimeConversion tc = absl::ConvertDateTime(2014, 1, 1, 0, 0, 0 - 1, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 31, 23, 59, 59, 0, false); -} - -TEST(TimeNormCase, OverflowLimits) { - const absl::TimeZone utc = absl::UTCTimeZone(); - absl::TimeConversion tc; - absl::Time::Breakdown bd; - - const int kintmax = std::numeric_limits::max(); - tc = absl::ConvertDateTime(0, kintmax, kintmax, kintmax, kintmax, kintmax, - utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 185085715, 11, 27, 12, 21, 7, 0, false); - - const int kintmin = std::numeric_limits::min(); - tc = absl::ConvertDateTime(0, kintmin, kintmin, kintmin, kintmin, kintmin, - utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, -185085717, 10, 31, 10, 37, 52, 0, false); - - const int64_t max_year = std::numeric_limits::max(); - tc = absl::ConvertDateTime(max_year, 12, 31, 23, 59, 59, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - EXPECT_EQ(absl::InfiniteFuture(), tc.pre); - - const int64_t min_year = std::numeric_limits::min(); - tc = absl::ConvertDateTime(min_year, 1, 1, 0, 0, 0, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - EXPECT_EQ(absl::InfinitePast(), tc.pre); -} - -TEST(TimeNormCase, ComplexOverflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = - ConvertDateTime(2013, 11, 15, 16, 32, 14 + 123456789, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 10, 14, 14, 5, 23, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 + 1234567, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2016, 3, 22, 0, 39, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16 + 123456, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2027, 12, 16, 16, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15 + 1234, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 4, 2, 16, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11 + 123, 15, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2024, 2, 15, 16, 32, 14, 0, false); -} - -TEST(TimeNormCase, ComplexUnderflow) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = - absl::ConvertDateTime(1999, 3, 0, 0, 0, 0, utc); // year 400 - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 2, 28, 0, 0, 0, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16, 32, 14 - 123456789, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2009, 12, 17, 18, 59, 5, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 - 1234567, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2011, 7, 12, 8, 25, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15, 16 - 123456, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 10, 16, 16, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11, 15 - 1234, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2010, 6, 30, 16, 32, 14, 0, false); - - tc = absl::ConvertDateTime(2013, 11 - 123, 15, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2003, 8, 15, 16, 32, 14, 0, false); -} - -TEST(TimeNormCase, Mishmash) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = - absl::ConvertDateTime(2013, 11 - 123, 15 + 1234, 16 - 123456, - 32 + 1234567, 14 - 123456789, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 1991, 5, 9, 3, 6, 5, 0, false); - - tc = absl::ConvertDateTime(2013, 11 + 123, 15 - 1234, 16 + 123456, - 32 - 1234567, 14 + 123456789, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2036, 5, 24, 5, 58, 23, 0, false); - - // Here is a normalization case we got wrong for a while. Because the - // day is converted to "1" within a 400-year (146097-day) period, we - // didn't need to roll the month and so we didn't mark it as normalized. - tc = absl::ConvertDateTime(2013, 11, -146097 + 1, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 1613, 11, 1, 16, 32, 14, 0, false); - - // Even though the month overflow compensates for the day underflow, - // this should still be marked as normalized. - tc = absl::ConvertDateTime(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 1, 16, 32, 14, 0, false); -} - -TEST(TimeNormCase, LeapYears) { - const absl::TimeZone utc = absl::UTCTimeZone(); - - absl::TimeConversion tc = - absl::ConvertDateTime(2013, 2, 28 + 1, 0, 0, 0, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - absl::Time::Breakdown bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 3, 1, 0, 0, 0, 0, false); - - tc = absl::ConvertDateTime(2012, 2, 28 + 1, 0, 0, 0, utc); - EXPECT_FALSE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 2, 29, 0, 0, 0, 0, false); - - tc = absl::ConvertDateTime(2000, 2, 28 + 1, 0, 0, 0, utc); - EXPECT_FALSE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 2000, 2, 29, 0, 0, 0, 0, false); - - tc = absl::ConvertDateTime(1900, 2, 28 + 1, 0, 0, 0, utc); - EXPECT_TRUE(tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - bd = tc.pre.In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, 1900, 3, 1, 0, 0, 0, 0, false); -} - -// Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31) -// and check that they normalize to the expected time. 146097 days span -// the 400-year Gregorian cycle used during normalization. -TEST(TimeNormCase, AllTheDays) { - const absl::TimeZone utc = absl::UTCTimeZone(); - absl::Time exp_time = absl::UnixEpoch(); - - for (int day = 1; day <= 146097; ++day) { - absl::TimeConversion tc = absl::ConvertDateTime(1970, 1, day, 0, 0, 0, utc); - EXPECT_EQ(day > 31, tc.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); - EXPECT_EQ(exp_time, tc.pre); - exp_time += absl::Hours(24); - } -} - -} // namespace diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc index 4f8f58a6..feca4587 100644 --- a/absl/time/time_test.cc +++ b/absl/time/time_test.cc @@ -28,6 +28,27 @@ namespace { +#if GTEST_USES_SIMPLE_RE +const char kZoneAbbrRE[] = ".*"; // just punt +#else +const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?"; +#endif + +// This helper is a macro so that failed expectations show up with the +// correct line numbers. +#define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst) \ + do { \ + EXPECT_EQ(y, ci.cs.year()); \ + EXPECT_EQ(m, ci.cs.month()); \ + EXPECT_EQ(d, ci.cs.day()); \ + EXPECT_EQ(h, ci.cs.hour()); \ + EXPECT_EQ(min, ci.cs.minute()); \ + EXPECT_EQ(s, ci.cs.second()); \ + EXPECT_EQ(off, ci.offset); \ + EXPECT_EQ(isdst, ci.is_dst); \ + EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \ + } while (0) + // A gMock matcher to match timespec values. Use this matcher like: // timespec ts1, ts2; // EXPECT_THAT(ts1, TimespecMatcher(ts2)); @@ -84,10 +105,10 @@ TEST(Time, ValueSemantics) { } TEST(Time, UnixEpoch) { - absl::Time::Breakdown bd = absl::UnixEpoch().In(absl::UTCTimeZone()); - ABSL_INTERNAL_EXPECT_TIME(bd, 1970, 1, 1, 0, 0, 0, 0, false); - EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); - EXPECT_EQ(4, bd.weekday); // Thursday + const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch()); + EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs); + EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); } TEST(Time, Breakdown) { @@ -95,26 +116,26 @@ TEST(Time, Breakdown) { absl::Time t = absl::UnixEpoch(); // The Unix epoch as seen in NYC. - absl::Time::Breakdown bd = t.In(tz); - ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 19, 0, 0, -18000, false); - EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); - EXPECT_EQ(3, bd.weekday); // Wednesday + auto ci = tz.At(t); + EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false); + EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); + EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); // Just before the epoch. t -= absl::Nanoseconds(1); - bd = t.In(tz); - ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 18, 59, 59, -18000, false); - EXPECT_EQ(absl::Nanoseconds(999999999), bd.subsecond); - EXPECT_EQ(3, bd.weekday); // Wednesday + ci = tz.At(t); + EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false); + EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond); + EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); // Some time later. t += absl::Hours(24) * 2735; t += absl::Hours(18) + absl::Minutes(30) + absl::Seconds(15) + absl::Nanoseconds(9); - bd = t.In(tz); - ABSL_INTERNAL_EXPECT_TIME(bd, 1977, 6, 28, 14, 30, 15, -14400, true); - EXPECT_EQ(8, bd.subsecond / absl::Nanoseconds(1)); - EXPECT_EQ(2, bd.weekday); // Tuesday + ci = tz.At(t); + EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true); + EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1)); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(absl::CivilDay(ci.cs))); } TEST(Time, AdditiveOperators) { @@ -550,67 +571,63 @@ TEST(Time, ToChronoTime) { absl::ToChronoTime(absl::UnixEpoch() - tick)); } -TEST(Time, ConvertDateTime) { - const absl::TimeZone utc = absl::UTCTimeZone(); - const absl::TimeZone goog = - absl::time_internal::LoadTimeZone("America/Los_Angeles"); +TEST(Time, TimeZoneAt) { const absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York"); const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)"; - // A simple case of normalization. - absl::TimeConversion oct32 = ConvertDateTime(2013, 10, 32, 8, 30, 0, goog); - EXPECT_TRUE(oct32.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, oct32.kind); - absl::TimeConversion nov01 = ConvertDateTime(2013, 11, 1, 8, 30, 0, goog); - EXPECT_FALSE(nov01.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, nov01.kind); - EXPECT_EQ(oct32.pre, nov01.pre); - EXPECT_EQ("Fri, 1 Nov 2013 08:30:00 -0700 (PDT)", - absl::FormatTime(fmt, nov01.pre, goog)); + // A non-transition where the civil time is unique. + absl::CivilSecond nov01(2013, 11, 1, 8, 30, 0); + const auto nov01_ci = nyc.At(nov01); + EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, nov01_ci.kind); + EXPECT_EQ("Fri, 1 Nov 2013 08:30:00 -0400 (EDT)", + absl::FormatTime(fmt, nov01_ci.pre, nyc)); + EXPECT_EQ(nov01_ci.pre, nov01_ci.trans); + EXPECT_EQ(nov01_ci.pre, nov01_ci.post); + EXPECT_EQ(nov01_ci.pre, absl::FromCivil(nov01, nyc)); // A Spring DST transition, when there is a gap in civil time // and we prefer the later of the possible interpretations of a // non-existent time. - absl::TimeConversion mar13 = ConvertDateTime(2011, 3, 13, 2, 15, 0, nyc); - EXPECT_FALSE(mar13.normalized); - EXPECT_EQ(absl::TimeConversion::SKIPPED, mar13.kind); + absl::CivilSecond mar13(2011, 3, 13, 2, 15, 0); + const auto mar_ci = nyc.At(mar13); + EXPECT_EQ(absl::TimeZone::TimeInfo::SKIPPED, mar_ci.kind); EXPECT_EQ("Sun, 13 Mar 2011 03:15:00 -0400 (EDT)", - absl::FormatTime(fmt, mar13.pre, nyc)); + absl::FormatTime(fmt, mar_ci.pre, nyc)); EXPECT_EQ("Sun, 13 Mar 2011 03:00:00 -0400 (EDT)", - absl::FormatTime(fmt, mar13.trans, nyc)); + absl::FormatTime(fmt, mar_ci.trans, nyc)); EXPECT_EQ("Sun, 13 Mar 2011 01:15:00 -0500 (EST)", - absl::FormatTime(fmt, mar13.post, nyc)); - EXPECT_EQ(mar13.pre, absl::FromDateTime(2011, 3, 13, 2, 15, 0, nyc)); + absl::FormatTime(fmt, mar_ci.post, nyc)); + EXPECT_EQ(mar_ci.trans, absl::FromCivil(mar13, nyc)); // A Fall DST transition, when civil times are repeated and // we prefer the earlier of the possible interpretations of an // ambiguous time. - absl::TimeConversion nov06 = ConvertDateTime(2011, 11, 6, 1, 15, 0, nyc); - EXPECT_FALSE(nov06.normalized); - EXPECT_EQ(absl::TimeConversion::REPEATED, nov06.kind); + absl::CivilSecond nov06(2011, 11, 6, 1, 15, 0); + const auto nov06_ci = nyc.At(nov06); + EXPECT_EQ(absl::TimeZone::TimeInfo::REPEATED, nov06_ci.kind); EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0400 (EDT)", - absl::FormatTime(fmt, nov06.pre, nyc)); + absl::FormatTime(fmt, nov06_ci.pre, nyc)); EXPECT_EQ("Sun, 6 Nov 2011 01:00:00 -0500 (EST)", - absl::FormatTime(fmt, nov06.trans, nyc)); + absl::FormatTime(fmt, nov06_ci.trans, nyc)); EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0500 (EST)", - absl::FormatTime(fmt, nov06.post, nyc)); - EXPECT_EQ(nov06.pre, absl::FromDateTime(2011, 11, 6, 1, 15, 0, nyc)); + absl::FormatTime(fmt, nov06_ci.post, nyc)); + EXPECT_EQ(nov06_ci.pre, absl::FromCivil(nov06, nyc)); // Check that (time_t) -1 is handled correctly. - absl::TimeConversion minus1 = ConvertDateTime(1969, 12, 31, 18, 59, 59, nyc); - EXPECT_FALSE(minus1.normalized); - EXPECT_EQ(absl::TimeConversion::UNIQUE, minus1.kind); - EXPECT_EQ(-1, absl::ToTimeT(minus1.pre)); + absl::CivilSecond minus1(1969, 12, 31, 18, 59, 59); + const auto minus1_cl = nyc.At(minus1); + EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, minus1_cl.kind); + EXPECT_EQ(-1, absl::ToTimeT(minus1_cl.pre)); EXPECT_EQ("Wed, 31 Dec 1969 18:59:59 -0500 (EST)", - absl::FormatTime(fmt, minus1.pre, nyc)); + absl::FormatTime(fmt, minus1_cl.pre, nyc)); EXPECT_EQ("Wed, 31 Dec 1969 23:59:59 +0000 (UTC)", - absl::FormatTime(fmt, minus1.pre, utc)); + absl::FormatTime(fmt, minus1_cl.pre, absl::UTCTimeZone())); } -// FromDateTime(year, mon, day, hour, min, sec, UTCTimeZone()) has -// a specialized fastpath implementation which we exercise here. -TEST(Time, FromDateTimeUTC) { +// FromCivil(CivilSecond(year, mon, day, hour, min, sec), UTCTimeZone()) +// has a specialized fastpath implementation, which we exercise here. +TEST(Time, FromCivilUTC) { const absl::TimeZone utc = absl::UTCTimeZone(); const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)"; const int kMax = std::numeric_limits::max(); @@ -618,65 +635,36 @@ TEST(Time, FromDateTimeUTC) { absl::Time t; // 292091940881 is the last positive year to use the fastpath. - t = absl::FromDateTime(292091940881, kMax, kMax, kMax, kMax, kMax, utc); + t = absl::FromCivil( + absl::CivilSecond(292091940881, kMax, kMax, kMax, kMax, kMax), utc); EXPECT_EQ("Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - t = absl::FromDateTime(292091940882, kMax, kMax, kMax, kMax, kMax, utc); - EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc)); // no overflow - t = absl::FromDateTime( - std::numeric_limits::max(), kMax, kMax, kMax, kMax, kMax, utc); + t = absl::FromCivil( + absl::CivilSecond(292091940882, kMax, kMax, kMax, kMax, kMax), utc); EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc)); // no overflow // -292091936940 is the last negative year to use the fastpath. - t = absl::FromDateTime(-292091936940, kMin, kMin, kMin, kMin, kMin, utc); + t = absl::FromCivil( + absl::CivilSecond(-292091936940, kMin, kMin, kMin, kMin, kMin), utc); EXPECT_EQ("Fri, 1 Nov -292277022657 10:37:52 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - t = absl::FromDateTime(-292091936941, kMin, kMin, kMin, kMin, kMin, utc); + t = absl::FromCivil( + absl::CivilSecond(-292091936941, kMin, kMin, kMin, kMin, kMin), utc); EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc)); // no underflow - t = absl::FromDateTime( - std::numeric_limits::min(), kMin, kMin, kMin, kMin, kMin, utc); - EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc)); // no overflow // Check that we're counting leap years correctly. - t = absl::FromDateTime(1900, 2, 28, 23, 59, 59, utc); + t = absl::FromCivil(absl::CivilSecond(1900, 2, 28, 23, 59, 59), utc); EXPECT_EQ("Wed, 28 Feb 1900 23:59:59 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - t = absl::FromDateTime(1900, 3, 1, 0, 0, 0, utc); + t = absl::FromCivil(absl::CivilSecond(1900, 3, 1, 0, 0, 0), utc); EXPECT_EQ("Thu, 1 Mar 1900 00:00:00 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - t = absl::FromDateTime(2000, 2, 29, 23, 59, 59, utc); + t = absl::FromCivil(absl::CivilSecond(2000, 2, 29, 23, 59, 59), utc); EXPECT_EQ("Tue, 29 Feb 2000 23:59:59 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - t = absl::FromDateTime(2000, 3, 1, 0, 0, 0, utc); + t = absl::FromCivil(absl::CivilSecond(2000, 3, 1, 0, 0, 0), utc); EXPECT_EQ("Wed, 1 Mar 2000 00:00:00 +0000 (UTC)", absl::FormatTime(fmt, t, utc)); - - // Check normalization. - const std::string ymdhms = "%Y-%m-%d %H:%M:%S"; - t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc); - EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc); - EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc); - EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc); - EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc); - EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc); - EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc); - EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc); - EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc); - EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc); - EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc); - EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); - t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc); - EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc)); } TEST(Time, ToTM) { @@ -684,8 +672,10 @@ TEST(Time, ToTM) { // Compares the results of ToTM() to gmtime_r() for lots of times over the // course of a few days. - const absl::Time start = absl::FromDateTime(2014, 1, 2, 3, 4, 5, utc); - const absl::Time end = absl::FromDateTime(2014, 1, 5, 3, 4, 5, utc); + const absl::Time start = + absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc); + const absl::Time end = + absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc); for (absl::Time t = start; t < end; t += absl::Seconds(30)) { const struct tm tm_bt = ToTM(t, utc); const time_t tt = absl::ToTimeT(t); @@ -711,12 +701,12 @@ TEST(Time, ToTM) { // Checks that the tm_isdst field is correct when in standard time. const absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York"); - absl::Time t = absl::FromDateTime(2014, 3, 1, 0, 0, 0, nyc); + absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc); struct tm tm = ToTM(t, nyc); EXPECT_FALSE(tm.tm_isdst); // Checks that the tm_isdst field is correct when in daylight time. - t = absl::FromDateTime(2014, 4, 1, 0, 0, 0, nyc); + t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc); tm = ToTM(t, nyc); EXPECT_TRUE(tm.tm_isdst); @@ -808,8 +798,8 @@ TEST(Time, TMRoundTrip) { absl::time_internal::LoadTimeZone("America/New_York"); // Test round-tripping across a skipped transition - absl::Time start = absl::FromDateTime(2014, 3, 9, 0, 0, 0, nyc); - absl::Time end = absl::FromDateTime(2014, 3, 9, 4, 0, 0, nyc); + absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc); + absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc); for (absl::Time t = start; t < end; t += absl::Minutes(1)) { struct tm tm = ToTM(t, nyc); absl::Time rt = FromTM(tm, nyc); @@ -817,8 +807,8 @@ TEST(Time, TMRoundTrip) { } // Test round-tripping across an ambiguous transition - start = absl::FromDateTime(2014, 11, 2, 0, 0, 0, nyc); - end = absl::FromDateTime(2014, 11, 2, 4, 0, 0, nyc); + start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc); + end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc); for (absl::Time t = start; t < end; t += absl::Minutes(1)) { struct tm tm = ToTM(t, nyc); absl::Time rt = FromTM(tm, nyc); @@ -826,8 +816,8 @@ TEST(Time, TMRoundTrip) { } // Test round-tripping of unique instants crossing a day boundary - start = absl::FromDateTime(2014, 6, 27, 22, 0, 0, nyc); - end = absl::FromDateTime(2014, 6, 28, 4, 0, 0, nyc); + start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc); + end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc); for (absl::Time t = start; t < end; t += absl::Minutes(1)) { struct tm tm = ToTM(t, nyc); absl::Time rt = FromTM(tm, nyc); @@ -980,27 +970,27 @@ TEST(Time, ConversionSaturation) { EXPECT_EQ(min_timespec_sec, ts.tv_sec); EXPECT_EQ(0, ts.tv_nsec); - // Checks how Time::In() saturates on infinities. - absl::Time::Breakdown bd = absl::InfiniteFuture().In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits::max(), 12, 31, 23, + // Checks how TimeZone::At() saturates on infinities. + auto ci = utc.At(absl::InfiniteFuture()); + EXPECT_CIVIL_INFO(ci, std::numeric_limits::max(), 12, 31, 23, 59, 59, 0, false); - EXPECT_EQ(absl::InfiniteDuration(), bd.subsecond); - EXPECT_EQ(4, bd.weekday); // Thursday - EXPECT_EQ(365, bd.yearday); - EXPECT_STREQ("-00", bd.zone_abbr); // artifact of absl::Time::In() - bd = absl::InfinitePast().In(utc); - ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits::min(), 1, 1, 0, 0, + EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(365, absl::GetYearDay(absl::CivilDay(ci.cs))); + EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At() + ci = utc.At(absl::InfinitePast()); + EXPECT_CIVIL_INFO(ci, std::numeric_limits::min(), 1, 1, 0, 0, 0, 0, false); - EXPECT_EQ(-absl::InfiniteDuration(), bd.subsecond); - EXPECT_EQ(7, bd.weekday); // Sunday - EXPECT_EQ(1, bd.yearday); - EXPECT_STREQ("-00", bd.zone_abbr); // artifact of absl::Time::In() + EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond); + EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(1, absl::GetYearDay(absl::CivilDay(ci.cs))); + EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At() // Approach the maximal Time value from below. - t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 6, utc); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 6), utc); EXPECT_EQ("292277026596-12-04T15:30:06+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); - t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 7, utc); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 7), utc); EXPECT_EQ("292277026596-12-04T15:30:07+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); EXPECT_EQ( @@ -1008,21 +998,21 @@ TEST(Time, ConversionSaturation) { // Checks that we can also get the maximal Time value for a far-east zone. const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60); - t = absl::FromDateTime(292277026596, 12, 5, 5, 30, 7, plus14); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 5, 30, 7), plus14); EXPECT_EQ("292277026596-12-05T05:30:07+14:00", absl::FormatTime(absl::RFC3339_full, t, plus14)); EXPECT_EQ( absl::UnixEpoch() + absl::Seconds(std::numeric_limits::max()), t); // One second later should push us to infinity. - t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 8, utc); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc); EXPECT_EQ("infinite-future", absl::FormatTime(absl::RFC3339_full, t, utc)); // Approach the minimal Time value from above. - t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 53, utc); + t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 53), utc); EXPECT_EQ("-292277022657-01-27T08:29:53+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); - t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 52, utc); + t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 52), utc); EXPECT_EQ("-292277022657-01-27T08:29:52+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); EXPECT_EQ( @@ -1030,14 +1020,15 @@ TEST(Time, ConversionSaturation) { // Checks that we can also get the minimal Time value for a far-west zone. const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60); - t = absl::FromDateTime(-292277022657, 1, 26, 20, 29, 52, minus12); + t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 26, 20, 29, 52), + minus12); EXPECT_EQ("-292277022657-01-26T20:29:52-12:00", absl::FormatTime(absl::RFC3339_full, t, minus12)); EXPECT_EQ( absl::UnixEpoch() + absl::Seconds(std::numeric_limits::min()), t); // One second before should push us to -infinity. - t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 51, utc); + t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc); EXPECT_EQ("infinite-past", absl::FormatTime(absl::RFC3339_full, t, utc)); } @@ -1051,38 +1042,97 @@ TEST(Time, ExtendedConversionSaturation) { absl::time_internal::LoadTimeZone("America/New_York"); const absl::Time max = absl::FromUnixSeconds(std::numeric_limits::max()); - absl::Time::Breakdown bd; + absl::TimeZone::CivilInfo ci; absl::Time t; // The maximal time converted in each zone. - bd = max.In(syd); - ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 5, 2, 30, 7, 39600, true); - t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 7, syd); + ci = syd.At(max); + EXPECT_CIVIL_INFO(ci, 292277026596, 12, 5, 2, 30, 7, 39600, true); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 7), syd); EXPECT_EQ(max, t); - bd = max.In(nyc); - ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 4, 10, 30, 7, -18000, false); - t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 7, nyc); + ci = nyc.At(max); + EXPECT_CIVIL_INFO(ci, 292277026596, 12, 4, 10, 30, 7, -18000, false); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 7), nyc); EXPECT_EQ(max, t); // One second later should push us to infinity. - t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 8, syd); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 8), syd); EXPECT_EQ(absl::InfiniteFuture(), t); - t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 8, nyc); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 8), nyc); EXPECT_EQ(absl::InfiniteFuture(), t); // And we should stick there. - t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 9, syd); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 9), syd); EXPECT_EQ(absl::InfiniteFuture(), t); - t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 9, nyc); + t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 9), nyc); EXPECT_EQ(absl::InfiniteFuture(), t); // All the way up to a saturated date/time, without overflow. - t = absl::FromDateTime( - std::numeric_limits::max(), 12, 31, 23, 59, 59, syd); + t = absl::FromCivil(absl::CivilSecond::max(), syd); EXPECT_EQ(absl::InfiniteFuture(), t); - t = absl::FromDateTime( - std::numeric_limits::max(), 12, 31, 23, 59, 59, nyc); + t = absl::FromCivil(absl::CivilSecond::max(), nyc); EXPECT_EQ(absl::InfiniteFuture(), t); } +TEST(Time, FromCivilAlignment) { + const absl::TimeZone utc = absl::UTCTimeZone(); + const absl::CivilSecond cs(2015, 2, 3, 4, 5, 6); + absl::Time t = absl::FromCivil(cs, utc); + EXPECT_EQ("2015-02-03T04:05:06+00:00", absl::FormatTime(t, utc)); + t = absl::FromCivil(absl::CivilMinute(cs), utc); + EXPECT_EQ("2015-02-03T04:05:00+00:00", absl::FormatTime(t, utc)); + t = absl::FromCivil(absl::CivilHour(cs), utc); + EXPECT_EQ("2015-02-03T04:00:00+00:00", absl::FormatTime(t, utc)); + t = absl::FromCivil(absl::CivilDay(cs), utc); + EXPECT_EQ("2015-02-03T00:00:00+00:00", absl::FormatTime(t, utc)); + t = absl::FromCivil(absl::CivilMonth(cs), utc); + EXPECT_EQ("2015-02-01T00:00:00+00:00", absl::FormatTime(t, utc)); + t = absl::FromCivil(absl::CivilYear(cs), utc); + EXPECT_EQ("2015-01-01T00:00:00+00:00", absl::FormatTime(t, utc)); +} + +TEST(Time, LegacyDateTime) { + const absl::TimeZone utc = absl::UTCTimeZone(); + const std::string ymdhms = "%Y-%m-%d %H:%M:%S"; + const int kMax = std::numeric_limits::max(); + const int kMin = std::numeric_limits::min(); + absl::Time t; + + t = absl::FromDateTime(std::numeric_limits::max(), + kMax, kMax, kMax, kMax, kMax, utc); + EXPECT_EQ("infinite-future", + absl::FormatTime(ymdhms, t, utc)); // no overflow + t = absl::FromDateTime(std::numeric_limits::min(), + kMin, kMin, kMin, kMin, kMin, utc); + EXPECT_EQ("infinite-past", + absl::FormatTime(ymdhms, t, utc)); // no overflow + + // Check normalization. + EXPECT_TRUE(absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, utc).normalized); + t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc); + EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc); + EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc); + EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc); + EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc); + EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc); + EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc); + EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc); + EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc); + EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc); + EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc); + EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); + t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc); + EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc)); +} + } // namespace -- cgit v1.2.3 From 94c298e2a0ae409e283cab96c954a685bd865a70 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 23 Oct 2018 18:09:41 -0700 Subject: Export of internal Abseil changes. -- 441d1aa02483cdc510eb2fef012b31384fd8e3a6 by Eric Fiselier : Fix str_format with non-POSIX libc implementations. PiperOrigin-RevId: 218441122 -- da6190130e74222af6eb161a5593364341370370 by Jon Cohen : Refactor ExceptionSafetyTester::Test in order to remove the levels of indirection related to unpacking tuples. PiperOrigin-RevId: 218403355 GitOrigin-RevId: 441d1aa02483cdc510eb2fef012b31384fd8e3a6 Change-Id: I6f6b978eb96fe261e8ee41ecdce185e5356a601d --- absl/base/exception_safety_testing_test.cc | 12 ++ absl/base/internal/exception_safety_testing.cc | 4 + absl/base/internal/exception_safety_testing.h | 242 +++++++++++-------------- absl/strings/internal/str_format/output.cc | 27 ++- absl/strings/str_format_test.cc | 5 +- 5 files changed, 154 insertions(+), 136 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/exception_safety_testing_test.cc b/absl/base/exception_safety_testing_test.cc index 106bc34b..7518264d 100644 --- a/absl/base/exception_safety_testing_test.cc +++ b/absl/base/exception_safety_testing_test.cc @@ -770,6 +770,18 @@ TEST(ExceptionCheckTest, ModifyingChecker) { .Test(invoker)); } +TEST(ExceptionSafetyTesterTest, ResetsCountdown) { + auto test = + testing::MakeExceptionSafetyTester() + .WithInitialValue(ThrowingValue<>()) + .WithContracts([](ThrowingValue<>*) { return AssertionSuccess(); }) + .WithOperation([](ThrowingValue<>*) {}); + ASSERT_TRUE(test.Test()); + // If the countdown isn't reset because there were no exceptions thrown, then + // this will fail with a termination from an unhandled exception + EXPECT_TRUE(test.Test()); +} + struct NonCopyable : public NonNegative { NonCopyable(const NonCopyable&) = delete; NonCopyable() : NonNegative{0} {} diff --git a/absl/base/internal/exception_safety_testing.cc b/absl/base/internal/exception_safety_testing.cc index f1d081f7..8207b7d7 100644 --- a/absl/base/internal/exception_safety_testing.cc +++ b/absl/base/internal/exception_safety_testing.cc @@ -23,6 +23,10 @@ exceptions_internal::NoThrowTag nothrow_ctor; exceptions_internal::StrongGuaranteeTagType strong_guarantee; +exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester() { + return {}; +} + namespace exceptions_internal { int countdown = -1; diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h index 5665a1b6..429f5c2f 100644 --- a/absl/base/internal/exception_safety_testing.h +++ b/absl/base/internal/exception_safety_testing.h @@ -190,70 +190,6 @@ class TrackedObject { ~TrackedObject() noexcept { ConstructorTracker::ObjectDestructed(this); } }; - -template -absl::optional TestSingleContractAtCountdownImpl( - const Factory& factory, const Operation& operation, int count, - const Contract& contract) { - auto t_ptr = factory(); - absl::optional current_res; - SetCountdown(count); - try { - operation(t_ptr.get()); - } catch (const exceptions_internal::TestException& e) { - current_res.emplace(contract(t_ptr.get())); - if (!current_res.value()) { - *current_res << e.what() << " failed contract check"; - } - } - UnsetCountdown(); - return current_res; -} - -template -absl::optional TestSingleContractAtCountdownImpl( - const Factory& factory, const Operation& operation, int count, - StrongGuaranteeTagType) { - using TPtr = typename decltype(factory())::pointer; - auto t_is_strong = [&](TPtr t) { return *t == *factory(); }; - return TestSingleContractAtCountdownImpl(factory, operation, count, - t_is_strong); -} - -template -int TestSingleContractAtCountdown( - const Factory& factory, const Operation& operation, int count, - const Contract& contract, - absl::optional* reduced_res) { - // If reduced_res is empty, it means the current call to - // TestSingleContractAtCountdown(...) is the first test being run so we do - // want to run it. Alternatively, if it's not empty (meaning a previous test - // has run) we want to check if it passed. If the previous test did pass, we - // want to contine running tests so we do want to run the current one. If it - // failed, we want to short circuit so as not to overwrite the AssertionResult - // output. If that's the case, we do not run the current test and instead we - // simply return. - if (!reduced_res->has_value() || reduced_res->value()) { - *reduced_res = - TestSingleContractAtCountdownImpl(factory, operation, count, contract); - } - return 0; -} - -template -inline absl::optional TestAllContractsAtCountdown( - const Factory& factory, const Operation& operation, int count, - const Contracts&... contracts) { - absl::optional reduced_res; - - // Run each checker, short circuiting after the first failure - int dummy[] = { - 0, (TestSingleContractAtCountdown(factory, operation, count, contracts, - &reduced_res))...}; - static_cast(dummy); - return reduced_res; -} - } // namespace exceptions_internal extern exceptions_internal::NoThrowTag nothrow_ctor; @@ -871,7 +807,7 @@ testing::AssertionResult TestNothrowOp(const Operation& operation) { namespace exceptions_internal { -// Dummy struct for ExceptionSafetyTester<> partial state. +// Dummy struct for ExceptionSafetyTestBuilder<> partial state. struct UninitializedT {}; template @@ -893,20 +829,97 @@ using EnableIfTestable = typename absl::enable_if_t< template -class ExceptionSafetyTester; +class ExceptionSafetyTestBuilder; } // namespace exceptions_internal -exceptions_internal::ExceptionSafetyTester<> MakeExceptionSafetyTester(); +/* + * Constructs an empty ExceptionSafetyTestBuilder. All + * ExceptionSafetyTestBuilder objects are immutable and all With[thing] mutation + * methods return new instances of ExceptionSafetyTestBuilder. + * + * In order to test a T for exception safety, a factory for that T, a testable + * operation, and at least one contract callback returning an assertion + * result must be applied using the respective methods. + */ +exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester(); namespace exceptions_internal { +template +struct IsUniquePtr : std::false_type {}; + +template +struct IsUniquePtr> : std::true_type {}; + +template +struct FactoryPtrTypeHelper { + using type = decltype(std::declval()()); + + static_assert(IsUniquePtr::value, "Factories must return a unique_ptr"); +}; + +template +using FactoryPtrType = typename FactoryPtrTypeHelper::type; + +template +using FactoryElementType = typename FactoryPtrType::element_type; + +template +class ExceptionSafetyTest { + using Factory = std::function()>; + using Operation = std::function; + using Contract = std::function; + + public: + template + explicit ExceptionSafetyTest(const Factory& f, const Operation& op, + const Contracts&... contracts) + : factory_(f), operation_(op), contracts_{WrapContract(contracts)...} {} + + AssertionResult Test() const { + for (int count = 0;; ++count) { + exceptions_internal::ConstructorTracker ct(count); + + for (const auto& contract : contracts_) { + auto t_ptr = factory_(); + try { + SetCountdown(count); + operation_(t_ptr.get()); + // Unset for the case that the operation throws no exceptions, which + // would leave the countdown set and break the *next* exception safety + // test after this one. + UnsetCountdown(); + return AssertionSuccess(); + } catch (const exceptions_internal::TestException& e) { + if (!contract(t_ptr.get())) { + return AssertionFailure() << e.what() << " failed contract check"; + } + } + } + } + } + + private: + template + Contract WrapContract(const ContractFn& contract) { + return [contract](T* t_ptr) { return AssertionResult(contract(t_ptr)); }; + } + + Contract WrapContract(StrongGuaranteeTagType) { + return [this](T* t_ptr) { return AssertionResult(*factory_() == *t_ptr); }; + } + + Factory factory_; + Operation operation_; + std::vector contracts_; +}; /* * Builds a tester object that tests if performing a operation on a T follows * exception safety guarantees. Verification is done via contract assertion * callbacks applied to T instances post-throw. * - * Template parameters for ExceptionSafetyTester: + * Template parameters for ExceptionSafetyTestBuilder: * * - Factory: The factory object (passed in via tester.WithFactory(...) or * tester.WithInitialValue(...)) must be invocable with the signature @@ -933,13 +946,13 @@ namespace exceptions_internal { * please. */ template -class ExceptionSafetyTester { +class ExceptionSafetyTestBuilder { public: /* - * Returns a new ExceptionSafetyTester with an included T factory based on the - * provided T instance. The existing factory will not be included in the newly - * created tester instance. The created factory returns a new T instance by - * copy-constructing the provided const T& t. + * Returns a new ExceptionSafetyTestBuilder with an included T factory based + * on the provided T instance. The existing factory will not be included in + * the newly created tester instance. The created factory returns a new T + * instance by copy-constructing the provided const T& t. * * Preconditions for tester.WithInitialValue(const T& t): * @@ -948,41 +961,41 @@ class ExceptionSafetyTester { * tester.WithFactory(...). */ template - ExceptionSafetyTester, Operation, Contracts...> + ExceptionSafetyTestBuilder, Operation, Contracts...> WithInitialValue(const T& t) const { return WithFactory(DefaultFactory(t)); } /* - * Returns a new ExceptionSafetyTester with the provided T factory included. - * The existing factory will not be included in the newly-created tester - * instance. This method is intended for use with types lacking a copy + * Returns a new ExceptionSafetyTestBuilder with the provided T factory + * included. The existing factory will not be included in the newly-created + * tester instance. This method is intended for use with types lacking a copy * constructor. Types that can be copy-constructed should instead use the * method tester.WithInitialValue(...). */ template - ExceptionSafetyTester, Operation, Contracts...> + ExceptionSafetyTestBuilder, Operation, Contracts...> WithFactory(const NewFactory& new_factory) const { return {new_factory, operation_, contracts_}; } /* - * Returns a new ExceptionSafetyTester with the provided testable operation - * included. The existing operation will not be included in the newly created - * tester. + * Returns a new ExceptionSafetyTestBuilder with the provided testable + * operation included. The existing operation will not be included in the + * newly created tester. */ template - ExceptionSafetyTester, Contracts...> + ExceptionSafetyTestBuilder, Contracts...> WithOperation(const NewOperation& new_operation) const { return {factory_, new_operation, contracts_}; } /* - * Returns a new ExceptionSafetyTester with the provided MoreContracts... + * Returns a new ExceptionSafetyTestBuilder with the provided MoreContracts... * combined with the Contracts... that were already included in the instance * on which the method was called. Contracts... cannot be removed or replaced - * once added to an ExceptionSafetyTester instance. A fresh object must be - * created in order to get an empty Contracts... list. + * once added to an ExceptionSafetyTestBuilder instance. A fresh object must + * be created in order to get an empty Contracts... list. * * In addition to passing in custom contract assertion callbacks, this method * accepts `testing::strong_guarantee` as an argument which checks T instances @@ -991,8 +1004,8 @@ class ExceptionSafetyTester { * properly rolled back. */ template - ExceptionSafetyTester...> + ExceptionSafetyTestBuilder...> WithContracts(const MoreContracts&... more_contracts) const { return { factory_, operation_, @@ -1039,48 +1052,27 @@ class ExceptionSafetyTester { typename LazyOperation = Operation, typename = EnableIfTestable> testing::AssertionResult Test() const { - return TestImpl(operation_, absl::index_sequence_for()); + return Test(operation_); } private: template - friend class ExceptionSafetyTester; + friend class ExceptionSafetyTestBuilder; - friend ExceptionSafetyTester<> testing::MakeExceptionSafetyTester(); + friend ExceptionSafetyTestBuilder<> testing::MakeExceptionSafetyTester(); - ExceptionSafetyTester() {} + ExceptionSafetyTestBuilder() {} - ExceptionSafetyTester(const Factory& f, const Operation& o, - const std::tuple& i) + ExceptionSafetyTestBuilder(const Factory& f, const Operation& o, + const std::tuple& i) : factory_(f), operation_(o), contracts_(i) {} template - testing::AssertionResult TestImpl(const SelectedOperation& selected_operation, + testing::AssertionResult TestImpl(SelectedOperation selected_operation, absl::index_sequence) const { - // Starting from 0 and counting upwards until one of the exit conditions is - // hit... - for (int count = 0;; ++count) { - exceptions_internal::ConstructorTracker ct(count); - - // Run the full exception safety test algorithm for the current countdown - auto reduced_res = - TestAllContractsAtCountdown(factory_, selected_operation, count, - std::get(contracts_)...); - // If there is no value in the optional, no contracts were run because no - // exception was thrown. This means that the test is complete and the loop - // can exit successfully. - if (!reduced_res.has_value()) { - return testing::AssertionSuccess(); - } - // If the optional is not empty and the value is falsy, an contract check - // failed so the test must exit to propegate the failure. - if (!reduced_res.value()) { - return reduced_res.value(); - } - // If the optional is not empty and the value is not falsy, it means - // exceptions were thrown but the contracts passed so the test must - // continue to run. - } + return ExceptionSafetyTest>( + factory_, selected_operation, std::get(contracts_)...) + .Test(); } Factory factory_; @@ -1090,20 +1082,6 @@ class ExceptionSafetyTester { } // namespace exceptions_internal -/* - * Constructs an empty ExceptionSafetyTester. All ExceptionSafetyTester - * objects are immutable and all With[thing] mutation methods return new - * instances of ExceptionSafetyTester. - * - * In order to test a T for exception safety, a factory for that T, a testable - * operation, and at least one contract callback returning an assertion - * result must be applied using the respective methods. - */ -inline exceptions_internal::ExceptionSafetyTester<> -MakeExceptionSafetyTester() { - return {}; -} - } // namespace testing #endif // ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_ diff --git a/absl/strings/internal/str_format/output.cc b/absl/strings/internal/str_format/output.cc index 5c3795b7..d7fef69b 100644 --- a/absl/strings/internal/str_format/output.cc +++ b/absl/strings/internal/str_format/output.cc @@ -20,6 +20,16 @@ namespace absl { namespace str_format_internal { +namespace { +struct ClearErrnoGuard { + ClearErrnoGuard() : old_value(errno) { errno = 0; } + ~ClearErrnoGuard() { + if (!errno) errno = old_value; + } + int old_value; +}; +} // namespace + void BufferRawSink::Write(string_view v) { size_t to_write = std::min(v.size(), size_); std::memcpy(buffer_, v.data(), to_write); @@ -30,14 +40,27 @@ void BufferRawSink::Write(string_view v) { void FILERawSink::Write(string_view v) { while (!v.empty() && !error_) { + // Reset errno to zero in case the libc implementation doesn't set errno + // when a failure occurs. + ClearErrnoGuard guard; + if (size_t result = std::fwrite(v.data(), 1, v.size(), output_)) { // Some progress was made. count_ += result; v.remove_prefix(result); } else { - // Some error occurred. - if (errno != EINTR) { + if (errno == EINTR) { + continue; + } else if (errno) { error_ = errno; + } else if (std::ferror(output_)) { + // Non-POSIX compliant libc implementations may not set errno, so we + // have check the streams error indicator. + error_ = EBADF; + } else { + // We're likely on a non-POSIX system that encountered EINTR but had no + // way of reporting it. + continue; } } } diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index ea9a3a17..87ed234f 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -242,6 +242,7 @@ class TempFile { std::string ReadFile() { std::fseek(file_, 0, SEEK_END); int size = std::ftell(file_); + EXPECT_GT(size, 0); std::rewind(file_); std::string str(2 * size, ' '); int read_bytes = std::fread(&str[0], 1, str.size(), file_); @@ -270,7 +271,7 @@ TEST_F(FormatEntryPointTest, FPrintFError) { EXPECT_EQ(errno, EBADF); } -#if __GNUC__ +#if __GLIBC__ TEST_F(FormatEntryPointTest, FprintfTooLarge) { std::FILE* f = std::fopen("/dev/null", "w"); int width = 2000000000; @@ -297,7 +298,7 @@ TEST_F(FormatEntryPointTest, PrintF) { EXPECT_EQ(result, 30); EXPECT_EQ(tmp.ReadFile(), "STRING: ABC NUMBER: -000000019"); } -#endif // __GNUC__ +#endif // __GLIBC__ TEST_F(FormatEntryPointTest, SNPrintF) { char buffer[16]; -- cgit v1.2.3 From febc5ee6a92d0eb7dac1fceaa6c648cf6521b4dc Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 6 Mar 2019 11:36:55 -0800 Subject: Export of internal Abseil changes. -- f9f068aa8a260dc576398e47b8e4540902e41358 by Derek Mauro : Fix test string with embedded NUL. Currently parses as octal. PiperOrigin-RevId: 237088193 -- d271ffdd3f450f817f6d30e98ff39d439aaf3a98 by Abseil Team : Make symbolizer examine any mapping with read+exec permission regardless of 'w' bit. PiperOrigin-RevId: 237056461 -- af315f8306d36a7367a452fd0b58cafdbf20719d by Abseil Team : Switch comments referencing base:: CondVar and Mutex to absl::. PiperOrigin-RevId: 236917884 -- c624d5d1c0bdb917bff5e651ba40599472f84e0e by Gennadiy Rozental : Internal change PiperOrigin-RevId: 236898300 -- 3cdc82429af964846d1152f49148abc61d196a4b by Samuel Benzaquen : Make the `long double` overload if AbslHashValue a template to avoid invalid conversions with implicit operators. This overload was never meant to capture anything other than `long double` and any current caller to it that wasn't a `long double` is potentially a bug. In particular, any type with an implicit `bool` conversion is calling this overload instead of trying to find a hash<> specialization, thus causing pretty bad hash behavior. PiperOrigin-RevId: 236877073 GitOrigin-RevId: f9f068aa8a260dc576398e47b8e4540902e41358 Change-Id: If9cc008dd814f0ca06ed881f612c06575f1f7137 --- absl/base/internal/exception_safety_testing.h | 6 +- absl/base/internal/raw_logging.h | 10 +-- absl/base/internal/thread_identity.h | 4 +- absl/container/fixed_array_test.cc | 3 +- absl/container/flat_hash_map_test.cc | 6 +- absl/container/inlined_vector_benchmark.cc | 4 +- absl/container/internal/hash_function_defaults.h | 5 +- .../internal/hash_function_defaults_test.cc | 8 +-- absl/container/internal/layout.h | 3 +- absl/container/internal/raw_hash_set.h | 4 +- absl/container/internal/raw_hash_set_test.cc | 15 +++-- absl/container/internal/unordered_set_test.cc | 10 +-- absl/debugging/failure_signal_handler_test.cc | 3 +- absl/debugging/internal/symbolize.h | 7 +-- absl/debugging/symbolize_elf.inc | 16 +++-- absl/hash/hash.h | 2 +- absl/hash/hash_test.cc | 24 ++++++- absl/hash/hash_testing.h | 4 +- absl/hash/internal/hash.h | 9 ++- absl/hash/internal/spy_hash_state.h | 4 +- absl/strings/charconv_test.cc | 3 +- absl/strings/escaping.cc | 14 +++-- absl/strings/escaping_test.cc | 64 ++++++++++--------- absl/strings/internal/ostringstream.h | 16 ++--- absl/strings/internal/resize_uninitialized.h | 6 +- absl/strings/internal/str_format/arg.h | 5 +- absl/strings/internal/str_format/bind.cc | 4 +- absl/strings/internal/str_format/bind.h | 8 +-- absl/strings/internal/str_format/checker_test.cc | 66 +++++++++---------- absl/strings/internal/str_format/extension.h | 2 +- absl/strings/internal/str_format/output_test.cc | 6 -- absl/strings/internal/str_join_internal.h | 13 ++-- absl/strings/internal/str_split_internal.h | 12 ++-- absl/strings/internal/utf8_test.cc | 8 +-- absl/strings/match_test.cc | 4 +- absl/strings/numbers_test.cc | 7 ++- absl/strings/str_cat.cc | 4 +- absl/strings/str_cat.h | 18 +++--- absl/strings/str_cat_test.cc | 26 +++----- absl/strings/str_format.h | 32 +++++----- absl/strings/str_format_test.cc | 12 ++-- absl/strings/str_join.h | 73 +++++++++++----------- absl/strings/str_join_benchmark.cc | 3 +- absl/strings/str_join_test.cc | 12 ++-- absl/strings/str_replace.cc | 5 +- absl/strings/str_replace.h | 40 ++++++------ absl/strings/str_replace_benchmark.cc | 12 ++-- absl/strings/str_replace_test.cc | 2 +- absl/strings/str_split.h | 15 +++-- absl/strings/str_split_benchmark.cc | 3 +- absl/strings/str_split_test.cc | 32 ++++++---- absl/strings/string_view.h | 2 +- absl/strings/string_view_test.cc | 11 ++-- absl/strings/substitute.h | 66 +++++++++---------- .../internal/per_thread_sem_test.cc | 7 +-- absl/time/BUILD.bazel | 1 + absl/time/civil_time.cc | 15 ++--- absl/time/civil_time.h | 2 +- absl/time/duration.cc | 4 +- absl/time/duration_benchmark.cc | 1 + absl/time/duration_test.cc | 2 +- absl/time/format.cc | 11 ++-- absl/time/format_test.cc | 7 ++- absl/time/time.h | 3 +- absl/types/optional_test.cc | 4 +- absl/types/span.h | 2 +- absl/types/span_test.cc | 6 +- absl/types/variant_test.cc | 69 ++++++++++++-------- absl/utility/utility.h | 6 +- absl/utility/utility_test.cc | 2 +- 70 files changed, 484 insertions(+), 411 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h index d4d41a8a..07ce47d4 100644 --- a/absl/base/internal/exception_safety_testing.h +++ b/absl/base/internal/exception_safety_testing.h @@ -169,8 +169,10 @@ class ConstructorTracker { return current_tracker_instance_ != nullptr; } - static std::string ErrorMessage(void* address, const std::string& address_description, - int countdown, const std::string& error_description) { + static std::string ErrorMessage(void* address, + const std::string& address_description, + int countdown, + const std::string& error_description) { return absl::Substitute( "With coundtown at $0:\n" " $1\n" diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h index 79a7bb9b..f34e263e 100644 --- a/absl/base/internal/raw_logging.h +++ b/absl/base/internal/raw_logging.h @@ -79,13 +79,13 @@ absl_raw_logging_internal_basename, __LINE__, message); \ } while (0) -#define ABSL_INTERNAL_CHECK(condition, message) \ - do { \ - if (ABSL_PREDICT_FALSE(!(condition))) { \ +#define ABSL_INTERNAL_CHECK(condition, message) \ + do { \ + if (ABSL_PREDICT_FALSE(!(condition))) { \ std::string death_message = "Check " #condition " failed: "; \ death_message += std::string(message); \ - ABSL_INTERNAL_LOG(FATAL, death_message); \ - } \ + ABSL_INTERNAL_LOG(FATAL, death_message); \ + } \ } while (0) #define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo diff --git a/absl/base/internal/thread_identity.h b/absl/base/internal/thread_identity.h index a51722f9..3b3b7b75 100644 --- a/absl/base/internal/thread_identity.h +++ b/absl/base/internal/thread_identity.h @@ -42,9 +42,9 @@ namespace base_internal { class SpinLock; struct ThreadIdentity; -// Used by the implementation of base::Mutex and base::CondVar. +// Used by the implementation of absl::Mutex and absl::CondVar. struct PerThreadSynch { - // The internal representation of base::Mutex and base::CondVar rely + // The internal representation of absl::Mutex and absl::CondVar rely // on the alignment of PerThreadSynch. Both store the address of the // PerThreadSynch in the high-order bits of their internal state, // which means the low kLowZeroBits of the address of PerThreadSynch diff --git a/absl/container/fixed_array_test.cc b/absl/container/fixed_array_test.cc index 6198e4bb..23cf7bb6 100644 --- a/absl/container/fixed_array_test.cc +++ b/absl/container/fixed_array_test.cc @@ -365,7 +365,8 @@ TEST(IteratorConstructorTest, Inline) { TEST(IteratorConstructorTest, NonPod) { char const* kInput[] = { "red", "orange", "yellow", "green", "blue", "indigo", "violet" }; - absl::FixedArray const fixed(kInput, kInput + ABSL_ARRAYSIZE(kInput)); + absl::FixedArray const fixed(kInput, + kInput + ABSL_ARRAYSIZE(kInput)); ASSERT_EQ(ABSL_ARRAYSIZE(kInput), fixed.size()); for (size_t i = 0; i < ABSL_ARRAYSIZE(kInput); ++i) { ASSERT_EQ(kInput[i], fixed[i]); diff --git a/absl/container/flat_hash_map_test.cc b/absl/container/flat_hash_map_test.cc index 84ec9d56..7340a747 100644 --- a/absl/container/flat_hash_map_test.cc +++ b/absl/container/flat_hash_map_test.cc @@ -37,9 +37,9 @@ using Map = flat_hash_map(), ""); using MapTypes = - ::testing::Types, Map, Map, - Map, Map, - Map>; + ::testing::Types, Map, + Map, Map, + Map, Map>; INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ConstructorTest, MapTypes); INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, LookupTest, MapTypes); diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc index 9ca93b27..ddd7b33c 100644 --- a/absl/container/inlined_vector_benchmark.cc +++ b/absl/container/inlined_vector_benchmark.cc @@ -89,7 +89,7 @@ void BM_InlinedVectorFillString(benchmark::State& state) { const int len = state.range(0); const int no_sso = GetNonShortStringOptimizationSize(); std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'), - std::string(no_sso, 'C'), std::string(no_sso, 'D')}; + std::string(no_sso, 'C'), std::string(no_sso, 'D')}; for (auto _ : state) { absl::InlinedVector v; @@ -105,7 +105,7 @@ void BM_StdVectorFillString(benchmark::State& state) { const int len = state.range(0); const int no_sso = GetNonShortStringOptimizationSize(); std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'), - std::string(no_sso, 'C'), std::string(no_sso, 'D')}; + std::string(no_sso, 'C'), std::string(no_sso, 'D')}; for (auto _ : state) { std::vector v; diff --git a/absl/container/internal/hash_function_defaults.h b/absl/container/internal/hash_function_defaults.h index 1f0d794d..6d112c79 100644 --- a/absl/container/internal/hash_function_defaults.h +++ b/absl/container/internal/hash_function_defaults.h @@ -39,8 +39,8 @@ // equal functions are still bound to T. This is important because some type U // can be hashed by/tested for equality differently depending on T. A notable // example is `const char*`. `const char*` is treated as a c-style string when -// the hash function is hash but as a pointer when the hash function is -// hash. +// the hash function is hash but as a pointer when the hash +// function is hash. // #ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ #define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ @@ -83,6 +83,7 @@ struct StringHashEq { } }; }; + template <> struct HashEq : StringHashEq {}; template <> diff --git a/absl/container/internal/hash_function_defaults_test.cc b/absl/container/internal/hash_function_defaults_test.cc index e8f62503..cc13576d 100644 --- a/absl/container/internal/hash_function_defaults_test.cc +++ b/absl/container/internal/hash_function_defaults_test.cc @@ -202,15 +202,11 @@ TYPED_TEST(HashPointer, Works) { EXPECT_NE(hash(&dummy), hash(cuptr)); } -// Cartesian product of (string, std::string, absl::string_view) -// with (string, std::string, absl::string_view, const char*). +// Cartesian product of (std::string, absl::string_view) +// with (std::string, absl::string_view, const char*). using StringTypesCartesianProduct = Types< // clang-format off - std::pair, - std::pair, - std::pair, - std::pair, std::pair, std::pair>; diff --git a/absl/container/internal/layout.h b/absl/container/internal/layout.h index 3d21ac96..98a72be1 100644 --- a/absl/container/internal/layout.h +++ b/absl/container/internal/layout.h @@ -643,7 +643,8 @@ class LayoutImpl, absl::index_sequence, std::string DebugString() const { const auto offsets = Offsets(); const size_t sizes[] = {SizeOf>()...}; - const std::string types[] = {adl_barrier::TypeName>()...}; + const std::string types[] = { + adl_barrier::TypeName>()...}; std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")"); for (size_t i = 0; i != NumOffsets - 1; ++i) { absl::StrAppend(&res, "[", size_[i], "]; @", offsets[i + 1], types[i + 1], diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 33f8f8fa..8814eb87 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -763,8 +763,8 @@ class raw_hash_set { // that accept std::initializer_list and std::initializer_list. // This is advantageous for performance. // - // // Turns {"abc", "def"} into std::initializer_list, then copies - // // the strings into the set. + // // Turns {"abc", "def"} into std::initializer_list, then + // // copies the strings into the set. // std::unordered_set s = {"abc", "def"}; // // // Turns {"abc", "def"} into std::initializer_list, then diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index 7adcc96d..f599fd3d 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -1460,7 +1460,8 @@ TEST(Table, MoveAssign) { TEST(Table, Equality) { StringTable t; - std::vector> v = {{"a", "b"}, {"aa", "bb"}}; + std::vector> v = {{"a", "b"}, + {"aa", "bb"}}; t.insert(std::begin(v), std::end(v)); StringTable u = t; EXPECT_EQ(u, t); @@ -1468,20 +1469,24 @@ TEST(Table, Equality) { TEST(Table, Equality2) { StringTable t; - std::vector> v1 = {{"a", "b"}, {"aa", "bb"}}; + std::vector> v1 = {{"a", "b"}, + {"aa", "bb"}}; t.insert(std::begin(v1), std::end(v1)); StringTable u; - std::vector> v2 = {{"a", "a"}, {"aa", "aa"}}; + std::vector> v2 = {{"a", "a"}, + {"aa", "aa"}}; u.insert(std::begin(v2), std::end(v2)); EXPECT_NE(u, t); } TEST(Table, Equality3) { StringTable t; - std::vector> v1 = {{"b", "b"}, {"bb", "bb"}}; + std::vector> v1 = {{"b", "b"}, + {"bb", "bb"}}; t.insert(std::begin(v1), std::end(v1)); StringTable u; - std::vector> v2 = {{"a", "a"}, {"aa", "aa"}}; + std::vector> v2 = {{"a", "a"}, + {"aa", "aa"}}; u.insert(std::begin(v2), std::end(v2)); EXPECT_NE(u, t); } diff --git a/absl/container/internal/unordered_set_test.cc b/absl/container/internal/unordered_set_test.cc index d5a8613e..1a340af8 100644 --- a/absl/container/internal/unordered_set_test.cc +++ b/absl/container/internal/unordered_set_test.cc @@ -23,11 +23,11 @@ namespace absl { namespace container_internal { namespace { -using SetTypes = - ::testing::Types>, - std::unordered_set>>; +using SetTypes = ::testing::Types< + std::unordered_set>, + std::unordered_set>>; INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, ConstructorTest, SetTypes); INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, LookupTest, SetTypes); diff --git a/absl/debugging/failure_signal_handler_test.cc b/absl/debugging/failure_signal_handler_test.cc index b0fe8f25..15e888be 100644 --- a/absl/debugging/failure_signal_handler_test.cc +++ b/absl/debugging/failure_signal_handler_test.cc @@ -133,7 +133,8 @@ constexpr int kFailureSignals[] = { }; std::string SignalParamToString(const ::testing::TestParamInfo& info) { - std::string result = absl::debugging_internal::FailureSignalToString(info.param); + std::string result = + absl::debugging_internal::FailureSignalToString(info.param); if (result.empty()) { result = absl::StrCat(info.param); } diff --git a/absl/debugging/internal/symbolize.h b/absl/debugging/internal/symbolize.h index 8d926fec..d719eda9 100644 --- a/absl/debugging/internal/symbolize.h +++ b/absl/debugging/internal/symbolize.h @@ -20,7 +20,6 @@ #include #include -#include "absl/base/port.h" // Needed for string vs std::string #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set @@ -42,9 +41,9 @@ namespace debugging_internal { // Returns true on success; otherwise returns false in case of errors. // // This is not async-signal-safe. -bool ForEachSection( - int fd, const std::function& - callback); +bool ForEachSection(int fd, + const std::function& callback); // Gets the section header for the given name, if it exists. Returns true on // success. Otherwise, returns false. diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index 97d767ab..5b16bb8b 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc @@ -925,6 +925,14 @@ static const char *GetHex(const char *start, const char *end, return p; } +// Normally we are only interested in "r?x" maps. +// On the PowerPC, function pointers point to descriptors in the .opd +// section. The descriptors themselves are not executable code, so +// we need to relax the check below to "r??". +static bool ShouldUseMapping(const char *const flags) { + return flags[0] == 'r' && (kPlatformUsesOPDSections || flags[2] == 'x'); +} + // Read /proc/self/maps and run "callback" for each mmapped file found. If // "callback" returns false, stop scanning and return true. Else continue // scanning /proc/self/maps. Return true if no parse error is found. @@ -994,12 +1002,8 @@ static ABSL_ATTRIBUTE_NOINLINE bool ReadAddrMap( return false; } - // Check flags. Normally we are only interested in "r-x" maps. On - // the PowerPC, function pointers point to descriptors in the .opd - // section. The descriptors themselves are not executable code. So - // we need to relax the check below to "r**". - if (memcmp(flags_start, "r-x", 3) != 0 && // Not a "r-x" map. - !(kPlatformUsesOPDSections && flags_start[0] == 'r')) { + // Check flags. + if (!ShouldUseMapping(flags_start)) { continue; // We skip this map. } ++cursor; // Skip ' '. diff --git a/absl/hash/hash.h b/absl/hash/hash.h index 36c7c2b3..3b1d6eab 100644 --- a/absl/hash/hash.h +++ b/absl/hash/hash.h @@ -243,7 +243,7 @@ using Hash = absl::hash_internal::Hash; // absl::HashState::combine(std::move(state), v1_, v2_); // } // int v1_; -// string v2_; +// std::string v2_; // }; class HashState : public hash_internal::HashStateBase { public: diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index 2c9e46b7..97c3449a 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -275,7 +275,6 @@ struct WrapInTuple { TEST(HashValueTest, Strings) { EXPECT_TRUE((is_hashable::value)); - EXPECT_TRUE((is_hashable::value)); const std::string small = "foo"; const std::string dup = "foofoo"; @@ -705,7 +704,8 @@ TEST(HashTest, HashNonUniquelyRepresentedType) { } TEST(HashTest, StandardHashContainerUsage) { - std::unordered_map> map = {{0, "foo"}, { 42, "bar" }}; + std::unordered_map> map = {{0, "foo"}, + {42, "bar"}}; EXPECT_NE(map.find(0), map.end()); EXPECT_EQ(map.find(1), map.end()); @@ -775,4 +775,24 @@ TEST(HashTest, TypeErased) { SpyHash(std::make_pair(size_t{7}, 17))); } +struct ValueWithBoolConversion { + operator bool() const { return false; } + int i; +}; + +} // namespace +namespace std { +template <> +struct hash { + size_t operator()(ValueWithBoolConversion v) { return v.i; } +}; +} // namespace std + +namespace { + +TEST(HashTest, DoesNotUseImplicitConversionsToBool) { + EXPECT_NE(absl::Hash()(ValueWithBoolConversion{0}), + absl::Hash()(ValueWithBoolConversion{1})); +} + } // namespace diff --git a/absl/hash/hash_testing.h b/absl/hash/hash_testing.h index 52bcb55a..06d09499 100644 --- a/absl/hash/hash_testing.h +++ b/absl/hash/hash_testing.h @@ -190,7 +190,9 @@ VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals) { struct Info { const V& value; size_t index; - std::string ToString() const { return absl::visit(PrintVisitor{index}, value); } + std::string ToString() const { + return absl::visit(PrintVisitor{index}, value); + } SpyHashState expand() const { return absl::visit(ExpandVisitor{}, value); } }; diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index bd07677a..4db816c7 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h @@ -221,7 +221,9 @@ typename std::enable_if::value, H>::type AbslHashValue( } // AbslHashValue() for hashing floating-point values template -typename std::enable_if::value, H>::type +typename std::enable_if::value || + std::is_same::value, + H>::type AbslHashValue(H hash_state, Float value) { return hash_internal::hash_bytes(std::move(hash_state), value == 0 ? 0 : value); @@ -231,8 +233,9 @@ AbslHashValue(H hash_state, Float value) { // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits // of it. This means we can't use hash_bytes on a long double and have to // convert it to something else first. -template -H AbslHashValue(H hash_state, long double value) { +template +typename std::enable_if::value, H>::type +AbslHashValue(H hash_state, LongDouble value) { const int category = std::fpclassify(value); switch (category) { case FP_INFINITE: diff --git a/absl/hash/internal/spy_hash_state.h b/absl/hash/internal/spy_hash_state.h index e7d1dfef..102e05de 100644 --- a/absl/hash/internal/spy_hash_state.h +++ b/absl/hash/internal/spy_hash_state.h @@ -39,8 +39,7 @@ namespace hash_internal { template class SpyHashStateImpl : public HashStateBase> { public: - SpyHashStateImpl() - : error_(std::make_shared>()) { + SpyHashStateImpl() : error_(std::make_shared>()) { static_assert(std::is_void::value, ""); } @@ -170,7 +169,6 @@ class SpyHashStateImpl : public HashStateBase> { // AbslHashValue directly (because the hash state type does not match). static bool direct_absl_hash_value_error_; - std::vector hash_representation_; // This is a shared_ptr because we want all instances of the particular // SpyHashState run to share the field. This way we can set the error for diff --git a/absl/strings/charconv_test.cc b/absl/strings/charconv_test.cc index d07537eb..d6a0a759 100644 --- a/absl/strings/charconv_test.cc +++ b/absl/strings/charconv_test.cc @@ -279,7 +279,8 @@ void TestHalfwayValue(const std::string& mantissa, int exponent, absl::from_chars(low_rep.data(), low_rep.data() + low_rep.size(), actual_low); EXPECT_EQ(expected_low, actual_low); - std::string high_rep = absl::StrCat(mantissa, std::string(1000, '0'), "1e", exponent); + std::string high_rep = + absl::StrCat(mantissa, std::string(1000, '0'), "1e", exponent); FloatType actual_high = 0; absl::from_chars(high_rep.data(), high_rep.data() + high_rep.size(), actual_high); diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 29f55c7c..0950ab9e 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -179,7 +179,8 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ch = (ch << 4) + hex_digit_to_int(*++p); if (ch > 0xFF) { if (error) { - *error = "Value of \\" + std::string(hex_start, p + 1 - hex_start) + + *error = "Value of \\" + + std::string(hex_start, p + 1 - hex_start) + " exceeds 0xff"; } return false; @@ -294,7 +295,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, // ---------------------------------------------------------------------- // CUnescapeInternal() // -// Same as above but uses a C++ string for output. 'source' and 'dest' +// Same as above but uses a std::string for output. 'source' and 'dest' // may be the same. // ---------------------------------------------------------------------- bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, @@ -324,7 +325,8 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, // // Escaped chars: \n, \r, \t, ", ', \, and !absl::ascii_isprint(). // ---------------------------------------------------------------------- -std::string CEscapeInternal(absl::string_view src, bool use_hex, bool utf8_safe) { +std::string CEscapeInternal(absl::string_view src, bool use_hex, + bool utf8_safe) { std::string dest; bool last_hex_escape = false; // true if last output char was \xNN. @@ -1011,7 +1013,8 @@ void HexStringToBytesInternal(const char* from, T to, ptrdiff_t num) { } } -// This is a templated function so that T can be either a char* or a string. +// This is a templated function so that T can be either a char* or a +// std::string. template void BytesToHexStringInternal(const unsigned char* src, T dest, ptrdiff_t num) { auto dest_ptr = &dest[0]; @@ -1028,7 +1031,8 @@ void BytesToHexStringInternal(const unsigned char* src, T dest, ptrdiff_t num) { // // See CUnescapeInternal() for implementation details. // ---------------------------------------------------------------------- -bool CUnescape(absl::string_view source, std::string* dest, std::string* error) { +bool CUnescape(absl::string_view source, std::string* dest, + std::string* error) { return CUnescapeInternal(source, kUnescapeNulls, dest, error); } diff --git a/absl/strings/escaping_test.cc b/absl/strings/escaping_test.cc index 9dc27f3f..77846dd2 100644 --- a/absl/strings/escaping_test.cc +++ b/absl/strings/escaping_test.cc @@ -36,18 +36,19 @@ struct epair { TEST(CEscape, EscapeAndUnescape) { const std::string inputs[] = { - std::string("foo\nxx\r\b\0023"), - std::string(""), - std::string("abc"), - std::string("\1chad_rules"), - std::string("\1arnar_drools"), - std::string("xxxx\r\t'\"\\"), - std::string("\0xx\0", 4), - std::string("\x01\x31"), - std::string("abc\xb\x42\141bc"), - std::string("123\1\x31\x32\x33"), - std::string("\xc1\xca\x1b\x62\x19o\xcc\x04"), - std::string("\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"), + std::string("foo\nxx\r\b\0023"), + std::string(""), + std::string("abc"), + std::string("\1chad_rules"), + std::string("\1arnar_drools"), + std::string("xxxx\r\t'\"\\"), + std::string("\0xx\0", 4), + std::string("\x01\x31"), + std::string("abc\xb\x42\141bc"), + std::string("123\1\x31\x32\x33"), + std::string("\xc1\xca\x1b\x62\x19o\xcc\x04"), + std::string( + "\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"), }; // Do this twice, once for octal escapes and once for hex escapes. for (int kind = 0; kind < 4; kind++) { @@ -159,15 +160,14 @@ TEST(Unescape, BasicFunction) { EXPECT_TRUE(absl::CUnescape(val.escaped, &out)); EXPECT_EQ(out, val.unescaped); } - std::string bad[] = - {"\\u1", // too short - "\\U1", // too short - "\\Uffffff", // exceeds 0x10ffff (largest Unicode) - "\\U00110000", // exceeds 0x10ffff (largest Unicode) - "\\uD835", // surrogate character (D800-DFFF) - "\\U0000DD04", // surrogate character (D800-DFFF) - "\\777", // exceeds 0xff - "\\xABCD"}; // exceeds 0xff + std::string bad[] = {"\\u1", // too short + "\\U1", // too short + "\\Uffffff", // exceeds 0x10ffff (largest Unicode) + "\\U00110000", // exceeds 0x10ffff (largest Unicode) + "\\uD835", // surrogate character (D800-DFFF) + "\\U0000DD04", // surrogate character (D800-DFFF) + "\\777", // exceeds 0xff + "\\xABCD"}; // exceeds 0xff for (const std::string& e : bad) { std::string error; std::string out; @@ -258,9 +258,11 @@ TEST_F(CUnescapeTest, UnescapesMultipleOctalNulls) { // All escapes, including newlines and null escapes, should have been // converted to the equivalent characters. EXPECT_EQ(std::string("\0\n" - "0\n" - "\0\n" - "\0", 7), result_string_); + "0\n" + "\0\n" + "\0", + 7), + result_string_); } @@ -268,17 +270,21 @@ TEST_F(CUnescapeTest, UnescapesMultipleHexNulls) { std::string original_string(kStringWithMultipleHexNulls); EXPECT_TRUE(absl::CUnescape(original_string, &result_string_)); EXPECT_EQ(std::string("\0\n" - "0\n" - "\0\n" - "\0", 7), result_string_); + "0\n" + "\0\n" + "\0", + 7), + result_string_); } TEST_F(CUnescapeTest, UnescapesMultipleUnicodeNulls) { std::string original_string(kStringWithMultipleUnicodeNulls); EXPECT_TRUE(absl::CUnescape(original_string, &result_string_)); EXPECT_EQ(std::string("\0\n" - "0\n" - "\0", 5), result_string_); + "0\n" + "\0", + 5), + result_string_); } static struct { diff --git a/absl/strings/internal/ostringstream.h b/absl/strings/internal/ostringstream.h index e81a89af..316379ca 100644 --- a/absl/strings/internal/ostringstream.h +++ b/absl/strings/internal/ostringstream.h @@ -25,18 +25,18 @@ namespace absl { namespace strings_internal { -// The same as std::ostringstream but appends to a user-specified string, +// The same as std::ostringstream but appends to a user-specified std::string, // and is faster. It is ~70% faster to create, ~50% faster to write to, and -// completely free to extract the result string. +// completely free to extract the result std::string. // -// string s; +// std::string s; // OStringStream strm(&s); // strm << 42 << ' ' << 3.14; // appends to `s` // // The stream object doesn't have to be named. Starting from C++11 operator<< // works with rvalues of std::ostream. // -// string s; +// std::string s; // OStringStream(&s) << 42 << ' ' << 3.14; // appends to `s` // // OStringStream is faster to create than std::ostringstream but it's still @@ -45,14 +45,14 @@ namespace strings_internal { // // Creates unnecessary instances of OStringStream: slow. // -// string s; +// std::string s; // OStringStream(&s) << 42; // OStringStream(&s) << ' '; // OStringStream(&s) << 3.14; // // Creates a single instance of OStringStream and reuses it: fast. // -// string s; +// std::string s; // OStringStream strm(&s); // strm << 42; // strm << ' '; @@ -64,8 +64,8 @@ class OStringStream : private std::basic_streambuf, public std::ostream { // The argument can be null, in which case you'll need to call str(p) with a // non-null argument before you can write to the stream. // - // The destructor of OStringStream doesn't use the std::string. It's OK to destroy - // the std::string before the stream. + // The destructor of OStringStream doesn't use the std::string. It's OK to + // destroy the std::string before the stream. explicit OStringStream(std::string* s) : std::ostream(this), s_(s) {} std::string* str() { return s_; } diff --git a/absl/strings/internal/resize_uninitialized.h b/absl/strings/internal/resize_uninitialized.h index c2da0da9..25d602b1 100644 --- a/absl/strings/internal/resize_uninitialized.h +++ b/absl/strings/internal/resize_uninitialized.h @@ -47,8 +47,8 @@ struct ResizeUninitializedTraits< } }; -// Returns true if the string implementation supports a resize where -// the new characters added to the string are left untouched. +// Returns true if the std::string implementation supports a resize where +// the new characters added to the std::string are left untouched. // // (A better name might be "STLStringSupportsUninitializedResize", alluding to // the previous function.) @@ -60,7 +60,7 @@ inline constexpr bool STLStringSupportsNontrashingResize(string_type*) { // Like str->resize(new_size), except any new characters added to "*str" as a // result of resizing may be left uninitialized, rather than being filled with // '0' bytes. Typically used when code is then going to overwrite the backing -// store of the string with known data. Uses a Google extension to ::string. +// store of the std::string with known data. template inline void STLStringResizeUninitialized(string_type* s, size_t new_size) { ResizeUninitializedTraits::Resize(s, new_size); diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index ebd40adc..c54cd1ab 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -54,7 +54,8 @@ ConvertResult FormatConvertImpl(VoidPtr v, ConversionSpec conv, FormatSinkImpl* sink); // Strings. -ConvertResult FormatConvertImpl(const std::string& v, ConversionSpec conv, +ConvertResult FormatConvertImpl(const std::string& v, + ConversionSpec conv, FormatSinkImpl* sink); ConvertResult FormatConvertImpl(string_view v, ConversionSpec conv, FormatSinkImpl* sink); @@ -409,7 +410,7 @@ class FormatArgImpl { ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long double, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(const char*, __VA_ARGS__); \ - ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__); \ + ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(string_view, __VA_ARGS__) ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern); diff --git a/absl/strings/internal/str_format/bind.cc b/absl/strings/internal/str_format/bind.cc index a89295d5..a31859ec 100644 --- a/absl/strings/internal/str_format/bind.cc +++ b/absl/strings/internal/str_format/bind.cc @@ -160,7 +160,7 @@ bool BindWithPack(const UnboundConversion* props, } std::string Summarize(const UntypedFormatSpecImpl format, - absl::Span args) { + absl::Span args) { typedef SummarizingConverter Converter; std::string out; { @@ -188,7 +188,7 @@ std::ostream& Streamable::Print(std::ostream& os) const { } std::string& AppendPack(std::string* out, const UntypedFormatSpecImpl format, - absl::Span args) { + absl::Span args) { size_t orig = out->size(); if (ABSL_PREDICT_FALSE(!FormatUntyped(out, format, args))) { out->erase(orig); diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index 1b52df9c..4f782952 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -153,7 +153,7 @@ class Streamable { // for testing std::string Summarize(UntypedFormatSpecImpl format, - absl::Span args); + absl::Span args); bool BindWithPack(const UnboundConversion* props, absl::Span pack, BoundConversion* bound); @@ -162,10 +162,10 @@ bool FormatUntyped(FormatRawSinkImpl raw_sink, absl::Span args); std::string& AppendPack(std::string* out, UntypedFormatSpecImpl format, - absl::Span args); + absl::Span args); inline std::string FormatPack(const UntypedFormatSpecImpl format, - absl::Span args) { + absl::Span args) { std::string out; AppendPack(&out, format, args); return out; @@ -176,7 +176,7 @@ int FprintF(std::FILE* output, UntypedFormatSpecImpl format, int SnprintF(char* output, size_t size, UntypedFormatSpecImpl format, absl::Span args); -// Returned by Streamed(v). Converts via '%s' to the string created +// Returned by Streamed(v). Converts via '%s' to the std::string created // by std::ostream << v. template class StreamedWrapper { diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index 14d11ea8..c1d8c769 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -62,32 +62,32 @@ TEST(StrFormatChecker, ValidFormat) { ValidFormat("%% %d"), // ValidFormat("%ld"), // ValidFormat("%lld"), // - ValidFormat("%s"), // - ValidFormat("%10s"), // + ValidFormat("%s"), // + ValidFormat("%10s"), // ValidFormat("%.10x"), // ValidFormat("%*.3x"), // ValidFormat("%1.d"), // ValidFormat("%.d"), // ValidFormat("%d %g"), // - ValidFormat("%*s"), // + ValidFormat("%*s"), // ValidFormat("%.*f"), // ValidFormat("%p %p"), // ValidFormat( "string_view=%s const char*=%s double=%f void*=%p)"), - ValidFormat("%% %1$d"), // - ValidFormat("%1$ld"), // - ValidFormat("%1$lld"), // - ValidFormat("%1$s"), // - ValidFormat("%1$10s"), // - ValidFormat("%1$.10x"), // - ValidFormat("%1$*1$.*1$d"), // - ValidFormat("%1$*2$.3x"), // - ValidFormat("%1$1.d"), // - ValidFormat("%1$.d"), // - ValidFormat("%2$d %1$g"), // - ValidFormat("%2$*1$s"), // - ValidFormat("%2$.*1$f"), // + ValidFormat("%% %1$d"), // + ValidFormat("%1$ld"), // + ValidFormat("%1$lld"), // + ValidFormat("%1$s"), // + ValidFormat("%1$10s"), // + ValidFormat("%1$.10x"), // + ValidFormat("%1$*1$.*1$d"), // + ValidFormat("%1$*2$.3x"), // + ValidFormat("%1$1.d"), // + ValidFormat("%1$.d"), // + ValidFormat("%2$d %1$g"), // + ValidFormat("%2$*1$s"), // + ValidFormat("%2$.*1$f"), // ValidFormat( "string_view=%2$s const char*=%3$s double=%4$f void*=%1$p " "repeat=%3$s)")}; @@ -99,25 +99,25 @@ TEST(StrFormatChecker, ValidFormat) { constexpr Case falses[] = { ValidFormat(""), // - ValidFormat("%s"), // - ValidFormat("%s"), // - ValidFormat<>("%s"), // - ValidFormat<>("%r"), // - ValidFormat("%s"), // - ValidFormat("%.1.d"), // - ValidFormat("%*1d"), // - ValidFormat("%1-d"), // + ValidFormat("%s"), // + ValidFormat("%s"), // + ValidFormat<>("%s"), // + ValidFormat<>("%r"), // + ValidFormat("%s"), // + ValidFormat("%.1.d"), // + ValidFormat("%*1d"), // + ValidFormat("%1-d"), // ValidFormat("%*s"), // - ValidFormat("%*d"), // + ValidFormat("%*d"), // ValidFormat("%p"), // - ValidFormat("%d"), // - - ValidFormat<>("%3$d"), // - ValidFormat<>("%1$r"), // - ValidFormat("%1$s"), // - ValidFormat("%1$.1.d"), // - ValidFormat("%1$*2$1d"), // - ValidFormat("%1$1-d"), // + ValidFormat("%d"), // + + ValidFormat<>("%3$d"), // + ValidFormat<>("%1$r"), // + ValidFormat("%1$s"), // + ValidFormat("%1$.1.d"), // + ValidFormat("%1$*2$1d"), // + ValidFormat("%1$1-d"), // ValidFormat("%2$*1$s"), // ValidFormat("%1$p"), diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index 7ce80d42..55be9284 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -360,7 +360,7 @@ enum class Conv : uint64_t { integral = d | i | u | o | x | X, floating = a | e | f | g | A | E | F | G, numeric = integral | floating, - string = s, // absl:ignore(std::string) + string = s, pointer = p }; diff --git a/absl/strings/internal/str_format/output_test.cc b/absl/strings/internal/str_format/output_test.cc index cc3c6155..305cc6e6 100644 --- a/absl/strings/internal/str_format/output_test.cc +++ b/absl/strings/internal/str_format/output_test.cc @@ -28,12 +28,6 @@ TEST(InvokeFlush, String) { std::string str = "ABC"; str_format_internal::InvokeFlush(&str, "DEF"); EXPECT_EQ(str, "ABCDEF"); - -#if UTIL_FORMAT_HAS_GLOBAL_STRING - std::string str2 = "ABC"; - str_format_internal::InvokeFlush(&str2, "DEF"); - EXPECT_EQ(str2, "ABCDEF"); -#endif // UTIL_FORMAT_HAS_GLOBAL_STRING } TEST(InvokeFlush, Stream) { diff --git a/absl/strings/internal/str_join_internal.h b/absl/strings/internal/str_join_internal.h index 0058fc8a..6281da6e 100644 --- a/absl/strings/internal/str_join_internal.h +++ b/absl/strings/internal/str_join_internal.h @@ -193,7 +193,7 @@ struct DefaultFormatter> // and formats each element using the provided Formatter object. template std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s, - Formatter&& f) { + Formatter&& f) { std::string result; absl::string_view sep(""); for (Iterator it = start; it != end; ++it) { @@ -212,7 +212,7 @@ std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s, // This is an overload of the previous JoinAlgorithm() function. Here the // Formatter argument is of type NoFormatter. Since NoFormatter is an internal // type, this overload is only invoked when strings::Join() is called with a -// range of string-like objects (e.g., string, absl::string_view), and an +// range of string-like objects (e.g., std::string, absl::string_view), and an // explicit Formatter argument was NOT specified. // // The optimization is that the needed space will be reserved in the output @@ -224,7 +224,7 @@ template ::iterator_category, std::forward_iterator_tag>::value>::type> std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s, - NoFormatter) { + NoFormatter) { std::string result; if (start != end) { // Sums size @@ -276,14 +276,15 @@ struct JoinTupleLoop { template std::string JoinAlgorithm(const std::tuple& tup, absl::string_view sep, - Formatter&& fmt) { + Formatter&& fmt) { std::string result; JoinTupleLoop<0, sizeof...(T)>()(&result, tup, sep, fmt); return result; } template -std::string JoinRange(Iterator first, Iterator last, absl::string_view separator) { +std::string JoinRange(Iterator first, Iterator last, + absl::string_view separator) { // No formatter was explicitly given, so a default must be chosen. typedef typename std::iterator_traits::value_type ValueType; typedef typename DefaultFormatter::Type Formatter; @@ -292,7 +293,7 @@ std::string JoinRange(Iterator first, Iterator last, absl::string_view separator template std::string JoinRange(const Range& range, absl::string_view separator, - Formatter&& fmt) { + Formatter&& fmt) { using std::begin; using std::end; return JoinAlgorithm(begin(range), end(range), separator, fmt); diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h index 81e8d555..34390a91 100644 --- a/absl/strings/internal/str_split_internal.h +++ b/absl/strings/internal/str_split_internal.h @@ -96,8 +96,8 @@ ConvertibleToStringView(std::string&& s) // NOLINT(runtime/explicit) } } - // Holds the data moved from temporary std::string arguments. Declared first so - // that 'value' can refer to 'copy_'. + // Holds the data moved from temporary std::string arguments. Declared first + // so that 'value' can refer to 'copy_'. std::string copy_; absl::string_view value_; }; @@ -376,10 +376,10 @@ class Splitter { // Partial specialization for a std::vector. // - // Optimized for the common case of splitting to a std::vector. In - // this case we first split the results to a std::vector so - // the returned std::vector can have space reserved to avoid std::string - // moves. + // Optimized for the common case of splitting to a std::vector. + // In this case we first split the results to a std::vector + // so the returned std::vector can have space reserved to avoid + // std::string moves. template struct ConvertToContainer, std::string, false> { std::vector operator()(const Splitter& splitter) const { diff --git a/absl/strings/internal/utf8_test.cc b/absl/strings/internal/utf8_test.cc index 07a000cb..6ffa36cd 100644 --- a/absl/strings/internal/utf8_test.cc +++ b/absl/strings/internal/utf8_test.cc @@ -29,10 +29,10 @@ namespace { #endif TEST(EncodeUTF8Char, BasicFunction) { std::pair tests[] = {{0x0030, u8"\u0030"}, - {0x00A3, u8"\u00A3"}, - {0x00010000, u8"\U00010000"}, - {0x0000FFFF, u8"\U0000FFFF"}, - {0x0010FFFD, u8"\U0010FFFD"}}; + {0x00A3, u8"\u00A3"}, + {0x00010000, u8"\U00010000"}, + {0x0000FFFF, u8"\U0000FFFF"}, + {0x0010FFFD, u8"\U0010FFFD"}}; for (auto &test : tests) { char buf0[7] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'}; char buf1[7] = {'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF'}; diff --git a/absl/strings/match_test.cc b/absl/strings/match_test.cc index c21e00bf..0dca33ae 100644 --- a/absl/strings/match_test.cc +++ b/absl/strings/match_test.cc @@ -19,7 +19,7 @@ namespace { TEST(MatchTest, StartsWith) { - const std::string s1("123" "\0" "456", 7); + const std::string s1("123\0abc", 7); const absl::string_view a("foobar"); const absl::string_view b(s1); const absl::string_view e; @@ -36,7 +36,7 @@ TEST(MatchTest, StartsWith) { } TEST(MatchTest, EndsWith) { - const std::string s1("123" "\0" "456", 7); + const std::string s1("123\0abc", 7); const absl::string_view a("foobar"); const absl::string_view b(s1); const absl::string_view e; diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index 099326c2..7edb73eb 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc @@ -191,7 +191,8 @@ void CheckUInt64(uint64_t x) { EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); - EXPECT_EQ(expected, std::string(&buffer[1], generic_actual)) << " Input " << x; + EXPECT_EQ(expected, std::string(&buffer[1], generic_actual)) + << " Input " << x; char* my_actual = absl::numbers_internal::FastIntToBuffer(MyUInt64(x), &buffer[1]); @@ -879,8 +880,8 @@ TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) { char buf[kSixDigitsToBufferSize]; ABSL_RAW_LOG( INFO, "%s", - absl::StrCat("Exp ", exponent, " powten=", powten, "(", - powten, ") (", + absl::StrCat("Exp ", exponent, " powten=", powten, "(", powten, + ") (", std::string(buf, SixDigitsToBuffer(powten, buf)), ")") .c_str()); } diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index b14d571f..b3a55d13 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -78,7 +78,7 @@ AlphaNum::AlphaNum(Dec dec) { // ---------------------------------------------------------------------- // StrCat() -// This merges the given strings or integers, with no delimiter. This +// This merges the given strings or integers, with no delimiter. This // is designed to be the fastest possible way to construct a string out // of a mix of raw C strings, string_views, strings, and integer values. // ---------------------------------------------------------------------- @@ -119,7 +119,7 @@ std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c) { } std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, - const AlphaNum& d) { + const AlphaNum& d) { std::string result; strings_internal::STLStringResizeUninitialized( &result, a.size() + b.size() + c.size() + d.size()); diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h index f4f4aca8..7b5bedb0 100644 --- a/absl/strings/str_cat.h +++ b/absl/strings/str_cat.h @@ -245,6 +245,7 @@ class AlphaNum { AlphaNum(const char* c_str) : piece_(c_str) {} // NOLINT(runtime/explicit) AlphaNum(absl::string_view pc) : piece_(pc) {} // NOLINT(runtime/explicit) + template AlphaNum( // NOLINT(runtime/explicit) const std::basic_string, Allocator>& str) @@ -317,16 +318,15 @@ ABSL_MUST_USE_RESULT inline std::string StrCat(const AlphaNum& a) { ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b); ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c); + const AlphaNum& c); ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d); + const AlphaNum& c, const AlphaNum& d); // Support 5 or more arguments template -ABSL_MUST_USE_RESULT inline std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, - const AV&... args) { +ABSL_MUST_USE_RESULT inline std::string StrCat( + const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, const AlphaNum& d, + const AlphaNum& e, const AV&... args) { return strings_internal::CatPieces( {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(), static_cast(args).Piece()...}); @@ -344,18 +344,18 @@ ABSL_MUST_USE_RESULT inline std::string StrCat(const AlphaNum& a, const AlphaNum // not try to check each of its input arguments to be sure that they are not // a subset of the string being appended to. That is, while this will work: // -// string s = "foo"; +// std::string s = "foo"; // s += s; // // This output is undefined: // -// string s = "foo"; +// std::string s = "foo"; // StrAppend(&s, s); // // This output is undefined as well, since `absl::string_view` does not own its // data: // -// string s = "foobar"; +// std::string s = "foobar"; // absl::string_view p = s; // StrAppend(&s, p); diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc index 07141072..af459c51 100644 --- a/absl/strings/str_cat_test.cc +++ b/absl/strings/str_cat_test.cc @@ -106,11 +106,7 @@ TEST(StrCat, Enums) { TEST(StrCat, Basics) { std::string result; - std::string strs[] = { - "Hello", - "Cruel", - "World" - }; + std::string strs[] = {"Hello", "Cruel", "World"}; std::string stdstrs[] = { "std::Hello", @@ -164,9 +160,10 @@ TEST(StrCat, Basics) { result = absl::StrCat(ui64s[0], ", ", ui64s[1], "!"); EXPECT_EQ(result, "12345678910, 10987654321!"); - std::string one = "1"; // Actually, it's the size of this std::string that we want; a - // 64-bit build distinguishes between size_t and uint64_t, - // even though they're both unsigned 64-bit values. + std::string one = + "1"; // Actually, it's the size of this std::string that we want; a + // 64-bit build distinguishes between size_t and uint64_t, + // even though they're both unsigned 64-bit values. result = absl::StrCat("And a ", one.size(), " and a ", &result[2] - &result[0], " and a ", one, " 2 3 4", "!"); EXPECT_EQ(result, "And a 1 and a 2 and a 1 2 3 4!"); @@ -306,11 +303,7 @@ TEST(StrCat, MaxArgs) { TEST(StrAppend, Basics) { std::string result = "existing text"; - std::string strs[] = { - "Hello", - "Cruel", - "World" - }; + std::string strs[] = {"Hello", "Cruel", "World"}; std::string stdstrs[] = { "std::Hello", @@ -365,9 +358,10 @@ TEST(StrAppend, Basics) { absl::StrAppend(&result, ui64s[0], ", ", ui64s[1], "!"); EXPECT_EQ(result.substr(old_size), "12345678910, 10987654321!"); - std::string one = "1"; // Actually, it's the size of this std::string that we want; a - // 64-bit build distinguishes between size_t and uint64_t, - // even though they're both unsigned 64-bit values. + std::string one = + "1"; // Actually, it's the size of this std::string that we want; a + // 64-bit build distinguishes between size_t and uint64_t, + // even though they're both unsigned 64-bit values. old_size = result.size(); absl::StrAppend(&result, "And a ", one.size(), " and a ", &result[2] - &result[0], " and a ", one, " 2 3 4", "!"); diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index 060909a2..4736bef1 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -24,7 +24,8 @@ // // Example: // -// string s = absl::StrFormat("%s %s You have $%d!", "Hello", name, dollars); +// std::string s = absl::StrFormat( +// "%s %s You have $%d!", "Hello", name, dollars); // // The library consists of the following basic utilities: // @@ -89,7 +90,7 @@ namespace absl { // Example: // // absl::UntypedFormatSpec format("%d"); -// string out; +// std::string out; // CHECK(absl::FormatUntyped(&out, format, {absl::FormatArg(1)})); class UntypedFormatSpec { public: @@ -135,8 +136,8 @@ str_format_internal::StreamedWrapper FormatStreamed(const T& v) { // Example: // // int n = 0; -// string s = absl::StrFormat("%s%d%n", "hello", 123, -// absl::FormatCountCapture(&n)); +// std::string s = absl::StrFormat("%s%d%n", "hello", 123, +// absl::FormatCountCapture(&n)); // EXPECT_EQ(8, n); class FormatCountCapture { public: @@ -223,7 +224,7 @@ class FormatCountCapture { // "%p", *int -> "0x7ffdeb6ad2a4" // // int n = 0; -// string s = absl::StrFormat( +// std::string s = absl::StrFormat( // "%s%d%n", "hello", 123, absl::FormatCountCapture(&n)); // EXPECT_EQ(8, n); // @@ -290,14 +291,14 @@ using ParsedFormat = str_format_internal::ExtendedParsedFormat< // // Example: // -// string s = absl::StrFormat( +// std::string s = absl::StrFormat( // "Welcome to %s, Number %d!", "The Village", 6); // EXPECT_EQ("Welcome to The Village, Number 6!", s); // // Returns an empty string in case of error. template ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec& format, - const Args&... args) { + const Args&... args) { return str_format_internal::FormatPack( str_format_internal::UntypedFormatSpecImpl::Extract(format), {str_format_internal::FormatArgImpl(args)...}); @@ -311,11 +312,12 @@ ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec& format, // // Example: // -// string orig("For example PI is approximately "); +// std::string orig("For example PI is approximately "); // std::cout << StrAppendFormat(&orig, "%12.6f", 3.14); template -std::string& StrAppendFormat(std::string* dst, const FormatSpec& format, - const Args&... args) { +std::string& StrAppendFormat(std::string* dst, + const FormatSpec& format, + const Args&... args) { return str_format_internal::AppendPack( dst, str_format_internal::UntypedFormatSpecImpl::Extract(format), {str_format_internal::FormatArgImpl(args)...}); @@ -434,7 +436,8 @@ class FormatRawSink { // `absl::FormatRawSink` interface), using a format string and zero or more // additional arguments. // -// By default, `string` and `std::ostream` are supported as destination objects. +// By default, `std::string` and `std::ostream` are supported as destination +// objects. // // `absl::Format()` is a generic version of `absl::StrFormat(), for custom // sinks. The format string, like format strings for `StrFormat()`, is checked @@ -483,9 +486,10 @@ using FormatArg = str_format_internal::FormatArgImpl; // // Example: // -// std::optional FormatDynamic(const string& in_format, -// const vector& in_args) { -// string out; +// std::optional FormatDynamic( +// const std::string& in_format, +// const vector& in_args) { +// std::string out; // std::vector args; // for (const auto& v : in_args) { // // It is important that 'v' is a reference to the objects in in_args. diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 87ed234f..d4cffa08 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -341,7 +341,7 @@ TEST(StrFormat, BehavesAsDocumented) { EXPECT_EQ(StrFormat("%c", int{'a'}), "a"); EXPECT_EQ(StrFormat("%c", long{'a'}), "a"); // NOLINT EXPECT_EQ(StrFormat("%c", uint64_t{'a'}), "a"); - // "s" - std::string Eg: "C" -> "C", std::string("C++") -> "C++" + // "s" - std::string Eg: "C" -> "C", std::string("C++") -> "C++" // Formats std::string, char*, string_view, and Cord. EXPECT_EQ(StrFormat("%s", "C"), "C"); EXPECT_EQ(StrFormat("%s", std::string("C++")), "C++"); @@ -606,21 +606,21 @@ TEST_F(ParsedFormatTest, RegressionMixPositional) { // Some codegen thunks that we can use to easily dump the generated assembly for // different StrFormat calls. -std::string CodegenAbslStrFormatInt(int i) { // NOLINT +std::string CodegenAbslStrFormatInt(int i) { // NOLINT return absl::StrFormat("%d", i); } std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, - int64_t i64) { // NOLINT + int64_t i64) { // NOLINT return absl::StrFormat("%d %s %d", i, s, i64); } -void CodegenAbslStrAppendFormatInt(std::string* out, int i) { // NOLINT +void CodegenAbslStrAppendFormatInt(std::string* out, int i) { // NOLINT absl::StrAppendFormat(out, "%d", i); } void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, - const std::string& s, - int64_t i64) { // NOLINT + const std::string& s, + int64_t i64) { // NOLINT absl::StrAppendFormat(out, "%d %s %d", i, s, i64); } diff --git a/absl/strings/str_join.h b/absl/strings/str_join.h index f9611ad3..69abc9c1 100644 --- a/absl/strings/str_join.h +++ b/absl/strings/str_join.h @@ -18,13 +18,13 @@ // ----------------------------------------------------------------------------- // // This header file contains functions for joining a range of elements and -// returning the result as a string. StrJoin operations are specified by passing -// a range, a separator string to use between the elements joined, and an -// optional Formatter responsible for converting each argument in the range to a -// string. If omitted, a default `AlphaNumFormatter()` is called on the elements -// to be joined, using the same formatting that `absl::StrCat()` uses. This -// package defines a number of default formatters, and you can define your own -// implementations. +// returning the result as a std::string. StrJoin operations are specified by +// passing a range, a separator string to use between the elements joined, and +// an optional Formatter responsible for converting each argument in the range +// to a string. If omitted, a default `AlphaNumFormatter()` is called on the +// elements to be joined, using the same formatting that `absl::StrCat()` uses. +// This package defines a number of default formatters, and you can define your +// own implementations. // // Ranges are specified by passing a container with `std::begin()` and // `std::end()` iterators, container-specific `begin()` and `end()` iterators, a @@ -37,8 +37,8 @@ // // Example: // -// std::vector v = {"foo", "bar", "baz"}; -// string s = absl::StrJoin(v, "-"); +// std::vector v = {"foo", "bar", "baz"}; +// std::string s = absl::StrJoin(v, "-"); // EXPECT_EQ("foo-bar-baz", s); // // See comments on the `absl::StrJoin()` function for more examples. @@ -66,16 +66,16 @@ namespace absl { // ----------------------------------------------------------------------------- // // A Formatter is a function object that is responsible for formatting its -// argument as a string and appending it to a given output string. Formatters -// may be implemented as function objects, lambdas, or normal functions. You may -// provide your own Formatter to enable `absl::StrJoin()` to work with arbitrary -// types. +// argument as a string and appending it to a given output std::string. +// Formatters may be implemented as function objects, lambdas, or normal +// functions. You may provide your own Formatter to enable `absl::StrJoin()` to +// work with arbitrary types. // // The following is an example of a custom Formatter that simply uses -// `std::to_string()` to format an integer as a string. +// `std::to_string()` to format an integer as a std::string. // // struct MyFormatter { -// void operator()(string* out, int i) const { +// void operator()(std::string* out, int i) const { // out->append(std::to_string(i)); // } // }; @@ -84,7 +84,7 @@ namespace absl { // argument to `absl::StrJoin()`: // // std::vector v = {1, 2, 3, 4}; -// string s = absl::StrJoin(v, "-", MyFormatter()); +// std::string s = absl::StrJoin(v, "-", MyFormatter()); // EXPECT_EQ("1-2-3-4", s); // // The following standard formatters are provided within this file: @@ -156,7 +156,7 @@ DereferenceFormatter() { // StrJoin() // ----------------------------------------------------------------------------- // -// Joins a range of elements and returns the result as a string. +// Joins a range of elements and returns the result as a std::string. // `absl::StrJoin()` takes a range, a separator string to use between the // elements joined, and an optional Formatter responsible for converting each // argument in the range to a string. @@ -167,22 +167,22 @@ DereferenceFormatter() { // Example 1: // // Joins a collection of strings. This pattern also works with a collection // // of `absl::string_view` or even `const char*`. -// std::vector v = {"foo", "bar", "baz"}; -// string s = absl::StrJoin(v, "-"); +// std::vector v = {"foo", "bar", "baz"}; +// std::string s = absl::StrJoin(v, "-"); // EXPECT_EQ("foo-bar-baz", s); // // Example 2: // // Joins the values in the given `std::initializer_list<>` specified using // // brace initialization. This pattern also works with an initializer_list // // of ints or `absl::string_view` -- any `AlphaNum`-compatible type. -// string s = absl::StrJoin({"foo", "bar", "baz"}, "-"); +// std::string s = absl::StrJoin({"foo", "bar", "baz"}, "-"); // EXPECT_EQ("foo-bar-baz", s); // // Example 3: // // Joins a collection of ints. This pattern also works with floats, // // doubles, int64s -- any `StrCat()`-compatible type. // std::vector v = {1, 2, 3, -4}; -// string s = absl::StrJoin(v, "-"); +// std::string s = absl::StrJoin(v, "-"); // EXPECT_EQ("1-2-3--4", s); // // Example 4: @@ -193,7 +193,7 @@ DereferenceFormatter() { // // `std::vector`. // int x = 1, y = 2, z = 3; // std::vector v = {&x, &y, &z}; -// string s = absl::StrJoin(v, "-"); +// std::string s = absl::StrJoin(v, "-"); // EXPECT_EQ("1-2-3", s); // // Example 5: @@ -202,53 +202,53 @@ DereferenceFormatter() { // v.emplace_back(new int(1)); // v.emplace_back(new int(2)); // v.emplace_back(new int(3)); -// string s = absl::StrJoin(v, "-"); +// std::string s = absl::StrJoin(v, "-"); // EXPECT_EQ("1-2-3", s); // // Example 6: // // Joins a `std::map`, with each key-value pair separated by an equals // // sign. This pattern would also work with, say, a // // `std::vector>`. -// std::map m = { +// std::map m = { // std::make_pair("a", 1), // std::make_pair("b", 2), // std::make_pair("c", 3)}; -// string s = absl::StrJoin(m, ",", absl::PairFormatter("=")); +// std::string s = absl::StrJoin(m, ",", absl::PairFormatter("=")); // EXPECT_EQ("a=1,b=2,c=3", s); // // Example 7: // // These examples show how `absl::StrJoin()` handles a few common edge // // cases: -// std::vector v_empty; +// std::vector v_empty; // EXPECT_EQ("", absl::StrJoin(v_empty, "-")); // -// std::vector v_one_item = {"foo"}; +// std::vector v_one_item = {"foo"}; // EXPECT_EQ("foo", absl::StrJoin(v_one_item, "-")); // -// std::vector v_empty_string = {""}; +// std::vector v_empty_string = {""}; // EXPECT_EQ("", absl::StrJoin(v_empty_string, "-")); // -// std::vector v_one_item_empty_string = {"a", ""}; +// std::vector v_one_item_empty_string = {"a", ""}; // EXPECT_EQ("a-", absl::StrJoin(v_one_item_empty_string, "-")); // -// std::vector v_two_empty_string = {"", ""}; +// std::vector v_two_empty_string = {"", ""}; // EXPECT_EQ("-", absl::StrJoin(v_two_empty_string, "-")); // // Example 8: // // Joins a `std::tuple` of heterogeneous types, converting each to -// // a string using the `absl::AlphaNum` class. -// string s = absl::StrJoin(std::make_tuple(123, "abc", 0.456), "-"); +// // a std::string using the `absl::AlphaNum` class. +// std::string s = absl::StrJoin(std::make_tuple(123, "abc", 0.456), "-"); // EXPECT_EQ("123-abc-0.456", s); template std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, - Formatter&& fmt) { + Formatter&& fmt) { return strings_internal::JoinAlgorithm(start, end, sep, fmt); } template std::string StrJoin(const Range& range, absl::string_view separator, - Formatter&& fmt) { + Formatter&& fmt) { return strings_internal::JoinRange(range, separator, fmt); } @@ -260,7 +260,7 @@ std::string StrJoin(std::initializer_list il, absl::string_view separator, template std::string StrJoin(const std::tuple& value, absl::string_view separator, - Formatter&& fmt) { + Formatter&& fmt) { return strings_internal::JoinAlgorithm(value, separator, fmt); } @@ -280,7 +280,8 @@ std::string StrJoin(std::initializer_list il, absl::string_view separator) { } template -std::string StrJoin(const std::tuple& value, absl::string_view separator) { +std::string StrJoin(const std::tuple& value, + absl::string_view separator) { return strings_internal::JoinAlgorithm(value, separator, AlphaNumFormatter()); } diff --git a/absl/strings/str_join_benchmark.cc b/absl/strings/str_join_benchmark.cc index 7fb0e497..f423e123 100644 --- a/absl/strings/str_join_benchmark.cc +++ b/absl/strings/str_join_benchmark.cc @@ -58,7 +58,8 @@ void BM_Join2_KeysAndValues(benchmark::State& state) { const int string_len = state.range(0); const int num_pairs = state.range(1); const std::string s(string_len, 'x'); - const std::vector> v(num_pairs, std::make_pair(s, 42)); + const std::vector> v(num_pairs, + std::make_pair(s, 42)); for (auto _ : state) { std::string s = absl::StrJoin(v, ",", absl::PairFormatter("=")); benchmark::DoNotOptimize(s); diff --git a/absl/strings/str_join_test.cc b/absl/strings/str_join_test.cc index c941f9c8..de9c3551 100644 --- a/absl/strings/str_join_test.cc +++ b/absl/strings/str_join_test.cc @@ -118,7 +118,7 @@ TEST(StrJoin, APIExamples) { { // A std::map, which is a collection of std::pair<>s. - std::map m = { {"a", 1}, {"b", 2}, {"c", 3} }; + std::map m = {{"a", 1}, {"b", 2}, {"c", 3}}; EXPECT_EQ("a=1,b=2,c=3", absl::StrJoin(m, ",", absl::PairFormatter("="))); } @@ -140,7 +140,8 @@ TEST(StrJoin, APIExamples) { } { - // A range of 1 element gives a std::string with that element but no separator. + // A range of 1 element gives a std::string with that element but no + // separator. std::vector v = {"foo"}; EXPECT_EQ("foo", absl::StrJoin(v, "-")); } @@ -173,9 +174,10 @@ TEST(StrJoin, APIExamples) { TEST(StrJoin, CustomFormatter) { std::vector v{"One", "Two", "Three"}; { - std::string joined = absl::StrJoin(v, "", [](std::string* out, const std::string& in) { - absl::StrAppend(out, "(", in, ")"); - }); + std::string joined = + absl::StrJoin(v, "", [](std::string* out, const std::string& in) { + absl::StrAppend(out, "(", in, ")"); + }); EXPECT_EQ("(One)(Two)(Three)", joined); } { diff --git a/absl/strings/str_replace.cc b/absl/strings/str_replace.cc index 69efa357..d01b8b11 100644 --- a/absl/strings/str_replace.cc +++ b/absl/strings/str_replace.cc @@ -68,11 +68,12 @@ int ApplySubstitutions( // aren't inlined. std::string StrReplaceAll(absl::string_view s, - strings_internal::FixedMapping replacements) { + strings_internal::FixedMapping replacements) { return StrReplaceAll(s, replacements); } -int StrReplaceAll(strings_internal::FixedMapping replacements, std::string* target) { +int StrReplaceAll(strings_internal::FixedMapping replacements, + std::string* target) { return StrReplaceAll(replacements, target); } diff --git a/absl/strings/str_replace.h b/absl/strings/str_replace.h index 3bfe4c61..6d284042 100644 --- a/absl/strings/str_replace.h +++ b/absl/strings/str_replace.h @@ -29,12 +29,12 @@ // // Example: // -// string html_escaped = absl::StrReplaceAll(user_input, { -// {"&", "&"}, -// {"<", "<"}, -// {">", ">"}, -// {"\"", """}, -// {"'", "'"}}); +// std::string html_escaped = absl::StrReplaceAll(user_input, { +// {"&", "&"}, +// {"<", "<"}, +// {">", ">"}, +// {"\"", """}, +// {"'", "'"}}); #ifndef ABSL_STRINGS_STR_REPLACE_H_ #define ABSL_STRINGS_STR_REPLACE_H_ @@ -58,10 +58,11 @@ namespace absl { // // Example: // -// string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!", -// {{"$count", absl::StrCat(5)}, -// {"$who", "Bob"}, -// {"#Noun", "Apples"}}); +// std::string s = absl::StrReplaceAll( +// "$who bought $count #Noun. Thanks $who!", +// {{"$count", absl::StrCat(5)}, +// {"$who", "Bob"}, +// {"#Noun", "Apples"}}); // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s); ABSL_MUST_USE_RESULT std::string StrReplaceAll( absl::string_view s, @@ -78,20 +79,22 @@ ABSL_MUST_USE_RESULT std::string StrReplaceAll( // replacements["$who"] = "Bob"; // replacements["$count"] = "5"; // replacements["#Noun"] = "Apples"; -// string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!", -// replacements); +// std::string s = absl::StrReplaceAll( +// "$who bought $count #Noun. Thanks $who!", +// replacements); // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s); // // // A std::vector of std::pair elements can be more efficient. -// std::vector> replacements; +// std::vector> replacements; // replacements.push_back({"&", "&"}); // replacements.push_back({"<", "<"}); // replacements.push_back({">", ">"}); -// string s = absl::StrReplaceAll("if (ptr < &foo)", +// std::string s = absl::StrReplaceAll("if (ptr < &foo)", // replacements); // EXPECT_EQ("if (ptr < &foo)", s); template -std::string StrReplaceAll(absl::string_view s, const StrToStrMapping& replacements); +std::string StrReplaceAll(absl::string_view s, + const StrToStrMapping& replacements); // Overload of `StrReplaceAll()` to replace character sequences within a given // output string *in place* with replacements provided within an initializer @@ -99,7 +102,7 @@ std::string StrReplaceAll(absl::string_view s, const StrToStrMapping& replacemen // // Example: // -// string s = std::string("$who bought $count #Noun. Thanks $who!"); +// std::string s = std::string("$who bought $count #Noun. Thanks $who!"); // int count; // count = absl::StrReplaceAll({{"$count", absl::StrCat(5)}, // {"$who", "Bob"}, @@ -117,7 +120,7 @@ int StrReplaceAll( // // Example: // -// string s = std::string("if (ptr < &foo)"); +// std::string s = std::string("if (ptr < &foo)"); // int count = absl::StrReplaceAll({{"&", "&"}, // {"<", "<"}, // {">", ">"}}, &s); @@ -187,7 +190,8 @@ int ApplySubstitutions(absl::string_view s, } // namespace strings_internal template -std::string StrReplaceAll(absl::string_view s, const StrToStrMapping& replacements) { +std::string StrReplaceAll(absl::string_view s, + const StrToStrMapping& replacements) { auto subs = strings_internal::FindSubstitutions(s, replacements); std::string result; result.reserve(s.size()); diff --git a/absl/strings/str_replace_benchmark.cc b/absl/strings/str_replace_benchmark.cc index 8386f2e6..07fd3a70 100644 --- a/absl/strings/str_replace_benchmark.cc +++ b/absl/strings/str_replace_benchmark.cc @@ -54,7 +54,7 @@ void SetUpStrings() { size_t r = 0; big_string = new std::string(1000 * 1000, ' '); for (std::string phrase : {"the quick brown fox jumped over the lazy dogs", - "pack my box with the five dozen liquor jugs"}) { + "pack my box with the five dozen liquor jugs"}) { for (int i = 0; i < 10 * 1000; ++i) { r = r * 237 + 41; // not very random. memcpy(&(*big_string)[r % (big_string->size() - phrase.size())], @@ -108,11 +108,11 @@ void BM_StrReplaceAll(benchmark::State& state) { std::string src = *big_string; for (auto _ : state) { std::string dest = absl::StrReplaceAll(src, {{"the", "box"}, - {"brown", "quick"}, - {"jumped", "liquored"}, - {"dozen", "brown"}, - {"lazy", "pack"}, - {"liquor", "shakes"}}); + {"brown", "quick"}, + {"jumped", "liquored"}, + {"dozen", "brown"}, + {"lazy", "pack"}, + {"liquor", "shakes"}}); ABSL_RAW_CHECK(dest == *after_replacing_many, "not benchmarking intended behavior"); } diff --git a/absl/strings/str_replace_test.cc b/absl/strings/str_replace_test.cc index 5d003a22..73c69a59 100644 --- a/absl/strings/str_replace_test.cc +++ b/absl/strings/str_replace_test.cc @@ -148,7 +148,7 @@ TEST(StrReplaceAll, ManyReplacementsInMap) { replacements["$count"] = "5"; replacements["#Noun"] = "Apples"; std::string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!", - replacements); + replacements); EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s); } diff --git a/absl/strings/str_split.h b/absl/strings/str_split.h index 86effd30..dc45bc8a 100644 --- a/absl/strings/str_split.h +++ b/absl/strings/str_split.h @@ -132,8 +132,7 @@ class ByString { // ByChar // // A single character delimiter. `ByChar` is functionally equivalent to a -// 1-char string within a `ByString` delimiter, but slightly more -// efficient. +// 1-char string within a `ByString` delimiter, but slightly more efficient. // // Example: // @@ -414,10 +413,10 @@ struct SkipWhitespace { // // The `StrSplit()` function adapts the returned collection to the collection // specified by the caller (e.g. `std::vector` above). The returned collections -// may contain `string`, `absl::string_view` (in which case the original string -// being split must ensure that it outlives the collection), or any object that -// can be explicitly created from an `absl::string_view`. This behavior works -// for: +// may contain `std::string`, `absl::string_view` (in which case the original +// string being split must ensure that it outlives the collection), or any +// object that can be explicitly created from an `absl::string_view`. This +// behavior works for: // // 1) All standard STL containers including `std::vector`, `std::list`, // `std::deque`, `std::set`,`std::multiset`, 'std::map`, and `std::multimap` @@ -461,7 +460,7 @@ struct SkipWhitespace { // Example: // // // Stores first two split strings as the members in a std::pair. -// std::pair p = absl::StrSplit("a,b,c", ','); +// std::pair p = absl::StrSplit("a,b,c", ','); // // p.first == "a", p.second == "b" // "c" is omitted. // // The `StrSplit()` function can be used multiple times to perform more @@ -471,7 +470,7 @@ struct SkipWhitespace { // // // The input string "a=b=c,d=e,f=,g" becomes // // { "a" => "b=c", "d" => "e", "f" => "", "g" => "" } -// std::map m; +// std::map m; // for (absl::string_view sp : absl::StrSplit("a=b=c,d=e,f=,g", ',')) { // m.insert(absl::StrSplit(sp, absl::MaxSplits('=', 1))); // } diff --git a/absl/strings/str_split_benchmark.cc b/absl/strings/str_split_benchmark.cc index 0ac297c8..6280568a 100644 --- a/absl/strings/str_split_benchmark.cc +++ b/absl/strings/str_split_benchmark.cc @@ -69,7 +69,8 @@ BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20); void BM_Split2SplitStringUsing(benchmark::State& state) { std::string test = MakeTestString(state.range(0)); for (auto _ : state) { - std::vector result = absl::StrSplit(test, ';', absl::SkipEmpty()); + std::vector result = + absl::StrSplit(test, ';', absl::SkipEmpty()); benchmark::DoNotOptimize(result); } } diff --git a/absl/strings/str_split_test.cc b/absl/strings/str_split_test.cc index a0085582..babead92 100644 --- a/absl/strings/str_split_test.cc +++ b/absl/strings/str_split_test.cc @@ -40,8 +40,8 @@ using ::testing::UnorderedElementsAre; TEST(Split, TraitsTest) { static_assert(!absl::strings_internal::SplitterIsConvertibleTo::value, ""); - static_assert(!absl::strings_internal::SplitterIsConvertibleTo::value, - ""); + static_assert( + !absl::strings_internal::SplitterIsConvertibleTo::value, ""); static_assert(absl::strings_internal::SplitterIsConvertibleTo< std::vector>::value, ""); @@ -182,7 +182,8 @@ TEST(Split, APIExamples) { { // Uses the SkipWhitespace predicate. using absl::SkipWhitespace; - std::vector v = absl::StrSplit(" a , ,,b,", ',', SkipWhitespace()); + std::vector v = + absl::StrSplit(" a , ,,b,", ',', SkipWhitespace()); EXPECT_THAT(v, ElementsAre(" a ", "b")); } @@ -215,7 +216,8 @@ TEST(Split, APIExamples) { { // Results stored in a std::multimap. - std::multimap m = absl::StrSplit("a,1,b,2,a,3", ','); + std::multimap m = + absl::StrSplit("a,1,b,2,a,3", ','); EXPECT_EQ(3, m.size()); auto it = m.find("a"); EXPECT_EQ("1", it->second); @@ -271,7 +273,8 @@ TEST(SplitIterator, Basics) { EXPECT_EQ("a", *it); // tests dereference ++it; // tests preincrement EXPECT_NE(it, end); - EXPECT_EQ("b", std::string(it->data(), it->size())); // tests dereference as ptr + EXPECT_EQ("b", + std::string(it->data(), it->size())); // tests dereference as ptr it++; // tests postincrement EXPECT_EQ(it, end); } @@ -295,7 +298,8 @@ TEST(SplitIterator, Predicate) { EXPECT_EQ("a", *it); // tests dereference ++it; // tests preincrement -- "b" should be skipped here. EXPECT_NE(it, end); - EXPECT_EQ("c", std::string(it->data(), it->size())); // tests dereference as ptr + EXPECT_EQ("c", + std::string(it->data(), it->size())); // tests dereference as ptr it++; // tests postincrement EXPECT_EQ(it, end); } @@ -421,10 +425,13 @@ TEST(Splitter, ConversionOperator) { TestMapConversionOperator>(splitter); TestMapConversionOperator< std::multimap>(splitter); - TestMapConversionOperator>(splitter); - TestMapConversionOperator>(splitter); + TestMapConversionOperator>( + splitter); + TestMapConversionOperator>( + splitter); TestMapConversionOperator>(splitter); - TestMapConversionOperator>(splitter); + TestMapConversionOperator>( + splitter); // Tests conversion to std::pair @@ -568,10 +575,9 @@ TEST(Split, AcceptsCertainTemporaries) { } TEST(Split, Temporary) { - // Use a std::string longer than the small-std::string-optimization length, so that when - // the temporary is destroyed, if the splitter keeps a reference to the - // std::string's contents, it'll reference freed memory instead of just dead - // on-stack memory. + // Use a std::string longer than the SSO length, so that when the temporary is + // destroyed, if the splitter keeps a reference to the std::string's contents, + // it'll reference freed memory instead of just dead on-stack memory. const char input[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u"; EXPECT_LT(sizeof(std::string), ABSL_ARRAYSIZE(input)) << "Input should be larger than fits on the stack."; diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index 8cd4fa24..72f0f414 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -277,7 +277,7 @@ class string_view { // Checks if the `string_view` is empty (refers to no characters). constexpr bool empty() const noexcept { return length_ == 0; } - // std::string:view::operator[] + // string_view::operator[] // // Returns the ith element of an `string_view` using the array operator. // Note that this operator does not perform any bounds checking. diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc index ed34ed83..2150b4e5 100644 --- a/absl/strings/string_view_test.cc +++ b/absl/strings/string_view_test.cc @@ -284,9 +284,10 @@ TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) { } #undef COMPARE -// Sadly, our users often confuse string::npos with absl::string_view::npos; -// So much so that we test here that they are the same. They need to -// both be unsigned, and both be the maximum-valued integer of their type. +// Sadly, our users often confuse std::string::npos with +// absl::string_view::npos; So much so that we test here that they are the same. +// They need to both be unsigned, and both be the maximum-valued integer of +// their type. template struct is_type { @@ -995,8 +996,8 @@ TEST(StringViewTest, ConstexprCompiles) { TEST(StringViewTest, Noexcept) { EXPECT_TRUE((std::is_nothrow_constructible::value)); - EXPECT_TRUE( - (std::is_nothrow_constructible::value)); + EXPECT_TRUE((std::is_nothrow_constructible::value)); EXPECT_TRUE(std::is_nothrow_constructible::value); constexpr absl::string_view sp; EXPECT_TRUE(noexcept(sp.begin())); diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h index 4de7b4e7..2b74b382 100644 --- a/absl/strings/substitute.h +++ b/absl/strings/substitute.h @@ -36,18 +36,18 @@ // use at that location within the format string. // // Example 1: -// string s = Substitute("$1 purchased $0 $2. Thanks $1!", -// 5, "Bob", "Apples"); +// std::string s = Substitute("$1 purchased $0 $2. Thanks $1!", +// 5, "Bob", "Apples"); // EXPECT_EQ("Bob purchased 5 Apples. Thanks Bob!", s); // // Example 2: -// string s = "Hi. "; +// std::string s = "Hi. "; // SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5); // EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s); // // // Supported types: -// * absl::string_view, string, const char* (null is equivalent to "") +// * absl::string_view, std::string, const char* (null is equivalent to "") // * int32_t, int64_t, uint32_t, uint64 // * float, double // * bool (Printed as "true" or "false") @@ -456,7 +456,7 @@ void SubstituteAndAppend( // Example: // template // void VarMsg(absl::string_view format, const Args&... args) { -// string s = absl::Substitute(format, args...); +// std::string s = absl::Substitute(format, args...); ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) { std::string result; @@ -574,70 +574,70 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0) "contains one of $1-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1) + const substitute_internal::Arg& a1) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3, "There were 2 substitution arguments given, but " "this format std::string is either missing its $0/$1, or " "contains one of $2-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7, "There were 3 substitution arguments given, but " "this format std::string is either missing its $0/$1/$2, or " "contains one of $3-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2, - const substitute_internal::Arg& a3) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2, + const substitute_internal::Arg& a3) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15, "There were 4 substitution arguments given, but " "this format std::string is either missing its $0-$3, or " "contains one of $4-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2, - const substitute_internal::Arg& a3, - const substitute_internal::Arg& a4) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2, + const substitute_internal::Arg& a3, + const substitute_internal::Arg& a4) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31, "There were 5 substitution arguments given, but " "this format std::string is either missing its $0-$4, or " "contains one of $5-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2, - const substitute_internal::Arg& a3, - const substitute_internal::Arg& a4, - const substitute_internal::Arg& a5) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2, + const substitute_internal::Arg& a3, + const substitute_internal::Arg& a4, + const substitute_internal::Arg& a5) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63, "There were 6 substitution arguments given, but " "this format std::string is either missing its $0-$5, or " "contains one of $6-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2, - const substitute_internal::Arg& a3, - const substitute_internal::Arg& a4, - const substitute_internal::Arg& a5, - const substitute_internal::Arg& a6) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2, + const substitute_internal::Arg& a3, + const substitute_internal::Arg& a4, + const substitute_internal::Arg& a5, + const substitute_internal::Arg& a6) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127, "There were 7 substitution arguments given, but " "this format std::string is either missing its $0-$6, or " "contains one of $7-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, - const substitute_internal::Arg& a1, - const substitute_internal::Arg& a2, - const substitute_internal::Arg& a3, - const substitute_internal::Arg& a4, - const substitute_internal::Arg& a5, - const substitute_internal::Arg& a6, - const substitute_internal::Arg& a7) + const substitute_internal::Arg& a1, + const substitute_internal::Arg& a2, + const substitute_internal::Arg& a3, + const substitute_internal::Arg& a4, + const substitute_internal::Arg& a5, + const substitute_internal::Arg& a6, + const substitute_internal::Arg& a7) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255, "There were 8 substitution arguments given, but " "this format std::string is either missing its $0-$7, or " diff --git a/absl/synchronization/internal/per_thread_sem_test.cc b/absl/synchronization/internal/per_thread_sem_test.cc index 81cbe957..8a318a51 100644 --- a/absl/synchronization/internal/per_thread_sem_test.cc +++ b/absl/synchronization/internal/per_thread_sem_test.cc @@ -114,10 +114,9 @@ class PerThreadSemTest : public testing::Test { min_cycles = std::min(min_cycles, cycles); total_cycles += cycles; } - std::string out = - StrCat(msg, "min cycle count=", min_cycles, " avg cycle count=", - absl::SixDigits(static_cast(total_cycles) / - kNumIterations)); + std::string out = StrCat( + msg, "min cycle count=", min_cycles, " avg cycle count=", + absl::SixDigits(static_cast(total_cycles) / kNumIterations)); printf("%s\n", out.c_str()); partner_thread.join(); diff --git a/absl/time/BUILD.bazel b/absl/time/BUILD.bazel index 2082f52c..578dc917 100644 --- a/absl/time/BUILD.bazel +++ b/absl/time/BUILD.bazel @@ -110,6 +110,7 @@ cc_test( ":test_util", ":time", "//absl/base", + "//absl/base:core_headers", "//absl/hash", "@com_github_google_benchmark//:benchmark_main", ], diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc index 56541799..c7ba8916 100644 --- a/absl/time/civil_time.cc +++ b/absl/time/civil_time.cc @@ -38,7 +38,8 @@ std::string FormatYearAnd(string_view fmt, CivilSecond cs) { cs.hour(), cs.minute(), cs.second()); const TimeZone utc = UTCTimeZone(); // TODO(absl-team): Avoid conversion of fmt std::string. - return StrCat(cs.year(), FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); + return StrCat(cs.year(), + FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); } } // namespace @@ -52,15 +53,9 @@ std::string FormatCivilTime(CivilMinute c) { std::string FormatCivilTime(CivilHour c) { return FormatYearAnd("-%m-%dT%H", c); } -std::string FormatCivilTime(CivilDay c) { - return FormatYearAnd("-%m-%d", c); -} -std::string FormatCivilTime(CivilMonth c) { - return FormatYearAnd("-%m", c); -} -std::string FormatCivilTime(CivilYear c) { - return FormatYearAnd("", c); -} +std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d", c); } +std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m", c); } +std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("", c); } namespace time_internal { diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h index 30d73c87..fd7f1e85 100644 --- a/absl/time/civil_time.h +++ b/absl/time/civil_time.h @@ -451,7 +451,7 @@ inline int GetYearDay(CivilDay cd) { // Example: // // absl::CivilDay d = absl::CivilDay(1969, 7, 20); -// string day_string = absl::FormatCivilTime(d); // "1969-07-20" +// std::string day_string = absl::FormatCivilTime(d); // "1969-07-20" // std::string FormatCivilTime(CivilSecond c); std::string FormatCivilTime(CivilMinute c); diff --git a/absl/time/duration.cc b/absl/time/duration.cc index b77d5ec9..7d4af8c7 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -751,7 +751,7 @@ void AppendNumberUnit(std::string* out, double n, DisplayUnit unit) { // From Go's doc at http://golang.org/pkg/time/#Duration.String // [FormatDuration] returns a string representing the duration in the -// form "72h3m0.5s". Leading zero units are omitted. As a special +// form "72h3m0.5s". Leading zero units are omitted. As a special // case, durations less than one second format use a smaller unit // (milli-, micro-, or nanoseconds) to ensure that the leading digit // is non-zero. The zero duration formats as 0, with no unit. @@ -856,7 +856,7 @@ bool ConsumeDurationUnit(const char** start, Duration* unit) { } // namespace // From Go's doc at http://golang.org/pkg/time/#ParseDuration -// [ParseDuration] parses a duration string. A duration string is +// [ParseDuration] parses a duration string. A duration string is // a possibly signed sequence of decimal numbers, each with optional // fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". // Valid time units are "ns", "us" "ms", "s", "m", "h". diff --git a/absl/time/duration_benchmark.cc b/absl/time/duration_benchmark.cc index d5657bd5..f5fcdfb8 100644 --- a/absl/time/duration_benchmark.cc +++ b/absl/time/duration_benchmark.cc @@ -17,6 +17,7 @@ #include #include +#include "absl/base/attributes.h" #include "absl/time/time.h" #include "benchmark/benchmark.h" diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc index 61f3c5c0..6dc307a9 100644 --- a/absl/time/duration_test.cc +++ b/absl/time/duration_test.cc @@ -1771,7 +1771,7 @@ TEST(Duration, ParseDuration) { TEST(Duration, FormatParseRoundTrip) { #define TEST_PARSE_ROUNDTRIP(d) \ do { \ - std::string s = absl::FormatDuration(d); \ + std::string s = absl::FormatDuration(d); \ absl::Duration dur; \ EXPECT_TRUE(absl::ParseDuration(s, &dur)); \ EXPECT_EQ(d, dur); \ diff --git a/absl/time/format.cc b/absl/time/format.cc index ee597e40..a3671510 100644 --- a/absl/time/format.cc +++ b/absl/time/format.cc @@ -67,7 +67,8 @@ absl::Time Join(const cctz_parts& parts) { } // namespace -std::string FormatTime(const std::string& format, absl::Time t, absl::TimeZone tz) { +std::string FormatTime(const std::string& format, absl::Time t, + absl::TimeZone tz) { if (t == absl::InfiniteFuture()) return kInfiniteFutureStr; if (t == absl::InfinitePast()) return kInfinitePastStr; const auto parts = Split(t); @@ -83,15 +84,15 @@ std::string FormatTime(absl::Time t) { return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone()); } -bool ParseTime(const std::string& format, const std::string& input, absl::Time* time, - std::string* err) { +bool ParseTime(const std::string& format, const std::string& input, + absl::Time* time, std::string* err) { return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err); } // If the input string does not contain an explicit UTC offset, interpret // the fields with respect to the given TimeZone. -bool ParseTime(const std::string& format, const std::string& input, absl::TimeZone tz, - absl::Time* time, std::string* err) { +bool ParseTime(const std::string& format, const std::string& input, + absl::TimeZone tz, absl::Time* time, std::string* err) { const char* data = input.c_str(); while (std::isspace(*data)) ++data; diff --git a/absl/time/format_test.cc b/absl/time/format_test.cc index ac8d5ea3..46b972e6 100644 --- a/absl/time/format_test.cc +++ b/absl/time/format_test.cc @@ -27,8 +27,8 @@ namespace { // A helper that tests the given format specifier by itself, and with leading // and trailing characters. For example: TestFormatSpecifier(t, "%a", "Thu"). -void TestFormatSpecifier(absl::Time t, absl::TimeZone tz, const std::string& fmt, - const std::string& ans) { +void TestFormatSpecifier(absl::Time t, absl::TimeZone tz, + const std::string& fmt, const std::string& ans) { EXPECT_EQ(ans, absl::FormatTime(fmt, t, tz)); EXPECT_EQ("xxx " + ans, absl::FormatTime("xxx " + fmt, t, tz)); EXPECT_EQ(ans + " yyy", absl::FormatTime(fmt + " yyy", t, tz)); @@ -375,7 +375,8 @@ TEST(FormatParse, RoundTrip) { // RFC3339, which renders subseconds. { absl::Time out; - const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, lax); + const std::string s = + absl::FormatTime(absl::RFC3339_full, in + subseconds, lax); EXPECT_TRUE(absl::ParseTime(absl::RFC3339_full, s, &out, &err)) << s << ": " << err; EXPECT_EQ(in + subseconds, out); // RFC3339_full includes %Ez diff --git a/absl/time/time.h b/absl/time/time.h index 3cf9ac02..45df1fc7 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -77,7 +77,6 @@ #include #include -#include "absl/base/port.h" // Needed for string vs std::string #include "absl/strings/string_view.h" #include "absl/time/civil_time.h" #include "absl/time/internal/cctz/include/cctz/time_zone.h" @@ -1222,7 +1221,7 @@ extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z // // absl::CivilSecond cs(2013, 1, 2, 3, 4, 5); // absl::Time t = absl::FromCivil(cs, lax); -// string f = absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05" +// std::string f = absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05" // f = absl::FormatTime("%H:%M:%E3S", t, lax); // "03:04:05.000" // // Note: If the given `absl::Time` is `absl::InfiniteFuture()`, the returned diff --git a/absl/types/optional_test.cc b/absl/types/optional_test.cc index bedf5b0e..cdbf1404 100644 --- a/absl/types/optional_test.cc +++ b/absl/types/optional_test.cc @@ -1476,8 +1476,8 @@ TEST(optionalTest, MoveAssignRegression) { TEST(optionalTest, ValueType) { EXPECT_TRUE((std::is_same::value_type, int>::value)); - EXPECT_TRUE( - (std::is_same::value_type, std::string>::value)); + EXPECT_TRUE((std::is_same::value_type, + std::string>::value)); EXPECT_FALSE( (std::is_same::value_type, absl::nullopt_t>::value)); } diff --git a/absl/types/span.h b/absl/types/span.h index 911af0c5..a445f7af 100644 --- a/absl/types/span.h +++ b/absl/types/span.h @@ -87,7 +87,7 @@ constexpr auto GetDataImpl(C& c, char) noexcept // NOLINT(runtime/references) return c.data(); } -// Before C++17, string::data returns a const char* in all cases. +// Before C++17, std::string::data returns a const char* in all cases. inline char* GetDataImpl(std::string& s, // NOLINT(runtime/references) int) noexcept { return &s[0]; diff --git a/absl/types/span_test.cc b/absl/types/span_test.cc index f4203b52..ae71ebc2 100644 --- a/absl/types/span_test.cc +++ b/absl/types/span_test.cc @@ -139,8 +139,10 @@ TEST(CharSpan, StringCtor) { EXPECT_THAT(s_const_abc, SpanIs(abc)); EXPECT_FALSE((std::is_constructible, std::string>::value)); - EXPECT_FALSE((std::is_constructible, std::string>::value)); - EXPECT_TRUE((std::is_convertible>::value)); + EXPECT_FALSE( + (std::is_constructible, std::string>::value)); + EXPECT_TRUE( + (std::is_convertible>::value)); } TEST(IntSpan, FromConstPointer) { diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc index 463d775a..c18cb77a 100644 --- a/absl/types/variant_test.cc +++ b/absl/types/variant_test.cc @@ -484,7 +484,8 @@ TEST(VariantTest, InPlaceType) { } TEST(VariantTest, InPlaceTypeInitializerList) { - using Var = variant; + using Var = + variant; Var v1(in_place_type_t(), {1, 2, 3, 4, 5}, 6); ASSERT_TRUE(absl::holds_alternative(v1)); @@ -519,7 +520,8 @@ TEST(VariantTest, InPlaceIndex) { } TEST(VariantTest, InPlaceIndexInitializerList) { - using Var = variant; + using Var = + variant; Var v1(in_place_index_t<3>(), {1, 2, 3, 4, 5}, 6); ASSERT_TRUE(absl::holds_alternative(v1)); @@ -831,7 +833,8 @@ TEST(VariantTest, TestEmplaceBasic) { } TEST(VariantTest, TestEmplaceInitializerList) { - using Var = variant; + using Var = + variant; Var v1(absl::in_place_index_t<0>{}, 555); MoveOnlyWithListConstructor& emplace_result = @@ -868,7 +871,8 @@ TEST(VariantTest, TestEmplaceIndex) { } TEST(VariantTest, TestEmplaceIndexInitializerList) { - using Var = variant; + using Var = + variant; Var v1(absl::in_place_index_t<0>{}, 555); MoveOnlyWithListConstructor& emplace_result = @@ -1306,7 +1310,8 @@ TEST(VariantTest, BadGetType) { absl::get(std::move(v))); const Var& const_v = v; - ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get(const_v)); + ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS( + absl::get(const_v)); ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS( absl::get(std::move(const_v))); // NOLINT } @@ -1363,7 +1368,8 @@ TEST(VariantTest, GetIfIndex) { EXPECT_EQ(*elem, 0); { auto* bad_elem = absl::get_if<1>(&const_v); - EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE( + (std::is_same::value)); EXPECT_EQ(bad_elem, nullptr); } { @@ -1472,7 +1478,8 @@ TEST(VariantTest, GetIfIndex) { } { auto* bad_elem = absl::get_if<1>(&const_v); - EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE( + (std::is_same::value)); EXPECT_EQ(bad_elem, nullptr); } { @@ -1525,7 +1532,8 @@ TEST(VariantTest, GetIfIndex) { } { auto* bad_elem = absl::get_if<1>(&const_v); - EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE( + (std::is_same::value)); EXPECT_EQ(bad_elem, nullptr); } { @@ -1728,9 +1736,13 @@ TEST(VariantTest, VisitRValue) { bool operator()(std::string&&) const { return true; } // NOLINT int operator()(const std::string&, const std::string&) const { return 0; } - int operator()(const std::string&, std::string&&) const { return 1; } // NOLINT - int operator()(std::string&&, const std::string&) const { return 2; } // NOLINT - int operator()(std::string&&, std::string&&) const { return 3; } // NOLINT + int operator()(const std::string&, std::string&&) const { + return 1; + } // NOLINT + int operator()(std::string&&, const std::string&) const { + return 2; + } // NOLINT + int operator()(std::string&&, std::string&&) const { return 3; } // NOLINT }; EXPECT_FALSE(absl::visit(Visitor{}, v)); EXPECT_TRUE(absl::visit(Visitor{}, absl::move(v))); @@ -1806,9 +1818,9 @@ TEST(VariantTest, VisitVariadic) { EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")), B(std::unique_ptr(new int(7)))), ::testing::Pair(5, 7)); - EXPECT_THAT( - absl::visit(Visitor(), A(std::string("BBBBB")), B(absl::string_view("ABC"))), - ::testing::Pair(5, 3)); + EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")), + B(absl::string_view("ABC"))), + ::testing::Pair(5, 3)); } TEST(VariantTest, VisitNoArgs) { @@ -2163,7 +2175,8 @@ TEST(VariantTest, TestImplicitConversion) { // We still need the explicit cast for std::string, because C++ won't apply // two user-defined implicit conversions in a row. - EXPECT_TRUE(absl::holds_alternative(PassThrough(std::string("foo")))); + EXPECT_TRUE( + absl::holds_alternative(PassThrough(std::string("foo")))); } struct Convertible2; @@ -2187,7 +2200,8 @@ struct Convertible2 { TEST(VariantTest, TestRvalueConversion) { variant var( - ConvertVariantTo>(variant(0))); + ConvertVariantTo>( + variant(0))); ASSERT_TRUE(absl::holds_alternative(var)); EXPECT_EQ(0.0, absl::get(var)); @@ -2279,7 +2293,8 @@ TEST(VariantTest, TestLvalueConversion) { TEST(VariantTest, TestMoveConversion) { using Variant = variant, std::unique_ptr>; - using OtherVariant = variant, std::unique_ptr>; + using OtherVariant = + variant, std::unique_ptr>; Variant var( ConvertVariantTo(OtherVariant{absl::make_unique(0)})); @@ -2287,8 +2302,8 @@ TEST(VariantTest, TestMoveConversion) { ASSERT_NE(absl::get>(var), nullptr); EXPECT_EQ(0, *absl::get>(var)); - var = - ConvertVariantTo(OtherVariant(absl::make_unique("foo"))); + var = ConvertVariantTo( + OtherVariant(absl::make_unique("foo"))); ASSERT_TRUE(absl::holds_alternative>(var)); EXPECT_EQ("foo", *absl::get>(var)); } @@ -2299,7 +2314,8 @@ TEST(VariantTest, DoesNotMoveFromLvalues) { // whether moving or copying has occurred. using Variant = variant, std::shared_ptr>; - using OtherVariant = variant, std::shared_ptr>; + using OtherVariant = + variant, std::shared_ptr>; Variant v1(std::make_shared(0)); @@ -2328,7 +2344,8 @@ TEST(VariantTest, DoesNotMoveFromLvalues) { TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) { variant var( - ConvertVariantTo>(variant(3))); + ConvertVariantTo>( + variant(3))); EXPECT_THAT(absl::get_if(&var), Pointee(3.0)); var = ConvertVariantTo>( @@ -2370,7 +2387,8 @@ TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) { variant source2 = "foo"; destination = ConvertVariantTo>(source2); - EXPECT_THAT(absl::get_if(&destination), Pointee(std::string("foo"))); + EXPECT_THAT(absl::get_if(&destination), + Pointee(std::string("foo"))); variant source3(42); variant singleton(ConvertVariantTo>(source3)); @@ -2407,15 +2425,16 @@ TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) { TEST(VariantTest, TestMoveConversionViaConvertVariantTo) { using Variant = variant, std::unique_ptr>; - using OtherVariant = variant, std::unique_ptr>; + using OtherVariant = + variant, std::unique_ptr>; Variant var( ConvertVariantTo(OtherVariant{absl::make_unique(3)})); EXPECT_THAT(absl::get_if>(&var), Pointee(Pointee(3))); - var = - ConvertVariantTo(OtherVariant(absl::make_unique("foo"))); + var = ConvertVariantTo( + OtherVariant(absl::make_unique("foo"))); EXPECT_THAT(absl::get_if>(&var), Pointee(Pointee(std::string("foo")))); } diff --git a/absl/utility/utility.h b/absl/utility/utility.h index 104ec829..741b1f81 100644 --- a/absl/utility/utility.h +++ b/absl/utility/utility.h @@ -238,14 +238,14 @@ auto apply_helper(Functor&& functor, Tuple&& t, index_sequence) // public: // void Bar(int); // }; -// void user_function1(int, string); +// void user_function1(int, std::string); // void user_function2(std::unique_ptr); // auto user_lambda = [](int, int) {}; // // int main() // { -// std::tuple tuple1(42, "bar"); -// // Invokes the first user function on int, string. +// std::tuple tuple1(42, "bar"); +// // Invokes the first user function on int, std::string. // absl::apply(&user_function1, tuple1); // // std::tuple> tuple2(absl::make_unique()); diff --git a/absl/utility/utility_test.cc b/absl/utility/utility_test.cc index 3c447b20..7f425fc7 100644 --- a/absl/utility/utility_test.cc +++ b/absl/utility/utility_test.cc @@ -135,7 +135,7 @@ struct PoorStrCat { template std::vector TupStringVecImpl(const Tup& tup, - absl::index_sequence) { + absl::index_sequence) { return {Fmt(std::get(tup))...}; } -- cgit v1.2.3 From 0cbdc774b97f7e80ab60dbe2ed4eaca3b2e33fc8 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 10 May 2019 12:38:49 -0700 Subject: Export of internal Abseil changes. -- ab1a58c85a462884413ec0022dc1fff19ccb8602 by Abseil Team : Clarified the documentation in str_format.h to say that use of absl::FormatSpec is ok for wrapper functions. Added tests that express this. PiperOrigin-RevId: 247657991 -- fef9481e58d579f1514babcb960ca60a51883fd8 by CJ Johnson : Adds exception safety tests for InlinedVector::InlinedVector() and InlinedVector::InlinedVector(const allocator_type&). PiperOrigin-RevId: 247617048 -- ef3217e1cd1e9a6ff5f2025e061b8ce3735af78f by Abseil Team : Internal change. PiperOrigin-RevId: 247614063 -- ed4c3345c4a04d8ec5c9e627058f17fce55925b1 by CJ Johnson : Update InlinedVector::clear() Introduces inlined_vector_exception_safety_test with the first test (clear), adds new benchmarks (for clear), and updates the implementation of clear. PiperOrigin-RevId: 247496049 -- 144a3a77c93bc8b2226da6f4b56166ee3d9868de by Derek Mauro : Internal change PiperOrigin-RevId: 247482532 -- 286bbb89e154d5424955b644edad5fe04be487f8 by Derek Mauro : Add scripts to run ASAN and TSAN on CI. PiperOrigin-RevId: 247479658 GitOrigin-RevId: ab1a58c85a462884413ec0022dc1fff19ccb8602 Change-Id: Ief4c5a62587d0c59d405735df469d498aa6bf101 --- absl/base/BUILD.bazel | 2 +- absl/container/BUILD.bazel | 12 ++++ absl/container/CMakeLists.txt | 16 +++++ absl/container/inlined_vector.h | 20 +++--- absl/container/inlined_vector_benchmark.cc | 74 ++++++++++++++++++- .../inlined_vector_exception_safety_test.cc | 55 ++++++++++++++ absl/container/internal/inlined_vector.h | 28 ++++++++ absl/debugging/BUILD.bazel | 3 + absl/hash/BUILD.bazel | 2 +- absl/strings/BUILD.bazel | 4 +- absl/strings/str_format.h | 15 ++-- absl/strings/str_format_test.cc | 22 +++++- absl/synchronization/mutex.cc | 16 ++--- absl/synchronization/mutex_test.cc | 15 ++++ absl/types/compare.h | 6 +- absl/types/compare_test.cc | 4 -- ci/linux_clang-latest_libcxx_asan_bazel.sh | 81 +++++++++++++++++++++ ci/linux_clang-latest_libcxx_tsan_bazel.sh | 83 ++++++++++++++++++++++ 18 files changed, 422 insertions(+), 36 deletions(-) create mode 100644 absl/container/inlined_vector_exception_safety_test.cc create mode 100755 ci/linux_clang-latest_libcxx_asan_bazel.sh create mode 100755 ci/linux_clang-latest_libcxx_tsan_bazel.sh (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 9fb1d8cd..317452da 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -444,7 +444,7 @@ cc_test( cc_test( name = "low_level_alloc_test", - size = "small", + size = "medium", srcs = ["internal/low_level_alloc_test.cc"], copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel index 0488857e..99a72482 100644 --- a/absl/container/BUILD.bazel +++ b/absl/container/BUILD.bazel @@ -198,11 +198,23 @@ cc_test( deps = [ ":inlined_vector", "//absl/base", + "//absl/base:core_headers", "//absl/strings", "@com_github_google_benchmark//:benchmark_main", ], ) +cc_test( + name = "inlined_vector_exception_safety_test", + srcs = ["inlined_vector_exception_safety_test.cc"], + copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG, + deps = [ + ":inlined_vector", + "//absl/base:exception_safety_testing", + "@com_google_googletest//:gtest_main", + ], +) + cc_library( name = "test_instance_tracker", testonly = 1, diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt index 1e203dbf..526e37af 100644 --- a/absl/container/CMakeLists.txt +++ b/absl/container/CMakeLists.txt @@ -195,6 +195,22 @@ absl_cc_test( gmock_main ) +absl_cc_test( + NAME + inlined_vector_exception_safety_test + SRCS + "inlined_vector_exception_safety_test.cc" + COPTS + ${ABSL_TEST_COPTS} + ${ABSL_EXCEPTIONS_FLAG} + LINKOPTS + ${ABSL_EXCEPTIONS_FLAG_LINKOPTS} + DEPS + absl::inlined_vector + absl::exception_safety_testing + gmock_main +) + absl_cc_library( NAME test_instance_tracker diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 16865272..61e0cfb4 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -784,16 +784,20 @@ class InlinedVector { // Destroys all elements in the inlined vector, sets the size of `0` and // deallocates the heap allocation if the inlined vector was allocated. void clear() noexcept { - size_type s = size(); - if (storage_.GetIsAllocated()) { - Destroy(storage_.GetAllocatedData(), storage_.GetAllocatedData() + s); - AllocatorTraits::deallocate(storage_.GetAllocator(), - storage_.GetAllocatedData(), + const bool is_allocated = storage_.GetIsAllocated(); + + pointer the_data = + is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData(); + + inlined_vector_internal::DestroyElements(storage_.GetAllocator(), the_data, + storage_.GetSize()); + + if (is_allocated) { + AllocatorTraits::deallocate(storage_.GetAllocator(), the_data, storage_.GetAllocatedCapacity()); - } else if (s != 0) { // do nothing for empty vectors - Destroy(storage_.GetInlinedData(), storage_.GetInlinedData() + s); } - storage_.SetInlinedSize(0); + + storage_.SetInlinedSize(/* size = */ 0); } // `InlinedVector::reserve()` diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc index 867a29ea..7bb3271b 100644 --- a/absl/container/inlined_vector_benchmark.cc +++ b/absl/container/inlined_vector_benchmark.cc @@ -1,4 +1,4 @@ -// Copyright 2017 The Abseil Authors. +// Copyright 2019 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "absl/container/inlined_vector.h" - #include #include #include "benchmark/benchmark.h" #include "absl/base/internal/raw_logging.h" +#include "absl/base/macros.h" +#include "absl/container/inlined_vector.h" #include "absl/strings/str_cat.h" namespace { @@ -373,4 +373,72 @@ void BM_StdVectorEmpty(benchmark::State& state) { } BENCHMARK(BM_StdVectorEmpty); +constexpr size_t kInlineElements = 4; +constexpr size_t kSmallSize = kInlineElements / 2; +constexpr size_t kLargeSize = kInlineElements * 2; +constexpr size_t kBatchSize = 100; + +struct TrivialType { + size_t val; +}; + +using TrivialVec = absl::InlinedVector; + +class NontrivialType { + public: + ABSL_ATTRIBUTE_NOINLINE NontrivialType() : val_() {} + + ABSL_ATTRIBUTE_NOINLINE NontrivialType(const NontrivialType& other) + : val_(other.val_) {} + + ABSL_ATTRIBUTE_NOINLINE NontrivialType& operator=( + const NontrivialType& other) { + val_ = other.val_; + return *this; + } + + ABSL_ATTRIBUTE_NOINLINE ~NontrivialType() noexcept {} + + private: + size_t val_; +}; + +using NontrivialVec = absl::InlinedVector; + +#define BENCHMARK_OPERATION(BM_Function) \ + BENCHMARK_TEMPLATE(BM_Function, TrivialVec, kSmallSize); \ + BENCHMARK_TEMPLATE(BM_Function, TrivialVec, kLargeSize); \ + BENCHMARK_TEMPLATE(BM_Function, NontrivialVec, kSmallSize); \ + BENCHMARK_TEMPLATE(BM_Function, NontrivialVec, kLargeSize) + +template +void BatchedBenchmark(benchmark::State& state, PrepareVec prepare_vec, + TestVec test_vec) { + VecT vectors[kBatchSize]; + + while (state.KeepRunningBatch(kBatchSize)) { + // Prepare batch + state.PauseTiming(); + for (auto& vec : vectors) { + prepare_vec(&vec); + } + benchmark::DoNotOptimize(vectors); + state.ResumeTiming(); + + // Test batch + for (auto& vec : vectors) { + test_vec(&vec); + } + } +} + +template +void BM_Clear(benchmark::State& state) { + BatchedBenchmark( + state, + /* prepare_vec = */ [](VecT* vec) { vec->resize(Size); }, + /* test_vec = */ [](VecT* vec) { vec->clear(); }); +} +BENCHMARK_OPERATION(BM_Clear); + } // namespace diff --git a/absl/container/inlined_vector_exception_safety_test.cc b/absl/container/inlined_vector_exception_safety_test.cc new file mode 100644 index 00000000..0af048b1 --- /dev/null +++ b/absl/container/inlined_vector_exception_safety_test.cc @@ -0,0 +1,55 @@ +// Copyright 2019 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gtest/gtest.h" +#include "absl/base/internal/exception_safety_testing.h" +#include "absl/container/inlined_vector.h" + +namespace { + +constexpr size_t kInlined = 4; +constexpr size_t kSmallSize = kInlined / 2; +constexpr size_t kLargeSize = kInlined * 2; + +using Thrower = testing::ThrowingValue<>; +using ThrowerAlloc = testing::ThrowingAllocator; + +template > +using InlVec = absl::InlinedVector; + +TEST(InlinedVector, DefaultConstructor) { + testing::TestThrowingCtor>(); + + testing::TestThrowingCtor>(); +} + +TEST(InlinedVector, AllocConstructor) { + auto alloc = std::allocator(); + testing::TestThrowingCtor>(alloc); + + auto throw_alloc = ThrowerAlloc(); + testing::TestThrowingCtor>(throw_alloc); +} + +TEST(InlinedVector, Clear) { + auto small_vec = InlVec<>(kSmallSize); + EXPECT_TRUE(testing::TestNothrowOp([&]() { small_vec.clear(); })); + + auto large_vec = InlVec<>(kLargeSize); + EXPECT_TRUE(testing::TestNothrowOp([&]() { large_vec.clear(); })); +} + +} // namespace diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 6a5a75be..4589ce08 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -16,6 +16,7 @@ #define ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_ #include +#include #include #include #include @@ -31,6 +32,33 @@ using IsAtLeastForwardIterator = std::is_convertible< typename std::iterator_traits::iterator_category, std::forward_iterator_tag>; +template +void DestroyElements(AllocatorType alloc, ValueType* destroy_first, + SizeType destroy_size) { + using AllocatorTraits = std::allocator_traits; + + // Destroys `destroy_size` elements from `destroy_first`. + // + // Destroys the range + // [`destroy_first`, `destroy_first + destroy_size`). + // + // NOTE: We assume destructors do not throw and thus make no attempt to roll + // back. + for (SizeType i = 0; i < destroy_size; ++i) { + AllocatorTraits::destroy(alloc, destroy_first + i); + } + +#ifndef NDEBUG + // Overwrite unused memory with `0xab` so we can catch uninitialized usage. + // + // Cast to `void*` to tell the compiler that we don't care that we might be + // scribbling on a vtable pointer. + void* memory = reinterpret_cast(destroy_first); + size_t memory_size = sizeof(ValueType) * destroy_size; + std::memset(memory, 0xab, memory_size); +#endif // NDEBUG +} + template class Storage { public: diff --git a/absl/debugging/BUILD.bazel b/absl/debugging/BUILD.bazel index 0854314e..e4aed5e4 100644 --- a/absl/debugging/BUILD.bazel +++ b/absl/debugging/BUILD.bazel @@ -244,6 +244,7 @@ cc_test( "//conditions:default": [], }), linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS, + tags = ["notsan"], deps = [ ":leak_check_api_enabled_for_testing", "//absl/base", @@ -256,6 +257,7 @@ cc_test( srcs = ["leak_check_test.cc"], copts = ["-UABSL_EXPECT_LEAK_SANITIZER"], linkopts = ABSL_DEFAULT_LINKOPTS, + tags = ["noasan"], deps = [ ":leak_check_api_disabled_for_testing", "//absl/base", # for raw_logging @@ -271,6 +273,7 @@ cc_test( name = "disabled_leak_check_test", srcs = ["leak_check_fail_test.cc"], linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS, + tags = ["notsan"], deps = [ ":leak_check_api_enabled_for_testing", ":leak_check_disable", diff --git a/absl/hash/BUILD.bazel b/absl/hash/BUILD.bazel index e1e6eae8..8c2daf70 100644 --- a/absl/hash/BUILD.bazel +++ b/absl/hash/BUILD.bazel @@ -35,10 +35,10 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + ":city", "//absl/base:core_headers", "//absl/base:endian", "//absl/container:fixed_array", - "//absl/hash:city", "//absl/meta:type_traits", "//absl/numeric:int128", "//absl/strings", diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index 2b194737..d6ce88da 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -404,7 +404,7 @@ cc_test( cc_test( name = "numbers_test", - size = "small", + size = "medium", srcs = [ "internal/numbers_test_common.h", "numbers_test.cc", @@ -628,7 +628,7 @@ cc_test( cc_test( name = "str_format_convert_test", - size = "small", + size = "medium", srcs = ["internal/str_format/convert_test.cc"], copts = ABSL_TEST_COPTS, visibility = ["//visibility:private"], diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index 539d9516..da3208e1 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -50,7 +50,7 @@ // * A `ParsedFormat` instance, which encapsulates a specific, pre-compiled // format string for a specific set of type(s), and which can be passed // between API boundaries. (The `FormatSpec` type should not be used -// directly.) +// directly except as an argument type for wrapper functions.) // // The `str_format` library provides the ability to output its format strings to // arbitrary sink types: @@ -157,10 +157,15 @@ class FormatCountCapture { // FormatSpec // // The `FormatSpec` type defines the makeup of a format string within the -// `str_format` library. You should not need to use or manipulate this type -// directly. A `FormatSpec` is a variadic class template that is evaluated at -// compile-time, according to the format string and arguments that are passed -// to it. +// `str_format` library. It is a variadic class template that is evaluated at +// compile-time, according to the format string and arguments that are passed to +// it. +// +// You should not need to manipulate this type directly. You should only name it +// if you are writing wrapper functions which accept format arguments that will +// be provided unmodified to functions in this library. Such a wrapper function +// might be a class method that provides format arguments and/or internally uses +// the result of formatting. // // For a `FormatSpec` to be valid at compile-time, it must be provided as // either: diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index d4cffa08..80830b36 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -13,7 +13,7 @@ namespace absl { namespace { using str_format_internal::FormatArgImpl; -class FormatEntryPointTest : public ::testing::Test { }; +using FormatEntryPointTest = ::testing::Test; TEST_F(FormatEntryPointTest, Format) { std::string sink; @@ -458,7 +458,7 @@ std::string SummarizeParsedFormat(const ParsedFormatBase& pc) { return out; } -class ParsedFormatTest : public testing::Test {}; +using ParsedFormatTest = ::testing::Test; TEST_F(ParsedFormatTest, SimpleChecked) { EXPECT_EQ("[ABC]{d:1$d}[DEF]", @@ -600,6 +600,24 @@ TEST_F(ParsedFormatTest, RegressionMixPositional) { EXPECT_FALSE((ExtendedParsedFormat::New("%1$d %o"))); } +using FormatWrapperTest = ::testing::Test; + +// Plain wrapper for StrFormat. +template +std::string WrappedFormat(const absl::FormatSpec& format, + const Args&... args) { + return StrFormat(format, args...); +} + +TEST_F(FormatWrapperTest, ConstexprStringFormat) { + EXPECT_EQ(WrappedFormat("%s there", "hello"), "hello there"); +} + +TEST_F(FormatWrapperTest, ParsedFormat) { + ParsedFormat<'s'> format("%s there"); + EXPECT_EQ(WrappedFormat(format, "hello"), "hello there"); +} + } // namespace } // namespace absl diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 37ffff3d..3e8033a0 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc @@ -154,7 +154,7 @@ static int Delay(int32_t c, DelayMode mode) { if (c < limit) { c++; // spin } else { - ABSL_TSAN_MUTEX_PRE_DIVERT(0, 0); + ABSL_TSAN_MUTEX_PRE_DIVERT(nullptr, 0); if (c == limit) { // yield once AbslInternalMutexYield(); c++; @@ -162,7 +162,7 @@ static int Delay(int32_t c, DelayMode mode) { absl::SleepFor(absl::Microseconds(10)); c = 0; } - ABSL_TSAN_MUTEX_POST_DIVERT(0, 0); + ABSL_TSAN_MUTEX_POST_DIVERT(nullptr, 0); } return (c); } @@ -2583,7 +2583,7 @@ void CondVar::Wakeup(PerThreadSynch *w) { } void CondVar::Signal() { - ABSL_TSAN_MUTEX_PRE_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0); intptr_t v; int c = 0; for (v = cv_.load(std::memory_order_relaxed); v != 0; @@ -2612,17 +2612,17 @@ void CondVar::Signal() { if ((v & kCvEvent) != 0) { PostSynchEvent(this, SYNCH_EV_SIGNAL); } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); return; } else { c = Delay(c, GENTLE); } } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); } void CondVar::SignalAll () { - ABSL_TSAN_MUTEX_PRE_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0); intptr_t v; int c = 0; for (v = cv_.load(std::memory_order_relaxed); v != 0; @@ -2649,13 +2649,13 @@ void CondVar::SignalAll () { if ((v & kCvEvent) != 0) { PostSynchEvent(this, SYNCH_EV_SIGNALALL); } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); return; } else { c = Delay(c, GENTLE); // try again after a delay } } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); } void ReleasableMutexLock::Release() { diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc index 10211229..2e10f098 100644 --- a/absl/synchronization/mutex_test.cc +++ b/absl/synchronization/mutex_test.cc @@ -815,7 +815,12 @@ TEST(Mutex, MutexReaderDecrementBug) NO_THREAD_SAFETY_ANALYSIS { // Test that we correctly handle the situation when a lock is // held and then destroyed (w/o unlocking). +#ifdef THREAD_SANITIZER +// TSAN reports errors when locked Mutexes are destroyed. +TEST(Mutex, DISABLED_LockedMutexDestructionBug) NO_THREAD_SAFETY_ANALYSIS { +#else TEST(Mutex, LockedMutexDestructionBug) NO_THREAD_SAFETY_ANALYSIS { +#endif for (int i = 0; i != 10; i++) { // Create, lock and destroy 10 locks. const int kNumLocks = 10; @@ -1062,7 +1067,12 @@ class ScopedDisableBazelTestWarnings { const char ScopedDisableBazelTestWarnings::kVarName[] = "TEST_WARNINGS_OUTPUT_FILE"; +#ifdef THREAD_SANITIZER +// This test intentionally creates deadlocks to test the deadlock detector. +TEST(Mutex, DISABLED_DeadlockDetectorBazelWarning) { +#else TEST(Mutex, DeadlockDetectorBazelWarning) { +#endif absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kReport); // Cause deadlock detection to detect something, if it's @@ -1109,7 +1119,12 @@ TEST(Mutex, DeadlockDetectorStessTest) NO_THREAD_SAFETY_ANALYSIS { } } +#ifdef THREAD_SANITIZER +// TSAN reports errors when locked Mutexes are destroyed. +TEST(Mutex, DISABLED_DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS { +#else TEST(Mutex, DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS { +#endif // Test a scenario where a cached deadlock graph node id in the // list of held locks is not invalidated when the corresponding // mutex is deleted. diff --git a/absl/types/compare.h b/absl/types/compare.h index 7fed3081..50361d62 100644 --- a/absl/types/compare.h +++ b/absl/types/compare.h @@ -452,8 +452,10 @@ namespace compare_internal { // Helper functions to do a boolean comparison of two keys given a boolean // or three-way comparator. -constexpr bool compare_result_as_less_than(const bool r) { return r; } -constexpr bool compare_result_as_less_than(const int r) { return r < 0; } +// SFINAE prevents implicit conversions to bool (such as from int). +template ::value, int> = 0> +constexpr bool compare_result_as_less_than(const Bool r) { return r; } constexpr bool compare_result_as_less_than(const absl::weak_ordering r) { return r < 0; } diff --git a/absl/types/compare_test.cc b/absl/types/compare_test.cc index dd0388c1..3a855421 100644 --- a/absl/types/compare_test.cc +++ b/absl/types/compare_test.cc @@ -200,10 +200,6 @@ TEST(CompareResultAsLessThan, SanityTest) { EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(false)); EXPECT_TRUE(absl::compare_internal::compare_result_as_less_than(true)); - EXPECT_TRUE(absl::compare_internal::compare_result_as_less_than(-1)); - EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(0)); - EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(1)); - EXPECT_TRUE( absl::compare_internal::compare_result_as_less_than(weak_ordering::less)); EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than( diff --git a/ci/linux_clang-latest_libcxx_asan_bazel.sh b/ci/linux_clang-latest_libcxx_asan_bazel.sh new file mode 100755 index 00000000..113acdbe --- /dev/null +++ b/ci/linux_clang-latest_libcxx_asan_bazel.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# +# Copyright 2019 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script that can be invoked to test abseil-cpp in a hermetic environment +# using a Docker image on Linux. You must have Docker installed to use this +# script. + +set -euox pipefail + +if [ -z ${ABSEIL_ROOT:-} ]; then + ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" +fi + +if [ -z ${STD:-} ]; then + STD="c++11 c++14 c++17" +fi + +if [ -z ${COMPILATION_MODE:-} ]; then + COMPILATION_MODE="fastbuild opt" +fi + +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" + +# USE_BAZEL_CACHE=1 only works on Kokoro. +# Without access to the credentials this won't work. +if [ ${USE_BAZEL_CACHE:-0} -ne 0 ]; then + DOCKER_EXTRA_ARGS="--volume=${KOKORO_KEYSTORE_DIR}:/keystore:ro ${DOCKER_EXTRA_ARGS:-}" + # Bazel doesn't track changes to tools outside of the workspace + # (e.g. /usr/bin/gcc), so by appending the docker container to the + # remote_http_cache url, we make changes to the container part of + # the cache key. Hashing the key is to make it shorter and url-safe. + container_key=$(echo ${DOCKER_CONTAINER} | sha256sum | head -c 16) + BAZEL_EXTRA_ARGS="--remote_http_cache=https://storage.googleapis.com/absl-bazel-remote-cache/${container_key} --google_credentials=/keystore/73103_absl-bazel-remote-cache ${BAZEL_EXTRA_ARGS:-}" +fi + +for std in ${STD}; do + for compilation_mode in ${COMPILATION_MODE}; do + echo "--------------------------------------------------------------------" + echo "Testing with --compilation_mode=${compilation_mode} and --std=${std}" + + time docker run \ + --volume="${ABSEIL_ROOT}:/abseil-cpp:ro" \ + --workdir=/abseil-cpp \ + --cap-add=SYS_PTRACE \ + --rm \ + -e CC="/opt/llvm/clang/bin/clang" \ + -e BAZEL_COMPILER="llvm" \ + -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ + -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib" \ + -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \ + ${DOCKER_EXTRA_ARGS:-} \ + ${DOCKER_CONTAINER} \ + /usr/local/bin/bazel test ... \ + --compilation_mode=${compilation_mode} \ + --copt="-DDYNAMIC_ANNOTATIONS_ENABLED=1" \ + --copt="-DADDRESS_SANITIZER" \ + --copt="-fsanitize=address" \ + --copt=-Werror \ + --keep_going \ + --linkopt="-fsanitize=address" \ + --show_timestamps \ + --test_env="ASAN_SYMBOLIZER_PATH=/opt/llvm/clang/bin/llvm-symbolizer" \ + --test_env="TZDIR=/abseil-cpp/absl/time/internal/cctz/testdata/zoneinfo" \ + --test_output=errors \ + --test_tag_filters="-benchmark,-noasan" \ + ${BAZEL_EXTRA_ARGS:-} + done +done diff --git a/ci/linux_clang-latest_libcxx_tsan_bazel.sh b/ci/linux_clang-latest_libcxx_tsan_bazel.sh new file mode 100755 index 00000000..c4edd198 --- /dev/null +++ b/ci/linux_clang-latest_libcxx_tsan_bazel.sh @@ -0,0 +1,83 @@ +#!/bin/bash +# +# Copyright 2019 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script that can be invoked to test abseil-cpp in a hermetic environment +# using a Docker image on Linux. You must have Docker installed to use this +# script. + +set -euox pipefail + +if [ -z ${ABSEIL_ROOT:-} ]; then + ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" +fi + +if [ -z ${STD:-} ]; then + STD="c++11 c++14 c++17" +fi + +if [ -z ${COMPILATION_MODE:-} ]; then + COMPILATION_MODE="fastbuild opt" +fi + +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" + +# USE_BAZEL_CACHE=1 only works on Kokoro. +# Without access to the credentials this won't work. +if [ ${USE_BAZEL_CACHE:-0} -ne 0 ]; then + DOCKER_EXTRA_ARGS="--volume=${KOKORO_KEYSTORE_DIR}:/keystore:ro ${DOCKER_EXTRA_ARGS:-}" + # Bazel doesn't track changes to tools outside of the workspace + # (e.g. /usr/bin/gcc), so by appending the docker container to the + # remote_http_cache url, we make changes to the container part of + # the cache key. Hashing the key is to make it shorter and url-safe. + container_key=$(echo ${DOCKER_CONTAINER} | sha256sum | head -c 16) + BAZEL_EXTRA_ARGS="--remote_http_cache=https://storage.googleapis.com/absl-bazel-remote-cache/${container_key} --google_credentials=/keystore/73103_absl-bazel-remote-cache ${BAZEL_EXTRA_ARGS:-}" +fi + +for std in ${STD}; do + for compilation_mode in ${COMPILATION_MODE}; do + echo "--------------------------------------------------------------------" + echo "Testing with --compilation_mode=${compilation_mode} and --std=${std}" + + time docker run \ + --volume="${ABSEIL_ROOT}:/abseil-cpp:ro" \ + --workdir=/abseil-cpp \ + --cap-add=SYS_PTRACE \ + --rm \ + -e CC="/opt/llvm/clang/bin/clang" \ + -e BAZEL_COMPILER="llvm" \ + -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ + -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx-tsan/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx-tsan/lib" \ + -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx-tsan/include/c++/v1" \ + ${DOCKER_EXTRA_ARGS:-} \ + ${DOCKER_CONTAINER} \ + /usr/local/bin/bazel test ... \ + --build_tag_filters="-notsan" \ + --compilation_mode=${compilation_mode} \ + --copt="-DDYNAMIC_ANNOTATIONS_ENABLED=1" \ + --copt="-DTHREAD_SANITIZER" \ + --copt="-fsanitize=thread" \ + --copt=-Werror \ + --keep_going \ + --linkopt="-fsanitize=thread" \ + --show_timestamps \ + --test_env="TSAN_OPTIONS=report_atomic_races=0" \ + --test_env="TSAN_SYMBOLIZER_PATH=/opt/llvm/clang/bin/llvm-symbolizer" \ + --test_env="TZDIR=/abseil-cpp/absl/time/internal/cctz/testdata/zoneinfo" \ + --test_output=errors \ + --test_tag_filters="-benchmark,-notsan" \ + ${BAZEL_EXTRA_ARGS:-} + done +done -- cgit v1.2.3 From 2f76a9bf50046e396138cc8eeb3cdc17b7a5ac24 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 30 May 2019 08:42:34 -0700 Subject: Export of internal Abseil changes. -- 0925eb11f7730d5a1e538b9e6c2d1f8239f5fdc0 by Abseil Team : Import of CCTZ from GitHub. PiperOrigin-RevId: 250694613 -- 4e1690e492a8399da1b1450ff5f21adf435fcef5 by Greg Falcon : Import of CCTZ from GitHub. PiperOrigin-RevId: 250684222 -- c7281b44eb005c21d45bdc0b437e47c7787275bc by Derek Mauro : Fix warnings on Xcode/clang. PiperOrigin-RevId: 250552120 -- 5ea3b1cfa759a7e6c70e7558f27c762baf818f7d by CJ Johnson : Minor edits to InlinedVector benchmarks: Adds DoNotOptimize to the member functions of NontrivialType, removes unused template parameter for InlVec typeder PiperOrigin-RevId: 250505482 -- 7fe9c02b49121936c5b47034f20272a916111174 by Matt Kulukundis : Internal change. PiperOrigin-RevId: 250376825 -- ad348c9c0eb37449874648e8544c56343c1dfaef by CJ Johnson : Minor edits to InlinedVector benchmark PiperOrigin-RevId: 250361830 -- 55e8b411431e982059af73b160fa6bcef90e87f7 by CJ Johnson : Switches use of allocator_traits to the Abseil backport to leverage existing bug fixes and workarounds PiperOrigin-RevId: 250359321 -- b0edbe3218940128523e36388a31ff90df01a364 by CJ Johnson : Updates exception safety test for InlinedVector to use TYPED_TEST (with fixtures) which increases the coverage without complicating the code. PiperOrigin-RevId: 250359198 -- 8ab55c9a8c191aabcb562cf1789f360eba0b1a81 by Abseil Team : Internal change. PiperOrigin-RevId: 250281509 -- dd8a67f4f9e5e8930457203c18205183b8306b5a by Abseil Team : Change the suggestions for the non-strict Next/PrevWeekday() calls. Previously we suggested `PrevWeekday(d, Weekday::thursday) + 7` to get the _following_ Thursday if d is not already a Thursday, but `NextWeekday(d - 1, Weekday::thursday)` is more intuitive, and probably even a little faster. Similarly for the _previous_ Thursday if d is not already a Thursday, suggest `PrevWeekday(d + 1, Weekday::thursday)` instead of `NextWeekday(d, Weekday::thursday) - 7`. PiperOrigin-RevId: 249840844 -- 4775dce26cdb0560011a5d1ecdc8e0c20f856911 by Abseil Team : Change the DbgHelp lib pragma to use lowercase and an extension. This matches the conventions used in the Windows SDK. PiperOrigin-RevId: 249826229 -- cbc49e865f3715ebe1983307d4f02e50998b2958 by Abseil Team : Introduce Abseil prefixed thread annotation macros. PiperOrigin-RevId: 249825200 -- 8e97c8dfbadb78743c2421b494398be25f632cb1 by Abseil Team : Internal change. PiperOrigin-RevId: 249737936 -- 0d720538bc6af3e77ac19def27a4a0514c2800d2 by Abseil Team : Tell CMake Abseil is a C++ project. PiperOrigin-RevId: 249726509 -- 20651845edf236757b3ffb3eaeff330af81be40a by Abseil Team : Internal change. PiperOrigin-RevId: 249726377 GitOrigin-RevId: 0925eb11f7730d5a1e538b9e6c2d1f8239f5fdc0 Change-Id: I05d18d30ad4e9ace6b60a17d2dc6fd699643fe30 --- CMake/install_test_project/CMakeLists.txt | 2 +- CMakeLists.txt | 2 +- absl/base/BUILD.bazel | 4 +- absl/base/CMakeLists.txt | 1 + absl/base/internal/thread_annotations.h | 271 +++++++++++++++++++++ absl/base/thread_annotations.h | 178 +++++++------- absl/container/BUILD.bazel | 1 + absl/container/CMakeLists.txt | 1 + absl/container/inlined_vector.h | 6 +- absl/container/inlined_vector_benchmark.cc | 59 +++-- .../inlined_vector_exception_safety_test.cc | 87 +++++-- absl/container/internal/hashtablez_sampler_test.cc | 2 +- absl/container/internal/inlined_vector.h | 13 +- absl/debugging/symbolize_win32.inc | 2 +- absl/strings/str_format_test.cc | 2 +- absl/time/civil_time.h | 4 +- absl/time/civil_time_test.cc | 2 +- absl/types/BUILD.bazel | 2 +- 18 files changed, 486 insertions(+), 153 deletions(-) create mode 100644 absl/base/internal/thread_annotations.h (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/install_test_project/CMakeLists.txt b/CMake/install_test_project/CMakeLists.txt index b8e27dd1..06b797e9 100644 --- a/CMake/install_test_project/CMakeLists.txt +++ b/CMake/install_test_project/CMakeLists.txt @@ -16,7 +16,7 @@ # A simple CMakeLists.txt for testing cmake installation cmake_minimum_required(VERSION 3.5) -project(absl_cmake_testing) +project(absl_cmake_testing CXX) set(CMAKE_CXX_STANDARD 11) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7587f72..86f56344 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ cmake_policy(SET CMP0057 NEW) # Project version variables are the empty std::string if version is unspecified cmake_policy(SET CMP0048 NEW) -project(absl) +project(absl CXX) # when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp)) # in the source tree of a project that uses it, install rules are disabled. diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index c6f0e4d6..5b7b295a 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -69,6 +69,9 @@ cc_library( cc_library( name = "core_headers", + srcs = [ + "internal/thread_annotations.h", + ], hdrs = [ "attributes.h", "const_init.h", @@ -385,7 +388,6 @@ cc_test( srcs = ["internal/endian_test.cc"], copts = ABSL_TEST_COPTS, deps = [ - ":base", ":config", ":endian", "@com_google_googletest//:gtest_main", diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index b9f35bc0..34bfba27 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt @@ -67,6 +67,7 @@ absl_cc_library( "optimization.h" "port.h" "thread_annotations.h" + "internal/thread_annotations.h" COPTS ${ABSL_DEFAULT_COPTS} DEPS diff --git a/absl/base/internal/thread_annotations.h b/absl/base/internal/thread_annotations.h new file mode 100644 index 00000000..4dab6a9c --- /dev/null +++ b/absl/base/internal/thread_annotations.h @@ -0,0 +1,271 @@ +// Copyright 2019 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// File: thread_annotations.h +// ----------------------------------------------------------------------------- +// +// WARNING: This is a backwards compatible header and it will be removed after +// the migration to prefixed thread annotations is finished; please include +// "absl/base/thread_annotations.h". +// +// This header file contains macro definitions for thread safety annotations +// that allow developers to document the locking policies of multi-threaded +// code. The annotations can also help program analysis tools to identify +// potential thread safety issues. +// +// These annotations are implemented using compiler attributes. Using the macros +// defined here instead of raw attributes allow for portability and future +// compatibility. +// +// When referring to mutexes in the arguments of the attributes, you should +// use variable names or more complex expressions (e.g. my_object->mutex_) +// that evaluate to a concrete mutex object whenever possible. If the mutex +// you want to refer to is not in scope, you may use a member pointer +// (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object. + +#ifndef ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_ +#define ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_ + +#if defined(__clang__) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op +#endif + +// GUARDED_BY() +// +// Documents if a shared field or global variable needs to be protected by a +// mutex. GUARDED_BY() allows the user to specify a particular mutex that +// should be held when accessing the annotated variable. +// +// Although this annotation (and PT_GUARDED_BY, below) cannot be applied to +// local variables, a local variable and its associated mutex can often be +// combined into a small class or struct, thereby allowing the annotation. +// +// Example: +// +// class Foo { +// Mutex mu_; +// int p1_ GUARDED_BY(mu_); +// ... +// }; +#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) + +// PT_GUARDED_BY() +// +// Documents if the memory location pointed to by a pointer should be guarded +// by a mutex when dereferencing the pointer. +// +// Example: +// class Foo { +// Mutex mu_; +// int *p1_ PT_GUARDED_BY(mu_); +// ... +// }; +// +// Note that a pointer variable to a shared memory location could itself be a +// shared variable. +// +// Example: +// +// // `q_`, guarded by `mu1_`, points to a shared memory location that is +// // guarded by `mu2_`: +// int *q_ GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_); +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +// ACQUIRED_AFTER() / ACQUIRED_BEFORE() +// +// Documents the acquisition order between locks that can be held +// simultaneously by a thread. For any two locks that need to be annotated +// to establish an acquisition order, only one of them needs the annotation. +// (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER +// and ACQUIRED_BEFORE.) +// +// As with GUARDED_BY, this is only applicable to mutexes that are shared +// fields or global variables. +// +// Example: +// +// Mutex m1_; +// Mutex m2_ ACQUIRED_AFTER(m1_); +#define ACQUIRED_AFTER(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) + +#define ACQUIRED_BEFORE(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) + +// EXCLUSIVE_LOCKS_REQUIRED() / SHARED_LOCKS_REQUIRED() +// +// Documents a function that expects a mutex to be held prior to entry. +// The mutex is expected to be held both on entry to, and exit from, the +// function. +// +// An exclusive lock allows read-write access to the guarded data member(s), and +// only one thread can acquire a lock exclusively at any one time. A shared lock +// allows read-only access, and any number of threads can acquire a shared lock +// concurrently. +// +// Generally, non-const methods should be annotated with +// EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with +// SHARED_LOCKS_REQUIRED. +// +// Example: +// +// Mutex mu1, mu2; +// int a GUARDED_BY(mu1); +// int b GUARDED_BY(mu2); +// +// void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... } +// void bar() const SHARED_LOCKS_REQUIRED(mu1, mu2) { ... } +#define EXCLUSIVE_LOCKS_REQUIRED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) + +#define SHARED_LOCKS_REQUIRED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) + +// LOCKS_EXCLUDED() +// +// Documents the locks acquired in the body of the function. These locks +// cannot be held when calling this function (as Abseil's `Mutex` locks are +// non-reentrant). +#define LOCKS_EXCLUDED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +// LOCK_RETURNED() +// +// Documents a function that returns a mutex without acquiring it. For example, +// a public getter method that returns a pointer to a private mutex should +// be annotated with LOCK_RETURNED. +#define LOCK_RETURNED(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) + +// LOCKABLE +// +// Documents if a class/type is a lockable type (such as the `Mutex` class). +#define LOCKABLE \ + THREAD_ANNOTATION_ATTRIBUTE__(lockable) + +// SCOPED_LOCKABLE +// +// Documents if a class does RAII locking (such as the `MutexLock` class). +// The constructor should use `LOCK_FUNCTION()` to specify the mutex that is +// acquired, and the destructor should use `UNLOCK_FUNCTION()` with no +// arguments; the analysis will assume that the destructor unlocks whatever the +// constructor locked. +#define SCOPED_LOCKABLE \ + THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) + +// EXCLUSIVE_LOCK_FUNCTION() +// +// Documents functions that acquire a lock in the body of a function, and do +// not release it. +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) + +// SHARED_LOCK_FUNCTION() +// +// Documents functions that acquire a shared (reader) lock in the body of a +// function, and do not release it. +#define SHARED_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) + +// UNLOCK_FUNCTION() +// +// Documents functions that expect a lock to be held on entry to the function, +// and release it in the body of the function. +#define UNLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) + +// EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION() +// +// Documents functions that try to acquire a lock, and return success or failure +// (or a non-boolean value that can be interpreted as a boolean). +// The first argument should be `true` for functions that return `true` on +// success, or `false` for functions that return `false` on success. The second +// argument specifies the mutex that is locked on success. If unspecified, this +// mutex is assumed to be `this`. +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) + +#define SHARED_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) + +// ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK() +// +// Documents functions that dynamically check to see if a lock is held, and fail +// if it is not held. +#define ASSERT_EXCLUSIVE_LOCK(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__)) + +#define ASSERT_SHARED_LOCK(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__)) + +// NO_THREAD_SAFETY_ANALYSIS +// +// Turns off thread safety checking within the body of a particular function. +// This annotation is used to mark functions that are known to be correct, but +// the locking behavior is more complicated than the analyzer can handle. +#define NO_THREAD_SAFETY_ANALYSIS \ + THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) + +//------------------------------------------------------------------------------ +// Tool-Supplied Annotations +//------------------------------------------------------------------------------ + +// TS_UNCHECKED should be placed around lock expressions that are not valid +// C++ syntax, but which are present for documentation purposes. These +// annotations will be ignored by the analysis. +#define TS_UNCHECKED(x) "" + +// TS_FIXME is used to mark lock expressions that are not valid C++ syntax. +// It is used by automated tools to mark and disable invalid expressions. +// The annotation should either be fixed, or changed to TS_UNCHECKED. +#define TS_FIXME(x) "" + +// Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of +// a particular function. However, this attribute is used to mark functions +// that are incorrect and need to be fixed. It is used by automated tools to +// avoid breaking the build when the analysis is updated. +// Code owners are expected to eventually fix the routine. +#define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS + +// Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY +// annotation that needs to be fixed, because it is producing thread safety +// warning. It disables the GUARDED_BY. +#define GUARDED_BY_FIXME(x) + +// Disables warnings for a single read operation. This can be used to avoid +// warnings when it is known that the read is not actually involved in a race, +// but the compiler cannot confirm that. +#define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x) + + +namespace thread_safety_analysis { + +// Takes a reference to a guarded data member, and returns an unguarded +// reference. +template +inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS { + return v; +} + +template +inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS { + return v; +} + +} // namespace thread_safety_analysis + +#endif // ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_ diff --git a/absl/base/thread_annotations.h b/absl/base/thread_annotations.h index 0b2c306c..f98af9f9 100644 --- a/absl/base/thread_annotations.h +++ b/absl/base/thread_annotations.h @@ -34,19 +34,22 @@ #ifndef ABSL_BASE_THREAD_ANNOTATIONS_H_ #define ABSL_BASE_THREAD_ANNOTATIONS_H_ +// TODO(mbonadei): Remove after the backward compatibility period. +#include "absl/base/internal/thread_annotations.h" // IWYU pragma: export + #if defined(__clang__) -#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#define ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(x) __attribute__((x)) #else -#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op +#define ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(x) // no-op #endif -// GUARDED_BY() +// ABSL_GUARDED_BY() // // Documents if a shared field or global variable needs to be protected by a -// mutex. GUARDED_BY() allows the user to specify a particular mutex that +// mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that // should be held when accessing the annotated variable. // -// Although this annotation (and PT_GUARDED_BY, below) cannot be applied to +// Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to // local variables, a local variable and its associated mutex can often be // combined into a small class or struct, thereby allowing the annotation. // @@ -54,12 +57,13 @@ // // class Foo { // Mutex mu_; -// int p1_ GUARDED_BY(mu_); +// int p1_ ABSL_GUARDED_BY(mu_); // ... // }; -#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) +#define ABSL_GUARDED_BY(x) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(guarded_by(x)) -// PT_GUARDED_BY() +// ABSL_PT_GUARDED_BY() // // Documents if the memory location pointed to by a pointer should be guarded // by a mutex when dereferencing the pointer. @@ -67,7 +71,7 @@ // Example: // class Foo { // Mutex mu_; -// int *p1_ PT_GUARDED_BY(mu_); +// int *p1_ ABSL_PT_GUARDED_BY(mu_); // ... // }; // @@ -78,31 +82,32 @@ // // // `q_`, guarded by `mu1_`, points to a shared memory location that is // // guarded by `mu2_`: -// int *q_ GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_); -#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) +// int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_); +#define ABSL_PT_GUARDED_BY(x) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(pt_guarded_by(x)) -// ACQUIRED_AFTER() / ACQUIRED_BEFORE() +// ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE() // // Documents the acquisition order between locks that can be held // simultaneously by a thread. For any two locks that need to be annotated // to establish an acquisition order, only one of them needs the annotation. -// (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER -// and ACQUIRED_BEFORE.) +// (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER +// and ABSL_ACQUIRED_BEFORE.) // -// As with GUARDED_BY, this is only applicable to mutexes that are shared +// As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared // fields or global variables. // // Example: // // Mutex m1_; -// Mutex m2_ ACQUIRED_AFTER(m1_); -#define ACQUIRED_AFTER(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) +// Mutex m2_ ABSL_ACQUIRED_AFTER(m1_); +#define ABSL_ACQUIRED_AFTER(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(acquired_after(__VA_ARGS__)) -#define ACQUIRED_BEFORE(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) +#define ABSL_ACQUIRED_BEFORE(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(acquired_before(__VA_ARGS__)) -// EXCLUSIVE_LOCKS_REQUIRED() / SHARED_LOCKS_REQUIRED() +// ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED() // // Documents a function that expects a mutex to be held prior to entry. // The mutex is expected to be held both on entry to, and exit from, the @@ -114,77 +119,78 @@ // concurrently. // // Generally, non-const methods should be annotated with -// EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with -// SHARED_LOCKS_REQUIRED. +// ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with +// ABSL_SHARED_LOCKS_REQUIRED. // // Example: // // Mutex mu1, mu2; -// int a GUARDED_BY(mu1); -// int b GUARDED_BY(mu2); +// int a ABSL_GUARDED_BY(mu1); +// int b ABSL_GUARDED_BY(mu2); // -// void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... } -// void bar() const SHARED_LOCKS_REQUIRED(mu1, mu2) { ... } -#define EXCLUSIVE_LOCKS_REQUIRED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) +// void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... } +// void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... } +#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \ + exclusive_locks_required(__VA_ARGS__)) -#define SHARED_LOCKS_REQUIRED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) +#define ABSL_SHARED_LOCKS_REQUIRED(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(shared_locks_required(__VA_ARGS__)) -// LOCKS_EXCLUDED() +// ABSL_LOCKS_EXCLUDED() // // Documents the locks acquired in the body of the function. These locks // cannot be held when calling this function (as Abseil's `Mutex` locks are // non-reentrant). -#define LOCKS_EXCLUDED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) +#define ABSL_LOCKS_EXCLUDED(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(locks_excluded(__VA_ARGS__)) -// LOCK_RETURNED() +// ABSL_LOCK_RETURNED() // // Documents a function that returns a mutex without acquiring it. For example, // a public getter method that returns a pointer to a private mutex should -// be annotated with LOCK_RETURNED. -#define LOCK_RETURNED(x) \ - THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) +// be annotated with ABSL_LOCK_RETURNED. +#define ABSL_LOCK_RETURNED(x) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(lock_returned(x)) -// LOCKABLE +// ABSL_LOCKABLE // // Documents if a class/type is a lockable type (such as the `Mutex` class). -#define LOCKABLE \ - THREAD_ANNOTATION_ATTRIBUTE__(lockable) +#define ABSL_LOCKABLE ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(lockable) -// SCOPED_LOCKABLE +// ABSL_SCOPED_LOCKABLE // // Documents if a class does RAII locking (such as the `MutexLock` class). // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no // arguments; the analysis will assume that the destructor unlocks whatever the // constructor locked. -#define SCOPED_LOCKABLE \ - THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) +#define ABSL_SCOPED_LOCKABLE \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(scoped_lockable) -// EXCLUSIVE_LOCK_FUNCTION() +// ABSL_EXCLUSIVE_LOCK_FUNCTION() // // Documents functions that acquire a lock in the body of a function, and do // not release it. -#define EXCLUSIVE_LOCK_FUNCTION(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) +#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \ + exclusive_lock_function(__VA_ARGS__)) -// SHARED_LOCK_FUNCTION() +// ABSL_SHARED_LOCK_FUNCTION() // // Documents functions that acquire a shared (reader) lock in the body of a // function, and do not release it. -#define SHARED_LOCK_FUNCTION(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) +#define ABSL_SHARED_LOCK_FUNCTION(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(shared_lock_function(__VA_ARGS__)) -// UNLOCK_FUNCTION() +// ABSL_UNLOCK_FUNCTION() // // Documents functions that expect a lock to be held on entry to the function, // and release it in the body of the function. -#define UNLOCK_FUNCTION(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) +#define ABSL_UNLOCK_FUNCTION(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(unlock_function(__VA_ARGS__)) -// EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION() +// ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION() // // Documents functions that try to acquire a lock, and return success or failure // (or a non-boolean value that can be interpreted as a boolean). @@ -192,76 +198,80 @@ // success, or `false` for functions that return `false` on success. The second // argument specifies the mutex that is locked on success. If unspecified, this // mutex is assumed to be `this`. -#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) +#define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \ + exclusive_trylock_function(__VA_ARGS__)) -#define SHARED_TRYLOCK_FUNCTION(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) +#define ABSL_SHARED_TRYLOCK_FUNCTION(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE( \ + shared_trylock_function(__VA_ARGS__)) -// ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK() +// ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK() // // Documents functions that dynamically check to see if a lock is held, and fail // if it is not held. -#define ASSERT_EXCLUSIVE_LOCK(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__)) +#define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(assert_exclusive_lock(__VA_ARGS__)) -#define ASSERT_SHARED_LOCK(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__)) +#define ABSL_ASSERT_SHARED_LOCK(...) \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(assert_shared_lock(__VA_ARGS__)) -// NO_THREAD_SAFETY_ANALYSIS +// ABSL_NO_THREAD_SAFETY_ANALYSIS // // Turns off thread safety checking within the body of a particular function. // This annotation is used to mark functions that are known to be correct, but // the locking behavior is more complicated than the analyzer can handle. -#define NO_THREAD_SAFETY_ANALYSIS \ - THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) +#define ABSL_NO_THREAD_SAFETY_ANALYSIS \ + ABSL_INTERNAL_THREAD_ANNOTATION_ATTRIBUTE(no_thread_safety_analysis) //------------------------------------------------------------------------------ // Tool-Supplied Annotations //------------------------------------------------------------------------------ -// TS_UNCHECKED should be placed around lock expressions that are not valid +// ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid // C++ syntax, but which are present for documentation purposes. These // annotations will be ignored by the analysis. -#define TS_UNCHECKED(x) "" +#define ABSL_TS_UNCHECKED(x) "" -// TS_FIXME is used to mark lock expressions that are not valid C++ syntax. +// ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax. // It is used by automated tools to mark and disable invalid expressions. -// The annotation should either be fixed, or changed to TS_UNCHECKED. -#define TS_FIXME(x) "" +// The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED. +#define ABSL_TS_FIXME(x) "" -// Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of -// a particular function. However, this attribute is used to mark functions +// Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body +// of a particular function. However, this attribute is used to mark functions // that are incorrect and need to be fixed. It is used by automated tools to // avoid breaking the build when the analysis is updated. // Code owners are expected to eventually fix the routine. -#define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS +#define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS -// Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY -// annotation that needs to be fixed, because it is producing thread safety -// warning. It disables the GUARDED_BY. -#define GUARDED_BY_FIXME(x) +// Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a +// ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing +// thread safety warning. It disables the ABSL_GUARDED_BY. +#define ABSL_GUARDED_BY_FIXME(x) // Disables warnings for a single read operation. This can be used to avoid // warnings when it is known that the read is not actually involved in a race, // but the compiler cannot confirm that. -#define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x) - +#define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x) -namespace thread_safety_analysis { +namespace absl { +namespace base_internal { // Takes a reference to a guarded data member, and returns an unguarded // reference. +// Do not used this function directly, use ABSL_TS_UNCHECKED_READ instead. template -inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS { +inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS { return v; } template -inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS { +inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS { return v; } -} // namespace thread_safety_analysis +} // namespace base_internal +} // namespace absl #endif // ABSL_BASE_THREAD_ANNOTATIONS_H_ diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel index 99a72482..331030ac 100644 --- a/absl/container/BUILD.bazel +++ b/absl/container/BUILD.bazel @@ -124,6 +124,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":compressed_tuple", + "//absl/memory", "//absl/meta:type_traits", ], ) diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt index 526e37af..c75b0a2b 100644 --- a/absl/container/CMakeLists.txt +++ b/absl/container/CMakeLists.txt @@ -124,6 +124,7 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} DEPS absl::compressed_tuple + absl::memory absl::type_traits PUBLIC ) diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 564c9535..c31c319c 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -890,7 +890,7 @@ class InlinedVector { template reference Construct(pointer p, Args&&... args) { - std::allocator_traits::construct( + absl::allocator_traits::construct( *storage_.GetAllocPtr(), p, std::forward(args)...); return *p; } @@ -908,8 +908,8 @@ class InlinedVector { // Destroy [`from`, `to`) in place. void Destroy(pointer from, pointer to) { for (pointer cur = from; cur != to; ++cur) { - std::allocator_traits::destroy(*storage_.GetAllocPtr(), - cur); + absl::allocator_traits::destroy(*storage_.GetAllocPtr(), + cur); } #if !defined(NDEBUG) // Overwrite unused memory with `0xab` so we can catch uninitialized usage. diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc index d906997a..b57fc1de 100644 --- a/absl/container/inlined_vector_benchmark.cc +++ b/absl/container/inlined_vector_benchmark.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include @@ -373,71 +374,75 @@ void BM_StdVectorEmpty(benchmark::State& state) { } BENCHMARK(BM_StdVectorEmpty); -constexpr size_t kInlineElements = 4; -constexpr size_t kSmallSize = kInlineElements / 2; -constexpr size_t kLargeSize = kInlineElements * 2; +constexpr size_t kInlinedCapacity = 4; +constexpr size_t kLargeSize = kInlinedCapacity * 2; +constexpr size_t kSmallSize = kInlinedCapacity / 2; constexpr size_t kBatchSize = 100; +template +using InlVec = absl::InlinedVector; + struct TrivialType { size_t val; }; -using TrivialVec = absl::InlinedVector; - class NontrivialType { public: - ABSL_ATTRIBUTE_NOINLINE NontrivialType() : val_() {} + ABSL_ATTRIBUTE_NOINLINE NontrivialType() : val_() { + benchmark::DoNotOptimize(*this); + } ABSL_ATTRIBUTE_NOINLINE NontrivialType(const NontrivialType& other) - : val_(other.val_) {} + : val_(other.val_) { + benchmark::DoNotOptimize(*this); + } ABSL_ATTRIBUTE_NOINLINE NontrivialType& operator=( const NontrivialType& other) { val_ = other.val_; + benchmark::DoNotOptimize(*this); return *this; } - ABSL_ATTRIBUTE_NOINLINE ~NontrivialType() noexcept {} + ABSL_ATTRIBUTE_NOINLINE ~NontrivialType() noexcept { + benchmark::DoNotOptimize(*this); + } private: size_t val_; }; -using NontrivialVec = absl::InlinedVector; - -template -void BatchedBenchmark(benchmark::State& state, PrepareVec prepare_vec, - TestVec test_vec) { - VecT vectors[kBatchSize]; +template +void BatchedBenchmark(benchmark::State& state, PrepareVecFn prepare_vec, + TestVecFn test_vec) { + std::array, kBatchSize> vector_batch{}; while (state.KeepRunningBatch(kBatchSize)) { // Prepare batch state.PauseTiming(); - for (auto& vec : vectors) { + for (auto& vec : vector_batch) { prepare_vec(&vec); } - benchmark::DoNotOptimize(vectors); + benchmark::DoNotOptimize(vector_batch); state.ResumeTiming(); // Test batch - for (auto& vec : vectors) { + for (auto& vec : vector_batch) { test_vec(&vec); } } } -template +template void BM_Clear(benchmark::State& state) { - BatchedBenchmark( + BatchedBenchmark( state, - /* prepare_vec = */ [](VecT* vec) { vec->resize(FromSize); }, - /* test_vec = */ [](VecT* vec) { vec->clear(); }); + /* prepare_vec = */ [](InlVec* vec) { vec->resize(FromSize); }, + /* test_vec = */ [](InlVec* vec) { vec->clear(); }); } - -BENCHMARK_TEMPLATE(BM_Clear, TrivialVec, kSmallSize); -BENCHMARK_TEMPLATE(BM_Clear, TrivialVec, kLargeSize); - -BENCHMARK_TEMPLATE(BM_Clear, NontrivialVec, kSmallSize); -BENCHMARK_TEMPLATE(BM_Clear, NontrivialVec, kLargeSize); +BENCHMARK_TEMPLATE(BM_Clear, TrivialType, kLargeSize); +BENCHMARK_TEMPLATE(BM_Clear, TrivialType, kSmallSize); +BENCHMARK_TEMPLATE(BM_Clear, NontrivialType, kLargeSize); +BENCHMARK_TEMPLATE(BM_Clear, NontrivialType, kSmallSize); } // namespace diff --git a/absl/container/inlined_vector_exception_safety_test.cc b/absl/container/inlined_vector_exception_safety_test.cc index 0af048b1..1aae0b04 100644 --- a/absl/container/inlined_vector_exception_safety_test.cc +++ b/absl/container/inlined_vector_exception_safety_test.cc @@ -20,36 +20,85 @@ namespace { -constexpr size_t kInlined = 4; -constexpr size_t kSmallSize = kInlined / 2; -constexpr size_t kLargeSize = kInlined * 2; +constexpr size_t kInlinedCapacity = 4; +constexpr size_t kLargeSize = kInlinedCapacity * 2; +constexpr size_t kSmallSize = kInlinedCapacity / 2; using Thrower = testing::ThrowingValue<>; -using ThrowerAlloc = testing::ThrowingAllocator; +using MovableThrower = testing::ThrowingValue; +using ThrowAlloc = testing::ThrowingAllocator; -template > -using InlVec = absl::InlinedVector; +using ThrowerVec = absl::InlinedVector; +using MovableThrowerVec = absl::InlinedVector; -TEST(InlinedVector, DefaultConstructor) { - testing::TestThrowingCtor>(); +using ThrowAllocThrowerVec = + absl::InlinedVector; +using ThrowAllocMovableThrowerVec = + absl::InlinedVector; - testing::TestThrowingCtor>(); +template +class TestParams { + public: + using VecT = TheVecT; + constexpr static size_t GetSizeAt(size_t i) { return kSizes[1 + i]; } + + private: + constexpr static size_t kSizes[1 + sizeof...(TheSizes)] = {1, TheSizes...}; +}; + +using NoSizeTestParams = + ::testing::Types, TestParams, + TestParams, + TestParams>; + +using OneSizeTestParams = + ::testing::Types, + TestParams, + TestParams, + TestParams, + TestParams, + TestParams, + TestParams, + TestParams>; + +template +struct NoSizeTest : ::testing::Test {}; +TYPED_TEST_SUITE(NoSizeTest, NoSizeTestParams); + +template +struct OneSizeTest : ::testing::Test {}; +TYPED_TEST_SUITE(OneSizeTest, OneSizeTestParams); + +// Function that always returns false is correct, but refactoring is required +// for clarity. It's needed to express that, as a contract, certain operations +// should not throw at all. Execution of this function means an exception was +// thrown and thus the test should fail. +// TODO(johnsoncj): Add `testing::NoThrowGuarantee` to the framework +template +bool NoThrowGuarantee(VecT* /* vec */) { + return false; } -TEST(InlinedVector, AllocConstructor) { - auto alloc = std::allocator(); - testing::TestThrowingCtor>(alloc); +TYPED_TEST(NoSizeTest, DefaultConstructor) { + using VecT = typename TypeParam::VecT; + using allocator_type = typename VecT::allocator_type; - auto throw_alloc = ThrowerAlloc(); - testing::TestThrowingCtor>(throw_alloc); + testing::TestThrowingCtor(); + + testing::TestThrowingCtor(allocator_type{}); } -TEST(InlinedVector, Clear) { - auto small_vec = InlVec<>(kSmallSize); - EXPECT_TRUE(testing::TestNothrowOp([&]() { small_vec.clear(); })); +TYPED_TEST(OneSizeTest, Clear) { + using VecT = typename TypeParam::VecT; + constexpr static auto size = TypeParam::GetSizeAt(0); + + auto tester = testing::MakeExceptionSafetyTester() + .WithInitialValue(VecT(size)) + .WithContracts(NoThrowGuarantee); - auto large_vec = InlVec<>(kLargeSize); - EXPECT_TRUE(testing::TestNothrowOp([&]() { large_vec.clear(); })); + EXPECT_TRUE(tester.Test([](VecT* vec) { + vec->clear(); // + })); } } // namespace diff --git a/absl/container/internal/hashtablez_sampler_test.cc b/absl/container/internal/hashtablez_sampler_test.cc index d2435ed8..7f9e8dd2 100644 --- a/absl/container/internal/hashtablez_sampler_test.cc +++ b/absl/container/internal/hashtablez_sampler_test.cc @@ -199,7 +199,7 @@ TEST(HashtablezSamplerTest, Sample) { SetHashtablezSampleParameter(100); int64_t num_sampled = 0; int64_t total = 0; - double sample_rate; + double sample_rate = 0.0; for (int i = 0; i < 1000000; ++i) { HashtablezInfoHandle h = Sample(); ++total; diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index a2b3d24d..79533a41 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -22,6 +22,7 @@ #include #include "absl/container/internal/compressed_tuple.h" +#include "absl/memory/memory.h" #include "absl/meta/type_traits.h" namespace absl { @@ -35,15 +36,7 @@ using IsAtLeastForwardIterator = std::is_convertible< template void DestroyElements(AllocatorType* alloc_ptr, ValueType* destroy_first, SizeType destroy_size) { - using AllocatorTraits = std::allocator_traits; - - // Destroys `destroy_size` elements from `destroy_first`. - // - // Destroys the range - // [`destroy_first`, `destroy_first + destroy_size`). - // - // NOTE: We assume destructors do not throw and thus make no attempt to roll - // back. + using AllocatorTraits = absl::allocator_traits; for (SizeType i = 0; i < destroy_size; ++i) { AllocatorTraits::destroy(*alloc_ptr, destroy_first + i); } @@ -75,7 +68,7 @@ class Storage { using const_iterator = const_pointer; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - using AllocatorTraits = std::allocator_traits; + using AllocatorTraits = absl::allocator_traits; explicit Storage(const allocator_type& alloc) : metadata_(alloc, /* empty and inlined */ 0) {} diff --git a/absl/debugging/symbolize_win32.inc b/absl/debugging/symbolize_win32.inc index 7ed3bd3a..5a55f29d 100644 --- a/absl/debugging/symbolize_win32.inc +++ b/absl/debugging/symbolize_win32.inc @@ -23,7 +23,7 @@ #include #pragma warning(pop) -#pragma comment(lib, "DbgHelp") +#pragma comment(lib, "dbghelp.lib") #include #include diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 80830b36..96c62349 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -271,7 +271,7 @@ TEST_F(FormatEntryPointTest, FPrintFError) { EXPECT_EQ(errno, EBADF); } -#if __GLIBC__ +#ifdef __GLIBC__ TEST_F(FormatEntryPointTest, FprintfTooLarge) { std::FILE* f = std::fopen("/dev/null", "w"); int width = 2000000000; diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h index 2dfcbd27..6bb7eec7 100644 --- a/absl/time/civil_time.h +++ b/absl/time/civil_time.h @@ -407,9 +407,9 @@ inline Weekday GetWeekday(CivilDay cd) { // // absl::CivilDay d = ... // // Gets the following Thursday if d is not already Thursday -// absl::CivilDay thurs1 = absl::PrevWeekday(d, absl::Weekday::thursday) + 7; +// absl::CivilDay thurs1 = absl::NextWeekday(d - 1, absl::Weekday::thursday); // // Gets the previous Thursday if d is not already Thursday -// absl::CivilDay thurs2 = absl::NextWeekday(d, absl::Weekday::thursday) - 7; +// absl::CivilDay thurs2 = absl::PrevWeekday(d + 1, absl::Weekday::thursday); // inline CivilDay NextWeekday(CivilDay cd, Weekday wd) { return CivilDay(time_internal::cctz::next_weekday(cd, wd)); diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc index b8d57135..070f0d57 100644 --- a/absl/time/civil_time_test.cc +++ b/absl/time/civil_time_test.cc @@ -1028,7 +1028,7 @@ TEST(CivilTime, LeapYears) { TEST(CivilTime, FirstThursdayInMonth) { const absl::CivilDay nov1(2014, 11, 1); const absl::CivilDay thursday = - absl::PrevWeekday(nov1, absl::Weekday::thursday) + 7; + absl::NextWeekday(nov1 - 1, absl::Weekday::thursday); EXPECT_EQ("2014-11-06", absl::FormatCivilTime(thursday)); // Bonus: Date of Thanksgiving in the United States diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel index 134f9f22..66ecb044 100644 --- a/absl/types/BUILD.bazel +++ b/absl/types/BUILD.bazel @@ -18,9 +18,9 @@ load( "//absl:copts/configure_copts.bzl", "ABSL_DEFAULT_COPTS", "ABSL_DEFAULT_LINKOPTS", - "ABSL_TEST_COPTS", "ABSL_EXCEPTIONS_FLAG", "ABSL_EXCEPTIONS_FLAG_LINKOPTS", + "ABSL_TEST_COPTS", ) package(default_visibility = ["//visibility:public"]) -- cgit v1.2.3 From e6b050212c859fbaf67abac76105da10ec348274 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 2 Jul 2019 12:50:48 -0700 Subject: Export of internal Abseil changes. -- c2e2e2b21c3fe59b63279e7418c93c7289ee3e27 by Mark Barolak : Import of CCTZ from GitHub. PiperOrigin-RevId: 256220326 -- 3996b01f0c3eb60f72825b154dce8019b6215f1d by Derek Mauro : Add GCC 4.9 test script. This will become our new minumum version and GCC 4.8 will be removed soon. PiperOrigin-RevId: 256160891 -- 2c13aa44bc8e497ebae9abe8b8adf73c2152622d by Abseil Team : [Trivial] tweak flat_hash_map.h doc comment The comment is probably right both ways, but the lack of an "if" here seemed jarring to me. PiperOrigin-RevId: 256089069 -- 16bc03b9b30fbf08d7dc61025fa8ec4b57077be8 by Abseil Team : Fix symbolization for elf files whose SYMTAB section is stripped, but have a DYNSYM section. Previously, if we did not find a SYMTAB section, we would bail out of symbolization early, rather than checking the DYNSYM section. PiperOrigin-RevId: 256061954 -- 4c60ee329b1eeb0b0d10c4f76f282e5fbae2c5b2 by Derek Mauro : Update to LLVM r363242 and Bazel 0.27.0 PiperOrigin-RevId: 256024405 -- 18e1ba970d33f122026803d8ca90035b9088949d by Eric Fiselier : Disable variant tests that break with P0608R3 PiperOrigin-RevId: 255975764 -- 0a89858464977e86096b62476faa3b64eb94aa1d by Abseil Team : Internal change PiperOrigin-RevId: 255891019 -- 9b7424cac66f5407f0ed74ed288bf3099a4fa330 by CJ Johnson : Updates the implementation of InlinedVector::insert(...) to be exception safe and adds an exception safety tests for insert(...) PiperOrigin-RevId: 255881809 -- 1288f4ba3883c510d92b09437fb8b051c19aa241 by CJ Johnson : Updates the implementation of InlinedVector::insert(...) to be exception safe and adds an exception safety tests for insert(...) PiperOrigin-RevId: 255875277 -- 39c04f3621491eaff9e2eda619718d5b5f20fbd9 by Abseil Team : Use a typedef to allow building with NVCC Without this change NVCC fails to compile compressed_tuple.h. NVCC is relevant because TensorFlow uses NVCC on Ubuntu and inclues abseil. PiperOrigin-RevId: 255850176 -- e23f0309ccad69eb508ca02c9034cd4cdd740da0 by Abseil Team : Internal change PiperOrigin-RevId: 255787167 -- 054aafcebf595077054164f1da3703124ab209b4 by Abseil Team : Updates the ScopedAllocatorWorks test for InlinedVector to not rely on the byte count allocated by the standard library In doing so, removes LegacyNextCapacityFrom(...) impl function from InlinedVector Also applies clang-format to the test file PiperOrigin-RevId: 255760356 -- eb05fc9f78e3a163c93f1866e9fe9a8ad0d01622 by Abseil Team : Internal change PiperOrigin-RevId: 255706834 -- 97abb824417604c45d9fcbb3e4ff1aa3000836f2 by Jorg Brown : Enhance compatibility of abseil's strings package with nvcc. PiperOrigin-RevId: 255688500 -- efc5b9c221ee31e15d10b35d31c8f3ae6eddaa8c by Abseil Team : Follow CCTZ's lead and allow GetWeekday() and GetYearDay() to be called with any civil-time type. A CivilSecond, for example, has a weekday just as much as a CivilDay does. PiperOrigin-RevId: 255659840 -- a75acbe954457919d8c6c8f4c2339b543760b375 by Derek Mauro : Increase the timeout of randen_engine_test. It seems to timeout under TSAN often enough to justify the increase. PiperOrigin-RevId: 255628086 -- 160976ba47c7c6eb57af08e21f8eb640aa51e91b by Derek Mauro : Update CMake documentation Fixes https://github.com/abseil/abseil-cpp/issues/332 PiperOrigin-RevId: 255607313 GitOrigin-RevId: c2e2e2b21c3fe59b63279e7418c93c7289ee3e27 Change-Id: Iba4ac7ed23cbcdb22965b4958601f689be92cda4 --- CMake/README.md | 16 +-- absl/container/flat_hash_map.h | 2 +- absl/container/inlined_vector_test.cc | 135 ++++++++++++--------- absl/container/internal/compressed_tuple.h | 11 +- absl/container/internal/inlined_vector.h | 16 ++- absl/copts/GENERATED_AbseilCopts.cmake | 2 - absl/copts/GENERATED_copts.bzl | 2 - absl/copts/copts.py | 2 - absl/debugging/symbolize_elf.inc | 48 +++----- absl/random/internal/BUILD.bazel | 10 +- absl/strings/BUILD.bazel | 1 - absl/strings/CMakeLists.txt | 1 - absl/strings/internal/str_format/bind.h | 20 ++- absl/strings/str_format_test.cc | 17 +-- absl/time/civil_time.h | 12 +- absl/time/civil_time_test.cc | 12 ++ .../internal/cctz/include/cctz/civil_time_detail.h | 14 +-- absl/time/internal/cctz/src/time_zone_info.cc | 4 +- absl/time/time.cc | 5 +- absl/time/time_test.cc | 16 +-- absl/types/variant_test.cc | 12 +- ci/linux_clang-latest_libcxx_asan_bazel.sh | 2 +- ci/linux_clang-latest_libcxx_bazel.sh | 2 +- ci/linux_clang-latest_libcxx_tsan_bazel.sh | 2 +- ci/linux_clang-latest_libstdcxx_bazel.sh | 2 +- ci/linux_gcc-4.9_libstdcxx_bazel.sh | 75 ++++++++++++ ci/linux_gcc-latest_libstdcxx_bazel.sh | 2 +- 27 files changed, 284 insertions(+), 159 deletions(-) create mode 100755 ci/linux_gcc-4.9_libstdcxx_bazel.sh (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/README.md b/CMake/README.md index 02359d36..469dfef5 100644 --- a/CMake/README.md +++ b/CMake/README.md @@ -37,20 +37,12 @@ section of your executable or of your library.
Here is a short CMakeLists.txt example of a project file using Abseil. ```cmake -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.5) project(my_project) -set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ ${CMAKE_CXX_FLAGS}") - -if(MSVC) - # /wd4005 macro-redefinition - # /wd4068 unknown pragma - # /wd4244 conversion from 'type1' to 'type2' - # /wd4267 conversion from 'size_t' to 'type2' - # /wd4800 force value to bool 'true' or 'false' (performance warning) - add_compile_options(/wd4005 /wd4068 /wd4244 /wd4267 /wd4800) - add_definitions(/DNOMINMAX /DWIN32_LEAN_AND_MEAN=1 /D_CRT_SECURE_NO_WARNINGS) -endif() +# Pick the C++ standard to compile with. +# Abseil currently supports C++11, C++14, and C++17. +set(CMAKE_CXX_STANDARD 11) add_subdirectory(abseil-cpp) diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h index 00cc4dcf..0bc501b1 100644 --- a/absl/container/flat_hash_map.h +++ b/absl/container/flat_hash_map.h @@ -77,7 +77,7 @@ struct FlatHashMapPolicy; // NOTE: A `flat_hash_map` stores its value types directly inside its // implementation array to avoid memory indirection. Because a `flat_hash_map` // is designed to move data when rehashed, map values will not retain pointer -// stability. If you require pointer stability, or your values are large, +// stability. If you require pointer stability, or if your values are large, // consider using `absl::flat_hash_map>` instead. // If your types are not moveable or you require pointer stability for keys, // consider `absl::node_hash_map`. diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index 50315b83..60fe89b2 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -76,9 +76,12 @@ TYPED_TEST_SUITE_P(InstanceTest); // destroyed in the erase(begin, end) test. class RefCounted { public: - RefCounted(int value, int* count) : value_(value), count_(count) { Ref(); } + RefCounted(int value, int* count) : value_(value), count_(count) { + Ref(); + } - RefCounted(const RefCounted& v) : value_(v.value_), count_(v.count_) { + RefCounted(const RefCounted& v) + : value_(v.value_), count_(v.count_) { Ref(); } @@ -287,7 +290,7 @@ TEST(RefCountedVec, EraseBeginEnd) { } // Check that the elements at the end are preserved. - for (int i = erase_end; i < len; ++i) { + for (int i = erase_end; i< len; ++i) { EXPECT_EQ(1, counts[i]); } } @@ -549,10 +552,10 @@ TEST(IntVec, Resize) { static const int kResizeElem = 1000000; for (int k = 0; k < 10; k++) { // Enlarging resize - v.resize(len + k, kResizeElem); - EXPECT_EQ(len + k, v.size()); - EXPECT_LE(len + k, v.capacity()); - for (int i = 0; i < len + k; i++) { + v.resize(len+k, kResizeElem); + EXPECT_EQ(len+k, v.size()); + EXPECT_LE(len+k, v.capacity()); + for (int i = 0; i < len+k; i++) { if (i < len) { EXPECT_EQ(i, v[i]); } else { @@ -863,7 +866,7 @@ TYPED_TEST_P(InstanceTest, Swap) { auto min_len = std::min(l1, l2); auto max_len = std::max(l1, l2); for (int i = 0; i < l1; i++) a.push_back(Instance(i)); - for (int i = 0; i < l2; i++) b.push_back(Instance(100 + i)); + for (int i = 0; i < l2; i++) b.push_back(Instance(100+i)); EXPECT_EQ(tracker.instances(), l1 + l2); tracker.ResetCopiesMovesSwaps(); { @@ -931,7 +934,7 @@ TEST(IntVec, EqualAndNotEqual) { EXPECT_FALSE(a == b); EXPECT_TRUE(a != b); - b[i] = b[i] - 1; // Back to before + b[i] = b[i] - 1; // Back to before EXPECT_TRUE(a == b); EXPECT_FALSE(a != b); } @@ -998,7 +1001,7 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructors) { // reserve() must not increase the number of initialized objects SCOPED_TRACE("reserve"); - v.reserve(len + 1000); + v.reserve(len+1000); EXPECT_EQ(tracker.instances(), len); EXPECT_EQ(tracker.copies() + tracker.moves(), len); @@ -1244,8 +1247,9 @@ void InstanceCountElemAssignWithAllocationTest() { absl::InlinedVector v(original_contents.begin(), original_contents.end()); v.assign(3, Instance(123)); - EXPECT_THAT(v, AllOf(SizeIs(3), ElementsAre(ValueIs(123), ValueIs(123), - ValueIs(123)))); + EXPECT_THAT(v, + AllOf(SizeIs(3), + ElementsAre(ValueIs(123), ValueIs(123), ValueIs(123)))); EXPECT_LE(v.size(), v.capacity()); } } @@ -1524,8 +1528,8 @@ TYPED_TEST_P(InstanceTest, InitializerListAssign) { SCOPED_TRACE(original_size); absl::InlinedVector v(original_size, Instance(12345)); v.assign({Instance(3), Instance(4), Instance(5)}); - EXPECT_THAT( - v, AllOf(SizeIs(3), ElementsAre(ValueIs(3), ValueIs(4), ValueIs(5)))); + EXPECT_THAT(v, AllOf(SizeIs(3), + ElementsAre(ValueIs(3), ValueIs(4), ValueIs(5)))); EXPECT_LE(3, v.capacity()); } } @@ -1550,7 +1554,7 @@ TEST(DynamicVec, DynamicVecCompiles) { TEST(AllocatorSupportTest, Constructors) { using MyAlloc = CountingAllocator; using AllocVec = absl::InlinedVector; - const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7}; + const int ia[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; int64_t allocated = 0; MyAlloc alloc(&allocated); { AllocVec ABSL_ATTRIBUTE_UNUSED v; } @@ -1566,7 +1570,7 @@ TEST(AllocatorSupportTest, Constructors) { TEST(AllocatorSupportTest, CountAllocations) { using MyAlloc = CountingAllocator; using AllocVec = absl::InlinedVector; - const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7}; + const int ia[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; int64_t allocated = 0; MyAlloc alloc(&allocated); { @@ -1630,8 +1634,8 @@ TEST(AllocatorSupportTest, SwapBothAllocated) { int64_t allocated1 = 0; int64_t allocated2 = 0; { - const int ia1[] = {0, 1, 2, 3, 4, 5, 6, 7}; - const int ia2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + const int ia1[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + const int ia2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; MyAlloc a1(&allocated1); MyAlloc a2(&allocated2); AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1); @@ -1655,8 +1659,8 @@ TEST(AllocatorSupportTest, SwapOneAllocated) { int64_t allocated1 = 0; int64_t allocated2 = 0; { - const int ia1[] = {0, 1, 2, 3, 4, 5, 6, 7}; - const int ia2[] = {0, 1, 2, 3}; + const int ia1[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + const int ia2[] = { 0, 1, 2, 3 }; MyAlloc a1(&allocated1); MyAlloc a2(&allocated2); AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1); @@ -1677,42 +1681,65 @@ TEST(AllocatorSupportTest, SwapOneAllocated) { TEST(AllocatorSupportTest, ScopedAllocatorWorks) { using StdVector = std::vector>; - using Alloc = CountingAllocator; - using ScopedAlloc = std::scoped_allocator_adaptor; - using AllocVec = absl::InlinedVector; - - { - int64_t total_allocated_byte_count = 0; - - AllocVec inlined_case(ScopedAlloc(Alloc(+&total_allocated_byte_count))); - inlined_case.emplace_back(); - - int64_t absl_responsible_for_count = total_allocated_byte_count; - EXPECT_EQ(absl_responsible_for_count, 0); - - inlined_case[0].emplace_back(); - EXPECT_GT(total_allocated_byte_count, absl_responsible_for_count); - - inlined_case.clear(); - EXPECT_EQ(total_allocated_byte_count, 0); - } - - { - int64_t total_allocated_byte_count = 0; - - AllocVec allocated_case(ScopedAlloc(Alloc(+&total_allocated_byte_count))); - allocated_case.emplace_back(); - allocated_case.emplace_back(); - - int64_t absl_responsible_for_count = total_allocated_byte_count; - EXPECT_GT(absl_responsible_for_count, 0); + using MyAlloc = + std::scoped_allocator_adaptor>; + using AllocVec = absl::InlinedVector; + + // MSVC 2017's std::vector allocates different amounts of memory in debug + // versus opt mode. + int64_t test_allocated = 0; + StdVector v(CountingAllocator{&test_allocated}); + // The amount of memory allocated by a default constructed vector + auto default_std_vec_allocated = test_allocated; + v.push_back(1); + // The amound of memory allocated by a copy-constructed vector with one + // element. + int64_t one_element_std_vec_copy_allocated = test_allocated; - allocated_case[1].emplace_back(); - EXPECT_GT(total_allocated_byte_count, absl_responsible_for_count); + int64_t allocated = 0; + AllocVec vec(MyAlloc{CountingAllocator{&allocated}}); + EXPECT_EQ(allocated, 0); - allocated_case.clear(); - EXPECT_EQ(total_allocated_byte_count, 0); - } + // This default constructs a vector, but the allocator should pass itself + // into the vector, so check allocation compared to that. + // The absl::InlinedVector does not allocate any memory. + // The vector may allocate any memory. + auto expected = default_std_vec_allocated; + vec.resize(1); + EXPECT_EQ(allocated, expected); + + // We make vector allocate memory. + // It must go through the allocator even though we didn't construct the + // vector directly. This assumes that vec[0] doesn't need to grow its + // allocation. + expected += sizeof(int); + vec[0].push_back(1); + EXPECT_EQ(allocated, expected); + + // Another allocating vector. + expected += one_element_std_vec_copy_allocated; + vec.push_back(vec[0]); + EXPECT_EQ(allocated, expected); + + // Overflow the inlined memory. + // The absl::InlinedVector will now allocate. + expected += sizeof(StdVector) * 8 + default_std_vec_allocated * 3; + vec.resize(5); + EXPECT_EQ(allocated, expected); + + // Adding one more in external mode should also work. + expected += one_element_std_vec_copy_allocated; + vec.push_back(vec[0]); + EXPECT_EQ(allocated, expected); + + // And extending these should still work. This assumes that vec[0] does not + // need to grow its allocation. + expected += sizeof(int); + vec[0].push_back(1); + EXPECT_EQ(allocated, expected); + + vec.clear(); + EXPECT_EQ(allocated, 0); } TEST(AllocatorSupportTest, SizeAllocConstructor) { diff --git a/absl/container/internal/compressed_tuple.h b/absl/container/internal/compressed_tuple.h index 1713ad68..c29ab41e 100644 --- a/absl/container/internal/compressed_tuple.h +++ b/absl/container/internal/compressed_tuple.h @@ -188,6 +188,9 @@ class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple template using ElemT = internal_compressed_tuple::ElemT; + template + using StorageT = internal_compressed_tuple::Storage, I>; + public: constexpr CompressedTuple() = default; explicit constexpr CompressedTuple(Ts... base) @@ -200,19 +203,17 @@ class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple template constexpr const ElemT& get() const& { - return internal_compressed_tuple::Storage, I>::get(); + return StorageT::get(); } template ElemT&& get() && { - return std::move(*this) - .internal_compressed_tuple::template Storage, I>::get(); + return std::move(*this).StorageT::get(); } template constexpr const ElemT&& get() const&& { - return absl::move(*this) - .internal_compressed_tuple::template Storage, I>::get(); + return absl::move(*this).StorageT::get(); } }; diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 84b97791..0ab2d7da 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -402,6 +402,16 @@ class Storage { return current_capacity * 2; } + static size_type LegacyNextCapacityFrom(size_type current_capacity, + size_type requested_capacity) { + // TODO(johnsoncj): Get rid of this old behavior. + size_type new_capacity = current_capacity; + while (new_capacity < requested_capacity) { + new_capacity *= 2; + } + return new_capacity; + } + using Metadata = container_internal::CompressedTuple; @@ -512,7 +522,8 @@ auto Storage::Resize(ValueAdapter values, size_type new_size) -> void { absl::Span destroy_loop; if (new_size > storage_view.capacity) { - pointer new_data = allocation_tx.Allocate(new_size); + pointer new_data = allocation_tx.Allocate( + LegacyNextCapacityFrom(storage_view.capacity, new_size)); // Construct new objects in `new_data` construct_loop = {new_data + storage_view.size, @@ -632,7 +643,8 @@ auto Storage::Reserve(size_type requested_capacity) -> void { IteratorValueAdapter move_values( MoveIterator(storage_view.data)); - pointer new_data = allocation_tx.Allocate(requested_capacity); + pointer new_data = allocation_tx.Allocate( + LegacyNextCapacityFrom(storage_view.capacity, requested_capacity)); inlined_vector_internal::ConstructElements(GetAllocPtr(), new_data, &move_values, storage_view.size); diff --git a/absl/copts/GENERATED_AbseilCopts.cmake b/absl/copts/GENERATED_AbseilCopts.cmake index 88400e98..01bd40b2 100644 --- a/absl/copts/GENERATED_AbseilCopts.cmake +++ b/absl/copts/GENERATED_AbseilCopts.cmake @@ -77,7 +77,6 @@ list(APPEND ABSL_CLANG_CL_TEST_FLAGS "-Wno-unused-template" "-Wno-used-but-marked-unused" "-Wno-zero-as-null-pointer-constant" - "-Wno-gnu-include-next" "-Wno-gnu-zero-variadic-macro-arguments" ) @@ -181,7 +180,6 @@ list(APPEND ABSL_LLVM_TEST_FLAGS "-Wno-unused-template" "-Wno-used-but-marked-unused" "-Wno-zero-as-null-pointer-constant" - "-Wno-gnu-include-next" "-Wno-gnu-zero-variadic-macro-arguments" ) diff --git a/absl/copts/GENERATED_copts.bzl b/absl/copts/GENERATED_copts.bzl index d7edc936..82f332f4 100644 --- a/absl/copts/GENERATED_copts.bzl +++ b/absl/copts/GENERATED_copts.bzl @@ -78,7 +78,6 @@ ABSL_CLANG_CL_TEST_FLAGS = [ "-Wno-unused-template", "-Wno-used-but-marked-unused", "-Wno-zero-as-null-pointer-constant", - "-Wno-gnu-include-next", "-Wno-gnu-zero-variadic-macro-arguments", ] @@ -182,7 +181,6 @@ ABSL_LLVM_TEST_FLAGS = [ "-Wno-unused-template", "-Wno-used-but-marked-unused", "-Wno-zero-as-null-pointer-constant", - "-Wno-gnu-include-next", "-Wno-gnu-zero-variadic-macro-arguments", ] diff --git a/absl/copts/copts.py b/absl/copts/copts.py index d850bb4f..068abceb 100644 --- a/absl/copts/copts.py +++ b/absl/copts/copts.py @@ -105,8 +105,6 @@ LLVM_TEST_DISABLE_WARNINGS_FLAGS = [ "-Wno-unused-template", "-Wno-used-but-marked-unused", "-Wno-zero-as-null-pointer-constant", - # For a libc++ bug fixed in r357267 - "-Wno-gnu-include-next", # gtest depends on this GNU extension being offered. "-Wno-gnu-zero-variadic-macro-arguments", ] diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index 5ac7ff5d..e7305213 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc @@ -762,37 +762,27 @@ FindSymbolResult Symbolizer::GetSymbolFromObjectFile( } } - // Consult a regular symbol table first. - if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, - obj.elf_header.e_shoff, SHT_SYMTAB, &symtab, - tmp_buf, tmp_buf_size)) { - return SYMBOL_NOT_FOUND; - } - if (!ReadFromOffsetExact( - obj.fd, &strtab, sizeof(strtab), - obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { - return SYMBOL_NOT_FOUND; - } - const FindSymbolResult rc = - FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, - opd_ptr, tmp_buf, tmp_buf_size); - if (rc != SYMBOL_NOT_FOUND) { - return rc; // Found the symbol in a regular symbol table. + // Consult a regular symbol table, then fall back to the dynamic symbol table. + for (const auto symbol_table_type : {SHT_SYMTAB, SHT_DYNSYM}) { + if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, + obj.elf_header.e_shoff, symbol_table_type, + &symtab, tmp_buf, tmp_buf_size)) { + continue; + } + if (!ReadFromOffsetExact( + obj.fd, &strtab, sizeof(strtab), + obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { + continue; + } + const FindSymbolResult rc = + FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, + opd_ptr, tmp_buf, tmp_buf_size); + if (rc != SYMBOL_NOT_FOUND) { + return rc; + } } - // If the symbol is not found, then consult a dynamic symbol table. - if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, - obj.elf_header.e_shoff, SHT_DYNSYM, &symtab, - tmp_buf, tmp_buf_size)) { - return SYMBOL_NOT_FOUND; - } - if (!ReadFromOffsetExact( - obj.fd, &strtab, sizeof(strtab), - obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { - return SYMBOL_NOT_FOUND; - } - return FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, - opd_ptr, tmp_buf, tmp_buf_size); + return SYMBOL_NOT_FOUND; } namespace { diff --git a/absl/random/internal/BUILD.bazel b/absl/random/internal/BUILD.bazel index c9e4e880..fd5471a6 100644 --- a/absl/random/internal/BUILD.bazel +++ b/absl/random/internal/BUILD.bazel @@ -522,7 +522,7 @@ cc_test( cc_test( name = "randen_engine_test", - size = "small", + size = "medium", srcs = [ "randen_engine_test.cc", ], @@ -598,12 +598,12 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + ":distribution_impl", + ":fast_uniform_bits", + ":iostream_state_saver", + ":traits", "//absl/base:core_headers", "//absl/meta:type_traits", - "//absl/random/internal:distribution_impl", - "//absl/random/internal:fast_uniform_bits", - "//absl/random/internal:iostream_state_saver", - "//absl/random/internal:traits", ], ) diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index f18b1600..20511a35 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -559,7 +559,6 @@ cc_library( ":strings", "//absl/base:config", "//absl/base:core_headers", - "//absl/container:inlined_vector", "//absl/meta:type_traits", "//absl/numeric:int128", "//absl/types:span", diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt index f7821290..e63eec19 100644 --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -387,7 +387,6 @@ absl_cc_library( absl::strings absl::config absl::core_headers - absl::inlined_vector absl::type_traits absl::int128 absl::span diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index 4f782952..7df140a4 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -7,7 +7,6 @@ #include #include "absl/base/port.h" -#include "absl/container/inlined_vector.h" #include "absl/strings/internal/str_format/arg.h" #include "absl/strings/internal/str_format/checker.h" #include "absl/strings/internal/str_format/parser.h" @@ -138,7 +137,17 @@ class Streamable { public: Streamable(const UntypedFormatSpecImpl& format, absl::Span args) - : format_(format), args_(args.begin(), args.end()) {} + : format_(format) { + if (args.size() <= ABSL_ARRAYSIZE(few_args_)) { + for (size_t i = 0; i < args.size(); ++i) { + few_args_[i] = args[i]; + } + args_ = absl::MakeSpan(few_args_, args.size()); + } else { + many_args_.assign(args.begin(), args.end()); + args_ = many_args_; + } + } std::ostream& Print(std::ostream& os) const; @@ -148,7 +157,12 @@ class Streamable { private: const UntypedFormatSpecImpl& format_; - absl::InlinedVector args_; + absl::Span args_; + // if args_.size() is 4 or less: + FormatArgImpl few_args_[4] = {FormatArgImpl(0), FormatArgImpl(0), + FormatArgImpl(0), FormatArgImpl(0)}; + // if args_.size() is more than 4: + std::vector many_args_; }; // for testing diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 96c62349..cfd81bb3 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -30,8 +30,8 @@ TEST_F(FormatEntryPointTest, UntypedFormat) { "", "a", "%80d", -#if !defined(_MSC_VER) && !defined(__ANDROID__) - // MSVC and Android don't support positional syntax. +#if !defined(_MSC_VER) && !defined(__ANDROID__) && !defined(__native_client__) + // MSVC, NaCL and Android don't support positional syntax. "complicated multipart %% %1$d format %1$0999d", #endif // _MSC_VER }; @@ -153,17 +153,20 @@ TEST_F(FormatEntryPointTest, Stream) { "", "a", "%80d", -#if !defined(_MSC_VER) && !defined(__ANDROID__) - // MSVC doesn't support positional syntax. + "%d %u %c %s %f %g", +#if !defined(_MSC_VER) && !defined(__ANDROID__) && !defined(__native_client__) + // MSVC, NaCL and Android don't support positional syntax. "complicated multipart %% %1$d format %1$080d", #endif // _MSC_VER }; std::string buf(4096, '\0'); for (const auto& fmt : formats) { - const auto parsed = ParsedFormat<'d'>::NewAllowIgnored(fmt); + const auto parsed = + ParsedFormat<'d', 'u', 'c', 's', 'f', 'g'>::NewAllowIgnored(fmt); std::ostringstream oss; - oss << StreamFormat(*parsed, 123); - int fmt_result = snprintf(&*buf.begin(), buf.size(), fmt.c_str(), 123); + oss << StreamFormat(*parsed, 123, 3, 49, "multistreaming!!!", 1.01, 1.01); + int fmt_result = snprintf(&*buf.begin(), buf.size(), fmt.c_str(), // + 123, 3, 49, "multistreaming!!!", 1.01, 1.01); ASSERT_TRUE(oss) << fmt; ASSERT_TRUE(fmt_result >= 0 && static_cast(fmt_result) < buf.size()) << fmt_result; diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h index 6bb7eec7..beaf7d89 100644 --- a/absl/time/civil_time.h +++ b/absl/time/civil_time.h @@ -370,15 +370,15 @@ using Weekday = time_internal::cctz::weekday; // GetWeekday() // -// Returns the absl::Weekday for the given absl::CivilDay. +// Returns the absl::Weekday for the given (realigned) civil-time value. // // Example: // // absl::CivilDay a(2015, 8, 13); // absl::Weekday wd = absl::GetWeekday(a); // wd == absl::Weekday::thursday // -inline Weekday GetWeekday(CivilDay cd) { - return time_internal::cctz::get_weekday(cd); +inline Weekday GetWeekday(CivilSecond cs) { + return time_internal::cctz::get_weekday(cs); } // NextWeekday() @@ -420,7 +420,7 @@ inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) { // GetYearDay() // -// Returns the day-of-year for the given absl::CivilDay. +// Returns the day-of-year for the given (realigned) civil-time value. // // Example: // @@ -429,8 +429,8 @@ inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) { // absl::CivilDay b(2015, 12, 31); // int yd_dec_31 = absl::GetYearDay(b); // yd_dec_31 = 365 // -inline int GetYearDay(CivilDay cd) { - return time_internal::cctz::get_yearday(cd); +inline int GetYearDay(CivilSecond cs) { + return time_internal::cctz::get_yearday(cs); } // FormatCivilTime() diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc index 070f0d57..03cd1f12 100644 --- a/absl/time/civil_time_test.cc +++ b/absl/time/civil_time_test.cc @@ -616,6 +616,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(4, ss.hour()); EXPECT_EQ(5, ss.minute()); EXPECT_EQ(6, ss.second()); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(ss)); + EXPECT_EQ(34, absl::GetYearDay(ss)); absl::CivilMinute mm(2015, 2, 3, 4, 5, 6); EXPECT_EQ(2015, mm.year()); @@ -624,6 +626,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(4, mm.hour()); EXPECT_EQ(5, mm.minute()); EXPECT_EQ(0, mm.second()); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(mm)); + EXPECT_EQ(34, absl::GetYearDay(mm)); absl::CivilHour hh(2015, 2, 3, 4, 5, 6); EXPECT_EQ(2015, hh.year()); @@ -632,6 +636,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(4, hh.hour()); EXPECT_EQ(0, hh.minute()); EXPECT_EQ(0, hh.second()); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(hh)); + EXPECT_EQ(34, absl::GetYearDay(hh)); absl::CivilDay d(2015, 2, 3, 4, 5, 6); EXPECT_EQ(2015, d.year()); @@ -640,6 +646,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(0, d.hour()); EXPECT_EQ(0, d.minute()); EXPECT_EQ(0, d.second()); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(d)); + EXPECT_EQ(34, absl::GetYearDay(d)); absl::CivilMonth m(2015, 2, 3, 4, 5, 6); EXPECT_EQ(2015, m.year()); @@ -648,6 +656,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(0, m.hour()); EXPECT_EQ(0, m.minute()); EXPECT_EQ(0, m.second()); + EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(m)); + EXPECT_EQ(32, absl::GetYearDay(m)); absl::CivilYear y(2015, 2, 3, 4, 5, 6); EXPECT_EQ(2015, y.year()); @@ -656,6 +666,8 @@ TEST(CivilTime, Properties) { EXPECT_EQ(0, y.hour()); EXPECT_EQ(0, y.minute()); EXPECT_EQ(0, y.second()); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(y)); + EXPECT_EQ(1, absl::GetYearDay(y)); } TEST(CivilTime, Format) { diff --git a/absl/time/internal/cctz/include/cctz/civil_time_detail.h b/absl/time/internal/cctz/include/cctz/civil_time_detail.h index 0dcb1ad4..433078a7 100644 --- a/absl/time/internal/cctz/include/cctz/civil_time_detail.h +++ b/absl/time/internal/cctz/include/cctz/civil_time_detail.h @@ -535,8 +535,7 @@ enum class weekday { sunday, }; -template -CONSTEXPR_F weekday get_weekday(const civil_time& ct) noexcept { +CONSTEXPR_F weekday get_weekday(const civil_second& cs) noexcept { CONSTEXPR_D weekday k_weekday_by_mon_off[13] = { weekday::monday, weekday::tuesday, weekday::wednesday, weekday::thursday, weekday::friday, weekday::saturday, @@ -547,9 +546,9 @@ CONSTEXPR_F weekday get_weekday(const civil_time& ct) noexcept { CONSTEXPR_D int k_weekday_offsets[1 + 12] = { -1, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4, }; - year_t wd = 2400 + (ct.year() % 400) - (ct.month() < 3); + year_t wd = 2400 + (cs.year() % 400) - (cs.month() < 3); wd += wd / 4 - wd / 100 + wd / 400; - wd += k_weekday_offsets[ct.month()] + ct.day(); + wd += k_weekday_offsets[cs.month()] + cs.day(); return k_weekday_by_mon_off[wd % 7 + 6]; } @@ -595,13 +594,12 @@ CONSTEXPR_F civil_day prev_weekday(civil_day cd, weekday wd) noexcept { } } -template -CONSTEXPR_F int get_yearday(const civil_time& ct) noexcept { +CONSTEXPR_F int get_yearday(const civil_second& cs) noexcept { CONSTEXPR_D int k_month_offsets[1 + 12] = { -1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, }; - const int feb29 = (ct.month() > 2 && impl::is_leap_year(ct.year())); - return k_month_offsets[ct.month()] + feb29 + ct.day(); + const int feb29 = (cs.month() > 2 && impl::is_leap_year(cs.year())); + return k_month_offsets[cs.month()] + feb29 + cs.day(); } //////////////////////////////////////////////////////////////////////// diff --git a/absl/time/internal/cctz/src/time_zone_info.cc b/absl/time/internal/cctz/src/time_zone_info.cc index 184bd434..72bb3bde 100644 --- a/absl/time/internal/cctz/src/time_zone_info.cc +++ b/absl/time/internal/cctz/src/time_zone_info.cc @@ -505,9 +505,9 @@ bool TimeZoneInfo::Load(const std::string& name, ZoneInfoSource* zip) { if (tzh.tzh_version[0] != '\0') { // Snarf up the NL-enclosed future POSIX spec. Note // that version '3' files utilize an extended format. - auto get_char = [](ZoneInfoSource* zip) -> int { + auto get_char = [](ZoneInfoSource* azip) -> int { unsigned char ch; // all non-EOF results are positive - return (zip->Read(&ch, 1) == 1) ? ch : EOF; + return (azip->Read(&ch, 1) == 1) ? ch : EOF; }; if (get_char(zip) != '\n') return false; diff --git a/absl/time/time.cc b/absl/time/time.cc index 338c4523..6a387bce 100644 --- a/absl/time/time.cc +++ b/absl/time/time.cc @@ -457,8 +457,7 @@ struct tm ToTM(absl::Time t, absl::TimeZone tz) { tm.tm_year = static_cast(cs.year() - 1900); } - const CivilDay cd(cs); - switch (GetWeekday(cd)) { + switch (GetWeekday(cs)) { case Weekday::sunday: tm.tm_wday = 0; break; @@ -481,7 +480,7 @@ struct tm ToTM(absl::Time t, absl::TimeZone tz) { tm.tm_wday = 6; break; } - tm.tm_yday = GetYearDay(cd) - 1; + tm.tm_yday = GetYearDay(cs) - 1; tm.tm_isdst = ci.is_dst ? 1 : 0; return tm; diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc index e2b2f809..37af39d9 100644 --- a/absl/time/time_test.cc +++ b/absl/time/time_test.cc @@ -112,7 +112,7 @@ TEST(Time, UnixEpoch) { const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch()); EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs); EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); - EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(ci.cs)); } TEST(Time, Breakdown) { @@ -123,14 +123,14 @@ TEST(Time, Breakdown) { auto ci = tz.At(t); EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false); EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); - EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(ci.cs)); // Just before the epoch. t -= absl::Nanoseconds(1); ci = tz.At(t); EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false); EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond); - EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(ci.cs)); // Some time later. t += absl::Hours(24) * 2735; @@ -139,7 +139,7 @@ TEST(Time, Breakdown) { ci = tz.At(t); EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true); EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1)); - EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(ci.cs)); } TEST(Time, AdditiveOperators) { @@ -979,15 +979,15 @@ TEST(Time, ConversionSaturation) { EXPECT_CIVIL_INFO(ci, std::numeric_limits::max(), 12, 31, 23, 59, 59, 0, false); EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond); - EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); - EXPECT_EQ(365, absl::GetYearDay(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(ci.cs)); + EXPECT_EQ(365, absl::GetYearDay(ci.cs)); EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At() ci = utc.At(absl::InfinitePast()); EXPECT_CIVIL_INFO(ci, std::numeric_limits::min(), 1, 1, 0, 0, 0, 0, false); EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond); - EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(absl::CivilDay(ci.cs))); - EXPECT_EQ(1, absl::GetYearDay(absl::CivilDay(ci.cs))); + EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(ci.cs)); + EXPECT_EQ(1, absl::GetYearDay(ci.cs)); EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At() // Approach the maximal Time value from below. diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc index 2efaf21e..85201b3e 100644 --- a/absl/types/variant_test.cc +++ b/absl/types/variant_test.cc @@ -2112,7 +2112,8 @@ TEST(VariantTest, Hash) { // Miscellaneous and deprecated tests // //////////////////////////////////////// -// Test that a set requiring a basic type conversion works correctly. +// Test that a set requiring a basic type conversion works correctly +#if !defined(ABSL_HAVE_STD_VARIANT) TEST(VariantTest, TestConvertingSet) { typedef variant Variant; Variant v(1.0); @@ -2122,6 +2123,7 @@ TEST(VariantTest, TestConvertingSet) { ASSERT_TRUE(nullptr != absl::get_if(&v)); EXPECT_DOUBLE_EQ(2, absl::get(v)); } +#endif // ABSL_HAVE_STD_VARIANT // Test that a vector of variants behaves reasonably. TEST(VariantTest, Container) { @@ -2273,6 +2275,7 @@ struct Convertible2 { }; TEST(VariantTest, TestRvalueConversion) { +#if !defined(ABSL_HAVE_STD_VARIANT) variant var( ConvertVariantTo>( variant(0))); @@ -2305,6 +2308,7 @@ TEST(VariantTest, TestRvalueConversion) { variant2 = ConvertVariantTo>(variant(42)); ASSERT_TRUE(absl::holds_alternative(variant2)); EXPECT_EQ(42, absl::get(variant2)); +#endif // !ABSL_HAVE_STD_VARIANT variant variant3( ConvertVariantTo>( @@ -2317,6 +2321,7 @@ TEST(VariantTest, TestRvalueConversion) { } TEST(VariantTest, TestLvalueConversion) { +#if !defined(ABSL_HAVE_STD_VARIANT) variant source1 = 0; variant destination( ConvertVariantTo>(source1)); @@ -2353,6 +2358,7 @@ TEST(VariantTest, TestLvalueConversion) { variant2 = ConvertVariantTo>(source6); ASSERT_TRUE(absl::holds_alternative(variant2)); EXPECT_EQ(42, absl::get(variant2)); +#endif variant source7((Convertible1())); variant variant3( @@ -2417,6 +2423,7 @@ TEST(VariantTest, DoesNotMoveFromLvalues) { } TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) { +#if !defined(ABSL_HAVE_STD_VARIANT) variant var( ConvertVariantTo>( variant(3))); @@ -2442,6 +2449,7 @@ TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) { variant2 = ConvertVariantTo>(variant(42)); EXPECT_THAT(absl::get_if(&variant2), Pointee(42)); +#endif variant variant3( ConvertVariantTo>( @@ -2454,6 +2462,7 @@ TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) { } TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) { +#if !defined(ABSL_HAVE_STD_VARIANT) variant source1 = 3; variant destination( ConvertVariantTo>(source1)); @@ -2485,6 +2494,7 @@ TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) { variant source6(42); variant2 = ConvertVariantTo>(source6); EXPECT_THAT(absl::get_if(&variant2), Pointee(42)); +#endif // !ABSL_HAVE_STD_VARIANT variant source7((Convertible1())); variant variant3( diff --git a/ci/linux_clang-latest_libcxx_asan_bazel.sh b/ci/linux_clang-latest_libcxx_asan_bazel.sh index 113acdbe..4d1d28a9 100755 --- a/ci/linux_clang-latest_libcxx_asan_bazel.sh +++ b/ci/linux_clang-latest_libcxx_asan_bazel.sh @@ -32,7 +32,7 @@ if [ -z ${COMPILATION_MODE:-} ]; then COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190701" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work. diff --git a/ci/linux_clang-latest_libcxx_bazel.sh b/ci/linux_clang-latest_libcxx_bazel.sh index f32cbef7..1197369a 100755 --- a/ci/linux_clang-latest_libcxx_bazel.sh +++ b/ci/linux_clang-latest_libcxx_bazel.sh @@ -32,7 +32,7 @@ if [ -z ${COMPILATION_MODE:-} ]; then COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190701" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work. diff --git a/ci/linux_clang-latest_libcxx_tsan_bazel.sh b/ci/linux_clang-latest_libcxx_tsan_bazel.sh index c4edd198..04171df8 100755 --- a/ci/linux_clang-latest_libcxx_tsan_bazel.sh +++ b/ci/linux_clang-latest_libcxx_tsan_bazel.sh @@ -32,7 +32,7 @@ if [ -z ${COMPILATION_MODE:-} ]; then COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190701" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work. diff --git a/ci/linux_clang-latest_libstdcxx_bazel.sh b/ci/linux_clang-latest_libstdcxx_bazel.sh index 7bbecd6f..c2217a07 100755 --- a/ci/linux_clang-latest_libstdcxx_bazel.sh +++ b/ci/linux_clang-latest_libstdcxx_bazel.sh @@ -32,7 +32,7 @@ if [ -z ${COMPILATION_MODE:-} ]; then COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190701" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work. diff --git a/ci/linux_gcc-4.9_libstdcxx_bazel.sh b/ci/linux_gcc-4.9_libstdcxx_bazel.sh new file mode 100755 index 00000000..bdd3e468 --- /dev/null +++ b/ci/linux_gcc-4.9_libstdcxx_bazel.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# +# Copyright 2019 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script that can be invoked to test abseil-cpp in a hermetic environment +# using a Docker image on Linux. You must have Docker installed to use this +# script. + +set -euox pipefail + +if [ -z ${ABSEIL_ROOT:-} ]; then + ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" +fi + +if [ -z ${STD:-} ]; then + STD="c++11 c++14" +fi + +if [ -z ${COMPILATION_MODE:-} ]; then + COMPILATION_MODE="fastbuild opt" +fi + +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-4.9:20190702" + +# USE_BAZEL_CACHE=1 only works on Kokoro. +# Without access to the credentials this won't work. +if [ ${USE_BAZEL_CACHE:-0} -ne 0 ]; then + DOCKER_EXTRA_ARGS="--volume=${KOKORO_KEYSTORE_DIR}:/keystore:ro ${DOCKER_EXTRA_ARGS:-}" + # Bazel doesn't track changes to tools outside of the workspace + # (e.g. /usr/bin/gcc), so by appending the docker container to the + # remote_http_cache url, we make changes to the container part of + # the cache key. Hashing the key is to make it shorter and url-safe. + container_key=$(echo ${DOCKER_CONTAINER} | sha256sum | head -c 16) + BAZEL_EXTRA_ARGS="--remote_http_cache=https://storage.googleapis.com/absl-bazel-remote-cache/${container_key} --google_credentials=/keystore/73103_absl-bazel-remote-cache ${BAZEL_EXTRA_ARGS:-}" +fi + +for std in ${STD}; do + for compilation_mode in ${COMPILATION_MODE}; do + echo "--------------------------------------------------------------------" + echo "Testing with --compilation_mode=${compilation_mode} and --std=${std}" + + time docker run \ + --volume="${ABSEIL_ROOT}:/abseil-cpp:ro" \ + --workdir=/abseil-cpp \ + --cap-add=SYS_PTRACE \ + --rm \ + -e CC="/usr/bin/gcc-4.9" \ + -e BAZEL_CXXOPTS="-std=${std}" \ + ${DOCKER_EXTRA_ARGS:-} \ + ${DOCKER_CONTAINER} \ + /usr/local/bin/bazel test ... \ + --compilation_mode=${compilation_mode} \ + --copt=-Werror \ + --define="absl=1" \ + --keep_going \ + --show_timestamps \ + --test_env="GTEST_INSTALL_FAILURE_SIGNAL_HANDLER=1" \ + --test_env="TZDIR=/abseil-cpp/absl/time/internal/cctz/testdata/zoneinfo" \ + --test_output=errors \ + --test_tag_filters=-benchmark \ + ${BAZEL_EXTRA_ARGS:-} + done +done diff --git a/ci/linux_gcc-latest_libstdcxx_bazel.sh b/ci/linux_gcc-latest_libstdcxx_bazel.sh index 24469628..2ef81114 100755 --- a/ci/linux_gcc-latest_libstdcxx_bazel.sh +++ b/ci/linux_gcc-latest_libstdcxx_bazel.sh @@ -32,7 +32,7 @@ if [ -z ${COMPILATION_MODE:-} ]; then COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-latest:20190318" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-latest:20190701" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work. -- cgit v1.2.3 From 12bc53e0318d80569270a5b26ccbc62b52022b89 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 12 Dec 2019 10:36:03 -0800 Subject: Export of internal Abseil changes -- c99f979ad34f155fbeeea69b88bdc7458d89a21c by Derek Mauro : Remove a floating point division by zero test. This isn't testing behavior related to the library, and MSVC warns about it in opt mode. PiperOrigin-RevId: 285220804 -- 68b015491f0dbf1ab547994673281abd1f34cd4b by Gennadiy Rozental : This CL introduces following changes to the class FlagImpl: * We eliminate the CommandLineFlagLocks struct. Instead callback guard and callback function are combined into a single CallbackData struct, while primary data lock is stored separately. * CallbackData member of class FlagImpl is initially set to be nullptr and is only allocated and initialized when a flag's callback is being set. For most flags we do not pay for the extra space and extra absl::Mutex now. * Primary data guard is stored in data_guard_ data member. This is a properly aligned character buffer of necessary size. During initialization of the flag we construct absl::Mutex in this space using placement new call. * We now avoid extra value copy after successful attempt to parse value out of string. Instead we swap flag's current value with tentative value we just produced. PiperOrigin-RevId: 285132636 -- ed45d118fb818969eb13094cf7827c885dfc562c by Tom Manshreck : Change null-term* (and nul-term*) to NUL-term* in comments PiperOrigin-RevId: 285036610 -- 729619017944db895ce8d6d29c1995aa2e5628a5 by Derek Mauro : Use the Posix implementation of thread identity on MinGW. Some versions of MinGW suffer from thread_local bugs. PiperOrigin-RevId: 285022920 -- 39a25493503c76885bc3254c28f66a251c5b5bb0 by Greg Falcon : Implementation detail change. Add further ABSL_NAMESPACE_BEGIN and _END annotation macros to files in Abseil. PiperOrigin-RevId: 285012012 GitOrigin-RevId: c99f979ad34f155fbeeea69b88bdc7458d89a21c Change-Id: I4c85d3704e45d11a9ac50d562f39640a6adbedc1 --- absl/algorithm/BUILD.bazel | 1 + absl/algorithm/CMakeLists.txt | 2 + absl/algorithm/algorithm.h | 4 + absl/algorithm/container.h | 2 + absl/base/internal/raw_logging.h | 2 +- absl/base/internal/thread_identity.h | 2 +- absl/container/BUILD.bazel | 8 ++ absl/container/CMakeLists.txt | 8 ++ absl/container/btree_map.h | 2 + absl/container/btree_set.h | 2 + absl/container/btree_test.cc | 2 + absl/container/btree_test.h | 2 + absl/container/fixed_array.h | 2 + .../container/fixed_array_exception_safety_test.cc | 2 + absl/container/flat_hash_map.h | 2 + absl/container/flat_hash_map_test.cc | 2 + absl/container/flat_hash_set.h | 2 + absl/container/flat_hash_set_test.cc | 2 + absl/container/inlined_vector.h | 2 + absl/container/internal/btree.h | 2 + absl/container/internal/btree_container.h | 2 + absl/container/internal/common.h | 2 + absl/container/internal/compressed_tuple.h | 2 + absl/container/internal/compressed_tuple_test.cc | 2 + absl/container/internal/container_memory.h | 2 + absl/container/internal/container_memory_test.cc | 2 + absl/container/internal/counting_allocator.h | 4 + absl/container/internal/hash_function_defaults.h | 2 + .../internal/hash_function_defaults_test.cc | 4 + absl/container/internal/hash_generator_testing.cc | 2 + absl/container/internal/hash_generator_testing.h | 2 + absl/container/internal/hash_policy_testing.h | 2 + .../container/internal/hash_policy_testing_test.cc | 2 + absl/container/internal/hash_policy_traits.h | 2 + absl/container/internal/hash_policy_traits_test.cc | 2 + absl/container/internal/hashtable_debug.h | 2 + absl/container/internal/hashtable_debug_hooks.h | 4 + absl/container/internal/hashtablez_sampler.cc | 2 + absl/container/internal/hashtablez_sampler.h | 2 + .../hashtablez_sampler_force_weak_definition.cc | 2 + absl/container/internal/hashtablez_sampler_test.cc | 2 + absl/container/internal/inlined_vector.h | 2 + absl/container/internal/layout.h | 2 + absl/container/internal/layout_test.cc | 2 + absl/container/internal/node_hash_policy.h | 4 + absl/container/internal/node_hash_policy_test.cc | 2 + absl/container/internal/raw_hash_map.h | 2 + absl/container/internal/raw_hash_set.cc | 2 + absl/container/internal/raw_hash_set.h | 2 + .../internal/raw_hash_set_allocator_test.cc | 2 + absl/container/internal/raw_hash_set_test.cc | 2 + absl/container/internal/test_instance_tracker.cc | 2 + absl/container/internal/test_instance_tracker.h | 2 + absl/container/internal/tracked.h | 5 + .../internal/unordered_map_constructor_test.h | 2 + .../container/internal/unordered_map_lookup_test.h | 2 + .../internal/unordered_map_members_test.h | 2 + .../internal/unordered_map_modifiers_test.h | 2 + absl/container/internal/unordered_map_test.cc | 2 + .../internal/unordered_set_constructor_test.h | 2 + .../container/internal/unordered_set_lookup_test.h | 2 + .../internal/unordered_set_members_test.h | 2 + .../internal/unordered_set_modifiers_test.h | 2 + absl/container/internal/unordered_set_test.cc | 2 + absl/container/node_hash_map.h | 2 + absl/container/node_hash_map_test.cc | 2 + absl/container/node_hash_set.h | 2 + absl/container/node_hash_set_test.cc | 2 + absl/debugging/BUILD.bazel | 18 +++- absl/debugging/CMakeLists.txt | 6 ++ absl/debugging/failure_signal_handler.cc | 2 + absl/debugging/failure_signal_handler.h | 4 + absl/debugging/internal/address_is_readable.cc | 4 + absl/debugging/internal/address_is_readable.h | 4 + absl/debugging/internal/demangle.cc | 2 + absl/debugging/internal/demangle.h | 4 + absl/debugging/internal/demangle_test.cc | 2 + absl/debugging/internal/elf_mem_image.cc | 2 + absl/debugging/internal/elf_mem_image.h | 4 + absl/debugging/internal/examine_stack.cc | 2 + absl/debugging/internal/examine_stack.h | 4 + absl/debugging/internal/stack_consumption.cc | 2 + absl/debugging/internal/stack_consumption.h | 4 + absl/debugging/internal/stack_consumption_test.cc | 2 + absl/debugging/internal/stacktrace_aarch64-inl.inc | 2 + absl/debugging/internal/stacktrace_arm-inl.inc | 2 + absl/debugging/internal/stacktrace_generic-inl.inc | 2 + absl/debugging/internal/stacktrace_powerpc-inl.inc | 2 + .../internal/stacktrace_unimplemented-inl.inc | 2 + absl/debugging/internal/stacktrace_win32-inl.inc | 2 + absl/debugging/internal/stacktrace_x86-inl.inc | 2 + absl/debugging/internal/symbolize.h | 6 ++ absl/debugging/internal/vdso_support.cc | 2 + absl/debugging/internal/vdso_support.h | 2 + absl/debugging/leak_check.cc | 4 + absl/debugging/leak_check.h | 4 + absl/debugging/stacktrace.cc | 2 + absl/debugging/stacktrace.h | 4 + absl/debugging/symbolize.h | 2 + absl/debugging/symbolize_elf.inc | 2 + absl/debugging/symbolize_unimplemented.inc | 2 + absl/debugging/symbolize_win32.inc | 2 + absl/flags/declare.h | 2 + absl/flags/flag.cc | 2 + absl/flags/flag.h | 2 + absl/flags/flag_test_defs.cc | 2 + absl/flags/internal/commandlineflag.cc | 2 + absl/flags/internal/commandlineflag.h | 4 +- absl/flags/internal/flag.cc | 113 ++++++++++++++------- absl/flags/internal/flag.h | 54 +++++----- absl/flags/internal/parse.h | 2 + absl/flags/internal/path_util.h | 2 + absl/flags/internal/program_name.cc | 2 + absl/flags/internal/program_name.h | 2 + absl/flags/internal/registry.cc | 4 +- absl/flags/internal/registry.h | 2 + absl/flags/internal/type_erased.cc | 2 + absl/flags/internal/type_erased.h | 2 + absl/flags/internal/usage.cc | 2 + absl/flags/internal/usage.h | 2 + absl/flags/marshalling.cc | 2 + absl/flags/marshalling.h | 2 + absl/flags/parse.cc | 4 + absl/flags/parse.h | 2 + absl/flags/usage.cc | 2 + absl/flags/usage.h | 2 + absl/flags/usage_config.cc | 2 + absl/flags/usage_config.h | 2 + absl/functional/function_ref.h | 2 + absl/functional/function_ref_benchmark.cc | 2 + absl/functional/function_ref_test.cc | 2 + absl/functional/internal/function_ref.h | 2 + absl/hash/hash.h | 2 + absl/hash/hash_test.cc | 2 + absl/hash/hash_testing.h | 2 + absl/hash/internal/city.cc | 2 + absl/hash/internal/city.h | 5 + absl/hash/internal/city_test.cc | 2 + absl/hash/internal/hash.cc | 2 + absl/hash/internal/hash.h | 2 + absl/hash/internal/spy_hash_state.h | 2 + absl/memory/memory.h | 2 + absl/memory/memory_exception_safety_test.cc | 2 + absl/meta/type_traits.h | 2 + absl/numeric/int128.cc | 2 + absl/numeric/int128.h | 6 ++ absl/random/CMakeLists.txt | 14 +++ absl/random/bernoulli_distribution.h | 2 + absl/random/beta_distribution.h | 2 + absl/random/bit_gen_ref.h | 2 + absl/random/bit_gen_ref_test.cc | 2 + absl/random/discrete_distribution.cc | 2 + absl/random/discrete_distribution.h | 2 + absl/random/distribution_format_traits.h | 2 + absl/random/distributions.h | 2 + absl/random/exponential_distribution.h | 2 + absl/random/gaussian_distribution.cc | 2 + absl/random/gaussian_distribution.h | 2 + absl/random/internal/BUILD.bazel | 9 ++ absl/random/internal/chi_square.cc | 2 + absl/random/internal/chi_square.h | 4 + absl/random/internal/distribution_caller.h | 4 + absl/random/internal/distribution_test_util.cc | 2 + absl/random/internal/distribution_test_util.h | 2 + absl/random/internal/distributions.h | 2 + absl/random/internal/explicit_seed_seq.h | 4 + absl/random/internal/fast_uniform_bits.h | 4 + absl/random/internal/fast_uniform_bits_test.cc | 2 + absl/random/internal/fastmath.h | 2 + .../internal/gaussian_distribution_gentables.cc | 2 + absl/random/internal/generate_real.h | 2 + absl/random/internal/iostream_state_saver.h | 2 + absl/random/internal/mock_overload_set.h | 2 + absl/random/internal/mocking_bit_gen_base.h | 2 + absl/random/internal/nanobenchmark.cc | 2 + absl/random/internal/nanobenchmark.h | 4 + absl/random/internal/nanobenchmark_test.cc | 2 + absl/random/internal/nonsecure_base.h | 2 + absl/random/internal/pcg_engine.h | 2 + absl/random/internal/pool_urbg.cc | 2 + absl/random/internal/pool_urbg.h | 2 + absl/random/internal/randen.cc | 2 + absl/random/internal/randen.h | 2 + absl/random/internal/randen_detect.cc | 2 + absl/random/internal/randen_detect.h | 4 + absl/random/internal/randen_engine.h | 2 + absl/random/internal/randen_hwaes.cc | 4 + absl/random/internal/randen_hwaes.h | 4 + absl/random/internal/randen_slow.cc | 2 + absl/random/internal/randen_slow.h | 4 + absl/random/internal/randen_traits.h | 4 + absl/random/internal/salted_seed_seq.h | 2 + absl/random/internal/seed_material.cc | 2 + absl/random/internal/seed_material.h | 2 + absl/random/internal/sequence_urbg.h | 4 + absl/random/internal/traits.h | 2 + absl/random/internal/uniform_helper.h | 2 + absl/random/internal/wide_multiply.h | 2 + absl/random/log_uniform_int_distribution.h | 2 + absl/random/mock_distributions.h | 2 + absl/random/mocking_bit_gen.cc | 2 + absl/random/mocking_bit_gen.h | 2 + absl/random/poisson_distribution.h | 2 + absl/random/random.h | 2 + absl/random/seed_gen_exception.cc | 2 + absl/random/seed_gen_exception.h | 4 + absl/random/seed_sequences.cc | 2 + absl/random/seed_sequences.h | 2 + absl/random/uniform_int_distribution.h | 2 + absl/random/uniform_real_distribution.h | 2 + absl/random/zipf_distribution.h | 2 + absl/strings/BUILD.bazel | 5 + absl/strings/CMakeLists.txt | 6 ++ absl/strings/ascii.cc | 2 + absl/strings/ascii.h | 2 + absl/strings/charconv.cc | 2 + absl/strings/charconv.h | 4 + absl/strings/escaping.cc | 2 + absl/strings/escaping.h | 2 + absl/strings/internal/char_map.h | 2 + absl/strings/internal/charconv_bigint.cc | 2 + absl/strings/internal/charconv_bigint.h | 2 + absl/strings/internal/charconv_bigint_test.cc | 2 + absl/strings/internal/charconv_parse.cc | 2 + absl/strings/internal/charconv_parse.h | 3 + absl/strings/internal/escaping_test_common.h | 2 + absl/strings/internal/memutil.cc | 2 + absl/strings/internal/memutil.h | 2 + absl/strings/internal/numbers_test_common.h | 4 + absl/strings/internal/ostringstream.cc | 2 + absl/strings/internal/ostringstream.h | 2 + absl/strings/internal/pow10_helper.cc | 2 + absl/strings/internal/pow10_helper.h | 4 + absl/strings/internal/pow10_helper_test.cc | 2 + absl/strings/internal/resize_uninitialized.h | 2 + absl/strings/internal/stl_type_traits.h | 2 + absl/strings/internal/str_format/arg.cc | 2 + absl/strings/internal/str_format/arg.h | 2 + absl/strings/internal/str_format/arg_test.cc | 2 + absl/strings/internal/str_format/bind.cc | 2 + absl/strings/internal/str_format/bind.h | 2 + absl/strings/internal/str_format/bind_test.cc | 2 + absl/strings/internal/str_format/checker.h | 2 + absl/strings/internal/str_format/checker_test.cc | 2 + absl/strings/internal/str_format/convert_test.cc | 2 + absl/strings/internal/str_format/extension.cc | 2 + absl/strings/internal/str_format/extension.h | 2 + .../internal/str_format/float_conversion.cc | 2 + .../strings/internal/str_format/float_conversion.h | 2 + absl/strings/internal/str_format/output.cc | 2 + absl/strings/internal/str_format/output.h | 2 + absl/strings/internal/str_format/output_test.cc | 2 + absl/strings/internal/str_format/parser.cc | 2 + absl/strings/internal/str_format/parser.h | 2 + absl/strings/internal/str_format/parser_test.cc | 2 + absl/strings/internal/str_join_internal.h | 2 + absl/strings/internal/str_split_internal.h | 2 + absl/strings/internal/utf8.cc | 2 + absl/strings/internal/utf8.h | 4 + absl/strings/match.cc | 2 + absl/strings/match.h | 4 +- absl/strings/numbers.cc | 2 + absl/strings/numbers.h | 4 + absl/strings/str_cat.cc | 2 + absl/strings/str_cat.h | 2 + absl/strings/str_format.h | 2 + absl/strings/str_format_test.cc | 2 + absl/strings/str_join.h | 2 + absl/strings/str_replace.cc | 2 + absl/strings/str_replace.h | 2 + absl/strings/str_split.cc | 2 + absl/strings/str_split.h | 2 + absl/strings/string_view.cc | 2 + absl/strings/string_view.h | 20 ++-- absl/strings/strip.h | 2 + absl/strings/substitute.cc | 2 + absl/strings/substitute.h | 2 + absl/synchronization/BUILD.bazel | 1 + absl/synchronization/CMakeLists.txt | 1 + absl/synchronization/barrier.cc | 2 + absl/synchronization/barrier.h | 2 + absl/synchronization/blocking_counter.cc | 2 + absl/synchronization/blocking_counter.h | 2 + absl/synchronization/blocking_counter_test.cc | 2 + .../internal/create_thread_identity.cc | 2 + .../internal/create_thread_identity.h | 2 + absl/synchronization/internal/graphcycles.cc | 2 + absl/synchronization/internal/graphcycles.h | 4 + absl/synchronization/internal/graphcycles_test.cc | 2 + absl/synchronization/internal/kernel_timeout.h | 2 + absl/synchronization/internal/mutex_nonprod.cc | 2 + absl/synchronization/internal/mutex_nonprod.inc | 2 + absl/synchronization/internal/per_thread_sem.cc | 2 + absl/synchronization/internal/per_thread_sem.h | 2 + .../internal/per_thread_sem_test.cc | 2 + absl/synchronization/internal/thread_pool.h | 2 + absl/synchronization/internal/waiter.cc | 2 + absl/synchronization/internal/waiter.h | 2 + absl/synchronization/mutex.cc | 4 +- absl/synchronization/mutex.h | 4 +- absl/synchronization/notification.cc | 2 + absl/synchronization/notification.h | 2 + absl/synchronization/notification_test.cc | 2 + absl/time/civil_time.cc | 2 + absl/time/civil_time.h | 2 + absl/time/clock.cc | 8 ++ absl/time/clock.h | 2 + absl/time/duration.cc | 2 + absl/time/duration_test.cc | 5 - absl/time/format.cc | 2 + absl/time/internal/get_current_time_chrono.inc | 2 + absl/time/internal/get_current_time_posix.inc | 2 + absl/time/internal/test_util.cc | 4 + absl/time/internal/test_util.h | 2 + absl/time/time.cc | 2 + absl/time/time.h | 2 + absl/types/any.h | 4 + absl/types/bad_any_cast.cc | 2 + absl/types/bad_any_cast.h | 4 + absl/types/bad_optional_access.cc | 2 + absl/types/bad_optional_access.h | 4 + absl/types/bad_variant_access.cc | 2 + absl/types/bad_variant_access.h | 4 + absl/types/compare.h | 2 + absl/types/compare_test.cc | 2 + absl/types/internal/conformance_aliases.h | 2 + absl/types/internal/conformance_archetype.h | 2 + absl/types/internal/conformance_profile.h | 2 + absl/types/internal/optional.h | 2 + absl/types/internal/span.h | 2 + absl/types/internal/variant.h | 2 + absl/types/optional.h | 4 + absl/types/optional_exception_safety_test.cc | 2 + absl/types/span.h | 2 + absl/types/variant.h | 6 ++ absl/types/variant_benchmark.cc | 2 + absl/types/variant_exception_safety_test.cc | 2 + absl/types/variant_test.cc | 2 + absl/utility/utility.h | 2 + 339 files changed, 949 insertions(+), 83 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/algorithm/BUILD.bazel b/absl/algorithm/BUILD.bazel index 2ee8c09e..6a96420b 100644 --- a/absl/algorithm/BUILD.bazel +++ b/absl/algorithm/BUILD.bazel @@ -31,6 +31,7 @@ cc_library( hdrs = ["algorithm.h"], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = ["//absl/base:config"], ) cc_test( diff --git a/absl/algorithm/CMakeLists.txt b/absl/algorithm/CMakeLists.txt index 9fbe36f6..56cd0fb8 100644 --- a/absl/algorithm/CMakeLists.txt +++ b/absl/algorithm/CMakeLists.txt @@ -21,6 +21,8 @@ absl_cc_library( "algorithm.h" COPTS ${ABSL_DEFAULT_COPTS} + DEPS + absl::config PUBLIC ) diff --git a/absl/algorithm/algorithm.h b/absl/algorithm/algorithm.h index 771228a0..e9b47338 100644 --- a/absl/algorithm/algorithm.h +++ b/absl/algorithm/algorithm.h @@ -26,7 +26,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace algorithm_internal { @@ -150,6 +153,7 @@ ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator>()); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_ALGORITHM_ALGORITHM_H_ diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h index 5dae9fd6..d72532de 100644 --- a/absl/algorithm/container.h +++ b/absl/algorithm/container.h @@ -55,6 +55,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_algorithm_internal { // NOTE: it is important to defer to ADL lookup for building with C++ modules, @@ -1720,6 +1721,7 @@ OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first, output_first, std::forward(op)); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_ALGORITHM_CONTAINER_H_ diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h index 12145c48..cff45058 100644 --- a/absl/base/internal/raw_logging.h +++ b/absl/base/internal/raw_logging.h @@ -154,7 +154,7 @@ using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file, // // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro // was located. -// The null-terminated logged message lives in the buffer between 'buf_start' +// The NUL-terminated logged message lives in the buffer between 'buf_start' // and 'buf_end'. 'prefix_end' points to the first non-prefix character of the // buffer (as written by the LogPrefixHook.) using AbortHook = void (*)(const char* file, int line, const char* buf_start, diff --git a/absl/base/internal/thread_identity.h b/absl/base/internal/thread_identity.h index a1aa45ff..5dfd0715 100644 --- a/absl/base/internal/thread_identity.h +++ b/absl/base/internal/thread_identity.h @@ -209,7 +209,7 @@ void ClearCurrentThreadIdentity(); #error ABSL_THREAD_IDENTITY_MODE cannot be direcly set #elif defined(ABSL_FORCE_THREAD_IDENTITY_MODE) #define ABSL_THREAD_IDENTITY_MODE ABSL_FORCE_THREAD_IDENTITY_MODE -#elif defined(_WIN32) +#elif defined(_WIN32) && !defined(__MINGW32__) #define ABSL_THREAD_IDENTITY_MODE ABSL_THREAD_IDENTITY_MODE_USE_CPP11 #elif ABSL_PER_THREAD_TLS && defined(__GOOGLE_GRTE_VERSION__) && \ (__GOOGLE_GRTE_VERSION__ >= 20140228L) diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel index e60979b2..1f7abe07 100644 --- a/absl/container/BUILD.bazel +++ b/absl/container/BUILD.bazel @@ -141,6 +141,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, visibility = ["//visibility:private"], + deps = ["//absl/base:config"], ) cc_test( @@ -478,6 +479,9 @@ cc_library( hdrs = ["internal/hashtable_debug_hooks.h"], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + "//absl/base:config", + ], ) cc_library( @@ -521,6 +525,7 @@ cc_library( hdrs = ["internal/node_hash_policy.h"], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = ["//absl/base:config"], ) cc_test( @@ -662,6 +667,9 @@ cc_library( hdrs = ["internal/tracked.h"], copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + "//absl/base:config", + ], ) cc_library( diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt index aa33659b..a931f334 100644 --- a/absl/container/CMakeLists.txt +++ b/absl/container/CMakeLists.txt @@ -204,6 +204,8 @@ absl_cc_library( "internal/counting_allocator.h" COPTS ${ABSL_DEFAULT_COPTS} + DEPS + absl::config ) absl_cc_test( @@ -574,6 +576,8 @@ absl_cc_library( "internal/hashtable_debug_hooks.h" COPTS ${ABSL_DEFAULT_COPTS} + DEPS + absl::config PUBLIC ) @@ -593,6 +597,8 @@ absl_cc_library( "internal/node_hash_policy.h" COPTS ${ABSL_DEFAULT_COPTS} + DEPS + absl::config PUBLIC ) @@ -735,6 +741,8 @@ absl_cc_library( "internal/tracked.h" COPTS ${ABSL_TEST_COPTS} + DEPS + absl::config TESTONLY ) diff --git a/absl/container/btree_map.h b/absl/container/btree_map.h index 9f35f639..470e3197 100644 --- a/absl/container/btree_map.h +++ b/absl/container/btree_map.h @@ -51,6 +51,7 @@ #include "absl/container/internal/btree_container.h" // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN // absl::btree_map<> // @@ -700,6 +701,7 @@ void swap(btree_multimap &x, btree_multimap &y) { return x.swap(y); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_BTREE_MAP_H_ diff --git a/absl/container/btree_set.h b/absl/container/btree_set.h index 6e47b4aa..2a4d0ace 100644 --- a/absl/container/btree_set.h +++ b/absl/container/btree_set.h @@ -51,6 +51,7 @@ #include "absl/container/internal/btree_container.h" // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN // absl::btree_set<> // @@ -648,6 +649,7 @@ void swap(btree_multiset &x, btree_multiset &y) { return x.swap(y); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_BTREE_SET_H_ diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index ea73f032..f8aadd62 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -42,6 +42,7 @@ ABSL_FLAG(int, test_values, 10000, "The number of values to use for tests"); namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -2304,4 +2305,5 @@ TEST(Btree, EmptyTree) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/btree_test.h b/absl/container/btree_test.h index 5ecf43ce..218ba41d 100644 --- a/absl/container/btree_test.h +++ b/absl/container/btree_test.h @@ -28,6 +28,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // Like remove_const but propagates the removal through std::pair. @@ -148,6 +149,7 @@ std::vector GenerateValuesWithSeed(int n, int maxval, int seed) { } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_BTREE_TEST_H_ diff --git a/absl/container/fixed_array.h b/absl/container/fixed_array.h index 70e94ad5..a9ce99ba 100644 --- a/absl/container/fixed_array.h +++ b/absl/container/fixed_array.h @@ -50,6 +50,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN constexpr static auto kFixedArrayUseDefault = static_cast(-1); @@ -508,6 +509,7 @@ void FixedArray::NonEmptyInlinedStorage::AnnotateDestruct( #endif // ADDRESS_SANITIZER static_cast(n); // Mark used when not in asan mode } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_FIXED_ARRAY_H_ diff --git a/absl/container/fixed_array_exception_safety_test.cc b/absl/container/fixed_array_exception_safety_test.cc index 5ebeac05..a5bb009d 100644 --- a/absl/container/fixed_array_exception_safety_test.cc +++ b/absl/container/fixed_array_exception_safety_test.cc @@ -23,6 +23,7 @@ #include "absl/base/internal/exception_safety_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -195,6 +196,7 @@ TEST(FixedArrayExceptionSafety, FillWithAlloc) { } // namespace +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_EXCEPTIONS diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h index 283f2439..fb570cd4 100644 --- a/absl/container/flat_hash_map.h +++ b/absl/container/flat_hash_map.h @@ -42,6 +42,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template struct FlatHashMapPolicy; @@ -584,6 +585,7 @@ struct IsUnorderedContainer< } // namespace container_algorithm_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_FLAT_HASH_MAP_H_ diff --git a/absl/container/flat_hash_map_test.cc b/absl/container/flat_hash_map_test.cc index 02d1f879..dae8e003 100644 --- a/absl/container/flat_hash_map_test.cc +++ b/absl/container/flat_hash_map_test.cc @@ -24,6 +24,7 @@ #include "absl/types/any.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { using ::absl::container_internal::hash_internal::Enum; @@ -251,4 +252,5 @@ TEST(FlatHashMap, Any) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/flat_hash_set.h b/absl/container/flat_hash_set.h index 2a51c341..930107ea 100644 --- a/absl/container/flat_hash_set.h +++ b/absl/container/flat_hash_set.h @@ -40,6 +40,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template struct FlatHashSetPolicy; @@ -488,6 +489,7 @@ struct IsUnorderedContainer> } // namespace container_algorithm_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_FLAT_HASH_SET_H_ diff --git a/absl/container/flat_hash_set_test.cc b/absl/container/flat_hash_set_test.cc index b55be59b..6eacb1bb 100644 --- a/absl/container/flat_hash_set_test.cc +++ b/absl/container/flat_hash_set_test.cc @@ -25,6 +25,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -125,4 +126,5 @@ TEST(FlatHashSet, MergeExtractInsert) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index d5c67db5..2388d471 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -54,6 +54,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // InlinedVector // ----------------------------------------------------------------------------- @@ -841,6 +842,7 @@ H AbslHashValue(H h, const absl::InlinedVector& a) { return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INLINED_VECTOR_H_ diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 40217dd5..aef861dc 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -70,6 +70,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // A helper class that indicates if the Compare parameter is a key-compare-to @@ -2606,6 +2607,7 @@ int btree

::internal_verify( } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_BTREE_H_ diff --git a/absl/container/internal/btree_container.h b/absl/container/internal/btree_container.h index 774412d9..04795c2e 100644 --- a/absl/container/internal/btree_container.h +++ b/absl/container/internal/btree_container.h @@ -26,6 +26,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // A common base class for btree_set, btree_map, btree_multiset, and @@ -602,6 +603,7 @@ class btree_multimap_container : public btree_multiset_container { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_ diff --git a/absl/container/internal/common.h b/absl/container/internal/common.h index cc7633dc..853a5b21 100644 --- a/absl/container/internal/common.h +++ b/absl/container/internal/common.h @@ -22,6 +22,7 @@ #include "absl/types/optional.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -196,6 +197,7 @@ struct InsertReturnType { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_CONTAINER_H_ diff --git a/absl/container/internal/compressed_tuple.h b/absl/container/internal/compressed_tuple.h index 7d08e370..4bfe92fd 100644 --- a/absl/container/internal/compressed_tuple.h +++ b/absl/container/internal/compressed_tuple.h @@ -48,6 +48,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -256,6 +257,7 @@ template <> class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple<> {}; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC diff --git a/absl/container/internal/compressed_tuple_test.cc b/absl/container/internal/compressed_tuple_test.cc index 19af8f10..76bc9213 100644 --- a/absl/container/internal/compressed_tuple_test.cc +++ b/absl/container/internal/compressed_tuple_test.cc @@ -48,6 +48,7 @@ struct TwoValues { namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -408,4 +409,5 @@ TEST(CompressedTupleTest, EmptyFinalClass) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h index e5bb9773..d24b0f84 100644 --- a/absl/container/internal/container_memory.h +++ b/absl/container/internal/container_memory.h @@ -34,6 +34,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // Allocates at least n bytes aligned to the specified alignment. @@ -433,6 +434,7 @@ struct map_slot_policy { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_ diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc index d6b0495f..7942c7be 100644 --- a/absl/container/internal/container_memory_test.cc +++ b/absl/container/internal/container_memory_test.cc @@ -23,6 +23,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -185,4 +186,5 @@ TEST(DecomposePair, NotDecomposable) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/counting_allocator.h b/absl/container/internal/counting_allocator.h index 4e717bef..9efdc662 100644 --- a/absl/container/internal/counting_allocator.h +++ b/absl/container/internal/counting_allocator.h @@ -19,7 +19,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // This is a stateful allocator, but the state lives outside of the @@ -74,6 +77,7 @@ class CountingAllocator : public std::allocator { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ diff --git a/absl/container/internal/hash_function_defaults.h b/absl/container/internal/hash_function_defaults.h index cb8f03c8..401ddf4d 100644 --- a/absl/container/internal/hash_function_defaults.h +++ b/absl/container/internal/hash_function_defaults.h @@ -56,6 +56,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // The hash of an object of type T is computed by using absl::Hash. @@ -139,6 +140,7 @@ template using hash_default_eq = typename container_internal::HashEq::Eq; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ diff --git a/absl/container/internal/hash_function_defaults_test.cc b/absl/container/internal/hash_function_defaults_test.cc index 82708dbe..2eefc7e0 100644 --- a/absl/container/internal/hash_function_defaults_test.cc +++ b/absl/container/internal/hash_function_defaults_test.cc @@ -22,6 +22,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -248,6 +249,7 @@ TYPED_TEST_SUITE(StringLikeTest, StringTypesCartesianProduct); } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl enum Hash : size_t { @@ -278,6 +280,7 @@ struct hash> { } // namespace std namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -292,4 +295,5 @@ TEST(Delegate, HashDispatch) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hash_generator_testing.cc b/absl/container/internal/hash_generator_testing.cc index 37a23d60..75c4db6c 100644 --- a/absl/container/internal/hash_generator_testing.cc +++ b/absl/container/internal/hash_generator_testing.cc @@ -17,6 +17,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace hash_internal { namespace { @@ -69,4 +70,5 @@ absl::string_view Generator::operator()() const { } // namespace hash_internal } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hash_generator_testing.h b/absl/container/internal/hash_generator_testing.h index 477215cd..6869fe45 100644 --- a/absl/container/internal/hash_generator_testing.h +++ b/absl/container/internal/hash_generator_testing.h @@ -33,6 +33,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace hash_internal { namespace generator_internal { @@ -154,6 +155,7 @@ using GeneratedType = decltype( } // namespace hash_internal } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_ diff --git a/absl/container/internal/hash_policy_testing.h b/absl/container/internal/hash_policy_testing.h index c57407a0..01c40d2e 100644 --- a/absl/container/internal/hash_policy_testing.h +++ b/absl/container/internal/hash_policy_testing.h @@ -30,6 +30,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace hash_testing_internal { @@ -162,6 +163,7 @@ auto keys(const Set& s) } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl // ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS is false for glibcxx versions diff --git a/absl/container/internal/hash_policy_testing_test.cc b/absl/container/internal/hash_policy_testing_test.cc index 0c95eb5e..f0b20fe3 100644 --- a/absl/container/internal/hash_policy_testing_test.cc +++ b/absl/container/internal/hash_policy_testing_test.cc @@ -17,6 +17,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -40,4 +41,5 @@ TEST(_, Hash) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h index fd007de7..3e1209c6 100644 --- a/absl/container/internal/hash_policy_traits.h +++ b/absl/container/internal/hash_policy_traits.h @@ -23,6 +23,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // Defines how slots are initialized/destroyed/moved. @@ -184,6 +185,7 @@ struct hash_policy_traits { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_ diff --git a/absl/container/internal/hash_policy_traits_test.cc b/absl/container/internal/hash_policy_traits_test.cc index e643d189..6ef8b9e0 100644 --- a/absl/container/internal/hash_policy_traits_test.cc +++ b/absl/container/internal/hash_policy_traits_test.cc @@ -22,6 +22,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -139,4 +140,5 @@ TEST_F(Test, with_transfer) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hashtable_debug.h b/absl/container/internal/hashtable_debug.h index 71930004..19d52121 100644 --- a/absl/container/internal/hashtable_debug.h +++ b/absl/container/internal/hashtable_debug.h @@ -38,6 +38,7 @@ #include "absl/container/internal/hashtable_debug_hooks.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // Returns the number of probes required to lookup `key`. Returns 0 for a @@ -103,6 +104,7 @@ size_t LowerBoundAllocatedByteSize(size_t num_elements) { } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_ diff --git a/absl/container/internal/hashtable_debug_hooks.h b/absl/container/internal/hashtable_debug_hooks.h index 371ce81f..3e9ea595 100644 --- a/absl/container/internal/hashtable_debug_hooks.h +++ b/absl/container/internal/hashtable_debug_hooks.h @@ -23,7 +23,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace hashtable_debug_internal { @@ -76,6 +79,7 @@ struct HashtableDebugAccess { } // namespace hashtable_debug_internal } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_ diff --git a/absl/container/internal/hashtablez_sampler.cc b/absl/container/internal/hashtablez_sampler.cc index 6deeca44..e15f4444 100644 --- a/absl/container/internal/hashtablez_sampler.cc +++ b/absl/container/internal/hashtablez_sampler.cc @@ -28,6 +28,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { constexpr int HashtablezInfo::kMaxStackDepth; @@ -265,4 +266,5 @@ void SetHashtablezMaxSamples(int32_t max) { } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hashtablez_sampler.h b/absl/container/internal/hashtablez_sampler.h index c694df05..c4f9629f 100644 --- a/absl/container/internal/hashtablez_sampler.h +++ b/absl/container/internal/hashtablez_sampler.h @@ -51,6 +51,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // Stores information about a sampled hashtable. All mutations to this *must* @@ -281,6 +282,7 @@ void SetHashtablezMaxSamples(int32_t max); extern "C" bool AbslContainerInternalSampleEverything(); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_ diff --git a/absl/container/internal/hashtablez_sampler_force_weak_definition.cc b/absl/container/internal/hashtablez_sampler_force_weak_definition.cc index 984dce5d..78b9d362 100644 --- a/absl/container/internal/hashtablez_sampler_force_weak_definition.cc +++ b/absl/container/internal/hashtablez_sampler_force_weak_definition.cc @@ -17,6 +17,7 @@ #include "absl/base/attributes.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // See hashtablez_sampler.h for details. @@ -25,4 +26,5 @@ extern "C" ABSL_ATTRIBUTE_WEAK bool AbslContainerInternalSampleEverything() { } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/hashtablez_sampler_test.cc b/absl/container/internal/hashtablez_sampler_test.cc index af4b5ee1..102b2375 100644 --- a/absl/container/internal/hashtablez_sampler_test.cc +++ b/absl/container/internal/hashtablez_sampler_test.cc @@ -36,6 +36,7 @@ constexpr int kProbeLength = 8; #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { class HashtablezInfoHandlePeer { public: @@ -354,4 +355,5 @@ TEST(HashtablezSamplerTest, Callback) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 84aa785a..4d80b727 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -30,6 +30,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace inlined_vector_internal { template @@ -885,6 +886,7 @@ auto Storage::Swap(Storage* other_storage_ptr) -> void { } } // namespace inlined_vector_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_ diff --git a/absl/container/internal/layout.h b/absl/container/internal/layout.h index bbdde507..69cc85dd 100644 --- a/absl/container/internal/layout.h +++ b/absl/container/internal/layout.h @@ -188,6 +188,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // A type wrapper that instructs `Layout` to use the specific alignment for the @@ -734,6 +735,7 @@ class Layout : public internal_layout::LayoutType { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_LAYOUT_H_ diff --git a/absl/container/internal/layout_test.cc b/absl/container/internal/layout_test.cc index 33b72bd3..8f3628a1 100644 --- a/absl/container/internal/layout_test.cc +++ b/absl/container/internal/layout_test.cc @@ -28,6 +28,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -1562,4 +1563,5 @@ TEST(CompactString, Works) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/node_hash_policy.h b/absl/container/internal/node_hash_policy.h index 19b4fc09..4617162f 100644 --- a/absl/container/internal/node_hash_policy.h +++ b/absl/container/internal/node_hash_policy.h @@ -39,7 +39,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -83,6 +86,7 @@ struct node_hash_policy { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ diff --git a/absl/container/internal/node_hash_policy_test.cc b/absl/container/internal/node_hash_policy_test.cc index f1d3ec30..84aabba9 100644 --- a/absl/container/internal/node_hash_policy_test.cc +++ b/absl/container/internal/node_hash_policy_test.cc @@ -21,6 +21,7 @@ #include "absl/container/internal/hash_policy_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -64,4 +65,5 @@ TEST_F(NodeTest, transfer) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/raw_hash_map.h b/absl/container/internal/raw_hash_map.h index 7dad120a..0a02757d 100644 --- a/absl/container/internal/raw_hash_map.h +++ b/absl/container/internal/raw_hash_map.h @@ -24,6 +24,7 @@ #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -190,6 +191,7 @@ class raw_hash_map : public raw_hash_set { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_ diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc index ac2d10a7..919ac074 100644 --- a/absl/container/internal/raw_hash_set.cc +++ b/absl/container/internal/raw_hash_set.cc @@ -20,6 +20,7 @@ #include "absl/base/config.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { constexpr size_t Group::kWidth; @@ -43,4 +44,5 @@ bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl) { } } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 469226fe..4103e02a 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -118,6 +118,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -1861,6 +1862,7 @@ struct HashtableDebugAccess> { } // namespace hashtable_debug_internal } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_ diff --git a/absl/container/internal/raw_hash_set_allocator_test.cc b/absl/container/internal/raw_hash_set_allocator_test.cc index a5eff0b3..7ac4b9f7 100644 --- a/absl/container/internal/raw_hash_set_allocator_test.cc +++ b/absl/container/internal/raw_hash_set_allocator_test.cc @@ -20,6 +20,7 @@ #include "absl/container/internal/tracked.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -425,4 +426,5 @@ TEST_F(PropagateOnAll, Swap) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index ec8f9231..38e5e0e8 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -35,6 +35,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { struct RawHashSetTestOnlyAccess { @@ -1913,4 +1914,5 @@ TEST(Sanitizer, PoisoningOnErase) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/test_instance_tracker.cc b/absl/container/internal/test_instance_tracker.cc index 5a66cb4d..f9947f04 100644 --- a/absl/container/internal/test_instance_tracker.cc +++ b/absl/container/internal/test_instance_tracker.cc @@ -15,6 +15,7 @@ #include "absl/container/internal/test_instance_tracker.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace test_internal { int BaseCountedInstance::num_instances_ = 0; int BaseCountedInstance::num_live_instances_ = 0; @@ -24,4 +25,5 @@ int BaseCountedInstance::num_swaps_ = 0; int BaseCountedInstance::num_comparisons_ = 0; } // namespace test_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/test_instance_tracker.h b/absl/container/internal/test_instance_tracker.h index c4731dbe..5ff6fd71 100644 --- a/absl/container/internal/test_instance_tracker.h +++ b/absl/container/internal/test_instance_tracker.h @@ -21,6 +21,7 @@ #include "absl/types/compare.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace test_internal { // A type that counts number of occurrences of the type, the live occurrences of @@ -267,6 +268,7 @@ class MovableOnlyInstance : public BaseCountedInstance { }; } // namespace test_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_ diff --git a/absl/container/internal/tracked.h b/absl/container/internal/tracked.h index 75173ab0..29f5829f 100644 --- a/absl/container/internal/tracked.h +++ b/absl/container/internal/tracked.h @@ -16,10 +16,14 @@ #define ABSL_CONTAINER_INTERNAL_TRACKED_H_ #include + #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { // A class that tracks its copies and moves so that it can be queried in tests. @@ -73,6 +77,7 @@ class Tracked { }; } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_TRACKED_H_ diff --git a/absl/container/internal/unordered_map_constructor_test.h b/absl/container/internal/unordered_map_constructor_test.h index 68817e4e..76ee95e6 100644 --- a/absl/container/internal/unordered_map_constructor_test.h +++ b/absl/container/internal/unordered_map_constructor_test.h @@ -24,6 +24,7 @@ #include "absl/container/internal/hash_policy_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -482,6 +483,7 @@ REGISTER_TYPED_TEST_CASE_P( AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_ diff --git a/absl/container/internal/unordered_map_lookup_test.h b/absl/container/internal/unordered_map_lookup_test.h index ebd3612b..e76421e5 100644 --- a/absl/container/internal/unordered_map_lookup_test.h +++ b/absl/container/internal/unordered_map_lookup_test.h @@ -21,6 +21,7 @@ #include "absl/container/internal/hash_policy_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -110,6 +111,7 @@ REGISTER_TYPED_TEST_CASE_P(LookupTest, At, OperatorBracket, Count, Find, EqualRange); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_ diff --git a/absl/container/internal/unordered_map_members_test.h b/absl/container/internal/unordered_map_members_test.h index 1bf31ab4..7d48cdb8 100644 --- a/absl/container/internal/unordered_map_members_test.h +++ b/absl/container/internal/unordered_map_members_test.h @@ -21,6 +21,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -80,6 +81,7 @@ TYPED_TEST_P(MembersTest, BeginEnd) { REGISTER_TYPED_TEST_SUITE_P(MembersTest, Typedefs, SimpleFunctions, BeginEnd); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MEMBERS_TEST_H_ diff --git a/absl/container/internal/unordered_map_modifiers_test.h b/absl/container/internal/unordered_map_modifiers_test.h index f6aff542..b8c513f1 100644 --- a/absl/container/internal/unordered_map_modifiers_test.h +++ b/absl/container/internal/unordered_map_modifiers_test.h @@ -23,6 +23,7 @@ #include "absl/container/internal/hash_policy_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -309,6 +310,7 @@ TYPED_TEST_P(UniquePtrModifiersTest, TryEmplace) { REGISTER_TYPED_TEST_SUITE_P(UniquePtrModifiersTest, TryEmplace); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_ diff --git a/absl/container/internal/unordered_map_test.cc b/absl/container/internal/unordered_map_test.cc index 114b342d..9cbf512f 100644 --- a/absl/container/internal/unordered_map_test.cc +++ b/absl/container/internal/unordered_map_test.cc @@ -21,6 +21,7 @@ #include "absl/container/internal/unordered_map_modifiers_test.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -45,4 +46,5 @@ INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, UniquePtrModifiersTest, } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/internal/unordered_set_constructor_test.h b/absl/container/internal/unordered_set_constructor_test.h index f4844683..41165b05 100644 --- a/absl/container/internal/unordered_set_constructor_test.h +++ b/absl/container/internal/unordered_set_constructor_test.h @@ -26,6 +26,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -489,6 +490,7 @@ REGISTER_TYPED_TEST_CASE_P( AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_ diff --git a/absl/container/internal/unordered_set_lookup_test.h b/absl/container/internal/unordered_set_lookup_test.h index 05b32b5d..8f2f4b20 100644 --- a/absl/container/internal/unordered_set_lookup_test.h +++ b/absl/container/internal/unordered_set_lookup_test.h @@ -21,6 +21,7 @@ #include "absl/container/internal/hash_policy_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -84,6 +85,7 @@ TYPED_TEST_P(LookupTest, EqualRange) { REGISTER_TYPED_TEST_CASE_P(LookupTest, Count, Find, EqualRange); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_ diff --git a/absl/container/internal/unordered_set_members_test.h b/absl/container/internal/unordered_set_members_test.h index b96c945a..4c5e104a 100644 --- a/absl/container/internal/unordered_set_members_test.h +++ b/absl/container/internal/unordered_set_members_test.h @@ -21,6 +21,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -79,6 +80,7 @@ TYPED_TEST_P(MembersTest, BeginEnd) { REGISTER_TYPED_TEST_SUITE_P(MembersTest, Typedefs, SimpleFunctions, BeginEnd); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MEMBERS_TEST_H_ diff --git a/absl/container/internal/unordered_set_modifiers_test.h b/absl/container/internal/unordered_set_modifiers_test.h index 79a8d422..26be58d9 100644 --- a/absl/container/internal/unordered_set_modifiers_test.h +++ b/absl/container/internal/unordered_set_modifiers_test.h @@ -21,6 +21,7 @@ #include "absl/container/internal/hash_policy_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template @@ -183,6 +184,7 @@ REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint, EraseKey, Swap); } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_ diff --git a/absl/container/internal/unordered_set_test.cc b/absl/container/internal/unordered_set_test.cc index 6478fac1..a134b539 100644 --- a/absl/container/internal/unordered_set_test.cc +++ b/absl/container/internal/unordered_set_test.cc @@ -20,6 +20,7 @@ #include "absl/container/internal/unordered_set_modifiers_test.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -36,4 +37,5 @@ INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, ModifiersTest, SetTypes); } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index a718842b..e8065a98 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h @@ -48,6 +48,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template class NodeHashMapPolicy; @@ -581,6 +582,7 @@ struct IsUnorderedContainer< } // namespace container_algorithm_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_NODE_HASH_MAP_H_ diff --git a/absl/container/node_hash_map_test.cc b/absl/container/node_hash_map_test.cc index 0f2714a7..f923e915 100644 --- a/absl/container/node_hash_map_test.cc +++ b/absl/container/node_hash_map_test.cc @@ -21,6 +21,7 @@ #include "absl/container/internal/unordered_map_modifiers_test.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { @@ -217,4 +218,5 @@ TEST(NodeHashMap, MergeExtractInsert) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index 0cd1fe57..43ada3f9 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h @@ -44,6 +44,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { template struct NodeHashSetPolicy; @@ -483,6 +484,7 @@ struct IsUnorderedContainer> : std::true_type {}; } // namespace container_algorithm_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_NODE_HASH_SET_H_ diff --git a/absl/container/node_hash_set_test.cc b/absl/container/node_hash_set_test.cc index 0ea76e7c..e1d544ff 100644 --- a/absl/container/node_hash_set_test.cc +++ b/absl/container/node_hash_set_test.cc @@ -20,6 +20,7 @@ #include "absl/container/internal/unordered_set_modifiers_test.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { using ::absl::container_internal::hash_internal::Enum; @@ -102,4 +103,5 @@ TEST(NodeHashSet, MergeExtractInsert) { } // namespace } // namespace container_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/BUILD.bazel b/absl/debugging/BUILD.bazel index 2601090b..32cacefd 100644 --- a/absl/debugging/BUILD.bazel +++ b/absl/debugging/BUILD.bazel @@ -38,6 +38,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":debugging_internal", + "//absl/base:config", "//absl/base:core_headers", ], ) @@ -63,6 +64,7 @@ cc_library( ":debugging_internal", ":demangle_internal", "//absl/base", + "//absl/base:config", "//absl/base:core_headers", "//absl/base:dynamic_annotations", "//absl/base:malloc_internal", @@ -106,6 +108,7 @@ cc_library( deps = [ ":stacktrace", ":symbolize", + "//absl/base:config", "//absl/base:core_headers", "//absl/base:raw_logging_internal", ], @@ -168,6 +171,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + "//absl/base:config", "//absl/base:core_headers", "//absl/base:dynamic_annotations", "//absl/base:raw_logging_internal", @@ -181,6 +185,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, deps = [ "//absl/base", + "//absl/base:config", "//absl/base:core_headers", ], ) @@ -205,7 +210,10 @@ cc_library( srcs = ["leak_check.cc"], hdrs = ["leak_check.h"], linkopts = ABSL_DEFAULT_LINKOPTS, - deps = ["//absl/base:core_headers"], + deps = [ + "//absl/base:config", + "//absl/base:core_headers", + ], ) # Adding a dependency to leak_check_disable will disable @@ -216,6 +224,7 @@ cc_library( srcs = ["leak_check_disable.cc"], linkopts = ABSL_DEFAULT_LINKOPTS, linkstatic = 1, + deps = ["//absl/base:config"], alwayslink = 1, ) @@ -237,6 +246,9 @@ cc_library( }), linkopts = ABSL_DEFAULT_LINKOPTS, visibility = ["//visibility:private"], + deps = [ + "//absl/base:config", + ], ) cc_library( @@ -247,6 +259,9 @@ cc_library( copts = ["-ULEAK_SANITIZER"], linkopts = ABSL_DEFAULT_LINKOPTS, visibility = ["//visibility:private"], + deps = [ + "//absl/base:config", + ], ) cc_test( @@ -304,6 +319,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, visibility = ["//visibility:private"], deps = [ + "//absl/base:config", "//absl/base:core_headers", "//absl/base:raw_logging_internal", ], diff --git a/absl/debugging/CMakeLists.txt b/absl/debugging/CMakeLists.txt index eff504b4..ef9e71fc 100644 --- a/absl/debugging/CMakeLists.txt +++ b/absl/debugging/CMakeLists.txt @@ -25,6 +25,7 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} DEPS absl::debugging_internal + absl::config absl::core_headers PUBLIC ) @@ -49,6 +50,7 @@ absl_cc_library( absl::debugging_internal absl::demangle_internal absl::base + absl::config absl::core_headers absl::dynamic_annotations absl::malloc_internal @@ -88,6 +90,7 @@ absl_cc_library( DEPS absl::stacktrace absl::symbolize + absl::config absl::core_headers absl::raw_logging_internal ) @@ -151,6 +154,7 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} DEPS absl::core_headers + absl::config absl::dynamic_annotations absl::raw_logging_internal ) @@ -196,6 +200,7 @@ absl_cc_library( COPTS ${ABSL_DEFAULT_COPTS} DEPS + absl::config absl::core_headers PUBLIC ) @@ -293,6 +298,7 @@ absl_cc_library( COPTS ${ABSL_DEFAULT_COPTS} DEPS + absl::config absl::core_headers absl::raw_logging_internal TESTONLY diff --git a/absl/debugging/failure_signal_handler.cc b/absl/debugging/failure_signal_handler.cc index c6a4d962..470d6768 100644 --- a/absl/debugging/failure_signal_handler.cc +++ b/absl/debugging/failure_signal_handler.cc @@ -47,6 +47,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN ABSL_CONST_INIT static FailureSignalHandlerOptions fsh_options; @@ -356,4 +357,5 @@ void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options) { } } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/failure_signal_handler.h b/absl/debugging/failure_signal_handler.h index 1beb78b9..f5a83962 100644 --- a/absl/debugging/failure_signal_handler.h +++ b/absl/debugging/failure_signal_handler.h @@ -44,7 +44,10 @@ #ifndef ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_ #define ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN // FailureSignalHandlerOptions // @@ -112,6 +115,7 @@ namespace debugging_internal { const char* FailureSignalToString(int signo); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_ diff --git a/absl/debugging/internal/address_is_readable.cc b/absl/debugging/internal/address_is_readable.cc index 99c4c64b..f45e59b3 100644 --- a/absl/debugging/internal/address_is_readable.cc +++ b/absl/debugging/internal/address_is_readable.cc @@ -20,12 +20,14 @@ #if !defined(__linux__) || defined(__ANDROID__) namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // On platforms other than Linux, just return true. bool AddressIsReadable(const void* /* addr */) { return true; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #else @@ -40,6 +42,7 @@ bool AddressIsReadable(const void* /* addr */) { return true; } #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Pack a pid and two file descriptors into a 64-bit word, @@ -128,6 +131,7 @@ bool AddressIsReadable(const void *addr) { } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif diff --git a/absl/debugging/internal/address_is_readable.h b/absl/debugging/internal/address_is_readable.h index ca8003e6..4bbaf4d6 100644 --- a/absl/debugging/internal/address_is_readable.h +++ b/absl/debugging/internal/address_is_readable.h @@ -15,7 +15,10 @@ #ifndef ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_ #define ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Return whether the byte at *addr is readable, without faulting. @@ -23,6 +26,7 @@ namespace debugging_internal { bool AddressIsReadable(const void *addr); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_ diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc index 3809e496..fc615c3f 100644 --- a/absl/debugging/internal/demangle.cc +++ b/absl/debugging/internal/demangle.cc @@ -24,6 +24,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { typedef struct { @@ -1890,4 +1891,5 @@ bool Demangle(const char *mangled, char *out, int out_size) { } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/internal/demangle.h b/absl/debugging/internal/demangle.h index 81bb0dfd..c314d9bc 100644 --- a/absl/debugging/internal/demangle.h +++ b/absl/debugging/internal/demangle.h @@ -53,7 +53,10 @@ #ifndef ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_ #define ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Demangle `mangled`. On success, return true and write the @@ -62,6 +65,7 @@ namespace debugging_internal { bool Demangle(const char *mangled, char *out, int out_size); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_ diff --git a/absl/debugging/internal/demangle_test.cc b/absl/debugging/internal/demangle_test.cc index a68ce324..c6f1ce18 100644 --- a/absl/debugging/internal/demangle_test.cc +++ b/absl/debugging/internal/demangle_test.cc @@ -23,6 +23,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { namespace { @@ -190,4 +191,5 @@ TEST(DemangleRegression, DeeplyNestedArrayType) { } // namespace } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/internal/elf_mem_image.cc b/absl/debugging/internal/elf_mem_image.cc index e7408bca..24cc0130 100644 --- a/absl/debugging/internal/elf_mem_image.cc +++ b/absl/debugging/internal/elf_mem_image.cc @@ -38,6 +38,7 @@ #define VERSYM_VERSION 0x7fff namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { namespace { @@ -375,6 +376,7 @@ void ElfMemImage::SymbolIterator::Update(int increment) { } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_ELF_MEM_IMAGE diff --git a/absl/debugging/internal/elf_mem_image.h b/absl/debugging/internal/elf_mem_image.h index d84200db..46bfade3 100644 --- a/absl/debugging/internal/elf_mem_image.h +++ b/absl/debugging/internal/elf_mem_image.h @@ -23,6 +23,8 @@ // used. #include +#include "absl/base/config.h" + // Maybe one day we can rewrite this file not to require the elf // symbol extensions in glibc, but for right now we need them. #ifdef ABSL_HAVE_ELF_MEM_IMAGE @@ -39,6 +41,7 @@ #include // for ElfW namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // An in-memory ELF image (may not exist on disk). @@ -123,6 +126,7 @@ class ElfMemImage { }; } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_ELF_MEM_IMAGE diff --git a/absl/debugging/internal/examine_stack.cc b/absl/debugging/internal/examine_stack.cc index 1ebc788f..22f41b46 100644 --- a/absl/debugging/internal/examine_stack.cc +++ b/absl/debugging/internal/examine_stack.cc @@ -30,6 +30,7 @@ #include "absl/debugging/symbolize.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Returns the program counter from signal context, nullptr if @@ -150,4 +151,5 @@ void DumpPCAndFrameSizesAndStackTrace( } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/internal/examine_stack.h b/absl/debugging/internal/examine_stack.h index 56c9763e..39336913 100644 --- a/absl/debugging/internal/examine_stack.h +++ b/absl/debugging/internal/examine_stack.h @@ -17,7 +17,10 @@ #ifndef ABSL_DEBUGGING_INTERNAL_EXAMINE_STACK_H_ #define ABSL_DEBUGGING_INTERNAL_EXAMINE_STACK_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Returns the program counter from signal context, or nullptr if @@ -33,6 +36,7 @@ void DumpPCAndFrameSizesAndStackTrace( void (*writerfn)(const char*, void*), void* writerfn_arg); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_EXAMINE_STACK_H_ diff --git a/absl/debugging/internal/stack_consumption.cc b/absl/debugging/internal/stack_consumption.cc index d4703264..875ca6d9 100644 --- a/absl/debugging/internal/stack_consumption.cc +++ b/absl/debugging/internal/stack_consumption.cc @@ -27,6 +27,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { namespace { @@ -177,6 +178,7 @@ int GetSignalHandlerStackConsumption(void (*signal_handler)(int)) { } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION diff --git a/absl/debugging/internal/stack_consumption.h b/absl/debugging/internal/stack_consumption.h index b860a3c1..5e60ec42 100644 --- a/absl/debugging/internal/stack_consumption.h +++ b/absl/debugging/internal/stack_consumption.h @@ -18,6 +18,8 @@ #ifndef ABSL_DEBUGGING_INTERNAL_STACK_CONSUMPTION_H_ #define ABSL_DEBUGGING_INTERNAL_STACK_CONSUMPTION_H_ +#include "absl/base/config.h" + // The code in this module is not portable. // Use this feature test macro to detect its availability. #ifdef ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION @@ -27,6 +29,7 @@ #define ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION 1 namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Returns the stack consumption in bytes for the code exercised by @@ -38,6 +41,7 @@ namespace debugging_internal { int GetSignalHandlerStackConsumption(void (*signal_handler)(int)); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION diff --git a/absl/debugging/internal/stack_consumption_test.cc b/absl/debugging/internal/stack_consumption_test.cc index 68bfa126..80445bf4 100644 --- a/absl/debugging/internal/stack_consumption_test.cc +++ b/absl/debugging/internal/stack_consumption_test.cc @@ -23,6 +23,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { namespace { @@ -43,6 +44,7 @@ TEST(SignalHandlerStackConsumptionTest, MeasuresStackConsumption) { } // namespace } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION diff --git a/absl/debugging/internal/stacktrace_aarch64-inl.inc b/absl/debugging/internal/stacktrace_aarch64-inl.inc index 7ed6b3eb..411ea308 100644 --- a/absl/debugging/internal/stacktrace_aarch64-inl.inc +++ b/absl/debugging/internal/stacktrace_aarch64-inl.inc @@ -180,11 +180,13 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return true; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ diff --git a/absl/debugging/internal/stacktrace_arm-inl.inc b/absl/debugging/internal/stacktrace_arm-inl.inc index c8408337..fffda968 100644 --- a/absl/debugging/internal/stacktrace_arm-inl.inc +++ b/absl/debugging/internal/stacktrace_arm-inl.inc @@ -113,11 +113,13 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return false; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_ diff --git a/absl/debugging/internal/stacktrace_generic-inl.inc b/absl/debugging/internal/stacktrace_generic-inl.inc index 81a49ef2..ac034c9f 100644 --- a/absl/debugging/internal/stacktrace_generic-inl.inc +++ b/absl/debugging/internal/stacktrace_generic-inl.inc @@ -87,11 +87,13 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return true; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_GENERIC_INL_H_ diff --git a/absl/debugging/internal/stacktrace_powerpc-inl.inc b/absl/debugging/internal/stacktrace_powerpc-inl.inc index 3a070ee4..2e7c2f40 100644 --- a/absl/debugging/internal/stacktrace_powerpc-inl.inc +++ b/absl/debugging/internal/stacktrace_powerpc-inl.inc @@ -236,11 +236,13 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return true; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_ diff --git a/absl/debugging/internal/stacktrace_unimplemented-inl.inc b/absl/debugging/internal/stacktrace_unimplemented-inl.inc index e256fdd4..5b8fb191 100644 --- a/absl/debugging/internal/stacktrace_unimplemented-inl.inc +++ b/absl/debugging/internal/stacktrace_unimplemented-inl.inc @@ -12,11 +12,13 @@ static int UnwindImpl(void** /* result */, int* /* sizes */, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return false; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_UNIMPLEMENTED_INL_H_ diff --git a/absl/debugging/internal/stacktrace_win32-inl.inc b/absl/debugging/internal/stacktrace_win32-inl.inc index 247e7428..9c2c5580 100644 --- a/absl/debugging/internal/stacktrace_win32-inl.inc +++ b/absl/debugging/internal/stacktrace_win32-inl.inc @@ -73,11 +73,13 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return false; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_ diff --git a/absl/debugging/internal/stacktrace_x86-inl.inc b/absl/debugging/internal/stacktrace_x86-inl.inc index 9494441e..972febdd 100644 --- a/absl/debugging/internal/stacktrace_x86-inl.inc +++ b/absl/debugging/internal/stacktrace_x86-inl.inc @@ -330,11 +330,13 @@ static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count, } namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { bool StackTraceWorksForTest() { return true; } } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_X86_INL_INC_ diff --git a/absl/debugging/internal/symbolize.h b/absl/debugging/internal/symbolize.h index 3e537893..5d0858b5 100644 --- a/absl/debugging/internal/symbolize.h +++ b/absl/debugging/internal/symbolize.h @@ -21,6 +21,8 @@ #include #include +#include "absl/base/config.h" + #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set #elif defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) && \ @@ -33,6 +35,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // Iterates over all sections, invoking callback on each with the section name @@ -51,11 +54,13 @@ bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, ElfW(Shdr) *out); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { struct SymbolDecoratorArgs { @@ -117,6 +122,7 @@ bool GetFileMappingHint(const void** start, const char** filename); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ diff --git a/absl/debugging/internal/vdso_support.cc b/absl/debugging/internal/vdso_support.cc index d13ef25d..1e8a78ac 100644 --- a/absl/debugging/internal/vdso_support.cc +++ b/absl/debugging/internal/vdso_support.cc @@ -38,6 +38,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { ABSL_CONST_INIT @@ -187,6 +188,7 @@ static class VDSOInitHelper { } vdso_init_helper; } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_VDSO_SUPPORT diff --git a/absl/debugging/internal/vdso_support.h b/absl/debugging/internal/vdso_support.h index 9895b48d..6562c6c2 100644 --- a/absl/debugging/internal/vdso_support.h +++ b/absl/debugging/internal/vdso_support.h @@ -53,6 +53,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { // NOTE: this class may be used from within tcmalloc, and can not @@ -149,6 +150,7 @@ class VDSOSupport { int GetCPU(); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_ELF_MEM_IMAGE diff --git a/absl/debugging/leak_check.cc b/absl/debugging/leak_check.cc index ffe3d1bd..ff904955 100644 --- a/absl/debugging/leak_check.cc +++ b/absl/debugging/leak_check.cc @@ -21,12 +21,14 @@ #ifndef LEAK_SANITIZER namespace absl { +ABSL_NAMESPACE_BEGIN bool HaveLeakSanitizer() { return false; } void DoIgnoreLeak(const void*) { } void RegisterLivePointers(const void*, size_t) { } void UnRegisterLivePointers(const void*, size_t) { } LeakCheckDisabler::LeakCheckDisabler() { } LeakCheckDisabler::~LeakCheckDisabler() { } +ABSL_NAMESPACE_END } // namespace absl #else @@ -34,6 +36,7 @@ LeakCheckDisabler::~LeakCheckDisabler() { } #include namespace absl { +ABSL_NAMESPACE_BEGIN bool HaveLeakSanitizer() { return true; } void DoIgnoreLeak(const void* ptr) { __lsan_ignore_object(ptr); } void RegisterLivePointers(const void* ptr, size_t size) { @@ -44,6 +47,7 @@ void UnRegisterLivePointers(const void* ptr, size_t size) { } LeakCheckDisabler::LeakCheckDisabler() { __lsan_disable(); } LeakCheckDisabler::~LeakCheckDisabler() { __lsan_enable(); } +ABSL_NAMESPACE_END } // namespace absl #endif // LEAK_SANITIZER diff --git a/absl/debugging/leak_check.h b/absl/debugging/leak_check.h index 4d489c58..7a5a22dd 100644 --- a/absl/debugging/leak_check.h +++ b/absl/debugging/leak_check.h @@ -32,7 +32,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN // HaveLeakSanitizer() // @@ -104,6 +107,7 @@ void RegisterLivePointers(const void* ptr, size_t size); // `RegisterLivePointers()`, enabling leak checking of those pointers. void UnRegisterLivePointers(const void* ptr, size_t size); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_LEAK_CHECK_H_ diff --git a/absl/debugging/stacktrace.cc b/absl/debugging/stacktrace.cc index 9de8782f..1f7c7d82 100644 --- a/absl/debugging/stacktrace.cc +++ b/absl/debugging/stacktrace.cc @@ -57,6 +57,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace { typedef int (*Unwinder)(void**, int*, int, int, const void*, int*); @@ -135,4 +136,5 @@ int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip, return n; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/stacktrace.h b/absl/debugging/stacktrace.h index cd8fae76..0ec0ffda 100644 --- a/absl/debugging/stacktrace.h +++ b/absl/debugging/stacktrace.h @@ -31,7 +31,10 @@ #ifndef ABSL_DEBUGGING_STACKTRACE_H_ #define ABSL_DEBUGGING_STACKTRACE_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN // GetStackFrames() // @@ -222,6 +225,7 @@ namespace debugging_internal { // working. extern bool StackTraceWorksForTest(); } // namespace debugging_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_STACKTRACE_H_ diff --git a/absl/debugging/symbolize.h b/absl/debugging/symbolize.h index a73dbd9e..a8ff3740 100644 --- a/absl/debugging/symbolize.h +++ b/absl/debugging/symbolize.h @@ -55,6 +55,7 @@ #include "absl/debugging/internal/symbolize.h" namespace absl { +ABSL_NAMESPACE_BEGIN // InitializeSymbolizer() // @@ -92,6 +93,7 @@ void InitializeSymbolizer(const char* argv0); // } bool Symbolize(const void *pc, char *out, int out_size); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_DEBUGGING_SYMBOLIZE_H_ diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index 14f0c972..c371635f 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc @@ -76,6 +76,7 @@ #include "absl/debugging/internal/vdso_support.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Value of argv[0]. Used by MaybeInitializeObjFile(). static char *argv0_value = nullptr; @@ -1475,4 +1476,5 @@ bool Symbolize(const void *pc, char *out, int out_size) { return ok; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/symbolize_unimplemented.inc b/absl/debugging/symbolize_unimplemented.inc index 7c580fe4..db24456b 100644 --- a/absl/debugging/symbolize_unimplemented.inc +++ b/absl/debugging/symbolize_unimplemented.inc @@ -17,6 +17,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace debugging_internal { @@ -35,4 +36,5 @@ bool GetFileMappingHint(const void **, const void **, uint64_t *, const char **) void InitializeSymbolizer(const char*) {} bool Symbolize(const void *, char *, int) { return false; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/debugging/symbolize_win32.inc b/absl/debugging/symbolize_win32.inc index bd6fbd5e..c3df46f6 100644 --- a/absl/debugging/symbolize_win32.inc +++ b/absl/debugging/symbolize_win32.inc @@ -31,6 +31,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN static HANDLE process = NULL; @@ -76,4 +77,5 @@ bool Symbolize(const void* pc, char* out, int out_size) { return true; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/declare.h b/absl/flags/declare.h index 4926a09e..f7509ce7 100644 --- a/absl/flags/declare.h +++ b/absl/flags/declare.h @@ -28,6 +28,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // absl::Flag represents a flag of type 'T' created by ABSL_FLAG. @@ -47,6 +48,7 @@ template using Flag = flags_internal::Flag; #endif +ABSL_NAMESPACE_END } // namespace absl // ABSL_DECLARE_FLAG() diff --git a/absl/flags/flag.cc b/absl/flags/flag.cc index 37bbce23..7faa7ade 100644 --- a/absl/flags/flag.cc +++ b/absl/flags/flag.cc @@ -18,6 +18,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN // We want to validate the type mismatch between type definition and // declaration. The lock-free implementation does not allow us to do it, @@ -55,4 +56,5 @@ absl::Mutex* GetGlobalConstructionGuard() { return &construction_guard; } #endif +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/flag.h b/absl/flags/flag.h index 52c3cede..b6fbd116 100644 --- a/absl/flags/flag.h +++ b/absl/flags/flag.h @@ -38,6 +38,7 @@ #include "absl/flags/marshalling.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Flag // @@ -219,6 +220,7 @@ void SetFlag(absl::Flag* flag, const V& v) { flag->Set(value); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/flag_test_defs.cc b/absl/flags/flag_test_defs.cc index 3366c580..49f91dee 100644 --- a/absl/flags/flag_test_defs.cc +++ b/absl/flags/flag_test_defs.cc @@ -20,3 +20,5 @@ ABSL_FLAG(int, mistyped_int_flag, 0, ""); ABSL_FLAG(std::string, mistyped_string_flag, "", ""); +ABSL_RETIRED_FLAG(bool, old_bool_flag, true, + "repetition of retired flag definition"); diff --git a/absl/flags/internal/commandlineflag.cc b/absl/flags/internal/commandlineflag.cc index 88f91e16..09249274 100644 --- a/absl/flags/internal/commandlineflag.cc +++ b/absl/flags/internal/commandlineflag.cc @@ -18,6 +18,7 @@ #include "absl/flags/usage_config.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // The help message indicating that the commandline flag has been @@ -57,4 +58,5 @@ std::string CommandLineFlag::Filename() const { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h index 7964f1bf..49e13d1e 100644 --- a/absl/flags/internal/commandlineflag.h +++ b/absl/flags/internal/commandlineflag.h @@ -23,6 +23,7 @@ #include "absl/types/optional.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // Type-specific operations, eg., parsing, copying, etc. are provided @@ -158,7 +159,7 @@ class CommandLineFlag { : name_(name), filename_(filename) {} // Virtual destructor - virtual void Destroy() const = 0; + virtual void Destroy() = 0; // Not copyable/assignable. CommandLineFlag(const CommandLineFlag&) = delete; @@ -280,6 +281,7 @@ class CommandLineFlag { A(float) } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_ diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 435cc362..599bd7a4 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -19,6 +19,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { @@ -34,20 +35,40 @@ bool ShouldValidateFlagValue(const CommandLineFlag& flag) { return true; } +// RAII helper used to temporarily unlock and relock `absl::Mutex`. +// This is used when we need to ensure that locks are released while +// invoking user supplied callbacks and then reacquired, since callbacks may +// need to acquire these locks themselves. +class MutexRelock { + public: + explicit MutexRelock(absl::Mutex* mu) : mu_(mu) { mu_->Unlock(); } + ~MutexRelock() { mu_->Lock(); } + + MutexRelock(const MutexRelock&) = delete; + MutexRelock& operator=(const MutexRelock&) = delete; + + private: + absl::Mutex* mu_; +}; + +// This global lock guards the initialization and destruction of data_guard_, +// which is used to guard the other Flag data. +ABSL_CONST_INIT static absl::Mutex flag_mutex_lifetime_guard(absl::kConstInit); + } // namespace void FlagImpl::Init() { - ABSL_CONST_INIT static absl::Mutex init_lock(absl::kConstInit); - { - absl::MutexLock lock(&init_lock); + absl::MutexLock lock(&flag_mutex_lifetime_guard); - if (locks_ == nullptr) { // Must initialize Mutexes for this flag. - locks_ = new FlagImpl::CommandLineFlagLocks; + // Must initialize data guard for this flag. + if (!is_data_guard_inited_) { + new (&data_guard_) absl::Mutex; + is_data_guard_inited_ = true; } } - absl::MutexLock lock(&locks_->primary_mu); + absl::MutexLock lock(reinterpret_cast(&data_guard_)); if (cur_ != nullptr) { inited_.store(true, std::memory_order_release); @@ -67,21 +88,29 @@ absl::Mutex* FlagImpl::DataGuard() const { const_cast(this)->Init(); } - // All fields initialized; locks_ is therefore safe to read. - return &locks_->primary_mu; + // data_guard_ is initialized. + return reinterpret_cast(&data_guard_); } -void FlagImpl::Destroy() const { +void FlagImpl::Destroy() { { absl::MutexLock l(DataGuard()); // Values are heap allocated for Abseil Flags. if (cur_) Delete(op_, cur_); - if (def_kind_ == FlagDefaultSrcKind::kDynamicValue) + + // Release the dynamically allocated default value if any. + if (def_kind_ == FlagDefaultSrcKind::kDynamicValue) { Delete(op_, default_src_.dynamic_value); + } + + // If this flag has an assigned callback, release callback data. + if (callback_data_) delete callback_data_; } - delete locks_; + absl::MutexLock l(&flag_mutex_lifetime_guard); + DataGuard()->~Mutex(); + is_data_guard_inited_ = false; } std::unique_ptr FlagImpl::MakeInitValue() const { @@ -126,13 +155,20 @@ void FlagImpl::SetCallback( const flags_internal::FlagCallback mutation_callback) { absl::MutexLock l(DataGuard()); - callback_ = mutation_callback; + if (callback_data_ == nullptr) { + callback_data_ = new CallbackData; + } + callback_data_->func = mutation_callback; InvokeCallback(); } void FlagImpl::InvokeCallback() const { - if (!callback_) return; + if (!callback_data_) return; + + // Make a copy of the C-style function pointer that we are about to invoke + // before we release the lock guarding it. + FlagCallback cb = callback_data_->func; // If the flag has a mutation callback this function invokes it. While the // callback is being invoked the primary flag's mutex is unlocked and it is @@ -145,14 +181,9 @@ void FlagImpl::InvokeCallback() const { // and it also can be different by the time the callback invocation is // completed. Requires that *primary_lock be held in exclusive mode; it may be // released and reacquired by the implementation. - DataGuard()->Unlock(); - - { - absl::MutexLock lock(&locks_->callback_mu); - callback_(); - } - - DataGuard()->Lock(); + MutexRelock relock(DataGuard()); + absl::MutexLock lock(&callback_data_->guard); + cb(); } bool FlagImpl::RestoreState(const CommandLineFlag& flag, const void* value, @@ -177,12 +208,13 @@ bool FlagImpl::RestoreState(const CommandLineFlag& flag, const void* value, } // Attempts to parse supplied `value` string using parsing routine in the `flag` -// argument. If parsing successful, this function stores the parsed value in -// 'dst' assuming it is a pointer to the flag's value type. In case if any error -// is encountered in either step, the error message is stored in 'err' -bool FlagImpl::TryParse(const CommandLineFlag& flag, void* dst, +// argument. If parsing successful, this function replaces the dst with newly +// parsed value. In case if any error is encountered in either step, the error +// message is stored in 'err' +bool FlagImpl::TryParse(const CommandLineFlag& flag, void** dst, absl::string_view value, std::string* err) const { auto tentative_value = MakeInitValue(); + std::string parse_err; if (!Parse(marshalling_op_, value, tentative_value.get(), &parse_err)) { auto type_name = flag.Typename(); @@ -194,7 +226,10 @@ bool FlagImpl::TryParse(const CommandLineFlag& flag, void* dst, return false; } - Copy(op_, tentative_value.get(), dst); + void* old_val = *dst; + *dst = tentative_value.release(); + tentative_value.reset(old_val); + return true; } @@ -274,7 +309,7 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag, switch (set_mode) { case SET_FLAGS_VALUE: { // set or modify the flag's value - if (!TryParse(flag, cur_, value, err)) return false; + if (!TryParse(flag, &cur_, value, err)) return false; modified_ = true; counter_++; StoreAtomic(); @@ -288,7 +323,7 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag, case SET_FLAG_IF_DEFAULT: { // set the flag's value, but only if it hasn't been set by someone else if (!modified_) { - if (!TryParse(flag, cur_, value, err)) return false; + if (!TryParse(flag, &cur_, value, err)) return false; modified_ = true; counter_++; StoreAtomic(); @@ -305,18 +340,19 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag, break; } case SET_FLAGS_DEFAULT: { - // Flag's new default-value. - auto new_default_value = MakeInitValue(); - - if (!TryParse(flag, new_default_value.get(), value, err)) return false; - if (def_kind_ == FlagDefaultSrcKind::kDynamicValue) { - // Release old default value. - Delete(op_, default_src_.dynamic_value); - } + if (!TryParse(flag, &default_src_.dynamic_value, value, err)) { + return false; + } + } else { + void* new_default_val = nullptr; + if (!TryParse(flag, &new_default_val, value, err)) { + return false; + } - default_src_.dynamic_value = new_default_value.release(); - def_kind_ = FlagDefaultSrcKind::kDynamicValue; + default_src_.dynamic_value = new_default_val; + def_kind_ = FlagDefaultSrcKind::kDynamicValue; + } if (!modified_) { // Need to set both default value *and* current, in this case @@ -361,4 +397,5 @@ bool FlagImpl::ValidateInputValue(absl::string_view value) const { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index 947a8cad..20de406f 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -27,6 +27,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { constexpr int64_t AtomicInit() { return 0xababababababababll; } @@ -156,10 +157,11 @@ class FlagImpl { help_(help.source), help_source_kind_(help.kind), def_kind_(flags_internal::FlagDefaultSrcKind::kGenFunc), - default_src_(default_value_gen) {} + default_src_(default_value_gen), + data_guard_{} {} // Forces destruction of the Flag's data. - void Destroy() const; + void Destroy(); // Constant access methods std::string Help() const; @@ -170,9 +172,10 @@ class FlagImpl { void Read(const CommandLineFlag& flag, void* dst, const flags_internal::FlagOpFn dst_op) const ABSL_LOCKS_EXCLUDED(*DataGuard()); - // Attempts to parse supplied `value` std::string. - bool TryParse(const CommandLineFlag& flag, void* dst, absl::string_view value, - std::string* err) const + // Attempts to parse supplied `value` std::string. If parsing is successful, then + // it replaces `dst` with the new value. + bool TryParse(const CommandLineFlag& flag, void** dst, + absl::string_view value, std::string* err) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); template bool AtomicGet(T* v) const { @@ -226,8 +229,7 @@ class FlagImpl { void Init(); // Ensures that the lazily initialized data is initialized, // and returns pointer to the mutex guarding flags data. - absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED(locks_->primary_mu); - + absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED((absl::Mutex*)&data_guard_); // Returns heap allocated value of type T initialized with default value. std::unique_ptr MakeInitValue() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); @@ -239,9 +241,12 @@ class FlagImpl { // Indicates if help message was supplied as literal or generator func. const FlagHelpSrcKind help_source_kind_; - // Mutable Flag's data. (guarded by DataGuard()). - // Indicates that locks_ and cur_ fields have been lazily initialized. + // Indicates that the Flag state is initialized. std::atomic inited_{false}; + // Mutable Flag state (guarded by data_guard_). + // Additional bool to protect against multiple concurrent constructions + // of `data_guard_`. + bool is_data_guard_inited_ = false; // Has flag value been modified? bool modified_ ABSL_GUARDED_BY(*DataGuard()) = false; // Specified on command line. @@ -261,22 +266,20 @@ class FlagImpl { // For some types, a copy of the current value is kept in an atomically // accessible field. std::atomic atomic_{flags_internal::AtomicInit()}; - // Mutation callback - FlagCallback callback_ = nullptr; - - // Lazily initialized mutexes for this flag value. We cannot inline a - // SpinLock or Mutex here because those have non-constexpr constructors and - // so would prevent constant initialization of this type. - // TODO(rogeeff): fix it once Mutex has constexpr constructor - // The following struct contains the locks in a CommandLineFlag struct. - // They are in a separate struct that is lazily allocated to avoid problems - // with static initialization and to avoid multiple allocations. - struct CommandLineFlagLocks { - absl::Mutex primary_mu; // protects several fields in CommandLineFlag - absl::Mutex callback_mu; // used to serialize callbacks - }; - CommandLineFlagLocks* locks_ = nullptr; // locks, laziliy allocated. + struct CallbackData { + FlagCallback func; + absl::Mutex guard; // Guard for concurrent callback invocations. + }; + CallbackData* callback_data_ ABSL_GUARDED_BY(*DataGuard()) = nullptr; + // This is reserved space for an absl::Mutex to guard flag data. It will be + // initialized in FlagImpl::Init via placement new. + // We can't use "absl::Mutex data_guard_", since this class is not literal. + // We do not want to use "absl::Mutex* data_guard_", since this would require + // heap allocation during initialization, which is both slows program startup + // and can fail. Using reserved space + placement new allows us to avoid both + // problems. + alignas(absl::Mutex) mutable char data_guard_[sizeof(absl::Mutex)]; }; // This is "unspecified" implementation of absl::Flag type. @@ -354,7 +357,7 @@ class Flag final : public flags_internal::CommandLineFlag { private: friend class FlagState; - void Destroy() const override { impl_.Destroy(); } + void Destroy() override { impl_.Destroy(); } void Read(void* dst) const override { impl_.Read(*this, dst, &flags_internal::FlagOps); @@ -414,6 +417,7 @@ T* MakeFromDefaultValue(EmptyBraces) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_FLAG_H_ diff --git a/absl/flags/internal/parse.h b/absl/flags/internal/parse.h index fd3aca61..e534635b 100644 --- a/absl/flags/internal/parse.h +++ b/absl/flags/internal/parse.h @@ -27,6 +27,7 @@ ABSL_DECLARE_FLAG(std::vector, tryfromenv); ABSL_DECLARE_FLAG(std::vector, undefok); namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { enum class ArgvListAction { kRemoveParsedArgs, kKeepParsedArgs }; @@ -43,6 +44,7 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], OnUndefinedFlag on_undef_flag); } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_PARSE_H_ diff --git a/absl/flags/internal/path_util.h b/absl/flags/internal/path_util.h index 5615c0e6..41696377 100644 --- a/absl/flags/internal/path_util.h +++ b/absl/flags/internal/path_util.h @@ -20,6 +20,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // A portable interface that returns the basename of the filename passed as an @@ -55,6 +56,7 @@ inline absl::string_view Package(absl::string_view filename) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ diff --git a/absl/flags/internal/program_name.cc b/absl/flags/internal/program_name.cc index f0811f14..df0c3309 100644 --- a/absl/flags/internal/program_name.cc +++ b/absl/flags/internal/program_name.cc @@ -21,6 +21,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { ABSL_CONST_INIT static absl::Mutex program_name_guard(absl::kConstInit); @@ -50,4 +51,5 @@ void SetProgramInvocationName(absl::string_view prog_name_str) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/program_name.h b/absl/flags/internal/program_name.h index 326f24bb..317a7c5c 100644 --- a/absl/flags/internal/program_name.h +++ b/absl/flags/internal/program_name.h @@ -24,6 +24,7 @@ // Program name namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // Returns program invocation name or "UNKNOWN" if `SetProgramInvocationName()` @@ -42,6 +43,7 @@ std::string ShortProgramInvocationName(); void SetProgramInvocationName(absl::string_view prog_name_str); } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_PROGRAM_NAME_H_ diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc index 52d9f3c7..5eae933c 100644 --- a/absl/flags/internal/registry.cc +++ b/absl/flags/internal/registry.cc @@ -30,6 +30,7 @@ // set it. namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // -------------------------------------------------------------------- @@ -281,7 +282,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag { op_(ops) {} private: - void Destroy() const override { + void Destroy() override { // Values are heap allocated for Retired Flags. delete this; } @@ -336,4 +337,5 @@ bool IsRetiredFlag(absl::string_view name, bool* type_is_bool) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h index 1889f3a0..d2145a8a 100644 --- a/absl/flags/internal/registry.h +++ b/absl/flags/internal/registry.h @@ -27,6 +27,7 @@ // Global flags registry API. namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { CommandLineFlag* FindCommandLineFlag(absl::string_view name); @@ -115,6 +116,7 @@ class FlagSaver { }; } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_REGISTRY_H_ diff --git a/absl/flags/internal/type_erased.cc b/absl/flags/internal/type_erased.cc index a1650fcf..7910db8f 100644 --- a/absl/flags/internal/type_erased.cc +++ b/absl/flags/internal/type_erased.cc @@ -21,6 +21,7 @@ #include "absl/strings/str_cat.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { bool GetCommandLineOption(absl::string_view name, std::string* value) { @@ -79,4 +80,5 @@ bool SpecifiedOnCommandLine(absl::string_view name) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/type_erased.h b/absl/flags/internal/type_erased.h index a9551166..6cbd84cd 100644 --- a/absl/flags/internal/type_erased.h +++ b/absl/flags/internal/type_erased.h @@ -25,6 +25,7 @@ // Registry interfaces operating on type erased handles. namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // If a flag named "name" exists, store its current value in *OUTPUT @@ -81,6 +82,7 @@ inline bool GetByName(absl::string_view name, T* dst) { } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_ diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc index dc12e32f..4602c019 100644 --- a/absl/flags/internal/usage.cc +++ b/absl/flags/internal/usage.cc @@ -44,6 +44,7 @@ ABSL_FLAG(std::string, helpmatch, "", "show help on modules whose name contains the specified substr"); namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { @@ -404,4 +405,5 @@ int HandleUsageFlags(std::ostream& out, } } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/usage.h b/absl/flags/internal/usage.h index 76075b08..5e8ca6a1 100644 --- a/absl/flags/internal/usage.h +++ b/absl/flags/internal/usage.h @@ -27,6 +27,7 @@ // Usage reporting interfaces namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // The format to report the help messages in. @@ -64,6 +65,7 @@ int HandleUsageFlags(std::ostream& out, absl::string_view program_usage_message); } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl ABSL_DECLARE_FLAG(bool, help); diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc index f4ebe0e3..87020a27 100644 --- a/absl/flags/marshalling.cc +++ b/absl/flags/marshalling.cc @@ -27,6 +27,7 @@ #include "absl/strings/str_split.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // -------------------------------------------------------------------- @@ -226,4 +227,5 @@ std::string AbslUnparseFlag(absl::LogSeverity v) { return absl::UnparseFlag(static_cast(v)); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/marshalling.h b/absl/flags/marshalling.h index 6d391804..b9fca752 100644 --- a/absl/flags/marshalling.h +++ b/absl/flags/marshalling.h @@ -168,6 +168,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types. @@ -256,6 +257,7 @@ enum class LogSeverity : int; bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*); std::string AbslUnparseFlag(absl::LogSeverity); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_MARSHALLING_H_ diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 16c4d68f..5a56a356 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -38,6 +38,7 @@ // -------------------------------------------------------------------- namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { @@ -52,6 +53,7 @@ ABSL_CONST_INIT bool tryfromenv_needs_processing } // namespace } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl ABSL_FLAG(std::vector, flagfile, {}, @@ -109,6 +111,7 @@ ABSL_FLAG(std::vector, undefok, {}, "with that name"); namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { @@ -748,4 +751,5 @@ std::vector ParseCommandLine(int argc, char* argv[]) { flags_internal::OnUndefinedFlag::kAbortIfUndefined); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/parse.h b/absl/flags/parse.h index dbb75101..871fc993 100644 --- a/absl/flags/parse.h +++ b/absl/flags/parse.h @@ -29,6 +29,7 @@ #include "absl/flags/internal/parse.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ParseCommandLine() // @@ -53,6 +54,7 @@ namespace absl { // help messages and then exits the program. std::vector ParseCommandLine(int argc, char* argv[]); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_PARSE_H_ diff --git a/absl/flags/usage.cc b/absl/flags/usage.cc index dff7a3da..60459bc2 100644 --- a/absl/flags/usage.cc +++ b/absl/flags/usage.cc @@ -20,6 +20,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { ABSL_CONST_INIT absl::Mutex usage_message_guard(absl::kConstInit); @@ -53,4 +54,5 @@ absl::string_view ProgramUsageMessage() { : "Warning: SetProgramUsageMessage() never called"; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/usage.h b/absl/flags/usage.h index 3a121071..299e5c34 100644 --- a/absl/flags/usage.h +++ b/absl/flags/usage.h @@ -22,6 +22,7 @@ // Usage reporting interfaces namespace absl { +ABSL_NAMESPACE_BEGIN // Sets the "usage" message to be used by help reporting routines. // For example: @@ -35,6 +36,7 @@ void SetProgramUsageMessage(absl::string_view new_usage_message); // Returns the usage message set by SetProgramUsageMessage(). absl::string_view ProgramUsageMessage(); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FLAGS_USAGE_H_ diff --git a/absl/flags/usage_config.cc b/absl/flags/usage_config.cc index f97bf300..21a2dd01 100644 --- a/absl/flags/usage_config.cc +++ b/absl/flags/usage_config.cc @@ -34,6 +34,7 @@ ABSL_ATTRIBUTE_WEAK void AbslInternalReportFatalUsageError(absl::string_view) {} } // extern "C" namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { @@ -149,4 +150,5 @@ void SetFlagsUsageConfig(FlagsUsageConfig usage_config) { flags_internal::custom_usage_config = new FlagsUsageConfig(usage_config); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/usage_config.h b/absl/flags/usage_config.h index bfd0eedb..e6428e0a 100644 --- a/absl/flags/usage_config.h +++ b/absl/flags/usage_config.h @@ -54,6 +54,7 @@ // Shows help on modules whose name contains the specified substring namespace absl { +ABSL_NAMESPACE_BEGIN namespace flags_internal { using FlagKindFilter = std::function; @@ -118,6 +119,7 @@ FlagsUsageConfig GetUsageConfig(); void ReportUsageError(absl::string_view msg, bool is_fatal); } // namespace flags_internal +ABSL_NAMESPACE_END } // namespace absl extern "C" { diff --git a/absl/functional/function_ref.h b/absl/functional/function_ref.h index 42d9f16f..370acc55 100644 --- a/absl/functional/function_ref.h +++ b/absl/functional/function_ref.h @@ -54,6 +54,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN // FunctionRef // @@ -132,6 +133,7 @@ class FunctionRef { absl::functional_internal::Invoker invoker_; }; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FUNCTIONAL_FUNCTION_REF_H_ diff --git a/absl/functional/function_ref_benchmark.cc b/absl/functional/function_ref_benchmark.cc index f6dfcabf..045305bf 100644 --- a/absl/functional/function_ref_benchmark.cc +++ b/absl/functional/function_ref_benchmark.cc @@ -20,6 +20,7 @@ #include "absl/base/attributes.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { int dummy = 0; @@ -137,4 +138,5 @@ void BM_NonTrivialArgsFunctionRef(benchmark::State& state) { BENCHMARK(BM_NonTrivialArgsFunctionRef); } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/functional/function_ref_test.cc b/absl/functional/function_ref_test.cc index 90829db0..3aa59745 100644 --- a/absl/functional/function_ref_test.cc +++ b/absl/functional/function_ref_test.cc @@ -22,6 +22,7 @@ #include "absl/memory/memory.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { void RunFun(FunctionRef f) { f(); } @@ -252,4 +253,5 @@ TEST(FunctionRef, PassByValueTypes) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/functional/internal/function_ref.h b/absl/functional/internal/function_ref.h index fcb0496c..d1575054 100644 --- a/absl/functional/internal/function_ref.h +++ b/absl/functional/internal/function_ref.h @@ -23,6 +23,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace functional_internal { // Like a void* that can handle function pointers as well. The standard does not @@ -99,6 +100,7 @@ template using EnableIf = typename ::std::enable_if::type; } // namespace functional_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_ diff --git a/absl/hash/hash.h b/absl/hash/hash.h index 297dc9cb..23a65ea8 100644 --- a/absl/hash/hash.h +++ b/absl/hash/hash.h @@ -73,6 +73,7 @@ #include "absl/hash/internal/hash.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // `absl::Hash` @@ -317,6 +318,7 @@ class HashState : public hash_internal::HashStateBase { void (*combine_contiguous_)(void*, const unsigned char*, size_t); }; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HASH_HASH_H_ diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index 42c0c3d8..7a9d57f7 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -681,6 +681,7 @@ H AbslHashValue(H state, CustomHashType t) { } // namespace namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { template struct is_uniquely_represented< @@ -688,6 +689,7 @@ struct is_uniquely_represented< typename EnableIfContained::type> : std::true_type {}; } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ diff --git a/absl/hash/hash_testing.h b/absl/hash/hash_testing.h index c45bc154..1e1c5741 100644 --- a/absl/hash/hash_testing.h +++ b/absl/hash/hash_testing.h @@ -28,6 +28,7 @@ #include "absl/types/variant.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Run the absl::Hash algorithm over all the elements passed in and verify that // their hash expansion is congruent with their `==` operator. @@ -371,6 +372,7 @@ VerifyTypeImplementsAbslHashCorrectly(std::initializer_list values, equals); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HASH_HASH_TESTING_H_ diff --git a/absl/hash/internal/city.cc b/absl/hash/internal/city.cc index dc7650a7..e122c184 100644 --- a/absl/hash/internal/city.cc +++ b/absl/hash/internal/city.cc @@ -30,6 +30,7 @@ #include "absl/base/optimization.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { #ifdef ABSL_IS_BIG_ENDIAN @@ -341,4 +342,5 @@ uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, } } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/hash/internal/city.h b/absl/hash/internal/city.h index b43d3407..161c7748 100644 --- a/absl/hash/internal/city.h +++ b/absl/hash/internal/city.h @@ -47,9 +47,13 @@ #include #include // for size_t. + #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { typedef std::pair uint128; @@ -86,6 +90,7 @@ inline uint64_t Hash128to64(const uint128 &x) { } } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HASH_INTERNAL_CITY_H_ diff --git a/absl/hash/internal/city_test.cc b/absl/hash/internal/city_test.cc index 71b4ecce..251d381d 100644 --- a/absl/hash/internal/city_test.cc +++ b/absl/hash/internal/city_test.cc @@ -20,6 +20,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; @@ -590,4 +591,5 @@ TEST(CityHashTest, Unchanging) { } } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/hash/internal/hash.cc b/absl/hash/internal/hash.cc index c17f3be1..b44ecb3a 100644 --- a/absl/hash/internal/hash.cc +++ b/absl/hash/internal/hash.cc @@ -15,6 +15,7 @@ #include "absl/hash/internal/hash.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { uint64_t CityHashState::CombineLargeContiguousImpl32(uint64_t state, @@ -50,4 +51,5 @@ uint64_t CityHashState::CombineLargeContiguousImpl64(uint64_t state, ABSL_CONST_INIT const void* const CityHashState::kSeed = &kSeed; } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index 7d44f57d..2564978a 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h @@ -50,6 +50,7 @@ #include "absl/hash/internal/city.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { class PiecewiseCombiner; @@ -980,6 +981,7 @@ H PiecewiseCombiner::finalize(H state) { } } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HASH_INTERNAL_HASH_H_ diff --git a/absl/hash/internal/spy_hash_state.h b/absl/hash/internal/spy_hash_state.h index 05c7cafe..c0831208 100644 --- a/absl/hash/internal/spy_hash_state.h +++ b/absl/hash/internal/spy_hash_state.h @@ -25,6 +25,7 @@ #include "absl/strings/str_join.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace hash_internal { // SpyHashState is an implementation of the HashState API that simply @@ -224,6 +225,7 @@ void AbslHashValue(SpyHashStateImpl, const U&); using SpyHashState = SpyHashStateImpl; } // namespace hash_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HASH_INTERNAL_SPY_HASH_STATE_H_ diff --git a/absl/memory/memory.h b/absl/memory/memory.h index 243a5dda..513f7103 100644 --- a/absl/memory/memory.h +++ b/absl/memory/memory.h @@ -34,6 +34,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // Function Template: WrapUnique() @@ -688,6 +689,7 @@ void CopyRange(Allocator& alloc, Iterator destination, InputIterator first, } } } // namespace memory_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_MEMORY_MEMORY_H_ diff --git a/absl/memory/memory_exception_safety_test.cc b/absl/memory/memory_exception_safety_test.cc index 729507e9..c0910dc7 100644 --- a/absl/memory/memory_exception_safety_test.cc +++ b/absl/memory/memory_exception_safety_test.cc @@ -22,6 +22,7 @@ #include "absl/base/internal/exception_safety_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { constexpr int kLength = 50; @@ -53,6 +54,7 @@ TEST(MakeUnique, CheckForLeaks) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_HAVE_EXCEPTIONS diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index 8cd5f043..ba87d2f0 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h @@ -48,6 +48,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN // Defined and documented later on in this file. template @@ -752,6 +753,7 @@ using swap_internal::Swap; using swap_internal::StdSwapIsUnconstrained; } // namespace type_traits_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_META_TYPE_TRAITS_H_ diff --git a/absl/numeric/int128.cc b/absl/numeric/int128.cc index 1eba09de..a20a77e7 100644 --- a/absl/numeric/int128.cc +++ b/absl/numeric/int128.cc @@ -23,6 +23,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN const uint128 kuint128max = MakeUint128(std::numeric_limits::max(), std::numeric_limits::max()); @@ -349,6 +350,7 @@ std::ostream& operator<<(std::ostream& os, int128 v) { return os << rep; } +ABSL_NAMESPACE_END } // namespace absl namespace std { diff --git a/absl/numeric/int128.h b/absl/numeric/int128.h index 50617612..718f70b1 100644 --- a/absl/numeric/int128.h +++ b/absl/numeric/int128.h @@ -49,6 +49,7 @@ #endif // defined(_MSC_VER) namespace absl { +ABSL_NAMESPACE_BEGIN class int128; @@ -245,6 +246,7 @@ constexpr uint128 Uint128Max() { (std::numeric_limits::max)()); } +ABSL_NAMESPACE_END } // namespace absl // Specialized numeric_limits for uint128. @@ -293,6 +295,7 @@ class numeric_limits { } // namespace std namespace absl { +ABSL_NAMESPACE_BEGIN // int128 // @@ -478,6 +481,7 @@ constexpr int128 Int128Min() { return int128((std::numeric_limits::min)(), 0); } +ABSL_NAMESPACE_END } // namespace absl // Specialized numeric_limits for int128. @@ -529,6 +533,7 @@ class numeric_limits { // Implementation details follow // -------------------------------------------------------------------------- namespace absl { +ABSL_NAMESPACE_BEGIN constexpr uint128 MakeUint128(uint64_t high, uint64_t low) { return uint128(high, low); @@ -1078,6 +1083,7 @@ constexpr int64_t BitCastToSigned(uint64_t v) { #include "absl/numeric/int128_no_intrinsic.inc" // IWYU pragma: export #endif // ABSL_HAVE_INTRINSIC_INT128 +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_INTERNAL_WCHAR_T diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index 13e96357..46dbc3ef 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt @@ -526,6 +526,8 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config ) # Internal-only target, do not depend on directly. @@ -559,6 +561,8 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config ) # Internal-only target, do not depend on directly. @@ -618,6 +622,8 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config TESTONLY ) @@ -631,6 +637,8 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config TESTONLY ) @@ -782,6 +790,8 @@ absl_cc_library( ${ABSL_DEFAULT_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config ) # Internal-only target, do not depend on directly. @@ -816,6 +826,7 @@ absl_cc_library( ${ABSL_DEFAULT_LINKOPTS} DEPS absl::random_internal_platform + absl::config ) # Internal-only target, do not depend on directly. @@ -835,6 +846,7 @@ absl_cc_library( DEPS absl::random_internal_platform absl::random_internal_randen_hwaes_impl + absl::config ) # Internal-only target, do not depend on directly. @@ -851,6 +863,7 @@ absl_cc_library( ${ABSL_DEFAULT_LINKOPTS} DEPS absl::random_internal_platform + absl::config ) # Internal-only target, do not depend on directly. @@ -868,6 +881,7 @@ absl_cc_library( LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS + absl::config absl::core_headers absl::raw_logging_internal absl::strings diff --git a/absl/random/bernoulli_distribution.h b/absl/random/bernoulli_distribution.h index 326fcb6e..25bd0d5c 100644 --- a/absl/random/bernoulli_distribution.h +++ b/absl/random/bernoulli_distribution.h @@ -24,6 +24,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::bernoulli_distribution is a drop in replacement for // std::bernoulli_distribution. It guarantees that (given a perfect @@ -193,6 +194,7 @@ bool bernoulli_distribution::Generate(double p, } } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_BERNOULLI_DISTRIBUTION_H_ diff --git a/absl/random/beta_distribution.h b/absl/random/beta_distribution.h index b09b02f0..c154066f 100644 --- a/absl/random/beta_distribution.h +++ b/absl/random/beta_distribution.h @@ -29,6 +29,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::beta_distribution: // Generate a floating-point variate conforming to a Beta distribution: @@ -420,6 +421,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_BETA_DISTRIBUTION_H_ diff --git a/absl/random/bit_gen_ref.h b/absl/random/bit_gen_ref.h index 00e904f8..e8771162 100644 --- a/absl/random/bit_gen_ref.h +++ b/absl/random/bit_gen_ref.h @@ -31,6 +31,7 @@ #include "absl/random/internal/mocking_bit_gen_base.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { template @@ -146,6 +147,7 @@ struct DistributionCaller { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_BIT_GEN_REF_H_ diff --git a/absl/random/bit_gen_ref_test.cc b/absl/random/bit_gen_ref_test.cc index bc02ca5c..ca0e4d70 100644 --- a/absl/random/bit_gen_ref_test.cc +++ b/absl/random/bit_gen_ref_test.cc @@ -21,6 +21,7 @@ #include "absl/random/random.h" namespace absl { +ABSL_NAMESPACE_BEGIN class ConstBitGen : public absl::random_internal::MockingBitGenBase { bool CallImpl(const std::type_info&, void*, void* result) override { @@ -96,4 +97,5 @@ TEST(BitGenRefTest, MockingBitGenBaseOverrides) { EXPECT_EQ(FnTest(gen_ref), 42); // Copy } } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/discrete_distribution.cc b/absl/random/discrete_distribution.cc index e6c09c51..081accee 100644 --- a/absl/random/discrete_distribution.cc +++ b/absl/random/discrete_distribution.cc @@ -15,6 +15,7 @@ #include "absl/random/discrete_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Initializes the distribution table for Walker's Aliasing algorithm, described @@ -93,4 +94,5 @@ std::vector> InitDiscreteDistribution( } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/discrete_distribution.h b/absl/random/discrete_distribution.h index 1560f03c..171aa11a 100644 --- a/absl/random/discrete_distribution.h +++ b/absl/random/discrete_distribution.h @@ -29,6 +29,7 @@ #include "absl/random/uniform_int_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::discrete_distribution // @@ -240,6 +241,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_ diff --git a/absl/random/distribution_format_traits.h b/absl/random/distribution_format_traits.h index f9f07058..22b358cc 100644 --- a/absl/random/distribution_format_traits.h +++ b/absl/random/distribution_format_traits.h @@ -36,6 +36,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN struct IntervalClosedClosedTag; struct IntervalClosedOpenTag; @@ -271,6 +272,7 @@ struct DistributionFormatTraits> { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_DISTRIBUTION_FORMAT_TRAITS_H_ diff --git a/absl/random/distributions.h b/absl/random/distributions.h index 6ced6061..c1fb6650 100644 --- a/absl/random/distributions.h +++ b/absl/random/distributions.h @@ -67,6 +67,7 @@ #include "absl/random/zipf_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN ABSL_INTERNAL_INLINE_CONSTEXPR(IntervalClosedClosedTag, IntervalClosedClosed, {}); @@ -458,6 +459,7 @@ IntType Zipf(URBG&& urbg, // NOLINT(runtime/references) distribution_t, format_t>(&urbg, hi, q, v); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_DISTRIBUTIONS_H_ diff --git a/absl/random/exponential_distribution.h b/absl/random/exponential_distribution.h index 24abf57e..b5caf8a1 100644 --- a/absl/random/exponential_distribution.h +++ b/absl/random/exponential_distribution.h @@ -27,6 +27,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::exponential_distribution: // Generates a number conforming to an exponential distribution and is @@ -158,6 +159,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_ diff --git a/absl/random/gaussian_distribution.cc b/absl/random/gaussian_distribution.cc index 5dd84619..c7a72cb2 100644 --- a/absl/random/gaussian_distribution.cc +++ b/absl/random/gaussian_distribution.cc @@ -4,6 +4,7 @@ #include "absl/random/gaussian_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { const gaussian_distribution_base::Tables @@ -96,6 +97,7 @@ const gaussian_distribution_base::Tables 0.9362826816850632339, 0.9635996931270905952, 1}}; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl // clang-format on diff --git a/absl/random/gaussian_distribution.h b/absl/random/gaussian_distribution.h index c299e944..c1427b06 100644 --- a/absl/random/gaussian_distribution.h +++ b/absl/random/gaussian_distribution.h @@ -33,6 +33,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // absl::gaussian_distribution_base implements the underlying ziggurat algorithm @@ -267,6 +268,7 @@ inline double gaussian_distribution_base::zignor( } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_GAUSSIAN_DISTRIBUTION_H_ diff --git a/absl/random/internal/BUILD.bazel b/absl/random/internal/BUILD.bazel index 952929ea..d7ad4efe 100644 --- a/absl/random/internal/BUILD.bazel +++ b/absl/random/internal/BUILD.bazel @@ -51,6 +51,7 @@ cc_library( visibility = [ "//absl/random:__pkg__", ], + deps = ["//absl/base:config"], ) cc_library( @@ -78,6 +79,7 @@ cc_library( visibility = [ "//absl/random:__pkg__", ], + deps = ["//absl/base:config"], ) cc_library( @@ -138,6 +140,7 @@ cc_library( ], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = ["//absl/base:config"], ) cc_library( @@ -148,6 +151,7 @@ cc_library( ], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + deps = ["//absl/base:config"], ) cc_library( @@ -269,6 +273,7 @@ cc_library( "randen-keys.inc", "platform.h", ], + deps = ["//absl/base:config"], ) cc_library( @@ -297,6 +302,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":platform", + "//absl/base:config", "//absl/base:core_headers", ], ) @@ -317,6 +323,7 @@ cc_library( deps = [ ":platform", ":randen_hwaes_impl", + "//absl/base:config", ], ) @@ -338,6 +345,7 @@ cc_library( linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":platform", + "//absl/base:config", "//absl/base:core_headers", ], ) @@ -368,6 +376,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + "//absl/base:config", "//absl/base:core_headers", "//absl/base:raw_logging_internal", "//absl/strings", diff --git a/absl/random/internal/chi_square.cc b/absl/random/internal/chi_square.cc index c0acc947..640d48ce 100644 --- a/absl/random/internal/chi_square.cc +++ b/absl/random/internal/chi_square.cc @@ -19,6 +19,7 @@ #include "absl/random/internal/distribution_test_util.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -227,4 +228,5 @@ double ChiSquarePValue(double chi_square, int dof) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/chi_square.h b/absl/random/internal/chi_square.h index fa8646f2..07f4fbe5 100644 --- a/absl/random/internal/chi_square.h +++ b/absl/random/internal/chi_square.h @@ -26,7 +26,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { constexpr const char kChiSquared[] = "chi-squared"; @@ -80,6 +83,7 @@ double ChiSquareValue(int dof, double p); double ChiSquarePValue(double chi_square, int dof); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ diff --git a/absl/random/internal/distribution_caller.h b/absl/random/internal/distribution_caller.h index 0318e1f8..02603cf8 100644 --- a/absl/random/internal/distribution_caller.h +++ b/absl/random/internal/distribution_caller.h @@ -19,7 +19,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // DistributionCaller provides an opportunity to overload the general @@ -51,6 +54,7 @@ struct DistributionCaller { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_ diff --git a/absl/random/internal/distribution_test_util.cc b/absl/random/internal/distribution_test_util.cc index 85c8d596..e9005658 100644 --- a/absl/random/internal/distribution_test_util.cc +++ b/absl/random/internal/distribution_test_util.cc @@ -25,6 +25,7 @@ #include "absl/strings/str_format.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -413,4 +414,5 @@ double MaxErrorTolerance(double acceptance_probability) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/distribution_test_util.h b/absl/random/internal/distribution_test_util.h index b5ba49fa..6d94cf6c 100644 --- a/absl/random/internal/distribution_test_util.h +++ b/absl/random/internal/distribution_test_util.h @@ -26,6 +26,7 @@ // non-test code. namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // http://webspace.ship.edu/pgmarr/Geo441/Lectures/Lec%205%20-%20Normality%20Testing.pdf @@ -106,6 +107,7 @@ double BetaIncomplete(double x, double p, double q); double BetaIncompleteInv(double p, double q, double alpha); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTION_TEST_UTIL_H_ diff --git a/absl/random/internal/distributions.h b/absl/random/internal/distributions.h index c8cec02b..d7e3c016 100644 --- a/absl/random/internal/distributions.h +++ b/absl/random/internal/distributions.h @@ -23,6 +23,7 @@ #include "absl/random/internal/uniform_helper.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // In the absence of an explicitly provided return-type, the template @@ -45,6 +46,7 @@ using uniform_inferred_return_t = is_widening_convertible::value, B, A>::type>; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ diff --git a/absl/random/internal/explicit_seed_seq.h b/absl/random/internal/explicit_seed_seq.h index b660ece5..6a743eaf 100644 --- a/absl/random/internal/explicit_seed_seq.h +++ b/absl/random/internal/explicit_seed_seq.h @@ -22,7 +22,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // This class conforms to the C++ Standard "Seed Sequence" concept @@ -82,6 +85,7 @@ class ExplicitSeedSeq { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_ diff --git a/absl/random/internal/fast_uniform_bits.h b/absl/random/internal/fast_uniform_bits.h index e8df92f3..f13c8729 100644 --- a/absl/random/internal/fast_uniform_bits.h +++ b/absl/random/internal/fast_uniform_bits.h @@ -20,7 +20,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Returns true if the input value is zero or a power of two. Useful for // determining if the range of output values in a URBG @@ -255,6 +258,7 @@ FastUniformBits::Generate(URBG& g, // NOLINT(runtime/references) } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_FAST_UNIFORM_BITS_H_ diff --git a/absl/random/internal/fast_uniform_bits_test.cc b/absl/random/internal/fast_uniform_bits_test.cc index 9f2e8268..f5b837e5 100644 --- a/absl/random/internal/fast_uniform_bits_test.cc +++ b/absl/random/internal/fast_uniform_bits_test.cc @@ -19,6 +19,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -269,4 +270,5 @@ TEST(FastUniformBitsTest, URBG32bitRegression) { } // namespace } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/fastmath.h b/absl/random/internal/fastmath.h index 4bd18410..6baeb5a7 100644 --- a/absl/random/internal/fastmath.h +++ b/absl/random/internal/fastmath.h @@ -25,6 +25,7 @@ #include "absl/base/internal/bits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Returns the position of the first bit set. @@ -67,6 +68,7 @@ inline constexpr uint64_t rotr(uint64_t value, uint8_t bits) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_FASTMATH_H_ diff --git a/absl/random/internal/gaussian_distribution_gentables.cc b/absl/random/internal/gaussian_distribution_gentables.cc index 16a23cb2..a2bf0394 100644 --- a/absl/random/internal/gaussian_distribution_gentables.cc +++ b/absl/random/internal/gaussian_distribution_gentables.cc @@ -27,6 +27,7 @@ #include "absl/base/macros.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -135,6 +136,7 @@ void TableGenerator::Print(std::ostream* os) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl int main(int, char**) { diff --git a/absl/random/internal/generate_real.h b/absl/random/internal/generate_real.h index 246d863e..20f6d208 100644 --- a/absl/random/internal/generate_real.h +++ b/absl/random/internal/generate_real.h @@ -29,6 +29,7 @@ #include "absl/random/internal/traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Tristate tag types controlling the output of GenerateRealFromBits. @@ -139,6 +140,7 @@ inline RealType GenerateRealFromBits(uint64_t bits, int exp_bias = 0) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_GENERATE_REAL_H_ diff --git a/absl/random/internal/iostream_state_saver.h b/absl/random/internal/iostream_state_saver.h index df88fa76..7378829a 100644 --- a/absl/random/internal/iostream_state_saver.h +++ b/absl/random/internal/iostream_state_saver.h @@ -24,6 +24,7 @@ #include "absl/numeric/int128.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // The null_state_saver does nothing. @@ -238,6 +239,7 @@ inline FloatType read_floating_point(IStream& is) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_IOSTREAM_STATE_SAVER_H_ diff --git a/absl/random/internal/mock_overload_set.h b/absl/random/internal/mock_overload_set.h index 539313d7..c2a30d89 100644 --- a/absl/random/internal/mock_overload_set.h +++ b/absl/random/internal/mock_overload_set.h @@ -23,6 +23,7 @@ #include "absl/random/mocking_bit_gen.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { template @@ -85,5 +86,6 @@ struct MockOverloadSet }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_ diff --git a/absl/random/internal/mocking_bit_gen_base.h b/absl/random/internal/mocking_bit_gen_base.h index aff2ba6d..eeeae9d2 100644 --- a/absl/random/internal/mocking_bit_gen_base.h +++ b/absl/random/internal/mocking_bit_gen_base.h @@ -25,6 +25,7 @@ #include "absl/strings/str_cat.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // MockingBitGenExpectationFormatter is invoked to format unsatisfied mocks @@ -113,6 +114,7 @@ class MockingBitGenBase { }; // namespace random_internal } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_MOCKING_BIT_GEN_BASE_H_ diff --git a/absl/random/internal/nanobenchmark.cc b/absl/random/internal/nanobenchmark.cc index feb81c85..8fee77fc 100644 --- a/absl/random/internal/nanobenchmark.cc +++ b/absl/random/internal/nanobenchmark.cc @@ -70,6 +70,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal_nanobenchmark { namespace { @@ -799,4 +800,5 @@ size_t Measure(const Func func, const void* arg, const FuncInput* inputs, } } // namespace random_internal_nanobenchmark +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/nanobenchmark.h b/absl/random/internal/nanobenchmark.h index c2b650d1..a5097ba2 100644 --- a/absl/random/internal/nanobenchmark.h +++ b/absl/random/internal/nanobenchmark.h @@ -50,7 +50,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal_nanobenchmark { // Input influencing the function being measured (e.g. number of bytes to copy). @@ -163,6 +166,7 @@ static inline size_t MeasureClosure(const Closure& closure, } } // namespace random_internal_nanobenchmark +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_NANOBENCHMARK_H_ diff --git a/absl/random/internal/nanobenchmark_test.cc b/absl/random/internal/nanobenchmark_test.cc index 383345a8..ab824ef5 100644 --- a/absl/random/internal/nanobenchmark_test.cc +++ b/absl/random/internal/nanobenchmark_test.cc @@ -18,6 +18,7 @@ #include "absl/strings/numbers.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal_nanobenchmark { namespace { @@ -67,6 +68,7 @@ void RunAll(const int argc, char* argv[]) { } // namespace } // namespace random_internal_nanobenchmark +ABSL_NAMESPACE_END } // namespace absl int main(int argc, char* argv[]) { diff --git a/absl/random/internal/nonsecure_base.h b/absl/random/internal/nonsecure_base.h index 8847e74b..730fa2ea 100644 --- a/absl/random/internal/nonsecure_base.h +++ b/absl/random/internal/nonsecure_base.h @@ -33,6 +33,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Each instance of NonsecureURBGBase will be seeded by variates produced @@ -143,6 +144,7 @@ class NonsecureURBGBase { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_ diff --git a/absl/random/internal/pcg_engine.h b/absl/random/internal/pcg_engine.h index b5df4eaf..53c23fe1 100644 --- a/absl/random/internal/pcg_engine.h +++ b/absl/random/internal/pcg_engine.h @@ -24,6 +24,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // pcg_engine is a simplified implementation of Melissa O'Neil's PCG engine in @@ -300,6 +301,7 @@ using pcg32_2018_engine = pcg_engine< random_internal::pcg_xsh_rr_64_32>; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_PCG_ENGINE_H_ diff --git a/absl/random/internal/pool_urbg.cc b/absl/random/internal/pool_urbg.cc index f2e1c1f6..5bee5307 100644 --- a/absl/random/internal/pool_urbg.cc +++ b/absl/random/internal/pool_urbg.cc @@ -37,6 +37,7 @@ using absl::base_internal::SpinLock; using absl::base_internal::SpinLockHolder; namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -249,4 +250,5 @@ template class RandenPool; template class RandenPool; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/pool_urbg.h b/absl/random/internal/pool_urbg.h index 9b2dd4bf..05721929 100644 --- a/absl/random/internal/pool_urbg.h +++ b/absl/random/internal/pool_urbg.h @@ -22,6 +22,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // RandenPool is a thread-safe random number generator [random.req.urbg] that @@ -124,6 +125,7 @@ class PoolURBG { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_POOL_URBG_H_ diff --git a/absl/random/internal/randen.cc b/absl/random/internal/randen.cc index bab8075a..78a1e00c 100644 --- a/absl/random/internal/randen.cc +++ b/absl/random/internal/randen.cc @@ -41,6 +41,7 @@ // structured/low-entropy counters to digits of Pi. namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -86,4 +87,5 @@ Randen::Randen() { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/randen.h b/absl/random/internal/randen.h index a4ff2545..c2834aaf 100644 --- a/absl/random/internal/randen.h +++ b/absl/random/internal/randen.h @@ -23,6 +23,7 @@ #include "absl/random/internal/randen_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // RANDen = RANDom generator or beetroots in Swiss German. @@ -95,6 +96,7 @@ class Randen { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_H_ diff --git a/absl/random/internal/randen_detect.cc b/absl/random/internal/randen_detect.cc index d5946b21..d63230c2 100644 --- a/absl/random/internal/randen_detect.cc +++ b/absl/random/internal/randen_detect.cc @@ -95,6 +95,7 @@ static uint32_t GetAuxval(uint32_t hwcap_type) { #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // The default return at the end of the function might be unreachable depending @@ -216,4 +217,5 @@ bool CPUSupportsRandenHwAes() { #endif } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/randen_detect.h b/absl/random/internal/randen_detect.h index 44c5c667..f283f432 100644 --- a/absl/random/internal/randen_detect.h +++ b/absl/random/internal/randen_detect.h @@ -15,7 +15,10 @@ #ifndef ABSL_RANDOM_INTERNAL_RANDEN_DETECT_H_ #define ABSL_RANDOM_INTERNAL_RANDEN_DETECT_H_ +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Returns whether the current CPU supports RandenHwAes implementation. @@ -24,6 +27,7 @@ namespace random_internal { bool CPUSupportsRandenHwAes(); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_DETECT_H_ diff --git a/absl/random/internal/randen_engine.h b/absl/random/internal/randen_engine.h index 02212a13..6b337313 100644 --- a/absl/random/internal/randen_engine.h +++ b/absl/random/internal/randen_engine.h @@ -28,6 +28,7 @@ #include "absl/random/internal/randen.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Deterministic pseudorandom byte generator with backtracking resistance @@ -223,6 +224,7 @@ class alignas(16) randen_engine { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_ENGINE_H_ diff --git a/absl/random/internal/randen_hwaes.cc b/absl/random/internal/randen_hwaes.cc index 6cc36fd3..e23844f1 100644 --- a/absl/random/internal/randen_hwaes.cc +++ b/absl/random/internal/randen_hwaes.cc @@ -75,6 +75,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // No accelerated implementation. @@ -106,6 +107,7 @@ void RandenHwAes::Generate(const void*, void*) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #else // defined(ABSL_RANDEN_HWAES_IMPL) @@ -518,6 +520,7 @@ inline ABSL_TARGET_CRYPTO void Permute( } // namespace namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { bool HasRandenHwAesImplementation() { return true; } @@ -629,6 +632,7 @@ void ABSL_TARGET_CRYPTO RandenHwAes::Generate(const void* keys, #endif } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // (ABSL_RANDEN_HWAES_IMPL) diff --git a/absl/random/internal/randen_hwaes.h b/absl/random/internal/randen_hwaes.h index d8e6055f..bce36b52 100644 --- a/absl/random/internal/randen_hwaes.h +++ b/absl/random/internal/randen_hwaes.h @@ -15,12 +15,15 @@ #ifndef ABSL_RANDOM_INTERNAL_RANDEN_HWAES_H_ #define ABSL_RANDOM_INTERNAL_RANDEN_HWAES_H_ +#include "absl/base/config.h" + // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate // symbols from arbitrary system and other headers, since it may be built // with different flags from other targets, using different levels of // optimization, potentially introducing ODR violations. namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // RANDen = RANDom generator or beetroots in Swiss German. @@ -41,6 +44,7 @@ class RandenHwAes { bool HasRandenHwAesImplementation(); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_HWAES_H_ diff --git a/absl/random/internal/randen_slow.cc b/absl/random/internal/randen_slow.cc index e7959c7e..8d074582 100644 --- a/absl/random/internal/randen_slow.cc +++ b/absl/random/internal/randen_slow.cc @@ -462,6 +462,7 @@ inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void Permute( } // namespace namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { const void* RandenSlow::GetKeys() { @@ -501,4 +502,5 @@ void RandenSlow::Generate(const void* keys, void* state_void) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/randen_slow.h b/absl/random/internal/randen_slow.h index 30586130..72f92b54 100644 --- a/absl/random/internal/randen_slow.h +++ b/absl/random/internal/randen_slow.h @@ -17,7 +17,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // RANDen = RANDom generator or beetroots in Swiss German. @@ -38,6 +41,7 @@ class RandenSlow { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_SLOW_H_ diff --git a/absl/random/internal/randen_traits.h b/absl/random/internal/randen_traits.h index 4f1f408d..2b8bbe73 100644 --- a/absl/random/internal/randen_traits.h +++ b/absl/random/internal/randen_traits.h @@ -22,7 +22,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // RANDen = RANDom generator or beetroots in Swiss German. @@ -54,6 +57,7 @@ struct RandenTraits { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_ diff --git a/absl/random/internal/salted_seed_seq.h b/absl/random/internal/salted_seed_seq.h index 86487006..5953a090 100644 --- a/absl/random/internal/salted_seed_seq.h +++ b/absl/random/internal/salted_seed_seq.h @@ -30,6 +30,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // This class conforms to the C++ Standard "Seed Sequence" concept @@ -160,6 +161,7 @@ SaltedSeedSeq::type> MakeSaltedSeedSeq(SSeq&& seq) { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_SALTED_SEED_SEQ_H_ diff --git a/absl/random/internal/seed_material.cc b/absl/random/internal/seed_material.cc index ab4dd0c2..4d38a574 100644 --- a/absl/random/internal/seed_material.cc +++ b/absl/random/internal/seed_material.cc @@ -61,6 +61,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { @@ -214,4 +215,5 @@ absl::optional GetSaltMaterial() { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/internal/seed_material.h b/absl/random/internal/seed_material.h index 57de8a24..4be10e92 100644 --- a/absl/random/internal/seed_material.h +++ b/absl/random/internal/seed_material.h @@ -27,6 +27,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Returns the number of 32-bit blocks needed to contain the given number of @@ -97,6 +98,7 @@ void MixIntoSeedMaterial(absl::Span sequence, absl::optional GetSaltMaterial(); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_ diff --git a/absl/random/internal/sequence_urbg.h b/absl/random/internal/sequence_urbg.h index 9a9b5773..bc96a12c 100644 --- a/absl/random/internal/sequence_urbg.h +++ b/absl/random/internal/sequence_urbg.h @@ -21,7 +21,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // `sequence_urbg` is a simple random number generator which meets the @@ -51,6 +54,7 @@ class sequence_urbg { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_ diff --git a/absl/random/internal/traits.h b/absl/random/internal/traits.h index 40eb011f..75772bd9 100644 --- a/absl/random/internal/traits.h +++ b/absl/random/internal/traits.h @@ -22,6 +22,7 @@ #include "absl/base/config.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // random_internal::is_widening_convertible @@ -94,6 +95,7 @@ struct make_unsigned_bits { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_TRAITS_H_ diff --git a/absl/random/internal/uniform_helper.h b/absl/random/internal/uniform_helper.h index f68b1823..663107cb 100644 --- a/absl/random/internal/uniform_helper.h +++ b/absl/random/internal/uniform_helper.h @@ -22,6 +22,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN template class uniform_int_distribution; @@ -173,6 +174,7 @@ struct UniformDistributionWrapper : public UniformDistribution { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_UNIFORM_HELPER_H_ diff --git a/absl/random/internal/wide_multiply.h b/absl/random/internal/wide_multiply.h index ebbfa1f2..6e4cf1be 100644 --- a/absl/random/internal/wide_multiply.h +++ b/absl/random/internal/wide_multiply.h @@ -31,6 +31,7 @@ #include "absl/random/internal/traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { // Helper object to multiply two 64-bit values to a 128-bit value. @@ -104,6 +105,7 @@ struct wide_multiply { #endif } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_ diff --git a/absl/random/log_uniform_int_distribution.h b/absl/random/log_uniform_int_distribution.h index de58bdbe..960816e2 100644 --- a/absl/random/log_uniform_int_distribution.h +++ b/absl/random/log_uniform_int_distribution.h @@ -30,6 +30,7 @@ #include "absl/random/uniform_int_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN // log_uniform_int_distribution: // @@ -247,6 +248,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_LOG_UNIFORM_INT_DISTRIBUTION_H_ diff --git a/absl/random/mock_distributions.h b/absl/random/mock_distributions.h index 1af98a24..d36d5ba0 100644 --- a/absl/random/mock_distributions.h +++ b/absl/random/mock_distributions.h @@ -53,6 +53,7 @@ #include "absl/random/mocking_bit_gen.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // absl::MockUniform @@ -254,6 +255,7 @@ using MockZipf = IntType(MockingBitGen&, IntType, double, double)>; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_ diff --git a/absl/random/mocking_bit_gen.cc b/absl/random/mocking_bit_gen.cc index 73144528..6bb1e414 100644 --- a/absl/random/mocking_bit_gen.cc +++ b/absl/random/mocking_bit_gen.cc @@ -18,6 +18,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN MockingBitGen::~MockingBitGen() { for (const auto& del : deleters_) { @@ -25,4 +26,5 @@ MockingBitGen::~MockingBitGen() { } } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/mocking_bit_gen.h b/absl/random/mocking_bit_gen.h index d1b524a9..36cef911 100644 --- a/absl/random/mocking_bit_gen.h +++ b/absl/random/mocking_bit_gen.h @@ -51,6 +51,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace random_internal { @@ -189,6 +190,7 @@ struct DistributionCaller { }; } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_MOCKING_BIT_GEN_H_ diff --git a/absl/random/poisson_distribution.h b/absl/random/poisson_distribution.h index 23a953ff..cb5f5d5d 100644 --- a/absl/random/poisson_distribution.h +++ b/absl/random/poisson_distribution.h @@ -28,6 +28,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::poisson_distribution: // Generates discrete variates conforming to a Poisson distribution. @@ -251,6 +252,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_POISSON_DISTRIBUTION_H_ diff --git a/absl/random/random.h b/absl/random/random.h index dc6852f4..c8f326e6 100644 --- a/absl/random/random.h +++ b/absl/random/random.h @@ -41,6 +41,7 @@ #include "absl/random/seed_sequences.h" // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // absl::BitGen @@ -182,6 +183,7 @@ using InsecureBitGen = // discards the intermediate results. // --------------------------------------------------------------------------- +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_RANDOM_H_ diff --git a/absl/random/seed_gen_exception.cc b/absl/random/seed_gen_exception.cc index e4271baa..fdcb54a8 100644 --- a/absl/random/seed_gen_exception.cc +++ b/absl/random/seed_gen_exception.cc @@ -19,6 +19,7 @@ #include "absl/base/config.h" namespace absl { +ABSL_NAMESPACE_BEGIN static constexpr const char kExceptionMessage[] = "Failed generating seed-material for URBG."; @@ -41,4 +42,5 @@ void ThrowSeedGenException() { } } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/seed_gen_exception.h b/absl/random/seed_gen_exception.h index b464d52f..53539005 100644 --- a/absl/random/seed_gen_exception.h +++ b/absl/random/seed_gen_exception.h @@ -28,7 +28,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN //------------------------------------------------------------------------------ // SeedGenException @@ -46,6 +49,7 @@ namespace random_internal { [[noreturn]] void ThrowSeedGenException(); } // namespace random_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_SEED_GEN_EXCEPTION_H_ diff --git a/absl/random/seed_sequences.cc b/absl/random/seed_sequences.cc index 9f319615..426eafd3 100644 --- a/absl/random/seed_sequences.cc +++ b/absl/random/seed_sequences.cc @@ -17,6 +17,7 @@ #include "absl/random/internal/pool_urbg.h" namespace absl { +ABSL_NAMESPACE_BEGIN SeedSeq MakeSeedSeq() { SeedSeq::result_type seed_material[8]; @@ -24,4 +25,5 @@ SeedSeq MakeSeedSeq() { return SeedSeq(std::begin(seed_material), std::end(seed_material)); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/seed_sequences.h b/absl/random/seed_sequences.h index 631d1ecd..ff1340cc 100644 --- a/absl/random/seed_sequences.h +++ b/absl/random/seed_sequences.h @@ -34,6 +34,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // absl::SeedSeq @@ -103,6 +104,7 @@ SeedSeq CreateSeedSeqFrom(URBG* urbg) { // SeedSeq MakeSeedSeq(); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_SEED_SEQUENCES_H_ diff --git a/absl/random/uniform_int_distribution.h b/absl/random/uniform_int_distribution.h index dc8ba8c1..da66564a 100644 --- a/absl/random/uniform_int_distribution.h +++ b/absl/random/uniform_int_distribution.h @@ -40,6 +40,7 @@ #include "absl/random/internal/wide_multiply.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::uniform_int_distribution // @@ -268,6 +269,7 @@ uniform_int_distribution::Generate( return helper::hi(product); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_ diff --git a/absl/random/uniform_real_distribution.h b/absl/random/uniform_real_distribution.h index bf2ed2c5..5ba17b23 100644 --- a/absl/random/uniform_real_distribution.h +++ b/absl/random/uniform_real_distribution.h @@ -45,6 +45,7 @@ #include "absl/random/internal/iostream_state_saver.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::uniform_real_distribution // @@ -195,6 +196,7 @@ std::basic_istream& operator>>( } return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_ diff --git a/absl/random/zipf_distribution.h b/absl/random/zipf_distribution.h index d7b4ac38..22ebc756 100644 --- a/absl/random/zipf_distribution.h +++ b/absl/random/zipf_distribution.h @@ -26,6 +26,7 @@ #include "absl/random/uniform_real_distribution.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::zipf_distribution produces random integer-values in the range [0, k], // distributed according to the discrete probability function: @@ -264,6 +265,7 @@ std::basic_istream& operator>>( return is; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_ diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index e38c8ad6..8d0a6b6d 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -94,6 +94,7 @@ cc_library( ], copts = ABSL_DEFAULT_COPTS, deps = [ + "//absl/base:config", "//absl/base:core_headers", "//absl/base:endian", "//absl/meta:type_traits", @@ -413,6 +414,7 @@ cc_test( deps = [ ":pow10_helper", ":strings", + "//absl/base:config", "//absl/base:raw_logging_internal", "//absl/random", "//absl/random:distributions", @@ -489,6 +491,7 @@ cc_test( copts = ABSL_TEST_COPTS, deps = [ ":strings", + "//absl/base:config", "//absl/base:raw_logging_internal", "@com_google_googletest//:gtest_main", ], @@ -504,6 +507,7 @@ cc_test( copts = ABSL_TEST_COPTS, deps = [ ":strings", + "//absl/base:config", "@com_google_googletest//:gtest_main", ], ) @@ -668,6 +672,7 @@ cc_library( srcs = ["internal/pow10_helper.cc"], hdrs = ["internal/pow10_helper.h"], visibility = ["//visibility:private"], + deps = ["//absl/base:config"], ) cc_test( diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt index cd52a472..98101573 100644 --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -81,6 +81,7 @@ absl_cc_library( COPTS ${ABSL_DEFAULT_COPTS} DEPS + absl::config absl::core_headers absl::endian absl::type_traits @@ -276,6 +277,7 @@ absl_cc_test( absl::strings absl::core_headers absl::pow10_helper + absl::config absl::raw_logging_internal absl::random_random absl::random_distributions @@ -331,6 +333,7 @@ absl_cc_test( ${ABSL_TEST_COPTS} DEPS absl::strings + absl::config absl::raw_logging_internal gmock_main ) @@ -346,6 +349,7 @@ absl_cc_test( ${ABSL_TEST_COPTS} DEPS absl::strings + absl::config gmock_main ) @@ -502,6 +506,8 @@ absl_cc_library( "internal/pow10_helper.cc" COPTS ${ABSL_TEST_COPTS} + DEPS + absl::config TESTONLY ) diff --git a/absl/strings/ascii.cc b/absl/strings/ascii.cc index 3f7c581f..abea3e4f 100644 --- a/absl/strings/ascii.cc +++ b/absl/strings/ascii.cc @@ -15,6 +15,7 @@ #include "absl/strings/ascii.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace ascii_internal { // # Table generated by this Python code (bit 0x02 is currently unused): @@ -195,4 +196,5 @@ void RemoveExtraAsciiWhitespace(std::string* str) { str->erase(output_it - &(*str)[0]); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/ascii.h b/absl/strings/ascii.h index f9e4fd1d..792aabe5 100644 --- a/absl/strings/ascii.h +++ b/absl/strings/ascii.h @@ -59,6 +59,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace ascii_internal { // Declaration for an array of bitfields holding character information. @@ -234,6 +235,7 @@ inline void StripAsciiWhitespace(std::string* str) { // Removes leading, trailing, and consecutive internal whitespace. void RemoveExtraAsciiWhitespace(std::string*); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_ASCII_H_ diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc index bc07e7ab..d9bc2dd2 100644 --- a/absl/strings/charconv.cc +++ b/absl/strings/charconv.cc @@ -57,6 +57,7 @@ // narrower mantissas. namespace absl { +ABSL_NAMESPACE_BEGIN namespace { template @@ -980,4 +981,5 @@ const int16_t kPower10ExponentTable[] = { }; } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/charconv.h b/absl/strings/charconv.h index 3f5891ba..e04be32f 100644 --- a/absl/strings/charconv.h +++ b/absl/strings/charconv.h @@ -17,7 +17,10 @@ #include // NOLINT(build/c++11) +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN // Workalike compatibilty version of std::chars_format from C++17. // @@ -110,6 +113,7 @@ inline chars_format& operator^=(chars_format& lhs, chars_format rhs) { return lhs; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_CHARCONV_H_ diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 18b746e3..d2fcd9c1 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -33,6 +33,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { // These are used for the leave_nulls_escaped argument to CUnescapeInternal(). @@ -1106,4 +1107,5 @@ std::string BytesToHexString(absl::string_view from) { return result; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/escaping.h b/absl/strings/escaping.h index 198b9348..f5ca26c5 100644 --- a/absl/strings/escaping.h +++ b/absl/strings/escaping.h @@ -33,6 +33,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // CUnescape() // @@ -157,6 +158,7 @@ std::string HexStringToBytes(absl::string_view from); // `2*from.size()`. std::string BytesToHexString(absl::string_view from); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_ESCAPING_H_ diff --git a/absl/strings/internal/char_map.h b/absl/strings/internal/char_map.h index b9108b8c..a76e6036 100644 --- a/absl/strings/internal/char_map.h +++ b/absl/strings/internal/char_map.h @@ -28,6 +28,7 @@ #include "absl/base/port.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { class Charmap { @@ -149,6 +150,7 @@ constexpr Charmap GraphCharmap() { return PrintCharmap() & ~SpaceCharmap(); } constexpr Charmap PunctCharmap() { return GraphCharmap() & ~AlnumCharmap(); } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_CHAR_MAP_H_ diff --git a/absl/strings/internal/charconv_bigint.cc b/absl/strings/internal/charconv_bigint.cc index 95d471d9..860c27b2 100644 --- a/absl/strings/internal/charconv_bigint.cc +++ b/absl/strings/internal/charconv_bigint.cc @@ -19,6 +19,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { namespace { @@ -354,4 +355,5 @@ template class BigUnsigned<4>; template class BigUnsigned<84>; } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/charconv_bigint.h b/absl/strings/internal/charconv_bigint.h index 7da9a7e7..108e1eb2 100644 --- a/absl/strings/internal/charconv_bigint.h +++ b/absl/strings/internal/charconv_bigint.h @@ -25,6 +25,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // The largest power that 5 that can be raised to, and still fit in a uint32_t. @@ -414,6 +415,7 @@ extern template class BigUnsigned<4>; extern template class BigUnsigned<84>; } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_CHARCONV_BIGINT_H_ diff --git a/absl/strings/internal/charconv_bigint_test.cc b/absl/strings/internal/charconv_bigint_test.cc index 745714ac..363bcb03 100644 --- a/absl/strings/internal/charconv_bigint_test.cc +++ b/absl/strings/internal/charconv_bigint_test.cc @@ -19,6 +19,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { TEST(BigUnsigned, ShiftLeft) { @@ -200,4 +201,5 @@ TEST(BigUnsigned, TenToTheNth) { } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/charconv_parse.cc b/absl/strings/internal/charconv_parse.cc index fa9a8965..d9a57a78 100644 --- a/absl/strings/internal/charconv_parse.cc +++ b/absl/strings/internal/charconv_parse.cc @@ -22,6 +22,7 @@ #include "absl/strings/internal/memutil.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { // ParseFloat<10> will read the first 19 significant digits of the mantissa. @@ -499,4 +500,5 @@ template ParsedFloat ParseFloat<16>(const char* begin, const char* end, chars_format format_flags); } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/charconv_parse.h b/absl/strings/internal/charconv_parse.h index 44d06b2e..505998b5 100644 --- a/absl/strings/internal/charconv_parse.h +++ b/absl/strings/internal/charconv_parse.h @@ -17,9 +17,11 @@ #include +#include "absl/base/config.h" #include "absl/strings/charconv.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // Enum indicating whether a parsed float is a number or special value. @@ -92,5 +94,6 @@ extern template ParsedFloat ParseFloat<16>(const char* begin, const char* end, absl::chars_format format_flags); } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_ diff --git a/absl/strings/internal/escaping_test_common.h b/absl/strings/internal/escaping_test_common.h index bd803031..7b18017a 100644 --- a/absl/strings/internal/escaping_test_common.h +++ b/absl/strings/internal/escaping_test_common.h @@ -22,6 +22,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { struct base64_testcase { @@ -126,6 +127,7 @@ inline const std::array& base64_strings() { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_ESCAPING_TEST_COMMON_H_ diff --git a/absl/strings/internal/memutil.cc b/absl/strings/internal/memutil.cc index 77aa63c2..2519c688 100644 --- a/absl/strings/internal/memutil.cc +++ b/absl/strings/internal/memutil.cc @@ -17,6 +17,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { int memcasecmp(const char* s1, const char* s2, size_t len) { @@ -107,4 +108,5 @@ const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle, } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/memutil.h b/absl/strings/internal/memutil.h index 7c071a82..9ad05358 100644 --- a/absl/strings/internal/memutil.h +++ b/absl/strings/internal/memutil.h @@ -69,6 +69,7 @@ #include "absl/strings/ascii.h" // for absl::ascii_tolower namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { inline char* memcat(char* dest, size_t destlen, const char* src, @@ -141,6 +142,7 @@ const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle, size_t neelen); } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_MEMUTIL_H_ diff --git a/absl/strings/internal/numbers_test_common.h b/absl/strings/internal/numbers_test_common.h index a263219e..1a1e50c4 100644 --- a/absl/strings/internal/numbers_test_common.h +++ b/absl/strings/internal/numbers_test_common.h @@ -23,7 +23,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { template @@ -175,6 +178,7 @@ inline const std::array& strtouint64_test_cases() { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_ diff --git a/absl/strings/internal/ostringstream.cc b/absl/strings/internal/ostringstream.cc index d0f0f84b..05324c78 100644 --- a/absl/strings/internal/ostringstream.cc +++ b/absl/strings/internal/ostringstream.cc @@ -15,6 +15,7 @@ #include "absl/strings/internal/ostringstream.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { OStringStream::Buf::int_type OStringStream::overflow(int c) { @@ -31,4 +32,5 @@ std::streamsize OStringStream::xsputn(const char* s, std::streamsize n) { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/ostringstream.h b/absl/strings/internal/ostringstream.h index 20792015..d25d6047 100644 --- a/absl/strings/internal/ostringstream.h +++ b/absl/strings/internal/ostringstream.h @@ -23,6 +23,7 @@ #include "absl/base/port.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // The same as std::ostringstream but appends to a user-specified std::string, @@ -82,6 +83,7 @@ class OStringStream : private std::basic_streambuf, public std::ostream { }; } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_ diff --git a/absl/strings/internal/pow10_helper.cc b/absl/strings/internal/pow10_helper.cc index 03ed8d07..42e96c34 100644 --- a/absl/strings/internal/pow10_helper.cc +++ b/absl/strings/internal/pow10_helper.cc @@ -17,6 +17,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { namespace { @@ -117,4 +118,5 @@ double Pow10(int exp) { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/pow10_helper.h b/absl/strings/internal/pow10_helper.h index 9d1aa710..c37c2c3f 100644 --- a/absl/strings/internal/pow10_helper.h +++ b/absl/strings/internal/pow10_helper.h @@ -22,7 +22,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // Computes the precise value of 10^exp. (I.e. the nearest representable @@ -31,6 +34,7 @@ namespace strings_internal { double Pow10(int exp); } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_POW10_HELPER_H_ diff --git a/absl/strings/internal/pow10_helper_test.cc b/absl/strings/internal/pow10_helper_test.cc index a4a68b5d..a4ff76d3 100644 --- a/absl/strings/internal/pow10_helper_test.cc +++ b/absl/strings/internal/pow10_helper_test.cc @@ -20,6 +20,7 @@ #include "absl/strings/str_format.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { namespace { @@ -117,4 +118,5 @@ TEST(Pow10HelperTest, Works) { } // namespace } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/resize_uninitialized.h b/absl/strings/internal/resize_uninitialized.h index 0f5e964d..e42628e3 100644 --- a/absl/strings/internal/resize_uninitialized.h +++ b/absl/strings/internal/resize_uninitialized.h @@ -25,6 +25,7 @@ #include "absl/meta/type_traits.h" // for void_t namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // Is a subclass of true_type or false_type, depending on whether or not @@ -66,6 +67,7 @@ inline void STLStringResizeUninitialized(string_type* s, size_t new_size) { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ diff --git a/absl/strings/internal/stl_type_traits.h b/absl/strings/internal/stl_type_traits.h index 202ab374..6035ca45 100644 --- a/absl/strings/internal/stl_type_traits.h +++ b/absl/strings/internal/stl_type_traits.h @@ -40,6 +40,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { template class T> @@ -242,5 +243,6 @@ struct IsStrictlyBaseOfAndConvertibleToSTLContainer IsConvertibleToSTLContainer> {}; } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STL_TYPE_TRAITS_H_ diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index b567a5c5..875bd99c 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -14,6 +14,7 @@ #include "absl/strings/internal/str_format/float_conversion.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -386,4 +387,5 @@ ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(); } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index a209a927..b672a229 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -19,6 +19,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN class Cord; class FormatCountCapture; @@ -426,6 +427,7 @@ ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern); } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_ diff --git a/absl/strings/internal/str_format/arg_test.cc b/absl/strings/internal/str_format/arg_test.cc index 3421fac1..96c9cfd3 100644 --- a/absl/strings/internal/str_format/arg_test.cc +++ b/absl/strings/internal/str_format/arg_test.cc @@ -14,6 +14,7 @@ #include "absl/strings/str_format.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -108,4 +109,5 @@ const char kMyArray[] = "ABCDE"; } // namespace } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/bind.cc b/absl/strings/internal/str_format/bind.cc index 45e335a3..1ee281af 100644 --- a/absl/strings/internal/str_format/bind.cc +++ b/absl/strings/internal/str_format/bind.cc @@ -6,6 +6,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -236,4 +237,5 @@ int SnprintF(char* output, size_t size, const UntypedFormatSpecImpl format, } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index dafcd610..2bf0c085 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -13,6 +13,7 @@ #include "absl/types/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN class UntypedFormatSpec; @@ -202,6 +203,7 @@ class StreamedWrapper { }; } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_ diff --git a/absl/strings/internal/str_format/bind_test.cc b/absl/strings/internal/str_format/bind_test.cc index 2574801a..f6817419 100644 --- a/absl/strings/internal/str_format/bind_test.cc +++ b/absl/strings/internal/str_format/bind_test.cc @@ -6,6 +6,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -138,4 +139,5 @@ TEST_F(FormatBindTest, FormatPack) { } // namespace } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/checker.h b/absl/strings/internal/str_format/checker.h index 04a88827..8993a79b 100644 --- a/absl/strings/internal/str_format/checker.h +++ b/absl/strings/internal/str_format/checker.h @@ -14,6 +14,7 @@ #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { constexpr bool AllOf() { return true; } @@ -319,6 +320,7 @@ constexpr bool ValidFormatImpl(string_view format) { #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_ diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index 7aa194a7..c309e203 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -5,6 +5,7 @@ #include "absl/strings/str_format.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -147,4 +148,5 @@ TEST(StrFormatChecker, LongFormat) { } // namespace } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/convert_test.cc b/absl/strings/internal/str_format/convert_test.cc index 5f198059..ca994346 100644 --- a/absl/strings/internal/str_format/convert_test.cc +++ b/absl/strings/internal/str_format/convert_test.cc @@ -11,6 +11,7 @@ #include "absl/strings/internal/str_format/bind.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -646,4 +647,5 @@ TEST_F(FormatConvertTest, ExpectedFailures) { } // namespace } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/extension.cc b/absl/strings/internal/str_format/extension.cc index d7f58159..559011bf 100644 --- a/absl/strings/internal/str_format/extension.cc +++ b/absl/strings/internal/str_format/extension.cc @@ -20,6 +20,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { // clang-format off @@ -81,4 +82,5 @@ bool FormatSinkImpl::PutPaddedString(string_view v, int w, int p, bool l) { } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index 3f4788c9..5726ea5c 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -26,6 +26,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN class Cord; @@ -406,6 +407,7 @@ inline size_t Excess(size_t used, size_t capacity) { } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_ diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index 9236acdc..ebe4da5b 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -9,6 +9,7 @@ #include "absl/base/config.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -482,4 +483,5 @@ bool ConvertFloatImpl(double v, const ConversionSpec &conv, } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/float_conversion.h b/absl/strings/internal/str_format/float_conversion.h index 8ba5566d..49a6a636 100644 --- a/absl/strings/internal/str_format/float_conversion.h +++ b/absl/strings/internal/str_format/float_conversion.h @@ -4,6 +4,7 @@ #include "absl/strings/internal/str_format/extension.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { bool ConvertFloatImpl(float v, const ConversionSpec &conv, @@ -16,6 +17,7 @@ bool ConvertFloatImpl(long double v, const ConversionSpec &conv, FormatSinkImpl *sink); } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_ diff --git a/absl/strings/internal/str_format/output.cc b/absl/strings/internal/str_format/output.cc index 38987b63..c4b24706 100644 --- a/absl/strings/internal/str_format/output.cc +++ b/absl/strings/internal/str_format/output.cc @@ -18,6 +18,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -67,4 +68,5 @@ void FILERawSink::Write(string_view v) { } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/output.h b/absl/strings/internal/str_format/output.h index 6dc2f3f7..28b288b7 100644 --- a/absl/strings/internal/str_format/output.h +++ b/absl/strings/internal/str_format/output.h @@ -29,6 +29,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN class Cord; @@ -97,6 +98,7 @@ auto InvokeFlush(T* out, string_view s) } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_ diff --git a/absl/strings/internal/str_format/output_test.cc b/absl/strings/internal/str_format/output_test.cc index 6e04abef..e54e6f70 100644 --- a/absl/strings/internal/str_format/output_test.cc +++ b/absl/strings/internal/str_format/output_test.cc @@ -21,6 +21,7 @@ #include "gtest/gtest.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { TEST(InvokeFlush, String) { @@ -67,5 +68,6 @@ TEST(BufferRawSink, Limits) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/parser.cc b/absl/strings/internal/str_format/parser.cc index 9ef5615c..eff68f35 100644 --- a/absl/strings/internal/str_format/parser.cc +++ b/absl/strings/internal/str_format/parser.cc @@ -14,6 +14,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { using CC = ConversionChar::Id; @@ -300,4 +301,5 @@ bool ParsedFormatBase::MatchesConversions( } } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index 4b441f71..116dda06 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -16,6 +16,7 @@ #include "absl/strings/internal/str_format/extension.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { // The analyzed properties of a single specified conversion. @@ -317,6 +318,7 @@ class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase { : ParsedFormatBase(s, allow_ignored, {C...}) {} }; } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_ diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 6d356093..33ed8f09 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -7,6 +7,7 @@ #include "absl/base/macros.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { @@ -389,4 +390,5 @@ TEST_F(ParsedFormatTest, ParsingFlagOrder) { } // namespace } // namespace str_format_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/str_join_internal.h b/absl/strings/internal/str_join_internal.h index 7c35f4de..31dbf672 100644 --- a/absl/strings/internal/str_join_internal.h +++ b/absl/strings/internal/str_join_internal.h @@ -43,6 +43,7 @@ #include "absl/strings/str_cat.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // @@ -307,6 +308,7 @@ std::string JoinRange(const Range& range, absl::string_view separator) { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_ diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h index 52f62226..b54f6ebe 100644 --- a/absl/strings/internal/str_split_internal.h +++ b/absl/strings/internal/str_split_internal.h @@ -47,6 +47,7 @@ #endif // _GLIBCXX_DEBUG namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // This class is implicitly constructible from everything that absl::string_view @@ -448,6 +449,7 @@ class Splitter { }; } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_SPLIT_INTERNAL_H_ diff --git a/absl/strings/internal/utf8.cc b/absl/strings/internal/utf8.cc index 82d36c24..8fd8edc1 100644 --- a/absl/strings/internal/utf8.cc +++ b/absl/strings/internal/utf8.cc @@ -17,6 +17,7 @@ #include "absl/strings/internal/utf8.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { size_t EncodeUTF8Char(char *buffer, char32_t utf8_char) { @@ -48,4 +49,5 @@ size_t EncodeUTF8Char(char *buffer, char32_t utf8_char) { } } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/internal/utf8.h b/absl/strings/internal/utf8.h index 04236304..32fb1093 100644 --- a/absl/strings/internal/utf8.h +++ b/absl/strings/internal/utf8.h @@ -20,7 +20,10 @@ #include #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // For Unicode code points 0 through 0x10FFFF, EncodeUTF8Char writes @@ -41,6 +44,7 @@ enum { kMaxEncodedUTF8Size = 4 }; size_t EncodeUTF8Char(char *buffer, char32_t utf8_char); } // namespace strings_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_UTF8_H_ diff --git a/absl/strings/match.cc b/absl/strings/match.cc index 7b24241a..8127cb0c 100644 --- a/absl/strings/match.cc +++ b/absl/strings/match.cc @@ -17,6 +17,7 @@ #include "absl/strings/internal/memutil.h" namespace absl { +ABSL_NAMESPACE_BEGIN bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) { return (piece1.size() == piece2.size() && @@ -35,4 +36,5 @@ bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix) { EqualsIgnoreCase(text.substr(text.size() - suffix.size()), suffix); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/match.h b/absl/strings/match.h index 762f359f..90fca98a 100644 --- a/absl/strings/match.h +++ b/absl/strings/match.h @@ -20,7 +20,7 @@ // This file contains simple utilities for performing string matching checks. // All of these function parameters are specified as `absl::string_view`, // meaning that these functions can accept `std::string`, `absl::string_view` or -// nul-terminated C-style strings. +// NUL-terminated C-style strings. // // Examples: // std::string s = "foo"; @@ -38,6 +38,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // StrContains() // @@ -83,6 +84,7 @@ bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix); // case in the comparison. bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix); +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_MATCH_H_ diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index 4890bd54..caab4631 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc @@ -40,6 +40,7 @@ #include "absl/strings/str_cat.h" namespace absl { +ABSL_NAMESPACE_BEGIN bool SimpleAtof(absl::string_view str, float* out) { *out = 0.0; @@ -911,4 +912,5 @@ bool safe_strtou128_base(absl::string_view text, uint128* value, int base) { } } // namespace numbers_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/numbers.h b/absl/strings/numbers.h index 7a0d6f50..61204683 100644 --- a/absl/strings/numbers.h +++ b/absl/strings/numbers.h @@ -51,6 +51,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // SimpleAtoi() // @@ -95,11 +96,13 @@ ABSL_MUST_USE_RESULT bool SimpleAtod(absl::string_view str, double* out); // unspecified state. ABSL_MUST_USE_RESULT bool SimpleAtob(absl::string_view str, bool* out); +ABSL_NAMESPACE_END } // namespace absl // End of public API. Implementation details follow. namespace absl { +ABSL_NAMESPACE_BEGIN namespace numbers_internal { // Digit conversion. @@ -254,6 +257,7 @@ ABSL_MUST_USE_RESULT inline bool SimpleAtoi(absl::string_view str, return numbers_internal::safe_strtou128_base(str, out, 10); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_NUMBERS_H_ diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index 4bd56f5f..d9afe2f3 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -25,6 +25,7 @@ #include "absl/strings/numbers.h" namespace absl { +ABSL_NAMESPACE_BEGIN AlphaNum::AlphaNum(Hex hex) { static_assert(numbers_internal::kFastToBufferSize >= 32, @@ -241,4 +242,5 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, assert(out == begin + dest->size()); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h index 663a1945..292fa235 100644 --- a/absl/strings/str_cat.h +++ b/absl/strings/str_cat.h @@ -64,6 +64,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { // AlphaNumBuffer allows a way to pass a string to StrCat without having to do @@ -401,6 +402,7 @@ SixDigits(double d) { return result; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STR_CAT_H_ diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index c11c93a2..fbbfe43d 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -82,6 +82,7 @@ #include "absl/strings/internal/str_format/parser.h" // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN // UntypedFormatSpec // @@ -530,6 +531,7 @@ ABSL_MUST_USE_RESULT inline bool FormatUntyped( str_format_internal::UntypedFormatSpecImpl::Extract(format), args); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STR_FORMAT_H_ diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index cfd81bb3..d33bcaa2 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -10,6 +10,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { using str_format_internal::FormatArgImpl; @@ -622,6 +623,7 @@ TEST_F(FormatWrapperTest, ParsedFormat) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl // Some codegen thunks that we can use to easily dump the generated assembly for diff --git a/absl/strings/str_join.h b/absl/strings/str_join.h index 4772f5d1..ae5731a4 100644 --- a/absl/strings/str_join.h +++ b/absl/strings/str_join.h @@ -60,6 +60,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // Concept: Formatter @@ -286,6 +287,7 @@ std::string StrJoin(const std::tuple& value, return strings_internal::JoinAlgorithm(value, separator, AlphaNumFormatter()); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STR_JOIN_H_ diff --git a/absl/strings/str_replace.cc b/absl/strings/str_replace.cc index 280f63d3..2bd5fa98 100644 --- a/absl/strings/str_replace.cc +++ b/absl/strings/str_replace.cc @@ -17,6 +17,7 @@ #include "absl/strings/str_cat.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace strings_internal { using FixedMapping = @@ -77,4 +78,5 @@ int StrReplaceAll(strings_internal::FixedMapping replacements, return StrReplaceAll(replacements, target); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/str_replace.h b/absl/strings/str_replace.h index 30540d02..273c7077 100644 --- a/absl/strings/str_replace.h +++ b/absl/strings/str_replace.h @@ -46,6 +46,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // StrReplaceAll() // @@ -212,6 +213,7 @@ int StrReplaceAll(const StrToStrMapping& replacements, std::string* target) { return substitutions; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STR_REPLACE_H_ diff --git a/absl/strings/str_split.cc b/absl/strings/str_split.cc index 25931307..d0f86669 100644 --- a/absl/strings/str_split.cc +++ b/absl/strings/str_split.cc @@ -27,6 +27,7 @@ #include "absl/strings/ascii.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -134,4 +135,5 @@ absl::string_view ByLength::Find(absl::string_view text, return absl::string_view(substr.data() + length_, 0); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/str_split.h b/absl/strings/str_split.h index 73330789..a79cd4a0 100644 --- a/absl/strings/str_split.h +++ b/absl/strings/str_split.h @@ -49,6 +49,7 @@ #include "absl/strings/strip.h" namespace absl { +ABSL_NAMESPACE_BEGIN //------------------------------------------------------------------------------ // Delimiters @@ -506,6 +507,7 @@ StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d, std::move(text), DelimiterType(d), std::move(p)); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STR_SPLIT_H_ diff --git a/absl/strings/string_view.cc b/absl/strings/string_view.cc index d5e1a3de..c5f5de93 100644 --- a/absl/strings/string_view.cc +++ b/absl/strings/string_view.cc @@ -24,6 +24,7 @@ #include "absl/strings/internal/memutil.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { void WritePadding(std::ostream& o, size_t pad) { @@ -228,6 +229,7 @@ constexpr string_view::size_type string_view::npos; ABSL_STRING_VIEW_SELECTANY constexpr string_view::size_type string_view::kMaxSize; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_STRING_VIEW diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index 6a650874..4b34e563 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -35,7 +35,9 @@ #include // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN using std::string_view; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_STRING_VIEW @@ -61,6 +63,7 @@ using std::string_view; #include "absl/base/port.h" namespace absl { +ABSL_NAMESPACE_BEGIN // absl::string_view // @@ -109,10 +112,10 @@ namespace absl { // example, when splitting a string, `std::vector` is a // natural data type for the output. // -// When constructed from a source which is nul-terminated, the `string_view` -// itself will not include the nul-terminator unless a specific size (including -// the nul) is passed to the constructor. As a result, common idioms that work -// on nul-terminated strings do not work on `string_view` objects. If you write +// When constructed from a source which is NUL-terminated, the `string_view` +// itself will not include the NUL-terminator unless a specific size (including +// the NUL) is passed to the constructor. As a result, common idioms that work +// on NUL-terminated strings do not work on `string_view` objects. If you write // code that scans a `string_view`, you must check its length rather than test // for nul, for example. Note, however, that nuls may still be embedded within // a `string_view` explicitly. @@ -179,7 +182,7 @@ class string_view { // doesn't need to be reevaluated after `ptr_` is set. : string_view(str.data(), str.size()) {} - // Implicit constructor of a `string_view` from nul-terminated `str`. When + // Implicit constructor of a `string_view` from NUL-terminated `str`. When // accepting possibly null strings, use `absl::NullSafeStringView(str)` // instead (see below). constexpr string_view(const char* str) // NOLINT(runtime/explicit) @@ -309,8 +312,8 @@ class string_view { // // Returns a pointer to the underlying character array (which is of course // stored elsewhere). Note that `string_view::data()` may contain embedded nul - // characters, but the returned buffer may or may not be nul-terminated; - // therefore, do not pass `data()` to a routine that expects a nul-terminated + // characters, but the returned buffer may or may not be NUL-terminated; + // therefore, do not pass `data()` to a routine that expects a NUL-terminated // std::string. constexpr const_pointer data() const noexcept { return ptr_; } @@ -577,6 +580,7 @@ constexpr bool operator>=(string_view x, string_view y) noexcept { // IO Insertion Operator std::ostream& operator<<(std::ostream& o, string_view piece); +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_INTERNAL_STRING_VIEW_MEMCMP @@ -584,6 +588,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece); #endif // ABSL_USES_STD_STRING_VIEW namespace absl { +ABSL_NAMESPACE_BEGIN // ClippedSubstr() // @@ -604,6 +609,7 @@ inline string_view NullSafeStringView(const char* p) { return p ? string_view(p) : string_view(); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STRING_VIEW_H_ diff --git a/absl/strings/strip.h b/absl/strings/strip.h index e1341e08..111872ca 100644 --- a/absl/strings/strip.h +++ b/absl/strings/strip.h @@ -30,6 +30,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ConsumePrefix() // @@ -84,6 +85,7 @@ ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix( return str; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_STRIP_H_ diff --git a/absl/strings/substitute.cc b/absl/strings/substitute.cc index 5d28c528..5b69a3ef 100644 --- a/absl/strings/substitute.cc +++ b/absl/strings/substitute.cc @@ -23,6 +23,7 @@ #include "absl/strings/string_view.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace substitute_internal { void SubstituteAndAppendArray(std::string* output, absl::string_view format, @@ -166,4 +167,5 @@ Arg::Arg(Dec dec) { } } // namespace substitute_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h index 233e9dcf..766aca42 100644 --- a/absl/strings/substitute.h +++ b/absl/strings/substitute.h @@ -86,6 +86,7 @@ #include "absl/strings/strip.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace substitute_internal { // Arg @@ -681,6 +682,7 @@ std::string Substitute( "format std::string doesn't contain all of $0 through $9"); #endif // ABSL_BAD_CALL_IF +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_SUBSTITUTE_H_ diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel index 36dc98f3..3f876b9f 100644 --- a/absl/synchronization/BUILD.bazel +++ b/absl/synchronization/BUILD.bazel @@ -43,6 +43,7 @@ cc_library( deps = [ "//absl/base", "//absl/base:base_internal", + "//absl/base:config", "//absl/base:core_headers", "//absl/base:malloc_internal", "//absl/base:raw_logging_internal", diff --git a/absl/synchronization/CMakeLists.txt b/absl/synchronization/CMakeLists.txt index 3c47a1bc..dfe5d05d 100644 --- a/absl/synchronization/CMakeLists.txt +++ b/absl/synchronization/CMakeLists.txt @@ -26,6 +26,7 @@ absl_cc_library( DEPS absl::base absl::base_internal + absl::config absl::core_headers absl::malloc_internal absl::raw_logging_internal diff --git a/absl/synchronization/barrier.cc b/absl/synchronization/barrier.cc index c2c539ac..0dfd795e 100644 --- a/absl/synchronization/barrier.cc +++ b/absl/synchronization/barrier.cc @@ -18,6 +18,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Return whether int *arg is zero. static bool IsZero(void *arg) { @@ -47,4 +48,5 @@ bool Barrier::Block() { return this->num_to_exit_ == 0; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/barrier.h b/absl/synchronization/barrier.h index cb5d821a..d8e75440 100644 --- a/absl/synchronization/barrier.h +++ b/absl/synchronization/barrier.h @@ -23,6 +23,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Barrier // @@ -73,5 +74,6 @@ class Barrier { int num_to_exit_ ABSL_GUARDED_BY(lock_); }; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_BARRIER_H_ diff --git a/absl/synchronization/blocking_counter.cc b/absl/synchronization/blocking_counter.cc index 481a06b2..3cea7aed 100644 --- a/absl/synchronization/blocking_counter.cc +++ b/absl/synchronization/blocking_counter.cc @@ -17,6 +17,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Return whether int *arg is zero. static bool IsZero(void *arg) { @@ -52,4 +53,5 @@ void BlockingCounter::Wait() { // after we return from this method. } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/blocking_counter.h b/absl/synchronization/blocking_counter.h index 77560fc0..1f53f9f2 100644 --- a/absl/synchronization/blocking_counter.h +++ b/absl/synchronization/blocking_counter.h @@ -24,6 +24,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN // BlockingCounter // @@ -92,6 +93,7 @@ class BlockingCounter { int num_waiting_ ABSL_GUARDED_BY(lock_); }; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_BLOCKING_COUNTER_H_ diff --git a/absl/synchronization/blocking_counter_test.cc b/absl/synchronization/blocking_counter_test.cc index c63e3392..2926224a 100644 --- a/absl/synchronization/blocking_counter_test.cc +++ b/absl/synchronization/blocking_counter_test.cc @@ -22,6 +22,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { void PauseAndDecreaseCounter(BlockingCounter* counter, int* done) { @@ -63,4 +64,5 @@ TEST(BlockingCounterTest, BasicFunctionality) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/create_thread_identity.cc b/absl/synchronization/internal/create_thread_identity.cc index ec49d1ca..fa0070a9 100644 --- a/absl/synchronization/internal/create_thread_identity.cc +++ b/absl/synchronization/internal/create_thread_identity.cc @@ -27,6 +27,7 @@ #include "absl/synchronization/internal/per_thread_sem.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // ThreadIdentity storage is persistent, we maintain a free-list of previously @@ -133,6 +134,7 @@ base_internal::ThreadIdentity* CreateThreadIdentity() { } } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_LOW_LEVEL_ALLOC_MISSING diff --git a/absl/synchronization/internal/create_thread_identity.h b/absl/synchronization/internal/create_thread_identity.h index 97237a6e..e121f683 100644 --- a/absl/synchronization/internal/create_thread_identity.h +++ b/absl/synchronization/internal/create_thread_identity.h @@ -29,6 +29,7 @@ #include "absl/base/port.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // Allocates and attaches a ThreadIdentity object for the calling thread. @@ -53,6 +54,7 @@ inline base_internal::ThreadIdentity* GetOrCreateCurrentThreadIdentity() { } } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_ diff --git a/absl/synchronization/internal/graphcycles.cc b/absl/synchronization/internal/graphcycles.cc index 0c8c7564..6a2bcdf6 100644 --- a/absl/synchronization/internal/graphcycles.cc +++ b/absl/synchronization/internal/graphcycles.cc @@ -44,6 +44,7 @@ // Do not use STL. This module does not use standard memory allocation. namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { namespace { @@ -690,6 +691,7 @@ int GraphCycles::GetStackTrace(GraphId id, void*** ptr) { } } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_LOW_LEVEL_ALLOC_MISSING diff --git a/absl/synchronization/internal/graphcycles.h b/absl/synchronization/internal/graphcycles.h index e08dc09d..ceba33e4 100644 --- a/absl/synchronization/internal/graphcycles.h +++ b/absl/synchronization/internal/graphcycles.h @@ -40,7 +40,10 @@ #include +#include "absl/base/config.h" + namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // Opaque identifier for a graph node. @@ -132,6 +135,7 @@ class GraphCycles { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif diff --git a/absl/synchronization/internal/graphcycles_test.cc b/absl/synchronization/internal/graphcycles_test.cc index 58e8477b..74eaffe7 100644 --- a/absl/synchronization/internal/graphcycles_test.cc +++ b/absl/synchronization/internal/graphcycles_test.cc @@ -25,6 +25,7 @@ #include "absl/base/macros.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // We emulate a GraphCycles object with a node vector and an edge vector. @@ -459,4 +460,5 @@ TEST_F(GraphCyclesTest, ManyEdges) { } } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/kernel_timeout.h b/absl/synchronization/internal/kernel_timeout.h index 61c72e75..d6ac5db0 100644 --- a/absl/synchronization/internal/kernel_timeout.h +++ b/absl/synchronization/internal/kernel_timeout.h @@ -34,6 +34,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { class Futex; @@ -148,6 +149,7 @@ class KernelTimeout { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_ diff --git a/absl/synchronization/internal/mutex_nonprod.cc b/absl/synchronization/internal/mutex_nonprod.cc index 267deaff..4590b98d 100644 --- a/absl/synchronization/internal/mutex_nonprod.cc +++ b/absl/synchronization/internal/mutex_nonprod.cc @@ -31,6 +31,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { namespace { @@ -315,4 +316,5 @@ bool Condition::Eval() const { void RegisterSymbolizer(bool (*)(const void*, char*, int)) {} +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/mutex_nonprod.inc b/absl/synchronization/internal/mutex_nonprod.inc index b8d5af79..85dae0ff 100644 --- a/absl/synchronization/internal/mutex_nonprod.inc +++ b/absl/synchronization/internal/mutex_nonprod.inc @@ -36,6 +36,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN class Condition; namespace synchronization_internal { @@ -256,4 +257,5 @@ class SynchronizationStorage { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/per_thread_sem.cc b/absl/synchronization/internal/per_thread_sem.cc index 2a78b20f..821ca9b4 100644 --- a/absl/synchronization/internal/per_thread_sem.cc +++ b/absl/synchronization/internal/per_thread_sem.cc @@ -25,6 +25,7 @@ #include "absl/synchronization/internal/waiter.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { void PerThreadSem::SetThreadBlockedCounter(std::atomic *counter) { @@ -62,6 +63,7 @@ void PerThreadSem::Tick(base_internal::ThreadIdentity *identity) { } } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl extern "C" { diff --git a/absl/synchronization/internal/per_thread_sem.h b/absl/synchronization/internal/per_thread_sem.h index 113cdfb7..8ab43915 100644 --- a/absl/synchronization/internal/per_thread_sem.h +++ b/absl/synchronization/internal/per_thread_sem.h @@ -32,6 +32,7 @@ #include "absl/synchronization/internal/kernel_timeout.h" namespace absl { +ABSL_NAMESPACE_BEGIN class Mutex; @@ -85,6 +86,7 @@ class PerThreadSem { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl // In some build configurations we pass --detect-odr-violations to the diff --git a/absl/synchronization/internal/per_thread_sem_test.cc b/absl/synchronization/internal/per_thread_sem_test.cc index dba72390..b5a2f6d4 100644 --- a/absl/synchronization/internal/per_thread_sem_test.cc +++ b/absl/synchronization/internal/per_thread_sem_test.cc @@ -33,6 +33,7 @@ // primitives which might use PerThreadSem, most notably absl::Mutex. namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { class SimpleSemaphore { @@ -175,4 +176,5 @@ TEST_F(PerThreadSemTest, Timeouts) { } // namespace } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/thread_pool.h b/absl/synchronization/internal/thread_pool.h index a00f2be8..0cb96dac 100644 --- a/absl/synchronization/internal/thread_pool.h +++ b/absl/synchronization/internal/thread_pool.h @@ -26,6 +26,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // A simple ThreadPool implementation for tests. @@ -86,6 +87,7 @@ class ThreadPool { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_ diff --git a/absl/synchronization/internal/waiter.cc b/absl/synchronization/internal/waiter.cc index e7000d3a..ddd6081e 100644 --- a/absl/synchronization/internal/waiter.cc +++ b/absl/synchronization/internal/waiter.cc @@ -49,6 +49,7 @@ #include "absl/synchronization/internal/kernel_timeout.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { static void MaybeBecomeIdle() { @@ -481,4 +482,5 @@ void Waiter::InternalCondVarPoke() { #endif } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/internal/waiter.h b/absl/synchronization/internal/waiter.h index 9540598b..5af5c282 100644 --- a/absl/synchronization/internal/waiter.h +++ b/absl/synchronization/internal/waiter.h @@ -62,6 +62,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN namespace synchronization_internal { // Waiter is an OS-specific semaphore. @@ -157,6 +158,7 @@ class Waiter { }; } // namespace synchronization_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_ diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 100def2d..46af8a52 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc @@ -71,6 +71,7 @@ ABSL_ATTRIBUTE_WEAK void AbslInternalMutexYield() { std::this_thread::yield(); } } // extern "C" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -295,7 +296,7 @@ static struct SynchEvent { // this is a trivial hash table for the events bool log; // logging turned on // Constant after initialization - char name[1]; // actually longer---null-terminated std::string + char name[1]; // actually longer---NUL-terminated std::string } * synch_event[kNSynchEvent] ABSL_GUARDED_BY(synch_event_mu); // Ensure that the object at "addr" has a SynchEvent struct associated with it, @@ -2720,4 +2721,5 @@ bool Condition::GuaranteedEqual(const Condition *a, const Condition *b) { a->arg_ == b->arg_ && a->method_ == b->method_; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h index ccd94618..8c70c4ce 100644 --- a/absl/synchronization/mutex.h +++ b/absl/synchronization/mutex.h @@ -82,6 +82,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN class Condition; struct SynchWaitParams; @@ -1000,7 +1001,7 @@ void RegisterCondVarTracer(void (*fn)(const char *msg, const void *cv)); // // 'pc' is the program counter being symbolized, 'out' is the buffer to write // into, and 'out_size' is the size of the buffer. This function can return -// false if symbolizing failed, or true if a null-terminated symbol was written +// false if symbolizing failed, or true if a NUL-terminated symbol was written // to 'out.' // // This has the same memory ordering concerns as RegisterMutexProfiler() above. @@ -1039,6 +1040,7 @@ enum class OnDeadlockCycle { // the manner chosen here. void SetMutexDeadlockDetectionMode(OnDeadlockCycle mode); +ABSL_NAMESPACE_END } // namespace absl // In some build configurations we pass --detect-odr-violations to the diff --git a/absl/synchronization/notification.cc b/absl/synchronization/notification.cc index 1eb7f416..e91b9038 100644 --- a/absl/synchronization/notification.cc +++ b/absl/synchronization/notification.cc @@ -22,6 +22,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN void Notification::Notify() { MutexLock l(&this->mutex_); @@ -73,4 +74,5 @@ bool Notification::WaitForNotificationWithDeadline(absl::Time deadline) const { return notified; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/synchronization/notification.h b/absl/synchronization/notification.h index 36c2ce3d..9a354ca2 100644 --- a/absl/synchronization/notification.h +++ b/absl/synchronization/notification.h @@ -57,6 +57,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // Notification @@ -116,6 +117,7 @@ class Notification { std::atomic notified_yet_; // written under mutex_ }; +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_SYNCHRONIZATION_NOTIFICATION_H_ diff --git a/absl/synchronization/notification_test.cc b/absl/synchronization/notification_test.cc index 059d4cd2..100ea76f 100644 --- a/absl/synchronization/notification_test.cc +++ b/absl/synchronization/notification_test.cc @@ -21,6 +21,7 @@ #include "absl/synchronization/mutex.h" namespace absl { +ABSL_NAMESPACE_BEGIN // A thread-safe class that holds a counter. class ThreadSafeCounter { @@ -128,4 +129,5 @@ TEST(NotificationTest, SanityTest) { BasicTests(true, &local_notification2); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc index 7b86fa8b..ada82cbc 100644 --- a/absl/time/civil_time.cc +++ b/absl/time/civil_time.cc @@ -21,6 +21,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -170,4 +171,5 @@ std::ostream& operator<<(std::ostream& os, CivilSecond s) { } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h index 77c3be2e..bb460044 100644 --- a/absl/time/civil_time.h +++ b/absl/time/civil_time.h @@ -76,6 +76,7 @@ #include "absl/time/internal/cctz/include/cctz/civil_time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { struct second_tag : cctz::detail::second_tag {}; @@ -531,6 +532,7 @@ std::ostream& operator<<(std::ostream& os, CivilSecond s); } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TIME_CIVIL_TIME_H_ diff --git a/absl/time/clock.cc b/absl/time/clock.cc index bc6f5ccf..9e9a0386 100644 --- a/absl/time/clock.cc +++ b/absl/time/clock.cc @@ -34,6 +34,7 @@ #include "absl/base/thread_annotations.h" namespace absl { +ABSL_NAMESPACE_BEGIN Time Now() { // TODO(bww): Get a timespec instead so we don't have to divide. int64_t n = absl::GetCurrentTimeNanos(); @@ -43,6 +44,7 @@ Time Now() { } return time_internal::FromUnixDuration(absl::Nanoseconds(n)); } +ABSL_NAMESPACE_END } // namespace absl // Decide if we should use the fast GetCurrentTimeNanos() algorithm @@ -71,9 +73,11 @@ Time Now() { #if !ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS namespace absl { +ABSL_NAMESPACE_BEGIN int64_t GetCurrentTimeNanos() { return GET_CURRENT_TIME_NANOS_FROM_SYSTEM(); } +ABSL_NAMESPACE_END } // namespace absl #else // Use the cyclecounter-based implementation below. @@ -91,6 +95,7 @@ static int64_t stats_slow_paths; static int64_t stats_fast_slow_paths; namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { // This is a friend wrapper around UnscaledCycleClock::Now() // (needed to access UnscaledCycleClock). @@ -515,10 +520,12 @@ static uint64_t UpdateLastSample(uint64_t now_cycles, uint64_t now_ns, return estimated_base_ns; } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS namespace absl { +ABSL_NAMESPACE_BEGIN namespace { // Returns the maximum duration that SleepOnce() can sleep for. @@ -546,6 +553,7 @@ void SleepOnce(absl::Duration to_sleep) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl extern "C" { diff --git a/absl/time/clock.h b/absl/time/clock.h index bb52e4f6..27764a92 100644 --- a/absl/time/clock.h +++ b/absl/time/clock.h @@ -26,6 +26,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN // Now() // @@ -49,6 +50,7 @@ int64_t GetCurrentTimeNanos(); // * Returns immediately when passed a nonpositive duration. void SleepFor(absl::Duration duration); +ABSL_NAMESPACE_END } // namespace absl // ----------------------------------------------------------------------------- diff --git a/absl/time/duration.cc b/absl/time/duration.cc index f0b4631d..b1af8406 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -71,6 +71,7 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -917,4 +918,5 @@ bool ParseFlag(const std::string& text, Duration* dst, std::string* ) { std::string UnparseFlag(Duration d) { return FormatDuration(d); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc index 5dce9ac8..49ebd118 100644 --- a/absl/time/duration_test.cc +++ b/absl/time/duration_test.cc @@ -762,11 +762,6 @@ TEST(Duration, DivisionByZero) { const double dbl_inf = std::numeric_limits::infinity(); const double dbl_denorm = std::numeric_limits::denorm_min(); - // IEEE 754 behavior - double z = 0.0, two = 2.0; - EXPECT_TRUE(std::isinf(two / z)); - EXPECT_TRUE(std::isnan(z / z)); // We'll return inf - // Operator/(Duration, double) EXPECT_EQ(inf, zero / 0.0); EXPECT_EQ(-inf, zero / -0.0); diff --git a/absl/time/format.cc b/absl/time/format.cc index ebe872cf..5997ef0c 100644 --- a/absl/time/format.cc +++ b/absl/time/format.cc @@ -22,6 +22,7 @@ namespace cctz = absl::time_internal::cctz; namespace absl { +ABSL_NAMESPACE_BEGIN extern const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez"; extern const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez"; @@ -145,4 +146,5 @@ std::string UnparseFlag(absl::Time t) { return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone()); } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/internal/get_current_time_chrono.inc b/absl/time/internal/get_current_time_chrono.inc index 5180230d..5eeb6406 100644 --- a/absl/time/internal/get_current_time_chrono.inc +++ b/absl/time/internal/get_current_time_chrono.inc @@ -16,6 +16,7 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { static int64_t GetCurrentTimeNanosFromSystem() { @@ -26,4 +27,5 @@ static int64_t GetCurrentTimeNanosFromSystem() { } } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/internal/get_current_time_posix.inc b/absl/time/internal/get_current_time_posix.inc index 65474ca6..42072000 100644 --- a/absl/time/internal/get_current_time_posix.inc +++ b/absl/time/internal/get_current_time_posix.inc @@ -7,6 +7,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { static int64_t GetCurrentTimeNanosFromSystem() { @@ -19,4 +20,5 @@ static int64_t GetCurrentTimeNanosFromSystem() { } } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/internal/test_util.cc b/absl/time/internal/test_util.cc index fbddbb73..9bffe121 100644 --- a/absl/time/internal/test_util.cc +++ b/absl/time/internal/test_util.cc @@ -24,6 +24,7 @@ namespace cctz = absl::time_internal::cctz; namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { TimeZone LoadTimeZone(const std::string& name) { @@ -33,9 +34,11 @@ TimeZone LoadTimeZone(const std::string& name) { } } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { namespace cctz_extension { namespace { @@ -123,4 +126,5 @@ ZoneInfoSourceFactory zone_info_source_factory = TestFactory; } // namespace cctz_extension } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/internal/test_util.h b/absl/time/internal/test_util.h index d7319ea8..5c4bf1f6 100644 --- a/absl/time/internal/test_util.h +++ b/absl/time/internal/test_util.h @@ -20,12 +20,14 @@ #include "absl/time/time.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace time_internal { // Loads the named timezone, but dies on any failure. absl::TimeZone LoadTimeZone(const std::string& name); } // namespace time_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TIME_INTERNAL_TEST_UTIL_H_ diff --git a/absl/time/time.cc b/absl/time/time.cc index 60382be7..6bb36cb3 100644 --- a/absl/time/time.cc +++ b/absl/time/time.cc @@ -47,6 +47,7 @@ namespace cctz = absl::time_internal::cctz; namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -494,4 +495,5 @@ struct tm ToTM(absl::Time t, absl::TimeZone tz) { return tm; } +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/time/time.h b/absl/time/time.h index be064813..7507b0cd 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -89,6 +89,7 @@ struct timeval; #include "absl/time/internal/cctz/include/cctz/time_zone.h" namespace absl { +ABSL_NAMESPACE_BEGIN class Duration; // Defined below class Time; // Defined below @@ -1574,6 +1575,7 @@ constexpr Time FromTimeT(time_t t) { return time_internal::FromUnixDuration(Seconds(t)); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TIME_TIME_H_ diff --git a/absl/types/any.h b/absl/types/any.h index f7967694..16bda79c 100644 --- a/absl/types/any.h +++ b/absl/types/any.h @@ -61,10 +61,12 @@ #include // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN using std::any; using std::any_cast; using std::bad_any_cast; using std::make_any; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_ANY @@ -91,6 +93,7 @@ using std::make_any; #endif // !defined(__GNUC__) || defined(__GXX_RTTI) namespace absl { +ABSL_NAMESPACE_BEGIN namespace any_internal { @@ -534,6 +537,7 @@ T* any_cast(any* operand) noexcept { : nullptr; } +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_ANY_DETAIL_HAS_RTTI diff --git a/absl/types/bad_any_cast.cc b/absl/types/bad_any_cast.cc index 2a538126..b0592cc9 100644 --- a/absl/types/bad_any_cast.cc +++ b/absl/types/bad_any_cast.cc @@ -22,6 +22,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN bad_any_cast::~bad_any_cast() = default; @@ -39,6 +40,7 @@ void ThrowBadAnyCast() { } } // namespace any_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_ANY diff --git a/absl/types/bad_any_cast.h b/absl/types/bad_any_cast.h index 6a53c010..114cef80 100644 --- a/absl/types/bad_any_cast.h +++ b/absl/types/bad_any_cast.h @@ -30,12 +30,15 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN using std::bad_any_cast; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_ANY namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // bad_any_cast @@ -64,6 +67,7 @@ namespace any_internal { [[noreturn]] void ThrowBadAnyCast(); } // namespace any_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_ANY diff --git a/absl/types/bad_optional_access.cc b/absl/types/bad_optional_access.cc index d9ec21bf..26aca70d 100644 --- a/absl/types/bad_optional_access.cc +++ b/absl/types/bad_optional_access.cc @@ -22,6 +22,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN bad_optional_access::~bad_optional_access() = default; @@ -41,6 +42,7 @@ void throw_bad_optional_access() { } } // namespace optional_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_OPTIONAL diff --git a/absl/types/bad_optional_access.h b/absl/types/bad_optional_access.h index 32dd6a91..a500286a 100644 --- a/absl/types/bad_optional_access.h +++ b/absl/types/bad_optional_access.h @@ -30,12 +30,15 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN using std::bad_optional_access; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_OPTIONAL namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // bad_optional_access @@ -67,6 +70,7 @@ namespace optional_internal { [[noreturn]] void throw_bad_optional_access(); } // namespace optional_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_OPTIONAL diff --git a/absl/types/bad_variant_access.cc b/absl/types/bad_variant_access.cc index dbaea9e9..3dc88cc0 100644 --- a/absl/types/bad_variant_access.cc +++ b/absl/types/bad_variant_access.cc @@ -23,6 +23,7 @@ #include "absl/base/internal/raw_logging.h" namespace absl { +ABSL_NAMESPACE_BEGIN ////////////////////////// // [variant.bad.access] // @@ -57,6 +58,7 @@ void Rethrow() { } } // namespace variant_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_VARIANT diff --git a/absl/types/bad_variant_access.h b/absl/types/bad_variant_access.h index 6935d01b..095969f9 100644 --- a/absl/types/bad_variant_access.h +++ b/absl/types/bad_variant_access.h @@ -30,12 +30,15 @@ #include namespace absl { +ABSL_NAMESPACE_BEGIN using std::bad_variant_access; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_VARIANT namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // bad_variant_access @@ -71,6 +74,7 @@ namespace variant_internal { [[noreturn]] void Rethrow(); } // namespace variant_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_USES_STD_VARIANT diff --git a/absl/types/compare.h b/absl/types/compare.h index a213e0b0..c29ced52 100644 --- a/absl/types/compare.h +++ b/absl/types/compare.h @@ -39,6 +39,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace compare_internal { using value_type = int8_t; @@ -551,6 +552,7 @@ constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, } } // namespace compare_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_COMPARE_H_ diff --git a/absl/types/compare_test.cc b/absl/types/compare_test.cc index ee396fc5..955844b5 100644 --- a/absl/types/compare_test.cc +++ b/absl/types/compare_test.cc @@ -18,6 +18,7 @@ #include "absl/base/casts.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { // This is necessary to avoid a bunch of lint warnings suggesting that we use @@ -334,4 +335,5 @@ TEST(Compare, StaticAsserts) { #endif // __cpp_inline_variables } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/types/internal/conformance_aliases.h b/absl/types/internal/conformance_aliases.h index 7d5d0e09..0cc6884e 100644 --- a/absl/types/internal/conformance_aliases.h +++ b/absl/types/internal/conformance_aliases.h @@ -26,6 +26,7 @@ #include "absl/types/internal/conformance_profile.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace types_internal { // Creates both a Profile and a corresponding Archetype with root name "name". @@ -438,6 +439,7 @@ using ExpandSupportedProfiles = Receiver< // (potentially) non-noexcept moves. } // namespace types_internal +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_INTERNAL_PROFILE_AND_ARCHETYPE_ALIAS diff --git a/absl/types/internal/conformance_archetype.h b/absl/types/internal/conformance_archetype.h index 97ee7265..2349e0f7 100644 --- a/absl/types/internal/conformance_archetype.h +++ b/absl/types/internal/conformance_archetype.h @@ -43,6 +43,7 @@ #include "absl/types/internal/conformance_profile.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace types_internal { // A minimum-conforming implementation of a type with properties specified in @@ -961,6 +962,7 @@ struct EnabledHash { }; } // namespace types_internal +ABSL_NAMESPACE_END } // namespace absl namespace std { diff --git a/absl/types/internal/conformance_profile.h b/absl/types/internal/conformance_profile.h index dce3bbee..e62004fd 100644 --- a/absl/types/internal/conformance_profile.h +++ b/absl/types/internal/conformance_profile.h @@ -44,6 +44,7 @@ // TODO(calabrese) Add support for extending profiles. namespace absl { +ABSL_NAMESPACE_BEGIN namespace types_internal { template @@ -369,6 +370,7 @@ template struct IsProfile : IsProfileImpl::type {}; } // namespace types_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_INTERNAL_CONFORMANCE_PROFILE_H_ diff --git a/absl/types/internal/optional.h b/absl/types/internal/optional.h index d41ccc75..92932b60 100644 --- a/absl/types/internal/optional.h +++ b/absl/types/internal/optional.h @@ -54,6 +54,7 @@ #endif namespace absl { +ABSL_NAMESPACE_BEGIN // Forward declaration template @@ -387,6 +388,7 @@ struct optional_hash_base >()( }; } // namespace optional_internal +ABSL_NAMESPACE_END } // namespace absl #undef ABSL_OPTIONAL_USE_INHERITING_CONSTRUCTORS diff --git a/absl/types/internal/span.h b/absl/types/internal/span.h index d203aad5..112612f4 100644 --- a/absl/types/internal/span.h +++ b/absl/types/internal/span.h @@ -26,6 +26,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace span_internal { // A constexpr min function @@ -121,6 +122,7 @@ template using EnableIfConvertibleTo = typename std::enable_if::value>::type; } // namespace span_internal +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_INTERNAL_SPAN_H_ diff --git a/absl/types/internal/variant.h b/absl/types/internal/variant.h index 58b38592..71bd3adf 100644 --- a/absl/types/internal/variant.h +++ b/absl/types/internal/variant.h @@ -40,6 +40,7 @@ #if !defined(ABSL_USES_STD_VARIANT) namespace absl { +ABSL_NAMESPACE_BEGIN template class variant; @@ -1638,6 +1639,7 @@ struct VariantHashBase // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN using std::bad_optional_access; using std::optional; using std::make_optional; using std::nullopt_t; using std::nullopt; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_OPTIONAL @@ -65,6 +67,7 @@ using std::nullopt; #include "absl/types/internal/optional.h" namespace absl { +ABSL_NAMESPACE_BEGIN // nullopt_t // @@ -754,6 +757,7 @@ constexpr auto operator>=(const U& v, const optional& x) return static_cast(x) ? static_cast(v >= *x) : true; } +ABSL_NAMESPACE_END } // namespace absl namespace std { diff --git a/absl/types/optional_exception_safety_test.cc b/absl/types/optional_exception_safety_test.cc index 0f7fae6c..8e5fe851 100644 --- a/absl/types/optional_exception_safety_test.cc +++ b/absl/types/optional_exception_safety_test.cc @@ -24,6 +24,7 @@ #include "absl/base/internal/exception_safety_testing.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { @@ -285,6 +286,7 @@ TEST(OptionalExceptionSafety, NothrowMoveAssign) { } // namespace +ABSL_NAMESPACE_END } // namespace absl #endif // #if !defined(ABSL_USES_STD_OPTIONAL) && defined(ABSL_HAVE_EXCEPTIONS) diff --git a/absl/types/span.h b/absl/types/span.h index b007fc1f..3283145a 100644 --- a/absl/types/span.h +++ b/absl/types/span.h @@ -71,6 +71,7 @@ #include "absl/types/internal/span.h" namespace absl { +ABSL_NAMESPACE_BEGIN //------------------------------------------------------------------------------ // Span @@ -707,5 +708,6 @@ template constexpr Span MakeConstSpan(const T (&array)[N]) noexcept { return Span(array, N); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_SPAN_H_ diff --git a/absl/types/variant.h b/absl/types/variant.h index f3558703..776d19a1 100644 --- a/absl/types/variant.h +++ b/absl/types/variant.h @@ -50,6 +50,7 @@ #include // IWYU pragma: export namespace absl { +ABSL_NAMESPACE_BEGIN using std::bad_variant_access; using std::get; using std::get_if; @@ -62,6 +63,7 @@ using std::variant_npos; using std::variant_size; using std::variant_size_v; using std::visit; +ABSL_NAMESPACE_END } // namespace absl #else // ABSL_USES_STD_VARIANT @@ -77,6 +79,7 @@ using std::visit; #include "absl/types/internal/variant.h" namespace absl { +ABSL_NAMESPACE_BEGIN // ----------------------------------------------------------------------------- // absl::variant @@ -795,6 +798,7 @@ operator>=(const variant& a, const variant& b) { a.index()); } +ABSL_NAMESPACE_END } // namespace absl namespace std { @@ -815,6 +819,7 @@ struct hash> #endif // ABSL_USES_STD_VARIANT namespace absl { +ABSL_NAMESPACE_BEGIN namespace variant_internal { // Helper visitor for converting a variant` into another type (mostly @@ -850,6 +855,7 @@ To ConvertVariantTo(Variant&& variant) { std::forward(variant)); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_VARIANT_H_ diff --git a/absl/types/variant_benchmark.cc b/absl/types/variant_benchmark.cc index a5f52164..350b1753 100644 --- a/absl/types/variant_benchmark.cc +++ b/absl/types/variant_benchmark.cc @@ -28,6 +28,7 @@ #include "absl/utility/utility.h" namespace absl { +ABSL_NAMESPACE_BEGIN namespace { template @@ -217,4 +218,5 @@ BENCHMARK_TEMPLATE(BM_RedundantVisit, 4, 2) ->DenseRange(0, integral_pow(4, 2) - 1); } // namespace +ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/types/variant_exception_safety_test.cc b/absl/types/variant_exception_safety_test.cc index b486a71e..439c6e1d 100644 --- a/absl/types/variant_exception_safety_test.cc +++ b/absl/types/variant_exception_safety_test.cc @@ -34,6 +34,7 @@ #if !defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE) namespace absl { +ABSL_NAMESPACE_BEGIN namespace { using ::testing::MakeExceptionSafetyTester; @@ -523,6 +524,7 @@ TEST(VariantExceptionSafetyTest, Swap) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl #endif // !defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE) diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc index 2913775a..96393333 100644 --- a/absl/types/variant_test.cc +++ b/absl/types/variant_test.cc @@ -70,6 +70,7 @@ struct hash { struct NonHashable {}; namespace absl { +ABSL_NAMESPACE_BEGIN namespace { using ::testing::DoubleEq; @@ -2709,6 +2710,7 @@ TEST(VariantTest, MoveCtorBug) { } } // namespace +ABSL_NAMESPACE_END } // namespace absl #endif // #if !defined(ABSL_USES_STD_VARIANT) diff --git a/absl/utility/utility.h b/absl/utility/utility.h index 5a98c2c3..e6647c7b 100644 --- a/absl/utility/utility.h +++ b/absl/utility/utility.h @@ -51,6 +51,7 @@ #include "absl/meta/type_traits.h" namespace absl { +ABSL_NAMESPACE_BEGIN // integer_sequence // @@ -343,6 +344,7 @@ constexpr T make_from_tuple(Tuple&& tup) { std::tuple_size>::value>{}); } +ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_UTILITY_UTILITY_H_ -- cgit v1.2.3 From 3c814105108680997d0821077694f663693b5382 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 14 Feb 2020 09:41:25 -0800 Subject: Export of internal Abseil changes -- 97faa5fdfa4cd5d7a74cd9332cddd8a7c1e67b89 by Abseil Team : Internal changes PiperOrigin-RevId: 295164378 -- 74990f100b3f4172c770ef8c76c05c8e99febdde by Xiaoyi Zhang : Release `absl::Cord`. PiperOrigin-RevId: 295161959 -- 6018c57f43c45c31dc1a61c0cd75fa2aa9be8dab by Gennadiy Rozental : Introduce independent notion of FlagStaticTypeID. This change separates static flag value type identification from the type specific "vtable" with all the operations specific to value type. This change allows us to do the following: * We can move most of "vtable" implementation from handle header, which will become public soon, into implementation details of Abseil Flag. * We can combine back marshalling ops and general ops into a single vtable routine. They were split previously to facilitate type identification without requiring marshalling routines to be exposed in header. * We do not need to store two vtable pointers. We can now store only one. The static type id can be deduced on request. Overall we are saving 24 bytes per flag according to size_tester run. PiperOrigin-RevId: 295149687 -- 986b78e9ba571aa85154e70bda4580edd45bb7bf by Abseil Team : Update internal comments. PiperOrigin-RevId: 295030681 -- 825412b29fd6015027bbc3e5f802706eee0d2837 by Matthew Brown : Change str_format_internal::ConversionChar to an enum (from a struct-wrapped enum). PiperOrigin-RevId: 294987462 -- f9f88d91809d2cc33fc129df70fa93e7a2c35c69 by Derek Mauro : Use more precise wording in the question on live-at-head PiperOrigin-RevId: 294957679 GitOrigin-RevId: 97faa5fdfa4cd5d7a74cd9332cddd8a7c1e67b89 Change-Id: I081e70d148ffac7296d65e2a2f775f643eaf70bf --- FAQ.md | 32 +- absl/debugging/symbolize.h | 2 +- absl/flags/flag.h | 7 +- absl/flags/flag_test.cc | 5 +- absl/flags/internal/commandlineflag.h | 124 +- absl/flags/internal/flag.cc | 76 +- absl/flags/internal/flag.h | 113 +- absl/flags/internal/registry.cc | 12 +- absl/flags/internal/registry.h | 4 +- absl/strings/BUILD.bazel | 69 + absl/strings/CMakeLists.txt | 54 + absl/strings/cord.cc | 2019 ++++++++++++++++++++ absl/strings/cord.h | 1121 +++++++++++ absl/strings/cord_test.cc | 1526 +++++++++++++++ absl/strings/cord_test_helpers.h | 60 + absl/strings/internal/cord_internal.h | 151 ++ absl/strings/internal/str_format/arg.cc | 37 +- absl/strings/internal/str_format/arg.h | 7 +- absl/strings/internal/str_format/arg_test.cc | 2 +- absl/strings/internal/str_format/extension.cc | 11 - absl/strings/internal/str_format/extension.h | 200 +- .../internal/str_format/float_conversion.cc | 18 +- absl/strings/internal/str_format/parser.cc | 6 +- absl/strings/internal/str_format/parser.h | 10 +- absl/strings/internal/str_format/parser_test.cc | 14 +- absl/strings/str_format_test.cc | 2 +- absl/time/duration_test.cc | 2 +- 27 files changed, 5348 insertions(+), 336 deletions(-) create mode 100644 absl/strings/cord.cc create mode 100644 absl/strings/cord.h create mode 100644 absl/strings/cord_test.cc create mode 100644 absl/strings/cord_test_helpers.h create mode 100644 absl/strings/internal/cord_internal.h (limited to 'absl/strings/str_format_test.cc') diff --git a/FAQ.md b/FAQ.md index af721307..78028fc0 100644 --- a/FAQ.md +++ b/FAQ.md @@ -33,8 +33,9 @@ instructions](https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md) for more information. For a longer answer to this question and to understand why some other approaches -don't work, see the answer to "What is ABI and why don't you recommend using a -pre-compiled version of Abseil?" +don't work, see the answer to ["What is ABI and why don't you recommend using a +pre-compiled version of +Abseil?"](#what-is-abi-and-why-dont-you-recommend-using-a-pre-compiled-version-of-abseil) ## What is ABI and why don't you recommend using a pre-compiled version of Abseil? @@ -117,7 +118,8 @@ to make it compatible. In practice, the need to use an automated tool is extremely rare. This means that upgrading from one source release to another should be a routine practice that can and should be performed often. -We recommend you update to the latest release of Abseil as often as +We recommend you update to the [latest commit in the `master` branch of +Abseil](https://github.com/abseil/abseil-cpp/commits/master) as often as possible. Not only will you pick up bug fixes more quickly, but if you have good automated testing, you will catch and be able to fix any [Hyrum's Law](https://www.hyrumslaw.com/) dependency problems on an incremental basis @@ -130,9 +132,27 @@ feature, updating the [`http_archive`](https://docs.bazel.build/versions/master/repo/http.html#http_archive) rule in your [`WORKSPACE`](https://docs.bazel.build/versions/master/be/workspace.html) for -`com_google_abseil` to point to the latest release is all you need to do. You -can commit the updated `WORKSPACE` file to your source control every time you -update, and if you have good automated testing, you might even consider +`com_google_abseil` to point to the [latest commit in the `master` branch of +Abseil](https://github.com/abseil/abseil-cpp/commits/master) is all you need to +do. For example, on February 11, 2020, the latest commit to the master branch +was `98eb410c93ad059f9bba1bf43f5bb916fc92a5ea`. To update to this commit, you +would add the following snippet to your `WORKSPACE` file: + +``` +http_archive( + name = "com_google_absl", + urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"], # 2020-02-11T18:50:53Z + strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea", + sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87", +) +``` + +To get the `sha256` of this URL, run `curl -sL --output - +https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip +| sha256sum -`. + +You can commit the updated `WORKSPACE` file to your source control every time +you update, and if you have good automated testing, you might even consider automating this. One thing we don't recommend is using GitHub's `master.zip` files (for example diff --git a/absl/debugging/symbolize.h b/absl/debugging/symbolize.h index 65f97854..43d93a86 100644 --- a/absl/debugging/symbolize.h +++ b/absl/debugging/symbolize.h @@ -71,7 +71,7 @@ ABSL_NAMESPACE_BEGIN // // Now you can use the symbolizer // } void InitializeSymbolizer(const char* argv0); - +// // Symbolize() // // Symbolizes a program counter (instruction pointer value) `pc` and, on diff --git a/absl/flags/flag.h b/absl/flags/flag.h index 274838cb..cff02c1f 100644 --- a/absl/flags/flag.h +++ b/absl/flags/flag.h @@ -100,12 +100,10 @@ class Flag { // constexpr initializable. #if _MSC_VER <= 1900 constexpr Flag(const char* name, const char* filename, - const flags_internal::FlagMarshallingOpFn marshalling_op, const flags_internal::HelpGenFunc help_gen, const flags_internal::FlagDfltGenFunc default_value_gen) : name_(name), filename_(filename), - marshalling_op_(marshalling_op), help_gen_(help_gen), default_value_gen_(default_value_gen), inited_(false), @@ -121,7 +119,7 @@ class Flag { } impl_ = - new flags_internal::Flag(name_, filename_, marshalling_op_, + new flags_internal::Flag(name_, filename_, {flags_internal::FlagHelpMsg(help_gen_), flags_internal::FlagHelpKind::kGenFunc}, default_value_gen_); @@ -161,7 +159,6 @@ class Flag { // this to be an aggregate type. const char* name_; const char* filename_; - const flags_internal::FlagMarshallingOpFn marshalling_op_; const flags_internal::HelpGenFunc help_gen_; const flags_internal::FlagDfltGenFunc default_value_gen_; @@ -335,7 +332,6 @@ ABSL_NAMESPACE_END ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ - &absl::flags_internal::FlagMarshallingOps, \ absl::flags_internal::HelpArg(0), \ &AbslFlagsInitFlag##name}; \ extern bool FLAGS_no##name; \ @@ -349,7 +345,6 @@ ABSL_NAMESPACE_END ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ - &absl::flags_internal::FlagMarshallingOps, \ &AbslFlagHelpGenFor##name::NonConst, &AbslFlagsInitFlag##name}; \ extern bool FLAGS_no##name; \ bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc index 6429a3e1..4984d284 100644 --- a/absl/flags/flag_test.cc +++ b/absl/flags/flag_test.cc @@ -53,14 +53,13 @@ template bool TestConstructionFor() { constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"), flags::FlagHelpKind::kLiteral}; - constexpr flags::Flag f1("f1", "file", &flags::FlagMarshallingOps, - help_arg, &TestMakeDflt); + constexpr flags::Flag f1("f1", "file", help_arg, &TestMakeDflt); EXPECT_EQ(f1.Name(), "f1"); EXPECT_EQ(f1.Help(), "literal help"); EXPECT_EQ(f1.Filename(), "file"); ABSL_CONST_INIT static flags::Flag f2( - "f2", "file", &flags::FlagMarshallingOps, + "f2", "file", {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, &TestMakeDflt); flags::FlagRegistrar(&f2).OnUpdate(TestCallback); diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h index 4ac50190..6363c661 100644 --- a/absl/flags/internal/commandlineflag.h +++ b/absl/flags/internal/commandlineflag.h @@ -34,22 +34,23 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace flags_internal { -// Type-specific operations, eg., parsing, copying, etc. are provided -// by function specific to that type with a signature matching FlagOpFn. -enum FlagOp { - kDelete, - kClone, - kCopy, - kCopyConstruct, - kSizeof, - kParse, - kUnparse, +// An alias for flag static type id. Values of type identify the flag value type +// simialarly to typeid(T), but without relying on RTTI being available. In most +// cases this id is enough to uniquely identify the flag's value type. In a few +// cases we'll have to resort to using actual RTTI implementation if it is +// available. +using FlagStaticTypeId = void* (*)(); + +// Address of this function template is used in current implementation as a flag +// static type id. +template +void* FlagStaticTypeIdGen() { #if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI) - kRuntimeTypeId + return const_cast(&typeid(T)); +#else + return nullptr; #endif -}; -using FlagOpFn = void* (*)(FlagOp, const void*, void*); -using FlagMarshallingOpFn = void* (*)(FlagOp, const void*, void*, void*); +} // Options that control SetCommandLineOptionWithMode. enum FlagSettingMode { @@ -72,97 +73,6 @@ enum ValueSource { kProgrammaticChange, }; -// The per-type function -template -void* FlagOps(FlagOp op, const void* v1, void* v2) { - switch (op) { - case kDelete: - delete static_cast(v1); - return nullptr; - case kClone: - return new T(*static_cast(v1)); - case kCopy: - *static_cast(v2) = *static_cast(v1); - return nullptr; - case kCopyConstruct: - new (v2) T(*static_cast(v1)); - return nullptr; - case kSizeof: - return reinterpret_cast(sizeof(T)); -#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI) - case kRuntimeTypeId: - return const_cast(&typeid(T)); - break; -#endif - default: - return nullptr; - } -} - -template -void* FlagMarshallingOps(FlagOp op, const void* v1, void* v2, void* v3) { - switch (op) { - case kParse: { - // initialize the temporary instance of type T based on current value in - // destination (which is going to be flag's default value). - T temp(*static_cast(v2)); - if (!absl::ParseFlag(*static_cast(v1), &temp, - static_cast(v3))) { - return nullptr; - } - *static_cast(v2) = std::move(temp); - return v2; - } - case kUnparse: - *static_cast(v2) = - absl::UnparseFlag(*static_cast(v1)); - return nullptr; - default: - return nullptr; - } -} - -// Functions that invoke flag-type-specific operations. -inline void Delete(FlagOpFn op, const void* obj) { - op(flags_internal::kDelete, obj, nullptr); -} - -inline void* Clone(FlagOpFn op, const void* obj) { - return op(flags_internal::kClone, obj, nullptr); -} - -inline void Copy(FlagOpFn op, const void* src, void* dst) { - op(flags_internal::kCopy, src, dst); -} - -inline void CopyConstruct(FlagOpFn op, const void* src, void* dst) { - op(flags_internal::kCopyConstruct, src, dst); -} - -inline bool Parse(FlagMarshallingOpFn op, absl::string_view text, void* dst, - std::string* error) { - return op(flags_internal::kParse, &text, dst, error) != nullptr; -} - -inline std::string Unparse(FlagMarshallingOpFn op, const void* val) { - std::string result; - op(flags_internal::kUnparse, val, &result, nullptr); - return result; -} - -inline size_t Sizeof(FlagOpFn op) { - // This sequence of casts reverses the sequence from base::internal::FlagOps() - return static_cast(reinterpret_cast( - op(flags_internal::kSizeof, nullptr, nullptr))); -} - -#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI) -inline const std::type_info& RuntimeTypeId(FlagOpFn op) { - return *static_cast( - op(flags_internal::kRuntimeTypeId, nullptr, nullptr)); -} -#endif - // Handle to FlagState objects. Specific flag state objects will restore state // of a flag produced this flag state from method CommandLineFlag::SaveState(). class FlagStateInterface { @@ -187,7 +97,7 @@ class CommandLineFlag { // Return true iff flag has type T. template inline bool IsOfType() const { - return TypeId() == &flags_internal::FlagOps; + return TypeId() == &flags_internal::FlagStaticTypeIdGen; } // Attempts to retrieve the flag value. Returns value on success, @@ -240,7 +150,7 @@ class CommandLineFlag { // Returns true iff this is a handle to an Abseil Flag. virtual bool IsAbseilFlag() const { return true; } // Returns id of the flag's value type. - virtual flags_internal::FlagOpFn TypeId() const = 0; + virtual FlagStaticTypeId TypeId() const = 0; virtual bool IsModified() const = 0; virtual bool IsSpecifiedOnCommandLine() const = 0; virtual std::string DefaultValue() const = 0; diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 721e411e..83ec8df1 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -47,23 +47,15 @@ const char kStrippedFlagHelp[] = "\001\002\003\004 (unknown) \004\003\002\001"; namespace { // Currently we only validate flag values for user-defined flag types. -bool ShouldValidateFlagValue(FlagOpFn flag_type_id) { +bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) { #define DONT_VALIDATE(T) \ - if (flag_type_id == &flags_internal::FlagOps) return false; + if (flag_type_id == &FlagStaticTypeIdGen) return false; ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE) #undef DONT_VALIDATE return true; } -#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI) -bool MatchRuntimeTypeId(FlagOpFn lhs_type_id, FlagOpFn rhs_type_id) { - return RuntimeTypeId(lhs_type_id) == RuntimeTypeId(rhs_type_id); -} -#else -bool MatchRuntimeTypeId(FlagOpFn, FlagOpFn) { return true; } -#endif - // RAII helper used to temporarily unlock and relock `absl::Mutex`. // This is used when we need to ensure that locks are released while // invoking user supplied callbacks and then reacquired, since callbacks may @@ -101,22 +93,35 @@ absl::Mutex* FlagImpl::DataGuard() const { return reinterpret_cast(&data_guard_); } -void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const { - // `op` is the unmarshaling operation corresponding to the declaration - // visibile at the call site. `op_` is the Flag's defined unmarshalling - // operation. They must match for this operation to be well-defined. - if (ABSL_PREDICT_FALSE(op != op_) && !MatchRuntimeTypeId(op, op_)) { - ABSL_INTERNAL_LOG( - FATAL, - absl::StrCat("Flag '", Name(), - "' is defined as one type and declared as another")); - } +void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const { + FlagStaticTypeId this_type_id = flags_internal::StaticTypeId(op_); + + // `type_id` is the type id corresponding to the declaration visibile at the + // call site. `this_type_id` is the type id corresponding to the type stored + // during flag definition. They must match for this operation to be + // well-defined. + if (ABSL_PREDICT_TRUE(type_id == this_type_id)) return; + + void* lhs_runtime_type_id = type_id(); + void* rhs_runtime_type_id = this_type_id(); + + if (lhs_runtime_type_id == rhs_runtime_type_id) return; + +#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI) + if (*reinterpret_cast(lhs_runtime_type_id) == + *reinterpret_cast(rhs_runtime_type_id)) + return; +#endif + + ABSL_INTERNAL_LOG( + FATAL, absl::StrCat("Flag '", Name(), + "' is defined as one type and declared as another")); } std::unique_ptr FlagImpl::MakeInitValue() const { void* res = nullptr; if (DefaultKind() == FlagDefaultKind::kDynamicValue) { - res = Clone(op_, default_src_.dynamic_value); + res = flags_internal::Clone(op_, default_src_.dynamic_value); } else { res = (*default_src_.gen_func)(); } @@ -148,13 +153,13 @@ std::string FlagImpl::DefaultValue() const { absl::MutexLock l(DataGuard()); auto obj = MakeInitValue(); - return Unparse(marshalling_op_, obj.get()); + return flags_internal::Unparse(op_, obj.get()); } std::string FlagImpl::CurrentValue() const { absl::MutexLock l(DataGuard()); - return Unparse(marshalling_op_, value_.dynamic); + return flags_internal::Unparse(op_, value_.dynamic); } void FlagImpl::SetCallback(const FlagCallbackFunc mutation_callback) { @@ -220,7 +225,7 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value, auto tentative_value = MakeInitValue(); std::string parse_err; - if (!Parse(marshalling_op_, value, tentative_value.get(), &parse_err)) { + if (!flags_internal::Parse(op_, value, tentative_value.get(), &parse_err)) { absl::string_view err_sep = parse_err.empty() ? "" : "; "; *err = absl::StrCat("Illegal value '", value, "' specified for flag '", Name(), "'", err_sep, parse_err); @@ -237,11 +242,11 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value, void FlagImpl::Read(void* dst) const { absl::ReaderMutexLock l(DataGuard()); - CopyConstruct(op_, value_.dynamic, dst); + flags_internal::CopyConstruct(op_, value_.dynamic, dst); } void FlagImpl::StoreAtomic() { - size_t data_size = Sizeof(op_); + size_t data_size = flags_internal::Sizeof(op_); if (data_size <= sizeof(int64_t)) { int64_t t = 0; @@ -260,20 +265,20 @@ void FlagImpl::StoreAtomic() { void FlagImpl::Write(const void* src) { absl::MutexLock l(DataGuard()); - if (ShouldValidateFlagValue(op_)) { - void* obj = Clone(op_, src); + if (ShouldValidateFlagValue(flags_internal::StaticTypeId(op_))) { + void* obj = flags_internal::Clone(op_, src); std::string ignored_error; - std::string src_as_str = Unparse(marshalling_op_, src); - if (!Parse(marshalling_op_, src_as_str, obj, &ignored_error)) { + std::string src_as_str = flags_internal::Unparse(op_, src); + if (!flags_internal::Parse(op_, src_as_str, obj, &ignored_error)) { ABSL_INTERNAL_LOG(ERROR, absl::StrCat("Attempt to set flag '", Name(), "' to invalid value ", src_as_str)); } - Delete(op_, obj); + flags_internal::Delete(op_, obj); } modified_ = true; counter_++; - Copy(op_, src, value_.dynamic); + flags_internal::Copy(op_, src, value_.dynamic); StoreAtomic(); InvokeCallback(); @@ -341,7 +346,7 @@ bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode, if (!modified_) { // Need to set both default value *and* current, in this case - Copy(op_, default_src_.dynamic_value, value_.dynamic); + flags_internal::Copy(op_, default_src_.dynamic_value, value_.dynamic); StoreAtomic(); InvokeCallback(); } @@ -359,7 +364,7 @@ void FlagImpl::CheckDefaultValueParsingRoundtrip() const { auto dst = MakeInitValue(); std::string error; - if (!flags_internal::Parse(marshalling_op_, v, dst.get(), &error)) { + if (!flags_internal::Parse(op_, v, dst.get(), &error)) { ABSL_INTERNAL_LOG( FATAL, absl::StrCat("Flag ", Name(), " (from ", Filename(), @@ -376,8 +381,7 @@ bool FlagImpl::ValidateInputValue(absl::string_view value) const { auto obj = MakeInitValue(); std::string ignored_error; - return flags_internal::Parse(marshalling_op_, value, obj.get(), - &ignored_error); + return flags_internal::Parse(op_, value, obj.get(), &ignored_error); } } // namespace flags_internal diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index b426ccb5..1c2f15dd 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -42,6 +42,94 @@ namespace flags_internal { template class Flag; +/////////////////////////////////////////////////////////////////////////////// +// Type-specific operations, eg., parsing, copying, etc. are provided +// by function specific to that type with a signature matching FlagOpFn. + +enum FlagOp { + kDelete, + kClone, + kCopy, + kCopyConstruct, + kSizeof, + kStaticTypeId, + kParse, + kUnparse, +}; +using FlagOpFn = void* (*)(FlagOp, const void*, void*, void*); + +// The per-type function +template +void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) { + switch (op) { + case flags_internal::kDelete: + delete static_cast(v1); + return nullptr; + case flags_internal::kClone: + return new T(*static_cast(v1)); + case flags_internal::kCopy: + *static_cast(v2) = *static_cast(v1); + return nullptr; + case flags_internal::kCopyConstruct: + new (v2) T(*static_cast(v1)); + return nullptr; + case flags_internal::kSizeof: + return reinterpret_cast(sizeof(T)); + case flags_internal::kStaticTypeId: + return reinterpret_cast(&FlagStaticTypeIdGen); + case flags_internal::kParse: { + // Initialize the temporary instance of type T based on current value in + // destination (which is going to be flag's default value). + T temp(*static_cast(v2)); + if (!absl::ParseFlag(*static_cast(v1), &temp, + static_cast(v3))) { + return nullptr; + } + *static_cast(v2) = std::move(temp); + return v2; + } + case flags_internal::kUnparse: + *static_cast(v2) = + absl::UnparseFlag(*static_cast(v1)); + return nullptr; + default: + return nullptr; + } +} + +// Functions that invoke flag-type-specific operations. +inline void Delete(FlagOpFn op, const void* obj) { + op(flags_internal::kDelete, obj, nullptr, nullptr); +} +inline void* Clone(FlagOpFn op, const void* obj) { + return op(flags_internal::kClone, obj, nullptr, nullptr); +} +inline void Copy(FlagOpFn op, const void* src, void* dst) { + op(flags_internal::kCopy, src, dst, nullptr); +} +inline void CopyConstruct(FlagOpFn op, const void* src, void* dst) { + op(flags_internal::kCopyConstruct, src, dst, nullptr); +} +inline bool Parse(FlagOpFn op, absl::string_view text, void* dst, + std::string* error) { + return op(flags_internal::kParse, &text, dst, error) != nullptr; +} +inline std::string Unparse(FlagOpFn op, const void* val) { + std::string result; + op(flags_internal::kUnparse, val, &result, nullptr); + return result; +} +inline size_t Sizeof(FlagOpFn op) { + // This sequence of casts reverses the sequence from + // `flags_internal::FlagOps()` + return static_cast(reinterpret_cast( + op(flags_internal::kSizeof, nullptr, nullptr, nullptr))); +} +inline FlagStaticTypeId StaticTypeId(FlagOpFn op) { + return reinterpret_cast( + op(flags_internal::kStaticTypeId, nullptr, nullptr, nullptr)); +} + /////////////////////////////////////////////////////////////////////////////// // Persistent state of the flag data. @@ -273,12 +361,10 @@ struct DynValueDeleter { class FlagImpl { public: constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op, - FlagMarshallingOpFn marshalling_op, FlagHelpArg help, - FlagDfltGenFunc default_value_gen) + FlagHelpArg help, FlagDfltGenFunc default_value_gen) : name_(name), filename_(filename), op_(op), - marshalling_op_(marshalling_op), help_(help.source), help_source_kind_(static_cast(help.kind)), def_kind_(static_cast(FlagDefaultKind::kGenFunc)), @@ -306,7 +392,7 @@ class FlagImpl { template ::value, int>::type = 0> void Get(T* dst) const { - AssertValidType(&flags_internal::FlagOps); + AssertValidType(&flags_internal::FlagStaticTypeIdGen); Read(dst); } // Overload for `GetFlag()` for types that support lock-free reads. @@ -317,7 +403,7 @@ class FlagImpl { // slowing down flag value access due to type validation. That's why // this validation is hidden behind !NDEBUG #ifndef NDEBUG - AssertValidType(&flags_internal::FlagOps); + AssertValidType(&flags_internal::FlagStaticTypeIdGen); #endif using U = flags_internal::BestAtomicType; typename U::type r = value_.atomics.template load(); @@ -329,7 +415,7 @@ class FlagImpl { } template void Set(const T& src) { - AssertValidType(&flags_internal::FlagOps); + AssertValidType(&flags_internal::FlagStaticTypeIdGen); Write(&src); } @@ -388,7 +474,7 @@ class FlagImpl { // int. To do that we pass the "assumed" type id (which is deduced from type // int) as an argument `op`, which is in turn is validated against the type id // stored in flag object by flag definition statement. - void AssertValidType(const flags_internal::FlagOpFn op) const; + void AssertValidType(FlagStaticTypeId type_id) const; // Immutable flag's state. @@ -396,10 +482,8 @@ class FlagImpl { const char* const name_; // The file name where ABSL_FLAG resides. const char* const filename_; - // Type-specific handler. + // Type-specific operations "vtable". const FlagOpFn op_; - // Marshalling ops handler. - const FlagMarshallingOpFn marshalling_op_; // Help message literal or function to generate it. const FlagHelpMsg help_; // Indicates if help message was supplied as literal or generator func. @@ -456,12 +540,9 @@ class FlagImpl { template class Flag final : public flags_internal::CommandLineFlag { public: - constexpr Flag(const char* name, const char* filename, - const FlagMarshallingOpFn marshalling_op, - const FlagHelpArg help, + constexpr Flag(const char* name, const char* filename, const FlagHelpArg help, const FlagDfltGenFunc default_value_gen) - : impl_(name, filename, &FlagOps, marshalling_op, help, - default_value_gen) {} + : impl_(name, filename, &FlagOps, help, default_value_gen) {} T Get() const { // See implementation notes in CommandLineFlag::Get(). @@ -520,7 +601,7 @@ class Flag final : public flags_internal::CommandLineFlag { friend class FlagState; void Read(void* dst) const override { impl_.Read(dst); } - FlagOpFn TypeId() const override { return &FlagOps; } + FlagStaticTypeId TypeId() const override { return &FlagStaticTypeIdGen; } // Flag's data FlagImpl impl_; diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc index 2ef16e84..e434a859 100644 --- a/absl/flags/internal/registry.cc +++ b/absl/flags/internal/registry.cc @@ -284,14 +284,14 @@ namespace { class RetiredFlagObj final : public flags_internal::CommandLineFlag { public: - constexpr RetiredFlagObj(const char* name, FlagOpFn ops) - : name_(name), op_(ops) {} + constexpr RetiredFlagObj(const char* name, FlagStaticTypeId type_id) + : name_(name), type_id_(type_id) {} private: absl::string_view Name() const override { return name_; } std::string Filename() const override { return "RETIRED"; } absl::string_view Typename() const override { return ""; } - flags_internal::FlagOpFn TypeId() const override { return op_; } + FlagStaticTypeId TypeId() const override { return type_id_; } std::string Help() const override { return ""; } bool IsRetired() const override { return true; } bool IsModified() const override { return false; } @@ -317,7 +317,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag { // Data members const char* const name_; - const FlagOpFn op_; + const FlagStaticTypeId type_id_; }; void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) { @@ -327,8 +327,8 @@ void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) { } // namespace -bool Retire(const char* name, FlagOpFn ops) { - auto* flag = new flags_internal::RetiredFlagObj(name, ops); +bool Retire(const char* name, FlagStaticTypeId type_id) { + auto* flag = new flags_internal::RetiredFlagObj(name, type_id); FlagRegistry::GlobalRegistry()->RegisterFlag(flag); return true; } diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h index 99cb685b..69ff889f 100644 --- a/absl/flags/internal/registry.h +++ b/absl/flags/internal/registry.h @@ -79,12 +79,12 @@ bool RegisterCommandLineFlag(CommandLineFlag*); // // Retire flag with name "name" and type indicated by ops. -bool Retire(const char* name, FlagOpFn ops); +bool Retire(const char* name, FlagStaticTypeId type_id); // Registered a retired flag with name 'flag_name' and type 'T'. template inline bool RetiredFlag(const char* flag_name) { - return flags_internal::Retire(flag_name, flags_internal::FlagOps); + return flags_internal::Retire(flag_name, &FlagStaticTypeIdGen); } // If the flag is retired, returns true and indicates in |*type_is_bool| diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index d5a362d0..b950ec76 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -126,6 +126,7 @@ cc_test( copts = ABSL_TEST_COPTS, visibility = ["//visibility:private"], deps = [ + ":cord", ":strings", "//absl/base:core_headers", "//absl/container:fixed_array", @@ -250,6 +251,74 @@ cc_test( ], ) +cc_library( + name = "cord_internal", + hdrs = ["internal/cord_internal.h"], + copts = ABSL_DEFAULT_COPTS, + visibility = ["//visibility:private"], + deps = [ + ":strings", + "//absl/meta:type_traits", + ], +) + +cc_library( + name = "cord", + srcs = [ + "cord.cc", + ], + hdrs = [ + "cord.h", + ], + copts = ABSL_DEFAULT_COPTS, + deps = [ + ":cord_internal", + ":internal", + ":str_format", + ":strings", + "//absl/base", + "//absl/base:base_internal", + "//absl/base:core_headers", + "//absl/base:endian", + "//absl/base:raw_logging_internal", + "//absl/container:fixed_array", + "//absl/container:inlined_vector", + "//absl/functional:function_ref", + "//absl/meta:type_traits", + ], +) + +cc_library( + name = "cord_test_helpers", + testonly = 1, + hdrs = [ + "cord_test_helpers.h", + ], + copts = ABSL_DEFAULT_COPTS, + deps = [ + ":cord", + ], +) + +cc_test( + name = "cord_test", + size = "medium", + srcs = ["cord_test.cc"], + copts = ABSL_TEST_COPTS, + visibility = ["//visibility:private"], + deps = [ + ":cord", + ":cord_test_helpers", + ":strings", + "//absl/base", + "//absl/base:config", + "//absl/base:endian", + "//absl/base:raw_logging_internal", + "//absl/container:fixed_array", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "substitute_test", size = "small", diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt index 3feb5e94..cebc5928 100644 --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -526,3 +526,57 @@ absl_cc_test( absl::str_format gmock_main ) + +absl_cc_library( + NAME + cord + HDRS + "cord.h" + SRCS + "cord.cc" + "internal/cord_internal.h" + COPTS + ${ABSL_DEFAULT_COPTS} + DEPS + absl::strings_internal + absl::base + absl::base_internal + absl::core_headers + absl::endian + absl::fixed_array + absl::function_ref + absl::inlined_vector + absl::raw_logging_internal + absl::type_traits + PUBLIC +) + +absl_cc_library( + NAME + cord_test_helpers + HDRS + "cord_test_helpers.h" + COPTS + ${ABSL_TEST_COPTS} + DEPS + absl::cord + TESTONLY +) + +absl_cc_test( + NAME + cord_test + SRCS + "cord_test.cc" + COPTS + ${ABSL_TEST_COPTS} + DEPS + absl::cord + absl::strings + absl::base + absl::config + absl::endian + absl::raw_logging_internal + absl::fixed_array + gmock_main +) diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc new file mode 100644 index 00000000..cc0cc9d7 --- /dev/null +++ b/absl/strings/cord.cc @@ -0,0 +1,2019 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/strings/cord.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/base/casts.h" +#include "absl/base/internal/raw_logging.h" +#include "absl/base/port.h" +#include "absl/container/fixed_array.h" +#include "absl/container/inlined_vector.h" +#include "absl/strings/escaping.h" +#include "absl/strings/internal/cord_internal.h" +#include "absl/strings/internal/resize_uninitialized.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN + +using ::absl::cord_internal::CordRep; +using ::absl::cord_internal::CordRepConcat; +using ::absl::cord_internal::CordRepExternal; +using ::absl::cord_internal::CordRepSubstring; + +// Various representations that we allow +enum CordRepKind { + CONCAT = 0, + EXTERNAL = 1, + SUBSTRING = 2, + + // We have different tags for different sized flat arrays, + // starting with FLAT + FLAT = 3, +}; + +namespace { + +// Type used with std::allocator for allocating and deallocating +// `CordRepExternal`. std::allocator is used because it opaquely handles the +// different new / delete overloads available on a given platform. +using ExternalAllocType = + absl::aligned_storage_t; + +// Returns the number of objects to pass in to std::allocator +// allocate() and deallocate() to create enough room for `CordRepExternal` with +// `releaser_size` bytes on the end. +constexpr size_t GetExternalAllocNumObjects(size_t releaser_size) { + // Be sure to round up since `releaser_size` could be smaller than + // sizeof(ExternalAllocType)`. + return (sizeof(CordRepExternal) + releaser_size + sizeof(ExternalAllocType) - + 1) / + sizeof(ExternalAllocType); +} + +// Allocates enough memory for `CordRepExternal` and a releaser with size +// `releaser_size` bytes. +void* AllocateExternal(size_t releaser_size) { + return std::allocator().allocate( + GetExternalAllocNumObjects(releaser_size)); +} + +// Deallocates the memory for a `CordRepExternal` assuming it was allocated with +// a releaser of given size and alignment. +void DeallocateExternal(CordRepExternal* p, size_t releaser_size) { + std::allocator().deallocate( + reinterpret_cast(p), + GetExternalAllocNumObjects(releaser_size)); +} + +// Returns a pointer to the type erased releaser for the given CordRepExternal. +void* GetExternalReleaser(CordRepExternal* rep) { + return rep + 1; +} + +} // namespace + +namespace cord_internal { + +inline CordRepConcat* CordRep::concat() { + assert(tag == CONCAT); + return static_cast(this); +} + +inline const CordRepConcat* CordRep::concat() const { + assert(tag == CONCAT); + return static_cast(this); +} + +inline CordRepSubstring* CordRep::substring() { + assert(tag == SUBSTRING); + return static_cast(this); +} + +inline const CordRepSubstring* CordRep::substring() const { + assert(tag == SUBSTRING); + return static_cast(this); +} + +inline CordRepExternal* CordRep::external() { + assert(tag == EXTERNAL); + return static_cast(this); +} + +inline const CordRepExternal* CordRep::external() const { + assert(tag == EXTERNAL); + return static_cast(this); +} + +} // namespace cord_internal + +static const size_t kFlatOverhead = offsetof(CordRep, data); + +static_assert(kFlatOverhead == 13, "Unittests assume kFlatOverhead == 13"); + +// Largest and smallest flat node lengths we are willing to allocate +// Flat allocation size is stored in tag, which currently can encode sizes up +// to 4K, encoded as multiple of either 8 or 32 bytes. +// If we allow for larger sizes, we need to change this to 8/64, 16/128, etc. +static constexpr size_t kMaxFlatSize = 4096; +static constexpr size_t kMaxFlatLength = kMaxFlatSize - kFlatOverhead; +static constexpr size_t kMinFlatLength = 32 - kFlatOverhead; + +// Prefer copying blocks of at most this size, otherwise reference count. +static const size_t kMaxBytesToCopy = 511; + +// Helper functions for rounded div, and rounding to exact sizes. +static size_t DivUp(size_t n, size_t m) { return (n + m - 1) / m; } +static size_t RoundUp(size_t n, size_t m) { return DivUp(n, m) * m; } + +// Returns the size to the nearest equal or larger value that can be +// expressed exactly as a tag value. +static size_t RoundUpForTag(size_t size) { + return RoundUp(size, (size <= 1024) ? 8 : 32); +} + +// Converts the allocated size to a tag, rounding down if the size +// does not exactly match a 'tag expressible' size value. The result is +// undefined if the size exceeds the maximum size that can be encoded in +// a tag, i.e., if size is larger than TagToAllocatedSize(). +static uint8_t AllocatedSizeToTag(size_t size) { + const size_t tag = (size <= 1024) ? size / 8 : 128 + size / 32 - 1024 / 32; + assert(tag <= std::numeric_limits::max()); + return tag; +} + +// Converts the provided tag to the corresponding allocated size +static constexpr size_t TagToAllocatedSize(uint8_t tag) { + return (tag <= 128) ? (tag * 8) : (1024 + (tag - 128) * 32); +} + +// Converts the provided tag to the corresponding available data length +static constexpr size_t TagToLength(uint8_t tag) { + return TagToAllocatedSize(tag) - kFlatOverhead; +} + +// Enforce that kMaxFlatSize maps to a well-known exact tag value. +static_assert(TagToAllocatedSize(224) == kMaxFlatSize, "Bad tag logic"); + +constexpr uint64_t Fibonacci(unsigned char n, uint64_t a = 0, uint64_t b = 1) { + return n == 0 ? a : Fibonacci(n - 1, b, a + b); +} + +static_assert(Fibonacci(63) == 6557470319842, + "Fibonacci values computed incorrectly"); + +// Minimum length required for a given depth tree -- a tree is considered +// balanced if +// length(t) >= min_length[depth(t)] +// The root node depth is allowed to become twice as large to reduce rebalancing +// for larger strings (see IsRootBalanced). +static constexpr uint64_t min_length[] = { + Fibonacci(2), + Fibonacci(3), + Fibonacci(4), + Fibonacci(5), + Fibonacci(6), + Fibonacci(7), + Fibonacci(8), + Fibonacci(9), + Fibonacci(10), + Fibonacci(11), + Fibonacci(12), + Fibonacci(13), + Fibonacci(14), + Fibonacci(15), + Fibonacci(16), + Fibonacci(17), + Fibonacci(18), + Fibonacci(19), + Fibonacci(20), + Fibonacci(21), + Fibonacci(22), + Fibonacci(23), + Fibonacci(24), + Fibonacci(25), + Fibonacci(26), + Fibonacci(27), + Fibonacci(28), + Fibonacci(29), + Fibonacci(30), + Fibonacci(31), + Fibonacci(32), + Fibonacci(33), + Fibonacci(34), + Fibonacci(35), + Fibonacci(36), + Fibonacci(37), + Fibonacci(38), + Fibonacci(39), + Fibonacci(40), + Fibonacci(41), + Fibonacci(42), + Fibonacci(43), + Fibonacci(44), + Fibonacci(45), + Fibonacci(46), + Fibonacci(47), + 0xffffffffffffffffull, // Avoid overflow +}; + +static const int kMinLengthSize = ABSL_ARRAYSIZE(min_length); + +// The inlined size to use with absl::InlinedVector. +// +// Note: The InlinedVectors in this file (and in cord.h) do not need to use +// the same value for their inlined size. The fact that they do is historical. +// It may be desirable for each to use a different inlined size optimized for +// that InlinedVector's usage. +// +// TODO(jgm): Benchmark to see if there's a more optimal value than 47 for +// the inlined vector size (47 exists for backward compatibility). +static const int kInlinedVectorSize = 47; + +static inline bool IsRootBalanced(CordRep* node) { + if (node->tag != CONCAT) { + return true; + } else if (node->concat()->depth() <= 15) { + return true; + } else if (node->concat()->depth() > kMinLengthSize) { + return false; + } else { + // Allow depth to become twice as large as implied by fibonacci rule to + // reduce rebalancing for larger strings. + return (node->length >= min_length[node->concat()->depth() / 2]); + } +} + +static CordRep* Rebalance(CordRep* node); +static void DumpNode(CordRep* rep, bool include_data, std::ostream* os); +static bool VerifyNode(CordRep* root, CordRep* start_node, + bool full_validation); + +static inline CordRep* VerifyTree(CordRep* node) { + // Verification is expensive, so only do it in debug mode. + // Even in debug mode we normally do only light validation. + // If you are debugging Cord itself, you should define the + // macro EXTRA_CORD_VALIDATION, e.g. by adding + // --copt=-DEXTRA_CORD_VALIDATION to the blaze line. +#ifdef EXTRA_CORD_VALIDATION + assert(node == nullptr || VerifyNode(node, node, /*full_validation=*/true)); +#else // EXTRA_CORD_VALIDATION + assert(node == nullptr || VerifyNode(node, node, /*full_validation=*/false)); +#endif // EXTRA_CORD_VALIDATION + static_cast(&VerifyNode); + + return node; +} + +// -------------------------------------------------------------------- +// Memory management + +inline CordRep* Ref(CordRep* rep) { + if (rep != nullptr) { + rep->refcount.Increment(); + } + return rep; +} + +// This internal routine is called from the cold path of Unref below. Keeping it +// in a separate routine allows good inlining of Unref into many profitable call +// sites. However, the call to this function can be highly disruptive to the +// register pressure in those callers. To minimize the cost to callers, we use +// a special LLVM calling convention that preserves most registers. This allows +// the call to this routine in cold paths to not disrupt the caller's register +// pressure. This calling convention is not available on all platforms; we +// intentionally allow LLVM to ignore the attribute rather than attempting to +// hardcode the list of supported platforms. +#if defined(__clang__) && !defined(__i386__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wattributes" +__attribute__((preserve_most)) +#pragma clang diagnostic pop +#endif +static void UnrefInternal(CordRep* rep) { + assert(rep != nullptr); + + absl::InlinedVector pending; + while (true) { + if (rep->tag == CONCAT) { + CordRepConcat* rep_concat = rep->concat(); + CordRep* right = rep_concat->right; + if (!right->refcount.Decrement()) { + pending.push_back(right); + } + CordRep* left = rep_concat->left; + delete rep_concat; + rep = nullptr; + if (!left->refcount.Decrement()) { + rep = left; + continue; + } + } else if (rep->tag == EXTERNAL) { + CordRepExternal* rep_external = rep->external(); + absl::string_view data(rep_external->base, rep->length); + void* releaser = GetExternalReleaser(rep_external); + size_t releaser_size = rep_external->releaser_invoker(releaser, data); + rep_external->~CordRepExternal(); + DeallocateExternal(rep_external, releaser_size); + rep = nullptr; + } else if (rep->tag == SUBSTRING) { + CordRepSubstring* rep_substring = rep->substring(); + CordRep* child = rep_substring->child; + delete rep_substring; + rep = nullptr; + if (!child->refcount.Decrement()) { + rep = child; + continue; + } + } else { + // Flat CordReps are allocated and constructed with raw ::operator new + // and placement new, and must be destructed and deallocated + // accordingly. +#if defined(__cpp_sized_deallocation) + size_t size = TagToAllocatedSize(rep->tag); + rep->~CordRep(); + ::operator delete(rep, size); +#else + rep->~CordRep(); + ::operator delete(rep); +#endif + rep = nullptr; + } + + if (!pending.empty()) { + rep = pending.back(); + pending.pop_back(); + } else { + break; + } + } +} + +inline void Unref(CordRep* rep) { + // Fast-path for two common, hot cases: a null rep and a shared root. + if (ABSL_PREDICT_TRUE(rep == nullptr || + rep->refcount.DecrementExpectHighRefcount())) { + return; + } + + UnrefInternal(rep); +} + +// Return the depth of a node +static int Depth(const CordRep* rep) { + if (rep->tag == CONCAT) { + return rep->concat()->depth(); + } else { + return 0; + } +} + +static void SetConcatChildren(CordRepConcat* concat, CordRep* left, + CordRep* right) { + concat->left = left; + concat->right = right; + + concat->length = left->length + right->length; + concat->set_depth(1 + std::max(Depth(left), Depth(right))); +} + +// Create a concatenation of the specified nodes. +// Does not change the refcounts of "left" and "right". +// The returned node has a refcount of 1. +static CordRep* RawConcat(CordRep* left, CordRep* right) { + // Avoid making degenerate concat nodes (one child is empty) + if (left == nullptr || left->length == 0) { + Unref(left); + return right; + } + if (right == nullptr || right->length == 0) { + Unref(right); + return left; + } + + CordRepConcat* rep = new CordRepConcat(); + rep->tag = CONCAT; + SetConcatChildren(rep, left, right); + + return rep; +} + +static CordRep* Concat(CordRep* left, CordRep* right) { + CordRep* rep = RawConcat(left, right); + if (rep != nullptr && !IsRootBalanced(rep)) { + rep = Rebalance(rep); + } + return VerifyTree(rep); +} + +// Make a balanced tree out of an array of leaf nodes. +static CordRep* MakeBalancedTree(CordRep** reps, size_t n) { + // Make repeated passes over the array, merging adjacent pairs + // until we are left with just a single node. + while (n > 1) { + size_t dst = 0; + for (size_t src = 0; src < n; src += 2) { + if (src + 1 < n) { + reps[dst] = Concat(reps[src], reps[src + 1]); + } else { + reps[dst] = reps[src]; + } + dst++; + } + n = dst; + } + + return reps[0]; +} + +// Create a new flat node. +static CordRep* NewFlat(size_t length_hint) { + if (length_hint <= kMinFlatLength) { + length_hint = kMinFlatLength; + } else if (length_hint > kMaxFlatLength) { + length_hint = kMaxFlatLength; + } + + // Round size up so it matches a size we can exactly express in a tag. + const size_t size = RoundUpForTag(length_hint + kFlatOverhead); + void* const raw_rep = ::operator new(size); + CordRep* rep = new (raw_rep) CordRep(); + rep->tag = AllocatedSizeToTag(size); + return VerifyTree(rep); +} + +// Create a new tree out of the specified array. +// The returned node has a refcount of 1. +static CordRep* NewTree(const char* data, + size_t length, + size_t alloc_hint) { + if (length == 0) return nullptr; + absl::FixedArray reps((length - 1) / kMaxFlatLength + 1); + size_t n = 0; + do { + const size_t len = std::min(length, kMaxFlatLength); + CordRep* rep = NewFlat(len + alloc_hint); + rep->length = len; + memcpy(rep->data, data, len); + reps[n++] = VerifyTree(rep); + data += len; + length -= len; + } while (length != 0); + return MakeBalancedTree(reps.data(), n); +} + +namespace cord_internal { + +ExternalRepReleaserPair NewExternalWithUninitializedReleaser( + absl::string_view data, ExternalReleaserInvoker invoker, + size_t releaser_size) { + assert(!data.empty()); + + void* raw_rep = AllocateExternal(releaser_size); + auto* rep = new (raw_rep) CordRepExternal(); + rep->length = data.size(); + rep->tag = EXTERNAL; + rep->base = data.data(); + rep->releaser_invoker = invoker; + return {VerifyTree(rep), GetExternalReleaser(rep)}; +} + +} // namespace cord_internal + +static CordRep* NewSubstring(CordRep* child, size_t offset, size_t length) { + // Never create empty substring nodes + if (length == 0) { + Unref(child); + return nullptr; + } else { + CordRepSubstring* rep = new CordRepSubstring(); + assert((offset + length) <= child->length); + rep->length = length; + rep->tag = SUBSTRING; + rep->start = offset; + rep->child = child; + return VerifyTree(rep); + } +} + +// -------------------------------------------------------------------- +// Cord::InlineRep functions + +// This will trigger LNK2005 in MSVC. +#ifndef COMPILER_MSVC +const unsigned char Cord::InlineRep::kMaxInline; +#endif // COMPILER_MSVC + +inline void Cord::InlineRep::set_data(const char* data, size_t n, + bool nullify_tail) { + static_assert(kMaxInline == 15, "set_data is hard-coded for a length of 15"); + + cord_internal::SmallMemmove(data_, data, n, nullify_tail); + data_[kMaxInline] = static_cast(n); +} + +inline char* Cord::InlineRep::set_data(size_t n) { + assert(n <= kMaxInline); + memset(data_, 0, sizeof(data_)); + data_[kMaxInline] = static_cast(n); + return data_; +} + +inline CordRep* Cord::InlineRep::force_tree(size_t extra_hint) { + size_t len = data_[kMaxInline]; + CordRep* result; + if (len > kMaxInline) { + memcpy(&result, data_, sizeof(result)); + } else { + result = NewFlat(len + extra_hint); + result->length = len; + memcpy(result->data, data_, len); + set_tree(result); + } + return result; +} + +inline void Cord::InlineRep::reduce_size(size_t n) { + size_t tag = data_[kMaxInline]; + assert(tag <= kMaxInline); + assert(tag >= n); + tag -= n; + memset(data_ + tag, 0, n); + data_[kMaxInline] = static_cast(tag); +} + +inline void Cord::InlineRep::remove_prefix(size_t n) { + cord_internal::SmallMemmove(data_, data_ + n, data_[kMaxInline] - n); + reduce_size(n); +} + +void Cord::InlineRep::AppendTree(CordRep* tree) { + if (tree == nullptr) return; + size_t len = data_[kMaxInline]; + if (len == 0) { + set_tree(tree); + } else { + set_tree(Concat(force_tree(0), tree)); + } +} + +void Cord::InlineRep::PrependTree(CordRep* tree) { + if (tree == nullptr) return; + size_t len = data_[kMaxInline]; + if (len == 0) { + set_tree(tree); + } else { + set_tree(Concat(tree, force_tree(0))); + } +} + +// Searches for a non-full flat node at the rightmost leaf of the tree. If a +// suitable leaf is found, the function will update the length field for all +// nodes to account for the size increase. The append region address will be +// written to region and the actual size increase will be written to size. +static inline bool PrepareAppendRegion(CordRep* root, char** region, + size_t* size, size_t max_length) { + // Search down the right-hand path for a non-full FLAT node. + CordRep* dst = root; + while (dst->tag == CONCAT && dst->refcount.IsOne()) { + dst = dst->concat()->right; + } + + if (dst->tag < FLAT || !dst->refcount.IsOne()) { + *region = nullptr; + *size = 0; + return false; + } + + const size_t in_use = dst->length; + const size_t capacity = TagToLength(dst->tag); + if (in_use == capacity) { + *region = nullptr; + *size = 0; + return false; + } + + size_t size_increase = std::min(capacity - in_use, max_length); + + // We need to update the length fields for all nodes, including the leaf node. + for (CordRep* rep = root; rep != dst; rep = rep->concat()->right) { + rep->length += size_increase; + } + dst->length += size_increase; + + *region = dst->data + in_use; + *size = size_increase; + return true; +} + +void Cord::InlineRep::GetAppendRegion(char** region, size_t* size, + size_t max_length) { + if (max_length == 0) { + *region = nullptr; + *size = 0; + return; + } + + // Try to fit in the inline buffer if possible. + size_t inline_length = data_[kMaxInline]; + if (inline_length < kMaxInline && max_length <= kMaxInline - inline_length) { + *region = data_ + inline_length; + *size = max_length; + data_[kMaxInline] = static_cast(inline_length + max_length); + return; + } + + CordRep* root = force_tree(max_length); + + if (PrepareAppendRegion(root, region, size, max_length)) { + return; + } + + // Allocate new node. + CordRep* new_node = + NewFlat(std::max(static_cast(root->length), max_length)); + new_node->length = + std::min(static_cast(TagToLength(new_node->tag)), max_length); + *region = new_node->data; + *size = new_node->length; + replace_tree(Concat(root, new_node)); +} + +void Cord::InlineRep::GetAppendRegion(char** region, size_t* size) { + const size_t max_length = std::numeric_limits::max(); + + // Try to fit in the inline buffer if possible. + size_t inline_length = data_[kMaxInline]; + if (inline_length < kMaxInline) { + *region = data_ + inline_length; + *size = kMaxInline - inline_length; + data_[kMaxInline] = kMaxInline; + return; + } + + CordRep* root = force_tree(max_length); + + if (PrepareAppendRegion(root, region, size, max_length)) { + return; + } + + // Allocate new node. + CordRep* new_node = NewFlat(root->length); + new_node->length = TagToLength(new_node->tag); + *region = new_node->data; + *size = new_node->length; + replace_tree(Concat(root, new_node)); +} + +// If the rep is a leaf, this will increment the value at total_mem_usage and +// will return true. +static bool RepMemoryUsageLeaf(const CordRep* rep, size_t* total_mem_usage) { + if (rep->tag >= FLAT) { + *total_mem_usage += TagToAllocatedSize(rep->tag); + return true; + } + if (rep->tag == EXTERNAL) { + *total_mem_usage += sizeof(CordRepConcat) + rep->length; + return true; + } + return false; +} + +void Cord::InlineRep::AssignSlow(const Cord::InlineRep& src) { + ClearSlow(); + + memcpy(data_, src.data_, sizeof(data_)); + if (is_tree()) { + Ref(tree()); + } +} + +void Cord::InlineRep::ClearSlow() { + if (is_tree()) { + Unref(tree()); + } + memset(data_, 0, sizeof(data_)); +} + +// -------------------------------------------------------------------- +// Constructors and destructors + +Cord::Cord(const Cord& src) : contents_(src.contents_) { + Ref(contents_.tree()); // Does nothing if contents_ has embedded data +} + +Cord::Cord(absl::string_view src) { + const size_t n = src.size(); + if (n <= InlineRep::kMaxInline) { + contents_.set_data(src.data(), n, false); + } else { + contents_.set_tree(NewTree(src.data(), n, 0)); + } +} + +// The destruction code is separate so that the compiler can determine +// that it does not need to call the destructor on a moved-from Cord. +void Cord::DestroyCordSlow() { + Unref(VerifyTree(contents_.tree())); +} + +// -------------------------------------------------------------------- +// Mutators + +void Cord::Clear() { + Unref(contents_.clear()); +} + +Cord& Cord::operator=(absl::string_view src) { + + const char* data = src.data(); + size_t length = src.size(); + CordRep* tree = contents_.tree(); + if (length <= InlineRep::kMaxInline) { + // Embed into this->contents_ + contents_.set_data(data, length, true); + Unref(tree); + return *this; + } + if (tree != nullptr && tree->tag >= FLAT && + TagToLength(tree->tag) >= length && tree->refcount.IsOne()) { + // Copy in place if the existing FLAT node is reusable. + memmove(tree->data, data, length); + tree->length = length; + VerifyTree(tree); + return *this; + } + contents_.set_tree(NewTree(data, length, 0)); + Unref(tree); + return *this; +} + +// TODO(sanjay): Move to Cord::InlineRep section of file. For now, +// we keep it here to make diffs easier. +void Cord::InlineRep::AppendArray(const char* src_data, size_t src_size) { + if (src_size == 0) return; // memcpy(_, nullptr, 0) is undefined. + // Try to fit in the inline buffer if possible. + size_t inline_length = data_[kMaxInline]; + if (inline_length < kMaxInline && src_size <= kMaxInline - inline_length) { + // Append new data to embedded array + data_[kMaxInline] = static_cast(inline_length + src_size); + memcpy(data_ + inline_length, src_data, src_size); + return; + } + + CordRep* root = tree(); + + size_t appended = 0; + if (root) { + char* region; + if (PrepareAppendRegion(root, ®ion, &appended, src_size)) { + memcpy(region, src_data, appended); + } + } else { + // It is possible that src_data == data_, but when we transition from an + // InlineRep to a tree we need to assign data_ = root via set_tree. To + // avoid corrupting the source data before we copy it, delay calling + // set_tree until after we've copied data. + // We are going from an inline size to beyond inline size. Make the new size + // either double the inlined size, or the added size + 10%. + const size_t size1 = inline_length * 2 + src_size; + const size_t size2 = inline_length + src_size / 10; + root = NewFlat(std::max(size1, size2)); + appended = std::min(src_size, TagToLength(root->tag) - inline_length); + memcpy(root->data, data_, inline_length); + memcpy(root->data + inline_length, src_data, appended); + root->length = inline_length + appended; + set_tree(root); + } + + src_data += appended; + src_size -= appended; + if (src_size == 0) { + return; + } + + // Use new block(s) for any remaining bytes that were not handled above. + // Alloc extra memory only if the right child of the root of the new tree is + // going to be a FLAT node, which will permit further inplace appends. + size_t length = src_size; + if (src_size < kMaxFlatLength) { + // The new length is either + // - old size + 10% + // - old_size + src_size + // This will cause a reasonable conservative step-up in size that is still + // large enough to avoid excessive amounts of small fragments being added. + length = std::max(root->length / 10, src_size); + } + set_tree(Concat(root, NewTree(src_data, src_size, length - src_size))); +} + +inline CordRep* Cord::TakeRep() const& { + return Ref(contents_.tree()); +} + +inline CordRep* Cord::TakeRep() && { + CordRep* rep = contents_.tree(); + contents_.clear(); + return rep; +} + +template +inline void Cord::AppendImpl(C&& src) { + if (empty()) { + // In case of an empty destination avoid allocating a new node, do not copy + // data. + *this = std::forward(src); + return; + } + + // For short cords, it is faster to copy data if there is room in dst. + const size_t src_size = src.contents_.size(); + if (src_size <= kMaxBytesToCopy) { + CordRep* src_tree = src.contents_.tree(); + if (src_tree == nullptr) { + // src has embedded data. + contents_.AppendArray(src.contents_.data(), src_size); + return; + } + if (src_tree->tag >= FLAT) { + // src tree just has one flat node. + contents_.AppendArray(src_tree->data, src_size); + return; + } + if (&src == this) { + // ChunkIterator below assumes that src is not modified during traversal. + Append(Cord(src)); + return; + } + // TODO(mec): Should we only do this if "dst" has space? + for (absl::string_view chunk : src.Chunks()) { + Append(chunk); + } + return; + } + + contents_.AppendTree(std::forward(src).TakeRep()); +} + +void Cord::Append(const Cord& src) { AppendImpl(src); } + +void Cord::Append(Cord&& src) { AppendImpl(std::move(src)); } + +void Cord::Prepend(const Cord& src) { + CordRep* src_tree = src.contents_.tree(); + if (src_tree != nullptr) { + Ref(src_tree); + contents_.PrependTree(src_tree); + return; + } + + // `src` cord is inlined. + absl::string_view src_contents(src.contents_.data(), src.contents_.size()); + return Prepend(src_contents); +} + +void Cord::Prepend(absl::string_view src) { + if (src.empty()) return; // memcpy(_, nullptr, 0) is undefined. + size_t cur_size = contents_.size(); + if (!contents_.is_tree() && cur_size + src.size() <= InlineRep::kMaxInline) { + // Use embedded storage. + char data[InlineRep::kMaxInline + 1] = {0}; + data[InlineRep::kMaxInline] = cur_size + src.size(); // set size + memcpy(data, src.data(), src.size()); + memcpy(data + src.size(), contents_.data(), cur_size); + memcpy(reinterpret_cast(&contents_), data, + InlineRep::kMaxInline + 1); + } else { + contents_.PrependTree(NewTree(src.data(), src.size(), 0)); + } +} + +static CordRep* RemovePrefixFrom(CordRep* node, size_t n) { + if (n >= node->length) return nullptr; + if (n == 0) return Ref(node); + absl::InlinedVector rhs_stack; + + while (node->tag == CONCAT) { + assert(n <= node->length); + if (n < node->concat()->left->length) { + // Push right to stack, descend left. + rhs_stack.push_back(node->concat()->right); + node = node->concat()->left; + } else { + // Drop left, descend right. + n -= node->concat()->left->length; + node = node->concat()->right; + } + } + assert(n <= node->length); + + if (n == 0) { + Ref(node); + } else { + size_t start = n; + size_t len = node->length - n; + if (node->tag == SUBSTRING) { + // Consider in-place update of node, similar to in RemoveSuffixFrom(). + start += node->substring()->start; + node = node->substring()->child; + } + node = NewSubstring(Ref(node), start, len); + } + while (!rhs_stack.empty()) { + node = Concat(node, Ref(rhs_stack.back())); + rhs_stack.pop_back(); + } + return node; +} + +// RemoveSuffixFrom() is very similar to RemovePrefixFrom(), with the +// exception that removing a suffix has an optimization where a node may be +// edited in place iff that node and all its ancestors have a refcount of 1. +static CordRep* RemoveSuffixFrom(CordRep* node, size_t n) { + if (n >= node->length) return nullptr; + if (n == 0) return Ref(node); + absl::InlinedVector lhs_stack; + bool inplace_ok = node->refcount.IsOne(); + + while (node->tag == CONCAT) { + assert(n <= node->length); + if (n < node->concat()->right->length) { + // Push left to stack, descend right. + lhs_stack.push_back(node->concat()->left); + node = node->concat()->right; + } else { + // Drop right, descend left. + n -= node->concat()->right->length; + node = node->concat()->left; + } + inplace_ok = inplace_ok && node->refcount.IsOne(); + } + assert(n <= node->length); + + if (n == 0) { + Ref(node); + } else if (inplace_ok && node->tag != EXTERNAL) { + // Consider making a new buffer if the current node capacity is much + // larger than the new length. + Ref(node); + node->length -= n; + } else { + size_t start = 0; + size_t len = node->length - n; + if (node->tag == SUBSTRING) { + start = node->substring()->start; + node = node->substring()->child; + } + node = NewSubstring(Ref(node), start, len); + } + while (!lhs_stack.empty()) { + node = Concat(Ref(lhs_stack.back()), node); + lhs_stack.pop_back(); + } + return node; +} + +void Cord::RemovePrefix(size_t n) { + ABSL_INTERNAL_CHECK(n <= size(), + absl::StrCat("Requested prefix size ", n, + " exceeds Cord's size ", size())); + CordRep* tree = contents_.tree(); + if (tree == nullptr) { + contents_.remove_prefix(n); + } else { + CordRep* newrep = RemovePrefixFrom(tree, n); + Unref(tree); + contents_.replace_tree(VerifyTree(newrep)); + } +} + +void Cord::RemoveSuffix(size_t n) { + ABSL_INTERNAL_CHECK(n <= size(), + absl::StrCat("Requested suffix size ", n, + " exceeds Cord's size ", size())); + CordRep* tree = contents_.tree(); + if (tree == nullptr) { + contents_.reduce_size(n); + } else { + CordRep* newrep = RemoveSuffixFrom(tree, n); + Unref(tree); + contents_.replace_tree(VerifyTree(newrep)); + } +} + +// Work item for NewSubRange(). +struct SubRange { + SubRange(CordRep* a_node, size_t a_pos, size_t a_n) + : node(a_node), pos(a_pos), n(a_n) {} + CordRep* node; // nullptr means concat last 2 results. + size_t pos; + size_t n; +}; + +static CordRep* NewSubRange(CordRep* node, size_t pos, size_t n) { + absl::InlinedVector results; + absl::InlinedVector todo; + todo.push_back(SubRange(node, pos, n)); + do { + const SubRange& sr = todo.back(); + node = sr.node; + pos = sr.pos; + n = sr.n; + todo.pop_back(); + + if (node == nullptr) { + assert(results.size() >= 2); + CordRep* right = results.back(); + results.pop_back(); + CordRep* left = results.back(); + results.pop_back(); + results.push_back(Concat(left, right)); + } else if (pos == 0 && n == node->length) { + results.push_back(Ref(node)); + } else if (node->tag != CONCAT) { + if (node->tag == SUBSTRING) { + pos += node->substring()->start; + node = node->substring()->child; + } + results.push_back(NewSubstring(Ref(node), pos, n)); + } else if (pos + n <= node->concat()->left->length) { + todo.push_back(SubRange(node->concat()->left, pos, n)); + } else if (pos >= node->concat()->left->length) { + pos -= node->concat()->left->length; + todo.push_back(SubRange(node->concat()->right, pos, n)); + } else { + size_t left_n = node->concat()->left->length - pos; + todo.push_back(SubRange(nullptr, 0, 0)); // Concat() + todo.push_back(SubRange(node->concat()->right, 0, n - left_n)); + todo.push_back(SubRange(node->concat()->left, pos, left_n)); + } + } while (!todo.empty()); + assert(results.size() == 1); + return results[0]; +} + +Cord Cord::Subcord(size_t pos, size_t new_size) const { + Cord sub_cord; + size_t length = size(); + if (pos > length) pos = length; + if (new_size > length - pos) new_size = length - pos; + CordRep* tree = contents_.tree(); + if (tree == nullptr) { + // sub_cord is newly constructed, no need to re-zero-out the tail of + // contents_ memory. + sub_cord.contents_.set_data(contents_.data() + pos, new_size, false); + } else if (new_size == 0) { + // We want to return empty subcord, so nothing to do. + } else if (new_size <= InlineRep::kMaxInline) { + Cord::ChunkIterator it = chunk_begin(); + it.AdvanceBytes(pos); + char* dest = sub_cord.contents_.data_; + size_t remaining_size = new_size; + while (remaining_size > it->size()) { + cord_internal::SmallMemmove(dest, it->data(), it->size()); + remaining_size -= it->size(); + dest += it->size(); + ++it; + } + cord_internal::SmallMemmove(dest, it->data(), remaining_size); + sub_cord.contents_.data_[InlineRep::kMaxInline] = new_size; + } else { + sub_cord.contents_.set_tree(NewSubRange(tree, pos, new_size)); + } + return sub_cord; +} + +// -------------------------------------------------------------------- +// Balancing + +class CordForest { + public: + explicit CordForest(size_t length) + : root_length_(length), trees_(kMinLengthSize, nullptr) {} + + void Build(CordRep* cord_root) { + std::vector pending = {cord_root}; + + while (!pending.empty()) { + CordRep* node = pending.back(); + pending.pop_back(); + CheckNode(node); + if (ABSL_PREDICT_FALSE(node->tag != CONCAT)) { + AddNode(node); + continue; + } + + CordRepConcat* concat_node = node->concat(); + if (concat_node->depth() >= kMinLengthSize || + concat_node->length < min_length[concat_node->depth()]) { + pending.push_back(concat_node->right); + pending.push_back(concat_node->left); + + if (concat_node->refcount.IsOne()) { + concat_node->left = concat_freelist_; + concat_freelist_ = concat_node; + } else { + Ref(concat_node->right); + Ref(concat_node->left); + Unref(concat_node); + } + } else { + AddNode(node); + } + } + } + + CordRep* ConcatNodes() { + CordRep* sum = nullptr; + for (auto* node : trees_) { + if (node == nullptr) continue; + + sum = PrependNode(node, sum); + root_length_ -= node->length; + if (root_length_ == 0) break; + } + ABSL_INTERNAL_CHECK(sum != nullptr, "Failed to locate sum node"); + return VerifyTree(sum); + } + + private: + CordRep* AppendNode(CordRep* node, CordRep* sum) { + return (sum == nullptr) ? node : MakeConcat(sum, node); + } + + CordRep* PrependNode(CordRep* node, CordRep* sum) { + return (sum == nullptr) ? node : MakeConcat(node, sum); + } + + void AddNode(CordRep* node) { + CordRep* sum = nullptr; + + // Collect together everything with which we will merge node + int i = 0; + for (; node->length > min_length[i + 1]; ++i) { + auto& tree_at_i = trees_[i]; + + if (tree_at_i == nullptr) continue; + sum = PrependNode(tree_at_i, sum); + tree_at_i = nullptr; + } + + sum = AppendNode(node, sum); + + // Insert sum into appropriate place in the forest + for (; sum->length >= min_length[i]; ++i) { + auto& tree_at_i = trees_[i]; + if (tree_at_i == nullptr) continue; + + sum = MakeConcat(tree_at_i, sum); + tree_at_i = nullptr; + } + + // min_length[0] == 1, which means sum->length >= min_length[0] + assert(i > 0); + trees_[i - 1] = sum; + } + + // Make concat node trying to resue existing CordRepConcat nodes we + // already collected in the concat_freelist_. + CordRep* MakeConcat(CordRep* left, CordRep* right) { + if (concat_freelist_ == nullptr) return RawConcat(left, right); + + CordRepConcat* rep = concat_freelist_; + if (concat_freelist_->left == nullptr) { + concat_freelist_ = nullptr; + } else { + concat_freelist_ = concat_freelist_->left->concat(); + } + SetConcatChildren(rep, left, right); + + return rep; + } + + static void CheckNode(CordRep* node) { + ABSL_INTERNAL_CHECK(node->length != 0u, ""); + if (node->tag == CONCAT) { + ABSL_INTERNAL_CHECK(node->concat()->left != nullptr, ""); + ABSL_INTERNAL_CHECK(node->concat()->right != nullptr, ""); + ABSL_INTERNAL_CHECK(node->length == (node->concat()->left->length + + node->concat()->right->length), + ""); + } + } + + size_t root_length_; + + // use an inlined vector instead of a flat array to get bounds checking + absl::InlinedVector trees_; + + // List of concat nodes we can re-use for Cord balancing. + CordRepConcat* concat_freelist_ = nullptr; +}; + +static CordRep* Rebalance(CordRep* node) { + VerifyTree(node); + assert(node->tag == CONCAT); + + if (node->length == 0) { + return nullptr; + } + + CordForest forest(node->length); + forest.Build(node); + return forest.ConcatNodes(); +} + +// -------------------------------------------------------------------- +// Comparators + +namespace { + +int ClampResult(int memcmp_res) { + return static_cast(memcmp_res > 0) - static_cast(memcmp_res < 0); +} + +int CompareChunks(absl::string_view* lhs, absl::string_view* rhs, + size_t* size_to_compare) { + size_t compared_size = std::min(lhs->size(), rhs->size()); + assert(*size_to_compare >= compared_size); + *size_to_compare -= compared_size; + + int memcmp_res = ::memcmp(lhs->data(), rhs->data(), compared_size); + if (memcmp_res != 0) return memcmp_res; + + lhs->remove_prefix(compared_size); + rhs->remove_prefix(compared_size); + + return 0; +} + +// This overload set computes comparison results from memcmp result. This +// interface is used inside GenericCompare below. Differet implementations +// are specialized for int and bool. For int we clamp result to {-1, 0, 1} +// set. For bool we just interested in "value == 0". +template +ResultType ComputeCompareResult(int memcmp_res) { + return ClampResult(memcmp_res); +} +template <> +bool ComputeCompareResult(int memcmp_res) { + return memcmp_res == 0; +} + +} // namespace + +// Helper routine. Locates the first flat chunk of the Cord without +// initializing the iterator. +inline absl::string_view Cord::InlineRep::FindFlatStartPiece() const { + size_t n = data_[kMaxInline]; + if (n <= kMaxInline) { + return absl::string_view(data_, n); + } + + CordRep* node = tree(); + if (node->tag >= FLAT) { + return absl::string_view(node->data, node->length); + } + + if (node->tag == EXTERNAL) { + return absl::string_view(node->external()->base, node->length); + } + + // Walk down the left branches until we hit a non-CONCAT node. + while (node->tag == CONCAT) { + node = node->concat()->left; + } + + // Get the child node if we encounter a SUBSTRING. + size_t offset = 0; + size_t length = node->length; + assert(length != 0); + + if (node->tag == SUBSTRING) { + offset = node->substring()->start; + node = node->substring()->child; + } + + if (node->tag >= FLAT) { + return absl::string_view(node->data + offset, length); + } + + assert((node->tag == EXTERNAL) && "Expect FLAT or EXTERNAL node here"); + + return absl::string_view(node->external()->base + offset, length); +} + +inline int Cord::CompareSlowPath(absl::string_view rhs, size_t compared_size, + size_t size_to_compare) const { + auto advance = [](Cord::ChunkIterator* it, absl::string_view* chunk) { + if (!chunk->empty()) return true; + ++*it; + if (it->bytes_remaining_ == 0) return false; + *chunk = **it; + return true; + }; + + Cord::ChunkIterator lhs_it = chunk_begin(); + + // compared_size is inside first chunk. + absl::string_view lhs_chunk = + (lhs_it.bytes_remaining_ != 0) ? *lhs_it : absl::string_view(); + assert(compared_size <= lhs_chunk.size()); + assert(compared_size <= rhs.size()); + lhs_chunk.remove_prefix(compared_size); + rhs.remove_prefix(compared_size); + size_to_compare -= compared_size; // skip already compared size. + + while (advance(&lhs_it, &lhs_chunk) && !rhs.empty()) { + int comparison_result = CompareChunks(&lhs_chunk, &rhs, &size_to_compare); + if (comparison_result != 0) return comparison_result; + if (size_to_compare == 0) return 0; + } + + return static_cast(rhs.empty()) - static_cast(lhs_chunk.empty()); +} + +inline int Cord::CompareSlowPath(const Cord& rhs, size_t compared_size, + size_t size_to_compare) const { + auto advance = [](Cord::ChunkIterator* it, absl::string_view* chunk) { + if (!chunk->empty()) return true; + ++*it; + if (it->bytes_remaining_ == 0) return false; + *chunk = **it; + return true; + }; + + Cord::ChunkIterator lhs_it = chunk_begin(); + Cord::ChunkIterator rhs_it = rhs.chunk_begin(); + + // compared_size is inside both first chunks. + absl::string_view lhs_chunk = + (lhs_it.bytes_remaining_ != 0) ? *lhs_it : absl::string_view(); + absl::string_view rhs_chunk = + (rhs_it.bytes_remaining_ != 0) ? *rhs_it : absl::string_view(); + assert(compared_size <= lhs_chunk.size()); + assert(compared_size <= rhs_chunk.size()); + lhs_chunk.remove_prefix(compared_size); + rhs_chunk.remove_prefix(compared_size); + size_to_compare -= compared_size; // skip already compared size. + + while (advance(&lhs_it, &lhs_chunk) && advance(&rhs_it, &rhs_chunk)) { + int memcmp_res = CompareChunks(&lhs_chunk, &rhs_chunk, &size_to_compare); + if (memcmp_res != 0) return memcmp_res; + if (size_to_compare == 0) return 0; + } + + return static_cast(rhs_chunk.empty()) - + static_cast(lhs_chunk.empty()); +} + +inline absl::string_view Cord::GetFirstChunk(const Cord& c) { + return c.contents_.FindFlatStartPiece(); +} +inline absl::string_view Cord::GetFirstChunk(absl::string_view sv) { + return sv; +} + +// Compares up to 'size_to_compare' bytes of 'lhs' with 'rhs'. It is assumed +// that 'size_to_compare' is greater that size of smallest of first chunks. +template +ResultType GenericCompare(const Cord& lhs, const RHS& rhs, + size_t size_to_compare) { + absl::string_view lhs_chunk = Cord::GetFirstChunk(lhs); + absl::string_view rhs_chunk = Cord::GetFirstChunk(rhs); + + size_t compared_size = std::min(lhs_chunk.size(), rhs_chunk.size()); + assert(size_to_compare >= compared_size); + int memcmp_res = ::memcmp(lhs_chunk.data(), rhs_chunk.data(), compared_size); + if (compared_size == size_to_compare || memcmp_res != 0) { + return ComputeCompareResult(memcmp_res); + } + + return ComputeCompareResult( + lhs.CompareSlowPath(rhs, compared_size, size_to_compare)); +} + +bool Cord::EqualsImpl(absl::string_view rhs, size_t size_to_compare) const { + return GenericCompare(*this, rhs, size_to_compare); +} + +bool Cord::EqualsImpl(const Cord& rhs, size_t size_to_compare) const { + return GenericCompare(*this, rhs, size_to_compare); +} + +template +inline int SharedCompareImpl(const Cord& lhs, const RHS& rhs) { + size_t lhs_size = lhs.size(); + size_t rhs_size = rhs.size(); + if (lhs_size == rhs_size) { + return GenericCompare(lhs, rhs, lhs_size); + } + if (lhs_size < rhs_size) { + auto data_comp_res = GenericCompare(lhs, rhs, lhs_size); + return data_comp_res == 0 ? -1 : data_comp_res; + } + + auto data_comp_res = GenericCompare(lhs, rhs, rhs_size); + return data_comp_res == 0 ? +1 : data_comp_res; +} + +int Cord::Compare(absl::string_view rhs) const { + return SharedCompareImpl(*this, rhs); +} + +int Cord::CompareImpl(const Cord& rhs) const { + return SharedCompareImpl(*this, rhs); +} + +bool Cord::EndsWith(absl::string_view rhs) const { + size_t my_size = size(); + size_t rhs_size = rhs.size(); + + if (my_size < rhs_size) return false; + + Cord tmp(*this); + tmp.RemovePrefix(my_size - rhs_size); + return tmp.EqualsImpl(rhs, rhs_size); +} + +bool Cord::EndsWith(const Cord& rhs) const { + size_t my_size = size(); + size_t rhs_size = rhs.size(); + + if (my_size < rhs_size) return false; + + Cord tmp(*this); + tmp.RemovePrefix(my_size - rhs_size); + return tmp.EqualsImpl(rhs, rhs_size); +} + +// -------------------------------------------------------------------- +// Misc. + +Cord::operator std::string() const { + std::string s; + absl::CopyCordToString(*this, &s); + return s; +} + +void CopyCordToString(const Cord& src, std::string* dst) { + if (!src.contents_.is_tree()) { + src.contents_.CopyTo(dst); + } else { + absl::strings_internal::STLStringResizeUninitialized(dst, src.size()); + src.CopyToArraySlowPath(&(*dst)[0]); + } +} + +void Cord::CopyToArraySlowPath(char* dst) const { + assert(contents_.is_tree()); + absl::string_view fragment; + if (GetFlatAux(contents_.tree(), &fragment)) { + memcpy(dst, fragment.data(), fragment.size()); + return; + } + for (absl::string_view chunk : Chunks()) { + memcpy(dst, chunk.data(), chunk.size()); + dst += chunk.size(); + } +} + +Cord::ChunkIterator& Cord::ChunkIterator::operator++() { + assert(bytes_remaining_ > 0 && "Attempted to iterate past `end()`"); + assert(bytes_remaining_ >= current_chunk_.size()); + bytes_remaining_ -= current_chunk_.size(); + + if (stack_of_right_children_.empty()) { + assert(!current_chunk_.empty()); // Called on invalid iterator. + // We have reached the end of the Cord. + return *this; + } + + // Process the next node on the stack. + CordRep* node = stack_of_right_children_.back(); + stack_of_right_children_.pop_back(); + + // Walk down the left branches until we hit a non-CONCAT node. Save the + // right children to the stack for subsequent traversal. + while (node->tag == CONCAT) { + stack_of_right_children_.push_back(node->concat()->right); + node = node->concat()->left; + } + + // Get the child node if we encounter a SUBSTRING. + size_t offset = 0; + size_t length = node->length; + if (node->tag == SUBSTRING) { + offset = node->substring()->start; + node = node->substring()->child; + } + + assert(node->tag == EXTERNAL || node->tag >= FLAT); + assert(length != 0); + const char* data = + node->tag == EXTERNAL ? node->external()->base : node->data; + current_chunk_ = absl::string_view(data + offset, length); + current_leaf_ = node; + return *this; +} + +Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) { + assert(bytes_remaining_ >= n && "Attempted to iterate past `end()`"); + Cord subcord; + + if (n <= InlineRep::kMaxInline) { + // Range to read fits in inline data. Flatten it. + char* data = subcord.contents_.set_data(n); + while (n > current_chunk_.size()) { + memcpy(data, current_chunk_.data(), current_chunk_.size()); + data += current_chunk_.size(); + n -= current_chunk_.size(); + ++*this; + } + memcpy(data, current_chunk_.data(), n); + if (n < current_chunk_.size()) { + RemoveChunkPrefix(n); + } else if (n > 0) { + ++*this; + } + return subcord; + } + if (n < current_chunk_.size()) { + // Range to read is a proper subrange of the current chunk. + assert(current_leaf_ != nullptr); + CordRep* subnode = Ref(current_leaf_); + const char* data = + subnode->tag == EXTERNAL ? subnode->external()->base : subnode->data; + subnode = NewSubstring(subnode, current_chunk_.data() - data, n); + subcord.contents_.set_tree(VerifyTree(subnode)); + RemoveChunkPrefix(n); + return subcord; + } + + // Range to read begins with a proper subrange of the current chunk. + assert(!current_chunk_.empty()); + assert(current_leaf_ != nullptr); + CordRep* subnode = Ref(current_leaf_); + if (current_chunk_.size() < subnode->length) { + const char* data = + subnode->tag == EXTERNAL ? subnode->external()->base : subnode->data; + subnode = NewSubstring(subnode, current_chunk_.data() - data, + current_chunk_.size()); + } + n -= current_chunk_.size(); + bytes_remaining_ -= current_chunk_.size(); + + // Process the next node(s) on the stack, reading whole subtrees depending on + // their length and how many bytes we are advancing. + CordRep* node = nullptr; + while (!stack_of_right_children_.empty()) { + node = stack_of_right_children_.back(); + stack_of_right_children_.pop_back(); + if (node->length > n) break; + // TODO(qrczak): This might unnecessarily recreate existing concat nodes. + // Avoiding that would need pretty complicated logic (instead of + // current_leaf_, keep current_subtree_ which points to the highest node + // such that the current leaf can be found on the path of left children + // starting from current_subtree_; delay creating subnode while node is + // below current_subtree_; find the proper node along the path of left + // children starting from current_subtree_ if this loop exits while staying + // below current_subtree_; etc.; alternatively, push parents instead of + // right children on the stack). + subnode = Concat(subnode, Ref(node)); + n -= node->length; + bytes_remaining_ -= node->length; + node = nullptr; + } + + if (node == nullptr) { + // We have reached the end of the Cord. + assert(bytes_remaining_ == 0); + subcord.contents_.set_tree(VerifyTree(subnode)); + return subcord; + } + + // Walk down the appropriate branches until we hit a non-CONCAT node. Save the + // right children to the stack for subsequent traversal. + while (node->tag == CONCAT) { + if (node->concat()->left->length > n) { + // Push right, descend left. + stack_of_right_children_.push_back(node->concat()->right); + node = node->concat()->left; + } else { + // Read left, descend right. + subnode = Concat(subnode, Ref(node->concat()->left)); + n -= node->concat()->left->length; + bytes_remaining_ -= node->concat()->left->length; + node = node->concat()->right; + } + } + + // Get the child node if we encounter a SUBSTRING. + size_t offset = 0; + size_t length = node->length; + if (node->tag == SUBSTRING) { + offset = node->substring()->start; + node = node->substring()->child; + } + + // Range to read ends with a proper (possibly empty) subrange of the current + // chunk. + assert(node->tag == EXTERNAL || node->tag >= FLAT); + assert(length > n); + if (n > 0) subnode = Concat(subnode, NewSubstring(Ref(node), offset, n)); + const char* data = + node->tag == EXTERNAL ? node->external()->base : node->data; + current_chunk_ = absl::string_view(data + offset + n, length - n); + current_leaf_ = node; + bytes_remaining_ -= n; + subcord.contents_.set_tree(VerifyTree(subnode)); + return subcord; +} + +void Cord::ChunkIterator::AdvanceBytesSlowPath(size_t n) { + assert(bytes_remaining_ >= n && "Attempted to iterate past `end()`"); + assert(n >= current_chunk_.size()); // This should only be called when + // iterating to a new node. + + n -= current_chunk_.size(); + bytes_remaining_ -= current_chunk_.size(); + + // Process the next node(s) on the stack, skipping whole subtrees depending on + // their length and how many bytes we are advancing. + CordRep* node = nullptr; + while (!stack_of_right_children_.empty()) { + node = stack_of_right_children_.back(); + stack_of_right_children_.pop_back(); + if (node->length > n) break; + n -= node->length; + bytes_remaining_ -= node->length; + node = nullptr; + } + + if (node == nullptr) { + // We have reached the end of the Cord. + assert(bytes_remaining_ == 0); + return; + } + + // Walk down the appropriate branches until we hit a non-CONCAT node. Save the + // right children to the stack for subsequent traversal. + while (node->tag == CONCAT) { + if (node->concat()->left->length > n) { + // Push right, descend left. + stack_of_right_children_.push_back(node->concat()->right); + node = node->concat()->left; + } else { + // Skip left, descend right. + n -= node->concat()->left->length; + bytes_remaining_ -= node->concat()->left->length; + node = node->concat()->right; + } + } + + // Get the child node if we encounter a SUBSTRING. + size_t offset = 0; + size_t length = node->length; + if (node->tag == SUBSTRING) { + offset = node->substring()->start; + node = node->substring()->child; + } + + assert(node->tag == EXTERNAL || node->tag >= FLAT); + assert(length > n); + const char* data = + node->tag == EXTERNAL ? node->external()->base : node->data; + current_chunk_ = absl::string_view(data + offset + n, length - n); + current_leaf_ = node; + bytes_remaining_ -= n; +} + +char Cord::operator[](size_t i) const { + assert(i < size()); + size_t offset = i; + const CordRep* rep = contents_.tree(); + if (rep == nullptr) { + return contents_.data()[i]; + } + while (true) { + assert(rep != nullptr); + assert(offset < rep->length); + if (rep->tag >= FLAT) { + // Get the "i"th character directly from the flat array. + return rep->data[offset]; + } else if (rep->tag == EXTERNAL) { + // Get the "i"th character from the external array. + return rep->external()->base[offset]; + } else if (rep->tag == CONCAT) { + // Recursively branch to the side of the concatenation that the "i"th + // character is on. + size_t left_length = rep->concat()->left->length; + if (offset < left_length) { + rep = rep->concat()->left; + } else { + offset -= left_length; + rep = rep->concat()->right; + } + } else { + // This must be a substring a node, so bypass it to get to the child. + assert(rep->tag == SUBSTRING); + offset += rep->substring()->start; + rep = rep->substring()->child; + } + } +} + +absl::string_view Cord::FlattenSlowPath() { + size_t total_size = size(); + CordRep* new_rep; + char* new_buffer; + + // Try to put the contents into a new flat rep. If they won't fit in the + // biggest possible flat node, use an external rep instead. + if (total_size <= kMaxFlatLength) { + new_rep = NewFlat(total_size); + new_rep->length = total_size; + new_buffer = new_rep->data; + CopyToArraySlowPath(new_buffer); + } else { + new_buffer = std::allocator().allocate(total_size); + CopyToArraySlowPath(new_buffer); + new_rep = absl::cord_internal::NewExternalRep( + absl::string_view(new_buffer, total_size), [](absl::string_view s) { + std::allocator().deallocate(const_cast(s.data()), + s.size()); + }); + } + Unref(contents_.tree()); + contents_.set_tree(new_rep); + return absl::string_view(new_buffer, total_size); +} + +/* static */ bool Cord::GetFlatAux(CordRep* rep, absl::string_view* fragment) { + assert(rep != nullptr); + if (rep->tag >= FLAT) { + *fragment = absl::string_view(rep->data, rep->length); + return true; + } else if (rep->tag == EXTERNAL) { + *fragment = absl::string_view(rep->external()->base, rep->length); + return true; + } else if (rep->tag == SUBSTRING) { + CordRep* child = rep->substring()->child; + if (child->tag >= FLAT) { + *fragment = + absl::string_view(child->data + rep->substring()->start, rep->length); + return true; + } else if (child->tag == EXTERNAL) { + *fragment = absl::string_view( + child->external()->base + rep->substring()->start, rep->length); + return true; + } + } + return false; +} + +/* static */ void Cord::ForEachChunkAux( + absl::cord_internal::CordRep* rep, + absl::FunctionRef callback) { + assert(rep != nullptr); + int stack_pos = 0; + constexpr int stack_max = 128; + // Stack of right branches for tree traversal + absl::cord_internal::CordRep* stack[stack_max]; + absl::cord_internal::CordRep* current_node = rep; + while (true) { + if (current_node->tag == CONCAT) { + if (stack_pos == stack_max) { + // There's no more room on our stack array to add another right branch, + // and the idea is to avoid allocations, so call this function + // recursively to navigate this subtree further. (This is not something + // we expect to happen in practice). + ForEachChunkAux(current_node, callback); + + // Pop the next right branch and iterate. + current_node = stack[--stack_pos]; + continue; + } else { + // Save the right branch for later traversal and continue down the left + // branch. + stack[stack_pos++] = current_node->concat()->right; + current_node = current_node->concat()->left; + continue; + } + } + // This is a leaf node, so invoke our callback. + absl::string_view chunk; + bool success = GetFlatAux(current_node, &chunk); + assert(success); + if (success) { + callback(chunk); + } + if (stack_pos == 0) { + // end of traversal + return; + } + current_node = stack[--stack_pos]; + } +} + +static void DumpNode(CordRep* rep, bool include_data, std::ostream* os) { + const int kIndentStep = 1; + int indent = 0; + absl::InlinedVector stack; + absl::InlinedVector indents; + for (;;) { + *os << std::setw(3) << rep->refcount.Get(); + *os << " " << std::setw(7) << rep->length; + *os << " ["; + if (include_data) *os << static_cast(rep); + *os << "]"; + *os << " " << (IsRootBalanced(rep) ? 'b' : 'u'); + *os << " " << std::setw(indent) << ""; + if (rep->tag == CONCAT) { + *os << "CONCAT depth=" << Depth(rep) << "\n"; + indent += kIndentStep; + indents.push_back(indent); + stack.push_back(rep->concat()->right); + rep = rep->concat()->left; + } else if (rep->tag == SUBSTRING) { + *os << "SUBSTRING @ " << rep->substring()->start << "\n"; + indent += kIndentStep; + rep = rep->substring()->child; + } else { // Leaf + if (rep->tag == EXTERNAL) { + *os << "EXTERNAL ["; + if (include_data) + *os << absl::CEscape(std::string(rep->external()->base, rep->length)); + *os << "]\n"; + } else { + *os << "FLAT cap=" << TagToLength(rep->tag) << " ["; + if (include_data) + *os << absl::CEscape(std::string(rep->data, rep->length)); + *os << "]\n"; + } + if (stack.empty()) break; + rep = stack.back(); + stack.pop_back(); + indent = indents.back(); + indents.pop_back(); + } + } + ABSL_INTERNAL_CHECK(indents.empty(), ""); +} + +static std::string ReportError(CordRep* root, CordRep* node) { + std::ostringstream buf; + buf << "Error at node " << node << " in:"; + DumpNode(root, true, &buf); + return buf.str(); +} + +static bool VerifyNode(CordRep* root, CordRep* start_node, + bool full_validation) { + absl::InlinedVector worklist; + worklist.push_back(start_node); + do { + CordRep* node = worklist.back(); + worklist.pop_back(); + + ABSL_INTERNAL_CHECK(node != nullptr, ReportError(root, node)); + if (node != root) { + ABSL_INTERNAL_CHECK(node->length != 0, ReportError(root, node)); + } + + if (node->tag == CONCAT) { + ABSL_INTERNAL_CHECK(node->concat()->left != nullptr, + ReportError(root, node)); + ABSL_INTERNAL_CHECK(node->concat()->right != nullptr, + ReportError(root, node)); + ABSL_INTERNAL_CHECK((node->length == node->concat()->left->length + + node->concat()->right->length), + ReportError(root, node)); + if (full_validation) { + worklist.push_back(node->concat()->right); + worklist.push_back(node->concat()->left); + } + } else if (node->tag >= FLAT) { + ABSL_INTERNAL_CHECK(node->length <= TagToLength(node->tag), + ReportError(root, node)); + } else if (node->tag == EXTERNAL) { + ABSL_INTERNAL_CHECK(node->external()->base != nullptr, + ReportError(root, node)); + } else if (node->tag == SUBSTRING) { + ABSL_INTERNAL_CHECK( + node->substring()->start < node->substring()->child->length, + ReportError(root, node)); + ABSL_INTERNAL_CHECK(node->substring()->start + node->length <= + node->substring()->child->length, + ReportError(root, node)); + } + } while (!worklist.empty()); + return true; +} + +// Traverses the tree and computes the total memory allocated. +/* static */ size_t Cord::MemoryUsageAux(const CordRep* rep) { + size_t total_mem_usage = 0; + + // Allow a quick exit for the common case that the root is a leaf. + if (RepMemoryUsageLeaf(rep, &total_mem_usage)) { + return total_mem_usage; + } + + // Iterate over the tree. cur_node is never a leaf node and leaf nodes will + // never be appended to tree_stack. This reduces overhead from manipulating + // tree_stack. + absl::InlinedVector tree_stack; + const CordRep* cur_node = rep; + while (true) { + const CordRep* next_node = nullptr; + + if (cur_node->tag == CONCAT) { + total_mem_usage += sizeof(CordRepConcat); + const CordRep* left = cur_node->concat()->left; + if (!RepMemoryUsageLeaf(left, &total_mem_usage)) { + next_node = left; + } + + const CordRep* right = cur_node->concat()->right; + if (!RepMemoryUsageLeaf(right, &total_mem_usage)) { + if (next_node) { + tree_stack.push_back(next_node); + } + next_node = right; + } + } else { + // Since cur_node is not a leaf or a concat node it must be a substring. + assert(cur_node->tag == SUBSTRING); + total_mem_usage += sizeof(CordRepSubstring); + next_node = cur_node->substring()->child; + if (RepMemoryUsageLeaf(next_node, &total_mem_usage)) { + next_node = nullptr; + } + } + + if (!next_node) { + if (tree_stack.empty()) { + return total_mem_usage; + } + next_node = tree_stack.back(); + tree_stack.pop_back(); + } + cur_node = next_node; + } +} + +std::ostream& operator<<(std::ostream& out, const Cord& cord) { + for (absl::string_view chunk : cord.Chunks()) { + out.write(chunk.data(), chunk.size()); + } + return out; +} + +namespace strings_internal { +size_t CordTestAccess::FlatOverhead() { return kFlatOverhead; } +size_t CordTestAccess::MaxFlatLength() { return kMaxFlatLength; } +size_t CordTestAccess::FlatTagToLength(uint8_t tag) { + return TagToLength(tag); +} +uint8_t CordTestAccess::LengthToTag(size_t s) { + ABSL_INTERNAL_CHECK(s <= kMaxFlatLength, absl::StrCat("Invalid length ", s)); + return AllocatedSizeToTag(s + kFlatOverhead); +} +size_t CordTestAccess::SizeofCordRepConcat() { return sizeof(CordRepConcat); } +size_t CordTestAccess::SizeofCordRepExternal() { + return sizeof(CordRepExternal); +} +size_t CordTestAccess::SizeofCordRepSubstring() { + return sizeof(CordRepSubstring); +} +} // namespace strings_internal +ABSL_NAMESPACE_END +} // namespace absl diff --git a/absl/strings/cord.h b/absl/strings/cord.h new file mode 100644 index 00000000..40566cba --- /dev/null +++ b/absl/strings/cord.h @@ -0,0 +1,1121 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// A Cord is a sequence of characters with some unusual access propreties. +// A Cord supports efficient insertions and deletions at the start and end of +// the byte sequence, but random access reads are slower, and random access +// modifications are not supported by the API. Cord also provides cheap copies +// (using a copy-on-write strategy) and cheap substring operations. +// +// Thread safety +// ------------- +// Cord has the same thread-safety properties as many other types like +// std::string, std::vector<>, int, etc -- it is thread-compatible. In +// particular, if no thread may call a non-const method, then it is safe to +// concurrently call const methods. Copying a Cord produces a new instance that +// can be used concurrently with the original in arbitrary ways. +// +// Implementation is similar to the "Ropes" described in: +// Ropes: An alternative to strings +// Hans J. Boehm, Russ Atkinson, Michael Plass +// Software Practice and Experience, December 1995 + +#ifndef ABSL_STRINGS_CORD_H_ +#define ABSL_STRINGS_CORD_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/base/internal/endian.h" +#include "absl/base/internal/invoke.h" +#include "absl/base/internal/per_thread_tls.h" +#include "absl/base/macros.h" +#include "absl/base/port.h" +#include "absl/container/inlined_vector.h" +#include "absl/functional/function_ref.h" +#include "absl/meta/type_traits.h" +#include "absl/strings/internal/cord_internal.h" +#include "absl/strings/internal/resize_uninitialized.h" +#include "absl/strings/string_view.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +class Cord; +class CordTestPeer; +template +Cord MakeCordFromExternal(absl::string_view, Releaser&&); +void CopyCordToString(const Cord& src, std::string* dst); +namespace hash_internal { +template +H HashFragmentedCord(H, const Cord&); +} + +// A Cord is a sequence of characters. +class Cord { + private: + template + using EnableIfString = + absl::enable_if_t::value, int>; + + public: + // -------------------------------------------------------------------- + // Constructors, destructors and helper factories + + // Create an empty cord + constexpr Cord() noexcept; + + // Cord is copyable and efficiently movable. + // The moved-from state is valid but unspecified. + Cord(const Cord& src); + Cord(Cord&& src) noexcept; + Cord& operator=(const Cord& x); + Cord& operator=(Cord&& x) noexcept; + + // Create a cord out of "src". This constructor is explicit on + // purpose so that people do not get automatic type conversions. + explicit Cord(absl::string_view src); + Cord& operator=(absl::string_view src); + + // These are templated to avoid ambiguities for types that are convertible to + // both `absl::string_view` and `std::string`, such as `const char*`. + // + // Note that these functions reserve the right to reuse the `string&&`'s + // memory and that they will do so in the future. + template = 0> + explicit Cord(T&& src) : Cord(absl::string_view(src)) {} + template = 0> + Cord& operator=(T&& src); + + // Destroy the cord + ~Cord() { + if (contents_.is_tree()) DestroyCordSlow(); + } + + // Creates a Cord that takes ownership of external memory. The contents of + // `data` are not copied. + // + // This function takes a callable that is invoked when all Cords are + // finished with `data`. The data must remain live and unchanging until the + // releaser is called. The requirements for the releaser are that it: + // * is move constructible, + // * supports `void operator()(absl::string_view) const`, + // * does not have alignment requirement greater than what is guaranteed by + // ::operator new. This is dictated by alignof(std::max_align_t) before + // C++17 and __STDCPP_DEFAULT_NEW_ALIGNMENT__ if compiling with C++17 or + // it is supported by the implementation. + // + // Example: + // + // Cord MakeCord(BlockPool* pool) { + // Block* block = pool->NewBlock(); + // FillBlock(block); + // return absl::MakeCordFromExternal( + // block->ToStringView(), + // [pool, block](absl::string_view /*ignored*/) { + // pool->FreeBlock(block); + // }); + // } + // + // WARNING: It's likely a bug if your releaser doesn't do anything. + // For example, consider the following: + // + // void Foo(const char* buffer, int len) { + // auto c = absl::MakeCordFromExternal(absl::string_view(buffer, len), + // [](absl::string_view) {}); + // + // // BUG: If Bar() copies its cord for any reason, including keeping a + // // substring of it, the lifetime of buffer might be extended beyond + // // when Foo() returns. + // Bar(c); + // } + template + friend Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser); + + // -------------------------------------------------------------------- + // Mutations + + void Clear(); + + void Append(const Cord& src); + void Append(Cord&& src); + void Append(absl::string_view src); + template = 0> + void Append(T&& src); + + void Prepend(const Cord& src); + void Prepend(absl::string_view src); + template = 0> + void Prepend(T&& src); + + void RemovePrefix(size_t n); + void RemoveSuffix(size_t n); + + // Returns a new cord representing the subrange [pos, pos + new_size) of + // *this. If pos >= size(), the result is empty(). If + // (pos + new_size) >= size(), the result is the subrange [pos, size()). + Cord Subcord(size_t pos, size_t new_size) const; + + friend void swap(Cord& x, Cord& y) noexcept; + + // -------------------------------------------------------------------- + // Accessors + + size_t size() const; + bool empty() const; + + // Returns the approximate number of bytes pinned by this Cord. Note that + // Cords that share memory could each be "charged" independently for the same + // shared memory. + size_t EstimatedMemoryUsage() const; + + // -------------------------------------------------------------------- + // Comparators + + // Compares 'this' Cord with rhs. This function and its relatives + // treat Cords as sequences of unsigned bytes. The comparison is a + // straightforward lexicographic comparison. Return value: + // -1 'this' Cord is smaller + // 0 two Cords are equal + // 1 'this' Cord is larger + int Compare(absl::string_view rhs) const; + int Compare(const Cord& rhs) const; + + // Does 'this' cord start/end with rhs + bool StartsWith(const Cord& rhs) const; + bool StartsWith(absl::string_view rhs) const; + bool EndsWith(absl::string_view rhs) const; + bool EndsWith(const Cord& rhs) const; + + // -------------------------------------------------------------------- + // Conversion to other types + + explicit operator std::string() const; + + // Copies the contents from `src` to `*dst`. + // + // This function optimizes the case of reusing the destination std::string since it + // can reuse previously allocated capacity. However, this function does not + // guarantee that pointers previously returned by `dst->data()` remain valid + // even if `*dst` had enough capacity to hold `src`. If `*dst` is a new + // object, prefer to simply use the conversion operator to `std::string`. + friend void CopyCordToString(const Cord& src, std::string* dst); + + // -------------------------------------------------------------------- + // Iteration + + class CharIterator; + + // Type for iterating over the chunks of a `Cord`. See comments for + // `Cord::chunk_begin()`, `Cord::chunk_end()` and `Cord::Chunks()` below for + // preferred usage. + // + // Additional notes: + // * The `string_view` returned by dereferencing a valid, non-`end()` + // iterator is guaranteed to be non-empty. + // * A `ChunkIterator` object is invalidated after any non-const + // operation on the `Cord` object over which it iterates. + // * Two `ChunkIterator` objects can be equality compared if and only if + // they remain valid and iterate over the same `Cord`. + // * This is a proxy iterator. This means the `string_view` returned by the + // iterator does not live inside the Cord, and its lifetime is limited to + // the lifetime of the iterator itself. To help prevent issues, + // `ChunkIterator::reference` is not a true reference type and is + // equivalent to `value_type`. + // * The iterator keeps state that can grow for `Cord`s that contain many + // nodes and are imbalanced due to sharing. Prefer to pass this type by + // const reference instead of by value. + class ChunkIterator { + public: + using iterator_category = std::input_iterator_tag; + using value_type = absl::string_view; + using difference_type = ptrdiff_t; + using pointer = const value_type*; + using reference = value_type; + + ChunkIterator() = default; + + ChunkIterator& operator++(); + ChunkIterator operator++(int); + bool operator==(const ChunkIterator& other) const; + bool operator!=(const ChunkIterator& other) const; + reference operator*() const; + pointer operator->() const; + + friend class Cord; + friend class CharIterator; + + private: + // Constructs a `begin()` iterator from `cord`. + explicit ChunkIterator(const Cord* cord); + + // Removes `n` bytes from `current_chunk_`. Expects `n` to be smaller than + // `current_chunk_.size()`. + void RemoveChunkPrefix(size_t n); + Cord AdvanceAndReadBytes(size_t n); + void AdvanceBytes(size_t n); + // Iterates `n` bytes, where `n` is expected to be greater than or equal to + // `current_chunk_.size()`. + void AdvanceBytesSlowPath(size_t n); + + // A view into bytes of the current `CordRep`. It may only be a view to a + // suffix of bytes if this is being used by `CharIterator`. + absl::string_view current_chunk_; + // The current leaf, or `nullptr` if the iterator points to short data. + // If the current chunk is a substring node, current_leaf_ points to the + // underlying flat or external node. + absl::cord_internal::CordRep* current_leaf_ = nullptr; + // The number of bytes left in the `Cord` over which we are iterating. + size_t bytes_remaining_ = 0; + absl::InlinedVector + stack_of_right_children_; + }; + + // Returns an iterator to the first chunk of the `Cord`. + // + // This is useful for getting a `ChunkIterator` outside the context of a + // range-based for-loop (in which case see `Cord::Chunks()` below). + // + // Example: + // + // absl::Cord::ChunkIterator FindAsChunk(const absl::Cord& c, + // absl::string_view s) { + // return std::find(c.chunk_begin(), c.chunk_end(), s); + // } + ChunkIterator chunk_begin() const; + // Returns an iterator one increment past the last chunk of the `Cord`. + ChunkIterator chunk_end() const; + + // Convenience wrapper over `Cord::chunk_begin()` and `Cord::chunk_end()` to + // enable range-based for-loop iteration over `Cord` chunks. + // + // Prefer to use `Cord::Chunks()` below instead of constructing this directly. + class ChunkRange { + public: + explicit ChunkRange(const Cord* cord) : cord_(cord) {} + + ChunkIterator begin() const; + ChunkIterator end() const; + + private: + const Cord* cord_; + }; + + // Returns a range for iterating over the chunks of a `Cord` with a + // range-based for-loop. + // + // Example: + // + // void ProcessChunks(const Cord& cord) { + // for (absl::string_view chunk : cord.Chunks()) { ... } + // } + // + // Note that the ordinary caveats of temporary lifetime extension apply: + // + // void Process() { + // for (absl::string_view chunk : CordFactory().Chunks()) { + // // The temporary Cord returned by CordFactory has been destroyed! + // } + // } + ChunkRange Chunks() const; + + // Type for iterating over the characters of a `Cord`. See comments for + // `Cord::char_begin()`, `Cord::char_end()` and `Cord::Chars()` below for + // preferred usage. + // + // Additional notes: + // * A `CharIterator` object is invalidated after any non-const + // operation on the `Cord` object over which it iterates. + // * Two `CharIterator` objects can be equality compared if and only if + // they remain valid and iterate over the same `Cord`. + // * The iterator keeps state that can grow for `Cord`s that contain many + // nodes and are imbalanced due to sharing. Prefer to pass this type by + // const reference instead of by value. + // * This type cannot be a forward iterator because a `Cord` can reuse + // sections of memory. This violates the requirement that if dereferencing + // two iterators returns the same object, the iterators must compare + // equal. + class CharIterator { + public: + using iterator_category = std::input_iterator_tag; + using value_type = char; + using difference_type = ptrdiff_t; + using pointer = const char*; + using reference = const char&; + + CharIterator() = default; + + CharIterator& operator++(); + CharIterator operator++(int); + bool operator==(const CharIterator& other) const; + bool operator!=(const CharIterator& other) const; + reference operator*() const; + pointer operator->() const; + + friend Cord; + + private: + explicit CharIterator(const Cord* cord) : chunk_iterator_(cord) {} + + ChunkIterator chunk_iterator_; + }; + + // Advances `*it` by `n_bytes` and returns the bytes passed as a `Cord`. + // + // `n_bytes` must be less than or equal to the number of bytes remaining for + // iteration. Otherwise the behavior is undefined. It is valid to pass + // `char_end()` and 0. + static Cord AdvanceAndRead(CharIterator* it, size_t n_bytes); + + // Advances `*it` by `n_bytes`. + // + // `n_bytes` must be less than or equal to the number of bytes remaining for + // iteration. Otherwise the behavior is undefined. It is valid to pass + // `char_end()` and 0. + static void Advance(CharIterator* it, size_t n_bytes); + + // Returns the longest contiguous view starting at the iterator's position. + // + // `it` must be dereferenceable. + static absl::string_view ChunkRemaining(const CharIterator& it); + + // Returns an iterator to the first character of the `Cord`. + CharIterator char_begin() const; + // Returns an iterator to one past the last character of the `Cord`. + CharIterator char_end() const; + + // Convenience wrapper over `Cord::char_begin()` and `Cord::char_end()` to + // enable range-based for-loop iterator over the characters of a `Cord`. + // + // Prefer to use `Cord::Chars()` below instead of constructing this directly. + class CharRange { + public: + explicit CharRange(const Cord* cord) : cord_(cord) {} + + CharIterator begin() const; + CharIterator end() const; + + private: + const Cord* cord_; + }; + + // Returns a range for iterating over the characters of a `Cord` with a + // range-based for-loop. + // + // Example: + // + // void ProcessCord(const Cord& cord) { + // for (char c : cord.Chars()) { ... } + // } + // + // Note that the ordinary caveats of temporary lifetime extension apply: + // + // void Process() { + // for (char c : CordFactory().Chars()) { + // // The temporary Cord returned by CordFactory has been destroyed! + // } + // } + CharRange Chars() const; + + // -------------------------------------------------------------------- + // Miscellaneous + + // Get the "i"th character of 'this' and return it. + // NOTE: This routine is reasonably efficient. It is roughly + // logarithmic in the number of nodes that make up the cord. Still, + // if you need to iterate over the contents of a cord, you should + // use a CharIterator/CordIterator rather than call operator[] or Get() + // repeatedly in a loop. + // + // REQUIRES: 0 <= i < size() + char operator[](size_t i) const; + + // Flattens the cord into a single array and returns a view of the data. + // + // If the cord was already flat, the contents are not modified. + absl::string_view Flatten(); + + private: + friend class CordTestPeer; + template + friend H absl::hash_internal::HashFragmentedCord(H, const Cord&); + friend bool operator==(const Cord& lhs, const Cord& rhs); + friend bool operator==(const Cord& lhs, absl::string_view rhs); + + // Call the provided function once for each cord chunk, in order. Unlike + // Chunks(), this API will not allocate memory. + void ForEachChunk(absl::FunctionRef) const; + + // Allocates new contiguous storage for the contents of the cord. This is + // called by Flatten() when the cord was not already flat. + absl::string_view FlattenSlowPath(); + + // Actual cord contents are hidden inside the following simple + // class so that we can isolate the bulk of cord.cc from changes + // to the representation. + // + // InlineRep holds either either a tree pointer, or an array of kMaxInline + // bytes. + class InlineRep { + public: + static const unsigned char kMaxInline = 15; + static_assert(kMaxInline >= sizeof(absl::cord_internal::CordRep*), ""); + // Tag byte & kMaxInline means we are storing a pointer. + static const unsigned char kTreeFlag = 1 << 4; + // Tag byte & kProfiledFlag means we are profiling the Cord. + static const unsigned char kProfiledFlag = 1 << 5; + + constexpr InlineRep() : data_{} {} + InlineRep(const InlineRep& src); + InlineRep(InlineRep&& src); + InlineRep& operator=(const InlineRep& src); + InlineRep& operator=(InlineRep&& src) noexcept; + + void Swap(InlineRep* rhs); + bool empty() const; + size_t size() const; + const char* data() const; // Returns nullptr if holding pointer + void set_data(const char* data, size_t n, + bool nullify_tail); // Discards pointer, if any + char* set_data(size_t n); // Write data to the result + // Returns nullptr if holding bytes + absl::cord_internal::CordRep* tree() const; + // Discards old pointer, if any + void set_tree(absl::cord_internal::CordRep* rep); + // Replaces a tree with a new root. This is faster than set_tree, but it + // should only be used when it's clear that the old rep was a tree. + void replace_tree(absl::cord_internal::CordRep* rep); + // Returns non-null iff was holding a pointer + absl::cord_internal::CordRep* clear(); + // Convert to pointer if necessary + absl::cord_internal::CordRep* force_tree(size_t extra_hint); + void reduce_size(size_t n); // REQUIRES: holding data + void remove_prefix(size_t n); // REQUIRES: holding data + void AppendArray(const char* src_data, size_t src_size); + absl::string_view FindFlatStartPiece() const; + void AppendTree(absl::cord_internal::CordRep* tree); + void PrependTree(absl::cord_internal::CordRep* tree); + void GetAppendRegion(char** region, size_t* size, size_t max_length); + void GetAppendRegion(char** region, size_t* size); + bool IsSame(const InlineRep& other) const { + return memcmp(data_, other.data_, sizeof(data_)) == 0; + } + int BitwiseCompare(const InlineRep& other) const { + uint64_t x, y; + // Use memcpy to avoid anti-aliasing issues. + memcpy(&x, data_, sizeof(x)); + memcpy(&y, other.data_, sizeof(y)); + if (x == y) { + memcpy(&x, data_ + 8, sizeof(x)); + memcpy(&y, other.data_ + 8, sizeof(y)); + if (x == y) return 0; + } + return absl::big_endian::FromHost64(x) < absl::big_endian::FromHost64(y) + ? -1 + : 1; + } + void CopyTo(std::string* dst) const { + // memcpy is much faster when operating on a known size. On most supported + // platforms, the small std::string optimization is large enough that resizing + // to 15 bytes does not cause a memory allocation. + absl::strings_internal::STLStringResizeUninitialized(dst, + sizeof(data_) - 1); + memcpy(&(*dst)[0], data_, sizeof(data_) - 1); + // erase is faster than resize because the logic for memory allocation is + // not needed. + dst->erase(data_[kMaxInline]); + } + + // Copies the inline contents into `dst`. Assumes the cord is not empty. + void CopyToArray(char* dst) const; + + bool is_tree() const { return data_[kMaxInline] > kMaxInline; } + + private: + friend class Cord; + + void AssignSlow(const InlineRep& src); + // Unrefs the tree, stops profiling, and zeroes the contents + void ClearSlow(); + + // If the data has length <= kMaxInline, we store it in data_[0..len-1], + // and store the length in data_[kMaxInline]. Else we store it in a tree + // and store a pointer to that tree in data_[0..sizeof(CordRep*)-1]. + alignas(absl::cord_internal::CordRep*) char data_[kMaxInline + 1]; + }; + InlineRep contents_; + + // Helper for MemoryUsage() + static size_t MemoryUsageAux(const absl::cord_internal::CordRep* rep); + + // Helper for GetFlat() + static bool GetFlatAux(absl::cord_internal::CordRep* rep, + absl::string_view* fragment); + + // Helper for ForEachChunk() + static void ForEachChunkAux( + absl::cord_internal::CordRep* rep, + absl::FunctionRef callback); + + // The destructor for non-empty Cords. + void DestroyCordSlow(); + + // Out-of-line implementation of slower parts of logic. + void CopyToArraySlowPath(char* dst) const; + int CompareSlowPath(absl::string_view rhs, size_t compared_size, + size_t size_to_compare) const; + int CompareSlowPath(const Cord& rhs, size_t compared_size, + size_t size_to_compare) const; + bool EqualsImpl(absl::string_view rhs, size_t size_to_compare) const; + bool EqualsImpl(const Cord& rhs, size_t size_to_compare) const; + int CompareImpl(const Cord& rhs) const; + + template + friend ResultType GenericCompare(const Cord& lhs, const RHS& rhs, + size_t size_to_compare); + static absl::string_view GetFirstChunk(const Cord& c); + static absl::string_view GetFirstChunk(absl::string_view sv); + + // Returns a new reference to contents_.tree(), or steals an existing + // reference if called on an rvalue. + absl::cord_internal::CordRep* TakeRep() const&; + absl::cord_internal::CordRep* TakeRep() &&; + + // Helper for Append() + template + void AppendImpl(C&& src); +}; + +ABSL_NAMESPACE_END +} // namespace absl + +namespace absl { +ABSL_NAMESPACE_BEGIN + +// allow a Cord to be logged +extern std::ostream& operator<<(std::ostream& out, const Cord& cord); + +// ------------------------------------------------------------------ +// Internal details follow. Clients should ignore. + +namespace cord_internal { + +// Fast implementation of memmove for up to 15 bytes. This implementation is +// safe for overlapping regions. If nullify_tail is true, the destination is +// padded with '\0' up to 16 bytes. +inline void SmallMemmove(char* dst, const char* src, size_t n, + bool nullify_tail = false) { + if (n >= 8) { + assert(n <= 16); + uint64_t buf1; + uint64_t buf2; + memcpy(&buf1, src, 8); + memcpy(&buf2, src + n - 8, 8); + if (nullify_tail) { + memset(dst + 8, 0, 8); + } + memcpy(dst, &buf1, 8); + memcpy(dst + n - 8, &buf2, 8); + } else if (n >= 4) { + uint32_t buf1; + uint32_t buf2; + memcpy(&buf1, src, 4); + memcpy(&buf2, src + n - 4, 4); + if (nullify_tail) { + memset(dst + 4, 0, 4); + memset(dst + 8, 0, 8); + } + memcpy(dst, &buf1, 4); + memcpy(dst + n - 4, &buf2, 4); + } else { + if (n != 0) { + dst[0] = src[0]; + dst[n / 2] = src[n / 2]; + dst[n - 1] = src[n - 1]; + } + if (nullify_tail) { + memset(dst + 8, 0, 8); + memset(dst + n, 0, 8); + } + } +} + +struct ExternalRepReleaserPair { + CordRep* rep; + void* releaser_address; +}; + +// Allocates a new external `CordRep` and returns a pointer to it and a pointer +// to `releaser_size` bytes where the desired releaser can be constructed. +// Expects `data` to be non-empty. +ExternalRepReleaserPair NewExternalWithUninitializedReleaser( + absl::string_view data, ExternalReleaserInvoker invoker, + size_t releaser_size); + +// Creates a new `CordRep` that owns `data` and `releaser` and returns a pointer +// to it, or `nullptr` if `data` was empty. +template +// NOLINTNEXTLINE - suppress clang-tidy raw pointer return. +CordRep* NewExternalRep(absl::string_view data, Releaser&& releaser) { + static_assert( +#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__) + alignof(Releaser) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__, +#else + alignof(Releaser) <= alignof(max_align_t), +#endif + "Releasers with alignment requirement greater than what is returned by " + "default `::operator new()` are not supported."); + + using ReleaserType = absl::decay_t; + if (data.empty()) { + // Never create empty external nodes. + ::absl::base_internal::Invoke( + ReleaserType(std::forward(releaser)), data); + return nullptr; + } + + auto releaser_invoker = [](void* type_erased_releaser, absl::string_view d) { + auto* my_releaser = static_cast(type_erased_releaser); + ::absl::base_internal::Invoke(std::move(*my_releaser), d); + my_releaser->~ReleaserType(); + return sizeof(Releaser); + }; + + ExternalRepReleaserPair external = NewExternalWithUninitializedReleaser( + data, releaser_invoker, sizeof(releaser)); + ::new (external.releaser_address) + ReleaserType(std::forward(releaser)); + return external.rep; +} + +// Overload for function reference types that dispatches using a function +// pointer because there are no `alignof()` or `sizeof()` a function reference. +// NOLINTNEXTLINE - suppress clang-tidy raw pointer return. +inline CordRep* NewExternalRep(absl::string_view data, + void (&releaser)(absl::string_view)) { + return NewExternalRep(data, &releaser); +} + +} // namespace cord_internal + +template +Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser) { + Cord cord; + cord.contents_.set_tree(::absl::cord_internal::NewExternalRep( + data, std::forward(releaser))); + return cord; +} + +inline Cord::InlineRep::InlineRep(const Cord::InlineRep& src) { + cord_internal::SmallMemmove(data_, src.data_, sizeof(data_)); +} + +inline Cord::InlineRep::InlineRep(Cord::InlineRep&& src) { + memcpy(data_, src.data_, sizeof(data_)); + memset(src.data_, 0, sizeof(data_)); +} + +inline Cord::InlineRep& Cord::InlineRep::operator=(const Cord::InlineRep& src) { + if (this == &src) { + return *this; + } + if (!is_tree() && !src.is_tree()) { + cord_internal::SmallMemmove(data_, src.data_, sizeof(data_)); + return *this; + } + AssignSlow(src); + return *this; +} + +inline Cord::InlineRep& Cord::InlineRep::operator=( + Cord::InlineRep&& src) noexcept { + if (is_tree()) { + ClearSlow(); + } + memcpy(data_, src.data_, sizeof(data_)); + memset(src.data_, 0, sizeof(data_)); + return *this; +} + +inline void Cord::InlineRep::Swap(Cord::InlineRep* rhs) { + if (rhs == this) { + return; + } + + Cord::InlineRep tmp; + cord_internal::SmallMemmove(tmp.data_, data_, sizeof(data_)); + cord_internal::SmallMemmove(data_, rhs->data_, sizeof(data_)); + cord_internal::SmallMemmove(rhs->data_, tmp.data_, sizeof(data_)); +} + +inline const char* Cord::InlineRep::data() const { + return is_tree() ? nullptr : data_; +} + +inline absl::cord_internal::CordRep* Cord::InlineRep::tree() const { + if (is_tree()) { + absl::cord_internal::CordRep* rep; + memcpy(&rep, data_, sizeof(rep)); + return rep; + } else { + return nullptr; + } +} + +inline bool Cord::InlineRep::empty() const { return data_[kMaxInline] == 0; } + +inline size_t Cord::InlineRep::size() const { + const char tag = data_[kMaxInline]; + if (tag <= kMaxInline) return tag; + return static_cast(tree()->length); +} + +inline void Cord::InlineRep::set_tree(absl::cord_internal::CordRep* rep) { + if (rep == nullptr) { + memset(data_, 0, sizeof(data_)); + } else { + bool was_tree = is_tree(); + memcpy(data_, &rep, sizeof(rep)); + memset(data_ + sizeof(rep), 0, sizeof(data_) - sizeof(rep) - 1); + if (!was_tree) { + data_[kMaxInline] = kTreeFlag; + } + } +} + +inline void Cord::InlineRep::replace_tree(absl::cord_internal::CordRep* rep) { + ABSL_ASSERT(is_tree()); + if (ABSL_PREDICT_FALSE(rep == nullptr)) { + set_tree(rep); + return; + } + memcpy(data_, &rep, sizeof(rep)); + memset(data_ + sizeof(rep), 0, sizeof(data_) - sizeof(rep) - 1); +} + +inline absl::cord_internal::CordRep* Cord::InlineRep::clear() { + const char tag = data_[kMaxInline]; + absl::cord_internal::CordRep* result = nullptr; + if (tag > kMaxInline) { + memcpy(&result, data_, sizeof(result)); + } + memset(data_, 0, sizeof(data_)); // Clear the cord + return result; +} + +inline void Cord::InlineRep::CopyToArray(char* dst) const { + assert(!is_tree()); + size_t n = data_[kMaxInline]; + assert(n != 0); + cord_internal::SmallMemmove(dst, data_, n); +} + +constexpr inline Cord::Cord() noexcept {} + +inline Cord& Cord::operator=(const Cord& x) { + contents_ = x.contents_; + return *this; +} + +inline Cord::Cord(Cord&& src) noexcept : contents_(std::move(src.contents_)) {} + +inline Cord& Cord::operator=(Cord&& x) noexcept { + contents_ = std::move(x.contents_); + return *this; +} + +template > +inline Cord& Cord::operator=(T&& src) { + *this = absl::string_view(src); + return *this; +} + +inline size_t Cord::size() const { + // Length is 1st field in str.rep_ + return contents_.size(); +} + +inline bool Cord::empty() const { return contents_.empty(); } + +inline size_t Cord::EstimatedMemoryUsage() const { + size_t result = sizeof(Cord); + if (const absl::cord_internal::CordRep* rep = contents_.tree()) { + result += MemoryUsageAux(rep); + } + return result; +} + +inline absl::string_view Cord::Flatten() { + absl::cord_internal::CordRep* rep = contents_.tree(); + if (rep == nullptr) { + return absl::string_view(contents_.data(), contents_.size()); + } else { + absl::string_view already_flat_contents; + if (GetFlatAux(rep, &already_flat_contents)) { + return already_flat_contents; + } + } + return FlattenSlowPath(); +} + +inline void Cord::Append(absl::string_view src) { + contents_.AppendArray(src.data(), src.size()); +} + +template > +inline void Cord::Append(T&& src) { + // Note that this function reserves the right to reuse the `string&&`'s + // memory and that it will do so in the future. + Append(absl::string_view(src)); +} + +template > +inline void Cord::Prepend(T&& src) { + // Note that this function reserves the right to reuse the `string&&`'s + // memory and that it will do so in the future. + Prepend(absl::string_view(src)); +} + +inline int Cord::Compare(const Cord& rhs) const { + if (!contents_.is_tree() && !rhs.contents_.is_tree()) { + return contents_.BitwiseCompare(rhs.contents_); + } + + return CompareImpl(rhs); +} + +// Does 'this' cord start/end with rhs +inline bool Cord::StartsWith(const Cord& rhs) const { + if (contents_.IsSame(rhs.contents_)) return true; + size_t rhs_size = rhs.size(); + if (size() < rhs_size) return false; + return EqualsImpl(rhs, rhs_size); +} + +inline bool Cord::StartsWith(absl::string_view rhs) const { + size_t rhs_size = rhs.size(); + if (size() < rhs_size) return false; + return EqualsImpl(rhs, rhs_size); +} + +inline Cord::ChunkIterator::ChunkIterator(const Cord* cord) + : bytes_remaining_(cord->size()) { + if (cord->empty()) return; + if (cord->contents_.is_tree()) { + stack_of_right_children_.push_back(cord->contents_.tree()); + operator++(); + } else { + current_chunk_ = absl::string_view(cord->contents_.data(), cord->size()); + } +} + +inline Cord::ChunkIterator Cord::ChunkIterator::operator++(int) { + ChunkIterator tmp(*this); + operator++(); + return tmp; +} + +inline bool Cord::ChunkIterator::operator==(const ChunkIterator& other) const { + return bytes_remaining_ == other.bytes_remaining_; +} + +inline bool Cord::ChunkIterator::operator!=(const ChunkIterator& other) const { + return !(*this == other); +} + +inline Cord::ChunkIterator::reference Cord::ChunkIterator::operator*() const { + assert(bytes_remaining_ != 0); + return current_chunk_; +} + +inline Cord::ChunkIterator::pointer Cord::ChunkIterator::operator->() const { + assert(bytes_remaining_ != 0); + return ¤t_chunk_; +} + +inline void Cord::ChunkIterator::RemoveChunkPrefix(size_t n) { + assert(n < current_chunk_.size()); + current_chunk_.remove_prefix(n); + bytes_remaining_ -= n; +} + +inline void Cord::ChunkIterator::AdvanceBytes(size_t n) { + if (ABSL_PREDICT_TRUE(n < current_chunk_.size())) { + RemoveChunkPrefix(n); + } else if (n != 0) { + AdvanceBytesSlowPath(n); + } +} + +inline Cord::ChunkIterator Cord::chunk_begin() const { + return ChunkIterator(this); +} + +inline Cord::ChunkIterator Cord::chunk_end() const { return ChunkIterator(); } + +inline Cord::ChunkIterator Cord::ChunkRange::begin() const { + return cord_->chunk_begin(); +} + +inline Cord::ChunkIterator Cord::ChunkRange::end() const { + return cord_->chunk_end(); +} + +inline Cord::ChunkRange Cord::Chunks() const { return ChunkRange(this); } + +inline Cord::CharIterator& Cord::CharIterator::operator++() { + if (ABSL_PREDICT_TRUE(chunk_iterator_->size() > 1)) { + chunk_iterator_.RemoveChunkPrefix(1); + } else { + ++chunk_iterator_; + } + return *this; +} + +inline Cord::CharIterator Cord::CharIterator::operator++(int) { + CharIterator tmp(*this); + operator++(); + return tmp; +} + +inline bool Cord::CharIterator::operator==(const CharIterator& other) const { + return chunk_iterator_ == other.chunk_iterator_; +} + +inline bool Cord::CharIterator::operator!=(const CharIterator& other) const { + return !(*this == other); +} + +inline Cord::CharIterator::reference Cord::CharIterator::operator*() const { + return *chunk_iterator_->data(); +} + +inline Cord::CharIterator::pointer Cord::CharIterator::operator->() const { + return chunk_iterator_->data(); +} + +inline Cord Cord::AdvanceAndRead(CharIterator* it, size_t n_bytes) { + assert(it != nullptr); + return it->chunk_iterator_.AdvanceAndReadBytes(n_bytes); +} + +inline void Cord::Advance(CharIterator* it, size_t n_bytes) { + assert(it != nullptr); + it->chunk_iterator_.AdvanceBytes(n_bytes); +} + +inline absl::string_view Cord::ChunkRemaining(const CharIterator& it) { + return *it.chunk_iterator_; +} + +inline Cord::CharIterator Cord::char_begin() const { + return CharIterator(this); +} + +inline Cord::CharIterator Cord::char_end() const { return CharIterator(); } + +inline Cord::CharIterator Cord::CharRange::begin() const { + return cord_->char_begin(); +} + +inline Cord::CharIterator Cord::CharRange::end() const { + return cord_->char_end(); +} + +inline Cord::CharRange Cord::Chars() const { return CharRange(this); } + +inline void Cord::ForEachChunk( + absl::FunctionRef callback) const { + absl::cord_internal::CordRep* rep = contents_.tree(); + if (rep == nullptr) { + callback(absl::string_view(contents_.data(), contents_.size())); + } else { + return ForEachChunkAux(rep, callback); + } +} + +// Nonmember Cord-to-Cord relational operarators. +inline bool operator==(const Cord& lhs, const Cord& rhs) { + if (lhs.contents_.IsSame(rhs.contents_)) return true; + size_t rhs_size = rhs.size(); + if (lhs.size() != rhs_size) return false; + return lhs.EqualsImpl(rhs, rhs_size); +} + +inline bool operator!=(const Cord& x, const Cord& y) { return !(x == y); } +inline bool operator<(const Cord& x, const Cord& y) { + return x.Compare(y) < 0; +} +inline bool operator>(const Cord& x, const Cord& y) { + return x.Compare(y) > 0; +} +inline bool operator<=(const Cord& x, const Cord& y) { + return x.Compare(y) <= 0; +} +inline bool operator>=(const Cord& x, const Cord& y) { + return x.Compare(y) >= 0; +} + +// Nonmember Cord-to-absl::string_view relational operators. +// +// Due to implicit conversions, these also enable comparisons of Cord with +// with std::string, ::string, and const char*. +inline bool operator==(const Cord& lhs, absl::string_view rhs) { + size_t lhs_size = lhs.size(); + size_t rhs_size = rhs.size(); + if (lhs_size != rhs_size) return false; + return lhs.EqualsImpl(rhs, rhs_size); +} + +inline bool operator==(absl::string_view x, const Cord& y) { return y == x; } +inline bool operator!=(const Cord& x, absl::string_view y) { return !(x == y); } +inline bool operator!=(absl::string_view x, const Cord& y) { return !(x == y); } +inline bool operator<(const Cord& x, absl::string_view y) { + return x.Compare(y) < 0; +} +inline bool operator<(absl::string_view x, const Cord& y) { + return y.Compare(x) > 0; +} +inline bool operator>(const Cord& x, absl::string_view y) { return y < x; } +inline bool operator>(absl::string_view x, const Cord& y) { return y < x; } +inline bool operator<=(const Cord& x, absl::string_view y) { return !(y < x); } +inline bool operator<=(absl::string_view x, const Cord& y) { return !(y < x); } +inline bool operator>=(const Cord& x, absl::string_view y) { return !(x < y); } +inline bool operator>=(absl::string_view x, const Cord& y) { return !(x < y); } + +// Overload of swap for Cord. The use of non-const references is +// required. :( +inline void swap(Cord& x, Cord& y) noexcept { y.contents_.Swap(&x.contents_); } + +// Some internals exposed to test code. +namespace strings_internal { +class CordTestAccess { + public: + static size_t FlatOverhead(); + static size_t MaxFlatLength(); + static size_t SizeofCordRepConcat(); + static size_t SizeofCordRepExternal(); + static size_t SizeofCordRepSubstring(); + static size_t FlatTagToLength(uint8_t tag); + static uint8_t LengthToTag(size_t s); +}; +} // namespace strings_internal +ABSL_NAMESPACE_END +} // namespace absl + +#endif // ABSL_STRINGS_CORD_H_ diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc new file mode 100644 index 00000000..434f3a24 --- /dev/null +++ b/absl/strings/cord_test.cc @@ -0,0 +1,1526 @@ +#include "absl/strings/cord.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/base/casts.h" +#include "absl/base/config.h" +#include "absl/base/internal/endian.h" +#include "absl/base/internal/raw_logging.h" +#include "absl/container/fixed_array.h" +#include "absl/strings/cord_test_helpers.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" + +typedef std::mt19937_64 RandomEngine; + +static std::string RandomLowercaseString(RandomEngine* rng); +static std::string RandomLowercaseString(RandomEngine* rng, size_t length); + +static int GetUniformRandomUpTo(RandomEngine* rng, int upper_bound) { + if (upper_bound > 0) { + std::uniform_int_distribution uniform(0, upper_bound - 1); + return uniform(*rng); + } else { + return 0; + } +} + +static size_t GetUniformRandomUpTo(RandomEngine* rng, size_t upper_bound) { + if (upper_bound > 0) { + std::uniform_int_distribution uniform(0, upper_bound - 1); + return uniform(*rng); + } else { + return 0; + } +} + +static int32_t GenerateSkewedRandom(RandomEngine* rng, int max_log) { + const uint32_t base = (*rng)() % (max_log + 1); + const uint32_t mask = ((base < 32) ? (1u << base) : 0u) - 1u; + return (*rng)() & mask; +} + +static std::string RandomLowercaseString(RandomEngine* rng) { + int length; + std::bernoulli_distribution one_in_1k(0.001); + std::bernoulli_distribution one_in_10k(0.0001); + // With low probability, make a large fragment + if (one_in_10k(*rng)) { + length = GetUniformRandomUpTo(rng, 1048576); + } else if (one_in_1k(*rng)) { + length = GetUniformRandomUpTo(rng, 10000); + } else { + length = GenerateSkewedRandom(rng, 10); + } + return RandomLowercaseString(rng, length); +} + +static std::string RandomLowercaseString(RandomEngine* rng, size_t length) { + std::string result(length, '\0'); + std::uniform_int_distribution chars('a', 'z'); + std::generate(result.begin(), result.end(), [&]() { + return static_cast(chars(*rng)); + }); + return result; +} + +static void DoNothing(absl::string_view /* data */, void* /* arg */) {} + +static void DeleteExternalString(absl::string_view data, void* arg) { + std::string* s = reinterpret_cast(arg); + EXPECT_EQ(data, *s); + delete s; +} + +// Add "s" to *dst via `MakeCordFromExternal` +static void AddExternalMemory(absl::string_view s, absl::Cord* dst) { + std::string* str = new std::string(s.data(), s.size()); + dst->Append(absl::MakeCordFromExternal(*str, [str](absl::string_view data) { + DeleteExternalString(data, str); + })); +} + +static void DumpGrowth() { + absl::Cord str; + for (int i = 0; i < 1000; i++) { + char c = 'a' + i % 26; + str.Append(absl::string_view(&c, 1)); + } +} + +// Make a Cord with some number of fragments. Return the size (in bytes) +// of the smallest fragment. +static size_t AppendWithFragments(const std::string& s, RandomEngine* rng, + absl::Cord* cord) { + size_t j = 0; + const size_t max_size = s.size() / 5; // Make approx. 10 fragments + size_t min_size = max_size; // size of smallest fragment + while (j < s.size()) { + size_t N = 1 + GetUniformRandomUpTo(rng, max_size); + if (N > (s.size() - j)) { + N = s.size() - j; + } + if (N < min_size) { + min_size = N; + } + + std::bernoulli_distribution coin_flip(0.5); + if (coin_flip(*rng)) { + // Grow by adding an external-memory. + AddExternalMemory(absl::string_view(s.data() + j, N), cord); + } else { + cord->Append(absl::string_view(s.data() + j, N)); + } + j += N; + } + return min_size; +} + +// Add an external memory that contains the specified std::string to cord +static void AddNewStringBlock(const std::string& str, absl::Cord* dst) { + char* data = new char[str.size()]; + memcpy(data, str.data(), str.size()); + dst->Append(absl::MakeCordFromExternal( + absl::string_view(data, str.size()), + [](absl::string_view s) { delete[] s.data(); })); +} + +// Make a Cord out of many different types of nodes. +static absl::Cord MakeComposite() { + absl::Cord cord; + cord.Append("the"); + AddExternalMemory(" quick brown", &cord); + AddExternalMemory(" fox jumped", &cord); + + absl::Cord full(" over"); + AddExternalMemory(" the lazy", &full); + AddNewStringBlock(" dog slept the whole day away", &full); + absl::Cord substring = full.Subcord(0, 18); + + // Make substring long enough to defeat the copying fast path in Append. + substring.Append(std::string(1000, '.')); + cord.Append(substring); + cord = cord.Subcord(0, cord.size() - 998); // Remove most of extra junk + + return cord; +} + +namespace absl { +ABSL_NAMESPACE_BEGIN + +class CordTestPeer { + public: + static void ForEachChunk( + const Cord& c, absl::FunctionRef callback) { + c.ForEachChunk(callback); + } +}; + +ABSL_NAMESPACE_END +} // namespace absl + +TEST(Cord, AllFlatSizes) { + using absl::strings_internal::CordTestAccess; + + for (size_t s = 0; s < CordTestAccess::MaxFlatLength(); s++) { + // Make a std::string of length s. + std::string src; + while (src.size() < s) { + src.push_back('a' + (src.size() % 26)); + } + + absl::Cord dst(src); + EXPECT_EQ(std::string(dst), src) << s; + } +} + +// We create a Cord at least 128GB in size using the fact that Cords can +// internally reference-count; thus the Cord is enormous without actually +// consuming very much memory. +TEST(GigabyteCord, FromExternal) { + const size_t one_gig = 1024U * 1024U * 1024U; + size_t max_size = 2 * one_gig; + if (sizeof(max_size) > 4) max_size = 128 * one_gig; + + size_t length = 128 * 1024; + char* data = new char[length]; + absl::Cord from = absl::MakeCordFromExternal( + absl::string_view(data, length), + [](absl::string_view sv) { delete[] sv.data(); }); + + // This loop may seem odd due to its combination of exponential doubling of + // size and incremental size increases. We do it incrementally to be sure the + // Cord will need rebalancing and will exercise code that, in the past, has + // caused crashes in production. We grow exponentially so that the code will + // execute in a reasonable amount of time. + absl::Cord c; + ABSL_RAW_LOG(INFO, "Made a Cord with %zu bytes!", c.size()); + c.Append(from); + while (c.size() < max_size) { + c.Append(c); + c.Append(from); + c.Append(from); + c.Append(from); + c.Append(from); + } + + for (int i = 0; i < 1024; ++i) { + c.Append(from); + } + ABSL_RAW_LOG(INFO, "Made a Cord with %zu bytes!", c.size()); + // Note: on a 32-bit build, this comes out to 2,818,048,000 bytes. + // Note: on a 64-bit build, this comes out to 171,932,385,280 bytes. +} + +static absl::Cord MakeExternalCord(int size) { + char* buffer = new char[size]; + memset(buffer, 'x', size); + absl::Cord cord; + cord.Append(absl::MakeCordFromExternal( + absl::string_view(buffer, size), + [](absl::string_view s) { delete[] s.data(); })); + return cord; +} + +// Extern to fool clang that this is not constant. Needed to suppress +// a warning of unsafe code we want to test. +extern bool my_unique_true_boolean; +bool my_unique_true_boolean = true; + +TEST(Cord, Assignment) { + absl::Cord x(absl::string_view("hi there")); + absl::Cord y(x); + ASSERT_EQ(std::string(x), "hi there"); + ASSERT_EQ(std::string(y), "hi there"); + ASSERT_TRUE(x == y); + ASSERT_TRUE(x <= y); + ASSERT_TRUE(y <= x); + + x = absl::string_view("foo"); + ASSERT_EQ(std::string(x), "foo"); + ASSERT_EQ(std::string(y), "hi there"); + ASSERT_TRUE(x < y); + ASSERT_TRUE(y > x); + ASSERT_TRUE(x != y); + ASSERT_TRUE(x <= y); + ASSERT_TRUE(y >= x); + + x = "foo"; + ASSERT_EQ(x, "foo"); + + // Test that going from inline rep to tree we don't leak memory. + std::vector> + test_string_pairs = {{"hi there", "foo"}, + {"loooooong coooooord", "short cord"}, + {"short cord", "loooooong coooooord"}, + {"loooooong coooooord1", "loooooong coooooord2"}}; + for (std::pair test_strings : + test_string_pairs) { + absl::Cord tmp(test_strings.first); + absl::Cord z(std::move(tmp)); + ASSERT_EQ(std::string(z), test_strings.first); + tmp = test_strings.second; + z = std::move(tmp); + ASSERT_EQ(std::string(z), test_strings.second); + } + { + // Test that self-move assignment doesn't crash/leak. + // Do not write such code! + absl::Cord my_small_cord("foo"); + absl::Cord my_big_cord("loooooong coooooord"); + // Bypass clang's warning on self move-assignment. + absl::Cord* my_small_alias = + my_unique_true_boolean ? &my_small_cord : &my_big_cord; + absl::Cord* my_big_alias = + !my_unique_true_boolean ? &my_small_cord : &my_big_cord; + + *my_small_alias = std::move(my_small_cord); + *my_big_alias = std::move(my_big_cord); + // my_small_cord and my_big_cord are in an unspecified but valid + // state, and will be correctly destroyed here. + } +} + +TEST(Cord, StartsEndsWith) { + absl::Cord x(absl::string_view("abcde")); + absl::Cord empty(""); + + ASSERT_TRUE(x.StartsWith(absl::Cord("abcde"))); + ASSERT_TRUE(x.StartsWith(absl::Cord("abc"))); + ASSERT_TRUE(x.StartsWith(absl::Cord(""))); + ASSERT_TRUE(empty.StartsWith(absl::Cord(""))); + ASSERT_TRUE(x.EndsWith(absl::Cord("abcde"))); + ASSERT_TRUE(x.EndsWith(absl::Cord("cde"))); + ASSERT_TRUE(x.EndsWith(absl::Cord(""))); + ASSERT_TRUE(empty.EndsWith(absl::Cord(""))); + + ASSERT_TRUE(!x.StartsWith(absl::Cord("xyz"))); + ASSERT_TRUE(!empty.StartsWith(absl::Cord("xyz"))); + ASSERT_TRUE(!x.EndsWith(absl::Cord("xyz"))); + ASSERT_TRUE(!empty.EndsWith(absl::Cord("xyz"))); + + ASSERT_TRUE(x.StartsWith("abcde")); + ASSERT_TRUE(x.StartsWith("abc")); + ASSERT_TRUE(x.StartsWith("")); + ASSERT_TRUE(empty.StartsWith("")); + ASSERT_TRUE(x.EndsWith("abcde")); + ASSERT_TRUE(x.EndsWith("cde")); + ASSERT_TRUE(x.EndsWith("")); + ASSERT_TRUE(empty.EndsWith("")); + + ASSERT_TRUE(!x.StartsWith("xyz")); + ASSERT_TRUE(!empty.StartsWith("xyz")); + ASSERT_TRUE(!x.EndsWith("xyz")); + ASSERT_TRUE(!empty.EndsWith("xyz")); +} + +TEST(Cord, Subcord) { + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + const std::string s = RandomLowercaseString(&rng, 1024); + + absl::Cord a; + AppendWithFragments(s, &rng, &a); + ASSERT_EQ(s.size(), a.size()); + + // Check subcords of a, from a variety of interesting points. + std::set positions; + for (int i = 0; i <= 32; ++i) { + positions.insert(i); + positions.insert(i * 32 - 1); + positions.insert(i * 32); + positions.insert(i * 32 + 1); + positions.insert(a.size() - i); + } + positions.insert(237); + positions.insert(732); + for (size_t pos : positions) { + if (pos > a.size()) continue; + for (size_t end_pos : positions) { + if (end_pos < pos || end_pos > a.size()) continue; + absl::Cord sa = a.Subcord(pos, end_pos - pos); + EXPECT_EQ(absl::string_view(s).substr(pos, end_pos - pos), + std::string(sa)) + << a; + } + } + + // Do the same thing for an inline cord. + const std::string sh = "short"; + absl::Cord c(sh); + for (size_t pos = 0; pos <= sh.size(); ++pos) { + for (size_t n = 0; n <= sh.size() - pos; ++n) { + absl::Cord sc = c.Subcord(pos, n); + EXPECT_EQ(sh.substr(pos, n), std::string(sc)) << c; + } + } + + // Check subcords of subcords. + absl::Cord sa = a.Subcord(0, a.size()); + std::string ss = s.substr(0, s.size()); + while (sa.size() > 1) { + sa = sa.Subcord(1, sa.size() - 2); + ss = ss.substr(1, ss.size() - 2); + EXPECT_EQ(ss, std::string(sa)) << a; + if (HasFailure()) break; // halt cascade + } + + // It is OK to ask for too much. + sa = a.Subcord(0, a.size() + 1); + EXPECT_EQ(s, std::string(sa)); + + // It is OK to ask for something beyond the end. + sa = a.Subcord(a.size() + 1, 0); + EXPECT_TRUE(sa.empty()); + sa = a.Subcord(a.size() + 1, 1); + EXPECT_TRUE(sa.empty()); +} + +TEST(Cord, Swap) { + absl::string_view a("Dexter"); + absl::string_view b("Mandark"); + absl::Cord x(a); + absl::Cord y(b); + swap(x, y); + ASSERT_EQ(x, absl::Cord(b)); + ASSERT_EQ(y, absl::Cord(a)); +} + +static void VerifyCopyToString(const absl::Cord& cord) { + std::string initially_empty; + absl::CopyCordToString(cord, &initially_empty); + EXPECT_EQ(initially_empty, cord); + + constexpr size_t kInitialLength = 1024; + std::string has_initial_contents(kInitialLength, 'x'); + const char* address_before_copy = has_initial_contents.data(); + absl::CopyCordToString(cord, &has_initial_contents); + EXPECT_EQ(has_initial_contents, cord); + + if (cord.size() <= kInitialLength) { + EXPECT_EQ(has_initial_contents.data(), address_before_copy) + << "CopyCordToString allocated new std::string storage; " + "has_initial_contents = \"" + << has_initial_contents << "\""; + } +} + +TEST(Cord, CopyToString) { + VerifyCopyToString(absl::Cord()); + VerifyCopyToString(absl::Cord("small cord")); + VerifyCopyToString( + absl::MakeFragmentedCord({"fragmented ", "cord ", "to ", "test ", + "copying ", "to ", "a ", "string."})); +} + +static bool IsFlat(const absl::Cord& c) { + return c.chunk_begin() == c.chunk_end() || ++c.chunk_begin() == c.chunk_end(); +} + +static void VerifyFlatten(absl::Cord c) { + std::string old_contents(c); + absl::string_view old_flat; + bool already_flat_and_non_empty = IsFlat(c) && !c.empty(); + if (already_flat_and_non_empty) { + old_flat = *c.chunk_begin(); + } + absl::string_view new_flat = c.Flatten(); + + // Verify that the contents of the flattened Cord are correct. + EXPECT_EQ(new_flat, old_contents); + EXPECT_EQ(std::string(c), old_contents); + + // If the Cord contained data and was already flat, verify that the data + // wasn't copied. + if (already_flat_and_non_empty) { + EXPECT_EQ(old_flat.data(), new_flat.data()) + << "Allocated new memory even though the Cord was already flat."; + } + + // Verify that the flattened Cord is in fact flat. + EXPECT_TRUE(IsFlat(c)); +} + +TEST(Cord, Flatten) { + VerifyFlatten(absl::Cord()); + VerifyFlatten(absl::Cord("small cord")); + VerifyFlatten(absl::Cord("larger than small buffer optimization")); + VerifyFlatten(absl::MakeFragmentedCord({"small ", "fragmented ", "cord"})); + + // Test with a cord that is longer than the largest flat buffer + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + VerifyFlatten(absl::Cord(RandomLowercaseString(&rng, 8192))); +} + +// Test data +namespace { +class TestData { + private: + std::vector data_; + + // Return a std::string of the specified length. + static std::string MakeString(int length) { + std::string result; + char buf[30]; + snprintf(buf, sizeof(buf), "(%d)", length); + while (result.size() < length) { + result += buf; + } + result.resize(length); + return result; + } + + public: + TestData() { + // short strings increasing in length by one + for (int i = 0; i < 30; i++) { + data_.push_back(MakeString(i)); + } + + // strings around half kMaxFlatLength + static const int kMaxFlatLength = 4096 - 9; + static const int kHalf = kMaxFlatLength / 2; + + for (int i = -10; i <= +10; i++) { + data_.push_back(MakeString(kHalf + i)); + } + + for (int i = -10; i <= +10; i++) { + data_.push_back(MakeString(kMaxFlatLength + i)); + } + } + + size_t size() const { return data_.size(); } + const std::string& data(size_t i) const { return data_[i]; } +}; +} // namespace + +TEST(Cord, MultipleLengths) { + TestData d; + for (size_t i = 0; i < d.size(); i++) { + std::string a = d.data(i); + + { // Construct from Cord + absl::Cord tmp(a); + absl::Cord x(tmp); + EXPECT_EQ(a, std::string(x)) << "'" << a << "'"; + } + + { // Construct from absl::string_view + absl::Cord x(a); + EXPECT_EQ(a, std::string(x)) << "'" << a << "'"; + } + + { // Append cord to self + absl::Cord self(a); + self.Append(self); + EXPECT_EQ(a + a, std::string(self)) << "'" << a << "' + '" << a << "'"; + } + + { // Prepend cord to self + absl::Cord self(a); + self.Prepend(self); + EXPECT_EQ(a + a, std::string(self)) << "'" << a << "' + '" << a << "'"; + } + + // Try to append/prepend others + for (size_t j = 0; j < d.size(); j++) { + std::string b = d.data(j); + + { // CopyFrom Cord + absl::Cord x(a); + absl::Cord y(b); + x = y; + EXPECT_EQ(b, std::string(x)) << "'" << a << "' + '" << b << "'"; + } + + { // CopyFrom absl::string_view + absl::Cord x(a); + x = b; + EXPECT_EQ(b, std::string(x)) << "'" << a << "' + '" << b << "'"; + } + + { // Cord::Append(Cord) + absl::Cord x(a); + absl::Cord y(b); + x.Append(y); + EXPECT_EQ(a + b, std::string(x)) << "'" << a << "' + '" << b << "'"; + } + + { // Cord::Append(absl::string_view) + absl::Cord x(a); + x.Append(b); + EXPECT_EQ(a + b, std::string(x)) << "'" << a << "' + '" << b << "'"; + } + + { // Cord::Prepend(Cord) + absl::Cord x(a); + absl::Cord y(b); + x.Prepend(y); + EXPECT_EQ(b + a, std::string(x)) << "'" << b << "' + '" << a << "'"; + } + + { // Cord::Prepend(absl::string_view) + absl::Cord x(a); + x.Prepend(b); + EXPECT_EQ(b + a, std::string(x)) << "'" << b << "' + '" << a << "'"; + } + } + } +} + +namespace { + +TEST(Cord, RemoveSuffixWithExternalOrSubstring) { + absl::Cord cord = absl::MakeCordFromExternal( + "foo bar baz", [](absl::string_view s) { DoNothing(s, nullptr); }); + + EXPECT_EQ("foo bar baz", std::string(cord)); + + // This RemoveSuffix() will wrap the EXTERNAL node in a SUBSTRING node. + cord.RemoveSuffix(4); + EXPECT_EQ("foo bar", std::string(cord)); + + // This RemoveSuffix() will adjust the SUBSTRING node in-place. + cord.RemoveSuffix(4); + EXPECT_EQ("foo", std::string(cord)); +} + +TEST(Cord, RemoveSuffixMakesZeroLengthNode) { + absl::Cord c; + c.Append(absl::Cord(std::string(100, 'x'))); + absl::Cord other_ref = c; // Prevent inplace appends + c.Append(absl::Cord(std::string(200, 'y'))); + c.RemoveSuffix(200); + EXPECT_EQ(std::string(100, 'x'), std::string(c)); +} + +} // namespace + +// CordSpliceTest contributed by hendrie. +namespace { + +// Create a cord with an external memory block filled with 'z' +absl::Cord CordWithZedBlock(size_t size) { + char* data = new char[size]; + if (size > 0) { + memset(data, 'z', size); + } + absl::Cord cord = absl::MakeCordFromExternal( + absl::string_view(data, size), + [](absl::string_view s) { delete[] s.data(); }); + return cord; +} + +// Establish that ZedBlock does what we think it does. +TEST(CordSpliceTest, ZedBlock) { + absl::Cord blob = CordWithZedBlock(10); + EXPECT_EQ(10, blob.size()); + std::string s; + absl::CopyCordToString(blob, &s); + EXPECT_EQ("zzzzzzzzzz", s); +} + +TEST(CordSpliceTest, ZedBlock0) { + absl::Cord blob = CordWithZedBlock(0); + EXPECT_EQ(0, blob.size()); + std::string s; + absl::CopyCordToString(blob, &s); + EXPECT_EQ("", s); +} + +TEST(CordSpliceTest, ZedBlockSuffix1) { + absl::Cord blob = CordWithZedBlock(10); + EXPECT_EQ(10, blob.size()); + absl::Cord suffix(blob); + suffix.RemovePrefix(9); + EXPECT_EQ(1, suffix.size()); + std::string s; + absl::CopyCordToString(suffix, &s); + EXPECT_EQ("z", s); +} + +// Remove all of a prefix block +TEST(CordSpliceTest, ZedBlockSuffix0) { + absl::Cord blob = CordWithZedBlock(10); + EXPECT_EQ(10, blob.size()); + absl::Cord suffix(blob); + suffix.RemovePrefix(10); + EXPECT_EQ(0, suffix.size()); + std::string s; + absl::CopyCordToString(suffix, &s); + EXPECT_EQ("", s); +} + +absl::Cord BigCord(size_t len, char v) { + std::string s(len, v); + return absl::Cord(s); +} + +// Splice block into cord. +absl::Cord SpliceCord(const absl::Cord& blob, int64_t offset, + const absl::Cord& block) { + ABSL_RAW_CHECK(offset >= 0, ""); + ABSL_RAW_CHECK(offset + block.size() <= blob.size(), ""); + absl::Cord result(blob); + result.RemoveSuffix(blob.size() - offset); + result.Append(block); + absl::Cord suffix(blob); + suffix.RemovePrefix(offset + block.size()); + result.Append(suffix); + ABSL_RAW_CHECK(blob.size() == result.size(), ""); + return result; +} + +// Taking an empty suffix of a block breaks appending. +TEST(CordSpliceTest, RemoveEntireBlock1) { + absl::Cord zero = CordWithZedBlock(10); + absl::Cord suffix(zero); + suffix.RemovePrefix(10); + absl::Cord result; + result.Append(suffix); +} + +TEST(CordSpliceTest, RemoveEntireBlock2) { + absl::Cord zero = CordWithZedBlock(10); + absl::Cord prefix(zero); + prefix.RemoveSuffix(10); + absl::Cord suffix(zero); + suffix.RemovePrefix(10); + absl::Cord result(prefix); + result.Append(suffix); +} + +TEST(CordSpliceTest, RemoveEntireBlock3) { + absl::Cord blob = CordWithZedBlock(10); + absl::Cord block = BigCord(10, 'b'); + blob = SpliceCord(blob, 0, block); +} + +struct CordCompareTestCase { + template + CordCompareTestCase(const LHS& lhs, const RHS& rhs) + : lhs_cord(lhs), rhs_cord(rhs) {} + + absl::Cord lhs_cord; + absl::Cord rhs_cord; +}; + +const auto sign = [](int x) { return x == 0 ? 0 : (x > 0 ? 1 : -1); }; + +void VerifyComparison(const CordCompareTestCase& test_case) { + std::string lhs_string(test_case.lhs_cord); + std::string rhs_string(test_case.rhs_cord); + int expected = sign(lhs_string.compare(rhs_string)); + EXPECT_EQ(expected, test_case.lhs_cord.Compare(test_case.rhs_cord)) + << "LHS=" << lhs_string << "; RHS=" << rhs_string; + EXPECT_EQ(expected, test_case.lhs_cord.Compare(rhs_string)) + << "LHS=" << lhs_string << "; RHS=" << rhs_string; + EXPECT_EQ(-expected, test_case.rhs_cord.Compare(test_case.lhs_cord)) + << "LHS=" << rhs_string << "; RHS=" << lhs_string; + EXPECT_EQ(-expected, test_case.rhs_cord.Compare(lhs_string)) + << "LHS=" << rhs_string << "; RHS=" << lhs_string; +} + +TEST(Cord, Compare) { + absl::Cord subcord("aaaaaBBBBBcccccDDDDD"); + subcord = subcord.Subcord(3, 10); + + absl::Cord tmp("aaaaaaaaaaaaaaaa"); + tmp.Append("BBBBBBBBBBBBBBBB"); + absl::Cord concat = absl::Cord("cccccccccccccccc"); + concat.Append("DDDDDDDDDDDDDDDD"); + concat.Prepend(tmp); + + absl::Cord concat2("aaaaaaaaaaaaa"); + concat2.Append("aaaBBBBBBBBBBBBBBBBccccc"); + concat2.Append("cccccccccccDDDDDDDDDDDDDD"); + concat2.Append("DD"); + + std::vector test_cases = {{ + // Inline cords + {"abcdef", "abcdef"}, + {"abcdef", "abcdee"}, + {"abcdef", "abcdeg"}, + {"bbcdef", "abcdef"}, + {"bbcdef", "abcdeg"}, + {"abcdefa", "abcdef"}, + {"abcdef", "abcdefa"}, + + // Small flat cords + {"aaaaaBBBBBcccccDDDDD", "aaaaaBBBBBcccccDDDDD"}, + {"aaaaaBBBBBcccccDDDDD", "aaaaaBBBBBxccccDDDDD"}, + {"aaaaaBBBBBcxcccDDDDD", "aaaaaBBBBBcccccDDDDD"}, + {"aaaaaBBBBBxccccDDDDD", "aaaaaBBBBBcccccDDDDX"}, + {"aaaaaBBBBBcccccDDDDDa", "aaaaaBBBBBcccccDDDDD"}, + {"aaaaaBBBBBcccccDDDDD", "aaaaaBBBBBcccccDDDDDa"}, + + // Subcords + {subcord, subcord}, + {subcord, "aaBBBBBccc"}, + {subcord, "aaBBBBBccd"}, + {subcord, "aaBBBBBccb"}, + {subcord, "aaBBBBBxcb"}, + {subcord, "aaBBBBBccca"}, + {subcord, "aaBBBBBcc"}, + + // Concats + {concat, concat}, + {concat, + "aaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBccccccccccccccccDDDDDDDDDDDDDDDD"}, + {concat, + "aaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBcccccccccccccccxDDDDDDDDDDDDDDDD"}, + {concat, + "aaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBacccccccccccccccDDDDDDDDDDDDDDDD"}, + {concat, + "aaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBccccccccccccccccDDDDDDDDDDDDDDD"}, + {concat, + "aaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBccccccccccccccccDDDDDDDDDDDDDDDDe"}, + + {concat, concat2}, + }}; + + for (const auto& tc : test_cases) { + VerifyComparison(tc); + } +} + +TEST(Cord, CompareAfterAssign) { + absl::Cord a("aaaaaa1111111"); + absl::Cord b("aaaaaa2222222"); + a = "cccccc"; + b = "cccccc"; + EXPECT_EQ(a, b); + EXPECT_FALSE(a < b); + + a = "aaaa"; + b = "bbbbb"; + a = ""; + b = ""; + EXPECT_EQ(a, b); + EXPECT_FALSE(a < b); +} + +// Test CompareTo() and ComparePrefix() against string and substring +// comparison methods from std::basic_string. +static void TestCompare(const absl::Cord& c, const absl::Cord& d, + RandomEngine* rng) { + typedef std::basic_string ustring; + ustring cs(reinterpret_cast(std::string(c).data()), c.size()); + ustring ds(reinterpret_cast(std::string(d).data()), d.size()); + // ustring comparison is ideal because we expect Cord comparisons to be + // based on unsigned byte comparisons regardless of whether char is signed. + int expected = sign(cs.compare(ds)); + EXPECT_EQ(expected, sign(c.Compare(d))) << c << ", " << d; +} + +TEST(Compare, ComparisonIsUnsigned) { + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + std::uniform_int_distribution uniform_uint8(0, 255); + char x = static_cast(uniform_uint8(rng)); + TestCompare( + absl::Cord(std::string(GetUniformRandomUpTo(&rng, 100), x)), + absl::Cord(std::string(GetUniformRandomUpTo(&rng, 100), x ^ 0x80)), &rng); +} + +TEST(Compare, RandomComparisons) { + const int kIters = 5000; + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + + int n = GetUniformRandomUpTo(&rng, 5000); + absl::Cord a[] = {MakeExternalCord(n), + absl::Cord("ant"), + absl::Cord("elephant"), + absl::Cord("giraffe"), + absl::Cord(std::string(GetUniformRandomUpTo(&rng, 100), + GetUniformRandomUpTo(&rng, 100))), + absl::Cord(""), + absl::Cord("x"), + absl::Cord("A"), + absl::Cord("B"), + absl::Cord("C")}; + for (int i = 0; i < kIters; i++) { + absl::Cord c, d; + for (int j = 0; j < (i % 7) + 1; j++) { + c.Append(a[GetUniformRandomUpTo(&rng, ABSL_ARRAYSIZE(a))]); + d.Append(a[GetUniformRandomUpTo(&rng, ABSL_ARRAYSIZE(a))]); + } + std::bernoulli_distribution coin_flip(0.5); + TestCompare(coin_flip(rng) ? c : absl::Cord(std::string(c)), + coin_flip(rng) ? d : absl::Cord(std::string(d)), &rng); + } +} + +template +void CompareOperators() { + const T1 a("a"); + const T2 b("b"); + + EXPECT_TRUE(a == a); + // For pointer type (i.e. `const char*`), operator== compares the address + // instead of the std::string, so `a == const char*("a")` isn't necessarily true. + EXPECT_TRUE(std::is_pointer::value || a == T1("a")); + EXPECT_TRUE(std::is_pointer::value || a == T2("a")); + EXPECT_FALSE(a == b); + + EXPECT_TRUE(a != b); + EXPECT_FALSE(a != a); + + EXPECT_TRUE(a < b); + EXPECT_FALSE(b < a); + + EXPECT_TRUE(b > a); + EXPECT_FALSE(a > b); + + EXPECT_TRUE(a >= a); + EXPECT_TRUE(b >= a); + EXPECT_FALSE(a >= b); + + EXPECT_TRUE(a <= a); + EXPECT_TRUE(a <= b); + EXPECT_FALSE(b <= a); +} + +TEST(ComparisonOperators, Cord_Cord) { + CompareOperators(); +} + +TEST(ComparisonOperators, Cord_StringPiece) { + CompareOperators(); +} + +TEST(ComparisonOperators, StringPiece_Cord) { + CompareOperators(); +} + +TEST(ComparisonOperators, Cord_string) { + CompareOperators(); +} + +TEST(ComparisonOperators, string_Cord) { + CompareOperators(); +} + +TEST(ComparisonOperators, stdstring_Cord) { + CompareOperators(); +} + +TEST(ComparisonOperators, Cord_stdstring) { + CompareOperators(); +} + +TEST(ComparisonOperators, charstar_Cord) { + CompareOperators(); +} + +TEST(ComparisonOperators, Cord_charstar) { + CompareOperators(); +} + +TEST(ConstructFromExternal, ReleaserInvoked) { + // Empty external memory means the releaser should be called immediately. + { + bool invoked = false; + auto releaser = [&invoked](absl::string_view) { invoked = true; }; + { + auto c = absl::MakeCordFromExternal("", releaser); + EXPECT_TRUE(invoked); + } + } + + // If the size of the data is small enough, a future constructor + // implementation may copy the bytes and immediately invoke the releaser + // instead of creating an external node. We make a large dummy std::string to + // make this test independent of such an optimization. + std::string large_dummy(2048, 'c'); + { + bool invoked = false; + auto releaser = [&invoked](absl::string_view) { invoked = true; }; + { + auto c = absl::MakeCordFromExternal(large_dummy, releaser); + EXPECT_FALSE(invoked); + } + EXPECT_TRUE(invoked); + } + + { + bool invoked = false; + auto releaser = [&invoked](absl::string_view) { invoked = true; }; + { + absl::Cord copy; + { + auto c = absl::MakeCordFromExternal(large_dummy, releaser); + copy = c; + EXPECT_FALSE(invoked); + } + EXPECT_FALSE(invoked); + } + EXPECT_TRUE(invoked); + } +} + +TEST(ConstructFromExternal, CompareContents) { + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + + for (int length = 1; length <= 2048; length *= 2) { + std::string data = RandomLowercaseString(&rng, length); + auto* external = new std::string(data); + auto cord = + absl::MakeCordFromExternal(*external, [external](absl::string_view sv) { + EXPECT_EQ(external->data(), sv.data()); + EXPECT_EQ(external->size(), sv.size()); + delete external; + }); + EXPECT_EQ(data, cord); + } +} + +TEST(ConstructFromExternal, LargeReleaser) { + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + constexpr size_t kLength = 256; + std::string data = RandomLowercaseString(&rng, kLength); + std::array data_array; + for (size_t i = 0; i < kLength; ++i) data_array[i] = data[i]; + bool invoked = false; + auto releaser = [data_array, &invoked](absl::string_view data) { + EXPECT_EQ(data, absl::string_view(data_array.data(), data_array.size())); + invoked = true; + }; + (void)absl::MakeCordFromExternal(data, releaser); + EXPECT_TRUE(invoked); +} + +TEST(ConstructFromExternal, FunctionPointerReleaser) { + static absl::string_view data("hello world"); + static bool invoked; + auto* releaser = + static_cast([](absl::string_view sv) { + EXPECT_EQ(data, sv); + invoked = true; + }); + invoked = false; + (void)absl::MakeCordFromExternal(data, releaser); + EXPECT_TRUE(invoked); + + invoked = false; + (void)absl::MakeCordFromExternal(data, *releaser); + EXPECT_TRUE(invoked); +} + +TEST(ConstructFromExternal, MoveOnlyReleaser) { + struct Releaser { + explicit Releaser(bool* invoked) : invoked(invoked) {} + Releaser(Releaser&& other) noexcept : invoked(other.invoked) {} + void operator()(absl::string_view) const { *invoked = true; } + + bool* invoked; + }; + + bool invoked = false; + (void)absl::MakeCordFromExternal("dummy", Releaser(&invoked)); + EXPECT_TRUE(invoked); +} + +TEST(ConstructFromExternal, NonTrivialReleaserDestructor) { + struct Releaser { + explicit Releaser(bool* destroyed) : destroyed(destroyed) {} + ~Releaser() { *destroyed = true; } + void operator()(absl::string_view) const {} + + bool* destroyed; + }; + + bool destroyed = false; + Releaser releaser(&destroyed); + (void)absl::MakeCordFromExternal("dummy", releaser); + EXPECT_TRUE(destroyed); +} + +TEST(ConstructFromExternal, ReferenceQualifierOverloads) { + struct Releaser { + void operator()(absl::string_view) & { *lvalue_invoked = true; } + void operator()(absl::string_view) && { *rvalue_invoked = true; } + + bool* lvalue_invoked; + bool* rvalue_invoked; + }; + + bool lvalue_invoked = false; + bool rvalue_invoked = false; + Releaser releaser = {&lvalue_invoked, &rvalue_invoked}; + (void)absl::MakeCordFromExternal("", releaser); + EXPECT_FALSE(lvalue_invoked); + EXPECT_TRUE(rvalue_invoked); + rvalue_invoked = false; + + (void)absl::MakeCordFromExternal("dummy", releaser); + EXPECT_FALSE(lvalue_invoked); + EXPECT_TRUE(rvalue_invoked); + rvalue_invoked = false; + + // NOLINTNEXTLINE: suppress clang-tidy std::move on trivially copyable type. + (void)absl::MakeCordFromExternal("dummy", std::move(releaser)); + EXPECT_FALSE(lvalue_invoked); + EXPECT_TRUE(rvalue_invoked); +} + +TEST(ExternalMemory, BasicUsage) { + static const char* strings[] = { "", "hello", "there" }; + for (const char* str : strings) { + absl::Cord dst("(prefix)"); + AddExternalMemory(str, &dst); + dst.Append("(suffix)"); + EXPECT_EQ((std::string("(prefix)") + str + std::string("(suffix)")), + std::string(dst)); + } +} + +TEST(ExternalMemory, RemovePrefixSuffix) { + // Exhaustively try all sub-strings. + absl::Cord cord = MakeComposite(); + std::string s = std::string(cord); + for (int offset = 0; offset <= s.size(); offset++) { + for (int length = 0; length <= s.size() - offset; length++) { + absl::Cord result(cord); + result.RemovePrefix(offset); + result.RemoveSuffix(result.size() - length); + EXPECT_EQ(s.substr(offset, length), std::string(result)) + << offset << " " << length; + } + } +} + +TEST(ExternalMemory, Get) { + absl::Cord cord("hello"); + AddExternalMemory(" world!", &cord); + AddExternalMemory(" how are ", &cord); + cord.Append(" you?"); + std::string s = std::string(cord); + for (int i = 0; i < s.size(); i++) { + EXPECT_EQ(s[i], cord[i]); + } +} + +// CordMemoryUsage tests verify the correctness of the EstimatedMemoryUsage() +// These tests take into account that the reported memory usage is approximate +// and non-deterministic. For all tests, We verify that the reported memory +// usage is larger than `size()`, and less than `size() * 1.5` as a cord should +// never reserve more 'extra' capacity than half of its size as it grows. +// Additionally we have some whiteboxed expectations based on our knowledge of +// the layout and size of empty and inlined cords, and flat nodes. + +TEST(CordMemoryUsage, Empty) { + EXPECT_EQ(sizeof(absl::Cord), absl::Cord().EstimatedMemoryUsage()); +} + +TEST(CordMemoryUsage, Embedded) { + absl::Cord a("hello"); + EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord)); +} + +TEST(CordMemoryUsage, EmbeddedAppend) { + absl::Cord a("a"); + absl::Cord b("bcd"); + EXPECT_EQ(b.EstimatedMemoryUsage(), sizeof(absl::Cord)); + a.Append(b); + EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord)); +} + +TEST(CordMemoryUsage, ExternalMemory) { + static const int kLength = 1000; + absl::Cord cord; + AddExternalMemory(std::string(kLength, 'x'), &cord); + EXPECT_GT(cord.EstimatedMemoryUsage(), kLength); + EXPECT_LE(cord.EstimatedMemoryUsage(), kLength * 1.5); +} + +TEST(CordMemoryUsage, Flat) { + static const int kLength = 125; + absl::Cord a(std::string(kLength, 'a')); + EXPECT_GT(a.EstimatedMemoryUsage(), kLength); + EXPECT_LE(a.EstimatedMemoryUsage(), kLength * 1.5); +} + +TEST(CordMemoryUsage, AppendFlat) { + using absl::strings_internal::CordTestAccess; + absl::Cord a(std::string(CordTestAccess::MaxFlatLength(), 'a')); + size_t length = a.EstimatedMemoryUsage(); + a.Append(std::string(CordTestAccess::MaxFlatLength(), 'b')); + size_t delta = a.EstimatedMemoryUsage() - length; + EXPECT_GT(delta, CordTestAccess::MaxFlatLength()); + EXPECT_LE(delta, CordTestAccess::MaxFlatLength() * 1.5); +} + +// Regtest for a change that had to be rolled back because it expanded out +// of the InlineRep too soon, which was observable through MemoryUsage(). +TEST(CordMemoryUsage, InlineRep) { + constexpr size_t kMaxInline = 15; // Cord::InlineRep::N + const std::string small_string(kMaxInline, 'x'); + absl::Cord c1(small_string); + + absl::Cord c2; + c2.Append(small_string); + EXPECT_EQ(c1, c2); + EXPECT_EQ(c1.EstimatedMemoryUsage(), c2.EstimatedMemoryUsage()); +} + +} // namespace + +// Regtest for 7510292 (fix a bug introduced by 7465150) +TEST(Cord, Concat_Append) { + // Create a rep of type CONCAT + absl::Cord s1("foobarbarbarbarbar"); + s1.Append("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg"); + size_t size = s1.size(); + + // Create a copy of s1 and append to it. + absl::Cord s2 = s1; + s2.Append("x"); + + // 7465150 modifies s1 when it shouldn't. + EXPECT_EQ(s1.size(), size); + EXPECT_EQ(s2.size(), size + 1); +} + +TEST(MakeFragmentedCord, MakeFragmentedCordFromInitializerList) { + absl::Cord fragmented = + absl::MakeFragmentedCord({"A ", "fragmented ", "Cord"}); + + EXPECT_EQ("A fragmented Cord", fragmented); + + auto chunk_it = fragmented.chunk_begin(); + + ASSERT_TRUE(chunk_it != fragmented.chunk_end()); + EXPECT_EQ("A ", *chunk_it); + + ASSERT_TRUE(++chunk_it != fragmented.chunk_end()); + EXPECT_EQ("fragmented ", *chunk_it); + + ASSERT_TRUE(++chunk_it != fragmented.chunk_end()); + EXPECT_EQ("Cord", *chunk_it); + + ASSERT_TRUE(++chunk_it == fragmented.chunk_end()); +} + +TEST(MakeFragmentedCord, MakeFragmentedCordFromVector) { + std::vector chunks = {"A ", "fragmented ", "Cord"}; + absl::Cord fragmented = absl::MakeFragmentedCord(chunks); + + EXPECT_EQ("A fragmented Cord", fragmented); + + auto chunk_it = fragmented.chunk_begin(); + + ASSERT_TRUE(chunk_it != fragmented.chunk_end()); + EXPECT_EQ("A ", *chunk_it); + + ASSERT_TRUE(++chunk_it != fragmented.chunk_end()); + EXPECT_EQ("fragmented ", *chunk_it); + + ASSERT_TRUE(++chunk_it != fragmented.chunk_end()); + EXPECT_EQ("Cord", *chunk_it); + + ASSERT_TRUE(++chunk_it == fragmented.chunk_end()); +} + +TEST(CordChunkIterator, Traits) { + static_assert(std::is_copy_constructible::value, + ""); + static_assert(std::is_copy_assignable::value, ""); + + // Move semantics to satisfy swappable via std::swap + static_assert(std::is_move_constructible::value, + ""); + static_assert(std::is_move_assignable::value, ""); + + static_assert( + std::is_same< + std::iterator_traits::iterator_category, + std::input_iterator_tag>::value, + ""); + static_assert( + std::is_same::value_type, + absl::string_view>::value, + ""); + static_assert( + std::is_same< + std::iterator_traits::difference_type, + ptrdiff_t>::value, + ""); + static_assert( + std::is_same::pointer, + const absl::string_view*>::value, + ""); + static_assert( + std::is_same::reference, + absl::string_view>::value, + ""); +} + +static void VerifyChunkIterator(const absl::Cord& cord, + size_t expected_chunks) { + EXPECT_EQ(cord.chunk_begin() == cord.chunk_end(), cord.empty()) << cord; + EXPECT_EQ(cord.chunk_begin() != cord.chunk_end(), !cord.empty()); + + absl::Cord::ChunkRange range = cord.Chunks(); + EXPECT_EQ(range.begin() == range.end(), cord.empty()); + EXPECT_EQ(range.begin() != range.end(), !cord.empty()); + + std::string content(cord); + size_t pos = 0; + auto pre_iter = cord.chunk_begin(), post_iter = cord.chunk_begin(); + size_t n_chunks = 0; + while (pre_iter != cord.chunk_end() && post_iter != cord.chunk_end()) { + EXPECT_FALSE(pre_iter == cord.chunk_end()); // NOLINT: explicitly test == + EXPECT_FALSE(post_iter == cord.chunk_end()); // NOLINT + + EXPECT_EQ(pre_iter, post_iter); + EXPECT_EQ(*pre_iter, *post_iter); + + EXPECT_EQ(pre_iter->data(), (*pre_iter).data()); + EXPECT_EQ(pre_iter->size(), (*pre_iter).size()); + + absl::string_view chunk = *pre_iter; + EXPECT_FALSE(chunk.empty()); + EXPECT_LE(pos + chunk.size(), content.size()); + EXPECT_EQ(absl::string_view(content.c_str() + pos, chunk.size()), chunk); + + int n_equal_iterators = 0; + for (absl::Cord::ChunkIterator it = range.begin(); it != range.end(); + ++it) { + n_equal_iterators += static_cast(it == pre_iter); + } + EXPECT_EQ(n_equal_iterators, 1); + + ++pre_iter; + EXPECT_EQ(*post_iter++, chunk); + + pos += chunk.size(); + ++n_chunks; + } + EXPECT_EQ(expected_chunks, n_chunks); + EXPECT_EQ(pos, content.size()); + EXPECT_TRUE(pre_iter == cord.chunk_end()); // NOLINT: explicitly test == + EXPECT_TRUE(post_iter == cord.chunk_end()); // NOLINT +} + +TEST(CordChunkIterator, Operations) { + absl::Cord empty_cord; + VerifyChunkIterator(empty_cord, 0); + + absl::Cord small_buffer_cord("small cord"); + VerifyChunkIterator(small_buffer_cord, 1); + + absl::Cord flat_node_cord("larger than small buffer optimization"); + VerifyChunkIterator(flat_node_cord, 1); + + VerifyChunkIterator( + absl::MakeFragmentedCord({"a ", "small ", "fragmented ", "cord ", "for ", + "testing ", "chunk ", "iterations."}), + 8); + + absl::Cord reused_nodes_cord(std::string(40, 'c')); + reused_nodes_cord.Prepend(absl::Cord(std::string(40, 'b'))); + reused_nodes_cord.Prepend(absl::Cord(std::string(40, 'a'))); + size_t expected_chunks = 3; + for (int i = 0; i < 8; ++i) { + reused_nodes_cord.Prepend(reused_nodes_cord); + expected_chunks *= 2; + VerifyChunkIterator(reused_nodes_cord, expected_chunks); + } + + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + absl::Cord flat_cord(RandomLowercaseString(&rng, 256)); + absl::Cord subcords; + for (int i = 0; i < 128; ++i) subcords.Prepend(flat_cord.Subcord(i, 128)); + VerifyChunkIterator(subcords, 128); +} + +TEST(CordCharIterator, Traits) { + static_assert(std::is_copy_constructible::value, + ""); + static_assert(std::is_copy_assignable::value, ""); + + // Move semantics to satisfy swappable via std::swap + static_assert(std::is_move_constructible::value, + ""); + static_assert(std::is_move_assignable::value, ""); + + static_assert( + std::is_same< + std::iterator_traits::iterator_category, + std::input_iterator_tag>::value, + ""); + static_assert( + std::is_same::value_type, + char>::value, + ""); + static_assert( + std::is_same< + std::iterator_traits::difference_type, + ptrdiff_t>::value, + ""); + static_assert( + std::is_same::pointer, + const char*>::value, + ""); + static_assert( + std::is_same::reference, + const char&>::value, + ""); +} + +static void VerifyCharIterator(const absl::Cord& cord) { + EXPECT_EQ(cord.char_begin() == cord.char_end(), cord.empty()); + EXPECT_EQ(cord.char_begin() != cord.char_end(), !cord.empty()); + + absl::Cord::CharRange range = cord.Chars(); + EXPECT_EQ(range.begin() == range.end(), cord.empty()); + EXPECT_EQ(range.begin() != range.end(), !cord.empty()); + + size_t i = 0; + absl::Cord::CharIterator pre_iter = cord.char_begin(); + absl::Cord::CharIterator post_iter = cord.char_begin(); + std::string content(cord); + while (pre_iter != cord.char_end() && post_iter != cord.char_end()) { + EXPECT_FALSE(pre_iter == cord.char_end()); // NOLINT: explicitly test == + EXPECT_FALSE(post_iter == cord.char_end()); // NOLINT + + EXPECT_LT(i, cord.size()); + EXPECT_EQ(content[i], *pre_iter); + + EXPECT_EQ(pre_iter, post_iter); + EXPECT_EQ(*pre_iter, *post_iter); + EXPECT_EQ(&*pre_iter, &*post_iter); + + EXPECT_EQ(&*pre_iter, pre_iter.operator->()); + + const char* character_address = &*pre_iter; + absl::Cord::CharIterator copy = pre_iter; + ++copy; + EXPECT_EQ(character_address, &*pre_iter); + + int n_equal_iterators = 0; + for (absl::Cord::CharIterator it = range.begin(); it != range.end(); ++it) { + n_equal_iterators += static_cast(it == pre_iter); + } + EXPECT_EQ(n_equal_iterators, 1); + + absl::Cord::CharIterator advance_iter = range.begin(); + absl::Cord::Advance(&advance_iter, i); + EXPECT_EQ(pre_iter, advance_iter); + + advance_iter = range.begin(); + EXPECT_EQ(absl::Cord::AdvanceAndRead(&advance_iter, i), cord.Subcord(0, i)); + EXPECT_EQ(pre_iter, advance_iter); + + advance_iter = pre_iter; + absl::Cord::Advance(&advance_iter, cord.size() - i); + EXPECT_EQ(range.end(), advance_iter); + + advance_iter = pre_iter; + EXPECT_EQ(absl::Cord::AdvanceAndRead(&advance_iter, cord.size() - i), + cord.Subcord(i, cord.size() - i)); + EXPECT_EQ(range.end(), advance_iter); + + ++i; + ++pre_iter; + post_iter++; + } + EXPECT_EQ(i, cord.size()); + EXPECT_TRUE(pre_iter == cord.char_end()); // NOLINT: explicitly test == + EXPECT_TRUE(post_iter == cord.char_end()); // NOLINT + + absl::Cord::CharIterator zero_advanced_end = cord.char_end(); + absl::Cord::Advance(&zero_advanced_end, 0); + EXPECT_EQ(zero_advanced_end, cord.char_end()); + + absl::Cord::CharIterator it = cord.char_begin(); + for (absl::string_view chunk : cord.Chunks()) { + while (!chunk.empty()) { + EXPECT_EQ(absl::Cord::ChunkRemaining(it), chunk); + chunk.remove_prefix(1); + ++it; + } + } +} + +TEST(CordCharIterator, Operations) { + absl::Cord empty_cord; + VerifyCharIterator(empty_cord); + + absl::Cord small_buffer_cord("small cord"); + VerifyCharIterator(small_buffer_cord); + + absl::Cord flat_node_cord("larger than small buffer optimization"); + VerifyCharIterator(flat_node_cord); + + VerifyCharIterator( + absl::MakeFragmentedCord({"a ", "small ", "fragmented ", "cord ", "for ", + "testing ", "character ", "iteration."})); + + absl::Cord reused_nodes_cord("ghi"); + reused_nodes_cord.Prepend(absl::Cord("def")); + reused_nodes_cord.Prepend(absl::Cord("abc")); + for (int i = 0; i < 4; ++i) { + reused_nodes_cord.Prepend(reused_nodes_cord); + VerifyCharIterator(reused_nodes_cord); + } + + RandomEngine rng(testing::GTEST_FLAG(random_seed)); + absl::Cord flat_cord(RandomLowercaseString(&rng, 256)); + absl::Cord subcords; + for (int i = 0; i < 4; ++i) subcords.Prepend(flat_cord.Subcord(16 * i, 128)); + VerifyCharIterator(subcords); +} + +TEST(Cord, StreamingOutput) { + absl::Cord c = + absl::MakeFragmentedCord({"A ", "small ", "fragmented ", "Cord", "."}); + std::stringstream output; + output << c; + EXPECT_EQ("A small fragmented Cord.", output.str()); +} + +TEST(Cord, ForEachChunk) { + for (int num_elements : {1, 10, 200}) { + SCOPED_TRACE(num_elements); + std::vector cord_chunks; + for (int i = 0; i < num_elements; ++i) { + cord_chunks.push_back(absl::StrCat("[", i, "]")); + } + absl::Cord c = absl::MakeFragmentedCord(cord_chunks); + + std::vector iterated_chunks; + absl::CordTestPeer::ForEachChunk(c, + [&iterated_chunks](absl::string_view sv) { + iterated_chunks.emplace_back(sv); + }); + EXPECT_EQ(iterated_chunks, cord_chunks); + } +} + +TEST(Cord, SmallBufferAssignFromOwnData) { + constexpr size_t kMaxInline = 15; + std::string contents = "small buff cord"; + EXPECT_EQ(contents.size(), kMaxInline); + for (size_t pos = 0; pos < contents.size(); ++pos) { + for (size_t count = contents.size() - pos; count > 0; --count) { + absl::Cord c(contents); + absl::string_view flat = c.Flatten(); + c = flat.substr(pos, count); + EXPECT_EQ(c, contents.substr(pos, count)) + << "pos = " << pos << "; count = " << count; + } + } +} diff --git a/absl/strings/cord_test_helpers.h b/absl/strings/cord_test_helpers.h new file mode 100644 index 00000000..f1036e3b --- /dev/null +++ b/absl/strings/cord_test_helpers.h @@ -0,0 +1,60 @@ +// +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef ABSL_STRINGS_CORD_TEST_HELPERS_H_ +#define ABSL_STRINGS_CORD_TEST_HELPERS_H_ + +#include "absl/strings/cord.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN + +// Creates a multi-segment Cord from an iterable container of strings. The +// resulting Cord is guaranteed to have one segment for every string in the +// container. This allows code to be unit tested with multi-segment Cord +// inputs. +// +// Example: +// +// absl::Cord c = absl::MakeFragmentedCord({"A ", "fragmented ", "Cord"}); +// EXPECT_FALSE(c.GetFlat(&unused)); +// +// The mechanism by which this Cord is created is an implementation detail. Any +// implementation that produces a multi-segment Cord may produce a flat Cord in +// the future as new optimizations are added to the Cord class. +// MakeFragmentedCord will, however, always be updated to return a multi-segment +// Cord. +template +Cord MakeFragmentedCord(const Container& c) { + Cord result; + for (const auto& s : c) { + auto* external = new std::string(s); + Cord tmp = absl::MakeCordFromExternal( + *external, [external](absl::string_view) { delete external; }); + tmp.Prepend(result); + result = tmp; + } + return result; +} + +inline Cord MakeFragmentedCord(std::initializer_list list) { + return MakeFragmentedCord>(list); +} + +ABSL_NAMESPACE_END +} // namespace absl + +#endif // ABSL_STRINGS_CORD_TEST_HELPERS_H_ diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h new file mode 100644 index 00000000..5b5d1083 --- /dev/null +++ b/absl/strings/internal/cord_internal.h @@ -0,0 +1,151 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ABSL_STRINGS_INTERNAL_CORD_INTERNAL_H_ +#define ABSL_STRINGS_INTERNAL_CORD_INTERNAL_H_ + +#include +#include +#include +#include +#include + +#include "absl/meta/type_traits.h" +#include "absl/strings/string_view.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace cord_internal { + +// Wraps std::atomic for reference counting. +class Refcount { + public: + Refcount() : count_{1} {} + ~Refcount() {} + + // Increments the reference count by 1. Imposes no memory ordering. + inline void Increment() { count_.fetch_add(1, std::memory_order_relaxed); } + + // Asserts that the current refcount is greater than 0. If the refcount is + // greater than 1, decrements the reference count by 1. + // + // Returns false if there are no references outstanding; true otherwise. + // Inserts barriers to ensure that state written before this method returns + // false will be visible to a thread that just observed this method returning + // false. + inline bool Decrement() { + int32_t refcount = count_.load(std::memory_order_acquire); + assert(refcount > 0); + return refcount != 1 && count_.fetch_sub(1, std::memory_order_acq_rel) != 1; + } + + // Same as Decrement but expect that refcount is greater than 1. + inline bool DecrementExpectHighRefcount() { + int32_t refcount = count_.fetch_sub(1, std::memory_order_acq_rel); + assert(refcount > 0); + return refcount != 1; + } + + // Returns the current reference count using acquire semantics. + inline int32_t Get() const { return count_.load(std::memory_order_acquire); } + + // Returns whether the atomic integer is 1. + // If the reference count is used in the conventional way, a + // reference count of 1 implies that the current thread owns the + // reference and no other thread shares it. + // This call performs the test for a reference count of one, and + // performs the memory barrier needed for the owning thread + // to act on the object, knowing that it has exclusive access to the + // object. + inline bool IsOne() { return count_.load(std::memory_order_acquire) == 1; } + + private: + std::atomic count_; +}; + +// The overhead of a vtable is too much for Cord, so we roll our own subclasses +// using only a single byte to differentiate classes from each other - the "tag" +// byte. Define the subclasses first so we can provide downcasting helper +// functions in the base class. + +struct CordRepConcat; +struct CordRepSubstring; +struct CordRepExternal; + +struct CordRep { + // The following three fields have to be less than 32 bytes since + // that is the smallest supported flat node size. + // We use uint64_t for the length even in 32-bit binaries. + uint64_t length; + Refcount refcount; + // If tag < FLAT, it represents CordRepKind and indicates the type of node. + // Otherwise, the node type is CordRepFlat and the tag is the encoded size. + uint8_t tag; + char data[1]; // Starting point for flat array: MUST BE LAST FIELD of CordRep + + inline CordRepConcat* concat(); + inline const CordRepConcat* concat() const; + inline CordRepSubstring* substring(); + inline const CordRepSubstring* substring() const; + inline CordRepExternal* external(); + inline const CordRepExternal* external() const; +}; + +struct CordRepConcat : public CordRep { + CordRep* left; + CordRep* right; + + uint8_t depth() const { return static_cast(data[0]); } + void set_depth(uint8_t depth) { data[0] = static_cast(depth); } +}; + +struct CordRepSubstring : public CordRep { + size_t start; // Starting offset of substring in child + CordRep* child; +}; + +// TODO(strel): replace the following logic (and related functions in cord.cc) +// with container_internal::Layout. + +// Alignment requirement for CordRepExternal so that the type erased releaser +// will be stored at a suitably aligned address. +constexpr size_t ExternalRepAlignment() { +#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__) + return __STDCPP_DEFAULT_NEW_ALIGNMENT__; +#else + return alignof(max_align_t); +#endif +} + +// Type for function pointer that will invoke and destroy the type-erased +// releaser function object. Accepts a pointer to the releaser and the +// `string_view` that were passed in to `NewExternalRep` below. The return value +// is the size of the `Releaser` type. +using ExternalReleaserInvoker = size_t (*)(void*, absl::string_view); + +// External CordReps are allocated together with a type erased releaser. The +// releaser is stored in the memory directly following the CordRepExternal. +struct alignas(ExternalRepAlignment()) CordRepExternal : public CordRep { + const char* base; + // Pointer to function that knows how to call and destroy the releaser. + ExternalReleaserInvoker releaser_invoker; +}; + +// TODO(strel): look into removing, it doesn't seem like anything relies on this +static_assert(sizeof(CordRepConcat) == sizeof(CordRepSubstring), ""); + +} // namespace cord_internal +ABSL_NAMESPACE_END +} // namespace absl +#endif // ABSL_STRINGS_INTERNAL_CORD_INTERNAL_H_ diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index d3904124..4d0604e0 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -88,7 +88,7 @@ class ConvertedIntInfo { template void UnsignedToStringRight(T u, ConversionChar conv) { char *p = end(); - switch (conv.radix()) { + switch (FormatConversionCharRadix(conv)) { default: case 10: for (; u; u /= 10) @@ -99,7 +99,7 @@ class ConvertedIntInfo { *--p = static_cast('0' + static_cast(u % 8)); break; case 16: { - const char *digits = kDigit[conv.upper() ? 1 : 0]; + const char *digits = kDigit[FormatConversionCharIsUpper(conv) ? 1 : 0]; for (; u; u /= 16) *--p = digits[static_cast(u % 16)]; break; } @@ -121,21 +121,20 @@ class ConvertedIntInfo { string_view BaseIndicator(const ConvertedIntInfo &info, const ConversionSpec conv) { bool alt = conv.flags().alt; - int radix = conv.conv().radix(); - if (conv.conv().id() == ConversionChar::p) - alt = true; // always show 0x for %p. + int radix = FormatConversionCharRadix(conv.conv()); + if (conv.conv() == ConversionChar::p) alt = true; // always show 0x for %p. // From the POSIX description of '#' flag: // "For x or X conversion specifiers, a non-zero result shall have // 0x (or 0X) prefixed to it." if (alt && radix == 16 && !info.digits().empty()) { - if (conv.conv().upper()) return "0X"; + if (FormatConversionCharIsUpper(conv.conv())) return "0X"; return "0x"; } return {}; } string_view SignColumn(bool neg, const ConversionSpec conv) { - if (conv.conv().is_signed()) { + if (FormatConversionCharIsSigned(conv.conv())) { if (neg) return "-"; if (conv.flags().show_pos) return "+"; if (conv.flags().sign_col) return " "; @@ -175,7 +174,7 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, if (!precision_specified) precision = 1; - if (conv.flags().alt && conv.conv().id() == ConversionChar::o) { + if (conv.flags().alt && conv.conv() == ConversionChar::o) { // From POSIX description of the '#' (alt) flag: // "For o conversion, it increases the precision (if necessary) to // force the first digit of the result to be zero." @@ -211,7 +210,7 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, template bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) { ConvertedIntInfo info(v, conv.conv()); - if (conv.flags().basic && conv.conv().id() != ConversionChar::p) { + if (conv.flags().basic && (conv.conv() != ConversionChar::p)) { if (info.is_neg()) sink->Append(1, '-'); if (info.digits().empty()) { sink->Append(1, '0'); @@ -225,14 +224,13 @@ bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) { template bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv().is_float()) { + if (FormatConversionCharIsFloat(conv.conv())) { return FormatConvertImpl(static_cast(v), conv, sink).value; } - if (conv.conv().id() == ConversionChar::c) + if (conv.conv() == ConversionChar::c) return ConvertCharImpl(static_cast(v), conv, sink); - if (!conv.conv().is_integral()) - return false; - if (!conv.conv().is_signed() && IsSigned::value) { + if (!FormatConversionCharIsIntegral(conv.conv())) return false; + if (!FormatConversionCharIsSigned(conv.conv()) && IsSigned::value) { using U = typename MakeUnsigned::type; return FormatConvertImpl(static_cast(v), conv, sink).value; } @@ -241,13 +239,13 @@ bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { template bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { - return conv.conv().is_float() && ConvertFloatImpl(v, conv, sink); + return FormatConversionCharIsFloat(conv.conv()) && + ConvertFloatImpl(v, conv, sink); } inline bool ConvertStringArg(string_view v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv().id() != ConversionChar::s) - return false; + if (conv.conv() != ConversionChar::s) return false; if (conv.flags().basic) { sink->Append(v); return true; @@ -274,7 +272,7 @@ ConvertResult FormatConvertImpl(string_view v, ConvertResult FormatConvertImpl(const char *v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv().id() == ConversionChar::p) + if (conv.conv() == ConversionChar::p) return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; size_t len; if (v == nullptr) { @@ -291,8 +289,7 @@ ConvertResult FormatConvertImpl(const char *v, // ==================== Raw pointers ==================== ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv().id() != ConversionChar::p) - return {false}; + if (conv.conv() != ConversionChar::p) return {false}; if (!v.value) { sink->Append("(nil)"); return {true}; diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index b672a229..7a937563 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -70,7 +70,7 @@ template FormatConvertImpl(const AbslCord& value, ConversionSpec conv, FormatSinkImpl* sink) { - if (conv.conv().id() != ConversionChar::s) return {false}; + if (conv.conv() != ConversionChar::s) return {false}; bool is_left = conv.flags().left; size_t space_remaining = 0; @@ -185,8 +185,7 @@ struct FormatCountCaptureHelper { FormatSinkImpl* sink) { const absl::enable_if_t& v2 = v; - if (conv.conv().id() != str_format_internal::ConversionChar::n) - return {false}; + if (conv.conv() != str_format_internal::ConversionChar::n) return {false}; *v2.p_ = static_cast(sink->size()); return {true}; } @@ -378,7 +377,7 @@ class FormatArgImpl { template static bool Dispatch(Data arg, ConversionSpec spec, void* out) { // A `none` conv indicates that we want the `int` conversion. - if (ABSL_PREDICT_FALSE(spec.conv().id() == ConversionChar::none)) { + if (ABSL_PREDICT_FALSE(spec.conv() == ConversionChar::none)) { return ToInt(arg, static_cast(out), std::is_integral(), std::is_enum()); } diff --git a/absl/strings/internal/str_format/arg_test.cc b/absl/strings/internal/str_format/arg_test.cc index 96c9cfd3..04fa56cd 100644 --- a/absl/strings/internal/str_format/arg_test.cc +++ b/absl/strings/internal/str_format/arg_test.cc @@ -96,7 +96,7 @@ TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) { std::string s; FormatSinkImpl sink(&s); ConversionSpec conv; - conv.set_conv(ConversionChar::FromChar('s')); + conv.set_conv(ConversionChar::s); conv.set_flags(Flags()); conv.set_width(-1); conv.set_precision(-1); diff --git a/absl/strings/internal/str_format/extension.cc b/absl/strings/internal/str_format/extension.cc index 21688e87..2e5bc2ce 100644 --- a/absl/strings/internal/str_format/extension.cc +++ b/absl/strings/internal/str_format/extension.cc @@ -23,15 +23,6 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { -const ConversionChar::Spec ConversionChar::kSpecs[] = { -#define X_VAL(id) { ConversionChar::id, #id[0] } -#define X_SEP , - ABSL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP), - {ConversionChar::none, '\0'}, -#undef X_VAL -#undef X_SEP -}; - std::string Flags::ToString() const { std::string s; s.append(left ? "-" : ""); @@ -42,8 +33,6 @@ std::string Flags::ToString() const { return s; } -const size_t ConversionChar::kNumValues; - bool FormatSinkImpl::PutPaddedString(string_view v, int w, int p, bool l) { size_t space_remaining = 0; if (w >= 0) space_remaining = w; diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index 4868eac3..1a863c20 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -148,117 +148,122 @@ struct Flags { X_VAL(g) X_SEP X_VAL(G) X_SEP X_VAL(a) X_SEP X_VAL(A) X_SEP \ /* misc */ \ X_VAL(n) X_SEP X_VAL(p) -// clang-format on -struct ABSL_DLL ConversionChar { - public: - enum Id : uint8_t { +enum class FormatConversionChar : uint8_t { c, C, s, S, // text d, i, o, u, x, X, // int f, F, e, E, g, G, a, A, // float n, p, // misc - none - }; - static const size_t kNumValues = none + 1; - - ConversionChar() : id_(none) {} - - public: - // Index into the opaque array of ConversionChar enums. - // Requires: i < kNumValues - static ConversionChar FromIndex(size_t i) { - return ConversionChar(kSpecs[i].value); - } + kNone, + none = kNone +}; +// clang-format on - static ConversionChar FromChar(char c) { - ConversionChar::Id out_id = ConversionChar::none; - switch (c) { -#define X_VAL(id) \ - case #id[0]: \ - out_id = ConversionChar::id; \ - break; - ABSL_CONVERSION_CHARS_EXPAND_(X_VAL, ) +inline FormatConversionChar FormatConversionCharFromChar(char c) { + switch (c) { +#define X_VAL(id) \ + case #id[0]: \ + return FormatConversionChar::id; + ABSL_CONVERSION_CHARS_EXPAND_(X_VAL, ) #undef X_VAL - default: - break; - } - return ConversionChar(out_id); } + return FormatConversionChar::kNone; +} - static ConversionChar FromId(Id id) { return ConversionChar(id); } - Id id() const { return id_; } - - int radix() const { - switch (id()) { - case x: case X: case a: case A: case p: return 16; - case o: return 8; - default: return 10; - } +inline int FormatConversionCharRadix(FormatConversionChar c) { + switch (c) { + case FormatConversionChar::x: + case FormatConversionChar::X: + case FormatConversionChar::a: + case FormatConversionChar::A: + case FormatConversionChar::p: + return 16; + case FormatConversionChar::o: + return 8; + default: + return 10; } +} - bool upper() const { - switch (id()) { - case X: case F: case E: case G: case A: return true; - default: return false; - } +inline bool FormatConversionCharIsUpper(FormatConversionChar c) { + switch (c) { + case FormatConversionChar::X: + case FormatConversionChar::F: + case FormatConversionChar::E: + case FormatConversionChar::G: + case FormatConversionChar::A: + return true; + default: + return false; } +} - bool is_signed() const { - switch (id()) { - case d: case i: return true; - default: return false; - } +inline bool FormatConversionCharIsSigned(FormatConversionChar c) { + switch (c) { + case FormatConversionChar::d: + case FormatConversionChar::i: + return true; + default: + return false; } +} - bool is_integral() const { - switch (id()) { - case d: case i: case u: case o: case x: case X: - return true; - default: return false; - } +inline bool FormatConversionCharIsIntegral(FormatConversionChar c) { + switch (c) { + case FormatConversionChar::d: + case FormatConversionChar::i: + case FormatConversionChar::u: + case FormatConversionChar::o: + case FormatConversionChar::x: + case FormatConversionChar::X: + return true; + default: + return false; } +} - bool is_float() const { - switch (id()) { - case a: case e: case f: case g: case A: case E: case F: case G: - return true; - default: return false; - } +inline bool FormatConversionCharIsFloat(FormatConversionChar c) { + switch (c) { + case FormatConversionChar::a: + case FormatConversionChar::e: + case FormatConversionChar::f: + case FormatConversionChar::g: + case FormatConversionChar::A: + case FormatConversionChar::E: + case FormatConversionChar::F: + case FormatConversionChar::G: + return true; + default: + return false; } +} - bool IsValid() const { return id() != none; } - - // The associated char. - char Char() const { return kSpecs[id_].name; } - - friend bool operator==(const ConversionChar& a, const ConversionChar& b) { - return a.id() == b.id(); - } - friend bool operator!=(const ConversionChar& a, const ConversionChar& b) { - return !(a == b); - } - friend std::ostream& operator<<(std::ostream& os, const ConversionChar& v) { - char c = v.Char(); - if (!c) c = '?'; - return os << c; +inline char FormatConversionCharToChar(FormatConversionChar c) { + switch (c) { +#define X_VAL(e) \ + case FormatConversionChar::e: \ + return #e[0]; +#define X_SEP + ABSL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP) + case FormatConversionChar::kNone: + return '\0'; +#undef X_VAL +#undef X_SEP } + return '\0'; +} - private: - struct Spec { - Id value; - char name; - }; - static const Spec kSpecs[]; - - explicit ConversionChar(Id id) : id_(id) {} - - Id id_; -}; +// The associated char. +inline std::ostream& operator<<(std::ostream& os, FormatConversionChar v) { + char c = FormatConversionCharToChar(v); + if (!c) c = '?'; + return os << c; +} class ConversionSpec { public: Flags flags() const { return flags_; } - ConversionChar conv() const { + FormatConversionChar conv() const { // Keep this field first in the struct . It generates better code when // accessing it when ConversionSpec is passed by value in registers. static_assert(offsetof(ConversionSpec, conv_) == 0, ""); @@ -273,22 +278,24 @@ class ConversionSpec { int precision() const { return precision_; } void set_flags(Flags f) { flags_ = f; } - void set_conv(ConversionChar c) { conv_ = c; } + void set_conv(FormatConversionChar c) { conv_ = c; } void set_width(int w) { width_ = w; } void set_precision(int p) { precision_ = p; } void set_left(bool b) { flags_.left = b; } private: - ConversionChar conv_; + FormatConversionChar conv_ = FormatConversionChar::kNone; Flags flags_; int width_; int precision_; }; -constexpr uint64_t ConversionCharToConvValue(char conv) { +constexpr uint64_t FormatConversionCharToConvValue(char conv) { return -#define CONV_SET_CASE(c) \ - conv == #c[0] ? (uint64_t{1} << (1 + ConversionChar::Id::c)): +#define CONV_SET_CASE(c) \ + conv == #c[0] \ + ? (uint64_t{1} << (1 + static_cast(FormatConversionChar::c))) \ + : ABSL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, ) #undef CONV_SET_CASE conv == '*' @@ -297,12 +304,12 @@ constexpr uint64_t ConversionCharToConvValue(char conv) { } enum class Conv : uint64_t { -#define CONV_SET_CASE(c) c = ConversionCharToConvValue(#c[0]), +#define CONV_SET_CASE(c) c = FormatConversionCharToConvValue(#c[0]), ABSL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, ) #undef CONV_SET_CASE // Used for width/precision '*' specification. - star = ConversionCharToConvValue('*'), + star = FormatConversionCharToConvValue('*'), // Some predefined values: integral = d | i | u | o | x | X, @@ -323,12 +330,12 @@ constexpr Conv operator|(Conv a, Conv b) { // Get a conversion with a single character in it. constexpr Conv ConversionCharToConv(char c) { - return Conv(ConversionCharToConvValue(c)); + return Conv(FormatConversionCharToConvValue(c)); } // Checks whether `c` exists in `set`. constexpr bool Contains(Conv set, char c) { - return (static_cast(set) & ConversionCharToConvValue(c)) != 0; + return (static_cast(set) & FormatConversionCharToConvValue(c)) != 0; } // Checks whether all the characters in `c` are contained in `set` @@ -353,6 +360,9 @@ inline size_t Excess(size_t used, size_t capacity) { return used < capacity ? capacity - used : 0; } +// Type alias for use during migration. +using ConversionChar = FormatConversionChar; + } // namespace str_format_internal ABSL_NAMESPACE_END diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index ebe4da5b..c98ed4ba 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -33,7 +33,7 @@ bool FallbackToSnprintf(const Float v, const ConversionSpec &conv, if (std::is_same()) { *fp++ = 'L'; } - *fp++ = conv.conv().Char(); + *fp++ = FormatConversionCharToChar(conv.conv()); *fp = 0; assert(fp < fmt + sizeof(fmt)); } @@ -100,9 +100,11 @@ bool ConvertNonNumericFloats(char sign_char, Float v, char text[4], *ptr = text; if (sign_char) *ptr++ = sign_char; if (std::isnan(v)) { - ptr = std::copy_n(conv.conv().upper() ? "NAN" : "nan", 3, ptr); + ptr = std::copy_n(FormatConversionCharIsUpper(conv.conv()) ? "NAN" : "nan", + 3, ptr); } else if (std::isinf(v)) { - ptr = std::copy_n(conv.conv().upper() ? "INF" : "inf", 3, ptr); + ptr = std::copy_n(FormatConversionCharIsUpper(conv.conv()) ? "INF" : "inf", + 3, ptr); } else { return false; } @@ -399,7 +401,7 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, Buffer buffer; - switch (conv.conv().id()) { + switch (conv.conv()) { case ConversionChar::f: case ConversionChar::F: if (!FloatToBuffer(decomposed, precision, &buffer, @@ -416,7 +418,8 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, return FallbackToSnprintf(v, conv, sink); } if (!conv.flags().alt && buffer.back() == '.') buffer.pop_back(); - PrintExponent(exp, conv.conv().upper() ? 'E' : 'e', &buffer); + PrintExponent(exp, FormatConversionCharIsUpper(conv.conv()) ? 'E' : 'e', + &buffer); break; case ConversionChar::g: @@ -447,7 +450,10 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, while (buffer.back() == '0') buffer.pop_back(); if (buffer.back() == '.') buffer.pop_back(); } - if (exp) PrintExponent(exp, conv.conv().upper() ? 'E' : 'e', &buffer); + if (exp) { + PrintExponent(exp, FormatConversionCharIsUpper(conv.conv()) ? 'E' : 'e', + &buffer); + } break; case ConversionChar::a: diff --git a/absl/strings/internal/str_format/parser.cc b/absl/strings/internal/str_format/parser.cc index c4b527bc..aab68db9 100644 --- a/absl/strings/internal/str_format/parser.cc +++ b/absl/strings/internal/str_format/parser.cc @@ -17,7 +17,7 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { -using CC = ConversionChar::Id; +using CC = ConversionChar; using LM = LengthMod; ABSL_CONST_INIT const ConvTag kTags[256] = { @@ -322,7 +322,9 @@ bool ParsedFormatBase::MatchesConversions( if (conv.width.is_from_arg() && !add_if_valid_conv(conv.width.get_from_arg(), '*')) return false; - if (!add_if_valid_conv(conv.arg_position, conv.conv.Char())) return false; + if (!add_if_valid_conv(conv.arg_position, + FormatConversionCharToChar(conv.conv))) + return false; } return used.size() == convs.size() || allow_ignored; } diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index 6cbe2576..45c90d1d 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -67,7 +67,7 @@ struct UnboundConversion { Flags flags; LengthMod length_mod = LengthMod::none; - ConversionChar conv; + ConversionChar conv = FormatConversionChar::kNone; }; // Consume conversion spec prefix (not including '%') of [p, end) if valid. @@ -79,10 +79,12 @@ const char* ConsumeUnboundConversion(const char* p, const char* end, UnboundConversion* conv, int* next_arg); // Helper tag class for the table below. -// It allows fast `char -> ConversionChar/LengthMod` checking and conversions. +// It allows fast `char -> ConversionChar/LengthMod` checking and +// conversions. class ConvTag { public: - constexpr ConvTag(ConversionChar::Id id) : tag_(id) {} // NOLINT + constexpr ConvTag(ConversionChar conversion_char) // NOLINT + : tag_(static_cast(conversion_char)) {} // We invert the length modifiers to make them negative so that we can easily // test for them. constexpr ConvTag(LengthMod length_mod) // NOLINT @@ -94,7 +96,7 @@ class ConvTag { bool is_length() const { return tag_ < 0 && tag_ != -128; } ConversionChar as_conv() const { assert(is_conv()); - return ConversionChar::FromId(static_cast(tag_)); + return static_cast(tag_); } LengthMod as_length() const { assert(is_length()); diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 4a8efd08..1b1ee030 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -41,7 +41,7 @@ TEST(LengthModTest, Names) { TEST(ConversionCharTest, Names) { struct Expectation { - ConversionChar::Id id; + ConversionChar id; char name; }; // clang-format off @@ -55,12 +55,10 @@ TEST(ConversionCharTest, Names) { {ConversionChar::none, '\0'}, }; // clang-format on - EXPECT_EQ(ABSL_ARRAYSIZE(kExpect), ConversionChar::kNumValues); for (auto e : kExpect) { SCOPED_TRACE(e.name); - ConversionChar v = ConversionChar::FromId(e.id); - EXPECT_EQ(e.id, v.id()); - EXPECT_EQ(e.name, v.Char()); + ConversionChar v = e.id; + EXPECT_EQ(e.name, FormatConversionCharToChar(v)); } } @@ -119,7 +117,7 @@ TEST_F(ConsumeUnboundConversionTest, BasicConversion) { EXPECT_FALSE(Run("dd")); // no excess allowed EXPECT_TRUE(Run("d")); - EXPECT_EQ('d', o.conv.Char()); + EXPECT_EQ('d', FormatConversionCharToChar(o.conv)); EXPECT_FALSE(o.width.is_from_arg()); EXPECT_LT(o.width.value(), 0); EXPECT_FALSE(o.precision.is_from_arg()); @@ -160,7 +158,7 @@ TEST_F(ConsumeUnboundConversionTest, ArgPosition) { TEST_F(ConsumeUnboundConversionTest, WidthAndPrecision) { EXPECT_TRUE(Run("14d")); - EXPECT_EQ('d', o.conv.Char()); + EXPECT_EQ('d', FormatConversionCharToChar(o.conv)); EXPECT_FALSE(o.width.is_from_arg()); EXPECT_EQ(14, o.width.value()); EXPECT_FALSE(o.precision.is_from_arg()); @@ -330,7 +328,7 @@ struct SummarizeConsumer { if (conv.precision.is_from_arg()) { *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*"; } - *out += conv.conv.Char(); + *out += FormatConversionCharToChar(conv.conv); *out += "}"; return true; } diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index d33bcaa2..acbdbf4a 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -450,7 +450,7 @@ struct SummarizeConsumer { if (conv.precision.is_from_arg()) { *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*"; } - *out += conv.conv.Char(); + *out += FormatConversionCharToChar(conv.conv); *out += "}"; return true; } diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc index 49ebd118..4d85a2c4 100644 --- a/absl/time/duration_test.cc +++ b/absl/time/duration_test.cc @@ -1045,7 +1045,7 @@ TEST(Duration, Multiplication) { EXPECT_EQ(absl::Seconds(666666666) + absl::Nanoseconds(666666667) + absl::Nanoseconds(1) / 2, sigfigs / 3); - sigfigs = absl::Seconds(7000000000LL); + sigfigs = absl::Seconds(int64_t{7000000000}); EXPECT_EQ(absl::Seconds(2333333333) + absl::Nanoseconds(333333333) + absl::Nanoseconds(1) / 4, sigfigs / 3); -- cgit v1.2.3 From a877af1f294be0866eab2676effd46687acb3b11 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 10 Mar 2020 09:28:06 -0700 Subject: Export of internal Abseil changes -- ea0cfebeb69b25bec343652bbe1a203f5476c51a by Mark Barolak : Change "std::string" to "string" in places where a "std::" qualification was incorrectly inserted by automation. PiperOrigin-RevId: 300108520 GitOrigin-RevId: ea0cfebeb69b25bec343652bbe1a203f5476c51a Change-Id: Ie3621e63a6ebad67b9fe56a3ebe33e1d50dac602 --- CMakeLists.txt | 2 +- absl/container/btree_test.cc | 4 +- absl/container/inlined_vector_benchmark.cc | 2 +- absl/container/inlined_vector_test.cc | 6 +-- absl/container/internal/btree.h | 2 +- absl/container/internal/compressed_tuple_test.cc | 4 +- absl/container/internal/raw_hash_set_test.cc | 6 +-- absl/debugging/failure_signal_handler.h | 2 +- absl/debugging/internal/demangle.cc | 6 +-- absl/debugging/leak_check_fail_test.cc | 4 +- absl/debugging/leak_check_test.cc | 6 +-- absl/debugging/symbolize_elf.inc | 2 +- absl/flags/internal/commandlineflag.h | 6 +-- absl/flags/internal/flag.cc | 2 +- absl/flags/internal/flag.h | 2 +- absl/flags/internal/usage.cc | 8 ++-- absl/flags/marshalling.cc | 2 +- absl/flags/parse.cc | 6 +-- absl/flags/usage_config.h | 2 +- absl/hash/hash_test.cc | 4 +- absl/random/bernoulli_distribution_test.cc | 4 +- absl/random/internal/nanobenchmark.cc | 2 +- absl/random/uniform_int_distribution_test.cc | 2 +- absl/status/status.cc | 2 +- absl/status/status.h | 4 +- absl/strings/charconv.cc | 4 +- absl/strings/charconv_benchmark.cc | 2 +- absl/strings/cord.h | 4 +- absl/strings/cord_test.cc | 8 ++-- absl/strings/escaping.cc | 18 ++++----- absl/strings/escaping_test.cc | 6 +-- absl/strings/internal/char_map.h | 2 +- absl/strings/internal/charconv_bigint.cc | 2 +- absl/strings/internal/charconv_bigint.h | 4 +- absl/strings/internal/charconv_parse.cc | 4 +- absl/strings/internal/charconv_parse_test.cc | 2 +- absl/strings/internal/numbers_test_common.h | 2 +- absl/strings/internal/str_format/bind.h | 4 +- absl/strings/internal/str_format/parser.h | 4 +- absl/strings/numbers_test.cc | 4 +- absl/strings/str_cat.h | 2 +- absl/strings/str_cat_benchmark.cc | 2 +- absl/strings/str_cat_test.cc | 6 +-- absl/strings/str_format_test.cc | 4 +- absl/strings/str_join_test.cc | 8 ++-- absl/strings/str_replace_benchmark.cc | 2 +- absl/strings/str_replace_test.cc | 12 +++--- absl/strings/str_split.cc | 4 +- absl/strings/str_split_test.cc | 32 +++++++-------- absl/strings/string_view.h | 14 +++---- absl/strings/string_view_test.cc | 28 ++++++------- absl/strings/substitute.cc | 10 ++--- absl/strings/substitute.h | 46 +++++++++++----------- absl/strings/substitute_test.cc | 8 ++-- absl/synchronization/mutex.cc | 2 +- absl/time/civil_time.cc | 6 +-- absl/time/duration.cc | 4 +- absl/time/format_test.cc | 6 +-- absl/time/internal/cctz/include/cctz/time_zone.h | 2 +- .../internal/cctz/include/cctz/zone_info_source.h | 2 +- absl/time/internal/cctz/src/time_zone_format.cc | 4 +- .../internal/cctz/src/time_zone_format_test.cc | 2 +- absl/time/internal/cctz/src/time_zone_impl.h | 2 +- absl/time/internal/cctz/src/time_zone_info.cc | 2 +- .../internal/cctz/src/time_zone_lookup_test.cc | 2 +- absl/time/internal/cctz/src/tzfile.h | 4 +- absl/time/time_zone_test.cc | 2 +- absl/types/variant_test.cc | 2 +- 68 files changed, 191 insertions(+), 191 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/CMakeLists.txt b/CMakeLists.txt index 74b5cd9d..e94dcd3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ cmake_policy(SET CMP0025 NEW) # if command can use IN_LIST cmake_policy(SET CMP0057 NEW) -# Project version variables are the empty std::string if version is unspecified +# Project version variables are the empty string if version is unspecified cmake_policy(SET CMP0048 NEW) project(absl CXX) diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index da8e7082..7ccdf6a1 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -2132,11 +2132,11 @@ TEST(Btree, UserProvidedKeyCompareToComparators) { TEST(Btree, TryEmplaceBasicTest) { absl::btree_map m; - // Should construct a std::string from the literal. + // Should construct a string from the literal. m.try_emplace(1, "one"); EXPECT_EQ(1, m.size()); - // Try other std::string constructors and const lvalue key. + // Try other string constructors and const lvalue key. const int key(42); m.try_emplace(key, 3, 'a'); m.try_emplace(2, std::string("two")); diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc index 3f2b4ed2..b8dafe93 100644 --- a/absl/container/inlined_vector_benchmark.cc +++ b/absl/container/inlined_vector_benchmark.cc @@ -83,7 +83,7 @@ int GetNonShortStringOptimizationSize() { } ABSL_RAW_LOG( FATAL, - "Failed to find a std::string larger than the short std::string optimization"); + "Failed to find a string larger than the short string optimization"); return -1; } diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index 2c9b0d0e..5965eac7 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -780,7 +780,7 @@ TEST(IntVec, Reserve) { TEST(StringVec, SelfRefPushBack) { std::vector std_v; absl::InlinedVector v; - const std::string s = "A quite long std::string to ensure heap."; + const std::string s = "A quite long string to ensure heap."; std_v.push_back(s); v.push_back(s); for (int i = 0; i < 20; ++i) { @@ -795,7 +795,7 @@ TEST(StringVec, SelfRefPushBack) { TEST(StringVec, SelfRefPushBackWithMove) { std::vector std_v; absl::InlinedVector v; - const std::string s = "A quite long std::string to ensure heap."; + const std::string s = "A quite long string to ensure heap."; std_v.push_back(s); v.push_back(s); for (int i = 0; i < 20; ++i) { @@ -808,7 +808,7 @@ TEST(StringVec, SelfRefPushBackWithMove) { } TEST(StringVec, SelfMove) { - const std::string s = "A quite long std::string to ensure heap."; + const std::string s = "A quite long string to ensure heap."; for (int len = 0; len < 20; len++) { SCOPED_TRACE(len); absl::InlinedVector v; diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 301c3656..d986f81e 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -186,7 +186,7 @@ struct key_compare_to_adapter> { template struct common_params { - // If Compare is a common comparator for a std::string-like type, then we adapt it + // If Compare is a common comparator for a string-like type, then we adapt it // to use heterogeneous lookup and to be a key-compare-to comparator. using key_compare = typename key_compare_to_adapter::type; // A type which indicates if we have a key-compare-to functor or a plain old diff --git a/absl/container/internal/compressed_tuple_test.cc b/absl/container/internal/compressed_tuple_test.cc index 1dae12db..62a7483e 100644 --- a/absl/container/internal/compressed_tuple_test.cc +++ b/absl/container/internal/compressed_tuple_test.cc @@ -277,11 +277,11 @@ TEST(CompressedTupleTest, Nested) { TEST(CompressedTupleTest, Reference) { int i = 7; - std::string s = "Very long std::string that goes in the heap"; + std::string s = "Very long string that goes in the heap"; CompressedTuple x(i, i, s, s); // Sanity check. We should have not moved from `s` - EXPECT_EQ(s, "Very long std::string that goes in the heap"); + EXPECT_EQ(s, "Very long string that goes in the heap"); EXPECT_EQ(x.get<0>(), x.get<1>()); EXPECT_NE(&x.get<0>(), &x.get<1>()); diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index a96ae68a..2fc85591 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -1666,9 +1666,9 @@ TEST(Nodes, EmptyNodeType) { } TEST(Nodes, ExtractInsert) { - constexpr char k0[] = "Very long std::string zero."; - constexpr char k1[] = "Very long std::string one."; - constexpr char k2[] = "Very long std::string two."; + constexpr char k0[] = "Very long string zero."; + constexpr char k1[] = "Very long string one."; + constexpr char k2[] = "Very long string two."; StringTable t = {{k0, ""}, {k1, ""}, {k2, ""}}; EXPECT_THAT(t, UnorderedElementsAre(Pair(k0, ""), Pair(k1, ""), Pair(k2, ""))); diff --git a/absl/debugging/failure_signal_handler.h b/absl/debugging/failure_signal_handler.h index f5a83962..0c0f585d 100644 --- a/absl/debugging/failure_signal_handler.h +++ b/absl/debugging/failure_signal_handler.h @@ -88,7 +88,7 @@ struct FailureSignalHandlerOptions { bool call_previous_handler = false; // If non-null, indicates a pointer to a callback function that will be called - // upon failure, with a std::string argument containing failure data. This function + // upon failure, with a string argument containing failure data. This function // may be used as a hook to write failure data to a secondary location, such // as a log file. This function may also be called with null data, as a hint // to flush any buffered data before the program may be terminated. Consider diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc index fc615c3f..fc262e50 100644 --- a/absl/debugging/internal/demangle.cc +++ b/absl/debugging/internal/demangle.cc @@ -151,7 +151,7 @@ static const AbbrevPair kSubstitutionList[] = { // frame, so every byte counts. typedef struct { int mangled_idx; // Cursor of mangled name. - int out_cur_idx; // Cursor of output std::string. + int out_cur_idx; // Cursor of output string. int prev_name_idx; // For constructors/destructors. signed int prev_name_length : 16; // For constructors/destructors. signed int nest_level : 15; // For nested names. @@ -172,8 +172,8 @@ static_assert(sizeof(ParseState) == 4 * sizeof(int), // Only one copy of this exists for each call to Demangle, so the size of this // struct is nearly inconsequential. typedef struct { - const char *mangled_begin; // Beginning of input std::string. - char *out; // Beginning of output std::string. + const char *mangled_begin; // Beginning of input string. + char *out; // Beginning of output string. int out_end_idx; // One past last allowed output character. int recursion_depth; // For stack exhaustion prevention. int steps; // Cap how much work we'll do, regardless of depth. diff --git a/absl/debugging/leak_check_fail_test.cc b/absl/debugging/leak_check_fail_test.cc index 2887ceab..c49b81a9 100644 --- a/absl/debugging/leak_check_fail_test.cc +++ b/absl/debugging/leak_check_fail_test.cc @@ -25,7 +25,7 @@ TEST(LeakCheckTest, LeakMemory) { // failed exit code. char* foo = strdup("lsan should complain about this leaked string"); - ABSL_RAW_LOG(INFO, "Should detect leaked std::string %s", foo); + ABSL_RAW_LOG(INFO, "Should detect leaked string %s", foo); } TEST(LeakCheckTest, LeakMemoryAfterDisablerScope) { @@ -34,7 +34,7 @@ TEST(LeakCheckTest, LeakMemoryAfterDisablerScope) { // failed exit code. { absl::LeakCheckDisabler disabler; } char* foo = strdup("lsan should also complain about this leaked string"); - ABSL_RAW_LOG(INFO, "Re-enabled leak detection.Should detect leaked std::string %s", + ABSL_RAW_LOG(INFO, "Re-enabled leak detection.Should detect leaked string %s", foo); } diff --git a/absl/debugging/leak_check_test.cc b/absl/debugging/leak_check_test.cc index 93a7edd2..b5cc4874 100644 --- a/absl/debugging/leak_check_test.cc +++ b/absl/debugging/leak_check_test.cc @@ -30,13 +30,13 @@ TEST(LeakCheckTest, DetectLeakSanitizer) { TEST(LeakCheckTest, IgnoreLeakSuppressesLeakedMemoryErrors) { auto foo = absl::IgnoreLeak(new std::string("some ignored leaked string")); - ABSL_RAW_LOG(INFO, "Ignoring leaked std::string %s", foo->c_str()); + ABSL_RAW_LOG(INFO, "Ignoring leaked string %s", foo->c_str()); } TEST(LeakCheckTest, LeakCheckDisablerIgnoresLeak) { absl::LeakCheckDisabler disabler; - auto foo = new std::string("some std::string leaked while checks are disabled"); - ABSL_RAW_LOG(INFO, "Ignoring leaked std::string %s", foo->c_str()); + auto foo = new std::string("some string leaked while checks are disabled"); + ABSL_RAW_LOG(INFO, "Ignoring leaked string %s", foo->c_str()); } } // namespace diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index c371635f..fe1d36ee 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc @@ -1402,7 +1402,7 @@ bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset if (g_num_file_mapping_hints >= kMaxFileMappingHints) { ret = false; } else { - // TODO(ckennelly): Move this into a std::string copy routine. + // TODO(ckennelly): Move this into a string copy routine. int len = strlen(filename); char *dst = static_cast( base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h index e91ddde6..9a740d57 100644 --- a/absl/flags/internal/commandlineflag.h +++ b/absl/flags/internal/commandlineflag.h @@ -141,7 +141,7 @@ class CommandLineFlag { // Returns name of the file where this flag is defined. virtual std::string Filename() const = 0; // Returns name of the flag's value type for some built-in types or empty - // std::string. + // string. virtual absl::string_view Typename() const = 0; // Returns help message associated with this flag. virtual std::string Help() const = 0; @@ -163,7 +163,7 @@ class CommandLineFlag { // or nullptr if flag does not support saving and restoring a state. virtual std::unique_ptr SaveState() = 0; - // Sets the value of the flag based on specified std::string `value`. If the flag + // Sets the value of the flag based on specified string `value`. If the flag // was successfully set to new value, it returns true. Otherwise, sets `error` // to indicate the error, leaves the flag unchanged, and returns false. There // are three ways to set the flag's value: @@ -176,7 +176,7 @@ class CommandLineFlag { flags_internal::ValueSource source, std::string* error) = 0; - // Checks that flags default value can be converted to std::string and back to the + // Checks that flags default value can be converted to string and back to the // flag's value type. virtual void CheckDefaultValueParsingRoundtrip() const = 0; diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index a944e16e..a12fe7c5 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -408,7 +408,7 @@ void FlagImpl::CheckDefaultValueParsingRoundtrip() const { ABSL_INTERNAL_LOG( FATAL, absl::StrCat("Flag ", Name(), " (from ", Filename(), - "): std::string form of default value '", v, + "): string form of default value '", v, "' could not be parsed; error=", error)); } diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index 307b7377..344e31f6 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -439,7 +439,7 @@ class FlagImpl { ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); // Flag initialization called via absl::call_once. void Init(); - // Attempts to parse supplied `value` std::string. If parsing is successful, + // Attempts to parse supplied `value` string. If parsing is successful, // returns new value. Otherwise returns nullptr. std::unique_ptr TryParse(absl::string_view value, std::string* err) const diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc index ff907161..a9a5cba9 100644 --- a/absl/flags/internal/usage.cc +++ b/absl/flags/internal/usage.cc @@ -134,14 +134,14 @@ class FlagHelpPrettyPrinter { first_line_(true) {} void Write(absl::string_view str, bool wrap_line = false) { - // Empty std::string - do nothing. + // Empty string - do nothing. if (str.empty()) return; std::vector tokens; if (wrap_line) { for (auto line : absl::StrSplit(str, absl::ByAnyChar("\n\r"))) { if (!tokens.empty()) { - // Keep line separators in the input std::string. + // Keep line separators in the input string. tokens.push_back("\n"); } for (auto token : @@ -156,13 +156,13 @@ class FlagHelpPrettyPrinter { for (auto token : tokens) { bool new_line = (line_len_ == 0); - // Respect line separators in the input std::string. + // Respect line separators in the input string. if (token == "\n") { EndLine(); continue; } - // Write the token, ending the std::string first if necessary/possible. + // Write the token, ending the string first if necessary/possible. if (!new_line && (line_len_ + token.size() >= max_line_len_)) { EndLine(); new_line = true; diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc index 6f2ddda8..09baae88 100644 --- a/absl/flags/marshalling.cc +++ b/absl/flags/marshalling.cc @@ -172,7 +172,7 @@ std::string Unparse(long long v) { return absl::StrCat(v); } std::string Unparse(unsigned long long v) { return absl::StrCat(v); } template std::string UnparseFloatingPointVal(T v) { - // digits10 is guaranteed to roundtrip correctly in std::string -> value -> std::string + // digits10 is guaranteed to roundtrip correctly in string -> value -> string // conversions, but may not be enough to represent all the values correctly. std::string digit10_str = absl::StrFormat("%.*g", std::numeric_limits::digits10, v); diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 812e4981..af5fb12d 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -533,10 +533,10 @@ std::tuple DeduceFlagValue(const CommandLineFlag& flag, curr_list->PopFront(); value = curr_list->Front(); - // Heuristic to detect the case where someone treats a std::string arg + // Heuristic to detect the case where someone treats a string arg // like a bool or just forgets to pass a value: // --my_string_var --foo=bar - // We look for a flag of std::string type, whose value begins with a + // We look for a flag of string type, whose value begins with a // dash and corresponds to known flag or standalone --. if (!value.empty() && value[0] == '-' && flag.IsOfType()) { auto maybe_flag_name = std::get<0>(SplitNameAndValue(value.substr(1))); @@ -646,7 +646,7 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], // 60. Split the current argument on '=' to figure out the argument // name and value. If flag name is empty it means we've got "--". value - // can be empty either if there were no '=' in argument std::string at all or + // can be empty either if there were no '=' in argument string at all or // an argument looked like "--foo=". In a latter case is_empty_value is // true. absl::string_view flag_name; diff --git a/absl/flags/usage_config.h b/absl/flags/usage_config.h index 0ed7e1b4..96eecea2 100644 --- a/absl/flags/usage_config.h +++ b/absl/flags/usage_config.h @@ -90,7 +90,7 @@ struct FlagsUsageConfig { // program output. flags_internal::FlagKindFilter contains_helppackage_flags; - // Generates std::string containing program version. This is the std::string reported + // Generates string containing program version. This is the string reported // when user specifies --version in a command line. std::function version_string; diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index e55e0ca9..5e6a8b18 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -316,7 +316,7 @@ TEST(HashValueTest, Strings) { t(std::string(huge)), t(absl::string_view(huge)), // t(FlatCord(huge)), t(FragmentedCord(huge))))); - // Make sure that hashing a `const char*` does not use its std::string-value. + // Make sure that hashing a `const char*` does not use its string-value. EXPECT_NE(SpyHash(static_cast("ABC")), SpyHash(absl::string_view("ABC"))); } @@ -512,7 +512,7 @@ TEST(HashValueTest, CombinePiecewiseBuffer) { SCOPED_TRACE(big_buffer_size); std::string big_buffer; for (int i = 0; i < big_buffer_size; ++i) { - // Arbitrary std::string + // Arbitrary string big_buffer.push_back(32 + (i * (i / 3)) % 64); } auto big_buffer_hash = hash(PiecewiseHashTester(big_buffer)); diff --git a/absl/random/bernoulli_distribution_test.cc b/absl/random/bernoulli_distribution_test.cc index f2c3b99c..5581af50 100644 --- a/absl/random/bernoulli_distribution_test.cc +++ b/absl/random/bernoulli_distribution_test.cc @@ -131,7 +131,7 @@ TEST(BernoulliTest, StabilityTest) { 0x275b0dc7e0a18acfull, 0x36cebe0d2653682eull, 0x0361e9b23861596bull, }); - // Generate a std::string of '0' and '1' for the distribution output. + // Generate a string of '0' and '1' for the distribution output. auto generate = [&urbg](absl::bernoulli_distribution& dist) { std::string output; output.reserve(36); @@ -176,7 +176,7 @@ TEST(BernoulliTest, StabilityTest2) { 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull, 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull}); - // Generate a std::string of '0' and '1' for the distribution output. + // Generate a string of '0' and '1' for the distribution output. auto generate = [&urbg](absl::bernoulli_distribution& dist) { std::string output; output.reserve(13); diff --git a/absl/random/internal/nanobenchmark.cc b/absl/random/internal/nanobenchmark.cc index 8fee77fc..c9181813 100644 --- a/absl/random/internal/nanobenchmark.cc +++ b/absl/random/internal/nanobenchmark.cc @@ -101,7 +101,7 @@ std::string BrandString() { char brand_string[49]; uint32_t abcd[4]; - // Check if brand std::string is supported (it is on all reasonable Intel/AMD) + // Check if brand string is supported (it is on all reasonable Intel/AMD) Cpuid(0x80000000U, 0, abcd); if (abcd[0] < 0x80000004U) { return std::string(); diff --git a/absl/random/uniform_int_distribution_test.cc b/absl/random/uniform_int_distribution_test.cc index aacff88d..69537603 100644 --- a/absl/random/uniform_int_distribution_test.cc +++ b/absl/random/uniform_int_distribution_test.cc @@ -123,7 +123,7 @@ TYPED_TEST(UniformIntDistributionTest, ViolatesPreconditionsDeathTest) { absl::uniform_int_distribution dist(10, 1); auto x = dist(gen); - // Any value will generate a non-empty std::string. + // Any value will generate a non-empty string. EXPECT_FALSE(absl::StrCat(+x).empty()) << x; #endif // NDEBUG } diff --git a/absl/status/status.cc b/absl/status/status.cc index 52ecc0ef..6d57a6be 100644 --- a/absl/status/status.cc +++ b/absl/status/status.cc @@ -177,7 +177,7 @@ void Status::ForEachPayload( visitor(elem.type_url, elem.payload); #else // In debug mode invalidate the type url to prevent users from relying on - // this std::string lifetime. + // this string lifetime. // NOLINTNEXTLINE intentional extra conversion to force temporary. visitor(std::string(elem.type_url), elem.payload); diff --git a/absl/status/status.h b/absl/status/status.h index 9706d4ba..67ff988f 100644 --- a/absl/status/status.h +++ b/absl/status/status.h @@ -122,7 +122,7 @@ class ABSL_MUST_USE_RESULT Status final { // Returns the error message. Note: prefer ToString() for debug logging. // This message rarely describes the error code. It is not unusual for the - // error message to be the empty std::string. + // error message to be the empty string. absl::string_view message() const; friend bool operator==(const Status&, const Status&); @@ -231,7 +231,7 @@ class ABSL_MUST_USE_RESULT Status final { static uintptr_t PointerToRep(status_internal::StatusRep* r); static status_internal::StatusRep* RepToPointer(uintptr_t r); - // Returns std::string for non-ok Status. + // Returns string for non-ok Status. std::string ToStringSlow() const; // Status supports two different representations. diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc index bdba768d..3613a652 100644 --- a/absl/strings/charconv.cc +++ b/absl/strings/charconv.cc @@ -619,10 +619,10 @@ from_chars_result FromCharsImpl(const char* first, const char* last, // Either we failed to parse a hex float after the "0x", or we read // "0xinf" or "0xnan" which we don't want to match. // - // However, a std::string that begins with "0x" also begins with "0", which + // However, a string that begins with "0x" also begins with "0", which // is normally a valid match for the number zero. So we want these // strings to match zero unless fmt_flags is `scientific`. (This flag - // means an exponent is required, which the std::string "0" does not have.) + // means an exponent is required, which the string "0" does not have.) if (fmt_flags == chars_format::scientific) { result.ec = std::errc::invalid_argument; } else { diff --git a/absl/strings/charconv_benchmark.cc b/absl/strings/charconv_benchmark.cc index 644b2abd..e8c7371d 100644 --- a/absl/strings/charconv_benchmark.cc +++ b/absl/strings/charconv_benchmark.cc @@ -132,7 +132,7 @@ BENCHMARK(BM_Absl_HugeMantissa); std::string MakeHardCase(int length) { // The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and // the next larger representable number. The digits of this number are in - // the std::string below. + // the string below. const std::string digits = "1." "152113937042223790993097181572444900347587985074226836242307364987727724" diff --git a/absl/strings/cord.h b/absl/strings/cord.h index 29ed7f75..3941f19c 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -278,7 +278,7 @@ class Cord { // Copies the contents from `src` to `*dst`. // - // This function optimizes the case of reusing the destination std::string since it + // This function optimizes the case of reusing the destination string since it // can reuse previously allocated capacity. However, this function does not // guarantee that pointers previously returned by `dst->data()` remain valid // even if `*dst` had enough capacity to hold `src`. If `*dst` is a new @@ -603,7 +603,7 @@ class Cord { } void CopyTo(std::string* dst) const { // memcpy is much faster when operating on a known size. On most supported - // platforms, the small std::string optimization is large enough that resizing + // platforms, the small string optimization is large enough that resizing // to 15 bytes does not cause a memory allocation. absl::strings_internal::STLStringResizeUninitialized(dst, sizeof(data_) - 1); diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index a683cc4b..d6e091f8 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -174,7 +174,7 @@ TEST(Cord, AllFlatSizes) { using absl::strings_internal::CordTestAccess; for (size_t s = 0; s < CordTestAccess::MaxFlatLength(); s++) { - // Make a std::string of length s. + // Make a string of length s. std::string src; while (src.size() < s) { src.push_back('a' + (src.size() % 26)); @@ -409,7 +409,7 @@ static void VerifyCopyToString(const absl::Cord& cord) { if (cord.size() <= kInitialLength) { EXPECT_EQ(has_initial_contents.data(), address_before_copy) - << "CopyCordToString allocated new std::string storage; " + << "CopyCordToString allocated new string storage; " "has_initial_contents = \"" << has_initial_contents << "\""; } @@ -856,7 +856,7 @@ TEST(Cord, CompareAfterAssign) { } // Test CompareTo() and ComparePrefix() against string and substring -// comparison methods from std::basic_string. +// comparison methods from basic_string. static void TestCompare(const absl::Cord& c, const absl::Cord& d, RandomEngine* rng) { typedef std::basic_string ustring; @@ -912,7 +912,7 @@ void CompareOperators() { EXPECT_TRUE(a == a); // For pointer type (i.e. `const char*`), operator== compares the address - // instead of the std::string, so `a == const char*("a")` isn't necessarily true. + // instead of the string, so `a == const char*("a")` isn't necessarily true. EXPECT_TRUE(std::is_pointer::value || a == T1("a")); EXPECT_TRUE(std::is_pointer::value || a == T2("a")); EXPECT_FALSE(a == b); diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 7adc1b65..9fceeef0 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -450,7 +450,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, // The GET_INPUT macro gets the next input character, skipping // over any whitespace, and stopping when we reach the end of the - // std::string or when we read any non-data character. The arguments are + // string or when we read any non-data character. The arguments are // an arbitrary identifier (used as a label for goto) and the number // of data bytes that must remain in the input to avoid aborting the // loop. @@ -473,18 +473,18 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, if (dest) { // This loop consumes 4 input bytes and produces 3 output bytes // per iteration. We can't know at the start that there is enough - // data left in the std::string for a full iteration, so the loop may + // data left in the string for a full iteration, so the loop may // break out in the middle; if so 'state' will be set to the // number of input bytes read. while (szsrc >= 4) { // We'll start by optimistically assuming that the next four - // bytes of the std::string (src[0..3]) are four good data bytes + // bytes of the string (src[0..3]) are four good data bytes // (that is, no nulls, whitespace, padding chars, or illegal // chars). We need to test src[0..2] for nulls individually // before constructing temp to preserve the property that we - // never read past a null in the std::string (no matter how long - // szsrc claims the std::string is). + // never read past a null in the string (no matter how long + // szsrc claims the string is). if (!src[0] || !src[1] || !src[2] || ((temp = ((unsigned(unbase64[src[0]]) << 18) | @@ -509,7 +509,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, temp = (temp << 6) | decode; } else { // We really did have four good data bytes, so advance four - // characters in the std::string. + // characters in the string. szsrc -= 4; src += 4; @@ -644,7 +644,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, state); } - // The remainder of the std::string should be all whitespace, mixed with + // The remainder of the string should be all whitespace, mixed with // exactly 0 equals signs, or exactly 'expected_equals' equals // signs. (Always accepting 0 equals signs is an Abseil extension // not covered in the RFC, as is accepting dot as the pad character.) @@ -771,7 +771,7 @@ constexpr char kWebSafeBase64Chars[] = template bool Base64UnescapeInternal(const char* src, size_t slen, String* dest, const signed char* unbase64) { - // Determine the size of the output std::string. Base64 encodes every 3 bytes into + // Determine the size of the output string. Base64 encodes every 3 bytes into // 4 characters. any leftover chars are added directly for good measure. // This is documented in the base64 RFC: http://tools.ietf.org/html/rfc3548 const size_t dest_len = 3 * (slen / 4) + (slen % 4); @@ -779,7 +779,7 @@ bool Base64UnescapeInternal(const char* src, size_t slen, String* dest, strings_internal::STLStringResizeUninitialized(dest, dest_len); // We are getting the destination buffer by getting the beginning of the - // std::string and converting it into a char *. + // string and converting it into a char *. size_t len; const bool ok = Base64UnescapeInternal(src, slen, &(*dest)[0], dest_len, unbase64, &len); diff --git a/absl/strings/escaping_test.cc b/absl/strings/escaping_test.cc index 1967975b..45671a0e 100644 --- a/absl/strings/escaping_test.cc +++ b/absl/strings/escaping_test.cc @@ -300,7 +300,7 @@ static struct { absl::string_view plaintext; absl::string_view cyphertext; } const base64_tests[] = { - // Empty std::string. + // Empty string. {{"", 0}, {"", 0}}, {{nullptr, 0}, {"", 0}}, // if length is zero, plaintext ptr must be ignored! @@ -586,7 +586,7 @@ void TestEscapeAndUnescape() { EXPECT_EQ(encoded, websafe); EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), websafe); - // Let's try the std::string version of the decoder + // Let's try the string version of the decoder decoded = "this junk should be ignored"; EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded)); EXPECT_EQ(decoded, tc.plaintext); @@ -625,7 +625,7 @@ TEST(Base64, DISABLED_HugeData) { std::string escaped; absl::Base64Escape(huge, &escaped); - // Generates the std::string that should match a base64 encoded "xxx..." std::string. + // Generates the string that should match a base64 encoded "xxx..." string. // "xxx" in base64 is "eHh4". std::string expected_encoding; expected_encoding.reserve(kSize / 3 * 4); diff --git a/absl/strings/internal/char_map.h b/absl/strings/internal/char_map.h index a76e6036..61484de0 100644 --- a/absl/strings/internal/char_map.h +++ b/absl/strings/internal/char_map.h @@ -72,7 +72,7 @@ class Charmap { CharMaskForWord(x, 2), CharMaskForWord(x, 3)); } - // Containing all the chars in the C-std::string 's'. + // Containing all the chars in the C-string 's'. // Note that this is expensively recursive because of the C++11 constexpr // formulation. Use only in constexpr initializers. static constexpr Charmap FromString(const char* s) { diff --git a/absl/strings/internal/charconv_bigint.cc b/absl/strings/internal/charconv_bigint.cc index 66f33e72..ebf8c079 100644 --- a/absl/strings/internal/charconv_bigint.cc +++ b/absl/strings/internal/charconv_bigint.cc @@ -208,7 +208,7 @@ int BigUnsigned::ReadDigits(const char* begin, const char* end, ++dropped_digits; } if (begin < end && *std::prev(end) == '.') { - // If the std::string ends in '.', either before or after dropping zeroes, then + // If the string ends in '.', either before or after dropping zeroes, then // drop the decimal point and look for more digits to drop. dropped_digits = 0; --end; diff --git a/absl/strings/internal/charconv_bigint.h b/absl/strings/internal/charconv_bigint.h index 999e9ae3..8f702976 100644 --- a/absl/strings/internal/charconv_bigint.h +++ b/absl/strings/internal/charconv_bigint.h @@ -66,7 +66,7 @@ class BigUnsigned { static_cast(v >> 32)} {} // Constructs a BigUnsigned from the given string_view containing a decimal - // value. If the input std::string is not a decimal integer, constructs a 0 + // value. If the input string is not a decimal integer, constructs a 0 // instead. explicit BigUnsigned(absl::string_view sv) : size_(0), words_{} { // Check for valid input, returning a 0 otherwise. This is reasonable @@ -210,7 +210,7 @@ class BigUnsigned { return words_[index]; } - // Returns this integer as a decimal std::string. This is not used in the decimal- + // Returns this integer as a decimal string. This is not used in the decimal- // to-binary conversion; it is intended to aid in testing. std::string ToString() const; diff --git a/absl/strings/internal/charconv_parse.cc b/absl/strings/internal/charconv_parse.cc index d9a57a78..fd6d9480 100644 --- a/absl/strings/internal/charconv_parse.cc +++ b/absl/strings/internal/charconv_parse.cc @@ -302,7 +302,7 @@ bool ParseInfinityOrNan(const char* begin, const char* end, switch (*begin) { case 'i': case 'I': { - // An infinity std::string consists of the characters "inf" or "infinity", + // An infinity string consists of the characters "inf" or "infinity", // case insensitive. if (strings_internal::memcasecmp(begin + 1, "nf", 2) != 0) { return false; @@ -326,7 +326,7 @@ bool ParseInfinityOrNan(const char* begin, const char* end, } out->type = strings_internal::FloatType::kNan; out->end = begin + 3; - // NaN is allowed to be followed by a parenthesized std::string, consisting of + // NaN is allowed to be followed by a parenthesized string, consisting of // only the characters [a-zA-Z0-9_]. Match that if it's present. begin += 3; if (begin < end && *begin == '(') { diff --git a/absl/strings/internal/charconv_parse_test.cc b/absl/strings/internal/charconv_parse_test.cc index 9511c987..bc2d1118 100644 --- a/absl/strings/internal/charconv_parse_test.cc +++ b/absl/strings/internal/charconv_parse_test.cc @@ -63,7 +63,7 @@ void ExpectParsedFloat(std::string s, absl::chars_format format_flags, } const std::string::size_type expected_characters_matched = s.find('$'); ABSL_RAW_CHECK(expected_characters_matched != std::string::npos, - "Input std::string must contain $"); + "Input string must contain $"); s.replace(expected_characters_matched, 1, ""); ParsedFloat parsed = diff --git a/absl/strings/internal/numbers_test_common.h b/absl/strings/internal/numbers_test_common.h index 1a1e50c4..eaa88a88 100644 --- a/absl/strings/internal/numbers_test_common.h +++ b/absl/strings/internal/numbers_test_common.h @@ -170,7 +170,7 @@ inline const std::array& strtouint64_test_cases() { {"0x1234", true, 16, 0x1234}, - // Base-10 std::string version. + // Base-10 string version. {"1234", true, 0, 1234}, {nullptr, false, 0, 0}, }}; diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index cf41b197..ee4475e0 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -76,11 +76,11 @@ class FormatSpecTemplate public: #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER - // Honeypot overload for when the std::string is not constexpr. + // Honeypot overload for when the string is not constexpr. // We use the 'unavailable' attribute to give a better compiler error than // just 'method is deleted'. FormatSpecTemplate(...) // NOLINT - __attribute__((unavailable("Format std::string is not constexpr."))); + __attribute__((unavailable("Format string is not constexpr."))); // Honeypot overload for when the format is constexpr and invalid. // We use the 'unavailable' attribute to give a better compiler error than diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index 45c90d1d..7d966517 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -143,7 +143,7 @@ bool ParseFormatString(string_view src, Consumer consumer) { auto tag = GetTagForChar(percent[1]); if (tag.is_conv()) { if (ABSL_PREDICT_FALSE(next_arg < 0)) { - // This indicates an error in the format std::string. + // This indicates an error in the format string. // The only way to get `next_arg < 0` here is to have a positional // argument first which sets next_arg to -1 and then a non-positional // argument. @@ -287,7 +287,7 @@ class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase { #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER __attribute__(( enable_if(str_format_internal::EnsureConstexpr(format), - "Format std::string is not constexpr."), + "Format string is not constexpr."), enable_if(str_format_internal::ValidFormatImpl(format), "Format specified does not match the template arguments."))) #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index 68229b15..bd4e1162 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc @@ -481,7 +481,7 @@ TEST(stringtest, safe_strto32_base) { EXPECT_TRUE(safe_strto32_base(std::string("0x1234"), &value, 16)); EXPECT_EQ(0x1234, value); - // Base-10 std::string version. + // Base-10 string version. EXPECT_TRUE(safe_strto32_base("1234", &value, 10)); EXPECT_EQ(1234, value); } @@ -622,7 +622,7 @@ TEST(stringtest, safe_strto64_base) { EXPECT_TRUE(safe_strto64_base(std::string("0x1234"), &value, 16)); EXPECT_EQ(0x1234, value); - // Base-10 std::string version. + // Base-10 string version. EXPECT_TRUE(safe_strto64_base("1234", &value, 10)); EXPECT_EQ(1234, value); } diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h index 292fa235..a8a85c73 100644 --- a/absl/strings/str_cat.h +++ b/absl/strings/str_cat.h @@ -253,7 +253,7 @@ class AlphaNum { const std::basic_string, Allocator>& str) : piece_(str) {} - // Use std::string literals ":" instead of character literals ':'. + // Use string literals ":" instead of character literals ':'. AlphaNum(char c) = delete; // NOLINT(runtime/explicit) AlphaNum(const AlphaNum&) = delete; diff --git a/absl/strings/str_cat_benchmark.cc b/absl/strings/str_cat_benchmark.cc index 14c63b3f..ee4ad112 100644 --- a/absl/strings/str_cat_benchmark.cc +++ b/absl/strings/str_cat_benchmark.cc @@ -23,7 +23,7 @@ namespace { const char kStringOne[] = "Once Upon A Time, "; -const char kStringTwo[] = "There was a std::string benchmark"; +const char kStringTwo[] = "There was a string benchmark"; // We want to include negative numbers in the benchmark, so this function // is used to count 0, 1, -1, 2, -2, 3, -3, ... diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc index be39880b..f3770dc0 100644 --- a/absl/strings/str_cat_test.cc +++ b/absl/strings/str_cat_test.cc @@ -162,7 +162,7 @@ TEST(StrCat, Basics) { EXPECT_EQ(result, "12345678910, 10987654321!"); std::string one = - "1"; // Actually, it's the size of this std::string that we want; a + "1"; // Actually, it's the size of this string that we want; a // 64-bit build distinguishes between size_t and uint64_t, // even though they're both unsigned 64-bit values. result = absl::StrCat("And a ", one.size(), " and a ", @@ -375,7 +375,7 @@ TEST(StrAppend, Basics) { EXPECT_EQ(result.substr(old_size), "12345678910, 10987654321!"); std::string one = - "1"; // Actually, it's the size of this std::string that we want; a + "1"; // Actually, it's the size of this string that we want; a // 64-bit build distinguishes between size_t and uint64_t, // even though they're both unsigned 64-bit values. old_size = result.size(); @@ -463,7 +463,7 @@ TEST(StrAppend, CornerCases) { } TEST(StrAppend, CornerCasesNonEmptyAppend) { - for (std::string result : {"hello", "a std::string too long to fit in the SSO"}) { + for (std::string result : {"hello", "a string too long to fit in the SSO"}) { const std::string expected = result; absl::StrAppend(&result, ""); EXPECT_EQ(result, expected); diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index acbdbf4a..554dca72 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -242,7 +242,7 @@ class TempFile { std::FILE* file() const { return file_; } - // Read the file into a std::string. + // Read the file into a string. std::string ReadFile() { std::fseek(file_, 0, SEEK_END); int size = std::ftell(file_); @@ -345,7 +345,7 @@ TEST(StrFormat, BehavesAsDocumented) { EXPECT_EQ(StrFormat("%c", int{'a'}), "a"); EXPECT_EQ(StrFormat("%c", long{'a'}), "a"); // NOLINT EXPECT_EQ(StrFormat("%c", uint64_t{'a'}), "a"); - // "s" - std::string Eg: "C" -> "C", std::string("C++") -> "C++" + // "s" - string Eg: "C" -> "C", std::string("C++") -> "C++" // Formats std::string, char*, string_view, and Cord. EXPECT_EQ(StrFormat("%s", "C"), "C"); EXPECT_EQ(StrFormat("%s", std::string("C++")), "C++"); diff --git a/absl/strings/str_join_test.cc b/absl/strings/str_join_test.cc index 921d9c2b..2be6256e 100644 --- a/absl/strings/str_join_test.cc +++ b/absl/strings/str_join_test.cc @@ -134,26 +134,26 @@ TEST(StrJoin, APIExamples) { // { - // Empty range yields an empty std::string. + // Empty range yields an empty string. std::vector v; EXPECT_EQ("", absl::StrJoin(v, "-")); } { - // A range of 1 element gives a std::string with that element but no + // A range of 1 element gives a string with that element but no // separator. std::vector v = {"foo"}; EXPECT_EQ("foo", absl::StrJoin(v, "-")); } { - // A range with a single empty std::string element + // A range with a single empty string element std::vector v = {""}; EXPECT_EQ("", absl::StrJoin(v, "-")); } { - // A range with 2 elements, one of which is an empty std::string + // A range with 2 elements, one of which is an empty string std::vector v = {"a", ""}; EXPECT_EQ("a-", absl::StrJoin(v, "-")); } diff --git a/absl/strings/str_replace_benchmark.cc b/absl/strings/str_replace_benchmark.cc index 95b2dc10..01331da2 100644 --- a/absl/strings/str_replace_benchmark.cc +++ b/absl/strings/str_replace_benchmark.cc @@ -62,7 +62,7 @@ void SetUpStrings() { } } // big_string->resize(50); - // OK, we've set up the std::string, now let's set up expectations - first by + // OK, we've set up the string, now let's set up expectations - first by // just replacing "the" with "box" after_replacing_the = new std::string(*big_string); for (size_t pos = 0; diff --git a/absl/strings/str_replace_test.cc b/absl/strings/str_replace_test.cc index 1ca23aff..9d8c7f75 100644 --- a/absl/strings/str_replace_test.cc +++ b/absl/strings/str_replace_test.cc @@ -25,7 +25,7 @@ TEST(StrReplaceAll, OneReplacement) { std::string s; - // Empty std::string. + // Empty string. s = absl::StrReplaceAll(s, {{"", ""}}); EXPECT_EQ(s, ""); s = absl::StrReplaceAll(s, {{"x", ""}}); @@ -47,7 +47,7 @@ TEST(StrReplaceAll, OneReplacement) { s = absl::StrReplaceAll("abc", {{"xyz", "123"}}); EXPECT_EQ(s, "abc"); - // Replace entire std::string. + // Replace entire string. s = absl::StrReplaceAll("abc", {{"abc", "xyz"}}); EXPECT_EQ(s, "xyz"); @@ -88,7 +88,7 @@ TEST(StrReplaceAll, OneReplacement) { TEST(StrReplaceAll, ManyReplacements) { std::string s; - // Empty std::string. + // Empty string. s = absl::StrReplaceAll("", {{"", ""}, {"x", ""}, {"", "y"}, {"x", "y"}}); EXPECT_EQ(s, ""); @@ -96,7 +96,7 @@ TEST(StrReplaceAll, ManyReplacements) { s = absl::StrReplaceAll("abc", {{"", ""}, {"", "y"}, {"x", ""}}); EXPECT_EQ(s, "abc"); - // Replace entire std::string, one char at a time + // Replace entire string, one char at a time s = absl::StrReplaceAll("abc", {{"a", "x"}, {"b", "y"}, {"c", "z"}}); EXPECT_EQ(s, "xyz"); s = absl::StrReplaceAll("zxy", {{"z", "x"}, {"x", "y"}, {"y", "z"}}); @@ -264,7 +264,7 @@ TEST(StrReplaceAll, Inplace) { std::string s; int reps; - // Empty std::string. + // Empty string. s = ""; reps = absl::StrReplaceAll({{"", ""}, {"x", ""}, {"", "y"}, {"x", "y"}}, &s); EXPECT_EQ(reps, 0); @@ -276,7 +276,7 @@ TEST(StrReplaceAll, Inplace) { EXPECT_EQ(reps, 0); EXPECT_EQ(s, "abc"); - // Replace entire std::string, one char at a time + // Replace entire string, one char at a time s = "abc"; reps = absl::StrReplaceAll({{"a", "x"}, {"b", "y"}, {"c", "z"}}, &s); EXPECT_EQ(reps, 3); diff --git a/absl/strings/str_split.cc b/absl/strings/str_split.cc index d0f86669..e08c26b6 100644 --- a/absl/strings/str_split.cc +++ b/absl/strings/str_split.cc @@ -42,7 +42,7 @@ absl::string_view GenericFind(absl::string_view text, absl::string_view delimiter, size_t pos, FindPolicy find_policy) { if (delimiter.empty() && text.length() > 0) { - // Special case for empty std::string delimiters: always return a zero-length + // Special case for empty string delimiters: always return a zero-length // absl::string_view referring to the item at position 1 past pos. return absl::string_view(text.data() + pos + 1, 0); } @@ -127,7 +127,7 @@ absl::string_view ByLength::Find(absl::string_view text, size_t pos) const { pos = std::min(pos, text.size()); // truncate `pos` absl::string_view substr = text.substr(pos); - // If the std::string is shorter than the chunk size we say we + // If the string is shorter than the chunk size we say we // "can't find the delimiter" so this will be the last chunk. if (substr.length() <= static_cast(length_)) return absl::string_view(text.data() + text.size(), 0); diff --git a/absl/strings/str_split_test.cc b/absl/strings/str_split_test.cc index 02f27bc4..67f62a78 100644 --- a/absl/strings/str_split_test.cc +++ b/absl/strings/str_split_test.cc @@ -71,7 +71,7 @@ TEST(Split, TraitsTest) { // namespaces just like callers will need to use. TEST(Split, APIExamples) { { - // Passes std::string delimiter. Assumes the default of ByString. + // Passes string delimiter. Assumes the default of ByString. std::vector v = absl::StrSplit("a,b,c", ","); // NOLINT EXPECT_THAT(v, ElementsAre("a", "b", "c")); @@ -97,7 +97,7 @@ TEST(Split, APIExamples) { } { - // Uses the Literal std::string "=>" as the delimiter. + // Uses the Literal string "=>" as the delimiter. const std::vector v = absl::StrSplit("a=>b=>c", "=>"); EXPECT_THAT(v, ElementsAre("a", "b", "c")); } @@ -121,17 +121,17 @@ TEST(Split, APIExamples) { } { - // Splits the input std::string into individual characters by using an empty - // std::string as the delimiter. + // Splits the input string into individual characters by using an empty + // string as the delimiter. std::vector v = absl::StrSplit("abc", ""); EXPECT_THAT(v, ElementsAre("a", "b", "c")); } { - // Splits std::string data with embedded NUL characters, using NUL as the + // Splits string data with embedded NUL characters, using NUL as the // delimiter. A simple delimiter of "\0" doesn't work because strlen() will - // say that's the empty std::string when constructing the absl::string_view - // delimiter. Instead, a non-empty std::string containing NUL can be used as the + // say that's the empty string when constructing the absl::string_view + // delimiter. Instead, a non-empty string containing NUL can be used as the // delimiter. std::string embedded_nulls("a\0b\0c", 5); std::string null_delim("\0", 1); @@ -436,7 +436,7 @@ TEST(Splitter, ConversionOperator) { // less-than, equal-to, and more-than 2 strings. TEST(Splitter, ToPair) { { - // Empty std::string + // Empty string std::pair p = absl::StrSplit("", ','); EXPECT_EQ("", p.first); EXPECT_EQ("", p.second); @@ -565,7 +565,7 @@ TEST(Split, AcceptsCertainTemporaries) { TEST(Split, Temporary) { // Use a std::string longer than the SSO length, so that when the temporary is - // destroyed, if the splitter keeps a reference to the std::string's contents, + // destroyed, if the splitter keeps a reference to the string's contents, // it'll reference freed memory instead of just dead on-stack memory. const char input[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u"; EXPECT_LT(sizeof(std::string), ABSL_ARRAYSIZE(input)) @@ -651,14 +651,14 @@ TEST(Split, UTF8) { // Tests splitting utf8 strings and utf8 delimiters. std::string utf8_string = u8"\u03BA\u1F79\u03C3\u03BC\u03B5"; { - // A utf8 input std::string with an ascii delimiter. + // A utf8 input string with an ascii delimiter. std::string to_split = "a," + utf8_string; std::vector v = absl::StrSplit(to_split, ','); EXPECT_THAT(v, ElementsAre("a", utf8_string)); } { - // A utf8 input std::string and a utf8 delimiter. + // A utf8 input string and a utf8 delimiter. std::string to_split = "a," + utf8_string + ",b"; std::string unicode_delimiter = "," + utf8_string + ","; std::vector v = @@ -667,7 +667,7 @@ TEST(Split, UTF8) { } { - // A utf8 input std::string and ByAnyChar with ascii chars. + // A utf8 input string and ByAnyChar with ascii chars. std::vector v = absl::StrSplit(u8"Foo h\u00E4llo th\u4E1Ere", absl::ByAnyChar(" \t")); EXPECT_THAT(v, ElementsAre("Foo", u8"h\u00E4llo", u8"th\u4E1Ere")); @@ -814,10 +814,10 @@ TEST(Delimiter, ByString) { ByString comma_string(","); TestComma(comma_string); - // The first occurrence of empty std::string ("") in a std::string is at position 0. + // The first occurrence of empty string ("") in a string is at position 0. // There is a test below that demonstrates this for absl::string_view::find(). // If the ByString delimiter returned position 0 for this, there would - // be an infinite loop in the SplitIterator code. To avoid this, empty std::string + // be an infinite loop in the SplitIterator code. To avoid this, empty string // is a special case in that it always returns the item at position 1. absl::string_view abc("abc"); EXPECT_EQ(0, abc.find("")); // "" is found at position 0 @@ -876,7 +876,7 @@ TEST(Delimiter, ByAnyChar) { EXPECT_FALSE(IsFoundAt("=", two_delims, -1)); // ByAnyChar behaves just like ByString when given a delimiter of empty - // std::string. That is, it always returns a zero-length absl::string_view + // string. That is, it always returns a zero-length absl::string_view // referring to the item at position 1, not position 0. ByAnyChar empty(""); EXPECT_FALSE(IsFoundAt("", empty, 0)); @@ -913,7 +913,7 @@ TEST(Split, WorksWithLargeStrings) { std::vector v = absl::StrSplit(s, '-'); EXPECT_EQ(2, v.size()); // The first element will contain 2G of 'x's. - // testing::StartsWith is too slow with a 2G std::string. + // testing::StartsWith is too slow with a 2G string. EXPECT_EQ('x', v[0][0]); EXPECT_EQ('x', v[0][1]); EXPECT_EQ('x', v[0][3]); diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index 55e80d62..b314bc34 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -319,7 +319,7 @@ class string_view { // stored elsewhere). Note that `string_view::data()` may contain embedded nul // characters, but the returned buffer may or may not be NUL-terminated; // therefore, do not pass `data()` to a routine that expects a NUL-terminated - // std::string. + // string. constexpr const_pointer data() const noexcept { return ptr_; } // Modifiers @@ -327,7 +327,7 @@ class string_view { // string_view::remove_prefix() // // Removes the first `n` characters from the `string_view`. Note that the - // underlying std::string is not changed, only the view. + // underlying string is not changed, only the view. void remove_prefix(size_type n) { assert(n <= length_); ptr_ += n; @@ -337,7 +337,7 @@ class string_view { // string_view::remove_suffix() // // Removes the last `n` characters from the `string_view`. Note that the - // underlying std::string is not changed, only the view. + // underlying string is not changed, only the view. void remove_suffix(size_type n) { assert(n <= length_); length_ -= n; @@ -394,7 +394,7 @@ class string_view { // // Performs a lexicographical comparison between the `string_view` and // another `absl::string_view`, returning -1 if `this` is less than, 0 if - // `this` is equal to, and 1 if `this` is greater than the passed std::string + // `this` is equal to, and 1 if `this` is greater than the passed string // view. Note that in the case of data equality, a further comparison is made // on the respective sizes of the two `string_view`s to determine which is // smaller, equal, or greater. @@ -420,17 +420,17 @@ class string_view { } // Overload of `string_view::compare()` for comparing a `string_view` and a - // a different C-style std::string `s`. + // a different C-style string `s`. int compare(const char* s) const { return compare(string_view(s)); } // Overload of `string_view::compare()` for comparing a substring of the - // `string_view` and a different std::string C-style std::string `s`. + // `string_view` and a different string C-style string `s`. int compare(size_type pos1, size_type count1, const char* s) const { return substr(pos1, count1).compare(string_view(s)); } // Overload of `string_view::compare()` for comparing a substring of the - // `string_view` and a substring of a different C-style std::string `s`. + // `string_view` and a substring of a different C-style string `s`. int compare(size_type pos1, size_type count1, const char* s, size_type count2) const { return substr(pos1, count1).compare(string_view(s, count2)); diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc index cb6a758f..6ba06144 100644 --- a/absl/strings/string_view_test.cc +++ b/absl/strings/string_view_test.cc @@ -410,7 +410,7 @@ TEST(StringViewTest, STL2) { EXPECT_EQ(a.find(e, 17), 17); absl::string_view g("xx not found bb"); EXPECT_EQ(a.find(g), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.find(b), absl::string_view::npos); EXPECT_EQ(e.find(b), absl::string_view::npos); EXPECT_EQ(d.find(b, 4), absl::string_view::npos); @@ -438,7 +438,7 @@ TEST(StringViewTest, STL2) { EXPECT_EQ(g.find('o', 4), 4); EXPECT_EQ(g.find('o', 5), 8); EXPECT_EQ(a.find('b', 5), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.find('\0'), absl::string_view::npos); EXPECT_EQ(e.find('\0'), absl::string_view::npos); EXPECT_EQ(d.find('\0', 4), absl::string_view::npos); @@ -465,7 +465,7 @@ TEST(StringViewTest, STL2) { EXPECT_EQ(e.rfind(b), absl::string_view::npos); EXPECT_EQ(d.rfind(b, 4), absl::string_view::npos); EXPECT_EQ(e.rfind(b, 7), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.rfind(d, 4), std::string().rfind(std::string())); EXPECT_EQ(e.rfind(d, 7), std::string().rfind(std::string())); EXPECT_EQ(d.rfind(e, 4), std::string().rfind(std::string())); @@ -484,7 +484,7 @@ TEST(StringViewTest, STL2) { EXPECT_EQ(f.rfind('\0', 12), 3); EXPECT_EQ(f.rfind('3'), 2); EXPECT_EQ(f.rfind('5'), 5); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.rfind('o'), absl::string_view::npos); EXPECT_EQ(e.rfind('o'), absl::string_view::npos); EXPECT_EQ(d.rfind('o', 4), absl::string_view::npos); @@ -520,7 +520,7 @@ TEST(StringViewTest, STL2FindFirst) { EXPECT_EQ(g.find_first_of(c), 0); EXPECT_EQ(a.find_first_of(f), absl::string_view::npos); EXPECT_EQ(f.find_first_of(a), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(a.find_first_of(d), absl::string_view::npos); EXPECT_EQ(a.find_first_of(e), absl::string_view::npos); EXPECT_EQ(d.find_first_of(b), absl::string_view::npos); @@ -538,7 +538,7 @@ TEST(StringViewTest, STL2FindFirst) { EXPECT_EQ(a.find_first_not_of(f), 0); EXPECT_EQ(a.find_first_not_of(d), 0); EXPECT_EQ(a.find_first_not_of(e), 0); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(a.find_first_not_of(d), 0); EXPECT_EQ(a.find_first_not_of(e), 0); EXPECT_EQ(a.find_first_not_of(d, 1), 1); @@ -566,7 +566,7 @@ TEST(StringViewTest, STL2FindFirst) { EXPECT_EQ(f.find_first_not_of('\0'), 0); EXPECT_EQ(f.find_first_not_of('\0', 3), 4); EXPECT_EQ(f.find_first_not_of('\0', 2), 2); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.find_first_not_of('x'), absl::string_view::npos); EXPECT_EQ(e.find_first_not_of('x'), absl::string_view::npos); EXPECT_EQ(d.find_first_not_of('\0'), absl::string_view::npos); @@ -606,7 +606,7 @@ TEST(StringViewTest, STL2FindLast) { EXPECT_EQ(f.find_last_of(i, 5), 5); EXPECT_EQ(f.find_last_of(i, 6), 6); EXPECT_EQ(f.find_last_of(a, 4), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(f.find_last_of(d), absl::string_view::npos); EXPECT_EQ(f.find_last_of(e), absl::string_view::npos); EXPECT_EQ(f.find_last_of(d, 4), absl::string_view::npos); @@ -632,7 +632,7 @@ TEST(StringViewTest, STL2FindLast) { EXPECT_EQ(a.find_last_not_of(c, 24), 22); EXPECT_EQ(a.find_last_not_of(b, 3), 3); EXPECT_EQ(a.find_last_not_of(b, 2), absl::string_view::npos); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(f.find_last_not_of(d), f.size()-1); EXPECT_EQ(f.find_last_not_of(e), f.size()-1); EXPECT_EQ(f.find_last_not_of(d, 4), 4); @@ -656,7 +656,7 @@ TEST(StringViewTest, STL2FindLast) { EXPECT_EQ(h.find_last_not_of('x', 2), 2); EXPECT_EQ(h.find_last_not_of('=', 2), absl::string_view::npos); EXPECT_EQ(b.find_last_not_of('b', 1), 0); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.find_last_not_of('x'), absl::string_view::npos); EXPECT_EQ(e.find_last_not_of('x'), absl::string_view::npos); EXPECT_EQ(d.find_last_not_of('\0'), absl::string_view::npos); @@ -678,7 +678,7 @@ TEST(StringViewTest, STL2Substr) { EXPECT_EQ(a.substr(23, 99), c); EXPECT_EQ(a.substr(0), a); EXPECT_EQ(a.substr(3, 2), "de"); - // empty std::string nonsense + // empty string nonsense EXPECT_EQ(d.substr(0, 99), e); // use of npos EXPECT_EQ(a.substr(0, absl::string_view::npos), a); @@ -859,7 +859,7 @@ TEST(StringViewTest, NULLInput) { EXPECT_EQ(s.size(), 0); // .ToString() on a absl::string_view with nullptr should produce the empty - // std::string. + // string. EXPECT_EQ("", std::string(s)); #endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR } @@ -977,7 +977,7 @@ TEST(StringViewTest, ConstexprCompiles) { #if defined(ABSL_USES_STD_STRING_VIEW) // In libstdc++ (as of 7.2), `std::string_view::string_view(const char*)` - // calls `std::char_traits::length(const char*)` to get the std::string + // calls `std::char_traits::length(const char*)` to get the string // length, but it is not marked constexpr yet. See GCC bug: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78156 // Also, there is a LWG issue that adds constexpr to length() which was just @@ -1180,7 +1180,7 @@ TEST(FindOneCharTest, EdgeCases) { TEST(HugeStringView, TwoPointTwoGB) { if (sizeof(size_t) <= 4 || RunningOnValgrind()) return; - // Try a huge std::string piece. + // Try a huge string piece. const size_t size = size_t{2200} * 1000 * 1000; std::string s(size, 'a'); absl::string_view sp(s); diff --git a/absl/strings/substitute.cc b/absl/strings/substitute.cc index 5b69a3ef..1f3c7409 100644 --- a/absl/strings/substitute.cc +++ b/absl/strings/substitute.cc @@ -36,7 +36,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, if (i + 1 >= format.size()) { #ifndef NDEBUG ABSL_RAW_LOG(FATAL, - "Invalid absl::Substitute() format std::string: \"%s\".", + "Invalid absl::Substitute() format string: \"%s\".", absl::CEscape(format).c_str()); #endif return; @@ -46,8 +46,8 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, #ifndef NDEBUG ABSL_RAW_LOG( FATAL, - "Invalid absl::Substitute() format std::string: asked for \"$" - "%d\", but only %d args were given. Full format std::string was: " + "Invalid absl::Substitute() format string: asked for \"$" + "%d\", but only %d args were given. Full format string was: " "\"%s\".", index, static_cast(num_args), absl::CEscape(format).c_str()); #endif @@ -61,7 +61,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, } else { #ifndef NDEBUG ABSL_RAW_LOG(FATAL, - "Invalid absl::Substitute() format std::string: \"%s\".", + "Invalid absl::Substitute() format string: \"%s\".", absl::CEscape(format).c_str()); #endif return; @@ -73,7 +73,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, if (size == 0) return; - // Build the std::string. + // Build the string. size_t original_size = output->size(); strings_internal::STLStringResizeUninitialized(output, original_size + size); char* target = &(*output)[original_size]; diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h index 4d0984d3..e7b4c1e6 100644 --- a/absl/strings/substitute.h +++ b/absl/strings/substitute.h @@ -99,7 +99,7 @@ namespace substitute_internal { // This class has implicit constructors. class Arg { public: - // Overloads for std::string-y things + // Overloads for string-y things // // Explicitly overload `const char*` so the compiler doesn't cast to `bool`. Arg(const char* value) // NOLINT(runtime/explicit) @@ -360,13 +360,13 @@ inline void SubstituteAndAppend( void SubstituteAndAppend(std::string* output, const char* format) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0, "There were no substitution arguments " - "but this format std::string has a $[0-9] in it"); + "but this format string has a $[0-9] in it"); void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a0) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1, "There was 1 substitution argument given, but " - "this format std::string is either missing its $0, or " + "this format string is either missing its $0, or " "contains one of $1-$9"); void SubstituteAndAppend(std::string* output, const char* format, @@ -374,7 +374,7 @@ void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a1) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3, "There were 2 substitution arguments given, but " - "this format std::string is either missing its $0/$1, or " + "this format string is either missing its $0/$1, or " "contains one of $2-$9"); void SubstituteAndAppend(std::string* output, const char* format, @@ -383,7 +383,7 @@ void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a2) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7, "There were 3 substitution arguments given, but " - "this format std::string is either missing its $0/$1/$2, or " + "this format string is either missing its $0/$1/$2, or " "contains one of $3-$9"); void SubstituteAndAppend(std::string* output, const char* format, @@ -393,7 +393,7 @@ void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a3) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15, "There were 4 substitution arguments given, but " - "this format std::string is either missing its $0-$3, or " + "this format string is either missing its $0-$3, or " "contains one of $4-$9"); void SubstituteAndAppend(std::string* output, const char* format, @@ -404,7 +404,7 @@ void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a4) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31, "There were 5 substitution arguments given, but " - "this format std::string is either missing its $0-$4, or " + "this format string is either missing its $0-$4, or " "contains one of $5-$9"); void SubstituteAndAppend(std::string* output, const char* format, @@ -416,7 +416,7 @@ void SubstituteAndAppend(std::string* output, const char* format, const substitute_internal::Arg& a5) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63, "There were 6 substitution arguments given, but " - "this format std::string is either missing its $0-$5, or " + "this format string is either missing its $0-$5, or " "contains one of $6-$9"); void SubstituteAndAppend( @@ -426,7 +426,7 @@ void SubstituteAndAppend( const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127, "There were 7 substitution arguments given, but " - "this format std::string is either missing its $0-$6, or " + "this format string is either missing its $0-$6, or " "contains one of $7-$9"); void SubstituteAndAppend( @@ -437,7 +437,7 @@ void SubstituteAndAppend( const substitute_internal::Arg& a7) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255, "There were 8 substitution arguments given, but " - "this format std::string is either missing its $0-$7, or " + "this format string is either missing its $0-$7, or " "contains one of $8-$9"); void SubstituteAndAppend( @@ -449,7 +449,7 @@ void SubstituteAndAppend( ABSL_BAD_CALL_IF( substitute_internal::PlaceholderBitmask(format) != 511, "There were 9 substitution arguments given, but " - "this format std::string is either missing its $0-$8, or contains a $9"); + "this format string is either missing its $0-$8, or contains a $9"); void SubstituteAndAppend( std::string* output, const char* format, const substitute_internal::Arg& a0, @@ -460,7 +460,7 @@ void SubstituteAndAppend( const substitute_internal::Arg& a9) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023, "There were 10 substitution arguments given, but this " - "format std::string doesn't contain all of $0 through $9"); + "format string doesn't contain all of $0 through $9"); #endif // ABSL_BAD_CALL_IF // Substitute() @@ -586,19 +586,19 @@ ABSL_MUST_USE_RESULT inline std::string Substitute( std::string Substitute(const char* format) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0, "There were no substitution arguments " - "but this format std::string has a $[0-9] in it"); + "but this format string has a $[0-9] in it"); std::string Substitute(const char* format, const substitute_internal::Arg& a0) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1, "There was 1 substitution argument given, but " - "this format std::string is either missing its $0, or " + "this format string is either missing its $0, or " "contains one of $1-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a1) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3, "There were 2 substitution arguments given, but " - "this format std::string is either missing its $0/$1, or " + "this format string is either missing its $0/$1, or " "contains one of $2-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -606,7 +606,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a2) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7, "There were 3 substitution arguments given, but " - "this format std::string is either missing its $0/$1/$2, or " + "this format string is either missing its $0/$1/$2, or " "contains one of $3-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -615,7 +615,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a3) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15, "There were 4 substitution arguments given, but " - "this format std::string is either missing its $0-$3, or " + "this format string is either missing its $0-$3, or " "contains one of $4-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -625,7 +625,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a4) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31, "There were 5 substitution arguments given, but " - "this format std::string is either missing its $0-$4, or " + "this format string is either missing its $0-$4, or " "contains one of $5-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -636,7 +636,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a5) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63, "There were 6 substitution arguments given, but " - "this format std::string is either missing its $0-$5, or " + "this format string is either missing its $0-$5, or " "contains one of $6-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -648,7 +648,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a6) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127, "There were 7 substitution arguments given, but " - "this format std::string is either missing its $0-$6, or " + "this format string is either missing its $0-$6, or " "contains one of $7-$9"); std::string Substitute(const char* format, const substitute_internal::Arg& a0, @@ -661,7 +661,7 @@ std::string Substitute(const char* format, const substitute_internal::Arg& a0, const substitute_internal::Arg& a7) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255, "There were 8 substitution arguments given, but " - "this format std::string is either missing its $0-$7, or " + "this format string is either missing its $0-$7, or " "contains one of $8-$9"); std::string Substitute( @@ -673,7 +673,7 @@ std::string Substitute( ABSL_BAD_CALL_IF( substitute_internal::PlaceholderBitmask(format) != 511, "There were 9 substitution arguments given, but " - "this format std::string is either missing its $0-$8, or contains a $9"); + "this format string is either missing its $0-$8, or contains a $9"); std::string Substitute( const char* format, const substitute_internal::Arg& a0, @@ -684,7 +684,7 @@ std::string Substitute( const substitute_internal::Arg& a9) ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023, "There were 10 substitution arguments given, but this " - "format std::string doesn't contain all of $0 through $9"); + "format string doesn't contain all of $0 through $9"); #endif // ABSL_BAD_CALL_IF ABSL_NAMESPACE_END diff --git a/absl/strings/substitute_test.cc b/absl/strings/substitute_test.cc index 450cd2bc..442c9215 100644 --- a/absl/strings/substitute_test.cc +++ b/absl/strings/substitute_test.cc @@ -89,7 +89,7 @@ TEST(SubstituteTest, Substitute) { str = absl::Substitute("$0", char_buf); EXPECT_EQ("print me too", str); - // null char* is "doubly" special. Represented as the empty std::string. + // null char* is "doubly" special. Represented as the empty string. char_p = nullptr; str = absl::Substitute("$0", char_p); EXPECT_EQ("", str); @@ -189,14 +189,14 @@ TEST(SubstituteTest, VectorBoolRef) { TEST(SubstituteDeathTest, SubstituteDeath) { EXPECT_DEBUG_DEATH( static_cast(absl::Substitute(absl::string_view("-$2"), "a", "b")), - "Invalid absl::Substitute\\(\\) format std::string: asked for \"\\$2\", " + "Invalid absl::Substitute\\(\\) format string: asked for \"\\$2\", " "but only 2 args were given."); EXPECT_DEBUG_DEATH( static_cast(absl::Substitute(absl::string_view("-$z-"))), - "Invalid absl::Substitute\\(\\) format std::string: \"-\\$z-\""); + "Invalid absl::Substitute\\(\\) format string: \"-\\$z-\""); EXPECT_DEBUG_DEATH( static_cast(absl::Substitute(absl::string_view("-$"))), - "Invalid absl::Substitute\\(\\) format std::string: \"-\\$\""); + "Invalid absl::Substitute\\(\\) format string: \"-\\$\""); } #endif // GTEST_HAS_DEATH_TEST diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index e0879b05..426558e6 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc @@ -299,7 +299,7 @@ static struct SynchEvent { // this is a trivial hash table for the events bool log; // logging turned on // Constant after initialization - char name[1]; // actually longer---NUL-terminated std::string + char name[1]; // actually longer---NUL-terminated string } * synch_event[kNSynchEvent] ABSL_GUARDED_BY(synch_event_mu); // Ensure that the object at "addr" has a SynchEvent struct associated with it, diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc index ada82cbc..c4202c73 100644 --- a/absl/time/civil_time.cc +++ b/absl/time/civil_time.cc @@ -38,7 +38,7 @@ std::string FormatYearAnd(string_view fmt, CivilSecond cs) { const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(), cs.hour(), cs.minute(), cs.second()); const TimeZone utc = UTCTimeZone(); - // TODO(absl-team): Avoid conversion of fmt std::string. + // TODO(absl-team): Avoid conversion of fmt string. return StrCat(cs.year(), FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); } @@ -47,7 +47,7 @@ template bool ParseYearAnd(string_view fmt, string_view s, CivilT* c) { // Civil times support a larger year range than absl::Time, so we need to // parse the year separately, normalize it, then use absl::ParseTime on the - // normalized std::string. + // normalized string. const std::string ss = std::string(s); // TODO(absl-team): Avoid conversion. const char* const np = ss.c_str(); char* endp; @@ -82,7 +82,7 @@ bool ParseAs(string_view s, CivilT2* c) { template bool ParseLenient(string_view s, CivilT* c) { - // A fastpath for when the given std::string data parses exactly into the given + // A fastpath for when the given string data parses exactly into the given // type T (e.g., s="YYYY-MM-DD" and CivilT=CivilDay). if (ParseCivilTime(s, c)) return true; // Try parsing as each of the 6 types, trying the most common types first diff --git a/absl/time/duration.cc b/absl/time/duration.cc index b1af8406..1353fa0a 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -874,12 +874,12 @@ bool ParseDuration(const std::string& dur_string, Duration* d) { ++start; } - // Can't parse a duration from an empty std::string. + // Can't parse a duration from an empty string. if (*start == '\0') { return false; } - // Special case for a std::string of "0". + // Special case for a string of "0". if (*start == '0' && *(start + 1) == '\0') { *d = ZeroDuration(); return true; diff --git a/absl/time/format_test.cc b/absl/time/format_test.cc index ab1f3059..a9a1eb8e 100644 --- a/absl/time/format_test.cc +++ b/absl/time/format_test.cc @@ -173,7 +173,7 @@ TEST(ParseTime, WithTimeZone) { absl::Time t; std::string e; - // We can parse a std::string without a UTC offset if we supply a timezone. + // We can parse a string without a UTC offset if we supply a timezone. EXPECT_TRUE( absl::ParseTime("%Y-%m-%d %H:%M:%S", "2013-06-28 19:08:09", tz, &t, &e)) << e; @@ -327,7 +327,7 @@ TEST(ParseTime, InfiniteTime) { EXPECT_TRUE(absl::ParseTime("%H:%M blah", " infinite-past ", &t, &err)); EXPECT_EQ(absl::InfinitePast(), t); - // "infinite-future" as literal std::string + // "infinite-future" as literal string absl::TimeZone tz = absl::UTCTimeZone(); EXPECT_TRUE(absl::ParseTime("infinite-future %H:%M", "infinite-future 03:04", &t, &err)); @@ -335,7 +335,7 @@ TEST(ParseTime, InfiniteTime) { EXPECT_EQ(3, tz.At(t).cs.hour()); EXPECT_EQ(4, tz.At(t).cs.minute()); - // "infinite-past" as literal std::string + // "infinite-past" as literal string EXPECT_TRUE( absl::ParseTime("infinite-past %H:%M", "infinite-past 03:04", &t, &err)); EXPECT_NE(absl::InfinitePast(), t); diff --git a/absl/time/internal/cctz/include/cctz/time_zone.h b/absl/time/internal/cctz/include/cctz/time_zone.h index d05147a1..d4ea90ef 100644 --- a/absl/time/internal/cctz/include/cctz/time_zone.h +++ b/absl/time/internal/cctz/include/cctz/time_zone.h @@ -209,7 +209,7 @@ class time_zone { // version() and description() provide additional information about the // time zone. The content of each of the returned strings is unspecified, // however, when the IANA Time Zone Database is the underlying data source - // the version() std::string will be in the familar form (e.g, "2018e") or + // the version() string will be in the familar form (e.g, "2018e") or // empty when unavailable. // // Note: These functions are for informational or testing purposes only. diff --git a/absl/time/internal/cctz/include/cctz/zone_info_source.h b/absl/time/internal/cctz/include/cctz/zone_info_source.h index 912b44ba..012eb4ec 100644 --- a/absl/time/internal/cctz/include/cctz/zone_info_source.h +++ b/absl/time/internal/cctz/include/cctz/zone_info_source.h @@ -37,7 +37,7 @@ class ZoneInfoSource { // Until the zoneinfo data supports versioning information, we provide // a way for a ZoneInfoSource to indicate it out-of-band. The default - // implementation returns an empty std::string. + // implementation returns an empty string. virtual std::string Version() const; }; diff --git a/absl/time/internal/cctz/src/time_zone_format.cc b/absl/time/internal/cctz/src/time_zone_format.cc index 950b23a1..179975e0 100644 --- a/absl/time/internal/cctz/src/time_zone_format.cc +++ b/absl/time/internal/cctz/src/time_zone_format.cc @@ -189,7 +189,7 @@ void FormatTM(std::string* out, const std::string& fmt, const std::tm& tm) { // strftime(3) returns the number of characters placed in the output // array (which may be 0 characters). It also returns 0 to indicate // an error, like the array wasn't large enough. To accommodate this, - // the following code grows the buffer size from 2x the format std::string + // the following code grows the buffer size from 2x the format string // length up to 32x. for (std::size_t i = 2; i != 32; i *= 2) { std::size_t buf_size = fmt.size() * i; @@ -839,7 +839,7 @@ bool parse(const std::string& format, const std::string& input, // Skip any remaining whitespace. while (std::isspace(*data)) ++data; - // parse() must consume the entire input std::string. + // parse() must consume the entire input string. if (*data != '\0') { if (err != nullptr) *err = "Illegal trailing data in input string"; return false; diff --git a/absl/time/internal/cctz/src/time_zone_format_test.cc b/absl/time/internal/cctz/src/time_zone_format_test.cc index caebcc4d..87382e15 100644 --- a/absl/time/internal/cctz/src/time_zone_format_test.cc +++ b/absl/time/internal/cctz/src/time_zone_format_test.cc @@ -767,7 +767,7 @@ TEST(Parse, WithTimeZone) { EXPECT_TRUE(load_time_zone("America/Los_Angeles", &tz)); time_point tp; - // We can parse a std::string without a UTC offset if we supply a timezone. + // We can parse a string without a UTC offset if we supply a timezone. EXPECT_TRUE(parse("%Y-%m-%d %H:%M:%S", "2013-06-28 19:08:09", tz, &tp)); ExpectTime(tp, tz, 2013, 6, 28, 19, 8, 9, -7 * 60 * 60, true, "PDT"); diff --git a/absl/time/internal/cctz/src/time_zone_impl.h b/absl/time/internal/cctz/src/time_zone_impl.h index 69806c10..7d747ba9 100644 --- a/absl/time/internal/cctz/src/time_zone_impl.h +++ b/absl/time/internal/cctz/src/time_zone_impl.h @@ -71,7 +71,7 @@ class time_zone::Impl { return zone_->PrevTransition(tp, trans); } - // Returns an implementation-defined version std::string for this time zone. + // Returns an implementation-defined version string for this time zone. std::string Version() const { return zone_->Version(); } // Returns an implementation-defined description of this time zone. diff --git a/absl/time/internal/cctz/src/time_zone_info.cc b/absl/time/internal/cctz/src/time_zone_info.cc index f1697cdf..665fb424 100644 --- a/absl/time/internal/cctz/src/time_zone_info.cc +++ b/absl/time/internal/cctz/src/time_zone_info.cc @@ -506,7 +506,7 @@ bool TimeZoneInfo::Load(const std::string& name, ZoneInfoSource* zip) { // If we did not find version information during the standard loading // process (as of tzh_version '3' that is unsupported), then ask the - // ZoneInfoSource for any out-of-bound version std::string it may be privy to. + // ZoneInfoSource for any out-of-bound version string it may be privy to. if (version_.empty()) { version_ = zip->Version(); } diff --git a/absl/time/internal/cctz/src/time_zone_lookup_test.cc b/absl/time/internal/cctz/src/time_zone_lookup_test.cc index 99137a08..35911ce5 100644 --- a/absl/time/internal/cctz/src/time_zone_lookup_test.cc +++ b/absl/time/internal/cctz/src/time_zone_lookup_test.cc @@ -749,7 +749,7 @@ TEST(TimeZone, Failures) { EXPECT_EQ(chrono::system_clock::from_time_t(0), convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC - // Loading an empty std::string timezone should fail. + // Loading an empty string timezone should fail. tz = LoadZone("America/Los_Angeles"); EXPECT_FALSE(load_time_zone("", &tz)); EXPECT_EQ(chrono::system_clock::from_time_t(0), diff --git a/absl/time/internal/cctz/src/tzfile.h b/absl/time/internal/cctz/src/tzfile.h index 1ed55e0f..269fa36c 100644 --- a/absl/time/internal/cctz/src/tzfile.h +++ b/absl/time/internal/cctz/src/tzfile.h @@ -83,13 +83,13 @@ struct tzhead { ** If tzh_version is '2' or greater, the above is followed by a second instance ** of tzhead and a second instance of the data in which each coded transition ** time uses 8 rather than 4 chars, -** then a POSIX-TZ-environment-variable-style std::string for use in handling +** then a POSIX-TZ-environment-variable-style string for use in handling ** instants after the last transition time stored in the file ** (with nothing between the newlines if there is no POSIX representation for ** such instants). ** ** If tz_version is '3' or greater, the above is extended as follows. -** First, the POSIX TZ std::string's hour offset may range from -167 +** First, the POSIX TZ string's hour offset may range from -167 ** through 167 as compared to the POSIX-required 0 through 24. ** Second, its DST start time may be January 1 at 00:00 and its stop ** time December 31 at 24:00 plus the difference between DST and diff --git a/absl/time/time_zone_test.cc b/absl/time/time_zone_test.cc index 8f1e74ac..229fcfcc 100644 --- a/absl/time/time_zone_test.cc +++ b/absl/time/time_zone_test.cc @@ -88,7 +88,7 @@ TEST(TimeZone, Failures) { EXPECT_FALSE(LoadTimeZone("Invalid/TimeZone", &tz)); EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC - // Loading an empty std::string timezone should fail. + // Loading an empty string timezone should fail. tz = absl::time_internal::LoadTimeZone("America/Los_Angeles"); EXPECT_FALSE(LoadTimeZone("", &tz)); EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc index 96393333..4639c42e 100644 --- a/absl/types/variant_test.cc +++ b/absl/types/variant_test.cc @@ -679,7 +679,7 @@ TEST(VariantTest, TestSelfAssignment) { object.operator=(object); EXPECT_EQ(0, counter); - // A std::string long enough that it's likely to defeat any inline representation + // A string long enough that it's likely to defeat any inline representation // optimization. const std::string long_str(128, 'a'); -- cgit v1.2.3 From 79e0dc11514df035a8d07a356f9ee1800fb2160c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 26 Mar 2020 08:48:01 -0700 Subject: Export of internal Abseil changes -- 990253454819ce26ff1dda9ab4bbc145b61d01e4 by Xiaoyi Zhang : Import github PR https://github.com/abseil/abseil-cpp/pull/645 PiperOrigin-RevId: 303119797 -- 5ac845cb7929b7d1eaf59a309afd811db5001175 by Abseil Team : Fix internal exception spec compatibility error PiperOrigin-RevId: 303104081 -- 3290595dd866eecab3c7044e2e3ca0adb74f1bf5 by Gennadiy Rozental : Use FlagValue to represent the value of a flag. Place it directly after FlagImpl and use a computed offset refer to it. The offset is computed based on the assumption that the `value_` data member is placed directly after the impl_ data member in Flag. This change will allow us to migrate to `T`-specific storage in the generic case. This change decreases the overhead for int flags by 32 bytes. PiperOrigin-RevId: 303038099 -- f2b37722cd7a6d3a60ef9713f0d2bbff56f3ddbf by Derek Mauro : Minor correctness fix for an ABSL_HAVE_BUILTIN conditional PiperOrigin-RevId: 302980666 -- 39c079a6141ae1c5728af8bf33a39c8aff9deb9f by Abseil Team : Use ABSL_HARDENING_ASSERT in b-tree and SwissTable iterators. PiperOrigin-RevId: 302970075 -- 9668a044e080c789df32bcaa1ffb5100831cd9fa by Benjamin Barenblat : Correct `add_subdirectory` line in CMake googletest support Commit bcefbdcdf6ad85046ccacee0aeffba5404d3e528 added support for building with CMake against a local googletest checkout, but I missed a line when constructing the diff. Change the `add_subdirectory` line to reference the correct directories. PiperOrigin-RevId: 302947488 -- 0a3c10fabf80a43ca69ab8b1570030e55f2be741 by Andy Soffer : Remove unused distribution format traits. PiperOrigin-RevId: 302896176 -- 0478f2f6270e5ed64c0e28ec09556ca90b2d46a9 by Samuel Benzaquen : Fix for CWG:2310. PiperOrigin-RevId: 302734089 -- 3cb978dda5cae5905affdc0914dcc2d27671ed11 by Samuel Benzaquen : Fix the Allocate/Deallocate functions to use the same underlying allocator type. PiperOrigin-RevId: 302721804 -- ae38d3984fb68b4e3ddc165fa8d5c24d5936be52 by Matthew Brown : Internal Change PiperOrigin-RevId: 302717314 -- 7357cf7abd03cc60b6e82b5f28a8e34935c3b4dc by Andy Getzendanner : Fix typo: s/ABSL_HARDENED_ASSERT/ABSL_HARDENING_ASSERT/ PiperOrigin-RevId: 302532164 GitOrigin-RevId: 990253454819ce26ff1dda9ab4bbc145b61d01e4 Change-Id: Ie595a221c16e1e7e1255ad42e029b646c5f3e11d --- CMake/Googletest/DownloadGTest.cmake | 4 +- absl/base/macros.h | 7 +- absl/container/internal/btree.h | 11 +- absl/container/internal/common.h | 3 +- absl/container/internal/container_memory.h | 7 +- absl/container/internal/container_memory_test.cc | 38 +++ absl/container/internal/raw_hash_set.h | 5 +- absl/flags/flag_test.cc | 24 +- absl/flags/internal/flag.cc | 82 +++--- absl/flags/internal/flag.h | 259 ++++++++++++------- absl/random/BUILD.bazel | 5 - absl/random/CMakeLists.txt | 3 - absl/random/bit_gen_ref.h | 5 +- absl/random/distribution_format_traits.h | 278 --------------------- absl/random/distributions.h | 37 +-- absl/random/internal/distribution_caller.h | 15 +- absl/random/internal/mocking_bit_gen_base.h | 39 +-- absl/random/mocking_bit_gen.cc | 30 --- absl/random/mocking_bit_gen.h | 8 +- absl/strings/BUILD.bazel | 1 + absl/strings/CMakeLists.txt | 1 + absl/strings/internal/str_format/arg.cc | 52 ++-- absl/strings/internal/str_format/arg.h | 16 +- absl/strings/internal/str_format/bind.cc | 2 +- absl/strings/internal/str_format/checker_test.cc | 10 +- .../internal/str_format/float_conversion.cc | 41 +-- absl/strings/internal/str_format/parser_test.cc | 2 +- absl/strings/numbers_test.cc | 1 + absl/strings/str_format_test.cc | 8 +- 29 files changed, 387 insertions(+), 607 deletions(-) delete mode 100644 absl/random/distribution_format_traits.h delete mode 100644 absl/random/mocking_bit_gen.cc (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/Googletest/DownloadGTest.cmake b/CMake/Googletest/DownloadGTest.cmake index 6552e1da..9d071c91 100644 --- a/CMake/Googletest/DownloadGTest.cmake +++ b/CMake/Googletest/DownloadGTest.cmake @@ -38,6 +38,4 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Add googletest directly to our build. This defines the gtest and gtest_main # targets. -add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src - ${CMAKE_BINARY_DIR}/googletest-build - EXCLUDE_FROM_ALL) +add_subdirectory(${absl_gtest_src_dir} ${absl_gtest_build_dir} EXCLUDE_FROM_ALL) diff --git a/absl/base/macros.h b/absl/base/macros.h index 2f6089f4..2c4e3570 100644 --- a/absl/base/macros.h +++ b/absl/base/macros.h @@ -212,7 +212,8 @@ ABSL_NAMESPACE_END // aborts the program in release mode (when NDEBUG is defined). The // implementation should abort the program as quickly as possible and ideally it // should not be possible to ignore the abort request. -#if ABSL_HAVE_BUILTIN(__builtin_trap) || \ +#if (ABSL_HAVE_BUILTIN(__builtin_trap) && \ + ABSL_HAVE_BUILTIN(__builtin_unreachable)) || \ (defined(__GNUC__) && !defined(__clang__)) #define ABSL_INTERNAL_HARDENING_ABORT() \ do { \ @@ -225,11 +226,11 @@ ABSL_NAMESPACE_END // ABSL_HARDENING_ASSERT() // -// `ABSL_HARDENED_ASSERT()` is like `ABSL_ASSERT()`, but used to implement +// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement // runtime assertions that should be enabled in hardened builds even when // `NDEBUG` is defined. // -// When `NDEBUG` is not defined, `ABSL_HARDENED_ASSERT()` is identical to +// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to // `ABSL_ASSERT()`. // // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index adf49f81..4504e9ce 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -937,8 +937,13 @@ struct btree_iterator { } // Accessors for the key/value the iterator is pointing at. - reference operator*() const { return node->value(position); } - pointer operator->() const { return &node->value(position); } + reference operator*() const { + ABSL_HARDENING_ASSERT(node != nullptr); + ABSL_HARDENING_ASSERT(node->start() <= position); + ABSL_HARDENING_ASSERT(node->finish() > position); + return node->value(position); + } + pointer operator->() const { return &operator*(); } btree_iterator &operator++() { increment(); @@ -1769,6 +1774,7 @@ void btree_iterator::increment_slow() { position = node->position(); node = node->parent(); } + // TODO(ezb): assert we aren't incrementing end() instead of handling. if (position == node->finish()) { *this = save; } @@ -1792,6 +1798,7 @@ void btree_iterator::decrement_slow() { position = node->position() - 1; node = node->parent(); } + // TODO(ezb): assert we aren't decrementing begin() instead of handling. if (position < node->start()) { *this = save; } diff --git a/absl/container/internal/common.h b/absl/container/internal/common.h index 5037d803..8990f294 100644 --- a/absl/container/internal/common.h +++ b/absl/container/internal/common.h @@ -138,6 +138,7 @@ class node_handle> : public node_handle_base { using Base = node_handle_base; + using slot_type = typename PolicyTraits::slot_type; public: using key_type = typename Policy::key_type; @@ -145,7 +146,7 @@ class node_handle decltype(PolicyTraits::key(this->slot())) { + auto key() const -> decltype(PolicyTraits::key(std::declval())) { return PolicyTraits::key(this->slot()); } diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h index d24b0f84..55b59c7f 100644 --- a/absl/container/internal/container_memory.h +++ b/absl/container/internal/container_memory.h @@ -37,6 +37,9 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace container_internal { +template +struct alignas(Alignment) AlignedType {}; + // Allocates at least n bytes aligned to the specified alignment. // Alignment must be a power of 2. It must be positive. // @@ -48,7 +51,7 @@ template void* Allocate(Alloc* alloc, size_t n) { static_assert(Alignment > 0, ""); assert(n && "n must be positive"); - struct alignas(Alignment) M {}; + using M = AlignedType; using A = typename absl::allocator_traits::template rebind_alloc; using AT = typename absl::allocator_traits::template rebind_traits; A mem_alloc(*alloc); @@ -64,7 +67,7 @@ template void Deallocate(Alloc* alloc, void* p, size_t n) { static_assert(Alignment > 0, ""); assert(n && "n must be positive"); - struct alignas(Alignment) M {}; + using M = AlignedType; using A = typename absl::allocator_traits::template rebind_alloc; using AT = typename absl::allocator_traits::template rebind_traits; A mem_alloc(*alloc); diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc index 7942c7be..e3262e3c 100644 --- a/absl/container/internal/container_memory_test.cc +++ b/absl/container/internal/container_memory_test.cc @@ -16,6 +16,8 @@ #include #include +#include +#include #include #include "gmock/gmock.h" @@ -27,6 +29,9 @@ ABSL_NAMESPACE_BEGIN namespace container_internal { namespace { +using ::testing::Gt; +using ::testing::_; +using ::testing::ElementsAre; using ::testing::Pair; TEST(Memory, AlignmentLargerThanBase) { @@ -45,6 +50,39 @@ TEST(Memory, AlignmentSmallerThanBase) { Deallocate<2>(&alloc, mem, 3); } +std::map& AllocationMap() { + static auto* map = new std::map; + return *map; +} + +template +struct TypeCountingAllocator { + TypeCountingAllocator() = default; + template + TypeCountingAllocator(const TypeCountingAllocator&) {} // NOLINT + + using value_type = T; + + T* allocate(size_t n, const void* = nullptr) { + AllocationMap()[typeid(T)] += n; + return std::allocator().allocate(n); + } + void deallocate(T* p, std::size_t n) { + AllocationMap()[typeid(T)] -= n; + return std::allocator().deallocate(p, n); + } +}; + +TEST(Memory, AllocateDeallocateMatchType) { + TypeCountingAllocator alloc; + void* mem = Allocate<1>(&alloc, 1); + // Verify that it was allocated + EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, Gt(0)))); + Deallocate<1>(&alloc, mem, 1); + // Verify that the deallocation matched. + EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, 0))); +} + class Fixture : public ::testing::Test { using Alloc = std::allocator; diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index fb47f62f..e47e1fed 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -104,6 +104,7 @@ #include "absl/base/internal/bits.h" #include "absl/base/internal/endian.h" +#include "absl/base/macros.h" #include "absl/base/port.h" #include "absl/container/internal/common.h" #include "absl/container/internal/compressed_tuple.h" @@ -651,9 +652,9 @@ class raw_hash_set { iterator(ctrl_t* ctrl) : ctrl_(ctrl) {} // for end() iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {} - void assert_is_full() const { assert(IsFull(*ctrl_)); } + void assert_is_full() const { ABSL_HARDENING_ASSERT(IsFull(*ctrl_)); } void assert_is_valid() const { - assert(!ctrl_ || IsFull(*ctrl_) || *ctrl_ == kSentinel); + ABSL_HARDENING_ASSERT(!ctrl_ || IsFull(*ctrl_) || *ctrl_ == kSentinel); } void skip_empty_or_deleted() { diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc index 3a025576..377e3b2b 100644 --- a/absl/flags/flag_test.cc +++ b/absl/flags/flag_test.cc @@ -91,30 +91,30 @@ struct S2 { }; TEST_F(FlagTest, Traits) { - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kOneWordAtomic); - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kOneWordAtomic); - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kOneWordAtomic); - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kOneWordAtomic); #if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD) - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kTwoWordsAtomic); - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kTwoWordsAtomic); #else - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kHeapAllocated); - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kHeapAllocated); #endif - EXPECT_EQ(flags::FlagValue::Kind(), + EXPECT_EQ(flags::StorageKind(), flags::FlagValueStorageKind::kHeapAllocated); - EXPECT_EQ(flags::FlagValue::Kind>(), + EXPECT_EQ(flags::StorageKind>(), flags::FlagValueStorageKind::kHeapAllocated); } @@ -624,10 +624,10 @@ TEST_F(FlagTest, TestNonDefaultConstructibleType) { EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag2).value, 25); } -// -------------------------------------------------------------------- - } // namespace +// -------------------------------------------------------------------- + ABSL_RETIRED_FLAG(bool, old_bool_flag, true, "old descr"); ABSL_RETIRED_FLAG(int, old_int_flag, (int)std::sqrt(10), "old descr"); ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr")); diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 56a5ed2b..5b4499ab 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -25,6 +25,7 @@ #include #include "absl/base/attributes.h" +#include "absl/base/casts.h" #include "absl/base/config.h" #include "absl/base/const_init.h" #include "absl/base/optimization.h" @@ -135,18 +136,18 @@ void FlagImpl::Init() { (*default_value_.gen_func)(), DynValueDeleter{op_}); switch (ValueStorageKind()) { case FlagValueStorageKind::kHeapAllocated: - value_.dynamic = init_value.release(); + HeapAllocatedValue() = init_value.release(); break; case FlagValueStorageKind::kOneWordAtomic: { int64_t atomic_value; - std::memcpy(&atomic_value, init_value.get(), flags_internal::Sizeof(op_)); - value_.one_word_atomic.store(atomic_value, std::memory_order_release); + std::memcpy(&atomic_value, init_value.get(), Sizeof(op_)); + OneWordValue().store(atomic_value, std::memory_order_release); break; } case FlagValueStorageKind::kTwoWordsAtomic: { AlignedTwoWords atomic_value{0, 0}; - std::memcpy(&atomic_value, init_value.get(), flags_internal::Sizeof(op_)); - value_.two_words_atomic.store(atomic_value, std::memory_order_release); + std::memcpy(&atomic_value, init_value.get(), Sizeof(op_)); + TwoWordsValue().store(atomic_value, std::memory_order_release); break; } } @@ -198,18 +199,18 @@ std::unique_ptr FlagImpl::MakeInitValue() const { void FlagImpl::StoreValue(const void* src) { switch (ValueStorageKind()) { case FlagValueStorageKind::kHeapAllocated: - flags_internal::Copy(op_, src, value_.dynamic); + Copy(op_, src, HeapAllocatedValue()); break; case FlagValueStorageKind::kOneWordAtomic: { - int64_t one_word_val; - std::memcpy(&one_word_val, src, flags_internal::Sizeof(op_)); - value_.one_word_atomic.store(one_word_val, std::memory_order_release); + int64_t one_word_val = 0; + std::memcpy(&one_word_val, src, Sizeof(op_)); + OneWordValue().store(one_word_val, std::memory_order_release); break; } case FlagValueStorageKind::kTwoWordsAtomic: { AlignedTwoWords two_words_val{0, 0}; - std::memcpy(&two_words_val, src, flags_internal::Sizeof(op_)); - value_.two_words_atomic.store(two_words_val, std::memory_order_release); + std::memcpy(&two_words_val, src, Sizeof(op_)); + TwoWordsValue().store(two_words_val, std::memory_order_release); break; } } @@ -258,17 +259,19 @@ std::string FlagImpl::CurrentValue() const { switch (ValueStorageKind()) { case FlagValueStorageKind::kHeapAllocated: { absl::MutexLock l(guard); - return flags_internal::Unparse(op_, value_.dynamic); + return flags_internal::Unparse(op_, HeapAllocatedValue()); } case FlagValueStorageKind::kOneWordAtomic: { const auto one_word_val = - value_.one_word_atomic.load(std::memory_order_acquire); - return flags_internal::Unparse(op_, &one_word_val); + absl::bit_cast>( + OneWordValue().load(std::memory_order_acquire)); + return flags_internal::Unparse(op_, one_word_val.data()); } case FlagValueStorageKind::kTwoWordsAtomic: { const auto two_words_val = - value_.two_words_atomic.load(std::memory_order_acquire); - return flags_internal::Unparse(op_, &two_words_val); + absl::bit_cast>( + TwoWordsValue().load(std::memory_order_acquire)); + return flags_internal::Unparse(op_, two_words_val.data()); } } @@ -317,18 +320,18 @@ std::unique_ptr FlagImpl::SaveState() { switch (ValueStorageKind()) { case FlagValueStorageKind::kHeapAllocated: { return absl::make_unique( - this, flags_internal::Clone(op_, value_.dynamic), modified, + this, flags_internal::Clone(op_, HeapAllocatedValue()), modified, on_command_line, counter_); } case FlagValueStorageKind::kOneWordAtomic: { return absl::make_unique( - this, value_.one_word_atomic.load(std::memory_order_acquire), - modified, on_command_line, counter_); + this, OneWordValue().load(std::memory_order_acquire), modified, + on_command_line, counter_); } case FlagValueStorageKind::kTwoWordsAtomic: { return absl::make_unique( - this, value_.two_words_atomic.load(std::memory_order_acquire), - modified, on_command_line, counter_); + this, TwoWordsValue().load(std::memory_order_acquire), modified, + on_command_line, counter_); } } return nullptr; @@ -359,6 +362,28 @@ bool FlagImpl::RestoreState(const FlagState& flag_state) { return true; } +template +typename StorageT::value_type& FlagImpl::OffsetValue() const { + char* p = reinterpret_cast(const_cast(this)); + // The offset is deduced via Flag value type specific op_. + size_t offset = flags_internal::ValueOffset(op_); + + return reinterpret_cast(p + offset)->value; +} + +void*& FlagImpl::HeapAllocatedValue() const { + assert(ValueStorageKind() == FlagValueStorageKind::kHeapAllocated); + return OffsetValue(); +} +std::atomic& FlagImpl::OneWordValue() const { + assert(ValueStorageKind() == FlagValueStorageKind::kOneWordAtomic); + return OffsetValue(); +} +std::atomic& FlagImpl::TwoWordsValue() const { + assert(ValueStorageKind() == FlagValueStorageKind::kTwoWordsAtomic); + return OffsetValue(); +} + // Attempts to parse supplied `value` string using parsing routine in the `flag` // argument. If parsing successful, this function replaces the dst with newly // parsed value. In case if any error is encountered in either step, the error @@ -383,20 +408,19 @@ void FlagImpl::Read(void* dst) const { switch (ValueStorageKind()) { case FlagValueStorageKind::kHeapAllocated: { absl::MutexLock l(guard); - - flags_internal::CopyConstruct(op_, value_.dynamic, dst); + flags_internal::CopyConstruct(op_, HeapAllocatedValue(), dst); break; } case FlagValueStorageKind::kOneWordAtomic: { - const auto one_word_val = - value_.one_word_atomic.load(std::memory_order_acquire); - std::memcpy(dst, &one_word_val, flags_internal::Sizeof(op_)); + const int64_t one_word_val = + OneWordValue().load(std::memory_order_acquire); + std::memcpy(dst, &one_word_val, Sizeof(op_)); break; } case FlagValueStorageKind::kTwoWordsAtomic: { - const auto two_words_val = - value_.two_words_atomic.load(std::memory_order_acquire); - std::memcpy(dst, &two_words_val, flags_internal::Sizeof(op_)); + const AlignedTwoWords two_words_val = + TwoWordsValue().load(std::memory_order_acquire); + std::memcpy(dst, &two_words_val, Sizeof(op_)); break; } } diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index f27e558b..19119bbb 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -53,56 +53,13 @@ enum class FlagOp { kStaticTypeId, kParse, kUnparse, + kValueOffset, }; using FlagOpFn = void* (*)(FlagOp, const void*, void*, void*); -// Flag value specific operations routine. +// Forward declaration for Flag value specific operations. template -void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) { - switch (op) { - case FlagOp::kDelete: - delete static_cast(v1); - return nullptr; - case FlagOp::kClone: - return new T(*static_cast(v1)); - case FlagOp::kCopy: - *static_cast(v2) = *static_cast(v1); - return nullptr; - case FlagOp::kCopyConstruct: - new (v2) T(*static_cast(v1)); - return nullptr; - case FlagOp::kSizeof: - return reinterpret_cast(sizeof(T)); - case FlagOp::kStaticTypeId: { - auto* static_id = &FlagStaticTypeIdGen; - - // Cast from function pointer to void* is not portable. - // We don't have an easy way to work around this, but it works fine - // on all the platforms we test and as long as size of pointers match - // we should be fine to do reinterpret cast. - static_assert(sizeof(void*) == sizeof(static_id), - "Flag's static type id does not work on this platform"); - return reinterpret_cast(static_id); - } - case FlagOp::kParse: { - // Initialize the temporary instance of type T based on current value in - // destination (which is going to be flag's default value). - T temp(*static_cast(v2)); - if (!absl::ParseFlag(*static_cast(v1), &temp, - static_cast(v3))) { - return nullptr; - } - *static_cast(v2) = std::move(temp); - return v2; - } - case FlagOp::kUnparse: - *static_cast(v2) = - absl::UnparseFlag(*static_cast(v1)); - return nullptr; - default: - return nullptr; - } -} +void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3); // Deletes memory interpreting obj as flag value type pointer. inline void Delete(FlagOpFn op, const void* obj) { @@ -144,6 +101,16 @@ inline FlagStaticTypeId StaticTypeId(FlagOpFn op) { return reinterpret_cast( op(FlagOp::kStaticTypeId, nullptr, nullptr, nullptr)); } +// Returns offset of the field value_ from the field impl_ inside of +// absl::Flag data. Given FlagImpl pointer p you can get the +// location of the corresponding value as: +// reinterpret_cast(p) + ValueOffset(). +inline ptrdiff_t ValueOffset(FlagOpFn op) { + // This sequence of casts reverses the sequence from + // `flags_internal::FlagOps()` + return static_cast(reinterpret_cast( + op(FlagOp::kValueOffset, nullptr, nullptr, nullptr))); +} /////////////////////////////////////////////////////////////////////////////// // Flag help auxiliary structs. @@ -239,6 +206,10 @@ using FlagUseOneWordStorage = std::integral_constant< struct alignas(16) AlignedTwoWords { int64_t first; int64_t second; + + bool IsInitialized() const { + return first != flags_internal::UninitializedFlagValue(); + } }; template @@ -248,8 +219,14 @@ using FlagUseTwoWordsStorage = std::integral_constant< #else // This is actually unused and only here to avoid ifdefs in other palces. struct AlignedTwoWords { - constexpr AlignedTwoWords() = default; - constexpr AlignedTwoWords(int64_t, int64_t) {} + constexpr AlignedTwoWords() noexcept : dummy() {} + constexpr AlignedTwoWords(int64_t, int64_t) noexcept : dummy() {} + char dummy; + + bool IsInitialized() const { + std::abort(); + return true; + } }; // This trait should be type dependent, otherwise SFINAE below will fail @@ -269,23 +246,70 @@ enum class FlagValueStorageKind : uint8_t { kTwoWordsAtomic = 2 }; -union FlagValue { - constexpr explicit FlagValue(int64_t v) : one_word_atomic(v) {} +template +static constexpr FlagValueStorageKind StorageKind() { + return FlagUseHeapStorage::value + ? FlagValueStorageKind::kHeapAllocated + : FlagUseOneWordStorage::value + ? FlagValueStorageKind::kOneWordAtomic + : FlagUseTwoWordsStorage::value + ? FlagValueStorageKind::kTwoWordsAtomic + : FlagValueStorageKind::kHeapAllocated; +} + +struct FlagHeapAllocatedValue { + using value_type = void*; + + value_type value; +}; + +struct FlagOneWordValue { + using value_type = std::atomic; + constexpr FlagOneWordValue() : value(UninitializedFlagValue()) {} - template - static constexpr FlagValueStorageKind Kind() { - return FlagUseHeapStorage::value - ? FlagValueStorageKind::kHeapAllocated - : FlagUseOneWordStorage::value - ? FlagValueStorageKind::kOneWordAtomic - : FlagUseTwoWordsStorage::value - ? FlagValueStorageKind::kTwoWordsAtomic - : FlagValueStorageKind::kHeapAllocated; + value_type value; +}; + +struct FlagTwoWordsValue { + using value_type = std::atomic; + constexpr FlagTwoWordsValue() + : value(AlignedTwoWords{UninitializedFlagValue(), 0}) {} + + value_type value; +}; + +template ()> +struct FlagValue; + +template +struct FlagValue + : FlagHeapAllocatedValue { + bool Get(T*) const { return false; } +}; + +template +struct FlagValue : FlagOneWordValue { + bool Get(T* dst) const { + int64_t one_word_val = value.load(std::memory_order_acquire); + if (ABSL_PREDICT_FALSE(one_word_val == UninitializedFlagValue())) { + return false; + } + std::memcpy(dst, static_cast(&one_word_val), sizeof(T)); + return true; } +}; - void* dynamic; - std::atomic one_word_atomic; - std::atomic two_words_atomic; +template +struct FlagValue : FlagTwoWordsValue { + bool Get(T* dst) const { + AlignedTwoWords two_words_val = value.load(std::memory_order_acquire); + if (ABSL_PREDICT_FALSE(!two_words_val.IsInitialized())) { + return false; + } + std::memcpy(dst, static_cast(&two_words_val), sizeof(T)); + return true; + } }; /////////////////////////////////////////////////////////////////////////////// @@ -333,35 +357,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag { counter_(0), callback_(nullptr), default_value_(default_value_gen), - value_(flags_internal::UninitializedFlagValue()), data_guard_{} {} // Constant access methods void Read(void* dst) const override ABSL_LOCKS_EXCLUDED(*DataGuard()); - template ::value, - int>::type = 0> - void Get(T* dst) const { - Read(dst); - } - template ::value, - int>::type = 0> - void Get(T* dst) const { - int64_t one_word_val = - value_.one_word_atomic.load(std::memory_order_acquire); - if (ABSL_PREDICT_FALSE(one_word_val == UninitializedFlagValue())) { - DataGuard(); // Make sure flag initialized - one_word_val = value_.one_word_atomic.load(std::memory_order_acquire); - } - std::memcpy(dst, static_cast(&one_word_val), sizeof(T)); - } - template ::value, int>::type = 0> - void Get(T* dst) const { - DataGuard(); // Make sure flag initialized - const auto two_words_val = - value_.two_words_atomic.load(std::memory_order_acquire); - std::memcpy(dst, &two_words_val, sizeof(T)); - } // Mutating access methods void Write(const void* src) ABSL_LOCKS_EXCLUDED(*DataGuard()); @@ -391,6 +390,25 @@ class FlagImpl final : public flags_internal::CommandLineFlag { ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); // Flag initialization called via absl::call_once. void Init(); + + // Offset value access methods. One per storage kind. These methods to not + // respect const correctness, so be very carefull using them. + + // This is a shared helper routine which encapsulates most of the magic. Since + // it is only used inside the three routines below, which are defined in + // flag.cc, we can define it in that file as well. + template + typename StorageT::value_type& OffsetValue() const; + // This is an accessor for a value stored in heap allocated storage. + // Returns a mutable reference to a pointer to allow vlaue mutation. + void*& HeapAllocatedValue() const; + // This is an accessor for a value stored as one word atomic. Returns a + // mutable reference to an atomic value. + std::atomic& OneWordValue() const; + // This is an accessor for a value stored as two words atomic. Returns a + // mutable reference to an atomic value. + std::atomic& TwoWordsValue() const; + // Attempts to parse supplied `value` string. If parsing is successful, // returns new value. Otherwise returns nullptr. std::unique_ptr TryParse(absl::string_view value, @@ -488,13 +506,6 @@ class FlagImpl final : public flags_internal::CommandLineFlag { // these two cases. FlagDefaultSrc default_value_; - // Atomically mutable flag's state - - // Flag's value. This can be either the atomically stored small value or - // pointer to the heap allocated dynamic value. value_storage_kind_ is used - // to distinguish these cases. - FlagValue value_; - // This is reserved space for an absl::Mutex to guard flag data. It will be // initialized in FlagImpl::Init via placement new. // We can't use "absl::Mutex data_guard_", since this class is not literal. @@ -514,8 +525,9 @@ class Flag { public: constexpr Flag(const char* name, const char* filename, const FlagHelpArg help, const FlagDfltGenFunc default_value_gen) - : impl_(name, filename, &FlagOps, help, FlagValue::Kind(), - default_value_gen) {} + : impl_(name, filename, &FlagOps, help, + flags_internal::StorageKind(), default_value_gen), + value_() {} T Get() const { // See implementation notes in CommandLineFlag::Get(). @@ -530,7 +542,7 @@ class Flag { impl_.AssertValidType(&flags_internal::FlagStaticTypeIdGen); #endif - impl_.Get(&u.value); + if (!value_.Get(&u.value)) impl_.Read(&u.value); return std::move(u.value); } void Set(const T& v) { @@ -556,10 +568,63 @@ class Flag { private: template friend class FlagRegistrar; + // Flag's data + // The implementation depends on value_ field to be placed exactly after the + // impl_ field, so that impl_ can figure out the offset to the value and + // access it. FlagImpl impl_; + FlagValue value_; }; +/////////////////////////////////////////////////////////////////////////////// +// Implementation of Flag value specific operations routine. +template +void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) { + switch (op) { + case FlagOp::kDelete: + delete static_cast(v1); + return nullptr; + case FlagOp::kClone: + return new T(*static_cast(v1)); + case FlagOp::kCopy: + *static_cast(v2) = *static_cast(v1); + return nullptr; + case FlagOp::kCopyConstruct: + new (v2) T(*static_cast(v1)); + return nullptr; + case FlagOp::kSizeof: + return reinterpret_cast(static_cast(sizeof(T))); + case FlagOp::kStaticTypeId: + return reinterpret_cast(&FlagStaticTypeIdGen); + case FlagOp::kParse: { + // Initialize the temporary instance of type T based on current value in + // destination (which is going to be flag's default value). + T temp(*static_cast(v2)); + if (!absl::ParseFlag(*static_cast(v1), &temp, + static_cast(v3))) { + return nullptr; + } + *static_cast(v2) = std::move(temp); + return v2; + } + case FlagOp::kUnparse: + *static_cast(v2) = + absl::UnparseFlag(*static_cast(v1)); + return nullptr; + case FlagOp::kValueOffset: { + // Round sizeof(FlagImp) to a multiple of alignof(FlagValue) to get the + // offset of the data. + ptrdiff_t round_to = alignof(FlagValue); + ptrdiff_t offset = + (sizeof(FlagImpl) + round_to - 1) / round_to * round_to; + return reinterpret_cast(offset); + } + } + return nullptr; +} + +/////////////////////////////////////////////////////////////////////////////// // This class facilitates Flag object registration and tail expression-based // flag definition, for example: // ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher); diff --git a/absl/random/BUILD.bazel b/absl/random/BUILD.bazel index f78fbc7e..4d94e1ba 100644 --- a/absl/random/BUILD.bazel +++ b/absl/random/BUILD.bazel @@ -53,7 +53,6 @@ cc_library( "bernoulli_distribution.h", "beta_distribution.h", "discrete_distribution.h", - "distribution_format_traits.h", "distributions.h", "exponential_distribution.h", "gaussian_distribution.h", @@ -141,16 +140,12 @@ cc_library( cc_library( name = "mocking_bit_gen", testonly = 1, - srcs = [ - "mocking_bit_gen.cc", - ], hdrs = [ "mocking_bit_gen.h", ], linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ ":distributions", - "//absl/base:raw_logging_internal", "//absl/container:flat_hash_map", "//absl/meta:type_traits", "//absl/random/internal:distribution_caller", diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index efa55d8f..69bedbd6 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt @@ -102,8 +102,6 @@ absl_cc_library( HDRS "mock_distributions.h" "mocking_bit_gen.h" - SRCS - "mocking_bit_gen.cc" COPTS ${ABSL_DEFAULT_COPTS} LINKOPTS @@ -168,7 +166,6 @@ absl_cc_library( "bernoulli_distribution.h" "beta_distribution.h" "discrete_distribution.h" - "distribution_format_traits.h" "distributions.h" "exponential_distribution.h" "gaussian_distribution.h" diff --git a/absl/random/bit_gen_ref.h b/absl/random/bit_gen_ref.h index e8771162..59591a47 100644 --- a/absl/random/bit_gen_ref.h +++ b/absl/random/bit_gen_ref.h @@ -132,7 +132,7 @@ namespace random_internal { template <> struct DistributionCaller { - template + template static typename DistrT::result_type Call(absl::BitGenRef* gen_ref, Args&&... args) { auto* mock_ptr = gen_ref->mocked_gen_ptr_; @@ -140,8 +140,7 @@ struct DistributionCaller { DistrT dist(std::forward(args)...); return dist(*gen_ref); } else { - return mock_ptr->template Call( - std::forward(args)...); + return mock_ptr->template Call(std::forward(args)...); } } }; diff --git a/absl/random/distribution_format_traits.h b/absl/random/distribution_format_traits.h deleted file mode 100644 index 22b358cc..00000000 --- a/absl/random/distribution_format_traits.h +++ /dev/null @@ -1,278 +0,0 @@ -// -// Copyright 2018 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#ifndef ABSL_RANDOM_DISTRIBUTION_FORMAT_TRAITS_H_ -#define ABSL_RANDOM_DISTRIBUTION_FORMAT_TRAITS_H_ - -#include -#include -#include - -#include "absl/meta/type_traits.h" -#include "absl/random/bernoulli_distribution.h" -#include "absl/random/beta_distribution.h" -#include "absl/random/exponential_distribution.h" -#include "absl/random/gaussian_distribution.h" -#include "absl/random/log_uniform_int_distribution.h" -#include "absl/random/poisson_distribution.h" -#include "absl/random/uniform_int_distribution.h" -#include "absl/random/uniform_real_distribution.h" -#include "absl/random/zipf_distribution.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/str_join.h" -#include "absl/strings/string_view.h" -#include "absl/types/span.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN - -struct IntervalClosedClosedTag; -struct IntervalClosedOpenTag; -struct IntervalOpenClosedTag; -struct IntervalOpenOpenTag; - -namespace random_internal { - -// ScalarTypeName defines a preferred hierarchy of preferred type names for -// scalars, and is evaluated at compile time for the specific type -// specialization. -template -constexpr const char* ScalarTypeName() { - static_assert(std::is_integral() || std::is_floating_point(), ""); - // clang-format off - return - std::is_same::value ? "float" : - std::is_same::value ? "double" : - std::is_same::value ? "long double" : - std::is_same::value ? "bool" : - std::is_signed::value && sizeof(T) == 1 ? "int8_t" : - std::is_signed::value && sizeof(T) == 2 ? "int16_t" : - std::is_signed::value && sizeof(T) == 4 ? "int32_t" : - std::is_signed::value && sizeof(T) == 8 ? "int64_t" : - std::is_unsigned::value && sizeof(T) == 1 ? "uint8_t" : - std::is_unsigned::value && sizeof(T) == 2 ? "uint16_t" : - std::is_unsigned::value && sizeof(T) == 4 ? "uint32_t" : - std::is_unsigned::value && sizeof(T) == 8 ? "uint64_t" : - "undefined"; - // clang-format on - - // NOTE: It would be nice to use typeid(T).name(), but that's an - // implementation-defined attribute which does not necessarily - // correspond to a name. We could potentially demangle it - // using, e.g. abi::__cxa_demangle. -} - -// Distribution traits used by DistributionCaller and internal implementation -// details of the mocking framework. -/* -struct DistributionFormatTraits { - // Returns the parameterized name of the distribution function. - static constexpr const char* FunctionName() - // Format DistrT parameters. - static std::string FormatArgs(DistrT& dist); - // Format DistrT::result_type results. - static std::string FormatResults(DistrT& dist); -}; -*/ -template -struct DistributionFormatTraits; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::uniform_int_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Uniform"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat("absl::IntervalClosedClosed, ", (d.min)(), ", ", - (d.max)()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::uniform_real_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Uniform"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat((d.min)(), ", ", (d.max)()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::exponential_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Exponential"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat(d.lambda()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::poisson_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Poisson"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat(d.mean()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template <> -struct DistributionFormatTraits { - using distribution_t = absl::bernoulli_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Bernoulli"; } - - static constexpr const char* FunctionName() { return Name(); } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat(d.p()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::beta_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Beta"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat(d.alpha(), ", ", d.beta()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::zipf_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Zipf"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat(d.k(), ", ", d.v(), ", ", d.q()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::gaussian_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "Gaussian"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrJoin(std::make_tuple(d.mean(), d.stddev()), ", "); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct DistributionFormatTraits> { - using distribution_t = absl::log_uniform_int_distribution; - using result_t = typename distribution_t::result_type; - - static constexpr const char* Name() { return "LogUniform"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrJoin(std::make_tuple((d.min)(), (d.max)(), d.base()), ", "); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -template -struct UniformDistributionWrapper; - -template -struct DistributionFormatTraits> { - using distribution_t = UniformDistributionWrapper; - using result_t = NumType; - - static constexpr const char* Name() { return "Uniform"; } - - static std::string FunctionName() { - return absl::StrCat(Name(), "<", ScalarTypeName(), ">"); - } - static std::string FormatArgs(const distribution_t& d) { - return absl::StrCat((d.min)(), ", ", (d.max)()); - } - static std::string FormatResults(absl::Span results) { - return absl::StrJoin(results, ", "); - } -}; - -} // namespace random_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_RANDOM_DISTRIBUTION_FORMAT_TRAITS_H_ diff --git a/absl/random/distributions.h b/absl/random/distributions.h index d026d92b..8680f6a6 100644 --- a/absl/random/distributions.h +++ b/absl/random/distributions.h @@ -55,7 +55,6 @@ #include "absl/base/internal/inline_variable.h" #include "absl/random/bernoulli_distribution.h" #include "absl/random/beta_distribution.h" -#include "absl/random/distribution_format_traits.h" #include "absl/random/exponential_distribution.h" #include "absl/random/gaussian_distribution.h" #include "absl/random/internal/distributions.h" // IWYU pragma: export @@ -126,14 +125,13 @@ Uniform(TagType tag, R lo, R hi) { using gen_t = absl::decay_t; using distribution_t = random_internal::UniformDistributionWrapper; - using format_t = random_internal::DistributionFormatTraits; auto a = random_internal::uniform_lower_bound(tag, lo, hi); auto b = random_internal::uniform_upper_bound(tag, lo, hi); if (a > b) return a; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, tag, lo, hi); + distribution_t>(&urbg, tag, lo, hi); } // absl::Uniform(bitgen, lo, hi) @@ -146,7 +144,6 @@ Uniform(URBG&& urbg, // NOLINT(runtime/references) R lo, R hi) { using gen_t = absl::decay_t; using distribution_t = random_internal::UniformDistributionWrapper; - using format_t = random_internal::DistributionFormatTraits; constexpr auto tag = absl::IntervalClosedOpen; auto a = random_internal::uniform_lower_bound(tag, lo, hi); @@ -154,7 +151,7 @@ Uniform(URBG&& urbg, // NOLINT(runtime/references) if (a > b) return a; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, lo, hi); + distribution_t>(&urbg, lo, hi); } // absl::Uniform(tag, bitgen, lo, hi) @@ -172,14 +169,13 @@ Uniform(TagType tag, using gen_t = absl::decay_t; using return_t = typename random_internal::uniform_inferred_return_t; using distribution_t = random_internal::UniformDistributionWrapper; - using format_t = random_internal::DistributionFormatTraits; auto a = random_internal::uniform_lower_bound(tag, lo, hi); auto b = random_internal::uniform_upper_bound(tag, lo, hi); if (a > b) return a; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, tag, static_cast(lo), + distribution_t>(&urbg, tag, static_cast(lo), static_cast(hi)); } @@ -196,7 +192,6 @@ Uniform(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using return_t = typename random_internal::uniform_inferred_return_t; using distribution_t = random_internal::UniformDistributionWrapper; - using format_t = random_internal::DistributionFormatTraits; constexpr auto tag = absl::IntervalClosedOpen; auto a = random_internal::uniform_lower_bound(tag, lo, hi); @@ -204,7 +199,7 @@ Uniform(URBG&& urbg, // NOLINT(runtime/references) if (a > b) return a; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, static_cast(lo), + distribution_t>(&urbg, static_cast(lo), static_cast(hi)); } @@ -217,10 +212,9 @@ typename absl::enable_if_t::value, R> // Uniform(URBG&& urbg) { // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = random_internal::UniformDistributionWrapper; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg); + distribution_t>(&urbg); } // ----------------------------------------------------------------------------- @@ -248,10 +242,9 @@ bool Bernoulli(URBG&& urbg, // NOLINT(runtime/references) double p) { using gen_t = absl::decay_t; using distribution_t = absl::bernoulli_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, p); + distribution_t>(&urbg, p); } // ----------------------------------------------------------------------------- @@ -281,10 +274,9 @@ RealType Beta(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::beta_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, alpha, beta); + distribution_t>(&urbg, alpha, beta); } // ----------------------------------------------------------------------------- @@ -314,10 +306,9 @@ RealType Exponential(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::exponential_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, lambda); + distribution_t>(&urbg, lambda); } // ----------------------------------------------------------------------------- @@ -346,10 +337,9 @@ RealType Gaussian(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::gaussian_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, mean, stddev); + distribution_t>(&urbg, mean, stddev); } // ----------------------------------------------------------------------------- @@ -389,10 +379,9 @@ IntType LogUniform(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::log_uniform_int_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, lo, hi, base); + distribution_t>(&urbg, lo, hi, base); } // ----------------------------------------------------------------------------- @@ -420,10 +409,9 @@ IntType Poisson(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::poisson_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, mean); + distribution_t>(&urbg, mean); } // ----------------------------------------------------------------------------- @@ -453,10 +441,9 @@ IntType Zipf(URBG&& urbg, // NOLINT(runtime/references) using gen_t = absl::decay_t; using distribution_t = typename absl::zipf_distribution; - using format_t = random_internal::DistributionFormatTraits; return random_internal::DistributionCaller::template Call< - distribution_t, format_t>(&urbg, hi, q, v); + distribution_t>(&urbg, hi, q, v); } ABSL_NAMESPACE_END diff --git a/absl/random/internal/distribution_caller.h b/absl/random/internal/distribution_caller.h index 02603cf8..4e072444 100644 --- a/absl/random/internal/distribution_caller.h +++ b/absl/random/internal/distribution_caller.h @@ -33,20 +33,7 @@ struct DistributionCaller { // Call the provided distribution type. The parameters are expected // to be explicitly specified. // DistrT is the distribution type. - // FormatT is the formatter type: - // - // struct FormatT { - // using result_type = distribution_t::result_type; - // static std::string FormatCall( - // const distribution_t& distr, - // absl::Span); - // - // static std::string FormatExpectation( - // absl::string_view match_args, - // absl::Span results); - // } - // - template + template static typename DistrT::result_type Call(URBG* urbg, Args&&... args) { DistrT dist(std::forward(args)...); return dist(*urbg); diff --git a/absl/random/internal/mocking_bit_gen_base.h b/absl/random/internal/mocking_bit_gen_base.h index eeeae9d2..23ecaf6c 100644 --- a/absl/random/internal/mocking_bit_gen_base.h +++ b/absl/random/internal/mocking_bit_gen_base.h @@ -16,8 +16,6 @@ #ifndef ABSL_RANDOM_INTERNAL_MOCKING_BIT_GEN_BASE_H_ #define ABSL_RANDOM_INTERNAL_MOCKING_BIT_GEN_BASE_H_ -#include -#include #include #include @@ -28,27 +26,6 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace random_internal { -// MockingBitGenExpectationFormatter is invoked to format unsatisfied mocks -// and remaining results into a description string. -template -struct MockingBitGenExpectationFormatter { - std::string operator()(absl::string_view args) { - return absl::StrCat(FormatT::FunctionName(), "(", args, ")"); - } -}; - -// MockingBitGenCallFormatter is invoked to format each distribution call -// into a description string for the mock log. -template -struct MockingBitGenCallFormatter { - std::string operator()(const DistrT& dist, - const typename DistrT::result_type& result) { - return absl::StrCat( - FormatT::FunctionName(), "(", FormatT::FormatArgs(dist), ") => {", - FormatT::FormatResults(absl::MakeSpan(&result, 1)), "}"); - } -}; - class MockingBitGenBase { template friend struct DistributionCaller; @@ -61,14 +38,9 @@ class MockingBitGenBase { static constexpr result_type(max)() { return (generator_type::max)(); } result_type operator()() { return gen_(); } - MockingBitGenBase() : gen_(), observed_call_log_() {} virtual ~MockingBitGenBase() = default; protected: - const std::deque& observed_call_log() { - return observed_call_log_; - } - // CallImpl is the type-erased virtual dispatch. // The type of dist is always distribution, // The type of result is always distribution::result_type. @@ -81,10 +53,9 @@ class MockingBitGenBase { } // Call the generating distribution function. - // Invoked by DistributionCaller<>::Call. + // Invoked by DistributionCaller<>::Call. // DistT is the distribution type. - // FormatT is the distribution formatter traits type. - template + template typename DistrT::result_type Call(Args&&... args) { using distr_result_type = typename DistrT::result_type; using ArgTupleT = std::tuple...>; @@ -100,17 +71,11 @@ class MockingBitGenBase { result = dist(gen_); } - // TODO(asoffer): Forwarding the args through means we no longer need to - // extract them from the from the distribution in formatter traits. We can - // just StrJoin them. - observed_call_log_.push_back( - MockingBitGenCallFormatter{}(dist, result)); return result; } private: generator_type gen_; - std::deque observed_call_log_; }; // namespace random_internal } // namespace random_internal diff --git a/absl/random/mocking_bit_gen.cc b/absl/random/mocking_bit_gen.cc deleted file mode 100644 index 6bb1e414..00000000 --- a/absl/random/mocking_bit_gen.cc +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright 2018 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#include "absl/random/mocking_bit_gen.h" - -#include - -namespace absl { -ABSL_NAMESPACE_BEGIN -MockingBitGen::~MockingBitGen() { - - for (const auto& del : deleters_) { - del(); - } -} - -ABSL_NAMESPACE_END -} // namespace absl diff --git a/absl/random/mocking_bit_gen.h b/absl/random/mocking_bit_gen.h index 36cef911..3d8a979e 100644 --- a/absl/random/mocking_bit_gen.h +++ b/absl/random/mocking_bit_gen.h @@ -100,7 +100,9 @@ class MockingBitGen : public absl::random_internal::MockingBitGenBase { public: MockingBitGen() {} - ~MockingBitGen() override; + ~MockingBitGen() override { + for (const auto& del : deleters_) del(); + } private: template @@ -182,10 +184,10 @@ namespace random_internal { template <> struct DistributionCaller { - template + template static typename DistrT::result_type Call(absl::MockingBitGen* gen, Args&&... args) { - return gen->template Call(std::forward(args)...); + return gen->template Call(std::forward(args)...); } }; diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index e72db82c..64f55fb4 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -485,6 +485,7 @@ cc_test( copts = ABSL_TEST_COPTS, visibility = ["//visibility:private"], deps = [ + ":internal", ":pow10_helper", ":strings", "//absl/base:config", diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt index 668d722b..c7874ecf 100644 --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -284,6 +284,7 @@ absl_cc_test( absl::raw_logging_internal absl::random_random absl::random_distributions + absl::strings_internal gmock_main ) diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index 4d0604e0..3aa0296b 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -120,24 +120,25 @@ class ConvertedIntInfo { // the '#' flag is specified to modify the precision for 'o' conversions. string_view BaseIndicator(const ConvertedIntInfo &info, const ConversionSpec conv) { - bool alt = conv.flags().alt; - int radix = FormatConversionCharRadix(conv.conv()); - if (conv.conv() == ConversionChar::p) alt = true; // always show 0x for %p. + bool alt = conv.has_alt_flag(); + int radix = FormatConversionCharRadix(conv.conversion_char()); + if (conv.conversion_char() == ConversionChar::p) + alt = true; // always show 0x for %p. // From the POSIX description of '#' flag: // "For x or X conversion specifiers, a non-zero result shall have // 0x (or 0X) prefixed to it." if (alt && radix == 16 && !info.digits().empty()) { - if (FormatConversionCharIsUpper(conv.conv())) return "0X"; + if (FormatConversionCharIsUpper(conv.conversion_char())) return "0X"; return "0x"; } return {}; } string_view SignColumn(bool neg, const ConversionSpec conv) { - if (FormatConversionCharIsSigned(conv.conv())) { + if (FormatConversionCharIsSigned(conv.conversion_char())) { if (neg) return "-"; - if (conv.flags().show_pos) return "+"; - if (conv.flags().sign_col) return " "; + if (conv.has_show_pos_flag()) return "+"; + if (conv.has_sign_col_flag()) return " "; } return {}; } @@ -147,9 +148,9 @@ bool ConvertCharImpl(unsigned char v, const ConversionSpec conv, size_t fill = 0; if (conv.width() >= 0) fill = conv.width(); ReducePadding(1, &fill); - if (!conv.flags().left) sink->Append(fill, ' '); + if (!conv.has_left_flag()) sink->Append(fill, ' '); sink->Append(1, v); - if (conv.flags().left) sink->Append(fill, ' '); + if (conv.has_left_flag()) sink->Append(fill, ' '); return true; } @@ -174,7 +175,7 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, if (!precision_specified) precision = 1; - if (conv.flags().alt && conv.conv() == ConversionChar::o) { + if (conv.has_alt_flag() && conv.conversion_char() == ConversionChar::o) { // From POSIX description of the '#' (alt) flag: // "For o conversion, it increases the precision (if necessary) to // force the first digit of the result to be zero." @@ -187,13 +188,13 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, size_t num_zeroes = Excess(formatted.size(), precision); ReducePadding(num_zeroes, &fill); - size_t num_left_spaces = !conv.flags().left ? fill : 0; - size_t num_right_spaces = conv.flags().left ? fill : 0; + size_t num_left_spaces = !conv.has_left_flag() ? fill : 0; + size_t num_right_spaces = conv.has_left_flag() ? fill : 0; // From POSIX description of the '0' (zero) flag: // "For d, i, o, u, x, and X conversion specifiers, if a precision // is specified, the '0' flag is ignored." - if (!precision_specified && conv.flags().zero) { + if (!precision_specified && conv.has_zero_flag()) { num_zeroes += num_left_spaces; num_left_spaces = 0; } @@ -209,8 +210,8 @@ bool ConvertIntImplInner(const ConvertedIntInfo &info, template bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) { - ConvertedIntInfo info(v, conv.conv()); - if (conv.flags().basic && (conv.conv() != ConversionChar::p)) { + ConvertedIntInfo info(v, conv.conversion_char()); + if (conv.is_basic() && (conv.conversion_char() != ConversionChar::p)) { if (info.is_neg()) sink->Append(1, '-'); if (info.digits().empty()) { sink->Append(1, '0'); @@ -224,13 +225,14 @@ bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) { template bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (FormatConversionCharIsFloat(conv.conv())) { + if (FormatConversionCharIsFloat(conv.conversion_char())) { return FormatConvertImpl(static_cast(v), conv, sink).value; } - if (conv.conv() == ConversionChar::c) + if (conv.conversion_char() == ConversionChar::c) return ConvertCharImpl(static_cast(v), conv, sink); - if (!FormatConversionCharIsIntegral(conv.conv())) return false; - if (!FormatConversionCharIsSigned(conv.conv()) && IsSigned::value) { + if (!FormatConversionCharIsIntegral(conv.conversion_char())) return false; + if (!FormatConversionCharIsSigned(conv.conversion_char()) && + IsSigned::value) { using U = typename MakeUnsigned::type; return FormatConvertImpl(static_cast(v), conv, sink).value; } @@ -239,19 +241,19 @@ bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { template bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { - return FormatConversionCharIsFloat(conv.conv()) && + return FormatConversionCharIsFloat(conv.conversion_char()) && ConvertFloatImpl(v, conv, sink); } inline bool ConvertStringArg(string_view v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv() != ConversionChar::s) return false; - if (conv.flags().basic) { + if (conv.conversion_char() != ConversionChar::s) return false; + if (conv.is_basic()) { sink->Append(v); return true; } return sink->PutPaddedString(v, conv.width(), conv.precision(), - conv.flags().left); + conv.has_left_flag()); } } // namespace @@ -272,7 +274,7 @@ ConvertResult FormatConvertImpl(string_view v, ConvertResult FormatConvertImpl(const char *v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv() == ConversionChar::p) + if (conv.conversion_char() == ConversionChar::p) return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; size_t len; if (v == nullptr) { @@ -289,7 +291,7 @@ ConvertResult FormatConvertImpl(const char *v, // ==================== Raw pointers ==================== ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conv() != ConversionChar::p) return {false}; + if (conv.conversion_char() != ConversionChar::p) return {false}; if (!v.value) { sink->Append("(nil)"); return {true}; diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 7a937563..1c36e309 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -70,9 +70,11 @@ template FormatConvertImpl(const AbslCord& value, ConversionSpec conv, FormatSinkImpl* sink) { - if (conv.conv() != ConversionChar::s) return {false}; + if (conv.conversion_char() != ConversionChar::s) { + return {false}; + } - bool is_left = conv.flags().left; + bool is_left = conv.has_left_flag(); size_t space_remaining = 0; int width = conv.width(); @@ -106,8 +108,8 @@ ConvertResult FormatConvertImpl(const AbslCord& value, } using IntegralConvertResult = - ConvertResult; -using FloatingConvertResult = ConvertResult; + ConvertResult; +using FloatingConvertResult = ConvertResult; // Floats. FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv, @@ -185,7 +187,9 @@ struct FormatCountCaptureHelper { FormatSinkImpl* sink) { const absl::enable_if_t& v2 = v; - if (conv.conv() != str_format_internal::ConversionChar::n) return {false}; + if (conv.conversion_char() != str_format_internal::ConversionChar::n) { + return {false}; + } *v2.p_ = static_cast(sink->size()); return {true}; } @@ -377,7 +381,7 @@ class FormatArgImpl { template static bool Dispatch(Data arg, ConversionSpec spec, void* out) { // A `none` conv indicates that we want the `int` conversion. - if (ABSL_PREDICT_FALSE(spec.conv() == ConversionChar::none)) { + if (ABSL_PREDICT_FALSE(spec.conversion_char() == ConversionChar::kNone)) { return ToInt(arg, static_cast(out), std::is_integral(), std::is_enum()); } diff --git a/absl/strings/internal/str_format/bind.cc b/absl/strings/internal/str_format/bind.cc index 27522fdb..6980ed1d 100644 --- a/absl/strings/internal/str_format/bind.cc +++ b/absl/strings/internal/str_format/bind.cc @@ -147,7 +147,7 @@ class SummarizingConverter { << FormatConversionSpecImplFriend::FlagsToString(bound); if (bound.width() >= 0) ss << bound.width(); if (bound.precision() >= 0) ss << "." << bound.precision(); - ss << bound.conv() << "}"; + ss << bound.conversion_char() << "}"; Append(ss.str()); return true; } diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index ea2a7681..49a24b40 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -9,13 +9,17 @@ ABSL_NAMESPACE_BEGIN namespace str_format_internal { namespace { -std::string ConvToString(Conv conv) { +std::string ConvToString(FormatConversionCharSet conv) { std::string out; #define CONV_SET_CASE(c) \ - if (Contains(conv, Conv::c)) out += #c; + if (Contains(conv, FormatConversionCharSet::c)) { \ + out += #c; \ + } ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, ) #undef CONV_SET_CASE - if (Contains(conv, Conv::star)) out += "*"; + if (Contains(conv, FormatConversionCharSet::kStar)) { + out += "*"; + } return out; } diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index d4c647c3..d5a1ee40 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -33,7 +33,7 @@ bool FallbackToSnprintf(const Float v, const ConversionSpec &conv, if (std::is_same()) { *fp++ = 'L'; } - *fp++ = FormatConversionCharToChar(conv.conv()); + *fp++ = FormatConversionCharToChar(conv.conversion_char()); *fp = 0; assert(fp < fmt + sizeof(fmt)); } @@ -100,17 +100,19 @@ bool ConvertNonNumericFloats(char sign_char, Float v, char text[4], *ptr = text; if (sign_char) *ptr++ = sign_char; if (std::isnan(v)) { - ptr = std::copy_n(FormatConversionCharIsUpper(conv.conv()) ? "NAN" : "nan", - 3, ptr); + ptr = std::copy_n( + FormatConversionCharIsUpper(conv.conversion_char()) ? "NAN" : "nan", 3, + ptr); } else if (std::isinf(v)) { - ptr = std::copy_n(FormatConversionCharIsUpper(conv.conv()) ? "INF" : "inf", - 3, ptr); + ptr = std::copy_n( + FormatConversionCharIsUpper(conv.conversion_char()) ? "INF" : "inf", 3, + ptr); } else { return false; } return sink->PutPaddedString(string_view(text, ptr - text), conv.width(), -1, - conv.flags().left); + conv.has_left_flag()); } // Round up the last digit of the value. @@ -358,9 +360,9 @@ void WriteBufferToSink(char sign_char, string_view str, static_cast(sign_char != 0), 0) : 0; - if (conv.flags().left) { + if (conv.has_left_flag()) { right_spaces = missing_chars; - } else if (conv.flags().zero) { + } else if (conv.has_zero_flag()) { zeros = missing_chars; } else { left_spaces = missing_chars; @@ -382,9 +384,9 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, if (std::signbit(abs_v)) { sign_char = '-'; abs_v = -abs_v; - } else if (conv.flags().show_pos) { + } else if (conv.has_show_pos_flag()) { sign_char = '+'; - } else if (conv.flags().sign_col) { + } else if (conv.has_sign_col_flag()) { sign_char = ' '; } @@ -401,14 +403,14 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, Buffer buffer; - switch (conv.conv()) { + switch (conv.conversion_char()) { case ConversionChar::f: case ConversionChar::F: if (!FloatToBuffer(decomposed, precision, &buffer, nullptr)) { return FallbackToSnprintf(v, conv, sink); } - if (!conv.flags().alt && buffer.back() == '.') buffer.pop_back(); + if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); break; case ConversionChar::e: @@ -417,9 +419,10 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, &exp)) { return FallbackToSnprintf(v, conv, sink); } - if (!conv.flags().alt && buffer.back() == '.') buffer.pop_back(); - PrintExponent(exp, FormatConversionCharIsUpper(conv.conv()) ? 'E' : 'e', - &buffer); + if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); + PrintExponent( + exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', + &buffer); break; case ConversionChar::g: @@ -446,13 +449,15 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, } exp = 0; } - if (!conv.flags().alt) { + if (!conv.has_alt_flag()) { while (buffer.back() == '0') buffer.pop_back(); if (buffer.back() == '.') buffer.pop_back(); } if (exp) { - PrintExponent(exp, FormatConversionCharIsUpper(conv.conv()) ? 'E' : 'e', - &buffer); + PrintExponent( + exp, + FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', + &buffer); } break; diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 1b1ee030..51eb53f5 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -52,7 +52,7 @@ TEST(ConversionCharTest, Names) { X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float X(n), X(p), // misc #undef X - {ConversionChar::none, '\0'}, + {ConversionChar::kNone, '\0'}, }; // clang-format on for (auto e : kExpect) { diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index bd4e1162..7db85e75 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc @@ -40,6 +40,7 @@ #include "absl/random/distributions.h" #include "absl/random/random.h" #include "absl/strings/internal/numbers_test_common.h" +#include "absl/strings/internal/ostringstream.h" #include "absl/strings/internal/pow10_helper.h" #include "absl/strings/str_cat.h" diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 554dca72..f0d1f0ad 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -540,19 +540,19 @@ TEST_F(ParsedFormatTest, UncheckedCorrect) { EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); std::string format = "%sFFF%dZZZ%f"; - auto f2 = - ExtendedParsedFormat::New(format); + auto f2 = ExtendedParsedFormat::New( + format); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); - f2 = ExtendedParsedFormat::New( + f2 = ExtendedParsedFormat::New( "%s %d %f"); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); - auto star = ExtendedParsedFormat::New("%*d"); + auto star = ExtendedParsedFormat::New("%*d"); ASSERT_TRUE(star); EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); -- cgit v1.2.3 From ca9856cabc23d771bcce634677650eb6fc4363ae Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 30 Apr 2020 17:44:10 -0700 Subject: Export of internal Abseil changes -- 53550735f5a943dfb99225e7c53f211c2d6e7951 by Gennadiy Rozental : Import of CCTZ from GitHub. PiperOrigin-RevId: 309333648 -- 847bbf8a1d9cd322ec058c6f932d1f687fd3d331 by Gennadiy Rozental : Make Validation interfaces private in CommandLineFlag. Calls are rewired via private interface access struct. PiperOrigin-RevId: 309323013 -- a600fc5051e0a0af50a7850450fd3ed1aef3f316 by Matthew Brown : Internal Change. PiperOrigin-RevId: 309292207 -- 937d00ce3cf62c5f23f59b5377471fd01d6bfbc7 by Gennadiy Rozental : Make TypeId interface private in CommandLineFlag. We also rewire the SaveState via the new PrivateHandleInterface trampoline class. This class will be the only way to access private methods of class CommandLineFlag. PiperOrigin-RevId: 309282547 -- 796c4bd35073b6a8337762bdb13603dae12a4df1 by Derek Mauro : Cleanup uses of kLinkerInitialized PiperOrigin-RevId: 309274734 -- c831446c52d9ef4bdcb1ea369840904620abc4b9 by Gennadiy Rozental : Eliminate the interface IsModified of CommndLineFlag. PiperOrigin-RevId: 309256248 -- a1db59d7f7aa39cb0a37dbf80f8c04e371da8465 by Gennadiy Rozental : Avoid default value generator if default value expression is constexpr. If possible, we detect constexpr-ness of default value expression and avoid storing default value generator in side of flag and instead set the flag's value to the value of that expression at const initialization time of flag objects. At the moment we only do this for flags of (all) integral, float and double value types PiperOrigin-RevId: 309110630 -- ae3b4a139aacd8fc165c9acd2a3cbae1f9e26af4 by Gennadiy Rozental : Make SaveState a private method of the CommandLineFlag and make it only accessible from FlagSaverImpl. There is no other call sites for this call. PiperOrigin-RevId: 309073989 -- cbc24b4dcc166dd6b0208e9d7620484eaaaa7ee0 by Abseil Team : Eliminate the interface IsModified of CommndLineFlag. PiperOrigin-RevId: 309064639 -- 08e79645a89d71785c5381cea9c413357db9824a by Gennadiy Rozental : Eliminate the interface IsModified of CommndLineFlag. PiperOrigin-RevId: 309054430 -- 4a6c70233c60dc8c39b7fa9beb5fa687c215261f by Gennadiy Rozental : Internal change PiperOrigin-RevId: 308900784 -- 13160efdf7710f142778d5a1e4c85aa309f019b6 by Abseil Team : Provide definitions of static member variables -- improved C++11 support. PiperOrigin-RevId: 308900290 -- 0343b8228657b9b313afdfe88c4a7b2137d56db4 by Gennadiy Rozental : Rename method Get to TryGet per approved spec before making interface public. PiperOrigin-RevId: 308889113 -- 7b84e27fb857fc1296a05504970f506d47d2f2c1 by Derek Mauro : Remove node_hash_* methods that were deprecated on release PiperOrigin-RevId: 308837933 -- 599d44ee72c02b6bb6e1c1a1db72873841441416 by Gennadiy Rozental : Eliminate CommandLineFlag::Typename interface per approved spec before making CommandLineFlag public. PiperOrigin-RevId: 308814376 GitOrigin-RevId: 53550735f5a943dfb99225e7c53f211c2d6e7951 Change-Id: Iae52c65b7322152c7e58f222d60eb5a21699a2cb --- absl/base/internal/spinlock.cc | 7 + absl/base/internal/spinlock.h | 5 + absl/base/internal/sysinfo.cc | 11 +- absl/base/internal/thread_identity_test.cc | 9 +- absl/base/spinlock_test_common.cc | 14 +- absl/container/node_hash_map.h | 6 - absl/container/node_hash_set.h | 6 - absl/flags/flag.h | 61 ++--- absl/flags/flag_test.cc | 67 ++--- absl/flags/internal/commandlineflag.cc | 15 +- absl/flags/internal/commandlineflag.h | 46 ++-- absl/flags/internal/commandlineflag_test.cc | 3 - absl/flags/internal/flag.cc | 61 +++-- absl/flags/internal/flag.h | 99 +++++--- absl/flags/internal/registry.cc | 12 +- absl/flags/internal/type_erased.cc | 4 +- absl/flags/internal/type_erased.h | 2 +- absl/flags/internal/usage.cc | 32 +-- absl/strings/internal/str_format/arg.cc | 28 +-- absl/strings/internal/str_format/arg.h | 85 ++++--- absl/strings/internal/str_format/bind.h | 5 +- absl/strings/internal/str_format/checker.h | 24 +- absl/strings/internal/str_format/extension.h | 277 +++++++++++++++------ absl/strings/internal/str_format/extension_test.cc | 9 - .../internal/str_format/float_conversion.cc | 110 ++++---- absl/strings/internal/str_format/parser.cc | 10 +- absl/strings/internal/str_format/parser.h | 12 +- absl/strings/internal/str_format/parser_test.cc | 27 +- absl/strings/str_format_test.cc | 2 +- .../internal/create_thread_identity.cc | 6 +- absl/synchronization/internal/graphcycles.cc | 6 +- absl/synchronization/mutex.cc | 17 +- absl/time/clock.cc | 6 +- absl/time/internal/cctz/src/cctz_benchmark.cc | 1 + .../internal/cctz/src/time_zone_lookup_test.cc | 1 + 35 files changed, 646 insertions(+), 440 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/internal/spinlock.cc b/absl/base/internal/spinlock.cc index fd0c733e..7cac72f9 100644 --- a/absl/base/internal/spinlock.cc +++ b/absl/base/internal/spinlock.cc @@ -66,6 +66,13 @@ void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock, submit_profile_data.Store(fn); } +// Static member variable definitions. +constexpr uint32_t SpinLock::kSpinLockHeld; +constexpr uint32_t SpinLock::kSpinLockCooperative; +constexpr uint32_t SpinLock::kSpinLockDisabledScheduling; +constexpr uint32_t SpinLock::kSpinLockSleeper; +constexpr uint32_t SpinLock::kWaitTimeMask; + // Uncommon constructors. SpinLock::SpinLock(base_internal::SchedulingMode mode) : lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) { diff --git a/absl/base/internal/spinlock.h b/absl/base/internal/spinlock.h index 89e93aad..2b08a2d6 100644 --- a/absl/base/internal/spinlock.h +++ b/absl/base/internal/spinlock.h @@ -36,6 +36,7 @@ #include #include "absl/base/attributes.h" +#include "absl/base/const_init.h" #include "absl/base/dynamic_annotations.h" #include "absl/base/internal/low_level_scheduling.h" #include "absl/base/internal/raw_logging.h" @@ -77,6 +78,10 @@ class ABSL_LOCKABLE SpinLock { SpinLock(base_internal::LinkerInitialized, base_internal::SchedulingMode mode); + // Constructor for global SpinLock instances. See absl/base/const_init.h. + constexpr SpinLock(absl::ConstInitType, base_internal::SchedulingMode mode) + : lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {} + ~SpinLock() { ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static); } // Acquire this SpinLock. diff --git a/absl/base/internal/sysinfo.cc b/absl/base/internal/sysinfo.cc index c3f275ca..6c69683f 100644 --- a/absl/base/internal/sysinfo.cc +++ b/absl/base/internal/sysinfo.cc @@ -344,15 +344,16 @@ pid_t GetTID() { #else // Fallback implementation of GetTID using pthread_getspecific. -static once_flag tid_once; -static pthread_key_t tid_key; -static absl::base_internal::SpinLock tid_lock( - absl::base_internal::kLinkerInitialized); +ABSL_CONST_INIT static once_flag tid_once; +ABSL_CONST_INIT static pthread_key_t tid_key; +ABSL_CONST_INIT static absl::base_internal::SpinLock tid_lock( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); // We set a bit per thread in this array to indicate that an ID is in // use. ID 0 is unused because it is the default value returned by // pthread_getspecific(). -static std::vector* tid_array ABSL_GUARDED_BY(tid_lock) = nullptr; +ABSL_CONST_INIT static std::vector *tid_array + ABSL_GUARDED_BY(tid_lock) = nullptr; static constexpr int kBitsPerWord = 32; // tid_array is uint32_t. // Returns the TID to tid_array. diff --git a/absl/base/internal/thread_identity_test.cc b/absl/base/internal/thread_identity_test.cc index 3685779c..624d5b96 100644 --- a/absl/base/internal/thread_identity_test.cc +++ b/absl/base/internal/thread_identity_test.cc @@ -21,6 +21,7 @@ #include "absl/base/attributes.h" #include "absl/base/internal/spinlock.h" #include "absl/base/macros.h" +#include "absl/base/thread_annotations.h" #include "absl/synchronization/internal/per_thread_sem.h" #include "absl/synchronization/mutex.h" @@ -29,10 +30,9 @@ ABSL_NAMESPACE_BEGIN namespace base_internal { namespace { -// protects num_identities_reused -static absl::base_internal::SpinLock map_lock( - absl::base_internal::kLinkerInitialized); -static int num_identities_reused; +ABSL_CONST_INIT static absl::base_internal::SpinLock map_lock( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); +ABSL_CONST_INIT static int num_identities_reused ABSL_GUARDED_BY(map_lock); static const void* const kCheckNoIdentity = reinterpret_cast(1); @@ -90,6 +90,7 @@ TEST(ThreadIdentityTest, BasicIdentityWorksThreaded) { // We should have recycled ThreadIdentity objects above; while (external) // library threads allocating their own identities may preclude some // reuse, we should have sufficient repetitions to exclude this. + absl::base_internal::SpinLockHolder l(&map_lock); EXPECT_LT(kNumThreads, num_identities_reused); } diff --git a/absl/base/spinlock_test_common.cc b/absl/base/spinlock_test_common.cc index 08f61ba8..b68c51a1 100644 --- a/absl/base/spinlock_test_common.cc +++ b/absl/base/spinlock_test_common.cc @@ -56,12 +56,10 @@ namespace { static constexpr int kArrayLength = 10; static uint32_t values[kArrayLength]; -static SpinLock static_spinlock(base_internal::kLinkerInitialized); -static SpinLock static_cooperative_spinlock( - base_internal::kLinkerInitialized, - base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL); -static SpinLock static_noncooperative_spinlock( - base_internal::kLinkerInitialized, base_internal::SCHEDULE_KERNEL_ONLY); +ABSL_CONST_INIT static SpinLock static_cooperative_spinlock( + absl::kConstInit, base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL); +ABSL_CONST_INIT static SpinLock static_noncooperative_spinlock( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); // Simple integer hash function based on the public domain lookup2 hash. // http://burtleburtle.net/bob/c/lookup2.c @@ -191,10 +189,6 @@ TEST(SpinLock, WaitCyclesEncoding) { EXPECT_GT(expected_max_value_decoded, before_max_value_decoded); } -TEST(SpinLockWithThreads, StaticSpinLock) { - ThreadedTest(&static_spinlock); -} - TEST(SpinLockWithThreads, StackSpinLock) { SpinLock spinlock; ThreadedTest(&spinlock); diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index fccea184..174b971e 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h @@ -514,12 +514,6 @@ class node_hash_map // // Returns the function used for comparing keys equality. using Base::key_eq; - - ABSL_DEPRECATED("Call `hash_function()` instead.") - typename Base::hasher hash_funct() { return this->hash_function(); } - - ABSL_DEPRECATED("Call `rehash()` instead.") - void resize(typename Base::size_type hint) { this->rehash(hint); } }; // erase_if(node_hash_map<>, Pred) diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index ad54b6dc..56bab5c2 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h @@ -427,12 +427,6 @@ class node_hash_set // // Returns the function used for comparing keys equality. using Base::key_eq; - - ABSL_DEPRECATED("Call `hash_function()` instead.") - typename Base::hasher hash_funct() { return this->hash_function(); } - - ABSL_DEPRECATED("Call `rehash()` instead.") - void resize(typename Base::size_type hint) { this->rehash(hint); } }; // erase_if(node_hash_set<>, Pred) diff --git a/absl/flags/flag.h b/absl/flags/flag.h index 194a9d31..8dd1b9be 100644 --- a/absl/flags/flag.h +++ b/absl/flags/flag.h @@ -118,11 +118,12 @@ class Flag { return impl_; } - impl_ = - new flags_internal::Flag(name_, filename_, - {flags_internal::FlagHelpMsg(help_gen_), - flags_internal::FlagHelpKind::kGenFunc}, - default_value_gen_); + impl_ = new flags_internal::Flag( + name_, filename_, + {flags_internal::FlagHelpMsg(help_gen_), + flags_internal::FlagHelpKind::kGenFunc}, + {flags_internal::FlagDefaultSrc(default_value_gen_), + flags_internal::FlagDefaultKind::kGenFunc}); inited_.store(true, std::memory_order_release); } @@ -132,14 +133,12 @@ class Flag { // Public methods of `absl::Flag` are NOT part of the Abseil Flags API. // See https://abseil.io/docs/cpp/guides/flags bool IsRetired() const { return GetImpl()->IsRetired(); } - bool IsAbseilFlag() const { return GetImpl()->IsAbseilFlag(); } absl::string_view Name() const { return GetImpl()->Name(); } std::string Help() const { return GetImpl()->Help(); } bool IsModified() const { return GetImpl()->IsModified(); } bool IsSpecifiedOnCommandLine() const { return GetImpl()->IsSpecifiedOnCommandLine(); } - absl::string_view Typename() const { return GetImpl()->Typename(); } std::string Filename() const { return GetImpl()->Filename(); } std::string DefaultValue() const { return GetImpl()->DefaultValue(); } std::string CurrentValue() const { return GetImpl()->CurrentValue(); } @@ -311,9 +310,12 @@ ABSL_NAMESPACE_END static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \ } -#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ - static void AbslFlagsInitFlag##name(void* dst) { \ - absl::flags_internal::MakeFromDefaultValue(dst, default_value); \ +#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ + struct AbslFlagDefaultGenFor##name { \ + Type value = absl::flags_internal::InitDefaultValue(default_value); \ + static void Gen(void* p) { \ + new (p) Type(AbslFlagDefaultGenFor##name{}.value); \ + } \ } // ABSL_FLAG_IMPL @@ -322,29 +324,30 @@ ABSL_NAMESPACE_END // global name for FLAGS_no symbol, thus preventing the possibility // of defining two flags with names foo and nofoo. #if !defined(_MSC_VER) || defined(__clang__) -#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ - namespace absl /* block flags in namespaces */ {} \ - ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ - ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ - ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ - ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ - absl::flags_internal::HelpArg(0), \ - &AbslFlagsInitFlag##name}; \ - extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ - absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ + +#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ + namespace absl /* block flags in namespaces */ {} \ + ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value); \ + ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ + ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ + ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ + absl::flags_internal::HelpArg(0), \ + absl::flags_internal::DefaultArg(0)}; \ + extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ + absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) #else // MSVC version uses aggregate initialization. We also do not try to // optimize away help wrapper. -#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ - namespace absl /* block flags in namespaces */ {} \ - ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ - ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ - ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ - ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ - &AbslFlagHelpGenFor##name::NonConst, &AbslFlagsInitFlag##name}; \ - extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ - absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ +#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ + namespace absl /* block flags in namespaces */ {} \ + ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value); \ + ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \ + ABSL_CONST_INIT absl::Flag FLAGS_##name{ \ + ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \ + &AbslFlagHelpGenFor##name::NonConst, &AbslFlagDefaultGenFor##name::Gen}; \ + extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \ + absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \ ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) #endif diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc index 6fa178f1..015b1fc9 100644 --- a/absl/flags/flag_test.cc +++ b/absl/flags/flag_test.cc @@ -128,26 +128,27 @@ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"), using String = std::string; -#define DEFINE_CONSTRUCTED_FLAG(T) \ - constexpr flags::Flag f1##T("f1", "file", help_arg, &TestMakeDflt); \ - ABSL_CONST_INIT flags::Flag f2##T( \ - "f2", "file", \ - {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \ - &TestMakeDflt) - -#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, &f2##T); - -DEFINE_CONSTRUCTED_FLAG(bool); -DEFINE_CONSTRUCTED_FLAG(int16_t); -DEFINE_CONSTRUCTED_FLAG(uint16_t); -DEFINE_CONSTRUCTED_FLAG(int32_t); -DEFINE_CONSTRUCTED_FLAG(uint32_t); -DEFINE_CONSTRUCTED_FLAG(int64_t); -DEFINE_CONSTRUCTED_FLAG(uint64_t); -DEFINE_CONSTRUCTED_FLAG(float); -DEFINE_CONSTRUCTED_FLAG(double); -DEFINE_CONSTRUCTED_FLAG(String); -DEFINE_CONSTRUCTED_FLAG(UDT); +#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \ + constexpr flags::FlagDefaultArg f1default##T{ \ + flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \ + constexpr flags::Flag f1##T("f1", "file", help_arg, f1default##T); \ + ABSL_CONST_INIT flags::Flag f2##T( \ + "f2", "file", \ + {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \ + flags::FlagDefaultArg{flags::FlagDefaultSrc(&TestMakeDflt), \ + flags::FlagDefaultKind::kGenFunc}) + +DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord); +DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord); +DEFINE_CONSTRUCTED_FLAG(uint16_t, 2, kOneWord); +DEFINE_CONSTRUCTED_FLAG(int32_t, 3, kOneWord); +DEFINE_CONSTRUCTED_FLAG(uint32_t, 4, kOneWord); +DEFINE_CONSTRUCTED_FLAG(int64_t, 5, kOneWord); +DEFINE_CONSTRUCTED_FLAG(uint64_t, 6, kOneWord); +DEFINE_CONSTRUCTED_FLAG(float, 7.8, kFloat); +DEFINE_CONSTRUCTED_FLAG(double, 9.10, kDouble); +DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt, kGenFunc); +DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt, kGenFunc); template bool TestConstructionFor(const flags::Flag& f1, flags::Flag* f2) { @@ -164,6 +165,8 @@ bool TestConstructionFor(const flags::Flag& f1, flags::Flag* f2) { return true; } +#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, &f2##T); + TEST_F(FlagTest, TestConstruction) { TEST_CONSTRUCTED_FLAG(bool); TEST_CONSTRUCTED_FLAG(int16_t); @@ -443,29 +446,29 @@ TEST_F(FlagTest, TestGetSet) { TEST_F(FlagTest, TestGetViaReflection) { auto* handle = flags::FindCommandLineFlag("test_flag_01"); - EXPECT_EQ(*handle->Get(), true); + EXPECT_EQ(*handle->TryGet(), true); handle = flags::FindCommandLineFlag("test_flag_02"); - EXPECT_EQ(*handle->Get(), 1234); + EXPECT_EQ(*handle->TryGet(), 1234); handle = flags::FindCommandLineFlag("test_flag_03"); - EXPECT_EQ(*handle->Get(), -34); + EXPECT_EQ(*handle->TryGet(), -34); handle = flags::FindCommandLineFlag("test_flag_04"); - EXPECT_EQ(*handle->Get(), 189); + EXPECT_EQ(*handle->TryGet(), 189); handle = flags::FindCommandLineFlag("test_flag_05"); - EXPECT_EQ(*handle->Get(), 10765); + EXPECT_EQ(*handle->TryGet(), 10765); handle = flags::FindCommandLineFlag("test_flag_06"); - EXPECT_EQ(*handle->Get(), 40000); + EXPECT_EQ(*handle->TryGet(), 40000); handle = flags::FindCommandLineFlag("test_flag_07"); - EXPECT_EQ(*handle->Get(), -1234567); + EXPECT_EQ(*handle->TryGet(), -1234567); handle = flags::FindCommandLineFlag("test_flag_08"); - EXPECT_EQ(*handle->Get(), 9876543); + EXPECT_EQ(*handle->TryGet(), 9876543); handle = flags::FindCommandLineFlag("test_flag_09"); - EXPECT_NEAR(*handle->Get(), -9.876e-50, 1e-55); + EXPECT_NEAR(*handle->TryGet(), -9.876e-50, 1e-55); handle = flags::FindCommandLineFlag("test_flag_10"); - EXPECT_NEAR(*handle->Get(), 1.234e12f, 1e5f); + EXPECT_NEAR(*handle->TryGet(), 1.234e12f, 1e5f); handle = flags::FindCommandLineFlag("test_flag_11"); - EXPECT_EQ(*handle->Get(), ""); + EXPECT_EQ(*handle->TryGet(), ""); handle = flags::FindCommandLineFlag("test_flag_12"); - EXPECT_EQ(*handle->Get(), absl::Minutes(10)); + EXPECT_EQ(*handle->TryGet(), absl::Minutes(10)); } // -------------------------------------------------------------------- diff --git a/absl/flags/internal/commandlineflag.cc b/absl/flags/internal/commandlineflag.cc index 90765a3e..de588c13 100644 --- a/absl/flags/internal/commandlineflag.cc +++ b/absl/flags/internal/commandlineflag.cc @@ -22,7 +22,20 @@ namespace flags_internal { FlagStateInterface::~FlagStateInterface() {} bool CommandLineFlag::IsRetired() const { return false; } -bool CommandLineFlag::IsAbseilFlag() const { return true; } + +FlagFastTypeId PrivateHandleInterface::TypeId(const CommandLineFlag& flag) { + return flag.TypeId(); +} + +std::unique_ptr PrivateHandleInterface::SaveState( + CommandLineFlag* flag) { + return flag->SaveState(); +} + +bool PrivateHandleInterface::ValidateInputValue(const CommandLineFlag& flag, + absl::string_view value) { + return flag.ValidateInputValue(value); +} } // namespace flags_internal ABSL_NAMESPACE_END diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h index ef992f7f..f807fb9a 100644 --- a/absl/flags/internal/commandlineflag.h +++ b/absl/flags/internal/commandlineflag.h @@ -93,7 +93,7 @@ class CommandLineFlag { // Attempts to retrieve the flag value. Returns value on success, // absl::nullopt otherwise. template - absl::optional Get() const { + absl::optional TryGet() const { if (IsRetired() || !IsOfType()) { return absl::nullopt; } @@ -130,29 +130,14 @@ class CommandLineFlag { virtual absl::string_view Name() const = 0; // Returns name of the file where this flag is defined. virtual std::string Filename() const = 0; - // Returns name of the flag's value type for some built-in types or empty - // string. - virtual absl::string_view Typename() const = 0; // Returns help message associated with this flag. virtual std::string Help() const = 0; // Returns true iff this object corresponds to retired flag. virtual bool IsRetired() const; - // Returns true iff this is a handle to an Abseil Flag. - virtual bool IsAbseilFlag() const; - // Returns id of the flag's value type. - virtual FlagFastTypeId TypeId() const = 0; - virtual bool IsModified() const = 0; virtual bool IsSpecifiedOnCommandLine() const = 0; virtual std::string DefaultValue() const = 0; virtual std::string CurrentValue() const = 0; - // Interfaces to operate on validators. - virtual bool ValidateInputValue(absl::string_view value) const = 0; - - // Interface to save flag to some persistent state. Returns current flag state - // or nullptr if flag does not support saving and restoring a state. - virtual std::unique_ptr SaveState() = 0; - // Sets the value of the flag based on specified string `value`. If the flag // was successfully set to new value, it returns true. Otherwise, sets `error` // to indicate the error, leaves the flag unchanged, and returns false. There @@ -174,9 +159,38 @@ class CommandLineFlag { ~CommandLineFlag() = default; private: + friend class PrivateHandleInterface; + + // Returns id of the flag's value type. + virtual FlagFastTypeId TypeId() const = 0; + + // Interface to save flag to some persistent state. Returns current flag state + // or nullptr if flag does not support saving and restoring a state. + virtual std::unique_ptr SaveState() = 0; + // Copy-construct a new value of the flag's type in a memory referenced by // the dst based on the current flag's value. virtual void Read(void* dst) const = 0; + + // Interfaces to operate on validators. + // Validates supplied value usign validator or parseflag routine + virtual bool ValidateInputValue(absl::string_view value) const = 0; +}; + +// This class serves as a trampoline to access private methods of +// CommandLineFlag. This class is intended for use exclusively internally inside +// of the Abseil Flags implementation +class PrivateHandleInterface { + public: + // Access to CommandLineFlag::TypeId. + static FlagFastTypeId TypeId(const CommandLineFlag& flag); + + // Access to CommandLineFlag::SaveState. + static std::unique_ptr SaveState(CommandLineFlag* flag); + + // Access to CommandLineFlag::ValidateInputValue. + static bool ValidateInputValue(const CommandLineFlag& flag, + absl::string_view value); }; // This macro is the "source of truth" for the list of supported flag built-in diff --git a/absl/flags/internal/commandlineflag_test.cc b/absl/flags/internal/commandlineflag_test.cc index c1142b7c..c31679f9 100644 --- a/absl/flags/internal/commandlineflag_test.cc +++ b/absl/flags/internal/commandlineflag_test.cc @@ -67,7 +67,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ASSERT_TRUE(flag_01); EXPECT_EQ(flag_01->Name(), "int_flag"); EXPECT_EQ(flag_01->Help(), "int_flag help"); - EXPECT_EQ(flag_01->Typename(), ""); EXPECT_TRUE(!flag_01->IsRetired()); EXPECT_TRUE(flag_01->IsOfType()); EXPECT_TRUE( @@ -80,7 +79,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ASSERT_TRUE(flag_02); EXPECT_EQ(flag_02->Name(), "string_flag"); EXPECT_EQ(flag_02->Help(), "string_flag help"); - EXPECT_EQ(flag_02->Typename(), ""); EXPECT_TRUE(!flag_02->IsRetired()); EXPECT_TRUE(flag_02->IsOfType()); EXPECT_TRUE( @@ -93,7 +91,6 @@ TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) { ASSERT_TRUE(flag_03); EXPECT_EQ(flag_03->Name(), "bool_retired_flag"); EXPECT_EQ(flag_03->Help(), ""); - EXPECT_EQ(flag_03->Typename(), ""); EXPECT_TRUE(flag_03->IsRetired()); EXPECT_TRUE(flag_03->IsOfType()); EXPECT_EQ(flag_03->Filename(), "RETIRED"); diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 089567f7..8f0777fa 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -139,19 +139,43 @@ void DynValueDeleter::operator()(void* ptr) const { void FlagImpl::Init() { new (&data_guard_) absl::Mutex; - // At this point the default_value_ always points to gen_func. + auto def_kind = static_cast(def_kind_); + switch (ValueStorageKind()) { case FlagValueStorageKind::kAlignedBuffer: + // For this storage kind the default_value_ always points to gen_func + // during initialization. + assert(def_kind == FlagDefaultKind::kGenFunc); (*default_value_.gen_func)(AlignedBufferValue()); break; case FlagValueStorageKind::kOneWordAtomic: { alignas(int64_t) std::array buf{}; - (*default_value_.gen_func)(buf.data()); - auto value = absl::bit_cast(buf); - OneWordValue().store(value, std::memory_order_release); + switch (def_kind) { + case FlagDefaultKind::kOneWord: + std::memcpy(buf.data(), &default_value_.one_word, + sizeof(default_value_.one_word)); + break; + case FlagDefaultKind::kFloat: + std::memcpy(buf.data(), &default_value_.float_value, + sizeof(default_value_.float_value)); + break; + case FlagDefaultKind::kDouble: + std::memcpy(buf.data(), &default_value_.double_value, + sizeof(default_value_.double_value)); + break; + default: + assert(def_kind == FlagDefaultKind::kGenFunc); + (*default_value_.gen_func)(buf.data()); + break; + } + OneWordValue().store(absl::bit_cast(buf), + std::memory_order_release); break; } case FlagValueStorageKind::kTwoWordsAtomic: { + // For this storage kind the default_value_ always points to gen_func + // during initialization. + assert(def_kind == FlagDefaultKind::kGenFunc); alignas(AlignedTwoWords) std::array buf{}; (*default_value_.gen_func)(buf.data()); auto atomic_value = absl::bit_cast(buf); @@ -196,11 +220,23 @@ void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id, std::unique_ptr FlagImpl::MakeInitValue() const { void* res = nullptr; - if (DefaultKind() == FlagDefaultKind::kDynamicValue) { - res = flags_internal::Clone(op_, default_value_.dynamic_value); - } else { - res = flags_internal::Alloc(op_); - (*default_value_.gen_func)(res); + switch (DefaultKind()) { + case FlagDefaultKind::kDynamicValue: + res = flags_internal::Clone(op_, default_value_.dynamic_value); + break; + case FlagDefaultKind::kGenFunc: + res = flags_internal::Alloc(op_); + (*default_value_.gen_func)(res); + break; + case FlagDefaultKind::kOneWord: + res = flags_internal::Clone(op_, &default_value_.one_word); + break; + case FlagDefaultKind::kFloat: + res = flags_internal::Clone(op_, &default_value_.float_value); + break; + case FlagDefaultKind::kDouble: + res = flags_internal::Clone(op_, &default_value_.double_value); + break; } return {res, DynValueDeleter{op_}}; } @@ -235,8 +271,6 @@ std::string FlagImpl::Filename() const { return flags_internal::GetUsageConfig().normalize_filename(filename_); } -absl::string_view FlagImpl::Typename() const { return ""; } - std::string FlagImpl::Help() const { return HelpSourceKind() == FlagHelpKind::kLiteral ? help_.literal : help_.gen_func(); @@ -246,11 +280,6 @@ FlagFastTypeId FlagImpl::TypeId() const { return flags_internal::FastTypeId(op_); } -bool FlagImpl::IsModified() const { - absl::MutexLock l(DataGuard()); - return modified_; -} - bool FlagImpl::IsSpecifiedOnCommandLine() const { absl::MutexLock l(DataGuard()); return on_command_line_; diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index 6da25aa9..b2208824 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -206,12 +206,72 @@ using FlagDfltGenFunc = void (*)(void*); union FlagDefaultSrc { constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg) : gen_func(gen_func_arg) {} + template + constexpr explicit FlagDefaultSrc(T one_word_value) + : one_word(static_cast(one_word_value)) {} + constexpr explicit FlagDefaultSrc(float f) : float_value(f) {} + constexpr explicit FlagDefaultSrc(double d) : double_value(d) {} void* dynamic_value; FlagDfltGenFunc gen_func; + int64_t one_word; + float float_value; + double double_value; +}; + +enum class FlagDefaultKind : uint8_t { + kDynamicValue = 0, + kGenFunc = 1, + kOneWord = 2, + kFloat = 3, + kDouble = 4 +}; + +struct FlagDefaultArg { + FlagDefaultSrc source; + FlagDefaultKind kind; }; -enum class FlagDefaultKind : uint8_t { kDynamicValue = 0, kGenFunc = 1 }; +// This struct and corresponding overload to InitDefaultValue are used to +// facilitate usage of {} as default value in ABSL_FLAG macro. +// TODO(rogeeff): Fix handling types with explicit constructors. +struct EmptyBraces {}; + +template +constexpr T InitDefaultValue(T t) { + return t; +} + +template +constexpr T InitDefaultValue(EmptyBraces) { + return T{}; +} + +template ::value, int>::type = + (GenT{}, 0)> +constexpr FlagDefaultArg DefaultArg(int) { + return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord}; +} + +template ::value, + int>::type = (GenT{}, 0)> +constexpr FlagDefaultArg DefaultArg(int) { + return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kFloat}; +} + +template ::value, + int>::type = (GenT{}, 0)> +constexpr FlagDefaultArg DefaultArg(int) { + return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kDouble}; +} + +template +constexpr FlagDefaultArg DefaultArg(char) { + return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc}; +} /////////////////////////////////////////////////////////////////////////////// // Flag current value auxiliary structs. @@ -356,19 +416,19 @@ class FlagImpl final : public flags_internal::CommandLineFlag { public: constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op, FlagHelpArg help, FlagValueStorageKind value_kind, - FlagDfltGenFunc default_value_gen) + FlagDefaultArg default_arg) : name_(name), filename_(filename), op_(op), help_(help.source), help_source_kind_(static_cast(help.kind)), value_storage_kind_(static_cast(value_kind)), - def_kind_(static_cast(FlagDefaultKind::kGenFunc)), + def_kind_(static_cast(default_arg.kind)), modified_(false), on_command_line_(false), counter_(0), callback_(nullptr), - default_value_(default_value_gen), + default_value_(default_arg.source), data_guard_{} {} // Constant access methods @@ -444,10 +504,8 @@ class FlagImpl final : public flags_internal::CommandLineFlag { // CommandLineFlag interface implementation absl::string_view Name() const override; std::string Filename() const override; - absl::string_view Typename() const override; std::string Help() const override; FlagFastTypeId TypeId() const override; - bool IsModified() const override ABSL_LOCKS_EXCLUDED(*DataGuard()); bool IsSpecifiedOnCommandLine() const override ABSL_LOCKS_EXCLUDED(*DataGuard()); std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard()); @@ -492,9 +550,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag { // Mutable flag's state (guarded by `data_guard_`). - // If def_kind_ == kDynamicValue, default_value_ holds a dynamically allocated - // value. - uint8_t def_kind_ : 1 ABSL_GUARDED_BY(*DataGuard()); + // def_kind_ is not guard by DataGuard() since it is accessed in Init without + // locks. If necessary we can decrease number of bits used to 2 by folding + // one_word storage cases. + uint8_t def_kind_ : 3; // Has this flag's value been modified? bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard()); // Has this flag been specified on command line. @@ -530,10 +589,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag { template class Flag { public: - constexpr Flag(const char* name, const char* filename, const FlagHelpArg help, - const FlagDfltGenFunc default_value_gen) + constexpr Flag(const char* name, const char* filename, FlagHelpArg help, + const FlagDefaultArg default_arg) : impl_(name, filename, &FlagOps, help, - flags_internal::StorageKind(), default_value_gen), + flags_internal::StorageKind(), default_arg), value_() {} T Get() const { @@ -560,9 +619,7 @@ class Flag { // CommandLineFlag interface absl::string_view Name() const { return impl_.Name(); } std::string Filename() const { return impl_.Filename(); } - absl::string_view Typename() const { return ""; } std::string Help() const { return impl_.Help(); } - bool IsModified() const { return impl_.IsModified(); } bool IsSpecifiedOnCommandLine() const { return impl_.IsSpecifiedOnCommandLine(); } @@ -662,20 +719,6 @@ class FlagRegistrar { Flag* flag_; // Flag being registered (not owned). }; -// This struct and corresponding overload to MakeDefaultValue are used to -// facilitate usage of {} as default value in ABSL_FLAG macro. -struct EmptyBraces {}; - -template -void MakeFromDefaultValue(void* dst, T t) { - new (dst) T(std::move(t)); -} - -template -void MakeFromDefaultValue(void* dst, EmptyBraces) { - new (dst) T{}; -} - } // namespace flags_internal ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc index eb619c70..62b5b40d 100644 --- a/absl/flags/internal/registry.cc +++ b/absl/flags/internal/registry.cc @@ -127,14 +127,13 @@ void FlagRegistry::RegisterFlag(CommandLineFlag* flag) { (flag->IsRetired() ? old_flag->Filename() : flag->Filename()), "'."), true); - } else if (flag->TypeId() != old_flag->TypeId()) { + } else if (flags_internal::PrivateHandleInterface::TypeId(*flag) != + flags_internal::PrivateHandleInterface::TypeId(*old_flag)) { flags_internal::ReportUsageError( absl::StrCat("Flag '", flag->Name(), "' was defined more than once but with " "differing types. Defined in files '", - old_flag->Filename(), "' and '", flag->Filename(), - "' with types '", old_flag->Typename(), "' and '", - flag->Typename(), "', respectively."), + old_flag->Filename(), "' and '", flag->Filename(), "'."), true); } else if (old_flag->IsRetired()) { // Retired flag can just be deleted. @@ -206,7 +205,8 @@ class FlagSaverImpl { void SaveFromRegistry() { assert(backup_registry_.empty()); // call only once! flags_internal::ForEachFlag([&](flags_internal::CommandLineFlag* flag) { - if (auto flag_state = flag->SaveState()) { + if (auto flag_state = + flags_internal::PrivateHandleInterface::SaveState(flag)) { backup_registry_.emplace_back(std::move(flag_state)); } }); @@ -290,11 +290,9 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag { private: absl::string_view Name() const override { return name_; } std::string Filename() const override { return "RETIRED"; } - absl::string_view Typename() const override { return ""; } FlagFastTypeId TypeId() const override { return type_id_; } std::string Help() const override { return ""; } bool IsRetired() const override { return true; } - bool IsModified() const override { return false; } bool IsSpecifiedOnCommandLine() const override { return false; } std::string DefaultValue() const override { return ""; } std::string CurrentValue() const override { return ""; } diff --git a/absl/flags/internal/type_erased.cc b/absl/flags/internal/type_erased.cc index 75b4cdf8..5038625b 100644 --- a/absl/flags/internal/type_erased.cc +++ b/absl/flags/internal/type_erased.cc @@ -72,7 +72,9 @@ bool IsValidFlagValue(absl::string_view name, absl::string_view value) { CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); return flag != nullptr && - (flag->IsRetired() || flag->ValidateInputValue(value)); + (flag->IsRetired() || + flags_internal::PrivateHandleInterface::ValidateInputValue(*flag, + value)); } // -------------------------------------------------------------------- diff --git a/absl/flags/internal/type_erased.h b/absl/flags/internal/type_erased.h index 188429c7..ffe319ba 100644 --- a/absl/flags/internal/type_erased.h +++ b/absl/flags/internal/type_erased.h @@ -75,7 +75,7 @@ inline bool GetByName(absl::string_view name, T* dst) { CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); if (!flag) return false; - if (auto val = flag->Get()) { + if (auto val = flag->TryGet()) { *dst = *val; return true; } diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc index a9a5cba9..9d856c87 100644 --- a/absl/flags/internal/usage.cc +++ b/absl/flags/internal/usage.cc @@ -54,27 +54,6 @@ ABSL_NAMESPACE_BEGIN namespace flags_internal { namespace { -absl::string_view TypenameForHelp(const flags_internal::CommandLineFlag& flag) { - // Only report names of v1 built-in types -#define HANDLE_V1_BUILTIN_TYPE(t) \ - if (flag.IsOfType()) { \ - return #t; \ - } - - HANDLE_V1_BUILTIN_TYPE(bool); - HANDLE_V1_BUILTIN_TYPE(int32_t); - HANDLE_V1_BUILTIN_TYPE(int64_t); - HANDLE_V1_BUILTIN_TYPE(uint64_t); - HANDLE_V1_BUILTIN_TYPE(double); -#undef HANDLE_V1_BUILTIN_TYPE - - if (flag.IsOfType()) { - return "string"; - } - - return ""; -} - // This class is used to emit an XML element with `tag` and `text`. // It adds opening and closing tags and escapes special characters in the text. // For example: @@ -212,23 +191,20 @@ void FlagHelpHumanReadable(const flags_internal::CommandLineFlag& flag, // Flag help. printer.Write(absl::StrCat("(", flag.Help(), ");"), /*wrap_line=*/true); - // Flag data type (for V1 flags only). - if (!flag.IsAbseilFlag() && !flag.IsRetired()) { - printer.Write(absl::StrCat("type: ", TypenameForHelp(flag), ";")); - } - // The listed default value will be the actual default from the flag // definition in the originating source file, unless the value has // subsequently been modified using SetCommandLineOption() with mode // SET_FLAGS_DEFAULT. std::string dflt_val = flag.DefaultValue(); + std::string curr_val = flag.CurrentValue(); + bool is_modified = curr_val != dflt_val; + if (flag.IsOfType()) { dflt_val = absl::StrCat("\"", dflt_val, "\""); } printer.Write(absl::StrCat("default: ", dflt_val, ";")); - if (flag.IsModified()) { - std::string curr_val = flag.CurrentValue(); + if (is_modified) { if (flag.IsOfType()) { curr_val = absl::StrCat("\"", curr_val, "\""); } diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index 12842276..a112071c 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -315,7 +315,7 @@ bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { inline bool ConvertStringArg(string_view v, const ConversionSpec conv, FormatSinkImpl *sink) { - if (conv.conversion_char() != ConversionChar::s) return false; + if (conv.conversion_char() != FormatConversionCharInternal::s) return false; if (conv.is_basic()) { sink->Append(v); return true; @@ -327,22 +327,22 @@ inline bool ConvertStringArg(string_view v, const ConversionSpec conv, } // namespace // ==================== Strings ==================== -ConvertResult FormatConvertImpl(const std::string &v, - const ConversionSpec conv, - FormatSinkImpl *sink) { +StringConvertResult FormatConvertImpl(const std::string &v, + const ConversionSpec conv, + FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } -ConvertResult FormatConvertImpl(string_view v, - const ConversionSpec conv, - FormatSinkImpl *sink) { +StringConvertResult FormatConvertImpl(string_view v, const ConversionSpec conv, + FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } -ConvertResult FormatConvertImpl(const char *v, - const ConversionSpec conv, - FormatSinkImpl *sink) { - if (conv.conversion_char() == ConversionChar::p) +ArgConvertResult +FormatConvertImpl(const char *v, const ConversionSpec conv, + FormatSinkImpl *sink) { + if (conv.conversion_char() == FormatConversionCharInternal::p) return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; size_t len; if (v == nullptr) { @@ -357,9 +357,9 @@ ConvertResult FormatConvertImpl(const char *v, } // ==================== Raw pointers ==================== -ConvertResult FormatConvertImpl(VoidPtr v, const ConversionSpec conv, - FormatSinkImpl *sink) { - if (conv.conversion_char() != ConversionChar::p) return {false}; +ArgConvertResult FormatConvertImpl( + VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) { + if (conv.conversion_char() != FormatConversionCharInternal::p) return {false}; if (!v.value) { sink->Append("(nil)"); return {true}; diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 1c36e309..f4ac940a 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -31,10 +31,11 @@ template struct HasUserDefinedConvert : std::false_type {}; template -struct HasUserDefinedConvert< - T, void_t(), std::declval(), - std::declval()))>> : std::true_type {}; +struct HasUserDefinedConvert(), + std::declval(), + std::declval()))>> + : std::true_type {}; template class StreamedWrapper; @@ -52,25 +53,36 @@ struct VoidPtr { : value(ptr ? reinterpret_cast(ptr) : 0) {} uintptr_t value; }; -ConvertResult FormatConvertImpl(VoidPtr v, ConversionSpec conv, - FormatSinkImpl* sink); + +template +struct ArgConvertResult { + bool value; +}; + +template +constexpr FormatConversionCharSet ExtractCharSet(ArgConvertResult) { + return C; +} + +using StringConvertResult = + ArgConvertResult; +ArgConvertResult FormatConvertImpl( + VoidPtr v, ConversionSpec conv, FormatSinkImpl* sink); // Strings. -ConvertResult FormatConvertImpl(const std::string& v, - ConversionSpec conv, - FormatSinkImpl* sink); -ConvertResult FormatConvertImpl(string_view v, ConversionSpec conv, - FormatSinkImpl* sink); -ConvertResult FormatConvertImpl(const char* v, - ConversionSpec conv, - FormatSinkImpl* sink); -template ::value>::type* = nullptr> -ConvertResult FormatConvertImpl(const AbslCord& value, - ConversionSpec conv, - FormatSinkImpl* sink) { - if (conv.conversion_char() != ConversionChar::s) { +StringConvertResult FormatConvertImpl(const std::string& v, ConversionSpec conv, + FormatSinkImpl* sink); +StringConvertResult FormatConvertImpl(string_view v, ConversionSpec conv, + FormatSinkImpl* sink); +ArgConvertResult +FormatConvertImpl(const char* v, ConversionSpec conv, FormatSinkImpl* sink); +template ::value>::type* = nullptr> +StringConvertResult FormatConvertImpl(const AbslCord& value, + ConversionSpec conv, + FormatSinkImpl* sink) { + if (conv.conversion_char() != FormatConversionCharInternal::s) { return {false}; } @@ -107,9 +119,12 @@ ConvertResult FormatConvertImpl(const AbslCord& value, return {true}; } -using IntegralConvertResult = - ConvertResult; -using FloatingConvertResult = ConvertResult; +using IntegralConvertResult = ArgConvertResult; +using FloatingConvertResult = + ArgConvertResult; // Floats. FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv, @@ -169,9 +184,9 @@ typename std::enable_if::value && FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink); template -ConvertResult FormatConvertImpl(const StreamedWrapper& v, - ConversionSpec conv, - FormatSinkImpl* out) { +StringConvertResult FormatConvertImpl(const StreamedWrapper& v, + ConversionSpec conv, + FormatSinkImpl* out) { std::ostringstream oss; oss << v.v_; if (!oss) return {false}; @@ -182,12 +197,12 @@ ConvertResult FormatConvertImpl(const StreamedWrapper& v, // until after FormatCountCapture is fully defined. struct FormatCountCaptureHelper { template - static ConvertResult ConvertHelper(const FormatCountCapture& v, - ConversionSpec conv, - FormatSinkImpl* sink) { + static ArgConvertResult ConvertHelper( + const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) { const absl::enable_if_t& v2 = v; - if (conv.conversion_char() != str_format_internal::ConversionChar::n) { + if (conv.conversion_char() != + str_format_internal::FormatConversionCharInternal::n) { return {false}; } *v2.p_ = static_cast(sink->size()); @@ -196,9 +211,8 @@ struct FormatCountCaptureHelper { }; template -ConvertResult FormatConvertImpl(const FormatCountCapture& v, - ConversionSpec conv, - FormatSinkImpl* sink) { +ArgConvertResult FormatConvertImpl( + const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) { return FormatCountCaptureHelper::ConvertHelper(v, conv, sink); } @@ -381,7 +395,8 @@ class FormatArgImpl { template static bool Dispatch(Data arg, ConversionSpec spec, void* out) { // A `none` conv indicates that we want the `int` conversion. - if (ABSL_PREDICT_FALSE(spec.conversion_char() == ConversionChar::kNone)) { + if (ABSL_PREDICT_FALSE(spec.conversion_char() == + FormatConversionCharInternal::kNone)) { return ToInt(arg, static_cast(out), std::is_integral(), std::is_enum()); } diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index d30fdf50..05105d8d 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -189,9 +189,8 @@ class StreamedWrapper { private: template - friend ConvertResult FormatConvertImpl(const StreamedWrapper& v, - ConversionSpec conv, - FormatSinkImpl* out); + friend ArgConvertResult FormatConvertImpl( + const StreamedWrapper& v, ConversionSpec conv, FormatSinkImpl* out); const T& v_; }; diff --git a/absl/strings/internal/str_format/checker.h b/absl/strings/internal/str_format/checker.h index 8993a79b..73ef05ff 100644 --- a/absl/strings/internal/str_format/checker.h +++ b/absl/strings/internal/str_format/checker.h @@ -25,10 +25,12 @@ constexpr bool AllOf(bool b, T... t) { } template -constexpr Conv ArgumentToConv() { - return decltype(str_format_internal::FormatConvertImpl( - std::declval(), std::declval(), - std::declval()))::kConv; +constexpr FormatConversionCharSet ArgumentToConv() { + return absl::str_format_internal::ExtractCharSet( + decltype(str_format_internal::FormatConvertImpl( + std::declval(), + std::declval(), + std::declval())){}); } #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER @@ -39,14 +41,14 @@ constexpr bool ContainsChar(const char* chars, char c) { // A constexpr compatible list of Convs. struct ConvList { - const Conv* array; + const FormatConversionCharSet* array; int count; // We do the bound check here to avoid having to do it on the callers. - // Returning an empty Conv has the same effect as short circuiting because it - // will never match any conversion. - constexpr Conv operator[](int i) const { - return i < count ? array[i] : Conv{}; + // Returning an empty FormatConversionCharSet has the same effect as + // short circuiting because it will never match any conversion. + constexpr FormatConversionCharSet operator[](int i) const { + return i < count ? array[i] : FormatConversionCharSet{}; } constexpr ConvList without_front() const { @@ -57,7 +59,7 @@ struct ConvList { template struct ConvListT { // Make sure the array has size > 0. - Conv list[count ? count : 1]; + FormatConversionCharSet list[count ? count : 1]; }; constexpr char GetChar(string_view str, size_t index) { @@ -310,7 +312,7 @@ class FormatParser { ConvList args_; }; -template +template constexpr bool ValidFormatImpl(string_view format) { return FormatParser(format, {ConvListT{{C...}}.list, sizeof...(C)}) diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index fb31a9db..f0cffe1e 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -30,7 +30,10 @@ namespace absl { ABSL_NAMESPACE_BEGIN + namespace str_format_internal { +enum class FormatConversionCharSet : uint64_t; +enum class FormatConversionChar : uint8_t; class FormatRawSinkImpl { public: @@ -149,13 +152,39 @@ struct Flags { X_VAL(g) X_SEP X_VAL(G) X_SEP X_VAL(a) X_SEP X_VAL(A) X_SEP \ /* misc */ \ X_VAL(n) X_SEP X_VAL(p) +// clang-format on -enum class FormatConversionChar : uint8_t { +// This type should not be referenced, it exists only to provide labels +// internally that match the values declared in FormatConversionChar in +// str_format.h. This is meant to allow internal libraries to use the same +// declared interface type as the public interface +// (absl::StrFormatConversionChar) while keeping the definition in a public +// header. +// Internal libraries should use the form +// `FormatConversionCharInternal::c`, `FormatConversionCharInternal::kNone` for +// comparisons. Use in switch statements is not recommended due to a bug in how +// gcc 4.9 -Wswitch handles declared but undefined enums. +struct FormatConversionCharInternal { + FormatConversionCharInternal() = delete; + + private: + // clang-format off + enum class Enum : uint8_t { c, C, s, S, // text d, i, o, u, x, X, // int f, F, e, E, g, G, a, A, // float n, p, // misc kNone + }; + // clang-format on + public: +#define ABSL_INTERNAL_X_VAL(id) \ + static constexpr FormatConversionChar id = \ + static_cast(Enum::id); + ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, ) +#undef ABSL_INTERNAL_X_VAL + static constexpr FormatConversionChar kNone = + static_cast(Enum::kNone); }; // clang-format on @@ -163,56 +192,56 @@ inline FormatConversionChar FormatConversionCharFromChar(char c) { switch (c) { #define ABSL_INTERNAL_X_VAL(id) \ case #id[0]: \ - return FormatConversionChar::id; + return FormatConversionCharInternal::id; ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, ) #undef ABSL_INTERNAL_X_VAL } - return FormatConversionChar::kNone; + return FormatConversionCharInternal::kNone; } inline bool FormatConversionCharIsUpper(FormatConversionChar c) { - switch (c) { - case FormatConversionChar::X: - case FormatConversionChar::F: - case FormatConversionChar::E: - case FormatConversionChar::G: - case FormatConversionChar::A: - return true; - default: - return false; + if (c == FormatConversionCharInternal::X || + c == FormatConversionCharInternal::F || + c == FormatConversionCharInternal::E || + c == FormatConversionCharInternal::G || + c == FormatConversionCharInternal::A) { + return true; + } else { + return false; } } inline bool FormatConversionCharIsFloat(FormatConversionChar c) { - switch (c) { - case FormatConversionChar::a: - case FormatConversionChar::e: - case FormatConversionChar::f: - case FormatConversionChar::g: - case FormatConversionChar::A: - case FormatConversionChar::E: - case FormatConversionChar::F: - case FormatConversionChar::G: - return true; - default: - return false; + if (c == FormatConversionCharInternal::a || + c == FormatConversionCharInternal::e || + c == FormatConversionCharInternal::f || + c == FormatConversionCharInternal::g || + c == FormatConversionCharInternal::A || + c == FormatConversionCharInternal::E || + c == FormatConversionCharInternal::F || + c == FormatConversionCharInternal::G) { + return true; + } else { + return false; } } inline char FormatConversionCharToChar(FormatConversionChar c) { - switch (c) { -#define ABSL_INTERNAL_X_VAL(e) \ - case FormatConversionChar::e: \ + if (c == FormatConversionCharInternal::kNone) { + return '\0'; + +#define ABSL_INTERNAL_X_VAL(e) \ + } else if (c == FormatConversionCharInternal::e) { \ return #e[0]; #define ABSL_INTERNAL_X_SEP - ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, - ABSL_INTERNAL_X_SEP) - case FormatConversionChar::kNone: - return '\0'; + ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, + ABSL_INTERNAL_X_SEP) + } else { + return '\0'; + } + #undef ABSL_INTERNAL_X_VAL #undef ABSL_INTERNAL_X_SEP - } - return '\0'; } // The associated char. @@ -224,7 +253,7 @@ inline std::ostream& operator<<(std::ostream& os, FormatConversionChar v) { struct FormatConversionSpecImplFriend; -class FormatConversionSpec { +class FormatConversionSpecImpl { public: // Width and precison are not specified, no flags are set. bool is_basic() const { return flags_.basic; } @@ -237,7 +266,7 @@ class FormatConversionSpec { FormatConversionChar conversion_char() const { // Keep this field first in the struct . It generates better code when // accessing it when ConversionSpec is passed by value in registers. - static_assert(offsetof(FormatConversionSpec, conv_) == 0, ""); + static_assert(offsetof(FormatConversionSpecImpl, conv_) == 0, ""); return conv_; } @@ -248,37 +277,62 @@ class FormatConversionSpec { // negative value. int precision() const { return precision_; } + template + T Wrap() { + return T(*this); + } + private: friend struct str_format_internal::FormatConversionSpecImplFriend; - FormatConversionChar conv_ = FormatConversionChar::kNone; + FormatConversionChar conv_ = FormatConversionCharInternal::kNone; Flags flags_; int width_; int precision_; }; struct FormatConversionSpecImplFriend final { - static void SetFlags(Flags f, FormatConversionSpec* conv) { + static void SetFlags(Flags f, FormatConversionSpecImpl* conv) { conv->flags_ = f; } static void SetConversionChar(FormatConversionChar c, - FormatConversionSpec* conv) { + FormatConversionSpecImpl* conv) { conv->conv_ = c; } - static void SetWidth(int w, FormatConversionSpec* conv) { conv->width_ = w; } - static void SetPrecision(int p, FormatConversionSpec* conv) { + static void SetWidth(int w, FormatConversionSpecImpl* conv) { + conv->width_ = w; + } + static void SetPrecision(int p, FormatConversionSpecImpl* conv) { conv->precision_ = p; } - static std::string FlagsToString(const FormatConversionSpec& spec) { + static std::string FlagsToString(const FormatConversionSpecImpl& spec) { return spec.flags_.ToString(); } }; -constexpr uint64_t FormatConversionCharToConvValue(char conv) { +// Type safe OR operator. +// We need this for two reasons: +// 1. operator| on enums makes them decay to integers and the result is an +// integer. We need the result to stay as an enum. +// 2. We use "enum class" which would not work even if we accepted the decay. +constexpr FormatConversionCharSet FormatConversionCharSetUnion( + FormatConversionCharSet a) { + return a; +} + +template +constexpr FormatConversionCharSet FormatConversionCharSetUnion( + FormatConversionCharSet a, CharSet... rest) { + return static_cast( + static_cast(a) | + static_cast(FormatConversionCharSetUnion(rest...))); +} + +constexpr uint64_t FormatConversionCharToConvInt(char conv) { return -#define ABSL_INTERNAL_CHAR_SET_CASE(c) \ - conv == #c[0] \ - ? (uint64_t{1} << (1 + static_cast(FormatConversionChar::c))) \ - : +#define ABSL_INTERNAL_CHAR_SET_CASE(c) \ + conv == #c[0] ? (uint64_t{1} << (1 + static_cast( \ + FormatConversionCharInternal::c))) \ + : ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, ) #undef ABSL_INTERNAL_CHAR_SET_CASE conv == '*' @@ -286,20 +340,31 @@ constexpr uint64_t FormatConversionCharToConvValue(char conv) { : 0; } -enum class FormatConversionCharSet : uint64_t { -#define ABSL_INTERNAL_CHAR_SET_CASE(c) \ - c = FormatConversionCharToConvValue(#c[0]), +constexpr FormatConversionCharSet FormatConversionCharToConvValue(char conv) { + return static_cast( + FormatConversionCharToConvInt(conv)); +} + +struct FormatConversionCharSetInternal { +#define ABSL_INTERNAL_CHAR_SET_CASE(c) \ + static constexpr FormatConversionCharSet c = \ + FormatConversionCharToConvValue(#c[0]); ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, ) #undef ABSL_INTERNAL_CHAR_SET_CASE // Used for width/precision '*' specification. - kStar = FormatConversionCharToConvValue('*'), - // Some predefined values: - kIntegral = d | i | u | o | x | X, - kFloating = a | e | f | g | A | E | F | G, - kNumeric = kIntegral | kFloating, - kString = s, - kPointer = p + static constexpr FormatConversionCharSet kStar = + FormatConversionCharToConvValue('*'); + + // Some predefined values (TODO(matthewbr), delete any that are unused). + static constexpr FormatConversionCharSet kIntegral = + FormatConversionCharSetUnion(d, i, u, o, x, X); + static constexpr FormatConversionCharSet kFloating = + FormatConversionCharSetUnion(a, e, f, g, A, E, F, G); + static constexpr FormatConversionCharSet kNumeric = + FormatConversionCharSetUnion(kIntegral, kFloating); + static constexpr FormatConversionCharSet kString = s; + static constexpr FormatConversionCharSet kPointer = p; }; // Type safe OR operator. @@ -309,8 +374,7 @@ enum class FormatConversionCharSet : uint64_t { // 2. We use "enum class" which would not work even if we accepted the decay. constexpr FormatConversionCharSet operator|(FormatConversionCharSet a, FormatConversionCharSet b) { - return FormatConversionCharSet(static_cast(a) | - static_cast(b)); + return FormatConversionCharSetUnion(a, b); } // Overloaded conversion functions to support absl::ParsedFormat. @@ -331,7 +395,8 @@ void ToFormatConversionCharSet(T) = delete; // Checks whether `c` exists in `set`. constexpr bool Contains(FormatConversionCharSet set, char c) { - return (static_cast(set) & FormatConversionCharToConvValue(c)) != 0; + return (static_cast(set) & + static_cast(FormatConversionCharToConvValue(c))) != 0; } // Checks whether all the characters in `c` are contained in `set` @@ -341,19 +406,6 @@ constexpr bool Contains(FormatConversionCharSet set, static_cast(c); } -// Return type of the AbslFormatConvert() functions. -// The FormatConversionCharSet template parameter is used to inform the -// framework of what conversion characters are supported by that -// AbslFormatConvert routine. -template -struct FormatConvertResult { - static constexpr FormatConversionCharSet kConv = C; - bool value; -}; - -template -constexpr FormatConversionCharSet FormatConvertResult::kConv; - // Return capacity - used, clipped to a minimum of 0. inline size_t Excess(size_t used, size_t capacity) { return used < capacity ? capacity - used : 0; @@ -361,10 +413,85 @@ inline size_t Excess(size_t used, size_t capacity) { // Type alias for use during migration. using ConversionChar = FormatConversionChar; -using ConversionSpec = FormatConversionSpec; +using ConversionSpec = FormatConversionSpecImpl; using Conv = FormatConversionCharSet; -template -using ConvertResult = FormatConvertResult; + +class FormatConversionSpec { + public: + // Width and precison are not specified, no flags are set. + bool is_basic() const { return impl_.is_basic(); } + bool has_left_flag() const { return impl_.has_left_flag(); } + bool has_show_pos_flag() const { return impl_.has_show_pos_flag(); } + bool has_sign_col_flag() const { return impl_.has_sign_col_flag(); } + bool has_alt_flag() const { return impl_.has_alt_flag(); } + bool has_zero_flag() const { return impl_.has_zero_flag(); } + + FormatConversionChar conversion_char() const { + return impl_.conversion_char(); + } + + // Returns the specified width. If width is unspecfied, it returns a negative + // value. + int width() const { return impl_.width(); } + // Returns the specified precision. If precision is unspecfied, it returns a + // negative value. + int precision() const { return impl_.precision(); } + + private: + explicit FormatConversionSpec( + str_format_internal::FormatConversionSpecImpl impl) + : impl_(impl) {} + + friend str_format_internal::FormatConversionSpecImpl; + + absl::str_format_internal::FormatConversionSpecImpl impl_; +}; + +// clang-format off +enum class FormatConversionChar : uint8_t { + c, C, s, S, // text + d, i, o, u, x, X, // int + f, F, e, E, g, G, a, A, // float + n, p // misc +}; +// clang-format on + +enum class FormatConversionCharSet : uint64_t { + // text + c = str_format_internal::FormatConversionCharToConvInt('c'), + C = str_format_internal::FormatConversionCharToConvInt('C'), + s = str_format_internal::FormatConversionCharToConvInt('s'), + S = str_format_internal::FormatConversionCharToConvInt('S'), + // integer + d = str_format_internal::FormatConversionCharToConvInt('d'), + i = str_format_internal::FormatConversionCharToConvInt('i'), + o = str_format_internal::FormatConversionCharToConvInt('o'), + u = str_format_internal::FormatConversionCharToConvInt('u'), + x = str_format_internal::FormatConversionCharToConvInt('x'), + X = str_format_internal::FormatConversionCharToConvInt('X'), + // Float + f = str_format_internal::FormatConversionCharToConvInt('f'), + F = str_format_internal::FormatConversionCharToConvInt('F'), + e = str_format_internal::FormatConversionCharToConvInt('e'), + E = str_format_internal::FormatConversionCharToConvInt('E'), + g = str_format_internal::FormatConversionCharToConvInt('g'), + G = str_format_internal::FormatConversionCharToConvInt('G'), + a = str_format_internal::FormatConversionCharToConvInt('a'), + A = str_format_internal::FormatConversionCharToConvInt('A'), + // misc + n = str_format_internal::FormatConversionCharToConvInt('n'), + p = str_format_internal::FormatConversionCharToConvInt('p'), + + // Used for width/precision '*' specification. + kStar = str_format_internal::FormatConversionCharToConvInt('*'), + + // Some predefined values: + kIntegral = d | i | u | o | x | X, + kFloating = a | e | f | g | A | E | F | G, + kNumeric = kIntegral | kFloating, + kString = s, + kPointer = p, +}; } // namespace str_format_internal diff --git a/absl/strings/internal/str_format/extension_test.cc b/absl/strings/internal/str_format/extension_test.cc index 561eaa36..0a023f9c 100644 --- a/absl/strings/internal/str_format/extension_test.cc +++ b/absl/strings/internal/str_format/extension_test.cc @@ -80,13 +80,4 @@ TEST(FormatExtensionTest, SinkAppendChars) { EXPECT_EQ(actual, expected); } } - -TEST(FormatExtensionTest, CustomSink) { - my_namespace::UserDefinedType sink; - absl::Format(&sink, "There were %04d little %s.", 3, "pigs"); - EXPECT_EQ("There were 0003 little pigs.", sink.Value()); - absl::Format(&sink, "And %-3llx bad wolf!", 1); - EXPECT_EQ("There were 0003 little pigs.And 1 bad wolf!", sink.Value()); -} - } // namespace diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index d5a1ee40..d6858cff 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -403,70 +403,62 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, Buffer buffer; - switch (conv.conversion_char()) { - case ConversionChar::f: - case ConversionChar::F: - if (!FloatToBuffer(decomposed, precision, &buffer, - nullptr)) { - return FallbackToSnprintf(v, conv, sink); - } - if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); - break; + FormatConversionChar c = conv.conversion_char(); - case ConversionChar::e: - case ConversionChar::E: - if (!FloatToBuffer(decomposed, precision, &buffer, - &exp)) { - return FallbackToSnprintf(v, conv, sink); + if (c == FormatConversionCharInternal::f || + c == FormatConversionCharInternal::F) { + if (!FloatToBuffer(decomposed, precision, &buffer, + nullptr)) { + return FallbackToSnprintf(v, conv, sink); + } + if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); + } else if (c == FormatConversionCharInternal::e || + c == FormatConversionCharInternal::E) { + if (!FloatToBuffer(decomposed, precision, &buffer, + &exp)) { + return FallbackToSnprintf(v, conv, sink); + } + if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); + PrintExponent( + exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', + &buffer); + } else if (c == FormatConversionCharInternal::g || + c == FormatConversionCharInternal::G) { + precision = std::max(0, precision - 1); + if (!FloatToBuffer(decomposed, precision, &buffer, + &exp)) { + return FallbackToSnprintf(v, conv, sink); + } + if (precision + 1 > exp && exp >= -4) { + if (exp < 0) { + // Have 1.23456, needs 0.00123456 + // Move the first digit + buffer.begin[1] = *buffer.begin; + // Add some zeros + for (; exp < -1; ++exp) *buffer.begin-- = '0'; + *buffer.begin-- = '.'; + *buffer.begin = '0'; + } else if (exp > 0) { + // Have 1.23456, needs 1234.56 + // Move the '.' exp positions to the right. + std::rotate(buffer.begin + 1, buffer.begin + 2, buffer.begin + exp + 2); } - if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); + exp = 0; + } + if (!conv.has_alt_flag()) { + while (buffer.back() == '0') buffer.pop_back(); + if (buffer.back() == '.') buffer.pop_back(); + } + if (exp) { PrintExponent( exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', &buffer); - break; - - case ConversionChar::g: - case ConversionChar::G: - precision = std::max(0, precision - 1); - if (!FloatToBuffer(decomposed, precision, &buffer, - &exp)) { - return FallbackToSnprintf(v, conv, sink); - } - if (precision + 1 > exp && exp >= -4) { - if (exp < 0) { - // Have 1.23456, needs 0.00123456 - // Move the first digit - buffer.begin[1] = *buffer.begin; - // Add some zeros - for (; exp < -1; ++exp) *buffer.begin-- = '0'; - *buffer.begin-- = '.'; - *buffer.begin = '0'; - } else if (exp > 0) { - // Have 1.23456, needs 1234.56 - // Move the '.' exp positions to the right. - std::rotate(buffer.begin + 1, buffer.begin + 2, - buffer.begin + exp + 2); - } - exp = 0; - } - if (!conv.has_alt_flag()) { - while (buffer.back() == '0') buffer.pop_back(); - if (buffer.back() == '.') buffer.pop_back(); - } - if (exp) { - PrintExponent( - exp, - FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e', - &buffer); - } - break; - - case ConversionChar::a: - case ConversionChar::A: - return FallbackToSnprintf(v, conv, sink); - - default: - return false; + } + } else if (c == FormatConversionCharInternal::a || + c == FormatConversionCharInternal::A) { + return FallbackToSnprintf(v, conv, sink); + } else { + return false; } WriteBufferToSink(sign_char, diff --git a/absl/strings/internal/str_format/parser.cc b/absl/strings/internal/str_format/parser.cc index aab68db9..61132739 100644 --- a/absl/strings/internal/str_format/parser.cc +++ b/absl/strings/internal/str_format/parser.cc @@ -17,7 +17,7 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { -using CC = ConversionChar; +using CC = FormatConversionCharInternal; using LM = LengthMod; ABSL_CONST_INIT const ConvTag kTags[256] = { @@ -296,15 +296,17 @@ struct ParsedFormatBase::ParsedFormatConsumer { char* data_pos; }; -ParsedFormatBase::ParsedFormatBase(string_view format, bool allow_ignored, - std::initializer_list convs) +ParsedFormatBase::ParsedFormatBase( + string_view format, bool allow_ignored, + std::initializer_list convs) : data_(format.empty() ? nullptr : new char[format.size()]) { has_error_ = !ParseFormatString(format, ParsedFormatConsumer(this)) || !MatchesConversions(allow_ignored, convs); } bool ParsedFormatBase::MatchesConversions( - bool allow_ignored, std::initializer_list convs) const { + bool allow_ignored, + std::initializer_list convs) const { std::unordered_set used; auto add_if_valid_conv = [&](int pos, char c) { if (static_cast(pos) > convs.size() || diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index 7d966517..fd2dc970 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -67,7 +67,7 @@ struct UnboundConversion { Flags flags; LengthMod length_mod = LengthMod::none; - ConversionChar conv = FormatConversionChar::kNone; + FormatConversionChar conv = FormatConversionCharInternal::kNone; }; // Consume conversion spec prefix (not including '%') of [p, end) if valid. @@ -186,8 +186,9 @@ constexpr bool EnsureConstexpr(string_view s) { class ParsedFormatBase { public: - explicit ParsedFormatBase(string_view format, bool allow_ignored, - std::initializer_list convs); + explicit ParsedFormatBase( + string_view format, bool allow_ignored, + std::initializer_list convs); ParsedFormatBase(const ParsedFormatBase& other) { *this = other; } @@ -234,8 +235,9 @@ class ParsedFormatBase { private: // Returns whether the conversions match and if !allow_ignored it verifies // that all conversions are used by the format. - bool MatchesConversions(bool allow_ignored, - std::initializer_list convs) const; + bool MatchesConversions( + bool allow_ignored, + std::initializer_list convs) const; struct ParsedFormatConsumer; diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 51eb53f5..26f5bec6 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -46,13 +46,13 @@ TEST(ConversionCharTest, Names) { }; // clang-format off const Expectation kExpect[] = { -#define X(c) {ConversionChar::c, #c[0]} +#define X(c) {FormatConversionCharInternal::c, #c[0]} X(c), X(C), X(s), X(S), // text X(d), X(i), X(o), X(u), X(x), X(X), // int X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float X(n), X(p), // misc #undef X - {ConversionChar::kNone, '\0'}, + {FormatConversionCharInternal::kNone, '\0'}, }; // clang-format on for (auto e : kExpect) { @@ -349,7 +349,8 @@ TEST_F(ParsedFormatTest, ValueSemantics) { ParsedFormatBase p2 = p1; // copy construct (empty) EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2)); - p1 = ParsedFormatBase("hello%s", true, {Conv::s}); // move assign + p1 = ParsedFormatBase("hello%s", true, + {FormatConversionCharSetInternal::s}); // move assign EXPECT_EQ("[hello]{s:1$s}", SummarizeParsedFormat(p1)); ParsedFormatBase p3 = p1; // copy construct (nonempty) @@ -377,9 +378,9 @@ TEST_F(ParsedFormatTest, Parsing) { const ExpectParse kExpect[] = { {"", {}, ""}, {"ab", {}, "[ab]"}, - {"a%d", {Conv::d}, "[a]{d:1$d}"}, - {"a%+d", {Conv::d}, "[a]{+d:1$d}"}, - {"a% d", {Conv::d}, "[a]{ d:1$d}"}, + {"a%d", {FormatConversionCharSetInternal::d}, "[a]{d:1$d}"}, + {"a%+d", {FormatConversionCharSetInternal::d}, "[a]{+d:1$d}"}, + {"a% d", {FormatConversionCharSetInternal::d}, "[a]{ d:1$d}"}, {"a%b %d", {}, "[a]!"}, // stop after error }; for (const auto& e : kExpect) { @@ -391,13 +392,13 @@ TEST_F(ParsedFormatTest, Parsing) { TEST_F(ParsedFormatTest, ParsingFlagOrder) { const ExpectParse kExpect[] = { - {"a%+ 0d", {Conv::d}, "[a]{+ 0d:1$d}"}, - {"a%+0 d", {Conv::d}, "[a]{+0 d:1$d}"}, - {"a%0+ d", {Conv::d}, "[a]{0+ d:1$d}"}, - {"a% +0d", {Conv::d}, "[a]{ +0d:1$d}"}, - {"a%0 +d", {Conv::d}, "[a]{0 +d:1$d}"}, - {"a% 0+d", {Conv::d}, "[a]{ 0+d:1$d}"}, - {"a%+ 0+d", {Conv::d}, "[a]{+ 0+d:1$d}"}, + {"a%+ 0d", {FormatConversionCharSetInternal::d}, "[a]{+ 0d:1$d}"}, + {"a%+0 d", {FormatConversionCharSetInternal::d}, "[a]{+0 d:1$d}"}, + {"a%0+ d", {FormatConversionCharSetInternal::d}, "[a]{0+ d:1$d}"}, + {"a% +0d", {FormatConversionCharSetInternal::d}, "[a]{ +0d:1$d}"}, + {"a%0 +d", {FormatConversionCharSetInternal::d}, "[a]{0 +d:1$d}"}, + {"a% 0+d", {FormatConversionCharSetInternal::d}, "[a]{ 0+d:1$d}"}, + {"a%+ 0+d", {FormatConversionCharSetInternal::d}, "[a]{+ 0+d:1$d}"}, }; for (const auto& e : kExpect) { SCOPED_TRACE(e.in); diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index f0d1f0ad..160f4c61 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -450,7 +450,7 @@ struct SummarizeConsumer { if (conv.precision.is_from_arg()) { *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*"; } - *out += FormatConversionCharToChar(conv.conv); + *out += str_format_internal::FormatConversionCharToChar(conv.conv); *out += "}"; return true; } diff --git a/absl/synchronization/internal/create_thread_identity.cc b/absl/synchronization/internal/create_thread_identity.cc index fa0070a9..53a71b34 100644 --- a/absl/synchronization/internal/create_thread_identity.cc +++ b/absl/synchronization/internal/create_thread_identity.cc @@ -32,9 +32,9 @@ namespace synchronization_internal { // ThreadIdentity storage is persistent, we maintain a free-list of previously // released ThreadIdentity objects. -static base_internal::SpinLock freelist_lock( - base_internal::kLinkerInitialized); -static base_internal::ThreadIdentity* thread_identity_freelist; +ABSL_CONST_INIT static base_internal::SpinLock freelist_lock( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); +ABSL_CONST_INIT static base_internal::ThreadIdentity* thread_identity_freelist; // A per-thread destructor for reclaiming associated ThreadIdentity objects. // Since we must preserve their storage we cache them for re-use. diff --git a/absl/synchronization/internal/graphcycles.cc b/absl/synchronization/internal/graphcycles.cc index 6a2bcdf6..19f9aab5 100644 --- a/absl/synchronization/internal/graphcycles.cc +++ b/absl/synchronization/internal/graphcycles.cc @@ -51,9 +51,9 @@ namespace { // Avoid LowLevelAlloc's default arena since it calls malloc hooks in // which people are doing things like acquiring Mutexes. -static absl::base_internal::SpinLock arena_mu( - absl::base_internal::kLinkerInitialized); -static base_internal::LowLevelAlloc::Arena* arena; +ABSL_CONST_INIT static absl::base_internal::SpinLock arena_mu( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); +ABSL_CONST_INIT static base_internal::LowLevelAlloc::Arena* arena; static void InitArenaIfNecessary() { arena_mu.Lock(); diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 8cda5a1c..1f8a696e 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc @@ -207,12 +207,12 @@ static void AtomicClearBits(std::atomic* pv, intptr_t bits, //------------------------------------------------------------------ // Data for doing deadlock detection. -static absl::base_internal::SpinLock deadlock_graph_mu( - absl::base_internal::kLinkerInitialized); +ABSL_CONST_INIT static absl::base_internal::SpinLock deadlock_graph_mu( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); -// graph used to detect deadlocks. -static GraphCycles *deadlock_graph ABSL_GUARDED_BY(deadlock_graph_mu) - ABSL_PT_GUARDED_BY(deadlock_graph_mu); +// Graph used to detect deadlocks. +ABSL_CONST_INIT static GraphCycles *deadlock_graph + ABSL_GUARDED_BY(deadlock_graph_mu) ABSL_PT_GUARDED_BY(deadlock_graph_mu); //------------------------------------------------------------------ // An event mechanism for debugging mutex use. @@ -273,13 +273,12 @@ static const struct { {0, "SignalAll on "}, }; -static absl::base_internal::SpinLock synch_event_mu( - absl::base_internal::kLinkerInitialized); -// protects synch_event +ABSL_CONST_INIT static absl::base_internal::SpinLock synch_event_mu( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); // Hash table size; should be prime > 2. // Can't be too small, as it's used for deadlock detection information. -static const uint32_t kNSynchEvent = 1031; +static constexpr uint32_t kNSynchEvent = 1031; static struct SynchEvent { // this is a trivial hash table for the events // struct is freed when refcount reaches 0 diff --git a/absl/time/clock.cc b/absl/time/clock.cc index 3b895c38..e5c423c7 100644 --- a/absl/time/clock.cc +++ b/absl/time/clock.cc @@ -226,9 +226,9 @@ static_assert(((kMinNSBetweenSamples << (kScale + 1)) >> (kScale + 1)) == // A reader-writer lock protecting the static locations below. // See SeqAcquire() and SeqRelease() above. -static absl::base_internal::SpinLock lock( - absl::base_internal::kLinkerInitialized); -static std::atomic seq(0); +ABSL_CONST_INIT static absl::base_internal::SpinLock lock( + absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); +ABSL_CONST_INIT static std::atomic seq(0); // data from a sample of the kernel's time value struct TimeSampleAtomic { diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc index d30a644e..a402760d 100644 --- a/absl/time/internal/cctz/src/cctz_benchmark.cc +++ b/absl/time/internal/cctz/src/cctz_benchmark.cc @@ -280,6 +280,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan", "America/North_Dakota/Beulah", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", + "America/Nuuk", "America/Ojinaga", "America/Panama", "America/Pangnirtung", diff --git a/absl/time/internal/cctz/src/time_zone_lookup_test.cc b/absl/time/internal/cctz/src/time_zone_lookup_test.cc index 35911ce5..0b0c1a3b 100644 --- a/absl/time/internal/cctz/src/time_zone_lookup_test.cc +++ b/absl/time/internal/cctz/src/time_zone_lookup_test.cc @@ -211,6 +211,7 @@ const char* const kTimeZoneNames[] = {"Africa/Abidjan", "America/North_Dakota/Beulah", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", + "America/Nuuk", "America/Ojinaga", "America/Panama", "America/Pangnirtung", -- cgit v1.2.3 From d85783fd0b1bb32b3d3e04d18367cec8d96c9e9a Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 5 May 2020 07:54:14 -0700 Subject: Export of internal Abseil changes -- f34cd235a12ad0ee1fea3a1ee5a427272dc2b285 by Abseil Team : Migrates uses of deprecated map types to recommended types. PiperOrigin-RevId: 309945156 -- e3410a47ad32c0775b6911610bc47b22938decad by Matthew Brown : Internal Change PiperOrigin-RevId: 309856021 -- a58cfa25e0bb59e7fa9647ac1aae65eaccff0086 by Greg Falcon : Internal change. PiperOrigin-RevId: 309804612 -- cdc5ec310035fbe25f496bda283fe655d94d7769 by Mark Barolak : Standardize the header comments for friend functions in cord.h PiperOrigin-RevId: 309779073 -- fe61602701be795e54477b0fdbf5ffc1df12a6b7 by Samuel Benzaquen : Implement %f natively for any input. It evaluates the input at runtime and allocates stack space accordingly. This removes a potential fallback into snprintf, improves performance, and removes all memory allocations in this formatting path. PiperOrigin-RevId: 309752501 -- 79e2a24f3f959e8b06ddf1d440bbabbd5f89b5b7 by Greg Falcon : Add a Cord::swap() method. Many other Abseil types already provide this, but it was missing here. We already provided a two-argument free function form of `swap()`, but that API is better suited for generic code. The swap member function is a better API when the types are known. PiperOrigin-RevId: 309751740 -- 85cdf60024f153fb4fcb7fe68ed2b14b9faf119d by Derek Mauro : Cleanup uses of "linker initialized" SpinLocks PiperOrigin-RevId: 309581867 -- 9e5443bfcec4b94056b13c75326576e987ab88fb by Matt Kulukundis : Clarify intended mixing properties of `absl::Hash` PiperOrigin-RevId: 309520174 -- a0630f0827b67f217aaeae68a448fe4c1101e17d by Greg Falcon : Comment out a test in Emscripten to sidestep `long double` issues. PiperOrigin-RevId: 309482953 GitOrigin-RevId: f34cd235a12ad0ee1fea3a1ee5a427272dc2b285 Change-Id: Icce0c9d547117374d596b9d684e4054ddd118669 --- absl/base/BUILD.bazel | 5 +- absl/base/CMakeLists.txt | 1 + absl/base/internal/low_level_alloc_test.cc | 4 +- absl/debugging/symbolize_elf.inc | 6 +- absl/hash/hash.h | 7 +- absl/random/gaussian_distribution_test.cc | 5 +- absl/random/internal/wide_multiply_test.cc | 2 +- absl/strings/BUILD.bazel | 5 +- absl/strings/CMakeLists.txt | 1 + absl/strings/cord.h | 21 +- absl/strings/cord_test.cc | 3 + absl/strings/internal/str_format/arg.cc | 112 ++-- absl/strings/internal/str_format/arg.h | 78 ++- absl/strings/internal/str_format/arg_test.cc | 5 +- absl/strings/internal/str_format/bind.h | 7 +- absl/strings/internal/str_format/checker_test.cc | 2 +- absl/strings/internal/str_format/convert_test.cc | 245 +++++++- absl/strings/internal/str_format/extension.h | 5 - .../internal/str_format/float_conversion.cc | 694 ++++++++++++++++++++- .../strings/internal/str_format/float_conversion.h | 6 +- absl/strings/internal/str_format/parser.h | 8 +- absl/strings/internal/str_format/parser_test.cc | 6 +- absl/strings/str_format_test.cc | 69 +- absl/strings/substitute.h | 2 +- 24 files changed, 1107 insertions(+), 192 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 1af9e45e..1664a351 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -541,7 +541,10 @@ cc_test( copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, tags = ["no_test_ios_x86_64"], - deps = [":malloc_internal"], + deps = [ + ":malloc_internal", + "//absl/container:node_hash_map", + ], ) cc_test( diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index a63b591c..2df2e971 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt @@ -497,6 +497,7 @@ absl_cc_test( ${ABSL_TEST_COPTS} DEPS absl::malloc_internal + absl::node_hash_map Threads::Threads ) diff --git a/absl/base/internal/low_level_alloc_test.cc b/absl/base/internal/low_level_alloc_test.cc index 7abbbf9c..2f2eaffa 100644 --- a/absl/base/internal/low_level_alloc_test.cc +++ b/absl/base/internal/low_level_alloc_test.cc @@ -21,6 +21,8 @@ #include #include +#include "absl/container/node_hash_map.h" + namespace absl { ABSL_NAMESPACE_BEGIN namespace base_internal { @@ -75,7 +77,7 @@ static bool using_low_level_alloc = false; // allocations and deallocations are reported via the MallocHook // interface. static void Test(bool use_new_arena, bool call_malloc_hook, int n) { - typedef std::unordered_map AllocMap; + typedef absl::node_hash_map AllocMap; AllocMap allocated; AllocMap::iterator it; BlockDesc block_desc; diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index fe1d36ee..ec86f9a9 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc @@ -149,13 +149,15 @@ struct FileMappingHint { // Moreover, we are using only TryLock(), if the decorator list // is being modified (is busy), we skip all decorators, and possibly // loose some info. Sorry, that's the best we could do. -base_internal::SpinLock g_decorators_mu(base_internal::kLinkerInitialized); +ABSL_CONST_INIT absl::base_internal::SpinLock g_decorators_mu( + absl::kConstInit, absl::base_internal::SCHEDULE_KERNEL_ONLY); const int kMaxFileMappingHints = 8; int g_num_file_mapping_hints; FileMappingHint g_file_mapping_hints[kMaxFileMappingHints]; // Protects g_file_mapping_hints. -base_internal::SpinLock g_file_mapping_mu(base_internal::kLinkerInitialized); +ABSL_CONST_INIT absl::base_internal::SpinLock g_file_mapping_mu( + absl::kConstInit, absl::base_internal::SCHEDULE_KERNEL_ONLY); // Async-signal-safe function to zero a buffer. // memset() is not guaranteed to be async-signal-safe. diff --git a/absl/hash/hash.h b/absl/hash/hash.h index 3dbeab69..d7386f6c 100644 --- a/absl/hash/hash.h +++ b/absl/hash/hash.h @@ -37,8 +37,11 @@ // types. Hashing of that combined state is separately done by `absl::Hash`. // // One should assume that a hash algorithm is chosen randomly at the start of -// each process. E.g., absl::Hash()(9) in one process and -// absl::Hash()(9) in another process are likely to differ. +// each process. E.g., `absl::Hash{}(9)` in one process and +// `absl::Hash{}(9)` in another process are likely to differ. +// +// `absl::Hash` is intended to strongly mix input bits with a target of passing +// an [Avalanche Test](https://en.wikipedia.org/wiki/Avalanche_effect). // // Example: // diff --git a/absl/random/gaussian_distribution_test.cc b/absl/random/gaussian_distribution_test.cc index 49c07513..398f0131 100644 --- a/absl/random/gaussian_distribution_test.cc +++ b/absl/random/gaussian_distribution_test.cc @@ -130,12 +130,15 @@ TYPED_TEST(GaussianDistributionInterfaceTest, SerializeTest) { ss >> after; #if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \ - defined(__ppc__) || defined(__PPC__) + defined(__ppc__) || defined(__PPC__) || defined(__EMSCRIPTEN__) if (std::is_same::value) { // Roundtripping floating point values requires sufficient precision // to reconstruct the exact value. It turns out that long double // has some errors doing this on ppc, particularly for values // near {1.0 +/- epsilon}. + // + // Emscripten is even worse, implementing long double as a 128-bit + // type, but shipping with a strtold() that doesn't support that. if (mean <= std::numeric_limits::max() && mean >= std::numeric_limits::lowest()) { EXPECT_EQ(static_cast(before.mean()), diff --git a/absl/random/internal/wide_multiply_test.cc b/absl/random/internal/wide_multiply_test.cc index 922603f2..ca8ce923 100644 --- a/absl/random/internal/wide_multiply_test.cc +++ b/absl/random/internal/wide_multiply_test.cc @@ -28,7 +28,7 @@ TEST(WideMultiplyTest, MultiplyU64ToU128Test) { EXPECT_EQ(absl::uint128(0), MultiplyU64ToU128(0, 0)); - // Max uint64 + // Max uint64_t EXPECT_EQ(MultiplyU64ToU128(kMax, kMax), absl::MakeUint128(0xfffffffffffffffe, 0x0000000000000001)); EXPECT_EQ(absl::MakeUint128(0, kMax), MultiplyU64ToU128(kMax, 1)); diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index 8aecbe59..8220896d 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -638,10 +638,13 @@ cc_library( visibility = ["//visibility:private"], deps = [ ":strings", + "//absl/base:bits", "//absl/base:config", "//absl/base:core_headers", + "//absl/functional:function_ref", "//absl/meta:type_traits", "//absl/numeric:int128", + "//absl/types:optional", "//absl/types:span", ], ) @@ -718,7 +721,7 @@ cc_test( deps = [ ":str_format_internal", "//absl/base:raw_logging_internal", - "//absl/numeric:int128", + "//absl/types:optional", "@com_google_googletest//:gtest_main", ], ) diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt index 003794f9..c0ea0c8e 100644 --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -392,6 +392,7 @@ absl_cc_library( COPTS ${ABSL_DEFAULT_COPTS} DEPS + absl::bits absl::strings absl::config absl::core_headers diff --git a/absl/strings/cord.h b/absl/strings/cord.h index ae3d2e71..86ae76fd 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -162,7 +162,7 @@ class Cord { if (contents_.is_tree()) DestroyCordSlow(); } - // Cord::MakeCordFromExternal(data, callable) + // MakeCordFromExternal() // // Creates a Cord that takes ownership of external string memory. The // contents of `data` are not copied to the Cord; instead, the external @@ -246,10 +246,17 @@ class Cord { // (pos + new_size) >= size(), the result is the subrange [pos, size()). Cord Subcord(size_t pos, size_t new_size) const; + // Cord::swap() + // + // Swaps the contents of the Cord with `other`. + void swap(Cord& other) noexcept; + // swap() // - // Swaps the data of Cord `x` with Cord `y`. - friend void swap(Cord& x, Cord& y) noexcept; + // Swaps the contents of two Cords. + friend void swap(Cord& x, Cord& y) noexcept { + x.swap(y); + } // Cord::size() // @@ -1032,6 +1039,10 @@ inline Cord& Cord::operator=(const Cord& x) { inline Cord::Cord(Cord&& src) noexcept : contents_(std::move(src.contents_)) {} +inline void Cord::swap(Cord& other) noexcept { + contents_.Swap(&other.contents_); +} + inline Cord& Cord::operator=(Cord&& x) noexcept { contents_ = std::move(x.contents_); return *this; @@ -1308,10 +1319,6 @@ inline bool operator<=(absl::string_view x, const Cord& y) { return !(y < x); } inline bool operator>=(const Cord& x, absl::string_view y) { return !(x < y); } inline bool operator>=(absl::string_view x, const Cord& y) { return !(x < y); } -// Overload of swap for Cord. The use of non-const references is -// required. :( -inline void swap(Cord& x, Cord& y) noexcept { y.contents_.Swap(&x.contents_); } - // Some internals exposed to test code. namespace strings_internal { class CordTestAccess { diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index 336cedde..4443c828 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -396,6 +396,9 @@ TEST(Cord, Swap) { swap(x, y); ASSERT_EQ(x, absl::Cord(b)); ASSERT_EQ(y, absl::Cord(a)); + x.swap(y); + ASSERT_EQ(x, absl::Cord(a)); + ASSERT_EQ(y, absl::Cord(b)); } static void VerifyCopyToString(const absl::Cord& cord) { diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index a112071c..964f25f7 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -167,24 +167,26 @@ class IntDigits { // Note: 'o' conversions do not have a base indicator, it's just that // the '#' flag is specified to modify the precision for 'o' conversions. string_view BaseIndicator(const IntDigits &as_digits, - const ConversionSpec conv) { + const FormatConversionSpecImpl conv) { // always show 0x for %p. - bool alt = conv.has_alt_flag() || conv.conversion_char() == ConversionChar::p; - bool hex = (conv.conversion_char() == FormatConversionChar::x || - conv.conversion_char() == FormatConversionChar::X || - conv.conversion_char() == FormatConversionChar::p); + bool alt = conv.has_alt_flag() || + conv.conversion_char() == FormatConversionCharInternal::p; + bool hex = (conv.conversion_char() == FormatConversionCharInternal::x || + conv.conversion_char() == FormatConversionCharInternal::X || + conv.conversion_char() == FormatConversionCharInternal::p); // From the POSIX description of '#' flag: // "For x or X conversion specifiers, a non-zero result shall have // 0x (or 0X) prefixed to it." if (alt && hex && !as_digits.without_neg_or_zero().empty()) { - return conv.conversion_char() == FormatConversionChar::X ? "0X" : "0x"; + return conv.conversion_char() == FormatConversionCharInternal::X ? "0X" + : "0x"; } return {}; } -string_view SignColumn(bool neg, const ConversionSpec conv) { - if (conv.conversion_char() == FormatConversionChar::d || - conv.conversion_char() == FormatConversionChar::i) { +string_view SignColumn(bool neg, const FormatConversionSpecImpl conv) { + if (conv.conversion_char() == FormatConversionCharInternal::d || + conv.conversion_char() == FormatConversionCharInternal::i) { if (neg) return "-"; if (conv.has_show_pos_flag()) return "+"; if (conv.has_sign_col_flag()) return " "; @@ -192,7 +194,7 @@ string_view SignColumn(bool neg, const ConversionSpec conv) { return {}; } -bool ConvertCharImpl(unsigned char v, const ConversionSpec conv, +bool ConvertCharImpl(unsigned char v, const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { size_t fill = 0; if (conv.width() >= 0) fill = conv.width(); @@ -204,7 +206,8 @@ bool ConvertCharImpl(unsigned char v, const ConversionSpec conv, } bool ConvertIntImplInnerSlow(const IntDigits &as_digits, - const ConversionSpec conv, FormatSinkImpl *sink) { + const FormatConversionSpecImpl conv, + FormatSinkImpl *sink) { // Print as a sequence of Substrings: // [left_spaces][sign][base_indicator][zeroes][formatted][right_spaces] size_t fill = 0; @@ -224,7 +227,8 @@ bool ConvertIntImplInnerSlow(const IntDigits &as_digits, if (!precision_specified) precision = 1; - if (conv.has_alt_flag() && conv.conversion_char() == ConversionChar::o) { + if (conv.has_alt_flag() && + conv.conversion_char() == FormatConversionCharInternal::o) { // From POSIX description of the '#' (alt) flag: // "For o conversion, it increases the precision (if necessary) to // force the first digit of the result to be zero." @@ -258,42 +262,43 @@ bool ConvertIntImplInnerSlow(const IntDigits &as_digits, } template -bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { +bool ConvertIntArg(T v, const FormatConversionSpecImpl conv, + FormatSinkImpl *sink) { using U = typename MakeUnsigned::type; IntDigits as_digits; switch (conv.conversion_char()) { - case FormatConversionChar::c: + case FormatConversionCharInternal::c: return ConvertCharImpl(static_cast(v), conv, sink); - case FormatConversionChar::o: + case FormatConversionCharInternal::o: as_digits.PrintAsOct(static_cast(v)); break; - case FormatConversionChar::x: + case FormatConversionCharInternal::x: as_digits.PrintAsHexLower(static_cast(v)); break; - case FormatConversionChar::X: + case FormatConversionCharInternal::X: as_digits.PrintAsHexUpper(static_cast(v)); break; - case FormatConversionChar::u: + case FormatConversionCharInternal::u: as_digits.PrintAsDec(static_cast(v)); break; - case FormatConversionChar::d: - case FormatConversionChar::i: + case FormatConversionCharInternal::d: + case FormatConversionCharInternal::i: as_digits.PrintAsDec(v); break; - case FormatConversionChar::a: - case FormatConversionChar::e: - case FormatConversionChar::f: - case FormatConversionChar::g: - case FormatConversionChar::A: - case FormatConversionChar::E: - case FormatConversionChar::F: - case FormatConversionChar::G: + case FormatConversionCharInternal::a: + case FormatConversionCharInternal::e: + case FormatConversionCharInternal::f: + case FormatConversionCharInternal::g: + case FormatConversionCharInternal::A: + case FormatConversionCharInternal::E: + case FormatConversionCharInternal::F: + case FormatConversionCharInternal::G: return ConvertFloatImpl(static_cast(v), conv, sink); default: @@ -308,12 +313,13 @@ bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { } template -bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) { +bool ConvertFloatArg(T v, const FormatConversionSpecImpl conv, + FormatSinkImpl *sink) { return FormatConversionCharIsFloat(conv.conversion_char()) && ConvertFloatImpl(v, conv, sink); } -inline bool ConvertStringArg(string_view v, const ConversionSpec conv, +inline bool ConvertStringArg(string_view v, const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { if (conv.conversion_char() != FormatConversionCharInternal::s) return false; if (conv.is_basic()) { @@ -328,19 +334,20 @@ inline bool ConvertStringArg(string_view v, const ConversionSpec conv, // ==================== Strings ==================== StringConvertResult FormatConvertImpl(const std::string &v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } -StringConvertResult FormatConvertImpl(string_view v, const ConversionSpec conv, +StringConvertResult FormatConvertImpl(string_view v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertStringArg(v, conv, sink)}; } ArgConvertResult -FormatConvertImpl(const char *v, const ConversionSpec conv, +FormatConvertImpl(const char *v, const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { if (conv.conversion_char() == FormatConversionCharInternal::p) return {FormatConvertImpl(VoidPtr(v), conv, sink).value}; @@ -358,7 +365,7 @@ FormatConvertImpl(const char *v, const ConversionSpec conv, // ==================== Raw pointers ==================== ArgConvertResult FormatConvertImpl( - VoidPtr v, const ConversionSpec conv, FormatSinkImpl *sink) { + VoidPtr v, const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { if (conv.conversion_char() != FormatConversionCharInternal::p) return {false}; if (!v.value) { sink->Append("(nil)"); @@ -370,82 +377,87 @@ ArgConvertResult FormatConvertImpl( } // ==================== Floats ==================== -FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec conv, +FloatingConvertResult FormatConvertImpl(float v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } -FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec conv, +FloatingConvertResult FormatConvertImpl(double v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } FloatingConvertResult FormatConvertImpl(long double v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertFloatArg(v, conv, sink)}; } // ==================== Chars ==================== -IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(char v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(signed char v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned char v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } // ==================== Ints ==================== IntegralConvertResult FormatConvertImpl(short v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } -IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(int v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } -IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(unsigned v, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(long v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(long long v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(absl::int128 v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } IntegralConvertResult FormatConvertImpl(absl::uint128 v, - const ConversionSpec conv, + const FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return {ConvertIntArg(v, conv, sink)}; } diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index f4ac940a..9a1e5ef2 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -67,20 +67,24 @@ constexpr FormatConversionCharSet ExtractCharSet(ArgConvertResult) { using StringConvertResult = ArgConvertResult; ArgConvertResult FormatConvertImpl( - VoidPtr v, ConversionSpec conv, FormatSinkImpl* sink); + VoidPtr v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); // Strings. -StringConvertResult FormatConvertImpl(const std::string& v, ConversionSpec conv, +StringConvertResult FormatConvertImpl(const std::string& v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); -StringConvertResult FormatConvertImpl(string_view v, ConversionSpec conv, +StringConvertResult FormatConvertImpl(string_view v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); ArgConvertResult -FormatConvertImpl(const char* v, ConversionSpec conv, FormatSinkImpl* sink); +FormatConvertImpl(const char* v, const FormatConversionSpecImpl conv, + FormatSinkImpl* sink); + template ::value>::type* = nullptr> StringConvertResult FormatConvertImpl(const AbslCord& value, - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink) { if (conv.conversion_char() != FormatConversionCharInternal::s) { return {false}; @@ -127,50 +131,55 @@ using FloatingConvertResult = ArgConvertResult; // Floats. -FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv, +FloatingConvertResult FormatConvertImpl(float v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); -FloatingConvertResult FormatConvertImpl(double v, ConversionSpec conv, +FloatingConvertResult FormatConvertImpl(double v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); -FloatingConvertResult FormatConvertImpl(long double v, ConversionSpec conv, +FloatingConvertResult FormatConvertImpl(long double v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); // Chars. -IntegralConvertResult FormatConvertImpl(char v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(char v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(signed char v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(signed char v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(unsigned char v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(unsigned char v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); // Ints. IntegralConvertResult FormatConvertImpl(short v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(int v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(int v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(unsigned v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(unsigned v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(long v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(long long v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(int128 v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(int128 v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); -IntegralConvertResult FormatConvertImpl(uint128 v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(uint128 v, + FormatConversionSpecImpl conv, FormatSinkImpl* sink); template ::value, int> = 0> -IntegralConvertResult FormatConvertImpl(T v, ConversionSpec conv, +IntegralConvertResult FormatConvertImpl(T v, FormatConversionSpecImpl conv, FormatSinkImpl* sink) { return FormatConvertImpl(static_cast(v), conv, sink); } @@ -181,11 +190,11 @@ template typename std::enable_if::value && !HasUserDefinedConvert::value, IntegralConvertResult>::type -FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink); +FormatConvertImpl(T v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); template StringConvertResult FormatConvertImpl(const StreamedWrapper& v, - ConversionSpec conv, + FormatConversionSpecImpl conv, FormatSinkImpl* out) { std::ostringstream oss; oss << v.v_; @@ -198,7 +207,8 @@ StringConvertResult FormatConvertImpl(const StreamedWrapper& v, struct FormatCountCaptureHelper { template static ArgConvertResult ConvertHelper( - const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) { + const FormatCountCapture& v, FormatConversionSpecImpl conv, + FormatSinkImpl* sink) { const absl::enable_if_t& v2 = v; if (conv.conversion_char() != @@ -212,7 +222,8 @@ struct FormatCountCaptureHelper { template ArgConvertResult FormatConvertImpl( - const FormatCountCapture& v, ConversionSpec conv, FormatSinkImpl* sink) { + const FormatCountCapture& v, FormatConversionSpecImpl conv, + FormatSinkImpl* sink) { return FormatCountCaptureHelper::ConvertHelper(v, conv, sink); } @@ -221,13 +232,13 @@ ArgConvertResult FormatConvertImpl( struct FormatArgImplFriend { template static bool ToInt(Arg arg, int* out) { - // A value initialized ConversionSpec has a `none` conv, which tells the - // dispatcher to run the `int` conversion. + // A value initialized FormatConversionSpecImpl has a `none` conv, which + // tells the dispatcher to run the `int` conversion. return arg.dispatcher_(arg.data_, {}, out); } template - static bool Convert(Arg arg, str_format_internal::ConversionSpec conv, + static bool Convert(Arg arg, FormatConversionSpecImpl conv, FormatSinkImpl* out) { return arg.dispatcher_(arg.data_, conv, out); } @@ -251,7 +262,7 @@ class FormatArgImpl { char buf[kInlinedSpace]; }; - using Dispatcher = bool (*)(Data, ConversionSpec, void* out); + using Dispatcher = bool (*)(Data, FormatConversionSpecImpl, void* out); template struct store_by_value @@ -393,7 +404,7 @@ class FormatArgImpl { } template - static bool Dispatch(Data arg, ConversionSpec spec, void* out) { + static bool Dispatch(Data arg, FormatConversionSpecImpl spec, void* out) { // A `none` conv indicates that we want the `int` conversion. if (ABSL_PREDICT_FALSE(spec.conversion_char() == FormatConversionCharInternal::kNone)) { @@ -410,8 +421,9 @@ class FormatArgImpl { Dispatcher dispatcher_; }; -#define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \ - E template bool FormatArgImpl::Dispatch(Data, ConversionSpec, void*) +#define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \ + E template bool FormatArgImpl::Dispatch(Data, FormatConversionSpecImpl, \ + void*) #define ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(...) \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(str_format_internal::VoidPtr, \ diff --git a/absl/strings/internal/str_format/arg_test.cc b/absl/strings/internal/str_format/arg_test.cc index 8d30d8b8..37e5b754 100644 --- a/absl/strings/internal/str_format/arg_test.cc +++ b/absl/strings/internal/str_format/arg_test.cc @@ -95,8 +95,9 @@ TEST_F(FormatArgImplTest, OtherPtrDecayToVoidPtr) { TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) { std::string s; FormatSinkImpl sink(&s); - ConversionSpec conv; - FormatConversionSpecImplFriend::SetConversionChar(ConversionChar::s, &conv); + FormatConversionSpecImpl conv; + FormatConversionSpecImplFriend::SetConversionChar(FormatConversionChar::s, + &conv); FormatConversionSpecImplFriend::SetFlags(Flags(), &conv); FormatConversionSpecImplFriend::SetWidth(-1, &conv); FormatConversionSpecImplFriend::SetPrecision(-1, &conv); diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index 05105d8d..585246e7 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -19,7 +19,7 @@ class UntypedFormatSpec; namespace str_format_internal { -class BoundConversion : public ConversionSpec { +class BoundConversion : public FormatConversionSpecImpl { public: const FormatArgImpl* arg() const { return arg_; } void set_arg(const FormatArgImpl* a) { arg_ = a; } @@ -119,7 +119,7 @@ class FormatSpecTemplate #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER - template ::type> @@ -190,7 +190,8 @@ class StreamedWrapper { private: template friend ArgConvertResult FormatConvertImpl( - const StreamedWrapper& v, ConversionSpec conv, FormatSinkImpl* out); + const StreamedWrapper& v, FormatConversionSpecImpl conv, + FormatSinkImpl* out); const T& v_; }; diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index 49a24b40..23348174 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -24,7 +24,7 @@ std::string ConvToString(FormatConversionCharSet conv) { } TEST(StrFormatChecker, ArgumentToConv) { - Conv conv = ArgumentToConv(); + FormatConversionCharSet conv = ArgumentToConv(); EXPECT_EQ(ConvToString(conv), "s"); conv = ArgumentToConv(); diff --git a/absl/strings/internal/str_format/convert_test.cc b/absl/strings/internal/str_format/convert_test.cc index cbcd7caf..dd167f76 100644 --- a/absl/strings/internal/str_format/convert_test.cc +++ b/absl/strings/internal/str_format/convert_test.cc @@ -1,14 +1,18 @@ #include #include #include + #include #include +#include #include +#include // NOLINT #include "gmock/gmock.h" #include "gtest/gtest.h" #include "absl/base/internal/raw_logging.h" #include "absl/strings/internal/str_format/bind.h" +#include "absl/types/optional.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -57,7 +61,7 @@ std::string Esc(const T &v) { return oss.str(); } -void StrAppend(std::string *dst, const char *format, va_list ap) { +void StrAppendV(std::string *dst, const char *format, va_list ap) { // First try with a small fixed size buffer static const int kSpaceLength = 1024; char space[kSpaceLength]; @@ -98,11 +102,18 @@ void StrAppend(std::string *dst, const char *format, va_list ap) { delete[] buf; } +void StrAppend(std::string *out, const char *format, ...) { + va_list ap; + va_start(ap, format); + StrAppendV(out, format, ap); + va_end(ap); +} + std::string StrPrint(const char *format, ...) { va_list ap; va_start(ap, format); std::string result; - StrAppend(&result, format, ap); + StrAppendV(&result, format, ap); va_end(ap); return result; } @@ -471,8 +482,8 @@ TEST_F(FormatConvertTest, Float) { #endif // _MSC_VER const char *const kFormats[] = { - "%", "%.3", "%8.5", "%9", "%.60", "%.30", "%03", "%+", - "% ", "%-10", "%#15.3", "%#.0", "%.0", "%1$*2$", "%1$.*2$"}; + "%", "%.3", "%8.5", "%500", "%.5000", "%.60", "%.30", "%03", + "%+", "% ", "%-10", "%#15.3", "%#.0", "%.0", "%1$*2$", "%1$.*2$"}; std::vector doubles = {0.0, -0.0, @@ -489,11 +500,6 @@ TEST_F(FormatConvertTest, Float) { std::numeric_limits::infinity(), -std::numeric_limits::infinity()}; -#ifndef __APPLE__ - // Apple formats NaN differently (+nan) vs. (nan) - doubles.push_back(std::nan("")); -#endif - // Some regression tests. doubles.push_back(0.99999999999999989); @@ -512,43 +518,204 @@ TEST_F(FormatConvertTest, Float) { } } + // Workaround libc bug. + // https://sourceware.org/bugzilla/show_bug.cgi?id=22142 + const bool gcc_bug_22142 = + StrPrint("%f", std::numeric_limits::max()) != + "1797693134862315708145274237317043567980705675258449965989174768031" + "5726078002853876058955863276687817154045895351438246423432132688946" + "4182768467546703537516986049910576551282076245490090389328944075868" + "5084551339423045832369032229481658085593321233482747978262041447231" + "68738177180919299881250404026184124858368.000000"; + + if (!gcc_bug_22142) { + for (int exp = -300; exp <= 300; ++exp) { + const double all_ones_mantissa = 0x1fffffffffffff; + doubles.push_back(std::ldexp(all_ones_mantissa, exp)); + } + } + + if (gcc_bug_22142) { + for (auto &d : doubles) { + using L = std::numeric_limits; + double d2 = std::abs(d); + if (d2 == L::max() || d2 == L::min() || d2 == L::denorm_min()) { + d = 0; + } + } + } + + // Remove duplicates to speed up the logic below. + std::sort(doubles.begin(), doubles.end()); + doubles.erase(std::unique(doubles.begin(), doubles.end()), doubles.end()); + +#ifndef __APPLE__ + // Apple formats NaN differently (+nan) vs. (nan) + doubles.push_back(std::nan("")); +#endif + + // Reserve the space to ensure we don't allocate memory in the output itself. + std::string str_format_result; + str_format_result.reserve(1 << 20); + std::string string_printf_result; + string_printf_result.reserve(1 << 20); + for (const char *fmt : kFormats) { for (char f : {'f', 'F', // 'g', 'G', // 'a', 'A', // 'e', 'E'}) { std::string fmt_str = std::string(fmt) + f; + + if (fmt == absl::string_view("%.5000") && f != 'f' && f != 'F') { + // This particular test takes way too long with snprintf. + // Disable for the case we are not implementing natively. + continue; + } + for (double d : doubles) { int i = -10; FormatArgImpl args[2] = {FormatArgImpl(d), FormatArgImpl(i)}; UntypedFormatSpecImpl format(fmt_str); - // We use ASSERT_EQ here because failures are usually correlated and a - // bug would print way too many failed expectations causing the test to - // time out. - ASSERT_EQ(StrPrint(fmt_str.c_str(), d, i), - FormatPack(format, absl::MakeSpan(args))) - << fmt_str << " " << StrPrint("%.18g", d) << " " - << StrPrint("%.999f", d); + + string_printf_result.clear(); + StrAppend(&string_printf_result, fmt_str.c_str(), d, i); + str_format_result.clear(); + + { + AppendPack(&str_format_result, format, absl::MakeSpan(args)); + } + + if (string_printf_result != str_format_result) { + // We use ASSERT_EQ here because failures are usually correlated and a + // bug would print way too many failed expectations causing the test + // to time out. + ASSERT_EQ(string_printf_result, str_format_result) + << fmt_str << " " << StrPrint("%.18g", d) << " " + << StrPrint("%a", d) << " " << StrPrint("%.1080f", d); + } } } } } +TEST_F(FormatConvertTest, FloatRound) { + std::string s; + const auto format = [&](const char *fmt, double d) -> std::string & { + s.clear(); + FormatArgImpl args[1] = {FormatArgImpl(d)}; + AppendPack(&s, UntypedFormatSpecImpl(fmt), absl::MakeSpan(args)); +#if !defined(_MSC_VER) + // MSVC has a different rounding policy than us so we can't test our + // implementation against the native one there. + EXPECT_EQ(StrPrint(fmt, d), s); +#endif // _MSC_VER + + return s; + }; + // All of these values have to be exactly represented. + // Otherwise we might not be testing what we think we are testing. + + // These values can fit in a 64bit "fast" representation. + const double exact_value = 0.00000000000005684341886080801486968994140625; + assert(exact_value == std::pow(2, -44)); + // Round up at a 5xx. + EXPECT_EQ(format("%.13f", exact_value), "0.0000000000001"); + // Round up at a >5 + EXPECT_EQ(format("%.14f", exact_value), "0.00000000000006"); + // Round down at a <5 + EXPECT_EQ(format("%.16f", exact_value), "0.0000000000000568"); + // Nine handling + EXPECT_EQ(format("%.35f", exact_value), + "0.00000000000005684341886080801486969"); + EXPECT_EQ(format("%.36f", exact_value), + "0.000000000000056843418860808014869690"); + // Round down the last nine. + EXPECT_EQ(format("%.37f", exact_value), + "0.0000000000000568434188608080148696899"); + EXPECT_EQ(format("%.10f", 0.000003814697265625), "0.0000038147"); + // Round up the last nine + EXPECT_EQ(format("%.11f", 0.000003814697265625), "0.00000381470"); + EXPECT_EQ(format("%.12f", 0.000003814697265625), "0.000003814697"); + + // Round to even (down) + EXPECT_EQ(format("%.43f", exact_value), + "0.0000000000000568434188608080148696899414062"); + // Exact + EXPECT_EQ(format("%.44f", exact_value), + "0.00000000000005684341886080801486968994140625"); + // Round to even (up), let make the last digits 75 instead of 25 + EXPECT_EQ(format("%.43f", exact_value + std::pow(2, -43)), + "0.0000000000001705302565824240446090698242188"); + // Exact, just to check. + EXPECT_EQ(format("%.44f", exact_value + std::pow(2, -43)), + "0.00000000000017053025658242404460906982421875"); + + // This value has to be small enough that it won't fit in the uint128 + // representation for printing. + const double small_exact_value = + 0.000000000000000000000000000000000000752316384526264005099991383822237233803945956334136013765601092018187046051025390625; // NOLINT + assert(small_exact_value == std::pow(2, -120)); + // Round up at a 5xx. + EXPECT_EQ(format("%.37f", small_exact_value), + "0.0000000000000000000000000000000000008"); + // Round down at a <5 + EXPECT_EQ(format("%.38f", small_exact_value), + "0.00000000000000000000000000000000000075"); + // Round up at a >5 + EXPECT_EQ(format("%.41f", small_exact_value), + "0.00000000000000000000000000000000000075232"); + // Nine handling + EXPECT_EQ(format("%.55f", small_exact_value), + "0.0000000000000000000000000000000000007523163845262640051"); + EXPECT_EQ(format("%.56f", small_exact_value), + "0.00000000000000000000000000000000000075231638452626400510"); + EXPECT_EQ(format("%.57f", small_exact_value), + "0.000000000000000000000000000000000000752316384526264005100"); + EXPECT_EQ(format("%.58f", small_exact_value), + "0.0000000000000000000000000000000000007523163845262640051000"); + // Round down the last nine + EXPECT_EQ(format("%.59f", small_exact_value), + "0.00000000000000000000000000000000000075231638452626400509999"); + // Round up the last nine + EXPECT_EQ(format("%.79f", small_exact_value), + "0.000000000000000000000000000000000000" + "7523163845262640050999913838222372338039460"); + + // Round to even (down) + EXPECT_EQ(format("%.119f", small_exact_value), + "0.000000000000000000000000000000000000" + "75231638452626400509999138382223723380" + "394595633413601376560109201818704605102539062"); + // Exact + EXPECT_EQ(format("%.120f", small_exact_value), + "0.000000000000000000000000000000000000" + "75231638452626400509999138382223723380" + "3945956334136013765601092018187046051025390625"); + // Round to even (up), let make the last digits 75 instead of 25 + EXPECT_EQ(format("%.119f", small_exact_value + std::pow(2, -119)), + "0.000000000000000000000000000000000002" + "25694915357879201529997415146671170141" + "183786900240804129680327605456113815307617188"); + // Exact, just to check. + EXPECT_EQ(format("%.120f", small_exact_value + std::pow(2, -119)), + "0.000000000000000000000000000000000002" + "25694915357879201529997415146671170141" + "1837869002408041296803276054561138153076171875"); +} + TEST_F(FormatConvertTest, LongDouble) { - const char *const kFormats[] = {"%", "%.3", "%8.5", "%9", +#ifdef _MSC_VER + // MSVC has a different rounding policy than us so we can't test our + // implementation against the native one there. + return; +#endif // _MSC_VER + const char *const kFormats[] = {"%", "%.3", "%8.5", "%9", "%.5000", "%.60", "%+", "% ", "%-10"}; - // This value is not representable in double, but it is in long double that - // uses the extended format. - // This is to verify that we are not truncating the value mistakenly through a - // double. - long double very_precise = 10000000000000000.25L; - std::vector doubles = { 0.0, -0.0, - very_precise, - 1 / very_precise, std::numeric_limits::max(), -std::numeric_limits::max(), std::numeric_limits::min(), @@ -556,22 +723,44 @@ TEST_F(FormatConvertTest, LongDouble) { std::numeric_limits::infinity(), -std::numeric_limits::infinity()}; + for (long double base : {1.L, 12.L, 123.L, 1234.L, 12345.L, 123456.L, + 1234567.L, 12345678.L, 123456789.L, 1234567890.L, + 12345678901.L, 123456789012.L, 1234567890123.L, + // This value is not representable in double, but it + // is in long double that uses the extended format. + // This is to verify that we are not truncating the + // value mistakenly through a double. + 10000000000000000.25L}) { + for (int exp : {-1000, -500, 0, 500, 1000}) { + for (int sign : {1, -1}) { + doubles.push_back(sign * std::ldexp(base, exp)); + doubles.push_back(sign / std::ldexp(base, exp)); + } + } + } + for (const char *fmt : kFormats) { for (char f : {'f', 'F', // 'g', 'G', // 'a', 'A', // 'e', 'E'}) { std::string fmt_str = std::string(fmt) + 'L' + f; + + if (fmt == absl::string_view("%.5000") && f != 'f' && f != 'F') { + // This particular test takes way too long with snprintf. + // Disable for the case we are not implementing natively. + continue; + } + for (auto d : doubles) { FormatArgImpl arg(d); UntypedFormatSpecImpl format(fmt_str); // We use ASSERT_EQ here because failures are usually correlated and a // bug would print way too many failed expectations causing the test to // time out. - ASSERT_EQ(StrPrint(fmt_str.c_str(), d), - FormatPack(format, {&arg, 1})) + ASSERT_EQ(StrPrint(fmt_str.c_str(), d), FormatPack(format, {&arg, 1})) << fmt_str << " " << StrPrint("%.18Lg", d) << " " - << StrPrint("%.999Lf", d); + << StrPrint("%La", d) << " " << StrPrint("%.1080Lf", d); } } } diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index f0cffe1e..33903df0 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -411,11 +411,6 @@ inline size_t Excess(size_t used, size_t capacity) { return used < capacity ? capacity - used : 0; } -// Type alias for use during migration. -using ConversionChar = FormatConversionChar; -using ConversionSpec = FormatConversionSpecImpl; -using Conv = FormatConversionCharSet; - class FormatConversionSpec { public: // Width and precison are not specified, no flags are set. diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index d6858cff..cdccc86f 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -1,12 +1,22 @@ #include "absl/strings/internal/str_format/float_conversion.h" #include + #include #include #include +#include #include +#include "absl/base/attributes.h" #include "absl/base/config.h" +#include "absl/base/internal/bits.h" +#include "absl/base/optimization.h" +#include "absl/functional/function_ref.h" +#include "absl/meta/type_traits.h" +#include "absl/numeric/int128.h" +#include "absl/types/optional.h" +#include "absl/types/span.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -14,13 +24,640 @@ namespace str_format_internal { namespace { -char *CopyStringTo(string_view v, char *out) { +// The code below wants to avoid heap allocations. +// To do so it needs to allocate memory on the stack. +// `StackArray` will allocate memory on the stack in the form of a uint32_t +// array and call the provided callback with said memory. +// It will allocate memory in increments of 512 bytes. We could allocate the +// largest needed unconditionally, but that is more than we need in most of +// cases. This way we use less stack in the common cases. +class StackArray { + using Func = absl::FunctionRef)>; + static constexpr size_t kStep = 512 / sizeof(uint32_t); + // 5 steps is 2560 bytes, which is enough to hold a long double with the + // largest/smallest exponents. + // The operations below will static_assert their particular maximum. + static constexpr size_t kNumSteps = 5; + + // We do not want this function to be inlined. + // Otherwise the caller will allocate the stack space unnecessarily for all + // the variants even though it only calls one. + template + ABSL_ATTRIBUTE_NOINLINE static void RunWithCapacityImpl(Func f) { + uint32_t values[steps * kStep]{}; + f(absl::MakeSpan(values)); + } + + public: + static constexpr size_t kMaxCapacity = kStep * kNumSteps; + + static void RunWithCapacity(size_t capacity, Func f) { + assert(capacity <= kMaxCapacity); + const size_t step = (capacity + kStep - 1) / kStep; + assert(step <= kNumSteps); + switch (step) { + case 1: + return RunWithCapacityImpl<1>(f); + case 2: + return RunWithCapacityImpl<2>(f); + case 3: + return RunWithCapacityImpl<3>(f); + case 4: + return RunWithCapacityImpl<4>(f); + case 5: + return RunWithCapacityImpl<5>(f); + } + + assert(false && "Invalid capacity"); + } +}; + +// Calculates `10 * (*v) + carry` and stores the result in `*v` and returns +// the carry. +template +inline Int MultiplyBy10WithCarry(Int *v, Int carry) { + using BiggerInt = absl::conditional_t; + BiggerInt tmp = 10 * static_cast(*v) + carry; + *v = static_cast(tmp); + return static_cast(tmp >> (sizeof(Int) * 8)); +} + +// Calculates `(2^64 * carry + *v) / 10`. +// Stores the quotient in `*v` and returns the remainder. +// Requires: `0 <= carry <= 9` +inline uint64_t DivideBy10WithCarry(uint64_t *v, uint64_t carry) { + constexpr uint64_t divisor = 10; + // 2^64 / divisor = chunk_quotient + chunk_remainder / divisor + constexpr uint64_t chunk_quotient = (uint64_t{1} << 63) / (divisor / 2); + constexpr uint64_t chunk_remainder = uint64_t{} - chunk_quotient * divisor; + + const uint64_t mod = *v % divisor; + const uint64_t next_carry = chunk_remainder * carry + mod; + *v = *v / divisor + carry * chunk_quotient + next_carry / divisor; + return next_carry % divisor; +} + +// Generates the decimal representation for an integer of the form `v * 2^exp`, +// where `v` and `exp` are both positive integers. +// It generates the digits from the left (ie the most significant digit first) +// to allow for direct printing into the sink. +// +// Requires `0 <= exp` and `exp <= numeric_limits::max_exponent`. +class BinaryToDecimal { + static constexpr int ChunksNeeded(int exp) { + // We will left shift a uint128 by `exp` bits, so we need `128+exp` total + // bits. Round up to 32. + // See constructor for details about adding `10%` to the value. + return (128 + exp + 31) / 32 * 11 / 10; + } + + public: + // Run the conversion for `v * 2^exp` and call `f(binary_to_decimal)`. + // This function will allocate enough stack space to perform the conversion. + static void RunConversion(uint128 v, int exp, + absl::FunctionRef f) { + assert(exp > 0); + assert(exp <= std::numeric_limits::max_exponent); + static_assert( + StackArray::kMaxCapacity >= + ChunksNeeded(std::numeric_limits::max_exponent), + ""); + + StackArray::RunWithCapacity( + ChunksNeeded(exp), + [=](absl::Span input) { f(BinaryToDecimal(input, v, exp)); }); + } + + int TotalDigits() const { + return static_cast((decimal_end_ - decimal_start_) * kDigitsPerChunk + + CurrentDigits().size()); + } + + // See the current block of digits. + absl::string_view CurrentDigits() const { + return absl::string_view(digits_ + kDigitsPerChunk - size_, size_); + } + + // Advance the current view of digits. + // Returns `false` when no more digits are available. + bool AdvanceDigits() { + if (decimal_start_ >= decimal_end_) return false; + + uint32_t w = data_[decimal_start_++]; + for (size_ = 0; size_ < kDigitsPerChunk; w /= 10) { + digits_[kDigitsPerChunk - ++size_] = w % 10 + '0'; + } + return true; + } + + private: + BinaryToDecimal(absl::Span data, uint128 v, int exp) : data_(data) { + // We need to print the digits directly into the sink object without + // buffering them all first. To do this we need two things: + // - to know the total number of digits to do padding when necessary + // - to generate the decimal digits from the left. + // + // In order to do this, we do a two pass conversion. + // On the first pass we convert the binary representation of the value into + // a decimal representation in which each uint32_t chunk holds up to 9 + // decimal digits. In the second pass we take each decimal-holding-uint32_t + // value and generate the ascii decimal digits into `digits_`. + // + // The binary and decimal representations actually share the same memory + // region. As we go converting the chunks from binary to decimal we free + // them up and reuse them for the decimal representation. One caveat is that + // the decimal representation is around 7% less efficient in space than the + // binary one. We allocate an extra 10% memory to account for this. See + // ChunksNeeded for this calculation. + int chunk_index = exp / 32; + decimal_start_ = decimal_end_ = ChunksNeeded(exp); + const int offset = exp % 32; + // Left shift v by exp bits. + data_[chunk_index] = static_cast(v << offset); + for (v >>= (32 - offset); v; v >>= 32) + data_[++chunk_index] = static_cast(v); + + while (chunk_index >= 0) { + // While we have more than one chunk available, go in steps of 1e9. + // `data_[chunk_index]` holds the highest non-zero binary chunk, so keep + // the variable updated. + uint32_t carry = 0; + for (int i = chunk_index; i >= 0; --i) { + uint64_t tmp = uint64_t{data_[i]} + (uint64_t{carry} << 32); + data_[i] = static_cast(tmp / uint64_t{1000000000}); + carry = static_cast(tmp % uint64_t{1000000000}); + } + + // If the highest chunk is now empty, remove it from view. + if (data_[chunk_index] == 0) --chunk_index; + + --decimal_start_; + assert(decimal_start_ != chunk_index); + data_[decimal_start_] = carry; + } + + // Fill the first set of digits. The first chunk might not be complete, so + // handle differently. + for (uint32_t first = data_[decimal_start_++]; first != 0; first /= 10) { + digits_[kDigitsPerChunk - ++size_] = first % 10 + '0'; + } + } + + private: + static constexpr size_t kDigitsPerChunk = 9; + + int decimal_start_; + int decimal_end_; + + char digits_[kDigitsPerChunk]; + int size_ = 0; + + absl::Span data_; +}; + +// Converts a value of the form `x * 2^-exp` into a sequence of decimal digits. +// Requires `-exp < 0` and +// `-exp >= limits::min_exponent - limits::digits`. +class FractionalDigitGenerator { + public: + // Run the conversion for `v * 2^exp` and call `f(generator)`. + // This function will allocate enough stack space to perform the conversion. + static void RunConversion( + uint128 v, int exp, absl::FunctionRef f) { + assert(-exp < 0); + assert(-exp >= std::numeric_limits::min_exponent - 128); + static_assert( + StackArray::kMaxCapacity >= + (128 - std::numeric_limits::min_exponent + 31) / 32, + ""); + StackArray::RunWithCapacity((exp + 31) / 32, + [=](absl::Span input) { + f(FractionalDigitGenerator(input, v, exp)); + }); + } + + // Returns true if there are any more non-zero digits left. + bool HasMoreDigits() const { return next_digit_ != 0 || chunk_index_ >= 0; } + + // Returns true if the remainder digits are greater than 5000... + bool IsGreaterThanHalf() const { + return next_digit_ > 5 || (next_digit_ == 5 && chunk_index_ >= 0); + } + // Returns true if the remainder digits are exactly 5000... + bool IsExactlyHalf() const { return next_digit_ == 5 && chunk_index_ < 0; } + + struct Digits { + int digit_before_nine; + int num_nines; + }; + + // Get the next set of digits. + // They are composed by a non-9 digit followed by a runs of zero or more 9s. + Digits GetDigits() { + Digits digits{next_digit_, 0}; + + next_digit_ = GetOneDigit(); + while (next_digit_ == 9) { + ++digits.num_nines; + next_digit_ = GetOneDigit(); + } + + return digits; + } + + private: + // Return the next digit. + int GetOneDigit() { + if (chunk_index_ < 0) return 0; + + uint32_t carry = 0; + for (int i = chunk_index_; i >= 0; --i) { + carry = MultiplyBy10WithCarry(&data_[i], carry); + } + // If the lowest chunk is now empty, remove it from view. + if (data_[chunk_index_] == 0) --chunk_index_; + return carry; + } + + FractionalDigitGenerator(absl::Span data, uint128 v, int exp) + : chunk_index_(exp / 32), data_(data) { + const int offset = exp % 32; + // Right shift `v` by `exp` bits. + data_[chunk_index_] = static_cast(v << (32 - offset)); + v >>= offset; + // Make sure we don't overflow the data. We already calculated that + // non-zero bits fit, so we might not have space for leading zero bits. + for (int pos = chunk_index_; v; v >>= 32) + data_[--pos] = static_cast(v); + + // Fill next_digit_, as GetDigits expects it to be populated always. + next_digit_ = GetOneDigit(); + } + + int next_digit_; + int chunk_index_; + absl::Span data_; +}; + +// Count the number of leading zero bits. +int LeadingZeros(uint64_t v) { return base_internal::CountLeadingZeros64(v); } +int LeadingZeros(uint128 v) { + auto high = static_cast(v >> 64); + auto low = static_cast(v); + return high != 0 ? base_internal::CountLeadingZeros64(high) + : 64 + base_internal::CountLeadingZeros64(low); +} + +// Round up the text digits starting at `p`. +// The buffer must have an extra digit that is known to not need rounding. +// This is done below by having an extra '0' digit on the left. +void RoundUp(char *p) { + while (*p == '9' || *p == '.') { + if (*p == '9') *p = '0'; + --p; + } + ++*p; +} + +// Check the previous digit and round up or down to follow the round-to-even +// policy. +void RoundToEven(char *p) { + if (*p == '.') --p; + if (*p % 2 == 1) RoundUp(p); +} + +// Simple integral decimal digit printing for values that fit in 64-bits. +// Returns the pointer to the last written digit. +char *PrintIntegralDigitsFromRightFast(uint64_t v, char *p) { + do { + *--p = DivideBy10WithCarry(&v, 0) + '0'; + } while (v != 0); + return p; +} + +// Simple integral decimal digit printing for values that fit in 128-bits. +// Returns the pointer to the last written digit. +char *PrintIntegralDigitsFromRightFast(uint128 v, char *p) { + auto high = static_cast(v >> 64); + auto low = static_cast(v); + + while (high != 0) { + uint64_t carry = DivideBy10WithCarry(&high, 0); + carry = DivideBy10WithCarry(&low, carry); + *--p = carry + '0'; + } + return PrintIntegralDigitsFromRightFast(low, p); +} + +// Simple fractional decimal digit printing for values that fir in 64-bits after +// shifting. +// Performs rounding if necessary to fit within `precision`. +// Returns the pointer to one after the last character written. +char *PrintFractionalDigitsFast(uint64_t v, char *start, int exp, + int precision) { + char *p = start; + v <<= (64 - exp); + while (precision > 0) { + if (!v) return p; + *p++ = MultiplyBy10WithCarry(&v, uint64_t{0}) + '0'; + --precision; + } + + // We need to round. + if (v < 0x8000000000000000) { + // We round down, so nothing to do. + } else if (v > 0x8000000000000000) { + // We round up. + RoundUp(p - 1); + } else { + RoundToEven(p - 1); + } + + assert(precision == 0); + // Precision can only be zero here. + return p; +} + +// Simple fractional decimal digit printing for values that fir in 128-bits +// after shifting. +// Performs rounding if necessary to fit within `precision`. +// Returns the pointer to one after the last character written. +char *PrintFractionalDigitsFast(uint128 v, char *start, int exp, + int precision) { + char *p = start; + v <<= (128 - exp); + auto high = static_cast(v >> 64); + auto low = static_cast(v); + + // While we have digits to print and `low` is not empty, do the long + // multiplication. + while (precision > 0 && low != 0) { + uint64_t carry = MultiplyBy10WithCarry(&low, uint64_t{0}); + carry = MultiplyBy10WithCarry(&high, carry); + + *p++ = carry + '0'; + --precision; + } + + // Now `low` is empty, so use a faster approach for the rest of the digits. + // This block is pretty much the same as the main loop for the 64-bit case + // above. + while (precision > 0) { + if (!high) return p; + *p++ = MultiplyBy10WithCarry(&high, uint64_t{0}) + '0'; + --precision; + } + + // We need to round. + if (high < 0x8000000000000000) { + // We round down, so nothing to do. + } else if (high > 0x8000000000000000 || low != 0) { + // We round up. + RoundUp(p - 1); + } else { + RoundToEven(p - 1); + } + + assert(precision == 0); + // Precision can only be zero here. + return p; +} + +struct FormatState { + char sign_char; + int precision; + const FormatConversionSpecImpl &conv; + FormatSinkImpl *sink; + + // In `alt` mode (flag #) we keep the `.` even if there are no fractional + // digits. In non-alt mode, we strip it. + bool ShouldPrintDot() const { return precision != 0 || conv.has_alt_flag(); } +}; + +struct Padding { + int left_spaces; + int zeros; + int right_spaces; +}; + +Padding ExtraWidthToPadding(int total_size, const FormatState &state) { + int missing_chars = std::max(state.conv.width() - total_size, 0); + if (state.conv.has_left_flag()) { + return {0, 0, missing_chars}; + } else if (state.conv.has_zero_flag()) { + return {0, missing_chars, 0}; + } else { + return {missing_chars, 0, 0}; + } +} + +void FinalPrint(absl::string_view data, int trailing_zeros, + const FormatState &state) { + if (state.conv.width() < 0) { + // No width specified. Fast-path. + if (state.sign_char != '\0') state.sink->Append(1, state.sign_char); + state.sink->Append(data); + state.sink->Append(trailing_zeros, '0'); + return; + } + + auto padding = + ExtraWidthToPadding((state.sign_char != '\0' ? 1 : 0) + + static_cast(data.size()) + trailing_zeros, + state); + + state.sink->Append(padding.left_spaces, ' '); + if (state.sign_char != '\0') state.sink->Append(1, state.sign_char); + state.sink->Append(padding.zeros, '0'); + state.sink->Append(data); + state.sink->Append(trailing_zeros, '0'); + state.sink->Append(padding.right_spaces, ' '); +} + +// Fastpath %f formatter for when the shifted value fits in a simple integral +// type. +// Prints `v*2^exp` with the options from `state`. +template +void FormatFFast(Int v, int exp, const FormatState &state) { + constexpr int input_bits = sizeof(Int) * 8; + + static constexpr size_t integral_size = + /* in case we need to round up an extra digit */ 1 + + /* decimal digits for uint128 */ 40 + 1; + char buffer[integral_size + /* . */ 1 + /* max digits uint128 */ 128]; + buffer[integral_size] = '.'; + char *const integral_digits_end = buffer + integral_size; + char *integral_digits_start; + char *const fractional_digits_start = buffer + integral_size + 1; + char *fractional_digits_end = fractional_digits_start; + + if (exp >= 0) { + const int total_bits = input_bits - LeadingZeros(v) + exp; + integral_digits_start = + total_bits <= 64 + ? PrintIntegralDigitsFromRightFast(static_cast(v) << exp, + integral_digits_end) + : PrintIntegralDigitsFromRightFast(static_cast(v) << exp, + integral_digits_end); + } else { + exp = -exp; + + integral_digits_start = PrintIntegralDigitsFromRightFast( + exp < input_bits ? v >> exp : 0, integral_digits_end); + // PrintFractionalDigits may pull a carried 1 all the way up through the + // integral portion. + integral_digits_start[-1] = '0'; + + fractional_digits_end = + exp <= 64 ? PrintFractionalDigitsFast(v, fractional_digits_start, exp, + state.precision) + : PrintFractionalDigitsFast(static_cast(v), + fractional_digits_start, exp, + state.precision); + // There was a carry, so include the first digit too. + if (integral_digits_start[-1] != '0') --integral_digits_start; + } + + size_t size = fractional_digits_end - integral_digits_start; + + // In `alt` mode (flag #) we keep the `.` even if there are no fractional + // digits. In non-alt mode, we strip it. + if (!state.ShouldPrintDot()) --size; + FinalPrint(absl::string_view(integral_digits_start, size), + static_cast(state.precision - (fractional_digits_end - + fractional_digits_start)), + state); +} + +// Slow %f formatter for when the shifted value does not fit in a uint128, and +// `exp > 0`. +// Prints `v*2^exp` with the options from `state`. +// This one is guaranteed to not have fractional digits, so we don't have to +// worry about anything after the `.`. +void FormatFPositiveExpSlow(uint128 v, int exp, const FormatState &state) { + BinaryToDecimal::RunConversion(v, exp, [&](BinaryToDecimal btd) { + const int total_digits = + btd.TotalDigits() + (state.ShouldPrintDot() ? state.precision + 1 : 0); + + const auto padding = ExtraWidthToPadding( + total_digits + (state.sign_char != '\0' ? 1 : 0), state); + + state.sink->Append(padding.left_spaces, ' '); + if (state.sign_char != '\0') state.sink->Append(1, state.sign_char); + state.sink->Append(padding.zeros, '0'); + + do { + state.sink->Append(btd.CurrentDigits()); + } while (btd.AdvanceDigits()); + + if (state.ShouldPrintDot()) state.sink->Append(1, '.'); + state.sink->Append(state.precision, '0'); + state.sink->Append(padding.right_spaces, ' '); + }); +} + +// Slow %f formatter for when the shifted value does not fit in a uint128, and +// `exp < 0`. +// Prints `v*2^exp` with the options from `state`. +// This one is guaranteed to be < 1.0, so we don't have to worry about integral +// digits. +void FormatFNegativeExpSlow(uint128 v, int exp, const FormatState &state) { + const int total_digits = + /* 0 */ 1 + (state.ShouldPrintDot() ? state.precision + 1 : 0); + auto padding = + ExtraWidthToPadding(total_digits + (state.sign_char ? 1 : 0), state); + padding.zeros += 1; + state.sink->Append(padding.left_spaces, ' '); + if (state.sign_char != '\0') state.sink->Append(1, state.sign_char); + state.sink->Append(padding.zeros, '0'); + + if (state.ShouldPrintDot()) state.sink->Append(1, '.'); + + // Print digits + int digits_to_go = state.precision; + + FractionalDigitGenerator::RunConversion( + v, exp, [&](FractionalDigitGenerator digit_gen) { + // There are no digits to print here. + if (state.precision == 0) return; + + // We go one digit at a time, while keeping track of runs of nines. + // The runs of nines are used to perform rounding when necessary. + + while (digits_to_go > 0 && digit_gen.HasMoreDigits()) { + auto digits = digit_gen.GetDigits(); + + // Now we have a digit and a run of nines. + // See if we can print them all. + if (digits.num_nines + 1 < digits_to_go) { + // We don't have to round yet, so print them. + state.sink->Append(1, digits.digit_before_nine + '0'); + state.sink->Append(digits.num_nines, '9'); + digits_to_go -= digits.num_nines + 1; + + } else { + // We can't print all the nines, see where we have to truncate. + + bool round_up = false; + if (digits.num_nines + 1 > digits_to_go) { + // We round up at a nine. No need to print them. + round_up = true; + } else { + // We can fit all the nines, but truncate just after it. + if (digit_gen.IsGreaterThanHalf()) { + round_up = true; + } else if (digit_gen.IsExactlyHalf()) { + // Round to even + round_up = + digits.num_nines != 0 || digits.digit_before_nine % 2 == 1; + } + } + + if (round_up) { + state.sink->Append(1, digits.digit_before_nine + '1'); + --digits_to_go; + // The rest will be zeros. + } else { + state.sink->Append(1, digits.digit_before_nine + '0'); + state.sink->Append(digits_to_go - 1, '9'); + digits_to_go = 0; + } + return; + } + } + }); + + state.sink->Append(digits_to_go, '0'); + state.sink->Append(padding.right_spaces, ' '); +} + +template +void FormatF(Int mantissa, int exp, const FormatState &state) { + if (exp >= 0) { + const int total_bits = sizeof(Int) * 8 - LeadingZeros(mantissa) + exp; + + // Fallback to the slow stack-based approach if we can't do it in a 64 or + // 128 bit state. + if (ABSL_PREDICT_FALSE(total_bits > 128)) { + return FormatFPositiveExpSlow(mantissa, exp, state); + } + } else { + // Fallback to the slow stack-based approach if we can't do it in a 64 or + // 128 bit state. + if (ABSL_PREDICT_FALSE(exp < -128)) { + return FormatFNegativeExpSlow(mantissa, -exp, state); + } + } + return FormatFFast(mantissa, exp, state); +} + +char *CopyStringTo(absl::string_view v, char *out) { std::memcpy(out, v.data(), v.size()); return out + v.size(); } template -bool FallbackToSnprintf(const Float v, const ConversionSpec &conv, +bool FallbackToSnprintf(const Float v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { int w = conv.width() >= 0 ? conv.width() : 0; int p = conv.precision() >= 0 ? conv.precision() : -1; @@ -38,12 +675,12 @@ bool FallbackToSnprintf(const Float v, const ConversionSpec &conv, assert(fp < fmt + sizeof(fmt)); } std::string space(512, '\0'); - string_view result; + absl::string_view result; while (true) { int n = snprintf(&space[0], space.size(), fmt, w, p, v); if (n < 0) return false; if (static_cast(n) < space.size()) { - result = string_view(space.data(), n); + result = absl::string_view(space.data(), n); break; } space.resize(n + 1); @@ -96,9 +733,10 @@ enum class FormatStyle { Fixed, Precision }; // Otherwise, return false. template bool ConvertNonNumericFloats(char sign_char, Float v, - const ConversionSpec &conv, FormatSinkImpl *sink) { + const FormatConversionSpecImpl &conv, + FormatSinkImpl *sink) { char text[4], *ptr = text; - if (sign_char) *ptr++ = sign_char; + if (sign_char != '\0') *ptr++ = sign_char; if (std::isnan(v)) { ptr = std::copy_n( FormatConversionCharIsUpper(conv.conversion_char()) ? "NAN" : "nan", 3, @@ -172,7 +810,12 @@ constexpr bool CanFitMantissa() { template struct Decomposed { - Float mantissa; + using MantissaType = + absl::conditional_t::value, uint128, + uint64_t>; + static_assert(std::numeric_limits::digits <= sizeof(MantissaType) * 8, + ""); + MantissaType mantissa; int exponent; }; @@ -183,7 +826,8 @@ Decomposed Decompose(Float v) { Float m = std::frexp(v, &exp); m = std::ldexp(m, std::numeric_limits::digits); exp -= std::numeric_limits::digits; - return {m, exp}; + + return {static_cast::MantissaType>(m), exp}; } // Print 'digits' as decimal. @@ -352,8 +996,9 @@ bool FloatToBuffer(Decomposed decomposed, int precision, Buffer *out, return false; } -void WriteBufferToSink(char sign_char, string_view str, - const ConversionSpec &conv, FormatSinkImpl *sink) { +void WriteBufferToSink(char sign_char, absl::string_view str, + const FormatConversionSpecImpl &conv, + FormatSinkImpl *sink) { int left_spaces = 0, zeros = 0, right_spaces = 0; int missing_chars = conv.width() >= 0 ? std::max(conv.width() - static_cast(str.size()) - @@ -369,14 +1014,14 @@ void WriteBufferToSink(char sign_char, string_view str, } sink->Append(left_spaces, ' '); - if (sign_char) sink->Append(1, sign_char); + if (sign_char != '\0') sink->Append(1, sign_char); sink->Append(zeros, '0'); sink->Append(str); sink->Append(right_spaces, ' '); } template -bool FloatToSink(const Float v, const ConversionSpec &conv, +bool FloatToSink(const Float v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { // Print the sign or the sign column. Float abs_v = v; @@ -407,11 +1052,9 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, if (c == FormatConversionCharInternal::f || c == FormatConversionCharInternal::F) { - if (!FloatToBuffer(decomposed, precision, &buffer, - nullptr)) { - return FallbackToSnprintf(v, conv, sink); - } - if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back(); + FormatF(decomposed.mantissa, decomposed.exponent, + {sign_char, precision, conv, sink}); + return true; } else if (c == FormatConversionCharInternal::e || c == FormatConversionCharInternal::E) { if (!FloatToBuffer(decomposed, precision, &buffer, @@ -462,25 +1105,32 @@ bool FloatToSink(const Float v, const ConversionSpec &conv, } WriteBufferToSink(sign_char, - string_view(buffer.begin, buffer.end - buffer.begin), conv, - sink); + absl::string_view(buffer.begin, buffer.end - buffer.begin), + conv, sink); return true; } } // namespace -bool ConvertFloatImpl(long double v, const ConversionSpec &conv, +bool ConvertFloatImpl(long double v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { + if (std::numeric_limits::digits == + 2 * std::numeric_limits::digits) { + // This is the `double-double` representation of `long double`. + // We do not handle it natively. Fallback to snprintf. + return FallbackToSnprintf(v, conv, sink); + } + return FloatToSink(v, conv, sink); } -bool ConvertFloatImpl(float v, const ConversionSpec &conv, +bool ConvertFloatImpl(float v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { return FloatToSink(v, conv, sink); } -bool ConvertFloatImpl(double v, const ConversionSpec &conv, +bool ConvertFloatImpl(double v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { return FloatToSink(v, conv, sink); } diff --git a/absl/strings/internal/str_format/float_conversion.h b/absl/strings/internal/str_format/float_conversion.h index 49a6a636..e78bc191 100644 --- a/absl/strings/internal/str_format/float_conversion.h +++ b/absl/strings/internal/str_format/float_conversion.h @@ -7,13 +7,13 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { -bool ConvertFloatImpl(float v, const ConversionSpec &conv, +bool ConvertFloatImpl(float v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink); -bool ConvertFloatImpl(double v, const ConversionSpec &conv, +bool ConvertFloatImpl(double v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink); -bool ConvertFloatImpl(long double v, const ConversionSpec &conv, +bool ConvertFloatImpl(long double v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink); } // namespace str_format_internal diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index fd2dc970..fffed04f 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -83,7 +83,7 @@ const char* ConsumeUnboundConversion(const char* p, const char* end, // conversions. class ConvTag { public: - constexpr ConvTag(ConversionChar conversion_char) // NOLINT + constexpr ConvTag(FormatConversionChar conversion_char) // NOLINT : tag_(static_cast(conversion_char)) {} // We invert the length modifiers to make them negative so that we can easily // test for them. @@ -94,9 +94,9 @@ class ConvTag { bool is_conv() const { return tag_ >= 0; } bool is_length() const { return tag_ < 0 && tag_ != -128; } - ConversionChar as_conv() const { + FormatConversionChar as_conv() const { assert(is_conv()); - return static_cast(tag_); + return static_cast(tag_); } LengthMod as_length() const { assert(is_length()); @@ -282,7 +282,7 @@ class ParsedFormatBase { // This is the only API that allows the user to pass a runtime specified format // string. These factory functions will return NULL if the format does not match // the conversions requested by the user. -template +template class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase { public: explicit ExtendedParsedFormat(string_view format) diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 26f5bec6..dae2d20f 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -41,7 +41,7 @@ TEST(LengthModTest, Names) { TEST(ConversionCharTest, Names) { struct Expectation { - ConversionChar id; + FormatConversionChar id; char name; }; // clang-format off @@ -57,7 +57,7 @@ TEST(ConversionCharTest, Names) { // clang-format on for (auto e : kExpect) { SCOPED_TRACE(e.name); - ConversionChar v = e.id; + FormatConversionChar v = e.id; EXPECT_EQ(e.name, FormatConversionCharToChar(v)); } } @@ -368,7 +368,7 @@ TEST_F(ParsedFormatTest, ValueSemantics) { struct ExpectParse { const char* in; - std::initializer_list conv_set; + std::initializer_list conv_set; const char* out; }; diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 160f4c61..3f14dba3 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -532,76 +532,103 @@ TEST_F(ParsedFormatTest, SimpleUncheckedIncorrect) { EXPECT_FALSE((ParsedFormat<'s', 'd', 'g'>::New(format))); } -using str_format_internal::Conv; +using absl::str_format_internal::FormatConversionCharSet; TEST_F(ParsedFormatTest, UncheckedCorrect) { - auto f = ExtendedParsedFormat::New("ABC%dDEF"); + auto f = ExtendedParsedFormat::New("ABC%dDEF"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); std::string format = "%sFFF%dZZZ%f"; - auto f2 = ExtendedParsedFormat::New( - format); + auto f2 = + ExtendedParsedFormat::New(format); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); - f2 = ExtendedParsedFormat::New( - "%s %d %f"); + f2 = + ExtendedParsedFormat::New("%s %d %f"); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); - auto star = ExtendedParsedFormat::New("%*d"); + auto star = ExtendedParsedFormat::New("%*d"); ASSERT_TRUE(star); EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); - auto dollar = ExtendedParsedFormat::New("%2$s %1$d"); + auto dollar = + ExtendedParsedFormat::New("%2$s %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); // with reuse - dollar = ExtendedParsedFormat::New("%2$s %1$d %1$d"); + dollar = + ExtendedParsedFormat::New("%2$s %1$d %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); } TEST_F(ParsedFormatTest, UncheckedIgnoredArgs) { - EXPECT_FALSE((ExtendedParsedFormat::New("ABC"))); - EXPECT_FALSE((ExtendedParsedFormat::New("%dABC"))); - EXPECT_FALSE((ExtendedParsedFormat::New("ABC%2$s"))); - auto f = ExtendedParsedFormat::NewAllowIgnored("ABC"); + EXPECT_FALSE((ExtendedParsedFormat::New("ABC"))); + EXPECT_FALSE( + (ExtendedParsedFormat::New("%dABC"))); + EXPECT_FALSE( + (ExtendedParsedFormat::New("ABC%2$s"))); + auto f = + ExtendedParsedFormat::NewAllowIgnored("ABC"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f)); - f = ExtendedParsedFormat::NewAllowIgnored("%dABC"); + f = ExtendedParsedFormat< + FormatConversionCharSet::d, + FormatConversionCharSet::s>::NewAllowIgnored("%dABC"); ASSERT_TRUE(f); EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f)); - f = ExtendedParsedFormat::NewAllowIgnored("ABC%2$s"); + f = ExtendedParsedFormat< + FormatConversionCharSet::d, + FormatConversionCharSet::s>::NewAllowIgnored("ABC%2$s"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f)); } TEST_F(ParsedFormatTest, UncheckedMultipleTypes) { - auto dx = ExtendedParsedFormat::New("%1$d %1$x"); + auto dx = ExtendedParsedFormat::New("%1$d %1$x"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}[ ]{1$x:1$x}", SummarizeParsedFormat(*dx)); - dx = ExtendedParsedFormat::New("%1$d"); + dx = ExtendedParsedFormat::New("%1$d"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}", SummarizeParsedFormat(*dx)); } TEST_F(ParsedFormatTest, UncheckedIncorrect) { - EXPECT_FALSE(ExtendedParsedFormat::New("")); + EXPECT_FALSE(ExtendedParsedFormat::New("")); - EXPECT_FALSE(ExtendedParsedFormat::New("ABC%dDEF%d")); + EXPECT_FALSE( + ExtendedParsedFormat::New("ABC%dDEF%d")); std::string format = "%sFFF%dZZZ%f"; - EXPECT_FALSE((ExtendedParsedFormat::New(format))); + EXPECT_FALSE((ExtendedParsedFormat::New(format))); } TEST_F(ParsedFormatTest, RegressionMixPositional) { - EXPECT_FALSE((ExtendedParsedFormat::New("%1$d %o"))); + EXPECT_FALSE( + (ExtendedParsedFormat::New("%1$d %o"))); } using FormatWrapperTest = ::testing::Test; diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h index e7b4c1e6..92c47236 100644 --- a/absl/strings/substitute.h +++ b/absl/strings/substitute.h @@ -50,7 +50,7 @@ // // Supported types: // * absl::string_view, std::string, const char* (null is equivalent to "") -// * int32_t, int64_t, uint32_t, uint64 +// * int32_t, int64_t, uint32_t, uint64_t // * float, double // * bool (Printed as "true" or "false") // * pointer types other than char* (Printed as "0x", -- cgit v1.2.3 From 768eb2ca2857342673fcd462792ce04b8bac3fa3 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 18 May 2020 17:53:14 -0700 Subject: Export of internal Abseil changes -- f012012ef78234a6a4585321b67d7b7c92ebc266 by Laramie Leavitt : Slight restructuring of absl/random/internal randen implementation. Convert round-keys.inc into randen_round_keys.cc file. Consistently use a 128-bit pointer type for internal method parameters. This allows simpler pointer arithmetic in C++ & permits removal of some constants and casts. Remove some redundancy in comments & constexpr variables. Specifically, all references to Randen algorithm parameters use RandenTraits; duplication in RandenSlow removed. PiperOrigin-RevId: 312190313 -- dc8b42e054046741e9ed65335bfdface997c6063 by Abseil Team : Internal change. PiperOrigin-RevId: 312167304 -- f13d248fafaf206492c1362c3574031aea3abaf7 by Matthew Brown : Cleanup StrFormat extensions a little. PiperOrigin-RevId: 312166336 -- 9d9117589667afe2332bb7ad42bc967ca7c54502 by Derek Mauro : Internal change PiperOrigin-RevId: 312105213 -- 9a12b9b3aa0e59b8ee6cf9408ed0029045543a9b by Abseil Team : Complete IGNORE_TYPE macro renaming. PiperOrigin-RevId: 311999699 -- 64756f20d61021d999bd0d4c15e9ad3857382f57 by Gennadiy Rozental : Switch to fixed bytes specific default value. This fixes the Abseil Flags for big endian platforms. PiperOrigin-RevId: 311844448 -- bdbe6b5b29791dbc3816ada1828458b3010ff1e9 by Laramie Leavitt : Change many distribution tests to use pcg_engine as a deterministic source of entropy. It's reasonable to test that the BitGen itself has good entropy, however when testing the cross product of all random distributions x all the architecture variations x all submitted changes results in a large number of tests. In order to account for these failures while still using good entropy requires that our allowed sigma need to account for all of these independent tests. Our current sigma values are too restrictive, and we see a lot of failures, so we have to either relax the sigma values or convert some of the statistical tests to use deterministic values. This changelist does the latter. PiperOrigin-RevId: 311840096 GitOrigin-RevId: f012012ef78234a6a4585321b67d7b7c92ebc266 Change-Id: Ic84886f38ff30d7d72c126e9b63c9a61eb729a1a --- CMake/AbseilDll.cmake | 1 + absl/flags/config.h | 20 + absl/flags/flag_test.cc | 4 +- absl/flags/internal/commandlineflag.h | 17 - absl/flags/internal/flag.cc | 36 +- absl/flags/internal/flag.h | 37 +- absl/flags/parse.cc | 6 +- absl/random/BUILD.bazel | 10 +- absl/random/CMakeLists.txt | 13 +- absl/random/bernoulli_distribution_test.cc | 6 +- absl/random/beta_distribution_test.cc | 17 +- absl/random/discrete_distribution_test.cc | 6 +- absl/random/exponential_distribution_test.cc | 6 +- absl/random/gaussian_distribution_test.cc | 5 +- absl/random/internal/BUILD.bazel | 5 +- absl/random/internal/randen-keys.inc | 207 ---------- absl/random/internal/randen_hwaes.cc | 398 ++++++++----------- absl/random/internal/randen_hwaes_test.cc | 12 +- absl/random/internal/randen_round_keys.cc | 462 +++++++++++++++++++++++ absl/random/internal/randen_slow.cc | 197 ++++------ absl/random/internal/randen_slow.h | 7 - absl/random/internal/randen_slow_test.cc | 8 +- absl/random/internal/randen_traits.h | 31 +- absl/random/log_uniform_int_distribution_test.cc | 7 +- absl/random/poisson_distribution_test.cc | 12 +- absl/random/uniform_int_distribution_test.cc | 13 +- absl/random/uniform_real_distribution_test.cc | 13 +- absl/random/zipf_distribution_test.cc | 6 +- absl/strings/internal/str_format/arg.cc | 36 +- absl/strings/internal/str_format/arg.h | 2 + absl/strings/internal/str_format/arg_test.cc | 4 +- absl/strings/internal/str_format/checker_test.cc | 8 +- absl/strings/internal/str_format/extension.cc | 15 +- absl/strings/internal/str_format/extension.h | 80 +--- absl/strings/str_format.h | 4 - absl/strings/str_format_test.cc | 97 ++--- 36 files changed, 974 insertions(+), 834 deletions(-) delete mode 100644 absl/random/internal/randen-keys.inc create mode 100644 absl/random/internal/randen_round_keys.cc (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/AbseilDll.cmake b/CMake/AbseilDll.cmake index d342959b..a5c9bc0d 100644 --- a/CMake/AbseilDll.cmake +++ b/CMake/AbseilDll.cmake @@ -147,6 +147,7 @@ set(ABSL_INTERNAL_DLL_FILES "random/internal/platform.h" "random/internal/pool_urbg.cc" "random/internal/pool_urbg.h" + "random/internal/randen_round_keys.cc" "random/internal/randen.cc" "random/internal/randen.h" "random/internal/randen_detect.cc" diff --git a/absl/flags/config.h b/absl/flags/config.h index 001f8fea..813a9257 100644 --- a/absl/flags/config.h +++ b/absl/flags/config.h @@ -64,4 +64,24 @@ #define ABSL_FLAGS_INTERNAL_HAS_RTTI 1 #endif // !defined(__GNUC__) || defined(__GXX_RTTI) +// These macros represent the "source of truth" for the list of supported +// built-in types. +#define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \ + A(bool, bool) \ + A(short, short) \ + A(unsigned short, unsigned_short) \ + A(int, int) \ + A(unsigned int, unsigned_int) \ + A(long, long) \ + A(unsigned long, unsigned_long) \ + A(long long, long_long) \ + A(unsigned long long, unsigned_long_long) \ + A(double, double) \ + A(float, float) + +#define ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(A) \ + ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \ + A(std::string, std_string) \ + A(std::vector, std_vector_of_string) + #endif // ABSL_FLAGS_CONFIG_H_ diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc index 015b1fc9..416a31e5 100644 --- a/absl/flags/flag_test.cc +++ b/absl/flags/flag_test.cc @@ -145,8 +145,8 @@ DEFINE_CONSTRUCTED_FLAG(int32_t, 3, kOneWord); DEFINE_CONSTRUCTED_FLAG(uint32_t, 4, kOneWord); DEFINE_CONSTRUCTED_FLAG(int64_t, 5, kOneWord); DEFINE_CONSTRUCTED_FLAG(uint64_t, 6, kOneWord); -DEFINE_CONSTRUCTED_FLAG(float, 7.8, kFloat); -DEFINE_CONSTRUCTED_FLAG(double, 9.10, kDouble); +DEFINE_CONSTRUCTED_FLAG(float, 7.8, kOneWord); +DEFINE_CONSTRUCTED_FLAG(double, 9.10, kOneWord); DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt, kGenFunc); DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt, kGenFunc); diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h index 0a7197b7..fa050209 100644 --- a/absl/flags/internal/commandlineflag.h +++ b/absl/flags/internal/commandlineflag.h @@ -178,23 +178,6 @@ class CommandLineFlag { virtual void CheckDefaultValueParsingRoundtrip() const = 0; }; -// This macro is the "source of truth" for the list of supported flag built-in -// types. -#define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \ - A(bool) \ - A(short) \ - A(unsigned short) \ - A(int) \ - A(unsigned int) \ - A(long) \ - A(unsigned long) \ - A(long long) \ - A(unsigned long long) \ - A(double) \ - A(float) \ - A(std::string) \ - A(std::vector) - } // namespace flags_internal ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 8f0777fa..96c026dc 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -49,9 +49,9 @@ namespace { // Currently we only validate flag values for user-defined flag types. bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) { -#define DONT_VALIDATE(T) \ +#define DONT_VALIDATE(T, _) \ if (flag_type_id == base_internal::FastTypeId()) return false; - ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE) + ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(DONT_VALIDATE) #undef DONT_VALIDATE return true; @@ -150,23 +150,11 @@ void FlagImpl::Init() { break; case FlagValueStorageKind::kOneWordAtomic: { alignas(int64_t) std::array buf{}; - switch (def_kind) { - case FlagDefaultKind::kOneWord: - std::memcpy(buf.data(), &default_value_.one_word, - sizeof(default_value_.one_word)); - break; - case FlagDefaultKind::kFloat: - std::memcpy(buf.data(), &default_value_.float_value, - sizeof(default_value_.float_value)); - break; - case FlagDefaultKind::kDouble: - std::memcpy(buf.data(), &default_value_.double_value, - sizeof(default_value_.double_value)); - break; - default: - assert(def_kind == FlagDefaultKind::kGenFunc); - (*default_value_.gen_func)(buf.data()); - break; + if (def_kind == FlagDefaultKind::kGenFunc) { + (*default_value_.gen_func)(buf.data()); + } else { + assert(def_kind != FlagDefaultKind::kDynamicValue); + std::memcpy(buf.data(), &default_value_, Sizeof(op_)); } OneWordValue().store(absl::bit_cast(buf), std::memory_order_release); @@ -228,14 +216,8 @@ std::unique_ptr FlagImpl::MakeInitValue() const { res = flags_internal::Alloc(op_); (*default_value_.gen_func)(res); break; - case FlagDefaultKind::kOneWord: - res = flags_internal::Clone(op_, &default_value_.one_word); - break; - case FlagDefaultKind::kFloat: - res = flags_internal::Clone(op_, &default_value_.float_value); - break; - case FlagDefaultKind::kDouble: - res = flags_internal::Clone(op_, &default_value_.double_value); + default: + res = flags_internal::Clone(op_, &default_value_); break; } return {res, DynValueDeleter{op_}}; diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index 146c3efc..e374ecde 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -231,25 +231,21 @@ using FlagDfltGenFunc = void (*)(void*); union FlagDefaultSrc { constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg) : gen_func(gen_func_arg) {} - template - constexpr explicit FlagDefaultSrc(T one_word_value) - : one_word(static_cast(one_word_value)) {} - constexpr explicit FlagDefaultSrc(float f) : float_value(f) {} - constexpr explicit FlagDefaultSrc(double d) : double_value(d) {} + +#define ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE(T, name) \ + T name##_value; \ + constexpr explicit FlagDefaultSrc(T value) : name##_value(value) {} // NOLINT + ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE) +#undef ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE void* dynamic_value; FlagDfltGenFunc gen_func; - int64_t one_word; - float float_value; - double double_value; }; enum class FlagDefaultKind : uint8_t { kDynamicValue = 0, kGenFunc = 1, - kOneWord = 2, - kFloat = 3, - kDouble = 4 + kOneWord = 2 // for default values UP to one word in size }; struct FlagDefaultArg { @@ -279,20 +275,6 @@ constexpr FlagDefaultArg DefaultArg(int) { return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord}; } -template ::value, - int>::type = (GenT{}, 0)> -constexpr FlagDefaultArg DefaultArg(int) { - return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kFloat}; -} - -template ::value, - int>::type = (GenT{}, 0)> -constexpr FlagDefaultArg DefaultArg(int) { - return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kDouble}; -} - template constexpr FlagDefaultArg DefaultArg(char) { return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc}; @@ -576,9 +558,8 @@ class FlagImpl final : public flags_internal::CommandLineFlag { // Mutable flag's state (guarded by `data_guard_`). // def_kind_ is not guard by DataGuard() since it is accessed in Init without - // locks. If necessary we can decrease number of bits used to 2 by folding - // one_word storage cases. - uint8_t def_kind_ : 3; + // locks. + uint8_t def_kind_ : 2; // Has this flag's value been modified? bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard()); // Has this flag been specified on command line. diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 2e8f03b4..fbf42675 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -309,11 +309,11 @@ void CheckDefaultValuesParsingRoundtrip() { flags_internal::ForEachFlag([&](CommandLineFlag* flag) { if (flag->IsRetired()) return; -#define IGNORE_TYPE(T) \ +#define ABSL_FLAGS_INTERNAL_IGNORE_TYPE(T, _) \ if (flag->IsOfType()) return; - ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(IGNORE_TYPE) -#undef IGNORE_TYPE + ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(ABSL_FLAGS_INTERNAL_IGNORE_TYPE) +#undef ABSL_FLAGS_INTERNAL_IGNORE_TYPE flags_internal::PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip( *flag); diff --git a/absl/random/BUILD.bazel b/absl/random/BUILD.bazel index e61d31b5..9ba75b52 100644 --- a/absl/random/BUILD.bazel +++ b/absl/random/BUILD.bazel @@ -168,6 +168,7 @@ cc_test( deps = [ ":distributions", ":random", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "@com_google_googletest//:gtest_main", ], @@ -186,6 +187,7 @@ cc_test( ":random", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "//absl/strings:str_format", @@ -233,9 +235,9 @@ cc_test( deps = [ ":distributions", ":random", - "//absl/base:core_headers", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "//absl/strings:str_format", @@ -256,6 +258,7 @@ cc_test( ":random", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "@com_google_googletest//:gtest_main", @@ -283,6 +286,7 @@ cc_test( "//absl/base:raw_logging_internal", "//absl/container:flat_hash_map", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "//absl/strings:str_format", @@ -302,6 +306,7 @@ cc_test( "//absl/base:core_headers", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "//absl/strings:str_format", @@ -345,6 +350,7 @@ cc_test( ":random", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "@com_google_googletest//:gtest_main", @@ -369,6 +375,7 @@ cc_test( ":random", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "@com_google_googletest//:gtest_main", @@ -388,6 +395,7 @@ cc_test( ":random", "//absl/base:raw_logging_internal", "//absl/random/internal:distribution_test_util", + "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg", "//absl/strings", "@com_google_googletest//:gtest_main", diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index 69bedbd6..ec616dd9 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt @@ -244,6 +244,7 @@ absl_cc_test( absl::random_distributions absl::random_random absl::random_internal_sequence_urbg + absl::random_internal_pcg_engine gmock gtest_main ) @@ -262,6 +263,7 @@ absl_cc_test( absl::random_random absl::random_internal_distribution_test_util absl::random_internal_sequence_urbg + absl::random_internal_pcg_engine absl::raw_logging_internal absl::strings absl::str_format @@ -311,9 +313,9 @@ absl_cc_test( ${ABSL_TEST_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} - absl::core_headers absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::raw_logging_internal @@ -335,6 +337,7 @@ absl_cc_test( DEPS absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::raw_logging_internal @@ -358,6 +361,7 @@ absl_cc_test( absl::core_headers absl::flat_hash_map absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::raw_logging_internal absl::strings @@ -379,6 +383,7 @@ absl_cc_test( absl::core_headers absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::raw_logging_internal @@ -422,6 +427,7 @@ absl_cc_test( DEPS absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::raw_logging_internal @@ -442,6 +448,7 @@ absl_cc_test( DEPS absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::strings @@ -461,6 +468,7 @@ absl_cc_test( DEPS absl::random_distributions absl::random_internal_distribution_test_util + absl::random_internal_pcg_engine absl::random_internal_sequence_urbg absl::random_random absl::raw_logging_internal @@ -782,8 +790,9 @@ absl_cc_library( random_internal_platform HDRS "internal/randen_traits.h" - "internal/randen-keys.inc" "internal/platform.h" + SRCS + "internal/randen_round_keys.cc" COPTS ${ABSL_DEFAULT_COPTS} LINKOPTS diff --git a/absl/random/bernoulli_distribution_test.cc b/absl/random/bernoulli_distribution_test.cc index 5581af50..b250f878 100644 --- a/absl/random/bernoulli_distribution_test.cc +++ b/absl/random/bernoulli_distribution_test.cc @@ -21,6 +21,7 @@ #include #include "gtest/gtest.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" @@ -63,7 +64,10 @@ TEST_P(BernoulliTest, Accuracy) { size_t trials = para.second; double p = para.first; - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng(0x2B7E151628AED2A6); size_t yes = 0; absl::bernoulli_distribution dist(p); diff --git a/absl/random/beta_distribution_test.cc b/absl/random/beta_distribution_test.cc index d0111b3e..277e4dc6 100644 --- a/absl/random/beta_distribution_test.cc +++ b/absl/random/beta_distribution_test.cc @@ -29,6 +29,7 @@ #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -159,8 +160,12 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { } TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng(0x2B7E151628AED2A6); + // Extreme cases when the params are abnormal. - absl::InsecureBitGen gen; constexpr int kCount = 1000; const TypeParam kSmallValues[] = { std::numeric_limits::min(), @@ -186,7 +191,7 @@ TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { int ones = 0; absl::beta_distribution d(alpha, beta); for (int i = 0; i < kCount; ++i) { - TypeParam x = d(gen); + TypeParam x = d(rng); if (x == 0.0) { zeros++; } else if (x == 1.0) { @@ -212,7 +217,7 @@ TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { for (TypeParam beta : kLargeValues) { absl::beta_distribution d(alpha, beta); for (int i = 0; i < kCount; ++i) { - EXPECT_EQ(d(gen), 0.0); + EXPECT_EQ(d(rng), 0.0); } } } @@ -227,7 +232,7 @@ TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { for (TypeParam beta : kSmallValues) { absl::beta_distribution d(alpha, beta); for (int i = 0; i < kCount; ++i) { - EXPECT_EQ(d(gen), 1.0); + EXPECT_EQ(d(rng), 1.0); } } } @@ -237,7 +242,7 @@ TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { absl::beta_distribution d(std::numeric_limits::max(), std::numeric_limits::max()); for (int i = 0; i < kCount; ++i) { - EXPECT_EQ(d(gen), 0.5); + EXPECT_EQ(d(rng), 0.5); } } { @@ -246,7 +251,7 @@ TYPED_TEST(BetaDistributionInterfaceTest, DegenerateCases) { std::numeric_limits::max(), std::numeric_limits::max() * 0.9999); for (int i = 0; i < kCount; ++i) { - TypeParam x = d(gen); + TypeParam x = d(rng); EXPECT_NE(x, 0.5f); EXPECT_FLOAT_EQ(x, 0.500025f); } diff --git a/absl/random/discrete_distribution_test.cc b/absl/random/discrete_distribution_test.cc index 7296f0ac..6d007006 100644 --- a/absl/random/discrete_distribution_test.cc +++ b/absl/random/discrete_distribution_test.cc @@ -29,6 +29,7 @@ #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -156,7 +157,10 @@ TEST(DiscreteDistributionTest, ChiSquaredTest50) { std::iota(std::begin(weights), std::end(weights), 1); absl::discrete_distribution dist(std::begin(weights), std::end(weights)); - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng(0x2B7E151628AED2A6); std::vector counts(kBuckets, 0); for (size_t i = 0; i < kTrials; i++) { diff --git a/absl/random/exponential_distribution_test.cc b/absl/random/exponential_distribution_test.cc index f3cfd764..8e9e69b6 100644 --- a/absl/random/exponential_distribution_test.cc +++ b/absl/random/exponential_distribution_test.cc @@ -32,6 +32,7 @@ #include "absl/base/macros.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -205,7 +206,10 @@ class ExponentialDistributionTests : public testing::TestWithParam, template double SingleChiSquaredTest(); - absl::InsecureBitGen rng_; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; template diff --git a/absl/random/gaussian_distribution_test.cc b/absl/random/gaussian_distribution_test.cc index 398f0131..02ac578a 100644 --- a/absl/random/gaussian_distribution_test.cc +++ b/absl/random/gaussian_distribution_test.cc @@ -216,7 +216,10 @@ class GaussianDistributionTests : public testing::TestWithParam, template double SingleChiSquaredTest(); - absl::InsecureBitGen rng_; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; template diff --git a/absl/random/internal/BUILD.bazel b/absl/random/internal/BUILD.bazel index dc452816..85d1fb81 100644 --- a/absl/random/internal/BUILD.bazel +++ b/absl/random/internal/BUILD.bazel @@ -255,13 +255,15 @@ cc_library( cc_library( name = "platform", + srcs = [ + "randen_round_keys.cc", + ], hdrs = [ "randen_traits.h", ], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, textual_hdrs = [ - "randen-keys.inc", "platform.h", ], deps = ["//absl/base:config"], @@ -613,6 +615,7 @@ cc_test( copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + ":platform", ":randen_slow", "@com_google_googletest//:gtest_main", ], diff --git a/absl/random/internal/randen-keys.inc b/absl/random/internal/randen-keys.inc deleted file mode 100644 index fa4b1668..00000000 --- a/absl/random/internal/randen-keys.inc +++ /dev/null @@ -1,207 +0,0 @@ -// Copyright 2017 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef ABSL_RANDOM_INTERNAL_RANDEN_KEYS_INC_ -#define ABSL_RANDOM_INTERNAL_RANDEN_KEYS_INC_ - -// Textual header to include the randen_keys where necessary. -// REQUIRES: struct u64x2{} -// -// PROVIDES: kKeys -// PROVIDES: round_keys[] - -// "Nothing up my sleeve" numbers from the first hex digits of Pi, obtained -// from http://hexpi.sourceforge.net/. The array was generated by following -// Python script: -/* -python << EOF -"""Generates Randen round keys array from pi-hex.62500.txt file.""" -import binascii - -KEYS = 136 - -def chunks(l, n): - """Yield successive n-sized chunks from l.""" - for i in range(0, len(l), n): - yield l[i:i + n] - -def pairwise(t): - """Transforms sequence into sequence of pairs.""" - it = iter(t) - return zip(it,it) - -def digits_from_pi(): - """Reads digits from hexpi.sourceforge.net file.""" - with open("pi-hex.62500.txt") as file: - return file.read() - -def digits_from_urandom(): - """Reads digits from /dev/urandom.""" - with open("/dev/urandom") as file: - return binascii.hexlify(file.read(KEYS * 16)) - -digits = digits_from_pi() -print("static constexpr const size_t kRoundKeys = {0};\n".format(KEYS)) -print("alignas(16) constexpr const u64x2 round_keys[kRoundKeys] = {") - -for i, (hi, lo) in zip(range(KEYS), pairwise(chunks(digits, 16))): - hi = "0x{0}ull".format(hi) - lo = "0x{0}ull".format(lo) - print(" u64x2({0}, {1}){2}".format(hi, lo, ',' if i+1 < KEYS else '')) - -print("};") -EOF -*/ - -static constexpr const size_t kRoundKeys = 136; - -alignas(16) constexpr u64x2 round_keys[kRoundKeys] = { - u64x2(0x243F6A8885A308D3ull, 0x13198A2E03707344ull), - u64x2(0xA4093822299F31D0ull, 0x082EFA98EC4E6C89ull), - u64x2(0x452821E638D01377ull, 0xBE5466CF34E90C6Cull), - u64x2(0xC0AC29B7C97C50DDull, 0x3F84D5B5B5470917ull), - u64x2(0x9216D5D98979FB1Bull, 0xD1310BA698DFB5ACull), - u64x2(0x2FFD72DBD01ADFB7ull, 0xB8E1AFED6A267E96ull), - u64x2(0xBA7C9045F12C7F99ull, 0x24A19947B3916CF7ull), - u64x2(0x0801F2E2858EFC16ull, 0x636920D871574E69ull), - u64x2(0xA458FEA3F4933D7Eull, 0x0D95748F728EB658ull), - u64x2(0x718BCD5882154AEEull, 0x7B54A41DC25A59B5ull), - u64x2(0x9C30D5392AF26013ull, 0xC5D1B023286085F0ull), - u64x2(0xCA417918B8DB38EFull, 0x8E79DCB0603A180Eull), - u64x2(0x6C9E0E8BB01E8A3Eull, 0xD71577C1BD314B27ull), - u64x2(0x78AF2FDA55605C60ull, 0xE65525F3AA55AB94ull), - u64x2(0x5748986263E81440ull, 0x55CA396A2AAB10B6ull), - u64x2(0xB4CC5C341141E8CEull, 0xA15486AF7C72E993ull), - u64x2(0xB3EE1411636FBC2Aull, 0x2BA9C55D741831F6ull), - u64x2(0xCE5C3E169B87931Eull, 0xAFD6BA336C24CF5Cull), - u64x2(0x7A32538128958677ull, 0x3B8F48986B4BB9AFull), - u64x2(0xC4BFE81B66282193ull, 0x61D809CCFB21A991ull), - u64x2(0x487CAC605DEC8032ull, 0xEF845D5DE98575B1ull), - u64x2(0xDC262302EB651B88ull, 0x23893E81D396ACC5ull), - u64x2(0x0F6D6FF383F44239ull, 0x2E0B4482A4842004ull), - u64x2(0x69C8F04A9E1F9B5Eull, 0x21C66842F6E96C9Aull), - u64x2(0x670C9C61ABD388F0ull, 0x6A51A0D2D8542F68ull), - u64x2(0x960FA728AB5133A3ull, 0x6EEF0B6C137A3BE4ull), - u64x2(0xBA3BF0507EFB2A98ull, 0xA1F1651D39AF0176ull), - u64x2(0x66CA593E82430E88ull, 0x8CEE8619456F9FB4ull), - u64x2(0x7D84A5C33B8B5EBEull, 0xE06F75D885C12073ull), - u64x2(0x401A449F56C16AA6ull, 0x4ED3AA62363F7706ull), - u64x2(0x1BFEDF72429B023Dull, 0x37D0D724D00A1248ull), - u64x2(0xDB0FEAD349F1C09Bull, 0x075372C980991B7Bull), - u64x2(0x25D479D8F6E8DEF7ull, 0xE3FE501AB6794C3Bull), - u64x2(0x976CE0BD04C006BAull, 0xC1A94FB6409F60C4ull), - u64x2(0x5E5C9EC2196A2463ull, 0x68FB6FAF3E6C53B5ull), - u64x2(0x1339B2EB3B52EC6Full, 0x6DFC511F9B30952Cull), - u64x2(0xCC814544AF5EBD09ull, 0xBEE3D004DE334AFDull), - u64x2(0x660F2807192E4BB3ull, 0xC0CBA85745C8740Full), - u64x2(0xD20B5F39B9D3FBDBull, 0x5579C0BD1A60320Aull), - u64x2(0xD6A100C6402C7279ull, 0x679F25FEFB1FA3CCull), - u64x2(0x8EA5E9F8DB3222F8ull, 0x3C7516DFFD616B15ull), - u64x2(0x2F501EC8AD0552ABull, 0x323DB5FAFD238760ull), - u64x2(0x53317B483E00DF82ull, 0x9E5C57BBCA6F8CA0ull), - u64x2(0x1A87562EDF1769DBull, 0xD542A8F6287EFFC3ull), - u64x2(0xAC6732C68C4F5573ull, 0x695B27B0BBCA58C8ull), - u64x2(0xE1FFA35DB8F011A0ull, 0x10FA3D98FD2183B8ull), - u64x2(0x4AFCB56C2DD1D35Bull, 0x9A53E479B6F84565ull), - u64x2(0xD28E49BC4BFB9790ull, 0xE1DDF2DAA4CB7E33ull), - u64x2(0x62FB1341CEE4C6E8ull, 0xEF20CADA36774C01ull), - u64x2(0xD07E9EFE2BF11FB4ull, 0x95DBDA4DAE909198ull), - u64x2(0xEAAD8E716B93D5A0ull, 0xD08ED1D0AFC725E0ull), - u64x2(0x8E3C5B2F8E7594B7ull, 0x8FF6E2FBF2122B64ull), - u64x2(0x8888B812900DF01Cull, 0x4FAD5EA0688FC31Cull), - u64x2(0xD1CFF191B3A8C1ADull, 0x2F2F2218BE0E1777ull), - u64x2(0xEA752DFE8B021FA1ull, 0xE5A0CC0FB56F74E8ull), - u64x2(0x18ACF3D6CE89E299ull, 0xB4A84FE0FD13E0B7ull), - u64x2(0x7CC43B81D2ADA8D9ull, 0x165FA26680957705ull), - u64x2(0x93CC7314211A1477ull, 0xE6AD206577B5FA86ull), - u64x2(0xC75442F5FB9D35CFull, 0xEBCDAF0C7B3E89A0ull), - u64x2(0xD6411BD3AE1E7E49ull, 0x00250E2D2071B35Eull), - u64x2(0x226800BB57B8E0AFull, 0x2464369BF009B91Eull), - u64x2(0x5563911D59DFA6AAull, 0x78C14389D95A537Full), - u64x2(0x207D5BA202E5B9C5ull, 0x832603766295CFA9ull), - u64x2(0x11C819684E734A41ull, 0xB3472DCA7B14A94Aull), - u64x2(0x1B5100529A532915ull, 0xD60F573FBC9BC6E4ull), - u64x2(0x2B60A47681E67400ull, 0x08BA6FB5571BE91Full), - u64x2(0xF296EC6B2A0DD915ull, 0xB6636521E7B9F9B6ull), - u64x2(0xFF34052EC5855664ull, 0x53B02D5DA99F8FA1ull), - u64x2(0x08BA47996E85076Aull, 0x4B7A70E9B5B32944ull), - u64x2(0xDB75092EC4192623ull, 0xAD6EA6B049A7DF7Dull), - u64x2(0x9CEE60B88FEDB266ull, 0xECAA8C71699A18FFull), - u64x2(0x5664526CC2B19EE1ull, 0x193602A575094C29ull), - u64x2(0xA0591340E4183A3Eull, 0x3F54989A5B429D65ull), - u64x2(0x6B8FE4D699F73FD6ull, 0xA1D29C07EFE830F5ull), - u64x2(0x4D2D38E6F0255DC1ull, 0x4CDD20868470EB26ull), - u64x2(0x6382E9C6021ECC5Eull, 0x09686B3F3EBAEFC9ull), - u64x2(0x3C9718146B6A70A1ull, 0x687F358452A0E286ull), - u64x2(0xB79C5305AA500737ull, 0x3E07841C7FDEAE5Cull), - u64x2(0x8E7D44EC5716F2B8ull, 0xB03ADA37F0500C0Dull), - u64x2(0xF01C1F040200B3FFull, 0xAE0CF51A3CB574B2ull), - u64x2(0x25837A58DC0921BDull, 0xD19113F97CA92FF6ull), - u64x2(0x9432477322F54701ull, 0x3AE5E58137C2DADCull), - u64x2(0xC8B576349AF3DDA7ull, 0xA94461460FD0030Eull), - u64x2(0xECC8C73EA4751E41ull, 0xE238CD993BEA0E2Full), - u64x2(0x3280BBA1183EB331ull, 0x4E548B384F6DB908ull), - u64x2(0x6F420D03F60A04BFull, 0x2CB8129024977C79ull), - u64x2(0x5679B072BCAF89AFull, 0xDE9A771FD9930810ull), - u64x2(0xB38BAE12DCCF3F2Eull, 0x5512721F2E6B7124ull), - u64x2(0x501ADDE69F84CD87ull, 0x7A5847187408DA17ull), - u64x2(0xBC9F9ABCE94B7D8Cull, 0xEC7AEC3ADB851DFAull), - u64x2(0x63094366C464C3D2ull, 0xEF1C18473215D808ull), - u64x2(0xDD433B3724C2BA16ull, 0x12A14D432A65C451ull), - u64x2(0x50940002133AE4DDull, 0x71DFF89E10314E55ull), - u64x2(0x81AC77D65F11199Bull, 0x043556F1D7A3C76Bull), - u64x2(0x3C11183B5924A509ull, 0xF28FE6ED97F1FBFAull), - u64x2(0x9EBABF2C1E153C6Eull, 0x86E34570EAE96FB1ull), - u64x2(0x860E5E0A5A3E2AB3ull, 0x771FE71C4E3D06FAull), - u64x2(0x2965DCB999E71D0Full, 0x803E89D65266C825ull), - u64x2(0x2E4CC9789C10B36Aull, 0xC6150EBA94E2EA78ull), - u64x2(0xA6FC3C531E0A2DF4ull, 0xF2F74EA7361D2B3Dull), - u64x2(0x1939260F19C27960ull, 0x5223A708F71312B6ull), - u64x2(0xEBADFE6EEAC31F66ull, 0xE3BC4595A67BC883ull), - u64x2(0xB17F37D1018CFF28ull, 0xC332DDEFBE6C5AA5ull), - u64x2(0x6558218568AB9702ull, 0xEECEA50FDB2F953Bull), - u64x2(0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull), - u64x2(0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull), - u64x2(0x0334FE1EAA0363CFull, 0xB5735C904C70A239ull), - u64x2(0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull), - u64x2(0x9CAB5CABB2F3846Eull, 0x648B1EAF19BDF0CAull), - u64x2(0xA02369B9655ABB50ull, 0x40685A323C2AB4B3ull), - u64x2(0x319EE9D5C021B8F7ull, 0x9B540B19875FA099ull), - u64x2(0x95F7997E623D7DA8ull, 0xF837889A97E32D77ull), - u64x2(0x11ED935F16681281ull, 0x0E358829C7E61FD6ull), - u64x2(0x96DEDFA17858BA99ull, 0x57F584A51B227263ull), - u64x2(0x9B83C3FF1AC24696ull, 0xCDB30AEB532E3054ull), - u64x2(0x8FD948E46DBC3128ull, 0x58EBF2EF34C6FFEAull), - u64x2(0xFE28ED61EE7C3C73ull, 0x5D4A14D9E864B7E3ull), - u64x2(0x42105D14203E13E0ull, 0x45EEE2B6A3AAABEAull), - u64x2(0xDB6C4F15FACB4FD0ull, 0xC742F442EF6ABBB5ull), - u64x2(0x654F3B1D41CD2105ull, 0xD81E799E86854DC7ull), - u64x2(0xE44B476A3D816250ull, 0xCF62A1F25B8D2646ull), - u64x2(0xFC8883A0C1C7B6A3ull, 0x7F1524C369CB7492ull), - u64x2(0x47848A0B5692B285ull, 0x095BBF00AD19489Dull), - u64x2(0x1462B17423820D00ull, 0x58428D2A0C55F5EAull), - u64x2(0x1DADF43E233F7061ull, 0x3372F0928D937E41ull), - u64x2(0xD65FECF16C223BDBull, 0x7CDE3759CBEE7460ull), - u64x2(0x4085F2A7CE77326Eull, 0xA607808419F8509Eull), - u64x2(0xE8EFD85561D99735ull, 0xA969A7AAC50C06C2ull), - u64x2(0x5A04ABFC800BCADCull, 0x9E447A2EC3453484ull), - u64x2(0xFDD567050E1E9EC9ull, 0xDB73DBD3105588CDull), - u64x2(0x675FDA79E3674340ull, 0xC5C43465713E38D8ull), - u64x2(0x3D28F89EF16DFF20ull, 0x153E21E78FB03D4Aull), - u64x2(0xE6E39F2BDB83ADF7ull, 0xE93D5A68948140F7ull), - u64x2(0xF64C261C94692934ull, 0x411520F77602D4F7ull), - u64x2(0xBCF46B2ED4A10068ull, 0xD40824713320F46Aull), - u64x2(0x43B7D4B7500061AFull, 0x1E39F62E97244546ull)}; - -#endif // ABSL_RANDOM_INTERNAL_RANDEN_KEYS_INC_ diff --git a/absl/random/internal/randen_hwaes.cc b/absl/random/internal/randen_hwaes.cc index e23844f1..9966486f 100644 --- a/absl/random/internal/randen_hwaes.cc +++ b/absl/random/internal/randen_hwaes.cc @@ -24,6 +24,7 @@ #include "absl/base/attributes.h" #include "absl/random/internal/platform.h" +#include "absl/random/internal/randen_traits.h" // ABSL_RANDEN_HWAES_IMPL indicates whether this file will contain // a hardware accelerated implementation of randen, or whether it @@ -115,8 +116,16 @@ ABSL_NAMESPACE_END // Accelerated implementations are supported. // We need the per-architecture includes and defines. // +namespace { -#include "absl/random/internal/randen_traits.h" +using absl::random_internal::RandenTraits; + +// Randen operates on 128-bit vectors. +struct alignas(16) u64x2 { + uint64_t data[2]; +}; + +} // namespace // TARGET_CRYPTO defines a crypto attribute for each architecture. // @@ -150,7 +159,6 @@ ABSL_NAMESPACE_END using Vector128 = __vector unsigned long long; // NOLINT(runtime/int) namespace { - inline ABSL_TARGET_CRYPTO Vector128 ReverseBytes(const Vector128& v) { // Reverses the bytes of the vector. const __vector unsigned char perm = {15, 14, 13, 12, 11, 10, 9, 8, @@ -177,14 +185,9 @@ inline ABSL_TARGET_CRYPTO Vector128 AesRound(const Vector128& state, } // Enables native loads in the round loop by pre-swapping. -inline ABSL_TARGET_CRYPTO void SwapEndian(uint64_t* state) { - using absl::random_internal::RandenTraits; - constexpr size_t kLanes = 2; - constexpr size_t kFeistelBlocks = RandenTraits::kFeistelBlocks; - - for (uint32_t branch = 0; branch < kFeistelBlocks; ++branch) { - const Vector128 v = ReverseBytes(Vector128Load(state + kLanes * branch)); - Vector128Store(v, state + kLanes * branch); +inline ABSL_TARGET_CRYPTO void SwapEndian(u64x2* state) { + for (uint32_t block = 0; block < RandenTraits::kFeistelBlocks; ++block) { + Vector128Store(ReverseBytes(Vector128Load(state + block)), state + block); } } @@ -251,7 +254,7 @@ inline ABSL_TARGET_CRYPTO Vector128 AesRound(const Vector128& state, return vaesmcq_u8(vaeseq_u8(state, uint8x16_t{})) ^ round_key; } -inline ABSL_TARGET_CRYPTO void SwapEndian(uint64_t*) {} +inline ABSL_TARGET_CRYPTO void SwapEndian(void*) {} } // namespace @@ -297,39 +300,12 @@ inline ABSL_TARGET_CRYPTO Vector128 AesRound(const Vector128& state, return Vector128(_mm_aesenc_si128(state.data(), round_key.data())); } -inline ABSL_TARGET_CRYPTO void SwapEndian(uint64_t*) {} +inline ABSL_TARGET_CRYPTO void SwapEndian(void*) {} } // namespace #endif -namespace { - -// u64x2 is a 128-bit, (2 x uint64_t lanes) struct used to store -// the randen_keys. -struct alignas(16) u64x2 { - constexpr u64x2(uint64_t hi, uint64_t lo) -#if defined(ABSL_ARCH_PPC) - // This has been tested with PPC running in little-endian mode; - // We byte-swap the u64x2 structure from little-endian to big-endian - // because altivec always runs in big-endian mode. - : v{__builtin_bswap64(hi), __builtin_bswap64(lo)} { -#else - : v{lo, hi} { -#endif - } - - constexpr bool operator==(const u64x2& other) const { - return v[0] == other.v[0] && v[1] == other.v[1]; - } - - constexpr bool operator!=(const u64x2& other) const { - return !(*this == other); - } - - uint64_t v[2]; -}; // namespace - #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunknown-pragmas" @@ -338,7 +314,6 @@ struct alignas(16) u64x2 { // At this point, all of the platform-specific features have been defined / // implemented. // -// REQUIRES: using u64x2 = ... // REQUIRES: using Vector128 = ... // REQUIRES: Vector128 Vector128Load(void*) {...} // REQUIRES: void Vector128Store(Vector128, void*) {...} @@ -347,94 +322,50 @@ struct alignas(16) u64x2 { // // PROVIDES: absl::random_internal::RandenHwAes::Absorb // PROVIDES: absl::random_internal::RandenHwAes::Generate - -// RANDen = RANDom generator or beetroots in Swiss German. -// 'Strong' (well-distributed, unpredictable, backtracking-resistant) random -// generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32. -// -// High-level summary: -// 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is -// a sponge-like random generator that requires a cryptographic permutation. -// It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by -// achieving backtracking resistance with only one Permute() per buffer. -// -// 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round -// Function" constructs up to 1024-bit permutations using an improved -// Generalized Feistel network with 2-round AES-128 functions. This Feistel -// block shuffle achieves diffusion faster and is less vulnerable to -// sliced-biclique attacks than the Type-2 cyclic shuffle. -// -// 3) "Improving the Generalized Feistel" and "New criterion for diffusion -// property" extends the same kind of improved Feistel block shuffle to 16 -// branches, which enables a 2048-bit permutation. -// -// We combine these three ideas and also change Simpira's subround keys from -// structured/low-entropy counters to digits of Pi. - -// Randen constants. -using absl::random_internal::RandenTraits; -constexpr size_t kStateBytes = RandenTraits::kStateBytes; -constexpr size_t kCapacityBytes = RandenTraits::kCapacityBytes; -constexpr size_t kFeistelBlocks = RandenTraits::kFeistelBlocks; -constexpr size_t kFeistelRounds = RandenTraits::kFeistelRounds; -constexpr size_t kFeistelFunctions = RandenTraits::kFeistelFunctions; - -// Independent keys (272 = 2.1 KiB) for the first AES subround of each function. -constexpr size_t kKeys = kFeistelRounds * kFeistelFunctions; - -// INCLUDE keys. -#include "absl/random/internal/randen-keys.inc" - -static_assert(kKeys == kRoundKeys, "kKeys and kRoundKeys must be equal"); -static_assert(round_keys[kKeys - 1] != u64x2(0, 0), - "Too few round_keys initializers"); - -// Number of uint64_t lanes per 128-bit vector; -constexpr size_t kLanes = 2; +namespace { // Block shuffles applies a shuffle to the entire state between AES rounds. // Improved odd-even shuffle from "New criterion for diffusion property". -inline ABSL_TARGET_CRYPTO void BlockShuffle(uint64_t* state) { - static_assert(kFeistelBlocks == 16, "Expecting 16 FeistelBlocks."); - - constexpr size_t shuffle[kFeistelBlocks] = {7, 2, 13, 4, 11, 8, 3, 6, - 15, 0, 9, 10, 1, 14, 5, 12}; - - // The fully unrolled loop without the memcpy improves the speed by about - // 30% over the equivalent loop. - const Vector128 v0 = Vector128Load(state + kLanes * shuffle[0]); - const Vector128 v1 = Vector128Load(state + kLanes * shuffle[1]); - const Vector128 v2 = Vector128Load(state + kLanes * shuffle[2]); - const Vector128 v3 = Vector128Load(state + kLanes * shuffle[3]); - const Vector128 v4 = Vector128Load(state + kLanes * shuffle[4]); - const Vector128 v5 = Vector128Load(state + kLanes * shuffle[5]); - const Vector128 v6 = Vector128Load(state + kLanes * shuffle[6]); - const Vector128 v7 = Vector128Load(state + kLanes * shuffle[7]); - const Vector128 w0 = Vector128Load(state + kLanes * shuffle[8]); - const Vector128 w1 = Vector128Load(state + kLanes * shuffle[9]); - const Vector128 w2 = Vector128Load(state + kLanes * shuffle[10]); - const Vector128 w3 = Vector128Load(state + kLanes * shuffle[11]); - const Vector128 w4 = Vector128Load(state + kLanes * shuffle[12]); - const Vector128 w5 = Vector128Load(state + kLanes * shuffle[13]); - const Vector128 w6 = Vector128Load(state + kLanes * shuffle[14]); - const Vector128 w7 = Vector128Load(state + kLanes * shuffle[15]); - - Vector128Store(v0, state + kLanes * 0); - Vector128Store(v1, state + kLanes * 1); - Vector128Store(v2, state + kLanes * 2); - Vector128Store(v3, state + kLanes * 3); - Vector128Store(v4, state + kLanes * 4); - Vector128Store(v5, state + kLanes * 5); - Vector128Store(v6, state + kLanes * 6); - Vector128Store(v7, state + kLanes * 7); - Vector128Store(w0, state + kLanes * 8); - Vector128Store(w1, state + kLanes * 9); - Vector128Store(w2, state + kLanes * 10); - Vector128Store(w3, state + kLanes * 11); - Vector128Store(w4, state + kLanes * 12); - Vector128Store(w5, state + kLanes * 13); - Vector128Store(w6, state + kLanes * 14); - Vector128Store(w7, state + kLanes * 15); +inline ABSL_TARGET_CRYPTO void BlockShuffle(u64x2* state) { + static_assert(RandenTraits::kFeistelBlocks == 16, + "Expecting 16 FeistelBlocks."); + + constexpr size_t shuffle[RandenTraits::kFeistelBlocks] = { + 7, 2, 13, 4, 11, 8, 3, 6, 15, 0, 9, 10, 1, 14, 5, 12}; + + const Vector128 v0 = Vector128Load(state + shuffle[0]); + const Vector128 v1 = Vector128Load(state + shuffle[1]); + const Vector128 v2 = Vector128Load(state + shuffle[2]); + const Vector128 v3 = Vector128Load(state + shuffle[3]); + const Vector128 v4 = Vector128Load(state + shuffle[4]); + const Vector128 v5 = Vector128Load(state + shuffle[5]); + const Vector128 v6 = Vector128Load(state + shuffle[6]); + const Vector128 v7 = Vector128Load(state + shuffle[7]); + const Vector128 w0 = Vector128Load(state + shuffle[8]); + const Vector128 w1 = Vector128Load(state + shuffle[9]); + const Vector128 w2 = Vector128Load(state + shuffle[10]); + const Vector128 w3 = Vector128Load(state + shuffle[11]); + const Vector128 w4 = Vector128Load(state + shuffle[12]); + const Vector128 w5 = Vector128Load(state + shuffle[13]); + const Vector128 w6 = Vector128Load(state + shuffle[14]); + const Vector128 w7 = Vector128Load(state + shuffle[15]); + + Vector128Store(v0, state + 0); + Vector128Store(v1, state + 1); + Vector128Store(v2, state + 2); + Vector128Store(v3, state + 3); + Vector128Store(v4, state + 4); + Vector128Store(v5, state + 5); + Vector128Store(v6, state + 6); + Vector128Store(v7, state + 7); + Vector128Store(w0, state + 8); + Vector128Store(w1, state + 9); + Vector128Store(w2, state + 10); + Vector128Store(w3, state + 11); + Vector128Store(w4, state + 12); + Vector128Store(w5, state + 13); + Vector128Store(w6, state + 14); + Vector128Store(w7, state + 15); } // Feistel round function using two AES subrounds. Very similar to F() @@ -443,27 +374,28 @@ inline ABSL_TARGET_CRYPTO void BlockShuffle(uint64_t* state) { // parallel hides the 7-cycle AESNI latency on HSW. Note that the Feistel // XORs are 'free' (included in the second AES instruction). inline ABSL_TARGET_CRYPTO const u64x2* FeistelRound( - uint64_t* state, const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys) { - static_assert(kFeistelBlocks == 16, "Expecting 16 FeistelBlocks."); + u64x2* state, const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys) { + static_assert(RandenTraits::kFeistelBlocks == 16, + "Expecting 16 FeistelBlocks."); // MSVC does a horrible job at unrolling loops. // So we unroll the loop by hand to improve the performance. - const Vector128 s0 = Vector128Load(state + kLanes * 0); - const Vector128 s1 = Vector128Load(state + kLanes * 1); - const Vector128 s2 = Vector128Load(state + kLanes * 2); - const Vector128 s3 = Vector128Load(state + kLanes * 3); - const Vector128 s4 = Vector128Load(state + kLanes * 4); - const Vector128 s5 = Vector128Load(state + kLanes * 5); - const Vector128 s6 = Vector128Load(state + kLanes * 6); - const Vector128 s7 = Vector128Load(state + kLanes * 7); - const Vector128 s8 = Vector128Load(state + kLanes * 8); - const Vector128 s9 = Vector128Load(state + kLanes * 9); - const Vector128 s10 = Vector128Load(state + kLanes * 10); - const Vector128 s11 = Vector128Load(state + kLanes * 11); - const Vector128 s12 = Vector128Load(state + kLanes * 12); - const Vector128 s13 = Vector128Load(state + kLanes * 13); - const Vector128 s14 = Vector128Load(state + kLanes * 14); - const Vector128 s15 = Vector128Load(state + kLanes * 15); + const Vector128 s0 = Vector128Load(state + 0); + const Vector128 s1 = Vector128Load(state + 1); + const Vector128 s2 = Vector128Load(state + 2); + const Vector128 s3 = Vector128Load(state + 3); + const Vector128 s4 = Vector128Load(state + 4); + const Vector128 s5 = Vector128Load(state + 5); + const Vector128 s6 = Vector128Load(state + 6); + const Vector128 s7 = Vector128Load(state + 7); + const Vector128 s8 = Vector128Load(state + 8); + const Vector128 s9 = Vector128Load(state + 9); + const Vector128 s10 = Vector128Load(state + 10); + const Vector128 s11 = Vector128Load(state + 11); + const Vector128 s12 = Vector128Load(state + 12); + const Vector128 s13 = Vector128Load(state + 13); + const Vector128 s14 = Vector128Load(state + 14); + const Vector128 s15 = Vector128Load(state + 15); // Encode even blocks with keys. const Vector128 e0 = AesRound(s0, Vector128Load(keys + 0)); @@ -486,14 +418,14 @@ inline ABSL_TARGET_CRYPTO const u64x2* FeistelRound( const Vector128 o15 = AesRound(e14, s15); // Store odd blocks. (These will be shuffled later). - Vector128Store(o1, state + kLanes * 1); - Vector128Store(o3, state + kLanes * 3); - Vector128Store(o5, state + kLanes * 5); - Vector128Store(o7, state + kLanes * 7); - Vector128Store(o9, state + kLanes * 9); - Vector128Store(o11, state + kLanes * 11); - Vector128Store(o13, state + kLanes * 13); - Vector128Store(o15, state + kLanes * 15); + Vector128Store(o1, state + 1); + Vector128Store(o3, state + 3); + Vector128Store(o5, state + 5); + Vector128Store(o7, state + 7); + Vector128Store(o9, state + 9); + Vector128Store(o11, state + 11); + Vector128Store(o13, state + 13); + Vector128Store(o15, state + 15); return keys + 8; } @@ -503,16 +435,13 @@ inline ABSL_TARGET_CRYPTO const u64x2* FeistelRound( // 2^64 queries if the round function is a PRF. This is similar to the b=8 case // of Simpira v2, but more efficient than its generic construction for b=16. inline ABSL_TARGET_CRYPTO void Permute( - const void* ABSL_RANDOM_INTERNAL_RESTRICT keys, uint64_t* state) { - const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys128 = - static_cast(keys); - + u64x2* state, const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys) { // (Successfully unrolled; the first iteration jumps into the second half) #ifdef __clang__ #pragma clang loop unroll_count(2) #endif - for (size_t round = 0; round < kFeistelRounds; ++round) { - keys128 = FeistelRound(state, keys128); + for (size_t round = 0; round < RandenTraits::kFeistelRounds; ++round) { + keys = FeistelRound(state, keys); BlockShuffle(state); } } @@ -528,96 +457,101 @@ bool HasRandenHwAesImplementation() { return true; } const void* ABSL_TARGET_CRYPTO RandenHwAes::GetKeys() { // Round keys for one AES per Feistel round and branch. // The canonical implementation uses first digits of Pi. - return round_keys; +#if defined(ABSL_ARCH_PPC) + return kRandenRoundKeysBE; +#else + return kRandenRoundKeys; +#endif } // NOLINTNEXTLINE void ABSL_TARGET_CRYPTO RandenHwAes::Absorb(const void* seed_void, void* state_void) { - auto* state = static_cast(state_void); - const auto* seed = static_cast(seed_void); - - constexpr size_t kCapacityBlocks = kCapacityBytes / sizeof(Vector128); - constexpr size_t kStateBlocks = kStateBytes / sizeof(Vector128); - - static_assert(kCapacityBlocks * sizeof(Vector128) == kCapacityBytes, - "Not i*V"); - static_assert(kCapacityBlocks == 1, "Unexpected Randen kCapacityBlocks"); - static_assert(kStateBlocks == 16, "Unexpected Randen kStateBlocks"); - - Vector128 b1 = Vector128Load(state + kLanes * 1); - b1 ^= Vector128Load(seed + kLanes * 0); - Vector128Store(b1, state + kLanes * 1); - - Vector128 b2 = Vector128Load(state + kLanes * 2); - b2 ^= Vector128Load(seed + kLanes * 1); - Vector128Store(b2, state + kLanes * 2); - - Vector128 b3 = Vector128Load(state + kLanes * 3); - b3 ^= Vector128Load(seed + kLanes * 2); - Vector128Store(b3, state + kLanes * 3); - - Vector128 b4 = Vector128Load(state + kLanes * 4); - b4 ^= Vector128Load(seed + kLanes * 3); - Vector128Store(b4, state + kLanes * 4); - - Vector128 b5 = Vector128Load(state + kLanes * 5); - b5 ^= Vector128Load(seed + kLanes * 4); - Vector128Store(b5, state + kLanes * 5); - - Vector128 b6 = Vector128Load(state + kLanes * 6); - b6 ^= Vector128Load(seed + kLanes * 5); - Vector128Store(b6, state + kLanes * 6); - - Vector128 b7 = Vector128Load(state + kLanes * 7); - b7 ^= Vector128Load(seed + kLanes * 6); - Vector128Store(b7, state + kLanes * 7); - - Vector128 b8 = Vector128Load(state + kLanes * 8); - b8 ^= Vector128Load(seed + kLanes * 7); - Vector128Store(b8, state + kLanes * 8); - - Vector128 b9 = Vector128Load(state + kLanes * 9); - b9 ^= Vector128Load(seed + kLanes * 8); - Vector128Store(b9, state + kLanes * 9); - - Vector128 b10 = Vector128Load(state + kLanes * 10); - b10 ^= Vector128Load(seed + kLanes * 9); - Vector128Store(b10, state + kLanes * 10); - - Vector128 b11 = Vector128Load(state + kLanes * 11); - b11 ^= Vector128Load(seed + kLanes * 10); - Vector128Store(b11, state + kLanes * 11); - - Vector128 b12 = Vector128Load(state + kLanes * 12); - b12 ^= Vector128Load(seed + kLanes * 11); - Vector128Store(b12, state + kLanes * 12); - - Vector128 b13 = Vector128Load(state + kLanes * 13); - b13 ^= Vector128Load(seed + kLanes * 12); - Vector128Store(b13, state + kLanes * 13); - - Vector128 b14 = Vector128Load(state + kLanes * 14); - b14 ^= Vector128Load(seed + kLanes * 13); - Vector128Store(b14, state + kLanes * 14); - - Vector128 b15 = Vector128Load(state + kLanes * 15); - b15 ^= Vector128Load(seed + kLanes * 14); - Vector128Store(b15, state + kLanes * 15); + static_assert(RandenTraits::kCapacityBytes / sizeof(Vector128) == 1, + "Unexpected Randen kCapacityBlocks"); + static_assert(RandenTraits::kStateBytes / sizeof(Vector128) == 16, + "Unexpected Randen kStateBlocks"); + + auto* state = + reinterpret_cast(state_void); + const auto* seed = + reinterpret_cast(seed_void); + + Vector128 b1 = Vector128Load(state + 1); + b1 ^= Vector128Load(seed + 0); + Vector128Store(b1, state + 1); + + Vector128 b2 = Vector128Load(state + 2); + b2 ^= Vector128Load(seed + 1); + Vector128Store(b2, state + 2); + + Vector128 b3 = Vector128Load(state + 3); + b3 ^= Vector128Load(seed + 2); + Vector128Store(b3, state + 3); + + Vector128 b4 = Vector128Load(state + 4); + b4 ^= Vector128Load(seed + 3); + Vector128Store(b4, state + 4); + + Vector128 b5 = Vector128Load(state + 5); + b5 ^= Vector128Load(seed + 4); + Vector128Store(b5, state + 5); + + Vector128 b6 = Vector128Load(state + 6); + b6 ^= Vector128Load(seed + 5); + Vector128Store(b6, state + 6); + + Vector128 b7 = Vector128Load(state + 7); + b7 ^= Vector128Load(seed + 6); + Vector128Store(b7, state + 7); + + Vector128 b8 = Vector128Load(state + 8); + b8 ^= Vector128Load(seed + 7); + Vector128Store(b8, state + 8); + + Vector128 b9 = Vector128Load(state + 9); + b9 ^= Vector128Load(seed + 8); + Vector128Store(b9, state + 9); + + Vector128 b10 = Vector128Load(state + 10); + b10 ^= Vector128Load(seed + 9); + Vector128Store(b10, state + 10); + + Vector128 b11 = Vector128Load(state + 11); + b11 ^= Vector128Load(seed + 10); + Vector128Store(b11, state + 11); + + Vector128 b12 = Vector128Load(state + 12); + b12 ^= Vector128Load(seed + 11); + Vector128Store(b12, state + 12); + + Vector128 b13 = Vector128Load(state + 13); + b13 ^= Vector128Load(seed + 12); + Vector128Store(b13, state + 13); + + Vector128 b14 = Vector128Load(state + 14); + b14 ^= Vector128Load(seed + 13); + Vector128Store(b14, state + 14); + + Vector128 b15 = Vector128Load(state + 15); + b15 ^= Vector128Load(seed + 14); + Vector128Store(b15, state + 15); } // NOLINTNEXTLINE -void ABSL_TARGET_CRYPTO RandenHwAes::Generate(const void* keys, +void ABSL_TARGET_CRYPTO RandenHwAes::Generate(const void* keys_void, void* state_void) { - static_assert(kCapacityBytes == sizeof(Vector128), "Capacity mismatch"); + static_assert(RandenTraits::kCapacityBytes == sizeof(Vector128), + "Capacity mismatch"); - auto* state = static_cast(state_void); + auto* state = reinterpret_cast(state_void); + const auto* keys = reinterpret_cast(keys_void); const Vector128 prev_inner = Vector128Load(state); SwapEndian(state); - Permute(keys, state); + Permute(state, keys); SwapEndian(state); diff --git a/absl/random/internal/randen_hwaes_test.cc b/absl/random/internal/randen_hwaes_test.cc index a7cbd46b..66ddb43f 100644 --- a/absl/random/internal/randen_hwaes_test.cc +++ b/absl/random/internal/randen_hwaes_test.cc @@ -27,12 +27,14 @@ namespace { using absl::random_internal::RandenHwAes; using absl::random_internal::RandenTraits; -struct randen { - static constexpr size_t kStateSizeT = - RandenTraits::kStateBytes / sizeof(uint64_t); +// Local state parameters. +constexpr size_t kSeedBytes = + RandenTraits::kStateBytes - RandenTraits::kCapacityBytes; +constexpr size_t kStateSizeT = RandenTraits::kStateBytes / sizeof(uint64_t); +constexpr size_t kSeedSizeT = kSeedBytes / sizeof(uint32_t); + +struct alignas(16) randen { uint64_t state[kStateSizeT]; - static constexpr size_t kSeedSizeT = - RandenTraits::kSeedBytes / sizeof(uint32_t); uint32_t seed[kSeedSizeT]; }; diff --git a/absl/random/internal/randen_round_keys.cc b/absl/random/internal/randen_round_keys.cc new file mode 100644 index 00000000..5fb3ca55 --- /dev/null +++ b/absl/random/internal/randen_round_keys.cc @@ -0,0 +1,462 @@ +// Copyright 2017 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/random/internal/randen_traits.h" + +// This file contains only the round keys for randen. +// +// "Nothing up my sleeve" numbers from the first hex digits of Pi, obtained +// from http://hexpi.sourceforge.net/. The array was generated by following +// Python script: + +/* +python >tmp.cc << EOF +"""Generates Randen round keys array from pi-hex.62500.txt file.""" +import binascii + +KEYS = 17 * 8 + +def chunks(l, n): + """Yield successive n-sized chunks from l.""" + for i in range(0, len(l), n): + yield l[i:i + n] + +def pairwise(t): + """Transforms sequence into sequence of pairs.""" + it = iter(t) + return zip(it,it) + +def digits_from_pi(): + """Reads digits from hexpi.sourceforge.net file.""" + with open("pi-hex.62500.txt") as file: + return file.read() + +def digits_from_urandom(): + """Reads digits from /dev/urandom.""" + with open("/dev/urandom") as file: + return binascii.hexlify(file.read(KEYS * 16)) + +def print_row(b) + print(" 0x{0}, 0x{1}, 0x{2}, 0x{3}, 0x{4}, 0x{5}, 0x{6}, 0x{7}, 0x{8}, 0x{9}, +0x{10}, 0x{11}, 0x{12}, 0x{13}, 0x{14}, 0x{15},".format(*b)) + + +digits = digits_from_pi() +#digits = digits_from_urandom() + +print("namespace {") +print("static constexpr size_t kKeyBytes = {0};\n".format(KEYS * 16)) +print("}") + +print("alignas(16) const unsigned char kRandenRoundKeysBE[kKeyBytes] = {") + +for i, u16 in zip(range(KEYS), chunks(digits, 32)): + b = list(chunks(u16, 2)) + print_row(b) + +print("};") + +print("alignas(16) const unsigned char kRandenRoundKeys[kKeyBytes] = {") + +for i, u16 in zip(range(KEYS), chunks(digits, 32)): + b = list(chunks(u16, 2)) + b.reverse() + print_row(b) + +print("};") + +EOF + +*/ + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace random_internal { +namespace { +static constexpr size_t kKeyBytes = 2176; +} + +alignas(16) const unsigned char kRandenRoundKeysBE[kKeyBytes] = { + 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3, 0x13, 0x19, 0x8A, 0x2E, + 0x03, 0x70, 0x73, 0x44, 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0, + 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89, 0x45, 0x28, 0x21, 0xE6, + 0x38, 0xD0, 0x13, 0x77, 0xBE, 0x54, 0x66, 0xCF, 0x34, 0xE9, 0x0C, 0x6C, + 0xC0, 0xAC, 0x29, 0xB7, 0xC9, 0x7C, 0x50, 0xDD, 0x3F, 0x84, 0xD5, 0xB5, + 0xB5, 0x47, 0x09, 0x17, 0x92, 0x16, 0xD5, 0xD9, 0x89, 0x79, 0xFB, 0x1B, + 0xD1, 0x31, 0x0B, 0xA6, 0x98, 0xDF, 0xB5, 0xAC, 0x2F, 0xFD, 0x72, 0xDB, + 0xD0, 0x1A, 0xDF, 0xB7, 0xB8, 0xE1, 0xAF, 0xED, 0x6A, 0x26, 0x7E, 0x96, + 0xBA, 0x7C, 0x90, 0x45, 0xF1, 0x2C, 0x7F, 0x99, 0x24, 0xA1, 0x99, 0x47, + 0xB3, 0x91, 0x6C, 0xF7, 0x08, 0x01, 0xF2, 0xE2, 0x85, 0x8E, 0xFC, 0x16, + 0x63, 0x69, 0x20, 0xD8, 0x71, 0x57, 0x4E, 0x69, 0xA4, 0x58, 0xFE, 0xA3, + 0xF4, 0x93, 0x3D, 0x7E, 0x0D, 0x95, 0x74, 0x8F, 0x72, 0x8E, 0xB6, 0x58, + 0x71, 0x8B, 0xCD, 0x58, 0x82, 0x15, 0x4A, 0xEE, 0x7B, 0x54, 0xA4, 0x1D, + 0xC2, 0x5A, 0x59, 0xB5, 0x9C, 0x30, 0xD5, 0x39, 0x2A, 0xF2, 0x60, 0x13, + 0xC5, 0xD1, 0xB0, 0x23, 0x28, 0x60, 0x85, 0xF0, 0xCA, 0x41, 0x79, 0x18, + 0xB8, 0xDB, 0x38, 0xEF, 0x8E, 0x79, 0xDC, 0xB0, 0x60, 0x3A, 0x18, 0x0E, + 0x6C, 0x9E, 0x0E, 0x8B, 0xB0, 0x1E, 0x8A, 0x3E, 0xD7, 0x15, 0x77, 0xC1, + 0xBD, 0x31, 0x4B, 0x27, 0x78, 0xAF, 0x2F, 0xDA, 0x55, 0x60, 0x5C, 0x60, + 0xE6, 0x55, 0x25, 0xF3, 0xAA, 0x55, 0xAB, 0x94, 0x57, 0x48, 0x98, 0x62, + 0x63, 0xE8, 0x14, 0x40, 0x55, 0xCA, 0x39, 0x6A, 0x2A, 0xAB, 0x10, 0xB6, + 0xB4, 0xCC, 0x5C, 0x34, 0x11, 0x41, 0xE8, 0xCE, 0xA1, 0x54, 0x86, 0xAF, + 0x7C, 0x72, 0xE9, 0x93, 0xB3, 0xEE, 0x14, 0x11, 0x63, 0x6F, 0xBC, 0x2A, + 0x2B, 0xA9, 0xC5, 0x5D, 0x74, 0x18, 0x31, 0xF6, 0xCE, 0x5C, 0x3E, 0x16, + 0x9B, 0x87, 0x93, 0x1E, 0xAF, 0xD6, 0xBA, 0x33, 0x6C, 0x24, 0xCF, 0x5C, + 0x7A, 0x32, 0x53, 0x81, 0x28, 0x95, 0x86, 0x77, 0x3B, 0x8F, 0x48, 0x98, + 0x6B, 0x4B, 0xB9, 0xAF, 0xC4, 0xBF, 0xE8, 0x1B, 0x66, 0x28, 0x21, 0x93, + 0x61, 0xD8, 0x09, 0xCC, 0xFB, 0x21, 0xA9, 0x91, 0x48, 0x7C, 0xAC, 0x60, + 0x5D, 0xEC, 0x80, 0x32, 0xEF, 0x84, 0x5D, 0x5D, 0xE9, 0x85, 0x75, 0xB1, + 0xDC, 0x26, 0x23, 0x02, 0xEB, 0x65, 0x1B, 0x88, 0x23, 0x89, 0x3E, 0x81, + 0xD3, 0x96, 0xAC, 0xC5, 0x0F, 0x6D, 0x6F, 0xF3, 0x83, 0xF4, 0x42, 0x39, + 0x2E, 0x0B, 0x44, 0x82, 0xA4, 0x84, 0x20, 0x04, 0x69, 0xC8, 0xF0, 0x4A, + 0x9E, 0x1F, 0x9B, 0x5E, 0x21, 0xC6, 0x68, 0x42, 0xF6, 0xE9, 0x6C, 0x9A, + 0x67, 0x0C, 0x9C, 0x61, 0xAB, 0xD3, 0x88, 0xF0, 0x6A, 0x51, 0xA0, 0xD2, + 0xD8, 0x54, 0x2F, 0x68, 0x96, 0x0F, 0xA7, 0x28, 0xAB, 0x51, 0x33, 0xA3, + 0x6E, 0xEF, 0x0B, 0x6C, 0x13, 0x7A, 0x3B, 0xE4, 0xBA, 0x3B, 0xF0, 0x50, + 0x7E, 0xFB, 0x2A, 0x98, 0xA1, 0xF1, 0x65, 0x1D, 0x39, 0xAF, 0x01, 0x76, + 0x66, 0xCA, 0x59, 0x3E, 0x82, 0x43, 0x0E, 0x88, 0x8C, 0xEE, 0x86, 0x19, + 0x45, 0x6F, 0x9F, 0xB4, 0x7D, 0x84, 0xA5, 0xC3, 0x3B, 0x8B, 0x5E, 0xBE, + 0xE0, 0x6F, 0x75, 0xD8, 0x85, 0xC1, 0x20, 0x73, 0x40, 0x1A, 0x44, 0x9F, + 0x56, 0xC1, 0x6A, 0xA6, 0x4E, 0xD3, 0xAA, 0x62, 0x36, 0x3F, 0x77, 0x06, + 0x1B, 0xFE, 0xDF, 0x72, 0x42, 0x9B, 0x02, 0x3D, 0x37, 0xD0, 0xD7, 0x24, + 0xD0, 0x0A, 0x12, 0x48, 0xDB, 0x0F, 0xEA, 0xD3, 0x49, 0xF1, 0xC0, 0x9B, + 0x07, 0x53, 0x72, 0xC9, 0x80, 0x99, 0x1B, 0x7B, 0x25, 0xD4, 0x79, 0xD8, + 0xF6, 0xE8, 0xDE, 0xF7, 0xE3, 0xFE, 0x50, 0x1A, 0xB6, 0x79, 0x4C, 0x3B, + 0x97, 0x6C, 0xE0, 0xBD, 0x04, 0xC0, 0x06, 0xBA, 0xC1, 0xA9, 0x4F, 0xB6, + 0x40, 0x9F, 0x60, 0xC4, 0x5E, 0x5C, 0x9E, 0xC2, 0x19, 0x6A, 0x24, 0x63, + 0x68, 0xFB, 0x6F, 0xAF, 0x3E, 0x6C, 0x53, 0xB5, 0x13, 0x39, 0xB2, 0xEB, + 0x3B, 0x52, 0xEC, 0x6F, 0x6D, 0xFC, 0x51, 0x1F, 0x9B, 0x30, 0x95, 0x2C, + 0xCC, 0x81, 0x45, 0x44, 0xAF, 0x5E, 0xBD, 0x09, 0xBE, 0xE3, 0xD0, 0x04, + 0xDE, 0x33, 0x4A, 0xFD, 0x66, 0x0F, 0x28, 0x07, 0x19, 0x2E, 0x4B, 0xB3, + 0xC0, 0xCB, 0xA8, 0x57, 0x45, 0xC8, 0x74, 0x0F, 0xD2, 0x0B, 0x5F, 0x39, + 0xB9, 0xD3, 0xFB, 0xDB, 0x55, 0x79, 0xC0, 0xBD, 0x1A, 0x60, 0x32, 0x0A, + 0xD6, 0xA1, 0x00, 0xC6, 0x40, 0x2C, 0x72, 0x79, 0x67, 0x9F, 0x25, 0xFE, + 0xFB, 0x1F, 0xA3, 0xCC, 0x8E, 0xA5, 0xE9, 0xF8, 0xDB, 0x32, 0x22, 0xF8, + 0x3C, 0x75, 0x16, 0xDF, 0xFD, 0x61, 0x6B, 0x15, 0x2F, 0x50, 0x1E, 0xC8, + 0xAD, 0x05, 0x52, 0xAB, 0x32, 0x3D, 0xB5, 0xFA, 0xFD, 0x23, 0x87, 0x60, + 0x53, 0x31, 0x7B, 0x48, 0x3E, 0x00, 0xDF, 0x82, 0x9E, 0x5C, 0x57, 0xBB, + 0xCA, 0x6F, 0x8C, 0xA0, 0x1A, 0x87, 0x56, 0x2E, 0xDF, 0x17, 0x69, 0xDB, + 0xD5, 0x42, 0xA8, 0xF6, 0x28, 0x7E, 0xFF, 0xC3, 0xAC, 0x67, 0x32, 0xC6, + 0x8C, 0x4F, 0x55, 0x73, 0x69, 0x5B, 0x27, 0xB0, 0xBB, 0xCA, 0x58, 0xC8, + 0xE1, 0xFF, 0xA3, 0x5D, 0xB8, 0xF0, 0x11, 0xA0, 0x10, 0xFA, 0x3D, 0x98, + 0xFD, 0x21, 0x83, 0xB8, 0x4A, 0xFC, 0xB5, 0x6C, 0x2D, 0xD1, 0xD3, 0x5B, + 0x9A, 0x53, 0xE4, 0x79, 0xB6, 0xF8, 0x45, 0x65, 0xD2, 0x8E, 0x49, 0xBC, + 0x4B, 0xFB, 0x97, 0x90, 0xE1, 0xDD, 0xF2, 0xDA, 0xA4, 0xCB, 0x7E, 0x33, + 0x62, 0xFB, 0x13, 0x41, 0xCE, 0xE4, 0xC6, 0xE8, 0xEF, 0x20, 0xCA, 0xDA, + 0x36, 0x77, 0x4C, 0x01, 0xD0, 0x7E, 0x9E, 0xFE, 0x2B, 0xF1, 0x1F, 0xB4, + 0x95, 0xDB, 0xDA, 0x4D, 0xAE, 0x90, 0x91, 0x98, 0xEA, 0xAD, 0x8E, 0x71, + 0x6B, 0x93, 0xD5, 0xA0, 0xD0, 0x8E, 0xD1, 0xD0, 0xAF, 0xC7, 0x25, 0xE0, + 0x8E, 0x3C, 0x5B, 0x2F, 0x8E, 0x75, 0x94, 0xB7, 0x8F, 0xF6, 0xE2, 0xFB, + 0xF2, 0x12, 0x2B, 0x64, 0x88, 0x88, 0xB8, 0x12, 0x90, 0x0D, 0xF0, 0x1C, + 0x4F, 0xAD, 0x5E, 0xA0, 0x68, 0x8F, 0xC3, 0x1C, 0xD1, 0xCF, 0xF1, 0x91, + 0xB3, 0xA8, 0xC1, 0xAD, 0x2F, 0x2F, 0x22, 0x18, 0xBE, 0x0E, 0x17, 0x77, + 0xEA, 0x75, 0x2D, 0xFE, 0x8B, 0x02, 0x1F, 0xA1, 0xE5, 0xA0, 0xCC, 0x0F, + 0xB5, 0x6F, 0x74, 0xE8, 0x18, 0xAC, 0xF3, 0xD6, 0xCE, 0x89, 0xE2, 0x99, + 0xB4, 0xA8, 0x4F, 0xE0, 0xFD, 0x13, 0xE0, 0xB7, 0x7C, 0xC4, 0x3B, 0x81, + 0xD2, 0xAD, 0xA8, 0xD9, 0x16, 0x5F, 0xA2, 0x66, 0x80, 0x95, 0x77, 0x05, + 0x93, 0xCC, 0x73, 0x14, 0x21, 0x1A, 0x14, 0x77, 0xE6, 0xAD, 0x20, 0x65, + 0x77, 0xB5, 0xFA, 0x86, 0xC7, 0x54, 0x42, 0xF5, 0xFB, 0x9D, 0x35, 0xCF, + 0xEB, 0xCD, 0xAF, 0x0C, 0x7B, 0x3E, 0x89, 0xA0, 0xD6, 0x41, 0x1B, 0xD3, + 0xAE, 0x1E, 0x7E, 0x49, 0x00, 0x25, 0x0E, 0x2D, 0x20, 0x71, 0xB3, 0x5E, + 0x22, 0x68, 0x00, 0xBB, 0x57, 0xB8, 0xE0, 0xAF, 0x24, 0x64, 0x36, 0x9B, + 0xF0, 0x09, 0xB9, 0x1E, 0x55, 0x63, 0x91, 0x1D, 0x59, 0xDF, 0xA6, 0xAA, + 0x78, 0xC1, 0x43, 0x89, 0xD9, 0x5A, 0x53, 0x7F, 0x20, 0x7D, 0x5B, 0xA2, + 0x02, 0xE5, 0xB9, 0xC5, 0x83, 0x26, 0x03, 0x76, 0x62, 0x95, 0xCF, 0xA9, + 0x11, 0xC8, 0x19, 0x68, 0x4E, 0x73, 0x4A, 0x41, 0xB3, 0x47, 0x2D, 0xCA, + 0x7B, 0x14, 0xA9, 0x4A, 0x1B, 0x51, 0x00, 0x52, 0x9A, 0x53, 0x29, 0x15, + 0xD6, 0x0F, 0x57, 0x3F, 0xBC, 0x9B, 0xC6, 0xE4, 0x2B, 0x60, 0xA4, 0x76, + 0x81, 0xE6, 0x74, 0x00, 0x08, 0xBA, 0x6F, 0xB5, 0x57, 0x1B, 0xE9, 0x1F, + 0xF2, 0x96, 0xEC, 0x6B, 0x2A, 0x0D, 0xD9, 0x15, 0xB6, 0x63, 0x65, 0x21, + 0xE7, 0xB9, 0xF9, 0xB6, 0xFF, 0x34, 0x05, 0x2E, 0xC5, 0x85, 0x56, 0x64, + 0x53, 0xB0, 0x2D, 0x5D, 0xA9, 0x9F, 0x8F, 0xA1, 0x08, 0xBA, 0x47, 0x99, + 0x6E, 0x85, 0x07, 0x6A, 0x4B, 0x7A, 0x70, 0xE9, 0xB5, 0xB3, 0x29, 0x44, + 0xDB, 0x75, 0x09, 0x2E, 0xC4, 0x19, 0x26, 0x23, 0xAD, 0x6E, 0xA6, 0xB0, + 0x49, 0xA7, 0xDF, 0x7D, 0x9C, 0xEE, 0x60, 0xB8, 0x8F, 0xED, 0xB2, 0x66, + 0xEC, 0xAA, 0x8C, 0x71, 0x69, 0x9A, 0x18, 0xFF, 0x56, 0x64, 0x52, 0x6C, + 0xC2, 0xB1, 0x9E, 0xE1, 0x19, 0x36, 0x02, 0xA5, 0x75, 0x09, 0x4C, 0x29, + 0xA0, 0x59, 0x13, 0x40, 0xE4, 0x18, 0x3A, 0x3E, 0x3F, 0x54, 0x98, 0x9A, + 0x5B, 0x42, 0x9D, 0x65, 0x6B, 0x8F, 0xE4, 0xD6, 0x99, 0xF7, 0x3F, 0xD6, + 0xA1, 0xD2, 0x9C, 0x07, 0xEF, 0xE8, 0x30, 0xF5, 0x4D, 0x2D, 0x38, 0xE6, + 0xF0, 0x25, 0x5D, 0xC1, 0x4C, 0xDD, 0x20, 0x86, 0x84, 0x70, 0xEB, 0x26, + 0x63, 0x82, 0xE9, 0xC6, 0x02, 0x1E, 0xCC, 0x5E, 0x09, 0x68, 0x6B, 0x3F, + 0x3E, 0xBA, 0xEF, 0xC9, 0x3C, 0x97, 0x18, 0x14, 0x6B, 0x6A, 0x70, 0xA1, + 0x68, 0x7F, 0x35, 0x84, 0x52, 0xA0, 0xE2, 0x86, 0xB7, 0x9C, 0x53, 0x05, + 0xAA, 0x50, 0x07, 0x37, 0x3E, 0x07, 0x84, 0x1C, 0x7F, 0xDE, 0xAE, 0x5C, + 0x8E, 0x7D, 0x44, 0xEC, 0x57, 0x16, 0xF2, 0xB8, 0xB0, 0x3A, 0xDA, 0x37, + 0xF0, 0x50, 0x0C, 0x0D, 0xF0, 0x1C, 0x1F, 0x04, 0x02, 0x00, 0xB3, 0xFF, + 0xAE, 0x0C, 0xF5, 0x1A, 0x3C, 0xB5, 0x74, 0xB2, 0x25, 0x83, 0x7A, 0x58, + 0xDC, 0x09, 0x21, 0xBD, 0xD1, 0x91, 0x13, 0xF9, 0x7C, 0xA9, 0x2F, 0xF6, + 0x94, 0x32, 0x47, 0x73, 0x22, 0xF5, 0x47, 0x01, 0x3A, 0xE5, 0xE5, 0x81, + 0x37, 0xC2, 0xDA, 0xDC, 0xC8, 0xB5, 0x76, 0x34, 0x9A, 0xF3, 0xDD, 0xA7, + 0xA9, 0x44, 0x61, 0x46, 0x0F, 0xD0, 0x03, 0x0E, 0xEC, 0xC8, 0xC7, 0x3E, + 0xA4, 0x75, 0x1E, 0x41, 0xE2, 0x38, 0xCD, 0x99, 0x3B, 0xEA, 0x0E, 0x2F, + 0x32, 0x80, 0xBB, 0xA1, 0x18, 0x3E, 0xB3, 0x31, 0x4E, 0x54, 0x8B, 0x38, + 0x4F, 0x6D, 0xB9, 0x08, 0x6F, 0x42, 0x0D, 0x03, 0xF6, 0x0A, 0x04, 0xBF, + 0x2C, 0xB8, 0x12, 0x90, 0x24, 0x97, 0x7C, 0x79, 0x56, 0x79, 0xB0, 0x72, + 0xBC, 0xAF, 0x89, 0xAF, 0xDE, 0x9A, 0x77, 0x1F, 0xD9, 0x93, 0x08, 0x10, + 0xB3, 0x8B, 0xAE, 0x12, 0xDC, 0xCF, 0x3F, 0x2E, 0x55, 0x12, 0x72, 0x1F, + 0x2E, 0x6B, 0x71, 0x24, 0x50, 0x1A, 0xDD, 0xE6, 0x9F, 0x84, 0xCD, 0x87, + 0x7A, 0x58, 0x47, 0x18, 0x74, 0x08, 0xDA, 0x17, 0xBC, 0x9F, 0x9A, 0xBC, + 0xE9, 0x4B, 0x7D, 0x8C, 0xEC, 0x7A, 0xEC, 0x3A, 0xDB, 0x85, 0x1D, 0xFA, + 0x63, 0x09, 0x43, 0x66, 0xC4, 0x64, 0xC3, 0xD2, 0xEF, 0x1C, 0x18, 0x47, + 0x32, 0x15, 0xD8, 0x08, 0xDD, 0x43, 0x3B, 0x37, 0x24, 0xC2, 0xBA, 0x16, + 0x12, 0xA1, 0x4D, 0x43, 0x2A, 0x65, 0xC4, 0x51, 0x50, 0x94, 0x00, 0x02, + 0x13, 0x3A, 0xE4, 0xDD, 0x71, 0xDF, 0xF8, 0x9E, 0x10, 0x31, 0x4E, 0x55, + 0x81, 0xAC, 0x77, 0xD6, 0x5F, 0x11, 0x19, 0x9B, 0x04, 0x35, 0x56, 0xF1, + 0xD7, 0xA3, 0xC7, 0x6B, 0x3C, 0x11, 0x18, 0x3B, 0x59, 0x24, 0xA5, 0x09, + 0xF2, 0x8F, 0xE6, 0xED, 0x97, 0xF1, 0xFB, 0xFA, 0x9E, 0xBA, 0xBF, 0x2C, + 0x1E, 0x15, 0x3C, 0x6E, 0x86, 0xE3, 0x45, 0x70, 0xEA, 0xE9, 0x6F, 0xB1, + 0x86, 0x0E, 0x5E, 0x0A, 0x5A, 0x3E, 0x2A, 0xB3, 0x77, 0x1F, 0xE7, 0x1C, + 0x4E, 0x3D, 0x06, 0xFA, 0x29, 0x65, 0xDC, 0xB9, 0x99, 0xE7, 0x1D, 0x0F, + 0x80, 0x3E, 0x89, 0xD6, 0x52, 0x66, 0xC8, 0x25, 0x2E, 0x4C, 0xC9, 0x78, + 0x9C, 0x10, 0xB3, 0x6A, 0xC6, 0x15, 0x0E, 0xBA, 0x94, 0xE2, 0xEA, 0x78, + 0xA6, 0xFC, 0x3C, 0x53, 0x1E, 0x0A, 0x2D, 0xF4, 0xF2, 0xF7, 0x4E, 0xA7, + 0x36, 0x1D, 0x2B, 0x3D, 0x19, 0x39, 0x26, 0x0F, 0x19, 0xC2, 0x79, 0x60, + 0x52, 0x23, 0xA7, 0x08, 0xF7, 0x13, 0x12, 0xB6, 0xEB, 0xAD, 0xFE, 0x6E, + 0xEA, 0xC3, 0x1F, 0x66, 0xE3, 0xBC, 0x45, 0x95, 0xA6, 0x7B, 0xC8, 0x83, + 0xB1, 0x7F, 0x37, 0xD1, 0x01, 0x8C, 0xFF, 0x28, 0xC3, 0x32, 0xDD, 0xEF, + 0xBE, 0x6C, 0x5A, 0xA5, 0x65, 0x58, 0x21, 0x85, 0x68, 0xAB, 0x97, 0x02, + 0xEE, 0xCE, 0xA5, 0x0F, 0xDB, 0x2F, 0x95, 0x3B, 0x2A, 0xEF, 0x7D, 0xAD, + 0x5B, 0x6E, 0x2F, 0x84, 0x15, 0x21, 0xB6, 0x28, 0x29, 0x07, 0x61, 0x70, + 0xEC, 0xDD, 0x47, 0x75, 0x61, 0x9F, 0x15, 0x10, 0x13, 0xCC, 0xA8, 0x30, + 0xEB, 0x61, 0xBD, 0x96, 0x03, 0x34, 0xFE, 0x1E, 0xAA, 0x03, 0x63, 0xCF, + 0xB5, 0x73, 0x5C, 0x90, 0x4C, 0x70, 0xA2, 0x39, 0xD5, 0x9E, 0x9E, 0x0B, + 0xCB, 0xAA, 0xDE, 0x14, 0xEE, 0xCC, 0x86, 0xBC, 0x60, 0x62, 0x2C, 0xA7, + 0x9C, 0xAB, 0x5C, 0xAB, 0xB2, 0xF3, 0x84, 0x6E, 0x64, 0x8B, 0x1E, 0xAF, + 0x19, 0xBD, 0xF0, 0xCA, 0xA0, 0x23, 0x69, 0xB9, 0x65, 0x5A, 0xBB, 0x50, + 0x40, 0x68, 0x5A, 0x32, 0x3C, 0x2A, 0xB4, 0xB3, 0x31, 0x9E, 0xE9, 0xD5, + 0xC0, 0x21, 0xB8, 0xF7, 0x9B, 0x54, 0x0B, 0x19, 0x87, 0x5F, 0xA0, 0x99, + 0x95, 0xF7, 0x99, 0x7E, 0x62, 0x3D, 0x7D, 0xA8, 0xF8, 0x37, 0x88, 0x9A, + 0x97, 0xE3, 0x2D, 0x77, 0x11, 0xED, 0x93, 0x5F, 0x16, 0x68, 0x12, 0x81, + 0x0E, 0x35, 0x88, 0x29, 0xC7, 0xE6, 0x1F, 0xD6, 0x96, 0xDE, 0xDF, 0xA1, + 0x78, 0x58, 0xBA, 0x99, 0x57, 0xF5, 0x84, 0xA5, 0x1B, 0x22, 0x72, 0x63, + 0x9B, 0x83, 0xC3, 0xFF, 0x1A, 0xC2, 0x46, 0x96, 0xCD, 0xB3, 0x0A, 0xEB, + 0x53, 0x2E, 0x30, 0x54, 0x8F, 0xD9, 0x48, 0xE4, 0x6D, 0xBC, 0x31, 0x28, + 0x58, 0xEB, 0xF2, 0xEF, 0x34, 0xC6, 0xFF, 0xEA, 0xFE, 0x28, 0xED, 0x61, + 0xEE, 0x7C, 0x3C, 0x73, 0x5D, 0x4A, 0x14, 0xD9, 0xE8, 0x64, 0xB7, 0xE3, + 0x42, 0x10, 0x5D, 0x14, 0x20, 0x3E, 0x13, 0xE0, 0x45, 0xEE, 0xE2, 0xB6, + 0xA3, 0xAA, 0xAB, 0xEA, 0xDB, 0x6C, 0x4F, 0x15, 0xFA, 0xCB, 0x4F, 0xD0, + 0xC7, 0x42, 0xF4, 0x42, 0xEF, 0x6A, 0xBB, 0xB5, 0x65, 0x4F, 0x3B, 0x1D, + 0x41, 0xCD, 0x21, 0x05, 0xD8, 0x1E, 0x79, 0x9E, 0x86, 0x85, 0x4D, 0xC7, + 0xE4, 0x4B, 0x47, 0x6A, 0x3D, 0x81, 0x62, 0x50, 0xCF, 0x62, 0xA1, 0xF2, + 0x5B, 0x8D, 0x26, 0x46, 0xFC, 0x88, 0x83, 0xA0, 0xC1, 0xC7, 0xB6, 0xA3, + 0x7F, 0x15, 0x24, 0xC3, 0x69, 0xCB, 0x74, 0x92, 0x47, 0x84, 0x8A, 0x0B, + 0x56, 0x92, 0xB2, 0x85, 0x09, 0x5B, 0xBF, 0x00, 0xAD, 0x19, 0x48, 0x9D, + 0x14, 0x62, 0xB1, 0x74, 0x23, 0x82, 0x0D, 0x00, 0x58, 0x42, 0x8D, 0x2A, + 0x0C, 0x55, 0xF5, 0xEA, 0x1D, 0xAD, 0xF4, 0x3E, 0x23, 0x3F, 0x70, 0x61, + 0x33, 0x72, 0xF0, 0x92, 0x8D, 0x93, 0x7E, 0x41, 0xD6, 0x5F, 0xEC, 0xF1, + 0x6C, 0x22, 0x3B, 0xDB, 0x7C, 0xDE, 0x37, 0x59, 0xCB, 0xEE, 0x74, 0x60, + 0x40, 0x85, 0xF2, 0xA7, 0xCE, 0x77, 0x32, 0x6E, 0xA6, 0x07, 0x80, 0x84, + 0x19, 0xF8, 0x50, 0x9E, 0xE8, 0xEF, 0xD8, 0x55, 0x61, 0xD9, 0x97, 0x35, + 0xA9, 0x69, 0xA7, 0xAA, 0xC5, 0x0C, 0x06, 0xC2, 0x5A, 0x04, 0xAB, 0xFC, + 0x80, 0x0B, 0xCA, 0xDC, 0x9E, 0x44, 0x7A, 0x2E, 0xC3, 0x45, 0x34, 0x84, + 0xFD, 0xD5, 0x67, 0x05, 0x0E, 0x1E, 0x9E, 0xC9, 0xDB, 0x73, 0xDB, 0xD3, + 0x10, 0x55, 0x88, 0xCD, 0x67, 0x5F, 0xDA, 0x79, 0xE3, 0x67, 0x43, 0x40, + 0xC5, 0xC4, 0x34, 0x65, 0x71, 0x3E, 0x38, 0xD8, 0x3D, 0x28, 0xF8, 0x9E, + 0xF1, 0x6D, 0xFF, 0x20, 0x15, 0x3E, 0x21, 0xE7, 0x8F, 0xB0, 0x3D, 0x4A, + 0xE6, 0xE3, 0x9F, 0x2B, 0xDB, 0x83, 0xAD, 0xF7, 0xE9, 0x3D, 0x5A, 0x68, + 0x94, 0x81, 0x40, 0xF7, 0xF6, 0x4C, 0x26, 0x1C, 0x94, 0x69, 0x29, 0x34, + 0x41, 0x15, 0x20, 0xF7, 0x76, 0x02, 0xD4, 0xF7, 0xBC, 0xF4, 0x6B, 0x2E, + 0xD4, 0xA1, 0x00, 0x68, 0xD4, 0x08, 0x24, 0x71, 0x33, 0x20, 0xF4, 0x6A, + 0x43, 0xB7, 0xD4, 0xB7, 0x50, 0x00, 0x61, 0xAF, 0x1E, 0x39, 0xF6, 0x2E, + 0x97, 0x24, 0x45, 0x46, +}; + +alignas(16) const unsigned char kRandenRoundKeys[kKeyBytes] = { + 0x44, 0x73, 0x70, 0x03, 0x2E, 0x8A, 0x19, 0x13, 0xD3, 0x08, 0xA3, 0x85, + 0x88, 0x6A, 0x3F, 0x24, 0x89, 0x6C, 0x4E, 0xEC, 0x98, 0xFA, 0x2E, 0x08, + 0xD0, 0x31, 0x9F, 0x29, 0x22, 0x38, 0x09, 0xA4, 0x6C, 0x0C, 0xE9, 0x34, + 0xCF, 0x66, 0x54, 0xBE, 0x77, 0x13, 0xD0, 0x38, 0xE6, 0x21, 0x28, 0x45, + 0x17, 0x09, 0x47, 0xB5, 0xB5, 0xD5, 0x84, 0x3F, 0xDD, 0x50, 0x7C, 0xC9, + 0xB7, 0x29, 0xAC, 0xC0, 0xAC, 0xB5, 0xDF, 0x98, 0xA6, 0x0B, 0x31, 0xD1, + 0x1B, 0xFB, 0x79, 0x89, 0xD9, 0xD5, 0x16, 0x92, 0x96, 0x7E, 0x26, 0x6A, + 0xED, 0xAF, 0xE1, 0xB8, 0xB7, 0xDF, 0x1A, 0xD0, 0xDB, 0x72, 0xFD, 0x2F, + 0xF7, 0x6C, 0x91, 0xB3, 0x47, 0x99, 0xA1, 0x24, 0x99, 0x7F, 0x2C, 0xF1, + 0x45, 0x90, 0x7C, 0xBA, 0x69, 0x4E, 0x57, 0x71, 0xD8, 0x20, 0x69, 0x63, + 0x16, 0xFC, 0x8E, 0x85, 0xE2, 0xF2, 0x01, 0x08, 0x58, 0xB6, 0x8E, 0x72, + 0x8F, 0x74, 0x95, 0x0D, 0x7E, 0x3D, 0x93, 0xF4, 0xA3, 0xFE, 0x58, 0xA4, + 0xB5, 0x59, 0x5A, 0xC2, 0x1D, 0xA4, 0x54, 0x7B, 0xEE, 0x4A, 0x15, 0x82, + 0x58, 0xCD, 0x8B, 0x71, 0xF0, 0x85, 0x60, 0x28, 0x23, 0xB0, 0xD1, 0xC5, + 0x13, 0x60, 0xF2, 0x2A, 0x39, 0xD5, 0x30, 0x9C, 0x0E, 0x18, 0x3A, 0x60, + 0xB0, 0xDC, 0x79, 0x8E, 0xEF, 0x38, 0xDB, 0xB8, 0x18, 0x79, 0x41, 0xCA, + 0x27, 0x4B, 0x31, 0xBD, 0xC1, 0x77, 0x15, 0xD7, 0x3E, 0x8A, 0x1E, 0xB0, + 0x8B, 0x0E, 0x9E, 0x6C, 0x94, 0xAB, 0x55, 0xAA, 0xF3, 0x25, 0x55, 0xE6, + 0x60, 0x5C, 0x60, 0x55, 0xDA, 0x2F, 0xAF, 0x78, 0xB6, 0x10, 0xAB, 0x2A, + 0x6A, 0x39, 0xCA, 0x55, 0x40, 0x14, 0xE8, 0x63, 0x62, 0x98, 0x48, 0x57, + 0x93, 0xE9, 0x72, 0x7C, 0xAF, 0x86, 0x54, 0xA1, 0xCE, 0xE8, 0x41, 0x11, + 0x34, 0x5C, 0xCC, 0xB4, 0xF6, 0x31, 0x18, 0x74, 0x5D, 0xC5, 0xA9, 0x2B, + 0x2A, 0xBC, 0x6F, 0x63, 0x11, 0x14, 0xEE, 0xB3, 0x5C, 0xCF, 0x24, 0x6C, + 0x33, 0xBA, 0xD6, 0xAF, 0x1E, 0x93, 0x87, 0x9B, 0x16, 0x3E, 0x5C, 0xCE, + 0xAF, 0xB9, 0x4B, 0x6B, 0x98, 0x48, 0x8F, 0x3B, 0x77, 0x86, 0x95, 0x28, + 0x81, 0x53, 0x32, 0x7A, 0x91, 0xA9, 0x21, 0xFB, 0xCC, 0x09, 0xD8, 0x61, + 0x93, 0x21, 0x28, 0x66, 0x1B, 0xE8, 0xBF, 0xC4, 0xB1, 0x75, 0x85, 0xE9, + 0x5D, 0x5D, 0x84, 0xEF, 0x32, 0x80, 0xEC, 0x5D, 0x60, 0xAC, 0x7C, 0x48, + 0xC5, 0xAC, 0x96, 0xD3, 0x81, 0x3E, 0x89, 0x23, 0x88, 0x1B, 0x65, 0xEB, + 0x02, 0x23, 0x26, 0xDC, 0x04, 0x20, 0x84, 0xA4, 0x82, 0x44, 0x0B, 0x2E, + 0x39, 0x42, 0xF4, 0x83, 0xF3, 0x6F, 0x6D, 0x0F, 0x9A, 0x6C, 0xE9, 0xF6, + 0x42, 0x68, 0xC6, 0x21, 0x5E, 0x9B, 0x1F, 0x9E, 0x4A, 0xF0, 0xC8, 0x69, + 0x68, 0x2F, 0x54, 0xD8, 0xD2, 0xA0, 0x51, 0x6A, 0xF0, 0x88, 0xD3, 0xAB, + 0x61, 0x9C, 0x0C, 0x67, 0xE4, 0x3B, 0x7A, 0x13, 0x6C, 0x0B, 0xEF, 0x6E, + 0xA3, 0x33, 0x51, 0xAB, 0x28, 0xA7, 0x0F, 0x96, 0x76, 0x01, 0xAF, 0x39, + 0x1D, 0x65, 0xF1, 0xA1, 0x98, 0x2A, 0xFB, 0x7E, 0x50, 0xF0, 0x3B, 0xBA, + 0xB4, 0x9F, 0x6F, 0x45, 0x19, 0x86, 0xEE, 0x8C, 0x88, 0x0E, 0x43, 0x82, + 0x3E, 0x59, 0xCA, 0x66, 0x73, 0x20, 0xC1, 0x85, 0xD8, 0x75, 0x6F, 0xE0, + 0xBE, 0x5E, 0x8B, 0x3B, 0xC3, 0xA5, 0x84, 0x7D, 0x06, 0x77, 0x3F, 0x36, + 0x62, 0xAA, 0xD3, 0x4E, 0xA6, 0x6A, 0xC1, 0x56, 0x9F, 0x44, 0x1A, 0x40, + 0x48, 0x12, 0x0A, 0xD0, 0x24, 0xD7, 0xD0, 0x37, 0x3D, 0x02, 0x9B, 0x42, + 0x72, 0xDF, 0xFE, 0x1B, 0x7B, 0x1B, 0x99, 0x80, 0xC9, 0x72, 0x53, 0x07, + 0x9B, 0xC0, 0xF1, 0x49, 0xD3, 0xEA, 0x0F, 0xDB, 0x3B, 0x4C, 0x79, 0xB6, + 0x1A, 0x50, 0xFE, 0xE3, 0xF7, 0xDE, 0xE8, 0xF6, 0xD8, 0x79, 0xD4, 0x25, + 0xC4, 0x60, 0x9F, 0x40, 0xB6, 0x4F, 0xA9, 0xC1, 0xBA, 0x06, 0xC0, 0x04, + 0xBD, 0xE0, 0x6C, 0x97, 0xB5, 0x53, 0x6C, 0x3E, 0xAF, 0x6F, 0xFB, 0x68, + 0x63, 0x24, 0x6A, 0x19, 0xC2, 0x9E, 0x5C, 0x5E, 0x2C, 0x95, 0x30, 0x9B, + 0x1F, 0x51, 0xFC, 0x6D, 0x6F, 0xEC, 0x52, 0x3B, 0xEB, 0xB2, 0x39, 0x13, + 0xFD, 0x4A, 0x33, 0xDE, 0x04, 0xD0, 0xE3, 0xBE, 0x09, 0xBD, 0x5E, 0xAF, + 0x44, 0x45, 0x81, 0xCC, 0x0F, 0x74, 0xC8, 0x45, 0x57, 0xA8, 0xCB, 0xC0, + 0xB3, 0x4B, 0x2E, 0x19, 0x07, 0x28, 0x0F, 0x66, 0x0A, 0x32, 0x60, 0x1A, + 0xBD, 0xC0, 0x79, 0x55, 0xDB, 0xFB, 0xD3, 0xB9, 0x39, 0x5F, 0x0B, 0xD2, + 0xCC, 0xA3, 0x1F, 0xFB, 0xFE, 0x25, 0x9F, 0x67, 0x79, 0x72, 0x2C, 0x40, + 0xC6, 0x00, 0xA1, 0xD6, 0x15, 0x6B, 0x61, 0xFD, 0xDF, 0x16, 0x75, 0x3C, + 0xF8, 0x22, 0x32, 0xDB, 0xF8, 0xE9, 0xA5, 0x8E, 0x60, 0x87, 0x23, 0xFD, + 0xFA, 0xB5, 0x3D, 0x32, 0xAB, 0x52, 0x05, 0xAD, 0xC8, 0x1E, 0x50, 0x2F, + 0xA0, 0x8C, 0x6F, 0xCA, 0xBB, 0x57, 0x5C, 0x9E, 0x82, 0xDF, 0x00, 0x3E, + 0x48, 0x7B, 0x31, 0x53, 0xC3, 0xFF, 0x7E, 0x28, 0xF6, 0xA8, 0x42, 0xD5, + 0xDB, 0x69, 0x17, 0xDF, 0x2E, 0x56, 0x87, 0x1A, 0xC8, 0x58, 0xCA, 0xBB, + 0xB0, 0x27, 0x5B, 0x69, 0x73, 0x55, 0x4F, 0x8C, 0xC6, 0x32, 0x67, 0xAC, + 0xB8, 0x83, 0x21, 0xFD, 0x98, 0x3D, 0xFA, 0x10, 0xA0, 0x11, 0xF0, 0xB8, + 0x5D, 0xA3, 0xFF, 0xE1, 0x65, 0x45, 0xF8, 0xB6, 0x79, 0xE4, 0x53, 0x9A, + 0x5B, 0xD3, 0xD1, 0x2D, 0x6C, 0xB5, 0xFC, 0x4A, 0x33, 0x7E, 0xCB, 0xA4, + 0xDA, 0xF2, 0xDD, 0xE1, 0x90, 0x97, 0xFB, 0x4B, 0xBC, 0x49, 0x8E, 0xD2, + 0x01, 0x4C, 0x77, 0x36, 0xDA, 0xCA, 0x20, 0xEF, 0xE8, 0xC6, 0xE4, 0xCE, + 0x41, 0x13, 0xFB, 0x62, 0x98, 0x91, 0x90, 0xAE, 0x4D, 0xDA, 0xDB, 0x95, + 0xB4, 0x1F, 0xF1, 0x2B, 0xFE, 0x9E, 0x7E, 0xD0, 0xE0, 0x25, 0xC7, 0xAF, + 0xD0, 0xD1, 0x8E, 0xD0, 0xA0, 0xD5, 0x93, 0x6B, 0x71, 0x8E, 0xAD, 0xEA, + 0x64, 0x2B, 0x12, 0xF2, 0xFB, 0xE2, 0xF6, 0x8F, 0xB7, 0x94, 0x75, 0x8E, + 0x2F, 0x5B, 0x3C, 0x8E, 0x1C, 0xC3, 0x8F, 0x68, 0xA0, 0x5E, 0xAD, 0x4F, + 0x1C, 0xF0, 0x0D, 0x90, 0x12, 0xB8, 0x88, 0x88, 0x77, 0x17, 0x0E, 0xBE, + 0x18, 0x22, 0x2F, 0x2F, 0xAD, 0xC1, 0xA8, 0xB3, 0x91, 0xF1, 0xCF, 0xD1, + 0xE8, 0x74, 0x6F, 0xB5, 0x0F, 0xCC, 0xA0, 0xE5, 0xA1, 0x1F, 0x02, 0x8B, + 0xFE, 0x2D, 0x75, 0xEA, 0xB7, 0xE0, 0x13, 0xFD, 0xE0, 0x4F, 0xA8, 0xB4, + 0x99, 0xE2, 0x89, 0xCE, 0xD6, 0xF3, 0xAC, 0x18, 0x05, 0x77, 0x95, 0x80, + 0x66, 0xA2, 0x5F, 0x16, 0xD9, 0xA8, 0xAD, 0xD2, 0x81, 0x3B, 0xC4, 0x7C, + 0x86, 0xFA, 0xB5, 0x77, 0x65, 0x20, 0xAD, 0xE6, 0x77, 0x14, 0x1A, 0x21, + 0x14, 0x73, 0xCC, 0x93, 0xA0, 0x89, 0x3E, 0x7B, 0x0C, 0xAF, 0xCD, 0xEB, + 0xCF, 0x35, 0x9D, 0xFB, 0xF5, 0x42, 0x54, 0xC7, 0x5E, 0xB3, 0x71, 0x20, + 0x2D, 0x0E, 0x25, 0x00, 0x49, 0x7E, 0x1E, 0xAE, 0xD3, 0x1B, 0x41, 0xD6, + 0x1E, 0xB9, 0x09, 0xF0, 0x9B, 0x36, 0x64, 0x24, 0xAF, 0xE0, 0xB8, 0x57, + 0xBB, 0x00, 0x68, 0x22, 0x7F, 0x53, 0x5A, 0xD9, 0x89, 0x43, 0xC1, 0x78, + 0xAA, 0xA6, 0xDF, 0x59, 0x1D, 0x91, 0x63, 0x55, 0xA9, 0xCF, 0x95, 0x62, + 0x76, 0x03, 0x26, 0x83, 0xC5, 0xB9, 0xE5, 0x02, 0xA2, 0x5B, 0x7D, 0x20, + 0x4A, 0xA9, 0x14, 0x7B, 0xCA, 0x2D, 0x47, 0xB3, 0x41, 0x4A, 0x73, 0x4E, + 0x68, 0x19, 0xC8, 0x11, 0xE4, 0xC6, 0x9B, 0xBC, 0x3F, 0x57, 0x0F, 0xD6, + 0x15, 0x29, 0x53, 0x9A, 0x52, 0x00, 0x51, 0x1B, 0x1F, 0xE9, 0x1B, 0x57, + 0xB5, 0x6F, 0xBA, 0x08, 0x00, 0x74, 0xE6, 0x81, 0x76, 0xA4, 0x60, 0x2B, + 0xB6, 0xF9, 0xB9, 0xE7, 0x21, 0x65, 0x63, 0xB6, 0x15, 0xD9, 0x0D, 0x2A, + 0x6B, 0xEC, 0x96, 0xF2, 0xA1, 0x8F, 0x9F, 0xA9, 0x5D, 0x2D, 0xB0, 0x53, + 0x64, 0x56, 0x85, 0xC5, 0x2E, 0x05, 0x34, 0xFF, 0x44, 0x29, 0xB3, 0xB5, + 0xE9, 0x70, 0x7A, 0x4B, 0x6A, 0x07, 0x85, 0x6E, 0x99, 0x47, 0xBA, 0x08, + 0x7D, 0xDF, 0xA7, 0x49, 0xB0, 0xA6, 0x6E, 0xAD, 0x23, 0x26, 0x19, 0xC4, + 0x2E, 0x09, 0x75, 0xDB, 0xFF, 0x18, 0x9A, 0x69, 0x71, 0x8C, 0xAA, 0xEC, + 0x66, 0xB2, 0xED, 0x8F, 0xB8, 0x60, 0xEE, 0x9C, 0x29, 0x4C, 0x09, 0x75, + 0xA5, 0x02, 0x36, 0x19, 0xE1, 0x9E, 0xB1, 0xC2, 0x6C, 0x52, 0x64, 0x56, + 0x65, 0x9D, 0x42, 0x5B, 0x9A, 0x98, 0x54, 0x3F, 0x3E, 0x3A, 0x18, 0xE4, + 0x40, 0x13, 0x59, 0xA0, 0xF5, 0x30, 0xE8, 0xEF, 0x07, 0x9C, 0xD2, 0xA1, + 0xD6, 0x3F, 0xF7, 0x99, 0xD6, 0xE4, 0x8F, 0x6B, 0x26, 0xEB, 0x70, 0x84, + 0x86, 0x20, 0xDD, 0x4C, 0xC1, 0x5D, 0x25, 0xF0, 0xE6, 0x38, 0x2D, 0x4D, + 0xC9, 0xEF, 0xBA, 0x3E, 0x3F, 0x6B, 0x68, 0x09, 0x5E, 0xCC, 0x1E, 0x02, + 0xC6, 0xE9, 0x82, 0x63, 0x86, 0xE2, 0xA0, 0x52, 0x84, 0x35, 0x7F, 0x68, + 0xA1, 0x70, 0x6A, 0x6B, 0x14, 0x18, 0x97, 0x3C, 0x5C, 0xAE, 0xDE, 0x7F, + 0x1C, 0x84, 0x07, 0x3E, 0x37, 0x07, 0x50, 0xAA, 0x05, 0x53, 0x9C, 0xB7, + 0x0D, 0x0C, 0x50, 0xF0, 0x37, 0xDA, 0x3A, 0xB0, 0xB8, 0xF2, 0x16, 0x57, + 0xEC, 0x44, 0x7D, 0x8E, 0xB2, 0x74, 0xB5, 0x3C, 0x1A, 0xF5, 0x0C, 0xAE, + 0xFF, 0xB3, 0x00, 0x02, 0x04, 0x1F, 0x1C, 0xF0, 0xF6, 0x2F, 0xA9, 0x7C, + 0xF9, 0x13, 0x91, 0xD1, 0xBD, 0x21, 0x09, 0xDC, 0x58, 0x7A, 0x83, 0x25, + 0xDC, 0xDA, 0xC2, 0x37, 0x81, 0xE5, 0xE5, 0x3A, 0x01, 0x47, 0xF5, 0x22, + 0x73, 0x47, 0x32, 0x94, 0x0E, 0x03, 0xD0, 0x0F, 0x46, 0x61, 0x44, 0xA9, + 0xA7, 0xDD, 0xF3, 0x9A, 0x34, 0x76, 0xB5, 0xC8, 0x2F, 0x0E, 0xEA, 0x3B, + 0x99, 0xCD, 0x38, 0xE2, 0x41, 0x1E, 0x75, 0xA4, 0x3E, 0xC7, 0xC8, 0xEC, + 0x08, 0xB9, 0x6D, 0x4F, 0x38, 0x8B, 0x54, 0x4E, 0x31, 0xB3, 0x3E, 0x18, + 0xA1, 0xBB, 0x80, 0x32, 0x79, 0x7C, 0x97, 0x24, 0x90, 0x12, 0xB8, 0x2C, + 0xBF, 0x04, 0x0A, 0xF6, 0x03, 0x0D, 0x42, 0x6F, 0x10, 0x08, 0x93, 0xD9, + 0x1F, 0x77, 0x9A, 0xDE, 0xAF, 0x89, 0xAF, 0xBC, 0x72, 0xB0, 0x79, 0x56, + 0x24, 0x71, 0x6B, 0x2E, 0x1F, 0x72, 0x12, 0x55, 0x2E, 0x3F, 0xCF, 0xDC, + 0x12, 0xAE, 0x8B, 0xB3, 0x17, 0xDA, 0x08, 0x74, 0x18, 0x47, 0x58, 0x7A, + 0x87, 0xCD, 0x84, 0x9F, 0xE6, 0xDD, 0x1A, 0x50, 0xFA, 0x1D, 0x85, 0xDB, + 0x3A, 0xEC, 0x7A, 0xEC, 0x8C, 0x7D, 0x4B, 0xE9, 0xBC, 0x9A, 0x9F, 0xBC, + 0x08, 0xD8, 0x15, 0x32, 0x47, 0x18, 0x1C, 0xEF, 0xD2, 0xC3, 0x64, 0xC4, + 0x66, 0x43, 0x09, 0x63, 0x51, 0xC4, 0x65, 0x2A, 0x43, 0x4D, 0xA1, 0x12, + 0x16, 0xBA, 0xC2, 0x24, 0x37, 0x3B, 0x43, 0xDD, 0x55, 0x4E, 0x31, 0x10, + 0x9E, 0xF8, 0xDF, 0x71, 0xDD, 0xE4, 0x3A, 0x13, 0x02, 0x00, 0x94, 0x50, + 0x6B, 0xC7, 0xA3, 0xD7, 0xF1, 0x56, 0x35, 0x04, 0x9B, 0x19, 0x11, 0x5F, + 0xD6, 0x77, 0xAC, 0x81, 0xFA, 0xFB, 0xF1, 0x97, 0xED, 0xE6, 0x8F, 0xF2, + 0x09, 0xA5, 0x24, 0x59, 0x3B, 0x18, 0x11, 0x3C, 0xB1, 0x6F, 0xE9, 0xEA, + 0x70, 0x45, 0xE3, 0x86, 0x6E, 0x3C, 0x15, 0x1E, 0x2C, 0xBF, 0xBA, 0x9E, + 0xFA, 0x06, 0x3D, 0x4E, 0x1C, 0xE7, 0x1F, 0x77, 0xB3, 0x2A, 0x3E, 0x5A, + 0x0A, 0x5E, 0x0E, 0x86, 0x25, 0xC8, 0x66, 0x52, 0xD6, 0x89, 0x3E, 0x80, + 0x0F, 0x1D, 0xE7, 0x99, 0xB9, 0xDC, 0x65, 0x29, 0x78, 0xEA, 0xE2, 0x94, + 0xBA, 0x0E, 0x15, 0xC6, 0x6A, 0xB3, 0x10, 0x9C, 0x78, 0xC9, 0x4C, 0x2E, + 0x3D, 0x2B, 0x1D, 0x36, 0xA7, 0x4E, 0xF7, 0xF2, 0xF4, 0x2D, 0x0A, 0x1E, + 0x53, 0x3C, 0xFC, 0xA6, 0xB6, 0x12, 0x13, 0xF7, 0x08, 0xA7, 0x23, 0x52, + 0x60, 0x79, 0xC2, 0x19, 0x0F, 0x26, 0x39, 0x19, 0x83, 0xC8, 0x7B, 0xA6, + 0x95, 0x45, 0xBC, 0xE3, 0x66, 0x1F, 0xC3, 0xEA, 0x6E, 0xFE, 0xAD, 0xEB, + 0xA5, 0x5A, 0x6C, 0xBE, 0xEF, 0xDD, 0x32, 0xC3, 0x28, 0xFF, 0x8C, 0x01, + 0xD1, 0x37, 0x7F, 0xB1, 0x3B, 0x95, 0x2F, 0xDB, 0x0F, 0xA5, 0xCE, 0xEE, + 0x02, 0x97, 0xAB, 0x68, 0x85, 0x21, 0x58, 0x65, 0x70, 0x61, 0x07, 0x29, + 0x28, 0xB6, 0x21, 0x15, 0x84, 0x2F, 0x6E, 0x5B, 0xAD, 0x7D, 0xEF, 0x2A, + 0x96, 0xBD, 0x61, 0xEB, 0x30, 0xA8, 0xCC, 0x13, 0x10, 0x15, 0x9F, 0x61, + 0x75, 0x47, 0xDD, 0xEC, 0x39, 0xA2, 0x70, 0x4C, 0x90, 0x5C, 0x73, 0xB5, + 0xCF, 0x63, 0x03, 0xAA, 0x1E, 0xFE, 0x34, 0x03, 0xA7, 0x2C, 0x62, 0x60, + 0xBC, 0x86, 0xCC, 0xEE, 0x14, 0xDE, 0xAA, 0xCB, 0x0B, 0x9E, 0x9E, 0xD5, + 0xCA, 0xF0, 0xBD, 0x19, 0xAF, 0x1E, 0x8B, 0x64, 0x6E, 0x84, 0xF3, 0xB2, + 0xAB, 0x5C, 0xAB, 0x9C, 0xB3, 0xB4, 0x2A, 0x3C, 0x32, 0x5A, 0x68, 0x40, + 0x50, 0xBB, 0x5A, 0x65, 0xB9, 0x69, 0x23, 0xA0, 0x99, 0xA0, 0x5F, 0x87, + 0x19, 0x0B, 0x54, 0x9B, 0xF7, 0xB8, 0x21, 0xC0, 0xD5, 0xE9, 0x9E, 0x31, + 0x77, 0x2D, 0xE3, 0x97, 0x9A, 0x88, 0x37, 0xF8, 0xA8, 0x7D, 0x3D, 0x62, + 0x7E, 0x99, 0xF7, 0x95, 0xD6, 0x1F, 0xE6, 0xC7, 0x29, 0x88, 0x35, 0x0E, + 0x81, 0x12, 0x68, 0x16, 0x5F, 0x93, 0xED, 0x11, 0x63, 0x72, 0x22, 0x1B, + 0xA5, 0x84, 0xF5, 0x57, 0x99, 0xBA, 0x58, 0x78, 0xA1, 0xDF, 0xDE, 0x96, + 0x54, 0x30, 0x2E, 0x53, 0xEB, 0x0A, 0xB3, 0xCD, 0x96, 0x46, 0xC2, 0x1A, + 0xFF, 0xC3, 0x83, 0x9B, 0xEA, 0xFF, 0xC6, 0x34, 0xEF, 0xF2, 0xEB, 0x58, + 0x28, 0x31, 0xBC, 0x6D, 0xE4, 0x48, 0xD9, 0x8F, 0xE3, 0xB7, 0x64, 0xE8, + 0xD9, 0x14, 0x4A, 0x5D, 0x73, 0x3C, 0x7C, 0xEE, 0x61, 0xED, 0x28, 0xFE, + 0xEA, 0xAB, 0xAA, 0xA3, 0xB6, 0xE2, 0xEE, 0x45, 0xE0, 0x13, 0x3E, 0x20, + 0x14, 0x5D, 0x10, 0x42, 0xB5, 0xBB, 0x6A, 0xEF, 0x42, 0xF4, 0x42, 0xC7, + 0xD0, 0x4F, 0xCB, 0xFA, 0x15, 0x4F, 0x6C, 0xDB, 0xC7, 0x4D, 0x85, 0x86, + 0x9E, 0x79, 0x1E, 0xD8, 0x05, 0x21, 0xCD, 0x41, 0x1D, 0x3B, 0x4F, 0x65, + 0x46, 0x26, 0x8D, 0x5B, 0xF2, 0xA1, 0x62, 0xCF, 0x50, 0x62, 0x81, 0x3D, + 0x6A, 0x47, 0x4B, 0xE4, 0x92, 0x74, 0xCB, 0x69, 0xC3, 0x24, 0x15, 0x7F, + 0xA3, 0xB6, 0xC7, 0xC1, 0xA0, 0x83, 0x88, 0xFC, 0x9D, 0x48, 0x19, 0xAD, + 0x00, 0xBF, 0x5B, 0x09, 0x85, 0xB2, 0x92, 0x56, 0x0B, 0x8A, 0x84, 0x47, + 0xEA, 0xF5, 0x55, 0x0C, 0x2A, 0x8D, 0x42, 0x58, 0x00, 0x0D, 0x82, 0x23, + 0x74, 0xB1, 0x62, 0x14, 0x41, 0x7E, 0x93, 0x8D, 0x92, 0xF0, 0x72, 0x33, + 0x61, 0x70, 0x3F, 0x23, 0x3E, 0xF4, 0xAD, 0x1D, 0x60, 0x74, 0xEE, 0xCB, + 0x59, 0x37, 0xDE, 0x7C, 0xDB, 0x3B, 0x22, 0x6C, 0xF1, 0xEC, 0x5F, 0xD6, + 0x9E, 0x50, 0xF8, 0x19, 0x84, 0x80, 0x07, 0xA6, 0x6E, 0x32, 0x77, 0xCE, + 0xA7, 0xF2, 0x85, 0x40, 0xC2, 0x06, 0x0C, 0xC5, 0xAA, 0xA7, 0x69, 0xA9, + 0x35, 0x97, 0xD9, 0x61, 0x55, 0xD8, 0xEF, 0xE8, 0x84, 0x34, 0x45, 0xC3, + 0x2E, 0x7A, 0x44, 0x9E, 0xDC, 0xCA, 0x0B, 0x80, 0xFC, 0xAB, 0x04, 0x5A, + 0xCD, 0x88, 0x55, 0x10, 0xD3, 0xDB, 0x73, 0xDB, 0xC9, 0x9E, 0x1E, 0x0E, + 0x05, 0x67, 0xD5, 0xFD, 0xD8, 0x38, 0x3E, 0x71, 0x65, 0x34, 0xC4, 0xC5, + 0x40, 0x43, 0x67, 0xE3, 0x79, 0xDA, 0x5F, 0x67, 0x4A, 0x3D, 0xB0, 0x8F, + 0xE7, 0x21, 0x3E, 0x15, 0x20, 0xFF, 0x6D, 0xF1, 0x9E, 0xF8, 0x28, 0x3D, + 0xF7, 0x40, 0x81, 0x94, 0x68, 0x5A, 0x3D, 0xE9, 0xF7, 0xAD, 0x83, 0xDB, + 0x2B, 0x9F, 0xE3, 0xE6, 0xF7, 0xD4, 0x02, 0x76, 0xF7, 0x20, 0x15, 0x41, + 0x34, 0x29, 0x69, 0x94, 0x1C, 0x26, 0x4C, 0xF6, 0x6A, 0xF4, 0x20, 0x33, + 0x71, 0x24, 0x08, 0xD4, 0x68, 0x00, 0xA1, 0xD4, 0x2E, 0x6B, 0xF4, 0xBC, + 0x46, 0x45, 0x24, 0x97, 0x2E, 0xF6, 0x39, 0x1E, 0xAF, 0x61, 0x00, 0x50, + 0xB7, 0xD4, 0xB7, 0x43, +}; + +} // namespace random_internal +ABSL_NAMESPACE_END +} // namespace absl diff --git a/absl/random/internal/randen_slow.cc b/absl/random/internal/randen_slow.cc index 8d074582..4e5f3dc1 100644 --- a/absl/random/internal/randen_slow.cc +++ b/absl/random/internal/randen_slow.cc @@ -20,6 +20,7 @@ #include "absl/base/attributes.h" #include "absl/random/internal/platform.h" +#include "absl/random/internal/randen_traits.h" #if ABSL_HAVE_ATTRIBUTE(always_inline) || \ (defined(__GNUC__) && !defined(__clang__)) @@ -225,35 +226,16 @@ constexpr uint32_t te3[256] = { 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, }; -struct alignas(16) u64x2 { - constexpr u64x2() : v{0, 0} {}; - constexpr u64x2(uint64_t hi, uint64_t lo) : v{lo, hi} {} - - uint64_t v[2]; -}; - // Software implementation of the Vector128 class, using uint32_t // as an underlying vector register. -// -struct Vector128 { - inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE Vector128& operator^=( - const Vector128& other) { - s[0] ^= other.s[0]; - s[1] ^= other.s[1]; - s[2] ^= other.s[2]; - s[3] ^= other.s[3]; - return *this; - } - +struct alignas(16) Vector128 { uint32_t s[4]; }; inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE Vector128 -Vector128Load(const void* ABSL_RANDOM_INTERNAL_RESTRICT from) { +Vector128Load(const void* from) { Vector128 result; - const uint8_t* ABSL_RANDOM_INTERNAL_RESTRICT src = - reinterpret_cast(from); - + const uint8_t* src = reinterpret_cast(from); result.s[0] = static_cast(src[0]) << 24 | static_cast(src[1]) << 16 | static_cast(src[2]) << 8 | @@ -274,7 +256,7 @@ Vector128Load(const void* ABSL_RANDOM_INTERNAL_RESTRICT from) { } inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void Vector128Store( - const Vector128& v, void* ABSL_RANDOM_INTERNAL_RESTRICT to) { + const Vector128& v, void* to) { uint8_t* dst = reinterpret_cast(to); dst[0] = static_cast(v.s[0] >> 24); dst[1] = static_cast(v.s[0] >> 16); @@ -298,91 +280,57 @@ inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void Vector128Store( // symmetry of AES (ensures previously equal columns differ afterwards). inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE Vector128 AesRound(const Vector128& state, const Vector128& round_key) { - // clang-format off Vector128 result; - result.s[0] = round_key.s[0] ^ - te0[uint8_t(state.s[0] >> 24)] ^ - te1[uint8_t(state.s[1] >> 16)] ^ - te2[uint8_t(state.s[2] >> 8)] ^ + result.s[0] = round_key.s[0] ^ // + te0[uint8_t(state.s[0] >> 24)] ^ // + te1[uint8_t(state.s[1] >> 16)] ^ // + te2[uint8_t(state.s[2] >> 8)] ^ // te3[uint8_t(state.s[3])]; - result.s[1] = round_key.s[1] ^ - te0[uint8_t(state.s[1] >> 24)] ^ - te1[uint8_t(state.s[2] >> 16)] ^ - te2[uint8_t(state.s[3] >> 8)] ^ + result.s[1] = round_key.s[1] ^ // + te0[uint8_t(state.s[1] >> 24)] ^ // + te1[uint8_t(state.s[2] >> 16)] ^ // + te2[uint8_t(state.s[3] >> 8)] ^ // te3[uint8_t(state.s[0])]; - result.s[2] = round_key.s[2] ^ - te0[uint8_t(state.s[2] >> 24)] ^ - te1[uint8_t(state.s[3] >> 16)] ^ - te2[uint8_t(state.s[0] >> 8)] ^ + result.s[2] = round_key.s[2] ^ // + te0[uint8_t(state.s[2] >> 24)] ^ // + te1[uint8_t(state.s[3] >> 16)] ^ // + te2[uint8_t(state.s[0] >> 8)] ^ // te3[uint8_t(state.s[1])]; - result.s[3] = round_key.s[3] ^ - te0[uint8_t(state.s[3] >> 24)] ^ - te1[uint8_t(state.s[0] >> 16)] ^ - te2[uint8_t(state.s[1] >> 8)] ^ + result.s[3] = round_key.s[3] ^ // + te0[uint8_t(state.s[3] >> 24)] ^ // + te1[uint8_t(state.s[0] >> 16)] ^ // + te2[uint8_t(state.s[1] >> 8)] ^ // te3[uint8_t(state.s[2])]; return result; - // clang-format on } -// RANDen = RANDom generator or beetroots in Swiss German. -// 'Strong' (well-distributed, unpredictable, backtracking-resistant) random -// generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32. -// -// High-level summary: -// 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is -// a sponge-like random generator that requires a cryptographic permutation. -// It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by -// achieving backtracking resistance with only one Permute() per buffer. -// -// 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round -// Function" constructs up to 1024-bit permutations using an improved -// Generalized Feistel network with 2-round AES-128 functions. This Feistel -// block shuffle achieves diffusion faster and is less vulnerable to -// sliced-biclique attacks than the Type-2 cyclic shuffle. -// -// 3) "Improving the Generalized Feistel" and "New criterion for diffusion -// property" extends the same kind of improved Feistel block shuffle to 16 -// branches, which enables a 2048-bit permutation. -// -// Combine these three ideas and also change Simpira's subround keys from -// structured/low-entropy counters to digits of Pi. - -// Randen constants. -constexpr size_t kFeistelBlocks = 16; -constexpr size_t kFeistelFunctions = kFeistelBlocks / 2; // = 8 -constexpr size_t kFeistelRounds = 16 + 1; // > 4 * log2(kFeistelBlocks) -constexpr size_t kKeys = kFeistelRounds * kFeistelFunctions; - -// INCLUDE keys. -#include "absl/random/internal/randen-keys.inc" - -static_assert(kKeys == kRoundKeys, "kKeys and kRoundKeys must be equal"); +using ::absl::random_internal::RandenTraits; -// 2 uint64_t lanes per Vector128 -static constexpr size_t kLanes = 2; +// Randen operates on 128-bit vectors. +struct alignas(16) u64x2 { + uint64_t data[2]; +}; // The improved Feistel block shuffle function for 16 blocks. inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void BlockShuffle( - uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT state_u64) { - static_assert(kFeistelBlocks == 16, + u64x2* state) { + static_assert(RandenTraits::kFeistelBlocks == 16, "Feistel block shuffle only works for 16 blocks."); - constexpr size_t shuffle[kFeistelBlocks] = {7, 2, 13, 4, 11, 8, 3, 6, - 15, 0, 9, 10, 1, 14, 5, 12}; - - u64x2* ABSL_RANDOM_INTERNAL_RESTRICT state = - reinterpret_cast(state_u64); + constexpr size_t shuffle[RandenTraits::kFeistelBlocks] = { + 7, 2, 13, 4, 11, 8, 3, 6, 15, 0, 9, 10, 1, 14, 5, 12}; // The fully unrolled loop without the memcpy improves the speed by about - // 30% over the equivalent (leaving code here as a comment): - if (false) { - u64x2 source[kFeistelBlocks]; - std::memcpy(source, state, sizeof(source)); - for (size_t i = 0; i < kFeistelBlocks; i++) { - const u64x2 v0 = source[shuffle[i]]; - state[i] = v0; - } + // 30% over the equivalent: +#if 0 + u64x2 source[RandenTraits::kFeistelBlocks]; + std::memcpy(source, state, sizeof(source)); + for (size_t i = 0; i < RandenTraits::kFeistelBlocks; i++) { + const u64x2 v0 = source[shuffle[i]]; + state[i] = v0; } + return; +#endif const u64x2 v0 = state[shuffle[0]]; const u64x2 v1 = state[shuffle[1]]; @@ -424,23 +372,23 @@ inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void BlockShuffle( // parallel hides the 7-cycle AESNI latency on HSW. Note that the Feistel // XORs are 'free' (included in the second AES instruction). inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE const u64x2* FeistelRound( - uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT state, + u64x2* ABSL_RANDOM_INTERNAL_RESTRICT state, const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys) { - for (size_t branch = 0; branch < kFeistelBlocks; branch += 4) { - const Vector128 s0 = Vector128Load(state + kLanes * branch); - const Vector128 s1 = Vector128Load(state + kLanes * (branch + 1)); + for (size_t branch = 0; branch < RandenTraits::kFeistelBlocks; branch += 4) { + const Vector128 s0 = Vector128Load(state + branch); + const Vector128 s1 = Vector128Load(state + branch + 1); const Vector128 f0 = AesRound(s0, Vector128Load(keys)); keys++; const Vector128 o1 = AesRound(f0, s1); - Vector128Store(o1, state + kLanes * (branch + 1)); + Vector128Store(o1, state + branch + 1); // Manually unroll this loop once. about 10% better than not unrolled. - const Vector128 s2 = Vector128Load(state + kLanes * (branch + 2)); - const Vector128 s3 = Vector128Load(state + kLanes * (branch + 3)); + const Vector128 s2 = Vector128Load(state + branch + 2); + const Vector128 s3 = Vector128Load(state + branch + 3); const Vector128 f2 = AesRound(s2, Vector128Load(keys)); keys++; const Vector128 o3 = AesRound(f2, s3); - Vector128Store(o3, state + kLanes * (branch + 3)); + Vector128Store(o3, state + branch + 3); } return keys; } @@ -450,11 +398,9 @@ inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE const u64x2* FeistelRound( // 2^64 queries if the round function is a PRF. This is similar to the b=8 case // of Simpira v2, but more efficient than its generic construction for b=16. inline ABSL_RANDOM_INTERNAL_ATTRIBUTE_ALWAYS_INLINE void Permute( - const void* keys, uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT state) { - const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys128 = - static_cast(keys); - for (size_t round = 0; round < kFeistelRounds; ++round) { - keys128 = FeistelRound(state, keys128); + u64x2* state, const u64x2* ABSL_RANDOM_INTERNAL_RESTRICT keys) { + for (size_t round = 0; round < RandenTraits::kFeistelRounds; ++round) { + keys = FeistelRound(state, keys); BlockShuffle(state); } } @@ -468,37 +414,42 @@ namespace random_internal { const void* RandenSlow::GetKeys() { // Round keys for one AES per Feistel round and branch. // The canonical implementation uses first digits of Pi. - return round_keys; + return kRandenRoundKeys; } void RandenSlow::Absorb(const void* seed_void, void* state_void) { - uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT state = - reinterpret_cast(state_void); - const uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT seed = - reinterpret_cast(seed_void); - - constexpr size_t kCapacityBlocks = kCapacityBytes / sizeof(uint64_t); - static_assert(kCapacityBlocks * sizeof(uint64_t) == kCapacityBytes, - "Not i*V"); - for (size_t i = kCapacityBlocks; i < kStateBytes / sizeof(uint64_t); ++i) { + auto* state = + reinterpret_cast(state_void); + const auto* seed = + reinterpret_cast( + seed_void); + + constexpr size_t kCapacityBlocks = + RandenTraits::kCapacityBytes / sizeof(uint64_t); + static_assert( + kCapacityBlocks * sizeof(uint64_t) == RandenTraits::kCapacityBytes, + "Not i*V"); + + for (size_t i = kCapacityBlocks; + i < RandenTraits::kStateBytes / sizeof(uint64_t); ++i) { state[i] ^= seed[i - kCapacityBlocks]; } } -void RandenSlow::Generate(const void* keys, void* state_void) { - static_assert(kCapacityBytes == sizeof(Vector128), "Capacity mismatch"); +void RandenSlow::Generate(const void* keys_void, void* state_void) { + static_assert(RandenTraits::kCapacityBytes == sizeof(u64x2), + "Capacity mismatch"); - uint64_t* ABSL_RANDOM_INTERNAL_RESTRICT state = - reinterpret_cast(state_void); + auto* state = reinterpret_cast(state_void); + const auto* keys = reinterpret_cast(keys_void); - const Vector128 prev_inner = Vector128Load(state); + const u64x2 prev_inner = state[0]; - Permute(keys, state); + Permute(state, keys); // Ensure backtracking resistance. - Vector128 inner = Vector128Load(state); - inner ^= prev_inner; - Vector128Store(inner, state); + state[0].data[0] ^= prev_inner.data[0]; + state[0].data[1] ^= prev_inner.data[1]; } } // namespace random_internal diff --git a/absl/random/internal/randen_slow.h b/absl/random/internal/randen_slow.h index 72f92b54..b6f137eb 100644 --- a/absl/random/internal/randen_slow.h +++ b/absl/random/internal/randen_slow.h @@ -28,13 +28,6 @@ namespace random_internal { // architectures lacking AES hardware acceleration intrinsics. class RandenSlow { public: - // Size of the entire sponge / state for the randen PRNG. - static constexpr size_t kStateBytes = 256; // 2048-bit - - // Size of the 'inner' (inaccessible) part of the sponge. Larger values would - // require more frequent calls to RandenGenerate. - static constexpr size_t kCapacityBytes = 16; // 128-bit - static void Generate(const void* keys, void* state_void); static void Absorb(const void* seed_void, void* state_void); static const void* GetKeys(); diff --git a/absl/random/internal/randen_slow_test.cc b/absl/random/internal/randen_slow_test.cc index c07155d8..4a535837 100644 --- a/absl/random/internal/randen_slow_test.cc +++ b/absl/random/internal/randen_slow_test.cc @@ -17,18 +17,20 @@ #include #include "gtest/gtest.h" +#include "absl/random/internal/randen_traits.h" namespace { using absl::random_internal::RandenSlow; +using absl::random_internal::RandenTraits; // Local state parameters. constexpr size_t kSeedBytes = - RandenSlow::kStateBytes - RandenSlow::kCapacityBytes; -constexpr size_t kStateSizeT = RandenSlow::kStateBytes / sizeof(uint64_t); + RandenTraits::kStateBytes - RandenTraits::kCapacityBytes; +constexpr size_t kStateSizeT = RandenTraits::kStateBytes / sizeof(uint64_t); constexpr size_t kSeedSizeT = kSeedBytes / sizeof(uint32_t); -struct randen { +struct alignas(16) randen { uint64_t state[kStateSizeT]; uint32_t seed[kSeedSizeT]; }; diff --git a/absl/random/internal/randen_traits.h b/absl/random/internal/randen_traits.h index 2b8bbe73..53caa936 100644 --- a/absl/random/internal/randen_traits.h +++ b/absl/random/internal/randen_traits.h @@ -32,6 +32,25 @@ namespace random_internal { // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32. // +// High-level summary: +// 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is +// a sponge-like random generator that requires a cryptographic permutation. +// It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by +// achieving backtracking resistance with only one Permute() per buffer. +// +// 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round +// Function" constructs up to 1024-bit permutations using an improved +// Generalized Feistel network with 2-round AES-128 functions. This Feistel +// block shuffle achieves diffusion faster and is less vulnerable to +// sliced-biclique attacks than the Type-2 cyclic shuffle. +// +// 3) "Improving the Generalized Feistel" and "New criterion for diffusion +// property" extends the same kind of improved Feistel block shuffle to 16 +// branches, which enables a 2048-bit permutation. +// +// Combine these three ideas and also change Simpira's subround keys from +// structured/low-entropy counters to digits of Pi (or other random source). + // RandenTraits contains the basic algorithm traits, such as the size of the // state, seed, sponge, etc. struct RandenTraits { @@ -45,17 +64,23 @@ struct RandenTraits { // Size of the default seed consumed by the sponge. static constexpr size_t kSeedBytes = kStateBytes - kCapacityBytes; + // Assuming 128-bit blocks, the number of blocks in the state. // Largest size for which security proofs are known. static constexpr size_t kFeistelBlocks = 16; - // Type-2 generalized Feistel => one round function for every two blocks. - static constexpr size_t kFeistelFunctions = kFeistelBlocks / 2; // = 8 - // Ensures SPRP security and two full subblock diffusions. // Must be > 4 * log2(kFeistelBlocks). static constexpr size_t kFeistelRounds = 16 + 1; + + // Size of the key. A 128-bit key block is used for every-other + // feistel block (Type-2 generalized Feistel network) in each round. + static constexpr size_t kKeyBytes = 16 * kFeistelRounds * kFeistelBlocks / 2; }; +// Randen key arrays. In randen_round_keys.cc +extern const unsigned char kRandenRoundKeys[RandenTraits::kKeyBytes]; +extern const unsigned char kRandenRoundKeysBE[RandenTraits::kKeyBytes]; + } // namespace random_internal ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/random/log_uniform_int_distribution_test.cc b/absl/random/log_uniform_int_distribution_test.cc index 5270531d..5e780d96 100644 --- a/absl/random/log_uniform_int_distribution_test.cc +++ b/absl/random/log_uniform_int_distribution_test.cc @@ -27,6 +27,7 @@ #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -121,7 +122,10 @@ class LogUniformIntChiSquaredTest // data generated by the log-uniform-int distribution. double ChiSquaredTestImpl(); - absl::InsecureBitGen rng_; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; double LogUniformIntChiSquaredTest::ChiSquaredTestImpl() { @@ -194,7 +198,6 @@ double LogUniformIntChiSquaredTest::ChiSquaredTestImpl() { TEST_P(LogUniformIntChiSquaredTest, MultiTest) { const int kTrials = 5; - int failures = 0; for (int i = 0; i < kTrials; i++) { double p_value = ChiSquaredTestImpl(); diff --git a/absl/random/poisson_distribution_test.cc b/absl/random/poisson_distribution_test.cc index 9d215fbc..8baabd11 100644 --- a/absl/random/poisson_distribution_test.cc +++ b/absl/random/poisson_distribution_test.cc @@ -30,6 +30,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -257,7 +258,10 @@ class PoissonDistributionZTest : public testing::TestWithParam, template bool SingleZTest(const double p, const size_t samples); - absl::InsecureBitGen rng_; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; template @@ -357,9 +361,13 @@ class PoissonDistributionChiSquaredTest : public testing::TestWithParam, private: void InitChiSquaredTest(const double buckets); - absl::InsecureBitGen rng_; std::vector cutoffs_; std::vector expected_; + + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; void PoissonDistributionChiSquaredTest::InitChiSquaredTest( diff --git a/absl/random/uniform_int_distribution_test.cc b/absl/random/uniform_int_distribution_test.cc index 69537603..276d72ad 100644 --- a/absl/random/uniform_int_distribution_test.cc +++ b/absl/random/uniform_int_distribution_test.cc @@ -26,6 +26,7 @@ #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -134,7 +135,11 @@ TYPED_TEST(UniformIntDistributionTest, TestMoments) { using param_type = typename absl::uniform_int_distribution::param_type; - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6}; + std::vector values(kSize); for (const auto& param : {param_type(0, Limits::max()), param_type(13, 127)}) { @@ -178,7 +183,11 @@ TYPED_TEST(UniformIntDistributionTest, ChiSquaredTest50) { const TypeParam min = std::is_unsigned::value ? 37 : -37; const TypeParam max = min + kBuckets; - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6}; + absl::uniform_int_distribution dist(min, max); std::vector counts(kBuckets + 1, 0); diff --git a/absl/random/uniform_real_distribution_test.cc b/absl/random/uniform_real_distribution_test.cc index a56374a6..be107cdd 100644 --- a/absl/random/uniform_real_distribution_test.cc +++ b/absl/random/uniform_real_distribution_test.cc @@ -27,6 +27,7 @@ #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -207,7 +208,11 @@ TYPED_TEST(UniformRealDistributionTest, TestMoments) { constexpr int kSize = 1000000; std::vector values(kSize); - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6}; + absl::uniform_real_distribution dist; for (int i = 0; i < kSize; i++) { values[i] = dist(rng); @@ -237,7 +242,11 @@ TYPED_TEST(UniformRealDistributionTest, ChiSquaredTest50) { const int kThreshold = absl::random_internal::ChiSquareValue(kBuckets - 1, 0.999999); - absl::InsecureBitGen rng; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6}; + for (const auto& param : {param_type(0, 1), param_type(5, 12), param_type(-5, 13), param_type(-5, -2)}) { const double min_val = param.a(); diff --git a/absl/random/zipf_distribution_test.cc b/absl/random/zipf_distribution_test.cc index 4d4a0fcf..f8cf70e0 100644 --- a/absl/random/zipf_distribution_test.cc +++ b/absl/random/zipf_distribution_test.cc @@ -27,6 +27,7 @@ #include "gtest/gtest.h" #include "absl/base/internal/raw_logging.h" #include "absl/random/internal/chi_square.h" +#include "absl/random/internal/pcg_engine.h" #include "absl/random/internal/sequence_urbg.h" #include "absl/random/random.h" #include "absl/strings/str_cat.h" @@ -213,7 +214,10 @@ class ZipfTest : public testing::TestWithParam, public: ZipfTest() : ZipfModel(GetParam().k(), GetParam().q(), GetParam().v()) {} - absl::InsecureBitGen rng_; + // We use a fixed bit generator for distribution accuracy tests. This allows + // these tests to be deterministic, while still testing the qualify of the + // implementation. + absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; }; TEST_P(ZipfTest, ChiSquaredTest) { diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index 02646add..9feb2248 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -267,38 +267,42 @@ bool ConvertIntArg(T v, const FormatConversionSpecImpl conv, using U = typename MakeUnsigned::type; IntDigits as_digits; - switch (conv.conversion_char()) { - case FormatConversionCharInternal::c: + // This odd casting is due to a bug in -Wswitch behavior in gcc49 which causes + // it to complain about a switch/case type mismatch, even though both are + // FormatConverionChar. Likely this is because at this point + // FormatConversionChar is declared, but not defined. + switch (static_cast(conv.conversion_char())) { + case static_cast(FormatConversionCharInternal::c): return ConvertCharImpl(static_cast(v), conv, sink); - case FormatConversionCharInternal::o: + case static_cast(FormatConversionCharInternal::o): as_digits.PrintAsOct(static_cast(v)); break; - case FormatConversionCharInternal::x: + case static_cast(FormatConversionCharInternal::x): as_digits.PrintAsHexLower(static_cast(v)); break; - case FormatConversionCharInternal::X: + case static_cast(FormatConversionCharInternal::X): as_digits.PrintAsHexUpper(static_cast(v)); break; - case FormatConversionCharInternal::u: + case static_cast(FormatConversionCharInternal::u): as_digits.PrintAsDec(static_cast(v)); break; - case FormatConversionCharInternal::d: - case FormatConversionCharInternal::i: + case static_cast(FormatConversionCharInternal::d): + case static_cast(FormatConversionCharInternal::i): as_digits.PrintAsDec(v); break; - case FormatConversionCharInternal::a: - case FormatConversionCharInternal::e: - case FormatConversionCharInternal::f: - case FormatConversionCharInternal::g: - case FormatConversionCharInternal::A: - case FormatConversionCharInternal::E: - case FormatConversionCharInternal::F: - case FormatConversionCharInternal::G: + case static_cast(FormatConversionCharInternal::a): + case static_cast(FormatConversionCharInternal::e): + case static_cast(FormatConversionCharInternal::f): + case static_cast(FormatConversionCharInternal::g): + case static_cast(FormatConversionCharInternal::A): + case static_cast(FormatConversionCharInternal::E): + case static_cast(FormatConversionCharInternal::F): + case static_cast(FormatConversionCharInternal::G): return ConvertFloatImpl(static_cast(v), conv, sink); default: diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 8f79948b..d441e87f 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -27,6 +27,8 @@ class FormatSink; namespace str_format_internal { +class FormatConversionSpec; + template struct HasUserDefinedConvert : std::false_type {}; diff --git a/absl/strings/internal/str_format/arg_test.cc b/absl/strings/internal/str_format/arg_test.cc index 37e5b754..bf3d7e8e 100644 --- a/absl/strings/internal/str_format/arg_test.cc +++ b/absl/strings/internal/str_format/arg_test.cc @@ -96,8 +96,8 @@ TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) { std::string s; FormatSinkImpl sink(&s); FormatConversionSpecImpl conv; - FormatConversionSpecImplFriend::SetConversionChar(FormatConversionChar::s, - &conv); + FormatConversionSpecImplFriend::SetConversionChar( + FormatConversionCharInternal::s, &conv); FormatConversionSpecImplFriend::SetFlags(Flags(), &conv); FormatConversionSpecImplFriend::SetWidth(-1, &conv); FormatConversionSpecImplFriend::SetPrecision(-1, &conv); diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index 23348174..a76d70b0 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -11,13 +11,13 @@ namespace { std::string ConvToString(FormatConversionCharSet conv) { std::string out; -#define CONV_SET_CASE(c) \ - if (Contains(conv, FormatConversionCharSet::c)) { \ - out += #c; \ +#define CONV_SET_CASE(c) \ + if (Contains(conv, FormatConversionCharSetInternal::c)) { \ + out += #c; \ } ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, ) #undef CONV_SET_CASE - if (Contains(conv, FormatConversionCharSet::kStar)) { + if (Contains(conv, FormatConversionCharSetInternal::kStar)) { out += "*"; } return out; diff --git a/absl/strings/internal/str_format/extension.cc b/absl/strings/internal/str_format/extension.cc index 2e5bc2ce..94f2b9c2 100644 --- a/absl/strings/internal/str_format/extension.cc +++ b/absl/strings/internal/str_format/extension.cc @@ -33,16 +33,17 @@ std::string Flags::ToString() const { return s; } -bool FormatSinkImpl::PutPaddedString(string_view v, int w, int p, bool l) { +bool FormatSinkImpl::PutPaddedString(string_view value, int width, + int precision, bool left) { size_t space_remaining = 0; - if (w >= 0) space_remaining = w; - size_t n = v.size(); - if (p >= 0) n = std::min(n, static_cast(p)); - string_view shown(v.data(), n); + if (width >= 0) space_remaining = width; + size_t n = value.size(); + if (precision >= 0) n = std::min(n, static_cast(precision)); + string_view shown(value.data(), n); space_remaining = Excess(shown.size(), space_remaining); - if (!l) Append(space_remaining, ' '); + if (!left) Append(space_remaining, ' '); Append(shown); - if (l) Append(space_remaining, ' '); + if (left) Append(space_remaining, ' '); return true; } diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index 36e70646..6c60c6c3 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -32,8 +32,9 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { -enum class FormatConversionCharSet : uint64_t; + enum class FormatConversionChar : uint8_t; +enum class FormatConversionCharSet : uint64_t; class FormatRawSinkImpl { public: @@ -106,7 +107,7 @@ class FormatSinkImpl { size_t size() const { return size_; } // Put 'v' to 'sink' with specified width, precision, and left flag. - bool PutPaddedString(string_view v, int w, int p, bool l); + bool PutPaddedString(string_view v, int width, int precision, bool left); template T Wrap() { @@ -420,81 +421,6 @@ inline size_t Excess(size_t used, size_t capacity) { return used < capacity ? capacity - used : 0; } -class FormatConversionSpec { - public: - // Width and precison are not specified, no flags are set. - bool is_basic() const { return impl_.is_basic(); } - bool has_left_flag() const { return impl_.has_left_flag(); } - bool has_show_pos_flag() const { return impl_.has_show_pos_flag(); } - bool has_sign_col_flag() const { return impl_.has_sign_col_flag(); } - bool has_alt_flag() const { return impl_.has_alt_flag(); } - bool has_zero_flag() const { return impl_.has_zero_flag(); } - - FormatConversionChar conversion_char() const { - return impl_.conversion_char(); - } - - // Returns the specified width. If width is unspecfied, it returns a negative - // value. - int width() const { return impl_.width(); } - // Returns the specified precision. If precision is unspecfied, it returns a - // negative value. - int precision() const { return impl_.precision(); } - - private: - explicit FormatConversionSpec( - str_format_internal::FormatConversionSpecImpl impl) - : impl_(impl) {} - - friend str_format_internal::FormatConversionSpecImpl; - - absl::str_format_internal::FormatConversionSpecImpl impl_; -}; - -// clang-format off -enum class FormatConversionChar : uint8_t { - c, s, // text - d, i, o, u, x, X, // int - f, F, e, E, g, G, a, A, // float - n, p // misc -}; -// clang-format on - -enum class FormatConversionCharSet : uint64_t { - // text - c = str_format_internal::FormatConversionCharToConvInt('c'), - s = str_format_internal::FormatConversionCharToConvInt('s'), - // integer - d = str_format_internal::FormatConversionCharToConvInt('d'), - i = str_format_internal::FormatConversionCharToConvInt('i'), - o = str_format_internal::FormatConversionCharToConvInt('o'), - u = str_format_internal::FormatConversionCharToConvInt('u'), - x = str_format_internal::FormatConversionCharToConvInt('x'), - X = str_format_internal::FormatConversionCharToConvInt('X'), - // Float - f = str_format_internal::FormatConversionCharToConvInt('f'), - F = str_format_internal::FormatConversionCharToConvInt('F'), - e = str_format_internal::FormatConversionCharToConvInt('e'), - E = str_format_internal::FormatConversionCharToConvInt('E'), - g = str_format_internal::FormatConversionCharToConvInt('g'), - G = str_format_internal::FormatConversionCharToConvInt('G'), - a = str_format_internal::FormatConversionCharToConvInt('a'), - A = str_format_internal::FormatConversionCharToConvInt('A'), - // misc - n = str_format_internal::FormatConversionCharToConvInt('n'), - p = str_format_internal::FormatConversionCharToConvInt('p'), - - // Used for width/precision '*' specification. - kStar = str_format_internal::FormatConversionCharToConvInt('*'), - - // Some predefined values: - kIntegral = d | i | u | o | x | X, - kFloating = a | e | f | g | A | E | F | G, - kNumeric = kIntegral | kFloating, - kString = s, - kPointer = p, -}; - } // namespace str_format_internal ABSL_NAMESPACE_END diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index f48510b4..f833a80a 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -63,10 +63,6 @@ // loosely typed. `FormatUntyped()` is not a template and does not perform // any compile-time checking of the format string; instead, it returns a // boolean from a runtime check. -// -// In addition, the `str_format` library provides extension points for -// augmenting formatting to new types. These extensions are fully documented -// within the `str_format_extension.h` header file. #ifndef ABSL_STRINGS_STR_FORMAT_H_ #define ABSL_STRINGS_STR_FORMAT_H_ diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 3f14dba3..49a68849 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -1,4 +1,6 @@ +#include "absl/strings/str_format.h" + #include #include #include @@ -6,13 +8,14 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "absl/strings/str_format.h" +#include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" namespace absl { ABSL_NAMESPACE_BEGIN namespace { using str_format_internal::FormatArgImpl; +using str_format_internal::FormatConversionCharSetInternal; using FormatEntryPointTest = ::testing::Test; @@ -535,100 +538,106 @@ TEST_F(ParsedFormatTest, SimpleUncheckedIncorrect) { using absl::str_format_internal::FormatConversionCharSet; TEST_F(ParsedFormatTest, UncheckedCorrect) { - auto f = ExtendedParsedFormat::New("ABC%dDEF"); + auto f = + ExtendedParsedFormat::New("ABC%dDEF"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); std::string format = "%sFFF%dZZZ%f"; - auto f2 = - ExtendedParsedFormat::New(format); + auto f2 = ExtendedParsedFormat< + FormatConversionCharSetInternal::kString, + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::kFloating>::New(format); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); - f2 = - ExtendedParsedFormat::New("%s %d %f"); + f2 = ExtendedParsedFormat< + FormatConversionCharSetInternal::kString, + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::kFloating>::New("%s %d %f"); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); - auto star = ExtendedParsedFormat::New("%*d"); + auto star = + ExtendedParsedFormat::New("%*d"); ASSERT_TRUE(star); EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); - auto dollar = - ExtendedParsedFormat::New("%2$s %1$d"); + auto dollar = ExtendedParsedFormat< + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::New("%2$s %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); // with reuse - dollar = - ExtendedParsedFormat::New("%2$s %1$d %1$d"); + dollar = ExtendedParsedFormat< + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::New("%2$s %1$d %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); } TEST_F(ParsedFormatTest, UncheckedIgnoredArgs) { - EXPECT_FALSE((ExtendedParsedFormat::New("ABC"))); EXPECT_FALSE( - (ExtendedParsedFormat::New("%dABC"))); + (ExtendedParsedFormat::New("ABC"))); EXPECT_FALSE( - (ExtendedParsedFormat::New("ABC%2$s"))); - auto f = - ExtendedParsedFormat::NewAllowIgnored("ABC"); + (ExtendedParsedFormat::New("%dABC"))); + EXPECT_FALSE((ExtendedParsedFormat< + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::New("ABC%2$s"))); + auto f = ExtendedParsedFormat< + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::NewAllowIgnored("ABC"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f)); f = ExtendedParsedFormat< - FormatConversionCharSet::d, - FormatConversionCharSet::s>::NewAllowIgnored("%dABC"); + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::NewAllowIgnored("%dABC"); ASSERT_TRUE(f); EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f)); f = ExtendedParsedFormat< - FormatConversionCharSet::d, - FormatConversionCharSet::s>::NewAllowIgnored("ABC%2$s"); + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::s>::NewAllowIgnored("ABC%2$s"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f)); } TEST_F(ParsedFormatTest, UncheckedMultipleTypes) { - auto dx = ExtendedParsedFormat::New("%1$d %1$x"); + auto dx = ExtendedParsedFormat< + FormatConversionCharSetInternal::d | + FormatConversionCharSetInternal::x>::New("%1$d %1$x"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}[ ]{1$x:1$x}", SummarizeParsedFormat(*dx)); - dx = ExtendedParsedFormat::New("%1$d"); + dx = ExtendedParsedFormat::New("%1$d"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}", SummarizeParsedFormat(*dx)); } TEST_F(ParsedFormatTest, UncheckedIncorrect) { - EXPECT_FALSE(ExtendedParsedFormat::New("")); - EXPECT_FALSE( - ExtendedParsedFormat::New("ABC%dDEF%d")); + ExtendedParsedFormat::New("")); + + EXPECT_FALSE(ExtendedParsedFormat::New( + "ABC%dDEF%d")); std::string format = "%sFFF%dZZZ%f"; - EXPECT_FALSE((ExtendedParsedFormat::New(format))); + EXPECT_FALSE( + (ExtendedParsedFormat::New(format))); } TEST_F(ParsedFormatTest, RegressionMixPositional) { - EXPECT_FALSE( - (ExtendedParsedFormat::New("%1$d %o"))); + EXPECT_FALSE((ExtendedParsedFormat< + FormatConversionCharSetInternal::d, + FormatConversionCharSetInternal::o>::New("%1$d %o"))); } using FormatWrapperTest = ::testing::Test; -- cgit v1.2.3 From 33caf1097ecce4fe892567462fa8821d477854b4 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 26 May 2020 10:57:33 -0700 Subject: Export of internal Abseil changes -- 7d0468a6610ed85586d5c87fd65de8dac5118923 by Derek Mauro : Import of CCTZ from GitHub. PiperOrigin-RevId: 313226473 -- 1131ef6d116f5ce7d46537a82f300ea06dcaaa53 by Gennadiy Rozental : Migrate internal interface to use mutable references. PiperOrigin-RevId: 312931131 -- 96225212a9f5fbd0b38c71fe65539164992c7c3b by Laramie Leavitt : Remove random/internal/distributions.h This file was something of an historical artifact. All of the related code has either been removed or migraged, and so the only remaining type belongs with uniform_helper.h, as it is used to infer the return type of the absl::Uniform method in a few cases. PiperOrigin-RevId: 312878173 -- 6dcbd5be58ad425e08740ff64088373ee7fe4a72 by Mark Barolak : Release the StrFormat test case for Cords to open source. PiperOrigin-RevId: 312707974 -- 34484d18dfb63a0a7ad6e2aaeb570e33592968be by Abseil Team : Let Cord::Cord(string&&), Cord::operator=(string&&), Cord::Append(string&&), and Cord::Prepend(string&&) steal string data and embed it into the Cord as a single external chunk, instead of copying it into flat chunks (at most 4083-byte each). Stealing string data is faster, but it creates a long chunk, which leads to a higher more memory usage if its subcords are created and outlive the whole Cord. These functions revert to copying the data if any of the following conditions holds: - string size is at most kMaxBytesToCopy (511), to avoid the overhead of an external chunk for short strings; - less than half of string capacity is used, to avoid pinning to much unused memory. PiperOrigin-RevId: 312683785 GitOrigin-RevId: 7d0468a6610ed85586d5c87fd65de8dac5118923 Change-Id: If79b5a1dfe6d53a8ddddbc7da84338f11fc4cfa3 --- CMake/AbseilDll.cmake | 1 - absl/flags/commandlineflag.cc | 2 +- absl/flags/commandlineflag.h | 2 +- absl/flags/commandlineflag_test.cc | 50 ++++++------ absl/flags/flag.h | 32 ++++---- absl/flags/flag_test.cc | 10 +-- absl/flags/internal/flag.cc | 38 ++++----- absl/flags/internal/flag.h | 24 +++--- absl/flags/internal/private_handle_accessor.cc | 10 +-- absl/flags/internal/private_handle_accessor.h | 6 +- absl/flags/internal/registry.cc | 95 +++++++++++----------- absl/flags/internal/registry.h | 6 +- absl/flags/internal/type_erased.cc | 2 +- absl/flags/internal/usage.cc | 25 +++--- absl/flags/marshalling.cc | 21 ++--- absl/flags/parse.cc | 38 ++++----- absl/flags/parse_test.cc | 26 +++--- absl/random/BUILD.bazel | 3 +- absl/random/CMakeLists.txt | 29 +------ absl/random/distributions.h | 2 +- absl/random/internal/BUILD.bazel | 18 +--- absl/random/internal/distributions.h | 52 ------------ absl/random/internal/uniform_helper.h | 30 +++++++ absl/strings/cord.cc | 65 +++++++++++++++ absl/strings/cord.h | 27 ++---- absl/strings/str_format_test.cc | 2 + absl/time/internal/cctz/include/cctz/time_zone.h | 3 +- absl/time/internal/cctz/src/cctz_benchmark.cc | 16 ++-- absl/time/internal/cctz/src/time_zone_format.cc | 23 +++++- .../internal/cctz/src/time_zone_format_test.cc | 18 +++- .../internal/cctz/src/time_zone_lookup_test.cc | 2 +- 31 files changed, 345 insertions(+), 333 deletions(-) delete mode 100644 absl/random/internal/distributions.h (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/AbseilDll.cmake b/CMake/AbseilDll.cmake index a5c9bc0d..ccd409f3 100644 --- a/CMake/AbseilDll.cmake +++ b/CMake/AbseilDll.cmake @@ -135,7 +135,6 @@ set(ABSL_INTERNAL_DLL_FILES "random/exponential_distribution.h" "random/gaussian_distribution.cc" "random/gaussian_distribution.h" - "random/internal/distributions.h" "random/internal/distribution_caller.h" "random/internal/fast_uniform_bits.h" "random/internal/fastmath.h" diff --git a/absl/flags/commandlineflag.cc b/absl/flags/commandlineflag.cc index 83d14c1b..cea57234 100644 --- a/absl/flags/commandlineflag.cc +++ b/absl/flags/commandlineflag.cc @@ -21,7 +21,7 @@ ABSL_NAMESPACE_BEGIN bool CommandLineFlag::IsRetired() const { return false; } bool CommandLineFlag::ParseFrom(absl::string_view value, std::string* error) { return ParseFrom(value, flags_internal::SET_FLAGS_VALUE, - flags_internal::kProgrammaticChange, error); + flags_internal::kProgrammaticChange, *error); } namespace flags_internal { diff --git a/absl/flags/commandlineflag.h b/absl/flags/commandlineflag.h index f09f1f70..43055d04 100644 --- a/absl/flags/commandlineflag.h +++ b/absl/flags/commandlineflag.h @@ -159,7 +159,7 @@ class CommandLineFlag { virtual bool ParseFrom(absl::string_view value, flags_internal::FlagSettingMode set_mode, flags_internal::ValueSource source, - std::string* error) = 0; + std::string& error) = 0; // Returns id of the flag's value type. virtual flags_internal::FlagFastTypeId TypeId() const = 0; diff --git a/absl/flags/commandlineflag_test.cc b/absl/flags/commandlineflag_test.cc index 352edccf..4b9718e9 100644 --- a/absl/flags/commandlineflag_test.cc +++ b/absl/flags/commandlineflag_test.cc @@ -129,57 +129,57 @@ TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) { flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err)); + *flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11); EXPECT_FALSE( flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, - &err)); + *flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_FALSE( flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom( - flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, - &err)); + *flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'"); EXPECT_FALSE( flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom( - flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err)); + *flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123); EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'"); EXPECT_FALSE( flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, - &err)); + *flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16); EXPECT_FALSE( flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, &err)); + *flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11); EXPECT_TRUE(flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01)); EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom( - flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err)); + *flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err)); EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'"); auto* flag_02 = flags::FindCommandLineFlag("string_flag"); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, - &err)); + *flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz"); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, &err)); + *flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err)); EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), ""); } @@ -191,15 +191,15 @@ TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) { auto* flag_01 = flags::FindCommandLineFlag("int_flag"); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange, - &err)); + *flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange, + err)); EXPECT_EQ(flag_01->DefaultValue(), "111"); auto* flag_02 = flags::FindCommandLineFlag("string_flag"); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange, - &err)); + *flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange, + err)); EXPECT_EQ(flag_02->DefaultValue(), "abc"); } @@ -211,25 +211,25 @@ TEST_F(CommandLineFlagTest, TestParseFromIfDefault) { auto* flag_01 = flags::FindCommandLineFlag("int_flag"); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, - &err)) + *flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, + err)) << err; EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, - &err)); + *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22); // EXPECT_EQ(err, "ERROR: int_flag is already set to 22"); // Reset back to default value EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, - &err)); + *flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, + err)); EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom( - flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, - &err)); + *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange, + err)); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201); // EXPECT_EQ(err, "ERROR: int_flag is already set to 201"); } diff --git a/absl/flags/flag.h b/absl/flags/flag.h index f84853ea..dd36e6c7 100644 --- a/absl/flags/flag.h +++ b/absl/flags/flag.h @@ -110,12 +110,12 @@ class Flag { impl_(nullptr) {} #endif - flags_internal::Flag* GetImpl() const { + flags_internal::Flag& GetImpl() const { if (!inited_.load(std::memory_order_acquire)) { absl::MutexLock l(flags_internal::GetGlobalConstructionGuard()); if (inited_.load(std::memory_order_acquire)) { - return impl_; + return *impl_; } impl_ = new flags_internal::Flag( @@ -127,28 +127,28 @@ class Flag { inited_.store(true, std::memory_order_release); } - return impl_; + return *impl_; } // Public methods of `absl::Flag` are NOT part of the Abseil Flags API. // See https://abseil.io/docs/cpp/guides/flags - bool IsRetired() const { return GetImpl()->IsRetired(); } - absl::string_view Name() const { return GetImpl()->Name(); } - std::string Help() const { return GetImpl()->Help(); } - bool IsModified() const { return GetImpl()->IsModified(); } + bool IsRetired() const { return GetImpl().IsRetired(); } + absl::string_view Name() const { return GetImpl().Name(); } + std::string Help() const { return GetImpl().Help(); } + bool IsModified() const { return GetImpl().IsModified(); } bool IsSpecifiedOnCommandLine() const { - return GetImpl()->IsSpecifiedOnCommandLine(); + return GetImpl().IsSpecifiedOnCommandLine(); } - std::string Filename() const { return GetImpl()->Filename(); } - std::string DefaultValue() const { return GetImpl()->DefaultValue(); } - std::string CurrentValue() const { return GetImpl()->CurrentValue(); } + std::string Filename() const { return GetImpl().Filename(); } + std::string DefaultValue() const { return GetImpl().DefaultValue(); } + std::string CurrentValue() const { return GetImpl().CurrentValue(); } template inline bool IsOfType() const { - return GetImpl()->template IsOfType(); + return GetImpl().template IsOfType(); } - T Get() const { return GetImpl()->Get(); } - void Set(const T& v) { GetImpl()->Set(v); } - void InvokeCallback() { GetImpl()->InvokeCallback(); } + T Get() const { return GetImpl().Get(); } + void Set(const T& v) { GetImpl().Set(v); } + void InvokeCallback() { GetImpl().InvokeCallback(); } // The data members are logically private, but they need to be public for // this to be an aggregate type. @@ -265,7 +265,7 @@ ABSL_NAMESPACE_END // ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES #if !defined(_MSC_VER) || defined(__clang__) -#define ABSL_FLAG_IMPL_FLAG_PTR(flag) &flag +#define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag #define ABSL_FLAG_IMPL_HELP_ARG(name) \ absl::flags_internal::HelpArg( \ FLAGS_help_storage_##name) diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc index 8d53ecd6..58a07999 100644 --- a/absl/flags/flag_test.cc +++ b/absl/flags/flag_test.cc @@ -150,21 +150,21 @@ DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt, kGenFunc); DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt, kGenFunc); template -bool TestConstructionFor(const flags::Flag& f1, flags::Flag* f2) { +bool TestConstructionFor(const flags::Flag& f1, flags::Flag& f2) { EXPECT_EQ(f1.Name(), "f1"); EXPECT_EQ(f1.Help(), "literal help"); EXPECT_EQ(f1.Filename(), "file"); flags::FlagRegistrar(f2).OnUpdate(TestCallback); - EXPECT_EQ(f2->Name(), "f2"); - EXPECT_EQ(f2->Help(), "dynamic help"); - EXPECT_EQ(f2->Filename(), "file"); + EXPECT_EQ(f2.Name(), "f2"); + EXPECT_EQ(f2.Help(), "dynamic help"); + EXPECT_EQ(f2.Filename(), "file"); return true; } -#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, &f2##T); +#define TEST_CONSTRUCTED_FLAG(T) TestConstructionFor(f1##T, f2##T); TEST_F(FlagTest, TestConstruction) { TEST_CONSTRUCTED_FLAG(bool); diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc index 35ae55fb..ee974244 100644 --- a/absl/flags/internal/flag.cc +++ b/absl/flags/internal/flag.cc @@ -62,14 +62,14 @@ bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) { // need to acquire these locks themselves. class MutexRelock { public: - explicit MutexRelock(absl::Mutex* mu) : mu_(mu) { mu_->Unlock(); } - ~MutexRelock() { mu_->Lock(); } + explicit MutexRelock(absl::Mutex& mu) : mu_(mu) { mu_.Unlock(); } + ~MutexRelock() { mu_.Lock(); } MutexRelock(const MutexRelock&) = delete; MutexRelock& operator=(const MutexRelock&) = delete; private: - absl::Mutex* mu_; + absl::Mutex& mu_; }; } // namespace @@ -82,7 +82,7 @@ class FlagImpl; class FlagState : public flags_internal::FlagStateInterface { public: template - FlagState(FlagImpl* flag_impl, const V& v, bool modified, + FlagState(FlagImpl& flag_impl, const V& v, bool modified, bool on_command_line, int64_t counter) : flag_impl_(flag_impl), value_(v), @@ -91,9 +91,9 @@ class FlagState : public flags_internal::FlagStateInterface { counter_(counter) {} ~FlagState() override { - if (flag_impl_->ValueStorageKind() != FlagValueStorageKind::kAlignedBuffer) + if (flag_impl_.ValueStorageKind() != FlagValueStorageKind::kAlignedBuffer) return; - flags_internal::Delete(flag_impl_->op_, value_.heap_allocated); + flags_internal::Delete(flag_impl_.op_, value_.heap_allocated); } private: @@ -101,15 +101,15 @@ class FlagState : public flags_internal::FlagStateInterface { // Restores the flag to the saved state. void Restore() const override { - if (!flag_impl_->RestoreState(*this)) return; + if (!flag_impl_.RestoreState(*this)) return; - ABSL_INTERNAL_LOG( - INFO, absl::StrCat("Restore saved value of ", flag_impl_->Name(), - " to: ", flag_impl_->CurrentValue())); + ABSL_INTERNAL_LOG(INFO, + absl::StrCat("Restore saved value of ", flag_impl_.Name(), + " to: ", flag_impl_.CurrentValue())); } // Flag and saved flag data. - FlagImpl* flag_impl_; + FlagImpl& flag_impl_; union SavedValue { explicit SavedValue(void* v) : heap_allocated(v) {} explicit SavedValue(int64_t v) : one_word(v) {} @@ -326,7 +326,7 @@ void FlagImpl::InvokeCallback() const { // and it also can be different by the time the callback invocation is // completed. Requires that *primary_lock be held in exclusive mode; it may be // released and reacquired by the implementation. - MutexRelock relock(DataGuard()); + MutexRelock relock(*DataGuard()); absl::MutexLock lock(&callback_->guard); cb(); } @@ -339,17 +339,17 @@ std::unique_ptr FlagImpl::SaveState() { switch (ValueStorageKind()) { case FlagValueStorageKind::kAlignedBuffer: { return absl::make_unique( - this, flags_internal::Clone(op_, AlignedBufferValue()), modified, + *this, flags_internal::Clone(op_, AlignedBufferValue()), modified, on_command_line, counter_); } case FlagValueStorageKind::kOneWordAtomic: { return absl::make_unique( - this, OneWordValue().load(std::memory_order_acquire), modified, + *this, OneWordValue().load(std::memory_order_acquire), modified, on_command_line, counter_); } case FlagValueStorageKind::kTwoWordsAtomic: { return absl::make_unique( - this, TwoWordsValue().load(std::memory_order_acquire), modified, + *this, TwoWordsValue().load(std::memory_order_acquire), modified, on_command_line, counter_); } } @@ -410,14 +410,14 @@ std::atomic& FlagImpl::TwoWordsValue() const { // parsed value. In case if any error is encountered in either step, the error // message is stored in 'err' std::unique_ptr FlagImpl::TryParse( - absl::string_view value, std::string* err) const { + absl::string_view value, std::string& err) const { std::unique_ptr tentative_value = MakeInitValue(); std::string parse_err; if (!flags_internal::Parse(op_, value, tentative_value.get(), &parse_err)) { absl::string_view err_sep = parse_err.empty() ? "" : "; "; - *err = absl::StrCat("Illegal value '", value, "' specified for flag '", - Name(), "'", err_sep, parse_err); + err = absl::StrCat("Illegal value '", value, "' specified for flag '", + Name(), "'", err_sep, parse_err); return nullptr; } @@ -473,7 +473,7 @@ void FlagImpl::Write(const void* src) { // * Update the current flag value if it was never set before // The mode is selected based on 'set_mode' parameter. bool FlagImpl::ParseFrom(absl::string_view value, FlagSettingMode set_mode, - ValueSource source, std::string* err) { + ValueSource source, std::string& err) { absl::MutexLock l(DataGuard()); switch (set_mode) { diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h index 97ddb5f9..e1885809 100644 --- a/absl/flags/internal/flag.h +++ b/absl/flags/internal/flag.h @@ -374,31 +374,31 @@ struct FlagValue; template struct FlagValue { - bool Get(T*) const { return false; } + bool Get(T&) const { return false; } alignas(T) char value[sizeof(T)]; }; template struct FlagValue : FlagOneWordValue { - bool Get(T* dst) const { + bool Get(T& dst) const { int64_t one_word_val = value.load(std::memory_order_acquire); if (ABSL_PREDICT_FALSE(one_word_val == UninitializedFlagValue())) { return false; } - std::memcpy(dst, static_cast(&one_word_val), sizeof(T)); + std::memcpy(&dst, static_cast(&one_word_val), sizeof(T)); return true; } }; template struct FlagValue : FlagTwoWordsValue { - bool Get(T* dst) const { + bool Get(T& dst) const { AlignedTwoWords two_words_val = value.load(std::memory_order_acquire); if (ABSL_PREDICT_FALSE(!two_words_val.IsInitialized())) { return false; } - std::memcpy(dst, static_cast(&two_words_val), sizeof(T)); + std::memcpy(&dst, static_cast(&two_words_val), sizeof(T)); return true; } }; @@ -502,7 +502,7 @@ class FlagImpl final : public CommandLineFlag { // Attempts to parse supplied `value` string. If parsing is successful, // returns new value. Otherwise returns nullptr. std::unique_ptr TryParse(absl::string_view value, - std::string* err) const + std::string& err) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); // Stores the flag value based on the pointer to the source. void StoreValue(const void* src) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()); @@ -544,7 +544,7 @@ class FlagImpl final : public CommandLineFlag { ABSL_LOCKS_EXCLUDED(*DataGuard()); bool ParseFrom(absl::string_view value, FlagSettingMode set_mode, - ValueSource source, std::string* error) override + ValueSource source, std::string& error) override ABSL_LOCKS_EXCLUDED(*DataGuard()); // Immutable flag's state. @@ -651,7 +651,7 @@ class Flag { impl_.AssertValidType(base_internal::FastTypeId(), &GenRuntimeTypeId); #endif - if (!value_.Get(&u.value)) impl_.Read(&u.value); + if (!value_.Get(u.value)) impl_.Read(&u.value); return std::move(u.value); } void Set(const T& v) { @@ -730,12 +730,12 @@ struct FlagRegistrarEmpty {}; template class FlagRegistrar { public: - explicit FlagRegistrar(Flag* flag) : flag_(flag) { - if (do_register) flags_internal::RegisterCommandLineFlag(&flag_->impl_); + explicit FlagRegistrar(Flag& flag) : flag_(flag) { + if (do_register) flags_internal::RegisterCommandLineFlag(flag_.impl_); } FlagRegistrar OnUpdate(FlagCallbackFunc cb) && { - flag_->impl_.SetCallback(cb); + flag_.impl_.SetCallback(cb); return *this; } @@ -745,7 +745,7 @@ class FlagRegistrar { operator FlagRegistrarEmpty() const { return {}; } // NOLINT private: - Flag* flag_; // Flag being registered (not owned). + Flag& flag_; // Flag being registered (not owned). }; } // namespace flags_internal diff --git a/absl/flags/internal/private_handle_accessor.cc b/absl/flags/internal/private_handle_accessor.cc index 64fe3166..24b49136 100644 --- a/absl/flags/internal/private_handle_accessor.cc +++ b/absl/flags/internal/private_handle_accessor.cc @@ -24,8 +24,8 @@ FlagFastTypeId PrivateHandleAccessor::TypeId(const CommandLineFlag& flag) { } std::unique_ptr PrivateHandleAccessor::SaveState( - CommandLineFlag* flag) { - return flag->SaveState(); + CommandLineFlag& flag) { + return flag.SaveState(); } bool PrivateHandleAccessor::IsSpecifiedOnCommandLine( @@ -43,12 +43,12 @@ void PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip( flag.CheckDefaultValueParsingRoundtrip(); } -bool PrivateHandleAccessor::ParseFrom(CommandLineFlag* flag, +bool PrivateHandleAccessor::ParseFrom(CommandLineFlag& flag, absl::string_view value, flags_internal::FlagSettingMode set_mode, flags_internal::ValueSource source, - std::string* error) { - return flag->ParseFrom(value, set_mode, source, error); + std::string& error) { + return flag.ParseFrom(value, set_mode, source, error); } } // namespace flags_internal diff --git a/absl/flags/internal/private_handle_accessor.h b/absl/flags/internal/private_handle_accessor.h index 07838600..9a327a07 100644 --- a/absl/flags/internal/private_handle_accessor.h +++ b/absl/flags/internal/private_handle_accessor.h @@ -31,7 +31,7 @@ class PrivateHandleAccessor { static FlagFastTypeId TypeId(const CommandLineFlag& flag); // Access to CommandLineFlag::SaveState. - static std::unique_ptr SaveState(CommandLineFlag* flag); + static std::unique_ptr SaveState(CommandLineFlag& flag); // Access to CommandLineFlag::IsSpecifiedOnCommandLine. static bool IsSpecifiedOnCommandLine(const CommandLineFlag& flag); @@ -43,9 +43,9 @@ class PrivateHandleAccessor { // Access to CommandLineFlag::CheckDefaultValueParsingRoundtrip. static void CheckDefaultValueParsingRoundtrip(const CommandLineFlag& flag); - static bool ParseFrom(CommandLineFlag* flag, absl::string_view value, + static bool ParseFrom(CommandLineFlag& flag, absl::string_view value, flags_internal::FlagSettingMode set_mode, - flags_internal::ValueSource source, std::string* error); + flags_internal::ValueSource source, std::string& error); }; } // namespace flags_internal diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc index 70d76557..4bcebfa9 100644 --- a/absl/flags/internal/registry.cc +++ b/absl/flags/internal/registry.cc @@ -60,8 +60,8 @@ class FlagRegistry { FlagRegistry() = default; ~FlagRegistry() = default; - // Store a flag in this registry. Takes ownership of *flag. - void RegisterFlag(CommandLineFlag* flag); + // Store a flag in this registry. Takes ownership of *flag. + void RegisterFlag(CommandLineFlag& flag); void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); } void Unlock() ABSL_UNLOCK_FUNCTION(lock_) { lock_.Unlock(); } @@ -74,12 +74,12 @@ class FlagRegistry { // found or not retired. Does not emit a warning. CommandLineFlag* FindRetiredFlagLocked(absl::string_view name); - static FlagRegistry* GlobalRegistry(); // returns a singleton registry + static FlagRegistry& GlobalRegistry(); // returns a singleton registry private: friend class FlagSaverImpl; // reads all the flags in order to copy them friend void ForEachFlagUnlocked( - std::function visitor); + std::function visitor); // The map from name to flag, for FindFlagLocked(). using FlagMap = std::map; @@ -94,65 +94,62 @@ class FlagRegistry { FlagRegistry& operator=(const FlagRegistry&); }; -FlagRegistry* FlagRegistry::GlobalRegistry() { +FlagRegistry& FlagRegistry::GlobalRegistry() { static FlagRegistry* global_registry = new FlagRegistry; - return global_registry; + return *global_registry; } namespace { class FlagRegistryLock { public: - explicit FlagRegistryLock(FlagRegistry* fr) : fr_(fr) { fr_->Lock(); } - ~FlagRegistryLock() { fr_->Unlock(); } + explicit FlagRegistryLock(FlagRegistry& fr) : fr_(fr) { fr_.Lock(); } + ~FlagRegistryLock() { fr_.Unlock(); } private: - FlagRegistry* const fr_; + FlagRegistry& fr_; }; -void DestroyRetiredFlag(CommandLineFlag* flag); +void DestroyRetiredFlag(CommandLineFlag& flag); } // namespace -void FlagRegistry::RegisterFlag(CommandLineFlag* flag) { - FlagRegistryLock registry_lock(this); +void FlagRegistry::RegisterFlag(CommandLineFlag& flag) { + FlagRegistryLock registry_lock(*this); std::pair ins = - flags_.insert(FlagMap::value_type(flag->Name(), flag)); + flags_.insert(FlagMap::value_type(flag.Name(), &flag)); if (ins.second == false) { // means the name was already in the map - CommandLineFlag* old_flag = ins.first->second; - if (flag->IsRetired() != old_flag->IsRetired()) { + CommandLineFlag& old_flag = *ins.first->second; + if (flag.IsRetired() != old_flag.IsRetired()) { // All registrations must agree on the 'retired' flag. flags_internal::ReportUsageError( absl::StrCat( - "Retired flag '", flag->Name(), - "' was defined normally in file '", - (flag->IsRetired() ? old_flag->Filename() : flag->Filename()), - "'."), + "Retired flag '", flag.Name(), "' was defined normally in file '", + (flag.IsRetired() ? old_flag.Filename() : flag.Filename()), "'."), true); - } else if (flags_internal::PrivateHandleAccessor::TypeId(*flag) != - flags_internal::PrivateHandleAccessor::TypeId(*old_flag)) { + } else if (flags_internal::PrivateHandleAccessor::TypeId(flag) != + flags_internal::PrivateHandleAccessor::TypeId(old_flag)) { flags_internal::ReportUsageError( - absl::StrCat("Flag '", flag->Name(), + absl::StrCat("Flag '", flag.Name(), "' was defined more than once but with " "differing types. Defined in files '", - old_flag->Filename(), "' and '", flag->Filename(), "'."), + old_flag.Filename(), "' and '", flag.Filename(), "'."), true); - } else if (old_flag->IsRetired()) { + } else if (old_flag.IsRetired()) { // Retired flag can just be deleted. DestroyRetiredFlag(flag); return; - } else if (old_flag->Filename() != flag->Filename()) { + } else if (old_flag.Filename() != flag.Filename()) { flags_internal::ReportUsageError( - absl::StrCat("Flag '", flag->Name(), + absl::StrCat("Flag '", flag.Name(), "' was defined more than once (in files '", - old_flag->Filename(), "' and '", flag->Filename(), - "')."), + old_flag.Filename(), "' and '", flag.Filename(), "')."), true); } else { flags_internal::ReportUsageError( absl::StrCat( - "Something wrong with flag '", flag->Name(), "' in file '", - flag->Filename(), "'. One possibility: file '", flag->Filename(), + "Something wrong with flag '", flag.Name(), "' in file '", + flag.Filename(), "'. One possibility: file '", flag.Filename(), "' is being linked both statically and dynamically into this " "executable. e.g. some files listed as srcs to a test and also " "listed as srcs of some shared lib deps of the same test."), @@ -206,7 +203,7 @@ class FlagSaverImpl { // It's an error to call this more than once. void SaveFromRegistry() { assert(backup_registry_.empty()); // call only once! - flags_internal::ForEachFlag([&](CommandLineFlag* flag) { + flags_internal::ForEachFlag([&](CommandLineFlag& flag) { if (auto flag_state = flags_internal::PrivateHandleAccessor::SaveState(flag)) { backup_registry_.emplace_back(std::move(flag_state)); @@ -244,39 +241,39 @@ FlagSaver::~FlagSaver() { CommandLineFlag* FindCommandLineFlag(absl::string_view name) { if (name.empty()) return nullptr; - FlagRegistry* const registry = FlagRegistry::GlobalRegistry(); + FlagRegistry& registry = FlagRegistry::GlobalRegistry(); FlagRegistryLock frl(registry); - return registry->FindFlagLocked(name); + return registry.FindFlagLocked(name); } CommandLineFlag* FindRetiredFlag(absl::string_view name) { - FlagRegistry* const registry = FlagRegistry::GlobalRegistry(); + FlagRegistry& registry = FlagRegistry::GlobalRegistry(); FlagRegistryLock frl(registry); - return registry->FindRetiredFlagLocked(name); + return registry.FindRetiredFlagLocked(name); } // -------------------------------------------------------------------- -void ForEachFlagUnlocked(std::function visitor) { - FlagRegistry* const registry = FlagRegistry::GlobalRegistry(); - for (FlagRegistry::FlagConstIterator i = registry->flags_.begin(); - i != registry->flags_.end(); ++i) { - visitor(i->second); +void ForEachFlagUnlocked(std::function visitor) { + FlagRegistry& registry = FlagRegistry::GlobalRegistry(); + for (FlagRegistry::FlagConstIterator i = registry.flags_.begin(); + i != registry.flags_.end(); ++i) { + visitor(*i->second); } } -void ForEachFlag(std::function visitor) { - FlagRegistry* const registry = FlagRegistry::GlobalRegistry(); +void ForEachFlag(std::function visitor) { + FlagRegistry& registry = FlagRegistry::GlobalRegistry(); FlagRegistryLock frl(registry); ForEachFlagUnlocked(visitor); } // -------------------------------------------------------------------- -bool RegisterCommandLineFlag(CommandLineFlag* flag) { - FlagRegistry::GlobalRegistry()->RegisterFlag(flag); +bool RegisterCommandLineFlag(CommandLineFlag& flag) { + FlagRegistry::GlobalRegistry().RegisterFlag(flag); return true; } @@ -307,7 +304,7 @@ class RetiredFlagObj final : public CommandLineFlag { } bool ParseFrom(absl::string_view, flags_internal::FlagSettingMode, - flags_internal::ValueSource, std::string*) override { + flags_internal::ValueSource, std::string&) override { return false; } @@ -320,16 +317,16 @@ class RetiredFlagObj final : public CommandLineFlag { const FlagFastTypeId type_id_; }; -void DestroyRetiredFlag(CommandLineFlag* flag) { - assert(flag->IsRetired()); - delete static_cast(flag); +void DestroyRetiredFlag(CommandLineFlag& flag) { + assert(flag.IsRetired()); + delete static_cast(&flag); } } // namespace bool Retire(const char* name, FlagFastTypeId type_id) { auto* flag = new flags_internal::RetiredFlagObj(name, type_id); - FlagRegistry::GlobalRegistry()->RegisterFlag(flag); + FlagRegistry::GlobalRegistry().RegisterFlag(*flag); return true; } diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h index f722fd44..a118865a 100644 --- a/absl/flags/internal/registry.h +++ b/absl/flags/internal/registry.h @@ -39,14 +39,14 @@ CommandLineFlag* FindRetiredFlag(absl::string_view name); // Executes specified visitor for each non-retired flag in the registry. // Requires the caller hold the registry lock. -void ForEachFlagUnlocked(std::function visitor); +void ForEachFlagUnlocked(std::function visitor); // Executes specified visitor for each non-retired flag in the registry. While // callback are executed, the registry is locked and can't be changed. -void ForEachFlag(std::function visitor); +void ForEachFlag(std::function visitor); //----------------------------------------------------------------------------- -bool RegisterCommandLineFlag(CommandLineFlag*); +bool RegisterCommandLineFlag(CommandLineFlag&); //----------------------------------------------------------------------------- // Retired registrations: diff --git a/absl/flags/internal/type_erased.cc b/absl/flags/internal/type_erased.cc index 35b0d125..b2523b24 100644 --- a/absl/flags/internal/type_erased.cc +++ b/absl/flags/internal/type_erased.cc @@ -58,7 +58,7 @@ bool SetCommandLineOptionWithMode(absl::string_view name, std::string error; if (!flags_internal::PrivateHandleAccessor::ParseFrom( - flag, value, set_mode, kProgrammaticChange, &error)) { + *flag, value, set_mode, kProgrammaticChange, error)) { // Errors here are all of the form: the provided name was a recognized // flag, but the value was invalid (bad type, or validation failed). flags_internal::ReportUsageError(error, false); diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc index 11664e10..2a2231a7 100644 --- a/absl/flags/internal/usage.cc +++ b/absl/flags/internal/usage.cc @@ -107,8 +107,8 @@ class FlagHelpPrettyPrinter { public: // Pretty printer holds on to the std::ostream& reference to direct an output // to that stream. - FlagHelpPrettyPrinter(int max_line_len, std::ostream* out) - : out_(*out), + FlagHelpPrettyPrinter(int max_line_len, std::ostream& out) + : out_(out), max_line_len_(max_line_len), line_len_(0), first_line_(true) {} @@ -182,7 +182,7 @@ class FlagHelpPrettyPrinter { bool first_line_; }; -void FlagHelpHumanReadable(const CommandLineFlag& flag, std::ostream* out) { +void FlagHelpHumanReadable(const CommandLineFlag& flag, std::ostream& out) { FlagHelpPrettyPrinter printer(80, out); // Max line length is 80. // Flag name. @@ -244,29 +244,28 @@ void FlagsHelpImpl(std::ostream& out, flags_internal::FlagKindFilter filter_cb, // This map is used to output matching flags grouped by package and file // name. std::map>> + std::map>> matching_flags; - flags_internal::ForEachFlag([&](CommandLineFlag* flag) { - std::string flag_filename = flag->Filename(); + flags_internal::ForEachFlag([&](absl::CommandLineFlag& flag) { + std::string flag_filename = flag.Filename(); // Ignore retired flags. - if (flag->IsRetired()) return; + if (flag.IsRetired()) return; // If the flag has been stripped, pretend that it doesn't exist. - if (flag->Help() == flags_internal::kStrippedFlagHelp) return; + if (flag.Help() == flags_internal::kStrippedFlagHelp) return; // Make sure flag satisfies the filter if (!filter_cb || !filter_cb(flag_filename)) return; matching_flags[std::string(flags_internal::Package(flag_filename))] [flag_filename] - .push_back(flag); + .push_back(&flag); }); - absl::string_view - package_separator; // controls blank lines between packages. - absl::string_view file_separator; // controls blank lines between files. + absl::string_view package_separator; // controls blank lines between packages + absl::string_view file_separator; // controls blank lines between files for (const auto& package : matching_flags) { if (format == HelpFormat::kHumanReadable) { out << package_separator; @@ -304,7 +303,7 @@ void FlagsHelpImpl(std::ostream& out, flags_internal::FlagKindFilter filter_cb, void FlagHelp(std::ostream& out, const CommandLineFlag& flag, HelpFormat format) { if (format == HelpFormat::kHumanReadable) - flags_internal::FlagHelpHumanReadable(flag, &out); + flags_internal::FlagHelpHumanReadable(flag, out); } // -------------------------------------------------------------------- diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc index 09baae88..81f9cebd 100644 --- a/absl/flags/marshalling.cc +++ b/absl/flags/marshalling.cc @@ -74,15 +74,16 @@ static int NumericBase(absl::string_view text) { } template -inline bool ParseFlagImpl(absl::string_view text, IntType* dst) { +inline bool ParseFlagImpl(absl::string_view text, IntType& dst) { text = absl::StripAsciiWhitespace(text); - return absl::numbers_internal::safe_strtoi_base(text, dst, NumericBase(text)); + return absl::numbers_internal::safe_strtoi_base(text, &dst, + NumericBase(text)); } bool AbslParseFlag(absl::string_view text, short* dst, std::string*) { int val; - if (!ParseFlagImpl(text, &val)) return false; + if (!ParseFlagImpl(text, val)) return false; if (static_cast(val) != val) // worked, but number out of range return false; *dst = static_cast(val); @@ -91,7 +92,7 @@ bool AbslParseFlag(absl::string_view text, short* dst, std::string*) { bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) { unsigned int val; - if (!ParseFlagImpl(text, &val)) return false; + if (!ParseFlagImpl(text, val)) return false; if (static_cast(val) != val) // worked, but number out of range return false; @@ -100,28 +101,28 @@ bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) { } bool AbslParseFlag(absl::string_view text, int* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } bool AbslParseFlag(absl::string_view text, long* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } bool AbslParseFlag(absl::string_view text, unsigned long long* dst, std::string*) { - return ParseFlagImpl(text, dst); + return ParseFlagImpl(text, *dst); } // -------------------------------------------------------------------- diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 3f0a7a75..15300786 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -222,7 +222,7 @@ bool ArgsList::ReadFromFlagfile(const std::string& flag_file_name) { // Reads the environment variable with name `name` and stores results in // `value`. If variable is not present in environment returns false, otherwise // returns true. -bool GetEnvVar(const char* var_name, std::string* var_value) { +bool GetEnvVar(const char* var_name, std::string& var_value) { #ifdef _WIN32 char buf[1024]; auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf)); @@ -234,14 +234,14 @@ bool GetEnvVar(const char* var_name, std::string* var_value) { return false; } - *var_value = std::string(buf, get_res); + var_value = std::string(buf, get_res); #else const char* val = ::getenv(var_name); if (val == nullptr) { return false; } - *var_value = val; + var_value = val; #endif return true; @@ -306,17 +306,17 @@ std::tuple LocateFlag(absl::string_view flag_name) { // back. void CheckDefaultValuesParsingRoundtrip() { #ifndef NDEBUG - flags_internal::ForEachFlag([&](CommandLineFlag* flag) { - if (flag->IsRetired()) return; + flags_internal::ForEachFlag([&](CommandLineFlag& flag) { + if (flag.IsRetired()) return; #define ABSL_FLAGS_INTERNAL_IGNORE_TYPE(T, _) \ - if (flag->IsOfType()) return; + if (flag.IsOfType()) return; ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(ABSL_FLAGS_INTERNAL_IGNORE_TYPE) #undef ABSL_FLAGS_INTERNAL_IGNORE_TYPE flags_internal::PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip( - *flag); + flag); }); #endif } @@ -329,13 +329,13 @@ void CheckDefaultValuesParsingRoundtrip() { // the first flagfile in the input list are processed before the second flagfile // etc. bool ReadFlagfiles(const std::vector& flagfiles, - std::vector* input_args) { + std::vector& input_args) { bool success = true; for (auto it = flagfiles.rbegin(); it != flagfiles.rend(); ++it) { ArgsList al; if (al.ReadFromFlagfile(*it)) { - input_args->push_back(al); + input_args.push_back(al); } else { success = false; } @@ -350,7 +350,7 @@ bool ReadFlagfiles(const std::vector& flagfiles, // `flag_name` is a string from the input flag_names list. If successful we // append a single ArgList at the end of the input_args. bool ReadFlagsFromEnv(const std::vector& flag_names, - std::vector* input_args, + std::vector& input_args, bool fail_on_absent_in_env) { bool success = true; std::vector args; @@ -371,7 +371,7 @@ bool ReadFlagsFromEnv(const std::vector& flag_names, const std::string envname = absl::StrCat("FLAGS_", flag_name); std::string envval; - if (!GetEnvVar(envname.c_str(), &envval)) { + if (!GetEnvVar(envname.c_str(), envval)) { if (fail_on_absent_in_env) { flags_internal::ReportUsageError( absl::StrCat(envname, " not found in environment"), true); @@ -386,7 +386,7 @@ bool ReadFlagsFromEnv(const std::vector& flag_names, } if (success) { - input_args->emplace_back(args); + input_args.emplace_back(args); } return success; @@ -396,8 +396,8 @@ bool ReadFlagsFromEnv(const std::vector& flag_names, // Returns success status, which is true if were able to handle all generator // flags (flagfile, fromenv, tryfromemv) successfully. -bool HandleGeneratorFlags(std::vector* input_args, - std::vector* flagfile_value) { +bool HandleGeneratorFlags(std::vector& input_args, + std::vector& flagfile_value) { bool success = true; absl::MutexLock l(&flags_internal::processing_checks_guard); @@ -422,9 +422,9 @@ bool HandleGeneratorFlags(std::vector* input_args, if (flags_internal::flagfile_needs_processing) { auto flagfiles = absl::GetFlag(FLAGS_flagfile); - if (input_args->size() == 1) { - flagfile_value->insert(flagfile_value->end(), flagfiles.begin(), - flagfiles.end()); + if (input_args.size() == 1) { + flagfile_value.insert(flagfile_value.end(), flagfiles.begin(), + flagfiles.end()); } success &= ReadFlagfiles(flagfiles, input_args); @@ -647,7 +647,7 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], bool success = true; while (!input_args.empty()) { // 10. First we process the built-in generator flags. - success &= HandleGeneratorFlags(&input_args, &flagfile_value); + success &= HandleGeneratorFlags(input_args, flagfile_value); // 30. Select top-most (most recent) arguments list. If it is empty drop it // and re-try. @@ -733,7 +733,7 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], std::string error; if (!flags_internal::PrivateHandleAccessor::ParseFrom( - flag, value, SET_FLAGS_VALUE, kCommandLine, &error)) { + *flag, value, SET_FLAGS_VALUE, kCommandLine, error)) { flags_internal::ReportUsageError(error, true); success = false; } else { diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc index e6a53ae6..aea068ee 100644 --- a/absl/flags/parse_test.cc +++ b/absl/flags/parse_test.cc @@ -171,8 +171,8 @@ constexpr const char* const ff2_data[] = { // temporary directory location. This way we can test inclusion of one flagfile // from another flagfile. const char* GetFlagfileFlag(const std::vector& ffd, - std::string* flagfile_flag) { - *flagfile_flag = "--flagfile="; + std::string& flagfile_flag) { + flagfile_flag = "--flagfile="; absl::string_view separator; for (const auto& flagfile_data : ffd) { std::string flagfile_name = @@ -183,11 +183,11 @@ const char* GetFlagfileFlag(const std::vector& ffd, flagfile_out << absl::Substitute(line, GetTestTempDir()) << "\n"; } - absl::StrAppend(flagfile_flag, separator, flagfile_name); + absl::StrAppend(&flagfile_flag, separator, flagfile_name); separator = ","; } - return flagfile_flag->c_str(); + return flagfile_flag.c_str(); } } // namespace @@ -588,14 +588,14 @@ TEST_F(ParseTest, TestSimpleValidFlagfile) { const char* in_args1[] = { "testbin", GetFlagfileFlag({{"parse_test.ff1", absl::MakeConstSpan(ff1_data)}}, - &flagfile_flag), + flagfile_flag), }; TestParse(in_args1, -1, 0.1, "q2w2 ", true); const char* in_args2[] = { "testbin", GetFlagfileFlag({{"parse_test.ff2", absl::MakeConstSpan(ff2_data)}}, - &flagfile_flag), + flagfile_flag), }; TestParse(in_args2, 100, 0.1, "q2w2 ", false); } @@ -609,7 +609,7 @@ TEST_F(ParseTest, TestValidMultiFlagfile) { "testbin", GetFlagfileFlag({{"parse_test.ff2", absl::MakeConstSpan(ff2_data)}, {"parse_test.ff1", absl::MakeConstSpan(ff1_data)}}, - &flagfile_flag), + flagfile_flag), }; TestParse(in_args1, -1, 0.1, "q2w2 ", true); } @@ -622,7 +622,7 @@ TEST_F(ParseTest, TestFlagfileMixedWithRegularFlags) { const char* in_args1[] = { "testbin", "--int_flag=3", GetFlagfileFlag({{"parse_test.ff1", absl::MakeConstSpan(ff1_data)}}, - &flagfile_flag), + flagfile_flag), "-double_flag=0.2"}; TestParse(in_args1, -1, 0.2, "q2w2 ", true); } @@ -640,7 +640,7 @@ TEST_F(ParseTest, TestFlagfileInFlagfile) { const char* in_args1[] = { "testbin", GetFlagfileFlag({{"parse_test.ff3", absl::MakeConstSpan(ff3_data)}}, - &flagfile_flag), + flagfile_flag), }; TestParse(in_args1, 100, 0.1, "q2w2 ", false); } @@ -657,7 +657,7 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) { const char* in_args1[] = { "testbin", GetFlagfileFlag({{"parse_test.ff4", - absl::MakeConstSpan(ff4_data)}}, &flagfile_flag), + absl::MakeConstSpan(ff4_data)}}, flagfile_flag), }; EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args1), "Unknown command line flag 'unknown_flag'"); @@ -669,7 +669,7 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) { const char* in_args2[] = { "testbin", GetFlagfileFlag({{"parse_test.ff5", - absl::MakeConstSpan(ff5_data)}}, &flagfile_flag), + absl::MakeConstSpan(ff5_data)}}, flagfile_flag), }; EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args2), "Unknown command line flag 'int_flag 10'"); @@ -681,7 +681,7 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) { const char* in_args3[] = { "testbin", GetFlagfileFlag({{"parse_test.ff6", absl::MakeConstSpan(ff6_data)}}, - &flagfile_flag), + flagfile_flag), }; EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args3), "Flagfile can't contain position arguments or --"); @@ -702,7 +702,7 @@ TEST_F(ParseDeathTest, TestInvalidFlagfiles) { const char* in_args5[] = { "testbin", GetFlagfileFlag({{"parse_test.ff7", absl::MakeConstSpan(ff7_data)}}, - &flagfile_flag), + flagfile_flag), }; EXPECT_DEATH_IF_SUPPORTED(InvokeParse(in_args5), "Unexpected line in the flagfile .*: \\*bin\\*"); diff --git a/absl/random/BUILD.bazel b/absl/random/BUILD.bazel index 9ba75b52..4b804c86 100644 --- a/absl/random/BUILD.bazel +++ b/absl/random/BUILD.bazel @@ -69,7 +69,7 @@ cc_library( "//absl/base:config", "//absl/base:core_headers", "//absl/meta:type_traits", - "//absl/random/internal:distributions", + "//absl/random/internal:distribution_caller", "//absl/random/internal:fast_uniform_bits", "//absl/random/internal:fastmath", "//absl/random/internal:generate_real", @@ -78,7 +78,6 @@ cc_library( "//absl/random/internal:uniform_helper", "//absl/random/internal:wide_multiply", "//absl/strings", - "//absl/types:span", ], ) diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index ec616dd9..85a2ea37 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt @@ -183,7 +183,7 @@ absl_cc_library( absl::config absl::core_headers absl::random_internal_generate_real - absl::random_internal_distributions + absl::random_internal_distribution_caller absl::random_internal_fast_uniform_bits absl::random_internal_fastmath absl::random_internal_iostream_state_saver @@ -191,7 +191,6 @@ absl_cc_library( absl::random_internal_uniform_helper absl::random_internal_wide_multiply absl::strings - absl::span absl::type_traits ) @@ -536,27 +535,6 @@ absl_cc_library( absl::config ) -# Internal-only target, do not depend on directly. -absl_cc_library( - NAME - random_internal_distributions - HDRS - "internal/distributions.h" - COPTS - ${ABSL_DEFAULT_COPTS} - LINKOPTS - ${ABSL_DEFAULT_LINKOPTS} - DEPS - absl::random_internal_distribution_caller - absl::random_internal_fast_uniform_bits - absl::random_internal_fastmath - absl::random_internal_traits - absl::random_internal_uniform_helper - absl::span - absl::strings - absl::type_traits -) - # Internal-only target, do not depend on directly. absl_cc_library( NAME @@ -745,7 +723,6 @@ absl_cc_library( absl::random_internal_salted_seed_seq absl::random_internal_seed_material absl::span - absl::strings absl::type_traits ) @@ -1174,9 +1151,7 @@ absl_cc_library( LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS - absl::core_headers - absl::random_internal_fast_uniform_bits - absl::random_internal_iostream_state_saver + absl::config absl::random_internal_traits absl::type_traits ) diff --git a/absl/random/distributions.h b/absl/random/distributions.h index 8680f6a6..6775c5d6 100644 --- a/absl/random/distributions.h +++ b/absl/random/distributions.h @@ -57,7 +57,7 @@ #include "absl/random/beta_distribution.h" #include "absl/random/exponential_distribution.h" #include "absl/random/gaussian_distribution.h" -#include "absl/random/internal/distributions.h" // IWYU pragma: export +#include "absl/random/internal/distribution_caller.h" // IWYU pragma: export #include "absl/random/internal/uniform_helper.h" // IWYU pragma: export #include "absl/random/log_uniform_int_distribution.h" #include "absl/random/poisson_distribution.h" diff --git a/absl/random/internal/BUILD.bazel b/absl/random/internal/BUILD.bazel index 85d1fb81..813d926e 100644 --- a/absl/random/internal/BUILD.bazel +++ b/absl/random/internal/BUILD.bazel @@ -48,21 +48,6 @@ cc_library( deps = ["//absl/base:config"], ) -cc_library( - name = "distributions", - hdrs = ["distributions.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":distribution_caller", - ":traits", - ":uniform_helper", - "//absl/base", - "//absl/meta:type_traits", - "//absl/strings", - ], -) - cc_library( name = "fast_uniform_bits", hdrs = [ @@ -221,7 +206,6 @@ cc_library( ":seed_material", "//absl/base:core_headers", "//absl/meta:type_traits", - "//absl/strings", "//absl/types:optional", "//absl/types:span", ], @@ -672,6 +656,8 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + ":traits", + "//absl/base:config", "//absl/meta:type_traits", ], ) diff --git a/absl/random/internal/distributions.h b/absl/random/internal/distributions.h deleted file mode 100644 index d7e3c016..00000000 --- a/absl/random/internal/distributions.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2019 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ -#define ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ - -#include - -#include "absl/meta/type_traits.h" -#include "absl/random/internal/distribution_caller.h" -#include "absl/random/internal/traits.h" -#include "absl/random/internal/uniform_helper.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace random_internal { - -// In the absence of an explicitly provided return-type, the template -// "uniform_inferred_return_t" is used to derive a suitable type, based on -// the data-types of the endpoint-arguments {A lo, B hi}. -// -// Given endpoints {A lo, B hi}, one of {A, B} will be chosen as the -// return-type, if one type can be implicitly converted into the other, in a -// lossless way. The template "is_widening_convertible" implements the -// compile-time logic for deciding if such a conversion is possible. -// -// If no such conversion between {A, B} exists, then the overload for -// absl::Uniform() will be discarded, and the call will be ill-formed. -// Return-type for absl::Uniform() when the return-type is inferred. -template -using uniform_inferred_return_t = - absl::enable_if_t, - is_widening_convertible>::value, - typename std::conditional< - is_widening_convertible::value, B, A>::type>; - -} // namespace random_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ diff --git a/absl/random/internal/uniform_helper.h b/absl/random/internal/uniform_helper.h index 663107cb..5b2afecb 100644 --- a/absl/random/internal/uniform_helper.h +++ b/absl/random/internal/uniform_helper.h @@ -19,10 +19,13 @@ #include #include +#include "absl/base/config.h" #include "absl/meta/type_traits.h" +#include "absl/random/internal/traits.h" namespace absl { ABSL_NAMESPACE_BEGIN + template class uniform_int_distribution; @@ -58,6 +61,26 @@ struct IntervalOpenOpenTag : public random_internal::TagTypeCompare {}; namespace random_internal { + +// In the absence of an explicitly provided return-type, the template +// "uniform_inferred_return_t" is used to derive a suitable type, based on +// the data-types of the endpoint-arguments {A lo, B hi}. +// +// Given endpoints {A lo, B hi}, one of {A, B} will be chosen as the +// return-type, if one type can be implicitly converted into the other, in a +// lossless way. The template "is_widening_convertible" implements the +// compile-time logic for deciding if such a conversion is possible. +// +// If no such conversion between {A, B} exists, then the overload for +// absl::Uniform() will be discarded, and the call will be ill-formed. +// Return-type for absl::Uniform() when the return-type is inferred. +template +using uniform_inferred_return_t = + absl::enable_if_t, + is_widening_convertible>::value, + typename std::conditional< + is_widening_convertible::value, B, A>::type>; + // The functions // uniform_lower_bound(tag, a, b) // and @@ -149,12 +172,19 @@ uniform_upper_bound(Tag, FloatType, FloatType b) { return std::nextafter(b, (std::numeric_limits::max)()); } +// UniformDistribution selects either absl::uniform_int_distribution +// or absl::uniform_real_distribution depending on the NumType parameter. template using UniformDistribution = typename std::conditional::value, absl::uniform_int_distribution, absl::uniform_real_distribution>::type; +// UniformDistributionWrapper is used as the underlying distribution type +// by the absl::Uniform template function. It selects the proper Abseil +// uniform distribution and provides constructor overloads that match the +// expected parameter order as well as adjusting distribtuion bounds based +// on the tag. template struct UniformDistributionWrapper : public UniformDistribution { template diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc index 1ddd6aec..68f53987 100644 --- a/absl/strings/cord.cc +++ b/absl/strings/cord.cc @@ -705,6 +705,37 @@ Cord::Cord(absl::string_view src) { } } +template > +Cord::Cord(T&& src) { + if ( + // String is short: copy data to avoid external block overhead. + src.size() <= kMaxBytesToCopy || + // String is wasteful: copy data to avoid pinning too much unused memory. + src.size() < src.capacity() / 2 + ) { + if (src.size() <= InlineRep::kMaxInline) { + contents_.set_data(src.data(), src.size(), false); + } else { + contents_.set_tree(NewTree(src.data(), src.size(), 0)); + } + } else { + struct StringReleaser { + void operator()(absl::string_view /* data */) {} + std::string data; + }; + const absl::string_view original_data = src; + CordRepExternal* rep = + static_cast(absl::cord_internal::NewExternalRep( + original_data, StringReleaser{std::move(src)})); + // Moving src may have invalidated its data pointer, so adjust it. + rep->base = + static_cast(GetExternalReleaser(rep))->data.data(); + contents_.set_tree(rep); + } +} + +template Cord::Cord(std::string&& src); + // The destruction code is separate so that the compiler can determine // that it does not need to call the destructor on a moved-from Cord. void Cord::DestroyCordSlow() { @@ -742,6 +773,18 @@ Cord& Cord::operator=(absl::string_view src) { return *this; } +template > +Cord& Cord::operator=(T&& src) { + if (src.size() <= kMaxBytesToCopy) { + *this = absl::string_view(src); + } else { + *this = Cord(std::move(src)); + } + return *this; +} + +template Cord& Cord::operator=(std::string&& src); + // TODO(sanjay): Move to Cord::InlineRep section of file. For now, // we keep it here to make diffs easier. void Cord::InlineRep::AppendArray(const char* src_data, size_t src_size) { @@ -853,6 +896,17 @@ void Cord::Append(const Cord& src) { AppendImpl(src); } void Cord::Append(Cord&& src) { AppendImpl(std::move(src)); } +template > +void Cord::Append(T&& src) { + if (src.size() <= kMaxBytesToCopy) { + Append(absl::string_view(src)); + } else { + Append(Cord(std::move(src))); + } +} + +template void Cord::Append(std::string&& src); + void Cord::Prepend(const Cord& src) { CordRep* src_tree = src.contents_.tree(); if (src_tree != nullptr) { @@ -882,6 +936,17 @@ void Cord::Prepend(absl::string_view src) { } } +template > +inline void Cord::Prepend(T&& src) { + if (src.size() <= kMaxBytesToCopy) { + Prepend(absl::string_view(src)); + } else { + Prepend(Cord(std::move(src))); + } +} + +template void Cord::Prepend(std::string&& src); + static CordRep* RemovePrefixFrom(CordRep* node, size_t n) { if (n >= node->length) return nullptr; if (n == 0) return Ref(node); diff --git a/absl/strings/cord.h b/absl/strings/cord.h index 3be8d7d8..dc987454 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -147,11 +147,8 @@ class Cord { // Creates a Cord from a `std::string&&` rvalue. These constructors are // templated to avoid ambiguities for types that are convertible to both // `absl::string_view` and `std::string`, such as `const char*`. - // - // Note that these functions reserve the right to use the `string&&`'s - // memory and that they will do so in the future. template = 0> - explicit Cord(T&& src) : Cord(absl::string_view(src)) {} + explicit Cord(T&& src); template = 0> Cord& operator=(T&& src); @@ -1048,11 +1045,8 @@ inline Cord& Cord::operator=(Cord&& x) noexcept { return *this; } -template > -inline Cord& Cord::operator=(T&& src) { - *this = absl::string_view(src); - return *this; -} +extern template Cord::Cord(std::string&& src); +extern template Cord& Cord::operator=(std::string&& src); inline size_t Cord::size() const { // Length is 1st field in str.rep_ @@ -1098,19 +1092,8 @@ inline void Cord::Append(absl::string_view src) { contents_.AppendArray(src.data(), src.size()); } -template > -inline void Cord::Append(T&& src) { - // Note that this function reserves the right to reuse the `string&&`'s - // memory and that it will do so in the future. - Append(absl::string_view(src)); -} - -template > -inline void Cord::Prepend(T&& src) { - // Note that this function reserves the right to reuse the `string&&`'s - // memory and that it will do so in the future. - Prepend(absl::string_view(src)); -} +extern template void Cord::Append(std::string&& src); +extern template void Cord::Prepend(std::string&& src); inline int Cord::Compare(const Cord& rhs) const { if (!contents_.is_tree() && !rhs.contents_.is_tree()) { diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 49a68849..22cfef66 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -8,6 +8,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/strings/cord.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" @@ -353,6 +354,7 @@ TEST(StrFormat, BehavesAsDocumented) { EXPECT_EQ(StrFormat("%s", "C"), "C"); EXPECT_EQ(StrFormat("%s", std::string("C++")), "C++"); EXPECT_EQ(StrFormat("%s", string_view("view")), "view"); + EXPECT_EQ(StrFormat("%s", absl::Cord("cord")), "cord"); // Integral Conversion // These format integral types: char, int, long, uint64_t, etc. EXPECT_EQ(StrFormat("%d", char{10}), "10"); diff --git a/absl/time/internal/cctz/include/cctz/time_zone.h b/absl/time/internal/cctz/include/cctz/time_zone.h index d4ea90ef..b33e0c0a 100644 --- a/absl/time/internal/cctz/include/cctz/time_zone.h +++ b/absl/time/internal/cctz/include/cctz/time_zone.h @@ -292,6 +292,7 @@ bool parse(const std::string&, const std::string&, const time_zone&, // - %E#f - Fractional seconds with # digits of precision // - %E*f - Fractional seconds with full precision (a literal '*') // - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) +// - %ET - The RFC3339 "date-time" separator "T" // // Note that %E0S behaves like %S, and %E0f produces no characters. In // contrast %E*f always produces at least one digit, which may be '0'. @@ -321,7 +322,7 @@ inline std::string format(const std::string& fmt, const time_point& tp, // returns the corresponding time_point. Uses strftime()-like formatting // options, with the same extensions as cctz::format(), but with the // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez -// and %E*z also accept the same inputs. +// and %E*z also accept the same inputs. %ET accepts either 'T' or 't'. // // %Y consumes as many numeric characters as it can, so the matching data // should always be terminated with a non-numeric. %E4Y always consumes diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc index a402760d..4e39188f 100644 --- a/absl/time/internal/cctz/src/cctz_benchmark.cc +++ b/absl/time/internal/cctz/src/cctz_benchmark.cc @@ -97,8 +97,8 @@ void BM_PrevWeekday(benchmark::State& state) { } BENCHMARK(BM_PrevWeekday); -const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez"; -const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez"; +const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez"; +const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez"; const char RFC1123_full[] = "%a, %d %b %Y %H:%M:%S %z"; const char RFC1123_no_wday[] = "%d %b %Y %H:%M:%S %z"; @@ -991,12 +991,12 @@ void BM_Time_FromCivilDay0_Libc(benchmark::State& state) { BENCHMARK(BM_Time_FromCivilDay0_Libc); const char* const kFormats[] = { - RFC1123_full, // 0 - RFC1123_no_wday, // 1 - RFC3339_full, // 2 - RFC3339_sec, // 3 - "%Y-%m-%dT%H:%M:%S", // 4 - "%Y-%m-%d", // 5 + RFC1123_full, // 0 + RFC1123_no_wday, // 1 + RFC3339_full, // 2 + RFC3339_sec, // 3 + "%Y-%m-%d%ET%H:%M:%S", // 4 + "%Y-%m-%d", // 5 }; const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); diff --git a/absl/time/internal/cctz/src/time_zone_format.cc b/absl/time/internal/cctz/src/time_zone_format.cc index 179975e0..a4428632 100644 --- a/absl/time/internal/cctz/src/time_zone_format.cc +++ b/absl/time/internal/cctz/src/time_zone_format.cc @@ -290,6 +290,7 @@ const std::int_fast64_t kExp10[kDigits10_64 + 1] = { // - %E#S - Seconds with # digits of fractional precision // - %E*S - Seconds with full fractional precision (a literal '*') // - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) +// - %ET - The RFC3339 "date-time" separator "T" // // The standard specifiers from RFC3339_* (%Y, %m, %d, %H, %M, and %S) are // handled internally for performance reasons. strftime(3) is slow due to @@ -448,7 +449,14 @@ std::string format(const std::string& format, const time_point& tp, if (*cur != 'E' || ++cur == end) continue; // Format our extensions. - if (*cur == 'z') { + if (*cur == 'T') { + // Formats %ET. + if (cur - 2 != pending) { + FormatTM(&result, std::string(pending, cur - 2), tm); + } + result.append("T"); + pending = ++cur; + } else if (*cur == 'z') { // Formats %Ez. if (cur - 2 != pending) { FormatTM(&result, std::string(pending, cur - 2), tm); @@ -551,7 +559,7 @@ const char* ParseOffset(const char* dp, const char* mode, int* offset) { } else { dp = nullptr; } - } else if (first == 'Z') { // Zulu + } else if (first == 'Z' || first == 'z') { // Zulu *offset = 0; } else { dp = nullptr; @@ -607,7 +615,7 @@ const char* ParseTM(const char* dp, const char* fmt, std::tm* tm) { // Uses strptime(3) to parse the given input. Supports the same extended // format specifiers as format(), although %E#S and %E*S are treated // identically (and similarly for %E#f and %E*f). %Ez and %E*z also accept -// the same inputs. +// the same inputs. %ET accepts either 'T' or 't'. // // The standard specifiers from RFC3339_* (%Y, %m, %d, %H, %M, and %S) are // handled internally so that we can normally avoid strptime() altogether @@ -742,6 +750,15 @@ bool parse(const std::string& format, const std::string& input, data = (*data == '%' ? data + 1 : nullptr); continue; case 'E': + if (fmt[0] == 'T') { + if (*data == 'T' || *data == 't') { + ++data; + ++fmt; + } else { + data = nullptr; + } + continue; + } if (fmt[0] == 'z' || (fmt[0] == '*' && fmt[1] == 'z')) { data = ParseOffset(data, ":", &offset); if (data != nullptr) saw_offset = true; diff --git a/absl/time/internal/cctz/src/time_zone_format_test.cc b/absl/time/internal/cctz/src/time_zone_format_test.cc index 87382e15..13a4227e 100644 --- a/absl/time/internal/cctz/src/time_zone_format_test.cc +++ b/absl/time/internal/cctz/src/time_zone_format_test.cc @@ -48,8 +48,8 @@ namespace { EXPECT_STREQ(zone, al.abbr); \ } while (0) -const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez"; -const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez"; +const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez"; +const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez"; const char RFC1123_full[] = "%a, %d %b %Y %H:%M:%S %z"; const char RFC1123_no_wday[] = "%d %b %Y %H:%M:%S %z"; @@ -1379,10 +1379,20 @@ TEST(Parse, RFC3339Format) { EXPECT_TRUE(parse(RFC3339_sec, "2014-02-12T20:21:00+00:00", tz, &tp)); ExpectTime(tp, tz, 2014, 2, 12, 20, 21, 0, 0, false, "UTC"); - // Check that %Ez also accepts "Z" as a synonym for "+00:00". + // Check that %ET also accepts "t". time_point tp2; - EXPECT_TRUE(parse(RFC3339_sec, "2014-02-12T20:21:00Z", tz, &tp2)); + EXPECT_TRUE(parse(RFC3339_sec, "2014-02-12t20:21:00+00:00", tz, &tp2)); EXPECT_EQ(tp, tp2); + + // Check that %Ez also accepts "Z" as a synonym for "+00:00". + time_point tp3; + EXPECT_TRUE(parse(RFC3339_sec, "2014-02-12T20:21:00Z", tz, &tp3)); + EXPECT_EQ(tp, tp3); + + // Check that %Ez also accepts "z" as a synonym for "+00:00". + time_point tp4; + EXPECT_TRUE(parse(RFC3339_sec, "2014-02-12T20:21:00z", tz, &tp4)); + EXPECT_EQ(tp, tp4); } TEST(Parse, MaxRange) { diff --git a/absl/time/internal/cctz/src/time_zone_lookup_test.cc b/absl/time/internal/cctz/src/time_zone_lookup_test.cc index 0b0c1a3b..8f7ab154 100644 --- a/absl/time/internal/cctz/src/time_zone_lookup_test.cc +++ b/absl/time/internal/cctz/src/time_zone_lookup_test.cc @@ -933,7 +933,7 @@ TEST(MakeTime, Normalization) { // NOTE: Run this with -ftrapv to detect overflow problems. TEST(MakeTime, SysSecondsLimits) { - const char RFC3339[] = "%Y-%m-%dT%H:%M:%S%Ez"; + const char RFC3339[] = "%Y-%m-%d%ET%H:%M:%S%Ez"; const time_zone utc = utc_time_zone(); const time_zone east = fixed_time_zone(chrono::hours(14)); const time_zone west = fixed_time_zone(-chrono::hours(14)); -- cgit v1.2.3 From 4ccc0fce09836a25b474f4b1453146dae2c29f4d Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 17 Jun 2020 14:16:51 -0700 Subject: Export of internal Abseil changes -- 34c0d521b11ed4191ea3e071a864a84e5e5941b7 by Matthew Brown : Release absl::StrFormat custom type extensions - Allows StrFormat methods to be extended to accept types which implement AbslFormatConvert() - NOLINTNEXTLINE(readability-redundant-declaration) used, declarations are required in some compilers. PiperOrigin-RevId: 316963065 -- 4d475b5ad02d41057447d722ad35573fc4f48d1f by Evan Brown : Small fix to previous change: the first overload of insert_iterator_unique wasn't actually being selected. Fix that issue and add tests to verify that it actually works. Note: couldn't use TestInstanceTracker here because that counts instances (and decrements in destructor) rather than counting all constructor calls. PiperOrigin-RevId: 316927690 GitOrigin-RevId: 34c0d521b11ed4191ea3e071a864a84e5e5941b7 Change-Id: If8bbb8317b93af4084ac4cc55b752b99b1581b58 --- absl/container/btree_test.cc | 50 ++++ absl/container/internal/btree.h | 6 +- absl/strings/internal/str_format/arg.h | 34 ++- absl/strings/internal/str_format/arg_test.cc | 10 + absl/strings/internal/str_format/extension.cc | 23 ++ absl/strings/internal/str_format/extension.h | 6 +- absl/strings/internal/str_format/extension_test.cc | 15 ++ absl/strings/str_format.h | 270 +++++++++++++++++++++ absl/strings/str_format_test.cc | 160 ++++++++---- 9 files changed, 522 insertions(+), 52 deletions(-) (limited to 'absl/strings/str_format_test.cc') diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index 8234b017..1738d291 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -2416,6 +2416,41 @@ TEST(Btree, SetRangeConstructorAndInsertSupportExplicitConversionComparable) { EXPECT_THAT(name_set2, ElementsAreArray(names)); } +// A type that is explicitly convertible from int and counts constructor calls. +struct ConstructorCounted { + explicit ConstructorCounted(int i) : i(i) { ++constructor_calls; } + bool operator==(int other) const { return i == other; } + + int i; + static int constructor_calls; +}; +int ConstructorCounted::constructor_calls = 0; + +struct ConstructorCountedCompare { + bool operator()(int a, const ConstructorCounted &b) const { return a < b.i; } + bool operator()(const ConstructorCounted &a, int b) const { return a.i < b; } + bool operator()(const ConstructorCounted &a, + const ConstructorCounted &b) const { + return a.i < b.i; + } + using is_transparent = void; +}; + +TEST(Btree, + SetRangeConstructorAndInsertExplicitConvComparableLimitConstruction) { + const int i[] = {0, 1, 1}; + ConstructorCounted::constructor_calls = 0; + + absl::btree_set set{ + std::begin(i), std::end(i)}; + EXPECT_THAT(set, ElementsAre(0, 1)); + EXPECT_EQ(ConstructorCounted::constructor_calls, 2); + + set.insert(std::begin(i), std::end(i)); + EXPECT_THAT(set, ElementsAre(0, 1)); + EXPECT_EQ(ConstructorCounted::constructor_calls, 2); +} + TEST(Btree, SetRangeConstructorAndInsertSupportExplicitConversionNonComparable) { const int i[] = {0, 1}; @@ -2443,6 +2478,21 @@ TEST(Btree, MapRangeConstructorAndInsertSupportExplicitConversionComparable) { EXPECT_THAT(name_map2, ElementsAre(Pair("n1", 1), Pair("n2", 2))); } +TEST(Btree, + MapRangeConstructorAndInsertExplicitConvComparableLimitConstruction) { + const std::pair i[] = {{0, 1}, {1, 2}, {1, 3}}; + ConstructorCounted::constructor_calls = 0; + + absl::btree_map map{ + std::begin(i), std::end(i)}; + EXPECT_THAT(map, ElementsAre(Pair(0, 1), Pair(1, 2))); + EXPECT_EQ(ConstructorCounted::constructor_calls, 2); + + map.insert(std::begin(i), std::end(i)); + EXPECT_THAT(map, ElementsAre(Pair(0, 1), Pair(1, 2))); + EXPECT_EQ(ConstructorCounted::constructor_calls, 2); +} + TEST(Btree, MapRangeConstructorAndInsertSupportExplicitConversionNonComparable) { const std::pair i[] = {{0, 1}, {1, 2}}; diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 9fe1b27f..996afa61 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -1208,9 +1208,9 @@ class btree { // Note: the first overload avoids constructing a value_type if the key // already exists in the btree. template ()), - std::declval()))> + typename = decltype(std::declval()( + params_type::key(*std::declval()), + std::declval()))> void insert_iterator_unique(InputIterator b, InputIterator e, int); // We need the second overload for cases in which we need to construct a // value_type in order to compare it with the keys already in the btree. diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index d441e87f..3dbc1526 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -25,10 +25,12 @@ class Cord; class FormatCountCapture; class FormatSink; -namespace str_format_internal { - +template +struct FormatConvertResult; class FormatConversionSpec; +namespace str_format_internal { + template struct HasUserDefinedConvert : std::false_type {}; @@ -39,6 +41,22 @@ struct HasUserDefinedConvert()))>> : std::true_type {}; +void AbslFormatConvert(); // Stops the lexical name lookup +template +auto FormatConvertImpl(const T& v, FormatConversionSpecImpl conv, + FormatSinkImpl* sink) + -> decltype(AbslFormatConvert(v, + std::declval(), + std::declval())) { + using FormatConversionSpecT = + absl::enable_if_t; + using FormatSinkT = + absl::enable_if_t; + auto fcs = conv.Wrap(); + auto fs = sink->Wrap(); + return AbslFormatConvert(v, fcs, &fs); +} + template class StreamedWrapper; @@ -46,6 +64,13 @@ class StreamedWrapper; // then convert it, appending to `sink` and return `true`. // Otherwise fail and return `false`. +// AbslFormatConvert(v, conv, sink) is intended to be found by ADL on 'v' +// as an extension mechanism. These FormatConvertImpl functions are the default +// implementations. +// The ADL search is augmented via the 'Sink*' parameter, which also +// serves as a disambiguator to reject possible unintended 'AbslFormatConvert' +// functions in the namespaces associated with 'v'. + // Raw pointers. struct VoidPtr { VoidPtr() = default; @@ -61,6 +86,11 @@ struct ArgConvertResult { bool value; }; +template +constexpr FormatConversionCharSet ExtractCharSet(FormatConvertResult) { + return C; +} + template constexpr FormatConversionCharSet ExtractCharSet(ArgConvertResult) { return C; diff --git a/absl/strings/internal/str_format/arg_test.cc b/absl/strings/internal/str_format/arg_test.cc index bf3d7e8e..f53fd6bd 100644 --- a/absl/strings/internal/str_format/arg_test.cc +++ b/absl/strings/internal/str_format/arg_test.cc @@ -23,8 +23,17 @@ class FormatArgImplTest : public ::testing::Test { enum Color { kRed, kGreen, kBlue }; static const char *hi() { return "hi"; } + + struct X {}; + + X x_; }; +inline FormatConvertResult AbslFormatConvert( + const FormatArgImplTest::X &, const FormatConversionSpec &, FormatSink *) { + return {false}; +} + TEST_F(FormatArgImplTest, ToInt) { int out = 0; EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(1), &out)); @@ -59,6 +68,7 @@ TEST_F(FormatArgImplTest, ToInt) { FormatArgImpl(static_cast(nullptr)), &out)); EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(hi()), &out)); EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl("hi"), &out)); + EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(x_), &out)); EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(kBlue), &out)); EXPECT_EQ(2, out); } diff --git a/absl/strings/internal/str_format/extension.cc b/absl/strings/internal/str_format/extension.cc index 94f2b9c2..bb0d96cf 100644 --- a/absl/strings/internal/str_format/extension.cc +++ b/absl/strings/internal/str_format/extension.cc @@ -33,6 +33,29 @@ std::string Flags::ToString() const { return s; } +#define ABSL_INTERNAL_X_VAL(id) \ + constexpr absl::FormatConversionChar FormatConversionCharInternal::id; +ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, ) +#undef ABSL_INTERNAL_X_VAL +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone; + +#define ABSL_INTERNAL_CHAR_SET_CASE(c) \ + constexpr FormatConversionCharSet FormatConversionCharSetInternal::c; +ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, ) +#undef ABSL_INTERNAL_CHAR_SET_CASE + +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar; +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral; +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating; +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric; +// NOLINTNEXTLINE(readability-redundant-declaration) +constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer; + bool FormatSinkImpl::PutPaddedString(string_view value, int width, int precision, bool left) { size_t space_remaining = 0; diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h index 6c60c6c3..a9b9e137 100644 --- a/absl/strings/internal/str_format/extension.h +++ b/absl/strings/internal/str_format/extension.h @@ -31,11 +31,11 @@ namespace absl { ABSL_NAMESPACE_BEGIN -namespace str_format_internal { - enum class FormatConversionChar : uint8_t; enum class FormatConversionCharSet : uint64_t; +namespace str_format_internal { + class FormatRawSinkImpl { public: // Implicitly convert from any type that provides the hook function as @@ -361,14 +361,12 @@ struct FormatConversionCharSetInternal { static constexpr FormatConversionCharSet kStar = FormatConversionCharToConvValue('*'); - // Some predefined values (TODO(matthewbr), delete any that are unused). static constexpr FormatConversionCharSet kIntegral = FormatConversionCharSetUnion(d, i, u, o, x, X); static constexpr FormatConversionCharSet kFloating = FormatConversionCharSetUnion(a, e, f, g, A, E, F, G); static constexpr FormatConversionCharSet kNumeric = FormatConversionCharSetUnion(kIntegral, kFloating); - static constexpr FormatConversionCharSet kString = s; static constexpr FormatConversionCharSet kPointer = p; }; diff --git a/absl/strings/internal/str_format/extension_test.cc b/absl/strings/internal/str_format/extension_test.cc index 0a023f9c..1c93fdb1 100644 --- a/absl/strings/internal/str_format/extension_test.cc +++ b/absl/strings/internal/str_format/extension_test.cc @@ -80,4 +80,19 @@ TEST(FormatExtensionTest, SinkAppendChars) { EXPECT_EQ(actual, expected); } } + +TEST(FormatExtensionTest, VerifyEnumEquality) { +#define X_VAL(id) \ + EXPECT_EQ(absl::FormatConversionChar::id, \ + absl::str_format_internal::FormatConversionCharInternal::id); + ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, ); +#undef X_VAL + +#define X_VAL(id) \ + EXPECT_EQ(absl::FormatConversionCharSet::id, \ + absl::str_format_internal::FormatConversionCharSetInternal::id); + ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, ); +#undef X_VAL +} + } // namespace diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index 36bd84a3..01465107 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -63,6 +63,9 @@ // loosely typed. `FormatUntyped()` is not a template and does not perform // any compile-time checking of the format string; instead, it returns a // boolean from a runtime check. +// +// In addition, the `str_format` library provides extension points for +// augmenting formatting to new types. See "StrFormat Extensions" below. #ifndef ABSL_STRINGS_STR_FORMAT_H_ #define ABSL_STRINGS_STR_FORMAT_H_ @@ -278,9 +281,36 @@ using FormatSpec = str_format_internal::FormatSpecTemplate< // } else { // ... error case ... // } + +#if defined(__cpp_nontype_template_parameter_auto) +// If C++17 is available, an 'extended' format is also allowed that can specify +// multiple conversion characters per format argument, using a combination of +// `absl::FormatConversionCharSet` enum values (logically a set union) +// via the `|` operator. (Single character-based arguments are still accepted, +// but cannot be combined). Some common conversions also have predefined enum +// values, such as `absl::FormatConversionCharSet::kIntegral`. +// +// Example: +// // Extended format supports multiple conversion characters per argument, +// // specified via a combination of `FormatConversionCharSet` enums. +// using MyFormat = absl::ParsedFormat; +// MyFormat GetFormat(bool use_hex) { +// if (use_hex) return MyFormat("foo %x bar"); +// return MyFormat("foo %d bar"); +// } +// // `format` can be used with any value that supports 'd' and 'x', +// // like `int`. +// auto format = GetFormat(use_hex); +// value = StringF(format, i); +template +using ParsedFormat = absl::str_format_internal::ExtendedParsedFormat< + absl::str_format_internal::ToFormatConversionCharSet(Conv)...>; +#else template using ParsedFormat = str_format_internal::ExtendedParsedFormat< absl::str_format_internal::ToFormatConversionCharSet(Conv)...>; +#endif // defined(__cpp_nontype_template_parameter_auto) // StrFormat() // @@ -537,6 +567,246 @@ ABSL_MUST_USE_RESULT inline bool FormatUntyped( str_format_internal::UntypedFormatSpecImpl::Extract(format), args); } +//------------------------------------------------------------------------------ +// StrFormat Extensions +//------------------------------------------------------------------------------ +// +// AbslFormatConvert() +// +// The StrFormat library provides a customization API for formatting +// user-defined types using absl::StrFormat(). The API relies on detecting an +// overload in the user-defined type's namespace of a free (non-member) +// `AbslFormatConvert()` function, usually as a friend definition with the +// following signature: +// +// absl::FormatConvertResult<...> AbslFormatConvert( +// const X& value, +// const absl::FormatConversionSpec& spec, +// absl::FormatSink *sink); +// +// An `AbslFormatConvert()` overload for a type should only be declared in the +// same file and namespace as said type. +// +// The abstractions within this definition include: +// +// * An `absl::FormatConversionSpec` to specify the fields to pull from a +// user-defined type's format string +// * An `absl::FormatSink` to hold the converted string data during the +// conversion process. +// * An `absl::FormatConvertResult` to hold the status of the returned +// formatting operation +// +// The return type encodes all the conversion characters that your +// AbslFormatConvert() routine accepts. The return value should be {true}. +// A return value of {false} will result in `StrFormat()` returning +// an empty string. This result will be propagated to the result of +// `FormatUntyped`. +// +// Example: +// +// struct Point { +// // To add formatting support to `Point`, we simply need to add a free +// // (non-member) function `AbslFormatConvert()`. This method interprets +// // `spec` to print in the request format. The allowed conversion characters +// // can be restricted via the type of the result, in this example +// // string and integral formatting are allowed (but not, for instance +// // floating point characters like "%f"). You can add such a free function +// // using a friend declaration within the body of the class: +// friend absl::FormatConvertResult +// AbslFormatConvert(const Point& p, const absl::FormatConversionSpec& spec, +// absl::FormatSink* s) { +// if (spec.conversion_char() == absl::FormatConversionChar::s) { +// s->Append(absl::StrCat("x=", p.x, " y=", p.y)); +// } else { +// s->Append(absl::StrCat(p.x, ",", p.y)); +// } +// return {true}; +// } +// +// int x; +// int y; +// }; + +// clang-format off + +// FormatConversionChar +// +// Specifies the formatting character provided in the format string +// passed to `StrFormat()`. +enum class FormatConversionChar : uint8_t { + c, s, // text + d, i, o, u, x, X, // int + f, F, e, E, g, G, a, A, // float + n, p // misc +}; +// clang-format on + +// FormatConversionSpec +// +// Specifies modifications to the conversion of the format string, through use +// of one or more format flags in the source format string. +class FormatConversionSpec { + public: + // FormatConversionSpec::is_basic() + // + // Indicates that width and precision are not specified, and no additional + // flags are set for this conversion character in the format string. + bool is_basic() const { return impl_.is_basic(); } + + // FormatConversionSpec::has_left_flag() + // + // Indicates whether the result should be left justified for this conversion + // character in the format string. This flag is set through use of a '-' + // character in the format string. E.g. "%-s" + bool has_left_flag() const { return impl_.has_left_flag(); } + + // FormatConversionSpec::has_show_pos_flag() + // + // Indicates whether a sign column is prepended to the result for this + // conversion character in the format string, even if the result is positive. + // This flag is set through use of a '+' character in the format string. + // E.g. "%+d" + bool has_show_pos_flag() const { return impl_.has_show_pos_flag(); } + + // FormatConversionSpec::has_sign_col_flag() + // + // Indicates whether a mandatory sign column is added to the result for this + // conversion character. This flag is set through use of a space character + // (' ') in the format string. E.g. "% i" + bool has_sign_col_flag() const { return impl_.has_sign_col_flag(); } + + // FormatConversionSpec::has_alt_flag() + // + // Indicates whether an "alternate" format is applied to the result for this + // conversion character. Alternative forms depend on the type of conversion + // character, and unallowed alternatives are undefined. This flag is set + // through use of a '#' character in the format string. E.g. "%#h" + bool has_alt_flag() const { return impl_.has_alt_flag(); } + + // FormatConversionSpec::has_zero_flag() + // + // Indicates whether zeroes should be prepended to the result for this + // conversion character instead of spaces. This flag is set through use of the + // '0' character in the format string. E.g. "%0f" + bool has_zero_flag() const { return impl_.has_zero_flag(); } + + // FormatConversionSpec::conversion_char() + // + // Returns the underlying conversion character. + FormatConversionChar conversion_char() const { + return impl_.conversion_char(); + } + + // FormatConversionSpec::width() + // + // Returns the specified width (indicated through use of a non-zero integer + // value or '*' character) of the conversion character. If width is + // unspecified, it returns a negative value. + int width() const { return impl_.width(); } + + // FormatConversionSpec::precision() + // + // Returns the specified precision (through use of the '.' character followed + // by a non-zero integer value or '*' character) of the conversion character. + // If precision is unspecified, it returns a negative value. + int precision() const { return impl_.precision(); } + + private: + explicit FormatConversionSpec( + str_format_internal::FormatConversionSpecImpl impl) + : impl_(impl) {} + + friend str_format_internal::FormatConversionSpecImpl; + + absl::str_format_internal::FormatConversionSpecImpl impl_; +}; + +// Type safe OR operator for FormatConversionCharSet to allow accepting multiple +// conversion chars in custom format converters. +constexpr FormatConversionCharSet operator|(FormatConversionCharSet a, + FormatConversionCharSet b) { + return static_cast(static_cast(a) | + static_cast(b)); +} + +// FormatConversionCharSet +// +// Specifies the _accepted_ conversion types as a template parameter to +// FormatConvertResult for custom implementations of `AbslFormatConvert`. +// Note the helper predefined alias definitions (kIntegral, etc.) below. +enum class FormatConversionCharSet : uint64_t { + // text + c = str_format_internal::FormatConversionCharToConvInt('c'), + s = str_format_internal::FormatConversionCharToConvInt('s'), + // integer + d = str_format_internal::FormatConversionCharToConvInt('d'), + i = str_format_internal::FormatConversionCharToConvInt('i'), + o = str_format_internal::FormatConversionCharToConvInt('o'), + u = str_format_internal::FormatConversionCharToConvInt('u'), + x = str_format_internal::FormatConversionCharToConvInt('x'), + X = str_format_internal::FormatConversionCharToConvInt('X'), + // Float + f = str_format_internal::FormatConversionCharToConvInt('f'), + F = str_format_internal::FormatConversionCharToConvInt('F'), + e = str_format_internal::FormatConversionCharToConvInt('e'), + E = str_format_internal::FormatConversionCharToConvInt('E'), + g = str_format_internal::FormatConversionCharToConvInt('g'), + G = str_format_internal::FormatConversionCharToConvInt('G'), + a = str_format_internal::FormatConversionCharToConvInt('a'), + A = str_format_internal::FormatConversionCharToConvInt('A'), + // misc + n = str_format_internal::FormatConversionCharToConvInt('n'), + p = str_format_internal::FormatConversionCharToConvInt('p'), + + // Used for width/precision '*' specification. + kStar = static_cast( + absl::str_format_internal::FormatConversionCharSetInternal::kStar), + // Some predefined values: + kIntegral = d | i | u | o | x | X, + kFloating = a | e | f | g | A | E | F | G, + kNumeric = kIntegral | kFloating, + kString = s, + kPointer = p, +}; + +// FormatSink +// +// An abstraction to which conversions write their string data. +// +class FormatSink { + public: + // Appends `count` copies of `ch`. + void Append(size_t count, char ch) { sink_->Append(count, ch); } + + void Append(string_view v) { sink_->Append(v); } + + // Appends the first `precision` bytes of `v`. If this is less than + // `width`, spaces will be appended first (if `left` is false), or + // after (if `left` is true) to ensure the total amount appended is + // at least `width`. + bool PutPaddedString(string_view v, int width, int precision, bool left) { + return sink_->PutPaddedString(v, width, precision, left); + } + + private: + friend str_format_internal::FormatSinkImpl; + explicit FormatSink(str_format_internal::FormatSinkImpl* s) : sink_(s) {} + str_format_internal::FormatSinkImpl* sink_; +}; + +// FormatConvertResult +// +// Indicates whether a call to AbslFormatConvert() was successful. +// This return type informs the StrFormat extension framework (through +// ADL but using the return type) of what conversion characters are supported. +// It is strongly discouraged to return {false}, as this will result in an +// empty string in StrFormat. +template +struct FormatConvertResult { + bool value; +}; + ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index 22cfef66..d9fb25af 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -16,7 +16,6 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace { using str_format_internal::FormatArgImpl; -using str_format_internal::FormatConversionCharSetInternal; using FormatEntryPointTest = ::testing::Test; @@ -537,46 +536,90 @@ TEST_F(ParsedFormatTest, SimpleUncheckedIncorrect) { EXPECT_FALSE((ParsedFormat<'s', 'd', 'g'>::New(format))); } -using absl::str_format_internal::FormatConversionCharSet; +#if defined(__cpp_nontype_template_parameter_auto) + +template +std::true_type IsValidParsedFormatArgTest(ParsedFormat*); + +template +std::false_type IsValidParsedFormatArgTest(...); + +template +using IsValidParsedFormatArg = decltype(IsValidParsedFormatArgTest(nullptr)); + +TEST_F(ParsedFormatTest, OnlyValidTypesAllowed) { + ASSERT_TRUE(IsValidParsedFormatArg<'c'>::value); + + ASSERT_TRUE(IsValidParsedFormatArg::value); + + ASSERT_TRUE(IsValidParsedFormatArg::value); + ASSERT_TRUE( + IsValidParsedFormatArg::value); + + // This is an easy mistake to make, however, this will reduce to an integer + // which has no meaning, so we need to ensure it doesn't compile. + ASSERT_FALSE(IsValidParsedFormatArg<'x' | 'd'>::value); + + // For now, we disallow construction based on ConversionChar (rather than + // CharSet) + ASSERT_FALSE(IsValidParsedFormatArg::value); +} + +TEST_F(ParsedFormatTest, ExtendedTyping) { + EXPECT_FALSE(ParsedFormat::New("")); + ASSERT_TRUE(ParsedFormat::New("%d")); + auto v1 = ParsedFormat<'d', absl::FormatConversionCharSet::s>::New("%d%s"); + ASSERT_TRUE(v1); + auto v2 = ParsedFormat::New("%d%s"); + ASSERT_TRUE(v2); + auto v3 = ParsedFormat::New("%d%s"); + ASSERT_TRUE(v3); + auto v4 = ParsedFormat::New("%s%s"); + ASSERT_TRUE(v4); +} +#endif TEST_F(ParsedFormatTest, UncheckedCorrect) { auto f = - ExtendedParsedFormat::New("ABC%dDEF"); + ExtendedParsedFormat::New("ABC%dDEF"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f)); std::string format = "%sFFF%dZZZ%f"; auto f2 = ExtendedParsedFormat< - FormatConversionCharSetInternal::kString, - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::kFloating>::New(format); + absl::FormatConversionCharSet::kString, absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::kFloating>::New(format); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2)); f2 = ExtendedParsedFormat< - FormatConversionCharSetInternal::kString, - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::kFloating>::New("%s %d %f"); + absl::FormatConversionCharSet::kString, absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::kFloating>::New("%s %d %f"); ASSERT_TRUE(f2); EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2)); auto star = - ExtendedParsedFormat::New("%*d"); + ExtendedParsedFormat::New("%*d"); ASSERT_TRUE(star); EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star)); - auto dollar = ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::New("%2$s %1$d"); + auto dollar = + ExtendedParsedFormat::New("%2$s %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); // with reuse dollar = ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::New("%2$s %1$d %1$d"); + absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::s>::New("%2$s %1$d %1$d"); ASSERT_TRUE(dollar); EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar)); @@ -584,62 +627,61 @@ TEST_F(ParsedFormatTest, UncheckedCorrect) { TEST_F(ParsedFormatTest, UncheckedIgnoredArgs) { EXPECT_FALSE( - (ExtendedParsedFormat::New("ABC"))); + (ExtendedParsedFormat::New("ABC"))); EXPECT_FALSE( - (ExtendedParsedFormat::New("%dABC"))); - EXPECT_FALSE((ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::New("ABC%2$s"))); + (ExtendedParsedFormat::New("%dABC"))); + EXPECT_FALSE( + (ExtendedParsedFormat::New("ABC%2$s"))); auto f = ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::NewAllowIgnored("ABC"); + absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::s>::NewAllowIgnored("ABC"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f)); f = ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::NewAllowIgnored("%dABC"); + absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::s>::NewAllowIgnored("%dABC"); ASSERT_TRUE(f); EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f)); f = ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::s>::NewAllowIgnored("ABC%2$s"); + absl::FormatConversionCharSet::d, + absl::FormatConversionCharSet::s>::NewAllowIgnored("ABC%2$s"); ASSERT_TRUE(f); EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f)); } TEST_F(ParsedFormatTest, UncheckedMultipleTypes) { - auto dx = ExtendedParsedFormat< - FormatConversionCharSetInternal::d | - FormatConversionCharSetInternal::x>::New("%1$d %1$x"); + auto dx = + ExtendedParsedFormat::New("%1$d %1$x"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}[ ]{1$x:1$x}", SummarizeParsedFormat(*dx)); - dx = ExtendedParsedFormat::New("%1$d"); + dx = ExtendedParsedFormat::New("%1$d"); EXPECT_TRUE(dx); EXPECT_EQ("{1$d:1$d}", SummarizeParsedFormat(*dx)); } TEST_F(ParsedFormatTest, UncheckedIncorrect) { - EXPECT_FALSE( - ExtendedParsedFormat::New("")); + EXPECT_FALSE(ExtendedParsedFormat::New("")); - EXPECT_FALSE(ExtendedParsedFormat::New( + EXPECT_FALSE(ExtendedParsedFormat::New( "ABC%dDEF%d")); std::string format = "%sFFF%dZZZ%f"; EXPECT_FALSE( - (ExtendedParsedFormat::New(format))); + (ExtendedParsedFormat::New(format))); } TEST_F(ParsedFormatTest, RegressionMixPositional) { - EXPECT_FALSE((ExtendedParsedFormat< - FormatConversionCharSetInternal::d, - FormatConversionCharSetInternal::o>::New("%1$d %o"))); + EXPECT_FALSE( + (ExtendedParsedFormat::New("%1$d %o"))); } using FormatWrapperTest = ::testing::Test; @@ -664,6 +706,38 @@ TEST_F(FormatWrapperTest, ParsedFormat) { ABSL_NAMESPACE_END } // namespace absl +using FormatExtensionTest = ::testing::Test; + +struct Point { + friend absl::FormatConvertResult + AbslFormatConvert(const Point& p, const absl::FormatConversionSpec& spec, + absl::FormatSink* s) { + if (spec.conversion_char() == absl::FormatConversionChar::s) { + s->Append(absl::StrCat("x=", p.x, " y=", p.y)); + } else { + s->Append(absl::StrCat(p.x, ",", p.y)); + } + return {true}; + } + + int x = 10; + int y = 20; +}; + +TEST_F(FormatExtensionTest, AbslFormatConvertExample) { + Point p; + EXPECT_EQ(absl::StrFormat("a %s z", p), "a x=10 y=20 z"); + EXPECT_EQ(absl::StrFormat("a %d z", p), "a 10,20 z"); + + // Typed formatting will fail to compile an invalid format. + // StrFormat("%f", p); // Does not compile. + std::string actual; + absl::UntypedFormatSpec f1("%f"); + // FormatUntyped will return false for bad character. + EXPECT_FALSE(absl::FormatUntyped(&actual, f1, {absl::FormatArg(p)})); +} + // Some codegen thunks that we can use to easily dump the generated assembly for // different StrFormat calls. -- cgit v1.2.3 From 4b2fbb4adba905eede6c61b4494acfdb660a3bb7 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 12 Oct 2020 10:33:47 -0700 Subject: Export of internal Abseil changes -- a5af5874c1c5cc02bd2a748d455321f82b6f2a93 by Andy Getzendanner : fix compile fails with asan and -Wredundant-decls Import of https://github.com/abseil/abseil-cpp/pull/801 PiperOrigin-RevId: 336693223 -- ed9df42ab2b742386c6692c2bed015374c919d9c by Derek Mauro : Fix integer conversion warning Fixes #814 PiperOrigin-RevId: 336651814 -- 0ab4c23884e72dce17b67c1eb520f9dbb802565d by Derek Mauro : Internal change PiperOrigin-RevId: 336585378 -- eba0e3dccd52a6e91bcff84075bef0affc650b74 by Matt Kulukundis : Add bitset operations to Futex helper. PiperOrigin-RevId: 336409368 -- 8b0709a8b4500bf5f0af4b602d76a298d81645e8 by Abseil Team : Fix code indentation in a comment. PiperOrigin-RevId: 336368167 -- bc3961c87a7e7760c10319a5b0349c279f7ae3ad by Samuel Benzaquen : Improve performance of the registry: - Reduce contention - Reduce memory usage for each flag by `6*sizeof(void*)`. - Replace one immortal allocation per-flag with a single one for all the flags - Slightly improve single-threaded performance by avoiding the std::map indirections. PiperOrigin-RevId: 336365904 -- 264ad9f28f935aad8b6b1437f8bf804fa9104346 by Abseil Team : Fix typo in comment on absl::Condition. PiperOrigin-RevId: 336311680 -- b5b808a8c75ca0df7b09eff9a423ec171d80f771 by Derek Mauro : Add missing Apache license headers PiperOrigin-RevId: 336294980 -- 89446c3a4793df8b95060385cf3e219357c3db1d by Andy Soffer : Internal changes PiperOrigin-RevId: 336287465 -- 57c8be4e294881bc79a6a44b8e4bf7ecbb19b9b9 by Matt Kulukundis : Extract Futex from an implementation detail of Wait to a private interface. PiperOrigin-RevId: 336123209 GitOrigin-RevId: a5af5874c1c5cc02bd2a748d455321f82b6f2a93 Change-Id: Ie5a0ebe28e571814e3e11d4c05ca308523ccf311 --- CMake/AbseilDll.cmake | 1 + absl/algorithm/container.h | 2 +- absl/base/internal/unscaledcycleclock.cc | 4 +- absl/container/internal/layout_test.cc | 560 ++++++++++++--------- absl/container/internal/raw_hash_set_test.cc | 7 +- absl/debugging/internal/symbolize.h | 8 +- absl/flags/BUILD.bazel | 2 + absl/flags/flag_benchmark.cc | 33 ++ absl/flags/internal/registry.h | 5 +- absl/flags/parse.cc | 5 + absl/flags/reflection.cc | 68 ++- absl/hash/hash_test.cc | 8 +- absl/hash/internal/city.cc | 18 +- absl/hash/internal/city.h | 2 +- absl/random/internal/generate_real_test.cc | 4 +- absl/strings/cord_test.cc | 14 + absl/strings/internal/str_format/arg.cc | 14 + absl/strings/internal/str_format/arg.h | 14 + absl/strings/internal/str_format/bind.cc | 14 + absl/strings/internal/str_format/bind.h | 14 + absl/strings/internal/str_format/bind_test.cc | 14 + absl/strings/internal/str_format/checker.h | 14 + absl/strings/internal/str_format/checker_test.cc | 14 + absl/strings/internal/str_format/convert_test.cc | 14 + .../internal/str_format/float_conversion.cc | 14 + .../strings/internal/str_format/float_conversion.h | 14 + absl/strings/internal/str_format/parser.cc | 14 + absl/strings/internal/str_format/parser.h | 14 + absl/strings/internal/str_format/parser_test.cc | 14 + absl/strings/str_format_test.cc | 13 + absl/synchronization/BUILD.bazel | 1 + absl/synchronization/CMakeLists.txt | 1 + absl/synchronization/internal/futex.h | 139 +++++ absl/synchronization/internal/kernel_timeout.h | 3 +- absl/synchronization/internal/waiter.cc | 70 +-- absl/synchronization/mutex.cc | 9 +- absl/synchronization/mutex.h | 10 +- absl/time/clock.cc | 4 +- absl/time/duration.cc | 2 +- absl/time/time.cc | 7 +- absl/time/time.h | 12 +- absl/time/time_test.cc | 12 +- absl/types/internal/variant.h | 2 +- absl/types/variant_test.cc | 6 +- 44 files changed, 817 insertions(+), 397 deletions(-) create mode 100644 absl/synchronization/internal/futex.h (limited to 'absl/strings/str_format_test.cc') diff --git a/CMake/AbseilDll.cmake b/CMake/AbseilDll.cmake index cf6a8c9a..d67c4399 100644 --- a/CMake/AbseilDll.cmake +++ b/CMake/AbseilDll.cmake @@ -250,6 +250,7 @@ set(ABSL_INTERNAL_DLL_FILES "synchronization/notification.h" "synchronization/internal/create_thread_identity.cc" "synchronization/internal/create_thread_identity.h" + "synchronization/internal/futex.h" "synchronization/internal/graphcycles.cc" "synchronization/internal/graphcycles.h" "synchronization/internal/kernel_timeout.h" diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h index bb3d1c7c..09b91f74 100644 --- a/absl/algorithm/container.h +++ b/absl/algorithm/container.h @@ -93,7 +93,7 @@ using ContainerPointerType = // std::foo(begin(c), end(c)); // becomes // std::foo(container_algorithm_internal::begin(c), -// container_algorithm_internal::end(c)); +// container_algorithm_internal::end(c)); // These are meant for internal use only. template diff --git a/absl/base/internal/unscaledcycleclock.cc b/absl/base/internal/unscaledcycleclock.cc index f1e7bbef..1545288c 100644 --- a/absl/base/internal/unscaledcycleclock.cc +++ b/absl/base/internal/unscaledcycleclock.cc @@ -123,9 +123,7 @@ double UnscaledCycleClock::Frequency() { #pragma intrinsic(__rdtsc) -int64_t UnscaledCycleClock::Now() { - return __rdtsc(); -} +int64_t UnscaledCycleClock::Now() { return __rdtsc(); } double UnscaledCycleClock::Frequency() { return base_internal::NominalCPUFrequency(); diff --git a/absl/container/internal/layout_test.cc b/absl/container/internal/layout_test.cc index 757272f1..1d7158ff 100644 --- a/absl/container/internal/layout_test.cc +++ b/absl/container/internal/layout_test.cc @@ -128,8 +128,10 @@ TEST(Layout, ElementTypes) { { using L = Layout; SameType, L::ElementTypes>(); - SameType, decltype(L::Partial())::ElementTypes>(); - SameType, decltype(L::Partial(0))::ElementTypes>(); + SameType, + decltype(L::Partial())::ElementTypes>(); + SameType, + decltype(L::Partial(0))::ElementTypes>(); } { using L = Layout; @@ -368,18 +370,21 @@ TEST(Layout, PointerByIndex) { { using L = Layout; EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3).Pointer<0>(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(3).Pointer<0>(p)))); EXPECT_EQ(0, Distance(p, Type(L(3).Pointer<0>(p)))); } { using L = Layout; EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3).Pointer<0>(p)))); - EXPECT_EQ(12, Distance(p, Type(L::Partial(3).Pointer<1>(p)))); EXPECT_EQ(0, - Distance(p, Type(L::Partial(3, 5).Pointer<0>(p)))); + Distance(p, Type(L::Partial(3).Pointer<0>(p)))); EXPECT_EQ(12, - Distance(p, Type(L::Partial(3, 5).Pointer<1>(p)))); + Distance(p, Type(L::Partial(3).Pointer<1>(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(3, 5).Pointer<0>(p)))); + EXPECT_EQ( + 12, Distance(p, Type(L::Partial(3, 5).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L(3, 5).Pointer<0>(p)))); EXPECT_EQ(12, Distance(p, Type(L(3, 5).Pointer<1>(p)))); } @@ -387,39 +392,44 @@ TEST(Layout, PointerByIndex) { using L = Layout; EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer<1>(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(0).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(1).Pointer<0>(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1).Pointer<1>(p)))); + EXPECT_EQ(4, + Distance(p, Type(L::Partial(1).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(5).Pointer<0>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5).Pointer<1>(p)))); + EXPECT_EQ(8, + Distance(p, Type(L::Partial(5).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer<0>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0).Pointer<1>(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(0, 0).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer<2>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(1, 0).Pointer<0>(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1, 0).Pointer<1>(p)))); + EXPECT_EQ( + 4, Distance(p, Type(L::Partial(1, 0).Pointer<1>(p)))); EXPECT_EQ(8, Distance(p, Type(L::Partial(1, 0).Pointer<2>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(5, 3).Pointer<0>(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5, 3).Pointer<1>(p)))); + EXPECT_EQ( + 8, Distance(p, Type(L::Partial(5, 3).Pointer<1>(p)))); EXPECT_EQ(24, Distance(p, Type(L::Partial(5, 3).Pointer<2>(p)))); EXPECT_EQ( 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<0>(p)))); EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); + 0, + Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); EXPECT_EQ( 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<2>(p)))); EXPECT_EQ( 0, Distance(p, Type(L::Partial(1, 0, 0).Pointer<0>(p)))); EXPECT_EQ( - 4, Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); + 4, + Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); EXPECT_EQ( 8, Distance(p, Type(L::Partial(1, 0, 0).Pointer<2>(p)))); EXPECT_EQ( @@ -428,7 +438,8 @@ TEST(Layout, PointerByIndex) { 24, Distance(p, Type(L::Partial(5, 3, 1).Pointer<2>(p)))); EXPECT_EQ( - 8, Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); + 8, + Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer<0>(p)))); EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer<2>(p)))); EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer<1>(p)))); @@ -439,75 +450,78 @@ TEST(Layout, PointerByType) { alignas(max_align_t) const unsigned char p[100] = {}; { using L = Layout; - EXPECT_EQ(0, - Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(3).Pointer(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial().Pointer(p)))); + EXPECT_EQ( + 0, + Distance(p, Type(L::Partial(3).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L(3).Pointer(p)))); } { using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5).Pointer(p)))); EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); + 0, Distance(p, Type(L::Partial().Pointer(p)))); EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); + 0, Distance(p, Type(L::Partial(0).Pointer(p)))); EXPECT_EQ( 0, - Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + Distance(p, Type(L::Partial(0).Pointer(p)))); EXPECT_EQ( - 4, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + 0, Distance(p, Type(L::Partial(1).Pointer(p)))); EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + 4, + Distance(p, Type(L::Partial(1).Pointer(p)))); EXPECT_EQ( - 0, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + 0, Distance(p, Type(L::Partial(5).Pointer(p)))); EXPECT_EQ( - 8, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + 8, + Distance(p, Type(L::Partial(5).Pointer(p)))); EXPECT_EQ( - 24, - Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + 0, + Distance(p, Type(L::Partial(0, 0).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(0, 0).Pointer(p)))); EXPECT_EQ( 0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); + Distance(p, Type(L::Partial(0, 0).Pointer(p)))); EXPECT_EQ( 0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(0, 0, 0).Pointer(p)))); + Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + EXPECT_EQ(4, Distance(p, Type( + L::Partial(1, 0).Pointer(p)))); + EXPECT_EQ( + 8, + Distance(p, Type(L::Partial(1, 0).Pointer(p)))); EXPECT_EQ( 0, - Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); + Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + EXPECT_EQ(8, Distance(p, Type( + L::Partial(5, 3).Pointer(p)))); EXPECT_EQ( - 4, - Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); + 24, + Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(0, 0, 0).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(0, 0, 0).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(0, 0, 0).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(1, 0, 0).Pointer(p)))); + EXPECT_EQ(4, Distance(p, Type( + L::Partial(1, 0, 0).Pointer(p)))); EXPECT_EQ(8, Distance(p, Type( L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); + EXPECT_EQ(0, Distance(p, Type( + L::Partial(5, 3, 1).Pointer(p)))); EXPECT_EQ(24, Distance(p, Type( L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); + EXPECT_EQ(8, Distance(p, Type( + L::Partial(5, 3, 1).Pointer(p)))); EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer(p)))); + EXPECT_EQ( + 8, Distance(p, Type(L(5, 3, 1).Pointer(p)))); } } @@ -548,15 +562,18 @@ TEST(Layout, MutablePointerByIndex) { EXPECT_EQ(8, Distance(p, Type(L::Partial(5, 3).Pointer<1>(p)))); EXPECT_EQ(24, Distance(p, Type(L::Partial(5, 3).Pointer<2>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<2>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(1, 0, 0).Pointer<0>(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); + EXPECT_EQ(4, + Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); EXPECT_EQ(8, Distance(p, Type(L::Partial(1, 0, 0).Pointer<2>(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(5, 3, 1).Pointer<0>(p)))); EXPECT_EQ(24, Distance(p, Type(L::Partial(5, 3, 1).Pointer<2>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); + EXPECT_EQ(8, + Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer<0>(p)))); EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer<2>(p)))); EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer<1>(p)))); @@ -568,48 +585,61 @@ TEST(Layout, MutablePointerByType) { { using L = Layout; EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3).Pointer(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(3).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L(3).Pointer(p)))); } { using L = Layout; EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(0).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1).Pointer(p)))); + EXPECT_EQ(4, + Distance(p, Type(L::Partial(1).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); + EXPECT_EQ(8, + Distance(p, Type(L::Partial(5).Pointer(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(0, 0).Pointer(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(1, 0).Pointer(p)))); + EXPECT_EQ( + 4, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); EXPECT_EQ(8, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + EXPECT_EQ(0, + Distance(p, Type(L::Partial(5, 3).Pointer(p)))); + EXPECT_EQ( + 8, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); EXPECT_EQ(24, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); + EXPECT_EQ( + 0, + Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); EXPECT_EQ( 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); + EXPECT_EQ( + 4, + Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); EXPECT_EQ( 8, Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); + EXPECT_EQ( + 0, Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); EXPECT_EQ( 24, Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); + EXPECT_EQ( + 8, + Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer(p)))); EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer(p)))); EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer(p)))); @@ -790,67 +820,72 @@ TEST(Layout, SliceByIndexData) { { using L = Layout; EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(0).Slice<0>(p)).data())); EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); - EXPECT_EQ(0, Distance(p, Type>(L(3).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(3).Slice<0>(p)).data())); + EXPECT_EQ(0, + Distance(p, Type>(L(3).Slice<0>(p)).data())); } { using L = Layout; EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(3).Slice<0>(p)).data())); EXPECT_EQ( 0, - Distance(p, - Type>(L::Partial(3, 5).Slice<0>(p)).data())); + Distance( + p, Type>(L::Partial(3, 5).Slice<0>(p)).data())); EXPECT_EQ( 12, - Distance(p, - Type>(L::Partial(3, 5).Slice<1>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L(3, 5).Slice<0>(p)).data())); - EXPECT_EQ(12, - Distance(p, Type>(L(3, 5).Slice<1>(p)).data())); + Distance( + p, Type>(L::Partial(3, 5).Slice<1>(p)).data())); + EXPECT_EQ( + 0, Distance(p, Type>(L(3, 5).Slice<0>(p)).data())); + EXPECT_EQ( + 12, Distance(p, Type>(L(3, 5).Slice<1>(p)).data())); } { using L = Layout; EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(1).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(0).Slice<0>(p)).data())); EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(5).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(1).Slice<0>(p)).data())); EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); + p, Type>(L::Partial(5).Slice<0>(p)).data())); EXPECT_EQ( 0, - Distance(p, - Type>(L::Partial(0, 0).Slice<1>(p)).data())); + Distance( + p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); + 0, + Distance( + p, Type>(L::Partial(0, 0).Slice<1>(p)).data())); + EXPECT_EQ( + 0, + Distance( + p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); EXPECT_EQ( 4, - Distance(p, - Type>(L::Partial(1, 0).Slice<1>(p)).data())); + Distance( + p, Type>(L::Partial(1, 0).Slice<1>(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); + 0, + Distance( + p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); EXPECT_EQ( 8, - Distance(p, - Type>(L::Partial(5, 3).Slice<1>(p)).data())); + Distance( + p, Type>(L::Partial(5, 3).Slice<1>(p)).data())); EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); + p, + Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); EXPECT_EQ( 0, Distance( @@ -864,7 +899,8 @@ TEST(Layout, SliceByIndexData) { EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); + p, + Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); EXPECT_EQ( 4, Distance( @@ -878,7 +914,8 @@ TEST(Layout, SliceByIndexData) { EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); + p, + Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); EXPECT_EQ( 24, Distance( @@ -890,12 +927,14 @@ TEST(Layout, SliceByIndexData) { p, Type>(L::Partial(5, 3, 1).Slice<1>(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); + 0, + Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); EXPECT_EQ( 24, Distance(p, Type>(L(5, 3, 1).Slice<2>(p)).data())); EXPECT_EQ( - 8, Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); + 8, + Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); } } @@ -906,98 +945,94 @@ TEST(Layout, SliceByTypeData) { EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(0).Slice(p)).data())); + p, + Type>(L::Partial(0).Slice(p)).data())); EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(3).Slice(p)).data())); + p, + Type>(L::Partial(3).Slice(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L(3).Slice(p)).data())); + 0, + Distance(p, Type>(L(3).Slice(p)).data())); } { using L = Layout; - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0).Slice(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(1).Slice(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(5).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(0, 0).Slice(p)).data())); EXPECT_EQ( 0, Distance( p, - Type>(L::Partial(0, 0).Slice(p)).data())); + Type>(L::Partial(0).Slice(p)).data())); EXPECT_EQ( 0, - Distance( - p, Type>(L::Partial(1, 0).Slice(p)).data())); - EXPECT_EQ( - 4, Distance( p, - Type>(L::Partial(1, 0).Slice(p)).data())); + Type>(L::Partial(1).Slice(p)).data())); EXPECT_EQ( 0, - Distance( - p, Type>(L::Partial(5, 3).Slice(p)).data())); - EXPECT_EQ( - 8, Distance( p, - Type>(L::Partial(5, 3).Slice(p)).data())); + Type>(L::Partial(5).Slice(p)).data())); EXPECT_EQ( 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice(p)).data())); + Distance(p, Type>(L::Partial(0, 0).Slice(p)) + .data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(0, 0).Slice(p)) + .data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(0, 0, 0).Slice(p)) + Distance(p, Type>(L::Partial(1, 0).Slice(p)) .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(0, 0, 0).Slice(p)) + EXPECT_EQ(4, Distance(p, Type>( + L::Partial(1, 0).Slice(p)) .data())); EXPECT_EQ( 0, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 4, - Distance(p, Type>(L::Partial(1, 0, 0).Slice(p)) + Distance(p, Type>(L::Partial(5, 3).Slice(p)) .data())); + EXPECT_EQ(8, Distance(p, Type>( + L::Partial(5, 3).Slice(p)) + .data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(0, 0, 0).Slice(p)) + .data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(0, 0, 0).Slice(p)) + .data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(0, 0, 0).Slice(p)) + .data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(1, 0, 0).Slice(p)) + .data())); + EXPECT_EQ(4, Distance(p, Type>( + L::Partial(1, 0, 0).Slice(p)) + .data())); EXPECT_EQ(8, Distance(p, Type>( L::Partial(1, 0, 0).Slice(p)) .data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice(p)).data())); + EXPECT_EQ(0, Distance(p, Type>( + L::Partial(5, 3, 1).Slice(p)) + .data())); EXPECT_EQ(24, Distance(p, Type>( L::Partial(5, 3, 1).Slice(p)) .data())); - EXPECT_EQ( - 8, - Distance(p, Type>(L::Partial(5, 3, 1).Slice(p)) - .data())); + EXPECT_EQ(8, Distance(p, Type>( + L::Partial(5, 3, 1).Slice(p)) + .data())); EXPECT_EQ( 0, - Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); + Distance(p, + Type>(L(5, 3, 1).Slice(p)).data())); EXPECT_EQ( 24, Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); EXPECT_EQ( - 8, Distance( - p, Type>(L(5, 3, 1).Slice(p)).data())); + 8, + Distance( + p, Type>(L(5, 3, 1).Slice(p)).data())); } } @@ -1005,18 +1040,19 @@ TEST(Layout, MutableSliceByIndexData) { alignas(max_align_t) unsigned char p[100]; { using L = Layout; - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); + EXPECT_EQ( + 0, Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); + EXPECT_EQ( + 0, Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); EXPECT_EQ(0, Distance(p, Type>(L(3).Slice<0>(p)).data())); } { using L = Layout; - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(3, 5).Slice<0>(p)).data())); + 0, Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); + EXPECT_EQ( + 0, + Distance(p, Type>(L::Partial(3, 5).Slice<0>(p)).data())); EXPECT_EQ( 12, Distance(p, Type>(L::Partial(3, 5).Slice<1>(p)).data())); @@ -1025,55 +1061,63 @@ TEST(Layout, MutableSliceByIndexData) { } { using L = Layout; - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(1).Slice<0>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L::Partial(5).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(0, 0).Slice<1>(p)).data())); + 0, Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); + 0, Distance(p, Type>(L::Partial(1).Slice<0>(p)).data())); EXPECT_EQ( - 4, Distance(p, Type>(L::Partial(1, 0).Slice<1>(p)).data())); + 0, Distance(p, Type>(L::Partial(5).Slice<0>(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); + 0, + Distance(p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); EXPECT_EQ( - 8, Distance(p, Type>(L::Partial(5, 3).Slice<1>(p)).data())); + 0, + Distance(p, Type>(L::Partial(0, 0).Slice<1>(p)).data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); + Distance(p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); + EXPECT_EQ( + 4, + Distance(p, Type>(L::Partial(1, 0).Slice<1>(p)).data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(0, 0, 0).Slice<1>(p)).data())); + Distance(p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); + EXPECT_EQ( + 8, + Distance(p, Type>(L::Partial(5, 3).Slice<1>(p)).data())); + EXPECT_EQ( + 0, Distance( + p, Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); + EXPECT_EQ( + 0, Distance( + p, Type>(L::Partial(0, 0, 0).Slice<1>(p)).data())); EXPECT_EQ( 0, Distance( p, Type>(L::Partial(0, 0, 0).Slice<2>(p)).data())); EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); EXPECT_EQ( - 4, - Distance(p, Type>(L::Partial(1, 0, 0).Slice<1>(p)).data())); + 4, Distance( + p, Type>(L::Partial(1, 0, 0).Slice<1>(p)).data())); EXPECT_EQ( 8, Distance( p, Type>(L::Partial(1, 0, 0).Slice<2>(p)).data())); EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); + 0, Distance( + p, Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); EXPECT_EQ( 24, Distance( p, Type>(L::Partial(5, 3, 1).Slice<2>(p)).data())); EXPECT_EQ( - 8, - Distance(p, Type>(L::Partial(5, 3, 1).Slice<1>(p)).data())); - EXPECT_EQ(0, Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); + 8, Distance( + p, Type>(L::Partial(5, 3, 1).Slice<1>(p)).data())); + EXPECT_EQ(0, + Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); EXPECT_EQ(24, Distance(p, Type>(L(5, 3, 1).Slice<2>(p)).data())); - EXPECT_EQ(8, Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); + EXPECT_EQ(8, + Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); } } @@ -1082,66 +1126,84 @@ TEST(Layout, MutableSliceByTypeData) { { using L = Layout; EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0).Slice(p)).data())); + 0, Distance( + p, Type>(L::Partial(0).Slice(p)).data())); EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(3).Slice(p)).data())); - EXPECT_EQ(0, Distance(p, Type>(L(3).Slice(p)).data())); + 0, Distance( + p, Type>(L::Partial(3).Slice(p)).data())); + EXPECT_EQ(0, + Distance(p, Type>(L(3).Slice(p)).data())); } { using L = Layout; EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(0).Slice(p)).data())); + 0, + Distance(p, Type>(L::Partial(0).Slice(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(1).Slice(p)).data())); + 0, + Distance(p, Type>(L::Partial(1).Slice(p)).data())); EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(5).Slice(p)).data())); + 0, + Distance(p, Type>(L::Partial(5).Slice(p)).data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(0, 0).Slice(p)).data())); + Distance(p, + Type>(L::Partial(0, 0).Slice(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0, 0).Slice(p)).data())); + 0, + Distance( + p, Type>(L::Partial(0, 0).Slice(p)).data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(1, 0).Slice(p)).data())); + Distance(p, + Type>(L::Partial(1, 0).Slice(p)).data())); EXPECT_EQ( - 4, Distance( - p, Type>(L::Partial(1, 0).Slice(p)).data())); + 4, + Distance( + p, Type>(L::Partial(1, 0).Slice(p)).data())); EXPECT_EQ( 0, - Distance(p, Type>(L::Partial(5, 3).Slice(p)).data())); + Distance(p, + Type>(L::Partial(5, 3).Slice(p)).data())); EXPECT_EQ( - 8, Distance( - p, Type>(L::Partial(5, 3).Slice(p)).data())); + 8, + Distance( + p, Type>(L::Partial(5, 3).Slice(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice(p)).data())); + 0, + Distance( + p, + Type>(L::Partial(0, 0, 0).Slice(p)).data())); EXPECT_EQ( 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice(p)).data())); + p, + Type>(L::Partial(0, 0, 0).Slice(p)).data())); EXPECT_EQ( 0, Distance( p, Type>(L::Partial(0, 0, 0).Slice(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(1, 0, 0).Slice(p)).data())); + 0, + Distance( + p, + Type>(L::Partial(1, 0, 0).Slice(p)).data())); EXPECT_EQ( 4, Distance( - p, Type>(L::Partial(1, 0, 0).Slice(p)).data())); + p, + Type>(L::Partial(1, 0, 0).Slice(p)).data())); EXPECT_EQ( 8, Distance( p, Type>(L::Partial(1, 0, 0).Slice(p)).data())); EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(5, 3, 1).Slice(p)).data())); + 0, + Distance( + p, + Type>(L::Partial(5, 3, 1).Slice(p)).data())); EXPECT_EQ( 24, Distance( @@ -1150,14 +1212,16 @@ TEST(Layout, MutableSliceByTypeData) { EXPECT_EQ( 8, Distance( - p, Type>(L::Partial(5, 3, 1).Slice(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); + p, + Type>(L::Partial(5, 3, 1).Slice(p)).data())); + EXPECT_EQ( + 0, Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); EXPECT_EQ( 24, Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); EXPECT_EQ( - 8, Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); + 8, + Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); } } @@ -1256,17 +1320,17 @@ TEST(Layout, MutableSlices) { } { const auto x = L::Partial(1, 2, 3); - EXPECT_THAT( - (Type, Span, Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); + EXPECT_THAT((Type, Span, Span>>( + x.Slices(p))), + Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), + IsSameSlice(x.Slice<2>(p)))); } { const L x(1, 2, 3); - EXPECT_THAT( - (Type, Span, Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); + EXPECT_THAT((Type, Span, Span>>( + x.Slices(p))), + Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), + IsSameSlice(x.Slice<2>(p)))); } } @@ -1398,7 +1462,8 @@ TEST(Layout, DebugString) { x.DebugString()); } { - constexpr auto x = Layout::Partial(1, 2, 3); + constexpr auto x = + Layout::Partial(1, 2, 3); EXPECT_EQ( "@0(1)[1]; @4(4)[2]; @12(1)[3]; " "@16" + @@ -1406,7 +1471,8 @@ TEST(Layout, DebugString) { x.DebugString()); } { - constexpr auto x = Layout::Partial(1, 2, 3, 4); + constexpr auto x = + Layout::Partial(1, 2, 3, 4); EXPECT_EQ( "@0(1)[1]; @4(4)[2]; @12(1)[3]; " "@16" + diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index f5ae83c4..6210eb64 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -847,7 +847,8 @@ TEST(Table, EraseMaintainsValidIterator) { std::vector CollectBadMergeKeys(size_t N) { static constexpr int kGroupSize = Group::kWidth - 1; - auto topk_range = [](size_t b, size_t e, IntTable* t) -> std::vector { + auto topk_range = [](size_t b, size_t e, + IntTable* t) -> std::vector { for (size_t i = b; i != e; ++i) { t->emplace(i); } @@ -1001,8 +1002,8 @@ using ProbeStatsPerSize = std::map; // 1. Create new table and reserve it to keys.size() * 2 // 2. Insert all keys xored with seed // 3. Collect ProbeStats from final table. -ProbeStats CollectProbeStatsOnKeysXoredWithSeed(const std::vector& keys, - size_t num_iters) { +ProbeStats CollectProbeStatsOnKeysXoredWithSeed( + const std::vector& keys, size_t num_iters) { const size_t reserve_size = keys.size() * 2; ProbeStats stats; diff --git a/absl/debugging/internal/symbolize.h b/absl/debugging/internal/symbolize.h index b3729af7..4f26130f 100644 --- a/absl/debugging/internal/symbolize.h +++ b/absl/debugging/internal/symbolize.h @@ -118,16 +118,14 @@ bool RemoveAllSymbolDecorators(void); // filename != nullptr // // Returns true if the file was successfully registered. -bool RegisterFileMappingHint( - const void* start, const void* end, uint64_t offset, const char* filename); +bool RegisterFileMappingHint(const void* start, const void* end, + uint64_t offset, const char* filename); // Looks up the file mapping registered by RegisterFileMappingHint for an // address range. If there is one, the file name is stored in *filename and // *start and *end are modified to reflect the registered mapping. Returns // whether any hint was found. -bool GetFileMappingHint(const void** start, - const void** end, - uint64_t * offset, +bool GetFileMappingHint(const void** start, const void** end, uint64_t* offset, const char** filename); } // namespace debugging_internal diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel index 9de9e223..2bd94780 100644 --- a/absl/flags/BUILD.bazel +++ b/absl/flags/BUILD.bazel @@ -381,6 +381,8 @@ cc_binary( deps = [ ":flag", ":marshalling", + ":parse", + ":reflection", "//absl/strings", "//absl/time", "//absl/types:optional", diff --git a/absl/flags/flag_benchmark.cc b/absl/flags/flag_benchmark.cc index 7b52c9bc..9982b604 100644 --- a/absl/flags/flag_benchmark.cc +++ b/absl/flags/flag_benchmark.cc @@ -20,6 +20,8 @@ #include "absl/flags/flag.h" #include "absl/flags/marshalling.h" +#include "absl/flags/parse.h" +#include "absl/flags/reflection.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/optional.h" @@ -103,6 +105,23 @@ std::string AbslUnparseFlag(const UDT&) { return ""; } BENCHMARKED_TYPES(FLAG_DEF) +// Register thousands of flags to bloat up the size of the registry. +// This mimics real life production binaries. +#define DEFINE_FLAG_0(name) ABSL_FLAG(int, name, 0, ""); +#define DEFINE_FLAG_1(name) DEFINE_FLAG_0(name##0) DEFINE_FLAG_0(name##1) +#define DEFINE_FLAG_2(name) DEFINE_FLAG_1(name##0) DEFINE_FLAG_1(name##1) +#define DEFINE_FLAG_3(name) DEFINE_FLAG_2(name##0) DEFINE_FLAG_2(name##1) +#define DEFINE_FLAG_4(name) DEFINE_FLAG_3(name##0) DEFINE_FLAG_3(name##1) +#define DEFINE_FLAG_5(name) DEFINE_FLAG_4(name##0) DEFINE_FLAG_4(name##1) +#define DEFINE_FLAG_6(name) DEFINE_FLAG_5(name##0) DEFINE_FLAG_5(name##1) +#define DEFINE_FLAG_7(name) DEFINE_FLAG_6(name##0) DEFINE_FLAG_6(name##1) +#define DEFINE_FLAG_8(name) DEFINE_FLAG_7(name##0) DEFINE_FLAG_7(name##1) +#define DEFINE_FLAG_9(name) DEFINE_FLAG_8(name##0) DEFINE_FLAG_8(name##1) +#define DEFINE_FLAG_10(name) DEFINE_FLAG_9(name##0) DEFINE_FLAG_9(name##1) +#define DEFINE_FLAG_11(name) DEFINE_FLAG_10(name##0) DEFINE_FLAG_10(name##1) +#define DEFINE_FLAG_12(name) DEFINE_FLAG_11(name##0) DEFINE_FLAG_11(name##1) +DEFINE_FLAG_12(bloat_flag_); + namespace { #define BM_GetFlag(T) \ @@ -115,6 +134,20 @@ namespace { BENCHMARKED_TYPES(BM_GetFlag) +void BM_ThreadedFindCommandLineFlag(benchmark::State& state) { + char dummy[] = "dummy"; + char* argv[] = {dummy}; + // We need to ensure that flags have been parsed. That is where the registry + // is finalized. + absl::ParseCommandLine(1, argv); + + for (auto s : state) { + benchmark::DoNotOptimize( + absl::FindCommandLineFlag("bloat_flag_010101010101")); + } +} +BENCHMARK(BM_ThreadedFindCommandLineFlag)->ThreadRange(1, 16); + } // namespace #define InvokeGetFlag(T) \ diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h index 1df2db79..a8d9eb9c 100644 --- a/absl/flags/internal/registry.h +++ b/absl/flags/internal/registry.h @@ -30,9 +30,6 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace flags_internal { -// Executes specified visitor for each non-retired flag in the registry. -// Requires the caller hold the registry lock. -void ForEachFlagUnlocked(std::function visitor); // Executes specified visitor for each non-retired flag in the registry. While // callback are executed, the registry is locked and can't be changed. void ForEachFlag(std::function visitor); @@ -41,6 +38,8 @@ void ForEachFlag(std::function visitor); bool RegisterCommandLineFlag(CommandLineFlag&); +void FinalizeRegistry(); + //----------------------------------------------------------------------------- // Retired registrations: // diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc index 4f4bb3d5..1835a837 100644 --- a/absl/flags/parse.cc +++ b/absl/flags/parse.cc @@ -611,6 +611,11 @@ std::vector ParseCommandLineImpl(int argc, char* argv[], OnUndefinedFlag on_undef_flag) { ABSL_INTERNAL_CHECK(argc > 0, "Missing argv[0]"); + // Once parsing has started we will not have more flag registrations. + // If we did, they would be missing during parsing, which is a problem on + // itself. + flags_internal::FinalizeRegistry(); + // This routine does not return anything since we abort on failure. CheckDefaultValuesParsingRoundtrip(); diff --git a/absl/flags/reflection.cc b/absl/flags/reflection.cc index d7060221..c6bf8aab 100644 --- a/absl/flags/reflection.cc +++ b/absl/flags/reflection.cc @@ -17,6 +17,7 @@ #include +#include #include #include @@ -56,21 +57,23 @@ class FlagRegistry { // Returns the flag object for the specified name, or nullptr if not found. // Will emit a warning if a 'retired' flag is specified. - CommandLineFlag* FindFlagLocked(absl::string_view name); + CommandLineFlag* FindFlag(absl::string_view name); static FlagRegistry& GlobalRegistry(); // returns a singleton registry private: friend class flags_internal::FlagSaverImpl; // reads all the flags in order // to copy them - friend void ForEachFlagUnlocked( - std::function visitor); + friend void ForEachFlag(std::function visitor); + friend void FinalizeRegistry(); - // The map from name to flag, for FindFlagLocked(). + // The map from name to flag, for FindFlag(). using FlagMap = std::map; using FlagIterator = FlagMap::iterator; using FlagConstIterator = FlagMap::const_iterator; FlagMap flags_; + std::vector flat_flags_; + std::atomic finalized_flags_{false}; absl::Mutex lock_; @@ -79,15 +82,6 @@ class FlagRegistry { FlagRegistry& operator=(const FlagRegistry&); }; -CommandLineFlag* FlagRegistry::FindFlagLocked(absl::string_view name) { - FlagConstIterator i = flags_.find(name); - if (i == flags_.end()) { - return nullptr; - } - - return i->second; -} - namespace { class FlagRegistryLock { @@ -101,8 +95,24 @@ class FlagRegistryLock { } // namespace +CommandLineFlag* FlagRegistry::FindFlag(absl::string_view name) { + if (finalized_flags_.load(std::memory_order_acquire)) { + // We could save some gcus here if we make `Name()` be non-virtual. + // We could move the `const char*` name to the base class. + auto it = std::partition_point( + flat_flags_.begin(), flat_flags_.end(), + [=](CommandLineFlag* f) { return f->Name() < name; }); + if (it != flat_flags_.end() && (*it)->Name() == name) return *it; + } + + FlagRegistryLock frl(*this); + auto it = flags_.find(name); + return it != flags_.end() ? it->second : nullptr; +} + void FlagRegistry::RegisterFlag(CommandLineFlag& flag) { FlagRegistryLock registry_lock(*this); + std::pair ins = flags_.insert(FlagMap::value_type(flag.Name(), &flag)); if (ins.second == false) { // means the name was already in the map @@ -152,18 +162,15 @@ FlagRegistry& FlagRegistry::GlobalRegistry() { // -------------------------------------------------------------------- -void ForEachFlagUnlocked(std::function visitor) { +void ForEachFlag(std::function visitor) { FlagRegistry& registry = FlagRegistry::GlobalRegistry(); - for (FlagRegistry::FlagConstIterator i = registry.flags_.begin(); - i != registry.flags_.end(); ++i) { - visitor(*i->second); + + if (registry.finalized_flags_.load(std::memory_order_acquire)) { + for (const auto& i : registry.flat_flags_) visitor(*i); } -} -void ForEachFlag(std::function visitor) { - FlagRegistry& registry = FlagRegistry::GlobalRegistry(); FlagRegistryLock frl(registry); - ForEachFlagUnlocked(visitor); + for (const auto& i : registry.flags_) visitor(*i.second); } // -------------------------------------------------------------------- @@ -173,6 +180,21 @@ bool RegisterCommandLineFlag(CommandLineFlag& flag) { return true; } +void FinalizeRegistry() { + auto& registry = FlagRegistry::GlobalRegistry(); + FlagRegistryLock frl(registry); + if (registry.finalized_flags_.load(std::memory_order_relaxed)) { + // Was already finalized. Ignore the second time. + return; + } + registry.flat_flags_.reserve(registry.flags_.size()); + for (const auto& f : registry.flags_) { + registry.flat_flags_.push_back(f.second); + } + registry.flags_.clear(); + registry.finalized_flags_.store(true, std::memory_order_release); +} + // -------------------------------------------------------------------- namespace { @@ -298,9 +320,7 @@ CommandLineFlag* FindCommandLineFlag(absl::string_view name) { if (name.empty()) return nullptr; flags_internal::FlagRegistry& registry = flags_internal::FlagRegistry::GlobalRegistry(); - flags_internal::FlagRegistryLock frl(registry); - - return registry.FindFlagLocked(name); + return registry.FindFlag(name); } // -------------------------------------------------------------------- diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index 39ba24a8..1d2e6cf0 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -82,8 +82,8 @@ TYPED_TEST_P(HashValueIntTest, FastPath) { } REGISTER_TYPED_TEST_CASE_P(HashValueIntTest, BasicUsage, FastPath); -using IntTypes = testing::Types; +using IntTypes = testing::Types; INSTANTIATE_TYPED_TEST_CASE_P(My, HashValueIntTest, IntTypes); enum LegacyEnum { kValue1, kValue2, kValue3 }; @@ -819,8 +819,8 @@ TYPED_TEST_P(HashIntTest, BasicUsage) { } REGISTER_TYPED_TEST_CASE_P(HashIntTest, BasicUsage); -using IntTypes = testing::Types; +using IntTypes = testing::Types; INSTANTIATE_TYPED_TEST_CASE_P(My, HashIntTest, IntTypes); struct StructWithPadding { diff --git a/absl/hash/internal/city.cc b/absl/hash/internal/city.cc index e122c184..58d4bcb1 100644 --- a/absl/hash/internal/city.cc +++ b/absl/hash/internal/city.cc @@ -253,9 +253,8 @@ static uint64_t HashLen17to32(const char *s, size_t len) { // Return a 16-byte hash for 48 bytes. Quick and dirty. // Callers do best to use "random-looking" values for a and b. -static std::pair WeakHashLen32WithSeeds(uint64_t w, uint64_t x, - uint64_t y, uint64_t z, - uint64_t a, uint64_t b) { +static std::pair WeakHashLen32WithSeeds( + uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) { a += w; b = Rotate(b + a + z, 21); uint64_t c = a; @@ -266,8 +265,9 @@ static std::pair WeakHashLen32WithSeeds(uint64_t w, uint64_t } // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. -static std::pair WeakHashLen32WithSeeds(const char *s, uint64_t a, - uint64_t b) { +static std::pair WeakHashLen32WithSeeds(const char *s, + uint64_t a, + uint64_t b) { return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16), Fetch64(s + 24), a, b); } @@ -310,8 +310,10 @@ uint64_t CityHash64(const char *s, size_t len) { uint64_t x = Fetch64(s + len - 40); uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56); uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24)); - std::pair v = WeakHashLen32WithSeeds(s + len - 64, len, z); - std::pair w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x); + std::pair v = + WeakHashLen32WithSeeds(s + len - 64, len, z); + std::pair w = + WeakHashLen32WithSeeds(s + len - 32, y + k1, x); x = x * k1 + Fetch64(s); // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. @@ -337,7 +339,7 @@ uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) { } uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, - uint64_t seed1) { + uint64_t seed1) { return HashLen16(CityHash64(s, len) - seed0, seed1); } diff --git a/absl/hash/internal/city.h b/absl/hash/internal/city.h index 161c7748..9c1e7a57 100644 --- a/absl/hash/internal/city.h +++ b/absl/hash/internal/city.h @@ -71,7 +71,7 @@ uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed); // Hash function for a byte array. For convenience, two seeds are also // hashed into the result. uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, - uint64_t seed1); + uint64_t seed1); // Hash function for a byte array. Most useful in 32-bit binaries. uint32_t CityHash32(const char *s, size_t len); diff --git a/absl/random/internal/generate_real_test.cc b/absl/random/internal/generate_real_test.cc index aa02f0c2..4bdc4534 100644 --- a/absl/random/internal/generate_real_test.cc +++ b/absl/random/internal/generate_real_test.cc @@ -419,8 +419,8 @@ TEST(GenerateRealTest, ExhaustiveFloat) { }; // Rely on RandU64ToFloat generating values from greatest to least when - // supplied with uint64_t values from greatest (0xfff...) to least (0x0). Thus, - // this algorithm stores the previous value, and if the new value is at + // supplied with uint64_t values from greatest (0xfff...) to least (0x0). + // Thus, this algorithm stores the previous value, and if the new value is at // greater than or equal to the previous value, then there is a collision in // the generation algorithm. // diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index 4443c828..dbed3e9a 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/cord.h" #include diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index 9feb2248..e28a29b1 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // // POSIX spec: // http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 3dbc1526..7040c866 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_ diff --git a/absl/strings/internal/str_format/bind.cc b/absl/strings/internal/str_format/bind.cc index 6980ed1d..194e21af 100644 --- a/absl/strings/internal/str_format/bind.cc +++ b/absl/strings/internal/str_format/bind.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/internal/str_format/bind.h" #include diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h index 585246e7..727b2115 100644 --- a/absl/strings/internal/str_format/bind.h +++ b/absl/strings/internal/str_format/bind.h @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_ diff --git a/absl/strings/internal/str_format/bind_test.cc b/absl/strings/internal/str_format/bind_test.cc index 64790a85..1eef9c43 100644 --- a/absl/strings/internal/str_format/bind_test.cc +++ b/absl/strings/internal/str_format/bind_test.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/internal/str_format/bind.h" #include diff --git a/absl/strings/internal/str_format/checker.h b/absl/strings/internal/str_format/checker.h index 424c51f7..2a2601ec 100644 --- a/absl/strings/internal/str_format/checker.h +++ b/absl/strings/internal/str_format/checker.h @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_ diff --git a/absl/strings/internal/str_format/checker_test.cc b/absl/strings/internal/str_format/checker_test.cc index a76d70b0..7c70f47d 100644 --- a/absl/strings/internal/str_format/checker_test.cc +++ b/absl/strings/internal/str_format/checker_test.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include #include "gmock/gmock.h" diff --git a/absl/strings/internal/str_format/convert_test.cc b/absl/strings/internal/str_format/convert_test.cc index 634ee78b..375db0a0 100644 --- a/absl/strings/internal/str_format/convert_test.cc +++ b/absl/strings/internal/str_format/convert_test.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include #include #include diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index 20aeada5..d3c5f0a7 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/internal/str_format/float_conversion.h" #include diff --git a/absl/strings/internal/str_format/float_conversion.h b/absl/strings/internal/str_format/float_conversion.h index e78bc191..71100e71 100644 --- a/absl/strings/internal/str_format/float_conversion.h +++ b/absl/strings/internal/str_format/float_conversion.h @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_ diff --git a/absl/strings/internal/str_format/parser.cc b/absl/strings/internal/str_format/parser.cc index cc55dfa9..f308d023 100644 --- a/absl/strings/internal/str_format/parser.cc +++ b/absl/strings/internal/str_format/parser.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/internal/str_format/parser.h" #include diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h index fffed04f..6504dd3d 100644 --- a/absl/strings/internal/str_format/parser.h +++ b/absl/strings/internal/str_format/parser.h @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_ diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc index 5aced987..a5fa1c79 100644 --- a/absl/strings/internal/str_format/parser_test.cc +++ b/absl/strings/internal/str_format/parser_test.cc @@ -1,3 +1,17 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "absl/strings/internal/str_format/parser.h" #include diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index d9fb25af..c60027ad 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc @@ -1,3 +1,16 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "absl/strings/str_format.h" diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel index b2df4131..cd4009a1 100644 --- a/absl/synchronization/BUILD.bazel +++ b/absl/synchronization/BUILD.bazel @@ -80,6 +80,7 @@ cc_library( "barrier.h", "blocking_counter.h", "internal/create_thread_identity.h", + "internal/futex.h", "internal/per_thread_sem.h", "internal/waiter.h", "mutex.h", diff --git a/absl/synchronization/CMakeLists.txt b/absl/synchronization/CMakeLists.txt index c255b03e..e633d0bf 100644 --- a/absl/synchronization/CMakeLists.txt +++ b/absl/synchronization/CMakeLists.txt @@ -52,6 +52,7 @@ absl_cc_library( "barrier.h" "blocking_counter.h" "internal/create_thread_identity.h" + "internal/futex.h" "internal/per_thread_sem.h" "internal/waiter.h" "mutex.h" diff --git a/absl/synchronization/internal/futex.h b/absl/synchronization/internal/futex.h new file mode 100644 index 00000000..3539f491 --- /dev/null +++ b/absl/synchronization/internal/futex.h @@ -0,0 +1,139 @@ +// Copyright 2020 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_ +#define ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_ + +#include "absl/base/config.h" + +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +#ifdef __linux__ +#include +#include +#endif + +#include +#include +#include + +#include +#include + +#include "absl/base/optimization.h" +#include "absl/synchronization/internal/kernel_timeout.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace synchronization_internal { + +// Some Android headers are missing these definitions even though they +// support these futex operations. +#ifdef __BIONIC__ +#ifndef SYS_futex +#define SYS_futex __NR_futex +#endif +#ifndef FUTEX_WAIT_BITSET +#define FUTEX_WAIT_BITSET 9 +#endif +#ifndef FUTEX_PRIVATE_FLAG +#define FUTEX_PRIVATE_FLAG 128 +#endif +#ifndef FUTEX_CLOCK_REALTIME +#define FUTEX_CLOCK_REALTIME 256 +#endif +#ifndef FUTEX_BITSET_MATCH_ANY +#define FUTEX_BITSET_MATCH_ANY 0xFFFFFFFF +#endif +#endif + +#if defined(__NR_futex_time64) && !defined(SYS_futex_time64) +#define SYS_futex_time64 __NR_futex_time64 +#endif + +#if defined(SYS_futex_time64) && !defined(SYS_futex) +#define SYS_futex SYS_futex_time64 +#endif + +class FutexImpl { + public: + static int WaitUntil(std::atomic *v, int32_t val, + KernelTimeout t) { + int err = 0; + if (t.has_timeout()) { + // https://locklessinc.com/articles/futex_cheat_sheet/ + // Unlike FUTEX_WAIT, FUTEX_WAIT_BITSET uses absolute time. + struct timespec abs_timeout = t.MakeAbsTimespec(); + // Atomically check that the futex value is still 0, and if it + // is, sleep until abs_timeout or until woken by FUTEX_WAKE. + err = syscall( + SYS_futex, reinterpret_cast(v), + FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME, val, + &abs_timeout, nullptr, FUTEX_BITSET_MATCH_ANY); + } else { + // Atomically check that the futex value is still 0, and if it + // is, sleep until woken by FUTEX_WAKE. + err = syscall(SYS_futex, reinterpret_cast(v), + FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, nullptr); + } + if (ABSL_PREDICT_FALSE(err != 0)) { + err = -errno; + } + return err; + } + + static int WaitBitsetAbsoluteTimeout(std::atomic *v, int32_t val, + int32_t bits, + const struct timespec *abstime) { + int err = syscall(SYS_futex, reinterpret_cast(v), + FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG, val, abstime, + nullptr, bits); + if (ABSL_PREDICT_FALSE(err != 0)) { + err = -errno; + } + return err; + } + + static int Wake(std::atomic *v, int32_t count) { + int err = syscall(SYS_futex, reinterpret_cast(v), + FUTEX_WAKE | FUTEX_PRIVATE_FLAG, count); + if (ABSL_PREDICT_FALSE(err < 0)) { + err = -errno; + } + return err; + } + + // FUTEX_WAKE_BITSET + static int WakeBitset(std::atomic *v, int32_t count, int32_t bits) { + int err = syscall(SYS_futex, reinterpret_cast(v), + FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG, count, nullptr, + nullptr, bits); + if (ABSL_PREDICT_FALSE(err < 0)) { + err = -errno; + } + return err; + } +}; + +class Futex : public FutexImpl {}; + +} // namespace synchronization_internal +ABSL_NAMESPACE_END +} // namespace absl + +#endif // ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_H_ diff --git a/absl/synchronization/internal/kernel_timeout.h b/absl/synchronization/internal/kernel_timeout.h index 1084e1e6..bbd4d2d7 100644 --- a/absl/synchronization/internal/kernel_timeout.h +++ b/absl/synchronization/internal/kernel_timeout.h @@ -26,6 +26,7 @@ #define ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_ #include + #include #include @@ -142,7 +143,7 @@ inline struct timespec KernelTimeout::MakeAbsTimespec() { struct timespec abstime; int64_t seconds = (std::min)(n / kNanosPerSecond, - int64_t{(std::numeric_limits::max)()}); + int64_t{(std::numeric_limits::max)()}); abstime.tv_sec = static_cast(seconds); abstime.tv_nsec = static_cast(n % kNanosPerSecond); return abstime; diff --git a/absl/synchronization/internal/waiter.cc b/absl/synchronization/internal/waiter.cc index b6150b9b..c51855b0 100644 --- a/absl/synchronization/internal/waiter.cc +++ b/absl/synchronization/internal/waiter.cc @@ -48,6 +48,11 @@ #include "absl/base/optimization.h" #include "absl/synchronization/internal/kernel_timeout.h" +#if ABSL_WAITER_MODE == ABSL_WAITER_MODE_FUTEX +#include "absl/synchronization/internal/futex.h" +#endif + + namespace absl { ABSL_NAMESPACE_BEGIN namespace synchronization_internal { @@ -66,71 +71,6 @@ static void MaybeBecomeIdle() { #if ABSL_WAITER_MODE == ABSL_WAITER_MODE_FUTEX -// Some Android headers are missing these definitions even though they -// support these futex operations. -#ifdef __BIONIC__ -#ifndef SYS_futex -#define SYS_futex __NR_futex -#endif -#ifndef FUTEX_WAIT_BITSET -#define FUTEX_WAIT_BITSET 9 -#endif -#ifndef FUTEX_PRIVATE_FLAG -#define FUTEX_PRIVATE_FLAG 128 -#endif -#ifndef FUTEX_CLOCK_REALTIME -#define FUTEX_CLOCK_REALTIME 256 -#endif -#ifndef FUTEX_BITSET_MATCH_ANY -#define FUTEX_BITSET_MATCH_ANY 0xFFFFFFFF -#endif -#endif - -#if defined(__NR_futex_time64) && !defined(SYS_futex_time64) -#define SYS_futex_time64 __NR_futex_time64 -#endif - -#if defined(SYS_futex_time64) && !defined(SYS_futex) -#define SYS_futex SYS_futex_time64 -#endif - -class Futex { - public: - static int WaitUntil(std::atomic *v, int32_t val, - KernelTimeout t) { - int err = 0; - if (t.has_timeout()) { - // https://locklessinc.com/articles/futex_cheat_sheet/ - // Unlike FUTEX_WAIT, FUTEX_WAIT_BITSET uses absolute time. - struct timespec abs_timeout = t.MakeAbsTimespec(); - // Atomically check that the futex value is still 0, and if it - // is, sleep until abs_timeout or until woken by FUTEX_WAKE. - err = syscall( - SYS_futex, reinterpret_cast(v), - FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME, val, - &abs_timeout, nullptr, FUTEX_BITSET_MATCH_ANY); - } else { - // Atomically check that the futex value is still 0, and if it - // is, sleep until woken by FUTEX_WAKE. - err = syscall(SYS_futex, reinterpret_cast(v), - FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, nullptr); - } - if (err != 0) { - err = -errno; - } - return err; - } - - static int Wake(std::atomic *v, int32_t count) { - int err = syscall(SYS_futex, reinterpret_cast(v), - FUTEX_WAKE | FUTEX_PRIVATE_FLAG, count); - if (ABSL_PREDICT_FALSE(err < 0)) { - err = -errno; - } - return err; - } -}; - Waiter::Waiter() { futex_.store(0, std::memory_order_relaxed); } diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 9b7f088a..63f09408 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc @@ -88,8 +88,8 @@ ABSL_CONST_INIT std::atomic synch_deadlock_detection( ABSL_CONST_INIT std::atomic synch_check_invariants(false); ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES - absl::base_internal::AtomicHook - submit_profile_data; +absl::base_internal::AtomicHook + submit_profile_data; ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook mutex_tracer; @@ -491,7 +491,7 @@ struct SynchWaitParams { std::atomic *cv_word; int64_t contention_start_cycles; // Time (in cycles) when this thread started - // to contend for the mutex. + // to contend for the mutex. }; struct SynchLocksHeld { @@ -2311,7 +2311,8 @@ ABSL_ATTRIBUTE_NOINLINE void Mutex::UnlockSlow(SynchWaitParams *waitp) { if (!cond_waiter) { // Sample lock contention events only if the (first) waiter was trying to // acquire the lock, not waiting on a condition variable or Condition. - int64_t wait_cycles = base_internal::CycleClock::Now() - enqueue_timestamp; + int64_t wait_cycles = + base_internal::CycleClock::Now() - enqueue_timestamp; mutex_tracer("slow release", this, wait_cycles); ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0); submit_profile_data(enqueue_timestamp); diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h index 6340bd63..f686c4df 100644 --- a/absl/synchronization/mutex.h +++ b/absl/synchronization/mutex.h @@ -667,10 +667,10 @@ class Condition { // }; // mu_.Await(Condition(&reached)); // - // NOTE: never use "mu_.AssertHeld()" instead of "mu_.AssertReadHeld()" in the - // lambda as it may be called when the mutex is being unlocked from a scope - // holding only a reader lock, which will make the assertion not fulfilled and - // crash the binary. + // NOTE: never use "mu_.AssertHeld()" instead of "mu_.AssertReaderHeld()" in + // the lambda as it may be called when the mutex is being unlocked from a + // scope holding only a reader lock, which will make the assertion not + // fulfilled and crash the binary. // See class comment for performance advice. In particular, if there // might be more than one waiter for the same condition, make sure @@ -954,7 +954,7 @@ void RegisterMutexProfiler(void (*fn)(int64_t wait_timestamp)); // // This has the same memory ordering concerns as RegisterMutexProfiler() above. void RegisterMutexTracer(void (*fn)(const char *msg, const void *obj, - int64_t wait_cycles)); + int64_t wait_cycles)); // TODO(gfalcon): Combine RegisterMutexProfiler() and RegisterMutexTracer() // into a single interface, since they are only ever called in pairs. diff --git a/absl/time/clock.cc b/absl/time/clock.cc index e5c423c7..6862e011 100644 --- a/absl/time/clock.cc +++ b/absl/time/clock.cc @@ -74,9 +74,7 @@ ABSL_NAMESPACE_END #if !ABSL_USE_CYCLECLOCK_FOR_GET_CURRENT_TIME_NANOS namespace absl { ABSL_NAMESPACE_BEGIN -int64_t GetCurrentTimeNanos() { - return GET_CURRENT_TIME_NANOS_FROM_SYSTEM(); -} +int64_t GetCurrentTimeNanos() { return GET_CURRENT_TIME_NANOS_FROM_SYSTEM(); } ABSL_NAMESPACE_END } // namespace absl #else // Use the cyclecounter-based implementation below. diff --git a/absl/time/duration.cc b/absl/time/duration.cc index eca1a6d9..4443109a 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -356,7 +356,7 @@ namespace time_internal { // the remainder. If it does not saturate, the remainder remain accurate, // but the returned quotient will over/underflow int64_t and should not be used. int64_t IDivDuration(bool satq, const Duration num, const Duration den, - Duration* rem) { + Duration* rem) { int64_t q = 0; if (IDivFastPath(num, den, &q, rem)) { return q; diff --git a/absl/time/time.cc b/absl/time/time.cc index 6bb36cb3..1ec2026e 100644 --- a/absl/time/time.cc +++ b/absl/time/time.cc @@ -60,9 +60,10 @@ inline cctz::time_point unix_epoch() { inline int64_t FloorToUnit(absl::Duration d, absl::Duration unit) { absl::Duration rem; int64_t q = absl::IDivDuration(d, unit, &rem); - return (q > 0 || - rem >= ZeroDuration() || - q == std::numeric_limits::min()) ? q : q - 1; + return (q > 0 || rem >= ZeroDuration() || + q == std::numeric_limits::min()) + ? q + : q - 1; } inline absl::Time::Breakdown InfiniteFutureBreakdown() { diff --git a/absl/time/time.h b/absl/time/time.h index 37f6131d..72508031 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -639,7 +639,7 @@ class Time { // Deprecated. Use `absl::TimeZone::CivilInfo`. struct Breakdown { - int64_t year; // year (e.g., 2013) + int64_t year; // year (e.g., 2013) int month; // month of year [1:12] int day; // day of month [1:31] int hour; // hour of day [0:23] @@ -1494,12 +1494,10 @@ constexpr Duration Hours(int64_t n) { constexpr bool operator<(Duration lhs, Duration rhs) { return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs) ? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs) - : time_internal::GetRepHi(lhs) == - (std::numeric_limits::min)() - ? time_internal::GetRepLo(lhs) + 1 < - time_internal::GetRepLo(rhs) + 1 - : time_internal::GetRepLo(lhs) < - time_internal::GetRepLo(rhs); + : time_internal::GetRepHi(lhs) == (std::numeric_limits::min)() + ? time_internal::GetRepLo(lhs) + 1 < + time_internal::GetRepLo(rhs) + 1 + : time_internal::GetRepLo(lhs) < time_internal::GetRepLo(rhs); } constexpr bool operator==(Duration lhs, Duration rhs) { diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc index b28a99fb..cde9423f 100644 --- a/absl/time/time_test.cc +++ b/absl/time/time_test.cc @@ -1070,7 +1070,8 @@ TEST(Time, ConversionSaturation) { EXPECT_EQ("292277026596-12-04T15:30:07+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); EXPECT_EQ( - absl::UnixEpoch() + absl::Seconds(std::numeric_limits::max()), t); + absl::UnixEpoch() + absl::Seconds(std::numeric_limits::max()), + t); // Checks that we can also get the maximal Time value for a far-east zone. const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60); @@ -1078,7 +1079,8 @@ TEST(Time, ConversionSaturation) { EXPECT_EQ("292277026596-12-05T05:30:07+14:00", absl::FormatTime(absl::RFC3339_full, t, plus14)); EXPECT_EQ( - absl::UnixEpoch() + absl::Seconds(std::numeric_limits::max()), t); + absl::UnixEpoch() + absl::Seconds(std::numeric_limits::max()), + t); // One second later should push us to infinity. t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc); @@ -1092,7 +1094,8 @@ TEST(Time, ConversionSaturation) { EXPECT_EQ("-292277022657-01-27T08:29:52+00:00", absl::FormatTime(absl::RFC3339_full, t, utc)); EXPECT_EQ( - absl::UnixEpoch() + absl::Seconds(std::numeric_limits::min()), t); + absl::UnixEpoch() + absl::Seconds(std::numeric_limits::min()), + t); // Checks that we can also get the minimal Time value for a far-west zone. const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60); @@ -1101,7 +1104,8 @@ TEST(Time, ConversionSaturation) { EXPECT_EQ("-292277022657-01-26T20:29:52-12:00", absl::FormatTime(absl::RFC3339_full, t, minus12)); EXPECT_EQ( - absl::UnixEpoch() + absl::Seconds(std::numeric_limits::min()), t); + absl::UnixEpoch() + absl::Seconds(std::numeric_limits::min()), + t); // One second before should push us to -infinity. t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc); diff --git a/absl/types/internal/variant.h b/absl/types/internal/variant.h index d404e80c..772008c7 100644 --- a/absl/types/internal/variant.h +++ b/absl/types/internal/variant.h @@ -45,7 +45,7 @@ ABSL_NAMESPACE_BEGIN template class variant; -ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1); +ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, static_cast(-1)); template struct variant_size; diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc index cf8f7f33..cf237334 100644 --- a/absl/types/variant_test.cc +++ b/absl/types/variant_test.cc @@ -2311,7 +2311,8 @@ TEST(VariantTest, TestRvalueConversion) { ASSERT_TRUE(absl::holds_alternative(variant2)); EXPECT_EQ(42, absl::get(variant2)); - variant2 = ConvertVariantTo>(variant(42)); + variant2 = + ConvertVariantTo>(variant(42)); ASSERT_TRUE(absl::holds_alternative(variant2)); EXPECT_EQ(42, absl::get(variant2)); #endif // !ABSL_USES_STD_VARIANT @@ -2453,7 +2454,8 @@ TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) { ConvertVariantTo>(variant(42))); EXPECT_THAT(absl::get_if(&variant2), Pointee(42)); - variant2 = ConvertVariantTo>(variant(42)); + variant2 = + ConvertVariantTo>(variant(42)); EXPECT_THAT(absl::get_if(&variant2), Pointee(42)); #endif -- cgit v1.2.3