LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.4 % 129 127
Test Date: 2026-02-04 05:05:50 Functions: 100.0 % 25 25
Branches: 66.7 % 105 70

             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 <sstream>
      11                 :             : #include <string>
      12                 :             : #include <utility>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : const UniValue NullUniValue;
      16                 :             : 
      17                 :    11147028 : void UniValue::clear()
      18                 :             : {
      19                 :    11147028 :     typ = VNULL;
      20                 :    11147028 :     val.clear();
      21                 :    11147028 :     keys.clear();
      22                 :    11147028 :     values.clear();
      23                 :    11147028 : }
      24                 :             : 
      25                 :      208611 : void UniValue::setNull()
      26                 :             : {
      27                 :      208611 :     clear();
      28                 :      208611 : }
      29                 :             : 
      30                 :     6171734 : void UniValue::setBool(bool val_)
      31                 :             : {
      32                 :     6171734 :     clear();
      33                 :     6171734 :     typ = VBOOL;
      34         [ +  + ]:     6171734 :     if (val_)
      35                 :     5765245 :         val = "1";
      36                 :     6171734 : }
      37                 :             : 
      38                 :     1624341 : static bool validNumStr(const std::string& s)
      39                 :             : {
      40         [ -  + ]:     1624341 :     std::string tokenVal;
      41                 :     1624341 :     unsigned int consumed;
      42   [ -  +  +  - ]:     1624341 :     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
      43                 :     1624341 :     return (tt == JTOK_NUMBER);
      44                 :     1624341 : }
      45                 :             : 
      46                 :     1624341 : void UniValue::setNumStr(std::string str)
      47                 :             : {
      48         [ +  + ]:     1624341 :     if (!validNumStr(str)) {
      49   [ +  -  +  - ]:           3 :         throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
      50                 :             :     }
      51                 :             : 
      52                 :     1624340 :     clear();
      53                 :     1624340 :     typ = VNUM;
      54                 :     1624340 :     val = std::move(str);
      55                 :     1624340 : }
      56                 :             : 
      57                 :      833586 : void UniValue::setInt(uint64_t val_)
      58                 :             : {
      59                 :      833586 :     std::ostringstream oss;
      60                 :             : 
      61         [ +  - ]:      833586 :     oss << val_;
      62                 :             : 
      63   [ +  -  +  - ]:     1667172 :     return setNumStr(oss.str());
      64                 :      833586 : }
      65                 :             : 
      66                 :      724297 : void UniValue::setInt(int64_t val_)
      67                 :             : {
      68                 :      724297 :     std::ostringstream oss;
      69                 :             : 
      70         [ +  - ]:      724297 :     oss << val_;
      71                 :             : 
      72   [ +  -  +  - ]:     1448594 :     return setNumStr(oss.str());
      73                 :      724297 : }
      74                 :             : 
      75                 :       66422 : void UniValue::setFloat(double val_)
      76                 :             : {
      77                 :       66422 :     std::ostringstream oss;
      78                 :             : 
      79         [ +  - ]:       66422 :     oss << std::setprecision(16) << val_;
      80                 :             : 
      81   [ +  -  +  - ]:      132844 :     return setNumStr(oss.str());
      82                 :       66422 : }
      83                 :             : 
      84                 :     2732358 : void UniValue::setStr(std::string str)
      85                 :             : {
      86                 :     2732358 :     clear();
      87                 :     2732358 :     typ = VSTR;
      88                 :     2732358 :     val = std::move(str);
      89                 :     2732358 : }
      90                 :             : 
      91                 :         762 : void UniValue::setArray()
      92                 :             : {
      93                 :         762 :     clear();
      94                 :         762 :     typ = VARR;
      95                 :         762 : }
      96                 :             : 
      97                 :      204239 : void UniValue::setObject()
      98                 :             : {
      99                 :      204239 :     clear();
     100                 :      204239 :     typ = VOBJ;
     101                 :      204239 : }
     102                 :             : 
     103                 :     1131435 : void UniValue::push_back(UniValue val)
     104                 :             : {
     105                 :     1131435 :     checkType(VARR);
     106                 :             : 
     107                 :     1131434 :     values.push_back(std::move(val));
     108                 :     1131434 : }
     109                 :             : 
     110                 :           2 : void UniValue::push_backV(const std::vector<UniValue>& vec)
     111                 :             : {
     112                 :           2 :     checkType(VARR);
     113                 :             : 
     114                 :           1 :     values.insert(values.end(), vec.begin(), vec.end());
     115                 :           1 : }
     116                 :             : 
     117                 :     4495655 : void UniValue::pushKVEnd(std::string key, UniValue val)
     118                 :             : {
     119                 :     4495655 :     checkType(VOBJ);
     120                 :             : 
     121                 :     4495654 :     keys.push_back(std::move(key));
     122                 :     4495654 :     values.push_back(std::move(val));
     123                 :     4495654 : }
     124                 :             : 
     125                 :     4474551 : void UniValue::pushKV(std::string key, UniValue val)
     126                 :             : {
     127                 :     4474551 :     checkType(VOBJ);
     128                 :             : 
     129                 :     4474550 :     size_t idx;
     130         [ +  + ]:     4474550 :     if (findKey(key, idx))
     131                 :          11 :         values[idx] = std::move(val);
     132                 :             :     else
     133         [ +  - ]:     8949078 :         pushKVEnd(std::move(key), std::move(val));
     134                 :     4474550 : }
     135                 :             : 
     136                 :       10342 : void UniValue::pushKVs(UniValue obj)
     137                 :             : {
     138                 :       10342 :     checkType(VOBJ);
     139                 :       10341 :     obj.checkType(VOBJ);
     140                 :             : 
     141   [ -  +  +  + ]:       26172 :     for (size_t i = 0; i < obj.keys.size(); i++)
     142   [ +  -  +  - ]:       31662 :         pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
     143                 :       10341 : }
     144                 :             : 
     145                 :      416233 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
     146                 :             : {
     147         [ +  - ]:      416233 :     if (typ != VOBJ)
     148                 :             :         return;
     149                 :             : 
     150                 :      416233 :     kv.clear();
     151   [ -  +  +  + ]:     3890061 :     for (size_t i = 0; i < keys.size(); i++)
     152                 :     3473828 :         kv[keys[i]] = values[i];
     153                 :             : }
     154                 :             : 
     155                 :     4740492 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
     156                 :             : {
     157   [ -  +  +  + ]:    37571065 :     for (size_t i = 0; i < keys.size(); i++) {
     158         [ +  + ]:    33071671 :         if (keys[i] == key) {
     159                 :      241098 :             retIdx = i;
     160                 :      241098 :             return true;
     161                 :             :         }
     162                 :             :     }
     163                 :             : 
     164                 :             :     return false;
     165                 :             : }
     166                 :             : 
     167                 :          11 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
     168                 :             : {
     169         [ +  - ]:          11 :     if (typ != VOBJ) {
     170                 :             :         return false;
     171                 :             :     }
     172                 :             : 
     173         [ +  + ]:          51 :     for (const auto& object: t) {
     174                 :          42 :         size_t idx = 0;
     175         [ +  - ]:          42 :         if (!findKey(object.first, idx)) {
     176                 :             :             return false;
     177                 :             :         }
     178                 :             : 
     179         [ +  + ]:          42 :         if (values.at(idx).getType() != object.second) {
     180                 :             :             return false;
     181                 :             :         }
     182                 :             :     }
     183                 :             : 
     184                 :             :     return true;
     185                 :             : }
     186                 :             : 
     187                 :       26189 : const UniValue& UniValue::operator[](const std::string& key) const
     188                 :             : {
     189         [ +  + ]:       26189 :     if (typ != VOBJ)
     190                 :             :         return NullUniValue;
     191                 :             : 
     192                 :       26121 :     size_t index = 0;
     193         [ +  + ]:       26121 :     if (!findKey(key, index))
     194                 :             :         return NullUniValue;
     195                 :             : 
     196                 :       22865 :     return values.at(index);
     197                 :             : }
     198                 :             : 
     199                 :     2128085 : const UniValue& UniValue::operator[](size_t index) const
     200                 :             : {
     201         [ +  - ]:     2128085 :     if (typ != VOBJ && typ != VARR)
     202                 :             :         return NullUniValue;
     203   [ -  +  +  + ]:     2128085 :     if (index >= values.size())
     204                 :             :         return NullUniValue;
     205                 :             : 
     206                 :     1828771 :     return values.at(index);
     207                 :             : }
     208                 :             : 
     209                 :    14587912 : void UniValue::checkType(const VType& expected) const
     210                 :             : {
     211         [ +  + ]:    14587912 :     if (typ != expected) {
     212   [ +  -  +  -  :          90 :         throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
                   +  - ]
     213   [ +  -  +  -  :         180 :                                  std::string{uvTypeName(expected)}};
                   +  - ]
     214                 :             :     }
     215                 :    14587867 : }
     216                 :             : 
     217                 :       39201 : const char *uvTypeName(UniValue::VType t)
     218                 :             : {
     219   [ +  +  +  +  :       39201 :     switch (t) {
                +  -  + ]
     220                 :             :     case UniValue::VNULL: return "null";
     221                 :          17 :     case UniValue::VBOOL: return "bool";
     222                 :       11550 :     case UniValue::VOBJ: return "object";
     223                 :        1251 :     case UniValue::VARR: return "array";
     224                 :       16208 :     case UniValue::VSTR: return "string";
     225                 :          90 :     case UniValue::VNUM: return "number";
     226                 :             :     }
     227                 :             : 
     228                 :             :     // not reached
     229                 :           0 :     return nullptr;
     230                 :             : }
     231                 :             : 
     232                 :      906286 : const UniValue& UniValue::find_value(std::string_view key) const
     233                 :             : {
     234   [ -  +  +  + ]:     2298095 :     for (unsigned int i = 0; i < keys.size(); ++i) {
     235   [ -  +  +  + ]:     2273560 :         if (keys[i] == key) {
     236                 :      881751 :             return values.at(i);
     237                 :             :         }
     238                 :             :     }
     239                 :             :     return NullUniValue;
     240                 :             : }
     241                 :             : 
     242                 :       31350 : void UniValue::reserve(size_t new_cap)
     243                 :             : {
     244                 :       31350 :     values.reserve(new_cap);
     245         [ -  + ]:       31350 :     if (typ == VOBJ) {
     246                 :           0 :         keys.reserve(new_cap);
     247                 :             :     }
     248                 :       31350 : }
        

Generated by: LCOV version 2.0-1