【C++實現python字符串函數庫】二:字符串匹配函數startswith與endswith


【C++實現python字符串函數庫】字符串匹配函數startswith與endswith

這兩個函數用於匹配字符串的開頭或末尾,判斷是否包含另一個字符串,它們返回bool值。startswith()函數判斷文本的指定范圍字符段是否以某個字符開始,endswith()函數判斷文本是否以某個字符結束。默認的指定范圍為整個字符串:

>>> 
>>> a
'abcdefghijklmn'
>>> a.startswith('abc')
True
>>> a.endswith('klmn')
True
>>> a.startswith('bc')
False
>>> a.endswith('nm')
False
>>> 

也可以指定一個匹配范圍:

>>> 
>>> a
'abcdefghijklmn'
>>> a.startswith('cd',2,10)
True
>>> 

python字符串范圍校准。

在使用字符串函數時,很多時候我們可以使用start與end參數來指定要進行操作的字符串的一個范圍。例如在上面的函數中我們就使用到了('cd',2,10)語句,來對字符串a下標從2~10的范圍進行匹配操作。

當我們輸入的范圍不合法時,python是如何處理的呢?例如我們輸入了一個負數的start或者輸入一個遠大於字符串長度的end,python的處理絕不是以字符串開始或結束位置作為標准來校正范圍,請看下面這段程序:

>>> a
'abcdefghijklmn'
>>> len(a)
14
>>> a.startswith('ef',-10,10) #實際范圍:(-10+14,10)=(4,10)

具體的校准方法,我們可以使用這函數來描述:


	void AdjustIndices(int &start, int & end, std::string::size_type len)
	{
               len =(int)len;
	    //如果end超出字符串長度
		if (end > len)
			end = len; //則以字符串長度為准
		else if (end < 0)
		{//如果end為負數
			end += len; //則先加上字符串長度
			if (end < 0)//如果還是為負數
				end = 0;//則為0
		}
		//如果start為負數
		if (start < 0)
		{
		    //則加上字符串長度,注意不是以0校准
			start += len;
			if (start < 0)//如果還是負數
				start = 0;//才以0校准
		}
	}

然而在我們的函數庫實現中,我們並不打算把范圍校准操作作為一個函數。我們將它作為一個宏來處理,原因如下:

  • 操作簡單,不會出來宏函數常見的問題,直接的替換足以解決問題。
  • 省去函數調用的花銷
  • 多個地方都需要范圍校准。

C++實現

范圍校准宏

#define ADJUST_INDICES(start, end, len)     \
    if (end > len)                          \
        end = len;                          \
    else if (end < 0) {                     \
        end += len;                         \
        if (end < 0)                        \
        end = 0;                            \
    }                                       \
    if (start < 0) {                        \
        start += len;                       \ 
        if (start < 0)                      \
        start = 0;                          \
    }

有上面的解說,這段宏定義應該看得懂。

_string_tailmatch函數

	//匹配函數:endswith與startwith的內部調用函數
	int _string_tailmatch(const std::string&self, const std::string&substr, int start, int end, int direction)
	{
		int selflen = (int)self.size();
		int slen = (int)substr.size();
		
		const char* str = self.c_str();
		const char* sub = substr.c_str();

		//對輸入的范圍進行校准
		ADJUST_INDICES(start, end, selflen);

		//字符串頭部匹配(即startswith)
		if (direction < 0)
		{
			if (start + slen>selflen)
				return 0;
		}
		//字符串尾部匹配(即endswith)
		else
		{
			if (end - start<slen || start>selflen)
				return 0;
			if (end - slen > start)
				start = end - slen;
		}
		if (end - start >= slen)
			//mcmcmp函數用於比較buf1與buf2的前n個字節
			return !std::memcmp(str + start, sub, slen);
		return 0;
		
	}

endswith函數

bool endswith(const std::string&str, const std::string&suffix, int start = 0, int end = MAX_32BIT_INT)
	{
           //調用_string_tailmatch函數,參數+1表示字符串尾部匹配
		int result = _string_tailmatch(str, suffix, start, end, +1);
		return static_cast<bool>(result);
	}
	

startswith函數

	bool startswith(const std::string&str, const std::string&suffix, int start = 0, int end = MAX_32BIT_INT)
	{
               //調用_string_tailmatch函數,參數-1表示字符串頭部匹配
		int result = _string_tailmatch(str, suffix, start, end, -1);
		return static_cast<bool>(result);
	}

測試

	string str = "abcdefghijklmn";

	string temp1 = "ab";
	cout << startswith(str, temp1)<<endl;//使用默認參數

	string temp2 = "mn";
	cout << endswith(str, temp2) << endl;

	string temp3 = "ef";
	cout << startswith(str, temp3, 4, 10)<<endl;

	string temp4 = "qq";
	cout << startswith(str, temp3, 0, 100) << endl;

測試結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM