Commit 7104e9a7 authored by twanvl's avatar twanvl

to_code for strings now adds quotes and escapes

parent 4058bc80
......@@ -400,6 +400,7 @@ inline ScriptValueP to_script(long v) { return to_script((int) v); }
ScriptValueP to_script(AColor v);
ScriptValueP to_script(wxDateTime v);
inline ScriptValueP to_script(bool v) { return v ? script_true : script_false; }
inline ScriptValueP to_script(ScriptValueP const& v) { return v; }
template <typename T>
inline ScriptValueP to_script(const vector<T>* v) { return intrusive(new ScriptCollection<vector<T> >(v)); }
template <typename K, typename V>
......
......@@ -224,6 +224,22 @@ ScriptValueP to_script(double v) {
// ----------------------------------------------------------------------------- : String type
String quote_string(String const& str) {
String out;
out.reserve(str.size() + 2);
out += _('"');
FOR_EACH_CONST(c, str) {
if (c == _('"') || c == _('\\')) { out += _('\\'); out += c; }
else if (c == _('\1')) out += _("\\<");
else if (c == _('\n')) out += _("\\n");
else if (c == _('\r')) out += _("\\r");
else if (c == _('\t')) out += _("\\t");
else out += c;
}
out += _('"');
return out;
}
// String values
class ScriptString : public ScriptValue {
public:
......@@ -231,6 +247,9 @@ class ScriptString : public ScriptValue {
virtual ScriptType type() const { return SCRIPT_STRING; }
virtual String typeName() const { return _TYPE_("string") + _(" (\"") + (value.size() < 30 ? value : value.substr(0,30) + _("...")) + _("\")"); }
virtual String toString() const { return value; }
virtual String toCode() const {
return quote_string(value);
}
virtual double toDouble() const {
double d;
if (value.ToDouble(&d)) {
......@@ -358,11 +377,6 @@ class ScriptNil : public ScriptValue {
virtual GeneratedImageP toImage() const {
return intrusive(new BlankImage());
}
virtual CompareWhat compareAs(String& compare_str, void const*&) const {
// TODO: do we want both nil=="" and nil==0? Then we would get non-transitive equality
compare_str.clear();
return COMPARE_AS_STRING;
}
protected:
virtual ScriptValueP do_eval(Context& ctx, bool) const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment