Skip to content

Commit a766987

Browse files
Abseil Teamvslashg
Abseil Team
authored andcommitted
Export of internal Abseil changes
-- a9eb3c976c6d8ef4fca3d416847f8fca4bd90dd7 by Derek Mauro <[email protected]>: Remove the deprecated container library, which doesn't do anything. This will help prevent user confusion, as seen in abseil#183. PiperOrigin-RevId: 360172262 -- 4f872f651e25a528bdc59ee4e24543fbbd358f00 by Abseil Team <[email protected]>: Remove unused nspace alias. PiperOrigin-RevId: 359487559 -- 43e877e464886cf9226012f5bb47910b8995e70f by Abseil Team <[email protected]>: Create a StatusToStringMode to control how the ToString behaves. PiperOrigin-RevId: 359339603 -- 0da1291569e167341613359846948c72c8a838e1 by Greg Falcon <[email protected]>: Fix a bug in SimpleAtoi/SimpleAtof, which accepted a prefix of "+-" (e.g., "+-5" was parsed as 5.0). This regression was introduced when we migrated these functions to use absl::from_chars. PiperOrigin-RevId: 359135105 GitOrigin-RevId: a9eb3c976c6d8ef4fca3d416847f8fca4bd90dd7 Change-Id: I0e2072cad80651e473ba1d34b1fb3a033dfaba80
1 parent 998805a commit a766987

File tree

7 files changed

+126
-32
lines changed

7 files changed

+126
-32
lines changed

absl/container/CMakeLists.txt

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
# limitations under the License.
1515
#
1616

