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

Last change on this file since 385 was 385, checked in by sz, 8 years ago

Switch to a different, multithreading-friendly SString implementation (not using copy-on-write)

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