perlclas.h
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 689 行 · 第 1/2 页
H
689 行
/*
* Version 1.6
* Written by Jim Morris, jegm@sgi.com
* Kudos to Larry Wall for inventing Perl
* Copyrights only exist on the regex stuff, and all have been left intact.
* The only thing I ask is that you let me know of any nifty fixes or
* additions.
*
* Credits:
* I'd like to thank Michael Golan <mg@Princeton.EDU> for his critiques
* and clever suggestions. Some of which have actually been implemented
*/
#ifndef _PERL_H
#define _PERL_H
#include <string.h>
#include "regexp.h"
#if DEBUG
#include <stdio.h>
#endif
#define INLINE inline
// This is the base class for PerlList, it handles the underlying
// dynamic array mechanism
template<class T>
class PerlListBase
{
private:
enum{ALLOCINC=20};
T *a;
int cnt;
int first;
int allocated;
int allocinc;
void grow(int amnt= 0, int newcnt= -1);
protected:
void compact(const int i);
public:
#ifdef USLCOMPILER
// USL 3.0 bug with enums losing the value
PerlListBase(int n= 20)
#else
PerlListBase(int n= ALLOCINC)
#endif
{
a= new T[n];
cnt= 0;
first= n>>1;
allocated= n;
allocinc= n;
# ifdef DEBUG
fprintf(stderr, "PerlListBase(int %d) a= %p\n", allocinc, a);
# endif
}
PerlListBase(const PerlListBase<T>& n);
PerlListBase<T>& PerlListBase<T>::operator=(const PerlListBase<T>& n);
virtual ~PerlListBase(){
# ifdef DEBUG
fprintf(stderr, "~PerlListBase() a= %p, allocinc= %d\n", a, allocinc);
# endif
delete [] a;
}
INLINE T& operator[](const int i);
INLINE const T& operator[](const int i) const;
int count(void) const{ return cnt; }
void add(const T& n);
void add(const int i, const T& n);
void erase(void){ cnt= 0; first= (allocated>>1);}
};
// PerlList
class PerlStringList;
template <class T>
class PerlList: private PerlListBase<T>
{
public:
PerlList(int sz= 10): PerlListBase<T>(sz){}
// stuff I want public to see from PerlListBase
T& operator[](const int i){return PerlListBase<T>::operator[](i);}
const T& operator[](const int i) const{return PerlListBase<T>::operator[](i);}
PerlListBase<T>::count;
// add perl-like synonym
void reset(void){ erase(); }
int scalar(void) const { return count(); }
operator void*() { return count()?this:0; } // so it can be used in tests
int isempty(void) const{ return !count(); } // for those that don't like the above (hi michael)
T pop(void)
{
T tmp;
int n= count()-1;
if(n >= 0){
tmp= (*this)[n];
compact(n);
}
return tmp;
}
void push(const T& a){ add(a);}
void push(const PerlList<T>& l);
T shift(void)
{
T tmp= (*this)[0];
compact(0);
return tmp;
}
int unshift(const T& a)
{
add(0, a);
return count();
}
int unshift(const PerlList<T>& l);
PerlList<T> reverse(void);
PerlList<T> sort();
PerlList<T> splice(int offset, int len, const PerlList<T>& l);
PerlList<T> splice(int offset, int len);
PerlList<T> splice(int offset);
};
// just a mechanism for self deleteing strings which can be hacked
class TempString
{
private:
char *str;
public:
TempString(const char *s)
{
str= new char[strlen(s) + 1];
strcpy(str, s);
}
TempString(const char *s, int len)
{
str= new char[len + 1];
if(len) strncpy(str, s, len);
str[len]= '\0';
}
~TempString(){ delete [] str; }
operator char*() const { return str; }
};
/*
* This class takes care of the mechanism behind variable length strings
*/
class VarString
{
private:
enum{ALLOCINC=32};
char *a;
int len;
int allocated;
int allocinc;
INLINE void grow(int n= 0);
public:
#ifdef USLCOMPILER
// USL 3.0 bug with enums losing the value
INLINE VarString(int n= 32);
#else
INLINE VarString(int n= ALLOCINC);
#endif
INLINE VarString(const VarString& n);
INLINE VarString(const char *);
INLINE VarString(const char* s, int n);
INLINE VarString(char);
~VarString(){
# ifdef DEBUG
fprintf(stderr, "~VarString() a= %p, allocinc= %d\n", a, allocinc);
# endif
delete [] a;
}
VarString& operator=(const VarString& n);
VarString& operator=(const char *);
INLINE const char operator[](const int i) const;
INLINE char& operator[](const int i);
operator const char *() const{ return a; }
int length(void) const{ return len; }
void add(char);
void add(const char *);
void add(int, const char *);
void remove(int, int= 1);
void erase(void){ len= 0; }
};
class PerlStringList;
//
// Implements the perl specific string functionality
//
class PerlString
{
private:
VarString pstr; // variable length string mechanism
class substring;
public:
PerlString():pstr(){}
PerlString(const PerlString& n) : pstr(n.pstr){}
PerlString(const char *s) : pstr(s){}
PerlString(const char c) : pstr(c){}
PerlString(const substring& sb) : pstr(sb.pt, sb.len){}
PerlString& operator=(const char *s){pstr= s; return *this;}
PerlString& operator=(const PerlString& n);
PerlString& operator=(const substring& sb);
operator const char*() const{return pstr;}
const char operator[](int n) const{ return pstr[n]; }
int length(void) const{ return pstr.length(); }
char chop(void);
int index(const PerlString& s, int offset= 0);
int rindex(const PerlString& s, int offset= -1);
substring substr(int offset, int len= -1);
substring substr(const Range& r){ return substr(r.start(), r.length());}
int m(const char *, const char *opts=""); // the regexp match m/.../ equiv
int m(Regexp&);
int m(const char *, PerlStringList&, const char *opts="");
int m(Regexp&, PerlStringList&);
int tr(const char *, const char *, const char *opts="");
int s(const char *, const char *, const char *opts="");
PerlStringList split(const char *pat= "[ \t\n]+", int limit= -1);
int operator<(const PerlString& s) const { return (strcmp(pstr, s) < 0); }
int operator>(const PerlString& s) const { return (strcmp(pstr, s) > 0); }
int operator<=(const PerlString& s) const { return (strcmp(pstr, s) <= 0); }
int operator>=(const PerlString& s) const { return (strcmp(pstr, s) >= 0); }
int operator==(const PerlString& s) const { return (strcmp(pstr, s) == 0); }
int operator!=(const PerlString& s) const { return (strcmp(pstr, s) != 0); }
PerlString operator+(const PerlString& s) const;
PerlString operator+(const char *s) const;
PerlString operator+(char c) const;
friend PerlString operator+(const char *s1, const PerlString& s2);
PerlString& operator+=(const PerlString& s){pstr.add(s); return *this;}
PerlString& operator+=(const char *s){pstr.add(s); return *this;}
PerlString& operator+=(char c){pstr.add(c); return *this;}
friend class substring;
private:
void insert(int pos, int len, const char *pt, int nlen);
// This idea lifted from NIH class library -
// to handle substring LHS assignment
// Note if subclasses can't be used then take external and make
// the constructors private, and specify friend PerlString
class substring
{
public:
int pos, len;
PerlString& str;
char *pt;
public:
substring(PerlString& os, int p, int l) : str(os)
{
if(p > os.length()) p= os.length();
if((p+l) > os.length()) l= os.length() - p;
pos= p; len= l;
if(p == os.length()) pt= 0; // append to end of string
else pt= &os.pstr[p];
}
void operator=(const PerlString& s)
{
if(&str == &s){ // potentially overlapping
VarString tmp(s);
str.insert(pos, len, tmp, strlen(tmp));
}else str.insert(pos, len, s, s.length());
}
void operator=(const substring& s)
{
if(&str == &s.str){ // potentially overlapping
VarString tmp(s.pt, s.len);
str.insert(pos, len, tmp, strlen(tmp));
}else str.insert(pos, len, s.pt, s.len);
}
void operator=(const char *s)
{
str.insert(pos, len, s, strlen(s));
}
};
};
class PerlStringList: public PerlList<PerlString>
{
public:
PerlStringList(int sz= 6):PerlList<PerlString>(sz){}
// copy lists, need to duplicate all internal strings
PerlStringList(const PerlStringList& n);
PerlStringList& operator=(const PerlList<PerlString>& n);
int split(const char *str, const char *pat= "[ \t\n]+", int limit= -1);
PerlString join(const char *pat= " ");
int m(const char *rege, const char *targ, const char *opts=""); // makes list of sub exp matches
PerlStringList grep(const char *rege, const char *opts=""); // trys rege against elements in list
};
// This doesn't belong in any class
inline PerlStringList m(const char *pat, const char *str, const char *opts="")
{
PerlStringList l;
l.m(pat, str, opts);
l.shift(); // remove the first element which would be $&
return l;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?