From fdcdc6519ab7af487039ce69c92f7ab113404bd2 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Wed, 21 Nov 2018 01:49:30 +0100 Subject: [PATCH] better code quality --- src/bitpack.cc | 28 +++++++++++++--------------- src/common.cc | 7 ++++--- src/compress.cc | 3 --- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/bitpack.cc b/src/bitpack.cc index c36a031..ac965b8 100644 --- a/src/bitpack.cc +++ b/src/bitpack.cc @@ -65,13 +65,11 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, bool zero_rs = (right_shift == 0); right_shift -= step; - if (right_shift < 0) { // si right_shift est inférieur à zéro + if (right_shift < 0 && !zero_rs) { + // si right_shift est inférieur à zéro // si right_shift était différent de zéro, alors extra octet - if (!zero_rs) { - current_char = - static_cast(masks[t_n] >> (-right_shift) & 0xffu); - t_res.push_back(current_char); - } + current_char = static_cast(masks[t_n] >> (-right_shift) & 0xFFU); + t_res.push_back(current_char); } t_res.push_back(static_cast(masks[t_n])); return pack_n(it, t_input_end, t_res, t_n + 1); @@ -82,25 +80,24 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, left_shift = (left_shift - t_n) + step; } t_res.push_back( - static_cast(current_char | (*it >> left_shift & 0xFFu))); - // current_char = 0; + static_cast(current_char | (*it >> left_shift & 0xFFU))); bool zero_rs = (right_shift == 0); right_shift -= step; if (right_shift < 0) { if (!zero_rs) { - current_char = static_cast(*it >> (-right_shift) & 0xFFu); + current_char = static_cast(*it >> (-right_shift) & 0xFFU); t_res.push_back(current_char); } right_shift = 8 + right_shift; } if (right_shift == 0) { - current_char = static_cast(*it & 0xffu); + current_char = static_cast(*it & 0xFFU); t_res.push_back(current_char); current_char = 0; char_touched = false; } else { - current_char = static_cast(*it << right_shift & 0xFFu); + current_char = static_cast(*it << right_shift & 0xFFU); char_touched = true; } } @@ -112,9 +109,9 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, vuchar pack_16(const vuint16::const_iterator t_input_begin, const vuint16::const_iterator t_input_end, vuchar &t_res) { - std::for_each(t_input_begin, t_input_end, [&](const auto value) { - t_res.push_back(static_cast(value >> 8 & 0xFFu)); - t_res.push_back(static_cast(value & 0xFFu)); + std::for_each(t_input_begin, t_input_end, [&t_res](const auto value) { + t_res.push_back(static_cast(value >> 8 & 0xFFU)); + t_res.push_back(static_cast(value & 0xFFU)); }); return t_res; } @@ -159,7 +156,8 @@ vuint16 unpack_n(const ustring::const_iterator t_begin, if (current_char >= max_value) { // if it is the mask return unpack_n(it + 1, t_end, t_res, t_n + 1); } - t_res.push_back(current_char &= masks[t_n]); + current_char &= masks[t_n]; + t_res.push_back(current_char); if (right_shift == 0) { ++it; } diff --git a/src/common.cc b/src/common.cc index 3f4460f..ea1c0ba 100644 --- a/src/common.cc +++ b/src/common.cc @@ -27,9 +27,10 @@ std::pair dico(dic_comp_t &t_dictionary, return std::make_pair(true, t_c); } auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)]; - return (e != 0) ? std::make_pair(true, e) - : std::make_pair(false, (e = static_cast( - t_dictionary.size() + 255))); + if(e != 0) + return std::make_pair(true, e); + e = static_cast(t_dictionary.size() + 255); + return std::make_pair(false, e); } ustring dico_uncompress(std::map &t_dict, diff --git a/src/compress.cc b/src/compress.cc index 815a88a..9928a55 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -36,7 +36,6 @@ vvuint16 lzw_compress(ustring &&t_text) { vvuint16 res{}; const auto DICT_MAX = static_cast(ipow(2, 14) - 256); /* 16 bits */ uint16_t w = 0xFFFF; - bool pushed = false; vuint16 chunk{}; dict_t dict{}; for (const auto c : t_text) { @@ -52,11 +51,9 @@ vvuint16 lzw_compress(ustring &&t_text) { if (const auto &[exists, pos] = dico(dict, w, static_cast(c)); exists) { w = pos; - pushed = false; } else { chunk.push_back(w); w = static_cast(c); - pushed = true; } } if (w != 0xFFFF) {