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