Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(776)

Unified Diff: base/strings/safe_sprintf.h

Issue 665483002: SafeSPrintf: use C++ variadic template (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use sizeof... Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/strings/OWNERS ('k') | base/strings/safe_sprintf_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/strings/safe_sprintf.h
diff --git a/base/strings/safe_sprintf.h b/base/strings/safe_sprintf.h
index cbd7d0cd6435e202a4c427c0b70f51bbb72c8009..2d173202d3e78b52fcdb9b5a23b272f956b60c97 100644
--- a/base/strings/safe_sprintf.h
+++ b/base/strings/safe_sprintf.h
@@ -218,220 +218,20 @@ BASE_EXPORT size_t GetSafeSPrintfSSizeMaxForTest();
} // namespace internal
-// TODO(markus): C++11 has a much more concise and readable solution for
-// expressing what we are doing here.
-
-template<class T0, class T1, class T2, class T3, class T4,
- class T5, class T6, class T7, class T8, class T9>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N,
- class T0, class T1, class T2, class T3, class T4,
- class T5, class T6, class T7, class T8, class T9>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3, class T4,
- class T5, class T6, class T7, class T8>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7, T8 arg8) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N,
- class T0, class T1, class T2, class T3, class T4, class T5,
- class T6, class T7, class T8>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7, T8 arg8) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3, class T4, class T5,
- class T6, class T7>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N,
- class T0, class T1, class T2, class T3, class T4, class T5,
- class T6, class T7>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6, T7 arg7) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3, class T4, class T5,
- class T6>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
- T5 arg5, T6 arg6) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N,
- class T0, class T1, class T2, class T3, class T4, class T5,
- class T6>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5,
- T6 arg6) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = {
- arg0, arg1, arg2, arg3, arg4, arg5, arg6
- };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3, class T4, class T5>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4, arg5 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N,
- class T0, class T1, class T2, class T3, class T4, class T5>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4, arg5 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3, class T4>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N, class T0, class T1, class T2, class T3, class T4>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1,
- T2 arg2, T3 arg3, T4 arg4) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2, class T3>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N, class T0, class T1, class T2, class T3>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
- T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1, class T2>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
- T0 arg0, T1 arg1, T2 arg2) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N, class T0, class T1, class T2>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1,
- T2 arg2) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1, arg2 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0, class T1>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, T0 arg0, T1 arg1) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<size_t N, class T0, class T1>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1) {
- // Use Arg() object to record type information and then copy arguments to an
- // array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0, arg1 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
-}
-
-template<class T0>
-ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, T0 arg0) {
+template<typename... Args>
+ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, Args... args) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
+ const internal::Arg arg_array[] = { args... };
+ return internal::SafeSNPrintf(buf, N, fmt, arg_array, sizeof...(args));
}
-template<size_t N, class T0>
-ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0) {
+template<size_t N, typename... Args>
+ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, Args... args) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
- const internal::Arg arg_array[] = { arg0 };
- return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
+ const internal::Arg arg_array[] = { args... };
+ return internal::SafeSNPrintf(buf, N, fmt, arg_array, sizeof...(args));
}
// Fast-path when we don't actually need to substitute any arguments.
« no previous file with comments | « base/strings/OWNERS ('k') | base/strings/safe_sprintf_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698