?? str.cc
字號(hào):
/* * HT Editor * str.cc * * Copyright (C) 2002 Stefan Weyergraf (stefan@weyergraf.de) * Copyright (C) 2002, 2003 Sebastian Biallas (sb@biallas.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <new>#include <cctype>#include <cstdlib>#include <cstring>#include "except.h"#include "htdebug.h"#include "snprintf.h"#include "str.h"#include "stream.h"#include "strtools.h"#include "tools.h"#ifdef HAVE_HT_OBJECTS#include "atom.h"#endifextern "C" {#include "regex.h"}/* * CLASS String *//** * creates empty string */String::String(){ mContent = NULL; realloc(0);}/** * create string from char * */String::String(const char *s){ mContent = NULL; assign(s);}/** * copy constructor */String::String(const String *s){ mContent = NULL; assign(s);}/** * copy constructor */String::String(const String &s){ assert(&s != this); mContent = NULL; assign(s);}/** * creates string from array |s| size |aLength| */String::String(const byte *s, int aLength){ mContent = NULL; assign(s, aLength);}/** * creates string with |count| times |c| */String::String(char c, int count){ mContent = NULL; assign(c, count);}String::~String(){ free(mContent);}/** * (re-)assigns string to |s| */void String::assign(const String *s){ realloc(s->mLength); memcpy(mContent, s->mContent, mLength);}/** * (re-)assigns string to |s| */void String::assign(const String &s){ realloc(s.mLength); memcpy(mContent, s.mContent, mLength);}/** * (re-)assigns string to char * |s| */void String::assign(const char *s){ int slen = s ? strlen(s) : 0; realloc(slen); memcpy(mContent, s, mLength);}/** * (re-)assigns string to array |s| length |aLength| */void String::assign(const byte *s, int aLength){ realloc(aLength); memcpy(mContent, s, mLength);}/** * (re-)assigns string to |count| times |c| */void String::assign(char c, int count){ realloc(count); memset(mContent, c, count);}/** * (re-)assigns string via ht_snprintf */void String::assignFormat(const char *s, ...){ char buf[1024]; va_list vargs; va_start(vargs, s); ht_vsnprintf(buf, sizeof buf, s, vargs); va_end(vargs); assign(buf);}/** * appends |s| to the end */void String::append(const String &s){ if (s.mLength) { int oldLength = mLength; realloc(mLength + s.mLength); memcpy(&mContent[oldLength], s.mContent, s.mLength); }}void String::append(const char *s){ if (s && *s) { int oldLength = mLength; int slen = strlen(s); realloc(mLength + slen); memcpy(&mContent[oldLength], s, slen); }}void String::appendChar(char c){ realloc(mLength+1); mContent[mLength-1] = c;}void String::append(const byte *s, int aLength){ if (aLength <= 0) return; int oldLength = mLength; realloc(mLength + aLength); memcpy(&mContent[oldLength], s, aLength);}/** * (re-)append to string via ht_snprintf */void String::appendFormat(const char *s, ...){ char buf[1024]; va_list vargs; va_start(vargs, s); ht_vsnprintf(buf, sizeof buf, s, vargs); va_end(vargs); append(buf);}/** * prepends |s| to the front */void String::prepend(const String &s){ if (s.mLength) { int oldLength = mLength; realloc(mLength + s.mLength); memmove(&mContent[s.mLength], &mContent[0], oldLength); memcpy(&mContent[0], s.mContent, s.mLength); }}/** * Empties string. */void String::clear(){ realloc(0);}String *String::clone() const{ return new String(mContent, mLength);}/** * compares to characters. * used in compareTo() and findXXX() (and therefore replace()) * @returns 0 for equality, negative number if |c1<c2| and positive number if |c1>c2| */int String::compareChar(char c1, char c2) const{ if (c1 < c2) return -1; if (c1 > c2) return 1; return 0;}int String::compare(const char *s) const{ if (!mLength) { return (s) ? -1: 0; } if (!s) { return 1; } int l = mLength; for (int i=0; i < l; i++) { if (!*s) return 1; int r = compareChar(mContent[i], s[i]); if (r) return r; } if (s[l]) return -1; return 0;}int String::compare(const String &s) const{ if (!mContent) { return (s.mContent) ? -1: 0; } if (!s.mContent) { return 1; } int l = MIN(mLength, s.mLength); for (int i=0; i<l; i++) { int r = compareChar(mContent[i], s.mContent[i]); if (r) return r; } if (mLength < s.mLength) return -1; if (mLength == s.mLength) return 0; return 1;}/* * like compare(s) but considers a maximum of |aMax| characters */int String::compare(const String &s, int aMax) const{ if (aMax <= 0) return 0; if (!mContent) { return (s.mContent) ? -1: 0; } if (!s.mContent) { return 1; } int l = MIN(mLength, s.mLength); l = MIN(l, aMax); int i; for (i=0; i<l; i++) { int r = compareChar(mContent[i], s.mContent[i]); if (r) return r; } if (i == aMax) return 0; if (mLength < s.mLength) return -1; if (mLength == s.mLength) return 0; return 1;}int String::compareTo(const Object *o) const{ assert(getObjectID() == o->getObjectID()); return compare(*((String *)o));}uint String::countChar(char c) const{ int i = 0; uint n = 0; while (i < mLength) { if (compareChar(mContent[i], c) == 0) n++; i++; } return n;}/** * Crops the string to contain a maximum of |aNewLength| characters. */void String::crop(int aNewLength){ if ((aNewLength >= 0) && (aNewLength < mLength)) realloc(aNewLength);}/** * Deletes |aLength| characters at |pos| */void String::del(int pos, int aLength){ if (pos < 0) { aLength += pos; pos = 0; } if (aLength <= 0 || pos >= mLength) return; if (pos+aLength >= mLength) aLength = mLength-pos; if (!aLength) return; if (pos + aLength < mLength) { memmove(&mContent[pos], &mContent[pos+aLength], mLength-aLength-pos); } realloc(mLength-aLength);}/** * Escapes certains characters in a c-style manner (all characters < 0x20). * @param aSpecialChars characters that need a \ * @param bit7 hex encode (\x..) characters >127 */void String::escape(const char *aSpecialChars, bool bit7){ if (!mLength) return; String copy(this); realloc(mLength*4); realloc(escape_special((char*)mContent, mLength+1, copy.mContent, copy.mLength, aSpecialChars, bit7));}/** * Search forwards for |c| in string * @param c character to search for * @param start first character position to look for * @returns position of character or number < 0 if not found */int String::findCharFwd(char c, int start, int ith_match) const{ if (!mLength) return -1; if (start >= mLength) return -1; if (start < 0) start = 0; for (int i=start; i < mLength; i++) { if (compareChar(mContent[i], c) == 0) { if (ith_match <= 1) return i; ith_match--; } } return -1;}/** * Search backwards for |c| in string * @param c character to search for * @param start first character position to look for * @returns position of character or number < 0 if not found */int String::findCharBwd(char c, int start, int ith_match) const{ if (!mLength) return -1; if (start >= mLength) return -1; if (start < 0) start = mLength-1; for (int i=start; i>=0; i--) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -