NetBurner 3.5.6
PDF Version
nbstring.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
21#ifndef DEF_NB_STRING
22#define DEF_NB_STRING (1)
23
24#include <stdint.h>
25#include <stdio.h>
26#include <system.h>
27#include <fd_adapter.h>
28class JsonRef;
30
31
32const int native_string_storage = 15; // Strings up to this amount are stored locally instead of allocating memory
33
34
35//Flag for no position or last position in string.
36//named npos to be compaitblke with other string clsses...
37const size_t npos = -1;
38const size_t NO_POS =-1; //More inline with netburner code foiomats constats are all caps.
39
40const uint8_t FLAG_LOCAL = 0x80;
41const uint8_t FLAG_CONST = 0x40;
42const uint8_t FLAG_ALLOC = 0x20;
43const uint8_t FLAG_CLEAR_LOCAL_MASK = 0x7F;
44const int EXTRA_ALLOC_SPACE = 64;
45
46
47
48
49struct nbstring_allocated_info
50{
51 uint8_t flag; // 0 = local storage, > native_string_storage = allocated memory
52 uint8_t unused1;
53 uint16_t unused2;
54 uint32_t used; // How many bytes are used in the string
55 uint32_t alloced; // Size of data pointed at by pData (used and unused). Will be 0 for data declared as constant
56 uint8_t *pData; // Pointer to string
57};
58
59struct nbstring_local
60{
61 uint8_t siz_flag; // Number of bytes stored in local storage
62 uint8_t stor[native_string_storage]; // actual string data stored locally
63};
64
65class NBStorageManager
66{
67 private:
68 union
69 {
70 nbstring_local nbss;
71 nbstring_allocated_info inf;
72 };
73
74 public:
75 inline bool IsConstStore() const {return ((inf.flag & FLAG_CONST)!=0);};
76 inline bool IsLocalStore() const { return ((inf.flag & FLAG_LOCAL)!=0);};
77
78 void reservespace(size_t n);
79 void setusedsize(size_t n);
80
81 size_t GetUsed() const;
82 size_t GetAvailable() const;
83 char *GetBuf();
84 const char *GetConstBuf() const;
85
86 void SetFromConstant(const char *c, int len);
87
88 void clear();
89
90 ~NBStorageManager();
91 NBStorageManager();
92
93 static void swap(NBStorageManager &n1, NBStorageManager &n2);
94#ifdef STRING_DIAGS
95 void DumpDiag();
96#endif
97};
98
99class NBString;
100
101
102class NBStringBuilder : public NBStorageManager, public fd_adapter
103{
104 public:
105 int read(char *buf, int nbytes) override;
106 int write(const char *buf, int nbytes) override;
107 int close() override;
108
109 void moveTo(NBString &s);
110};
111
118{
119 bool endswith(const char *s, size_t slen);
120 protected:
121 NBStorageManager nbs;
122
123 void DoStorage(const char *cp, int len);
124
125 static bool testInterpolationModifier(const char *begin, const char *end);
126 void modifyInterpValue(char *bufStart, char *valStart, const char *valEnd, const char *modStart, const char *modEnd);
127
128
129 public:
130 // constructors
135
141 NBString(const NBString &str);
142
150 NBString(const NBString &str, size_t pos, size_t len = npos);
151
157 NBString(const char *s);
158
165 NBString(const char *s, size_t n);
166
174 NBString(const JsonRef &jr);
179
180 // Copy ...
181 NBString &operator=(const NBString &str);
182 NBString &operator=(const char *s);
183 NBString &operator=(char c);
184
185 //Actual assigment implimented in jsonlexer not NBString
186 NBString &operator=(const JsonRef &jr);
187
188
189
190 // Look up
191 char &posRef(size_t pos);
192 char &operator[](size_t pos);
193 const char &operator[](size_t pos) const;
194
203 const char *c_str() const;
204
213 NBString substr(size_t pos = 0, size_t len = npos) const;
214
224 int compare(const NBString &str) const;
225
235 int compare(const char *s) const;
236
237 /*
238 * The following methods implement finding a string within another string.
239 * str - The string to find in this string.
240 * pos - The position in this string to start searching.
241 * n - How many characters of str to search for.
242 * c - What character to find.
243 *
244 * Returns a -1 if unable to find or if the search fails.
245 */
255 size_t find(const NBString str, size_t pos = 0) const;
256
266 size_t find(const char *str, size_t pos = 0) const;
267
278 size_t find(const char *s, size_t pos, size_t n) const;
279
289 size_t find(char c, size_t pos = 0) const;
290
302 size_t replace(const char *findStr, char rep, size_t startPos = 0, size_t end = 0);
303
315 size_t replace(char c, char rep, size_t startPos = 0, size_t end = 0);
316
317
318 bool endswith(const char *s);
319
320
325 void shrink(int n);
326
327
333 size_t size() const;
334
340 size_t length() const;
341
348 void clear();
349
358 bool empty() const;
359
370 NBString &Append(const char *str, size_t len);
371
382 NBString &FdAppend(int fd, size_t len);
383
393 NBString &Reserve(size_t len);
394
395 NBString &operator+=(const NBString &str);
396 NBString &operator+=(const char *str);
397 NBString &operator+=(char c);
398
399
400 NBString & ToB64Url();
401
402
403
404#ifdef STRING_DIAGS
405 void DumpDiag();
406#endif
407
415 friend void swap(NBString &x, NBString &y);
416
417 friend void NBStringBuilder::moveTo(NBString &);
418 // concatinate
419 friend NBString operator+(const NBString &lhs, const NBString &rhs);
420 friend NBString operator+(const NBString &lhs, const char *rhs);
421 friend NBString operator+(const char *lhs, const NBString &rhs);
422 friend NBString operator+(const NBString &lhs, char rhs);
423 friend NBString operator+(char lhs, const NBString &rhs);
424
425 // Compare...
426 friend bool operator==(const NBString &lhs, const NBString &rhs);
427 friend bool operator==(const char *lhs, const NBString &rhs);
428 friend bool operator==(const NBString &lhs, const char *rhs);
429 friend bool operator!=(const NBString &lhs, const NBString &rhs);
430 friend bool operator!=(const char *lhs, const NBString &rhs);
431 friend bool operator!=(const NBString &lhs, const char *rhs);
432 friend bool operator<(const NBString &lhs, const NBString &rhs);
433 friend bool operator<(const char *lhs, const NBString &rhs);
434 friend bool operator<(const NBString &lhs, const char *rhs);
435 friend bool operator<=(const NBString &lhs, const NBString &rhs);
436 friend bool operator<=(const char *lhs, const NBString &rhs);
437 friend bool operator<=(const NBString &lhs, const char *rhs);
438 friend bool operator>(const NBString &lhs, const NBString &rhs);
439 friend bool operator>(const char *lhs, const NBString &rhs);
440 friend bool operator>(const NBString &lhs, const char *rhs);
441 friend bool operator>=(const NBString &lhs, const NBString &rhs);
442 friend bool operator>=(const char *lhs, const NBString &rhs);
443 friend bool operator>=(const NBString &lhs, const char *rhs);
444
445 // Member functions to add that are not in the standard string classes
446
453 int vsiprintf(const char *format, va_list &vl);
454
460 int sprintf(const char *format, ...);
461
467 int sprintf(NBString const format, ...);
468
474 int siprintf(const char *format, ...);
475
481 int siprintf(NBString const format, ...);
482
488 int stoi() const;
489
495 long stol() const;
496
502 unsigned int stoui() const;
503 unsigned int stouihex() const;
504
510 unsigned long stoul() const;
511 unsigned long stoulhex() const;
512
513
519 double stod() const;
520
527
528
529 /*
530 Calculate the base64url encode and returnt he string.
531 */
532 friend NBString b64UrlToString(const puint8_t p, int len);
533
534
535
536
537 /* Possible future additions, Compare substrings.
538 int compare (size_t pos, size_t len, const NBString& str) const;
539 int compare (size_t pos, size_t len, const NBString& str,size_t subpos, size_t sublen) const;
540 int compare (size_t pos, size_t len, const char* s) const;
541 int compare (size_t pos, size_t len, const char* s, size_t n) const;
542 */
543
553 size_t copy(char *s, size_t len, size_t pos = 0) const;
554
564 size_t strcopy(char *s, size_t len, size_t pos = 0) const;
565
582
595 inline bool Interpolate() { return Interpolate(*this); }
596};
597
598NBString b64UrlToString(const puint8_t p, int len);
599
600
655{
656 bool bNeedInterpolate;
657public:
658 // constructors
663
670
678 NBInterpolatedString(const NBString &str, size_t pos, size_t len = npos);
679
685 NBInterpolatedString(const char *s);
686
693 NBInterpolatedString(const char *s, size_t n);
694
699
700 // Copy ...
701 NBInterpolatedString &operator=(const NBString &str);
702 NBInterpolatedString &operator=(const char *s);
703
714 NBString &Append(const char *str, size_t len);
715
726 NBString &FdAppend(int fd, size_t len);
727
728 NBString &operator+=(const NBString &str);
729 NBString &operator+=(const char *str);
730 NBString &operator+=(char c);
731
743};
744
745
746
747
748
749
750
751// concatinate
752NBString operator+(const NBString &lhs, const NBString &rhs);
753NBString operator+(const NBString &lhs, const char *rhs);
754NBString operator+(const char *lhs, const NBString &rhs);
755NBString operator+(const NBString &lhs, char rhs);
756NBString operator+(char lhs, const NBString &rhs);
757
758// Compare...
759inline bool operator==(const NBString &lhs, const NBString &rhs)
760{
761 return lhs.compare(rhs) == 0;
762};
763inline bool operator==(const char *lhs, const NBString &rhs)
764{
765 return rhs.compare(lhs) == 0;
766};
767inline bool operator==(const NBString &lhs, const char *rhs)
768{
769 return lhs.compare(rhs) == 0;
770};
771inline bool operator!=(const NBString &lhs, const NBString &rhs)
772{
773 return lhs.compare(rhs) != 0;
774};
775inline bool operator!=(const char *lhs, const NBString &rhs)
776{
777 return rhs.compare(lhs) != 0;
778};
779inline bool operator!=(const NBString &lhs, const char *rhs)
780{
781 return lhs.compare(rhs) != 0;
782};
783inline bool operator<(const NBString &lhs, const NBString &rhs)
784{
785 return lhs.compare(rhs) > 0;
786};
787inline bool operator<(const char *lhs, const NBString &rhs)
788{
789 return rhs.compare(lhs) < 0;
790};
791inline bool operator<(const NBString &lhs, const char *rhs)
792{
793 return lhs.compare(rhs) > 0;
794};
795inline bool operator<=(const NBString &lhs, const NBString &rhs)
796{
797 return lhs.compare(rhs) >= 0;
798};
799inline bool operator<=(const char *lhs, const NBString &rhs)
800{
801 return rhs.compare(lhs) <= 0;
802};
803inline bool operator<=(const NBString &lhs, const char *rhs)
804{
805 return lhs.compare(rhs) >= 0;
806};
807inline bool operator>(const NBString &lhs, const NBString &rhs)
808{
809 return lhs.compare(rhs) < 0;
810};
811inline bool operator>(const char *lhs, const NBString &rhs)
812{
813 return rhs.compare(lhs) > 0;
814};
815inline bool operator>(const NBString &lhs, const char *rhs)
816{
817 return lhs.compare(rhs) < 0;
818};
819inline bool operator>=(const NBString &lhs, const NBString &rhs)
820{
821 return lhs.compare(rhs) <= 0;
822};
823inline bool operator>=(const char *lhs, const NBString &rhs)
824{
825 return rhs.compare(lhs) >= 0;
826};
827inline bool operator>=(const NBString &lhs, const char *rhs)
828{
829 return lhs.compare(rhs) <= 0;
830};
831
832
833
834
835#endif
836
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
Represents a positional reference (pointer) of a location inside a ParsedJsonDataSet object.
Definition json_lexer.h:113
A wrapper for NBString::Interpolate() ideal for easy access to ephemeral values.
Definition nbstring.h:655
NBString & Append(const char *str, size_t len)
Append a string to an existing NBString object.
~NBInterpolatedString()
NBInterpolatedString destructor.
NBInterpolatedString()
Construct a NBInterpolatedString object.
NBInterpolatedString & Interpolate()
Perform a string interpolation in-place. This allows NBStrings to easily access the NetBurner Config ...
NBInterpolatedString(const NBString &str)
Construct a new NBInterpolatedString object from an existing NBInterpolatedString object.
NBInterpolatedString(const char *s)
Construct a NBInterpolatedString object from a character string (null terminated array of characters)
NBInterpolatedString(const char *s, size_t n)
Construct a NBInterpolatedString object from a character string up to the specified amount.
NBInterpolatedString(const NBString &str, size_t pos, size_t len=npos)
Construct a new NBInterpolatedString object from a substring of an existing NBInterpolatedString obje...
NBString & FdAppend(int fd, size_t len)
Append from a file descriptor to an existing NBString object.
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
int compare(const NBString &str) const
Compares two NBString objects.
size_t find(const char *s, size_t pos, size_t n) const
Find a substring within a character string.
long stol() const
Parse the string value and convert to a long integer number.
~NBString()
NBString destructor.
NBString(const NBString &str)
Construct a new NBString object from an existing NBString object.
NBString(const char *s)
Construct a NBString object from a character string (null terminated array of characters)
size_t size() const
Returns the current size of memory allocated for the string.
const char * c_str() const
Method to pass a NBString as a constant char *.
bool Interpolate(NBString &dest)
Perform a string interpolation and place the finished interpolation in the Destination string....
size_t length() const
Returns the length of the string.
friend void swap(NBString &x, NBString &y)
Swaps the contents of two NBString objects.
IPADDR to_ipaddr() const
Parse the string value and convert to an IPADDR IP address.
unsigned long stoul() const
Parse the string value and convert to a const unsigned long integer number.
size_t find(const char *str, size_t pos=0) const
Find a substring within a character string.
void clear()
Clear a NBString object and free allocated memory.
size_t replace(const char *findStr, char rep, size_t startPos=0, size_t end=0)
Replace all occurrences of all "<findStr>" characters within the NBString object.
bool empty() const
Check if a string is empty.
int vsiprintf(const char *format, va_list &vl)
Print to a string with formatting.
int stoi() const
Parse the string and convert to an integer number.
unsigned int stoui() const
Parse the string value and convert to a const integer number.
int compare(const char *s) const
Compares the NBString object to a character string.
void shrink(int n)
Reduces the length of the string by n.
bool Interpolate()
Perform an in-place string interpolation. This allows NBStrings to easily access the NetBurner Config...
Definition nbstring.h:595
double stod() const
Parse the string value and convert to a const double float number.
NBString & FdAppend(int fd, size_t len)
Append from a file descriptor to an existing NBString object.
int siprintf(NBString const format,...)
isprintf (integer) to a string with formatting to a NBString object
NBString()
Construct a NBString object.
NBString substr(size_t pos=0, size_t len=npos) const
Creates a new NBString object from a substring of the existing NBString object.
NBString & Reserve(size_t len)
Reserve an additional buffer amount.
size_t copy(char *s, size_t len, size_t pos=0) const
Copy a substring, does not null terminate.
size_t find(const NBString str, size_t pos=0) const
Find a substring within a NBString object.
size_t find(char c, size_t pos=0) const
Find the first occurence of a character within the NBString object.
NBString(const JsonRef &jr)
Construct a NBString object using the current string referenced by a JsonRef. This allows for using L...
int sprintf(const char *format,...)
sprintf to a string with formatting to a character array
NBString & Append(const char *str, size_t len)
Append a string to an existing NBString object.
NBString(const char *s, size_t n)
Construct a NBString object from a character string up to the specified amount.
NBString(const NBString &str, size_t pos, size_t len=npos)
Construct a new NBString object from a substring of an existing NBString object.
size_t strcopy(char *s, size_t len, size_t pos=0) const
Copy a substring, with null termination.
int siprintf(const char *format,...)
isprintf (integer) to a string with formatting to a character array
int sprintf(NBString const format,...)
sprintf to a string with formatting as a NBString object
size_t replace(char c, char rep, size_t startPos=0, size_t end=0)
Replace all occurrences of all "<findStr>" characters within the NBString object.
A class to create, read, and modify a JSON object.
Definition json_lexer.h:535