stringop

/****************************************************************************
@File Name: stringop.h
@Author: 
@mail: 
@Created Time: Tue 04 Apr 2017 05:25:09 PM CST
****************************************************************************/
#ifndef STRING_OP_H
#define STRING_OP_H
#include <string>
namespace stringop
{
using namespace std;
class StringOp
{
public:
	/*
	 * @brief replace oldstr with new str in str
	 * */
	static void replace(string &str, 
						const string &oldstr,
						const string &newstr);
	/*
	 * @brief parse value from <type:filename:sha><...>...<...> to
	 * @types:type0;type1;...
	 * @shas:sha0;sha1;...
	 * @names:name0;name1;...
	 * */
	static void parsevalue(const string &value,
						   string &types,
						   string &shas,
						   string &names);
};
}
#endif

/****************************************************************************
@File Name: stringop.h
@Author: 
@mail: 
@Created Time: Tue 04 Apr 2017 05:25:09 PM CST
****************************************************************************/
#include "stringop.h"
namespace stringop
{
/*
 * @brief replace oldstr with new str in str
 * */
void StringOp::replace(string &str, 
					   const string &oldstr, 
					   const string &newstr)
{
	string::size_type pos = 0;
	string::size_type size = str.size();
	string::size_type oldsize = oldstr.size();
	string::size_type newsize = newstr.size();
	if(oldsize != newsize) return;
	while(pos < size)
	{
		if((pos = str.find(oldstr, pos)) != string::npos)
		{
			str.replace(pos, oldsize, newstr);
			pos += newsize;
		}
		else break;
	}
}
/*
 * @brief parse value from <type:filename:sha><...>...<...> to
 * @types:type0;type1;...
 * @shas:sha0;sha1;...
 * @names:name0;name1;...
 * */
void StringOp::parsevalue(const string &value, 
						  string &types,
						  string &shas,
						  string &names)
{
	int startpos;	// pointer to '<'
	int endpos;		// pointer to '>'
	int size = value.size();
	int pos = 0;
	int i;
	int mode;		// 0 -- type 1 -- sha 2 -- name
	types.clear();	// pre treated
	shas.clear();
	names.clear();
	while(pos < size)
	{
		startpos = value.find("<", pos);
		endpos = value.find(">", pos);
		if(startpos >= size || endpos >= size || startpos >= endpos) break;
		mode = 0;
		// transfer <...>
		for(i = startpos + 1;i < endpos;i++)
		{
			switch(mode)
			{
			case 0:	// type
				if(':' == value[i]) 
				{
					types += ";";
					++mode;
				}
				else types += value[i];
				break;
			case 1:	// sha
				if(':' == value[i]) 
				{
					shas += ";";
					++mode;
				}
				else shas += value[i];
				break;
			case 2:	// name
				if(':' == value[i]) 
				{
					names += ";";
					++mode;
				}
				else names += value[i];
				break;
			}
			if(mode >= 3) break;
		}
		if(';' != names[names.size() - 1]) names += ";";
		pos = endpos + 1;
	}
	// put off the last ';'
	types[types.size() - 1] = 0;
	shas[shas.size() - 1] = 0;
	names[names.size() - 1] = 0;
}
}


/****************************************************************************
@File Name: main.cpp
@Author:
@mail: 
@Created Time: Tue 04 Apr 2017 05:32:16 PM CST
****************************************************************************/
#include "stringop.h"
#include <iostream>
using namespace stringop;
int main()
{
	string str = "abc99abcabcdfgabchhhJJJJJabc788Acbabckkkk111111111abc";
	string str0= "abc";
	string str1= "LLL";
	StringOp::replace(str, str0, str1);
	cout << str << endl;
	string value = "<type0:filename0:sha0><type1:filename1:sha1>";
	string types;
	string shas;
	string names;
	StringOp::parsevalue(value, types, shas, names);
	cout << types << endl;
	cout << shas << endl;
	cout << names << endl;
	return 0;
}

CC=g++
all:
	$(CC) -std=c++11 -g -o stringopTest stringop.h stringop.cpp main.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值