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