【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;

测试结果

【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith的更多相关文章

  1. python实现 字符串匹配函数

    通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...

  2. numpy函数库中一些常用函数的记录

    ##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1) ...

  3. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  4. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

  5. Python字符串常用方法(二)

    二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...

  6. Python字符串中删除特定字符

    分析 在Python中,字符串是不可变的.所以无法直接删除字符串之间的特定字符. 所以想对字符串中字符进行操作的时候,需要将字符串转变为列表,列表是可变的,这样就可以实现对字符串中特定字符的操作. 1 ...

  7. python字符串格式化方法%s和format函数

    1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...

  8. BZOJ4259:残缺的字符串(FFT与字符串匹配)

    很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. 你想对这两 ...

  9. 逆向 time.h 函数库 time、gmtime 函数

    0x01 time 函数 函数原型:time_t time(time_t *t) 函数功能:返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位.如果 se ...

随机推荐

  1. Oracle过程及函数的参数模式,In、out、in out模式

    Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...

  2. sublime和python--path

    配置Sublime Text 2 的Python运行环境 (2013-09-11 11:36:17) 转载▼ 标签: python 分类: 科技相关     Sublime Text 2作为一款轻量级 ...

  3. AFNetworking 基本使用

    AFNetwork是一个轻量级的网络请求api类库.是以NSURLConnection, NSOperation和其他方法为基础的. 下面这个例子是用来处理json请求的 3如何选择AFNetwork ...

  4. Java集合系列:-----------04fail-fast总结(通过ArrayList来说明fail-fast的原理以及解决办法)

    前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例3 f ...

  5. velocity模板引擎学习(2)-velocity tools 2.0

    使用velocity后,原来的很多标签无法使用了,必须借助velocity tools来完成,目前velocity tools最新版本是2.0,下面是velocity tools的一些注意事项: 1. ...

  6. Theano2.1.8-基础知识之装载和保存

    来自:http://deeplearning.net/software/theano/tutorial/loading_and_saving.html loading and saving Pytho ...

  7. php缓存技术(减少数据库服务器压力)

    静态缓存(保存在磁盘上的静态文件,用PHP生成数据放入静态文件中) a)  php操作缓存 i.  生成缓存 ii.  获取缓存 iii. 删除缓存 判断目录是否存在:is_dir() dirname ...

  8. EmitMapper的使用

    1.普通的映射. public class UserInfo { public int id { get; set; } public string name { get; set; } public ...

  9. Xen

    Xen是一个开放源代码虚拟机监视器,由剑桥大学开发.它打算在单个计算机上运行多达128个有完全功能的操作系统. 在旧(无虚拟硬件)的处理器上执行Xen,操作系统必须进行显式地修改(“移植”)以在Xen ...

  10. Spring学习进阶 (三) Spring AOP

    一.是什么AOP是Aspect Oriented Programing的简称,最初被译为“面向方面编程”:AOP通过横向抽取机制为无法通过纵向继承体系进行抽象的重复性代码提供了解决方案.比如事务的控制 ...