NeoPZ
pzstring.cpp
Go to the documentation of this file.
1 
6 #include "pzstring.h"
7 
8 #include <stdio.h>
9 using namespace std;
10 
12 {
13  // NOTHING TO DO HERE!
14 }
15 
16 TPZString::TPZString(char const * source)
17 {
18  size_t len = strlen(source);
19  Resize(len + 1);
20  strncpy(fStore, source, NElements());
21 
22  // although the TPZStack class already stores the length, the null
23  // character is necessary to export string as a null character
24  // ended string
25  fStore[len] = '\0';
26 }
27 
28 
29 TPZString::TPZString(const int size)
30 {
31  Resize(size);
32 }
33 
34 TPZString::TPZString(const char chr)
35 {
36  Resize(2);
37  fStore[0] = chr;
38  fStore[1] = '\0';
39 }
40 
41 TPZString TPZString::operator + (const char * increment) const
42 {
43  TPZString newstring(* this);
44  newstring.Append(increment);
45  return newstring;
46 }
47 
48 TPZString TPZString::operator + (const TPZString & increment) const
49 {
50  TPZString newstring(* this);
51  newstring.Append(increment);
52  return newstring;
53 }
54 
55 void TPZString::operator += (const char increment)
56 {
57  this->Append(increment);
58 }
59 
60 void TPZString::operator += (const char * increment)
61 {
62  this->Append(increment);
63 }
64 
65 TPZString::operator const char * () const
66 {
67  return Str();
68 }
69 
70 const char * TPZString::Str() const
71 {
72  if (fNElements > 0)
73  {
74  return fStore;
75  }
76 
77  return NULL;
78 }
79 
80 size_t TPZString::Length() const
81 {
82  if (fNElements == 0) return 0;
83  return strlen(fStore);
84 }
85 
86 void TPZString::Append(const char TailIncrement)
87 {
88  int len = Length();
89  // if the allocated memory is less than the length +1 (null char)
90  // +1 (new char), reallocates it
91  if (fNElements < len + 2)
92  {
93  Push('\0');
94  }
95  else
96  {
97  fStore[len + 1] = '\0';
98  }
99 
100  fStore[len] = TailIncrement;
101 }
102 
103 void TPZString::Append(const char * TailIncrement)
104 {
105  int OldLength = Length();
106  size_t len = strlen(TailIncrement);
107 
108  if (fNElements < OldLength + len + 1) Resize(OldLength + len + 1); // the 1 stands for the null char
109 
110  strncpy(fStore + OldLength, TailIncrement,fNElements-OldLength);
111  fStore[Length()] = '\0'; // just to ensure the string contains a null character
112 }
113 
114 TPZString TPZString::SubStr(const int start, const int end) const
115 {
116  size_t len = Length();
117  if (start > len || start > end)
118  {
119  TPZString newstring;
120  return newstring;
121  }
122 
123  int startpos = start, endpos = end, i;
124 
125  if (startpos < 0) startpos = 0;
126 
127  if (endpos > len) endpos = len - 1; // null ending character index
128 
129  TPZString newstring(endpos - startpos + 2);
130  for (i = startpos; i <= endpos; i++) newstring[i - startpos] = fStore[i];
131 
132  newstring[newstring.NElements() - 1] = '\0';
133  return newstring;
134 }
135 
137 {
138  Resize(0);
139 }
140 
142 {
143  int len = Length();
144 
145  if (len + 1 < fNElements) Resize(len + 1);
146 }
147 
149 {
150  unsigned int i, newpos = 0;
151  const unsigned int length = Length();
152  char current = '0', next = '0';
153  for (i = 0; i < length - 1; i++)
154  {
155  current = fStore[i];
156  if (current == ' ')
157  {
158  if (i)
159  {
160  fStore[newpos] = ' ';
161  newpos++;
162  }
163  next = fStore[i + 1];
164  while (next == ' ')
165  {
166  i++;
167  next = fStore[i + 1];
168  }
169  fStore[newpos] = next;
170  newpos++;
171  }
172  else
173  {
174  if (newpos != i) fStore[newpos] = current;
175  newpos++;
176  }
177  }
178  fStore[newpos] = '\0';
179 }
180 
181 int TPZString::Replace( const char * replace_str, const char *new_substr){
182  string newstring(fStore);
183  const int replace_len = strlen(replace_str);
184  int count = 0;
185  string::size_type pos = newstring.find(replace_str, 0);
186  while( pos!= string::npos) {
187  newstring.replace(pos, replace_len, new_substr);
188  pos = newstring.find(replace_str, 0);
189  count++;
190  }
191  if(count) strncpy(fStore, newstring.c_str(), fNElements);
192  return(count);
193 }
194 
195 int TPZString::Find(const char * find_str){
196  string fchr(fStore);
197  return fchr.find(find_str, 0);
198 }
void Optimize()
Internally allocates the exact string size to store it.
Definition: pzstring.cpp:141
int Replace(const char *old_str, const char *new_str)
Replace the subset of string. Return the times of replacement.
Definition: pzstring.cpp:181
void Append(TPZVec< TVar > &u1, TPZVec< TVar > &u2, TPZVec< TVar > &u12)
Append u2 vector after u1 vector in u12 vector.
void operator+=(const char *increment)
Appends a string at the tail. Resizes the TPZString if necessary.
Definition: pzstring.cpp:60
const char * Str() const
Explicitly convertes a TPZString into a const null ended char string.
Definition: pzstring.cpp:70
void Empty()
Empties the string.
Definition: pzstring.cpp:136
String implementation.
size_t Length() const
Similar to strlen(string). Also returns the number of non-null characters.
Definition: pzstring.cpp:80
void Append(const char TailIncrement)
Appends a character at the end. Resizes if necessary.
Definition: pzstring.cpp:86
TPZString()
Default Constructor.
Definition: pzstring.cpp:11
Implements strings as stack. Utility.
Definition: pzstring.h:18
TPZString SubStr(const int start, const int end) const
Returns a subset string.
Definition: pzstring.cpp:114
TPZString operator+(const char *increment) const
Concatenates the &#39;this&#39; string with another character string (increment)
Definition: pzstring.cpp:41
#define Length(wts)
Definition: tpzintrulet.cpp:69
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
void SimplifyWhiteSpace()
Remove the repeat white spaces.
Definition: pzstring.cpp:148
int Find(const char *find_str)
Find the positions of the first occurence of the find string.
Definition: pzstring.cpp:195