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

1.41      paf         1: /** @file
1.43      paf         2:        Parser: string class decl.
                      3: 
1.234     moko        4:        Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.229     moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_STRING_H
                      9: #define PA_STRING_H
1.140     paf        10: 
1.240   ! moko       11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.239 2026/01/06 13:27:59 moko Exp $"
1.145     paf        12: 
                     13: // includes
1.4       paf        14: #include "pa_types.h"
1.239     moko       15: #include "pa_int.h"
1.145     paf        16: #include "pa_array.h"
                     17: 
                     18: extern "C" { // cord's author forgot to do that
                     19: #define CORD_NO_IO
1.224     moko       20: #include "../lib/cord/include/cord.h"
1.186     misha      21: 
                     22: #ifdef CORD_CAT_OPTIMIZATION
                     23: #define CORD_cat(x, y) CORD_cat_optimized(x, y)
                     24: #define CORD_cat_char_star(x, y, leny) CORD_cat_char_star_optimized(x, y, leny)
                     25: #endif
1.209     moko       26: }
1.4       paf        27: 
1.164     paf        28: // defines
                     29: 
1.183     misha      30: // cache hash code in String::Body for faster hash access
                     31: #define HASH_CODE_CACHING
                     32: 
1.187     misha      33: // cache String::Body.length() for char* strings only, CORDs have own length
                     34: #define STRING_LENGTH_CACHING
                     35: 
                     36: 
1.146     paf        37: // cord extension
                     38: /* Returns true if x does contain                                       */
                     39: /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x).  */
                     40: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c);
1.148     paf        41: size_t CORD_block_count(CORD x);
1.146     paf        42: 
1.145     paf        43: // forwards
1.9       paf        44: 
1.145     paf        45: class Charset;
1.135     paf        46: class Table;
1.71      paf        47: class SQL_Connection;
1.101     parser     48: class Dictionary;
1.145     paf        49: class Request_charsets;
                     50: class String;
1.231     moko       51: class VRegex;
                     52: 
                     53: #ifdef NDEBUG
1.145     paf        54: typedef Array<const String*> ArrayString;
1.231     moko       55: #else
                     56: class ArrayString : public Array<const String*> {
                     57: public:
                     58:        inline ArrayString(size_t initial=0) : Array(initial){
                     59:        }
1.232     moko       60:        inline Array& operator+=(element_type src) {
1.231     moko       61:                assert(src != NULL);
                     62:                return Array::operator+=(src);
                     63:        }
1.232     moko       64: 
                     65:        inline element_type get(size_t index) const {
                     66:                element_type result=Array::get(index);
                     67:                assert(result != NULL);
                     68:                return result;
                     69:        }
1.231     moko       70: };
                     71: #endif
1.145     paf        72: 
1.155     paf        73: // generally useful
                     74: 
1.145     paf        75: /// this is result of pos functions which mean that substr were not found
                     76: #define STRING_NOT_FOUND ((size_t)-1)
                     77: 
1.211     moko       78: /// CORD can't be empty string, thus checking it in assigment
                     79: #define AS_CORD(v) ((v) && *(v) ? (CORD)(v):0)
                     80: 
1.42      paf        81: /** 
1.146     paf        82:        String which knows the lang of all it's langs.
1.41      paf        83: 
                     84:        All pieces remember 
                     85:        - whether they are tainted or not, 
1.146     paf        86:          and the lang which should be used to detaint them
1.41      paf        87: */
1.176     misha      88: 
                     89: 
