LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_write.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 82 82 100.0 %
Date: 2021-06-29 14:35:33 Functions: 5 5 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 47 48 97.9 %

           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 http://www.opensource.org/licenses/mit-license.php.
#       4                 :            : 
#       5                 :            : #include <iomanip>
#       6                 :            : #include <sstream>
#       7                 :            : #include <stdio.h>
#       8                 :            : #include "univalue.h"
#       9                 :            : #include "univalue_escapes.h"
#      10                 :            : 
#      11                 :            : static std::string json_escape(const std::string& inS)
#      12                 :    7880491 : {
#      13                 :    7880491 :     std::string outS;
#      14                 :    7880491 :     outS.reserve(inS.size() * 2);
#      15                 :            : 
#      16         [ +  + ]:  381623321 :     for (unsigned int i = 0; i < inS.size(); i++) {
#      17                 :  373742830 :         unsigned char ch = inS[i];
#      18                 :  373742830 :         const char *escStr = escapes[ch];
#      19                 :            : 
#      20         [ +  + ]:  373742830 :         if (escStr)
#      21                 :      16363 :             outS += escStr;
#      22                 :  373726467 :         else
#      23                 :  373726467 :             outS += ch;
#      24                 :  373742830 :     }
#      25                 :            : 
#      26                 :    7880491 :     return outS;
#      27                 :    7880491 : }
#      28                 :            : 
#      29                 :            : std::string UniValue::write(unsigned int prettyIndent,
#      30                 :            :                             unsigned int indentLevel) const
#      31                 :    7564501 : {
#      32                 :    7564501 :     std::string s;
#      33                 :    7564501 :     s.reserve(1024);
#      34                 :            : 
#      35                 :    7564501 :     unsigned int modIndent = indentLevel;
#      36         [ +  + ]:    7564501 :     if (modIndent == 0)
#      37                 :     153565 :         modIndent = 1;
#      38                 :            : 
#      39         [ -  + ]:    7564501 :     switch (typ) {
#      40         [ +  + ]:     116557 :     case VNULL:
#      41                 :     116557 :         s += "null";
#      42                 :     116557 :         break;
#      43         [ +  + ]:     380202 :     case VOBJ:
#      44                 :     380202 :         writeObject(prettyIndent, modIndent, s);
#      45                 :     380202 :         break;
#      46         [ +  + ]:     157406 :     case VARR:
#      47                 :     157406 :         writeArray(prettyIndent, modIndent, s);
#      48                 :     157406 :         break;
#      49         [ +  + ]:    5037713 :     case VSTR:
#      50                 :    5037713 :         s += "\"" + json_escape(val) + "\"";
#      51                 :    5037713 :         break;
#      52         [ +  + ]:    1554647 :     case VNUM:
#      53                 :    1554647 :         s += val;
#      54                 :    1554647 :         break;
#      55         [ +  + ]:     317976 :     case VBOOL:
#      56         [ +  + ]:     317976 :         s += (val == "1" ? "true" : "false");
#      57                 :     317976 :         break;
#      58                 :    7564501 :     }
#      59                 :            : 
#      60                 :    7564501 :     return s;
#      61                 :    7564501 : }
#      62                 :            : 
#      63                 :            : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
#      64                 :      23218 : {
#      65                 :      23218 :     s.append(prettyIndent * indentLevel, ' ');
#      66                 :      23218 : }
#      67                 :            : 
#      68                 :            : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
#      69                 :     157406 : {
#      70                 :     157406 :     s += "[";
#      71         [ +  + ]:     157406 :     if (prettyIndent)
#      72                 :       3839 :         s += "\n";
#      73                 :            : 
#      74         [ +  + ]:    4724655 :     for (unsigned int i = 0; i < values.size(); i++) {
#      75         [ +  + ]:    4567249 :         if (prettyIndent)
#      76                 :      14241 :             indentStr(prettyIndent, indentLevel, s);
#      77                 :    4567249 :         s += values[i].write(prettyIndent, indentLevel + 1);
#      78         [ +  + ]:    4567249 :         if (i != (values.size() - 1)) {
#      79                 :    4435186 :             s += ",";
#      80                 :    4435186 :         }
#      81         [ +  + ]:    4567249 :         if (prettyIndent)
#      82                 :      14241 :             s += "\n";
#      83                 :    4567249 :     }
#      84                 :            : 
#      85         [ +  + ]:     157406 :     if (prettyIndent)
#      86                 :       3839 :         indentStr(prettyIndent, indentLevel - 1, s);
#      87                 :     157406 :     s += "]";
#      88                 :     157406 : }
#      89                 :            : 
#      90                 :            : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
#      91                 :     380202 : {
#      92                 :     380202 :     s += "{";
#      93         [ +  + ]:     380202 :     if (prettyIndent)
#      94                 :       1728 :         s += "\n";
#      95                 :            : 
#      96         [ +  + ]:    3222980 :     for (unsigned int i = 0; i < keys.size(); i++) {
#      97         [ +  + ]:    2842778 :         if (prettyIndent)
#      98                 :       3410 :             indentStr(prettyIndent, indentLevel, s);
#      99                 :    2842778 :         s += "\"" + json_escape(keys[i]) + "\":";
#     100         [ +  + ]:    2842778 :         if (prettyIndent)
#     101                 :       3410 :             s += " ";
#     102                 :    2842778 :         s += values.at(i).write(prettyIndent, indentLevel + 1);
#     103         [ +  + ]:    2842778 :         if (i != (values.size() - 1))
#     104                 :    2463744 :             s += ",";
#     105         [ +  + ]:    2842778 :         if (prettyIndent)
#     106                 :       3410 :             s += "\n";
#     107                 :    2842778 :     }
#     108                 :            : 
#     109         [ +  + ]:     380202 :     if (prettyIndent)
#     110                 :       1728 :         indentStr(prettyIndent, indentLevel - 1, s);
#     111                 :     380202 :     s += "}";
#     112                 :     380202 : }
#     113                 :            : 

Generated by: LCOV version 1.14