diff --git a/modules/juce_core/memory/juce_MemoryBlock.cpp b/modules/juce_core/memory/juce_MemoryBlock.cpp index 8bf8b76672..d533a6a1b0 100644 --- a/modules/juce_core/memory/juce_MemoryBlock.cpp +++ b/modules/juce_core/memory/juce_MemoryBlock.cpp @@ -23,11 +23,9 @@ namespace juce { -MemoryBlock::MemoryBlock() noexcept : size (0) -{ -} +MemoryBlock::MemoryBlock() noexcept {} -MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero) +MemoryBlock::MemoryBlock (size_t initialSize, bool initialiseToZero) { if (initialSize > 0) { @@ -147,7 +145,7 @@ void MemoryBlock::reset() size = 0; } -void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero) +void MemoryBlock::ensureSize (size_t minimumSize, bool initialiseToZero) { if (size < minimumSize) setSize (minimumSize, initialiseToZero); @@ -160,23 +158,23 @@ void MemoryBlock::swapWith (MemoryBlock& other) noexcept } //============================================================================== -void MemoryBlock::fillWith (const uint8 value) noexcept +void MemoryBlock::fillWith (uint8 value) noexcept { memset (data, (int) value, size); } -void MemoryBlock::append (const void* const srcData, const size_t numBytes) +void MemoryBlock::append (const void* srcData, size_t numBytes) { if (numBytes > 0) { jassert (srcData != nullptr); // this must not be null! - const size_t oldSize = size; + auto oldSize = size; setSize (size + numBytes); memcpy (data + oldSize, srcData, numBytes); } } -void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes) +void MemoryBlock::replaceWith (const void* srcData, size_t numBytes) { if (numBytes > 0) { @@ -186,13 +184,13 @@ void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes) } } -void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition) +void MemoryBlock::insert (const void* srcData, size_t numBytes, size_t insertPosition) { if (numBytes > 0) { jassert (srcData != nullptr); // this must not be null! insertPosition = jmin (size, insertPosition); - const size_t trailingDataSize = size - insertPosition; + auto trailingDataSize = size - insertPosition; setSize (size + numBytes, false); if (trailingDataSize > 0) @@ -204,7 +202,7 @@ void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size } } -void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesToRemove) +void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) { if (startByte + numBytesToRemove >= size) { @@ -222,7 +220,7 @@ void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesTo void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept { - const char* d = static_cast (src); + auto* d = static_cast (src); if (offset < 0) { @@ -240,7 +238,7 @@ void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexc void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept { - char* d = static_cast (dst); + auto* d = static_cast (dst); if (offset < 0) { @@ -252,7 +250,7 @@ void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcep if ((size_t) offset + num > size) { - const size_t newNum = (size_t) size - (size_t) offset; + auto newNum = (size_t) size - (size_t) offset; zeromem (d + newNum, num - newNum); num = newNum; } @@ -267,17 +265,17 @@ String MemoryBlock::toString() const } //============================================================================== -int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const noexcept +int MemoryBlock::getBitRange (size_t bitRangeStart, size_t numBits) const noexcept { int res = 0; - size_t byte = bitRangeStart >> 3; - size_t offsetInByte = bitRangeStart & 7; + auto byte = bitRangeStart >> 3; + auto offsetInByte = bitRangeStart & 7; size_t bitsSoFar = 0; while (numBits > 0 && (size_t) byte < size) { - const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte); + auto bitsThisTime = jmin (numBits, 8 - offsetInByte); const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte; res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar); @@ -293,13 +291,13 @@ int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept { - size_t byte = bitRangeStart >> 3; - size_t offsetInByte = bitRangeStart & 7; + auto byte = bitRangeStart >> 3; + auto offsetInByte = bitRangeStart & 7; uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits)); while (numBits > 0 && (size_t) byte < size) { - const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte); + auto bitsThisTime = jmin (numBits, 8 - offsetInByte); const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte); const uint32 tempBits = (uint32) bitsToSet << offsetInByte; @@ -319,7 +317,7 @@ void MemoryBlock::loadFromHexString (StringRef hex) { ensureSize ((size_t) hex.length() >> 1); char* dest = data; - String::CharPointerType t (hex.text); + auto t = hex.text; for (;;) { @@ -331,7 +329,7 @@ void MemoryBlock::loadFromHexString (StringRef hex) for (;;) { - const juce_wchar c = t.getAndAdvance(); + auto c = t.getAndAdvance(); if (c >= '0' && c <= '9') { byte |= c - '0'; break; } if (c >= 'a' && c <= 'z') { byte |= c - ('a' - 10); break; } @@ -354,18 +352,18 @@ static const char base64EncodingTable[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij String MemoryBlock::toBase64Encoding() const { - const size_t numChars = ((size << 3) + 5) / 6; + auto numChars = ((size << 3) + 5) / 6; String destString ((unsigned int) size); // store the length, followed by a '.', and then the data. - const int initialLen = destString.length(); + auto initialLen = destString.length(); destString.preallocateBytes (sizeof (String::CharPointerType::CharType) * (size_t) initialLen + 2 + numChars); - String::CharPointerType d (destString.getCharPointer()); + auto d = destString.getCharPointer(); d += initialLen; d.write ('.'); for (size_t i = 0; i < numChars; ++i) - d.write ((juce_wchar) (uint8) base64EncodingTable [getBitRange (i * 6, 6)]); + d.write ((juce_wchar) (uint8) base64EncodingTable[getBitRange (i * 6, 6)]); d.writeNull(); return destString; @@ -380,29 +378,30 @@ static const char base64DecodingTable[] = bool MemoryBlock::fromBase64Encoding (StringRef s) { - String::CharPointerType dot (CharacterFunctions::find (s.text, (juce_wchar) '.')); + auto dot = CharacterFunctions::find (s.text, (juce_wchar) '.'); if (dot.isEmpty()) return false; - const int numBytesNeeded = String (s.text, dot).getIntValue(); + auto numBytesNeeded = String (s.text, dot).getIntValue(); setSize ((size_t) numBytesNeeded, true); - String::CharPointerType srcChars (dot + 1); + auto srcChars = dot + 1; int pos = 0; for (;;) { - int c = (int) srcChars.getAndAdvance(); + auto c = (int) srcChars.getAndAdvance(); if (c == 0) return true; c -= 43; + if (isPositiveAndBelow (c, numElementsInArray (base64DecodingTable))) { - setBitRange ((size_t) pos, 6, base64DecodingTable [c]); + setBitRange ((size_t) pos, 6, base64DecodingTable[c]); pos += 6; } } diff --git a/modules/juce_core/memory/juce_MemoryBlock.h b/modules/juce_core/memory/juce_MemoryBlock.h index 888449a4be..d842244305 100644 --- a/modules/juce_core/memory/juce_MemoryBlock.h +++ b/modules/juce_core/memory/juce_MemoryBlock.h @@ -252,7 +252,7 @@ private: //============================================================================== typedef HeapBlock HeapBlockType; HeapBlockType data; - size_t size; + size_t size = 0; JUCE_LEAK_DETECTOR (MemoryBlock) }; diff --git a/modules/juce_core/streams/juce_InputStream.cpp b/modules/juce_core/streams/juce_InputStream.cpp index a01a12ff72..4e831770f4 100644 --- a/modules/juce_core/streams/juce_InputStream.cpp +++ b/modules/juce_core/streams/juce_InputStream.cpp @@ -25,7 +25,7 @@ namespace juce int64 InputStream::getNumBytesRemaining() { - int64 len = getTotalLength(); + auto len = getTotalLength(); if (len >= 0) len -= getPosition(); @@ -87,23 +87,26 @@ int InputStream::readIntBigEndian() int InputStream::readCompressedInt() { - const uint8 sizeByte = (uint8) readByte(); + auto sizeByte = (uint8) readByte(); + if (sizeByte == 0) return 0; const int numBytes = (sizeByte & 0x7f); + if (numBytes > 4) { - jassertfalse; // trying to read corrupt data - this method must only be used + jassertfalse; // trying to read corrupt data - this method must only be used // to read data that was written by OutputStream::writeCompressedInt() return 0; } - char bytes[4] = { 0, 0, 0, 0 }; + char bytes[4] = {}; + if (read (bytes, numBytes) != numBytes) return 0; - const int num = (int) ByteOrder::littleEndianInt (bytes); + auto num = (int) ByteOrder::littleEndianInt (bytes); return (sizeByte >> 7) ? -num : num; } @@ -158,51 +161,42 @@ double InputStream::readDoubleBigEndian() String InputStream::readString() { - MemoryBlock buffer (256); - char* data = static_cast (buffer.getData()); - size_t i = 0; + MemoryOutputStream buffer; - while ((data[i] = readByte()) != 0) + for (;;) { - if (++i >= buffer.getSize()) - { - buffer.setSize (buffer.getSize() + 512); - data = static_cast (buffer.getData()); - } - } + auto c = readByte(); + buffer.writeByte (c); - return String::fromUTF8 (data, (int) i); + if (c == 0) + return buffer.toUTF8(); + } } String InputStream::readNextLine() { - MemoryBlock buffer (256); - auto* data = static_cast (buffer.getData()); - size_t i = 0; + MemoryOutputStream buffer; - while ((data[i] = readByte()) != 0) + for (;;) { - if (data[i] == '\n') + auto c = readByte(); + buffer.writeByte (c); + + if (c == 0 || c == '\n') break; - if (data[i] == '\r') + if (c == '\r') { - const int64 lastPos = getPosition(); + auto lastPos = getPosition(); if (readByte() != '\n') setPosition (lastPos); break; } - - if (++i >= buffer.getSize()) - { - buffer.setSize (buffer.getSize() + 512); - data = static_cast (buffer.getData()); - } } - return String::fromUTF8 (data, (int) i); + return buffer.toUTF8(); } size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes) diff --git a/modules/juce_core/streams/juce_OutputStream.cpp b/modules/juce_core/streams/juce_OutputStream.cpp index 499f532cf7..6a24702179 100644 --- a/modules/juce_core/streams/juce_OutputStream.cpp +++ b/modules/juce_core/streams/juce_OutputStream.cpp @@ -63,7 +63,7 @@ OutputStream::~OutputStream() } //============================================================================== -bool OutputStream::writeBool (const bool b) +bool OutputStream::writeBool (bool b) { return writeByte (b ? (char) 1 : (char) 0); @@ -85,32 +85,32 @@ bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) bool OutputStream::writeShort (short value) { - const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value); + auto v = ByteOrder::swapIfBigEndian ((uint16) value); return write (&v, 2); } bool OutputStream::writeShortBigEndian (short value) { - const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value); + auto v = ByteOrder::swapIfLittleEndian ((uint16) value); return write (&v, 2); } bool OutputStream::writeInt (int value) { - const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value); + auto v = ByteOrder::swapIfBigEndian ((uint32) value); return write (&v, 4); } bool OutputStream::writeIntBigEndian (int value) { - const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value); + auto v = ByteOrder::swapIfLittleEndian ((uint32) value); return write (&v, 4); } bool OutputStream::writeCompressedInt (int value) { - unsigned int un = (value < 0) ? (unsigned int) -value - : (unsigned int) value; + auto un = (value < 0) ? (unsigned int) -value + : (unsigned int) value; uint8 data[5]; int num = 0; @@ -131,13 +131,13 @@ bool OutputStream::writeCompressedInt (int value) bool OutputStream::writeInt64 (int64 value) { - const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value); + auto v = ByteOrder::swapIfBigEndian ((uint64) value); return write (&v, 8); } bool OutputStream::writeInt64BigEndian (int64 value) { - const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value); + auto v = ByteOrder::swapIfLittleEndian ((uint64) value); return write (&v, 8); } @@ -183,20 +183,19 @@ bool OutputStream::writeString (const String& text) #endif } -bool OutputStream::writeText (const String& text, const bool asUTF16, - const bool writeUTF16ByteOrderMark) +bool OutputStream::writeText (const String& text, bool asUTF16, bool writeUTF16ByteOrderMark) { if (asUTF16) { if (writeUTF16ByteOrderMark) write ("\x0ff\x0fe", 2); - String::CharPointerType src (text.getCharPointer()); + auto src = text.getCharPointer(); bool lastCharWasReturn = false; for (;;) { - const juce_wchar c = src.getAndAdvance(); + auto c = src.getAndAdvance(); if (c == 0) break; @@ -213,7 +212,7 @@ bool OutputStream::writeText (const String& text, const bool asUTF16, else { const char* src = text.toUTF8(); - const char* t = src; + auto* t = src; for (;;) { @@ -258,8 +257,8 @@ int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToW while (numBytesToWrite > 0) { - char buffer [8192]; - const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer))); + char buffer[8192]; + auto num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer))); if (num <= 0) break; @@ -283,7 +282,7 @@ void OutputStream::setNewLineString (const String& newLineString_) template static void writeIntToStream (OutputStream& stream, IntegerType number) { - char buffer [NumberToStringConverters::charsNeededForInt]; + char buffer[NumberToStringConverters::charsNeededForInt]; char* end = buffer + numElementsInArray (buffer); const char* start = NumberToStringConverters::numberToString (end, number); stream.write (start, (size_t) (end - start - 1));