1.145     paf        90: class String: public PA_Object {
1.218     moko       91:        friend class StringSplitHelper;
1.1       paf        92: public:
1.48      paf        93: 
1.146     paf        94:        /** piece is tainted or not. the lang to use when detaint
1.106     parser     95:                remember to change String_Untaint_lang_name @ untaint.C along
1.148     paf        96: 
                     97:                WARNING WARNING WARNING WARNING WARNING WARNING 
                     98:                
                     99:                pos function compares(<=) languages, that is used in searching
                    100:                for table column separator being L_CLEAN or L_AS_IS.
                    101:                they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
                    102:                
                    103:                letters assigned for debugging, but it's important for no language-letter
                    104:                come before L_AS_IS other then L_CLEAN
                    105: 
                    106:                WARNING WARNING WARNING WARNING WARNING WARNING 
1.106     parser    107:        */
1.225     moko      108: 
                    109: #if _MSC_VER >= 1900
                    110:        /// required for VS2015+ to make sizeof(Languages::opt) == sizeof(CORD), will be 16 byte under x64 without it
                    111:        enum Language : size_t {
                    112: #else
1.145     paf       113:        enum Language {
1.225     moko      114: #endif
                    115:                L_UNSPECIFIED=0,        ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145     paf       116:                // these two must go before others, there are checks for >L_AS_IS
1.225     moko      117:                L_CLEAN='0',            ///< clean  WARNING: read above warning before changing
                    118:                L_AS_IS='A',            ///< leave all characters intact  WARNING: read above warning before changing
1.122     paf       119: 
1.225     moko      120:                L_TAINTED='T',          ///< tainted, untaint lang as assigned later
1.146     paf       121:                // untaint langs. assigned by ^untaint[lang]{...}
1.170     misha     122:                L_FILE_SPEC='F',        ///< file specification
                    123:                L_HTTP_HEADER='h',      ///< text in HTTP response header
                    124:                L_MAIL_HEADER='m',      ///< text in mail header
1.225     moko      125:                L_URI='U',              ///< text in uri
                    126:                L_SQL='Q',              ///< ^table:sql body
                    127:                L_JS='J',               ///< JavaScript code
                    128:                L_XML='X',              ///< ^xdoc:create xml
                    129:                L_HTML='H',             ///< HTML code
1.198     misha     130:                L_REGEX='R',            ///< RegExp
1.225     moko      131:                L_JSON='S',             ///< JSON code
1.170     misha     132:                L_HTTP_COOKIE='C',      ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode
1.192     misha     133:                L_PARSER_CODE='p',      ///< ^process body
1.148     paf       134:                // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146     paf       135:                L_OPTIMIZE_BIT = 0x80  ///< flag, requiring cstr whitespace optimization
1.27      paf       136:        };
                    137: 
1.157     paf       138:        enum Trim_kind {
                    139:                TRIM_BOTH,
                    140:                TRIM_START,
                    141:                TRIM_END
                    142:        };
                    143: 
1.187     misha     144:        class Body;
1.176     misha     145: 
1.147     paf       146:        union Languages {
1.146     paf       147: 
1.147     paf       148:                struct {
1.160     paf       149: #ifdef PA_LITTLE_ENDIAN
1.147     paf       150:                        Language lang:8;
1.179     misha     151:                        size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160     paf       152: #elif defined(PA_BIG_ENDIAN)
1.179     misha     153:                        size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160     paf       154:                        Language lang:8;
                    155: #else
                    156: #      error word endianness not determined for some obscure reason
                    157: #endif
1.147     paf       158:                } opt;
                    159:                CORD langs;
1.146     paf       160: 
1.187     misha     161:                CORD make_langs(const Body& current) const {
                    162:                        return opt.is_not_just_lang?langs:CORD_chars((char)opt.lang, current.length());
1.146     paf       163:                }
                    164: 
                    165:                CORD make_langs(size_t aoffset, size_t alength)  const {
1.147     paf       166:                        return opt.is_not_just_lang?
1.194     misha     167:                                CORD_substr(langs, aoffset, alength, 0)
1.162     paf       168:                                :CORD_chars((char)opt.lang, alength);
1.145     paf       169:                }
                    170: 
1.157     paf       171:                /// appending when 'langs' already contain something [simple cases handled elsewhere]
1.187     misha     172:                void append(size_t current, const CORD to_nonempty_target_langs) {
1.146     paf       173:                        assert(langs);
1.187     misha     174:                        if(opt.is_not_just_lang)
                    175:                                langs=CORD_cat(langs, to_nonempty_target_langs);
                    176:                        else {
                    177:                                assert(current);
                    178:                                langs=CORD_cat(CORD_chars((char)opt.lang, current), to_nonempty_target_langs);
                    179:                        }
                    180:                }
1.146     paf       181: 
1.187     misha     182:                void append(const Body& current, const CORD to_nonempty_target_langs) {
                    183:                        assert(langs);
1.147     paf       184:                        if(opt.is_not_just_lang)
1.146     paf       185:                                langs=CORD_cat(langs, to_nonempty_target_langs);
1.187     misha     186:                        else {
                    187:                                size_t current_size=current.length();
1.146     paf       188:                                assert(current_size);
1.187     misha     189:                                langs=CORD_cat(CORD_chars((char)opt.lang, current_size), to_nonempty_target_langs);
1.146     paf       190:                        }
1.145     paf       191:                }
1.146     paf       192: 
1.145     paf       193:        public:
1.146     paf       194: 
1.222     moko      195:                const char* visualize() const;
1.164     paf       196:                void dump() const;
1.146     paf       197: 
                    198:                Languages(): langs(0) {}
1.147     paf       199:                Languages(Language alang) {
                    200:                        opt.lang=alang;
                    201:                        opt.is_not_just_lang=0;
                    202:                }
1.146     paf       203: 
                    204:                /// MUST be called exactly prior to modification of current [uses it's length]
1.187     misha     205:                void append(size_t current, Language alang, size_t length) {
1.146     paf       206:                        assert(alang);
1.187     misha     207:                        assert(length);
1.146     paf       208: 
1.208     moko      209:                        if(!opt.is_not_just_lang) {
1.147     paf       210:                                if(opt.lang) {
1.181     misha     211:                                        if(opt.lang==alang) // same language? ignoring
1.146     paf       212:                                                return;
                    213:                                } else {
1.147     paf       214:                                        opt.lang=alang; // to uninitialized
1.146     paf       215:                                        return;
1.145     paf       216:                                }
1.208     moko      217:                        }
1.146     paf       218: 
1.187     misha     219:                        append(current, CORD_chars((char)alang, length));
1.146     paf       220:                }
                    221: 
1.187     misha     222:                void append(const Body &current, Language alang, size_t length) {
1.176     misha     223:                        assert(alang);
1.187     misha     224:                        assert(length);
1.176     misha     225: 
1.208     moko      226:                        if(!opt.is_not_just_lang) {
1.176     misha     227:                                if(opt.lang) {
1.183     misha     228:                                        if(opt.lang==alang) // same language? ignoring
1.176     misha     229:                                                return;
                    230:                                } else {
                    231:                                        opt.lang=alang; // to uninitialized
                    232:                                        return;
                    233:                                }
1.208     moko      234:                        }
1.176     misha     235: 
1.187     misha     236:                        append(current, CORD_chars((char)alang, length));
1.176     misha     237:                }
                    238: 
1.187     misha     239:                void appendHelper(const Body& current, Language alang, const Body &length_helper) {
                    240:                        assert(alang);
                    241: 
1.208     moko      242:                        if(!opt.is_not_just_lang) {
1.187     misha     243:                                if(opt.lang) {
                    244:                                        if(opt.lang==alang) // same language? ignoring
                    245:                                                return;
                    246:                                } else {
                    247:                                        opt.lang=alang; // to uninitialized
                    248:                                        return;
                    249:                                }
1.208     moko      250:                        }
1.187     misha     251: 
                    252:                        append(current, CORD_chars((char)alang, length_helper.length()));
                    253:                }
                    254: 
                    255:                void appendHelper(const Body& current, const Languages &src, const Body& length_helper) {
1.186     misha     256:                        if(!langs){
1.176     misha     257:                                langs=src.langs; // to uninitialized
1.186     misha     258: #ifdef CORD_CAT_OPTIMIZATION
                    259:                                if(opt.is_not_just_lang && !CORD_IS_STRING(langs))
                    260:                                        CORD_concatenation_protect(langs);
                    261: #endif
                    262:                        }
1.176     misha     263:                        else if(!src.opt.is_not_just_lang)
                    264:                                appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source
                    265:                        else
                    266:                                append(current, src.make_langs(length_helper));
                    267:                }
                    268: 
1.146     paf       269:                /// MUST be called exactly prior to modification of current [uses it's length]
1.187     misha     270:                void append(const Body& current, const Languages src, size_t aoffset, size_t alength) {
1.146     paf       271:                        assert(alength);
                    272: 
                    273:                        if(!langs) // to uninitialized?
1.147     paf       274:                                if(src.opt.is_not_just_lang)
1.194     misha     275:                                        langs=CORD_substr(src.langs, aoffset, alength, 0); // to uninitialized complex
1.146     paf       276:                                else
1.147     paf       277:                                        opt.lang=src.opt.lang; // to uninitialized simple
1.146     paf       278:                        else 
1.147     paf       279:                                if(!opt.is_not_just_lang && !src.opt.is_not_just_lang && opt.lang==src.opt.lang) // both simple & of same language?
1.146     paf       280:                                        return; // ignoring
                    281:                                else
                    282:                                        append(current, src.make_langs(aoffset, alength));
                    283:                }
                    284: 
                    285:                /// checks if we have lang<=alang all from aoffset to aoffset+alength
                    286:                bool check_lang(Language alang, size_t aoffset, size_t alength) const {
                    287:                        if(alang==L_UNSPECIFIED) // ignore lang?
                    288:                                return true;
                    289: 
1.147     paf       290:                        if(opt.is_not_just_lang)
1.146     paf       291:                                return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
                    292:                        else
1.169     misha     293:                                return (unsigned)opt.lang<=(unsigned)alang;
1.146     paf       294:                }
                    295: 
1.148     paf       296:                /// @returns count of blocks
                    297:                /// @todo currently there can be adjucent blocks of same language. someday merge them
                    298:                size_t count() const {
                    299:                        return opt.is_not_just_lang?
                    300:                                CORD_block_count(langs)
                    301:                                : opt.lang?
                    302:                                        1
                    303:                                        : 0;
                    304:                };
                    305: 
1.187     misha     306:                template<typename I> 
                    307:                void for_each(size_t current, int callback(char, size_t, I), I info) const {
                    308:                        if(opt.is_not_just_lang)
                    309:                                CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
                    310:                        else
                    311:                                callback(opt.lang, current, info);
                    312:                }
                    313: 
                    314:                template<typename I> 
                    315:                void for_each(const Body& current, int callback(char, size_t, I), I info) const {
1.147     paf       316:                        if(opt.is_not_just_lang)
1.146     paf       317:                                CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
                    318:                        else
1.187     misha     319:                                callback(opt.lang, current.length(), info);
1.146     paf       320:                }
                    321: 
1.164     paf       322:                bool invariant(size_t current_length) const {
1.146     paf       323:                        if(!langs)
                    324:                                return current_length==0;
1.147     paf       325:                        if(opt.is_not_just_lang)
1.146     paf       326:                                return CORD_len(langs)==current_length;
                    327:                        return true; // uncheckable, actually
                    328:                }
                    329:        };
                    330: 
1.213     moko      331:        struct C {
                    332:                const char *str;
                    333:                size_t length;
                    334:                C(): str(0), length(0) {}
                    335:                C(const char *astr, size_t asize): str(astr), length(asize) {}
1.221     moko      336:                explicit C(Body abody): str(abody.cstr()), length(abody.length()) {}
1.213     moko      337:        };
                    338: 
                    339:        struct Cm {
                    340:                char *str;
                    341:                size_t length;
                    342:                Cm(): str(0), length(0) {}
                    343:                Cm(char *astr, size_t asize): str(astr), length(asize) {}
1.221     moko      344:                explicit Cm(Body abody): str(abody.cstrm()), length(abody.length()) {}
1.213     moko      345:        };
                    346: 
1.146     paf       347:        class Body {
                    348: 
                    349:                CORD body;
                    350: 
1.183     misha     351: #ifdef HASH_CODE_CACHING
                    352:                // cached hash code is not reseted on write operations as test shows 
                    353:                // that string body does not change after it is stored as a hash key
                    354:                mutable uint hash_code;
1.213     moko      355: #define INIT_HASH_CODE(c) ,hash_code(c)
1.215     moko      356: #define ZERO_HASH_CODE hash_code=0;
1.213     moko      357: #else
                    358: #define INIT_HASH_CODE(c)
1.215     moko      359: #define ZERO_HASH_CODE
1.183     misha     360: #endif
                    361: 
1.187     misha     362: #ifdef STRING_LENGTH_CACHING
                    363:                // cached length is reseted on modification, used only for char*, not CORD
                    364:                mutable size_t string_length;
1.213     moko      365: #define INIT_LENGTH(l) ,string_length(l)
1.187     misha     366: #define ZERO_LENGTH string_length=0;
                    367: #else
1.213     moko      368: #define INIT_LENGTH(l)
1.187     misha     369: #define ZERO_LENGTH
                    370: #endif
                    371: 
1.146     paf       372:        public:
                    373: 
1.164     paf       374:                void dump() const;
1.146     paf       375: 
1.213     moko      376:                Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
1.237     moko      377:                explicit Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
                    378:                explicit Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {}
1.213     moko      379:                explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {}
                    380:                explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) {
1.186     misha     381: #ifdef CORD_CAT_OPTIMIZATION
                    382:                        assert(!body // no body
                    383:                                || *body // ordinary string
                    384:                                || body[1]==1 // CONCAT_HDR
                    385:                                || body[1]==3 // CONCAT_HDR_READ_ONLY
                    386:                                || body[1]==4 // FN_HDR 
                    387:                                || body[1]==6 // SUBSTR_HDR 
                    388:                                );
                    389: #else
1.146     paf       390:                        assert(!body // no body
                    391:                                || *body // ordinary string
                    392:                                || body[1]==1 // CONCAT_HDR
                    393:                                || body[1]==4 // FN_HDR 
                    394:                                || body[1]==6 // SUBSTR_HDR 
                    395:                                );
1.186     misha     396: #endif
1.146     paf       397:                }
1.181     misha     398: 
1.236     moko      399:                static Body uitoa(size_t aindex);
1.213     moko      400: 
1.215     moko      401:                void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; }
1.146     paf       402: 
                    403:                bool operator! () const { return is_empty(); }
                    404: 
1.211     moko      405:                inline CORD get_cord() const { return body; }
1.183     misha     406:                uint get_hash_code() const;
1.146     paf       407: 
1.221     moko      408:                // never null
1.197     misha     409:                const char* cstr() const {
                    410: #ifdef STRING_LENGTH_CACHING
                    411:                        string_length = length();
1.211     moko      412:                        if(string_length){
                    413:                                const char *result=CORD_to_const_char_star(body, string_length);
                    414:                                const_cast<Body*>(this)->body=(CORD)result;
                    415:                                return result;
                    416:                        }
1.197     misha     417: #endif
                    418:                        return CORD_to_const_char_star(body, length());
                    419:                }
                    420: 
1.221     moko      421:                // never null
1.194     misha     422:                char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146     paf       423: 
1.187     misha     424: #ifdef STRING_LENGTH_CACHING
                    425:                void set_length(size_t alength){ string_length = alength; }
1.211     moko      426:                size_t length() const { return body ? CORD_IS_STRING(body) ? string_length ? string_length : (string_length=strlen((const char *)body)) : CORD_len(body) : 0; }
1.187     misha     427: #else
1.146     paf       428:                size_t length() const { return CORD_len(body); }
1.187     misha     429: #endif
1.146     paf       430: 
1.212     moko      431:                inline bool is_empty() const { return body==CORD_EMPTY; }
1.146     paf       432: 
                    433:                void append_know_length(const char *str, size_t known_length) {
1.187     misha     434:                        if(known_length){
                    435:                                if(body){
                    436:                                        body = CORD_cat_char_star(body, str, known_length);
                    437:                                        ZERO_LENGTH 
                    438:                                } else {
1.211     moko      439:                                        body=(CORD)str;
1.187     misha     440: #ifdef STRING_LENGTH_CACHING
                    441:                                        string_length=known_length;
                    442: #endif
                    443:                                }
                    444:                        }
1.146     paf       445:                }
                    446:                void append_strdup_know_length(const char* str, size_t known_length) {
                    447:                        if(known_length)
                    448:                                append_know_length(pa_strdup(str, known_length), known_length);
                    449:                }
1.187     misha     450:                void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
                    451:                Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
                    452: 
1.146     paf       453:                Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
                    454: 
                    455:                bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
                    456:                bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
                    457:                bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
                    458:                bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
1.207     moko      459: 
1.146     paf       460:                bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
                    461:                bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
                    462: 
1.211     moko      463:                bool operator != (const char *src) const { return CORD_cmp(body, AS_CORD(src))!=0; }
                    464:                bool operator == (const char *src) const { return CORD_cmp(body, AS_CORD(src))==0; }
1.207     moko      465: 
1.146     paf       466:                int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
                    467:                        return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145     paf       468:                }
                    469: 
1.146     paf       470:                char fetch(size_t index) const { return CORD_fetch(body, index); }
1.211     moko      471:                Body mid(size_t aindex, size_t alength) const { return Body(CORD_substr(body, aindex, alength, length())); }
                    472:                size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, AS_CORD(substr), length()); }
1.146     paf       473:                size_t pos(const Body substr, size_t offset=0) const { 
1.180     misha     474:                        if(substr.is_empty())
1.146     paf       475:                                return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150     paf       476: 
                    477:                        // CORD_str checks for bad offset [CORD_chr does not]
1.194     misha     478:                        return CORD_str(body, offset, substr.body, length()); 
1.146     paf       479:                }
1.207     moko      480:                size_t pos(char c, size_t offset=0) const {
1.150     paf       481:                        if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
                    482:                                return STRING_NOT_FOUND;
                    483: 
1.146     paf       484:                        return CORD_chr(body, offset, c);
1.145     paf       485:                }
1.146     paf       486: 
1.203     misha     487:                size_t strrpbrk(const char* chars, size_t left, size_t right) const;
                    488: 
                    489:                size_t rskipchars(const char* chars, size_t left, size_t right) const;
                    490: 
1.187     misha     491:                template<typename I>
                    492:                int for_each(int (*f)(char c, I), I info) const {
1.159     paf       493:                        return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
                    494:                }
                    495: 
