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

moonbitlang / x / 288

02 Dec 2024 07:56AM UTC coverage: 85.037% (-2.8%) from 87.841%
288

Pull #78

github

web-flow
Merge 661d8f5e5 into d72df8024
Pull Request #78: feat: new package encoding

86 of 141 new or added lines in 3 files covered. (60.99%)

1148 of 1350 relevant lines covered (85.04%)

429.5 hits per line

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

87.5
/encoding/encoding.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
/// Encode a given string to the specified character encoding and returns the resulting bytes.
17
///
18
/// # Parameters
19
///
20
/// - `encoding` : The target encoding format.
21
/// - `src`: The input string to be encoded.
22
///
23
/// # Returns
24
///
25
/// A `bytes` representing the encoded string in the selected format.
26
///
27
/// # Examples
28
///
29
/// ```moonbit
30
/// let src = "Hello, World!"
31
/// let encoded_bytes = encode(UTF8, src)
32
/// ```
33
pub fn encode(encoding : Encoding, src : String) -> Bytes {
34
  // NOTE: special case: MoonBit String are already valid UTF16(LE) bytes
35
  match encoding {
4✔
36
    UTF16 | UTF16LE => return src.to_bytes()
2✔
37
    _ => ()
2✔
38
  }
39
  let bytes = src.to_bytes()
40
  let chars = decode_strict(UTF16LE, bytes)
41
  let new_buf = @buffer.T::new(size_hint=bytes.length())
42
  let write = match encoding {
43
    UTF8 => write_utf8_char
1✔
44
    UTF16BE => write_utf16be_char
1✔
NEW
45
    _ => abort("unreachable")
×
46
  }
47
  for char in chars {
48
    // SAFETY: Assume String are always valid UTF16LE
49
    write(new_buf, char.unwrap())
50
  }
51
  new_buf.to_bytes()
52
}
53

54
///|
55
fn write_char(
56
  write : (FixedArray[Byte], Int, Char) -> Int
57
) -> (@buffer.T, Char) -> Unit {
58
  let fixedArr = FixedArray::makei(4, fn { _ => b'\x00' })
2✔
59
  fn {
60
    buf, value => {
76✔
61
      let len = write(fixedArr, 0, value)
62
      let arr = fixedArr.iter().take(len).collect()
63
      buf.write_bytes(@bytes.from_array(arr))
64
    }
65
  }
66
}
67

68
///|
69
/// Write a char into buffer as UTF8.
70
pub let write_utf8_char : (@buffer.T, Char) -> Unit = write_char(
71
  FixedArray::set_utf8_char,
72
)
73

74
///|
75
/// Write a char into buffer as UTF16LE.
76
/// Alias for `write_utf16le_char`
77
pub let write_utf16_char : (@buffer.T, Char) -> Unit = @buffer.write_char
78

79
///|
80
/// Write a char into buffer as UTF16LE.
81
pub let write_utf16le_char : (@buffer.T, Char) -> Unit = @buffer.write_char
82

83
///|
84
/// Write a char into buffer as UTF16BE.
85
pub let write_utf16be_char : (@buffer.T, Char) -> Unit = write_char(
86
  FixedArray::set_utf16be_char,
87
)
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

© 2025 Coveralls, Inc