more serious work on JString.

This commit is contained in:
barant 2024-12-12 12:22:46 -08:00
parent c6e0486c14
commit 0d60cce8dc
8 changed files with 49 additions and 45 deletions

View File

@ -33,7 +33,7 @@ namespace coreutils {
throw coreutils::Exception("Fix me to accept integer type."); throw coreutils::Exception("Fix me to accept integer type.");
} }
} else { } else {
std::cout << "[" << unparsed() << "] >" << value << "<" << std::endl; // std::cout << "[" << unparsed() << "] >" << value << "<" << std::endl;
MString key; MString key;
if(notfirst) if(notfirst)
key << ","; key << ",";
@ -48,7 +48,7 @@ namespace coreutils {
} }
} }
ZString JString::find(ZString &path) { ZString JString::find(ZString path) {
reset(); reset();
if(locate(path)) { if(locate(path)) {
if(ifNext("\"")) if(ifNext("\""))
@ -64,23 +64,9 @@ namespace coreutils {
return ZString(); return ZString();
} }
bool JString::locate(ZString &path) { bool JString::locate(ZString path) {
std::cout << "path locate: " << path << std::endl; // std::cout << "path locate: " << path << std::endl;
path.split(".", 1); path.getTokenExclude("[.");
ZString pathx = path[0].getTokenExclude("[");
ZString index;
if(path[0].ifNext("[")) {
index = path[0].getTokenExclude("]");
if(path[0].ifNext("]")) {
// TODO: index value here
std::cout << ":::" << pathx << ":" << index << std::endl;
}
}
else
pathx = path[0];
pathx.reset();
index.reset();
path[0].reset();
notfirst = false; notfirst = false;
if(ifNext("{")) { if(ifNext("{")) {
while(startsWith("\"")) { while(startsWith("\"")) {
@ -93,13 +79,34 @@ namespace coreutils {
} else } else
throw coreutils::Exception("Labels must be contained in double quotes."); throw coreutils::Exception("Labels must be contained in double quotes.");
if(ifNext(":")) { if(ifNext(":")) {
// std::cout << "path: (" << pathx << ") " << label << std::endl; // std::cout << "path: (" << path.parsed() << ") (" << label << ") " << (label == path.parsed()) << std::endl;
if(label == pathx) { if(label == path.parsed()) {
if(path.getList().size() == 1) { if(path.unparsed() == "") {
std::cout << "found cursor: " << unparsed() << std::endl; // std::cout << "found cursor: " << unparsed() << std::endl;
return true; return true;
} }
return locate(path[1]); if(path.ifNext("."))
return locate(path.unparsed());
if(path.ifNext("[")) {
int index = path.asInteger();
if(!path.ifNext("]"))
throw coreutils::Exception("expecting ] after index value in array selector.");
if(ifNext("[")) {
while(index > 0) {
getContainer();
if(!ifNext(","))
throw coreutils::Exception("expecting comma to seperate array elements.");
--index;
}
return true;
} else
throw coreutils::Exception("Path is expecting an array for selector value.");
if(path.eod())
return false;
else if(path.ifNext("."))
return locate(path.unparsed());
throw coreutils::Exception("expecting '.' in path.");
}
} }
// std::cout << "+++" << unparsed() << std::endl; // std::cout << "+++" << unparsed() << std::endl;
if(startsWith("\"")) { if(startsWith("\"")) {
@ -117,9 +124,9 @@ namespace coreutils {
} }
} else if(ifNext("[")) { } else if(ifNext("[")) {
ZString label = path[0].getTokenExclude("["); ZString label = path[0].getTokenExclude("[");
path[0].ifNext("["); path.ifNext("[");
ZString key = path[0].getTokenExclude("="); ZString key = path[0].getTokenExclude("=");
path[0].ifNext("="); path.ifNext("=");
ZString value = path[0].getTokenExclude("]"); ZString value = path[0].getTokenExclude("]");
while(!ifNext("]")) { while(!ifNext("]")) {
push(); push();

View File

@ -42,9 +42,9 @@ namespace coreutils {
/// that begins the object's list. /// that begins the object's list.
/// ///
ZString find(ZString &path); ZString find(ZString path);
bool locate(ZString &path); bool locate(ZString path);
bool notfirst; bool notfirst;
ZString path; ZString path;
@ -82,7 +82,6 @@ namespace coreutils {
} }
JString & operator=(coreutils:: ZString value) { JString & operator=(coreutils:: ZString value) {
std::cout << "operator=" << value << std::endl;
setSize(value.getLength()); setSize(value.getLength());
memcpy(data, value.getData(), value.getLength()); memcpy(data, value.getData(), value.getLength());
length = value.getLength(); length = value.getLength();

View File

@ -210,19 +210,19 @@ namespace coreutils {
data[offset] = ch; data[offset] = ch;
} }
void MString::insert(ZString &value, int offset) { void MString::insert(ZString value, int offset) {
setSize(length + value.getLength()); setSize(length + value.getLength());
for (int ix = length; ix >= offset; ix--) for (int ix = length; ix >= offset; ix--)
getData()[ix + value.getLength()] = getData()[ix]; getData()[ix] = getData()[ix - value.getLength()];
std::memcpy(data + offset, value.getData(), value.getLength()); std::memcpy(data + offset, value.getData(), value.getLength());
} }
void MString::insert(std::string value, int offset) { // void MString::insert(std::string value, int offset) {
setSize(length + value.size()); // setSize(length + value.size());
for (int ix = length; ix >= offset; ix--) // for (int ix = length; ix >= offset; ix--)
getData()[ix + value.size()] = getData()[ix]; // getData()[ix + value.size()] = getData()[ix];
std::memcpy(data + offset, value.c_str(), value.size()); // std::memcpy(data + offset, value.c_str(), value.size());
} // }
void MString::replace(char ch, int offset) { void MString::replace(char ch, int offset) {
} }
@ -258,7 +258,6 @@ namespace coreutils {
int newBufferSize = ((size / 256) + 1) * 256; int newBufferSize = ((size / 256) + 1) * 256;
if (bufferSize != newBufferSize) { if (bufferSize != newBufferSize) {
bufferSize = newBufferSize; bufferSize = newBufferSize;
std::cout << "Adjusting storage size to " << bufferSize << " to accomodate " << size << "bytes." << std::endl;
data = (char *)realloc(data, bufferSize); data = (char *)realloc(data, bufferSize);
cursor = data + cursorOffset; cursor = data + cursorOffset;
} }

View File

@ -176,13 +176,13 @@ namespace coreutils {
/// ///
/// ///
void insert(ZString &value, int offset); void insert(ZString value, int offset);
/// ///
/// ///
/// ///
void insert(std::string value, int offset); // void insert(std::string value, int offset);
/// ///
/// ///

View File

@ -107,7 +107,7 @@ namespace coreutils {
bool ZString::operator==(const ZString valuex) { bool ZString::operator==(const ZString valuex) {
if(valuex.getLength() != length) if(valuex.getLength() != length)
return false; return false;
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0); return (strncmp(data, valuex.data, length) == 0);
} }
bool ZString::operator!=(const ZString &valuex) const { bool ZString::operator!=(const ZString &valuex) const {
@ -314,7 +314,7 @@ namespace coreutils {
char previous = 0; char previous = 0;
char *start = cursor; char *start = cursor;
do { do {
while ((cursor <= (data + length)) && !isCharacter(*cursor, exclude)) { while ((cursor < (data + length)) && !isCharacter(*cursor, exclude)) {
previous = *cursor; previous = *cursor;
++cursor; ++cursor;
} }

Binary file not shown.

View File

@ -17,6 +17,7 @@ int main(int argc, char **argv) {
test1["health"] = "100"; test1["health"] = "100";
test1["comment"] = "this is a comment"; test1["comment"] = "this is a comment";
test1["racex"] = "elvin";
test1["race"] = "human"; test1["race"] = "human";
test1["array"] = "[\"test1\",\"test2\",\"test3\"]"; test1["array"] = "[\"test1\",\"test2\",\"test3\"]";
test1["tester"] = "test field"; test1["tester"] = "test field";
@ -32,6 +33,7 @@ int main(int argc, char **argv) {
std::cout << test1["name"] << std::endl; std::cout << test1["name"] << std::endl;
std::cout << test1["health"] << std::endl; std::cout << test1["health"] << std::endl;
std::cout << test1["comment"] << std::endl; std::cout << test1["comment"] << std::endl;
std::cout << test1["racex"] << std::endl;
std::cout << test1["race"] << std::endl; std::cout << test1["race"] << std::endl;
std::cout << test1["array"] << std::endl; std::cout << test1["array"] << std::endl;
std::cout << test1["tester"] << std::endl; std::cout << test1["tester"] << std::endl;

View File

@ -8,11 +8,8 @@ int main(int argc, char **argv) {
std::cout << "char * operator=: [" << test1 << "]." << std::endl; std::cout << "char * operator=: [" << test1 << "]." << std::endl;
test1 = "new value."; test1 = "new value.";
std::cout << "char * operator=: [" << test1 << "]." << std::endl; std::cout << "char * operator=: [" << test1 << "]." << std::endl;
std::cout << "001" << std::endl;
std::string test11 = "this is a test"; std::string test11 = "this is a test";
std::cout << "002" << std::endl;
coreutils::MString test12 = test11; coreutils::MString test12 = test11;
std::cout << "003" << std::endl;
std::cout << "assign from std::string: [" << test12 << "]." << std::endl; std::cout << "assign from std::string: [" << test12 << "]." << std::endl;
coreutils::MString test2("this is another test."); coreutils::MString test2("this is another test.");