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