LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.2 % 124 123
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 24 24
Branches: 68.5 % 89 61

             Branch data     Line data    Source code
       1                 :             : // Copyright 2014 BitPay Inc.
       2                 :             : // Copyright 2015 Bitcoin Core Developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or https://opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <univalue.h>
       7                 :             : 
       8                 :             : #include <iomanip>
       9                 :             : #include <map>
      10                 :             : #include <memory>
      11                 :             : #include <sstream>
      12                 :             : #include <string>
      13                 :             : #include <utility>
      14                 :             : #include <vector>
      15                 :             : 
      16                 :             : const UniValue NullUniValue;
      17                 :             : 
      18                 :      441854 : void UniValue::clear()
      19                 :             : {
      20                 :      441854 :     typ = VNULL;
      21                 :      441854 :     val.clear();
      22                 :      441854 :     keys.clear();
      23                 :      441854 :     values.clear();
      24                 :      441854 : }
      25                 :             : 
      26                 :           1 : void UniValue::setNull()
      27                 :             : {
      28                 :           1 :     clear();
      29                 :           1 : }
      30                 :             : 
      31                 :      168059 : void UniValue::setBool(bool val_)
      32                 :             : {
      33                 :      168059 :     clear();
      34                 :      168059 :     typ = VBOOL;
      35         [ +  + ]:      168059 :     if (val_)
      36                 :        1371 :         val = "1";
      37                 :      168059 : }
      38                 :             : 
      39                 :        2286 : static bool validNumStr(const std::string& s)
      40                 :             : {
      41         [ +  - ]:        2286 :     std::string tokenVal;
      42                 :        2286 :     unsigned int consumed;
      43         [ +  - ]:        2286 :     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
      44                 :        2286 :     return (tt == JTOK_NUMBER);
      45                 :        2286 : }
      46                 :             : 
      47                 :        2286 : void UniValue::setNumStr(std::string str)
      48                 :             : {
      49         [ +  + ]:        2286 :     if (!validNumStr(str)) {
      50   [ +  -  +  - ]:           3 :         throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
      51                 :             :     }
      52                 :             : 
      53                 :        2285 :     clear();
      54                 :        2285 :     typ = VNUM;
      55                 :        2285 :     val = std::move(str);
      56                 :        2285 : }
      57                 :             : 
      58                 :         294 : void UniValue::setInt(uint64_t val_)
      59                 :             : {
      60                 :         294 :     std::ostringstream oss;
      61                 :             : 
      62         [ +  - ]:         294 :     oss << val_;
      63                 :             : 
      64   [ +  -  +  - ]:         588 :     return setNumStr(oss.str());
      65                 :         294 : }
      66                 :             : 
      67                 :        1873 : void UniValue::setInt(int64_t val_)
      68                 :             : {
      69                 :        1873 :     std::ostringstream oss;
      70                 :             : 
      71         [ +  - ]:        1873 :     oss << val_;
      72                 :             : 
      73   [ +  -  +  - ]:        3746 :     return setNumStr(oss.str());
      74                 :        1873 : }
      75                 :             : 
      76                 :          83 : void UniValue::setFloat(double val_)
      77                 :             : {
      78                 :          83 :     std::ostringstream oss;
      79                 :             : 
      80         [ +  - ]:          83 :     oss << std::setprecision(16) << val_;
      81                 :             : 
      82   [ +  -  +  - ]:         166 :     return setNumStr(oss.str());
      83                 :          83 : }
      84                 :             : 
      85                 :      271162 : void UniValue::setStr(std::string str)
      86                 :             : {
      87                 :      271162 :     clear();
      88                 :      271162 :     typ = VSTR;
      89                 :      271162 :     val = std::move(str);
      90                 :      271162 : }
      91                 :             : 
      92                 :          95 : void UniValue::setArray()
      93                 :             : {
      94                 :          95 :     clear();
      95                 :          95 :     typ = VARR;
      96                 :          95 : }
      97                 :             : 
      98                 :          61 : void UniValue::setObject()
      99                 :             : {
     100                 :          61 :     clear();
     101                 :          61 :     typ = VOBJ;
     102                 :          61 : }
     103                 :             : 
     104                 :        1181 : void UniValue::push_back(UniValue val)
     105                 :             : {
     106                 :        1181 :     checkType(VARR);
     107                 :             : 
     108                 :        1180 :     values.push_back(std::move(val));
     109                 :        1180 : }
     110                 :             : 
     111                 :           2 : void UniValue::push_backV(const std::vector<UniValue>& vec)
     112                 :             : {
     113                 :           2 :     checkType(VARR);
     114                 :             : 
     115                 :           1 :     values.insert(values.end(), vec.begin(), vec.end());
     116                 :           1 : }
     117                 :             : 
     118                 :        1920 : void UniValue::pushKVEnd(std::string key, UniValue val)
     119                 :             : {
     120                 :        1920 :     checkType(VOBJ);
     121                 :             : 
     122                 :        1919 :     keys.push_back(std::move(key));
     123                 :        1919 :     values.push_back(std::move(val));
     124                 :        1919 : }
     125                 :             : 
     126                 :        1724 : void UniValue::pushKV(std::string key, UniValue val)
     127                 :             : {
     128                 :        1724 :     checkType(VOBJ);
     129                 :             : 
     130                 :        1723 :     size_t idx;
     131         [ +  + ]:        1723 :     if (findKey(key, idx))
     132                 :           1 :         values[idx] = std::move(val);
     133                 :             :     else
     134         [ +  - ]:        3444 :         pushKVEnd(std::move(key), std::move(val));
     135                 :        1723 : }
     136                 :             : 
     137                 :           2 : void UniValue::pushKVs(UniValue obj)
     138                 :             : {
     139                 :           2 :     checkType(VOBJ);
     140                 :           1 :     obj.checkType(VOBJ);
     141                 :             : 
     142         [ +  + ]:           3 :     for (size_t i = 0; i < obj.keys.size(); i++)
     143   [ +  -  +  - ]:           4 :         pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
     144                 :           1 : }
     145                 :             : 
     146                 :           1 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
     147                 :             : {
     148         [ +  - ]:           1 :     if (typ != VOBJ)
     149                 :             :         return;
     150                 :             : 
     151                 :           1 :     kv.clear();
     152         [ +  + ]:           3 :     for (size_t i = 0; i < keys.size(); i++)
     153                 :           2 :         kv[keys[i]] = values[i];
     154                 :             : }
     155                 :             : 
     156                 :        2227 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
     157                 :             : {
     158         [ +  + ]:        6424 :     for (size_t i = 0; i < keys.size(); i++) {
     159         [ +  + ]:        4667 :         if (keys[i] == key) {
     160                 :         470 :             retIdx = i;
     161                 :         470 :             return true;
     162                 :             :         }
     163                 :             :     }
     164                 :             : 
     165                 :             :     return false;
     166                 :             : }
     167                 :             : 
     168                 :          11 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
     169                 :             : {
     170         [ +  - ]:          11 :     if (typ != VOBJ) {
     171                 :             :         return false;
     172                 :             :     }
     173                 :             : 
     174         [ +  + ]:          51 :     for (const auto& object: t) {
     175                 :          42 :         size_t idx = 0;
     176         [ +  - ]:          42 :         if (!findKey(object.first, idx)) {
     177                 :             :             return false;
     178                 :             :         }
     179                 :             : 
     180         [ +  + ]:          42 :         if (values.at(idx).getType() != object.second) {
     181                 :             :             return false;
     182                 :             :         }
     183                 :             :     }
     184                 :             : 
     185                 :             :     return true;
     186                 :             : }
     187                 :             : 
     188                 :         403 : const UniValue& UniValue::operator[](const std::string& key) const
     189                 :             : {
     190         [ +  - ]:         403 :     if (typ != VOBJ)
     191                 :             :         return NullUniValue;
     192                 :             : 
     193                 :         403 :     size_t index = 0;
     194         [ +  + ]:         403 :     if (!findKey(key, index))
     195                 :             :         return NullUniValue;
     196                 :             : 
     197                 :         400 :     return values.at(index);
     198                 :             : }
     199                 :             : 
     200                 :       17084 : const UniValue& UniValue::operator[](size_t index) const
     201                 :             : {
     202         [ +  - ]:       17084 :     if (typ != VOBJ && typ != VARR)
     203                 :             :         return NullUniValue;
     204         [ +  + ]:       17084 :     if (index >= values.size())
     205                 :             :         return NullUniValue;
     206                 :             : 
     207                 :       16996 :     return values.at(index);
     208                 :             : }
     209                 :             : 
     210                 :      142717 : void UniValue::checkType(const VType& expected) const
     211                 :             : {
     212         [ +  + ]:      142717 :     if (typ != expected) {
     213   [ +  -  +  -  :          54 :         throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
                   +  - ]
     214   [ +  -  +  -  :         108 :                                  std::string{uvTypeName(expected)}};
                   +  - ]
     215                 :             :     }
     216                 :      142690 : }
     217                 :             : 
     218                 :          58 : const char *uvTypeName(UniValue::VType t)
     219                 :             : {
     220   [ +  +  +  +  :          58 :     switch (t) {
                +  -  + ]
     221                 :             :     case UniValue::VNULL: return "null";
     222                 :           4 :     case UniValue::VBOOL: return "bool";
     223                 :          10 :     case UniValue::VOBJ: return "object";
     224                 :           9 :     case UniValue::VARR: return "array";
     225                 :          15 :     case UniValue::VSTR: return "string";
     226                 :          14 :     case UniValue::VNUM: return "number";
     227                 :             :     }
     228                 :             : 
     229                 :             :     // not reached
     230                 :           0 :     return nullptr;
     231                 :             : }
     232                 :             : 
     233                 :         493 : const UniValue& UniValue::find_value(std::string_view key) const
     234                 :             : {
     235         [ +  + ]:        1050 :     for (unsigned int i = 0; i < keys.size(); ++i) {
     236         [ +  + ]:        1002 :         if (keys[i] == key) {
     237                 :         445 :             return values.at(i);
     238                 :             :         }
     239                 :             :     }
     240                 :             :     return NullUniValue;
     241                 :             : }
     242                 :             : 
        

Generated by: LCOV version 2.0-1