Skip to content

Commit f72517e

Browse files
allanrenuccicopybara-github
authored andcommitted
Deprecate tsl::strings::Hex in favor of absl::Hex.
PiperOrigin-RevId: 751542019
1 parent d2d1190 commit f72517e

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

tsl/platform/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,9 @@ cc_library(
773773
deps = [
774774
":numbers",
775775
":stringpiece",
776+
"@com_google_absl//absl/base:core_headers",
776777
"@com_google_absl//absl/meta:type_traits",
778+
"@com_google_absl//absl/strings",
777779
"@xla//xla/tsl/platform:logging",
778780
"@xla//xla/tsl/platform:macros",
779781
"@xla//xla/tsl/platform:types",
@@ -1365,6 +1367,7 @@ tsl_cc_test(
13651367
"strcat_test.cc",
13661368
],
13671369
deps = [
1370+
":bfloat16",
13681371
":strcat",
13691372
":stringprintf",
13701373
"@com_google_absl//absl/strings",

tsl/platform/strcat.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ AlphaNum::AlphaNum(Hex hex) {
3232
char *const end = &digits_[kFastToBufferSize];
3333
char *writer = end;
3434
uint64 value = hex.value;
35-
uint64 width = hex.spec;
35+
uint64 width = hex.width;
3636
// We accomplish minimum width by OR'ing in 0x10000 to the user's value,
3737
// where 0x10000 is the smallest hex number that is as wide as the user
3838
// asked for.

tsl/platform/strcat.h

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ limitations under the License.
2222

2323
#include <string>
2424

25+
#include "absl/base/attributes.h"
26+
#include "absl/base/macros.h"
27+
#include "absl/strings/str_cat.h"
2528
#include "xla/tsl/platform/macros.h"
2629
#include "xla/tsl/platform/types.h"
2730
#include "tsl/platform/numbers.h"
@@ -52,47 +55,30 @@ limitations under the License.
5255
// You can convert to Hexadecimal output rather than Decimal output using Hex.
5356
// To do this, pass strings::Hex(my_int) as a parameter to StrCat. You may
5457
// specify a minimum field width using a separate parameter, so the equivalent
55-
// of Printf("%04x", my_int) is StrCat(Hex(my_int, strings::kZeroPad4))
58+
// of Printf("%04x", my_int) is StrCat(Hex(my_int, absl::kZeroPad4))
5659
//
5760
// This class has implicit constructors.
5861
namespace tsl {
5962
namespace strings {
6063

61-
enum PadSpec {
62-
kNoPad = 1,
63-
kZeroPad2,
64-
kZeroPad3,
65-
kZeroPad4,
66-
kZeroPad5,
67-
kZeroPad6,
68-
kZeroPad7,
69-
kZeroPad8,
70-
kZeroPad9,
71-
kZeroPad10,
72-
kZeroPad11,
73-
kZeroPad12,
74-
kZeroPad13,
75-
kZeroPad14,
76-
kZeroPad15,
77-
kZeroPad16
78-
};
79-
80-
struct Hex {
81-
uint64 value;
82-
enum PadSpec spec;
83-
template <class Int>
84-
explicit Hex(Int v, PadSpec s = kNoPad) : spec(s) {
85-
// Prevent sign-extension by casting integers to
86-
// their unsigned counterparts.
87-
static_assert(
88-
sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
89-
"Unknown integer type");
90-
value = sizeof(v) == 1 ? static_cast<uint8>(v)
91-
: sizeof(v) == 2 ? static_cast<uint16>(v)
92-
: sizeof(v) == 4 ? static_cast<uint32>(v)
93-
: static_cast<uint64>(v);
94-
}
95-
};
64+
using PadSpec ABSL_DEPRECATE_AND_INLINE() = absl::PadSpec;
65+
using absl::kNoPad;
66+
using absl::kZeroPad10;
67+
using absl::kZeroPad11;
68+
using absl::kZeroPad12;
69+
using absl::kZeroPad13;
70+
using absl::kZeroPad14;
71+
using absl::kZeroPad15;
72+
using absl::kZeroPad16;
73+
using absl::kZeroPad2;
74+
using absl::kZeroPad3;
75+
using absl::kZeroPad4;
76+
using absl::kZeroPad5;
77+
using absl::kZeroPad6;
78+
using absl::kZeroPad7;
79+
using absl::kZeroPad8;
80+
using absl::kZeroPad9;
81+
using Hex ABSL_DEPRECATE_AND_INLINE() = absl::Hex;
9682

9783
class AlphaNum {
9884
// NOLINTBEGIN(google-explicit-constructor)
@@ -121,10 +107,10 @@ class AlphaNum {
121107

122108
AlphaNum(Hex hex); // NOLINT(runtime/explicit)
123109

124-
AlphaNum(const char *c_str) : piece_(c_str) {} // NOLINT(runtime/explicit)
110+
AlphaNum(const char *c_str) : piece_(c_str) {} // NOLINT(runtime/explicit)
125111
AlphaNum(const absl::string_view &pc)
126-
: piece_(pc) {} // NOLINT(runtime/explicit)
127-
AlphaNum(const std::string &str) // NOLINT(runtime/explicit)
112+
: piece_(pc) {} // NOLINT(runtime/explicit)
113+
AlphaNum(const std::string &str) // NOLINT(runtime/explicit)
128114
: piece_(str) {}
129115
AlphaNum(const tstring &str) // NOLINT(runtime/explicit)
130116
: piece_(str) {}

tsl/platform/strcat_test.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ limitations under the License.
1515

1616
#include "tsl/platform/strcat.h"
1717

18+
#include <cstddef>
19+
#include <cstdint>
1820
#include <string>
1921

22+
#include "absl/strings/str_cat.h"
2023
#include "absl/strings/string_view.h"
2124
#include "xla/tsl/platform/test.h"
2225
#include "xla/tsl/platform/types.h"
26+
#include "tsl/platform/bfloat16.h"
2327
#include "tsl/platform/stringprintf.h"
2428

2529
#ifdef _MSC_VER
@@ -336,35 +340,35 @@ TEST(StrAppend, Death) {
336340
}
337341

338342
static void CheckHex64(uint64 v) {
339-
string actual = StrCat(Hex(v, kZeroPad16));
343+
string actual = strings::StrCat(absl::Hex(v, absl::kZeroPad16));
340344
string expected = Printf("%016llx", static_cast<unsigned long long>(v));
341345
EXPECT_EQ(expected, actual) << " decimal value " << v;
342346

343-
actual = StrCat(Hex(v, kZeroPad8));
347+
actual = strings::StrCat(absl::Hex(v, absl::kZeroPad8));
344348
expected = Printf("%08llx", static_cast<unsigned long long>(v));
345349
EXPECT_EQ(expected, actual) << " decimal value " << v;
346350

347-
actual = StrCat(Hex(v));
351+
actual = strings::StrCat(absl::Hex(v));
348352
expected = Printf("%llx", static_cast<unsigned long long>(v));
349353
EXPECT_EQ(expected, actual) << " decimal value " << v;
350354
}
351355

352356
static void CheckHex32(uint32 v) {
353-
string actual = StrCat(Hex(v, kZeroPad8));
357+
string actual = strings::StrCat(absl::Hex(v, absl::kZeroPad8));
354358
string expected = Printf("%08x", v);
355359
EXPECT_EQ(expected, actual) << " decimal value " << v;
356360

357-
actual = StrCat(Hex(v));
361+
actual = strings::StrCat(absl::Hex(v));
358362
expected = Printf("%x", v);
359363
EXPECT_EQ(expected, actual) << " decimal value " << v;
360364
}
361365

362366
static void CheckHexSigned32(int32_t v) {
363-
string actual = StrCat(Hex(v, kZeroPad8));
367+
string actual = strings::StrCat(absl::Hex(v, absl::kZeroPad8));
364368
string expected = Printf("%08x", v);
365369
EXPECT_EQ(expected, actual) << " decimal value " << v;
366370

367-
actual = StrCat(Hex(v));
371+
actual = strings::StrCat(absl::Hex(v));
368372
expected = Printf("%x", v);
369373
EXPECT_EQ(expected, actual) << " decimal value " << v;
370374
}
@@ -381,10 +385,10 @@ static void TestFastPrints() {
381385
CheckHex32(0x12345678);
382386

383387
int8_t minus_one_8bit = -1;
384-
EXPECT_EQ("ff", StrCat(Hex(minus_one_8bit)));
388+
EXPECT_EQ("ff", strings::StrCat(absl::Hex(minus_one_8bit)));
385389

386390
int16_t minus_one_16bit = -1;
387-
EXPECT_EQ("ffff", StrCat(Hex(minus_one_16bit)));
391+
EXPECT_EQ("ffff", strings::StrCat(absl::Hex(minus_one_16bit)));
388392
}
389393

390394
TEST(Numbers, TestFunctionsMovedOverFromNumbersMain) { TestFastPrints(); }

0 commit comments

Comments
 (0)