自己写的一段:

//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. AngularJs自定义指令详解(6) - controller、require

    在前面文章中提到一旦声明了require,则链接函数具有第四个参数:controller. 可见require和controller是配合使用的. 在自定义指令中使用controller,目的往往是要 ...

  2. Chrome 插件集推荐

    在前端这个行业里面,浏览器担任着及其重要的角色.今天我们可以选择的浏览器有很多,Chrome,Firefox,IE,Safari… 为了能获得更佳的开发体验,大家更多地选择 Chrome.今天介绍下我 ...

  3. [Gnu]Centos7 解决 gdb 提示 Missing separate debuginfos

    Centos7 上使用gdb: $ gdb php $ run /home/www/2.php 运行完 run,后面跟着很长的提示: …. Missing separate debuginfos, u ...

  4. 危险的“我以为”DDoS&丑陋的现实

    有些话题可能终其一生你也可以假装不知道就好了,比如全球气候在变暖,不过有些话题确是不得不面对的冰冷现实,在互联网日益发达的当下,比如你不得不面对的DDoS攻击. DDoS代表了分布式拒绝服务,通过许多 ...

  5. [06]APUE:系统数据文件和信息

    [a] getpwent / setpwent / endpwent #include <pwd.h> struct passwd *getpwent(void) //成功返回指针,出错或 ...

  6. PHP入门教程-开发环境搭建

    1.PHP简介: PHP是能让你生成动态网页的工具之一.PHP网页文件被当作一般HTML网页文件来处理并且在编辑时你可以用编辑HTML的常规方法编写PHP. 2.学习需要基础: a.HTML b.Ja ...

  7. webservice jsonp格式调用

    前端  $.ajax({            type: "get",            url: "http://baiduzd.yihu.com.cn/APIS ...

  8. 王爽-汇编语言-综合研究四-不使用main函数编程

    (一) 研究目的 使用C语言编程,我们一定要使用main函数么? (二) 研究过程 1) 最初的程序 首先,我们编写一个不写main函数的C语言程序. 程序如下: 在编译的过程中,没有发现错误.在链接 ...

  9. Linux Oracle删除归档日志

    今天遇到Oracle报这样的错:ORA-00257 查看了下,原来是Oracle的归档日志满了,解决方案两个 一:增加归档日志大小 二:删除无用的归档日志(我们选择这个方案) 什么也不说了Linux下 ...

  10. JS、C#及SQL中的DateTime

    一:SQL中的DataTime 1.       between and 相当于>= and <= 2.       常用的将DataTime查询成字符串的方法 Select CONVER ...