1.187     misha     496:                template<typename I>
                    497:                int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159     paf       498:                        return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148     paf       499:                }
1.146     paf       500: 
                    501:                void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
                    502: 
1.157     paf       503:                /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
                    504:                Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188     misha     505:                        size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145     paf       506:        };
                    507: 
1.218     moko      508: private:
1.145     paf       509: 
1.152     paf       510:        Body body; ///< all characters of string
1.146     paf       511:        Languages langs; ///< string characters lang
                    512: 
1.164     paf       513:        void dump() const;
                    514:        #define ASSERT_STRING_INVARIANT(string) \
                    515:                assert((string).langs.invariant((string).body.length()))
1.145     paf       516: 
1.8       paf       517: public:
1.151     paf       518: 
                    519:        static const String Empty;
1.8       paf       520: 
1.181     misha     521:        explicit String(){};
1.211     moko      522:        explicit String(const char* cstr, Language alang=L_CLEAN) : body(cstr){
                    523:                if(body.get_cord()){
1.182     misha     524:                        langs=alang;
1.181     misha     525:                }
                    526:        }
1.213     moko      527:        explicit String(C ac, Language alang=L_CLEAN) : body(ac){
1.211     moko      528:                if(body.get_cord()){
                    529:                        langs=alang;
                    530:                }
                    531:        }
