⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 superstr.cpp

📁 用bcg库编写的java IDE 源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
SS& SS::repeat (SS const & s, Pair<int> const & beglen)
{
	return repeat (s, beglen._0, beglen._1);
}

SS& SS::repeat (SS const & s, std::vector< Pair<int> > const & beglen)
{
	{for (int i=0; i<beglen.size(); i++) {
		repeat (s, beglen[i]);
	}}
	return *this;
}

SS& SS::zero ()
{
	return zero (nullchar, _len);
}

SS& SS::zero (int beg, int len)
{
	return fill (nullchar, beg, len);
}

int SS::compareBool (bool a, bool b)
{
	if (a == b) return 0;
	if (a) return 1;
	return -1;
}

int SS::compareChar (char b) const
{
	char s[1];
	s[0]=b;
	return compare (_rep, _len, s, 1);
}

int SS::compare (SS const & s) const { return compare (_rep, _len, s._rep, s._len); }
int SS::compare (string const & s) const { return compareString (_rep, _len, s); }
int SS::compare (std::vector<char> const & v) const { return compareVector (_rep, _len, v); }
int SS::compare (char const * s) const { return compare (_rep, _len, s, strLen(s)); }
int SS::compare (unsigned char const * s) const { return compare (_rep, _len, s, strLen(s)); }
int SS::compare (signed char const * s) const { return compare (_rep, _len, s, strLen(s)); }
int SS::compare (void const * v, int n) const { return compare (_rep, _len, v, n); }
int SS::compare (char          v) const { return compareChar (v); }
int SS::compare (unsigned char v) const { return compareChar (static_cast<char>(v)); }
int SS::compare (signed char   v) const { return compareChar (static_cast<char>(v)); }
int SS::compare (bool           v) const { return compareBool (toBool(),v); }
int SS::compare (short          v) const { return compareNumeric (toShort(),v); }
int SS::compare (unsigned short v) const { return compareNumeric (toUShort(),v); }
int SS::compare (int            v) const { return compareNumeric (toInt(),v); }
int SS::compare (unsigned int   v) const { return compareNumeric (toUInt(),v); }
int SS::compare (long           v) const { return compareNumeric (toLong(),v); }
int SS::compare (unsigned long  v) const { return compareNumeric (toULong(),v); }
int SS::compare (LongLong       v) const { return compareNumeric (toLongLong(),v); }
int SS::compare (ULongLong      v) const { return compareNumeric (toULongLong(),v); }

template <class T> static inline int compareDouble (T a, T b)
{
	T e = 3*std::numeric_limits<T>::epsilon();
	if (b == 0.0) {
		if (a > 0.0) return 1;
		if (a < 0.0) return -1;
		return 0;
	}
	T q = a/b;
	if (q<0) {
		if (a<0) return -1;
		return 1;
	}
	if (a<0) {
		if (q > 1.+e) return -1;
		if (q < 1.-e) return 1;
		return 0;
	}
	if (q > 1.+e) return 1;
	if (q < 1.-e) return -1;
	return 0;
}

int SS::compare (float v) const
{
//	return compareDouble (toFloat(), v, 3*FLT_EPSILON);
	return compareDouble (toFloat(), v);
}

int SS::compare (double v) const
{
//	return compareDouble (toDouble(), v, 3*DBL_EPSILON);
	return compareDouble (toDouble(), v);
}

int SS::compare (long double v) const
{
	return compareDouble (toLongDouble(), v);
}

int SS::findNext (Find const & finder, int beg, SS& result) const
{
	return doFind (finder, beg, result, 1);
}

int SS::findPrev (Find const & finder, int beg, SS& result) const
{
	return doFind (finder, beg, result, -1);
}

int SS::findNext (SS const & sequence, int beg, SS& result) const
{
	return doFindString (sequence, beg, result, 1);
}

int SS::findPrev (SS const & sequence, int beg, SS& result) const
{
	return doFindString (sequence, beg, result, -1);
}

int SS::findNextOf (SS const & charset, int beg, SS& result) const
{
	return doFind (FindSet(charset), beg, result, 1);
}

int SS::findPrevOf (SS const & charset, int beg, SS& result) const
{
	return doFind (FindSet(charset), beg, result, -1);
}

int SS::findNextNoCase (SS const & sequence, int beg, SS& result) const
{
	return doFind (FindStringNoCase (sequence), beg, result, 1);
}

int SS::findPrevNoCase (SS const & sequence, int beg, SS& result) const
{
	return doFind (FindStringNoCase (sequence), beg, result, -1);
}

Pair<int> SS::findNextMatch (SS const & sequence, int beg) const
{
	return doFindStringMatch (sequence, beg, 1);
}

Pair<int> SS::findPrevMatch (SS const & sequence, int beg) const
{
	return doFindStringMatch (sequence, beg, -1);
}

Pair<int> SS::findNextMatch (Find const & finder, int beg) const
{
	return doFindMatch (finder, beg, 1);
}

Pair<int> SS::findPrevMatch (Find const & finder, int beg) const
{
	return doFindMatch (finder, beg, -1);
}

Pair<int> SS::findNextNoCaseMatch (SS const & sequence, int beg) const
{
	return doFindMatch (FindStringNoCase (sequence), beg, 1);
}

Pair<int> SS::findPrevNoCaseMatch (SS const & sequence, int beg) const
{
	return doFindMatch (FindStringNoCase (sequence), beg, -1);
}

// a macro because SubSS is private
#define findSubStringHelper(t,beg,inc) \
int len=0; \
int pos = find (t, beg, &len, fullength, inc); \
if (pos < 0) ErrorMacro(ErrorNotFound("SS::findString - match not found in \""+makeHeadSample()+"\"")); \
return sub (pos, len);

SS SS::findNextString (SS const & sequence, int beg) const
{
	findSubStringHelper (sequence, beg, 1);
}

SS SS::findPrevString (SS const & sequence, int beg) const
{
	findSubStringHelper (sequence, beg, -1);
}

SS SS::findNextString (Find const & finder, int beg) const
{
	findSubStringHelper (finder, beg, 1);
}

SS SS::findPrevString (Find const & finder, int beg) const
{
	findSubStringHelper (finder, beg, -1);
}

SS::SubSS SS::findNextSubString (SS const & sequence, int beg)
{
	findSubStringHelper (sequence, beg, 1);
}

SS::SubSS SS::findPrevSubString (SS const & sequence, int beg)
{
	findSubStringHelper (sequence, beg, -1);
}

SS::SubSS SS::findNextSubString (Find const & finder, int beg)
{
	findSubStringHelper (finder, beg, 1);
}

SS::SubSS SS::findPrevSubString (Find const & finder, int beg)
{
	findSubStringHelper (finder, beg, -1);
}

bool SS::match (SS const & sequence, int pos, SS& result) const
{
	if (&result != &nullref) result = "";
	if (!matchAdjust (pos)) return false;
	bool ret = matchString (sequence._rep, sequence._len, pos);
	if (ret && &result != &nullref) result = get (pos, sequence._len);
	return ret;
}

bool SS::match (Find const & finder, int pos, SS& result) const
{
	int len;
	if (&result != &nullref) result = "";
	if (!matchAdjust (pos)) return false;
	bool ret = finder.found (*this, pos, len);
	if (ret && &result != &nullref) result = get (pos, len);
	return ret;
}

int SS::findChar (char c, int beg, int end, int inc) const
{
	if (inc==1) {
		int res = findNext (_rep+beg, c, end-beg+1);
		if (res>=0) return beg + res;
	} else if (inc>0) {
		{for (int i=beg; i<=end; i+=inc) {
			if (_rep[i] == c) return i;
		}}
	} else if (inc<0) {
		{for (int i=beg; i>=end; i+=inc) {
			if (_rep[i] == c) return i;
		}}
	}
	return notfound;
}

bool SS::matchString (char const * rep, int len, int pos) const
{
	if (pos <= _len - len) {
		{for (int i=0; i<len; i++) {
			if (_rep[pos+i] != rep[i]) return false;
		}}
		return true;
	}
	return false;
}

int SS::doFind (Find const & finder, int beg, SS& result, int inc) const
{
	if (&result != &nullref) result = "";
	int len=0;
	int ret = find (finder, beg, &len, fullength, inc);
	if (ret != notfound && &result != &nullref) result = get (ret, len);
	return ret;
}

int SS::doFindString (SS const & s, int beg, SS& result, int inc) const
{
	if (&result != &nullref) result = "";
	int ret = findString (s, beg, fullength, inc);
	if (ret != notfound && &result != &nullref) result = get (ret, s._len);
	return ret;
}

Pair<int> SS::doFindMatch (Find const & finder, int beg, int inc) const
{
	int len=0;
	int pos = find (finder, beg, &len, fullength, inc);
	return pos == notfound ? nomatch : Pair<int> (pos,len);
}

Pair<int> SS::doFindStringMatch (SS const & s, int beg, int inc) const
{
	int pos = findString (s, beg, fullength, inc);
	return pos == notfound ? nomatch : Pair<int> (pos,s._len);
}

int SS::findString (SS const & s, int beg, int end, int inc) const
{
	int ret = notfound;
	try {
		if (!findCheck (beg, end, inc)) return notfound;
		modAdjust (beg, end, inc);
		if (s._len==1) {
			ret = findChar (s[0], beg, end, inc);
		} else if (s._len>0) {
			ret = findStringHelper (s, beg, end, inc);
		}
	} catch (Error) {
	}
	return ret;
}

int SS::findStringHelper (SS const & s, int beg, int end, int inc) const
{
	int pos=beg;
	int n=s._len;
	while ((pos = findChar (s[0], pos, end, inc)) != notfound) {
		if (inc > 0 && pos > _len-n) break;
		if (n == 1) return pos;
		if (matchString (s._rep+1, n-1, pos+1)) return pos;
		pos += inc;
		if (inc < 0 && pos < end) break;
	}
	return notfound;
}

int SS::findFinder (Find const & finder, int beg, int end, int inc, int* len) const
{
	int ret = notfound;
	int leng=0;
	if (inc>0) {
		{for (int i=beg; i<=end; i+=inc) {
			if (finder.found (*this, i, leng)) {
				ret = i;	
				break;
			}
		}}
	} else if (inc<0) {
		{for (int i=beg; i>=end; i+=inc) {
			if (finder.found (*this, i, leng)) {
				ret = i;
				break;
			}
		}}
	}
	if (len) *len = leng;
	return ret;
}

int SS::find (Find const & finder, int beg, int* len, int end, int inc) const
{
	int ret = notfound;
	int leng=0;
	if (len) *len = 0;
	try {
		if (!findCheck (beg, end, inc)) return notfound;
		modAdjust (beg, end, inc);
		if (typeid (finder) == typeid (FindChar)) {
			leng=1;
			char c = dynamic_cast<FindChar const &>(finder)._c;
			ret = findChar (c, beg, end, inc);
		} else if (typeid (finder) == typeid (FindString)) {
			SS const & sequence = *(dynamic_cast<FindString const &>(finder)._str);
			leng = sequence.size();
			if (leng==1) {
				char c = sequence._rep[0];
				ret = findChar (c, beg, end, inc);
			} else if (leng>0) {
				ret = findStringHelper (sequence, beg, end, inc);
			}
		} else if (typeid (finder) == typeid (FindSet)) {
			SS const & set = *(dynamic_cast<FindSet const &>(finder)._set);
			leng = 1;
			int set_size = set._len;
			if (set_size==1) {
				char c = set._rep[0];
				ret = findChar (c, beg, end, inc);
			} else if (set_size>0) {
				ret = findFinder (finder, beg, end, inc, &leng);
			}
		} else {
			ret = findFinder (finder, beg, end, inc, &leng);
		}
	} catch (Error) {
	}
	if (len) *len = leng;
	return ret;
}

int SS::rfind (Find const & finder, int beg, int* len, int end) const
{
	return find (finder, beg, len, end, -1);
}

int SS::find (SS const & sequence, int beg, int* len, int end, int inc) const
{
	return find (FindString(sequence), beg, len, end, inc);
}

int SS::rfind (SS const & sequence, int beg, int* len, int end) const
{
	return find (FindString(sequence), beg, len, end, -1);
}

template <class T>
static inline bool containsHelper0 (SS const * s, T const & t, int beg, int len)
{
	return s->getRegion (beg, len).findNext (t) >= 0;
}

template <class T>
static inline bool containsHelper1 (SS const * s, T const & t, std::vector< Pair<int> > const & beglen)
{
	{for (int i=0; i<beglen.size(); i++) {
		if (containsHelper0 (s, t, beglen[i]._0, beglen[i]._1)) return true;
	}}
	return false;
}

bool SS::contains (SS const & sequence, int beg, int len) const
{
	return containsHelper0 (this, sequence, beg, len);
}

bool SS::contains (Find const & finder, int beg, int len) const
{
	return containsHelper0 (this, finder, beg, len);
}

bool SS::contains (SS const & sequence, Pair<int> const & beglen) const
{
	return contains (sequence, beglen._0, beglen._1);
}

bool SS::contains (Find const & finder, Pair<int> const & beglen) const
{
	return contains (finder, beglen._0, beglen._1);
}

bool SS::contains (SS const & sequence, std::vector< Pair<int> > const & beglen) const
{
	return containsHelper1 (this, sequence, beglen);
}

bool SS::contains (Find const & finder, std::vector< Pair<int> > const & beglen) const
{
	return containsHelper1 (this, finder, beglen);
}

template <class T>
static inline int populationHelper0 (SS const * s, T const & t, int beg, int len)
{
	int n=0;
	int pos=0;
	SS str = s->getRegion (beg, len);
	SS result;
	while ( (pos = str.findNext (t, pos, result)) >= 0) {
		n++;
		pos += result.length();
	}
	return n;
}

template <class T>
static inline int populationHelper1 (SS const * s, T const & t, std::vector< Pair<int> > const & beglen)
{
	int ret = 0;
	{for (int i=0; i<beglen.size(); i++) {
		ret += populationHelper0 (s, t, beglen[i]._0, beglen[i]._1);
	}}
	return ret;
}

int SS::population (SS const & sequence, int beg, int len) const
{
	return populationHelper0 (this, sequence, beg, len);
}

int SS::population (Find const & finder, int beg, int len) const
{
	return populationHelper0 (this, finder, beg, len);
}

int SS::population (SS const & sequence, Pair<int> const & beglen) const
{
	return population (sequence, beglen._0, beglen._1);
}

int SS::population (Find const & finder, Pair<int> const & beglen) const
{
	return population (finder, beglen._0, beglen._1);
}

int SS::population (SS const & sequence, std::vector< Pair<int> > const & beglen) const
{
	return populationHelper1 (this, sequence, beglen);
}

int SS::population (Find const & finder, std::vector< Pair<int> > const & beglen) const
{
	return populationHelper1 (this, finder, beglen);
}

SS& SS::remove (SS const & oldseq, int beg, int len)
{
	return replace (oldseq, "", beg, len);
}

SS& SS::remove (Find const & finder, int beg, int len)
{
	return replace (finder, "", beg, len);
}

SS& SS::remove (SS const & oldseq, Pair<int> const & beglen)
{
	return replace (oldseq, "", beglen);
}

SS& SS::remove (Find const & finder, Pair<int> const & beglen)
{
	return replace (finder, "", beglen);
}

SS& SS::remove (SS const & oldseq, std::vector< Pair<int> > const & beglen)
{
	return replace (oldseq, "", beglen);
}

SS& SS::remove (Find const & finder, std::vector< Pair<int> > const & beglen)
{
	return replace (finder, "", beglen);
}

SS& SS::removeRange (int beg, int len)
{
	return replaceRange ("", beg, len);
}

SS& SS::removeRange (Pair<int> const & beglen)
{
	return replaceRange ("", beglen);
}

SS& SS::removeRange (std::vector< Pair<int> > const & beglen)
{
	return replaceRange ("", beglen);
}

SS& SS::replaceRange (SS const & newseq, int beg, int len)
{
	adjust (beg, len);
	sub (beg, len) = newseq;
	return *this;
}

SS& SS::replaceRange (SS const & newseq, Pair<int> const & beglen)
{
	return replaceRange (newseq, beglen._0, beglen._1);
}

SS& SS::replaceRange (SS const & newseq, std::vector< Pair<int> > const & beglen)
{
	resizeOpHelper(this,beglen,newseq,replaceRange,adjust,Pair<int>,SS,0);
	return *this;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -