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

Last change on this file since 918 was 889, checked in by Maciej Komosinski, 5 years ago

Code formatting

  • Property svn:eol-style set to native
File size: 7.4 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
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
29class ExtValue;  //this include would result in recurrent inclusion: #include "extvalue.h"
30class ExtObject;
31
32class 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]47public:
[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
55class SString
56{
57private:
[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
69public:
[793]70        SString(); ///< make an empty string
71        SString(const char*t, int t_len = -1); ///< make a string from char*
72        SString(int x); ///< string with initial buffer size
73        SString(const SString& from); ///< duplicate string
74        SString(SString&& from); ///< move
75        ~SString();
[109]76
[793]77        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
[109]78
[889]79        void* operator new(size_t s, void* mem) { return mem; }
[109]80#ifdef _MSC_VER
[889]81        void operator delete(void* mem, void* t) {}
[109]82#endif
[889]83        void* operator new(size_t s) { return malloc(sizeof(SString)); }
[793]84        void operator delete(void* mem) { free(mem); }
[109]85
[793]86        int len() const { return buf->used; } ///< get string length
87        void shrink(); ///< free unnecessary buffer
[109]88
[793]89        /// after this call, you can modify sstring directly.
90        /// returned value is the pointer to the internal buffer.
91        /// <B>ensuresize</B> is minimal value of bytes you need,
92        /// the buffer will be resized as needed.
93        /// all "direct" operations have to leave the buffer with trailing '\0'
94        /// at the end. endWrite() will search for this value in order to determine
95        /// new string length.
96        /// <P>Sample:<CODE>
97        /// SString t;
98        /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);
99        /// t.endWrite();</CODE>
100        char *directWrite(int ensuresize = -1);
101        //char *directWrite();
102        /// like directWrite, but it returns the pointer to the first char after current string
103        /// for easy appending. <B>maxappend</B> is minimum of character in buffer
104        /// that can be appended after this call.
105        /// <P>Sample:<CODE>
106        /// SString t;
107        /// sprintf(t.directAppend(10),"c=%d",c);
108        /// t.endAppend();</CODE>
109        char *directAppend(int maxappend = 0);
110        /// update string length, after directWrite.
111        /// you don't have to to call endWrite after directWrite if the string's length doesn't change.
112        /// optional <B>newlength</B> parameter gives a chance to further optimize
113        /// this operation if you know exact length of resulting string.
114        /// <P>Sample:<CODE>
115        /// SString t("samplestring");
116        /// strncpy(t.directWrite(50),src,bytecount);
117        /// t.endWrite(bytecount);</CODE>
118        void endWrite(int newlength = -1);
119        /// update string length, after directAppend.
120        /// you will usually need to call endAppend (or endWrite) after directAppend,
121        /// because the purpose of directAppend is to change string's length.
122        /// optional <B>appendlength</B> parameter gives a chance to further optimize
123        /// this operation if you know exact length of the appended string.
124        /// <P>Sample:<CODE>
125        /// SString t("samplestring");
126        /// strncpy(t.directAppend(50),src,bytecount);
127        /// t.endAppend(bytecount);</CODE>
128        void endAppend(int appendlength = -1);
129        /// argument is the amount of memory, that will be probably used
130        /// by this string instance. string can use this value
131        /// to optimize memory allocation (bigger chunks will be allocated).
132        void memoryHint(int howbig);
133        int directMaxLen() { return buf->size; } ///< when called after directWrite: max number of characters allowed (can be more than requested)
[109]134
[793]135        /// find a character in SString.
136        /// return index if the character was found or -1 otherwise.
137        int indexOf(int character, int start = 0) const;
[109]138
[793]139        /// find a substring.
140        /// return index if the substring was found or -1 otherwise.
141        int indexOf(const char *substring, int start = 0) const;
[109]142
[793]143        /// find a substring.
144        /// return index if the substring was found or -1 otherwise.
145        int indexOf(const SString & substring, int start = 0) const;
[109]146
[793]147        const char* c_str() const { return buf->txt; } ///< get SString's readonly buffer
148        //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer
149        void operator=(const char*t); ///< assign from const char*
150        //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one
151        void operator=(const SString &s);
[109]152
[793]153        void append(const char *txt, int count);
154        SString operator+(const SString &s) const;
155        void operator+=(int x); ///< append x spaces after current string
156        void operator+=(const char*); ///< append char* contents
157        void operator+=(const SString&); ///< append other SString
[109]158
[793]159        bool equals(const SString &s) const; ///< TRUE if equal
160        bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal
161        bool operator!=(const SString &s) const { return !equals(s); }
162        const char* operator()(int p) const { return buf->txt + p; } ///< pointer to p'th character in SString
163        char operator[](int i) const { return buf->txt[i]; } ///< get char like in array
[109]164
[793]165        /// return a substring of the current string
166        SString substr(int begin, int length = 1 << 30) const;
[109]167
[793]168        /// simple tokenization:
169        /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character
170        /// and put it in output parameter <B>token</B>.
171        /// <B>pos</B> is moved accordingly.
172        /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.
173        bool getNextToken(int& pos, SString &token, char separator) const;
[109]174
[793]175        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
[109]176
[793]177        bool startsWith(const char *pattern) const;
178        char charAt(int pos) const { return buf->txt[pos]; }
179        uint32_t hash() const { return buf->hash(); }
[109]180
[793]181        static SString valueOf(int);
182        static SString valueOf(long);
183        static SString valueOf(double);
184        static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
185        static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
186        static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu
187        static SString sprintf(const char* format, ...);
[109]188
[793]189        static SString &empty();
[109]190};
191
192#endif //#ifdef SSTRING_SIMPLE
193
194#endif
Note: See TracBrowser for help on using the repository browser.