TRF Language Model
wb-string.h
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 
27 #pragma once
28 #include <cstring>
29 #include <algorithm>
30 #include <cstdarg> //include va_list
31 #include <string.h>
32 using namespace std;
33 #include "wb-vector.h"
34 #include "wb-linux.h"
35 
36 
37 namespace wb
38 {
39 
40  const int cn_default_str_len = 10;
41  const int cn_default_max_len = 32 * 1024;
42 
46 
53  class String
54  {
55  private:
56  char *m_pBuffer;
57  int m_nBufSize;
58 
59  char *m_tokPtr;
60  public:
61  String(int p_nLen = cn_default_str_len);
62  String(const char *p_str);
63  String(const char* p_str, int nLen);
64  String(char c);
65  String(const String &p_str);
66  ~String();
68  /* If the nLen<=m_nBufSize, then do nothing. \n
69  If the nLen>m_nBufSize, then re-alloc memory.
70  Return the buffer pointer.
71  */
72  char* Reset(int nLen);
74  char *GetBuffer() const { return m_pBuffer; }
76  char *End() const { return m_pBuffer + strlen(m_pBuffer) - 1; }
78  int GetSize() const { return m_nBufSize; }
80  int GetLength() const { return strlen(m_pBuffer); }
82  void Clean() { m_pBuffer[0] = '\0'; }
84  const char* Format(const char* p_pMessage, ...);
86  void operator = (const String &p_str);
88  void operator = (const char *pStr);
90  operator char*() const { return m_pBuffer; }
92  char &operator[] (int i) const { return m_pBuffer[i]; }
94  bool operator >(const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) > 0; }
96  bool operator < (const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) < 0; }
97  bool operator >= (const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) >= 0; }
98  bool operator <= (const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) <= 0; }
99  bool operator == (const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) == 0; }
100  bool operator != (const String &p_str) { return strcmp(m_pBuffer, p_str.m_pBuffer) != 0; }
102  String operator += (const String &str) { strcat(Reset(strlen(m_pBuffer) + strlen(str) + 1), str.m_pBuffer); return *this; }
105  bool operator > (const char *p) { return strcmp(m_pBuffer, p) > 0; }
107  bool operator < (const char *p) { return strcmp(m_pBuffer, p) < 0; }
108  bool operator >= (const char *p) { return strcmp(m_pBuffer, p) >= 0; }
109  bool operator <= (const char *p) { return strcmp(m_pBuffer, p) <= 0; }
110  bool operator == (const char *p) { return strcmp(m_pBuffer, p) == 0; }
111  bool operator != (const char *p) { return strcmp(m_pBuffer, p) != 0; }
113  String operator += (const char *p) { strcat(Reset(strlen(m_pBuffer) + strlen(p) + 1), p); return *this; }
115 
116  friend String operator + (const String &str, const char *p) {
117  return String(str) += p;
118  }
119  friend String operator + (const char *p, const String &str) {
120  return String(p) += str;
121  }
122  friend String operator + (const String &str1, const String &str2) {
123  return String(str1) += str2;
124  }
126  char *Toupper();
128  char *Tolower();
130  void DeleteSub(int nLocal, int nLen);
132  int Find(const char *sub);
134  String Replace(const char *src, const char *rpl);
136  inline char *TokBegin(const char *p);
138  inline char *TokSub(const char *p);
140  void Split(Array<String> &aStrs, const char* delimiter);
142  String FileName();
143  };
144 
146 }
147 
148 
a dynamic string class
Definition: wb-string.h:53
int GetSize() const
get buffer size
Definition: wb-string.h:78
const int cn_default_max_len
default maximum length
Definition: wb-string.h:41
char * End() const
get the pointer to the last position
Definition: wb-string.h:76
const int cn_default_str_len
default length of string
Definition: wb-string.h:40
void Clean()
set the string = "", but donot release the buffer
Definition: wb-string.h:82
int GetLength() const
get string length
Definition: wb-string.h:80
char * GetBuffer() const
get buffer
Definition: wb-string.h:74
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
Defination of simple dynamic array/stack/queue and so on.