1.146     paf       532:        String(Body abody, Language alang): body(abody), langs(alang) {
                    533:                ASSERT_STRING_INVARIANT(*this);
                    534:        }
                    535:        String(const String& src): body(src.body), langs(src.langs) {
                    536:                ASSERT_STRING_INVARIANT(*this);
1.145     paf       537:        }
1.212     moko      538:        String(int value, const char *format);
1.145     paf       539: 
                    540:        /// for convinient hash lookup
1.183     misha     541: #ifdef HASH_CODE_CACHING
                    542:        operator const Body&() const { return body; }
                    543: #else
1.146     paf       544:        operator const Body() const { return body; }
1.183     misha     545: #endif
1.145     paf       546: 
                    547:        bool is_empty() const { return body.is_empty(); }
                    548:        size_t length() const { return body.length(); }
1.171     misha     549:        size_t length(Charset& charset) const;
1.145     paf       550: 
1.191     misha     551:        /// convert to CORD forcing lang tainting
                    552:        Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
                    553:        /// convert to CORD with tainting dirty to lang
                    554:        Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145     paf       555: 
1.212     moko      556:        /// from body
                    557:        const char* cstr() const { return body.cstr(); }
                    558:        char* cstrm() const { return body.cstrm(); }
1.189     misha     559: 
                    560:        /// convert to constant C string forcing lang tainting
