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

moonbitlang / x / 329

20 Jan 2025 02:08AM UTC coverage: 83.894% (-1.4%) from 85.298%
329

Pull #93

github

web-flow
Merge 4c8e29b61 into c9fc23933
Pull Request #93: internal: add native_sub.c for sys package

0 of 23 new or added lines in 1 file covered. (0.0%)

1172 of 1397 relevant lines covered (83.89%)

439.26 hits per line

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

0.0
/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 {
NEW
17
  let res : Array[Byte] = []
×
18
  let len = str.length()
19
  let mut i = 0
NEW
20
  while i < len {
×
NEW
21
    let mut c = str[i].to_int()
×
22
    if 0xD800 <= c && c <= 0xDBFF {
NEW
23
      c -= 0xD800
×
24
      i = i + 1
25
      let l = str[i].to_int() - 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 {
NEW
31
      res.push(c.to_byte())
×
NEW
32
    } else if c < 0x800 {
×
NEW
33
      res.push((0xc0 + (c >> 6)).to_byte())
×
34
      res.push((0x80 + (c & 0x3f)).to_byte())
NEW
35
    } else if c < 0x10000 {
×
NEW
36
      res.push((0xe0 + (c >> 12)).to_byte())
×
37
      res.push((0x80 + ((c >> 6) & 0x3f)).to_byte())
38
      res.push((0x80 + (c & 0x3f)).to_byte())
39
    } else {
NEW
40
      res.push((0xf0 + (c >> 18)).to_byte())
×
41
      res.push((0x80 + ((c >> 12) & 0x3f)).to_byte())
42
      res.push((0x80 + ((c >> 6) & 0x3f)).to_byte())
43
      res.push((0x80 + (c & 0x3f)).to_byte())
44
    }
45
    i = i + 1
46
  }
47
  if is_filename {
NEW
48
    res.push((0).to_byte())
×
49
  }
50
  Bytes::from_array(res)
51
}
52

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