自己写的一段:

//gool
char* str_replace(char* source, const char* find, const char* replace){ if (source == NULL || find == NULL || find == "")
return strdup(source); int matchCount = 0;
int nowIndex = 0;
int findLength = strlen(find);
int replaceLength = strlen(replace);
int sourceLength = strlen(source);
int resultLength = sourceLength;
char* result;
int i;
for ( i = 0; i < sourceLength; i++)
{
if (nowIndex < findLength && source[i] == find[nowIndex]){
nowIndex++;
matchCount++;
}
else
{
if (matchCount == findLength)
{
source[i - 1] = '\0';
nowIndex = 0;
matchCount = 0;
resultLength -= findLength - replaceLength;
}
}
} nowIndex = 0;
matchCount = 0; result = (char*)malloc(2 * (resultLength + 1)); for ( i = 0; i < sourceLength; i++)
{
if (source[i] == '\0'){
source[i] = find[0];
while (matchCount < replaceLength)
{
*(result + nowIndex + matchCount) = replace[matchCount];
matchCount++;
}
matchCount = 0;
nowIndex += replaceLength;
i += findLength -1;
}
else
{
*(result + nowIndex) = source[i];
nowIndex++;
}
}
*(result + nowIndex + 1) = '\0';
return result;
}

  http://blog.csdn.net/glmmmm/article/details/9930969:

char *str_replace_2(const char *string, const char *substr, const char *replacement)
{
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL; /* if either substr or replacement is NULL, duplicate string a let caller handle it */
if (substr == NULL || replacement == NULL)
return strdup(string); newstr = strdup(string);
while ((tok = strstr(newstr, substr)))
{
oldstr = newstr;
newstr = (char*)malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
/*failed to alloc mem, free old string and return NULL */
if (newstr == NULL)
{
free(oldstr);
return NULL;
}
memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));
memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1); free(oldstr);
} return newstr;
}

http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c:

char *str_replace_3(char *orig, char *rep, char *with) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
int len_rep; // length of rep
int len_with; // length of with
int len_front; // distance between rep and end of last rep
int count; // number of replacements if (!orig)
return NULL;
if (!rep)
rep = "";
len_rep = strlen(rep);
if (!with)
with = "";
len_with = strlen(with); ins = orig;
for (count = 0; tmp = strstr(ins, rep); ++count) {
ins = tmp + len_rep;
} // first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
tmp = result = (char*)malloc(strlen(orig) + (len_with - len_rep) * count + 1); if (!result)
return NULL; while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
}
strcpy(tmp, orig);
return result;
}

  

平均测速:1. 84 clock/100000,2. 195 clock/100000,3.89 clock/100000

clang 简单的str_replace实现的更多相关文章

  1. php将中文符号全部替换为英文符号

    php将中文符号全部替换为英文符号 一.总结 一句话总结:可以用简单替换和规律替换 简单替换 str_replace() 规律替换 均相差 65248 方法一:简单替换(php代码) $val1=st ...

  2. 深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...

  3. 简单管理员权限与几个常用的PHP 常用函数,in_array(),explode(),implode(),join(),str_replace()

    先把今天要用的几个函数罗列出来: //explode()转换成数组,implode()转化成字符串 explode("分隔符",需要被分割的字符串或变量) $priv=" ...

  4. 【004: gcc 和 clang 的简单比较】

  5. 应用OpenMP的一个简单的设计模式

    小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...

  6. 基于Clang的Source to Source源代码转换(一)

    Clang中包含了非常多的关于抽象语法树(AST)的访问和操作的类和接口.我们程序开发人员可以直接通过继承其中的某些类,重写其中的关键成员方法,从而形成我们自己的对抽象语法树的操作. 那么,首先我们简 ...

  7. windows平台下基于VisualStudio的Clang安装和配置

    LLVM 是一个开源的编译器架构,它已经被成功应用到多个应用领域.Clang是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程 ...

  8. clang编译器简介

    本文部分内容引用: 中文维基百科. 结构化编译器前端--clang介绍. 什么是clang编译器? clang是LLVM编译器工具集的一个用于编译C.C++.Objective-C的前端.LLVM项目 ...

  9. 浅谈 block(1) – clang 改写后的 block 结构

    这几天为了巩固知识,从 iOS 的各个知识点开始学习,希望自己对每一个知识理解的更加深入的了解.这次来分享一下 block 的学习笔记. block 简介 block 被当做扩展特性而被加入 GCC ...

随机推荐

  1. 关于openssl几个API的一点小收获

    今天心血来潮突然想搞搞openssl了,趁着端午小假,刚好有空可以鼓捣孤岛自己喜欢的东西,出去东奔西跑的实在太造孽了,还是宅起来给自己充充电吧.下载openssl最新代码1.0.1g,修复了" ...

  2. 复制mueclipse项目到eclipse

    本文适用于将MyEclipse上的项目projectA检出后重命名为projectB的情况,如果只是检出projectA到Eclipse,也可以部分参考 1.从svn上检出Myeclipse项目到Ec ...

  3. Linux基本权限学习

    概念 权限就是用户对资源所能进行的操作 -- 这里涉及到三个重要的概念:用户.资源.操作. 首先,Linux中用户分为:u.g.o,就是用户.用户组.其他用户.--这里的用户是指拥有者!!!务必记住! ...

  4. Scala学习笔记一

    首先是安装Scala 下载Scala进行安装 http://www.scala-lang.org/ 安装好scala后,为scala配置系统环境参数 新建环境变量SCALA_HOME,值为scala安 ...

  5. 如何用Tacker将NFV带入OpenStack?

    最初社区里很多人争论过NFV是否属于OpenStack,而后来可以确定的是OpenStack的确占据了NFV会话中的很大一部分,并且形象地反映在了下面的ETSI MANO概念架构图中,OpenStac ...

  6. div+css实例教程

    DIV+CSS是WEB设计标准,它是一种网页的布局方法.与传统中通过表格(table)布局定位的方式不同,它可以实现网页页面内容与表现相分离. 对于初学者来说,可能比较模糊不熟悉.毕竟,样式布局需要通 ...

  7. 多个网站使用不同的SSH密钥登陆(zz)

    多个网站使用不同的SSH密钥登陆   1.创建不同的SSH密钥, -t指定加密方法,RSA或DSA:-C注释:-f指定文件名   www.2cto.com   ssh-keygen -t dsa -C ...

  8. CSS布局技巧 -- sticky属性

    在一些很长的表格中,往往需要使用表头悬浮的设计以方便用户使用,例如H5电商页面通过下滑展示大量商品列表时,顶部的导航栏需要在离开屏幕时,需要固定在屏幕顶部以方便用户筛选类别.这种效果一直以来需要通过J ...

  9. ElasticSearch学习-centos下安装

    1.安装java运行环境(jre) //这里我安装了jdk 其实只需要安装jre就可以了 0)cd /usr;mkdir /usr/java; cd java 1)wget http://downlo ...

  10. Web前端学习路线

    第一阶段: HTML+CSS:HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础:Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMAsc ...