📄 superstr.h
字号:
#ifndef SUPER_STRING_HH
#define SUPER_STRING_HH
// Super String 1.0 3/29/99
// written by Phil Haubert and released into the public domain.
// string and buffer operations
// C compatibility
// extensible searching
// conversions and comparisons
// see external documentation
// original header comment, c.1993
// faster than a speeding parser, more powerful than a 4GL, able to munch
// a million characters in a single gulp: it's ---
#define SS_STANDALONE
namespace std {
template <class E> struct char_traits;
template <class T> class allocator;
template <class E, class T=char_traits<E>, class A=allocator<E> > class basic_string;
template <class T, class A=allocator<T> > class vector;
typedef basic_string<char> string;
template <class E, class T=char_traits<E> > class basic_ostream;
typedef basic_ostream <char, char_traits<char> > ostream;
}
// more predeclarations
class Urror;
class UrrorBadArg;
class UrrorBadState;
class UrrorNumberFormat;
class UrrorOutOfRange;
class UrrorOverflow;
class UrrorNotFound;
template <class T> struct Pair;
class SS {
public:
SS ();
// allow an SS to be constructed for any type that supplies a non-member SSconvert
template <class T>
SS (T const & t) { _zero(); SSconvert (t, *this); }
SS (SS const & s);
SS (void const * v, int n);
virtual ~SS ();
// basic operations
int length () const; // does not include null terminator
int size () const; // std::string has both size and length
bool empty () const; // same as length()==0
char & operator [] (int ind);
char const & operator [] (int ind) const;
char & at (int ind);
char const & at (int ind) const;
// allow as much compatibility with C strings as possible...
operator char * ();
operator char const * () const;
char * c_str ();
char const * c_str () const;
char * data ();
char const * data () const;
SS clone () const; // return a copy
SS& erase (); // set to ""
// doubles as a buffer class -- these aren't string operations
SS& buffer (int n); // destroys contents of string
// simple assign (to rep) - doesn't copy (or delete), or zero terminate
SS& buffer (void const * v, int n);
SS& resize (int n); // preserves original contents
SS& resizeToNullTerminator (); // keep up to first embedded null
// support a constructor to create a buffer
class Buffer {
private:
int _n;
void const * _start;
SS* _fillValue;
bool _isAlloc;
void copy (Buffer const & b);
public:
// this creates a new string of length n, and zero terminated
Buffer (int n=0, SS const & fillvalue="");
Buffer (Buffer const & b);
Buffer& operator = (Buffer const & b);
// this assigns start directly to rep
// a string created this way is not necessarily zero terminated
Buffer (void const * start, int n);
void const * getStart () const;
int getSize () const;
SS getFillValue () const;
bool getAlloc () const;
~Buffer ();
};
// some typedefs
typedef std::string string;
typedef Pair<int> BegLen;
typedef std::vector< Pair<int> > BegLenVect;
typedef std::vector<int> PosVect;
// define these here and/or maybe in a separate "Umber" class
typedef __int64 LongLong;
typedef unsigned __int64 ULongLong;
private:
// allow use of SS in nested classes
class SSCopyPointer {
private:
SS* _rep;
public:
SSCopyPointer (SS const & s);
SSCopyPointer (SSCopyPointer const & p);
SS * operator -> ();
SS const * operator -> () const;
SS & operator * ();
SS const & operator * () const;
operator = (SSCopyPointer const & p);
~SSCopyPointer ();
};
class SubSS {
private:
int _beg, _len;
SS* _rep;
public:
SubSS (SS* s, int beg, int len);
// operator SS () const;
SS get () const;
SS& operator = (SS s);
};
public:
// a substring is either a copy of a range or an object that can be assigned to
// or decays, if copied, into a copy of the range
SubSS sub (int beg, int len);
SS sub (int beg, int len) const;
SubSS operator () (int beg, int len);
SS operator () (int beg, int len) const;
SubSS operator () (Pair<int> const & beglen);
SS operator () (Pair<int> const & beglen) const;
// bunch o' string operations
// copy a range of chars into a new string
SS get (int beg, int len=1) const;
SS getFrom (int beg) const;
SS get (Pair<int> const & beglen) const;
// concatenates the ranges
SS get (std::vector< Pair<int> > const & beglen) const;
// copy the ranges into vector of SS
std::vector<SS>& get (std::vector<SS>& s, std::vector< Pair<int> > const & beglen) const;
// set does not extend string
// the beg and len refers to the arg string
SS& set (SS const & s, int pos=0);
SS& set (SS const & s, int pos, int beg, int len);
SS& set (SS const & s, int pos, Pair<int> const & beglen);
// set s at each pos
SS& set (SS const & s, std::vector<int> const & pos);
// set an s at the corresponding pos
SS& set (std::vector<SS> const & s, std::vector<int> const & pos);
// removes some chars from string, returns the chars removed
SS cut (int beg, int len=1);
SS cutFrom (int beg);
SS cut (Pair<int> const & beglen);
SS& cut (SS& s, int beg, int len=1); // returns arg/cut string
SS& cut (SS& s, Pair<int> const & beglen);
// return s, containing all the cut strings
std::vector<SS>& cut (std::vector<SS>& s, std::vector< Pair<int> > const & beglen);
// insert the chars at pos. can extend string. pos==length is an append
// the beg and len refers to the arg string
SS& paste (SS const & s, int pos=0);
SS& paste (SS const & s, int pos, int beg, int len);
SS& paste (SS const & s, int pos, Pair<int> const & beglen);
// paste s at each pos
SS& paste (SS const & s, std::vector<int> const & pos);
// paste an s at the corresponding pos
SS& paste (std::vector<SS> const & s, std::vector<int> const & pos);
// remove or replace a range of characters
SS& removeRange (int beg=0, int len=fullength);
SS& removeRange (Pair<int> const & beglen);
SS& removeRange (std::vector< Pair<int> > const & beglen);
SS& replaceRange (SS const & newseq, int beg=0, int len=fullength);
SS& replaceRange (SS const & newseq, Pair<int> const & beglen);
// replace each range with newseq
SS& replaceRange (SS const & newseq, std::vector< Pair<int> > const & beglen);
// replace each range with the corresponding s
SS& replaceRange (std::vector<SS> const & s, std::vector< Pair<int> > const & beglen);
// reverse the range of chars
SS& reverse (int beg=0, int len=fullength);
SS& reverse (Pair<int> const & beglen);
SS& reverse (std::vector< Pair<int> > const & beglen); // each range separate
SS& itemReverse (std::vector< Pair<int> > const & beglen); // move around ranges
SS& tailReverse (int len);
// sort the chars in ascending order
SS& sort (int beg=0, int len=fullength);
SS& sort (Pair<int> const & beglen);
SS& sort (std::vector< Pair<int> > const & beglen); // each range separate
SS& itemSort (std::vector< Pair<int> > const & beglen); // move around ranges
SS& tailSort (int len);
SS& fill (char c, int beg=0, int len=fullength);
SS& fill (char c, Pair<int> const & beglen);
SS& fill (char c, std::vector< Pair<int> > const & beglen);
SS& repeat (SS const & s, int beg=0, int len=fullength);
SS& repeat (SS const & s, Pair<int> const & beglen);
SS& repeat (SS const & s, std::vector< Pair<int> > const & beglen);
// fills with zeros, not set to "", more a buffer operation
SS& zero ();
SS& zero (int beg, int len);
// get the chars at the beginning or end
SS head (int len) const;
SS tail (int len) const;
char & firstChar ();
char const & firstChar () const;
char & lastChar ();
char const & lastChar () const;
// used in matching
struct Finder;
struct Find {
Finder* _rep;
Find ();
Find (Find const & f);
Find& operator = (Find const & f);
virtual bool found (SS const & s, int pos, int& len) const;
virtual Finder* clone () const;
virtual ~Find ();
// for stl - will only work for single char compares
bool operator () (char c);
};
enum {notfound=-99999999,maxindex=-99999998,fullength=-99999997,allitems=-99999996};
static const char nullchar; // zero char
static const Pair<int> nomatch; // if no match found
// answer position or <0 ie notfound if fail
int findNext (SS const & sequence, int beg=0, SS& result=nullref) const;
int findPrev (SS const & sequence, int beg=maxindex, SS& result=nullref) const;
int findNext (Find const & finder, int beg=0, SS& result=nullref) const;
int findPrev (Find const & finder, int beg=maxindex, SS& result=nullref) const;
int findNextNoCase (SS const & sequence, int beg=0, SS& result=nullref) const;
int findPrevNoCase (SS const & sequence, int beg=maxindex, SS& result=nullref) const;
// find any char in set
int findNextOf (SS const & charset, int beg=0, SS& result=nullref) const;
int findPrevOf (SS const & charset, int beg=maxindex, SS& result=nullref) const;
// same as above but returns the beginning and length of match or SS::nomatch
Pair<int> findNextMatch (SS const & sequence, int beg=0) const;
Pair<int> findPrevMatch (SS const & sequence, int beg=maxindex) const;
Pair<int> findNextMatch (Find const & finder, int beg=0) const;
Pair<int> findPrevMatch (Find const & finder, int beg=maxindex) const;
Pair<int> findNextNoCaseMatch (SS const & sequence, int beg=0) const;
Pair<int> findPrevNoCaseMatch (SS const & sequence, int beg=maxindex) const;
// these throw ErrorNotFound
SS findNextString (SS const & sequence, int beg=0 ) const;
SS findPrevString (SS const & sequence, int beg=maxindex) const;
SS findNextString (Find const & finder, int beg=0 ) const;
SS findPrevString (Find const & finder, int beg=maxindex) const;
SubSS findNextSubString (SS const & sequence, int beg=0 );
SubSS findPrevSubString (SS const & sequence, int beg=maxindex);
SubSS findNextSubString (Find const & finder, int beg=0 );
SubSS findPrevSubString (Find const & finder, int beg=maxindex);
// base find, end is the last index tried
int find (Find const & finder, int beg, int* len=0, int end=fullength, int inc=1) const;
int rfind (Find const & finder, int beg, int* len=0, int end=fullength) const;
int find (SS const & sequence, int beg, int* len=0, int end=fullength, int inc=1) const;
int rfind (SS const & sequence, int beg, int* len=0, int end=fullength) const;
// does string contain the arg
bool contains (SS const & sequence, int beg=0, int len=fullength) const;
bool contains (Find const & finder, int beg=0, int len=fullength) const;
bool contains (SS const & sequence, Pair<int> const & beglen) const;
bool contains (Find const & finder, Pair<int> const & beglen) const;
bool contains (SS const & sequence, std::vector< Pair<int> > const & beglen) const;
bool contains (Find const & finder, std::vector< Pair<int> > const & beglen) const;
// number of matches contained in string
int population (SS const & sequence, int beg=0, int len=fullength) const;
int population (Find const & finder, int beg=0, int len=fullength) const;
int population (SS const & sequence, Pair<int> const & beglen) const;
int population (Find const & finder, Pair<int> const & beglen) const;
int population (SS const & sequence, std::vector< Pair<int> > const & beglen) const;
int population (Find const & finder, std::vector< Pair<int> > const & beglen) const;
// remove all occurrences in a region
SS& remove (SS const & oldseq, int beg=0, int len=fullength);
SS& remove (Find const & finder, int beg=0, int len=fullength);
SS& remove (SS const & oldseq, Pair<int> const & beglen);
SS& remove (Find const & finder, Pair<int> const & beglen);
SS& remove (SS const & oldseq, std::vector< Pair<int> > const & beglen);
SS& remove (Find const & finder, std::vector< Pair<int> > const & beglen);
// replace all occurrences in a region with newseq
SS& replace (SS const & oldseq, SS const & newseq, int beg=0, int len=fullength);
SS& replace (Find const & finder, SS const & newseq, int beg=0, int len=fullength);
SS& replace (SS const & oldseq, SS const & newseq, Pair<int> const & beglen);
SS& replace (Find const & finder, SS const & newseq, Pair<int> const & beglen);
SS& replace (SS const & oldseq, SS const & newseq, std::vector< Pair<int> > const & beglen);
SS& replace (Find const & finder, SS const & newseq, std::vector< Pair<int> > const & beglen);
// returns actual count of items removed or replaced
// a value for count of allitems removes or replaces all matches
int removeForward (SS const & oldseq, int count=1, int beg=0, int len=fullength);
int removeForward (Find const & finder, int count=1, int beg=0, int len=fullength);
int removeBackward (SS const & oldseq, int count=1, int beg=0, int len=fullength);
int removeBackward (Find const & finder, int count=1, int beg=0, int len=fullength);
int replaceForward (SS const & oldseq, SS const newseq, int count=1, int beg=0, int len=fullength);
int replaceForward (Find const & finder, SS const newseq, int count=1, int beg=0, int len=fullength);
int replaceBackward (SS const & oldseq, SS const newseq, int count=1, int beg=0, int len=fullength);
int replaceBackward (Find const & finder, SS const newseq, int count=1, int beg=0, int len=fullength);
// determines if chars starting at pos constitute a match
bool match (SS const & sequence, int pos, SS& result=nullref) const;
bool match (Find const & finder, int pos, SS& result=nullref) const;
std::vector< Pair<int> >& match (SS const & sequence, std::vector< Pair<int> >& beglen, int beg=0, int len=fullength) const;
std::vector< Pair<int> >& match (Find const & finder, std::vector< Pair<int> >& beglen, int beg=0, int len=fullength) const;
std::vector< Pair<int> >& match (SS const & sequence, std::vector< Pair<int> >& beglen, Pair<int> const & beglen_src) const;
std::vector< Pair<int> >& match (Find const & finder, std::vector< Pair<int> >& beglen, Pair<int> const & beglen_src) const;
std::vector< Pair<int> >& match (SS const & sequence, std::vector< Pair<int> >& beglen, std::vector< Pair<int> > const & beglen_src) const;
std::vector< Pair<int> >& match (Find const & finder, std::vector< Pair<int> >& beglen, std::vector< Pair<int> > const & beglen_src) const;
// returns array of matching positions and lengths
std::vector< Pair<int> >& matchForward (SS const & sequence, std::vector< Pair<int> >& beglen, int count=1, int beg=0, int len=fullength) const;
std::vector< Pair<int> >& matchForward (Find const & finder, std::vector< Pair<int> >& beglen, int count=1, int beg=0, int len=fullength) const;
std::vector< Pair<int> >& matchBackward (SS const & sequence, std::vector< Pair<int> >& beglen, int count=1, int beg=0, int len=fullength) const;
std::vector< Pair<int> >& matchBackward (Find const & finder, std::vector< Pair<int> >& beglen, int count=1, int beg=0, int len=fullength) const;
// returns array of start position and length of tokens, the inverse of match
// uses whitespace, a string, or a Find predicate as delimiter
std::vector< Pair<int> >& tokenize (std::vector< Pair<int> >& beglen) const;
std::vector< Pair<int> >& tokenize (SS const & delimiter, std::vector< Pair<int> >& beglen) const;
std::vector< Pair<int> >& tokenize (Find const & finder, std::vector< Pair<int> >& beglen) const;
// a few predefined classes for the finding members
// additional user predicates derive off Finder
// this hierarchy assumes that objects will be copied as SS::Find and also
// that objects are stored as SS::Find or as references/pointers to SS::Find
struct Finder : public Find {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -