Annotation of researchv9/cmd/cfront/CC/String.h, revision 1.1.1.1

1.1       root        1: #ifndef STRING_DOT_H
                      2: #define STRING_DOT_H
                      3: 
                      4: #define _OVERLOAD_STAT sleaze
                      5: #include <sys/types.h>
                      6: #include <sys/stat.h>
                      7: #undef _OVERLOAD_STAT
                      8: 
                      9: overload system, access, acct, chdir, chmod, chown, creat, link, mknod, mount,
                     10:        open, umount, unlink, read, write;
                     11: #include <sysent.h>
                     12: 
                     13: overload fopen, fdopen, freopen, gets, fgets, puts, fputs;
                     14: 
                     15: #include <string.h>
                     16: #include <stream.h>
                     17: #ifndef GENERICH
                     18: #include <generic.h>
                     19: #endif
                     20: 
                     21: #ifndef TRUE
                     22: #define FALSE  0
                     23: #define TRUE   (!FALSE)
                     24: #endif
                     25: 
                     26: #ifndef BIT_DEFINED
                     27: #define BIT_DEFINED
                     28: typedef int    bit;
                     29: #endif
                     30: 
                     31: #define MAXSUBSTRINGLENGTH     0x7FFF
                     32: const MAXSTRINGLENGTH = 0xFFFF-1;
                     33: 
                     34: class Rep;
                     35: class String;
                     36: class SubString;
                     37: class Subchar; /* the result of [] */
                     38: class Regexp;
                     39: 
                     40: struct Charfield;
                     41: 
                     42: struct MtRep
                     43: {
                     44:        MtRep   *next;
                     45:        int     dummy;
                     46: };
                     47: 
                     48: const empty_dummy = ~0 << 16 | 0374;
                     49: 
                     50: struct Rep
                     51: {
                     52:        char    *start; // the characters (NOT null terminated)
                     53:        unsigned short  len;
                     54:        unsigned char   refCount;
                     55:        unsigned char   flags;
                     56:        bit     is_immutable() { return (flags & 02) != 0; }
                     57:        void    immutable() { flags |= 02; }
                     58:        void    mutable() { flags &= ~02; }
                     59:        bit     is_constant() { return (flags & 01) != 0; }
                     60:        void    now_constant() { flags |= 01; }
                     61:        void    not_constant() { flags &= ~01; }
                     62:                Rep() {}
                     63:                Rep(const Rep&);
                     64:                Rep(unsigned);
                     65:                Rep(const char *, unsigned);
                     66:                Rep(const char *s) { this = new Rep(s, strlen(s)); }
                     67:                ~Rep();
                     68:        void    refDecr() { if (!is_constant() && --refCount == 0 )
                     69:                                delete this;
                     70:                }
                     71:        void    refIncr() { if (!is_constant()) ++refCount; }
                     72:        unsigned        length () { return len; }
                     73:        int     compare (const Rep&);   /* like strcmp(3C) */
                     74:        int     compare (const char*);
                     75:        int     match(const Rep&);      // return first differing position
                     76:        int     match(const char*);
                     77:        int     hashval ();     // for use in hash tables, etc.
                     78:        bit     extend(int);    // TRUE if the Rep is extended
                     79:        bit     isMt() { return ((MtRep *)this)->dummy == empty_dummy; }
                     80:        Charfield       *myField();
                     81:        Rep     *canCat(int);   // returns a new Rep if this can be catenated
                     82:        Rep     *newSub(int offset, int length);
                     83:        /* the following are from String(3) */
                     84:        int     strchr(char);   // position of first occurrence of char
                     85:        int     strrchr(char);  // ... last ... (-1 if char not there)
                     86:        int     strpbrk(const Rep&);
                     87:        int     strspn(const Rep&);
                     88:        int     strcspn(const Rep&);
                     89: };
                     90: 
                     91: // Strings are at the bottom working up, and Reps are at the top working down
                     92: struct Charfield
                     93: {
                     94:        Charfield       *next;
                     95:        MtRep   *emptyHead;     /* of list of free Reps with no space */
                     96:        Rep     *lastRep;
                     97:        char    *field;         /* beginning of characters */
                     98:        char    *end;           /* ... of data area */
                     99:        char    *firstFree;
                    100:        int     usedSpace;
                    101:                Charfield();
                    102:                Charfield(unsigned int);
                    103:        int     compactify(unsigned int);       /* return TRUE if no use */
                    104:        Rep     *newRep(unsigned int);
                    105:        char    *getSpace(int); /* new String space for old Rep */
                    106:        void    putMt(MtRep *);
                    107:        Rep     *getMt();
                    108: };
                    109: 
                    110: extern Rep     *nullRep;       /* the null String */
                    111: extern Rep     *oneChar;       /* all one-character Strings */
                    112: extern Charfield       *currfield;
                    113: 
                    114: extern char    *Memcpy(char *to, const char *from, int);
                    115: 
                    116: extern ostream& operator<<(ostream&, Rep&);
                    117: 
                    118: class String
                    119: {
                    120:        static GPT      handler;
                    121:        static bit      startedUp;
                    122:        void    error(int = 0, char * = 0);
                    123:        Rep     *d;
                    124:        friend SubString;
                    125:        friend Subchar;
                    126: public:
                    127:                String();
                    128:                String(char);
                    129:                String(char, char);
                    130:                String(char, char, char);
                    131:                String(char, char, char, char);
                    132:                String(const char *);
                    133:                String(const char *, int);
                    134:                String(const String& s) { d = s.d; d->refIncr(); }
                    135:                String(const SubString&);
                    136:                String(Rep& r) { d = &r; r.refCount++; }
                    137:                ~String() { d->refDecr(); }
                    138:        friend int stat(const String&, struct stat*);
                    139:        friend int system(const String&);
                    140:        friend int access(const String&, int);
                    141:        friend int acct(const String&);
                    142:        friend int chdir(const String&);
                    143:        friend int chmod(const String&, int);
                    144:        friend int chown(const String&, int, int);
                    145:        friend int creat(const String&, int);
                    146:        friend int link(const String&, const String&);
                    147:        friend int mknod(const String&, int, int);
                    148:        friend int mount(const String&, const String&, int);
                    149:        friend int open(const String&, int);
                    150:        friend int umount(const String&);
                    151:        friend int unlink(const String&);
                    152:        friend int read(int, String&, int);
                    153:        friend int write(int, const String&);
                    154:        friend FILE* fopen(const String&, const String&);
                    155:        friend FILE* fdopen(int, const String&);
                    156:        friend FILE* freopen(const String&, const String&, FILE*);
                    157:        friend puts(const String&);
                    158:        friend fputs(const String&, FILE*);
                    159:        friend inline ostream& operator<<(ostream&, const String&);
                    160:        friend istream& operator>>(istream&, String&);
                    161:        friend String sgets(istream&);
                    162:        friend String operator+(const char, const String&);
                    163:        friend String operator+(const char*, const String&);
                    164:        friend Rep;
                    165:        friend Regexp;
                    166:        friend void startUp();
                    167:        unsigned        length (){ return d->length(); }
                    168:                operator void*() { return length() ? this : 0; }
                    169:        int     hashval() { return d->hashval(); }
                    170:        int     compare(const String&); /* like strcmp(3C) */
                    171:        int     compare(const char *p) { return d->compare(p); }
                    172:        bit     operator==(const String& oo) { return compare(oo) == 0; }
                    173:        bit     operator>(const String& oo) { return compare(oo) > 0; }
                    174:        bit     operator>=(const String& oo) { return compare(oo) >= 0; }
                    175:        bit     operator<=(const String& oo) { return compare(oo) <= 0; }
                    176:        bit     operator<(const String& oo) { return compare(oo) < 0; }
                    177:        bit     operator!=(const String& oo) { return compare(oo) != 0; }
                    178:        bit     operator==(const char* p) { return compare(p) == 0; }
                    179:        bit     operator>(const char* p) { return compare(p) > 0; }
                    180:        bit     operator>=(const char* p) { return compare(p) >= 0; }
                    181:        bit     operator<=(const char* p) { return compare(p) <= 0; }
                    182:        bit     operator<(const char* p) { return compare(p) < 0; }
                    183:        bit     operator!=(const char* p) { return compare(p) != 0; }
                    184:        // match returns the first differing position
                    185:        int     match(const String&);
                    186:        int     match (const char* p) { return d->match(p); }
                    187:        String  operator+(const String&);       /* catenate */
                    188:        String  operator+(const char*); /* catenate */
                    189:        String  operator+(const char);  /* catenate */
                    190:        String& operator=(const char);
                    191:        String& operator=(const char *);
                    192:        String& operator=(const String& oo);
                    193:        String& put(const char);        /* append or put */
                    194:        String& put(const String&);     /* append or put */
                    195:        String& operator+=(const char c) { return put(c); }
                    196:        String& operator+=(const String& oo) { return put(oo); }
                    197:        bit     getX(char&);    /* get or lop */
                    198:        bit     get() { char c; return getX(c); }
                    199:        String& unget(const char);              /* prepend */
                    200:        String& unget(const String&);           /* prepend */
                    201:        bit     unputX(char&);          /* remove from back */
                    202:        bit     unput() { char c; return unputX(c); }
                    203:        bit     firstX(char&);
                    204:        bit     lastX(char&);
                    205:        SubString&      operator() (const unsigned start, const unsigned length);
                    206:        Subchar&        operator[] (const unsigned);    /* character selection */
                    207:        void    dump(char *);
                    208:        GPT     sethandler(GPT);
                    209:        /* the following are from String(3) */
                    210:        /* position of first occurrence of char */
                    211:        int     strchr(const char c) { return d->strchr(c); }
                    212:        /* ... last ... (-1 if char not there) */
                    213:        int     strrchr(const char c) { return d->strrchr(c); }
                    214:        int     strpbrk(const String& oo) { return d->strpbrk(*oo.d); }
                    215:        int     strspn(const String& oo) { return d->strspn(*oo.d); }
                    216:        int     strcspn(const String& oo) { return d->strcspn(*oo.d); }
                    217: };
                    218: 
                    219: inline bit
                    220: operator==(const char* p, const String s)
                    221: {
                    222:        return s.compare(p) == 0;
                    223: }
                    224: inline bit
                    225: operator>(const char* p, const String s)
                    226: {
                    227:        return s < p;
                    228: }
                    229: inline bit
                    230: operator>=(const char* p, const String s)
                    231: {
                    232:        return s >= p;
                    233: }
                    234: inline bit
                    235: operator<=(const char* p, const String s)
                    236: {
                    237:        return s >= p;
                    238: }
                    239: inline bit
                    240: operator<(const char* p, const String s)
                    241: {
                    242:        return s > p;
                    243: }
                    244: inline bit
                    245: operator!=(const char* p, const String s)
                    246: {
                    247:        return s != p;
                    248: }
                    249: 
                    250: inline ostream&
                    251: operator<<(ostream& oo, const String& ss)
                    252: {
                    253:        return oo << *ss.d;
                    254: }
                    255: 
                    256: class SubString
                    257: {
                    258:        static String   *ss;
                    259:        static int      oo;
                    260:        static int      ll;
                    261:        static GPT      handler;
                    262:        void    error(int = 0, char * = 0);
                    263:                SubString(const SubString&);
                    264:                SubString(const String &ii, int off, int len) { this = 0;
                    265:                                ss = &ii; oo = off; ll = len; }
                    266:                ~SubString() { this = 0; }
                    267:        void    operator=(const String&);
                    268: public:
                    269:        GPT     sethandler(GPT);
                    270:        String  *it() { return ss; }
                    271:        int     offset() { return oo; }
                    272:        int     length() { return ll; }
                    273: };
                    274: 
                    275: class Subchar
                    276: {
                    277:        static String   *ss;
                    278:        static int      oo;     /* position in the String */
                    279:        static GPT      handler;
                    280:        void    error(int = 0, char * = 0);
                    281:                Subchar(const Subchar&);
                    282:                Subchar(const String &ii, int off) {
                    283:                                this = 0; ss = &ii; oo = off; }
                    284:                ~Subchar() { this = 0; }
                    285:        char    operator=(const char);
                    286:        operator char() { return *(ss->d->start + oo); }
                    287: public:
                    288:        GPT     sethandler(GPT);
                    289:        String  *it() { return ss; }
                    290:        int     offset() { return oo; }
                    291: };
                    292: 
                    293: overload length;
                    294: int    length(const String&);
                    295: inline int     length(const String &s) { return s.length(); }
                    296: overload hashval;
                    297: int    hashval(const String&);
                    298: inline int     hashval(const String &s) { return s.hashval(); }
                    299: overload compare;
                    300: int    compare(const String&,const String&);
                    301: inline int     compare(const String &s,const String &t) { return s.compare(t); }
                    302: overload strchr;
                    303: int    strchr(const String&, const char);
                    304: inline int     strchr(const String& s, const char c) { return s.strchr(c); }
                    305: overload strrchr;
                    306: int    strrchr(const String&, const char);
                    307: inline int     strrchr(const String& s, const char c) { return s.strrchr(c); }
                    308: overload strpbrk;
                    309: int    strpbrk(const String&, const String&);
                    310: inline int     strpbrk(const String& s, const String& t) { return s.strpbrk(t); }
                    311: overload strspn;
                    312: int    strspn(const String&, const String&);
                    313: inline int     strspn(const String& s, const String& t) { return s.strspn(t); }
                    314: overload strcspn;
                    315: int    strcspn(const String&, const String&);
                    316: inline int     strcspn(const String& s, const String& t) { return s.strcspn(t); }
                    317: 
                    318: overload min;
                    319: int min(int, int);
                    320: int min(int, int, int);
                    321: inline int min(int i, int j) { return i<j ? i : j; }
                    322: inline int min(int i, int j, int k) { return i<j ? min(i,k) : min(j,k); }
                    323: 
                    324: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.