LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_write.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 97.2 % 71 69
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 5 5
Branches: 76.6 % 77 59

             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                 :       22167 : static std::string json_escape(const std::string& inS)
      13                 :             : {
      14         [ +  - ]:       22167 :     std::string outS;
      15         [ +  - ]:       22167 :     outS.reserve(inS.size() * 2);
      16                 :             : 
      17         [ +  + ]:      943785 :     for (unsigned int i = 0; i < inS.size(); i++) {
      18         [ +  + ]:      921618 :         unsigned char ch = static_cast<unsigned char>(inS[i]);
      19                 :      921618 :         const char *escStr = escapes[ch];
      20                 :             : 
      21         [ +  + ]:      921618 :         if (escStr)
      22         [ +  - ]:          67 :             outS += escStr;
      23                 :             :         else
      24         [ +  - ]:     1843169 :             outS += static_cast<char>(ch);
      25                 :             :     }
      26                 :             : 
      27                 :       22167 :     return outS;
      28                 :           0 : }
      29                 :             : 
      30                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      31                 :       28628 : std::string UniValue::write(unsigned int prettyIndent,
      32                 :             :                             unsigned int indentLevel) const
      33                 :             : {
      34         [ +  - ]:       28628 :     std::string s;
      35         [ +  - ]:       28628 :     s.reserve(1024);
      36                 :             : 
      37                 :       28628 :     unsigned int modIndent = indentLevel;
      38         [ +  + ]:       28628 :     if (modIndent == 0)
      39                 :        8621 :         modIndent = 1;
      40                 :             : 
      41   [ +  +  +  +  :       28628 :     switch (typ) {
                +  +  - ]
      42                 :          15 :     case VNULL:
      43         [ +  - ]:          15 :         s += "null";
      44                 :             :         break;
      45                 :         647 :     case VOBJ:
      46         [ +  - ]:         647 :         writeObject(prettyIndent, modIndent, s);
      47                 :             :         break;
      48                 :        4877 :     case VARR:
      49         [ +  - ]:        4877 :         writeArray(prettyIndent, modIndent, s);
      50                 :             :         break;
      51                 :       20152 :     case VSTR:
      52   [ +  -  +  -  :       60456 :         s += "\"" + json_escape(val) + "\"";
                   +  - ]
      53                 :       20152 :         break;
      54                 :        2216 :     case VNUM:
      55         [ +  - ]:        2216 :         s += val;
      56                 :             :         break;
      57                 :         721 :     case VBOOL:
      58         [ +  + ]:       29349 :         s += (val == "1" ? "true" : "false");
      59                 :             :         break;
      60                 :             :     }
      61                 :             : 
      62                 :       28628 :     return s;
      63                 :           0 : }
      64                 :             : 
      65                 :       10273 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
      66                 :             : {
      67                 :       10273 :     s.append(prettyIndent * indentLevel, ' ');
      68                 :       10273 : }
      69                 :             : 
      70                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      71                 :        4877 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      72                 :             : {
      73                 :        4877 :     s += "[";
      74         [ +  + ]:        4877 :     if (prettyIndent)
      75                 :        1790 :         s += "\n";
      76                 :             : 
      77         [ +  + ]:       22679 :     for (unsigned int i = 0; i < values.size(); i++) {
      78         [ +  + ]:       17802 :         if (prettyIndent)
      79                 :        6747 :             indentStr(prettyIndent, indentLevel, s);
      80         [ +  - ]:       35604 :         s += values[i].write(prettyIndent, indentLevel + 1);
      81         [ +  + ]:       17802 :         if (i != (values.size() - 1)) {
      82                 :       13125 :             s += ",";
      83                 :             :         }
      84         [ +  + ]:       17802 :         if (prettyIndent)
      85                 :        6747 :             s += "\n";
      86                 :             :     }
      87                 :             : 
      88         [ +  + ]:        4877 :     if (prettyIndent)
      89                 :        1790 :         indentStr(prettyIndent, indentLevel - 1, s);
      90                 :        4877 :     s += "]";
      91                 :        4877 : }
      92                 :             : 
      93                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      94                 :         647 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
      95                 :             : {
      96                 :         647 :     s += "{";
      97         [ +  + ]:         647 :     if (prettyIndent)
      98                 :         423 :         s += "\n";
      99                 :             : 
     100         [ +  + ]:        2662 :     for (unsigned int i = 0; i < keys.size(); i++) {
     101         [ +  + ]:        2015 :         if (prettyIndent)
     102                 :        1313 :             indentStr(prettyIndent, indentLevel, s);
     103   [ +  -  +  - ]:        6045 :         s += "\"" + json_escape(keys[i]) + "\":";
     104         [ +  + ]:        2015 :         if (prettyIndent)
     105                 :        1313 :             s += " ";
     106         [ +  - ]:        4030 :         s += values.at(i).write(prettyIndent, indentLevel + 1);
     107         [ +  + ]:        2015 :         if (i != (values.size() - 1))
     108                 :        1369 :             s += ",";
     109         [ +  + ]:        2015 :         if (prettyIndent)
     110                 :        1313 :             s += "\n";
     111                 :             :     }
     112                 :             : 
     113         [ +  + ]:         647 :     if (prettyIndent)
     114                 :         423 :         indentStr(prettyIndent, indentLevel - 1, s);
     115                 :         647 :     s += "}";
     116                 :         647 : }
     117                 :             : 
        

Generated by: LCOV version 2.0-1