jstring changes
This commit is contained in:
parent
8f8c14d7b2
commit
f39bdc98f6
35
JString.h
35
JString.h
@ -11,19 +11,44 @@ namespace coreutils {
|
|||||||
/// Use the JString object when you need a JSON interface to C++. JString uses
|
/// 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.
|
/// an MString to store a JSON string representing the object(s) contained.
|
||||||
/// The [] operator is overriden from the ZString and provides a method to
|
/// 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 {
|
class JString : public MString {
|
||||||
|
|
||||||
public:
|
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);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ namespace coreutils {
|
|||||||
///
|
///
|
||||||
|
|
||||||
void remove(int offset, int length);
|
void remove(int offset, int length);
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
@ -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);
|
||||||
|
44
ZString.cpp
44
ZString.cpp
@ -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())
|
||||||
@ -205,11 +205,35 @@ namespace coreutils {
|
|||||||
++cursor;
|
++cursor;
|
||||||
return ZString(start, cursor - start);
|
return ZString(start, cursor - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
ZString ZString::getTokenExclude(std::string exclude) {
|
ZString ZString::getTokenExclude(std::string exclude) {
|
||||||
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];
|
||||||
}
|
}
|
||||||
@ -227,7 +251,7 @@ namespace coreutils {
|
|||||||
return false;
|
return false;
|
||||||
return strncmp(data, value, length) == 0;
|
return strncmp(data, value, length) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ZString::equals(char *value) {
|
bool ZString::equals(char *value) {
|
||||||
if (strlen(value) != length)
|
if (strlen(value) != length)
|
||||||
return false;
|
return false;
|
||||||
|
14
ZString.h
14
ZString.h
@ -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);
|
||||||
|
|
||||||
|
@ -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.
@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user