File indexing completed on 2024-05-05 17:57:59
0001 /* 0002 This file is part of the Okteta Core library, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2004, 2011 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "octalbytecodec.hpp" 0010 0011 // Qt 0012 #include <QString> 0013 0014 namespace Okteta { 0015 static constexpr Byte octalDigitsFilledLimit = 32; 0016 0017 unsigned int OctalByteCodec::encodingWidth() const { return 3; } 0018 Byte OctalByteCodec::digitsFilledLimit() const { return octalDigitsFilledLimit; } 0019 0020 void OctalByteCodec::encode(QString* digits, unsigned int pos, Byte byte) const 0021 { 0022 const int minSizeNeeded = pos + 3; 0023 if (digits->size() < minSizeNeeded) { 0024 digits->resize(minSizeNeeded); 0025 } 0026 0027 (*digits)[pos++] = QLatin1Char('0' + (byte >> 6)); 0028 (*digits)[pos++] = QLatin1Char('0' + ((byte >> 3) & 0x07)); 0029 (*digits)[pos] = QLatin1Char('0' + ((byte) & 0x07)); 0030 } 0031 0032 void OctalByteCodec::encodeShort(QString* digits, unsigned int pos, Byte byte) const 0033 { 0034 const int encodingLength = (byte > 077) ? 3 : (byte > 07) ? 2 : 1; 0035 const int minSizeNeeded = pos + encodingLength; 0036 if (digits->size() < minSizeNeeded) { 0037 digits->resize(minSizeNeeded); 0038 } 0039 0040 const unsigned char firstDigitValue = byte >> 6; 0041 if (firstDigitValue > 0) { 0042 (*digits)[pos++] = QLatin1Char('0' + firstDigitValue); 0043 } 0044 const unsigned char secondDigitValue = (byte >> 3) & 0x07; 0045 if (secondDigitValue > 0 || firstDigitValue > 0) { 0046 (*digits)[pos++] = QLatin1Char('0' + secondDigitValue); 0047 } 0048 const unsigned char lastDigitValue = byte & 0x07; 0049 (*digits)[pos] = QLatin1Char('0' + lastDigitValue); 0050 } 0051 0052 bool OctalByteCodec::isValidDigit(unsigned char digit) const 0053 { 0054 return ('0' <= digit && digit <= '7'); 0055 } 0056 0057 bool OctalByteCodec::turnToValue(unsigned char* digit) const 0058 { 0059 if (isValidDigit(*digit)) { 0060 *digit -= '0'; 0061 return true; 0062 } 0063 return false; 0064 } 0065 0066 bool OctalByteCodec::appendDigit(Byte* byte, unsigned char digit) const 0067 { 0068 if (turnToValue(&digit)) { 0069 Byte _byte = *byte; 0070 if (_byte < octalDigitsFilledLimit) { 0071 _byte <<= 3; 0072 _byte += digit; 0073 *byte = _byte; 0074 return true; 0075 } 0076 } 0077 return false; 0078 } 0079 0080 void OctalByteCodec::removeLastDigit(Byte* byte) const 0081 { 0082 *byte >>= 3; 0083 } 0084 0085 }