1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use rxing_one_d_proc_derive::OneDWriter;
use crate::common::Result;
use crate::BarcodeFormat;
use super::{code_128_reader, OneDimensionalCodeWriter};
const CODE_START_A: usize = 103;
const CODE_START_B: usize = 104;
const CODE_START_C: usize = 105;
const CODE_CODE_A: usize = 101;
const CODE_CODE_B: usize = 100;
const CODE_CODE_C: usize = 99;
const CODE_STOP: usize = 106;
// Dummy characters used to specify control characters in input
const ESCAPE_FNC_1: char = '\u{00f1}';
const ESCAPE_FNC_2: char = '\u{00f2}';
const ESCAPE_FNC_3: char = '\u{00f3}';
const ESCAPE_FNC_4: char = '\u{00f4}';
const CODE_FNC_1: usize = 102; // Code A, Code B, Code C
const CODE_FNC_2: usize = 97; // Code A, Code B
const CODE_FNC_3: usize = 96; // Code A, Code B
const CODE_FNC_4_A: usize = 101; // Code A
const CODE_FNC_4_B: usize = 100; // Code B
// RXingResults of minimal lookahead for code C
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CType {
Uncodable,
OneDigit,
TwoDigits,
Fnc1,
}
/**
* This object renders a CODE128 code as a {@link BitMatrix}.
*
* @author erik.barbara@gmail.com (Erik Barbara)
*/
#[derive(OneDWriter, Default)]
pub struct Code128Writer;
impl OneDimensionalCodeWriter for Code128Writer {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
self.encode_oned_with_hints(contents, &HashMap::new())
}
fn getSupportedWriteFormats(&self) -> Option<Vec<crate::BarcodeFormat>> {
Some(vec![BarcodeFormat::CODE_128])
}
fn encode_oned_with_hints(
&self,
contents: &str,
hints: &crate::EncodingHintDictionary,
) -> Result<Vec<bool>> {
let forcedCodeSet = check(contents, hints)?;
let hasCompactionHint = matches!(
hints.get(&EncodeHintType::CODE128_COMPACT),
Some(EncodeHintValue::Code128Compact(true))
);
// let hasCompactionHint = if let Some(EncodeHintValue::Code128Compact(compat)) =
// hints.get(&EncodeHintType::CODE128_COMPACT)
// {
// *compat
// } else {
// false
// };
// let hasCompactionHint = hints != null && hints.containsKey(EncodeHintType::CODE128_COMPACT) &&
// Boolean.parseBoolean(hints.get(EncodeHintType::CODE128_COMPACT).toString());
if hasCompactionHint {
MinimalEncoder::encode(contents)
} else {
encodeFast(contents, forcedCodeSet)
}
}
}
fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32> {
let length = contents.chars().count();
// Check length
if !(1..=80).contains(&length) {
return Err(Exceptions::illegal_argument_with(format!(
"Contents length should be between 1 and 80 characters, but got {length}"
)));
}
// Check for forced code set hint.
let mut forcedCodeSet = -1_i32;
if hints.contains_key(&EncodeHintType::FORCE_CODE_SET) {
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) =
hints.get(&EncodeHintType::FORCE_CODE_SET)
else {
return Err(Exceptions::ILLEGAL_STATE);
};
match codeSetHint.as_str() {
"A" => forcedCodeSet = CODE_CODE_A as i32,
"B" => forcedCodeSet = CODE_CODE_B as i32,
"C" => forcedCodeSet = CODE_CODE_C as i32,
_ => {
return Err(Exceptions::illegal_argument_with(format!(
"Unsupported code set hint: {codeSetHint}"
)))
}
}
}
// Check content
for ch in contents.chars() {
let c = ch as u32;
// for (int i = 0; i < length; i++) {
// char c = contents.charAt(i);
// check for non ascii characters that are not special GS1 characters
match ch {
// special function characters
ESCAPE_FNC_1 | ESCAPE_FNC_2 | ESCAPE_FNC_3 | ESCAPE_FNC_4 => {}
// non ascii characters
_ => {
if c > 127 {
// no full Latin-1 character set available at the moment
// shift and manual code change are not supported
return Err(Exceptions::illegal_argument_with(format!(
"Bad character in input: ASCII value={c}"
)));
}
}
}
// check characters for compatibility with forced code set
const CODE_CODE_A_I32: i32 = CODE_CODE_A as i32;
const CODE_CODE_B_I32: i32 = CODE_CODE_B as i32;
const CODE_CODE_C_I32: i32 = CODE_CODE_C as i32;
match forcedCodeSet {
CODE_CODE_A_I32 =>
// allows no ascii above 95 (no lower caps, no special symbols)
{
if c > 95 && c <= 127 {
return Err(Exceptions::illegal_argument_with(format!(
"Bad character in input for forced code set A: ASCII value={c}"
)));
}
}
CODE_CODE_B_I32 =>
// allows no ascii below 32 (terminal symbols)
{
if c <= 32 {
return Err(Exceptions::illegal_argument_with(format!(
"Bad character in input for forced code set B: ASCII value={c}"
)));
}
}
CODE_CODE_C_I32 =>
// allows only numbers and no FNC 2/3/4
{
if c < 48
|| (c > 57 && c <= 127)
|| ch == ESCAPE_FNC_2
|| ch == ESCAPE_FNC_3
|| ch == ESCAPE_FNC_4
{
return Err(Exceptions::illegal_argument_with(format!(
"Bad character in input for forced code set C: ASCII value={c}"
)));
}
}
_ => {}
}
}
Ok(forcedCodeSet)
}
fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>> {
let length = contents.chars().count();
let mut patterns: Vec<Vec<usize>> = Vec::new(); //new ArrayList<>(); // temporary storage for patterns
let mut checkSum = 0;
let mut checkWeight = 1;
let mut codeSet = 0; // selected code (CODE_CODE_B or CODE_CODE_C)
let mut position = 0; // position in contents
while position < length {
//Select code to use
let newCodeSet = if forcedCodeSet == -1 {
chooseCode(contents, position, codeSet).ok_or(Exceptions::ILLEGAL_STATE)?
} else {
forcedCodeSet as usize // THIS IS RISKY
};
//Get the pattern index
let mut patternIndex: isize;
if newCodeSet == codeSet {
// Encode the current character
// First handle escapes
match contents
.chars()
.nth(position)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
{
ESCAPE_FNC_1 => patternIndex = CODE_FNC_1 as isize,
ESCAPE_FNC_2 => patternIndex = CODE_FNC_2 as isize,
ESCAPE_FNC_3 => patternIndex = CODE_FNC_3 as isize,
ESCAPE_FNC_4 => {
if codeSet == CODE_CODE_A {
patternIndex = CODE_FNC_4_A as isize;
} else {
patternIndex = CODE_FNC_4_B as isize;
}
}
_ =>
// Then handle normal characters otherwise
{
match codeSet {
CODE_CODE_A => {
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
as isize
- ' ' as isize;
if patternIndex < 0 {
// everything below a space character comes behind the underscore in the code patterns table
patternIndex += '`' as isize;
}
}
CODE_CODE_B => {
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
as isize
- ' ' as isize
}
_ => {
// CODE_CODE_C
if position + 1 == length {
// this is the last character, but the encoding is C, which always encodes two characers
return Err(Exceptions::illegal_argument_with(
"Bad number of characters for digit only encoding.",
));
}
let s: String = contents
.char_indices()
.skip(position)
.take(2)
.map(|(_u, c)| c)
.collect();
patternIndex = s.parse::<isize>().map_err(|e| {
Exceptions::parse_with(format!("issue parsing {s}: {e}"))
})?;
position += 1;
} // Also incremented below
}
}
}
position += 1;
} else {
// Should we change the current code?
// Do we have a code set?
if codeSet == 0 {
// No, we don't have a code set
match newCodeSet {
CODE_CODE_A => patternIndex = CODE_START_A as isize,
CODE_CODE_B => patternIndex = CODE_START_B as isize,
_ => patternIndex = CODE_START_C as isize,
}
} else {
// Yes, we have a code set
patternIndex = newCodeSet as isize;
}
codeSet = newCodeSet;
}
// Get the pattern
patterns.push(
code_128_reader::CODE_PATTERNS[patternIndex as usize]
.iter()
.map(|x| *x as usize)
.collect(),
);
// Compute checksum
checkSum += patternIndex * checkWeight;
if position != 0 {
checkWeight += 1;
}
}
Ok(produceRXingResult(&mut patterns, checkSum as usize))
}
fn produceRXingResult(patterns: &mut Vec<Vec<usize>>, checkSum: usize) -> Vec<bool> {
// Compute and append checksum
let mut checkSum = checkSum;
checkSum %= 103;
patterns.push(
code_128_reader::CODE_PATTERNS[checkSum]
.iter()
.map(|x| *x as usize)
.collect(),
);
// Append stop code
patterns.push(
code_128_reader::CODE_PATTERNS[CODE_STOP]
.iter()
.map(|x| *x as usize)
.collect(),
);
// Compute code width
let mut codeWidth = 0_usize;
for pattern in &mut *patterns {
codeWidth += pattern.iter().sum::<usize>();
}
// Compute result
let mut result = vec![false; codeWidth];
let mut pos = 0;
for pattern in patterns {
// for (int[] pattern : patterns) {
pos += Code128Writer::appendPattern(&mut result, pos, pattern, true) as usize;
}
result
}
fn findCType(value: &str, start: usize) -> Option<CType> {
let last = value.chars().count();
if start >= last {
return Some(CType::Uncodable);
}
let c = value.chars().nth(start)?;
if c == ESCAPE_FNC_1 {
return Some(CType::Fnc1);
}
if !c.is_ascii_digit() {
return Some(CType::Uncodable);
}
if start + 1 >= last {
return Some(CType::OneDigit);
}
let c = value.chars().nth(start + 1)?;
if !c.is_ascii_digit() {
return Some(CType::OneDigit);
}
Some(CType::TwoDigits)
}
fn chooseCode(value: &str, start: usize, oldCode: usize) -> Option<usize> {
let mut lookahead = findCType(value, start)?;
if lookahead == CType::OneDigit {
if oldCode == CODE_CODE_A {
return Some(CODE_CODE_A);
}
return Some(CODE_CODE_B);
}
if lookahead == CType::Uncodable {
if start < value.chars().count() {
let c = value.chars().nth(start)?;
if c < ' '
|| (oldCode == CODE_CODE_A && (c < '`' || (c >= ESCAPE_FNC_1 && c <= ESCAPE_FNC_4)))
{
// can continue in code A, encodes ASCII 0 to 95 or FNC1 to FNC4
return Some(CODE_CODE_A);
}
}
return Some(CODE_CODE_B); // no choice
}
if oldCode == CODE_CODE_A && lookahead == CType::Fnc1 {
return Some(CODE_CODE_A);
}
if oldCode == CODE_CODE_C {
// can continue in code C
return Some(CODE_CODE_C);
}
if oldCode == CODE_CODE_B {
if lookahead == CType::Fnc1 {
return Some(CODE_CODE_B); // can continue in code B
}
// Seen two consecutive digits, see what follows
lookahead = findCType(value, start + 2)?;
if lookahead == CType::Uncodable || lookahead == CType::OneDigit {
return Some(CODE_CODE_B); // not worth switching now
}
if lookahead == CType::Fnc1 {
// two digits, then FNC_1...
lookahead = findCType(value, start + 3)?;
if lookahead == CType::TwoDigits {
// then two more digits, switch
return Some(CODE_CODE_C);
} else {
return Some(CODE_CODE_B); // otherwise not worth switching
}
}
// At this point, there are at least 4 consecutive digits.
// Look ahead to choose whether to switch now or on the next round.
let mut index = start + 4;
let mut lookahead = findCType(value, index)?;
while lookahead == CType::TwoDigits {
// while (lookahead = findCType(value, index)) == CType::TWO_DIGITS {
index += 2;
lookahead = findCType(value, index)?;
}
if lookahead == CType::OneDigit {
// odd number of digits, switch later
return Some(CODE_CODE_B);
}
return Some(CODE_CODE_C); // even number of digits, switch now
}
// Here oldCode == 0, which means we are choosing the initial code
if lookahead == CType::Fnc1 {
// ignore FNC_1
lookahead = findCType(value, start + 1)?;
}
if lookahead == CType::TwoDigits {
// at least two digits, start in code C
return Some(CODE_CODE_C);
}
Some(CODE_CODE_B)
}
/**
* Encodes minimally using Divide-And-Conquer with Memoization
**/
// struct MinimalEncoder {
// memoizedCost:Vec<Vec<u32>>,
// minPath:Vec<Vec<Latch>>,
// }
mod MinimalEncoder {
use crate::{common::Result, oned::code_128_reader, Exceptions};
use super::{
produceRXingResult, CODE_CODE_A, CODE_CODE_B, CODE_CODE_C, CODE_FNC_1, CODE_FNC_2,
CODE_FNC_3, CODE_FNC_4_A, CODE_FNC_4_B, CODE_START_A, CODE_START_B, CODE_START_C,
ESCAPE_FNC_1, ESCAPE_FNC_2, ESCAPE_FNC_3, ESCAPE_FNC_4,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Charset {
A,
B,
C,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Latch {
A,
B,
C,
Shift,
None,
}
const A : &str = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\u{0000}\u{0001}\u{0002}/
\u{0003}\u{0004}\u{0005}\u{0006}\u{0007}\u{0008}\u{0009}\n\u{000B}\u{000C}\r\u{000E}\u{000F}\u{0010}\u{0011}/
\u{0012}\u{0013}\u{0014}\u{0015}\u{0016}\u{0017}\u{0018}\u{0019}\u{001A}\u{001B}\u{001C}\u{001D}\u{001E}\u{001F}/
\u{00FF}";
const B: &str =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr\
stuvwxyz{|}~\u{007F}\u{00FF}";
const CODE_SHIFT: usize = 98;
pub fn encode(contents: &str) -> Result<Vec<bool>> {
let length = contents.chars().count();
let mut memoizedCost = vec![vec![0_u32; length]; 4]; //new int[4][contents.length()];
let mut minPath = vec![vec![Latch::None; length]; 4]; //new Latch[4][contents.length()];
encode_with_start_position(contents, Charset::None, 0, &mut memoizedCost, &mut minPath)?;
let mut patterns: Vec<Vec<usize>> = Vec::new(); //new ArrayList<>();
let mut checkSum = vec![0_usize]; //new int[] {0};
let mut checkWeight = vec![1]; //new int[] {1};
let mut charset = Charset::None;
let mut i = 0;
while i < length {
// for i in 0..length {
// for (int i = 0; i < length; i++) {
let latch = minPath[charset.ordinal()][i];
match latch {
Latch::A => {
charset = Charset::A;
addPattern(
&mut patterns,
if i == 0 { CODE_START_A } else { CODE_CODE_A },
&mut checkSum,
&mut checkWeight,
i,
);
}
Latch::B => {
charset = Charset::B;
addPattern(
&mut patterns,
if i == 0 { CODE_START_B } else { CODE_CODE_B },
&mut checkSum,
&mut checkWeight,
i,
);
}
Latch::C => {
charset = Charset::C;
addPattern(
&mut patterns,
if i == 0 { CODE_START_C } else { CODE_CODE_C },
&mut checkSum,
&mut checkWeight,
i,
);
}
Latch::Shift => addPattern(
&mut patterns,
CODE_SHIFT,
&mut checkSum,
&mut checkWeight,
i,
),
Latch::None => { /* skip */ }
}
if charset == Charset::C {
if contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
== ESCAPE_FNC_1
{
addPattern(
&mut patterns,
CODE_FNC_1,
&mut checkSum,
&mut checkWeight,
i,
);
} else {
let s: String = contents
.char_indices()
.skip(i)
.take(2)
.map(|(_u, c)| c)
.collect();
addPattern(
&mut patterns,
s.parse::<usize>().map_err(|e| {
Exceptions::parse_with(format!("unable to parse {s} {e}"))
})?,
&mut checkSum,
&mut checkWeight,
i,
);
assert!(i + 1 < length); //the algorithm never leads to a single trailing digit in character set C
if i + 1 < length {
i += 1;
}
}
} else {
// charset A or B
let mut patternIndex = match contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
{
ESCAPE_FNC_1 => CODE_FNC_1 as isize,
ESCAPE_FNC_2 => CODE_FNC_2 as isize,
ESCAPE_FNC_3 => CODE_FNC_3 as isize,
ESCAPE_FNC_4 => {
if (charset == Charset::A && latch != Latch::Shift)
|| (charset == Charset::B && latch == Latch::Shift)
{
CODE_FNC_4_A as isize
} else {
CODE_FNC_4_B as isize
}
}
_ => {
contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
as isize
- ' ' as isize
}
};
if ((charset == Charset::A && latch != Latch::Shift)
|| (charset == Charset::B && latch == Latch::Shift))
&& patternIndex < 0
{
patternIndex += '`' as isize;
}
addPattern(
&mut patterns,
patternIndex as usize,
&mut checkSum,
&mut checkWeight,
i,
);
}
i += 1;
}
// memoizedCost.clear();
// minPath.clear();
Ok(produceRXingResult(&mut patterns, checkSum[0]))
}
fn addPattern(
patterns: &mut Vec<Vec<usize>>,
patternIndex: usize,
checkSum: &mut [usize],
checkWeight: &mut [u32],
position: usize,
) {
patterns.push(
code_128_reader::CODE_PATTERNS[patternIndex]
.iter()
.map(|x| *x as usize)
.collect(),
);
if position != 0 {
checkWeight[0] += 1;
}
checkSum[0] += patternIndex * checkWeight[0] as usize;
}
fn isDigit(c: char) -> bool {
c.is_ascii_digit()
}
fn canEncode(contents: &str, charset: Charset, position: usize) -> bool {
let Some(c) = contents.chars().nth(position) else {
return false;
};
match charset {
Charset::A => {
c == ESCAPE_FNC_1
|| c == ESCAPE_FNC_2
|| c == ESCAPE_FNC_3
|| c == ESCAPE_FNC_4
|| A.find(c).is_some()
}
Charset::B => {
c == ESCAPE_FNC_1
|| c == ESCAPE_FNC_2
|| c == ESCAPE_FNC_3
|| c == ESCAPE_FNC_4
|| B.find(c).is_some()
}
Charset::C => {
let Some(c_p_1) = contents.chars().nth(position + 1) else {
return false;
};
c == ESCAPE_FNC_1
|| (position + 1 < contents.chars().count() && isDigit(c) && isDigit(c_p_1))
}
_ => false,
}
}
/**
* Encode the string starting at position position starting with the character set charset
**/
fn encode_with_start_position(
contents: &str,
charset: Charset,
position: usize,
memoizedCost: &mut Vec<Vec<u32>>,
minPath: &mut Vec<Vec<Latch>>,
) -> Result<u32> {
if position >= contents.chars().count() {
return Err(Exceptions::ILLEGAL_STATE);
}
let mCost = memoizedCost[charset.ordinal()][position];
if mCost > 0 {
return Ok(mCost);
}
let mut minCost = u32::MAX;
let mut minLatch = Latch::None;
let atEnd = position + 1 >= contents.chars().count();
let sets = [Charset::A, Charset::B];
for i in 0..=1 {
// for (int i = 0; i <= 1; i++) {
if canEncode(contents, sets[i], position) {
let mut cost = 1;
let mut latch = Latch::None;
if charset != sets[i] {
cost += 1;
latch = sets[i].into();
}
if !atEnd {
cost += encode_with_start_position(
contents,
sets[i],
position + 1,
memoizedCost,
minPath,
)?;
}
if cost < minCost {
minCost = cost;
minLatch = latch;
}
cost = 1;
if charset == sets[(i + 1) % 2] {
cost += 1;
latch = Latch::Shift;
if !atEnd {
cost += encode_with_start_position(
contents,
charset,
position + 1,
memoizedCost,
minPath,
)?;
}
if cost < minCost {
minCost = cost;
minLatch = latch;
}
}
}
}
if canEncode(contents, Charset::C, position) {
let mut cost = 1;
let mut latch = Latch::None;
if charset != Charset::C {
cost += 1;
latch = Latch::C;
}
let advance = if contents.chars().nth(position).unwrap_or_default() == ESCAPE_FNC_1 {
1
} else {
2
};
if position + advance < contents.chars().count() {
cost += encode_with_start_position(
contents,
Charset::C,
position + advance,
memoizedCost,
minPath,
)?;
}
if cost < minCost {
minCost = cost;
minLatch = latch;
}
}
if minCost == u32::MAX {
return Err(Exceptions::illegal_argument_with(format!(
"Bad character in input: ASCII value={}",
contents.chars().nth(position).unwrap_or('x')
)));
// throw new IllegalArgumentException("Bad character in input: ASCII value=" + (int) contents.charAt(position));
}
memoizedCost[charset.ordinal()][position] = minCost;
minPath[charset.ordinal()][position] = minLatch;
Ok(minCost)
}
trait HasOrdinal {
fn ordinal(&self) -> usize;
}
impl HasOrdinal for Charset {
fn ordinal(&self) -> usize {
match self {
Charset::A => 0,
Charset::B => 1,
Charset::C => 2,
Charset::None => 3,
}
}
}
impl HasOrdinal for Latch {
fn ordinal(&self) -> usize {
match self {
Latch::A => 0,
Latch::B => 1,
Latch::C => 2,
Latch::Shift => 3,
Latch::None => 4,
}
}
}
impl From<Charset> for Latch {
fn from(cs: Charset) -> Self {
match cs {
Charset::A => Latch::A,
Charset::B => Latch::B,
Charset::C => Latch::C,
Charset::None => Latch::None,
}
}
}
}