larbin源码分析(二) gloabl文件 LarbinString类对象的实现
LarbinString类对象的实现
一 该类介绍
LarbinString类主要是字符串处理,主要的成员参数是 char * chaine 表示字符串的内容存储的指针地址。
还有pos 表示当前string的位置,size表示最大的容量。
成员函数,都为一些添加字符,添加缓冲区的操作。其中的主要的是 recycle() ,getString() ,giveStirng()等函数。
二 类的头文件
//
Larbin
// Sebastien Ailleret
// 20-12-99 -> 05-05-01
#ifndef STRING_HH
#define STRING_HH
#include < assert.h >
#include " types.h "
#include " utils/debug.h "
class LarbinString {
private :
char * chaine ; //内存指针
uint pos ; //当前位置
uint size ; //总共的大小
public :
//Constructor
LarbinString(uint size= STRING_SIZE) ;
~LarbinString() ;
//Recycle this string
void recycle(uint size=STRING_SIZE) ;
//get the char *
//it is deleted when you delete this Stringobject
char * getString() ;
//give a char * : it creates a new one
char * giveString();
//append a char
void addChar(char c) ;
//append a char *
void addString(char * s) ;
//append a buffer
void addBuffer(char * s , uint len) ;
//length of the string
inline uint getLength() {return pos ;}
//get a char of this string
inline uint operator[] (uint i)
{
assert(i<=pos) ;
return chaine[i] ;
}
//change a char
void setChar(uint i , char c) ;
} ;
#endif // STRING_HH
// Sebastien Ailleret
// 20-12-99 -> 05-05-01
#ifndef STRING_HH
#define STRING_HH
#include < assert.h >
#include " types.h "
#include " utils/debug.h "
class LarbinString {
private :
char * chaine ; //内存指针
uint pos ; //当前位置
uint size ; //总共的大小
public :
//Constructor
LarbinString(uint size= STRING_SIZE) ;
~LarbinString() ;
//Recycle this string
void recycle(uint size=STRING_SIZE) ;
//get the char *
//it is deleted when you delete this Stringobject
char * getString() ;
//give a char * : it creates a new one
char * giveString();
//append a char
void addChar(char c) ;
//append a char *
void addString(char * s) ;
//append a buffer
void addBuffer(char * s , uint len) ;
//length of the string
inline uint getLength() {return pos ;}
//get a char of this string
inline uint operator[] (uint i)
{
assert(i<=pos) ;
return chaine[i] ;
}
//change a char
void setChar(uint i , char c) ;
} ;
#endif // STRING_HH
三 实现代码
该代码实质上是实现了,一个string类型,可以自动地增长容量,实现动态地增添操作。
//
Larbin
// Sebastien Ailleret
// 20-12-99 -> 10-12-01
#include < string .h >
#include < iostream >
#include " options.h "
#include " utils/text.h "
#include " utils/string.h "
using namespace std ;
// Constructor
LarbinString::LarbinString ( uint size) {
chaine = new char[size] ;
pos = 0 ;
this->size = size ;
chaine[0] = 0 ;
}
// Destructor
LarbinString:: ~ LarbinString () {
delete [] chaine ;
}
// Recycle this string
void LarbinString::recycle ( uint size) {
if(this->size > size) //当大小小于当前的大小时
{
delete [] chaine ;
chaine = new char[size] ;
this->size = size ;
}
pos = 0 ;
chaine[0] = 0 ;
}
// get the char *
char * LarbinString::getString () {
return chaine ;
}
/**/ /** give a new string (allocate a new one
* the caller will have to delete it
*/
char * LarbinString::giveString () {
return newString(chaine) ;
}
// append a char
void LarbinString::addChar ( char c) {
chaine[pos] = c;
pos++ ;
if(pos >= size) //如果当前的
{
char * tmp = new char[2 * size] ;
memcpy(tmp , chaine , pos) ;
delete [] chaine ;
chaine = tmp ;
size *= 2 ;
}
chaine[pos] = 0 ;
}
// append a char *
void LarbinString::addString ( char * s) {
uint len = strlen(s);
addBuffer(s, len);
}
// append a buffer
void LarbinString::addBuffer ( char * s, uint len) {
if (size <= pos + len) {
size *= 2;
if (size <= pos + len) size = pos + len + 1;
char *tmp = new char[size];
memcpy(tmp, chaine, pos);
delete [] chaine;
chaine = tmp;
}
memcpy(chaine+pos, s, len);
pos += len;
chaine[pos] = 0;
}
// change a char
void LarbinString::setChar ( uint i, char c) {
chaine[i] = c;
}
// Sebastien Ailleret
// 20-12-99 -> 10-12-01
#include < string .h >
#include < iostream >
#include " options.h "
#include " utils/text.h "
#include " utils/string.h "
using namespace std ;
// Constructor
LarbinString::LarbinString ( uint size) {
chaine = new char[size] ;
pos = 0 ;
this->size = size ;
chaine[0] = 0 ;
}
// Destructor
LarbinString:: ~ LarbinString () {
delete [] chaine ;
}
// Recycle this string
void LarbinString::recycle ( uint size) {
if(this->size > size) //当大小小于当前的大小时
{
delete [] chaine ;
chaine = new char[size] ;
this->size = size ;
}
pos = 0 ;
chaine[0] = 0 ;
}
// get the char *
char * LarbinString::getString () {
return chaine ;
}
/**/ /** give a new string (allocate a new one
* the caller will have to delete it
*/
char * LarbinString::giveString () {
return newString(chaine) ;
}
// append a char
void LarbinString::addChar ( char c) {
chaine[pos] = c;
pos++ ;
if(pos >= size) //如果当前的
{
char * tmp = new char[2 * size] ;
memcpy(tmp , chaine , pos) ;
delete [] chaine ;
chaine = tmp ;
size *= 2 ;
}
chaine[pos] = 0 ;
}
// append a char *
void LarbinString::addString ( char * s) {
uint len = strlen(s);
addBuffer(s, len);
}
// append a buffer
void LarbinString::addBuffer ( char * s, uint len) {
if (size <= pos + len) {
size *= 2;
if (size <= pos + len) size = pos + len + 1;
char *tmp = new char[size];
memcpy(tmp, chaine, pos);
delete [] chaine;
chaine = tmp;
}
memcpy(chaine+pos, s, len);
pos += len;
chaine[pos] = 0;
}
// change a char
void LarbinString::setChar ( uint i, char c) {
chaine[i] = c;
}
四 总结
LarbinString类主要进行的是字符串处理,实质上是自己实现了一个String库。