jstring changes

This commit is contained in:
Brad Arant 2023-03-08 13:54:42 -08:00
parent 8f8c14d7b2
commit f39bdc98f6
9 changed files with 92 additions and 19 deletions

View File

@ -11,19 +11,44 @@ namespace coreutils {
/// Use the JString object when you need a JSON interface to C++. JString uses
/// an MString to store a JSON string representing the object(s) contained.
/// The [] operator is overriden from the ZString and provides a method to
/// access the objects elements using a named path as the index to the items.
/// access the objects elements using a named path as the index to the items.
///
class JString : public MString {
public:
MString & operator[](char *key);
JString();
JString(const char *data);
MString & operator[](ZString &key);
void set(ZString path, const char *value);
///
/// Use the find method to look through an object for member specified by
/// the key in parameter 1. The cursor must be pointing to the { character
/// that begins the object's list.
///
ZString find(ZString path);
void operator=(ZString &value);
JString& operator=(const char *value);
// ZString operator[](const char *path);
JString& operator[](const char *path);
// operator const ZString () const {
// return *this;
// }
// MString & operator[](ZString &key);
// MString & operator[](std::string key);
private:
bool locate(ZString path);
bool notfirst;
MString path;
MString & operator[](std::string key);
};
}

View File

@ -166,6 +166,7 @@ namespace coreutils {
}
void MString::replace(ZString &value, int offset) {
}
void MString::replace(std::string value, int offset) {
@ -202,4 +203,9 @@ namespace coreutils {
length = size;
}
int MString::offset() {
return cursor - data;
}
}

View File

@ -121,7 +121,7 @@ namespace coreutils {
///
void remove(int offset, int length);
///
///
///
@ -134,6 +134,8 @@ namespace coreutils {
MString& write(ZString &value);
int offset();
private:
int bufferSize = 0;
void setSize(int size);

View File

@ -74,15 +74,15 @@ namespace coreutils {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0);
}
bool ZString::operator==(std::string value) {
if(value.length() != length)
return false;
return (strncmp(data, value.c_str(), length) == 0);
}
// bool ZString::operator==(std::string value) {
// if(value.length() != length)
// return false;
// return (strncmp(data, value.c_str(), length) == 0);
// }
bool ZString::operator==(const char *valuex) const {
return (strncmp(data, valuex, length) == 0);
}
// bool ZString::operator==(const char *valuex) const {
// return (strncmp(data, valuex, length) == 0);
// }
bool ZString::operator!=(const ZString &valuex) const {
// if(length != valuex.getLength())
@ -205,11 +205,35 @@ namespace coreutils {
++cursor;
return ZString(start, cursor - start);
}
ZString ZString::getTokenExclude(std::string exclude) {
return getTokenExclude(exclude.c_str());
}
ZString ZString::getContainer() {
char term = *cursor == '"' ? '"': *cursor == '(' ? ')': *cursor == '[' ? ']': *cursor == '{' ? '}': 0;
if(!term)
return ZString();
char *start = cursor;
char nest = *cursor;
char *end = cursor + length;
int level = 0;
++cursor;
while(cursor != end) {
if(*cursor == term) {
if(!level) {
++cursor;
return ZString(start, cursor - start);
}
--level;
} else if(*cursor == nest) {
++level;
}
++cursor;
}
return ZString();
}
ZString &ZString::operator[](int index) {
return list[index];
}
@ -227,7 +251,7 @@ namespace coreutils {
return false;
return strncmp(data, value, length) == 0;
}
bool ZString::equals(char *value) {
if (strlen(value) != length)
return false;

View File

@ -105,8 +105,8 @@ namespace coreutils {
bool operator>(const ZString &valuex) const;
bool operator==(const ZString &valuex);
bool operator==(std::string value);
bool operator==(const char *valuex) const;
// bool operator==(std::string value);
// bool operator==(const char *valuex) const;
bool operator!=(const ZString &valuex) const;
bool operator!=(const char *valuex) const;
@ -207,6 +207,14 @@ namespace coreutils {
ZString getTokenExclude(std::string exclude);
///
/// If the cursor is over a container character then a call to this method
/// will return the contents of the container. The cursor will remain at
/// the position following the container.
///
ZString getContainer();
///
/// Use the [] operator to retrieve values delimited by the split method.
///
@ -358,9 +366,9 @@ namespace coreutils {
char *cursor;
size_t length;
char *cdata = NULL;
std::vector<ZString> list;
private:
std::vector<ZString> list;
std::stack<char *> stack;
bool isCharacter(char ch, const char *string);

View File

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

Binary file not shown.

Binary file not shown.

View File

@ -32,4 +32,11 @@ int main(int argc, char **argv) {
coreutils::ZString test2("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
std::cout << "substring: [" << test2.substring(10) << "]" << std::endl;
// Test getContainer.
coreutils::ZString test4("test(this is a container (with a nesting))012345");
test4.getTokenExclude("(");
std::cout << "container: " << test4.getContainer() << std::endl;
std::cout << "rest: " << test4.unparsed() << std::endl;
}