[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[973] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #ifndef _SSTRING_H_ |
---|
| 6 | #define _SSTRING_H_ |
---|
| 7 | |
---|
[385] | 8 | #define SSTRING_SIMPLE |
---|
[109] | 9 | |
---|
| 10 | #ifdef SSTRING_SIMPLE |
---|
| 11 | |
---|
| 12 | // simple sstring implementation using direct character arrays |
---|
| 13 | // - duplicate = copy all characters |
---|
| 14 | // - no mutex needed |
---|
| 15 | |
---|
| 16 | #include "sstring-simple.h" |
---|
| 17 | |
---|
| 18 | #else |
---|
| 19 | /////////////////////////////////////////////////////////////////////////// |
---|
| 20 | // old sstring implementation using SBuf references |
---|
| 21 | // - duplicate = copy buffer pointer |
---|
| 22 | // - mutex required to be thread safe |
---|
| 23 | |
---|
[247] | 24 | #include <stdint.h> |
---|
[109] | 25 | #include <string.h> |
---|
| 26 | #include <stdlib.h> |
---|
| 27 | #include <stdio.h> |
---|
| 28 | |
---|
| 29 | class ExtValue; //this include would result in recurrent inclusion: #include "extvalue.h" |
---|
| 30 | class ExtObject; |
---|
| 31 | |
---|
| 32 | class SBuf |
---|
| 33 | { |
---|
[793] | 34 | char *txt; |
---|
| 35 | int used; ///< data size |
---|
| 36 | int size; ///< buffer size, not including \0, special case: 0==buffer not allocated |
---|
| 37 | int refcount; ///< buffer is used by 'refcount' objects. |
---|
| 38 | void initEmpty(); |
---|
| 39 | void ensureSize(int wantsize); |
---|
| 40 | void copyFrom(const char* ch, int chlen = -1); |
---|
| 41 | void freeBuf(); |
---|
| 42 | void append(const char* ch, int chlen = -1); |
---|
| 43 | static SBuf &empty(); |
---|
| 44 | SBuf(int initsize); |
---|
| 45 | friend class SString; |
---|
| 46 | SBuf(const SBuf& b) {} |
---|
[109] | 47 | public: |
---|
[793] | 48 | SBuf(); |
---|
| 49 | ~SBuf(); |
---|
| 50 | uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash |
---|
[109] | 51 | }; |
---|
| 52 | |
---|
| 53 | /// (not so) simple text string class |
---|
| 54 | |
---|
| 55 | class SString |
---|
| 56 | { |
---|
| 57 | private: |
---|
[793] | 58 | SBuf *buf; ///< buffer |
---|
| 59 | int appending; ///< append mode, changes can occur after character # 'appending' |
---|
| 60 | //int memhint; |
---|
[109] | 61 | |
---|
[793] | 62 | void initEmpty(); |
---|
| 63 | int guessMemSize(int request); |
---|
| 64 | void copyFrom(SString &from); ///< copy from SString, reference if possible |
---|
| 65 | void detach(); ///< detach from shared buffer, if any |
---|
| 66 | void detachEmpty(int ensuresize = 0); ///< detach and make empty |
---|
| 67 | void detachCopy(int ensuresize = 0); ///< detach and make private copy |
---|
[109] | 68 | |
---|
| 69 | public: |
---|
[793] | 70 | SString(); ///< make an empty string |
---|
| 71 | SString(const char*t, int t_len = -1); ///< make a string from char* |
---|
[955] | 72 | SString(int x) = delete; ///< disallow the former 'int' constructor (so the new 'char' version is not used through implicit conversion) |
---|
[793] | 73 | SString(const SString& from); ///< duplicate string |
---|
| 74 | SString(SString&& from); ///< move |
---|
[955] | 75 | SString(char in); |
---|
[793] | 76 | ~SString(); |
---|
[109] | 77 | |
---|
[793] | 78 | void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown |
---|
[109] | 79 | |
---|
[889] | 80 | void* operator new(size_t s, void* mem) { return mem; } |
---|
[109] | 81 | #ifdef _MSC_VER |
---|
[889] | 82 | void operator delete(void* mem, void* t) {} |
---|
[109] | 83 | #endif |
---|
[889] | 84 | void* operator new(size_t s) { return malloc(sizeof(SString)); } |
---|
[793] | 85 | void operator delete(void* mem) { free(mem); } |
---|
[109] | 86 | |
---|
[973] | 87 | int size() const { return buf->used; } ///< get string length |
---|
| 88 | int length() const { return buf->used; } ///< get string length |
---|
[793] | 89 | void shrink(); ///< free unnecessary buffer |
---|
[973] | 90 | void reserve(int needed) { detachCopy(needed); } ///< like in std::string |
---|
[109] | 91 | |
---|
[793] | 92 | /// after this call, you can modify sstring directly. |
---|
| 93 | /// returned value is the pointer to the internal buffer. |
---|
| 94 | /// <B>ensuresize</B> is minimal value of bytes you need, |
---|
| 95 | /// the buffer will be resized as needed. |
---|
| 96 | /// all "direct" operations have to leave the buffer with trailing '\0' |
---|
| 97 | /// at the end. endWrite() will search for this value in order to determine |
---|
| 98 | /// new string length. |
---|
| 99 | /// <P>Sample:<CODE> |
---|
| 100 | /// SString t; |
---|
| 101 | /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); |
---|
| 102 | /// t.endWrite();</CODE> |
---|
| 103 | char *directWrite(int ensuresize = -1); |
---|
| 104 | //char *directWrite(); |
---|
| 105 | /// like directWrite, but it returns the pointer to the first char after current string |
---|
| 106 | /// for easy appending. <B>maxappend</B> is minimum of character in buffer |
---|
| 107 | /// that can be appended after this call. |
---|
| 108 | /// <P>Sample:<CODE> |
---|
| 109 | /// SString t; |
---|
| 110 | /// sprintf(t.directAppend(10),"c=%d",c); |
---|
| 111 | /// t.endAppend();</CODE> |
---|
| 112 | char *directAppend(int maxappend = 0); |
---|
| 113 | /// update string length, after directWrite. |
---|
| 114 | /// you don't have to to call endWrite after directWrite if the string's length doesn't change. |
---|
| 115 | /// optional <B>newlength</B> parameter gives a chance to further optimize |
---|
| 116 | /// this operation if you know exact length of resulting string. |
---|
| 117 | /// <P>Sample:<CODE> |
---|
| 118 | /// SString t("samplestring"); |
---|
| 119 | /// strncpy(t.directWrite(50),src,bytecount); |
---|
| 120 | /// t.endWrite(bytecount);</CODE> |
---|
| 121 | void endWrite(int newlength = -1); |
---|
| 122 | /// update string length, after directAppend. |
---|
| 123 | /// you will usually need to call endAppend (or endWrite) after directAppend, |
---|
| 124 | /// because the purpose of directAppend is to change string's length. |
---|
| 125 | /// optional <B>appendlength</B> parameter gives a chance to further optimize |
---|
| 126 | /// this operation if you know exact length of the appended string. |
---|
| 127 | /// <P>Sample:<CODE> |
---|
| 128 | /// SString t("samplestring"); |
---|
| 129 | /// strncpy(t.directAppend(50),src,bytecount); |
---|
| 130 | /// t.endAppend(bytecount);</CODE> |
---|
| 131 | void endAppend(int appendlength = -1); |
---|
[973] | 132 | int capacity() { return buf->size; } ///< std::string.capacity() |
---|
[109] | 133 | |
---|
[793] | 134 | /// find a character in SString. |
---|
| 135 | /// return index if the character was found or -1 otherwise. |
---|
| 136 | int indexOf(int character, int start = 0) const; |
---|
[109] | 137 | |
---|
[793] | 138 | /// find a substring. |
---|
| 139 | /// return index if the substring was found or -1 otherwise. |
---|
| 140 | int indexOf(const char *substring, int start = 0) const; |
---|
[109] | 141 | |
---|
[793] | 142 | /// find a substring. |
---|
| 143 | /// return index if the substring was found or -1 otherwise. |
---|
| 144 | int indexOf(const SString & substring, int start = 0) const; |
---|
[109] | 145 | |
---|
[793] | 146 | const char* c_str() const { return buf->txt; } ///< get SString's readonly buffer |
---|
| 147 | //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer |
---|
| 148 | void operator=(const char*t); ///< assign from const char* |
---|
| 149 | //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one |
---|
| 150 | void operator=(const SString &s); |
---|
[109] | 151 | |
---|
[793] | 152 | void append(const char *txt, int count); |
---|
| 153 | SString operator+(const SString &s) const; |
---|
| 154 | void operator+=(int x); ///< append x spaces after current string |
---|
| 155 | void operator+=(const char*); ///< append char* contents |
---|
| 156 | void operator+=(const SString&); ///< append other SString |
---|
[109] | 157 | |
---|
[793] | 158 | bool equals(const SString &s) const; ///< TRUE if equal |
---|
| 159 | bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal |
---|
| 160 | bool operator!=(const SString &s) const { return !equals(s); } |
---|
| 161 | const char* operator()(int p) const { return buf->txt + p; } ///< pointer to p'th character in SString |
---|
| 162 | char operator[](int i) const { return buf->txt[i]; } ///< get char like in array |
---|
[109] | 163 | |
---|
[793] | 164 | /// return a substring of the current string |
---|
| 165 | SString substr(int begin, int length = 1 << 30) const; |
---|
[109] | 166 | |
---|
[793] | 167 | /// simple tokenization: |
---|
| 168 | /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character |
---|
| 169 | /// and put it in output parameter <B>token</B>. |
---|
| 170 | /// <B>pos</B> is moved accordingly. |
---|
| 171 | /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. |
---|
| 172 | bool getNextToken(int& pos, SString &token, char separator) const; |
---|
[109] | 173 | |
---|
[793] | 174 | void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character |
---|
[109] | 175 | |
---|
[793] | 176 | bool startsWith(const char *pattern) const; |
---|
| 177 | char charAt(int pos) const { return buf->txt[pos]; } |
---|
| 178 | uint32_t hash() const { return buf->hash(); } |
---|
[109] | 179 | |
---|
[793] | 180 | static SString valueOf(int); |
---|
| 181 | static SString valueOf(long); |
---|
| 182 | static SString valueOf(double); |
---|
| 183 | static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
| 184 | static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
| 185 | static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
| 186 | static SString sprintf(const char* format, ...); |
---|
[109] | 187 | |
---|
[793] | 188 | static SString &empty(); |
---|
[109] | 189 | }; |
---|
| 190 | |
---|
| 191 | #endif //#ifdef SSTRING_SIMPLE |
---|
| 192 | |
---|
| 193 | #endif |
---|