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

@ -18,11 +18,36 @@ namespace coreutils {
public: public:
MString & operator[](char *key); JString();
JString(const char *data);
MString & operator[](ZString &key); void set(ZString path, const char *value);
MString & operator[](std::string key); ///
/// 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;
}; };

View File

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

View File

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

View File

@ -74,15 +74,15 @@ namespace coreutils {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0); return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0);
} }
bool ZString::operator==(std::string value) { // bool ZString::operator==(std::string value) {
if(value.length() != length) // if(value.length() != length)
return false; // return false;
return (strncmp(data, value.c_str(), length) == 0); // return (strncmp(data, value.c_str(), length) == 0);
} // }
bool ZString::operator==(const char *valuex) const { // bool ZString::operator==(const char *valuex) const {
return (strncmp(data, valuex, length) == 0); // return (strncmp(data, valuex, length) == 0);
} // }
bool ZString::operator!=(const ZString &valuex) const { bool ZString::operator!=(const ZString &valuex) const {
// if(length != valuex.getLength()) // if(length != valuex.getLength())
@ -210,6 +210,30 @@ namespace coreutils {
return getTokenExclude(exclude.c_str()); 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) { ZString &ZString::operator[](int index) {
return list[index]; return list[index];
} }

View File

@ -105,8 +105,8 @@ namespace coreutils {
bool operator>(const ZString &valuex) const; bool operator>(const ZString &valuex) const;
bool operator==(const ZString &valuex); bool operator==(const ZString &valuex);
bool operator==(std::string value); // bool operator==(std::string value);
bool operator==(const char *valuex) const; // bool operator==(const char *valuex) const;
bool operator!=(const ZString &valuex) const; bool operator!=(const ZString &valuex) const;
bool operator!=(const char *valuex) const; bool operator!=(const char *valuex) const;
@ -207,6 +207,14 @@ namespace coreutils {
ZString getTokenExclude(std::string exclude); 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. /// Use the [] operator to retrieve values delimited by the split method.
/// ///
@ -358,9 +366,9 @@ namespace coreutils {
char *cursor; char *cursor;
size_t length; size_t length;
char *cdata = NULL; char *cdata = NULL;
std::vector<ZString> list;
private: private:
std::vector<ZString> list;
std::stack<char *> stack; std::stack<char *> stack;
bool isCharacter(char ch, const char *string); bool isCharacter(char ch, const char *string);

View File

@ -1,3 +1,4 @@
#!/bin/bash #!/bin/bash
g++ -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils g++ -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -o mstring_test mstring_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"); coreutils::ZString test2("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
std::cout << "substring: [" << test2.substring(10) << "]" << std::endl; 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;
} }