Annotation of parser3/src/include/pa_string.h, revision 1.61

1.41      paf         1: /** @file
1.43      paf         2:        Parser: string class decl.
                      3: 
1.29      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.43      paf         5: 
1.30      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29      paf         7: 
1.61    ! paf         8:        $Id: pa_string.h,v 1.60 2001/04/02 09:29:17 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_STRING_H
                     12: #define PA_STRING_H
                     13: 
1.49      paf        14: #include "pa_config_includes.h"
1.9       paf        15: 
1.1       paf        16: #include <stddef.h>
                     17: 
1.15      paf        18: #include "pa_pool.h"
1.4       paf        19: #include "pa_types.h"
                     20: 
1.50      paf        21: /**
                     22:        $MAIN:html-typo table elements must enlarge string not more that that
                     23:        that's a tradeoff - otherwise we'd have to scan string twice:
                     24:        - first for buffer length
                     25:        - second for replacements themselves
                     26: */
1.31      paf        27: #define UNTAINT_TIMES_BIGGER 10
                     28: 
1.9       paf        29: #ifndef NO_STRING_ORIGIN
1.33      paf        30: #      define STRING_APPEND_PARAMS \
                     31:                const char *src, size_t size,  \
1.52      paf        32:                String::Untaint_lang lang, \
1.33      paf        33:                const char *file, uint line
1.54      paf        34: /// appends piece to String  @see String::real_append
                     35: #      define APPEND(src, size, lang, file, line) \
                     36:                real_append(src, size, lang, file, line)
1.9       paf        37: #else
1.33      paf        38: #      define STRING_APPEND_PARAMS \
                     39:                const char *src, \
                     40:                size_t size, \
1.52      paf        41:                String::Untaint_lang lang
1.54      paf        42: /// appends piece to String  @see String::real_append
                     43: #      define APPEND(src, size, lang, file, line) \
                     44:                real_append(src, size, lang)
                     45: #endif
1.48      paf        46: /// appends clean piece to String  @see String::real_append
1.54      paf        47: #define APPEND_CLEAN(src, size, file, line) \
                     48:        APPEND(src, size, String::UL_NO, file, line)
1.48      paf        49: /// appends tainted piece to String  @see String::real_append
1.54      paf        50: #define APPEND_TAINTED(src, size, file, line) \
                     51:        APPEND(src, size, String::UL_YES, file, line)
1.48      paf        52: /// handy: appends const char* piece to String  @see String::real_append
1.54      paf        53: #define        APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0)
1.9       paf        54: 
1.42      paf        55: /** 
1.41      paf        56:        Pooled string.
                     57: 
1.47      paf        58:        Internal structure:
                     59:        @verbatim
                     60:                String                          Chunk0
                     61:                ======                          ========
                     62:                head--------------->[ptr, size, ...]
                     63:                append_here-------->[ptr, size, ...]
                     64:                                                        .
                     65:                                                        .
                     66:                                                        [ptr, size, ...]
                     67:                link_row----------->[link to the next chunk]
1.41      paf        68:        @endverbatim
                     69: 
                     70:        All pieces remember 
                     71:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     72:        - whether they are tainted or not, 
                     73:          and the language which should be used to detaint them
                     74: */
