Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Buf] Str.encode and Buf.decode

Also a few other goodies, such as the Stringy role.
A bunch of tests now pass in t/spec/S32-str/encode.rakudo.
  • Loading branch information...
commit c5fdb17fb3e642ef2789fa4ef5b27be72295d267 1 parent 63c5a5c
Carl Mäsak authored May 23, 2010
2  build/Makefile.in
@@ -190,6 +190,8 @@ CORE_SOURCES = \
190 190
   src/core/Num.pm \
191 191
   src/core/Rat.pm \
192 192
   src/core/Complex.pm \
  193
+  src/core/Stringy.pm \
  194
+  src/core/Buf.pm \
193 195
   src/core/Str.pm \
194 196
   src/core/Whatever.pm \
195 197
   src/core/Pair.pm \
42  src/core/Buf.pm
... ...
@@ -0,0 +1,42 @@
  1
+role Buf[::T = Int] does Stringy {
  2
+    has @.contents;
  3
+
  4
+    multi method new(@contents) {
  5
+        self.bless(*, :contents(@contents.list));
  6
+    }
  7
+
  8
+    multi method new(*@contents) {
  9
+        self.bless(*, :contents(@contents.list));
  10
+    }
  11
+
  12
+    multi method decode($encoding = 'UTF-8') {
  13
+        my @contents = @.contents;
  14
+        my $str = ~Q:PIR {
  15
+            $P0 = find_lex '@contents'
  16
+
  17
+            .local pmc iterator
  18
+            iterator = iter $P0
  19
+            .local pmc sb
  20
+            sb = new 'StringBuilder'
  21
+            sb = unicode:""
  22
+            loop:
  23
+              unless iterator goto done
  24
+              $P1 = shift iterator
  25
+              $I1 = $P1
  26
+              $S1 = chr $I1
  27
+              sb .= $S1
  28
+              goto loop
  29
+            done:
  30
+              %r = sb
  31
+        };
  32
+        return $str;
  33
+    }
  34
+
  35
+    multi method elems() {
  36
+        @.contents.elems;
  37
+    }
  38
+}
  39
+
  40
+our multi sub infix:<eqv>(Buf $a, Buf $b) {
  41
+    return $a.contents ~~ $b.contents;
  42
+}
27  src/core/Str.pm
... ...
@@ -1,4 +1,4 @@
1  
-augment class Str {
  1
+augment class Str does Stringy {
2 2
     multi method Bool { ?(pir::istrue__IP(self)); }
3 3
 
4 4
     method Str() { self }
@@ -19,4 +19,29 @@ augment class Str {
19 19
     method s() {
20 20
         self.e ?? pir::stat__ISI(self, 1) !! Any;
21 21
     }
  22
+
  23
+    # XXX: We have no $?ENC or $?NF compile-time constants yet.
  24
+    multi method encode($encoding = 'UTF-8', $nf = '') {
  25
+        my @bytes = Q:PIR {
  26
+            .local int bin_coding, i, max, byte
  27
+            .local string bin_string
  28
+            .local pmc it, result
  29
+            $P0 = find_lex 'self'
  30
+            $S0 = $P0
  31
+            bin_coding = find_encoding 'fixed_8'
  32
+            bin_string = trans_encoding $S0, bin_coding
  33
+            result = new ['Parcel']
  34
+            i = 0
  35
+            max = length bin_string
  36
+          bytes_loop:
  37
+            if i >= max goto bytes_done
  38
+            byte = ord bin_string, i
  39
+            push result, byte
  40
+            inc i
  41
+            goto bytes_loop
  42
+          bytes_done:
  43
+            %r = result
  44
+        };
  45
+        return Buf.new(@bytes);
  46
+    }
22 47
 }
2  src/core/Stringy.pm
... ...
@@ -0,0 +1,2 @@
  1
+role Stringy {
  2
+}

0 notes on commit c5fdb17

Please sign in to comment.
Something went wrong with that request. Please try again.