ailiy 发表于 2015-11-29 14:20:16

【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 - startselflen)
  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(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(result);
  }
  

  

测试
  

string str = "abcdefghijklmn";  

  string temp1 = "ab";

  cout
页: [1]
查看完整版本: 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith