LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_write.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.2 % 71 69
Test Date: 2025-08-25 05:11:47 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 <memory>
       9                 :             : #include <string>
      10                 :             : #include <vector>
      11                 :             : 
      12                 :     6362482 : static std::string json_escape(const std::string& inS)
      13                 :             : {
      14         [ -  + ]:     6362482 :     std::string outS;
      15   [ -  +  +  - ]:     6362482 :     outS.reserve(inS.size() * 2);
      16                 :             : 
      17   [ -  +  +  + ]:  1028297896 :     for (unsigned int i = 0; i < inS.size(); i++) {
      18         [ +  + ]:  1021935414 :         unsigned char ch = static_cast<unsigned char>(inS[i]);
      19                 :  1021935414 :         const char *escStr = escapes[ch];
      20                 :             : 
      21         [ +  + ]:  1021935414 :         if (escStr)
      22         [ +  - ]:       26121 :             outS += escStr;
      23                 :             :         else
      24         [ +  - ]:  2043844707 :             outS += static_cast<char>(ch);
      25                 :             :     }
      26                 :             : 
      27                 :     6362482 :     return outS;
      28                 :           0 : }
      29                 :             : 
      30                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      31                 :     5285892 : std::string UniValue::write(unsigned int prettyIndent,
      32                 :             :                             unsigned int indentLevel) const
      33                 :             : {
      34         [ +  - ]:     5285892 :     std::string s;
      35         [ +  - ]:     5285892 :     s.reserve(1024);
      36                 :             : 
      37                 :     5285892 :     unsigned int modIndent = indentLevel;
      38         [ +  + ]:     5285892 :     if (modIndent == 0)
      39                 :      246389 :         modIndent = 1;
      40                 :             : 
      41   [ +  +  +  +  :     5285892 :     switch (typ) {
                +  +  - ]
      42                 :       27669 :     case VNULL:
      43         [ +  - ]:       27669 :         s += "null";
      44                 :             :         break;
      45                 :      648847 :     case VOBJ:
      46         [ +  - ]:      648847 :         writeObject(prettyIndent, modIndent, s);
      47                 :             :         break;
      48                 :      189181 :     case VARR:
      49         [ +  - ]:      189181 :         writeArray(prettyIndent, modIndent, s);
      50                 :             :         break;
      51                 :     2066936 :     case VSTR:
      52   [ +  -  +  -  :     6200808 :         s += "\"" + json_escape(val) + "\"";
                   -  + ]
      53                 :     2066936 :         break;
      54                 :     1911707 :     case VNUM:
      55         [ -  + ]:     1911707 :         s += val;
      56                 :             :         break;
      57                 :      441552 :     case VBOOL:
      58         [ +  + ]:     5727444 :         s += (val == "1" ? "true" : "false");
      59                 :             :         break;
      60                 :             :     }
      61                 :             : 
      62                 :     5285892 :     return s;
      63                 :           0 : }
      64                 :             : 
      65                 :       20013 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
      66                 :             : {
      67                 :       20013 :     s.append(prettyIndent * indentLevel, ' ');
      68                 :       20013 : }
      69                 :             : 
      70                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      71                 :      189181 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      72                 :             : {
      73                 :      189181 :     s += "[";
      74         [ +  + ]:      189181 :     if (prettyIndent)
      75                 :        2844 :         s += "\n";
      76                 :             : 
      77   [ -  +  +  + ]:      931124 :     for (unsigned int i = 0; i < values.size(); i++) {
      78         [ +  + ]:      741943 :         if (prettyIndent)
      79                 :        7860 :             indentStr(prettyIndent, indentLevel, s);
      80         [ -  + ]:     1483886 :         s += values[i].write(prettyIndent, indentLevel + 1);
      81   [ -  +  +  + ]:      741943 :         if (i != (values.size() - 1)) {
      82                 :      598398 :             s += ",";
      83                 :             :         }
      84         [ +  + ]:      741943 :         if (prettyIndent)
      85                 :        7860 :             s += "\n";
      86                 :             :     }
      87                 :             : 
      88         [ +  + ]:      189181 :     if (prettyIndent)
      89                 :        2844 :         indentStr(prettyIndent, indentLevel - 1, s);
      90                 :      189181 :     s += "]";
      91                 :      189181 : }
      92                 :             : 
      93                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      94                 :      648847 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      95                 :             : {
      96                 :      648847 :     s += "{";
      97         [ +  + ]:      648847 :     if (prettyIndent)
      98                 :        2963 :         s += "\n";
      99                 :             : 
     100   [ -  +  +  + ]:     4944393 :     for (unsigned int i = 0; i < keys.size(); i++) {
     101         [ +  + ]:     4295546 :         if (prettyIndent)
     102                 :        6346 :             indentStr(prettyIndent, indentLevel, s);
     103   [ +  -  -  + ]:    12886638 :         s += "\"" + json_escape(keys[i]) + "\":";
     104         [ +  + ]:     4295546 :         if (prettyIndent)
     105                 :        6346 :             s += " ";
     106         [ -  + ]:     8591092 :         s += values.at(i).write(prettyIndent, indentLevel + 1);
     107   [ -  +  +  + ]:     4295546 :         if (i != (values.size() - 1))
     108                 :     3653808 :             s += ",";
     109         [ +  + ]:     4295546 :         if (prettyIndent)
     110                 :        6346 :             s += "\n";
     111                 :             :     }
     112                 :             : 
     113         [ +  + ]:      648847 :     if (prettyIndent)
     114                 :        2963 :         indentStr(prettyIndent, indentLevel - 1, s);
     115                 :      648847 :     s += "}";
     116                 :      648847 : }
     117                 :             : 
        

Generated by: LCOV version 2.0-1