aboutsummaryrefslogtreecommitdiff
path: root/src/core_read.cpp
diff options
context:
space:
mode:
authorJoão Barbosa <joao.paulo.barbosa@gmail.com>2020-08-31 19:29:22 +0100
committerJoão Barbosa <joao.paulo.barbosa@gmail.com>2020-10-06 12:34:05 +0100
commitc92387232f750397da7d131f262c150a608408c2 (patch)
tree54b3f5a0d00da1bd9272209185bcb337ab84e261 /src/core_read.cpp
parent89a8299a14af68c1f96ca1650cbfd4fc2952e77b (diff)
downloadbitcoin-c92387232f750397da7d131f262c150a608408c2.tar.xz
refactor: Extract ParseOpCode from ParseScript
A second lookup in mapOpNames is also removed.
Diffstat (limited to 'src/core_read.cpp')
-rw-r--r--src/core_read.cpp25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/core_read.cpp b/src/core_read.cpp
index 1c0a8a096d..fbcb0d5e3b 100644
--- a/src/core_read.cpp
+++ b/src/core_read.cpp
@@ -21,10 +21,10 @@
#include <algorithm>
#include <string>
-CScript ParseScript(const std::string& s)
-{
- CScript result;
+namespace {
+opcodetype ParseOpCode(const std::string& s)
+{
static std::map<std::string, opcodetype> mapOpNames;
if (mapOpNames.empty())
@@ -45,6 +45,17 @@ CScript ParseScript(const std::string& s)
}
}
+ auto it = mapOpNames.find(s);
+ if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
+ return it->second;
+}
+
+} // namespace
+
+CScript ParseScript(const std::string& s)
+{
+ CScript result;
+
std::vector<std::string> words;
boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
@@ -82,14 +93,10 @@ CScript ParseScript(const std::string& s)
std::vector<unsigned char> value(w->begin()+1, w->end()-1);
result << value;
}
- else if (mapOpNames.count(*w))
- {
- // opcode, e.g. OP_ADD or ADD:
- result << mapOpNames[*w];
- }
else
{
- throw std::runtime_error("script parse error");
+ // opcode, e.g. OP_ADD or ADD:
+ result << ParseOpCode(*w);
}
}