⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.cpp

📁 贡献一份commoncpp2,有兴趣的可以研究一下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copyright (C) 1999-2005 Open Source Telecom Corporation.//  // This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// // This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // As a special exception, you may use this file as part of a free software// library without restriction.  Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License.  This exception does not however    // invalidate any other reasons why the executable file might be covered by// the GNU General Public License.    //// This exception applies only to the code released under the name GNU// Common C++.  If you copy code from other releases into a copy of GNU// Common C++, as the General Public License permits, the exception does// not apply to the code that you add in this way.  To avoid misleading// anyone as to the status of such modified files, you must delete// this exception notice from them.//// If you write modifications of your own for GNU Common C++, it is your choice// whether to permit this exception to apply to your modifications.// If you do not wish that, delete this exception notice.//#include <cc++/config.h>#include <cc++/export.h>#include <cc++/thread.h>#include <cc++/misc.h>#include <cc++/strchar.h>#include <cc++/string.h>#include "private.h"#ifdef	__BORLANDC__#include <stdarg.h>#include <stdio.h>#else#include <cstdarg>#include <cstdio>#endif#ifdef	CCXX_NAMESPACESnamespace ost {using std::streambuf;using std::ofstream;using std::ostream;using std::endl;using std::ios;#endifstatic Mutex mutex;MemPager *String::pager = NULL;char **String::idx = NULL;const unsigned String::minsize = ((sizeof(char *) + (sizeof(unsigned) * 2) + 1));const unsigned String::slotsize = 32;const unsigned String::pagesize = 1024;const unsigned String::slotlimit = 512;const unsigned String::slotcount = ((slotlimit / slotsize) + 1);const size_t String::npos = (size_t)(-1);String::String(){	init();}String::String(std::string str){       init();       set(str.c_str());}String::String(const String &original){	init();	copy(original);}String::String(size_t chars, const char chr){	init();	resize(chars + 1);	memset(getText(), chr, chars);	setLength(chars);}#ifdef	HAVE_SNPRINTFString::String(size_t chars, const char *format, ...){        va_list args;        va_start(args, format);	init();	resize(chars);        char *ptr = getText();	vsnprintf(ptr, chars, format, args);        setLength(strlen(ptr));	va_end(args);}#elseString::String(size_t chars, const char *str){        init();        resize(chars);        if(!str || !*str)                return;        set(str);}#endifString::String(const char *str){	init();	set(str);}String::String(const String &str, size_t start, size_t chars){	init();	char *ptr = str.getText();	size_t len = str.getLength();	if(start >= len)		return;	ptr += start;	len -= start;		if(chars >= len)		chars = len;	set(ptr, chars);}char String::at(ssize_t ind) const{	if(ind < 0)		ind = (ssize_t)(getLength() - ind + 1);	if((size_t)ind > getLength() || ind < 0)		return 0;	return (getText())[ind];}void String::insert(size_t start, const char *str, size_t chars){	char *ptr = getText();	size_t len = getLength();	size_t sz = getSize();	if(!str)		str = "";	if(!chars)		chars = strlen(str);	if(!chars || start > len)		return;	if(len + chars >= sz)	{		resize(len + chars + 1);		ptr = getText();	}	if(start == len)	{		memmove(ptr + start, str, chars);		len += chars;		setLength(len);		ptr[len] = 0;		return;	}	memmove(ptr + start + chars, ptr + start, len - start);	memmove(ptr + start, str, chars);	len += chars;	setLength(len);	ptr[len] = 0;	return;}	void String::insert(size_t start, const String &s){	insert(start, s.getText(), s.getLength());}void String::replace(size_t start, size_t len, const char *cp, size_t chars){	erase(start, len);	insert(start, cp, chars);}void String::replace(size_t start, size_t len, const String &s){	erase(start, len);	insert(start, s);}void String::erase(size_t start, size_t chars){	size_t len = getLength();	char *ptr = getText();			if(start >= len)		return;	if(start + chars >= len || chars == npos || !chars)	{		setLength(start);		ptr[start] = 0;		return;	}			memmove(ptr + start, ptr + start + chars, len - start - chars);	len -= chars;	setLength(len);	ptr[len] = 0;}void String::append(const char *str, size_t offset, size_t len){	size_t slen = getLength();	char *ptr = getText();	if(slen < offset)	{		append(str, len);		return;	}	setLength(offset);	ptr[offset] = 0;	append(str, len);}void String::append(const char *str, size_t len){	if(!str)		return;	if(!len)		len = strlen(str);	if(!len)		return;	if(getLength() + len >= getSize())		resize(getLength() + len + 1);	memmove(getText() + getLength(), str, len);	len += getLength();	setLength(len);	getText()[len] = 0;	return;}void String::add(char c){	size_t len = getLength();	char *ptr;	if(len + 1 >= getSize())		resize(len + 2);		ptr = getText();	ptr[len++] = c;	setLength(len);	ptr[len] = 0;}void String::trim(size_t chars){	size_t len = getLength();	char *ptr = getText();	if(chars >= len)		chars = len;	len -= chars;	ptr[len] = 0;	setLength(len);}String String::token(const char *delim, size_t offset){        char *ptr = getText();        size_t len = getLength();        size_t chars = 0;        String result;	bool found = false;	if(offset >= len)		return result;	len -= offset;	ptr += offset;                                                                                while(chars < len)        {                if(strchr(delim, ptr[chars]))		{			found = true;                        break;		}                ++chars;        }	if(!chars && found)		erase(offset, 1);                                                                                if(!chars)		return result;        result.set(ptr, chars);                                                                        	if(found)		++chars;        erase(offset, chars);	return result;}void String::strip(const char *chars){	size_t len = strtrim(chars, getText(), getLength());	if(!len)	{		setLength(len);		return;	}	setLength(strchop(chars, getText(), len));}#ifdef	HAVE_SNPRINTFconst char *String::set(size_t chars, const char *format, ...){        va_list args;        va_start(args, format);	if(chars <= minsize)		clear();	if(chars > getSize())		resize(chars);        char *ptr = getText();	vsnprintf(ptr, chars, format, args);        setLength(strlen(ptr));	va_end(args);	return ptr;}void String::append(size_t chars, const char *format, ...){        va_list args;        va_start(args, format);	size_t len = getLength();	if(len + chars <= minsize)		clear();	if(len + chars > getSize())		resize(len + chars);        char *ptr = getText() + len;	vsnprintf(ptr, chars, format, args);        setLength(strlen(getText()));	va_end(args);}#endifconst char *String::set(const char *str, size_t len){	if(!str)	{		clear();		return str;	}	if(!len)		len = strlen(str);	// if we are making a short string, lets clear prior alloc	if(len < minsize)		clear();	if(len >= getSize())		resize(len + 1);	memmove(getText(), str, len);	getText()[len] = 0;	setLength(len);	return str;}void String::set(const String &str){	set(str.getText(), str.getLength());}void String::append(const String &str){	append(str.getText(), str.getLength());}void String::copy(const String &original){	clear();	if(original.getLength() < minsize)	{		content.ministring.length = (unsigned)original.getLength();		memmove(content.ministring.text, original.getText(), original.getLength() + 1);		content.ministring.big = false;		return;	}//	ptr = original.getText();	content.bigstring.length = original.getLength();	content.bigstring.size = setSize(original.getLength() + 1);	content.bigstring.text = getSpace(content.bigstring.size);	content.ministring.big = true;	memmove(content.bigstring.text, original.getText(), original.getLength() + 1);}String::~String(){	clear();}void String::init(void){	content.ministring.big = false;	content.ministring.length = 0;	content.ministring.text[0] = 0;}size_t String::search(const char *cp, size_t clen, size_t ind) const{	size_t len = getLength();	 	if(!cp)		cp = "";	if(!clen)		clen = strlen(cp);	while(clen + ind <= len)	{		if(compare(cp, clen, ind) == 0)			return ind;		++ind;	}	return npos;}size_t String::find(const char *cp, size_t ind, size_t len, unsigned instance) const{	size_t pos;	if(!cp)		cp = "";	if(!len)		len = strlen(cp);	while(instance--)	{		pos = search(cp, len, ind);		if(pos == npos)			break;		ind = pos + 1;	}	return pos;}size_t String::rfind(const char *cp, size_t ind, size_t len) const{	size_t result = npos;        if(!cp)                cp = "";        if(!len)                len = strlen(cp);	for(;;)	{		ind = search(cp, len, ind);		if(ind == npos)			break;		result = ind++;	}	return result;}unsigned String::count(const char *cp, size_t ind, size_t len) const{	unsigned chars = 0;	if(!cp)		cp = "";	if(!len)		len = strlen(cp);	for(;;)	{		ind = search(cp, len, ind);		if(ind == npos)			break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -