xref: /aosp_15_r20/external/federated-compute/fcp/base/string_stream.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 #include "fcp/base/string_stream.h"
2 
3 #include <stdio.h>
4 
5 #include <string>
6 
7 namespace fcp {
8 namespace internal {
9 
operator <<(bool x)10 StringStream& StringStream::operator<<(bool x) {
11   return *this << (x ? "true" : "false");
12 }
13 
14 template <typename T>
AppendWithFormat(StringStream & string_buffer,const char * format,T value)15 StringStream& AppendWithFormat(StringStream& string_buffer, const char* format,
16                                T value) {
17   char buf[64];
18   // The buffer is large enough for any possible value.
19   sprintf(buf, format, value);  // NOLINT
20   return string_buffer << buf;
21 }
22 
operator <<(int32_t x)23 StringStream& StringStream::operator<<(int32_t x) {
24   return AppendWithFormat(*this, "%d", x);
25 }
26 
operator <<(uint32_t x)27 StringStream& StringStream::operator<<(uint32_t x) {
28   return AppendWithFormat(*this, "%u", x);
29 }
30 
operator <<(int64_t x)31 StringStream& StringStream::operator<<(int64_t x) {
32   return AppendWithFormat(*this, "%ld", x);
33 }
34 
operator <<(uint64_t x)35 StringStream& StringStream::operator<<(uint64_t x) {
36   return AppendWithFormat(*this, "%lu", x);
37 }
38 
operator <<(double x)39 StringStream& StringStream::operator<<(double x) {
40   return AppendWithFormat(*this, "%f", x);
41 }
42 
operator <<(const char * x)43 StringStream& StringStream::operator<<(const char* x) {
44   str_.append(x);
45   return *this;
46 }
47 
operator <<(const std::string & x)48 StringStream& StringStream::operator<<(const std::string& x) {
49   str_.append(x);
50   return *this;
51 }
52 
53 }  // namespace internal
54 }  // namespace fcp
55