1.190     misha     561:        const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     562:                return cstr_to_string_body_taint(lang, connection, charsets).cstr();
                    563:        }
1.190     misha     564:        char *taint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     565:                return cstr_to_string_body_taint(lang, connection, charsets).cstrm();
                    566:        }
                    567: 
                    568:        /// convert to constant C string with tainting dirty to lang
1.190     misha     569:        const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     570:                return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
                    571:        }
1.190     misha     572:        char *untaint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     573:                return cstr_to_string_body_untaint(lang, connection, charsets).cstrm();
                    574:        }
1.195     misha     575: 
1.196     misha     576:        const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195     misha     577: 
1.202     moko      578:        bool is_not_just_lang() const {
                    579:                return langs.opt.is_not_just_lang !=0;
                    580:        }
                    581:        
                    582:        Language just_lang() const {
                    583:                return langs.opt.lang;
                    584:        }
                    585: 
1.222     moko      586:        char* visualize_langs() const;
                    587: 
1.108     parser    588:        /// puts pieces to buf
1.145     paf       589:        Cm serialize(size_t prolog_size) const;
1.108     parser    590:        /// appends pieces from buf to self
1.145     paf       591:        bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146     paf       592:        /// @see Body::append_know_length
1.145     paf       593:        String& append_know_length(const char* str, size_t known_length, Language lang);
1.146     paf       594:        /// @see Body::append_help_length
1.145     paf       595:        String& append_help_length(const char* str, size_t helper_length, Language lang);
                    596:        String& append_strdup(const char* str, size_t helper_length, Language lang);
                    597: 
