Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Cstring.cc
Go to the documentation of this file.
1 // Cstring: C String Wrapper
2 //
3 // Project: Objexx Fortran Compatibility Library (ObjexxFCL)
4 //
5 // Version: 3.0.0
6 //
7 // Language: C++
8 //
9 // Copyright (c) 2000-2009 Objexx Engineering, Inc. All Rights Reserved.
10 // Use of this source code or any derivative of it is restricted by license.
11 // Licensing is available from Objexx Engineering, Inc.: http://objexx.com Objexx@objexx.com
12 
13 
14 // ObjexxFCL Headers
15 #include <ObjexxFCL/Cstring.hh>
16 
17 // C++ Headers
18 #include <cctype>
19 #include <iostream>
20 
21 
22 namespace ObjexxFCL {
23 
24 
25 // Constants
26 char const SPACE( ' ' );
27 char const TAB( '\t' );
28 char const NULLC( '\000' );
29 
30 
31 // Cstring
32 
33 
34  /// @brief Has Any Character of a Cstring?
35  bool
36  Cstring::has_any_of( Cstring const & s ) const
37  {
38  size_type const s_len( s.length() );
39  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
40  for ( size_type j = 0; j < s_len; ++j ) {
41  if ( str_[ i ] == s.str_[ j ] ) return true;
42  }
43  }
44  return false; // No matches
45  }
46 
47 
48  /// @brief Has Any Character of a cstring?
49  bool
51  {
52  size_type const s_len( std::strlen( s ) );
53  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
54  for ( size_type j = 0; j < s_len; ++j ) {
55  if ( str_[ i ] == s[ j ] ) return true;
56  }
57  }
58  return false; // No matches
59  }
60 
61 
62  /// @brief Has Any Character of a std::string?
63  bool
64  Cstring::has_any_of( std::string const s ) const
65  {
66  size_type const s_len( s.length() );
67  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
68  for ( size_type j = 0; j < s_len; ++j ) {
69  if ( str_[ i ] == s[ j ] ) return true;
70  }
71  }
72  return false; // No matches
73  }
74 
75 
76  /// @brief Has a Character?
77  bool
78  Cstring::has_any_of( char const c ) const
79  {
80  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
81  if ( str_[ i ] == c ) return true;
82  }
83  return false; // No matches
84  }
85 
86 
87  /// @brief Has a Character?
88  bool
89  Cstring::has( char const c ) const
90  {
91  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
92  if ( str_[ i ] == c ) return true;
93  }
94  return false; // No matches
95  }
96 
97 
98  /// @brief Length Space-Trimmed
101  {
102  for ( size_type i = std::strlen( str_ ); i > 0; --i ) {
103  if ( str_[ i - 1 ] != SPACE ) return i;
104  }
105  return 0;
106  }
107 
108 
109  /// @brief Length Whitespace-Trimmed
112  {
113  for ( size_type i = std::strlen( str_ ); i > 0; --i ) {
114  char const c( str_[ i - 1 ] );
115  if ( ( c != SPACE ) && ( c != TAB ) && ( c != NULLC ) ) return i;
116  }
117  return 0;
118  }
119 
120 
121  /// @brief Find First Occurrence of a Character
123  Cstring::find( char const c ) const
124  {
125  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
126  if ( str_[ i ] == c ) return i;
127  }
128  return npos; // No matches
129  }
130 
131 
132  /// @brief Find Last Occurrence of a Character
134  Cstring::find_last( char const c ) const
135  {
136  for ( size_type i = std::strlen( str_ ); i > 0; --i ) {
137  if ( str_[ i - 1 ] == c ) return i;
138  }
139  return npos; // No matches
140  }
141 
142 
143  /// @brief Lowercase
144  Cstring &
146  {
147  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
148  str_[ i ] = std::tolower( str_[ i ] );
149  }
150  return *this;
151  }
152 
153 
154  /// @brief Uppercase
155  Cstring &
157  {
158  for ( size_type i = 0; i < std::strlen( str_ ); ++i ) {
159  str_[ i ] = std::toupper( str_[ i ] );
160  }
161  return *this;
162  }
163 
164 
165  /// @brief Left Justify
166  Cstring &
168  {
169  size_type const len( std::strlen( str_ ) );
170  for ( size_type i = 0; i < len; ++i ) {
171  if ( str_[ i ] != SPACE ) {
172  if ( i > 0 ) {
173  std::memmove( str_, str_ + i, len - i );
174  std::memset( str_ + len - i, SPACE, i );
175  }
176  return *this;
177  }
178  }
179  return *this;
180  }
181 
182 
183  /// @brief Right Justify
184  Cstring &
186  {
187  size_type const len( std::strlen( str_ ) );
188  for ( size_type i = len; i > 0; --i ) {
189  if ( str_[ i - 1 ] != SPACE ) {
190  if ( i < len ) {
191  std::memmove( str_ + len - i, str_, i );
192  std::memset( str_, SPACE, len - i );
193  }
194  return *this;
195  }
196  }
197  return *this;
198  }
199 
200 
201  /// @brief Center
202  Cstring &
204  {
205  left_justify();
206  size_type const len_t( len_trim() );
207  size_type const pad( ( length() - len_t ) / 2 );
208  if ( pad > 0 ) {
209  std::memmove( str_ + pad, str_, len_t );
210  std::memset( str_, SPACE, pad );
211  }
212  return *this;
213  }
214 
215 
216  /// @brief Compress Out Whitespace
217  Cstring &
219  {
220  size_type const len( std::strlen( str_ ) );
221  size_type j = 0;
222  for ( size_type i = 0; i < len; ++i ) {
223  char const c( str_[ i ] );
224  if ( ( c != SPACE ) && ( c != TAB ) && ( c != NULLC ) ) str_[ j++ ] = c;
225  }
226  if ( j < len ) std::memset( str_ + j, SPACE, len - j );
227  return *this;
228  }
229 
230 
231  /// @brief Cstring == Cstring Case-Insensitively?
232  bool
233  equali( Cstring const & s, Cstring const & t )
234  {
235  typedef Cstring::size_type size_type;
236  size_type const s_len( s.length() );
237  if ( s_len != t.length() ) {
238  return false;
239  } else {
240  for ( size_type i = 0; i < s_len; ++i ) {
241  if ( std::tolower( s.str_[ i ] ) != std::tolower( t.str_[ i ] ) ) return false;
242  }
243  return true;
244  }
245  }
246 
247 
248  /// @brief Cstring == cstring Case-Insensitively?
249  bool
250  equali( Cstring const & s, c_cstring const t )
251  {
252  typedef Cstring::size_type size_type;
253  size_type const s_len( s.length() );
254  if ( s_len != std::strlen( t ) ) {
255  return false;
256  } else {
257  for ( size_type i = 0; i < s_len; ++i ) {
258  if ( std::tolower( s.str_[ i ] ) != std::tolower( t[ i ] ) ) return false;
259  }
260  return true;
261  }
262  }
263 
264 
265  /// @brief cstring == Cstring Case-Insensitively?
266  bool
267  equali( c_cstring const s, Cstring const & t )
268  {
269  typedef Cstring::size_type size_type;
270  size_type const s_len( std::strlen( s ) );
271  if ( s_len != t.length() ) {
272  return false;
273  } else {
274  for ( size_type i = 0; i < s_len; ++i ) {
275  if ( std::tolower( s[ i ] ) != std::tolower( t.str_[ i ] ) ) return false;
276  }
277  return true;
278  }
279  }
280 
281 
282  /// @brief Cstring == std::string Case-Insensitively?
283  bool
284  equali( Cstring const & s, std::string const & t )
285  {
286  typedef Cstring::size_type size_type;
287  size_type const s_len( s.length() );
288  if ( s_len != t.length() ) {
289  return false;
290  } else {
291  for ( size_type i = 0; i < s_len; ++i ) {
292  if ( std::tolower( s.str_[ i ] ) != std::tolower( t[ i ] ) ) return false;
293  }
294  return true;
295  }
296  }
297 
298 
299  /// @brief std::string == Cstring Case-Insensitively?
300  bool
301  equali( std::string const & s, Cstring const & t )
302  {
303  typedef Cstring::size_type size_type;
304  size_type const s_len( s.length() );
305  if ( s_len != t.length() ) {
306  return false;
307  } else {
308  for ( size_type i = 0; i < s_len; ++i ) {
309  if ( std::tolower( s[ i ] ) != std::tolower( t.str_[ i ] ) ) return false;
310  }
311  return true;
312  }
313  }
314 
315 
316  /// @brief Cstring == char Case-Insensitively?
317  bool
318  equali( Cstring const & s, char const c )
319  {
320  return ( ( s.length() == 1 ) && ( std::tolower( s.str_[ 0 ] ) == std::tolower( c ) ) );
321  }
322 
323 
324  /// @brief char == Cstring Case-Insensitively?
325  bool
326  equali( char const c, Cstring const & s )
327  {
328  return ( ( s.length() == 1 ) && ( std::tolower( s.str_[ 0 ] ) == std::tolower( c ) ) );
329  }
330 
331 
332  /// @brief Output to Stream
333  std::ostream &
334  operator <<( std::ostream & stream, Cstring const & s )
335  {
336  for ( Cstring::size_type i = 0; i < std::strlen( s.str_ ); ++i ) {
337  stream << s.str_[ i ];
338  }
339  return stream;
340  }
341 
342 
343  /// @brief Input from Stream
344  std::istream &
345  operator >>( std::istream & stream, Cstring & s )
346  {
347  std::string buffer;
348  stream >> buffer;
349  s = buffer;
350  return stream;
351  }
352 
353 
354 // Cstring
355 
356 
357 // Static Data Member Definitions
358 
359 #ifndef _MSC_VER // Microsoft Visual C++ doesn't need or like these
360 
362 
363 #endif // _MSC_VER
364 
365 
366 } // namespace ObjexxFCL
std::istream & operator>>(std::istream &stream, byte &b)
Stream Input.
Definition: byte.hh:409
Cstring & compress()
Compress Out Whitespace.
Definition: Cstring.cc:218
bool has(char const c) const
Has a Character?
Definition: Cstring.cc:89
char const TAB( '\t')
Cstring & left_justify()
Left Justify.
Definition: Cstring.cc:167
size_type find(char const c) const
Find First Occurrence of a Character.
Definition: Cstring.cc:123
size_type len_trim() const
Length Space-Trimmed.
Definition: Cstring.cc:100
size_type len_trim_whitespace() const
Length Whitespace-Trimmed.
Definition: Cstring.cc:111
Cstring & uppercase()
Uppercase.
Definition: Cstring.cc:156
Cstring & center()
Center.
Definition: Cstring.cc:203
bool has_any_of(Cstring const &s) const
Has Any Character of a Cstring?
Definition: Cstring.cc:36
char const * c_cstring
bool equali(char const c, char const d)
char == char Case-Insensitively
size_type length() const
Length.
Definition: Cstring.hh:361
Cstring: C String Wrapper.
Definition: Cstring.hh:47
std::string & pad(std::string &s, std::string::size_type const len)
Pad a string to a Specified Length.
Cstring & lowercase()
Lowercase.
Definition: Cstring.cc:145
char * str_
String.
Definition: Cstring.hh:900
Cstring & right_justify()
Right Justify.
Definition: Cstring.cc:185
char const NULLC( '\000')
std::size_t size_type
Definition: Cstring.hh:55
size_type find_last(char const c) const
Find Last Occurrence of a Character.
Definition: Cstring.cc:134
static size_type const npos
Definition: Cstring.hh:893
size_type len() const
Length.
Definition: Cstring.hh:370
std::ostream & operator<<(std::ostream &stream, CArray< T > const &a)
stream << CArray
Definition: CArray.io.hh:33
char const SPACE( ' ')