Changes to MString to add insert and remove methods.

This commit is contained in:
Brad Arant 2023-01-05 11:51:20 -08:00
parent 88d51cb598
commit 8f8c14d7b2
6 changed files with 103 additions and 1 deletions

31
JString.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef __JString_h__
#define __JString_h__
#include "MString.h"
#include <iostream>
#include <sstream>
namespace coreutils {
///
/// Use the JString object when you need a JSON interface to C++. JString uses
/// an MString to store a JSON string representing the object(s) contained.
/// The [] operator is overriden from the ZString and provides a method to
/// access the objects elements using a named path as the index to the items.
///
class JString : public MString {
public:
MString & operator[](char *key);
MString & operator[](ZString &key);
MString & operator[](std::string key);
};
}
#endif

View File

@ -2,6 +2,7 @@
#include "Log.h"
#include <stdlib.h>
#include <iomanip>
#include <cstring>
namespace coreutils {
@ -139,6 +140,44 @@ namespace coreutils {
return *this;
}
void MString::insert(char ch, int offset) {
setSize(length + 1);
for(int ix = length; ix >= offset; ix--)
getData()[ix + 1] = getData()[ix];
data[offset] = ch;
}
void MString::insert(ZString &value, int offset) {
setSize(length + value.getLength());
for(int ix = length; ix >= offset; ix--)
getData()[ix + value.getLength()] = getData()[ix];
std::memcpy(data + offset, value.getData(), value.getLength());
}
void MString::insert(std::string value, int offset) {
setSize(length + value.size());
for(int ix = length; ix >= offset; ix--)
getData()[ix + value.size()] = getData()[ix];
std::memcpy(data + offset, value.c_str(), value.size());
}
void MString::replace(char ch, int offset) {
}
void MString::replace(ZString &value, int offset) {
}
void MString::replace(std::string value, int offset) {
}
void MString::remove(int offset, int length) {
// (data + this->length) > (data + offset + length) ? length: length; // TODO: Need to calculate correct length.
for(int ix = offset; ix < this->length; ix++)
getData()[ix] = getData()[ix + length];
setSize(this->length - length);
}
MString& MString::write(char ch) {
setSize(length + 1);
*(data + length - 1) = ch;

View File

@ -31,7 +31,7 @@ namespace coreutils {
MString(const char *data);
///
/// Consructor providing the initial setup of the ZString object.
/// Consructor providing the initial setup of the MString object.
///
MString(char *data, size_t length);
@ -104,6 +104,28 @@ namespace coreutils {
///
///
void insert(char ch, int offset);
void insert(ZString &value, int offset);
void insert(std::string value, int offset);
///
///
///
void replace(char ch, int offset);
void replace(ZString &value, int offset);
void replace(std::string value, int offset);
///
///
///
void remove(int offset, int length);
///
///
///
MString& write(char ch);
///

Binary file not shown.

View File

@ -30,6 +30,16 @@ int main(int argc, char **argv) {
test6 << "Will this work? " << number << test4 << " See how this works.";
std::cout << "streaming: [" << test6 << "]." << std::endl;
coreutils::MString test9("XXXXXXXXXYYYYYYYYYY");
std::cout << "test insert: " << test9 << std::endl;
coreutils::MString test8("zzz");
test9.insert(test8, 9);
std::cout << "inserting: " << test9 << std::endl;
test9.insert("aaaaa", 9);
std::cout << "inserting: " << test9 << std::endl;
test9.remove(10, 7);
std::cout << "removing: " << test9 << std::endl;
return 0;
}

Binary file not shown.