Use std::to_string instead of snprintf

This commit is contained in:
Kim Grasman 2020-08-01 14:08:13 +02:00 committed by Kim Gräsman
parent 2d9bd45816
commit bff9327eb2
2 changed files with 4 additions and 18 deletions

View File

@ -89,7 +89,6 @@
#include <algorithm> // for swap, find, make_pair #include <algorithm> // for swap, find, make_pair
#include <cstddef> // for size_t #include <cstddef> // for size_t
#include <cstdio> // for snprintf
#include <cstdlib> // for atoi, exit #include <cstdlib> // for atoi, exit
#include <cstring> #include <cstring>
#include <deque> // for swap #include <deque> // for swap
@ -232,12 +231,6 @@ using std::vector;
namespace { namespace {
string IntToString(int i) {
char buf[64]; // big enough for any number
snprintf(buf, sizeof(buf), "%d", i);
return buf;
}
bool CanIgnoreLocation(SourceLocation loc) { bool CanIgnoreLocation(SourceLocation loc) {
// If we're in a macro expansion, we always want to treat this as // If we're in a macro expansion, we always want to treat this as
// being in the expansion location, never the as-written location, // being in the expansion location, never the as-written location,
@ -451,7 +444,7 @@ class BaseAstVisitor : public RecursiveASTVisitor<Derived> {
// etc. // etc.
string AnnotatedName(const string& name) const { string AnnotatedName(const string& name) const {
return (PrintableCurrentLoc() + ": (" + return (PrintableCurrentLoc() + ": (" +
IntToString(current_ast_node_->depth()) + GetSymbolAnnotation() + std::to_string(current_ast_node_->depth()) + GetSymbolAnnotation() +
(current_ast_node_->in_forward_declare_context() ? (current_ast_node_->in_forward_declare_context() ?
", fwd decl" : "") + ", fwd decl" : "") +
") [ " + name + " ] "); ") [ " + name + " ] ");

View File

@ -13,7 +13,6 @@
#include "iwyu_include_picker.h" #include "iwyu_include_picker.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <vector> #include <vector>
@ -36,27 +35,21 @@ namespace include_what_you_use {
namespace { namespace {
string IntToString(int i) {
char buf[64]; // big enough for any number
snprintf(buf, sizeof(buf), "%d", i);
return buf;
}
// Returns a string representing the first element where actual (a vector), // Returns a string representing the first element where actual (a vector),
// and expected (an array) differ, or "" if they're identical. // and expected (an array) differ, or "" if they're identical.
template <size_t kCount> string VectorDiff(const string (&expected)[kCount], template <size_t kCount> string VectorDiff(const string (&expected)[kCount],
const vector<string>& actual) { const vector<string>& actual) {
for (int i = 0; i < std::min(kCount, actual.size()); ++i) { for (int i = 0; i < std::min(kCount, actual.size()); ++i) {
if (expected[i] != actual[i]) { if (expected[i] != actual[i]) {
return ("Differ at #" + IntToString(i) + ": expected=" + expected[i] + return ("Differ at #" + std::to_string(i) + ": expected=" + expected[i] +
", actual=" + actual[i]); ", actual=" + actual[i]);
} }
} }
if (kCount < actual.size()) { if (kCount < actual.size()) {
return ("Differ at #" + IntToString(kCount) + return ("Differ at #" + std::to_string(kCount) +
": expected at EOF, actual=" + actual[kCount]); ": expected at EOF, actual=" + actual[kCount]);
} else if (actual.size() < kCount) { } else if (actual.size() < kCount) {
return ("Differ at #" + IntToString(kCount) + ": expected=" + return ("Differ at #" + std::to_string(kCount) + ": expected=" +
expected[actual.size()] + ", actual at EOF"); expected[actual.size()] + ", actual at EOF");
} else { } else {
return ""; return "";