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

moonbitlang / x / 489

06 Jun 2025 10:13AM UTC coverage: 92.072% (+1.9%) from 90.218%
489

Pull #147

github

web-flow
Merge bea124531 into ae19a4d88
Pull Request #147: minor: adapt latest toolchain

23 of 29 new or added lines in 5 files covered. (79.31%)

29 existing lines in 8 files now uncovered.

1835 of 1993 relevant lines covered (92.07%)

407.49 hits per line

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

23.81
/internal/ffi/string_convert.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
pub fn mbt_string_to_utf8_bytes(str : String, is_filename : Bool) -> Bytes {
17
  let res : Array[Byte] = []
1✔
18
  let len = str.length()
1✔
19
  let mut i = 0
20
  while i < len {
21
    let mut c = str.charcode_at(i)
14✔
22
    if 0xD800 <= c && c <= 0xDBFF {
23
      c -= 0xD800
×
24
      i = i + 1
UNCOV
25
      let l = str.charcode_at(i) - 0xDC00
×
26
      c = (c << 10) + l + 0x10000
27
    }
28

29
    // stdout accepts UTF-8, so convert the stream to UTF-8 first
30
    if c < 0x80 {
31
      res.push(c.to_byte())
14✔
32
    } else if c < 0x800 {
×
33
      res.push((0xc0 + (c >> 6)).to_byte())
×
UNCOV
34
      res.push((0x80 + (c & 0x3f)).to_byte())
×
35
    } else if c < 0x10000 {
×
36
      res.push((0xe0 + (c >> 12)).to_byte())
×
UNCOV
37
      res.push((0x80 + ((c >> 6) & 0x3f)).to_byte())
×
UNCOV
38
      res.push((0x80 + (c & 0x3f)).to_byte())
×
39
    } else {
40
      res.push((0xf0 + (c >> 18)).to_byte())
×
UNCOV
41
      res.push((0x80 + ((c >> 12) & 0x3f)).to_byte())
×
UNCOV
42
      res.push((0x80 + ((c >> 6) & 0x3f)).to_byte())
×
UNCOV
43
      res.push((0x80 + (c & 0x3f)).to_byte())
×
44
    }
45
    i = i + 1
46
  }
47
  if is_filename {
48
    res.push((0).to_byte())
×
49
  }
50
  Bytes::from_array(res)
1✔
51
}
52

53
///|
54
pub fn utf8_bytes_to_mbt_string(bytes : Bytes) -> String {
55
  let res : Array[Char] = []
1✔
56
  let len = bytes.length()
1✔
57
  let mut i = 0
58
  while i < len {
59
    let mut c = bytes[i].to_int()
14✔
60
    if c < 0x80 {
61
      res.push(Int::unsafe_to_char(c))
14✔
62
      i += 1
63
    } else if c < 0xE0 {
×
64
      if i + 1 >= len {
×
65
        break
×
66
      }
UNCOV
67
      c = ((c & 0x1F) << 6) | (bytes[i + 1].to_int() & 0x3F)
×
UNCOV
68
      res.push(Int::unsafe_to_char(c))
×
69
      i += 2
70
    } else if c < 0xF0 {
×
71
      if i + 2 >= len {
×
72
        break
×
73
      }
74
      c = ((c & 0x0F) << 12) |
UNCOV
75
        ((bytes[i + 1].to_int() & 0x3F) << 6) |
×
UNCOV
76
        (bytes[i + 2].to_int() & 0x3F)
×
UNCOV
77
      res.push(Int::unsafe_to_char(c))
×
78
      i += 3
79
    } else {
80
      if i + 3 >= len {
×
81
        break
×
82
      }
83
      c = ((c & 0x07) << 18) |
UNCOV
84
        ((bytes[i + 1].to_int() & 0x3F) << 12) |
×
UNCOV
85
        ((bytes[i + 2].to_int() & 0x3F) << 6) |
×
UNCOV
86
        (bytes[i + 3].to_int() & 0x3F)
×
87
      c -= 0x10000
UNCOV
88
      res.push(Int::unsafe_to_char((c >> 10) + 0xD800))
×
UNCOV
89
      res.push(Int::unsafe_to_char((c & 0x3FF) + 0xDC00))
×
90
      i += 4
91
    }
92
  }
93
  String::from_array(res)
1✔
94
}
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