From fff400a5df032c2203eef9f76a5dbb164672df3d Mon Sep 17 00:00:00 2001 From: tsepez Date: Wed, 4 Jan 2017 06:34:19 -0800 Subject: Remove CFX_ArrayTemplate in BC_QRCoderEncoder. Also, class Make_Pair is confusing given std::make_pair(). No need for vectors of pointers when there are reasonable copy ctors. Save 288 bytes in a table while we're at it, and make local to file. Fix spelling of "Numberic". Review-Url: https://codereview.chromium.org/2595203002 --- xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp | 187 +++++++++++------------------ xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h | 22 ++-- 2 files changed, 82 insertions(+), 127 deletions(-) diff --git a/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp b/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp index ddfbff2e3c..332f1de9c2 100644 --- a/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp +++ b/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp @@ -40,7 +40,9 @@ #include "xfa/fxbarcode/qrcode/BC_QRCoderMode.h" #include "xfa/fxbarcode/qrcode/BC_QRCoderVersion.h" -const int32_t CBC_QRCoderEncoder::m_alphaNumbericTable[] = { +namespace { + +const int8_t g_alphaNumericTable[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, @@ -48,31 +50,12 @@ const int32_t CBC_QRCoderEncoder::m_alphaNumbericTable[] = { -1, 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, -1, -1, -1, -1, -1}; +} // namespace + CBC_QRCoderEncoder::CBC_QRCoderEncoder() {} CBC_QRCoderEncoder::~CBC_QRCoderEncoder() {} -class Make_Pair { - public: - CBC_QRCoderMode* m_mode; - CFX_ByteString m_string; - - private: - Make_Pair(const Make_Pair& mode_string) {} - Make_Pair& operator=(Make_Pair& mode_string) { - if (this == &mode_string) { - return *this; - } - m_mode = mode_string.m_mode; - m_string = mode_string.m_string; - return *this; - } - - public: - Make_Pair(CBC_QRCoderMode* mode, const CFX_ByteString& str) - : m_mode(mode), m_string(str) {} - ~Make_Pair() {} -}; void CBC_QRCoderEncoder::Encode(const CFX_ByteString& content, CBC_QRCoderErrorCorrectionLevel* ecLevel, CBC_QRCoder* qrCode, @@ -93,26 +76,27 @@ void CBC_QRCoderEncoder::Encode(const CFX_ByteString& content, void CBC_QRCoderEncoder::AppendECI(CBC_QRCoderBitVector* bits) {} void CBC_QRCoderEncoder::AppendDataModeLenghInfo( - const CFX_ArrayTemplate& splitResult, + const std::vector>& + splitResults, CBC_QRCoderBitVector& headerAndDataBits, CBC_QRCoderMode* tempMode, CBC_QRCoder* qrCode, CFX_ByteString& encoding, int32_t& e) { - for (int32_t i = 0; i < splitResult.GetSize(); i++) { - tempMode = splitResult[i]->m_mode; + for (const auto& splitResult : splitResults) { + tempMode = splitResult.first; if (tempMode == CBC_QRCoderMode::sGBK) { AppendModeInfo(tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendLengthInfo(splitResult[i]->m_string.GetLength(), - qrCode->GetVersion(), tempMode, &headerAndDataBits, e); + AppendLengthInfo(splitResult.second.GetLength(), qrCode->GetVersion(), + tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendBytes(splitResult[i]->m_string, tempMode, &headerAndDataBits, - encoding, e); + AppendBytes(splitResult.second, tempMode, &headerAndDataBits, encoding, + e); BC_EXCEPTION_CHECK_ReturnVoid(e); } else if (tempMode == CBC_QRCoderMode::sBYTE) { CFX_ByteArray bytes; - CBC_UtilCodingConvert::LocaleToUtf8(splitResult[i]->m_string, bytes); + CBC_UtilCodingConvert::LocaleToUtf8(splitResult.second, bytes); AppendModeInfo(tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); AppendLengthInfo(bytes.GetSize(), qrCode->GetVersion(), tempMode, @@ -123,20 +107,20 @@ void CBC_QRCoderEncoder::AppendDataModeLenghInfo( } else if (tempMode == CBC_QRCoderMode::sALPHANUMERIC) { AppendModeInfo(tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendLengthInfo(splitResult[i]->m_string.GetLength(), - qrCode->GetVersion(), tempMode, &headerAndDataBits, e); + AppendLengthInfo(splitResult.second.GetLength(), qrCode->GetVersion(), + tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendBytes(splitResult[i]->m_string, tempMode, &headerAndDataBits, - encoding, e); + AppendBytes(splitResult.second, tempMode, &headerAndDataBits, encoding, + e); BC_EXCEPTION_CHECK_ReturnVoid(e); } else if (tempMode == CBC_QRCoderMode::sNUMERIC) { AppendModeInfo(tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendLengthInfo(splitResult[i]->m_string.GetLength(), - qrCode->GetVersion(), tempMode, &headerAndDataBits, e); + AppendLengthInfo(splitResult.second.GetLength(), qrCode->GetVersion(), + tempMode, &headerAndDataBits, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - AppendBytes(splitResult[i]->m_string, tempMode, &headerAndDataBits, - encoding, e); + AppendBytes(splitResult.second, tempMode, &headerAndDataBits, encoding, + e); BC_EXCEPTION_CHECK_ReturnVoid(e); } else { e = BCExceptionUnknown; @@ -145,8 +129,9 @@ void CBC_QRCoderEncoder::AppendDataModeLenghInfo( } } -void CBC_QRCoderEncoder::SplitString(const CFX_ByteString& content, - CFX_ArrayTemplate* result) { +void CBC_QRCoderEncoder::SplitString( + const CFX_ByteString& content, + std::vector>* result) { int32_t index = 0, flag = 0; while ( (((uint8_t)content[index] >= 0xA1 && (uint8_t)content[index] <= 0xAA) || @@ -155,8 +140,7 @@ void CBC_QRCoderEncoder::SplitString(const CFX_ByteString& content, index += 2; } if (index != flag) { - result->Add( - new Make_Pair(CBC_QRCoderMode::sGBK, content.Mid(flag, index - flag))); + result->push_back({CBC_QRCoderMode::sGBK, content.Mid(flag, index - flag)}); } flag = index; if (index >= content.GetLength()) { @@ -179,8 +163,8 @@ void CBC_QRCoderEncoder::SplitString(const CFX_ByteString& content, } } if (index != flag) { - result->Add( - new Make_Pair(CBC_QRCoderMode::sBYTE, content.Mid(flag, index - flag))); + result->push_back( + {CBC_QRCoderMode::sBYTE, content.Mid(flag, index - flag)}); } flag = index; if (index >= content.GetLength()) { @@ -191,8 +175,8 @@ void CBC_QRCoderEncoder::SplitString(const CFX_ByteString& content, index++; } if (index != flag) { - result->Add(new Make_Pair(CBC_QRCoderMode::sNUMERIC, - content.Mid(flag, index - flag))); + result->push_back( + {CBC_QRCoderMode::sNUMERIC, content.Mid(flag, index - flag)}); } flag = index; if (index >= content.GetLength()) { @@ -203,8 +187,8 @@ void CBC_QRCoderEncoder::SplitString(const CFX_ByteString& content, index++; } if (index != flag) { - result->Add(new Make_Pair(CBC_QRCoderMode::sALPHANUMERIC, - content.Mid(flag, index - flag))); + result->push_back( + {CBC_QRCoderMode::sALPHANUMERIC, content.Mid(flag, index - flag)}); } flag = index; if (index < content.GetLength()) @@ -258,59 +242,50 @@ int32_t CBC_QRCoderEncoder::GetSpanByVersion(CBC_QRCoderMode* modeFirst, return -1; } -void CBC_QRCoderEncoder::MergeString(CFX_ArrayTemplate* result, - int32_t versionNum, - int32_t& e) { - Make_Pair* first = nullptr; - Make_Pair* second = nullptr; +void CBC_QRCoderEncoder::MergeString( + std::vector>* result, + int32_t versionNum, + int32_t& e) { size_t mergeNum = 0; - int32_t i; - for (i = 0; ((i < result->GetSize()) && (i + 1 < result->GetSize())); i++) { - first = (*result)[i]; - second = (*result)[i + 1]; - if (first->m_mode == CBC_QRCoderMode::sALPHANUMERIC) { + for (size_t i = 0; i + 1 < result->size(); i++) { + auto element1 = &(*result)[i]; + auto element2 = &(*result)[i + 1]; + if (element1->first == CBC_QRCoderMode::sALPHANUMERIC) { int32_t tmp = GetSpanByVersion(CBC_QRCoderMode::sALPHANUMERIC, CBC_QRCoderMode::sBYTE, versionNum, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - if ((second->m_mode == CBC_QRCoderMode::sBYTE) && - (first->m_string.GetLength() < tmp)) { - CFX_ByteString str = first->m_string + second->m_string; - second->m_string = str; - delete first; - result->RemoveAt(i); + if (element2->first == CBC_QRCoderMode::sBYTE && + element1->second.GetLength() < tmp) { + element2->second = element1->second + element2->second; + result->erase(result->begin() + i); i--; mergeNum++; } - } else if (first->m_mode == CBC_QRCoderMode::sBYTE) { - if (second->m_mode == CBC_QRCoderMode::sBYTE) { - first->m_string += second->m_string; - delete second; - result->RemoveAt(i + 1); + } else if (element1->first == CBC_QRCoderMode::sBYTE) { + if (element2->first == CBC_QRCoderMode::sBYTE) { + element1->second += element2->second; + result->erase(result->begin() + i + 1); i--; mergeNum++; } - } else if (first->m_mode == CBC_QRCoderMode::sNUMERIC) { + } else if (element1->first == CBC_QRCoderMode::sNUMERIC) { int32_t tmp = GetSpanByVersion(CBC_QRCoderMode::sNUMERIC, CBC_QRCoderMode::sBYTE, versionNum, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - if ((second->m_mode == CBC_QRCoderMode::sBYTE) && - (first->m_string.GetLength() < tmp)) { - CFX_ByteString str = first->m_string + second->m_string; - second->m_string = str; - delete first; - result->RemoveAt(i); + if (element2->first == CBC_QRCoderMode::sBYTE && + element1->second.GetLength() < tmp) { + element2->second = element1->second + element2->second; + result->erase(result->begin() + i); i--; mergeNum++; } tmp = GetSpanByVersion(CBC_QRCoderMode::sNUMERIC, CBC_QRCoderMode::sALPHANUMERIC, versionNum, e); BC_EXCEPTION_CHECK_ReturnVoid(e); - if ((second->m_mode == CBC_QRCoderMode::sALPHANUMERIC) && - (first->m_string.GetLength() < tmp)) { - CFX_ByteString str = first->m_string + second->m_string; - second->m_string = str; - delete first; - result->RemoveAt(i); + if (element2->first == CBC_QRCoderMode::sALPHANUMERIC && + element1->second.GetLength() < tmp) { + element2->second = element1->second + element2->second; + result->erase(result->begin() + i); i--; mergeNum++; } @@ -360,42 +335,31 @@ void CBC_QRCoderEncoder::EncodeWithSpecifyVersion( int32_t& e) { CFX_ByteString encoding = "utf8"; CBC_QRCoderMode* mode = CBC_QRCoderMode::sBYTE; - CFX_ArrayTemplate splitResult; + std::vector> splitResult; CBC_QRCoderBitVector dataBits; dataBits.Init(); SplitString(content, &splitResult); MergeString(&splitResult, versionSpecify, e); BC_EXCEPTION_CHECK_ReturnVoid(e) CBC_QRCoderMode* tempMode = nullptr; - for (int32_t i = 0; i < splitResult.GetSize(); i++) { - AppendBytes(splitResult[i]->m_string, splitResult[i]->m_mode, &dataBits, - encoding, e); - if (e != BCExceptionNO) { - for (int32_t y = 0; y < splitResult.GetSize(); y++) - delete splitResult[y]; + for (const auto& result : splitResult) { + AppendBytes(result.second, result.first, &dataBits, encoding, e); + if (e != BCExceptionNO) return; - } } int32_t numInputBytes = dataBits.sizeInBytes(); CBC_QRCoderBitVector headerAndDataBits; headerAndDataBits.Init(); InitQRCode(numInputBytes, versionSpecify, ecLevel, mode, qrCode, e); - if (e != BCExceptionNO) { - for (int32_t k = 0; k < splitResult.GetSize(); k++) - delete splitResult[k]; + if (e != BCExceptionNO) return; - } + AppendDataModeLenghInfo(splitResult, headerAndDataBits, tempMode, qrCode, encoding, e); - if (e != BCExceptionNO) { - for (int32_t k = 0; k < splitResult.GetSize(); k++) - delete splitResult[k]; + if (e != BCExceptionNO) return; - } + numInputBytes = headerAndDataBits.sizeInBytes(); TerminateBits(qrCode->GetNumDataBytes(), &headerAndDataBits, e); - for (int32_t j = 0; j < splitResult.GetSize(); j++) { - delete splitResult[j]; - } if (e != BCExceptionNO) return; @@ -430,21 +394,17 @@ void CBC_QRCoderEncoder::EncodeWithAutoVersion( int32_t& e) { CFX_ByteString encoding = "utf8"; CBC_QRCoderMode* mode = CBC_QRCoderMode::sBYTE; - CFX_ArrayTemplate splitResult; + std::vector> splitResult; CBC_QRCoderBitVector dataBits; dataBits.Init(); SplitString(content, &splitResult); MergeString(&splitResult, 8, e); BC_EXCEPTION_CHECK_ReturnVoid(e); CBC_QRCoderMode* tempMode = nullptr; - for (int32_t i = 0; i < splitResult.GetSize(); i++) { - AppendBytes(splitResult[i]->m_string, splitResult[i]->m_mode, &dataBits, - encoding, e); - if (e != BCExceptionNO) { - for (int32_t l = 0; l < splitResult.GetSize(); l++) - delete splitResult[l]; + for (const auto& result : splitResult) { + AppendBytes(result.second, result.first, &dataBits, encoding, e); + if (e != BCExceptionNO) return; - } } int32_t numInputBytes = dataBits.sizeInBytes(); InitQRCode(numInputBytes, ecLevel, mode, qrCode, e); @@ -477,13 +437,9 @@ catchException: e = BCExceptionNO; goto sign; } else { - for (int32_t j = 0; j < splitResult.GetSize(); j++) - delete splitResult[j]; return; } } - for (int32_t k = 0; k < splitResult.GetSize(); k++) - delete splitResult[k]; CBC_QRCoderBitVector finalBits; finalBits.Init(); @@ -655,10 +611,7 @@ CBC_QRCoderMode* CBC_QRCoderEncoder::ChooseMode(const CFX_ByteString& content, } int32_t CBC_QRCoderEncoder::GetAlphaNumericCode(int32_t code) { - if (code < 96 && code >= 0) { - return m_alphaNumbericTable[code]; - } - return -1; + return (code >= 0 && code < 96) ? g_alphaNumericTable[code] : -1; } void CBC_QRCoderEncoder::AppendBytes(const CFX_ByteString& content, diff --git a/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h b/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h index 6303d7c1c5..649938a060 100644 --- a/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h +++ b/xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h @@ -7,6 +7,9 @@ #ifndef XFA_FXBARCODE_QRCODE_BC_QRCODERENCODER_H_ #define XFA_FXBARCODE_QRCODE_BC_QRCODERENCODER_H_ +#include +#include + #include "core/fxcrt/fx_basic.h" #include "core/fxcrt/fx_string.h" @@ -16,12 +19,8 @@ class CBC_QRCoderMode; class CBC_QRCoderBitVector; class CBC_CommonByteArray; class CBC_CommonByteMatrix; -class Make_Pair; class CBC_QRCoderEncoder { - private: - static const int32_t m_alphaNumbericTable[96]; - public: CBC_QRCoderEncoder(); virtual ~CBC_QRCoderEncoder(); @@ -121,13 +120,16 @@ class CBC_QRCoderEncoder { CBC_QRCoderMode* modeSecond, int32_t versionNum, int32_t& e); - static void MergeString(CFX_ArrayTemplate* result, - int32_t versionNum, - int32_t& e); - static void SplitString(const CFX_ByteString& content, - CFX_ArrayTemplate* result); + static void MergeString( + std::vector>* result, + int32_t versionNum, + int32_t& e); + static void SplitString( + const CFX_ByteString& content, + std::vector>* result); static void AppendDataModeLenghInfo( - const CFX_ArrayTemplate& splitResult, + const std::vector>& + splitResult, CBC_QRCoderBitVector& headerAndDataBits, CBC_QRCoderMode* tempMode, CBC_QRCoder* qrCode, -- cgit v1.2.3