Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 111 lines (102 sloc) 3.346 kb
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
1 augment class Str does Stringy {
d53e02e Solomon Foster Implement Str.Int and tweak Str.Bool declaration.
colomon authored
2 multi method Bool { ?(pir::istrue__IP(self)); }
3
393fcd4 Moritz Lenz fix stringification of Any, and thus RT #73410
moritz authored
4 method Str() { self }
5
d53e02e Solomon Foster Implement Str.Int and tweak Str.Bool declaration.
colomon authored
6 # CHEAT: this implementation is a bit of a cheat,
7 # but works fine for now.
8 multi method Int { (+self).Int; }
542bfa5 Solomon Foster Cheat version of Str.Num.
colomon authored
9 multi method Num { (+self).Num; }
f0a5552 Solomon Foster Remove obsolete Str.z and Str.filebytes functions. Add Str.d, Str.f, an...
colomon authored
10
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
11 # XXX: We have no $?ENC or $?NF compile-time constants yet.
12 multi method encode($encoding = 'UTF-8', $nf = '') {
13 my @bytes = Q:PIR {
7d80cd4 Carl Mäsak [Buf] switched from ByteView to ByteBuffer
masak authored
14 .local int byte
15 .local pmc bytebuffer, it, result
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
16 $P0 = find_lex 'self'
17 $S0 = $P0
7d80cd4 Carl Mäsak [Buf] switched from ByteView to ByteBuffer
masak authored
18 bytebuffer = new ['ByteBuffer']
19 bytebuffer = $S0
359dbdf Carl Mäsak [Buf] improvements to Str.encode
masak authored
20
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
21 result = new ['Parcel']
7d80cd4 Carl Mäsak [Buf] switched from ByteView to ByteBuffer
masak authored
22 it = iter bytebuffer
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
23 bytes_loop:
7d80cd4 Carl Mäsak [Buf] switched from ByteView to ByteBuffer
masak authored
24 unless it goto done
25 byte = shift it
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
26 push result, byte
27 goto bytes_loop
7d80cd4 Carl Mäsak [Buf] switched from ByteView to ByteBuffer
masak authored
28 done:
c5fdb17 Carl Mäsak [Buf] Str.encode and Buf.decode
masak authored
29 %r = result
30 };
31 return Buf.new(@bytes);
32 }
1424333 Solomon Foster Rewrite the Actions handling of dec_number (ie Rat and Num constants) to...
colomon authored
33
34 our sub str2num-int($src) {
35 Q:PIR {
36 .local pmc src
37 .local string src_s
38 src = find_lex '$src'
39 src_s = src
40 .local int pos, eos
41 .local num result
42 pos = 0
43 eos = length src_s
44 result = 0
45 str_loop:
46 unless pos < eos goto str_done
47 .local string char
48 char = substr src_s, pos, 1
49 if char == '_' goto str_next
50 .local int digitval
51 digitval = index "0123456789", char
52 if digitval < 0 goto err_base
53 if digitval >= 10 goto err_base
54 result *= 10
55 result += digitval
56 str_next:
57 inc pos
58 goto str_loop
59 err_base:
60 src.'panic'('Invalid radix conversion of "', char, '"')
61 str_done:
62 %r = box result
63 };
64 }
65
66 our sub str2num-base($src) {
67 Q:PIR {
68 .local pmc src
69 .local string src_s
70 src = find_lex '$src'
71 src_s = src
72 .local int pos, eos
73 .local num result
74 pos = 0
75 eos = length src_s
76 result = 1
77 str_loop:
78 unless pos < eos goto str_done
79 .local string char
80 char = substr src_s, pos, 1
81 if char == '_' goto str_next
82 result *= 10
83 str_next:
84 inc pos
85 goto str_loop
86 err_base:
87 src.'panic'('Invalid radix conversion of "', char, '"')
88 str_done:
89 %r = box result
90 };
91 }
92
93 our sub str2num-rat($negate, $int-part, $frac-part is copy) is export {
94 $frac-part.=subst(/(\d)0+$/, { ~$_[0] });
95 my $result = upgrade_to_num_if_needed(str2num-int($int-part))
96 + upgrade_to_num_if_needed(str2num-int($frac-part))
97 / upgrade_to_num_if_needed(str2num-base($frac-part));
98 $result = -$result if $negate;
99 $result;
100 }
101
102 our sub str2num-num($negate, $int-part, $frac-part, $exp-part-negate, $exp-part) is export {
103 my $exp = str2num-int($exp-part);
104 $exp = -$exp if $exp-part-negate;
105 my $result = (str2num-int($int-part) + str2num-int($frac-part) / str2num-base($frac-part))
106 * 10 ** $exp;
107 $result = -$result if $negate;
108 $result;
109 }
d53e02e Solomon Foster Implement Str.Int and tweak Str.Bool declaration.
colomon authored
110 }
Something went wrong with that request. Please try again.