Added char * << to MString for string assignment.

This commit is contained in:
Brad Arant 2022-07-21 19:39:47 -07:00
parent 287933f9ce
commit d8df083d6b
5 changed files with 73 additions and 1 deletions

View File

@ -1,6 +1,5 @@
#include "MString.h" #include "MString.h"
#include "Log.h" #include "Log.h"
#include <iostream>
#include <stdlib.h> #include <stdlib.h>
#include <iomanip> #include <iomanip>
@ -56,6 +55,27 @@ namespace coreutils {
memcpy(data + len, value.getData(), value.getLength()); memcpy(data + len, value.getData(), value.getLength());
return *this; return *this;
} }
MString& MString::operator=(const char *value) {
int len = strlen(value);
setSize(len);
memcpy(data, value, len);
return *this;
}
MString& MString::operator=(char value) {
int len = 1;
setSize(1);
*data = value;
return *this;
}
MString& MString::operator<<(const char *value) {
int len = strlen(value);
setSize(len);
memcpy(data, value, len);
return *this;
}
int MString::write(char ch) { int MString::write(char ch) {
setSize(length + 1); setSize(length + 1);

View File

@ -2,6 +2,8 @@
#define __MString_h__ #define __MString_h__
#include "ZString.h" #include "ZString.h"
#include <iostream>
#include <sstream>
namespace coreutils { namespace coreutils {
@ -56,6 +58,24 @@ namespace coreutils {
MString& operator=(coreutils::ZString& data); MString& operator=(coreutils::ZString& data);
///
/// Assignment operator will copy data to backing store.
///
MString& operator=(const char *data);
///
/// Assignment operator will copy data to backing store.
///
MString& operator=(char data);
///
///
///
MString& operator<<(const char *value);
/// ///
/// ///
/// ///

2
testing/compile Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
g++ -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils

BIN
testing/mstring_test Executable file

Binary file not shown.

30
testing/mstring_test.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include "../MString.h"
int main(int argc, char **argv) {
// Literal string assignment tests.
coreutils::MString test1 = "this is an assignment test.";
std::cout << "char * operator=: [" << test1 << "]." << std::endl;
test1 = "new value.";
std::cout << "char * operator=: [" << test1 << "]." << std::endl;
coreutils::MString test2("this is another test.");
std::cout << "assign on constructor: [" << test2 << "]." << std::endl;
// Character assignment tests.
coreutils::MString test3;
test3 = 'a';
std::cout << "char operator=: [" << test3 << "]." << std::endl;
coreutils::MString test4;
test4 << "this is a test.";
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
// coreutils::ZString temp("this is literal zstring data.");
// coreutils::MString test = temp;
// std::cout << "operator=: [" << test << "]." << std::endl;
return 0;
}