<commit_before>#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include "node.hpp"

using std::string;
using std::stringstream;
using std::cout;
using std::cerr;
using std::endl;

namespace Sass {

  string Node::to_string(Type inside_of) const
  {
    switch (type())
    {
      case selector_group:
      case media_expression_group: { // really only needed for arg to :not
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += ", ";
          result += at(i).to_string();
        }
        return result;
      } break;

      case media_expression: {
        string result;
        if (at(0).type() == rule) {
          result += "(";
          result += at(0).to_string();
          result += ")";
        }
        else {
          result += at(0).to_string();
        }
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += " ";
          if (at(i).type() == rule) {
            result += "(";
            result += at(i).to_string();
            result += ")";
          }
          else {
            result += at(i).to_string();
          }
        }
        return result;
      } break;
      
      case selector: {
        string result;
        
        result += at(0).to_string();
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += " ";
          result += at(i).to_string();
        }
        return result;
      }  break;
      
      case selector_combinator: {
        return token().to_string();
      } break;
      
      case simple_selector_sequence: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        return result;
      }  break;
      
      case pseudo:
      case simple_selector: {
        return token().to_string();
      } break;
      
      case pseudo_negation: {
        string result;
        result += at(0).to_string();
        result += at(1).to_string();
        result += ')';
        return result;
      } break;
      
      case functional_pseudo: {
        string result;
        result += at(0).to_string();
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        result += ')';
        return result;
      } break;
      
      case attribute_selector: {
        string result;
        result += "[";
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        result += ']';
        return result;
      } break;

      case rule: {
        string result(at(0).to_string(property));
        result += ": ";
        result += at(1).to_string();
        return result;
      } break;

      case comma_list: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() == nil) continue;
          result += ", ";
          result += at(i).to_string();
        }
        return result;
      } break;
      
      case space_list: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() == nil) continue;
          result += " ";
          result += at(i).to_string();
        }
        return result;
      } break;
      
      case expression:
      case term: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() != add && at(i).type() != mul) {
            result += at(i).to_string();
          }
        }
        return result;
      } break;
      
      //edge case
      case sub: {
        return "-";
      } break;
      
      case div: {
        return "/";
      } break;
      
      case css_import: {
        stringstream ss;
        ss << "@import url(";
        ss << at(0).to_string();
        ss << ")";
        return ss.str();
      }
      
      case function_call: {
        stringstream ss;
        ss << at(0).to_string();
        ss << "(";
        ss << at(1).to_string();
        ss << ")";
        return ss.str();
      }
      
      case arguments: {
        stringstream ss;
        size_t S = size();
        if (S > 0) {
          ss << at(0).to_string();
          for (size_t i = 1; i < S; ++i) {
            ss << ", ";
            ss << at(i).to_string();
          }
        }
        return ss.str();
      }
      
      case unary_plus: {
        stringstream ss;
        ss << "+";
        ss << at(0).to_string();
        return ss.str();
      }
      
      case unary_minus: {
        stringstream ss;
        ss << "-";
        ss << at(0).to_string();
        return ss.str();
      }
      
      case numeric_percentage: {
        stringstream ss;
        ss << numeric_value();
        ss << '%';
        return ss.str();
      }
      
      case numeric_dimension: {
        stringstream ss;
        ss << numeric_value() << unit().to_string();
        return ss.str();
      } break;
      
      case number: {
        stringstream ss;
        ss << numeric_value();
        return ss.str();
      } break;
      
      case numeric_color: {
        if (at(3).numeric_value() >= 1.0)
        {
          double a = at(0).numeric_value();
          double b = at(1).numeric_value();
          double c = at(2).numeric_value();
          if (a >= 0xff && b >= 0xff && c >= 0xff)
          { return "white"; }
          else if (a >= 0xff && b >= 0xff && c == 0)
          { return "yellow"; }
          else if (a == 0 && b >= 0xff && c >= 0xff)
          { return "aqua"; } 
          else if (a >= 0xff && b == 0 && c >= 0xff)
          { return "fuchsia"; }
          else if (a >= 0xff && b == 0 && c == 0)
          { return "red"; }
          else if (a == 0 && b >= 0xff && c == 0)
          { return "lime"; }
          else if (a == 0 && b == 0 && c >= 0xff)
          { return "blue"; }
          else if (a <= 0 && b <= 0 && c <= 0)
          { return "black"; }
          else
          {
            stringstream ss;
            ss << '#' << std::setw(2) << std::setfill('0') << std::hex;
            for (size_t i = 0; i < 3; ++i) {
              double x = at(i).numeric_value();
              if (x > 0xff) x = 0xff;
              else if (x < 0) x = 0;
              ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(x+0.5));
            }
            return ss.str();
          }
        }
        else
        {
          stringstream ss;
          ss << "rgba(" << static_cast<unsigned long>(at(0).numeric_value());
          for (size_t i = 1; i < 3; ++i) {
            ss << ", " << static_cast<unsigned long>(at(i).numeric_value());
          }
          ss << ", " << at(3).numeric_value() << ')';
          return ss.str();
        }
      } break;
      
      case uri: {
        string result("url(");
        result += token().to_string();
        result += ")";
        return result;
      } break;

      case expansion: {
        // ignore it
        return "";
      } break;
      
      case string_constant: {
        if (is_unquoted()) return token().unquote();
        else {
          string result(token().to_string());
          if (result[0] != '"' && result[0] != '\'') return "\"" + result + "\"";
          else                                       return result;
        }
      } break;

      case identifier: {
        string result(token().to_string());
        if (is_quoted()) return "\"" + result + "\"";
        else             return result;
      } break;
      
      case boolean: {
        if (boolean_value()) return "true";
        else return "false";
      } break;
      
      case important: {
        return "!important";
      } break;
      
      case value_schema:
      case identifier_schema: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          if (at(i).type() == string_constant) {
            result += at(i).token().unquote();
          }
          else {
            result += at(i).to_string(identifier_schema);
          }
        }
        return result;
      } break;
      
      case string_schema: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          string chunk(at(i).to_string());
          if (at(i).type() == string_constant) {
            result += chunk.substr(1, chunk.size()-2);
          }
          else {
            result += chunk;
          }
        }
        return result;
      } break;

      case concatenation: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string().substr(1, at(i).token().length()-2);
        }
        if (inside_of == identifier_schema || inside_of == property) return result;
        else               return "\"" + result + "\"";
      } break;<commit_msg>Fixing some more quoting/unquoting stuff.<commit_after>#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include "node.hpp"

