SystemC  2.3.1
Accellera SystemC proof-of-concept library
sc_string.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  *****************************************************************************/
17 
18 /*****************************************************************************
19 
20  sc_string.h -- Implementation of a simple string class.
21 
22  Original Author: Stan Y. Liao, Synopsys, Inc.
23 
24  CHANGE LOG AT END OF FILE
25  *****************************************************************************/
26 
27 // $Log: sc_string.h,v $
28 // Revision 1.3 2011/08/26 20:46:19 acg
29 // Andy Goodrich: moved the modification log to the end of the file to
30 // eliminate source line number skew when check-ins are done.
31 //
32 #ifndef SC_STRING_H
33 #define SC_STRING_H
34 
35 
36 #include "sysc/utils/sc_iostream.h"
37 #include "sysc/utils/sc_report.h"
38 
39 namespace sc_dt {
40  class sc_string_old;
41 }
42 
43 #ifdef SC_USE_SC_STRING_OLD
44  typedef sc_dt::sc_string_old sc_string;
45 #endif
46 #ifdef SC_USE_STD_STRING
47  typedef ::std::string sc_string;
48 #endif
49 
50 namespace sc_dt {
51 
52 // forward class declarations
53 class sc_string_rep;
54 
55 // friend operator declarations
56 sc_string_old operator + ( const char* s, const sc_string_old& t );
57 
58 
59 // ----------------------------------------------------------------------------
60 // CLASS : sc_string
61 //
62 // String class (yet another).
63 // ----------------------------------------------------------------------------
64 
66 {
69 
70 public:
71 
72  // constructors
73 
74  explicit sc_string_old( int size = 16 );
75  sc_string_old( const char* s );
76  sc_string_old( const char* s, int n ); // get first n chars from the string
77  sc_string_old( const sc_string_old& s );
78 
79 
80  // destructor
81 
83 
84 
85  // concatenation and assignment
86 
87  sc_string_old& operator = ( const char* s );
89 
90  sc_string_old& operator += ( const char* s );
91  sc_string_old& operator += ( char c );
93 
94  sc_string_old operator + ( const char* s ) const;
95  sc_string_old operator + ( char c ) const;
96  sc_string_old operator + ( const sc_string_old& s ) const;
97 
98  friend sc_string_old operator + ( const char* s, const sc_string_old& t );
99 
100 
101  // returns substring [first,last]
102 
103  sc_string_old substr( int first, int last ) const;
104 
105 
106  // string comparison operators
107 
108  bool operator == ( const char* s ) const;
109  bool operator != ( const char* s ) const;
110  bool operator < ( const char* s ) const;
111  bool operator <= ( const char* s ) const;
112  bool operator > ( const char* s ) const;
113  bool operator >= ( const char* s ) const;
114  bool operator == ( const sc_string_old& s ) const;
115  bool operator != ( const sc_string_old& s ) const;
116  bool operator < ( const sc_string_old& s ) const;
117  bool operator <= ( const sc_string_old& s ) const;
118  bool operator > ( const sc_string_old& s ) const;
119  bool operator >= ( const sc_string_old& s ) const;
120 
121  //
122  // returns length of the string (excluding trailing \0)
123  //
124  int length() const;
125 
126  //
127  // returns c-style string
128  //
129  const char* c_str() const;
130  //
131  // returns c-style string
132  //
133  operator const char*() const;
134  //
135  // returns character at "index" position
136  //
137  char operator[](int index) const;
138  //
139  // l-value subscript
140  //
141  char& operator[](int index);
142 
143  // formatted string (see printf description)
144  static sc_string_old to_string(const char* format, ...);
145  //
146  // conveniece formatting functions for common types
147  // e.g. sc_string_old("a=%d, s is %s").fmt(1).fmt("string")
148  // should produce: a=1, s is string
149  // it should be safe: if less arguments specified
150  // it should print %specifier; extra arguments should be ignored
151  // TODO: if the type of the argument is incompatible with format
152  // specifier it should be ignored
153  //
154  // must have it inlined because of some compilers
155  template<class T> sc_string_old& fmt(const T& t)
156  {
157  // search %
158  int index;
159  int last_char = length()-1;
160  sc_string_old temp(*this);
161  do
162  {
163  index = temp.pos("%");
164  if(index == last_char)
165  return *this;
166  temp = substr(index,last_char);
167  } while(temp[0] != '%');
168  int f_len = (int)temp.fmt_length(); // length of format field
169  temp = to_string(substr(0,index+f_len-1).c_str(),t);
170  return (*this) = temp + substr(index+f_len,last_char);
171  }
172  sc_string_old& fmt(const sc_string_old& s);
173  //
174  // find position of substring in this string
175  // returns -1 if not found
176  //
177  int pos(const sc_string_old& sub_string)const;
178  //
179  // remove "count" characters from "index"
180  //
181  sc_string_old& remove(unsigned index, unsigned length);
182  //
183  // insert "substring" before "index"
184  //
185  sc_string_old& insert(const sc_string_old& sub_string, unsigned index);
186  //
187  // returns true if the character at byte index in this string matches
188  // any character in the delimiters string
189  //
190  bool is_delimiter(const sc_string_old& str, unsigned index)const;
191  //
192  // returns true if string contains the character
193  //
194  bool contains(char c)const;
195  //
196  // produce upper case string from this one
197  //
198  sc_string_old uppercase()const;
199  //
200  // produce lower case string from this one
201  //
202  sc_string_old lowercase()const;
203  //
204  // legacy methods
205  //
206  static sc_string_old make_str(long n);
207  void set( int index, char c );
208  int cmp( const char* s ) const;
209  int cmp( const sc_string_old& s ) const;
210 
211 
212  void print( systemc_ostream& os = ::std::cout ) const;
213 
214 private:
215 
216  sc_string_old( sc_string_rep* r );
217 
218  sc_string_rep* rep;
219 
220  void test(int position)const;
221  unsigned fmt_length()const;
222 };
223 
224 
225 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
226 
227 inline
230 {
231  a.print( os );
232  return os;
233 }
234 
235 } // namespace sc_dt
236 
237 // Revision 1.2 2011/02/18 20:38:44 acg
238 // Andy Goodrich: Updated Copyright notice.
239 //
240 // Revision 1.1.1.1 2006/12/15 20:20:06 acg
241 // SystemC 2.3
242 //
243 // Revision 1.4 2006/05/08 17:50:51 acg
244 // Andy Goodrich: added David Long's forward declarations for friend
245 // functions, methods, and operators to keep the Microsoft compiler happy.
246 //
247 // Revision 1.3 2006/01/13 18:53:11 acg
248 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
249 // the source.
250 //
251 
252 #endif
::std::istream systemc_istream
Definition: sc_iostream.h:46
sc_string_old & operator=(const char *s)
bool operator!=(const char *s) const
::std::ostream systemc_ostream
Definition: sc_iostream.h:47
sc_signed operator+(const sc_unsigned &u, const sc_signed &v)
static sc_string_old to_string(const char *format,...)
bool operator>(const char *s) const
friend systemc_istream & operator>>(systemc_istream &is, sc_string_old &a)
friend systemc_ostream & operator<<(systemc_ostream &os, const sc_string_old &a)
Definition: sc_string.h:229
sc_string_old & fmt(const T &t)
Definition: sc_string.h:155
bool operator<(const char *s) const
char operator[](int index) const
sc_string_old & insert(const sc_string_old &sub_string, unsigned index)
bool operator==(const char *s) const
bool contains(char c) const
sc_string_old(int size=16)
sc_string_old lowercase() const
void print(systemc_ostream &os=::std::cout) const
sc_string_old operator+(const char *s) const
static sc_string_old make_str(long n)
sc_string_old & operator+=(const char *s)
int cmp(const char *s) const
void set(int index, char c)
sc_string_old substr(int first, int last) const
int length() const
int pos(const sc_string_old &sub_string) const
sc_string_old uppercase() const
const char * c_str() const
bool is_delimiter(const sc_string_old &str, unsigned index) const
inline::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:386
bool operator<=(const char *s) const
bool operator>=(const char *s) const