Examples › General Examples
general_memory_buffer
C++20 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_general/memory_buffer/testprog.cpp · Modern: examples_modern/general/memory_buffer/modern.cpp
Modern equivalent of the MemoryBuffer test program. A growable block of bytes is std::vector<unsigned char> (or std::vector<std::byte>): copy, assign, append, insert, erase, resize, fill and search come from the container and <algorithm>. std::search() finds a byte pattern anywhere, including bytes of zero. C++20 std::span gives a view of part of a buffer without a copy, and of any contiguous bytes (an array, a vector, a string) through one function signature.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| Find(), IFind(), FindLast() | find(), a std::search with a case-folding predicate, rfind() | C++98 | Watch out: rfind("") returns size(), where FindLast("") returns -1. |
| DeleteAt(), InsertAt(), ReplaceAt(), ReplaceString() | erase(), insert(), replace(); std::replace for characters | C++98 | A count past the end is clamped; a position past the end throws std::out_of_range. |
| MemoryBuffer | std::vector<unsigned char> or std::vector<std::byte>; std::span for views | C++98 / C++17 / C++20 | std::search finds a byte pattern; std::fill for MemSet(). |
| strcpy(), strncpy(), memmove() into char arrays | std::string; std::array<char, N> + string_view::copy() | C++11 / C++17 |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_general/memory_buffer/testprog.cpp 222 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #endif // __USE_ANSI_CPP__ #include "membuf.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif void PausePrg() { cout << "\n"; cout << "Press enter to continue..." << "\n"; cin.get(); } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif cout << "Testing MemoryBuffer constructors..." << "\n"; char *test_pattern = "The quick brown fox jumps over the lazy dog"; char *test_message = "This is a test..."; MemoryBuffer a((char *)test_pattern, strlen(test_pattern)+1); a[strlen(test_pattern)] = 0; // Null terminate the buffer cout << (char *)a << "\n"; MemoryBuffer b; MemoryBuffer c((char *)test_message, strlen(test_message)+1); c[strlen(test_message)] = 0; // Null terminate the buffer cout << (char *)c << "\n"; PausePrg(); cout << "Testing overloaded assignment operators..." << "\n"; b = a; cout << (char *)b << "\n"; b = c; cout << (char *)b << "\n"; PausePrg(); cout << "Testing copy constructor..." << "\n"; MemoryBuffer aa(a); cout << (char *)aa << "\n"; MemoryBuffer bb(b); cout << (char *)bb << "\n"; PausePrg(); cout << "Testing overloaded += operator..." << "\n"; char *num_test = " 0123456789"; MemoryBuffer sbuf((char *)test_pattern, strlen(test_pattern)); MemoryBuffer buf((char *)num_test, strlen(num_test)); sbuf += buf; sbuf += '\0'; // Null terminate the buffer cout << (char *)sbuf << "\n"; PausePrg(); cout << "Testing concatenation..." << "\n"; char *s1 = "DOG "; char *s2 = "CAT "; char *s3 = "MOUSE"; MemoryBuffer m1; m1.Load(s1, strlen(s1)); m1.Cat(s2, strlen(s2)); m1.Cat(s3, strlen(s3)); m1.Cat('\0'); // Null terminate the buffer cout << (char *)m1 << "\n"; PausePrg(); cout << "Testing find functions..." << "\n"; cout << "String = " << (char *)m1 << "\n"; if(m1.Find(s2, strlen(s2), 0) == __MEMBUF_NO_MATCH__) cout << "Pattern not found!" << "\n"; else cout << "Found pattern: " << s1 << "\n"; if(m1.Find(s3, strlen(s3), 0) == __MEMBUF_NO_MATCH__) cout << "Pattern not found!" << "\n"; else cout << "Found pattern: " << s3 << "\n"; cout << "\n"; cout << "Testing repeated pattern finding..." << "\n"; MemoryBuffer m2(c); cout << "String = " << (char *)m2 << "\n"; char *s4 = "is"; unsigned offset = 0; while(1) { offset = m2.Find((char *)s4, strlen(s4), offset); if (offset == __MEMBUF_NO_MATCH__) break; cout << "Found pattern \"" << s4 << "\" at index: " << offset << "\n"; offset++; } PausePrg(); cout << "Testing delete function..." << "\n"; MemoryBuffer x1(a); cout << (char *)x1 << "\n"; char *xx = "fox"; int Index = x1.Find((char *)xx); cout << "Deleting fox from string..." << "\n"; x1.DeleteAt(Index, strlen(xx)); cout << (char *)x1 << "\n"; PausePrg(); cout << "Testing replace functions..." << "\n"; MemoryBuffer x2(a); cout << (char *)x2 << "\n"; char *xy = "cat"; Index = x2.Find(xx); cout << "Replacing fox:" << "\n"; x2.ReplaceAt(Index, xy, strlen(xy)); cout << (char *)x2 << "\n"; cout << "Replacing jumps:" << "\n"; Index = x2.Find((void *)"jumps", strlen("jumps")); x2.ReplaceAt(Index, "runs ", strlen("runs ")); cout << (char *)x2 << "\n"; MemoryBuffer x3((char *)"cow", 3); cout << "Replacing dog:" << "\n"; Index = x2.Find((void *)"dog"); x2.ReplaceAt(Index, x3); cout << (char *)x2 << "\n"; PausePrg(); cout << "Testing the insert functions..." << "\n"; MemoryBuffer x4(a); cout << (char *)x4 << "\n"; char *xz = "and yellow "; cout << "Inserting text into string:" << "\n"; Index = x4.Find(xx); x4.InsertAt(Index, xz, strlen(xz)); cout << (char *)x4 << "\n"; Index = x4.Find((void *)"over"); x4.InsertAt(Index, (char *)"around and ", strlen("around and ")); cout << (char *)x4 << "\n"; MemoryBuffer x5((char *)"cow and ", strlen("cow and ")); Index = x4.Find((void *)"dog"); x4.InsertAt(Index, x5); cout << (char *)x4 << "\n"; PausePrg(); cout << "Testing dynamic allocation functions..." << "\n"; MemoryBuffer z(c); cout << "String = " << (char *)z << "\n"; cout << "Buffer's logical length = " << z.length() << "\n"; cout << "Buffer's allocated length = " << z.dlength() << "\n"; cout << "Changing allocated length..." << "\n"; z.Realloc(z.length()+16); cout << "Buffer's allocated length = " << z.dlength() << "\n"; cout << "Destorying the buffer" << "\n"; z.Destroy(); cout << "Buffer's logical length = " << z.length() << "\n"; cout << "Buffer's allocated length = " << z.dlength() << "\n"; PausePrg(); cout << "Testing resizing function" << "\n"; MemoryBuffer rs(a); cout << "String = " << (char *)rs << "\n"; cout << "Buffer's logical length = " << rs.length() << "\n"; cout << "Buffer's allocated length = " << rs.dlength() << "\n"; cout << "Resizing the buffer to 24 bytes" << "\n"; rs.resize(24); cout << "Buffer's logical length = " << rs.length() << "\n"; cout << "Buffer's allocated length = " << rs.dlength() << "\n"; cout << "Freeing unused allocated space..." << "\n"; rs.FreeBytes(); cout << "Buffer's logical length = " << rs.length() << "\n"; cout << "Buffer's allocated length = " << rs.dlength() << "\n"; PausePrg(); cout << "Testing overload subscript operator..." << "\n"; char *Message = "This is a test message"; MemoryBuffer ss((char *)Message, strlen(Message)); unsigned i; for(i = 0; i < strlen(Message); i++) cout << ss[i]; cout << "\n"; PausePrg(); cout << "Testing memset function..." << "\n"; unsigned bytes = 25; MemoryBuffer fill_buf(bytes); fill_buf.MemSet('*'); // Fill the entire memory buffer for(i = 0; i < bytes; i++) cout << fill_buf[i]; cout << "\n"; // Fill the buffer with 5 bytes starting at zero based offset 10 fill_buf.MemSet('?', 10, 5); for(i = 0; i < bytes; i++) cout << fill_buf[i]; cout << "\n"; // Set the first and last bytes fill_buf.MemSet('!', 0, 1); fill_buf.MemSet('!', (bytes-1), 1); for(i = 0; i < bytes; i++) cout << fill_buf[i]; cout << "\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++20)
examples_modern/general/memory_buffer/modern.cpp 105 lines
#include <algorithm> #include <cstddef> #include <iostream> #include <span> #include <string_view> #include <vector> using Buffer = std::vector<unsigned char>; Buffer make(std::string_view s) { return Buffer(s.begin(), s.end()); } void print(const char *label, std::span<const unsigned char> bytes) { std::cout << label; std::cout.write(reinterpret_cast<const char *>(bytes.data()), static_cast<std::streamsize>(bytes.size())); std::cout << "\n"; } // MemoryBuffer::Find(): the offset of a pattern, or npos std::size_t find(std::span<const unsigned char> buf, std::string_view pat, std::size_t from = 0) { if(from > buf.size()) return std::string_view::npos; auto it = std::search(buf.begin() + from, buf.end(), pat.begin(), pat.end()); return it == buf.end() ? std::string_view::npos : std::size_t(it - buf.begin()); } int main() { Buffer a = make("The quick brown fox jumps over the lazy dog"); Buffer c = make("This is a test..."); Buffer b; print("a: ", a); b = a; print("b = a: ", b); // Copy assignment b = c; print("b = c: ", b); Buffer aa(a); // Copy constructor print("aa(a): ", aa); Buffer sbuf = a; // operator+= Buffer nums = make(" 0123456789"); sbuf.insert(sbuf.end(), nums.begin(), nums.end()); print("a += nums: ", sbuf); Buffer m1 = make("DOG "); // Load() and Cat() for(auto s : {"CAT ", "MOUSE"}) { std::string_view v(s); m1.insert(m1.end(), v.begin(), v.end()); } print("m1: ", m1); std::cout << "find(CAT) = " << find(m1, "CAT ") << "\n"; std::cout << "Every \"is\" in c:"; for(auto pos = find(c, "is"); pos != std::string_view::npos; pos = find(c, "is", pos + 1)) std::cout << " " << pos; std::cout << "\n"; Buffer x1 = a; // DeleteAt() auto at = find(x1, "fox"); x1.erase(x1.begin() + at, x1.begin() + at + 3); print("delete fox: ", x1); Buffer x2 = a; // ReplaceAt() (same length) at = find(x2, "fox"); std::string_view cat = "cat"; std::copy(cat.begin(), cat.end(), x2.begin() + at); print("replace fox: ", x2); Buffer x4 = a; // InsertAt() std::string_view yellow = "and yellow "; at = find(x4, "fox"); x4.insert(x4.begin() + at, yellow.begin(), yellow.end()); print("insert: ", x4); Buffer rs = a; // resize(), shrink_to_fit() rs.resize(24); rs.shrink_to_fit(); // FreeBytes() print("resize(24): ", rs); std::span<const unsigned char> view(a); // A view, no copy (C++20) print("span subspan(4, 5): ", view.subspan(4, 5)); Buffer fill_buf(25); // MemSet() std::fill(fill_buf.begin(), fill_buf.end(), '*'); print("fill: ", fill_buf); std::fill_n(fill_buf.begin() + 10, 5, '?'); print("fill_n: ", fill_buf); fill_buf.front() = fill_buf.back() = '!'; print("ends: ", fill_buf); std::vector<std::byte> raw(4, std::byte{0x80}); // Bytes, not characters std::cout << "std::byte buffer of " << raw.size() << " bytes, first = 0x" << std::hex << std::to_integer<int>(raw[0]) << std::dec << "\n"; c.clear(); c.shrink_to_fit(); // Destroy() std::cout << "after clear(): size " << c.size() << "\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=20 cmake --build build-modern --target general_memory_buffer ./build-modern/general_memory_buffer # or without CMake: g++ -std=c++20 -pthread -o general_memory_buffer examples_modern/general/memory_buffer/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
a: The quick brown fox jumps over the lazy dog b = a: The quick brown fox jumps over the lazy dog b = c: This is a test... aa(a): The quick brown fox jumps over the lazy dog a += nums: The quick brown fox jumps over the lazy dog 0123456789 m1: DOG CAT MOUSE find(CAT) = 4 Every "is" in c: 2 5 delete fox: The quick brown jumps over the lazy dog replace fox: The quick brown cat jumps over the lazy dog insert: The quick brown and yellow fox jumps over the lazy dog resize(24): The quick brown fox jump span subspan(4, 5): quick fill: ************************* fill_n: **********?????********** ends: !*********?????*********! std::byte buffer of 4 bytes, first = 0x80 after clear(): size 0