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

📄 string.h

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 H
字号:
//
// String.h
//
// $Id: //poco/Main/Foundation/include/Foundation/String.h#5 $
//
// String utility functions.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#ifndef Foundation_String_INCLUDED
#define Foundation_String_INCLUDED


#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef STD_LOCALE_INCLUDED
#include <locale>
#define STD_LOCALE_INCLUDED
#endif


Foundation_BEGIN


template <class S>
S trimLeft(const S& str)
	/// Returns a copy of str with all leading
	/// whitespace removed.
{
	std::locale loc;
	typename S::const_iterator it  = str.begin();
	typename S::const_iterator end = str.end();
	
	while (it != end && isspace(*it, loc)) ++it;
	return S(it, end);
}


template <class S>
S& trimLeftInPlace(S& str)
	/// Removes all leading whitespace in str.
{
	std::locale loc;
	typename S::iterator it  = str.begin();
	typename S::iterator end = str.end();
	
	while (it != end && isspace(*it, loc)) ++it;
	str.erase(str.begin(), it);
	return str;
}


template <class S>
S trimRight(const S& str)
	/// Returns a copy of str with all trailing
	/// whitespace removed.
{
	std::locale loc;
	int pos = int(str.size()) - 1;
		
	while (pos >= 0 && isspace(str[pos], loc)) --pos;
	return S(str, 0, pos + 1);
}


template <class S>
S& trimRightInPlace(S& str)
	/// Removes all trailing whitespace in str.
{
	std::locale loc;
	int pos = int(str.size()) - 1;
		
	while (pos >= 0 && isspace(str[pos], loc)) --pos;
	str.resize(pos + 1);

	return str;
}


template <class S>
S trim(const S& str)
	/// Returns a copy of str with all leading and
	/// trailing whitespace removed.
{
	std::locale loc;
	int first = 0;
	int last  = int(str.size()) - 1;
	
	while (first <= last && isspace(str[first], loc)) ++first;
	while (last >= first && isspace(str[last], loc)) --last;

	return S(str, first, last - first + 1);
}


template <class S>
S& trimInPlace(S& str)
	/// Removes all leading and trailing whitespace in str.
{
	std::locale loc;
	int first = 0;
	int last  = int(str.size()) - 1;
	
	while (first <= last && isspace(str[first], loc)) ++first;
	while (last >= first && isspace(str[last], loc)) --last;

	str.resize(last + 1);
	str.erase(0, first);

	return str;
}


template <class S>
S toUpper(const S& str)
	/// Returns a copy of str containing all upper-case characters.
{
	std::locale loc;
	typename S::const_iterator it  = str.begin();
	typename S::const_iterator end = str.end();

	S result;
	result.reserve(str.size());
	while (it != end) result += toupper(*it++, loc);
	return result;
}


template <class S>
S& toUpperInPlace(S& str)
	/// Replaces all characters in str with their upper-case counterparts.
{
	std::locale loc;
	typename S::iterator it  = str.begin();
	typename S::iterator end = str.end();

	while (it != end) { *it = toupper(*it, loc); ++it; }
	return str;
}


template <class S>
S toLower(const S& str)
	/// Returns a copy of str containing all lower-case characters.
{
	std::locale loc;
	typename S::const_iterator it  = str.begin();
	typename S::const_iterator end = str.end();

	S result;
	result.reserve(str.size());
	while (it != end) result += tolower(*it++, loc);
	return result;
}


template <class S>
S& toLowerInPlace(S& str)
	/// Replaces all characters in str with their lower-case counterparts.
{
	std::locale loc;
	typename S::iterator it  = str.begin();
	typename S::iterator end = str.end();

	while (it != end) { *it = tolower(*it, loc); ++it; }
	return str;
}


#if !defined(POCO_NO_TEMPLATE_ICOMPARE)


template <class S, class It>
int icompare(
	const S& str,
	typename S::size_type pos, 
	typename S::size_type n,
	It it2, 
	It end2)
	/// Case-insensitive string comparison
{
	typename S::size_type sz = str.size();
	if (pos > sz) pos = sz;
	if (pos + n > sz) n = sz - pos;
	It it1  = str.begin() + pos; 
	It end1 = str.begin() + pos + n;
	std::locale loc;
	while (it1 != end1 && it2 != end2)
	{
        typename S::value_type c1 = tolower(*it1, loc);
        typename S::value_type c2 = tolower(*it2, loc);
        if (c1 < c2)
            return -1;
        else if (c1 > c2)
            return 1;
        ++it1; ++it2;
	}
    
    if (it1 == end1)
		return it2 == end2 ? 0 : -1;
    else
        return 1;
}


template <class S>
int icompare(const S& str1, const S& str2)
{
	return icompare(str1, 0, str1.size(), str2.begin(), str2.end());
}


template <class S>
int icompare(const S& str1, typename S::size_type n1, const S& str2, typename S::size_type n2)
{
	if (n2 > str2.size()) n2 = str2.size();
	return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2);
}


template <class S>
int icompare(const S& str1, typename S::size_type n, const S& str2)
{
	if (n > str2.size()) n = str2.size();
	return icompare(str1, 0, n, str2.begin(), str2.begin() + n);
}


template <class S>
int icompare(const S& str1, typename S::size_type pos, typename S::size_type n, const S& str2)
{
	return icompare(str1, pos, n, str2.begin(), str2.end());
}


template <class S>
int icompare(
	const S& str1, 
	typename S::size_type pos1, 
	typename S::size_type n1, 
	const S& str2,
	typename S::size_type pos2,
	typename S::size_type n2)
{
	typename S::size_type sz2 = str2.size();
	if (pos2 > sz2) pos2 = sz2;
	if (pos2 + n2 > sz2) n2 = sz2 - pos2;
	return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2);
}


template <class S>
int icompare(
	const S& str1, 
	typename S::size_type pos1, 
	typename S::size_type n, 
	const S& str2,
	typename S::size_type pos2)
{
	typename S::size_type sz2 = str2.size();
	if (pos2 > sz2) pos2 = sz2;
	if (pos2 + n > sz2) n = sz2 - pos2;
	return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n);
}


template <class S>
int icompare(
	const S& str,
	typename S::size_type pos,
	typename S::size_type n,
	const typename S::value_type* ptr)
{
	poco_check_ptr (ptr);
	typename S::size_type sz = str.size();
	if (pos > sz) pos = sz;
	if (pos + n > sz) n = sz - pos;
	typename S::const_iterator it  = str.begin() + pos; 
	typename S::const_iterator end = str.begin() + pos + n;
	std::locale loc;
	while (it != end && *ptr)
	{
        typename S::value_type c1 = tolower(*it, loc);
        typename S::value_type c2 = tolower(*ptr, loc);
        if (c1 < c2)
            return -1;
        else if (c1 > c2)
            return 1;
        ++it; ++ptr;
	}
    
    if (it == end)
		return *ptr == 0 ? 0 : -1;
    else
        return 1;
}


template <class S>
int icompare(
	const S& str,
	typename S::size_type pos,
	const typename S::value_type* ptr)
{
	return icompare(str, pos, str.size() - pos, ptr);
}


template <class S>
int icompare(
	const S& str,
	const typename S::value_type* ptr)
{
	return icompare(str, 0, str.size(), ptr);
}


#else


int Foundation_API icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, std::string::const_iterator it2, std::string::const_iterator end2);
int Foundation_API icompare(const std::string& str1, const std::string& str2);
int Foundation_API icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2);
int Foundation_API icompare(const std::string& str1, std::string::size_type n, const std::string& str2);
int Foundation_API icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2);
int Foundation_API icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n1, const std::string& str2, std::string::size_type pos2, std::string::size_type n2);
int Foundation_API icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n, const std::string& str2, std::string::size_type pos2);
int Foundation_API icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr);
int Foundation_API icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr);
int Foundation_API icompare(const std::string& str, const std::string::value_type* ptr);


#endif


template <class S>
S translate(const S& str, const S& from, const S& to)
	/// Returns a copy of str with all characters in
	/// from replaced by the corresponding (by position)
	/// characters in to. If there is no corresponding
	/// character in to, the character is removed from
	/// the copy. 
{
	S result;
	result.reserve(str.size());
	typename S::const_iterator it  = str.begin();
	typename S::const_iterator end = str.end();
	typename S::size_type toSize = to.size();
	while (it != end)
	{
		typename S::size_type pos = from.find(*it);
		if (pos == S::npos)
		{
			result += *it;
		}
		else
		{
			if (pos < toSize) result += to[pos];
		}
		++it;
	}
	return result;
}


template <class S>
S translate(const S& str, const typename S::value_type* from, const typename S::value_type* to)
{
	poco_check_ptr (from);
	poco_check_ptr (to);
	return translate(str, S(from), S(to));
}


template <class S>
S& translateInPlace(S& str, const S& from, const S& to)
	/// Replaces in str all occurences of characters in from
	/// with the corresponding (by position) characters in to.
	/// If there is no corresponding character, the character
	/// is removed.
{
	str = translate(str, from, to);
	return str;
}


template <class S>
S translateInPlace(S& str, const typename S::value_type* from, const typename S::value_type* to)
{
	poco_check_ptr (from);
	poco_check_ptr (to);
	str = translate(str, S(from), S(to));
	return str;
}


template <class S>
S cat(const S& s1, const S& s2)
	/// Concatenates two strings.
{
	S result = s1;
	result.reserve(s1.size() + s2.size());
	result.append(s2);
	return result;
}


template <class S>
S cat(const S& s1, const S& s2, const S& s3)
	/// Concatenates three strings.
{
	S result = s1;
	result.reserve(s1.size() + s2.size() + s3.size());
	result.append(s2);
	result.append(s3);
	return result;
}


template <class S>
S cat(const S& s1, const S& s2, const S& s3, const S& s4)
	/// Concatenates four strings.
{
	S result = s1;
	result.reserve(s1.size() + s2.size() + s3.size() + s4.size());
	result.append(s2);
	result.append(s3);
	result.append(s4);
	return result;
}


template <class S>
S cat(const S& s1, const S& s2, const S& s3, const S& s4, const S& s5)
	/// Concatenates five strings.
{
	S result = s1;
	result.reserve(s1.size() + s2.size() + s3.size() + s4.size() + s5.size());
	result.append(s2);
	result.append(s3);
	result.append(s4);
	result.append(s5);
	return result;
}


template <class S>
S cat(const S& s1, const S& s2, const S& s3, const S& s4, const S& s5, const S& s6)
	/// Concatenates six strings.
{
	S result = s1;
	result.reserve(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size());
	result.append(s2);
	result.append(s3);
	result.append(s4);
	result.append(s5);
	result.append(s6);
	return result;
}


template <class S, class It>
S cat(const S& delim, const It& begin, const It& end)
	/// Concatenates a sequence of strings, delimited
	/// by the string given in delim.
{
	S result;
	for (It it = begin; it != end; ++it)
	{
		if (!result.empty()) result.append(delim);
		result += *it;
	}
	return result;
}


Foundation_END


#endif // Foundation_String_INCLUDED

⌨️ 快捷键说明

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