Pol  Revision:4b29d2b
impstr.h
Go to the documentation of this file.
1 
6 #ifndef H_BSCRIPT_IMPSTR_H
7 #define H_BSCRIPT_IMPSTR_H
8 #define H_IMPSTR_H
9 
10 #ifndef BSCRIPT_BOBJECT_H
11 #include "bobject.h"
12 #endif
13 
14 #include <stack>
15 #include <string>
16 
17 namespace Pol
18 {
19 namespace Bscript
20 {
21 class SubString;
22 
23 class String : public BObjectImp
24 {
25  typedef BObjectImp base;
26 
27 public:
28  String() : BObjectImp( OTString ), value_( "" ) {}
29  String( const char* str, int nchars );
30  explicit String( const char* str ) : BObjectImp( OTString ), value_( str ) {}
31  explicit String( const std::string& str ) : BObjectImp( OTString ), value_( str ) {}
32  explicit String( const std::string& str, std::string::size_type pos, std::string::size_type n )
33  : BObjectImp( OTString ), value_( str, pos, n )
34  {
35  }
36  String( const char* left, const char* right )
37  : BObjectImp( OTString ), value_( std::string( left ) + std::string( right ) )
38  {
39  }
40 
41  String( const String& left, const String& right )
42  : BObjectImp( OTString ), value_( left.value_ + right.value_ )
43  {
44  }
45  explicit String( BObjectImp& objimp );
46  String( const String& str ) : BObjectImp( OTString ), value_( str.value_ ) {}
47  static BObjectImp* unpack( const char* pstr );
48  static BObjectImp* unpack( std::istream& is );
49  static BObjectImp* unpackWithLen( std::istream& is );
50  virtual std::string pack() const POL_OVERRIDE;
51  virtual void packonto( std::ostream& os ) const POL_OVERRIDE;
52  static void packonto( std::ostream& os, const std::string& value );
53  virtual BObjectImp* copy() const POL_OVERRIDE { return new String( *this ); }
54  virtual size_t sizeEstimate() const POL_OVERRIDE;
55 
56  // FIXME: Possibly eliminate this later and have [ ] operator support?
57  // Or stick to functions, overload them with other substring getting methods.
58  String* StrStr( int begin, int len );
59  // FIXME: Possibly upgrade this later with overload functions in order to support different
60  // trim methods, or add ELTrim, etc?
61  String* ETrim( const char* CRSet, int type );
62  void EStrReplace( String* str1, String* str2 );
63  void ESubStrReplace( String* replace_with, unsigned int index, unsigned int len );
64 
65  void set( char* newstr ); /* String now owns newstr */
66  const char* data() const { return value_.c_str(); }
67  const std::string& value() const { return value_; }
68  size_t length() const { return value_.length(); }
69  void toUpper( void );
70  void toLower( void );
71 
72  virtual ~String() {}
73  String& operator=( const char* s )
74  {
75  value_ = s;
76  return *this;
77  }
78  String& operator=( const String& str )
79  {
80  value_ = str.value_;
81  return *this;
82  }
83  void copyvalue( const String& str ) { value_ = str.value_; }
84  operator const char*() const { return value_.data(); }
85  void remove( const char* s );
86  virtual bool isTrue() const POL_OVERRIDE { return !value_.empty(); }
87 
88 public:
89  virtual BObjectImp* selfPlusObjImp( const BObjectImp& objimp ) const POL_OVERRIDE;
90  virtual BObjectImp* selfPlusObj( const BObjectImp& objimp ) const POL_OVERRIDE;
91  virtual BObjectImp* selfPlusObj( const BLong& objimp ) const POL_OVERRIDE;
92  virtual BObjectImp* selfPlusObj( const Double& objimp ) const POL_OVERRIDE;
93  virtual BObjectImp* selfPlusObj( const String& objimp ) const POL_OVERRIDE;
94  virtual BObjectImp* selfPlusObj( const ObjArray& objimp ) const POL_OVERRIDE;
95  virtual void selfPlusObjImp( BObjectImp& objimp, BObject& obj ) POL_OVERRIDE;
96  virtual void selfPlusObj( BObjectImp& objimp, BObject& obj ) POL_OVERRIDE;
97  virtual void selfPlusObj( BLong& objimp, BObject& obj ) POL_OVERRIDE;
98  virtual void selfPlusObj( Double& objimp, BObject& obj ) POL_OVERRIDE;
99  virtual void selfPlusObj( String& objimp, BObject& obj ) POL_OVERRIDE;
100  virtual void selfPlusObj( ObjArray& objimp, BObject& obj ) POL_OVERRIDE;
101 
102  virtual BObjectImp* selfMinusObjImp( const BObjectImp& objimp ) const POL_OVERRIDE;
103  virtual BObjectImp* selfMinusObj( const BObjectImp& objimp ) const POL_OVERRIDE;
104  virtual BObjectImp* selfMinusObj( const BLong& objimp ) const POL_OVERRIDE;
105  virtual BObjectImp* selfMinusObj( const Double& objimp ) const POL_OVERRIDE;
106  virtual BObjectImp* selfMinusObj( const String& objimp ) const POL_OVERRIDE;
107  virtual BObjectImp* selfMinusObj( const ObjArray& objimp ) const POL_OVERRIDE;
108  virtual void selfMinusObjImp( BObjectImp& objimp, BObject& obj ) POL_OVERRIDE;
109  virtual void selfMinusObj( BObjectImp& objimp, BObject& obj ) POL_OVERRIDE;
110  virtual void selfMinusObj( BLong& objimp, BObject& obj ) POL_OVERRIDE;
111  virtual void selfMinusObj( Double& objimp, BObject& obj ) POL_OVERRIDE;
112  virtual void selfMinusObj( String& objimp, BObject& obj ) POL_OVERRIDE;
113  virtual void selfMinusObj( ObjArray& objimp, BObject& obj ) POL_OVERRIDE;
114 
115  virtual BObjectRef OperSubscript( const BObject& obj ) POL_OVERRIDE;
116  virtual BObjectRef OperMultiSubscript( std::stack<BObjectRef>& indices ) POL_OVERRIDE;
117  virtual BObjectRef OperMultiSubscriptAssign( std::stack<BObjectRef>& indices,
118  BObjectImp* target ) POL_OVERRIDE;
119 
120  int find( int begin, const char* target );
121  unsigned int alnumlen() const;
122  unsigned int SafeCharAmt() const;
123 
124  void reverse();
125 
126  virtual BObjectImp* array_assign( BObjectImp* idx, BObjectImp* target, bool copy ) POL_OVERRIDE;
127  int find( char* s, int* posn );
128 
129  virtual std::string getStringRep() const POL_OVERRIDE { return value_; }
130  virtual std::string getFormattedStringRep() const POL_OVERRIDE { return "\"" + value_ + "\""; }
131  virtual void printOn( std::ostream& ) const POL_OVERRIDE;
132 
133 protected:
134  virtual bool operator==( const BObjectImp& objimp ) const POL_OVERRIDE;
135  virtual bool operator<( const BObjectImp& objimp ) const POL_OVERRIDE;
136 
137  virtual BObjectImp* call_method( const char* methodname, Executor& ex ) POL_OVERRIDE;
138  virtual BObjectImp* call_method_id( const int id, Executor& ex,
139  bool forcebuiltin = false ) POL_OVERRIDE;
140 
141 private:
142  std::string value_;
143  String* midstring( int begin, int len ) const;
144  friend class SubString;
145 };
146 
147 class ConstString : public String
148 {
149 public:
150  explicit ConstString( const std::string& str ) : String( str ) {}
151 };
152 }
153 }
154 #endif
virtual void printOn(std::ostream &) const POL_OVERRIDE
Definition: objstrm.cpp:63
#define POL_OVERRIDE
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: str.cpp:160
const std::string & value() const
Definition: impstr.h:67
BObjectType type() const
Definition: bobject.h:358
virtual ~String()
Definition: impstr.h:72
String(const String &left, const String &right)
Definition: impstr.h:41
String(const std::string &str, std::string::size_type pos, std::string::size_type n)
Definition: impstr.h:32
int find(int begin, const char *target)
Definition: str.cpp:170
void EStrReplace(String *str1, String *str2)
Definition: str.cpp:84
String(const std::string &str)
Definition: impstr.h:31
virtual void packonto(std::ostream &os) const POL_OVERRIDE
Definition: str.cpp:104
virtual std::string getStringRep() const POL_OVERRIDE
Definition: impstr.h:129
STL namespace.
BObjectImp base
Definition: impstr.h:25
virtual bool operator<(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:355
virtual BObjectRef OperMultiSubscriptAssign(std::stack< BObjectRef > &indices, BObjectImp *target) POL_OVERRIDE
Definition: str.cpp:427
String(const char *str)
Definition: impstr.h:30
void ESubStrReplace(String *replace_with, unsigned int index, unsigned int len)
Definition: str.cpp:94
void toUpper(void)
Definition: str.cpp:368
virtual BObjectImp * selfPlusObj(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:231
String & operator=(const char *s)
Definition: impstr.h:73
String * midstring(int begin, int len) const
Definition: str.cpp:363
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:290
String(const char *left, const char *right)
Definition: impstr.h:36
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:345
virtual BObjectRef OperSubscript(const BObject &obj) POL_OVERRIDE
Definition: str.cpp:555
virtual BObjectImp * copy() const POL_OVERRIDE
Definition: impstr.h:53
friend class SubString
Definition: impstr.h:144
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
Definition: str.cpp:731
String & operator=(const String &str)
Definition: impstr.h:78
String * ETrim(const char *CRSet, int type)
Definition: str.cpp:42
virtual BObjectImp * selfPlusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:227
virtual BObjectRef OperMultiSubscript(std::stack< BObjectRef > &indices) POL_OVERRIDE
Definition: str.cpp:496
unsigned int alnumlen() const
Definition: str.cpp:182
unsigned int SafeCharAmt() const
Definition: str.cpp:192
static BObjectImp * unpack(const char *pstr)
Definition: str.cpp:113
String(const String &str)
Definition: impstr.h:46
String * StrStr(int begin, int len)
Definition: str.cpp:37
ConstString(const std::string &str)
Definition: impstr.h:150
virtual bool isTrue() const POL_OVERRIDE
Definition: impstr.h:86
virtual BObjectImp * call_method(const char *methodname, Executor &ex) POL_OVERRIDE
Definition: str.cpp:723
static BObjectImp * unpackWithLen(std::istream &is)
Definition: str.cpp:126
virtual BObjectImp * selfMinusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:286
std::string value_
Definition: impstr.h:142
virtual std::string pack() const POL_OVERRIDE
Definition: str.cpp:99
void toLower(void)
Definition: str.cpp:376
virtual BObjectImp * array_assign(BObjectImp *idx, BObjectImp *target, bool copy) POL_OVERRIDE
Definition: str.cpp:384
virtual std::string getFormattedStringRep() const POL_OVERRIDE
Definition: impstr.h:130
const char * data() const
Definition: impstr.h:66
Definition: berror.cpp:12
size_t length() const
Definition: impstr.h:68
void copyvalue(const String &str)
Definition: impstr.h:83