diff options
Diffstat (limited to 'src/Term.vala')
-rw-r--r-- | src/Term.vala | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/src/Term.vala b/src/Term.vala index 7037e48..46c03b3 100644 --- a/src/Term.vala +++ b/src/Term.vala @@ -37,7 +37,7 @@ namespace Eva { case Erl.TermType.ATOM: char[] value = new char[size+1]; assert(Erl.decode_atom(buffer, ref index, value) == 0); - return new Atom(((string)value).ndup(size)); + return new Atom(array_to_string(value, size)); case Erl.TermType.SMALL_TUPLE: case Erl.TermType.LARGE_TUPLE: @@ -68,7 +68,7 @@ namespace Eva { case Erl.TermType.STRING: char[] value = new char[size+1]; assert(Erl.decode_string(buffer, ref index, value) == 0); - return new String(((string)value).ndup(size)); + return new String(array_to_string(value, size)); case Erl.TermType.BINARY: char[] value = new char[size]; @@ -184,11 +184,33 @@ namespace Eva { public string value {get; construct;} public Atom(string v) { + assert(v.validate()); Object(value: v); } public string to_string() { - return value.to_string(); + if(value.length == 0) + return "''"; + + bool special = false; + unichar c = value.get_char(); + + if(!c.islower()) + special = true; + + for(unowned string rest = value.next_char(); rest.length != 0 && !special; rest = rest.next_char()) { + c = rest.get_char(); + + if(!(c == '_' || c.isalnum())) + special = true; + } + + if(!special) { + return value; + } + else { + return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'"; + } } public bool equals(Term o) { @@ -201,7 +223,9 @@ namespace Eva { } public void encode(Erl.Buffer buffer) { - buffer.encode_atom(value.to_utf8()); + char[]? array = string_to_array(value); + assert(array != null); + buffer.encode_atom(array); } } @@ -312,23 +336,30 @@ namespace Eva { string s = ""; foreach(Term t in list) { + unichar c; + if(t is Int) { Int i = t as Int; if(i.value < 0 || i.value > 255) return false; - s += i.value.to_string("%c"); + c = (unichar)i.value; } else if(t is UInt) { UInt i = t as UInt; if(i.value > 255) return false; - s += i.value.to_string("%c"); + c = (unichar)i.value; } else { return false; } + + string buf = string.nfill(6, 0); + c.to_utf8(buf); + + s += buf; } return (s == (o as String).value); @@ -382,7 +413,7 @@ namespace Eva { } public string to_string() { - return value.to_string(); + return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; } public bool equals(Term o) { @@ -398,7 +429,13 @@ namespace Eva { } public void encode(Erl.Buffer buffer) { - buffer.encode_string(value.to_utf8()); + char[]? array = string_to_array(value); + if(array != null) { + buffer.encode_string(array); + } + else { + string_to_list(value).encode(buffer); + } } } @@ -442,7 +479,7 @@ namespace Eva { public uint creation {get; construct;} public Pid(Erl.Pid pid) { - this.create(((string)pid.node).ndup(Erl.MAXATOMLEN), pid.num, pid.serial, pid.creation); + this.create(array_to_string(pid.node, Erl.MAXATOMLEN), pid.num, pid.serial, pid.creation); } private Pid.create(string node0, uint num0, uint serial0, uint creation0) { @@ -464,7 +501,8 @@ namespace Eva { public void encode(Erl.Buffer buffer) { Erl.Pid pid = Erl.Pid(); - char[] nodedata = node.to_utf8(); + char[]? nodedata = string_to_array(node); + assert(nodedata != null); Memory.copy(pid.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length)); pid.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0; @@ -483,7 +521,7 @@ namespace Eva { public uint creation {get; construct;} public Port(Erl.Port port) { - this.create(((string)port.node).ndup(Erl.MAXATOMLEN), port.id, port.creation); + this.create(array_to_string(port.node, Erl.MAXATOMLEN), port.id, port.creation); } private Port.create(string node0, uint id0, uint creation0) { @@ -505,7 +543,8 @@ namespace Eva { public void encode(Erl.Buffer buffer) { Erl.Port port = Erl.Port(); - char[] nodedata = node.to_utf8(); + char[]? nodedata = string_to_array(node); + assert(nodedata != null); Memory.copy(port.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length)); port.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0; @@ -524,7 +563,7 @@ namespace Eva { public uint creation {get; construct;} public Ref(Erl.Ref reference) { - this.create(((string)reference.node).ndup(Erl.MAXATOMLEN), reference.len, reference.n, reference.creation); + this.create(array_to_string(reference.node, Erl.MAXATOMLEN), reference.len, reference.n, reference.creation); } private Ref.create(string node0, int len0, uint* n0, uint creation0) { @@ -560,7 +599,8 @@ namespace Eva { public void encode(Erl.Buffer buffer) { Erl.Ref reference = Erl.Ref(); - char[] nodedata = node.to_utf8(); + char[]? nodedata = string_to_array(node); + assert(nodedata != null); Memory.copy(reference.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length)); reference.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0; |