-
Notifications
You must be signed in to change notification settings - Fork 10
/
StringUtils.cpp
47 lines (40 loc) · 968 Bytes
/
StringUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Useful string utility functions used by ccons.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include "StringUtils.h"
#include <stdlib.h>
#include <stdio.h>
namespace ccons {
// Like vsprintf(), but to a std::string.
void vstring_printf(std::string *dst, const char *fmt, va_list ap)
{
char *s;
if (vasprintf(&s, fmt, ap) > 0) {
dst->assign(s);
free(s);
}
}
// Like sprintf(), but to a std::string.
void string_printf(std::string *dst, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vstring_printf(dst, fmt, ap);
va_end(ap);
}
// Like printf(), but to a std::ostream.
void oprintf(std::ostream& out, const char *fmt, ...)
{
std::string str;
va_list ap;
va_start(ap, fmt);
vstring_printf(&str, fmt, ap);
va_end(ap);
out << str;
}
} // namespace ccons