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

moonbitlang / x / 289

04 Dec 2024 02:48PM UTC coverage: 84.795% (-3.0%) from 87.841%
289

Pull #78

github

web-flow
Merge 03aaaf488 into d72df8024
Pull Request #78: feat: new package encoding

98 of 159 new or added lines in 3 files covered. (61.64%)

1160 of 1368 relevant lines covered (84.8%)

424.01 hits per line

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

52.0
/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(String)
43
  Uchar(Char)
44
}
45

46
///|
47
fn malformed(buf : Bytes, offset : Int, length : Int) -> Decode {
48
  Malformed(buf.to_unchecked_string(offset~, length~))
18✔
49
}
50

51
///|
52
fn malformed_pair(
53
  be : Bool,
54
  hi : Int,
55
  buf : Bytes,
56
  offset : Int,
57
  length : Int
58
) -> Decode {
NEW
59
  let bs1 = buf.to_unchecked_string(offset~, length~).to_bytes()
×
60
  let bs0 = b"\x00\x00"
NEW
61
  let (j0, j1) = if be { (0, 1) } else { (1, 0) }
×
62
  bs0[j0] = (hi >> 8).to_byte()
63
  bs0[j1] = hi.land(0xFF).to_byte()
64
  let bs = @buffer.new(size_hint=bs0.length() + bs1.length())
65
  bs.write_bytes(bs0)
66
  bs.write_bytes(bs1)
67
  Malformed(bs.to_bytes().to_unchecked_string(offset=0, length=bs.length()))
68
}
69

70
// Chars
71

72
///|
73
type LossyChars Decoder
74

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

98
///|
99
pub fn to_string(self : LossyChars) -> String {
100
  let arr = self.iter().collect()
7✔
101
  String::from_array(arr)
102
}
103

104
///|
105
pub impl Show for LossyChars with output(self : LossyChars, logger : Logger) -> Unit {
106
  logger.write_string(self.to_string())
7✔
107
}
108

109
///|
110
type StrictChars Decoder
111

112
///|
113
pub(all) type! DecodeError String
114

115
///|
116
pub impl Show for DecodeError with output(self, logger) {
NEW
117
  match self {
×
NEW
118
    DecodeError(err) => logger.write_string(err)
×
119
  }
120
}
121

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

146
///|
147
pub fn to_string(self : StrictChars) -> String! {
NEW
148
  let arr = []
×
149
  for element in self {
150
    match element {
NEW
151
      Ok(x) => arr.push(x)
×
NEW
152
      Err(e) => raise e
×
153
    }
154
  }
155
  String::from_array(arr)
156
}
157

158
///|
159
pub impl Show for StrictChars with output(self : StrictChars, logger : Logger) -> Unit {
NEW
160
  logger.write_string(self.to_string?().to_string())
×
161
}
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