+#include "Syntax.hpp"
+
+enum CYStringType {
+ CYStringTypeSingle,
+ CYStringTypeDouble,
+ CYStringTypeTemplate,
+};
+
+void CYStringify(std::ostringstream &str, const char *data, size_t size, CYStringifyMode mode) {
+ if (size == 0) {
+ str << "\"\"";
+ return;
+ }
+
+ unsigned quot(0), apos(0), tick(0), line(0);
+ for (const char *value(data), *end(data + size); value != end; ++value)
+ switch (*value) {
+ case '"': ++quot; break;
+ case '\'': ++apos; break;
+ case '`': ++tick; break;
+ case '$': ++tick; break;
+ case '\n': ++line; break;
+ }
+
+ bool split;
+ if (mode != CYStringifyModeCycript)
+ split = false;
+ else {
+ double ratio(double(line) / size);
+ split = size > 10 && line > 2 && ratio > 0.005 && ratio < 0.10;
+ }
+
+ CYStringType type;
+ if (mode == CYStringifyModeNative)
+ type = CYStringTypeDouble;
+ else if (split)
+ type = CYStringTypeTemplate;
+ else if (quot > apos)
+ type = CYStringTypeSingle;
+ else
+ type = CYStringTypeDouble;
+
+ bool parens(split && mode != CYStringifyModeNative && type != CYStringTypeTemplate);
+ if (parens)
+ str << '(';
+
+ char border;
+ switch (type) {
+ case CYStringTypeSingle: border = '\''; break;
+ case CYStringTypeDouble: border = '"'; break;
+ case CYStringTypeTemplate: border = '`'; break;
+ }
+
+ str << border;
+
+ bool space(false);
+
+ for (const char *value(data), *end(data + size); value != end; ++value)
+ if (*value == ' ') {
+ space = true;
+ str << ' ';
+ } else { switch (uint8_t next = *value) {
+ case '\\': str << "\\\\"; break;
+ case '\b': str << "\\b"; break;
+ case '\f': str << "\\f"; break;
+ case '\r': str << "\\r"; break;
+ case '\t': str << "\\t"; break;
+ case '\v': str << "\\v"; break;
+
+ case '\a':
+ if (mode == CYStringifyModeNative)
+ str << "\\a";
+ else goto simple;
+ break;
+
+ case '\n':
+ if (!split)
+ str << "\\n";
+ /*else if (mode == CYStringifyModeNative)
+ str << border << "\\\n" << border;*/
+ else if (type != CYStringTypeTemplate)
+ str << border << '+' << border;
+ else if (!space)
+ str << '\n';
+ else
+ str << "\\n\\\n";
+ break;
+
+ case '$':
+ if (type == CYStringTypeTemplate)
+ str << "\\$";
+ else goto simple;
+ break;
+
+ case '`':
+ if (type == CYStringTypeTemplate)
+ str << "\\`";
+ else goto simple;
+ break;
+
+ case '"':
+ if (type == CYStringTypeDouble)
+ str << "\\\"";
+ else goto simple;
+ break;
+
+ case '\'':
+ if (type == CYStringTypeSingle)
+ str << "\\'";
+ else goto simple;
+ break;
+
+ case '\0':
+ if (mode != CYStringifyModeNative && value[1] >= '0' && value[1] <= '9')
+ str << "\\x00";
+ else
+ str << "\\0";
+ break;
+
+ default:
+ if (next >= 0x20 && next < 0x7f) simple:
+ str << *value;
+ else if (mode == CYStringifyModeNative)
+ str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value & 0xff);
+ else {
+ unsigned levels(1);
+ if ((next & 0x80) != 0)
+ while ((next & 0x80 >> ++levels) != 0);
+
+ unsigned point(next & 0xff >> levels);
+ while (--levels != 0)
+ point = point << 6 | uint8_t(*++value) & 0x3f;
+
+ if (point < 0x100)
+ str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << point;
+ else if (point < 0x10000)
+ str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << point;
+ else {
+ point -= 0x10000;
+ str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xd800 | point >> 0x0a);
+ str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xdc00 | point & 0x3ff);
+ }
+ }
+ } space = false; }
+
+ str << border;
+
+ if (parens)
+ str << ')';
+}
+
+void CYNumerify(std::ostringstream &str, double value) {
+ if (std::isinf(value)) {
+ if (value < 0)
+ str << '-';
+ str << "Infinity";
+ return;
+ }
+
+ char string[32];
+ // XXX: I want this to print 1e3 rather than 1000
+ sprintf(string, "%.17g", value);
+ str << string;
+}
+