1#[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
44pub 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
53pub 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
60pub 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
99pub 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
110pub 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
122pub 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
134pub 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 res.extend_from_slice(&next_utf8);
175 }
176 break;
177 }
178 }
179 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 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
204pub 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 res.is_empty() {
231 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 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 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 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 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 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 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 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 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 }
496
497 #[crate::ctb_test]
498 fn test_fragment_decode_bugs() {
499 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 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 assert_vec_u8_ok_eq(&[], dcbnb_get_last_char(&[239, 160, 129]));
529 assert_vec_u8_ok_eq(
531 &[],
532 dcbnb_get_last_char(&[97, 101, 244, 143, 191, 186]),
533 );
534 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 assert_vec_u8_ok_eq(&[], dcbnb_get_first_char(&[239, 160, 129]));
551 assert_vec_u8_ok_eq(
553 &[],
554 dcbnb_get_first_char(&[239, 160, 129, 97, 101]),
555 );
556 assert_vec_u8_ok_eq(
558 &[],
559 dcbnb_get_first_char(&[244, 143, 191, 186, 97, 101]),
560 );
561 }
568}