• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 290

04 Dec 2024 03:58PM UTC coverage: 84.953% (-2.9%) from 87.841%
290

Pull #78

github

web-flow
Merge 12dc8cc8a into d72df8024
Pull Request #78: feat: new package encoding

101 of 160 new or added lines in 3 files covered. (63.13%)

1163 of 1369 relevant lines covered (84.95%)

423.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.54
/encoding/types.mbt
1
// Copyright 2024 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///|
16
typealias Cont = (Decoder) -> Decode
17

18
///|
19
pub(all) enum Encoding {
20
  UTF8
21
  UTF16 // alias for UTF16LE
22
  UTF16LE
23
  UTF16BE
24
}
25

26
// Decoder
27

28
///|
29
priv struct Decoder {
30
  mut i : Bytes
31
  mut i_pos : Int
32
  mut i_max : Int
33
  t : Bytes
34
  mut t_len : Int
35
  mut t_need : Int
36
  mut k : Cont
37
}
38

39
///|
40
priv enum Decode {
41
  End
42
  Malformed(Bytes)
43
  Uchar(Char)
44
}
45

46
///|
47
fn slice(bytes : Bytes, offset : Int, length : Int) -> Bytes {
48
  let new_bytes = Bytes::new(length)
18✔
49
  new_bytes.blit(0, bytes, offset, length)
50
  new_bytes
51
}
52

53
///|
54
fn malformed(bytes : Bytes, offset : Int, length : Int) -> Decode {
55
  Malformed(slice(bytes, offset, length))
18✔
56
}
57

58
///|
59
fn malformed_pair(
60
  be : Bool,
61
  hi : Int,
62
  bytes : Bytes,
63
  offset : Int,
64
  length : Int
65
) -> Decode {
NEW
66
  let bs1 = bytes.to_unchecked_string(offset~, length~).to_bytes()
×
67
  let bs0 = b"\x00\x00"
NEW
68
  let (j0, j1) = if be { (0, 1) } else { (1, 0) }
×
69
  bs0[j0] = (hi >> 8).to_byte()
70
  bs0[j1] = hi.land(0xFF).to_byte()
71
  let bs = @buffer.new(size_hint=bs0.length() + bs1.length())
72
  bs.write_bytes(bs0)
73
  bs.write_bytes(bs1)
74
  Malformed(slice(bs.to_bytes(), 0, bs.length()))
75
}
76

77
// Chars
78

79
///|
80
type LossyChars Decoder
81

82
///|
83
pub fn iter(self : LossyChars) -> Iter[Char] {
84
  Iter::new(
14✔
85
    fn(yield_) {
86
      loop self._.decode() {
14✔
87
        Uchar(u) => {
75✔
88
          if yield_(u) == IterEnd {
NEW
89
            break IterEnd
×
90
          }
91
          continue self._.decode()
92
        }
93
        Malformed(_) => {
9✔
94
          if yield_(U_REP) == IterEnd {
NEW
95
            break IterEnd
×
96
          }
97
          continue self._.decode()
98
        }
99
        End => break IterEnd
14✔
100
      }
101
    },
102
  )
103
}
104

105
///|
106
pub fn to_string(self : LossyChars) -> String {
107
  let arr = self.iter().collect()
7✔
108
  String::from_array(arr)
109
}
110

111
///|
112
pub impl Show for LossyChars with output(self : LossyChars, logger : Logger) -> Unit {
113
  logger.write_string(self.to_string())
7✔
114
}
115

116
///|
117
type StrictChars Decoder
118

119
///|
120
pub(all) type! DecodeError Bytes
121

122
///|
123
pub impl Show for DecodeError with output(self, logger) {
124
  match self {
9✔
125
    DecodeError(err) => err.output(logger)
9✔
126
  }
127
}
128

129
///|
130
pub fn iter(self : StrictChars) -> Iter[Result[Char, DecodeError]] {
131
  Iter::new(
6✔
132
    fn(yield_) {
133
      loop self._.decode() {
6✔
134
        Uchar(u) => {
58✔
135
          if yield_(Ok(u)) == IterEnd {
NEW
136
            break IterEnd
×
137
          }
138
          continue self._.decode()
139
        }
140
        Malformed(s) => {
9✔
141
          let err = DecodeError(s)
142
          if yield_(Err(err)) == IterEnd {
NEW
143
            break IterEnd
×
144
          }
145
          continue self._.decode()
146
        }
147
        End => break IterEnd
6✔
148
      }
149
    },
150
  )
151
}
152

153
///|
154
pub fn to_string(self : StrictChars) -> String! {
NEW
155
  let arr = []
×
156
  for element in self {
157
    match element {
NEW
158
      Ok(x) => arr.push(x)
×
NEW
159
      Err(e) => raise e
×
160
    }
161
  }
162
  String::from_array(arr)
163
}
164

165
///|
166
pub impl Show for StrictChars with output(self : StrictChars, logger : Logger) -> Unit {
NEW
167
  logger.write_string(self.to_string?().to_string())
×
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc