📄 stringc.c
字号:
if ( p2 ) memcpy(&t._p[len1], p2, len2); t._len = len1 + len2; t._p[t._len] = 0;}StringCStringC::operator+(const StringC& s) const{ StringC t(_len+s._len); Add(_p, _len, s._p, s._len, t); return t;}StringCStringC::operator+(const SubStringC& ss) const{ StringC t(_len+ss._sublen); Add(_p, _len, ss._subp, ss._sublen, t); return t;}StringCStringC::operator+(const CharC& c) const{ StringC t(_len+c.Length()); Add(_p, _len, c.Addr(), c.Length(), t); return t;}StringCStringC::operator+(const char* cs) const{ u_int clen = cs ? strlen(cs) : 0; StringC t(_len+clen); Add(_p, _len, cs, clen, t); return t;}StringCStringC::operator+(char c) const{ StringC t(_len+1); if ( _p ) memcpy(t._p, _p, _len); // Copy first string t._len = _len; t._p[t._len++] = c; // Add character to end t._p[t._len] = 0; return (t);}StringCoperator+(const char* cs, const StringC& s){ StringC t(cs); return (t + s);}StringCoperator+(char c, const StringC& s){ char cs[2]; cs[0] = c; cs[1] = 0; StringC t(cs); return (t + s);}StringCoperator+(int i, const StringC& s){ char is[MAX_DIGITS_IN_INT]; sprintf (is, "%d", i); return (is + s);}StringCStringC::operator+(int i) const { char is[MAX_DIGITS_IN_INT]; // Can't inline because of array sprintf (is, "%d", i); return (*this + is);}/*--------------------------------------------------------------------------- * String appending methods */voidStringC::Append(const char *cs, u_int clen){ if ( cs == NULL || clen == 0 ) return;//// If cs points into this string, make a copy// if ( cs >= _p && cs <= (_p + _len) ) { CharC data(cs, clen); StringC t(data); *this += t; } else { reallocate(_len + clen); if ( _p ) { memcpy(&_p[_len], cs, clen); _p[_len+clen] = 0; } _len += clen; }} // End AppendStringC&StringC::operator+=(const StringC& s){ Append(s._p, s._len); return *this;}StringC&StringC::operator+=(const SubStringC& ss){ Append(ss._subp, ss._sublen); return *this;}StringC&StringC::operator+=(const CharC& c){ Append(c.Addr(), c.Length()); return *this;}StringC&StringC::operator+=(const char* cs){ Append(cs, cs ? strlen(cs) : 0); return *this;}StringC&StringC::operator+=(char c){ reallocate(_len+1); // Add more space if necessary _p[_len++] = c; // Add character in last position _p[_len] = 0; return *this;}StringC&StringC::operator+=(int i) { char is[MAX_DIGITS_IN_INT]; // Can't inline because of array sprintf (is, "%d", i); Append(is, strlen(is)); return *this;}/*--------------------------------------------------------------------------- * Methods to return substrings */SubStringCStringC::operator()(unsigned pos, unsigned len){ SubStringC sub(*this, pos, len); return (sub);}SubStringCStringC::operator()(const RangeC& r){ SubStringC sub(*this, r); return (sub);}const SubStringCStringC::operator()(unsigned pos, unsigned len) const{ SubStringC sub(*this, pos, len); return (sub);}const SubStringCStringC::operator()(const RangeC& r) const{ SubStringC sub(*this, r); return (sub);}constCharCStringC::Range(u_int first, u_int length) const{ if ( _len == 0 ) return *this; const char *newp; u_int newlen; if ( first >= _len ) { newp = _p + _len - 1; newlen = 0; } else if ( first + length > _len ) { newp = _p + first; newlen = _len - first; } else { newp = _p + first; newlen = length; } return CharC(newp, newlen);}constCharCStringC::Range(const RangeC& r) const{ return Range(r.firstIndex(), r.length());}CharCStringC::Range(u_int first, u_int length){ if ( _len == 0 ) return *this; const char *newp; u_int newlen; if ( first >= _len ) { newp = _p + _len - 1; newlen = 0; } else if ( first + length > _len ) { newp = _p + first; newlen = _len - first; } else { newp = _p + first; newlen = length; } return CharC(newp, newlen);}CharCStringC::Range(const RangeC& r){ return Range(r.firstIndex(), r.length());}/*--------------------------------------------------------------------------- * The following compare functions were implemented because strncmp is * not adequate for comparing character strings of unequal length. For * example, strncmp("abc","abcd",3) will return 0. */intStringC::compare(const StringC& s) const{ return ::compare(_p, _len, s._p, s._len);}intStringC::compare(const CharC& c) const{ return ::compare(_p, _len, c.Addr(), c.Length());}intStringC::compare(const char *cs) const{ return ::compare(_p, _len, cs, cs?strlen(cs):0);}intcompare(const char *cs, const StringC& s){ return ::compare(cs, cs?strlen(cs):0, s._p, s._len);}StringCoperator+(const char* cs, const SubStringC& ss){ if (cs) { StringC s(cs); return (s + ss); } else { return ((StringC)ss); }}intcompare(const char *cs, const SubStringC& ss){ StringC s(cs); return s.compare(ss);}/*--------------------------------------------------------------------------- * Remove whitespace from ends */voidStringC::Trim(){ if ( !_p || _len == 0 ) return;//// Remove from end// char *cp = _p + _len - 1; while ( (cp >= _p) && (*cp == ' ' || *cp == '\t' || *cp == '\n') ) cp--, _len--; *(cp+1) = 0;//// Remove from beginning// cp = _p; while ( (*cp != 0) && (*cp == ' ' || *cp == '\t' || *cp == '\n') ) cp++, _len--; memcpy(_p, cp, _len); *(_p+_len) = 0;//// If we're allowed to shrink, see if memory needs to be freed// if ( autoShrink ) reallocate(_len, True);} // End Trim/*--------------------------------------------------------------------------- * Clear the string starting with the specified position */voidStringC::Clear(int start){ if ( start < _len ) {//// Mark the end of the string and update the length// if ( _p ) *(_p+start) = 0; _len = start;//// If we're allowed to shrink, see if memory needs to be freed// if ( autoShrink ) reallocate(_len, True); }}/*--------------------------------------------------------------------------- * Conversion methods */voidStringC::toAscii(){ register unsigned i = _len; register char *q = _p; while (i--) { *q = toascii(*q); q++; }}voidStringC::toLower(){ register unsigned i = _len; register char *q = _p; while (i--) { *q = to_lower(*q); q++; }}voidStringC::toUpper(){ register unsigned i = _len; register char *q = _p; while (i--) { *q = to_upper(*q); q++; }}/*--------------------------------------------------------------------------- * Method to read a word separated by white space from a file */intStringC::GetWord(FILE *fp){//// Clear it...// *this = "";//// Skip over the initial white space...// register int c = getc(fp); while ( c == ' ' || c == '\n' || c == '\t' ) c=getc(fp);//// Loop until we hit EOF, newline, or white space...// while ( c != EOF && c != ' ' && c != '\t' && c != '\n' ) { *this += (char)c; c=getc(fp); } return c;} // end GetWord()/*--------------------------------------------------------------------------- * Method to read a line from a file */intStringC::GetLine(FILE *fp, int trim){#define MAXLINE 1024 char line[MAXLINE]; register int c = getc(fp); register char *cp = line; register char *ep = &line[MAXLINE-1];//// If we're trimming then skip the white space.// if ( trim ) { while ( c != EOF && (c == ' ' || c == '\t') ) c=getc(fp); }//// Loop until we hit EOF or newline// while ( c != EOF && c != '\n' ) { *cp++ = c;//// See if we've filled our buffer// if ( cp == ep ) { *cp = 0; *this += line; cp = line; }//// Get another character// c = getc(fp); } // End for each character//// Add the last bit// if ( cp != line ) { *cp = 0; *this += line; } if ( c != EOF && _p == 0 ) *this = ""; else if ( trim && _len ) { // trim the right edge. cp = _p + _len - 1; while ( _len > 0 && (*cp == ' ' || *cp == '\t') ) { _len--; cp--; } cp++; *cp = 0; } return c;}/*--------------------------------------------------------------------------- * Method to read the specified number of bytes from an open file and add * them to the end of this string. */intStringC::AppendFile(FILE *fp, unsigned count){//// If length not specified, see how far to end// if ( count == (unsigned)-1 ) { long cur = ftell(fp); fseek(fp, 0, SEEK_END); long last = ftell(fp); count = (unsigned)(last - cur); fseek(fp, cur, SEEK_SET); }//// Allocate space and read data// reallocate(_len + count); unsigned readCount = fread(_p+_len, 1, count, fp); int error = (readCount != count); _len += readCount; _p[_len] = 0; return !error;} // End AppendFile/*--------------------------------------------------------------------------- * Method to read a file and add it to the end of this string */intStringC::AppendFile(char *fname){//// Open file// FILE *fp = fopen(fname, "r"); if ( !fp ) return 0; int success = AppendFile(fp); fclose(fp); return success;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -