• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            子彈 の VISIONS

            NEVER back down ~~

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
            與大家分享我的工具箱中的一些小TOOLS
            歡迎自由取用……?? :-)
            歡迎多提意見
            waker615@gmail.com

            --------------------------------------------------
            以下為函數(shù)的聲明:
            	//-------------------------------------------------------------------------
            	// STRING
            	std::string& cut (std::string& s, const char ce =' '); // ce = "character of end"
            	std::string& trim(std::string& s); // trim all space both left and right
            	std::string& trimleft (std::string& s);
            	std::string& trimright(std::string& s);
            	std::string& toupper (std::string& s);
            	std::string& tolower(std::string& s);
            	std::string& format (std::string& s, const char* fmt, ...);
            
            	// replace all the old substring with the new one in the source string
            	std::string& replace(std::string& s, const std::string& oldS, const std::string& newS);
            
            	// split string to substrings by separator-string
            	std::vector<std::string>& split(const std::string& s, std::vector<std::string>& vs, const std::string& sep =";");
            	
            	// join substrings to a big string separated by separator-string
            	std::string& join(const std::vector<std::string>& vs, std::string& s, const std::string& sep =";");
            
            	// get all substrings by two tags WITH tags from a source string
            	// e.g.
            	// s = <a href="someurl">Click Here</a>
            	// getDataByTags(s, vs, "<", ">"); then 
            	// vs would hold two elemts:
            	// vs[0] = "<a href=\"someurl\">";
            	// vs[1] = "</a>";
            	bool getDataByTags(const std::string& s, std::vector<std::string >& tags,
            		const std::string& begTag, const std::string& endTag );
            	
            	// get all substrings by two tags but WITHOUT tags from a source string
            	// e.g.
            	// s = <a href="someurl">Click Here</a>
            	// getDataByTags(s, vs, "<", ">"); then 
            	// vs would hold two elemts:
            	// vs[0] = "a href=\"someurl\"";
            	// vs[1] = "/a";
            	bool getDataBetweenTags(const std::string& s, std::vector<std::string >& data,
            				 const std::string& begTag, const std::string& endTag );
            以下為函數(shù)的定義:
            	//-------------------------------------------------------------------------
            	// STRING
            	
            	std::string& cut (std::string& s, const char ce)
            	{
            		std::string::iterator end = std::find(s.begin(), s.end(), ce);
            		return (s.assign(s.begin(), end));
            	}
            
            	std::string& trim(std::string& s)
            	{
            		std::string::iterator beg = s.begin();
            		while (beg != s.end() && ::isspace(*beg))
            			++beg;
            		
            		std::string::reverse_iterator end = s.rbegin();
            		while (end != s.rend() && ::isspace(*end))
            			++end;
            		
            		return s.assign(beg, end.base());
            	}
            	
            	std::string& trimleft(std::string& s)
            	{
            		std::string::iterator it = s.begin();
            		while (it != s.end() && ::isspace(*it))
            			++it;
            		return s.assign(it, s.end());
            	}
            	
            	std::string& trimright(std::string& s)
            	{
            		std::string::reverse_iterator it = s.rbegin();
            		while (it != s.rend() && ::isspace(*it))
            			++it;
            		return s.assign(s.begin(), it.base());
            	}
            	
            	std::string& toupper(std::string& s)
            	{
            		typedef std::string::iterator SIT;
            		for (SIT it = s.begin(); it != s.end(); ++it)
            		{
            			*it = ::toupper(*it);
            		}
            		return s;
            	}
            	
            	std::string& tolower(std::string& s)
            	{
            		typedef std::string::iterator SIT;
            		for (SIT it = s.begin(); it != s.end(); ++it)
            		{
            			*it = ::tolower(*it);
            		}
            		return s;
            	}
            
            	std::string& format(std::string& s, const char* fmt, ...)
            	{
            		const int MAX_LINE_LEN = 1024; // user defined
            		char buf[MAX_LINE_LEN] = {0};
            		va_list ap;
            		va_start(ap, fmt);
            		_vsnprintf(buf, MAX_LINE_LEN, fmt, ap);
            		va_end(ap);
            		return (s.assign(buf));
            	}
            
            	std::string& replace(std::string& s, const std::string& oldS, const std::string& newS)
            	{
            		if (std::string::npos == s.find(oldS)) return (s);
            		
            		typedef std::string::size_type SST;
            		SST beg =0, tmp =0;
            		SST oldLen = oldS.size();
            		SST newLen = newS.size();
            		while (std::string::npos != (beg = (s.find(oldS, tmp))))
            		{
            			s.replace(beg, oldLen, newS);
            			tmp = beg + newLen;
            			std::cout << beg << "-" << tmp << ": " << s.c_str() << std::endl;
            		}
            		return (s);
            	}
            
            	std::string& join(const std::vector<std::string>& vs, std::string& s, const std::string& sep)
            	{
            		if (vs.empty()) return (s);
            
            		typedef std::vector<std::string>::const_iterator VSCIT;
            		
            		for (VSCIT it = vs.begin(); it != vs.end(); ++ it)
            		{
            			s += *it;
            			s += sep;
            		}
            		return (s);
            	}
            
            	//	bool split(const std::string& s, std::vector<std::string >& vs, const std::string& sep)
            
            	std::vector<std::string>& split(const std::string& s, std::vector<std::string>& vs, const std::string& sep)
            	{
            		if (s.empty()) 
            			return (vs);
            
            		if (sep.empty() || std::string::npos == s.find(sep))
            		{
            			vs.push_back(s);
            			return (vs);
            		}
            
            		typedef std::string::size_type SST;
            		SST seplen = sep.size();
            		SST beg =0, end =std::string::npos;
            
            		while ( std::string::npos != (end=s.find(sep, beg)) )
            		{
            			vs.push_back(s.substr(beg, end-beg));
            			beg = end+seplen;
            		}
            		vs.push_back(s.substr(beg, s.size()-beg));
            
            		return (vs);
            	}
            	
            	bool getDataByTags(const std::string& s, std::vector<std::string >& tags,
            				 const std::string& begTag, const std::string& endTag )
            	{
            		if (s.empty() || begTag.empty() || endTag.empty())
            			return false;
            
            		if (std::string::npos == s.find(begTag) ||
            			std::string::npos == s.find(endTag) )
            		{
            			return false;
            		}
            		
            		typedef std::string::size_type SST;
            		SST beglen = begTag.size();
            		SST endlen = endTag.size();
            		SST beg =0, end =0;
            		tags.clear();
            		
            		while ( std::string::npos != (beg=s.find(begTag, end)) )
            		{
            			if( std::string::npos != (end=s.find(endTag, beg+beglen)) )
            			{
            				end += endlen;
            				tags.push_back(s.substr(beg, end-beg));
            			}
            			else
            				break;
            		}
            
            		return true;
            	}
            	
            	bool getDataBetweenTags(const std::string& s, std::vector<std::string >& data,
            				 const std::string& begTag, const std::string& endTag )
            	{
            		if (s.empty() || begTag.empty() || endTag.empty())
            			return false;
            		
            		if (std::string::npos == s.find(begTag) ||
            			std::string::npos == s.find(endTag) )
            		{
            			return false;
            		}
            		
            		typedef std::string::size_type SST;
            		SST beglen = begTag.size();
            		SST endlen = endTag.size();
            		SST beg =0, end =0;
            		data.clear();
            		
            		while ( std::string::npos != (beg=s.find(begTag, end)) )
            		{
            			if( std::string::npos != (end=s.find(endTag, beg+beglen)) )
            			{
            				data.push_back(s.substr(beg+beglen, end-beg-beglen));
            				end += endlen;
            			}
            			else
            				break;
            		}
            		
            		return true;
            	}

            其實(shí),toupper和tolower可以這樣寫:
            std::string& toupper(std::string& s)
            {
            	typedef std::string::iterator SIT;
            	for (SIT it = s.begin(); it != s.end(); ++it)
            	{
            		if (*it >= 'a' && *it <= 'z')
            		{
            			*it += 'A' - 'a';
            		}
            	}
            	return s;
            }
            	
            std::string& tolower(std::string& s)
            {
            	typedef std::string::iterator SIT;
            	for (SIT it = s.begin(); it != s.end(); ++it)
            	{
            		if (*it >= 'A' && *it <= 'Z')
            		{
            			*it += 'a' - 'A';
            		}
            	}
            	return s;
            }

            上面是關(guān)于字符串的。其他的慢慢再帖……賣個(gè)關(guān)子先
            呵呵……
            posted on 2006-09-14 15:53 子彈のVISIONS 閱讀(1276) 評論(6)  編輯 收藏 引用

            Feedback

            # re: [ 子彈的工具箱 ] 之 String 類 2006-09-14 16:15 shaker
            注釋下吧 這么簡單的代碼 還要?jiǎng)e人去看代碼理解這個(gè)函數(shù) 實(shí)在是有點(diǎn)說不過去  回復(fù)  更多評論
              

            # re: [ 子彈的工具箱 ] 之 String 類 2006-09-14 16:40 子彈
            @shaker

            你說得對……
            嗯。為了免得大家浪費(fèi)時(shí)間,在聲明的地方加了注釋。

            :)

            歡迎討論  回復(fù)  更多評論
              

            # re: [ 子彈的工具箱 ] 之 String 類 2006-09-15 08:50 漂舟
            寫得不錯(cuò) ! 支持作者無私的精神 !
            代碼中頻繁使用 std::string ,
            當(dāng)然,你使用的typedef也解決了一部分。
            我覺得可否在實(shí)現(xiàn)的局部文件使用
            using namespace std::string,
            避免代碼看起來肥大。

            在下愚見。  回復(fù)  更多評論
              

            # re: [ 子彈的工具箱 ] 之 String 類 2006-09-17 16:39 Francis Arcanum
            這個(gè)有很多現(xiàn)成的,不需要自己寫  回復(fù)  更多評論
              

            # re: [ 子彈的工具箱 ] 之 String 類 2006-11-08 10:33 cat
            你的trim等對如果對中文或者日文雙字節(jié)字符可能會(huì)出問題哦。  回復(fù)  更多評論
              

            # re: [ 子彈的工具箱 ] 之 String 類 2006-11-15 10:45 子彈
            @cat

            雙字節(jié)字符串用wstring和locale配合進(jìn)行處理  回復(fù)  更多評論
              

            麻豆AV一区二区三区久久 | 香蕉久久久久久狠狠色| 久久97久久97精品免视看| 久久久精品视频免费观看| 久久噜噜久久久精品66| 久久精品日日躁夜夜躁欧美| 久久久久亚洲精品天堂| 久久伊人精品青青草原高清| 一本一道久久a久久精品综合| 久久久久久国产a免费观看黄色大片| 一本色道久久综合亚洲精品| 麻豆精品久久精品色综合| 99久久免费国产精品特黄| 欧美精品一区二区精品久久| 久久人做人爽一区二区三区| 国产精品久久久久一区二区三区 | 国产精品一区二区久久| 亚洲午夜久久久| 亚洲国产精品久久久久久| 亚洲午夜久久久久久久久久| 中文字幕成人精品久久不卡| 久久久精品2019免费观看| 四虎国产精品成人免费久久| 秋霞久久国产精品电影院| 亚洲国产精品久久久天堂 | 熟妇人妻久久中文字幕| 亚洲人成网站999久久久综合| 久久九九亚洲精品| 久久发布国产伦子伦精品| 精品综合久久久久久98| 人妻精品久久久久中文字幕| 国产精品视频久久| 99久久无码一区人妻| 国产精品久久久久影院色| 国产精品久久久久无码av| 99久久人妻无码精品系列| 热re99久久精品国99热| 日本欧美久久久久免费播放网| 久久精品国产精品亚洲精品| 国产亚洲美女精品久久久2020| 一级做a爰片久久毛片毛片 |