Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Preliminary Buf support
Supports encode, decode, and bytes, should be enough for a web server.
  • Loading branch information
sorear committed Aug 18, 2012
1 parent 49c157f commit c248dcf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Text;

namespace Niecza {
// IForeignInterpreter is used for runtime loading of the Perl 5
Expand Down Expand Up @@ -2921,4 +2922,16 @@ public class Blackhole : Variable {
}
}
}

public static byte[] encode(string inp, string enc) {
return Encoding.GetEncoding(enc).GetBytes(inp);
}

public static string decode(byte[] inp, string enc) {
return Encoding.GetEncoding(enc).GetString(inp);
}

public static int blob_len(byte[] inp) {
return inp.Length;
}
}
17 changes: 17 additions & 0 deletions lib/CORE.setting
Expand Up @@ -885,6 +885,7 @@ my class FatRat does Real {
method nude() { [ self.numerator, self.denominator ] }
}

my class Buf { ... }
my class Str is Cool {
method new() { "" }
method immutable() { True }
Expand Down Expand Up @@ -980,6 +981,22 @@ my class Str is Cool {
'"' ~ self.subst(/ <[$@%&{\b\cJ\cM\t\"\\]> /, { $esc{$/} }, :g)\
.subst(/ <-print> /, { ord($/).fmt('\x[%x]') }, :g) ~ '"'
}
method encode(Str $enc) {
Q:CgOp { (box (@ {Buf}) (encode (unbox str (@ {self})) (obj_getstr {$enc}))) }
}
}
my class Buf {
method decode(Str $enc) {
Q:CgOp { (box Str (decode (unbox blob (@ {self})) (obj_getstr {$enc}))) }
}
method elems() {
Q:CgOp { (box Int (unbox blob (@ {self}))) }
}
method bytes() { self.elems }
method Bool() { ?self.elems }
method Numeric() { self.elems }
method Int() { self.elems }
}
# ObjAt in Niecza is the type of return values from WHICH. It is a comparable
Expand Down
1 change: 1 addition & 0 deletions lib/CodeGen.cs
Expand Up @@ -2449,6 +2449,7 @@ class NamProcessor {
namtypes["strbuf"] = typeof(StringBuilder);
namtypes["treader"] = typeof(TextReader);
namtypes["twriter"] = typeof(TextWriter);
namtypes["blob"] = typeof(byte[]);

handlers = new Dictionary<string, Func<NamProcessor,object[],CpsOp>>();
thandlers = new Dictionary<string, Func<CpsOp[], CpsOp>>();
Expand Down

0 comments on commit c248dcf

Please sign in to comment.