NetBurner 3.5.0
PDF Version
 
nbstring.h
Go to the documentation of this file.
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 protected:
120 NBStorageManager nbs;
121
122 void DoStorage(const char *cp, int len);
123
124 static bool testInterpolationModifier(const char *begin, const char *end);
125 void modifyInterpValue(char *bufStart, char *valStart, const char *valEnd, const char *modStart, const char *modEnd);
126
127
128 public:
129 // constructors
134
140 NBString(const NBString &str);
141
149 NBString(const NBString &str, size_t pos, size_t len = npos);
150
156 NBString(const char *s);
157
164 NBString(const char *s, size_t n);
165
170
171 // Copy ...
172 NBString &operator=(const NBString &str);
173 NBString &operator=(const char *s);
174 NBString &operator=(char c);
175
176 //Actual assigment implimented in jsonlexer not NBString
177 NBString &operator=(const JsonRef &jr);
178
179
180
181 // Look up
182 char &operator[](size_t pos);
183 const char &operator[](size_t pos) const;
184
193 const char *c_str() const;
194
203 NBString substr(size_t pos = 0, size_t len = npos) const;
204
214 int compare(const NBString &str) const;
215
225 int compare(const char *s) const;
226
227 /*
228 * The following methods implement finding a string within another string.
229 * str - The string to find in this string.
230 * pos - The position in this string to start searching.
231 * n - How many characters of str to search for.
232 * c - What character to find.
233 *
234 * Returns a -1 if unable to find or if the search fails.
235 */
245 size_t find(const NBString str, size_t pos = 0) const;
246
256 size_t find(const char *str, size_t pos = 0) const;
257
268 size_t find(const char *s, size_t pos, size_t n) const;
269
279 size_t find(char c, size_t pos = 0) const;
280
292 size_t replace(const char *findStr, char rep, size_t startPos = 0, size_t end = 0);
293
305 size_t replace(char c, char rep, size_t startPos = 0, size_t end = 0);
306
307
308
309
310
316 size_t size() const;
317
323 size_t length() const;
324
331 void clear();
332
341 bool empty() const;
342
353 NBString &Append(const char *str, size_t len);
354
365 NBString &FdAppend(int fd, size_t len);
366
376 NBString &Reserve(size_t len);
377
378 NBString &operator+=(const NBString &str);
379 NBString &operator+=(const char *str);
380 NBString &operator+=(char c);
381
382
383 NBString & ToB64Url();
384
385
386
387#ifdef STRING_DIAGS
388 void DumpDiag();
389#endif
390
398 friend void swap(NBString &x, NBString &y);
399
400 friend void NBStringBuilder::moveTo(NBString &);
401 // concatinate
402 friend NBString operator+(const NBString &lhs, const NBString &rhs);
403 friend NBString operator+(const NBString &lhs, const char *rhs);
404 friend NBString operator+(const char *lhs, const NBString &rhs);
405 friend NBString operator+(const NBString &lhs, char rhs);
406 friend NBString operator+(char lhs, const NBString &rhs);
407
408 // Compare...
409 friend bool operator==(const NBString &lhs, const NBString &rhs);
410 friend bool operator==(const char *lhs, const NBString &rhs);
411 friend bool operator==(const NBString &lhs, const char *rhs);
412 friend bool operator!=(const NBString &lhs, const NBString &rhs);
413 friend bool operator!=(const char *lhs, const NBString &rhs);
414 friend bool operator!=(const NBString &lhs, const char *rhs);
415 friend bool operator<(const NBString &lhs, const NBString &rhs);
416 friend bool operator<(const char *lhs, const NBString &rhs);
417 friend bool operator<(const NBString &lhs, const char *rhs);
418 friend bool operator<=(const NBString &lhs, const NBString &rhs);
419 friend bool operator<=(const char *lhs, const NBString &rhs);
420 friend bool operator<=(const NBString &lhs, const char *rhs);
421 friend bool operator>(const NBString &lhs, const NBString &rhs);
422 friend bool operator>(const char *lhs, const NBString &rhs);
423 friend bool operator>(const NBString &lhs, const char *rhs);
424 friend bool operator>=(const NBString &lhs, const NBString &rhs);
425 friend bool operator>=(const char *lhs, const NBString &rhs);
426 friend bool operator>=(const NBString &lhs, const char *rhs);
427
428 // Member functions to add that are not in the standard string classes
429
436 int vsiprintf(const char *format, va_list &vl);
437
443 int sprintf(const char *format, ...);
444
450 int sprintf(NBString const format, ...);
451
457 int siprintf(const char *format, ...);
458
464 int siprintf(NBString const format, ...);
465
471 int stoi() const;
472
478 long stol() const;
479
485 unsigned int stoui() const;
486 unsigned int stouihex() const;
487
493 unsigned long stoul() const;
494 unsigned long stoulhex() const;
495
496
502 double stod() const;
503
510
511
512 /*
513 Calculate the base64url encode and returnt he string.
514 */
515 friend NBString b64UrlToString(const puint8_t p, int len);
516
517
518
519
520 /* Possible future additions, Compare substrings.
521 int compare (size_t pos, size_t len, const NBString& str) const;
522 int compare (size_t pos, size_t len, const NBString& str,size_t subpos, size_t sublen) const;
523 int compare (size_t pos, size_t len, const char* s) const;
524 int compare (size_t pos, size_t len, const char* s, size_t n) const;
525 */
526
536 size_t copy(char *s, size_t len, size_t pos = 0) const;
537
547 size_t strcopy(char *s, size_t len, size_t pos = 0) const;
548
549 /* *
550 * @brief Perform a string interpolation and place the finished interpolation
551 * in the Destination string
552 *
553 * @param dest Destination string
554 *
555 * @returns Whether the interpolation was successful
556 */
557 bool Interpolate(NBString &dest);
558
559};
560
561NBString b64UrlToString(const puint8_t p, int len);
562
563
564
565class NBInterpolatedString : public NBString
566{
567 bool bNeedInterpolate;
568public:
569 // constructors
573 NBInterpolatedString();
574
580 NBInterpolatedString(const NBString &str);
581
589 NBInterpolatedString(const NBString &str, size_t pos, size_t len = npos);
590
596 NBInterpolatedString(const char *s);
597
604 NBInterpolatedString(const char *s, size_t n);
605
609 ~NBInterpolatedString();
610
611 // Copy ...
612 NBInterpolatedString &operator=(const NBString &str);
613 NBInterpolatedString &operator=(const char *s);
614
625 NBString &Append(const char *str, size_t len);
626
637 NBString &FdAppend(int fd, size_t len);
638
639 NBString &operator+=(const NBString &str);
640 NBString &operator+=(const char *str);
641 NBString &operator+=(char c);
642
643 NBInterpolatedString &Interpolate();
644};
645
646
647
648
649
650
651
652// concatinate
653NBString operator+(const NBString &lhs, const NBString &rhs);
654NBString operator+(const NBString &lhs, const char *rhs);
655NBString operator+(const char *lhs, const NBString &rhs);
656NBString operator+(const NBString &lhs, char rhs);
657NBString operator+(char lhs, const NBString &rhs);
658
659// Compare...
660inline bool operator==(const NBString &lhs, const NBString &rhs)
661{
662 return lhs.compare(rhs) == 0;
663};
664inline bool operator==(const char *lhs, const NBString &rhs)
665{
666 return rhs.compare(lhs) == 0;
667};
668inline bool operator==(const NBString &lhs, const char *rhs)
669{
670 return lhs.compare(rhs) == 0;
671};
672inline bool operator!=(const NBString &lhs, const NBString &rhs)
673{
674 return lhs.compare(rhs) != 0;
675};
676inline bool operator!=(const char *lhs, const NBString &rhs)
677{
678 return rhs.compare(lhs) != 0;
679};
680inline bool operator!=(const NBString &lhs, const char *rhs)
681{
682 return lhs.compare(rhs) != 0;
683};
684inline bool operator<(const NBString &lhs, const NBString &rhs)
685{
686 return lhs.compare(rhs) > 0;
687};
688inline bool operator<(const char *lhs, const NBString &rhs)
689{
690 return rhs.compare(lhs) < 0;
691};
692inline bool operator<(const NBString &lhs, const char *rhs)
693{
694 return lhs.compare(rhs) > 0;
695};
696inline bool operator<=(const NBString &lhs, const NBString &rhs)
697{
698 return lhs.compare(rhs) >= 0;
699};
700inline bool operator<=(const char *lhs, const NBString &rhs)
701{
702 return rhs.compare(lhs) <= 0;
703};
704inline bool operator<=(const NBString &lhs, const char *rhs)
705{
706 return lhs.compare(rhs) >= 0;
707};
708inline bool operator>(const NBString &lhs, const NBString &rhs)
709{
710 return lhs.compare(rhs) < 0;
711};
712inline bool operator>(const char *lhs, const NBString &rhs)
713{
714 return rhs.compare(lhs) > 0;
715};
716inline bool operator>(const NBString &lhs, const char *rhs)
717{
718 return lhs.compare(rhs) < 0;
719};
720inline bool operator>=(const NBString &lhs, const NBString &rhs)
721{
722 return lhs.compare(rhs) <= 0;
723};
724inline bool operator>=(const char *lhs, const NBString &rhs)
725{
726 return rhs.compare(lhs) >= 0;
727};
728inline bool operator>=(const NBString &lhs, const char *rhs)
729{
730 return lhs.compare(rhs) <= 0;
731};
732
733
734#endif
735
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:112
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 *.
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.
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.
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:530
NetBurner System Functions.