Skip to content

Commit 9aad37c

Browse files
gharibiantensorflower-gardener
authored andcommitted
Migrated tensorflow/core/platform/ to use tstring.
Updated char* tstring::data() to conform to C++11. This is a part of a larger migration effort for tensorflow::tstring. See: tensorflow/community#91 PiperOrigin-RevId: 262752692
1 parent c63c04a commit 9aad37c

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

tensorflow/core/platform/tensor_coding.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void AssignRefCounted(StringPiece src, core::RefCounted* obj, string* out) {
3333
out->assign(src.data(), src.size());
3434
}
3535

36-
void EncodeStringList(const string* strings, int64 n, string* out) {
36+
void EncodeStringList(const tstring* strings, int64 n, string* out) {
3737
out->clear();
3838
for (int i = 0; i < n; ++i) {
3939
core::PutVarint32(out, strings[i].size());
@@ -43,7 +43,7 @@ void EncodeStringList(const string* strings, int64 n, string* out) {
4343
}
4444
}
4545

46-
bool DecodeStringList(const string& src, string* strings, int64 n) {
46+
bool DecodeStringList(const string& src, tstring* strings, int64 n) {
4747
std::vector<uint32> sizes(n);
4848
StringPiece reader(src);
4949
int64 tot = 0;
@@ -55,7 +55,7 @@ bool DecodeStringList(const string& src, string* strings, int64 n) {
5555
return false;
5656
}
5757

58-
string* data = strings;
58+
tstring* data = strings;
5959
for (int64 i = 0; i < n; ++i, ++data) {
6060
auto size = sizes[i];
6161
if (size > reader.size()) {
@@ -144,7 +144,7 @@ void AssignRefCounted(StringPiece src, core::RefCounted* obj, Cord* out) {
144144
cleanup);
145145
}
146146

147-
void EncodeStringList(const string* strings, int64 n, Cord* out) {
147+
void EncodeStringList(const tstring* strings, int64 n, Cord* out) {
148148
out->Clear();
149149
for (int i = 0; i < n; ++i) {
150150
::strings::CordAppendVarint(strings[i].size(), out);
@@ -154,7 +154,7 @@ void EncodeStringList(const string* strings, int64 n, Cord* out) {
154154
}
155155
}
156156

157-
bool DecodeStringList(const Cord& src, string* strings, int64 n) {
157+
bool DecodeStringList(const Cord& src, tstring* strings, int64 n) {
158158
std::vector<uint32> sizes(n);
159159
CordReader reader(src);
160160
int64 tot = 0;
@@ -165,14 +165,27 @@ bool DecodeStringList(const Cord& src, string* strings, int64 n) {
165165
if (tot != reader.Available()) {
166166
return false;
167167
}
168-
string* data = strings;
168+
tstring* data = strings;
169169
for (int i = 0; i < n; ++i, ++data) {
170170
auto size = sizes[i];
171171
if (size > reader.Available()) {
172172
return false;
173173
}
174+
#ifdef USE_TSTRING
175+
// TODO(dero): Consider adding resize_uninitialized() to tstring once the
176+
// tstring placeholder is replaced with the actual implementation.
177+
//
178+
// Currently, in the case of USE_TSTRING, the placeholder tstring class
179+
// encapsulates a single std::string. We avoid using
180+
// gtl::STLStringResizeUninitialized (and its associated header-include) in
181+
// tstring.h as we have no intention in using it in the actual
182+
// implementation. Thus, in the interim, we resort to resize().
183+
data->resize(size);
184+
reader.ReadN(size, data->data());
185+
#else // USE_TSTRING
174186
gtl::STLStringResizeUninitialized(data, size);
175187
reader.ReadN(size, gtl::string_as_array(data));
188+
#endif // USE_TSTRING
176189
}
177190
return true;
178191
}

tensorflow/core/platform/tensor_coding.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ inline void CopySubrangeToArray(const string& src, size_t pos, size_t n,
4747
}
4848

4949
// Store encoding of strings[0..n-1] in *out.
50-
void EncodeStringList(const string* strings, int64 n, string* out);
50+
void EncodeStringList(const tstring* strings, int64 n, string* out);
5151

5252
// Decode n strings from src and store in strings[0..n-1].
5353
// Returns true if successful, false on parse error.
54-
bool DecodeStringList(const string& src, string* strings, int64 n);
54+
bool DecodeStringList(const string& src, tstring* strings, int64 n);
5555

5656
// Assigns base[0..bytes-1] to *s
5757
void CopyFromArray(string* s, const char* base, size_t bytes);
@@ -112,11 +112,11 @@ inline void CopySubrangeToArray(const Cord& src, int64 pos, int64 n,
112112
}
113113

114114
// Store encoding of strings[0..n-1] in *out.
115-
void EncodeStringList(const string* strings, int64 n, Cord* out);
115+
void EncodeStringList(const tstring* strings, int64 n, Cord* out);
116116

117117
// Decode n strings from src and store in strings[0..n-1].
118118
// Returns true if successful, false on parse error.
119-
bool DecodeStringList(const Cord& src, string* strings, int64 n);
119+
bool DecodeStringList(const Cord& src, tstring* strings, int64 n);
120120

121121
// Assigns base[0..bytes-1] to *c
122122
void CopyFromArray(Cord* c, const char* base, size_t bytes);

tensorflow/core/platform/tstring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class tstring {
123123

124124
const char& operator[](size_t i) const { return str_[i]; }
125125

126-
char* data() { return str_.data(); }
126+
char* data() { return &str_[0]; }
127127

128128
char& operator[](size_t i) { return str_[i]; }
129129

0 commit comments

Comments
 (0)