ctb_formats_eite/formats/
dcbasenb.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! dcBasenb encoding/decoding: pack arbitrary Dcs into Unicode PUA.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use anyhow::Result;
30
31use crate::encoding::basenb::{
32    is_basenb_char, is_basenb_distinct_remainder_char,
33};
34use crate::encoding::pack32::pack32;
35use crate::encoding::utf8::{first_utf8_codepoint, last_utf8_codepoint};
36use crate::formats::FormatLog;
37use crate::formats::utf8::{UTF8FormatSettings, dca_from_utf8, dca_to_utf8};
38use crate::util::array::subset;
39use crate::{bail_if_none, log};
40
41pub const DC_BASENB_EMBEDDED_START: &str = "􍁝􋶀󼷢󺀊󸥎􈺍󲋠􏺐";
42pub const DC_BASENB_EMBEDDED_END: &str = "󼅹󴶯􈡺󿔊􆲦􍸂󲀰􏼝";
43
44/// Start UUID bytes for embedded dcBasenb: e82eef60-19bc-4a00-a44a-763a3445c16f
45/// In StageL/JS, this was called getArmoredUtf8EmbeddedStartUuid. I think it's
46/// specific to dcBasenb, though, not any basenb embedded within UTF-8.
47pub const DC_BASENB_EMBEDDED_START_BYTES: [u8; 32] = [
48    0xf4, 0x8d, 0x81, 0x9d, 0xf4, 0x8b, 0xb6, 0x80, 0xf3, 0xbc, 0xb7, 0xa2,
49    0xf3, 0xba, 0x80, 0x8a, 0xf3, 0xb8, 0xa5, 0x8e, 0xf4, 0x88, 0xba, 0x8d,
50    0xf3, 0xb2, 0x8b, 0xa0, 0xf4, 0x8f, 0xba, 0x90,
51];
52
53/// End UUID bytes for embedded dcBasenb: 60bc936b-f10f-4f50-ab65-3778084060e2
54pub const DC_BASENB_EMBEDDED_END_UUID_BYTES: [u8; 32] = [
55    0xf3, 0xbc, 0x85, 0xb9, 0xf3, 0xb4, 0xb6, 0xaf, 0xf4, 0x88, 0xa1, 0xba,
56    0xf3, 0xbf, 0x94, 0x8a, 0xf4, 0x86, 0xb2, 0xa6, 0xf4, 0x8d, 0xb8, 0x82,
57    0xf3, 0xb2, 0x80, 0xb0, 0xf4, 0x8f, 0xbc, 0x9d,
58];
59
60/*pub const DC_BASENB_EMBEDDED_START_ORIG: [[u8; 4]; 8] = [
61    [244, 141, 129, 157],
62    [244, 139, 182, 128],
63    [243, 188, 183, 162],
64    [243, 186, 128, 138],
65    [243, 184, 165, 142],
66    [244, 136, 186, 141],
67    [243, 178, 139, 160],
68    [244, 143, 186, 144],
69];
70pub const DC_BASENB_EMBEDDED_EN_ORIG: [[u8; 4]; 8] = [
71    [243, 188, 133, 185],
72    [243, 180, 182, 175],
73    [244, 136, 161, 186],
74    [243, 191, 148, 138],
75    [244, 134, 178, 166],
76    [244, 141, 184, 130],
77    [243, 178, 128, 176],
78    [244, 143, 188, 157],
79];
80pub const DC_BASENB_EMBEDDED_START_HEX: [u8; 32] = [
81    0xf4, 0x8d, 0x81, 0x9d, 0xf4, 0x8b, 0xb6, 0x80, 0xf3, 0xbc, 0xb7, 0xa2, 0xf3, 0xba, 0x80, 0x8a,
82    0xf3, 0xb8, 0xa5, 0x8e, 0xf4, 0x88, 0xba, 0x8d, 0xf3, 0xb2, 0x8b, 0xa0, 0xf4, 0x8f, 0xba, 0x90,
83];
84pub const DC_BASENB_EMBEDDED_END_HEX: [u8; 32] = [
85    0xf3, 0xbc, 0x85, 0xb9, 0xf3, 0xb4, 0xb6, 0xaf, 0xf4, 0x88, 0xa1, 0xba, 0xf3, 0xbf, 0x94, 0x8a,
86    0xf4, 0x86, 0xb2, 0xa6, 0xf4, 0x8d, 0xb8, 0x82, 0xf3, 0xb2, 0x80, 0xb0, 0xf4, 0x8f, 0xbc, 0x9d,
87];*/
88
89/// Encode Dcs to a dcBasenb run (with enclosing UUIDs) in UTF-8.
90pub fn dca_to_dcbnb_utf8(
91    dc_array: &[u32],
92    settings: &UTF8FormatSettings,
93) -> Result<(Vec<u8>, FormatLog)> {
94    let mut settings = settings.clone();
95    settings.dc_basenb_enabled = true;
96    dca_to_utf8(dc_array, &settings)
97}
98
99/// Decode Dcs from a dcBasenb run (with enclosing UUIDs) from UTF-8.
100pub fn dca_from_dcbnb_utf8(
101    utf8_bytes: &[u8],
102    settings: &UTF8FormatSettings,
103) -> Result<(Vec<u32>, FormatLog)> {
104    let mut settings = settings.clone();
105    settings.dc_basenb_enabled = true;
106
107    dca_from_utf8(utf8_bytes, &settings)
108}
109
110/// Encode Dcs to a dcBasenb fragment (without enclosing UUIDs) UTF-8.
111pub fn dca_to_dcbnb_fragment_utf8(
112    dc_array: &[u32],
113    settings: &UTF8FormatSettings,
114) -> Result<(Vec<u8>, FormatLog)> {
115    let mut settings = settings.clone();
116    settings.dc_basenb_enabled = true;
117    settings.dc_basenb_fragment_enabled = true;
118
119    dca_to_utf8(dc_array, &settings)
120}
121
122/// Decode Dcs from a dcBasenb fragment (without enclosing UUIDs) from UTF-8.
123pub fn dca_from_dcbnb_fragment_utf8(
124    utf8_bytes: &[u8],
125    settings: &UTF8FormatSettings,
126) -> Result<(Vec<u32>, FormatLog)> {
127    let mut settings = settings.clone();
128    settings.dc_basenb_enabled = true;
129    settings.dc_basenb_fragment_enabled = true;
130
131    dca_from_utf8(utf8_bytes, &settings)
132}
133
134/// Return the first "character" of a dcbnb UTF-8 byte slice, without doing any
135/// conversion. (?? The implementation actually appears to extract a whole dcbnb
136/// run up until the next non-dcbnb char. Maybe just a weird function name.)
137pub fn dcbnb_get_first_char(bytes: &[u8]) -> Result<Vec<u8>> {
138    let mut res: Vec<u8> = Vec::new();
139    if bytes.is_empty() {
140        return Ok(res);
141    }
142    let mut next_utf8;
143    let mut remaining = bytes.to_vec();
144
145    loop {
146        let (cp, consumed) = first_utf8_codepoint(&remaining)?;
147        if consumed == 0 {
148            break;
149        }
150        next_utf8 = pack32(cp)?;
151        if is_basenb_char(&next_utf8) {
152            res.extend_from_slice(&next_utf8);
153            if is_basenb_distinct_remainder_char(&next_utf8) {
154                break;
155            }
156            remaining = bail_if_none!(subset(
157                &remaining,
158                i64::try_from(next_utf8.len())?,
159                -1
160            )?);
161        } else {
162            if res.is_empty() {
163                // FIXME: Tests seem to expect this, but it seems logically
164                // nonsensical. Why not just push next_utf8? Otherwise, the
165                // returned output would mix Dcs and dcbnb characters with no
166                // clear separation.
167                /* let (converted, log) = &dca_from_utf8(&next_utf8, &UTF8FormatSettings::default())?;
168                if log.has_errors() {
169                    crate::error!("Could not decode in dcbnb_get_first_char");
170                    return Ok(Vec::new());
171                }
172                res.extend_from_slice(converted);
173                */
174                res.extend_from_slice(&next_utf8);
175            }
176            break;
177        }
178    }
179    // Check validity - required by some tests
180    let utf8_decoded = dca_from_dcbnb_fragment_utf8(
181        &res,
182        &UTF8FormatSettings {
183            dc_basenb_enabled: true,
184            dc_basenb_fragment_enabled: true,
185            dc_basenb_fragment_strict: true,
186            ..Default::default()
187        },
188    );
189    if utf8_decoded.is_err() {
190        crate::error!("Could not decode in dcbnb_get_first_char");
191        // bail!("Could not decode in dcbnb_get_first_char");
192        // This is just what the tests are looking for. Possibly it should
193        // return an Err instead once I understand its role better.
194        return Ok(Vec::new());
195    }
196    let (_utf8, log) = utf8_decoded?;
197    if log.has_errors() {
198        crate::error!("Could not decode in dcbnb_get_first_char");
199        return Ok(Vec::new());
200    }
201    Ok(res)
202}
203
204/// Return the last "character" (run of base-nb digits from the right, including
205/// at most one distinct remainder char) of a dcbnb UTF-8 byte slice, in the
206/// same packed form.
207pub fn dcbnb_get_last_char(bytes: &[u8]) -> Result<Vec<u8>> {
208    let mut res: Vec<u8> = Vec::new();
209    if bytes.is_empty() {
210        return Ok(res);
211    }
212
213    let mut next_utf8: Vec<u8> = Vec::new();
214    let mut remaining = bytes.to_vec();
215
216    let mut past_first_basenb_remainder_char = false;
217
218    loop {
219        if remaining.is_empty() {
220            next_utf8 = Vec::new();
221        } else {
222            let (cp, consumed) = last_utf8_codepoint(&remaining);
223            if consumed == 0 {
224                break;
225            }
226            next_utf8 = pack32(cp)?;
227        }
228        if !is_basenb_char(&next_utf8) {
229            // If not a basenb char and none collected yet, return just that char.
230            if res.is_empty() {
231                // Prepend next_utf8 to res
232                // (to match set an/res append an/nextUtf8 an/res)
233                res.splice(0..0, next_utf8.iter().copied());
234            }
235            break;
236        } else if is_basenb_distinct_remainder_char(&next_utf8) {
237            if past_first_basenb_remainder_char {
238                break;
239            }
240            res.splice(0..0, next_utf8.iter().copied());
241            remaining.truncate(remaining.len().saturating_sub(next_utf8.len()));
242            past_first_basenb_remainder_char = true;
243        } else {
244            res.splice(0..0, next_utf8.iter().copied());
245            remaining.truncate(remaining.len().saturating_sub(next_utf8.len()));
246        }
247    }
248    // Check validity - required by some tests
249    let utf8_decoded = dca_from_dcbnb_fragment_utf8(
250        &res,
251        &UTF8FormatSettings {
252            dc_basenb_enabled: true,
253            dc_basenb_fragment_enabled: true,
254            dc_basenb_fragment_strict: true,
255            ..Default::default()
256        },
257    );
258    log!("Decoded {:?}", &utf8_decoded);
259    if utf8_decoded.is_err() {
260        crate::error!("Could not decode in dcbnb_get_first_char");
261        // bail!("Could not decode in dcbnb_get_first_char");
262        // This is just what the tests are looking for. Possibly it should
263        // return an Err instead once I understand its role better.
264        return Ok(Vec::new());
265    }
266    let (_utf8, log) = utf8_decoded?;
267    if log.has_errors() {
268        crate::error!("Could not decode in dcbnb_get_first_char");
269        return Ok(Vec::new());
270    }
271    Ok(res)
272}
273
274#[cfg(test)]
275#[allow(
276    clippy::panic,
277    clippy::expect_used,
278    clippy::unwrap_used,
279    clippy::unwrap_in_result,
280    clippy::panic_in_result_fn,
281    clippy::indexing_slicing,
282    clippy::arithmetic_side_effects,
283    reason = "Standard repository test boilerplate"
284)]
285mod tests {
286
287    use const_default::ConstDefault;
288
289    use crate::utilities::assert_vec_u8_ok_eq;
290    use ctb_formats_utilities::{
291        assert_vec_u8_ok_eq_no_errors, assert_vec_u32_ok_eq_no_warnings,
292    };
293
294    use super::*;
295
296    const SETTINGS: UTF8FormatSettings =
297        <UTF8FormatSettings as ConstDefault>::DEFAULT;
298
299    #[crate::ctb_test]
300    fn test_format_dcbnb_conversions() {
301        // dcaToDcbnbUtf8 with single unmappable at end
302        let expected = {
303            let mut v = vec![49, 32, 50];
304            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
305            v.extend([244, 131, 173, 156, 239, 159, 185]);
306            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
307            v
308        };
309        let (_res, log) = assert_vec_u8_ok_eq_no_errors(
310            &expected,
311            dca_to_dcbnb_utf8(
312                &[35, 18, 36, 291],
313                &UTF8FormatSettings {
314                    skip_unmappable: true,
315                    debug: true,
316                    ..Default::default()
317                },
318            ),
319        );
320        assert!(log.has_warnings());
321
322        // dcaToDcbnbUtf8 intermixed mappable/nonmappable
323        let expected = {
324            let mut v = vec![49, 32, 50];
325            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
326            v.extend([244, 131, 173, 156, 239, 159, 185, 50]);
327            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
328            v
329        };
330        let (_res, log) = assert_vec_u8_ok_eq_no_errors(
331            &expected,
332            dca_to_dcbnb_utf8(&[35, 18, 36, 291, 36], &SETTINGS),
333        );
334        assert!(log.has_warnings());
335
336        // From UTF8+dcbnb (two variants differing by last codepoint)
337        let input_a = {
338            let mut v = vec![49, 32, 50];
339            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
340            v.extend([244, 131, 173, 156, 244, 143, 191, 173, 50]);
341            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
342            v
343        };
344        assert_vec_u32_ok_eq_no_warnings(
345            &[35, 18, 36, 291, 36],
346            dca_from_dcbnb_utf8(
347                &input_a,
348                &UTF8FormatSettings {
349                    debug: true,
350                    ..Default::default()
351                },
352            ),
353        );
354
355        let input_b = {
356            let mut v = vec![49, 32, 50];
357            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
358            v.extend([244, 131, 173, 156, 244, 143, 191, 173]);
359            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
360            v
361        };
362        assert_vec_u32_ok_eq_no_warnings(
363            &[35, 18, 36, 291],
364            dca_from_dcbnb_utf8(&input_b, &SETTINGS),
365        );
366
367        // UTF-8 embedded inline in an armored region in another UTF-8, with Dcs at the start and end
368        let input_c = {
369            let mut v = vec![49, 32, 50];
370            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
371            v.extend([
372                244, 131, 173, 156, 244, 143, 191, 173, 50, 244, 131, 173, 156,
373                244, 143, 191, 173,
374            ]);
375            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
376            v.extend([49, 32, 50]);
377            v
378        };
379        assert_vec_u32_ok_eq_no_warnings(
380            &[35, 18, 36, 291, 36, 291, 35, 18, 36],
381            dca_from_dcbnb_utf8(&input_c, &SETTINGS),
382        );
383
384        // Positioning bug tests
385        /* Make sure the dcbnb region gets output at the right place relative to the other chars (there's a bug where it outputs 18 18 11 instead of 18 11 18) */
386        let input_pos_a = {
387            let mut v = vec![32];
388            v.extend(DC_BASENB_EMBEDDED_START_BYTES);
389            v.extend([244, 143, 191, 180, 244, 143, 191, 181]);
390            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
391            v.extend([32]);
392            v
393        };
394        assert_vec_u32_ok_eq_no_warnings(
395            &[18, 11, 18],
396            dca_from_dcbnb_utf8(&input_pos_a, &SETTINGS),
397        );
398
399        let input_pos_b = {
400            let mut v = DC_BASENB_EMBEDDED_START_BYTES.to_vec();
401            v.extend([32, 244, 143, 191, 180, 244, 143, 191, 181, 32]);
402            v.extend(DC_BASENB_EMBEDDED_END_UUID_BYTES);
403            v
404        };
405        assert_vec_u32_ok_eq_no_warnings(
406            &[18, 11, 18],
407            dca_from_dcbnb_utf8(&input_pos_b, &SETTINGS),
408        );
409
410        // Complex multi-region tests
411        let input_first_region = [
412            104, 244, 141, 129, 157, 244, 139, 182, 128, 243, 188, 183, 162,
413            243, 186, 128, 138, 243, 184, 165, 142, 244, 136, 186, 141, 243,
414            178, 139, 160, 244, 143, 186, 144, 244, 143, 191, 184, 244, 143,
415            191, 181, 243, 188, 133, 185, 243, 180, 182, 175, 244, 136, 161,
416            186, 243, 191, 148, 138, 244, 134, 178, 166, 244, 141, 184, 130,
417            243, 178, 128, 176, 244, 143, 188, 157,
418        ];
419        assert_vec_u32_ok_eq_no_warnings(
420            &[89, 7],
421            dca_from_dcbnb_utf8(&input_first_region, &SETTINGS),
422        );
423
424        let input_second_half = [
425            244, 141, 129, 157, 244, 139, 182, 128, 243, 188, 183, 162, 243,
426            186, 128, 138, 243, 184, 165, 142, 244, 136, 186, 141, 243, 178,
427            139, 160, 244, 143, 186, 144, 244, 143, 191, 180, 244, 143, 191,
428            181, 243, 188, 133, 185, 243, 180, 182, 175, 244, 136, 161, 186,
429            243, 191, 148, 138, 244, 134, 178, 166, 244, 141, 184, 130, 243,
430            178, 128, 176, 244, 143, 188, 157,
431        ];
432        assert_vec_u32_ok_eq_no_warnings(
433            &[11],
434            dca_from_dcbnb_utf8(&input_second_half, &SETTINGS),
435        );
436
437        let input_two_regions = [
438            104, 244, 141, 129, 157, 244, 139, 182, 128, 243, 188, 183, 162,
439            243, 186, 128, 138, 243, 184, 165, 142, 244, 136, 186, 141, 243,
440            178, 139, 160, 244, 143, 186, 144, 244, 143, 191, 184, 244, 143,
441            191, 181, 243, 188, 133, 185, 243, 180, 182, 175, 244, 136, 161,
442            186, 243, 191, 148, 138, 244, 134, 178, 166, 244, 141, 184, 130,
443            243, 178, 128, 176, 244, 143, 188, 157, 244, 141, 129, 157, 244,
444            139, 182, 128, 243, 188, 183, 162, 243, 186, 128, 138, 243, 184,
445            165, 142, 244, 136, 186, 141, 243, 178, 139, 160, 244, 143, 186,
446            144, 244, 143, 191, 180, 244, 143, 191, 181, 243, 188, 133, 185,
447            243, 180, 182, 175, 244, 136, 161, 186, 243, 191, 148, 138, 244,
448            134, 178, 166, 244, 141, 184, 130, 243, 178, 128, 176, 244, 143,
449            188, 157,
450        ];
451        assert_vec_u32_ok_eq_no_warnings(
452            &[89, 7, 11],
453            dca_from_dcbnb_utf8(&input_two_regions, &SETTINGS),
454        );
455
456        let input_two_regions_no_leading_h = [
457            244, 141, 129, 157, 244, 139, 182, 128, 243, 188, 183, 162, 243,
458            186, 128, 138, 243, 184, 165, 142, 244, 136, 186, 141, 243, 178,
459            139, 160, 244, 143, 186, 144, 244, 143, 191, 184, 244, 143, 191,
460            181, 243, 188, 133, 185, 243, 180, 182, 175, 244, 136, 161, 186,
461            243, 191, 148, 138, 244, 134, 178, 166, 244, 141, 184, 130, 243,
462            178, 128, 176, 244, 143, 188, 157, 244, 141, 129, 157, 244, 139,
463            182, 128, 243, 188, 183, 162, 243, 186, 128, 138, 243, 184, 165,
464            142, 244, 136, 186, 141, 243, 178, 139, 160, 244, 143, 186, 144,
465            244, 143, 191, 180, 244, 143, 191, 181, 243, 188, 133, 185, 243,
466            180, 182, 175, 244, 136, 161, 186, 243, 191, 148, 138, 244, 134,
467            178, 166, 244, 141, 184, 130, 243, 178, 128, 176, 244, 143, 188,
468            157,
469        ];
470        assert_vec_u32_ok_eq_no_warnings(
471            &[7, 11],
472            dca_from_dcbnb_utf8(&input_two_regions_no_leading_h, &SETTINGS),
473        );
474
475        let input_two_regions_with_h_middle = [
476            244, 141, 129, 157, 244, 139, 182, 128, 243, 188, 183, 162, 243,
477            186, 128, 138, 243, 184, 165, 142, 244, 136, 186, 141, 243, 178,
478            139, 160, 244, 143, 186, 144, 244, 143, 191, 184, 244, 143, 191,
479            181, 243, 188, 133, 185, 243, 180, 182, 175, 244, 136, 161, 186,
480            243, 191, 148, 138, 244, 134, 178, 166, 244, 141, 184, 130, 243,
481            178, 128, 176, 244, 143, 188, 157, 104, 244, 141, 129, 157, 244,
482            139, 182, 128, 243, 188, 183, 162, 243, 186, 128, 138, 243, 184,
483            165, 142, 244, 136, 186, 141, 243, 178, 139, 160, 244, 143, 186,
484            144, 244, 143, 191, 180, 244, 143, 191, 181, 243, 188, 133, 185,
485            243, 180, 182, 175, 244, 136, 161, 186, 243, 191, 148, 138, 244,
486            134, 178, 166, 244, 141, 184, 130, 243, 178, 128, 176, 244, 143,
487            188, 157,
488        ];
489        assert_vec_u32_ok_eq_no_warnings(
490            &[7, 89, 11],
491            dca_from_dcbnb_utf8(&input_two_regions_with_h_middle, &SETTINGS),
492        );
493
494        /* "h\u{10d05d}\u{10bd80}\u{fcde2}\u{fa00a}\u{f894e}\u{108e8d}\u{f22e0}\u{10fe90}\u{10fff8}\u{10fff5}\u{fc179}\u{f4daf}\u{10887a}\u{ff50a}\u{106ca6}\u{10de02}\u{f2030}\u{10ff1d}\u{10d05d}\u{10bd80}\u{fcde2}\u{fa00a}\u{f894e}\u{108e8d}\u{f22e0}\u{10fe90}\u{10fff4}\u{10fff5}\u{fc179}\u{f4daf}\u{10887a}\u{ff50a}\u{106ca6}\u{10de02}\u{f2030}\u{10ff1d}" "244,141,129,157,244,139,182,128,243,188,183,162,243,186,128,138,243,184,165,142,244,136,186,141,243,178,139,160,244,143,186,144,244,143,191,184,244,143,191,181,243,188,133,185,243,180,182,175,244,136,161,186,243,191,148,138,244,134,178,166,244,141,184,130,243,178,128,176,244,143,188,157" "244,141,129,157,244,139,182,128,243,188,183,162,243,186,128,138,243,184,165,142,244,136,186,141,243,178,139,160,244,143,186,144,244,143,191,180,244,143,191,181,243,188,133,185,243,180,182,175,244,136,161,186,243,191,148,138,244,134,178,166,244,141,184,130,243,178,128,176,244,143,188,157" */
495    }
496
497    #[crate::ctb_test]
498    fn test_fragment_decode_bugs() {
499        // Fragment decode bug tests
500
501        /* Test for a bug that results in the output being 16 uppercase letter Bs */
502        assert_vec_u32_ok_eq_no_warnings(
503            &[6],
504            dca_from_dcbnb_fragment_utf8(
505                &[244, 143, 191, 185, 239, 160, 129],
506                &SETTINGS,
507            ),
508        );
509
510        assert_vec_u32_ok_eq_no_warnings(
511            &[82, 86, 5],
512            dca_from_dcbnb_fragment_utf8(
513                &[97, 101, 244, 143, 191, 186, 239, 160, 129],
514                &SETTINGS,
515            ),
516        );
517    }
518
519    #[crate::ctb_test]
520    fn test_dcbnb_get_first_last_char() {
521        // dcbnbGetLastChar tests
522        /* 82 86 5 */
523        assert_vec_u8_ok_eq(
524            &[244, 143, 191, 186, 239, 160, 129],
525            dcbnb_get_last_char(&[97, 101, 244, 143, 191, 186, 239, 160, 129]),
526        );
527        /* invalid */
528        assert_vec_u8_ok_eq(&[], dcbnb_get_last_char(&[239, 160, 129]));
529        /* invalid */
530        assert_vec_u8_ok_eq(
531            &[],
532            dcbnb_get_last_char(&[97, 101, 244, 143, 191, 186]),
533        );
534        /* invalid 82 */
535        // FIXME: I don't understand this assertion. It looks like it's mixing
536        // UTF-8 bytes in with a Dc which couldn't be distinguished from each
537        // other.
538        /* assert_vec_u8_ok_eq(
539            &[82],
540            dcbnb_get_last_char(&[244, 143, 191, 186, 97]),
541        ); */
542
543        // dcbnbGetFirstChar tests
544        /* 5 82 86 */
545        assert_vec_u8_ok_eq(
546            &[244, 143, 191, 186, 239, 160, 129],
547            dcbnb_get_first_char(&[244, 143, 191, 186, 239, 160, 129, 97, 101]),
548        );
549        /* invalid */
550        assert_vec_u8_ok_eq(&[], dcbnb_get_first_char(&[239, 160, 129]));
551        /* invalid 82 86 */
552        assert_vec_u8_ok_eq(
553            &[],
554            dcbnb_get_first_char(&[239, 160, 129, 97, 101]),
555        );
556        /* invalid 82 86 */
557        assert_vec_u8_ok_eq(
558            &[],
559            dcbnb_get_first_char(&[244, 143, 191, 186, 97, 101]),
560        );
561        /* 86 invalid */
562        // Ditto, see above. This est doesn't make sense to me.
563        /* assert_vec_u8_ok_eq(
564            &[86],
565            dcbnb_get_first_char(&[101, 244, 143, 191, 186]),
566        ); */
567    }
568}