stringprintf.h 2.1 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef BUTIL_STRINGS_STRINGPRINTF_H_
#define BUTIL_STRINGS_STRINGPRINTF_H_
gejun's avatar
gejun committed
7 8 9 10 11

#include <stdarg.h>   // va_list

#include <string>

12 13
#include "butil/base_export.h"
#include "butil/compiler_specific.h"
gejun's avatar
gejun committed
14

15
namespace butil {
gejun's avatar
gejun committed
16 17

// Return a C++ string given printf-like input.
18
BUTIL_EXPORT std::string StringPrintf(const char* format, ...)
gejun's avatar
gejun committed
19 20 21
    PRINTF_FORMAT(1, 2);
// OS_ANDROID's libc does not support wchar_t, so several overloads are omitted.
#if !defined(OS_ANDROID)
22
BUTIL_EXPORT std::wstring StringPrintf(const wchar_t* format, ...)
gejun's avatar
gejun committed
23 24 25 26
    WPRINTF_FORMAT(1, 2);
#endif

// Return a C++ string given vprintf-like input.
27
BUTIL_EXPORT std::string StringPrintV(const char* format, va_list ap)
gejun's avatar
gejun committed
28 29 30
    PRINTF_FORMAT(1, 0);

// Store result into a supplied string and return it.
31
BUTIL_EXPORT const std::string& SStringPrintf(std::string* dst,
gejun's avatar
gejun committed
32 33 34
                                             const char* format, ...)
    PRINTF_FORMAT(2, 3);
#if !defined(OS_ANDROID)
35
BUTIL_EXPORT const std::wstring& SStringPrintf(std::wstring* dst,
gejun's avatar
gejun committed
36 37 38 39 40
                                              const wchar_t* format, ...)
    WPRINTF_FORMAT(2, 3);
#endif

// Append result to a supplied string.
41
BUTIL_EXPORT void StringAppendF(std::string* dst, const char* format, ...)
gejun's avatar
gejun committed
42 43 44 45
    PRINTF_FORMAT(2, 3);
#if !defined(OS_ANDROID)
// TODO(evanm): this is only used in a few places in the code;
// replace with string16 version.
46
BUTIL_EXPORT void StringAppendF(std::wstring* dst, const wchar_t* format, ...)
gejun's avatar
gejun committed
47 48 49 50 51
    WPRINTF_FORMAT(2, 3);
#endif

// Lower-level routine that takes a va_list and appends to a specified
// string.  All other routines are just convenience wrappers around it.
52
BUTIL_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap)
gejun's avatar
gejun committed
53 54
    PRINTF_FORMAT(2, 0);
#if !defined(OS_ANDROID)
55
BUTIL_EXPORT void StringAppendV(std::wstring* dst,
gejun's avatar
gejun committed
56 57 58 59
                               const wchar_t* format, va_list ap)
    WPRINTF_FORMAT(2, 0);
#endif

60
}  // namespace butil
gejun's avatar
gejun committed
61

62
#endif  // BUTIL_STRINGS_STRINGPRINTF_H_