source: cpp/frams/util/sstring-simple.h @ 1156

Last change on this file since 1156 was 973, checked in by Maciej Komosinski, 4 years ago

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

  • Property svn:eol-style set to native
File size: 6.1 KB
RevLine 
[385]1#ifndef _SSTRING_SIMPLE_H_
2#define _SSTRING_SIMPLE_H_
3
4#include <stdint.h>
5#include <string.h>
6#include <stdlib.h>
7#include <stdio.h>
8
9class ExtValue;  //this include would result in recurrent inclusion: #include "extvalue.h"
10class ExtObject;
11
12class SString
13{
14private:
[793]15        char *txt;      ///< string buffer or NULL for empty string
[973]16        int allocated;       ///< allocated memory (including \0)
[793]17        int used;       ///< string length
18        int appending;  ///< append mode, changes can occur after character # 'appending'
[385]19
[793]20        void initEmpty();
21        void copyFrom(SString &from); ///< copy from SString
[973]22        void reallocate(int newsize);
23        void ensureAllocated(int needed);
[793]24        const char* getPtr() const { return txt ? txt : ""; }
[385]25
26public:
[793]27        SString(); ///< make an empty string
28        SString(const char*t, int t_len = -1); ///< make a string from char*
[955]29        SString(int x) = delete; ///< disallow the former 'int' constructor (so the new 'char' version is not used through implicit conversion)
[793]30        SString(const SString& from); ///< duplicate string
31        SString(SString&& from);///< move
[955]32        SString(char in);
[793]33        ~SString();
[385]34
[793]35        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
[973]36
37        void* operator new(size_t s, void* mem) { return mem; }
[385]38#ifdef _MSC_VER
[973]39        void operator delete(void* mem, void* t) {}
[385]40#endif
[973]41        void* operator new(size_t s) { return malloc(sizeof(SString)); }
[793]42        void operator delete(void* mem) { free(mem); }
[385]43
[973]44        int length() const { return used; } ///< get string length
45        int size() const { return length(); } ///< get string length
[793]46        void shrink(); ///< free unnecessary buffer
[973]47        void reserve(int cap) { ensureAllocated(cap + 1); } ///< like in std::string
[385]48
[793]49        /// after this call, you can modify sstring directly.
50        /// returned value is the pointer to the internal buffer.
51        /// <B>ensuresize</B> is minimal value of bytes you need,
52        /// the buffer will be resized as needed.
53        /// all "direct" operations have to leave the buffer with trailing '\0'
54        /// at the end. endWrite() will search for this value in order to determine
55        /// new string length.
56        /// <P>Sample:<CODE>
57        /// SString t;
58        /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);
59        /// t.endWrite();</CODE>
60        char *directWrite(int ensuresize = -1);
61        //char *directWrite();
62        /// like directWrite, but it returns the pointer to the first char after current string
63        /// for easy appending. <B>maxappend</B> is minimum of character in buffer
64        /// that can be appended after this call.
65        /// <P>Sample:<CODE>
66        /// SString t;
67        /// sprintf(t.directAppend(10),"c=%d",c);
68        /// t.endAppend();</CODE>
69        char *directAppend(int maxappend = 0);
70        /// update string length, after directWrite.
71        /// you don't have to to call endWrite after directWrite if the string's length doesn't change.
72        /// optional <B>newlength</B> parameter gives a chance to further optimize
73        /// this operation if you know exact length of resulting string.
74        /// <P>Sample:<CODE>
75        /// SString t("samplestring");
76        /// strncpy(t.directWrite(50),src,bytecount);
77        /// t.endWrite(bytecount);</CODE>
78        void endWrite(int newlength = -1);
79        /// update string length, after directAppend.
80        /// you will usually need to call endAppend (or endWrite) after directAppend,
81        /// because the purpose of directAppend is to change string's length.
82        /// optional <B>appendlength</B> parameter gives a chance to further optimize
83        /// this operation if you know exact length of the appended string.
84        /// <P>Sample:<CODE>
85        /// SString t("samplestring");
86        /// strncpy(t.directAppend(50),src,bytecount);
87        /// t.endAppend(bytecount);</CODE>
88        void endAppend(int appendlength = -1);
[385]89
[973]90        int capacity() { return (allocated > 0) ? (allocated - 1) : allocated; } ///< std::string.capacity()
[385]91
[793]92        /// find a character in SString.
93        /// return index if the character was found or -1 otherwise.
94        int indexOf(int character, int start = 0) const;
[385]95
[793]96        /// find a substring.
97        /// return index if the substring was found or -1 otherwise.
98        int indexOf(const char *substring, int start = 0) const;
[385]99
[793]100        /// find a substring.
101        /// return index if the substring was found or -1 otherwise.
102        int indexOf(const SString & substring, int start = 0) const;
[385]103
[793]104        const char* c_str() const { return getPtr(); } ///< get SString's readonly buffer
105        void operator=(const char*t); ///< assign from const char*
106        void operator=(const SString &s);
[385]107
[793]108        void append(const char *t, int n);
109        SString operator+(const SString &s) const;
110        void operator+=(int x); ///< append x spaces after current string
111        void operator+=(const char*); ///< append char* contents
112        void operator+=(const SString&); ///< append other SString
[385]113
[793]114        bool equals(const SString &s) const; ///< TRUE if equal
115        bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal
116        bool operator!=(const SString &s) const { return !equals(s); } ///< TRUE if not equal
117        bool operator<(const SString &s) const { return strcmp(getPtr(), s.getPtr()) < 1; }
118        const char* operator()(int p) const { return getPtr() + p; } ///< pointer to p'th character in SString
119        char operator[](int i) const { return getPtr()[i]; } ///< get char like in array
[385]120
[793]121        /// return a substring of the current string
122        SString substr(int begin, int length = 1 << 30) const;
[385]123
[793]124        /// simple tokenization:
125        /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character
126        /// and put it in output parameter <B>token</B>.
127        /// <B>pos</B> is moved accordingly.
128        /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.
129        bool getNextToken(int& pos, SString &token, char separator) const;
[385]130
[793]131        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
[385]132
[793]133        bool startsWith(const char *pattern) const;
134        char charAt(int pos) const { return operator[](pos); }
135        uint32_t hash() const;
[385]136
[793]137        static SString valueOf(int);
138        static SString valueOf(long);
139        static SString valueOf(double);
140        static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
141        static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
142        static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
143        static SString sprintf(const char* format, ...);
[385]144
[793]145        static SString &empty();
[385]146};
147
148#endif
Note: See TracBrowser for help on using the repository browser.