1.146     paf       598:        bool operator == (const char* y) const { return body==Body(y); }
                    599:        bool operator != (const char* y) const { return body!=Body(y); }
1.145     paf       600: 
                    601:        /// this starts with y
                    602:        bool starts_with(const char* y) const {
1.146     paf       603:                return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145     paf       604:        }
                    605:        /// x starts with this
                    606:        bool this_starts(const char* x) const {
1.146     paf       607:                return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26      paf       608:        }
                    609: 
1.220     moko      610:        String& append_to(String& dest) const;
1.187     misha     611:        String& append_to(String& dest, Language lang, bool forced=false) const;
1.145     paf       612:        String& append(const String& src, Language lang, bool forced=false) { 
                    613:                return src.append_to(*this, lang, forced);
                    614:        }
1.199     misha     615:        String& append_quoted(const String* src, Language lang=L_JSON){
                    616:                *this << "\"";
                    617:                if(src)
                    618:                        this->append(*src, lang, true/*forced lang*/);
                    619:                *this << "\"";
                    620:                return *this;
                    621:        }
                    622: 
1.220     moko      623:        String& operator << (const String& src) { return src.append_to(*this); }
1.145     paf       624:        String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187     misha     625:        String& operator << (const Body& src){
                    626:                langs.appendHelper(body, L_AS_IS, src);
                    627:                body<<src;
                    628:                return *this;
                    629:        }
1.100     parser    630: 
1.142     paf       631:        char first_char() const {
1.145     paf       632:                return is_empty()?0:body.fetch(0);
1.142     paf       633:        }
1.54      paf       634: 
1.205     moko      635:        char last_char() const {
                    636:                return is_empty()?0:body.fetch(body.length()-1);
                    637:        }
                    638: 
1.145     paf       639:        bool operator < (const String& src) const { return body<src.body; }
                    640:        bool operator > (const String& src) const { return body>src.body; }
                    641:        bool operator <= (const String& src) const { return body<=src.body; }
                    642:        bool operator >= (const String& src) const { return body>=src.body; }
                    643:        bool operator != (const String& src) const { return body!=src.body; }
                    644:        bool operator == (const String& src) const { return body==src.body; }
                    645: 