1.15      paf        75: class String : public Pooled {
1.1       paf        76: public:
1.48      paf        77: 
1.1       paf        78:        enum {
1.41      paf        79:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     80:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        81:        };
                     82: 
1.41      paf        83:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        84:        enum Untaint_lang {
1.60      paf        85:                UL_UNKNOWN=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.48      paf        86:                UL_NO, ///< clean
                     87:                UL_YES,  ///< tainted, untaint language as assigned later 
1.27      paf        88:                // untaint languages. assigned by ^untaint[lang]{...}
1.48      paf        89:                UL_PASS_APPENDED,
1.41      paf        90:                        /**<
                     91:                                leave language built into string being appended.
                     92:                                just a flag, that value not stored
                     93:                        */
1.48      paf        94:                UL_AS_IS,     ///< leave all characters intact
                     95:                UL_FILE_NAME, ///< filename
                     96:                UL_HEADER,    ///< text in response header
                     97:                UL_URI,       ///< text in uri
                     98:                UL_TABLE,     ///< ^table:set body
                     99:                UL_SQL,       ///< ^table:sql body
                    100:                UL_JS,        ///< JavaScript code
                    101:                UL_HTML,      ///< HTML code (for editing)
                    102:                UL_HTML_TYPO  ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf       103:        };
                    104: 
1.8       paf       105: public:
                    106: 
1.37      paf       107:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf       108:        String(const String& src);
                    109:        size_t size() const { return fsize; }
1.41      paf       110:        /// convert to C string
1.50      paf       111:        char *cstr() const {
                    112:                char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
                    113:                char *eol=store_to(result);
                    114:                *eol=0;
                    115:                return result;
                    116:        }
1.46      paf       117:        /** append fragment
1.54      paf       118:                @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46      paf       119:        */
1.9       paf       120:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       121:        /// @return <0 ==0 or >0 depending on comparison result
1.56      paf       122:        int cmp (int& partial, const String& src, size_t this_offset=0) const;
                    123:        bool operator < (const String& src) const {     int p; return cmp(p, src)<0; }
                    124:        bool operator > (const String& src) const {     int p; return cmp(p, src)>0; }
                    125:        bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
                    126:        bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26      paf       127:        bool operator == (const String& src) const {
                    128:                if(size()!=src.size()) // can speed up in trivial case
                    129:                        return false;
1.56      paf       130:                int p; return cmp(p, src)==0;
1.26      paf       131:        }
1.56      paf       132:        bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26      paf       133: 
1.50      paf       134:        /**
                    135:                 @param partial 
                    136:                        returns partial match status. 
1.51      paf       137:                        - -1: strings too different
                    138:                        -  0: full match
                    139:                        -  1: means @c this starts @c src
                    140:                        -  2: means @src starts @this
1.50      paf       141:        */
1.61    ! paf       142:        int cmp(int& partial, const char* src_ptr, size_t src_size=0, 
        !           143:                size_t this_offset=0) const;
1.50      paf       144:        bool operator == (const char* src_ptr) const { 
                    145:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    146:                if(size() != src_size)
                    147:                        return false;
                    148:                int partial; // unused
1.56      paf       149:                return cmp(partial, src_ptr, src_size)==0; 
1.50      paf       150:        }
                    151: 
1.42      paf       152:        /** 
                    153:                appends other String.
1.41      paf       154: 
1.47      paf       155:                marking all tainted pieces of it with @a lang.
                    156:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       157:        */
1.39      paf       158:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       159: 
1.41      paf       160:        /// simple hash code of string. used by Hash
1.14      paf       161:        uint hash_code() const;
1.54      paf       162: 
                    163:        /// extracts [start, finish) piece of string
                    164:        String& piece(size_t start, size_t finish) const;
1.55      paf       165: 
1.59      paf       166:        /// @return position of substr in string, -1 means "not found" [String version]
1.58      paf       167:        int pos(const String& substr, size_t this_offset=0) const;
1.59      paf       168:        /// @return position of substr in string, -1 means "not found" [const char* version]
1.61    ! paf       169:        int pos(const char *substr, size_t result) const;
1.13      paf       170: 
1.41      paf       171: #ifndef NO_STRING_ORIGIN
                    172:        /// origin of string. calculated by first row
1.43      paf       173:        const Origin& origin() const;
1.41      paf       174: #endif
1.8       paf       175: 
1.10      paf       176: private:
                    177: 
1.1       paf       178:        struct Chunk {
1.6       paf       179:                // the number of rows in chunk
1.56      paf       180:                size_t count;
1.1       paf       181:                union Row {
1.27      paf       182:                        // fragment
                    183:                        struct { 
                    184:                                const char *ptr;  // pointer to the start
                    185:                                size_t size;  // length
                    186:                                Untaint_lang lang; // untaint flag, later untaint language
                    187: #ifndef NO_STRING_ORIGIN
                    188:                                Origin origin;  // origin
                    189: #endif
1.1       paf       190:                        } item;
                    191:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       192:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       193:                // next rows are here
                    194:                Chunk *preallocated_link;
                    195:        }
                    196:                head;  // the head chunk of the chunk chain
                    197: 
                    198:        // next append would write to this record
                    199:        Chunk::Row *append_here;
                    200:        
                    201:        // the address of place where lies address 
                    202:        // of the link to the next chunk to allocate
                    203:        Chunk::Row *link_row;
                    204: 
1.5       paf       205: private:
1.25      paf       206:        // last chunk
                    207:        Chunk *last_chunk;
1.5       paf       208: 
                    209:        // string size
                    210:        size_t fsize;
                    211: 
                    212:        // used rows in all chunks
                    213:        int fused_rows;
                    214: 
                    215: private:
1.1       paf       216: 
                    217:        bool chunk_is_full() {
                    218:                return append_here == link_row;
                    219:        }
                    220:        void expand();
1.39      paf       221:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.50      paf       222:        char *String::store_to(char *dest) const;
1.7       paf       223: 
                    224: private: //disabled
                    225: 
1.12      paf       226:        String& operator = (const String&) { return *this; }
1.7       paf       227: 
1.1       paf       228: };
                    229: 
                    230: #endif

E-mail: