From 9ebc1416ce73716d374ab73a0e11758f194997c9 Mon Sep 17 00:00:00 2001 From: Christian Fremgen Date: Mon, 22 Jul 2019 13:52:23 +0200 Subject: [PATCH] Corrected data types to eliminate C++ sign-warnings Replaced data types with std::size_t to eliminate the warnings: * clang: comparison of integers of different signs: 'int' and 'std::size_t' * Microsoft cl 19.16.27030.1 warnings C4267 and C4244 --- cpp/BitBuffer.cpp | 8 ++++---- cpp/BitBuffer.hpp | 2 +- cpp/QrCode.cpp | 9 +++++---- cpp/QrSegment.cpp | 8 ++++---- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cpp/BitBuffer.cpp b/cpp/BitBuffer.cpp index e22e9d3..2eda9e7 100644 --- a/cpp/BitBuffer.cpp +++ b/cpp/BitBuffer.cpp @@ -31,11 +31,11 @@ BitBuffer::BitBuffer() : std::vector() {} -void BitBuffer::appendBits(std::uint32_t val, int len) { - if (len < 0 || len > 31 || val >> len != 0) +void BitBuffer::appendBits(std::size_t val, std::size_t len) { + if (len > 31 || val >> len != 0) // len < 0 does not need to be checked, because std::size_t is guaranteed to be unsigned throw std::domain_error("Value out of range"); - for (int i = len - 1; i >= 0; i--) // Append bit by bit - this->push_back(((val >> i) & 1) != 0); + for (std::size_t i = len; i > 0; i--) // Append bit by bit + this->push_back(((val >> (i-1)) & 1) != 0); } } diff --git a/cpp/BitBuffer.hpp b/cpp/BitBuffer.hpp index f30913a..32f483b 100644 --- a/cpp/BitBuffer.hpp +++ b/cpp/BitBuffer.hpp @@ -45,7 +45,7 @@ class BitBuffer final : public std::vector { // Appends the given number of low-order bits of the given value // to this buffer. Requires 0 <= len <= 31 and val < 2^len. - public: void appendBits(std::uint32_t val, int len); + public: void appendBits(std::size_t val, std::size_t len); }; diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 4630752..f68390a 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -235,9 +235,9 @@ void QrCode::drawFunctionPatterns() { // Draw numerous alignment patterns const vector alignPatPos = getAlignmentPatternPositions(); - int numAlign = alignPatPos.size(); - for (int i = 0; i < numAlign; i++) { - for (int j = 0; j < numAlign; j++) { + std::size_t numAlign = alignPatPos.size(); + for (std::size_t i = 0; i < numAlign; i++) { + for (std::size_t j = 0; j < numAlign; j++) { // Don't draw on the three finder corners if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))) drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j)); @@ -346,7 +346,8 @@ vector QrCode::addEccAndInterleave(const vector &data) const { // Split data into blocks and append ECC to each block vector > blocks; const vector rsDiv = reedSolomonComputeDivisor(blockEccLen); - for (int i = 0, k = 0; i < numBlocks; i++) { + std::size_t k = 0; + for (int i = 0; i < numBlocks; i++) { vector dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1))); k += dat.size(); const vector ecc = reedSolomonComputeRemainder(dat, rsDiv); diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index 64d77f7..5b87069 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -1,9 +1,9 @@ -/* +/* * QR Code generator library (C++) - * + * * Copyright (c) Project Nayuki. (MIT License) * https://www.nayuki.io/page/qr-code-generator-library - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to @@ -94,7 +94,7 @@ QrSegment QrSegment::makeNumeric(const char *digits) { QrSegment QrSegment::makeAlphanumeric(const char *text) { BitBuffer bb; - int accumData = 0; + std::size_t accumData = 0; int accumCount = 0; int charCount = 0; for (; *text != '\0'; text++, charCount++) {