aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/stubs/common.cc
diff options
context:
space:
mode:
authorGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-07-29 21:33:08 +0000
committerGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-07-29 21:33:08 +0000
commit4f3491ee5f0d2e6301988fa81714cce32ea4b461 (patch)
tree1b326068af7e39add870b186ced3e051d96e6600 /src/google/protobuf/stubs/common.cc
parent858a22be1122bf72ffd048f97c9d58d423cc41a1 (diff)
Decouple strutil from C++ lite library for a further 23k reduction.
Diffstat (limited to 'src/google/protobuf/stubs/common.cc')
-rw-r--r--src/google/protobuf/stubs/common.cc51
1 files changed, 37 insertions, 14 deletions
diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc
index 302be7ae..1e2d68d2 100644
--- a/src/google/protobuf/stubs/common.cc
+++ b/src/google/protobuf/stubs/common.cc
@@ -32,8 +32,6 @@
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/once.h>
-#include <google/protobuf/stubs/strutil.h>
-#include <google/protobuf/stubs/substitute.h>
#include <stdio.h>
#include <errno.h>
#include <vector>
@@ -43,6 +41,7 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN // We only need minimal includes
#include <windows.h>
+#define snprintf _snprintf // see comment in strutil.cc
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
#else
@@ -87,7 +86,15 @@ string VersionString(int version) {
int minor = (version / 1000) % 1000;
int micro = version % 1000;
- return strings::Substitute("$0.$1.$2", major, minor, micro);
+ // 128 bytes should always be enough, but we use snprintf() anyway to be
+ // safe.
+ char buffer[128];
+ snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
+
+ // Guard against broken MSVC snprintf().
+ buffer[sizeof(buffer)-1] = '\0';
+
+ return buffer;
}
} // namespace internal
@@ -131,23 +138,39 @@ void InitLogSilencerCountOnce() {
GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount);
}
-static string SimpleCtoa(char c) { return string(1, c); }
+LogMessage& LogMessage::operator<<(const string& value) {
+ message_ += value;
+ return *this;
+}
+
+LogMessage& LogMessage::operator<<(const char* value) {
+ message_ += value;
+ return *this;
+}
+// Since this is just for logging, we don't care if the current locale changes
+// the results -- in fact, we probably prefer that. So we use snprintf()
+// instead of Simple*toa().
#undef DECLARE_STREAM_OPERATOR
-#define DECLARE_STREAM_OPERATOR(TYPE, TOSTRING) \
+#define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \
LogMessage& LogMessage::operator<<(TYPE value) { \
- message_ += TOSTRING(value); \
+ /* 128 bytes should be big enough for any of the primitive */ \
+ /* values which we print with this, but well use snprintf() */ \
+ /* anyway to be extra safe. */ \
+ char buffer[128]; \
+ snprintf(buffer, sizeof(buffer), FORMAT, value); \
+ /* Guard against broken MSVC snprintf(). */ \
+ buffer[sizeof(buffer)-1] = '\0'; \
+ message_ += buffer; \
return *this; \
}
-DECLARE_STREAM_OPERATOR(const string&, )
-DECLARE_STREAM_OPERATOR(const char* , )
-DECLARE_STREAM_OPERATOR(char , SimpleCtoa)
-DECLARE_STREAM_OPERATOR(int , SimpleItoa)
-DECLARE_STREAM_OPERATOR(uint , SimpleItoa)
-DECLARE_STREAM_OPERATOR(long , SimpleItoa)
-DECLARE_STREAM_OPERATOR(unsigned long, SimpleItoa)
-DECLARE_STREAM_OPERATOR(double , SimpleDtoa)
+DECLARE_STREAM_OPERATOR(char , "%c" )
+DECLARE_STREAM_OPERATOR(int , "%d" )
+DECLARE_STREAM_OPERATOR(uint , "%u" )
+DECLARE_STREAM_OPERATOR(long , "%ld")
+DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
+DECLARE_STREAM_OPERATOR(double , "%g" )
#undef DECLARE_STREAM_OPERATOR
LogMessage::LogMessage(LogLevel level, const char* filename, int line)