LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_write.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 54 74 73.0 %
Date: 2023-09-26 12:08:55 Functions: 5 5 100.0 %

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

Generated by: LCOV version 1.14