Skip to content

Commit c81100b

Browse files
Abseil Teamcopybara-github
authored andcommitted
Add absl::StatusCodeToStringView.
PiperOrigin-RevId: 833435432 Change-Id: Id3d01c51765bb55bcee9bae4c7d879530b395c34
1 parent 01178c5 commit c81100b

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

absl/status/status.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ static_assert(
4747
"absl::Status assumes it can use the bottom 2 bits of a StatusRep*.");
4848

4949
std::string StatusCodeToString(StatusCode code) {
50+
return std::string(absl::StatusCodeToStringView(code));
51+
}
52+
53+
absl::string_view StatusCodeToStringView(StatusCode code) {
5054
switch (code) {
5155
case StatusCode::kOk:
5256
return "OK";

absl/status/status.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ enum class StatusCode : int {
284284
// Returns the name for the status code, or "" if it is an unknown value.
285285
std::string StatusCodeToString(StatusCode code);
286286

287+
// StatusCodeToStringView()
288+
//
289+
// Same as StatusCodeToString(), but returns a string_view.
290+
absl::string_view StatusCodeToStringView(StatusCode code);
291+
287292
// operator<<
288293
//
289294
// Streams StatusCodeToString(code) to `os`.

absl/status/status_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TEST(StatusCode, InsertionOperator) {
3939
std::ostringstream oss;
4040
oss << code;
4141
EXPECT_EQ(oss.str(), absl::StatusCodeToString(code));
42+
EXPECT_EQ(oss.str(), absl::StatusCodeToStringView(code));
4243
}
4344

4445
// This structure holds the details for testing a single error code,

0 commit comments

Comments
 (0)