|
| 1 | +// Copyright 2025 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 | +///| |
| 17 | +fn mbt_string_to_utf8_bytes(str : String, is_c_str : Bool) -> Bytes { |
| 18 | + let res : Array[Byte] = [] |
| 19 | + let len = str.length() |
| 20 | + let mut i = 0 |
| 21 | + while i < len { |
| 22 | + let mut c = str[i].to_int() |
| 23 | + if 0xD800 <= c && c <= 0xDBFF { |
| 24 | + c -= 0xD800 |
| 25 | + i = i + 1 |
| 26 | + let l = str[i].to_int() - 0xDC00 |
| 27 | + c = (c << 10) + l + 0x10000 |
| 28 | + } |
| 29 | + |
| 30 | + // stdout accepts UTF-8, so convert the stream to UTF-8 first |
| 31 | + if c < 0x80 { |
| 32 | + res.push(c.to_byte()) |
| 33 | + } else if c < 0x800 { |
| 34 | + res.push((0xc0 + (c >> 6)).to_byte()) |
| 35 | + res.push((0x80 + (c & 0x3f)).to_byte()) |
| 36 | + } else if c < 0x10000 { |
| 37 | + res.push((0xe0 + (c >> 12)).to_byte()) |
| 38 | + res.push((0x80 + ((c >> 6) & 0x3f)).to_byte()) |
| 39 | + res.push((0x80 + (c & 0x3f)).to_byte()) |
| 40 | + } else { |
| 41 | + res.push((0xf0 + (c >> 18)).to_byte()) |
| 42 | + res.push((0x80 + ((c >> 12) & 0x3f)).to_byte()) |
| 43 | + res.push((0x80 + ((c >> 6) & 0x3f)).to_byte()) |
| 44 | + res.push((0x80 + (c & 0x3f)).to_byte()) |
| 45 | + } |
| 46 | + i = i + 1 |
| 47 | + } |
| 48 | + if is_c_str { |
| 49 | + res.push((0).to_byte()) |
| 50 | + } |
| 51 | + Bytes::from_array(res) |
| 52 | +} |
| 53 | + |
| 54 | +fn utf8_bytes_to_mbt_string(bytes : Bytes) -> String { |
| 55 | + let res : Array[Char] = [] |
| 56 | + let len = bytes.length() |
| 57 | + let mut i = 0 |
| 58 | + while i < len { |
| 59 | + let mut c = bytes[i].to_int() |
| 60 | + if c < 0x80 { |
| 61 | + res.push(Char::from_int(c)) |
| 62 | + i += 1 |
| 63 | + } else if c < 0xE0 { |
| 64 | + if i + 1 >= len { |
| 65 | + break |
| 66 | + } |
| 67 | + c = ((c & 0x1F) << 6) | (bytes[i + 1].to_int() & 0x3F) |
| 68 | + res.push(Char::from_int(c)) |
| 69 | + i += 2 |
| 70 | + } else if c < 0xF0 { |
| 71 | + if i + 2 >= len { |
| 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 { |
| 80 | + if i + 3 >= len { |
| 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 | +} |
0 commit comments