1.54      paf       646:        /// extracts [start, finish) piece of string
1.145     paf       647:        String& mid(size_t substr_begin, size_t substr_end) const;
1.172     misha     648:        String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145     paf       649: 
1.235     moko      650:        /// return position of substr in string, -1 means "not found" [const char* version]
                    651:        size_t pos(const char* substr, size_t this_offset=0) const { return body.pos(substr, this_offset); }
                    652:        size_t pos(const Body substr, size_t this_offset=0) const { return body.pos(substr, this_offset); }
                    653:        size_t pos(const String& substr, size_t this_offset=0) const { return body.pos(substr.body, this_offset); }
                    654:        size_t pos(char c, size_t this_offset=0) const { return body.pos(c, this_offset); }
                    655:        /// ignore lang if it's L_UNSPECIFIED, otherwise look for substring that lies in ONE fragment in THAT lang
                    656:        size_t pos(const Body substr, size_t this_offset, Language lang) const;
                    657:        size_t pos(const String& substr, size_t this_offset, Language lang) const;
1.216     moko      658:        size_t pos(Charset& charset, const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55      paf       659: 
1.203     misha     660:        size_t strrpbrk(const char* chars, size_t left=0) const {
                    661:                return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
                    662:        }
                    663:        size_t strrpbrk(const char* chars, size_t left, size_t right) const {
                    664:                return body.strrpbrk(chars, left, right);
                    665:        }
                    666: 
                    667:        size_t rskipchars(const char* chars, size_t left=0) const {
                    668:                return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
                    669:        }
                    670:        size_t rskipchars(const char* chars, size_t left, size_t right) const {
                    671:                return body.rskipchars(chars, left, right);
                    672:        }
                    673: 
1.219     moko      674:        void split(ArrayString& result, size_t pos_after, const char* delim, Language lang=L_UNSPECIFIED) const;
                    675:        void split(ArrayString& result, size_t pos_after, const String& delim, Language lang=L_UNSPECIFIED) const;
1.216     moko      676: 
                    677:        typedef void (*Row_action)(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info);
                    678: 
1.87      parser    679:        /**
1.145     paf       680:                @return table of found items, if any.
1.87      parser    681:                table format is defined and fixed[can be used by others]: 
                    682:                @verbatim
                    683:                        prematch/match/postmatch/1/2/3/...
                    684:                @endverbatim
                    685:        */
1.216     moko      686:        Table* match(VRegex* vregex, Row_action row_action, void *info, int& matches_count) const;
                    687: 
1.87      parser    688:        enum Change_case_kind {
                    689:                CC_UPPER,
                    690:                CC_LOWER
                    691:        };
1.216     moko      692:        String& change_case(Charset& source_charset, Change_case_kind kind) const;
                    693: 
1.145     paf       694:        const String& replace(const Dictionary& dict) const;
1.188     misha     695:        const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155     paf       696:        double as_double() const { return pa_atod(cstr(), this); }
1.226     moko      697:        int as_int() const { return pa_atoi(cstr(), 0, this); }
1.240   ! moko      698:        pa_wint as_wint() const { return pa_atowi(cstr(), 0, this); }
        !           699:        bool as_bool() const { return as_wint()!=0; }
1.174     misha     700:        const String& escape(Charset& source_charset) const;
1.137     paf       701: 
1.7       paf       702: private: //disabled
                    703: 
1.12      paf       704:        String& operator = (const String&) { return *this; }
1.7       paf       705: 
1.1       paf       706: };
1.119     paf       707: 
1.183     misha     708: #ifndef HASH_CODE_CACHING
1.145     paf       709: /// simple hash code of string. used by Hash
1.146     paf       710: inline uint hash_code(const String::Body self) {
1.184     misha     711:        return self.get_hash_code();
1.119     paf       712: }
1.183     misha     713: #endif
1.147     paf       714: 
                    715: 
1.1       paf       716: #endif

E-mail: