forked from pixie-io/pixie
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotobuf_text_format.patch
More file actions
73 lines (71 loc) · 2.93 KB
/
protobuf_text_format.patch
File metadata and controls
73 lines (71 loc) · 2.93 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc
index 2c0a95d..1234567 100644
--- a/src/google/protobuf/text_format.cc
+++ b/src/google/protobuf/text_format.cc
@@ -82,6 +82,18 @@ inline bool IsOctNumber(const std::string& str) {
(str[1] >= '0' && str[1] < '8'));
}
+// Returns true if truncation occurred.
+bool TruncateString(int64_t max_length, absl::string_view* s) {
+ if (max_length > 0) {
+ int64_t excess = static_cast<int64_t>(s->size()) - max_length;
+ if (excess > 0) {
+ s->remove_suffix(excess);
+ return true;
+ }
+ }
+ return false;
+}
+
// The number of fields that are redacted in AbslStringify.
std::atomic<int64_t> num_redacted_field{0};
@@ -2738,20 +2750,22 @@ void TextFormat::Printer::PrintFieldValue(const Message& message,
? reflection->GetRepeatedStringReference(message, field, index,
&scratch)
: reflection->GetStringReference(message, field, &scratch);
- const std::string* value_to_print = &value;
- std::string truncated_value;
- if (truncate_string_field_longer_than_ > 0 &&
- static_cast<size_t>(truncate_string_field_longer_than_) <
- value.size()) {
- truncated_value = value.substr(0, truncate_string_field_longer_than_) +
- "...<truncated>...";
- value_to_print = &truncated_value;
- }
+ absl::string_view value_to_print(value);
+ bool truncated = TruncateString(truncate_string_field_longer_than_, &value_to_print);
+
if (field->type() == FieldDescriptor::TYPE_STRING) {
- printer->PrintString(*value_to_print, generator);
+ if (truncated) {
+ printer->PrintString(absl::StrCat(value_to_print, "...<truncated>..."), generator);
+ } else {
+ printer->PrintString(value, generator);
+ }
} else {
ABSL_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_BYTES);
- printer->PrintBytes(*value_to_print, generator);
+ if (truncated) {
+ printer->PrintBytes(absl::StrCat(value_to_print, "...<truncated>..."), generator);
+ } else {
+ printer->PrintBytes(value, generator);
+ }
}
break;
}
@@ -2937,7 +2951,14 @@ void TextFormat::Printer::PrintUnknownFields(
break;
}
generator->PrintMaybeWithMarker(MarkerToken(), ": ", "\"");
- generator->PrintString(absl::CEscape(value));
+ absl::string_view value_to_print(value);
+ bool truncated = TruncateString(truncate_string_field_longer_than_, &value_to_print);
+
+ if (truncated) {
+ generator->PrintString(absl::StrCat(absl::CEscape(value_to_print), "...<truncated>..."));
+ } else {
+ generator->PrintString(absl::CEscape(value));
+ }
if (single_line_mode_) {
generator->PrintLiteral("\" ");
} else {