home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + _string.c
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
- #include <LEDA/basic.h>
-
- #include <stdarg.h>
- #include <string.h>
-
-
- //------------------------------------------------------------------------------
- // String
- //------------------------------------------------------------------------------
-
-
- char* str_dup(const char* p)
- { if (p==nil) error_handler(1,"str_dup: nil argument");
- char* q = new char[strlen(p)+1];
- strcpy(q,p);
- return q;
- }
-
- char* str_cat(const char* p1, const char* p2)
- { char* q = new char[strlen(p1)+strlen(p2)+1];
- strcpy(q,p1);
- strcat(q,p2);
- return q;
- }
-
- void string::read(istream& s, char delim)
- { char buf[512];
- char* p = buf;
- char* q = p+511;
-
- while (s.get(*p) && p < q)
- { if (*p == delim) break;
- p++;
- }
- *p = '\0';
-
- *this = buf;
- }
-
-
- string::string(const char* format, ...) // printf-like constructor
- {
- va_list arg_list;
- Va_Start(arg_list,format);
-
- int arg[10];
-
- for(int i=0;i<10;i++) arg[i] = va_arg(arg_list,int);
-
- va_end(arg_list);
-
- char buf[512];
-
- sprintf(buf,format,arg[0],arg[1],arg[2],arg[3],arg[4],
- arg[5],arg[6],arg[7], arg[8],arg[9]);
-
- p = new srep(str_dup(buf));
- }
-
- int compare(string& x, string& y)
- { return strcmp(x.p->s,y.p->s); }
-
- int string::length() const
- { return strlen(p->s); }
-
- char string::operator[](int i) const
- { if (i<0 || strlen(p->s)<=i) error_handler(1,"string[]: index out of range");
- return p->s[i];
- }
-
- char& string::operator[](int i)
- { if (i<0 || strlen(p->s)<=i) error_handler(1,"string[]: index out of range");
- *this = p->s; //disconnect
- return p->s[i];
- }
-
-
- string& string::operator=(const char* s)
- { char* t = str_dup(s);
- if (p->n > 1) { p->n--; p = new srep(str_dup(""));}
- delete p->s;
- p->s = t;
- return *this;
- }
-
- string& string::operator=(const string& x)
- { x.p->n++;
- if (--p->n == 0) { delete p->s; delete p; }
- p = x.p;
- return *this;
- }
-
- string string::operator+(const string& x) const
- { string res;
- res.p->s = str_cat(p->s,x.p->s);
- return res;
- }
-
- string string::operator+(const char* s1) const
- { string res;
- res.p->s = str_cat(p->s,s1);
- return res;
- }
-
- string& string::operator+=(const char* s1)
- { *this = *this + s1;
- return *this;
- }
-
- string& string::operator+=(const string& x)
- { *this = *this + x;
- return *this;
- }
-
- int operator==(const string& x, const char* s)
- { return strcmp(x.p->s,s)==0; }
-
- int operator==(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)==0; }
-
- int operator!=(const string& x, const char* s)
- { return strcmp(x.p->s,s)!=0; }
-
- int operator!=(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)!=0; }
-
- int operator<(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)<0; }
-
- int operator>(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)>0; }
-
- int operator<=(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)<=0; }
-
- int operator>=(const string& x, const string& y)
- { return strcmp(x.p->s,y.p->s)>=0; }
-
-
- istream& operator>>(istream& in, string& x)
- { char buf[256];
- if (in >> buf) x = buf;
- return in;
- }
-
- ostream& operator<<(ostream& out, const string& x)
- { return out << x.p->s; }
-
-
- string string::sub(int i, int j) const
- {
- if (j>= strlen(p->s)) j = strlen(p->s) - 1;
-
- if (i < 0) i = 0;
-
- int l = j-i+1;
-
- if (l <= 0) return string("");
-
- char* q = new char[l+1];
- strncpy(q,p->s+i,l);
- q[l] = '\0';
- return string(q);
- }
-
- int string::pos(string s1, int i) const
- {
- int l1 = s1.length();
- if (l1==0 || i >= length()) return -1;
-
- char c = s1[0];
-
- char* q = p->s + i;
- while (*q)
- { while ( (*q) && (*q != c) ) q++;
- if ( strncmp(q,s1.p->s,l1)==0 ) break ;
- if (*q) q++;
- }
- return (*q) ? (q - p->s)/ sizeof(char) : -1;
- }
-
- string string::insert(string s, int i) const
- { return sub(0,i-1) + s + sub(i,length()-1); }
-
- string string::insert(int i, string s) const
- { return sub(0,i-1) + s + sub(i,length()-1); }
-
-
- string string::del(int i, int j) const
- { return sub(0,i-1) + sub(j+1,length()-1); }
-
- string string::del(string s, int n) const
- { return replace(s,"",n); }
-
-
-
- string string::replace(int i, int j, string s) const
- { return sub(0,i-1) + s + sub(j+1,length()-1); }
-
- string string::replace(string s1, string s2, int n) const
- {
- // replace n-th (all if n=0) occurrence of s1 by s2
-
- int i = 0;
- int match = 0;
-
- int l1 = s1.length();
-
- string tmp;
-
- for(;;)
- { int j = pos(s1,i);
- if (j < 0 ) break;
- tmp += sub(i,j-1);
-
- if (n==0 || ++match == n)
- tmp += s2;
- else
- tmp += s1;
-
- i = j+l1;
- }
-
- tmp += sub(i,length()-1);
-
- return tmp;
-
- }
-
-
- string string::format(string f) const
- {
- char buf[512];
- sprintf(buf,~f,p->s);
- return buf;
-
- }
-
-