Added char * << to MString for string assignment.
This commit is contained in:
parent
287933f9ce
commit
d8df083d6b
22
MString.cpp
22
MString.cpp
@ -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>
|
||||||
|
|
||||||
@ -57,6 +56,27 @@ namespace coreutils {
|
|||||||
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);
|
||||||
*(data + length - 1) = ch;
|
*(data + length - 1) = ch;
|
||||||
|
20
MString.h
20
MString.h
@ -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
2
testing/compile
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
g++ -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
|
BIN
testing/mstring_test
Executable file
BIN
testing/mstring_test
Executable file
Binary file not shown.
30
testing/mstring_test.cpp
Normal file
30
testing/mstring_test.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user