Permalink
Browse files

Preliminary Buf support

Supports encode, decode, and bytes, should be enough for a web server.
  • Loading branch information...
1 parent 49c157f commit c248dcf50a61dde669079a4e197eb6876b97ef9c @sorear committed Aug 18, 2012
Showing with 31 additions and 0 deletions.
  1. +13 −0 lib/Builtins.cs
  2. +17 −0 lib/CORE.setting
  3. +1 −0 lib/CodeGen.cs
View
@@ -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
@@ -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;
+ }
}
View
@@ -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 }
@@ -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
View
@@ -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>>();

0 comments on commit c248dcf

Please sign in to comment.