using std::string;
using std::stringstream;
using std::cout;
using std::cerr;
using std::endl;

namespace Sass {

  string Node::to_string(Type inside_of) const
  {
    switch (type())
    {
      case selector_group:
      case media_expression_group: { // really only needed for arg to :not
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += ", ";
          result += at(i).to_string();
        }
        return result;
      } break;

      case media_expression: {
        string result;
        if (at(0).type() == rule) {
          result += "(";
          result += at(0).to_string();
          result += ")";
        }
        else {
          result += at(0).to_string();
        }
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += " ";
          if (at(i).type() == rule) {
            result += "(";
            result += at(i).to_string();
            result += ")";
          }
          else {
            result += at(i).to_string();
          }
        }
        return result;
      } break;
      
      case selector: {
        string result;
        
        result += at(0).to_string();
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += " ";
          result += at(i).to_string();
        }
        return result;
      }  break;
      
      case selector_combinator: {
        return token().to_string();
      } break;
      
      case simple_selector_sequence: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        return result;
      }  break;
      
      case pseudo:
      case simple_selector: {
        return token().to_string();
      } break;
      
      case pseudo_negation: {
        string result;
        result += at(0).to_string();
        result += at(1).to_string();
        result += ')';
        return result;
      } break;
      
      case functional_pseudo: {
        string result;
        result += at(0).to_string();
        for (size_t i = 1, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        result += ')';
        return result;
      } break;
      
      case attribute_selector: {
        string result;
        result += "[";
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string();
        }
        result += ']';
        return result;
      } break;

      case rule: {
        string result(at(0).to_string(property));
        result += ": ";
        result += at(1).to_string();
        return result;
      } break;

      case comma_list: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() == nil) continue;
          result += ", ";
          result += at(i).to_string();
        }
        return result;
      } break;
      
      case space_list: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() == nil) continue;
          result += " ";
          result += at(i).to_string();
        }
        return result;
      } break;
      
      case expression:
      case term: {
        string result(at(0).to_string());
        for (size_t i = 1, S = size(); i < S; ++i) {
          if (at(i).type() != add && at(i).type() != mul) {
            result += at(i).to_string();
          }
        }
        return result;
      } break;
      
      //edge case
      case sub: {
        return "-";
      } break;
      
      case div: {
        return "/";
      } break;
      
      case css_import: {
        stringstream ss;
        ss << "@import url(";
        ss << at(0).to_string();
        ss << ")";
        return ss.str();
      }
      
      case function_call: {
        stringstream ss;
        ss << at(0).to_string();
        ss << "(";
        ss << at(1).to_string();
        ss << ")";
        return ss.str();
      }
      
      case arguments: {
        stringstream ss;
        size_t S = size();
        if (S > 0) {
          ss << at(0).to_string();
          for (size_t i = 1; i < S; ++i) {
            ss << ", ";
            ss << at(i).to_string();
          }
        }
        return ss.str();
      }
      
      case unary_plus: {
        stringstream ss;
        ss << "+";
        ss << at(0).to_string();
        return ss.str();
      }
      
      case unary_minus: {
        stringstream ss;
        ss << "-";
        ss << at(0).to_string();
        return ss.str();
      }
      
      case numeric_percentage: {
        stringstream ss;
        ss << numeric_value();
        ss << '%';
        return ss.str();
      }
      
      case numeric_dimension: {
        stringstream ss;
        ss << numeric_value() << unit().to_string();
        return ss.str();
      } break;
      
      case number: {
        stringstream ss;
        ss << numeric_value();
        return ss.str();
      } break;
      
      case numeric_color: {
        if (at(3).numeric_value() >= 1.0)
        {
          double a = at(0).numeric_value();
          double b = at(1).numeric_value();
          double c = at(2).numeric_value();
          if (a >= 0xff && b >= 0xff && c >= 0xff)
          { return "white"; }
          else if (a >= 0xff && b >= 0xff && c == 0)
          { return "yellow"; }
          else if (a == 0 && b >= 0xff && c >= 0xff)
          { return "aqua"; } 
          else if (a >= 0xff && b == 0 && c >= 0xff)
          { return "fuchsia"; }
          else if (a >= 0xff && b == 0 && c == 0)
          { return "red"; }
          else if (a == 0 && b >= 0xff && c == 0)
          { return "lime"; }
          else if (a == 0 && b == 0 && c >= 0xff)
          { return "blue"; }
          else if (a <= 0 && b <= 0 && c <= 0)
          { return "black"; }
          else
          {
            stringstream ss;
            ss << '#' << std::setw(2) << std::setfill('0') << std::hex;
            for (size_t i = 0; i < 3; ++i) {
              double x = at(i).numeric_value();
              if (x > 0xff) x = 0xff;
              else if (x < 0) x = 0;
              ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(x+0.5));
            }
            return ss.str();
          }
        }
        else
        {
          stringstream ss;
          ss << "rgba(" << static_cast<unsigned long>(at(0).numeric_value());
          for (size_t i = 1; i < 3; ++i) {
            ss << ", " << static_cast<unsigned long>(at(i).numeric_value());
          }
          ss << ", " << at(3).numeric_value() << ')';
          return ss.str();
        }
      } break;
      
      case uri: {
        string result("url(");
        result += token().to_string();
        result += ")";
        return result;
      } break;

      case expansion: {
        // ignore it
        return "";
      } break;
      
      case string_constant: {
        if (is_unquoted()) return token().unquote();
        else {
          string result(token().to_string());
          if (result[0] != '"' && result[0] != '\'') return "\"" + result + "\"";
          else                                       return result;
        }
      } break;

      case identifier: {
        string result(token().to_string());
        if (is_quoted()) return "\"" + result + "\"";
        else             return result;
      } break;
      
      case boolean: {
        if (boolean_value()) return "true";
        else return "false";
      } break;
      
      case important: {
        return "!important";
      } break;
      
      case value_schema:
      case identifier_schema: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          if (at(i).type() == string_constant) {
            result += at(i).token().unquote();
          }
          else {
            result += at(i).to_string(identifier_schema);
          }
        }
        if (is_quoted()) result = "\"" + result + "\"";
        return result;
      } break;
      
      case string_schema: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          string chunk(at(i).to_string());
          if (at(i).type() == string_constant) {
            result += chunk.substr(1, chunk.size()-2);
          }
          else {
            result += chunk;
          }
        }
        if (is_unquoted()) result = result.substr(1, result.length() - 2);
        return result;
      } break;

      case concatenation: {
        string result;
        for (size_t i = 0, S = size(); i < S; ++i) {
          result += at(i).to_string().substr(1, at(i).token().length()-2);
        }
        // if (inside_of == identifier_schema || inside_of == property) return result;
        // else                                                         return "\"" + result + "\"";
        if (!(inside_of == identifier_schema || inside_of == property) && !is_unquoted()) {
          result = "\"" + result + "\"";
        }
        return result;
      } break;
{'fraction_non_alphanumeric': 0.1626733921815889, 'fraction_numerical': 0.009320686441142607, 'mean_word_length': 1.3535483870967742, 'pattern_counts': {'":': 2, '<': 193, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5271847', 'n_tokens_mistral': 5707, 'n_tokens_neox': 5559, 'n_words': 1381}