source: cpp/frams/util/sstring.h @ 1000

Last change on this file since 1000 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: 7.4 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _SSTRING_H_
6#define _SSTRING_H_
7
8#define SSTRING_SIMPLE
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
24#include <stdint.h>
25#include <string.h>
26#include <stdlib.h>
27#include <stdio.h>
28
29class ExtValue;  //this include would result in recurrent inclusion: #include "extvalue.h"
30class ExtObject;
31
32class SBuf
33{
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) {}
47public:
48        SBuf();
49        ~SBuf();
50        uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
51};
52
53/// (not so) simple text string class
54
55class SString
56{
57private:
58        SBuf *buf;      ///< buffer
59        int appending;  ///< append mode, changes can occur after character # 'appending'
60        //int memhint;
61
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
68
69public:
70        SString(); ///< make an empty string
71        SString(const char*t, int t_len = -1); ///< make a string from char*
72        SString(int x) = delete; ///< disallow the former 'int' constructor (so the new 'char' version is not used through implicit conversion)
73        SString(const SString& from); ///< duplicate string
74        SString(SString&& from); ///< move
75        SString(char in);
76        ~SString();
77
78        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
79
80        void* operator new(size_t s, void* mem) { return mem; }
81#ifdef _MSC_VER
82        void operator delete(void* mem, void* t) {}
83#endif
84        void* operator new(size_t s) { return malloc(sizeof(SString)); }
85        void operator delete(void* mem) { free(mem); }
86
87        int size() const { return buf->used; } ///< get string length
88        int length() const { return buf->used; } ///< get string length
89        void shrink(); ///< free unnecessary buffer
90        void reserve(int needed) { detachCopy(needed); } ///< like in std::string
91
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);
132        int capacity() { return buf->size; } ///< std::string.capacity()
133
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;
137
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;
141
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;
145
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);
151
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
157
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
163
164        /// return a substring of the current string
165        SString substr(int begin, int length = 1 << 30) const;
166
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;
173
174        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
175
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(); }
179
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, ...);
187
188        static SString &empty();
189};
190
191#endif //#ifdef SSTRING_SIMPLE
192
193#endif
Note: See TracBrowser for help on using the repository browser.