stringprintf.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_STRINGPRINTF_H_
6#define BASE_STRINGPRINTF_H_
7
8#include <stdarg.h>   // va_list
9
10#include <string>
11
12#include "base/compiler_specific.h"
13
14namespace base {
15
16// Return a C++ string given printf-like input.
17std::string StringPrintf(const char* format, ...) PRINTF_FORMAT(1, 2);
18std::wstring StringPrintf(const wchar_t* format, ...) WPRINTF_FORMAT(1, 2);
19
20// Return a C++ string given vprintf-like input.
21std::string StringPrintV(const char* format, va_list ap) PRINTF_FORMAT(1, 0);
22
23// Store result into a supplied string and return it.
24const std::string& SStringPrintf(std::string* dst, const char* format, ...)
25    PRINTF_FORMAT(2, 3);
26const std::wstring& SStringPrintf(std::wstring* dst,
27                                  const wchar_t* format, ...)
28    WPRINTF_FORMAT(2, 3);
29
30// Append result to a supplied string.
31void StringAppendF(std::string* dst, const char* format, ...)
32    PRINTF_FORMAT(2, 3);
33void StringAppendF(std::wstring* dst, const wchar_t* format, ...)
34    WPRINTF_FORMAT(2, 3);
35
36// Lower-level routine that takes a va_list and appends to a specified
37// string.  All other routines are just convenience wrappers around it.
38void StringAppendV(std::string* dst, const char* format, va_list ap)
39    PRINTF_FORMAT(2, 0);
40void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap)
41    WPRINTF_FORMAT(2, 0);
42
43}  // namespace base
44
45// Don't require the namespace for legacy code. New code should use "base::" or
46// have its own using decl.
47//
48// TODO(brettw) remove these when calling code is converted.
49using base::StringPrintf;
50using base::StringPrintV;
51using base::SStringPrintf;
52using base::StringAppendV;
53using base::StringAppendF;
54using base::StringAppendV;
55
56#endif  // BASE_STRINGPRINTF_H_
57