Skip to content

Commit c57f03c

Browse files
committed
refactor: Replace const char* to std::string
Some functions should be returning std::string instead of const char*. This commit changes that.
1 parent dc5333d commit c57f03c

File tree

9 files changed

+24
-13
lines changed

9 files changed

+24
-13
lines changed

src/bitcoin-cli.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <functional>
2121
#include <memory>
2222
#include <stdio.h>
23+
#include <string>
2324
#include <tuple>
2425

2526
#include <event2/buffer.h>
@@ -157,7 +158,7 @@ struct HTTPReply
157158
std::string body;
158159
};
159160

160-
static const char *http_errorstring(int code)
161+
static std::string http_errorstring(int code)
161162
{
162163
switch(code) {
163164
#if LIBEVENT_VERSION_NUMBER >= 0x02010300

src/core_read.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <boost/algorithm/string/split.hpp>
2020

2121
#include <algorithm>
22+
#include <string>
2223

2324
CScript ParseScript(const std::string& s)
2425
{
@@ -34,10 +35,9 @@ CScript ParseScript(const std::string& s)
3435
if (op < OP_NOP && op != OP_RESERVED)
3536
continue;
3637

37-
const char* name = GetOpName(static_cast<opcodetype>(op));
38-
if (strcmp(name, "OP_UNKNOWN") == 0)
38+
std::string strName = GetOpName(static_cast<opcodetype>(op));
39+
if (strName == "OP_UNKNOWN")
3940
continue;
40-
std::string strName(name);
4141
mapOpNames[strName] = static_cast<opcodetype>(op);
4242
// Convenience: OP_ADD and just ADD are both recognized:
4343
boost::algorithm::replace_first(strName, "OP_", "");

src/script/script.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <util/strencodings.h>
99

10-
const char* GetOpName(opcodetype opcode)
10+
#include <string>
11+
12+
std::string GetOpName(opcodetype opcode)
1113
{
1214
switch (opcode)
1315
{

src/script/script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ enum opcodetype
193193
// Maximum value that an opcode can be
194194
static const unsigned int MAX_OPCODE = OP_NOP10;
195195

196-
const char* GetOpName(opcodetype opcode);
196+
std::string GetOpName(opcodetype opcode);
197197

198198
class scriptnum_error : public std::runtime_error
199199
{

src/script/script_error.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
#include <script/script_error.h>
77

8-
const char* ScriptErrorString(const ScriptError serror)
8+
#include <string>
9+
10+
std::string ScriptErrorString(const ScriptError serror)
911
{
1012
switch (serror)
1113
{

src/script/script_error.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef BITCOIN_SCRIPT_SCRIPT_ERROR_H
77
#define BITCOIN_SCRIPT_SCRIPT_ERROR_H
88

9+
#include <string>
10+
911
typedef enum ScriptError_t
1012
{
1113
SCRIPT_ERR_OK = 0,
@@ -73,6 +75,6 @@ typedef enum ScriptError_t
7375

7476
#define SCRIPT_ERR_LAST SCRIPT_ERR_ERROR_COUNT
7577

76-
const char* ScriptErrorString(const ScriptError error);
78+
std::string ScriptErrorString(const ScriptError error);
7779

7880
#endif // BITCOIN_SCRIPT_SCRIPT_ERROR_H

src/script/standard.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <pubkey.h>
1010
#include <script/script.h>
1111

12+
#include <string>
13+
1214
typedef std::vector<unsigned char> valtype;
1315

1416
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
@@ -25,7 +27,7 @@ WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
2527
CSHA256().Write(in.data(), in.size()).Finalize(begin());
2628
}
2729

28-
const char* GetTxnOutputType(txnouttype t)
30+
std::string GetTxnOutputType(txnouttype t)
2931
{
3032
switch (t)
3133
{
@@ -39,7 +41,7 @@ const char* GetTxnOutputType(txnouttype t)
3941
case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
4042
case TX_WITNESS_UNKNOWN: return "witness_unknown";
4143
}
42-
return nullptr;
44+
assert(false);
4345
}
4446

4547
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)

src/script/standard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <boost/variant.hpp>
1313

14+
#include <string>
15+
1416

1517
static const bool DEFAULT_ACCEPT_DATACARRIER = true;
1618

@@ -146,7 +148,7 @@ typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash,
146148
bool IsValidDestination(const CTxDestination& dest);
147149

148150
/** Get the name of a txnouttype as a C string, or nullptr if unknown. */
149-
const char* GetTxnOutputType(txnouttype t);
151+
std::string GetTxnOutputType(txnouttype t);
150152

151153
/**
152154
* Parse a scriptPubKey and identify script type for standard scripts. If

src/test/script_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static ScriptErrorDesc script_errors[]={
102102
{SCRIPT_ERR_SIG_FINDANDDELETE, "SIG_FINDANDDELETE"},
103103
};
104104

105-
static const char *FormatScriptError(ScriptError_t err)
105+
static std::string FormatScriptError(ScriptError_t err)
106106
{
107107
for (unsigned int i=0; i<ARRAYLEN(script_errors); ++i)
108108
if (script_errors[i].err == err)
@@ -134,7 +134,7 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
134134
CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit);
135135
CMutableTransaction tx2 = tx;
136136
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue), &err) == expect, message);
137-
BOOST_CHECK_MESSAGE(err == scriptError, std::string(FormatScriptError(err)) + " where " + std::string(FormatScriptError((ScriptError_t)scriptError)) + " expected: " + message);
137+
BOOST_CHECK_MESSAGE(err == scriptError, FormatScriptError(err) + " where " + FormatScriptError((ScriptError_t)scriptError) + " expected: " + message);
138138

139139
// Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
140140
for (int i = 0; i < 16; ++i) {

0 commit comments

Comments
 (0)