Skip to content

Commit efab298

Browse files
majnemertensorflower-gardener
authored andcommitted
Refactor: Use absl::ascii_* functions and improve string parsing.
This change replaces manual character range checks (e.g., `c >= '0' && c <= '9'`) with more idiomatic and potentially safer `absl::ascii_isdigit`, `absl::ascii_isalpha`, and `absl::ascii_isalnum` functions. Additionally, it refactors `ParseTensorName` to use `absl::string_view` methods and `tsl::str_util::ConsumeLeadingDigits`, and updates `ConsumeLeadingDigits` to use `std::from_chars`. The unused `Strnlen` function is also removed. PiperOrigin-RevId: 829600440
1 parent c4ff788 commit efab298

File tree

17 files changed

+67
-108
lines changed

17 files changed

+67
-108
lines changed

tensorflow/core/framework/ops_util.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16+
#include "tensorflow/core/framework/ops_util.h"
17+
1618
#include <algorithm>
1719
#include <cmath>
1820

21+
#include "absl/strings/ascii.h"
1922
#include "tensorflow/core/framework/attr_value.pb.h"
20-
#include "tensorflow/core/framework/ops_util.h"
2123
#include "tensorflow/core/lib/core/errors.h"
2224
#include "tensorflow/core/lib/strings/str_util.h"
2325
#include "tensorflow/core/util/padding.h"
@@ -66,8 +68,7 @@ string SanitizeThreadSuffix(string suffix) {
6668
string clean;
6769
for (int i = 0; i < suffix.size(); ++i) {
6870
const char ch = suffix[i];
69-
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
70-
(ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
71+
if (absl::ascii_isalnum(ch) || ch == '_' || ch == '-') {
7172
clean += ch;
7273
} else {
7374
clean += '_';

tensorflow/core/graph/tensor_id.cc

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ limitations under the License.
1515

1616
#include "tensorflow/core/graph/tensor_id.h"
1717

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

20-
#include "tensorflow/core/lib/core/stringpiece.h"
21-
#include "tensorflow/core/lib/strings/str_util.h"
22+
#include "absl/strings/string_view.h"
23+
#include "absl/strings/strip.h"
24+
#include "tensorflow/core/graph/graph.h"
25+
#include "tensorflow/core/platform/str_util.h"
2226

2327
namespace tensorflow {
2428

2529
TensorId::TensorId(const SafeTensorId& id) : TensorId(id.first, id.second) {}
2630

2731
SafeTensorId::SafeTensorId(const TensorId& id)
28-
: SafeTensorId(string(id.first), id.second) {}
29-
30-
TensorId ParseTensorName(const string& name) {
31-
return ParseTensorName(absl::string_view(name.data(), name.size()));
32-
}
32+
: SafeTensorId(std::string(id.first), id.second) {}
3333

3434
TensorId ParseTensorName(absl::string_view name) {
3535
// Parse either a name, ^name, or name:digits. To do so, we go backwards from
@@ -38,28 +38,19 @@ TensorId ParseTensorName(absl::string_view name) {
3838
// see if the name starts with '^', indicating a control edge. If we find
3939
// neither ':' nor '^' characters, the output index is implicitly 0, and the
4040
// whole name string forms the first part of the tensor name.
41-
const char* base = name.data();
42-
const char* p = base + name.size() - 1;
43-
unsigned int index = 0;
44-
unsigned int mul = 1;
45-
while (p > base && (*p >= '0' && *p <= '9')) {
46-
index += ((*p - '0') * mul);
47-
mul *= 10;
48-
p--;
41+
size_t colon_pos = name.rfind(':');
42+
if (colon_pos != absl::string_view::npos) {
43+
absl::string_view prefix = name.substr(0, colon_pos);
44+
absl::string_view suffix = name.substr(colon_pos + 1);
45+
uint64_t index;
46+
if (str_util::ConsumeLeadingDigits(&suffix, &index) && suffix.empty()) {
47+
return TensorId(prefix, index);
48+
}
4949
}
50-
TensorId id;
51-
if (p > base && *p == ':' && mul > 1) {
52-
id.first = absl::string_view(base, p - base);
53-
id.second = index;
54-
} else if (absl::StartsWith(name, "^")) {
55-
// Control edge
56-
id.first = absl::string_view(base + 1);
57-
id.second = Graph::kControlSlot;
58-
} else {
59-
id.first = name;
60-
id.second = 0;
50+
if (absl::ConsumePrefix(&name, "^")) {
51+
return TensorId(name, Graph::kControlSlot);
6152
}
62-
return id;
53+
return TensorId(name, 0);
6354
}
6455

6556
bool IsTensorIdControl(const TensorId& tensor_id) {

tensorflow/core/graph/tensor_id.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ struct TensorId : public std::pair<absl::string_view, int> {
5757
};
5858
};
5959

60-
TensorId ParseTensorName(const string& name);
6160
TensorId ParseTensorName(absl::string_view name);
6261

6362
bool IsTensorIdControl(const TensorId& tensor_id);

tensorflow/core/lib/db/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cc_library(
2525
"@com_google_absl//absl/log",
2626
"@com_google_absl//absl/log:check",
2727
"@com_google_absl//absl/status",
28+
"@com_google_absl//absl/strings",
2829
"@com_google_absl//absl/strings:str_format",
2930
"@org_sqlite",
3031
],

tensorflow/core/lib/db/sqlite.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
#include "absl/log/check.h"
2020
#include "absl/log/log.h"
2121
#include "absl/status/status.h"
22+
#include "absl/strings/ascii.h"
2223
#include "absl/strings/str_format.h"
2324
#include "tensorflow/core/lib/core/errors.h"
2425
#include "tensorflow/core/platform/status.h"
@@ -99,8 +100,7 @@ absl::Status SetPragma(Sqlite* db, const char* pragma,
99100
const absl::string_view& value) {
100101
if (value.empty()) return absl::OkStatus();
101102
for (auto p = value.begin(); p < value.end(); ++p) {
102-
if (!(('0' <= *p && *p <= '9') || ('A' <= *p && *p <= 'Z') ||
103-
('a' <= *p && *p <= 'z') || *p == '-')) {
103+
if (!(absl::ascii_isalnum(*p) || *p == '-')) {
104104
return errors::InvalidArgument("Illegal pragma character");
105105
}
106106
}

tensorflow/core/platform/str_util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ using tsl::str_util::StringReplace;
5151
using tsl::str_util::StripPrefix;
5252
using tsl::str_util::StripSuffix;
5353
using tsl::str_util::StripTrailingWhitespace;
54-
using tsl::str_util::Strnlen;
5554
using tsl::str_util::TitlecaseString;
5655
using tsl::str_util::Uppercase;
5756
// NOLINTEND(misc-unused-using-decls)

tensorflow/core/util/memmapped_file_system.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ bool MemmappedFileSystem::IsMemmappedPackageFilename(
269269

270270
namespace {
271271
bool IsValidRegionChar(char c) {
272-
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
273-
(c >= '0' && c <= '9') || c == '_' || c == '.';
272+
return absl::ascii_isalnum(c) || c == '_' || c == '.';
274273
}
275274
} // namespace
276275

tensorflow/core/util/semver_test.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515

1616
#include <cstddef>
1717

18+
#include "absl/strings/ascii.h"
1819
#include "absl/strings/strip.h"
1920
#include "tensorflow/core/lib/core/stringpiece.h"
2021
#include "tensorflow/core/lib/strings/str_util.h"
@@ -27,10 +28,7 @@ namespace {
2728
bool IsDotOrIdentifierChar(char c) {
2829
if (c == '.') return true;
2930
if (c == '-') return true;
30-
if (c >= 'A' && c <= 'Z') return true;
31-
if (c >= 'a' && c <= 'z') return true;
32-
if (c >= '0' && c <= '9') return true;
33-
return false;
31+
return absl::ascii_isalnum(c);
3432
}
3533

3634
bool ConsumeDotSeparatedIdentifiers(absl::string_view* s,

tensorflow/security/fuzzing/cc/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tf_cc_fuzz_test(
3333
deps = [
3434
"//tensorflow/core/platform:str_util",
3535
"//tensorflow/core/platform:stringpiece",
36+
"@com_google_absl//absl/strings",
3637
],
3738
)
3839

tensorflow/security/fuzzing/cc/arg_def_case_fuzz.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#include <string_view>
1818

1919
#include "fuzztest/fuzztest.h"
20+
#include "absl/strings/ascii.h"
2021
#include "tensorflow/core/platform/str_util.h"
2122
#include "tensorflow/core/platform/stringpiece.h"
2223

@@ -27,8 +28,8 @@ namespace {
2728
void FuzzTest(std::string_view data) {
2829
std::string ns = tensorflow::str_util::ArgDefCase(data);
2930
for (const auto &c : ns) {
30-
const bool is_letter = 'a' <= c && c <= 'z';
31-
const bool is_digit = '0' <= c && c <= '9';
31+
const bool is_letter = absl::ascii_isalpha(c);
32+
const bool is_digit = absl::ascii_isdigit(c);
3233
if (!is_letter && !is_digit) {
3334
assert(c == '_');
3435
}

0 commit comments

Comments
 (0)