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

Last change on this file since 198 was 198, checked in by Maciej Komosinski, 10 years ago

Hashing function for strings

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