Compare commits

...

10 Commits

Author SHA1 Message Date
Brad Arant
c40e288a67 added trim operand function. 2025-01-16 13:38:35 -08:00
barant
5fd02a7a44 Fixed data type on comparisons. 2025-01-09 09:04:17 -08:00
barant
cfd996fe5a cleanup for valgrind. 2024-12-23 13:00:22 -08:00
barant
0ebe8eaf5b cleanup for valgrind. 2024-12-23 11:15:53 -08:00
barant
e5bfd7a840 fixed bug with substring on ZString. 2024-12-23 09:51:10 -08:00
barant
921854e455 fixed bug with asInteger on ZString. 2024-12-23 09:37:14 -08:00
barant
f8557aa5f5 migrating conversion modifiers from JetCore::Global to MString. 2024-12-19 10:32:23 -08:00
barant
10df4a6a99 added changed event handler. 2024-12-18 11:26:58 -08:00
barant
7eb2417456 added a pretty output to JString. 2024-12-18 09:49:34 -08:00
barant
d74fa9689d added a pretty output to JString. Initial. 2024-12-17 16:56:05 -08:00
14 changed files with 328 additions and 33 deletions

View File

@ -34,7 +34,7 @@ namespace coreutils {
File::~File() {
close(fd);
setBufferSize(0);
free(buffer);
}
void File::setBufferSize(size_t size) {

2
File.h
View File

@ -17,7 +17,7 @@ namespace coreutils {
public:
File(ZString fileName, int mode = O_RDONLY, int authority = 0664);
~File();
virtual ~File();
void setBufferSize(size_t size);
int read();
int readLine();

View File

@ -19,12 +19,12 @@ namespace coreutils {
JSONFile(ZString path): JString(), File(path, O_RDWR, 0644) {
}
virtual ~JSONFile() {
write(*this);
}
void changed(ZString key, ZString value) overide;
};

View File

@ -169,5 +169,54 @@ namespace coreutils {
}
reset();
}
MString JString::pretty() {
MString output;
reset();
char ch;
int index = 0;
while(!eod()) {
ch = nextChar();
if(ch == '{') {
index += 3;
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else if(ch == '[') {
index += 3;
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else if(ch == ':') {
output << ": ";
}
else if(ch == '}') {
index -= 3;
output << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
output << ch;
}
else if(ch == ']') {
index -= 3;
output << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
output << ch;
}
else if(ch == ',') {
output << ch << '\n';
for(int ix = 0; ix < index; ++ix)
output << ' ';
}
else
output << ch;
}
return output;
}
void JString::changed(ZString key, ZString value) {}
}

View File

@ -67,8 +67,9 @@ namespace coreutils {
return jstring.find(key).str();
}
Proxy& operator=(coreutils::ZString value) {
Proxy& operator=(coreutils::MString value) {
jstring.set(key, value);
jstring.changed(key, value);
return *this;
}
@ -82,6 +83,10 @@ namespace coreutils {
}
JString & operator=(coreutils:: ZString value);
MString pretty();
virtual void changed(ZString key, ZString value);
private:
void removeWhitespace();

View File

@ -1,6 +1,7 @@
#include "MString.h"
#include "Log.h"
#include "uuid/uuid.h"
#include "Exception.h"
#include <cstring>
#include <string>
#include <format>
@ -186,13 +187,11 @@ namespace coreutils {
return *this;
}
// MString &MString::operator<<(std::string value) {
// int temp = length;
// int len = length + value.length();
// setSize(len);
// memcpy(data + temp, value.c_str(), value.length());
// return *this;
// }
MString &MString::operator<<(char value) {
setSize(length + 1);
data[length - 1] = value;
return *this;
}
// MString &MString::operator<<(std::ostream (*f)(std::ostream&)) {
MString &MString::operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&)) {
@ -281,5 +280,130 @@ namespace coreutils {
int MString::offset() {
return cursor - data;
}
MString MString::toBinary() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
if(strchr("\\'\".\0\r\n", temp))
target.write('\\');
target.write(temp);
}
pop();
return target;
}
MString MString::fromBinary() {
push();
reset();
MString target;
while(!eod()) {
if(ifNext("\\r"))
target.write(13);
else if(ifNext("\\n"))
target.write(10);
else if(ifNext("\\0"))
target.write(0);
else if(ifNext("\\\\"))
target.write("\\");
else if(ifNext("\\."))
target.write(".");
else if(ifNext("\\\""))
target.write("\"");
else if(ifNext("\\\'"))
target.write("'");
else
target.write(nextChar());
}
pop();
return target;
}
MString MString::toHex() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
char temp2 = temp;
temp >>= 4;
target.write(hexChar(temp));
target.write(hexChar(temp2));
}
pop();
return target;
}
MString MString::fromHex() {
push();
reset();
MString target;
while(!eod()) {
char ch1 = nextChar();
ch1 -= 48;
if(ch1 > 9)
ch1 -= 7;
ch1 <<= 4;
ch1 &= 240;
if(eod())
coreutils::Exception("conversion from hex requires even number of characters.");
char ch2 = nextChar();
ch2 -= 48;
if(ch2 > 9)
ch2 -= 7;
ch2 &= 15;
ch1 |= ch2;
target.write(ch1);
}
pop();
return target;
}
MString MString::toBase64() {
MString target;
return target;
}
MString MString::fromBase64() {
MString target;
return target;
}
MString MString::toUpper() {
MString target;
return target;
}
MString MString::toLower() {
MString target;
return target;
}
MString MString::toCGI() {
MString target;
return target;
}
MString MString::fromCGI() {
MString target;
return target;
}
char MString::hexChar(char c) {
c &= 15;
c += 48;
if(c > 57)
c += 7;
return c;
}
}

View File

@ -157,7 +157,7 @@ namespace coreutils {
///
///
// MString &operator<<(std::string value);
MString &operator<<(char value);
///
///
@ -231,14 +231,81 @@ namespace coreutils {
///
MString &read(int fd);
///
///
///
int offset();
///
///
///
MString toBinary();
///
///
///
MString fromBinary();
///
///
///
MString toHex();
///
///
///
MString fromHex();
///
///
///
MString toBase64();
///
///
///
MString fromBase64();
///
///
///
MString toUpper();
///
///
///
MString toLower();
///
///
///
MString toCGI();
///
///
///
MString fromCGI();
protected:
void setSize(int size);
private:
int bufferSize = 0;
char hexChar(char c);
};
}

View File

@ -66,8 +66,8 @@ namespace coreutils {
}
ZString::~ZString() {
if(cdata != NULL)
free(cdata);
if(cdata != NULL)
free(cdata);
}
bool ZString::operator<(const ZString &valuex) const {
@ -110,9 +110,9 @@ namespace coreutils {
return (strncmp(data, valuex.data, length) == 0);
}
bool ZString::operator!=(const ZString &valuex) const {
// if(length != valuex.getLength())
// return false;
bool ZString::operator!=(const ZString valuex) const {
if(length != valuex.getLength())
return true;
return (strncmp(data, valuex.data, length) != 0);
}
@ -137,7 +137,7 @@ namespace coreutils {
++cursor;
if(cursor == end)
return 0;
while((*cursor >- '0') && (*cursor <= '9')) {
while((*cursor >= '0') && (*cursor <= '9')) {
value *= 10;
value += *cursor - 48;
++cursor;
@ -232,7 +232,10 @@ namespace coreutils {
}
char* ZString::c_str() {
cdata = (char *)malloc(length + 1);
if(cdata == NULL)
cdata = (char *)malloc(length + 1);
else
cdata = (char *)realloc(cdata, length + 1);
strncpy(cdata, data, length);
cdata[length] = '\0';
return cdata;
@ -252,9 +255,8 @@ namespace coreutils {
char *startChar = data + start;
char *endChar = startChar + len;
char newlen = endChar > end ? endChar - end: len;
if(startChar < end) {
return ZString(startChar, newlen);
}
if(startChar < end)
return ZString(startChar, newlen);
return ZString();
}
@ -360,6 +362,12 @@ namespace coreutils {
return cursor >= data + length;
}
int ZString::goeod() {
char *temp = cursor;
cursor = data + length;
return cursor - temp;
}
bool ZString::startsWith(const char *value) {
return strncmp(cursor, value, strlen(value)) == 0;
}
@ -444,11 +452,28 @@ namespace coreutils {
}
int ZString::skipWhitespace() {
int len = strspn(cursor, " \n\t");
cursor += len;
char *end = data + length;
int len = 0;
while((cursor < end) && ((*cursor == ' ') || (*cursor == '\n') || (*cursor == '\t'))) {
++cursor;
++len;
}
return len;
}
int ZString::trimTrailingWhitespace() {
goeod();
int len = 0;
--cursor;
while((cursor > data) && ((*cursor == ' ') || (*cursor == '\n') || (*cursor == '\t'))) {
--cursor;
--length;
++len;
}
++cursor;
return len;
}
bool ZString::lineIsWhitespace() {
char *end = data + length;
char *temp = cursor;
@ -490,6 +515,8 @@ namespace coreutils {
}
ZString ZString::trim() {
trimTrailingWhitespace();
reset();
skipWhitespace();
return unparsed();
}

View File

@ -84,7 +84,7 @@ namespace coreutils {
/// Destructor for the ZString.
///
~ZString();
virtual ~ZString();
///
/// A friend method used to write the value of the ZString
@ -115,7 +115,7 @@ namespace coreutils {
bool operator<=(const ZString &valuex) const;
bool operator>=(const ZString &valuex) const;
bool operator==(const ZString valuex);
bool operator!=(const ZString &valuex) const;
bool operator!=(const ZString valuex) const;
bool operator!=(const char *valuex) const;
///
@ -248,6 +248,14 @@ namespace coreutils {
///
bool eod();
///
/// Go to the end of data.
///
/// Return number of bytes the cursor was moved.
///
int goeod();
///
///
@ -312,7 +320,7 @@ namespace coreutils {
///
/// Advance the cursor through the ZString for each whitespace character
/// encountered.
/// encountered.
///
int skipWhitespace();
@ -321,6 +329,12 @@ namespace coreutils {
///
///
int trimTrailingWhitespace();
///
///
///
bool lineIsWhitespace();
///
@ -454,9 +468,9 @@ namespace coreutils {
void moveBackToLineStart();
protected:
char *data;
char *cursor;
size_t length;
char *data = NULL;
char *cursor = NULL;
size_t length = 0;
char *cdata = NULL;
std::vector<ZString> list;

View File

@ -1,4 +1,4 @@
#!/bin/bash
#g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils

Binary file not shown.

View File

@ -27,6 +27,7 @@ int main(int argc, char **argv) {
test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}";
test1["object1.attr3"] = "Im not an object";
// test1["age"] = 64; future
std::cout << test1 << std::endl;
@ -58,6 +59,8 @@ int main(int argc, char **argv) {
test9 = test1["object1"];
std::cout << test9 << std::endl;
std::cout << test9["attr3"] << std::endl;
std::cout << test1.pretty() << std::endl;
return 0;
}

View File

@ -94,6 +94,9 @@ int main(int argc, char **argv) {
int test29 = 402;
test28 = test29;
std::cout << "int: " << test28 << std::endl;
coreutils::MString test30 = "ABCDEF";
std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl;
return 0;
}

View File

@ -122,5 +122,8 @@ int main(int argc, char **argv) {
std::cout << "integer '53524534' is [" << test24 << "]" << std::endl;
coreutils::ZString test25("-543");
std::cout << "integer '-543' is [" << test25 << "]" << std::endl;
coreutils::ZString test26(" this is a trim test ");
std::cout << "trimmed [" << test26.trim() << "]" << std::endl;
}