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() { File::~File() {
close(fd); close(fd);
setBufferSize(0); free(buffer);
} }
void File::setBufferSize(size_t size) { void File::setBufferSize(size_t size) {

2
File.h
View File

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

View File

@ -24,7 +24,7 @@ namespace coreutils {
write(*this); write(*this);
} }
void changed(ZString key, ZString value) overide;
}; };

View File

@ -170,4 +170,53 @@ namespace coreutils {
reset(); 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(); return jstring.find(key).str();
} }
Proxy& operator=(coreutils::ZString value) { Proxy& operator=(coreutils::MString value) {
jstring.set(key, value); jstring.set(key, value);
jstring.changed(key, value);
return *this; return *this;
} }
@ -83,6 +84,10 @@ namespace coreutils {
JString & operator=(coreutils:: ZString value); JString & operator=(coreutils:: ZString value);
MString pretty();
virtual void changed(ZString key, ZString value);
private: private:
void removeWhitespace(); void removeWhitespace();

View File

@ -1,6 +1,7 @@
#include "MString.h" #include "MString.h"
#include "Log.h" #include "Log.h"
#include "uuid/uuid.h" #include "uuid/uuid.h"
#include "Exception.h"
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <format> #include <format>
@ -186,13 +187,11 @@ namespace coreutils {
return *this; return *this;
} }
// MString &MString::operator<<(std::string value) { MString &MString::operator<<(char value) {
// int temp = length; setSize(length + 1);
// int len = length + value.length(); data[length - 1] = value;
// setSize(len); return *this;
// memcpy(data + temp, value.c_str(), value.length()); }
// return *this;
// }
// MString &MString::operator<<(std::ostream (*f)(std::ostream&)) { // MString &MString::operator<<(std::ostream (*f)(std::ostream&)) {
MString &MString::operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&)) { MString &MString::operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&)) {
@ -282,4 +281,129 @@ namespace coreutils {
return cursor - data; 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);
/// ///
/// ///
@ -232,13 +232,80 @@ namespace coreutils {
MString &read(int fd); MString &read(int fd);
///
///
///
int offset(); int offset();
///
///
///
MString toBinary();
///
///
///
MString fromBinary();
///
///
///
MString toHex();
///
///
///
MString fromHex();
///
///
///
MString toBase64();
///
///
///
MString fromBase64();
///
///
///
MString toUpper();
///
///
///
MString toLower();
///
///
///
MString toCGI();
///
///
///
MString fromCGI();
protected: protected:
void setSize(int size); void setSize(int size);
private: private:
int bufferSize = 0; int bufferSize = 0;
char hexChar(char c);
}; };
} }

View File

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

View File

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

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/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++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
g++ -o jstring_test jstring_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"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}";
test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}"; test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}";
test1["object1.attr3"] = "Im not an object"; test1["object1.attr3"] = "Im not an object";
// test1["age"] = 64; future
std::cout << test1 << std::endl; std::cout << test1 << std::endl;
@ -59,6 +60,8 @@ int main(int argc, char **argv) {
std::cout << test9 << std::endl; std::cout << test9 << std::endl;
std::cout << test9["attr3"] << std::endl; std::cout << test9["attr3"] << std::endl;
std::cout << test1.pretty() << std::endl;
return 0; return 0;
} }

View File

@ -95,6 +95,9 @@ int main(int argc, char **argv) {
test28 = test29; test28 = test29;
std::cout << "int: " << test28 << std::endl; 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; return 0;
} }

View File

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