| 25 | namespace { |
| 26 | |
| 27 | opcodetype ParseOpCode(const std::string &s) { |
| 28 | static std::map<std::string, opcodetype> mapOpNames; |
| 29 | |
| 30 | if (mapOpNames.empty()) { |
| 31 | for (int op = 0; op < FIRST_UNDEFINED_OP_VALUE; op++) { |
| 32 | if (op < OP_PUSHDATA1) { |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | std::string strName = GetOpName(static_cast<opcodetype>(op)); |
| 37 | if (strName == "OP_UNKNOWN") { |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | mapOpNames[strName] = static_cast<opcodetype>(op); |
| 42 | // Convenience: OP_ADD and just ADD are both recognized: |
| 43 | // strName starts with "OP_" |
| 44 | if (strName.compare(0, 3, "OP_") == 0) { |
| 45 | mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | auto it = mapOpNames.find(s); |
| 51 | if (it == mapOpNames.end()) { |
| 52 | throw std::runtime_error("script parse error: unknown opcode " + s); |
| 53 | } |
| 54 | return it->second; |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |