LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_write.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.2 % 71 69
Test Date: 2026-02-19 05:12:30 Functions: 100.0 % 5 5
Branches: 73.0 % 89 65

             Branch data     Line data    Source code
       1                 :             : // Copyright 2014 BitPay Inc.
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <univalue.h>
       6                 :             : #include <univalue_escapes.h>
       7                 :             : 
       8                 :             : #include <string>
       9                 :             : #include <vector>
      10                 :             : 
      11                 :     8243231 : static std::string json_escape(const std::string& inS)
      12                 :             : {
      13         [ -  + ]:     8243231 :     std::string outS;
      14   [ -  +  +  - ]:     8243231 :     outS.reserve(inS.size() * 2);
      15                 :             : 
      16   [ -  +  +  + ]:   157943973 :     for (unsigned int i = 0; i < inS.size(); i++) {
      17         [ +  + ]:   149700742 :         unsigned char ch = static_cast<unsigned char>(inS[i]);
      18                 :   149700742 :         const char *escStr = escapes[ch];
      19                 :             : 
      20         [ +  + ]:   149700742 :         if (escStr)
      21         [ +  - ]:     1923815 :             outS += escStr;
      22                 :             :         else
      23         [ +  - ]:   297477669 :             outS += static_cast<char>(ch);
      24                 :             :     }
      25                 :             : 
      26                 :     8243231 :     return outS;
      27                 :           0 : }
      28                 :             : 
      29                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      30                 :     8776633 : std::string UniValue::write(unsigned int prettyIndent,
      31                 :             :                             unsigned int indentLevel) const
      32                 :             : {
      33         [ +  - ]:     8776633 :     std::string s;
      34         [ +  - ]:     8776633 :     s.reserve(1024);
      35                 :             : 
      36                 :     8776633 :     unsigned int modIndent = indentLevel;
      37         [ +  + ]:     8776633 :     if (modIndent == 0)
      38                 :       15073 :         modIndent = 1;
      39                 :             : 
      40   [ +  +  +  +  :     8776633 :     switch (typ) {
                +  +  - ]
      41                 :       21409 :     case VNULL:
      42         [ +  - ]:       21409 :         s += "null";
      43                 :             :         break;
      44                 :     1603057 :     case VOBJ:
      45         [ +  - ]:     1603057 :         writeObject(prettyIndent, modIndent, s);
      46                 :             :         break;
      47                 :      313477 :     case VARR:
      48         [ +  - ]:      313477 :         writeArray(prettyIndent, modIndent, s);
      49                 :             :         break;
      50                 :     1641959 :     case VSTR:
      51   [ +  -  +  -  :     4925877 :         s += "\"" + json_escape(val) + "\"";
                   -  + ]
      52                 :     1641959 :         break;
      53                 :     5192763 :     case VNUM:
      54         [ -  + ]:     5192763 :         s += val;
      55                 :             :         break;
      56                 :        3968 :     case VBOOL:
      57         [ +  + ]:     8780601 :         s += (val == "1" ? "true" : "false");
      58                 :             :         break;
      59                 :             :     }
      60                 :             : 
      61                 :     8776633 :     return s;
      62                 :           0 : }
      63                 :             : 
      64                 :     9486862 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
      65                 :             : {
      66                 :     9486862 :     s.append(prettyIndent * indentLevel, ' ');
      67                 :     9486862 : }
      68                 :             : 
      69                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      70                 :      313477 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      71                 :             : {
      72                 :      313477 :     s += "[";
      73         [ +  + ]:      313477 :     if (prettyIndent)
      74                 :       35652 :         s += "\n";
      75                 :             : 
      76   [ -  +  +  + ]:     2438113 :     for (unsigned int i = 0; i < values.size(); i++) {
      77         [ +  + ]:     2124636 :         if (prettyIndent)
      78                 :     1557339 :             indentStr(prettyIndent, indentLevel, s);
      79         [ -  + ]:     4249272 :         s += values[i].write(prettyIndent, indentLevel + 1);
      80   [ -  +  +  + ]:     2124636 :         if (i != (values.size() - 1)) {
      81                 :     2075912 :             s += ",";
      82                 :             :         }
      83         [ +  + ]:     2124636 :         if (prettyIndent)
      84                 :     1557339 :             s += "\n";
      85                 :             :     }
      86                 :             : 
      87         [ +  + ]:      313477 :     if (prettyIndent)
      88                 :       35652 :         indentStr(prettyIndent, indentLevel - 1, s);
      89                 :      313477 :     s += "]";
      90                 :      313477 : }
      91                 :             : 
      92                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      93                 :     1603057 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      94                 :             : {
      95                 :     1603057 :     s += "{";
      96         [ +  + ]:     1603057 :     if (prettyIndent)
      97                 :     1593068 :         s += "\n";
      98                 :             : 
      99   [ -  +  +  + ]:     8204329 :     for (unsigned int i = 0; i < keys.size(); i++) {
     100         [ +  + ]:     6601272 :         if (prettyIndent)
     101                 :     6300803 :             indentStr(prettyIndent, indentLevel, s);
     102   [ +  -  -  + ]:    19803816 :         s += "\"" + json_escape(keys[i]) + "\":";
     103         [ +  + ]:     6601272 :         if (prettyIndent)
     104                 :     6300803 :             s += " ";
     105         [ -  + ]:    13202544 :         s += values.at(i).write(prettyIndent, indentLevel + 1);
     106   [ -  +  +  + ]:     6601272 :         if (i != (values.size() - 1))
     107                 :     5004827 :             s += ",";
     108         [ +  + ]:     6601272 :         if (prettyIndent)
     109                 :     6300803 :             s += "\n";
     110                 :             :     }
     111                 :             : 
     112         [ +  + ]:     1603057 :     if (prettyIndent)
     113                 :     1593068 :         indentStr(prettyIndent, indentLevel - 1, s);
     114                 :     1603057 :     s += "}";
     115                 :     1603057 : }
     116                 :             : 
        

Generated by: LCOV version 2.0-1