Ignore:
Timestamp:
05/29/18 16:51:14 (6 years ago)
Author:
Maciej Komosinski
Message:

Code formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstring.h

    r395 r793  
    3232class SBuf
    3333{
    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) {}
     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) {}
    4747public:
    48 SBuf();
    49 ~SBuf();
    50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
     48        SBuf();
     49        ~SBuf();
     50        uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
    5151};
    5252
     
    5656{
    5757private:
    58 SBuf *buf;      ///< buffer
    59 int appending;  ///< append mode, changes can occur after character # 'appending'
    60 //int memhint;
     58        SBuf *buf;      ///< buffer
     59        int appending;  ///< append mode, changes can occur after character # 'appending'
     60        //int memhint;
    6161
    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
     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
    6868
    6969public:
    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();
     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();
    7676
    77 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
     77        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
    7878
    79 void* operator new(size_t s, void* mem) {return mem;}
     79        void* operator new(size_t s, void* mem){ return mem; }
    8080#ifdef _MSC_VER
    81 void operator delete(void* mem, void* t) {}
     81                void operator delete(void* mem, void* t) {}
    8282#endif
    83 void* operator new(size_t s) {return malloc(sizeof(SString));}
    84 void operator delete(void* mem) {free(mem);}
     83        void* operator new(size_t s){ return malloc(sizeof(SString)); }
     84        void operator delete(void* mem) { free(mem); }
    8585
    86 int len() const {return buf->used;} ///< get string length
    87 void shrink(); ///< free unnecessary buffer
     86        int len() const { return buf->used; } ///< get string length
     87        void shrink(); ///< free unnecessary buffer
    8888
    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)
     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)
    134134
    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;
     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;
    138138
    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;
     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;
    142142
    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;
     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;
    146146
    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);
     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);
    152152
    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
     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
    158158
    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
     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
    164164
    165 /// return a substring of the current string
    166 SString substr(int begin, int length=1<<30) const;
     165        /// return a substring of the current string
     166        SString substr(int begin, int length = 1 << 30) const;
    167167
    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;
     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;
    174174
    175 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
     175        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
    176176
    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();}
     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(); }
    180180
    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, ...);
     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, ...);
    188188
    189 static SString &empty();
     189        static SString &empty();
    190190};
    191191
Note: See TracChangeset for help on using the changeset viewer.