Fix some nits in CBC_PDF417HighLevelEncoder. - Give methods and some variables the right names. - Make some constants constexpr and initialize them in the header or move them into an anonymous namespace. Change-Id: I521ad27f5236e7260cf9f5009128a17aabdc0e9b Reviewed-on: https://pdfium-review.googlesource.com/c/45670 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/pdf417/BC_PDF417.cpp b/fxbarcode/pdf417/BC_PDF417.cpp index ee45242..5bbe740 100644 --- a/fxbarcode/pdf417/BC_PDF417.cpp +++ b/fxbarcode/pdf417/BC_PDF417.cpp
@@ -409,7 +409,7 @@ int32_t e = BCExceptionNO; WideString highLevel = - CBC_PDF417HighLevelEncoder::encodeHighLevel(msg, m_compaction, e); + CBC_PDF417HighLevelEncoder::EncodeHighLevel(msg, m_compaction, e); if (e != BCExceptionNO) return false; int32_t sourceCodeWords = highLevel.GetLength();
diff --git a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp index e7e64e8..8a90495 100644 --- a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp +++ b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp
@@ -31,24 +31,25 @@ #define SUBMODE_LOWER 1 #define SUBMODE_MIXED 2 -const int32_t CBC_PDF417HighLevelEncoder::TEXT_COMPACTION = 0; -const int32_t CBC_PDF417HighLevelEncoder::BYTE_COMPACTION = 1; -const int32_t CBC_PDF417HighLevelEncoder::NUMERIC_COMPACTION = 2; -const int32_t CBC_PDF417HighLevelEncoder::SUBMODE_PUNCTUATION = 3; -const int32_t CBC_PDF417HighLevelEncoder::LATCH_TO_TEXT = 900; -const int32_t CBC_PDF417HighLevelEncoder::LATCH_TO_BYTE_PADDED = 901; -const int32_t CBC_PDF417HighLevelEncoder::LATCH_TO_NUMERIC = 902; -const int32_t CBC_PDF417HighLevelEncoder::SHIFT_TO_BYTE = 913; -const int32_t CBC_PDF417HighLevelEncoder::LATCH_TO_BYTE = 924; -const uint8_t CBC_PDF417HighLevelEncoder::TEXT_MIXED_RAW[] = { - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58, - 35, 45, 46, 36, 47, 43, 37, 42, 61, 94, 0, 32, 0, 0, 0}; -const uint8_t CBC_PDF417HighLevelEncoder::TEXT_PUNCTUATION_RAW[] = { +namespace { + +constexpr int32_t kLatchToText = 900; +constexpr int32_t kLatchToBytePadded = 901; +constexpr int32_t kLatchToNumeric = 902; +constexpr int32_t kShiftToByte = 913; +constexpr int32_t kLatchToByte = 924; + +constexpr uint8_t kTextMixedRaw[] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 38, 13, 9, 44, 58, 35, 45, 46, 36, 47, + 43, 37, 42, 61, 94, 0, 32, 0, 0, 0}; +constexpr uint8_t kTextPunctuationRaw[] = { 59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58, 10, 45, 46, 36, 47, 34, 124, 42, 40, 41, 63, 123, 125, 39, 0}; -int32_t CBC_PDF417HighLevelEncoder::MIXED[128] = {0}; -int32_t CBC_PDF417HighLevelEncoder::PUNCTUATION[128] = {0}; +int32_t g_mixed[128] = {0}; +int32_t g_punctuation[128] = {0}; + +} // namespace void CBC_PDF417HighLevelEncoder::Initialize() { Inverse(); @@ -56,7 +57,7 @@ void CBC_PDF417HighLevelEncoder::Finalize() {} -WideString CBC_PDF417HighLevelEncoder::encodeHighLevel(WideString wideMsg, +WideString CBC_PDF417HighLevelEncoder::EncodeHighLevel(WideString wideMsg, Compaction compaction, int32_t& e) { ByteString bytes; @@ -79,35 +80,35 @@ size_t p = 0; int32_t textSubMode = SUBMODE_ALPHA; if (compaction == TEXT) { - encodeText(msg, p, len, sb, textSubMode); + EncodeText(msg, p, len, sb, textSubMode); } else if (compaction == BYTES) { - encodeBinary(&byteArr, p, byteArr.size(), BYTE_COMPACTION, sb); + EncodeBinary(&byteArr, p, byteArr.size(), BYTE_COMPACTION, sb); } else if (compaction == NUMERIC) { - sb += LATCH_TO_NUMERIC; - encodeNumeric(msg, p, len, sb); + sb += kLatchToNumeric; + EncodeNumeric(msg, p, len, sb); } else { - int32_t encodingMode = LATCH_TO_TEXT; + int32_t encodingMode = kLatchToText; while (p < len) { - size_t n = determineConsecutiveDigitCount(msg, p); + size_t n = DetermineConsecutiveDigitCount(msg, p); if (n >= 13) { - sb += LATCH_TO_NUMERIC; + sb += kLatchToNumeric; encodingMode = NUMERIC_COMPACTION; textSubMode = SUBMODE_ALPHA; - encodeNumeric(msg, p, n, sb); + EncodeNumeric(msg, p, n, sb); p += n; } else { - size_t t = determineConsecutiveTextCount(msg, p); + size_t t = DetermineConsecutiveTextCount(msg, p); if (t >= 5 || n == len) { if (encodingMode != TEXT_COMPACTION) { - sb += LATCH_TO_TEXT; + sb += kLatchToText; encodingMode = TEXT_COMPACTION; textSubMode = SUBMODE_ALPHA; } - textSubMode = encodeText(msg, p, t, sb, textSubMode); + textSubMode = EncodeText(msg, p, t, sb, textSubMode); p += t; } else { Optional<size_t> b = - determineConsecutiveBinaryCount(msg, &byteArr, p); + DetermineConsecutiveBinaryCount(msg, &byteArr, p); if (!b) { e = BCExceptionNonEncodableCharacterDetected; return L" "; @@ -117,9 +118,9 @@ b_value = 1; } if (b_value == 1 && encodingMode == TEXT_COMPACTION) { - encodeBinary(&byteArr, p, 1, TEXT_COMPACTION, sb); + EncodeBinary(&byteArr, p, 1, TEXT_COMPACTION, sb); } else { - encodeBinary(&byteArr, p, b_value, encodingMode, sb); + EncodeBinary(&byteArr, p, b_value, encodingMode, sb); encodingMode = BYTE_COMPACTION; textSubMode = SUBMODE_ALPHA; } @@ -132,26 +133,26 @@ } void CBC_PDF417HighLevelEncoder::Inverse() { - for (size_t l = 0; l < FX_ArraySize(MIXED); ++l) - MIXED[l] = -1; + for (size_t l = 0; l < FX_ArraySize(g_mixed); ++l) + g_mixed[l] = -1; - for (uint8_t i = 0; i < FX_ArraySize(TEXT_MIXED_RAW); ++i) { - uint8_t b = TEXT_MIXED_RAW[i]; + for (uint8_t i = 0; i < FX_ArraySize(kTextMixedRaw); ++i) { + uint8_t b = kTextMixedRaw[i]; if (b != 0) - MIXED[b] = i; + g_mixed[b] = i; } - for (size_t l = 0; l < FX_ArraySize(PUNCTUATION); ++l) - PUNCTUATION[l] = -1; + for (size_t l = 0; l < FX_ArraySize(g_punctuation); ++l) + g_punctuation[l] = -1; - for (uint8_t i = 0; i < FX_ArraySize(TEXT_PUNCTUATION_RAW); ++i) { - uint8_t b = TEXT_PUNCTUATION_RAW[i]; + for (uint8_t i = 0; i < FX_ArraySize(kTextPunctuationRaw); ++i) { + uint8_t b = kTextPunctuationRaw[i]; if (b != 0) - PUNCTUATION[b] = i; + g_punctuation[b] = i; } } -int32_t CBC_PDF417HighLevelEncoder::encodeText(WideString msg, +int32_t CBC_PDF417HighLevelEncoder::EncodeText(WideString msg, size_t startpos, size_t count, WideString& sb, @@ -164,77 +165,77 @@ wchar_t ch = msg[startpos + idx]; switch (submode) { case SUBMODE_ALPHA: - if (isAlphaUpper(ch)) { + if (IsAlphaUpper(ch)) { if (ch == ' ') tmp += 26; else tmp += ch - 65; break; } - if (isAlphaLower(ch)) { + if (IsAlphaLower(ch)) { submode = SUBMODE_LOWER; tmp += 27; continue; } - if (isMixed(ch)) { + if (IsMixed(ch)) { submode = SUBMODE_MIXED; tmp += 28; continue; } tmp += 29; - tmp += PUNCTUATION[ch]; + tmp += g_punctuation[ch]; break; case SUBMODE_LOWER: - if (isAlphaLower(ch)) { + if (IsAlphaLower(ch)) { if (ch == ' ') tmp += 26; else tmp += ch - 97; break; } - if (isAlphaUpper(ch)) { + if (IsAlphaUpper(ch)) { tmp += 27; tmp += ch - 65; break; } - if (isMixed(ch)) { + if (IsMixed(ch)) { submode = SUBMODE_MIXED; tmp += 28; continue; } tmp += 29; - tmp += PUNCTUATION[ch]; + tmp += g_punctuation[ch]; break; case SUBMODE_MIXED: - if (isMixed(ch)) { - tmp += MIXED[ch]; + if (IsMixed(ch)) { + tmp += g_mixed[ch]; break; } - if (isAlphaUpper(ch)) { + if (IsAlphaUpper(ch)) { submode = SUBMODE_ALPHA; tmp += 28; continue; } - if (isAlphaLower(ch)) { + if (IsAlphaLower(ch)) { submode = SUBMODE_LOWER; tmp += 27; continue; } if (startpos + idx + 1 < count) { wchar_t next = msg[startpos + idx + 1]; - if (isPunctuation(next)) { + if (IsPunctuation(next)) { submode = SUBMODE_PUNCTUATION; tmp += 25; continue; } } tmp += 29; - tmp += PUNCTUATION[ch]; + tmp += g_punctuation[ch]; break; default: - if (isPunctuation(ch)) { - tmp += PUNCTUATION[ch]; + if (IsPunctuation(ch)) { + tmp += g_punctuation[ch]; break; } submode = SUBMODE_ALPHA; @@ -261,17 +262,17 @@ return submode; } -void CBC_PDF417HighLevelEncoder::encodeBinary(std::vector<uint8_t>* bytes, +void CBC_PDF417HighLevelEncoder::EncodeBinary(std::vector<uint8_t>* bytes, size_t startpos, size_t count, int32_t startmode, WideString& sb) { - if (count == 1 && startmode == TEXT_COMPACTION) { - sb += SHIFT_TO_BYTE; - } + if (count == 1 && startmode == TEXT_COMPACTION) + sb += kShiftToByte; + size_t idx = startpos; if (count >= 6) { - sb += LATCH_TO_BYTE; + sb += kLatchToByte; wchar_t chars[5]; while ((startpos + count - idx) >= 6) { int64_t t = 0; @@ -289,14 +290,14 @@ } } if (idx < startpos + count) - sb += LATCH_TO_BYTE_PADDED; + sb += kLatchToBytePadded; for (size_t i = idx; i < startpos + count; i++) { int32_t ch = (*bytes)[i] & 0xff; sb += ch; } } -void CBC_PDF417HighLevelEncoder::encodeNumeric(WideString msg, +void CBC_PDF417HighLevelEncoder::EncodeNumeric(WideString msg, size_t startpos, size_t count, WideString& sb) { @@ -318,31 +319,31 @@ } } -bool CBC_PDF417HighLevelEncoder::isDigit(wchar_t ch) { +bool CBC_PDF417HighLevelEncoder::IsDigit(wchar_t ch) { return ch >= '0' && ch <= '9'; } -bool CBC_PDF417HighLevelEncoder::isAlphaUpper(wchar_t ch) { +bool CBC_PDF417HighLevelEncoder::IsAlphaUpper(wchar_t ch) { return ch == ' ' || (ch >= 'A' && ch <= 'Z'); } -bool CBC_PDF417HighLevelEncoder::isAlphaLower(wchar_t ch) { +bool CBC_PDF417HighLevelEncoder::IsAlphaLower(wchar_t ch) { return ch == ' ' || (ch >= 'a' && ch <= 'z'); } -bool CBC_PDF417HighLevelEncoder::isMixed(wchar_t ch) { - return MIXED[ch] != -1; +bool CBC_PDF417HighLevelEncoder::IsMixed(wchar_t ch) { + return g_mixed[ch] != -1; } -bool CBC_PDF417HighLevelEncoder::isPunctuation(wchar_t ch) { - return PUNCTUATION[ch] != -1; +bool CBC_PDF417HighLevelEncoder::IsPunctuation(wchar_t ch) { + return g_punctuation[ch] != -1; } -bool CBC_PDF417HighLevelEncoder::isText(wchar_t ch) { +bool CBC_PDF417HighLevelEncoder::IsText(wchar_t ch) { return ch == '\t' || ch == '\n' || ch == '\r' || (ch >= 32 && ch <= 126); } -size_t CBC_PDF417HighLevelEncoder::determineConsecutiveDigitCount( +size_t CBC_PDF417HighLevelEncoder::DetermineConsecutiveDigitCount( WideString msg, size_t startpos) { size_t count = 0; @@ -350,7 +351,7 @@ size_t idx = startpos; if (idx < len) { wchar_t ch = msg[idx]; - while (isDigit(ch) && idx < len) { + while (IsDigit(ch) && idx < len) { count++; idx++; if (idx < len) @@ -360,7 +361,7 @@ return count; } -size_t CBC_PDF417HighLevelEncoder::determineConsecutiveTextCount( +size_t CBC_PDF417HighLevelEncoder::DetermineConsecutiveTextCount( WideString msg, size_t startpos) { size_t len = msg.GetLength(); @@ -368,7 +369,7 @@ while (idx < len) { wchar_t ch = msg[idx]; size_t numericCount = 0; - while (numericCount < 13 && isDigit(ch) && idx < len) { + while (numericCount < 13 && IsDigit(ch) && idx < len) { numericCount++; idx++; if (idx < len) @@ -379,14 +380,14 @@ if (numericCount > 0) continue; ch = msg[idx]; - if (!isText(ch)) + if (!IsText(ch)) break; idx++; } return idx - startpos; } -Optional<size_t> CBC_PDF417HighLevelEncoder::determineConsecutiveBinaryCount( +Optional<size_t> CBC_PDF417HighLevelEncoder::DetermineConsecutiveBinaryCount( WideString msg, std::vector<uint8_t>* bytes, size_t startpos) { @@ -395,7 +396,7 @@ while (idx < len) { wchar_t ch = msg[idx]; size_t numericCount = 0; - while (numericCount < 13 && isDigit(ch)) { + while (numericCount < 13 && IsDigit(ch)) { numericCount++; size_t i = idx + numericCount; if (i >= len) @@ -406,7 +407,7 @@ return idx - startpos; size_t textCount = 0; - while (textCount < 5 && isText(ch)) { + while (textCount < 5 && IsText(ch)) { textCount++; size_t i = idx + textCount; if (i >= len)
diff --git a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h index 5b652bd..c5a1512 100644 --- a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h +++ b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h
@@ -14,7 +14,7 @@ class CBC_PDF417HighLevelEncoder { public: - static WideString encodeHighLevel(WideString msg, + static WideString EncodeHighLevel(WideString msg, Compaction compaction, int32_t& e); static void Inverse(); @@ -22,44 +22,34 @@ static void Finalize(); private: - static const int32_t TEXT_COMPACTION; - static const int32_t BYTE_COMPACTION; - static const int32_t NUMERIC_COMPACTION; - static const int32_t SUBMODE_PUNCTUATION; - static const int32_t LATCH_TO_TEXT; - static const int32_t LATCH_TO_BYTE_PADDED; - static const int32_t LATCH_TO_NUMERIC; - static const int32_t SHIFT_TO_BYTE; - static const int32_t LATCH_TO_BYTE; - static const uint8_t TEXT_MIXED_RAW[]; - static const uint8_t TEXT_PUNCTUATION_RAW[]; + static constexpr int32_t TEXT_COMPACTION = 0; + static constexpr int32_t BYTE_COMPACTION = 1; + static constexpr int32_t NUMERIC_COMPACTION = 2; + static constexpr int32_t SUBMODE_PUNCTUATION = 3; - static int32_t MIXED[128]; - static int32_t PUNCTUATION[128]; - - static int32_t encodeText(WideString msg, + static int32_t EncodeText(WideString msg, size_t startpos, size_t count, WideString& sb, int32_t initialSubmode); - static void encodeBinary(std::vector<uint8_t>* bytes, + static void EncodeBinary(std::vector<uint8_t>* bytes, size_t startpos, size_t count, int32_t startmode, WideString& sb); - static void encodeNumeric(WideString msg, + static void EncodeNumeric(WideString msg, size_t startpos, size_t count, WideString& sb); - static bool isDigit(wchar_t ch); - static bool isAlphaUpper(wchar_t ch); - static bool isAlphaLower(wchar_t ch); - static bool isMixed(wchar_t ch); - static bool isPunctuation(wchar_t ch); - static bool isText(wchar_t ch); - static size_t determineConsecutiveDigitCount(WideString msg, size_t startpos); - static size_t determineConsecutiveTextCount(WideString msg, size_t startpos); - static Optional<size_t> determineConsecutiveBinaryCount( + static bool IsDigit(wchar_t ch); + static bool IsAlphaUpper(wchar_t ch); + static bool IsAlphaLower(wchar_t ch); + static bool IsMixed(wchar_t ch); + static bool IsPunctuation(wchar_t ch); + static bool IsText(wchar_t ch); + static size_t DetermineConsecutiveDigitCount(WideString msg, size_t startpos); + static size_t DetermineConsecutiveTextCount(WideString msg, size_t startpos); + static Optional<size_t> DetermineConsecutiveBinaryCount( WideString msg, std::vector<uint8_t>* bytes, size_t startpos);
diff --git a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp index ad4a59a..c06aeca 100644 --- a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp +++ b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp
@@ -49,7 +49,7 @@ } WideString expected(ptr->expected, ptr->expected_length); WideString result; - CBC_PDF417HighLevelEncoder::encodeBinary( + CBC_PDF417HighLevelEncoder::EncodeBinary( &input_array, ptr->offset, ptr->count, ptr->startmode, result); EXPECT_EQ(expected, result) << " for case number " << i; } @@ -112,7 +112,7 @@ WideString input(ptr->input); WideString expected(ptr->expected, ptr->expected_length); WideString result; - CBC_PDF417HighLevelEncoder::encodeNumeric(input, ptr->offset, ptr->count, + CBC_PDF417HighLevelEncoder::EncodeNumeric(input, ptr->offset, ptr->count, result); EXPECT_EQ(expected, result) << " for case number " << i; } @@ -158,7 +158,7 @@ ConsecutiveDigitCase* ptr = &consecutive_digit_cases[i]; WideString input(ptr->input); int actual_count = - CBC_PDF417HighLevelEncoder::determineConsecutiveDigitCount(input, + CBC_PDF417HighLevelEncoder::DetermineConsecutiveDigitCount(input, ptr->offset); EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i; } @@ -219,7 +219,7 @@ ConsecutiveTextCase* ptr = &consecutive_text_cases[i]; WideString input(ptr->input); int actual_count = - CBC_PDF417HighLevelEncoder::determineConsecutiveTextCount(input, + CBC_PDF417HighLevelEncoder::DetermineConsecutiveTextCount(input, ptr->offset); EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i; }