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

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.62    ! paf         8:        $Id: pa_string.h,v 1.61 2001/04/02 15:59:35 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) \
1.62    ! paf        48:        APPEND(src, size, String::UL_CLEAN, 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) \
1.62    ! paf        51:        APPEND(src, size, String::UL_TAINTED, 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.62    ! paf        55: class Array;
        !            56: 
1.42      paf        57: /** 
1.41      paf        58:        Pooled string.
                     59: 
1.47      paf        60:        Internal structure:
                     61:        @verbatim
                     62:                String                          Chunk0
                     63:                ======                          ========
                     64:                head--------------->[ptr, size, ...]
                     65:                append_here-------->[ptr, size, ...]
                     66:                                                        .
                     67:                                                        .
                     68:                                                        [ptr, size, ...]
                     69:                link_row----------->[link to the next chunk]
1.41      paf        70:        @endverbatim
                     71: 
                     72:        All pieces remember 
                     73:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     74:        - whether they are tainted or not, 
                     75:          and the language which should be used to detaint them
                     76: */
1.15      paf        77: class String : public Pooled {
1.1       paf        78: public:
1.48      paf        79: 
1.1       paf        80:        enum {
1.41      paf        81:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     82:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        83:        };
                     84: 
1.41      paf        85:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        86:        enum Untaint_lang {
1.60      paf        87:                UL_UNKNOWN=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.62    ! paf        88:                UL_CLEAN, ///< clean
        !            89:                UL_TAINTED,  ///< tainted, untaint language as assigned later 
1.27      paf        90:                // untaint languages. assigned by ^untaint[lang]{...}
1.48      paf        91:                UL_PASS_APPENDED,
1.41      paf        92:                        /**<
                     93:                                leave language built into string being appended.
                     94:                                just a flag, that value not stored
                     95:                        */
1.48      paf        96:                UL_AS_IS,     ///< leave all characters intact
                     97:                UL_FILE_NAME, ///< filename
                     98:                UL_HEADER,    ///< text in response header
                     99:                UL_URI,       ///< text in uri
                    100:                UL_TABLE,     ///< ^table:set body
                    101:                UL_SQL,       ///< ^table:sql body
                    102:                UL_JS,        ///< JavaScript code
                    103:                UL_HTML,      ///< HTML code (for editing)
                    104:                UL_HTML_TYPO  ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf       105:        };
                    106: 
1.8       paf       107: public:
                    108: 
1.37      paf       109:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf       110:        String(const String& src);
                    111:        size_t size() const { return fsize; }
1.41      paf       112:        /// convert to C string
1.50      paf       113:        char *cstr() const {
                    114:                char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
                    115:                char *eol=store_to(result);
                    116:                *eol=0;
                    117:                return result;
                    118:        }
1.46      paf       119:        /** append fragment
1.54      paf       120:                @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46      paf       121:        */
1.9       paf       122:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       123:        /// @return <0 ==0 or >0 depending on comparison result
1.62    ! paf       124:        int cmp (int& partial, const String& src, 
        !           125:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.56      paf       126:        bool operator < (const String& src) const {     int p; return cmp(p, src)<0; }
                    127:        bool operator > (const String& src) const {     int p; return cmp(p, src)>0; }
                    128:        bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
                    129:        bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26      paf       130:        bool operator == (const String& src) const {
                    131:                if(size()!=src.size()) // can speed up in trivial case
                    132:                        return false;
1.56      paf       133:                int p; return cmp(p, src)==0;
1.26      paf       134:        }
1.56      paf       135:        bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26      paf       136: 
1.50      paf       137:        /**
                    138:                 @param partial 
                    139:                        returns partial match status. 
1.51      paf       140:                        - -1: strings too different
                    141:                        -  0: full match
                    142:                        -  1: means @c this starts @c src
                    143:                        -  2: means @src starts @this
1.50      paf       144:        */
1.61      paf       145:        int cmp(int& partial, const char* src_ptr, size_t src_size=0, 
1.62    ! paf       146:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.50      paf       147:        bool operator == (const char* src_ptr) const { 
                    148:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    149:                if(size() != src_size)
                    150:                        return false;
                    151:                int partial; // unused
1.56      paf       152:                return cmp(partial, src_ptr, src_size)==0; 
1.50      paf       153:        }
                    154: 
1.42      paf       155:        /** 
                    156:                appends other String.
1.41      paf       157: 
1.47      paf       158:                marking all tainted pieces of it with @a lang.
                    159:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       160:        */
1.39      paf       161:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       162: 
1.41      paf       163:        /// simple hash code of string. used by Hash
1.14      paf       164:        uint hash_code() const;
1.54      paf       165: 
                    166:        /// extracts [start, finish) piece of string
                    167:        String& piece(size_t start, size_t finish) const;
1.55      paf       168: 
1.59      paf       169:        /// @return position of substr in string, -1 means "not found" [String version]
1.62    ! paf       170:        int pos(const String& substr, 
        !           171:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.59      paf       172:        /// @return position of substr in string, -1 means "not found" [const char* version]
1.62    ! paf       173:        int pos(const char *substr, size_t substr_size, 
        !           174:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
        !           175: 
        !           176:        void split(Array& result, 
        !           177:                size_t *pos_after_ref, 
        !           178:                const char *delim, size_t delim_size, 
        !           179:                Untaint_lang lang, int limit=-1) const;
        !           180:        void split(Array& result, 
        !           181:                size_t *pos_after_ref, 
        !           182:                const String& delim, 
        !           183:                Untaint_lang lang, int limit=-1) const;
        !           184: 
1.13      paf       185: 
1.41      paf       186: #ifndef NO_STRING_ORIGIN
                    187:        /// origin of string. calculated by first row
1.43      paf       188:        const Origin& origin() const;
1.41      paf       189: #endif
1.8       paf       190: 
1.10      paf       191: private:
                    192: 
1.1       paf       193:        struct Chunk {
1.6       paf       194:                // the number of rows in chunk
1.56      paf       195:                size_t count;
1.1       paf       196:                union Row {
1.27      paf       197:                        // fragment
                    198:                        struct { 
                    199:                                const char *ptr;  // pointer to the start
                    200:                                size_t size;  // length
                    201:                                Untaint_lang lang; // untaint flag, later untaint language
                    202: #ifndef NO_STRING_ORIGIN
                    203:                                Origin origin;  // origin
                    204: #endif
1.1       paf       205:                        } item;
                    206:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       207:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       208:                // next rows are here
                    209:                Chunk *preallocated_link;
                    210:        }
                    211:                head;  // the head chunk of the chunk chain
                    212: 
                    213:        // next append would write to this record
                    214:        Chunk::Row *append_here;
                    215:        
                    216:        // the address of place where lies address 
                    217:        // of the link to the next chunk to allocate
                    218:        Chunk::Row *link_row;
                    219: 
1.5       paf       220: private:
1.25      paf       221:        // last chunk
                    222:        Chunk *last_chunk;
1.5       paf       223: 
                    224:        // string size
                    225:        size_t fsize;
                    226: 
                    227:        // used rows in all chunks
                    228:        int fused_rows;
                    229: 
                    230: private:
1.1       paf       231: 
                    232:        bool chunk_is_full() {
                    233:                return append_here == link_row;
                    234:        }
                    235:        void expand();
1.50      paf       236:        char *String::store_to(char *dest) const;
1.7       paf       237: 
                    238: private: //disabled
                    239: 
1.12      paf       240:        String& operator = (const String&) { return *this; }
1.7       paf       241: 
1.1       paf       242: };
                    243: 
                    244: #endif

E-mail: