TRF Language Model
wb-string.cpp
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #include "wb-string.h"
19 
20 namespace wb
21 {
22  String::String(int p_nLen /*= cn_default_str_len*/) :m_pBuffer(NULL), m_nBufSize(0), m_tokPtr(NULL)
23  {
24  Reset(p_nLen+1);
25  m_pBuffer[0] = '\0';
26  }
27  String::String(const char *p_str) : m_pBuffer(NULL), m_nBufSize(0), m_tokPtr(NULL)
28  {
29  int nlen = strlen(p_str);
30  Reset(nlen + 1);
31  memcpy(m_pBuffer, p_str, nlen + 1);
32  }
33  String::String(const char* p_str, int nLen) :m_pBuffer(NULL), m_nBufSize(0), m_tokPtr(NULL)
34  {
35  Reset(nLen + 1);
36  nLen = min(nLen, (int)strlen(p_str));
37  memcpy(m_pBuffer, p_str, nLen);
38  m_pBuffer[nLen] = '\0';
39  }
40  String::String(char c) :m_pBuffer(NULL), m_nBufSize(0), m_tokPtr(NULL)
41  {
42  Reset(2);
43  m_pBuffer[0] = c;
44  m_pBuffer[1] = '\0';
45  }
46  String::String(const String &p_str) :m_pBuffer(NULL), m_nBufSize(0), m_tokPtr(NULL)
47  {
48  Reset(p_str.m_nBufSize);
49  memcpy(m_pBuffer, p_str.m_pBuffer, m_nBufSize);
50  }
52  {
53  delete[]m_pBuffer;
54  m_nBufSize = 0;
55  }
56  char* String::Reset(int nLen)
57  {
58  if (nLen > m_nBufSize) {
59  char *pNew = new char[nLen];
60  if (m_pBuffer) {
61  memcpy(pNew, m_pBuffer, sizeof(char)*m_nBufSize);
62  delete[] m_pBuffer;
63  }
64  m_pBuffer = pNew;
65  m_nBufSize = nLen;
66  }
67  return m_pBuffer;
68  }
69  const char* String::Format(const char* p_pMessage, ...)
70  {
71  char strBuffer[cn_default_max_len];
72  va_list vaParams;
73  va_start(vaParams, p_pMessage);
74 
75  _vsnprintf(strBuffer, cn_default_max_len, p_pMessage, vaParams);
76 
77  int nLen = strlen(strBuffer);
78  Reset(nLen + 1);
79  memcpy(m_pBuffer, strBuffer, sizeof(char)*(nLen + 1));
80 
81  return m_pBuffer;
82  }
83  void String::operator = (const String &p_str)
84  {
85  Reset(p_str.m_nBufSize);
86  memcpy(m_pBuffer, p_str.m_pBuffer, p_str.m_nBufSize);
87  }
88 
89  void String::operator = (const char *pStr)
90  {
91  int nLen = strlen(pStr);
92  Reset(nLen + 1);
93  memcpy(m_pBuffer, pStr, nLen + 1);
94  }
95  void String::DeleteSub(int nLocal, int nLen)
96  {
97  m_pBuffer[nLocal] = '\0';
98  *this += m_pBuffer + nLocal + nLen;
99  }
100  int String::Find(const char *sub)
101  {
102  char *p = strstr(m_pBuffer, sub);
103  if (p) {
104  return p - m_pBuffer;
105  }
106  return -1;
107  }
108  String String::Replace(const char *src, const char *rpl)
109  {
110  int nLocal = Find(src);
111  if (nLocal == -1) {
112  return "";
113  }
114 
115  String strnew(m_pBuffer, nLocal);
116  return strnew + rpl + &m_pBuffer[nLocal + strlen(src)];
117  }
118  char *String::TokBegin(const char *p)
119  {
120  m_tokPtr = m_pBuffer;
121  return TokSub(p);
122  }
123  char *String::TokSub(const char *p)
124  {
125  if (*m_tokPtr == '\0')
126  return NULL;
127 
128  char *pBeg = NULL;
129  for (; *m_tokPtr != '\0'; m_tokPtr++)
130  {
131  if (pBeg == NULL) {
132  //�ҵ���ʼ��
133  if (NULL == strchr(p, *m_tokPtr)) {
134  pBeg = m_tokPtr;
135  }
136  }
137  else {
138  //�ҵ�������
139  if (strchr(p, *m_tokPtr)) {
140  *m_tokPtr = '\0';
141  m_tokPtr++;
142  break;
143  }
144  }
145  }
146 
147  return pBeg;
148  }
149  void String::Split(Array<String> &aStrs, const char* delimiter)
150  {
151  char *psave = strdup(m_pBuffer); // save the string
152 
153  aStrs.Clean();
154  char *p = strtok(psave, delimiter);
155  while (p) {
156  aStrs.Add(p);
157  p = strtok(NULL, delimiter);
158  }
159 
160  free(psave); // free the string
161  }
163  {
164  char *p = strrchr(m_pBuffer, '.');
165  if (p)
166  return String(m_pBuffer, p - m_pBuffer);
167  return String(m_pBuffer);
168  }
169 
171  {
172  char *p = m_pBuffer;
173  while (*p = toupper(*p))
174  p++;
175  return m_pBuffer;
176  }
178  {
179  char *p = m_pBuffer;
180  while (*p = tolower(*p))
181  p++;
182  return m_pBuffer;
183  }
184 }
185 
186 
String(int p_nLen=cn_default_str_len)
store the top pointer. used in
Definition: wb-string.cpp:22
const char * Format(const char *p_pMessage,...)
format print to string
Definition: wb-string.cpp:69
a dynamic string class
Definition: wb-string.h:53
char * Tolower()
to lower
Definition: wb-string.cpp:177
define the class String
void Split(Array< String > &aStrs, const char *delimiter)
split to string array. Using strtok().
Definition: wb-string.cpp:149
const int cn_default_max_len
default maximum length
Definition: wb-string.h:41
String Replace(const char *src, const char *rpl)
replace
Definition: wb-string.cpp:108
void operator=(const String &p_str)
operator =
Definition: wb-string.cpp:83
char * Reset(int nLen)
Reset the string leng.
Definition: wb-string.cpp:56
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
Definition: wb-vector.h:258
void Add(T t)
Add a value to the tail of array.
Definition: wb-vector.h:242
char * TokSub(const char *p)
split next
Definition: wb-string.cpp:123
void DeleteSub(int nLocal, int nLen)
delete a sub-string
Definition: wb-string.cpp:95
char * TokBegin(const char *p)
split begin
Definition: wb-string.cpp:118
String FileName()
if the string is a path, this function return the file name.
Definition: wb-string.cpp:162
int Find(const char *sub)
Find.
Definition: wb-string.cpp:100
char * Toupper()
to upper
Definition: wb-string.cpp:170
define all the code written by Bin Wang.
Definition: wb-file.cpp:21