17-
# This is deprecated and will be removed in the future. It also doesn't do
18-
# anything anyways. Prefer to use the library associated with the API you are
19-
# using.
20-
absl_cc_library(
21-
NAME
22-
container
23-
PUBLIC
24-
)
25-
2617
absl_cc_library(
2718
NAME
2819
btree

absl/flags/reflection_test.cc

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
3434

3535
namespace {
3636

37-
namespace flags = absl::flags_internal;
38-
3937
class ReflectionTest : public testing::Test {
4038
protected:
4139
void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }

absl/status/status.cc

+18-12
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,26 @@ bool Status::EqualsSlow(const absl::Status& a, const absl::Status& b) {
291291
return true;
292292
}
293293

294-
std::string Status::ToStringSlow() const {
294+
std::string Status::ToStringSlow(StatusToStringMode mode) const {
295295
std::string text;
296296
absl::StrAppend(&text, absl::StatusCodeToString(code()), ": ", message());
297-
status_internal::StatusPayloadPrinter printer =
298-
status_internal::GetStatusPayloadPrinter();
299-
this->ForEachPayload([&](absl::string_view type_url,
300-
const absl::Cord& payload) {
301-
absl::optional<std::string> result;
302-
if (printer) result = printer(type_url, payload);
303-
absl::StrAppend(
304-
&text, " [", type_url, "='",
305-
result.has_value() ? *result : absl::CHexEscape(std::string(payload)),
306-
"']");
307-
});
297+
298+
const bool with_payload = (mode & StatusToStringMode::kWithPayload) ==
299+
StatusToStringMode::kWithPayload;
300+
301+
if (with_payload) {
302+
status_internal::StatusPayloadPrinter printer =
303+
status_internal::GetStatusPayloadPrinter();
304+
this->ForEachPayload([&](absl::string_view type_url,
305+
const absl::Cord& payload) {
306+
absl::optional<std::string> result;
307+
if (printer) result = printer(type_url, payload);
308+
absl::StrAppend(
309+
&text, " [", type_url, "='",
310+
result.has_value() ? *result : absl::CHexEscape(std::string(payload)),
311+
"']");
312+
});
313+
}
308314

309315
return text;
310316
}

absl/status/status.h

+59-9
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,55 @@ std::string StatusCodeToString(StatusCode code);
280280
// Streams StatusCodeToString(code) to `os`.
281281
std::ostream& operator<<(std::ostream& os, StatusCode code);
282282

283+
// absl::StatusToStringMode
284+
//
285+
// An `absl::StatusToStringMode` is an enumerated type indicating how
286+
// `absl::Status::ToString()` should construct the output string for an non-ok
287+
// status.
288+
enum class StatusToStringMode : int {
289+
// ToString will not contain any extra data (such as payloads). It will only
290+
// contain the error code and message, if any.
291+
kWithNoExtraData = 0,
292+
// ToString will contain the payloads.
293+
kWithPayload = 1 << 0,
294+
};
295+
296+
// absl::StatusToStringMode is specified as a bitmask type, which means the
297+
// following operations must be provided:
298+
inline constexpr StatusToStringMode operator&(StatusToStringMode lhs,
299+
StatusToStringMode rhs) {
300+
return static_cast<StatusToStringMode>(static_cast<int>(lhs) &
301+
static_cast<int>(rhs));
302+
}
303+
inline constexpr StatusToStringMode operator|(StatusToStringMode lhs,
304+
StatusToStringMode rhs) {
305+
return static_cast<StatusToStringMode>(static_cast<int>(lhs) |
306+
static_cast<int>(rhs));
307+
}
308+
inline constexpr StatusToStringMode operator^(StatusToStringMode lhs,
309+
StatusToStringMode rhs) {
310+
return static_cast<StatusToStringMode>(static_cast<int>(lhs) ^
311+
static_cast<int>(rhs));
312+
}
313+
inline constexpr StatusToStringMode operator~(StatusToStringMode arg) {
314+
return static_cast<StatusToStringMode>(~static_cast<int>(arg));
315+
}
316+
inline StatusToStringMode& operator&=(StatusToStringMode& lhs,
317+
StatusToStringMode rhs) {
318+
lhs = lhs & rhs;
319+
return lhs;
320+
}
321+
inline StatusToStringMode& operator|=(StatusToStringMode& lhs,
322+
StatusToStringMode rhs) {
323+
lhs = lhs | rhs;
324+
return lhs;
325+
}
326+
inline StatusToStringMode& operator^=(StatusToStringMode& lhs,
327+
StatusToStringMode rhs) {
328+
lhs = lhs ^ rhs;
329+
return lhs;
330+
}
331+
283332
// absl::Status
284333
//
285334
// The `absl::Status` class is generally used to gracefully handle errors
@@ -443,15 +492,17 @@ class ABSL_MUST_USE_RESULT Status final {
443492

444493
// Status::ToString()
445494
//
446-
// Returns a combination of the error code name, the message and any
447-
// associated payload messages. This string is designed simply to be human
448-
// readable and its exact format should not be load bearing. Do not depend on
449-
// the exact format of the result of `ToString()` which is subject to change.
495+
// Returns a string based on the `mode`. By default, it returns combination of
496+
// the error code name, the message and any associated payload messages. This
497+
// string is designed simply to be human readable and its exact format should
498+
// not be load bearing. Do not depend on the exact format of the result of
499+
// `ToString()` which is subject to change.
450500
//
451501
// The printed code name and the message are generally substrings of the
452502
// result, and the payloads to be printed use the status payload printer
453503
// mechanism (which is internal).
454-
std::string ToString() const;
504+
std::string ToString(
505+
StatusToStringMode mode = StatusToStringMode::kWithPayload) const;
455506

456507
// Status::IgnoreError()
457508
//
@@ -582,8 +633,7 @@ class ABSL_MUST_USE_RESULT Status final {
582633
static uintptr_t PointerToRep(status_internal::StatusRep* r);
583634
static status_internal::StatusRep* RepToPointer(uintptr_t r);
584635

585-
// Returns string for non-ok Status.
586-
std::string ToStringSlow() const;
636+
std::string ToStringSlow(StatusToStringMode mode) const;
587637

588638
// Status supports two different representations.
589639
// - When the low bit is off it is an inlined representation.
@@ -747,8 +797,8 @@ inline bool operator!=(const Status& lhs, const Status& rhs) {
747797
return !(lhs == rhs);
748798
}
749799

750-
inline std::string Status::ToString() const {
751-
return ok() ? "OK" : ToStringSlow();
800+
inline std::string Status::ToString(StatusToStringMode mode) const {
801+
return ok() ? "OK" : ToStringSlow(mode);
752802
}
753803

754804
inline void Status::IgnoreError() const {

absl/status/status_test.cc

+17
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,23 @@ TEST(Status, ToString) {
280280
HasSubstr("[bar='\\xff']")));
281281
}
282282

283+
TEST(Status, ToStringMode) {
284+
absl::Status s(absl::StatusCode::kInternal, "fail");
285+
s.SetPayload("foo", absl::Cord("bar"));
286+
s.SetPayload("bar", absl::Cord("\377"));
287+
288+
EXPECT_EQ("INTERNAL: fail",
289+
s.ToString(absl::StatusToStringMode::kWithNoExtraData));
290+
291+
EXPECT_THAT(s.ToString(absl::StatusToStringMode::kWithPayload),
292+
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
293+
HasSubstr("[bar='\\xff']")));
294+
295+
EXPECT_THAT(s.ToString(~absl::StatusToStringMode::kWithPayload),
296+
AllOf(HasSubstr("INTERNAL: fail"), Not(HasSubstr("[foo='bar']")),
297+
Not(HasSubstr("[bar='\\xff']"))));
298+
}
299+
283300
absl::Status EraseAndReturn(const absl::Status& base) {
284301
absl::Status copy = base;
285302
EXPECT_TRUE(copy.ErasePayload(kUrl1));

absl/strings/numbers.cc

+10
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ ABSL_NAMESPACE_BEGIN
4646
bool SimpleAtof(absl::string_view str, float* out) {
4747
*out = 0.0;
4848
str = StripAsciiWhitespace(str);
49+
// std::from_chars doesn't accept an initial +, but SimpleAtof does, so if one
50+
// is present, skip it, while avoiding accepting "+-0" as valid.
4951
if (!str.empty() && str[0] == '+') {
5052
str.remove_prefix(1);
53+
if (!str.empty() && str[0] == '-') {
54+
return false;
55+
}
5156
}
5257
auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
5358
if (result.ec == std::errc::invalid_argument) {
@@ -72,8 +77,13 @@ bool SimpleAtof(absl::string_view str, float* out) {
7277
bool SimpleAtod(absl::string_view str, double* out) {
7378
*out = 0.0;
7479
str = StripAsciiWhitespace(str);
80+
// std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one
81+
// is present, skip it, while avoiding accepting "+-0" as valid.
7582
if (!str.empty() && str[0] == '+') {
7683
str.remove_prefix(1);
84+
if (!str.empty() && str[0] == '-') {
85+
return false;
86+
}
7787
}
7888
auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
7989
if (result.ec == std::errc::invalid_argument) {

absl/strings/numbers_test.cc

+22
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,28 @@ TEST(NumbersTest, Atod) {
392392
EXPECT_TRUE(std::isnan(d));
393393
}
394394

395+
TEST(NumbersTest, Prefixes) {
396+
double d;
397+
EXPECT_FALSE(absl::SimpleAtod("++1", &d));
398+
EXPECT_FALSE(absl::SimpleAtod("+-1", &d));
399+
EXPECT_FALSE(absl::SimpleAtod("-+1", &d));
400+
EXPECT_FALSE(absl::SimpleAtod("--1", &d));
401+
EXPECT_TRUE(absl::SimpleAtod("-1", &d));
402+
EXPECT_EQ(d, -1.);
403+
EXPECT_TRUE(absl::SimpleAtod("+1", &d));
404+
EXPECT_EQ(d, +1.);
405+
406+
float f;
407+
EXPECT_FALSE(absl::SimpleAtof("++1", &f));
408+
EXPECT_FALSE(absl::SimpleAtof("+-1", &f));
409+
EXPECT_FALSE(absl::SimpleAtof("-+1", &f));
410+
EXPECT_FALSE(absl::SimpleAtof("--1", &f));
411+
EXPECT_TRUE(absl::SimpleAtof("-1", &f));
412+
EXPECT_EQ(f, -1.f);
413+
EXPECT_TRUE(absl::SimpleAtof("+1", &f));
414+
EXPECT_EQ(f, +1.f);
415+
}
416+
395417
TEST(NumbersTest, Atoenum) {
396418
enum E01 {
397419
E01_zero = 0,

0 commit comments

Comments
 (0)